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. |
| 215 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 216 | EEnd = Proto->exception_end(); |
| 217 | E != EEnd; ++E) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 218 | if (ExceptionsSeen.insert(Self->Context.getCanonicalType(*E))) |
Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 219 | Exceptions.push_back(*E); |
| 220 | } |
| 221 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 222 | void Sema::ImplicitExceptionSpecification::CalledExpr(Expr *E) { |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 223 | if (!E || ComputedEST == EST_MSAny) |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 224 | return; |
| 225 | |
| 226 | // FIXME: |
| 227 | // |
| 228 | // C++0x [except.spec]p14: |
NAKAMURA Takumi | 5364847 | 2011-06-21 03:19:28 +0000 | [diff] [blame] | 229 | // [An] implicit exception-specification specifies the type-id T if and |
| 230 | // only if T is allowed by the exception-specification of a function directly |
| 231 | // 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] | 232 | // function it directly invokes allows all exceptions, and f shall allow no |
| 233 | // exceptions if every function it directly invokes allows no exceptions. |
| 234 | // |
| 235 | // Note in particular that if an implicit exception-specification is generated |
| 236 | // for a function containing a throw-expression, that specification can still |
| 237 | // be noexcept(true). |
| 238 | // |
| 239 | // Note also that 'directly invoked' is not defined in the standard, and there |
| 240 | // is no indication that we should only consider potentially-evaluated calls. |
| 241 | // |
| 242 | // Ultimately we should implement the intent of the standard: the exception |
| 243 | // specification should be the set of exceptions which can be thrown by the |
| 244 | // implicit definition. For now, we assume that any non-nothrow expression can |
| 245 | // throw any exception. |
| 246 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 247 | if (Self->canThrow(E)) |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 248 | ComputedEST = EST_None; |
| 249 | } |
| 250 | |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 251 | bool |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 252 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | SourceLocation EqualLoc) { |
Anders Carlsson | 114056f | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 254 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 255 | diag::err_typecheck_decl_incomplete_type)) { |
| 256 | Param->setInvalidDecl(); |
| 257 | return true; |
| 258 | } |
| 259 | |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 260 | // C++ [dcl.fct.default]p5 |
| 261 | // A default argument expression is implicitly converted (clause |
| 262 | // 4) to the parameter type. The default argument expression has |
| 263 | // the same semantic constraints as the initializer expression in |
| 264 | // a declaration of a variable of the parameter type, using the |
| 265 | // copy-initialization semantics (8.5). |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 266 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 267 | Param); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 268 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 269 | EqualLoc); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 270 | InitializationSequence InitSeq(*this, Entity, Kind, Arg); |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 271 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Arg); |
Eli Friedman | 5f101b9 | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 272 | if (Result.isInvalid()) |
Anders Carlsson | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 273 | return true; |
Eli Friedman | 5f101b9 | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 274 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 275 | |
Richard Smith | c406cb7 | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 276 | CheckCompletedExpr(Arg, EqualLoc); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 277 | Arg = MaybeCreateExprWithCleanups(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 279 | // Okay: add the default argument to the parameter |
| 280 | Param->setDefaultArg(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Douglas Gregor | 758cb67 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 282 | // We have already instantiated this parameter; provide each of the |
| 283 | // instantiations with the uninstantiated default argument. |
| 284 | UnparsedDefaultArgInstantiationsMap::iterator InstPos |
| 285 | = UnparsedDefaultArgInstantiations.find(Param); |
| 286 | if (InstPos != UnparsedDefaultArgInstantiations.end()) { |
| 287 | for (unsigned I = 0, N = InstPos->second.size(); I != N; ++I) |
| 288 | InstPos->second[I]->setUninstantiatedDefaultArg(Arg); |
| 289 | |
| 290 | // We're done tracking this parameter's instantiations. |
| 291 | UnparsedDefaultArgInstantiations.erase(InstPos); |
| 292 | } |
| 293 | |
Anders Carlsson | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 294 | return false; |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 297 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 298 | /// provided for a function parameter is well-formed. If so, attach it |
| 299 | /// to the parameter declaration. |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 300 | void |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 301 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 302 | Expr *DefaultArg) { |
| 303 | if (!param || !DefaultArg) |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 304 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 306 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 307 | UnparsedDefaultArgLocs.erase(Param); |
| 308 | |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 309 | // Default arguments are only permitted in C++ |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 310 | if (!getLangOpts().CPlusPlus) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 311 | Diag(EqualLoc, diag::err_param_default_argument) |
| 312 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 313 | Param->setInvalidDecl(); |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 314 | return; |
| 315 | } |
| 316 | |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 317 | // Check for unexpanded parameter packs. |
| 318 | if (DiagnoseUnexpandedParameterPack(DefaultArg, UPPC_DefaultArgument)) { |
| 319 | Param->setInvalidDecl(); |
| 320 | return; |
| 321 | } |
| 322 | |
Anders Carlsson | f1c2695 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 323 | // Check that the default argument is well-formed |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 324 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 325 | if (DefaultArgChecker.Visit(DefaultArg)) { |
Anders Carlsson | f1c2695 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 326 | Param->setInvalidDecl(); |
| 327 | return; |
| 328 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 330 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 333 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 334 | /// argument for a function parameter, but we can't parse it yet |
| 335 | /// because we're inside a class definition. Note that this default |
| 336 | /// argument will be parsed later. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 337 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 338 | SourceLocation EqualLoc, |
| 339 | SourceLocation ArgLoc) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 340 | if (!param) |
| 341 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 343 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Nick Lewycky | 0f29289 | 2013-09-22 10:06:57 +0000 | [diff] [blame] | 344 | Param->setUnparsedDefaultArg(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 345 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 348 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 349 | /// the default argument for the parameter param failed. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 350 | void Sema::ActOnParamDefaultArgumentError(Decl *param) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 351 | if (!param) |
| 352 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 354 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 355 | Param->setInvalidDecl(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 356 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 359 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 360 | /// arguments in the declarator, which is not a function declaration |
| 361 | /// or definition and therefore is not permitted to have default |
| 362 | /// arguments. This routine should be invoked for every declarator |
| 363 | /// that is not a function declaration or definition. |
| 364 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 365 | // C++ [dcl.fct.default]p3 |
| 366 | // A default argument expression shall be specified only in the |
| 367 | // parameter-declaration-clause of a function declaration or in a |
| 368 | // template-parameter (14.1). It shall not be specified for a |
| 369 | // parameter pack. If it is specified in a |
| 370 | // parameter-declaration-clause, it shall not occur within a |
| 371 | // declarator or abstract-declarator of a parameter-declaration. |
Richard Smith | 5afcdf3f | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 372 | bool MightBeFunction = D.isFunctionDeclarationContext(); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 373 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 374 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 375 | if (chunk.Kind == DeclaratorChunk::Function) { |
Richard Smith | 5afcdf3f | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 376 | if (MightBeFunction) { |
| 377 | // This is a function declaration. It can have default arguments, but |
| 378 | // keep looking in case its return type is a function type with default |
| 379 | // arguments. |
| 380 | MightBeFunction = false; |
| 381 | continue; |
| 382 | } |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 383 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 384 | ParmVarDecl *Param = |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 385 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param); |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 386 | if (Param->hasUnparsedDefaultArg()) { |
| 387 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 388 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
Richard Smith | 5afcdf3f | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 389 | << SourceRange((*Toks)[1].getLocation(), |
| 390 | Toks->back().getLocation()); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 391 | delete Toks; |
| 392 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 393 | } else if (Param->getDefaultArg()) { |
| 394 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 395 | << Param->getDefaultArg()->getSourceRange(); |
| 396 | Param->setDefaultArg(0); |
Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
Richard Smith | 5afcdf3f | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 399 | } else if (chunk.Kind != DeclaratorChunk::Paren) { |
| 400 | MightBeFunction = false; |
Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
David Majnemer | 502b0ed | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 405 | static bool functionDeclHasDefaultArgument(const FunctionDecl *FD) { |
| 406 | for (unsigned NumParams = FD->getNumParams(); NumParams > 0; --NumParams) { |
| 407 | const ParmVarDecl *PVD = FD->getParamDecl(NumParams-1); |
| 408 | if (!PVD->hasDefaultArg()) |
| 409 | return false; |
| 410 | if (!PVD->hasInheritedDefaultArg()) |
| 411 | return true; |
| 412 | } |
| 413 | return false; |
| 414 | } |
| 415 | |
Craig Topper | e479428 | 2012-09-21 04:33:26 +0000 | [diff] [blame] | 416 | /// MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 417 | /// function, once we already know that they have the same |
| 418 | /// type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 419 | /// error, false otherwise. |
James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 420 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old, |
| 421 | Scope *S) { |
Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 422 | bool Invalid = false; |
| 423 | |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 424 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 425 | // For non-template functions, default arguments can be added in |
| 426 | // later declarations of a function in the same |
| 427 | // scope. Declarations in different scopes have completely |
| 428 | // distinct sets of default arguments. That is, declarations in |
| 429 | // inner scopes do not acquire default arguments from |
| 430 | // declarations in outer scopes, and vice versa. In a given |
| 431 | // function declaration, all parameters subsequent to a |
| 432 | // parameter with a default argument shall have default |
| 433 | // arguments supplied in this or previous declarations. A |
| 434 | // default argument shall not be redefined by a later |
| 435 | // declaration (not even to the same value). |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 436 | // |
| 437 | // C++ [dcl.fct.default]p6: |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 438 | // Except for member functions of class templates, the default arguments |
| 439 | // in a member function definition that appears outside of the class |
| 440 | // definition are added to the set of default arguments provided by the |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 441 | // member function declaration in the class definition. |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 442 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 443 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 444 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 445 | |
James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 446 | bool OldParamHasDfl = OldParam->hasDefaultArg(); |
| 447 | bool NewParamHasDfl = NewParam->hasDefaultArg(); |
| 448 | |
| 449 | NamedDecl *ND = Old; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 450 | |
| 451 | // The declaration context corresponding to the scope is the semantic |
| 452 | // parent, unless this is a local function declaration, in which case |
| 453 | // it is that surrounding function. |
| 454 | DeclContext *ScopeDC = New->getLexicalDeclContext(); |
| 455 | if (!ScopeDC->isFunctionOrMethod()) |
| 456 | ScopeDC = New->getDeclContext(); |
| 457 | if (S && !isDeclInScope(ND, ScopeDC, S) && |
| 458 | !New->getDeclContext()->isRecord()) |
James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 459 | // Ignore default parameters of old decl if they are not in |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 460 | // the same scope and this is not an out-of-line definition of |
| 461 | // a member function. |
James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 462 | OldParamHasDfl = false; |
| 463 | |
| 464 | if (OldParamHasDfl && NewParamHasDfl) { |
Francois Pichet | 8cb243a | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 465 | |
Francois Pichet | 53fe2bb | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 466 | unsigned DiagDefaultParamID = |
| 467 | diag::err_param_default_argument_redefinition; |
| 468 | |
| 469 | // MSVC accepts that default parameters be redefined for member functions |
| 470 | // of template class. The new default parameter's value is ignored. |
| 471 | Invalid = true; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 472 | if (getLangOpts().MicrosoftExt) { |
Francois Pichet | 53fe2bb | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 473 | CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(New); |
| 474 | if (MD && MD->getParent()->getDescribedClassTemplate()) { |
Francois Pichet | 8cb243a | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 475 | // Merge the old default argument into the new parameter. |
| 476 | NewParam->setHasInheritedDefaultArg(); |
| 477 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 478 | NewParam->setUninstantiatedDefaultArg( |
| 479 | OldParam->getUninstantiatedDefaultArg()); |
| 480 | else |
| 481 | NewParam->setDefaultArg(OldParam->getInit()); |
Francois Pichet | 9392165 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 482 | DiagDefaultParamID = diag::warn_param_default_argument_redefinition; |
Francois Pichet | 53fe2bb | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 483 | Invalid = false; |
| 484 | } |
| 485 | } |
Douglas Gregor | 08dc584 | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 486 | |
Francois Pichet | 8cb243a | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 487 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 488 | // hint here. Alternatively, we could walk the type-source information |
| 489 | // for NewParam to find the last source location in the type... but it |
| 490 | // isn't worth the effort right now. This is the kind of test case that |
| 491 | // is hard to get right: |
Douglas Gregor | 08dc584 | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 492 | // int f(int); |
| 493 | // void g(int (*fp)(int) = f); |
| 494 | // void g(int (*fp)(int) = &f); |
Francois Pichet | 53fe2bb | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 495 | Diag(NewParam->getLocation(), DiagDefaultParamID) |
Douglas Gregor | 08dc584 | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 496 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 497 | |
| 498 | // Look for the function declaration where the default argument was |
| 499 | // actually written, which may be a declaration prior to Old. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 500 | for (FunctionDecl *Older = Old->getPreviousDecl(); |
| 501 | Older; Older = Older->getPreviousDecl()) { |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 502 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 503 | break; |
| 504 | |
| 505 | OldParam = Older->getParamDecl(p); |
| 506 | } |
| 507 | |
| 508 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 509 | << OldParam->getDefaultArgRange(); |
James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 510 | } else if (OldParamHasDfl) { |
John McCall | e61b02b | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 511 | // Merge the old default argument into the new parameter. |
| 512 | // It's important to use getInit() here; getDefaultArg() |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 513 | // strips off any top-level ExprWithCleanups. |
John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 514 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | 4f15f4d | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 515 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 516 | NewParam->setUninstantiatedDefaultArg( |
| 517 | OldParam->getUninstantiatedDefaultArg()); |
| 518 | else |
John McCall | e61b02b | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 519 | NewParam->setDefaultArg(OldParam->getInit()); |
James Molloy | e943003 | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 520 | } else if (NewParamHasDfl) { |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 521 | if (New->getDescribedFunctionTemplate()) { |
| 522 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 523 | Diag(NewParam->getLocation(), |
| 524 | diag::err_param_default_argument_template_redecl) |
| 525 | << NewParam->getDefaultArgRange(); |
| 526 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 527 | << false; |
Douglas Gregor | 62e10f0 | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 528 | } else if (New->getTemplateSpecializationKind() |
| 529 | != TSK_ImplicitInstantiation && |
| 530 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 531 | // C++ [temp.expr.spec]p21: |
| 532 | // Default function arguments shall not be specified in a declaration |
| 533 | // or a definition for one of the following explicit specializations: |
| 534 | // - the explicit specialization of a function template; |
Douglas Gregor | 3362bde | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 535 | // - the explicit specialization of a member function template; |
| 536 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 62e10f0 | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 537 | // template where the class template specialization to which the |
| 538 | // member function specialization belongs is implicitly |
| 539 | // instantiated. |
| 540 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 541 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 542 | << New->getDeclName() |
| 543 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 544 | } else if (New->getDeclContext()->isDependentContext()) { |
| 545 | // C++ [dcl.fct.default]p6 (DR217): |
| 546 | // Default arguments for a member function of a class template shall |
| 547 | // be specified on the initial declaration of the member function |
| 548 | // within the class template. |
| 549 | // |
| 550 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 551 | // leads me to the conclusion that one cannot add default function |
| 552 | // arguments for an out-of-line definition of a member function of a |
| 553 | // dependent type. |
| 554 | int WhichKind = 2; |
| 555 | if (CXXRecordDecl *Record |
| 556 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 557 | if (Record->getDescribedClassTemplate()) |
| 558 | WhichKind = 0; |
| 559 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 560 | WhichKind = 1; |
| 561 | else |
| 562 | WhichKind = 2; |
| 563 | } |
| 564 | |
| 565 | Diag(NewParam->getLocation(), |
| 566 | diag::err_param_default_argument_member_template_redecl) |
| 567 | << WhichKind |
| 568 | << NewParam->getDefaultArgRange(); |
| 569 | } |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
Richard Smith | 58c3cc1 | 2012-11-28 03:45:24 +0000 | [diff] [blame] | 573 | // DR1344: If a default argument is added outside a class definition and that |
| 574 | // default argument makes the function a special member function, the program |
| 575 | // is ill-formed. This can only happen for constructors. |
| 576 | if (isa<CXXConstructorDecl>(New) && |
| 577 | New->getMinRequiredArguments() < Old->getMinRequiredArguments()) { |
| 578 | CXXSpecialMember NewSM = getSpecialMember(cast<CXXMethodDecl>(New)), |
| 579 | OldSM = getSpecialMember(cast<CXXMethodDecl>(Old)); |
| 580 | if (NewSM != OldSM) { |
| 581 | ParmVarDecl *NewParam = New->getParamDecl(New->getMinRequiredArguments()); |
| 582 | assert(NewParam->hasDefaultArg()); |
| 583 | Diag(NewParam->getLocation(), diag::err_default_arg_makes_ctor_special) |
| 584 | << NewParam->getDefaultArgRange() << NewSM; |
| 585 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 586 | } |
| 587 | } |
| 588 | |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 589 | // 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] | 590 | // template has a constexpr specifier then all its declarations shall |
Richard Smith | 5b8b3db | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 591 | // contain the constexpr specifier. |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 592 | if (New->isConstexpr() != Old->isConstexpr()) { |
| 593 | Diag(New->getLocation(), diag::err_constexpr_redecl_mismatch) |
| 594 | << New << New->isConstexpr(); |
| 595 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 596 | Invalid = true; |
| 597 | } |
| 598 | |
David Majnemer | 502b0ed | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 599 | // 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] | 600 | // argument expression, that declaration shall be a definition and shall be |
David Majnemer | 502b0ed | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 601 | // the only declaration of the function or function template in the |
| 602 | // translation unit. |
| 603 | if (Old->getFriendObjectKind() == Decl::FOK_Undeclared && |
| 604 | functionDeclHasDefaultArgument(Old)) { |
| 605 | Diag(New->getLocation(), diag::err_friend_decl_with_def_arg_redeclared); |
| 606 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 607 | Invalid = true; |
| 608 | } |
| 609 | |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 610 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4f4d7b5 | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 611 | Invalid = true; |
Sebastian Redl | 4f4d7b5 | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 612 | |
Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 613 | return Invalid; |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 616 | /// \brief Merge the exception specifications of two variable declarations. |
| 617 | /// |
| 618 | /// This is called when there's a redeclaration of a VarDecl. The function |
| 619 | /// checks if the redeclaration might have an exception specification and |
| 620 | /// validates compatibility and merges the specs if necessary. |
| 621 | void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) { |
| 622 | // Shortcut if exceptions are disabled. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 623 | if (!getLangOpts().CXXExceptions) |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 624 | return; |
| 625 | |
| 626 | assert(Context.hasSameType(New->getType(), Old->getType()) && |
| 627 | "Should only be called if types are otherwise the same."); |
| 628 | |
| 629 | QualType NewType = New->getType(); |
| 630 | QualType OldType = Old->getType(); |
| 631 | |
| 632 | // We're only interested in pointers and references to functions, as well |
| 633 | // as pointers to member functions. |
| 634 | if (const ReferenceType *R = NewType->getAs<ReferenceType>()) { |
| 635 | NewType = R->getPointeeType(); |
| 636 | OldType = OldType->getAs<ReferenceType>()->getPointeeType(); |
| 637 | } else if (const PointerType *P = NewType->getAs<PointerType>()) { |
| 638 | NewType = P->getPointeeType(); |
| 639 | OldType = OldType->getAs<PointerType>()->getPointeeType(); |
| 640 | } else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) { |
| 641 | NewType = M->getPointeeType(); |
| 642 | OldType = OldType->getAs<MemberPointerType>()->getPointeeType(); |
| 643 | } |
| 644 | |
| 645 | if (!NewType->isFunctionProtoType()) |
| 646 | return; |
| 647 | |
| 648 | // There's lots of special cases for functions. For function pointers, system |
| 649 | // libraries are hopefully not as broken so that we don't need these |
| 650 | // workarounds. |
| 651 | if (CheckEquivalentExceptionSpec( |
| 652 | OldType->getAs<FunctionProtoType>(), Old->getLocation(), |
| 653 | NewType->getAs<FunctionProtoType>(), New->getLocation())) { |
| 654 | New->setInvalidDecl(); |
| 655 | } |
| 656 | } |
| 657 | |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 658 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 659 | /// function declaration are well-formed according to C++ |
| 660 | /// [dcl.fct.default]. |
| 661 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 662 | unsigned NumParams = FD->getNumParams(); |
| 663 | unsigned p; |
| 664 | |
| 665 | // Find first parameter with a default argument |
| 666 | for (p = 0; p < NumParams; ++p) { |
| 667 | ParmVarDecl *Param = FD->getParamDecl(p); |
Richard Smith | 3cb4c63 | 2013-04-17 16:25:20 +0000 | [diff] [blame] | 668 | if (Param->hasDefaultArg()) |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 669 | break; |
| 670 | } |
| 671 | |
| 672 | // C++ [dcl.fct.default]p4: |
| 673 | // In a given function declaration, all parameters |
| 674 | // subsequent to a parameter with a default argument shall |
| 675 | // have default arguments supplied in this or previous |
| 676 | // declarations. A default argument shall not be redefined |
| 677 | // by a later declaration (not even to the same value). |
| 678 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | for (; p < NumParams; ++p) { |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 680 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5a53238 | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 681 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 682 | if (Param->isInvalidDecl()) |
| 683 | /* We already complained about this parameter. */; |
| 684 | else if (Param->getIdentifier()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | Diag(Param->getLocation(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 686 | diag::err_param_default_argument_missing_name) |
Chris Lattner | b91fd17 | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 687 | << Param->getIdentifier(); |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 688 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | Diag(Param->getLocation(), |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 690 | diag::err_param_default_argument_missing); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 692 | LastMissingDefaultArg = p; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | if (LastMissingDefaultArg > 0) { |
| 697 | // Some default arguments were missing. Clear out all of the |
| 698 | // default arguments up to (and including) the last missing |
| 699 | // default argument, so that we leave the function parameters |
| 700 | // in a semantically valid state. |
| 701 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 702 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 703 | if (Param->hasDefaultArg()) { |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 704 | Param->setDefaultArg(0); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 709 | |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 710 | // CheckConstexprParameterTypes - Check whether a function's parameter types |
| 711 | // 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] | 712 | // diagnostic and return false. |
| 713 | static bool CheckConstexprParameterTypes(Sema &SemaRef, |
| 714 | const FunctionDecl *FD) { |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 715 | unsigned ArgIndex = 0; |
| 716 | const FunctionProtoType *FT = FD->getType()->getAs<FunctionProtoType>(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 717 | for (FunctionProtoType::param_type_iterator i = FT->param_type_begin(), |
| 718 | e = FT->param_type_end(); |
| 719 | i != e; ++i, ++ArgIndex) { |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 720 | const ParmVarDecl *PD = FD->getParamDecl(ArgIndex); |
| 721 | SourceLocation ParamLoc = PD->getLocation(); |
| 722 | if (!(*i)->isDependentType() && |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 723 | SemaRef.RequireLiteralType(ParamLoc, *i, |
Douglas Gregor | a6c5abb | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 724 | diag::err_constexpr_non_literal_param, |
| 725 | ArgIndex+1, PD->getSourceRange(), |
| 726 | isa<CXXConstructorDecl>(FD))) |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 727 | return false; |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 728 | } |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 729 | return true; |
| 730 | } |
| 731 | |
| 732 | /// \brief Get diagnostic %select index for tag kind for |
| 733 | /// record diagnostic message. |
| 734 | /// WARNING: Indexes apply to particular diagnostics only! |
| 735 | /// |
| 736 | /// \returns diagnostic %select index. |
Joao Matos | a5c42e9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 737 | static unsigned getRecordDiagFromTagKind(TagTypeKind Tag) { |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 738 | switch (Tag) { |
Joao Matos | a5c42e9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 739 | case TTK_Struct: return 0; |
| 740 | case TTK_Interface: return 1; |
| 741 | case TTK_Class: return 2; |
| 742 | default: llvm_unreachable("Invalid tag kind for record diagnostic!"); |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 743 | } |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | // CheckConstexprFunctionDecl - Check whether a function declaration satisfies |
| 747 | // the requirements of a constexpr function definition or a constexpr |
| 748 | // constructor definition. If so, return true. If not, produce appropriate |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 749 | // diagnostics and return false. |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 750 | // |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 751 | // This implements C++11 [dcl.constexpr]p3,4, as amended by DR1360. |
| 752 | bool Sema::CheckConstexprFunctionDecl(const FunctionDecl *NewFD) { |
Richard Smith | 7971b69 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 753 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| 754 | if (MD && MD->isInstance()) { |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 755 | // C++11 [dcl.constexpr]p4: |
| 756 | // The definition of a constexpr constructor shall satisfy the following |
| 757 | // constraints: |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 758 | // - the class shall not have any virtual base classes; |
Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 759 | const CXXRecordDecl *RD = MD->getParent(); |
| 760 | if (RD->getNumVBases()) { |
| 761 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual_base) |
| 762 | << isa<CXXConstructorDecl>(NewFD) |
| 763 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases(); |
| 764 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 765 | E = RD->vbases_end(); I != E; ++I) |
| 766 | Diag(I->getLocStart(), |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 767 | diag::note_constexpr_virtual_base_here) << I->getSourceRange(); |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 768 | return false; |
| 769 | } |
Richard Smith | 7971b69 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | if (!isa<CXXConstructorDecl>(NewFD)) { |
| 773 | // C++11 [dcl.constexpr]p3: |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 774 | // The definition of a constexpr function shall satisfy the following |
| 775 | // constraints: |
| 776 | // - it shall not be virtual; |
| 777 | const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD); |
| 778 | if (Method && Method->isVirtual()) { |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 779 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual); |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 780 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 781 | // If it's not obvious why this function is virtual, find an overridden |
| 782 | // function which uses the 'virtual' keyword. |
| 783 | const CXXMethodDecl *WrittenVirtual = Method; |
| 784 | while (!WrittenVirtual->isVirtualAsWritten()) |
| 785 | WrittenVirtual = *WrittenVirtual->begin_overridden_methods(); |
| 786 | if (WrittenVirtual != Method) |
| 787 | Diag(WrittenVirtual->getLocation(), |
| 788 | diag::note_overridden_virtual_function); |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 789 | return false; |
| 790 | } |
| 791 | |
| 792 | // - its return type shall be a literal type; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 793 | QualType RT = NewFD->getReturnType(); |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 794 | if (!RT->isDependentType() && |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 795 | RequireLiteralType(NewFD->getLocation(), RT, |
Douglas Gregor | a6c5abb | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 796 | diag::err_constexpr_non_literal_return)) |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 797 | return false; |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Richard Smith | 7971b69 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 800 | // - each of its parameter types shall be a literal type; |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 801 | if (!CheckConstexprParameterTypes(*this, NewFD)) |
Richard Smith | 7971b69 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 802 | return false; |
| 803 | |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 804 | return true; |
| 805 | } |
| 806 | |
| 807 | /// Check the given declaration statement is legal within a constexpr function |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 808 | /// 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] | 809 | /// |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 810 | /// \return true if the body is OK (maybe only as an extension), false if we |
| 811 | /// have diagnosed a problem. |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 812 | static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl, |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 813 | DeclStmt *DS, SourceLocation &Cxx1yLoc) { |
| 814 | // C++11 [dcl.constexpr]p3 and p4: |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 815 | // The definition of a constexpr function(p3) or constructor(p4) [...] shall |
| 816 | // contain only |
| 817 | for (DeclStmt::decl_iterator DclIt = DS->decl_begin(), |
| 818 | DclEnd = DS->decl_end(); DclIt != DclEnd; ++DclIt) { |
| 819 | switch ((*DclIt)->getKind()) { |
| 820 | case Decl::StaticAssert: |
| 821 | case Decl::Using: |
| 822 | case Decl::UsingShadow: |
| 823 | case Decl::UsingDirective: |
| 824 | case Decl::UnresolvedUsingTypename: |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 825 | case Decl::UnresolvedUsingValue: |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 826 | // - static_assert-declarations |
| 827 | // - using-declarations, |
| 828 | // - using-directives, |
| 829 | continue; |
| 830 | |
| 831 | case Decl::Typedef: |
| 832 | case Decl::TypeAlias: { |
| 833 | // - typedef declarations and alias-declarations that do not define |
| 834 | // classes or enumerations, |
| 835 | TypedefNameDecl *TN = cast<TypedefNameDecl>(*DclIt); |
| 836 | if (TN->getUnderlyingType()->isVariablyModifiedType()) { |
| 837 | // Don't allow variably-modified types in constexpr functions. |
| 838 | TypeLoc TL = TN->getTypeSourceInfo()->getTypeLoc(); |
| 839 | SemaRef.Diag(TL.getBeginLoc(), diag::err_constexpr_vla) |
| 840 | << TL.getSourceRange() << TL.getType() |
| 841 | << isa<CXXConstructorDecl>(Dcl); |
| 842 | return false; |
| 843 | } |
| 844 | continue; |
| 845 | } |
| 846 | |
| 847 | case Decl::Enum: |
| 848 | case Decl::CXXRecord: |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 849 | // C++1y allows types to be defined, not just declared. |
| 850 | if (cast<TagDecl>(*DclIt)->isThisDeclarationADefinition()) |
| 851 | SemaRef.Diag(DS->getLocStart(), |
| 852 | SemaRef.getLangOpts().CPlusPlus1y |
| 853 | ? diag::warn_cxx11_compat_constexpr_type_definition |
| 854 | : diag::ext_constexpr_type_definition) |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 855 | << isa<CXXConstructorDecl>(Dcl); |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 856 | continue; |
| 857 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 858 | case Decl::EnumConstant: |
| 859 | case Decl::IndirectField: |
| 860 | case Decl::ParmVar: |
| 861 | // These can only appear with other declarations which are banned in |
| 862 | // C++11 and permitted in C++1y, so ignore them. |
| 863 | continue; |
| 864 | |
| 865 | case Decl::Var: { |
| 866 | // C++1y [dcl.constexpr]p3 allows anything except: |
| 867 | // a definition of a variable of non-literal type or of static or |
| 868 | // thread storage duration or for which no initialization is performed. |
| 869 | VarDecl *VD = cast<VarDecl>(*DclIt); |
| 870 | if (VD->isThisDeclarationADefinition()) { |
| 871 | if (VD->isStaticLocal()) { |
| 872 | SemaRef.Diag(VD->getLocation(), |
| 873 | diag::err_constexpr_local_var_static) |
| 874 | << isa<CXXConstructorDecl>(Dcl) |
| 875 | << (VD->getTLSKind() == VarDecl::TLS_Dynamic); |
| 876 | return false; |
| 877 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 878 | if (!VD->getType()->isDependentType() && |
| 879 | SemaRef.RequireLiteralType( |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 880 | VD->getLocation(), VD->getType(), |
| 881 | diag::err_constexpr_local_var_non_literal_type, |
| 882 | isa<CXXConstructorDecl>(Dcl))) |
| 883 | return false; |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 884 | if (!VD->getType()->isDependentType() && |
| 885 | !VD->hasInit() && !VD->isCXXForRangeDecl()) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 886 | SemaRef.Diag(VD->getLocation(), |
| 887 | diag::err_constexpr_local_var_no_init) |
| 888 | << isa<CXXConstructorDecl>(Dcl); |
| 889 | return false; |
| 890 | } |
| 891 | } |
| 892 | SemaRef.Diag(VD->getLocation(), |
| 893 | SemaRef.getLangOpts().CPlusPlus1y |
| 894 | ? diag::warn_cxx11_compat_constexpr_local_var |
| 895 | : diag::ext_constexpr_local_var) |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 896 | << isa<CXXConstructorDecl>(Dcl); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 897 | continue; |
| 898 | } |
| 899 | |
| 900 | case Decl::NamespaceAlias: |
| 901 | case Decl::Function: |
| 902 | // These are disallowed in C++11 and permitted in C++1y. Allow them |
| 903 | // everywhere as an extension. |
| 904 | if (!Cxx1yLoc.isValid()) |
| 905 | Cxx1yLoc = DS->getLocStart(); |
| 906 | continue; |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 907 | |
| 908 | default: |
| 909 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 910 | << isa<CXXConstructorDecl>(Dcl); |
| 911 | return false; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | return true; |
| 916 | } |
| 917 | |
| 918 | /// Check that the given field is initialized within a constexpr constructor. |
| 919 | /// |
| 920 | /// \param Dcl The constexpr constructor being checked. |
| 921 | /// \param Field The field being checked. This may be a member of an anonymous |
| 922 | /// struct or union nested within the class being checked. |
| 923 | /// \param Inits All declarations, including anonymous struct/union members and |
| 924 | /// indirect members, for which any initialization was provided. |
| 925 | /// \param Diagnosed Set to true if an error is produced. |
| 926 | static void CheckConstexprCtorInitializer(Sema &SemaRef, |
| 927 | const FunctionDecl *Dcl, |
| 928 | FieldDecl *Field, |
| 929 | llvm::SmallSet<Decl*, 16> &Inits, |
| 930 | bool &Diagnosed) { |
Eli Friedman | b13e64e | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 931 | if (Field->isInvalidDecl()) |
| 932 | return; |
| 933 | |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 934 | if (Field->isUnnamedBitfield()) |
| 935 | return; |
Richard Smith | 4d59eeb | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 936 | |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 937 | // Anonymous unions with no variant members and empty anonymous structs do not |
| 938 | // need to be explicitly initialized. FIXME: Anonymous structs that contain no |
| 939 | // indirect fields don't need initializing. |
Richard Smith | 4d59eeb | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 940 | if (Field->isAnonymousStructOrUnion() && |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 941 | (Field->getType()->isUnionType() |
| 942 | ? !Field->getType()->getAsCXXRecordDecl()->hasVariantMembers() |
| 943 | : Field->getType()->getAsCXXRecordDecl()->isEmpty())) |
Richard Smith | 4d59eeb | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 944 | return; |
| 945 | |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 946 | if (!Inits.count(Field)) { |
| 947 | if (!Diagnosed) { |
| 948 | SemaRef.Diag(Dcl->getLocation(), diag::err_constexpr_ctor_missing_init); |
| 949 | Diagnosed = true; |
| 950 | } |
| 951 | SemaRef.Diag(Field->getLocation(), diag::note_constexpr_ctor_missing_init); |
| 952 | } else if (Field->isAnonymousStructOrUnion()) { |
| 953 | const RecordDecl *RD = Field->getType()->castAs<RecordType>()->getDecl(); |
| 954 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 955 | I != E; ++I) |
| 956 | // If an anonymous union contains an anonymous struct of which any member |
| 957 | // is initialized, all members must be initialized. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 958 | if (!RD->isUnion() || Inits.count(*I)) |
| 959 | CheckConstexprCtorInitializer(SemaRef, Dcl, *I, Inits, Diagnosed); |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 963 | /// Check the provided statement is allowed in a constexpr function |
| 964 | /// definition. |
| 965 | static bool |
| 966 | CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S, |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 967 | SmallVectorImpl<SourceLocation> &ReturnStmts, |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 968 | SourceLocation &Cxx1yLoc) { |
| 969 | // - its function-body shall be [...] a compound-statement that contains only |
| 970 | switch (S->getStmtClass()) { |
| 971 | case Stmt::NullStmtClass: |
| 972 | // - null statements, |
| 973 | return true; |
| 974 | |
| 975 | case Stmt::DeclStmtClass: |
| 976 | // - static_assert-declarations |
| 977 | // - using-declarations, |
| 978 | // - using-directives, |
| 979 | // - typedef declarations and alias-declarations that do not define |
| 980 | // classes or enumerations, |
| 981 | if (!CheckConstexprDeclStmt(SemaRef, Dcl, cast<DeclStmt>(S), Cxx1yLoc)) |
| 982 | return false; |
| 983 | return true; |
| 984 | |
| 985 | case Stmt::ReturnStmtClass: |
| 986 | // - and exactly one return statement; |
| 987 | if (isa<CXXConstructorDecl>(Dcl)) { |
| 988 | // C++1y allows return statements in constexpr constructors. |
| 989 | if (!Cxx1yLoc.isValid()) |
| 990 | Cxx1yLoc = S->getLocStart(); |
| 991 | return true; |
| 992 | } |
| 993 | |
| 994 | ReturnStmts.push_back(S->getLocStart()); |
| 995 | return true; |
| 996 | |
| 997 | case Stmt::CompoundStmtClass: { |
| 998 | // C++1y allows compound-statements. |
| 999 | if (!Cxx1yLoc.isValid()) |
| 1000 | Cxx1yLoc = S->getLocStart(); |
| 1001 | |
| 1002 | CompoundStmt *CompStmt = cast<CompoundStmt>(S); |
| 1003 | for (CompoundStmt::body_iterator BodyIt = CompStmt->body_begin(), |
| 1004 | BodyEnd = CompStmt->body_end(); BodyIt != BodyEnd; ++BodyIt) { |
| 1005 | if (!CheckConstexprFunctionStmt(SemaRef, Dcl, *BodyIt, ReturnStmts, |
| 1006 | Cxx1yLoc)) |
| 1007 | return false; |
| 1008 | } |
| 1009 | return true; |
| 1010 | } |
| 1011 | |
| 1012 | case Stmt::AttributedStmtClass: |
| 1013 | if (!Cxx1yLoc.isValid()) |
| 1014 | Cxx1yLoc = S->getLocStart(); |
| 1015 | return true; |
| 1016 | |
| 1017 | case Stmt::IfStmtClass: { |
| 1018 | // C++1y allows if-statements. |
| 1019 | if (!Cxx1yLoc.isValid()) |
| 1020 | Cxx1yLoc = S->getLocStart(); |
| 1021 | |
| 1022 | IfStmt *If = cast<IfStmt>(S); |
| 1023 | if (!CheckConstexprFunctionStmt(SemaRef, Dcl, If->getThen(), ReturnStmts, |
| 1024 | Cxx1yLoc)) |
| 1025 | return false; |
| 1026 | if (If->getElse() && |
| 1027 | !CheckConstexprFunctionStmt(SemaRef, Dcl, If->getElse(), ReturnStmts, |
| 1028 | Cxx1yLoc)) |
| 1029 | return false; |
| 1030 | return true; |
| 1031 | } |
| 1032 | |
| 1033 | case Stmt::WhileStmtClass: |
| 1034 | case Stmt::DoStmtClass: |
| 1035 | case Stmt::ForStmtClass: |
| 1036 | case Stmt::CXXForRangeStmtClass: |
| 1037 | case Stmt::ContinueStmtClass: |
| 1038 | // C++1y allows all of these. We don't allow them as extensions in C++11, |
| 1039 | // because they don't make sense without variable mutation. |
| 1040 | if (!SemaRef.getLangOpts().CPlusPlus1y) |
| 1041 | break; |
| 1042 | if (!Cxx1yLoc.isValid()) |
| 1043 | Cxx1yLoc = S->getLocStart(); |
| 1044 | for (Stmt::child_range Children = S->children(); Children; ++Children) |
| 1045 | if (*Children && |
| 1046 | !CheckConstexprFunctionStmt(SemaRef, Dcl, *Children, ReturnStmts, |
| 1047 | Cxx1yLoc)) |
| 1048 | return false; |
| 1049 | return true; |
| 1050 | |
| 1051 | case Stmt::SwitchStmtClass: |
| 1052 | case Stmt::CaseStmtClass: |
| 1053 | case Stmt::DefaultStmtClass: |
| 1054 | case Stmt::BreakStmtClass: |
| 1055 | // C++1y allows switch-statements, and since they don't need variable |
| 1056 | // mutation, we can reasonably allow them in C++11 as an extension. |
| 1057 | if (!Cxx1yLoc.isValid()) |
| 1058 | Cxx1yLoc = S->getLocStart(); |
| 1059 | for (Stmt::child_range Children = S->children(); Children; ++Children) |
| 1060 | if (*Children && |
| 1061 | !CheckConstexprFunctionStmt(SemaRef, Dcl, *Children, ReturnStmts, |
| 1062 | Cxx1yLoc)) |
| 1063 | return false; |
| 1064 | return true; |
| 1065 | |
| 1066 | default: |
| 1067 | if (!isa<Expr>(S)) |
| 1068 | break; |
| 1069 | |
| 1070 | // C++1y allows expression-statements. |
| 1071 | if (!Cxx1yLoc.isValid()) |
| 1072 | Cxx1yLoc = S->getLocStart(); |
| 1073 | return true; |
| 1074 | } |
| 1075 | |
| 1076 | SemaRef.Diag(S->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 1077 | << isa<CXXConstructorDecl>(Dcl); |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1081 | /// Check the body for the given constexpr function declaration only contains |
| 1082 | /// the permitted types of statement. C++11 [dcl.constexpr]p3,p4. |
| 1083 | /// |
| 1084 | /// \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] | 1085 | bool Sema::CheckConstexprFunctionBody(const FunctionDecl *Dcl, Stmt *Body) { |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1086 | if (isa<CXXTryStmt>(Body)) { |
Richard Smith | 74388b4 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 1087 | // C++11 [dcl.constexpr]p3: |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1088 | // The definition of a constexpr function shall satisfy the following |
| 1089 | // constraints: [...] |
| 1090 | // - its function-body shall be = delete, = default, or a |
| 1091 | // compound-statement |
| 1092 | // |
Richard Smith | 74388b4 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 1093 | // C++11 [dcl.constexpr]p4: |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1094 | // In the definition of a constexpr constructor, [...] |
| 1095 | // - its function-body shall not be a function-try-block; |
| 1096 | Diag(Body->getLocStart(), diag::err_constexpr_function_try_block) |
| 1097 | << isa<CXXConstructorDecl>(Dcl); |
| 1098 | return false; |
| 1099 | } |
| 1100 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1101 | SmallVector<SourceLocation, 4> ReturnStmts; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1102 | |
| 1103 | // - its function-body shall be [...] a compound-statement that contains only |
| 1104 | // [... list of cases ...] |
| 1105 | CompoundStmt *CompBody = cast<CompoundStmt>(Body); |
| 1106 | SourceLocation Cxx1yLoc; |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1107 | for (CompoundStmt::body_iterator BodyIt = CompBody->body_begin(), |
| 1108 | BodyEnd = CompBody->body_end(); BodyIt != BodyEnd; ++BodyIt) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1109 | if (!CheckConstexprFunctionStmt(*this, Dcl, *BodyIt, ReturnStmts, Cxx1yLoc)) |
| 1110 | return false; |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1113 | if (Cxx1yLoc.isValid()) |
| 1114 | Diag(Cxx1yLoc, |
| 1115 | getLangOpts().CPlusPlus1y |
| 1116 | ? diag::warn_cxx11_compat_constexpr_body_invalid_stmt |
| 1117 | : diag::ext_constexpr_body_invalid_stmt) |
| 1118 | << isa<CXXConstructorDecl>(Dcl); |
| 1119 | |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1120 | if (const CXXConstructorDecl *Constructor |
| 1121 | = dyn_cast<CXXConstructorDecl>(Dcl)) { |
| 1122 | const CXXRecordDecl *RD = Constructor->getParent(); |
Richard Smith | 4d59eeb | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 1123 | // DR1359: |
| 1124 | // - every non-variant non-static data member and base class sub-object |
| 1125 | // shall be initialized; |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 1126 | // DR1460: |
| 1127 | // - 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] | 1128 | // shall be initialized; |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1129 | if (RD->isUnion()) { |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 1130 | if (Constructor->getNumCtorInitializers() == 0 && |
| 1131 | RD->hasVariantMembers()) { |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1132 | Diag(Dcl->getLocation(), diag::err_constexpr_union_ctor_no_init); |
| 1133 | return false; |
| 1134 | } |
Richard Smith | f368fb4 | 2011-10-10 16:38:04 +0000 | [diff] [blame] | 1135 | } else if (!Constructor->isDependentContext() && |
| 1136 | !Constructor->isDelegatingConstructor()) { |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1137 | assert(RD->getNumVBases() == 0 && "constexpr ctor with virtual bases"); |
| 1138 | |
| 1139 | // Skip detailed checking if we have enough initializers, and we would |
| 1140 | // allow at most one initializer per member. |
| 1141 | bool AnyAnonStructUnionMembers = false; |
| 1142 | unsigned Fields = 0; |
| 1143 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 1144 | E = RD->field_end(); I != E; ++I, ++Fields) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1145 | if (I->isAnonymousStructOrUnion()) { |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1146 | AnyAnonStructUnionMembers = true; |
| 1147 | break; |
| 1148 | } |
| 1149 | } |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 1150 | // DR1460: |
| 1151 | // - if the class is a union-like class, but is not a union, for each of |
| 1152 | // its anonymous union members having variant members, exactly one of |
| 1153 | // them shall be initialized; |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1154 | if (AnyAnonStructUnionMembers || |
| 1155 | Constructor->getNumCtorInitializers() != RD->getNumBases() + Fields) { |
| 1156 | // Check initialization of non-static data members. Base classes are |
| 1157 | // always initialized so do not need to be checked. Dependent bases |
| 1158 | // might not have initializers in the member initializer list. |
| 1159 | llvm::SmallSet<Decl*, 16> Inits; |
| 1160 | for (CXXConstructorDecl::init_const_iterator |
| 1161 | I = Constructor->init_begin(), E = Constructor->init_end(); |
| 1162 | I != E; ++I) { |
| 1163 | if (FieldDecl *FD = (*I)->getMember()) |
| 1164 | Inits.insert(FD); |
| 1165 | else if (IndirectFieldDecl *ID = (*I)->getIndirectMember()) |
| 1166 | Inits.insert(ID->chain_begin(), ID->chain_end()); |
| 1167 | } |
| 1168 | |
| 1169 | bool Diagnosed = false; |
| 1170 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 1171 | E = RD->field_end(); I != E; ++I) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1172 | CheckConstexprCtorInitializer(*this, Dcl, *I, Inits, Diagnosed); |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1173 | if (Diagnosed) |
| 1174 | return false; |
| 1175 | } |
| 1176 | } |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1177 | } else { |
| 1178 | if (ReturnStmts.empty()) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1179 | // C++1y doesn't require constexpr functions to contain a 'return' |
| 1180 | // statement. We still do, unless the return type is void, because |
| 1181 | // otherwise if there's no return statement, the function cannot |
| 1182 | // be used in a core constant expression. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1183 | bool OK = getLangOpts().CPlusPlus1y && Dcl->getReturnType()->isVoidType(); |
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) { |
| 1276 | for (CXXRecordDecl::base_class_const_iterator I = Current->bases_begin(), |
| 1277 | E = Current->bases_end(); |
| 1278 | I != E; ++I) { |
| 1279 | CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 1280 | if (!Base) |
| 1281 | continue; |
| 1282 | |
| 1283 | Base = Base->getDefinition(); |
| 1284 | if (!Base) |
| 1285 | continue; |
| 1286 | |
| 1287 | if (Base->getCanonicalDecl() == Class) |
| 1288 | return true; |
| 1289 | |
| 1290 | Queue.push_back(Base); |
| 1291 | } |
| 1292 | |
| 1293 | if (Queue.empty()) |
| 1294 | return false; |
| 1295 | |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 1296 | Current = Queue.pop_back_val(); |
Douglas Gregor | dc97457 | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | return false; |
Douglas Gregor | 6200470 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1302 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1303 | /// |
| 1304 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 1305 | /// and returns NULL otherwise. |
| 1306 | CXXBaseSpecifier * |
| 1307 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 1308 | SourceRange SpecifierRange, |
| 1309 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1310 | TypeSourceInfo *TInfo, |
| 1311 | SourceLocation EllipsisLoc) { |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1312 | QualType BaseType = TInfo->getType(); |
| 1313 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1314 | // C++ [class.union]p1: |
| 1315 | // A union shall not have base classes. |
| 1316 | if (Class->isUnion()) { |
| 1317 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 1318 | << SpecifierRange; |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1322 | if (EllipsisLoc.isValid() && |
| 1323 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 1324 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1325 | << TInfo->getTypeLoc().getSourceRange(); |
| 1326 | EllipsisLoc = SourceLocation(); |
| 1327 | } |
Douglas Gregor | 6200470 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1328 | |
| 1329 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
| 1330 | |
| 1331 | if (BaseType->isDependentType()) { |
| 1332 | // Make sure that we don't have circular inheritance among our dependent |
| 1333 | // bases. For non-dependent bases, the check for completeness below handles |
| 1334 | // this. |
| 1335 | if (CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl()) { |
| 1336 | if (BaseDecl->getCanonicalDecl() == Class->getCanonicalDecl() || |
| 1337 | ((BaseDecl = BaseDecl->getDefinition()) && |
Douglas Gregor | dc97457 | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1338 | findCircularInheritance(Class, BaseDecl))) { |
Douglas Gregor | 6200470 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1339 | Diag(BaseLoc, diag::err_circular_inheritance) |
| 1340 | << BaseType << Context.getTypeDeclType(Class); |
| 1341 | |
| 1342 | if (BaseDecl->getCanonicalDecl() != Class->getCanonicalDecl()) |
| 1343 | Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 1344 | << BaseType; |
| 1345 | |
| 1346 | return 0; |
| 1347 | } |
| 1348 | } |
| 1349 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1351 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1352 | Access, TInfo, EllipsisLoc); |
Douglas Gregor | 6200470 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1353 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1354 | |
| 1355 | // Base specifiers must be record types. |
| 1356 | if (!BaseType->isRecordType()) { |
| 1357 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 1358 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | // C++ [class.union]p1: |
| 1362 | // A union shall not be used as a base class. |
| 1363 | if (BaseType->isUnionType()) { |
| 1364 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | // C++ [class.derived]p2: |
| 1369 | // The class-name in a base-specifier shall not be an incompletely |
| 1370 | // defined class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | if (RequireCompleteType(BaseLoc, BaseType, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1372 | diag::err_incomplete_base_class, SpecifierRange)) { |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1373 | Class->setInvalidDecl(); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1374 | return 0; |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1375 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1376 | |
Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1377 | // 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] | 1378 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1379 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1380 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1381 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
David Majnemer | 626032f | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 1382 | CXXRecordDecl *CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1383 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | 89c038e | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 1384 | |
David Majnemer | 9b1754d | 2013-11-02 12:00:36 +0000 | [diff] [blame] | 1385 | // A class which contains a flexible array member is not suitable for use as a |
| 1386 | // base class: |
| 1387 | // - If the layout determines that a base comes before another base, |
| 1388 | // the flexible array member would index into the subsequent base. |
| 1389 | // - If the layout determines that base comes before the derived class, |
| 1390 | // the flexible array member would index into the derived class. |
| 1391 | if (CXXBaseDecl->hasFlexibleArrayMember()) { |
| 1392 | Diag(BaseLoc, diag::err_base_class_has_flexible_array_member) |
| 1393 | << CXXBaseDecl->getDeclName(); |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
Anders Carlsson | 65c76d3 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 1397 | // C++ [class]p3: |
David Majnemer | 4589760 | 2013-11-02 11:24:41 +0000 | [diff] [blame] | 1398 | // 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] | 1399 | // base-clause, the program is ill-formed. |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1400 | if (FinalAttr *FA = CXXBaseDecl->getAttr<FinalAttr>()) { |
David Majnemer | 4589760 | 2013-11-02 11:24:41 +0000 | [diff] [blame] | 1401 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1402 | << CXXBaseDecl->getDeclName() |
| 1403 | << FA->isSpelledAsSealed(); |
Anders Carlsson | fc1eef4 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 1404 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 1405 | << CXXBaseDecl->getDeclName(); |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1409 | if (BaseDecl->isInvalidDecl()) |
| 1410 | Class->setInvalidDecl(); |
David Majnemer | 4589760 | 2013-11-02 11:24:41 +0000 | [diff] [blame] | 1411 | |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1412 | // Create the base specifier. |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1413 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1414 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1415 | Access, TInfo, EllipsisLoc); |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1418 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 1419 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1420 | /// example: |
| 1421 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1422 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1423 | BaseResult |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1424 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1425 | ParsedAttributes &Attributes, |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1426 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1427 | ParsedType basetype, SourceLocation BaseLoc, |
| 1428 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1429 | if (!classdecl) |
| 1430 | return true; |
| 1431 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1432 | AdjustDeclIfTemplate(classdecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1433 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | beab56e | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 1434 | if (!Class) |
| 1435 | return true; |
| 1436 | |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1437 | // We do not support any C++11 attributes on base-specifiers yet. |
| 1438 | // Diagnose any attributes we see. |
| 1439 | if (!Attributes.empty()) { |
| 1440 | for (AttributeList *Attr = Attributes.getList(); Attr; |
| 1441 | Attr = Attr->getNext()) { |
| 1442 | if (Attr->isInvalid() || |
| 1443 | Attr->getKind() == AttributeList::IgnoredAttribute) |
| 1444 | continue; |
| 1445 | Diag(Attr->getLoc(), |
| 1446 | Attr->getKind() == AttributeList::UnknownAttribute |
| 1447 | ? diag::warn_unknown_attribute_ignored |
| 1448 | : diag::err_base_specifier_attribute) |
| 1449 | << Attr->getName(); |
| 1450 | } |
| 1451 | } |
| 1452 | |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1453 | TypeSourceInfo *TInfo = 0; |
| 1454 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1455 | |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1456 | if (EllipsisLoc.isInvalid() && |
| 1457 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
Douglas Gregor | 506bd56 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1458 | UPPC_BaseType)) |
| 1459 | return true; |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1460 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1461 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1462 | Virtual, Access, TInfo, |
| 1463 | EllipsisLoc)) |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1464 | return BaseSpec; |
Douglas Gregor | b9b8b81 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 1465 | else |
| 1466 | Class->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1468 | return true; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1469 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1470 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1471 | /// \brief Performs the actual work of attaching the given base class |
| 1472 | /// specifiers to a C++ class. |
| 1473 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 1474 | unsigned NumBases) { |
| 1475 | if (NumBases == 0) |
| 1476 | return false; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1477 | |
| 1478 | // Used to keep track of which base types we have already seen, so |
| 1479 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1480 | // that the key is always the unqualified canonical type of the base |
| 1481 | // class. |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1482 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 1483 | |
| 1484 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1485 | unsigned NumGoodBases = 0; |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1486 | bool Invalid = false; |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1487 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | QualType NewBaseType |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1489 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1490 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Benjamin Kramer | 73ecd70 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1491 | |
| 1492 | CXXBaseSpecifier *&KnownBase = KnownBaseTypes[NewBaseType]; |
| 1493 | if (KnownBase) { |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1494 | // C++ [class.mi]p3: |
| 1495 | // A class shall not be specified as a direct base class of a |
| 1496 | // derived class more than once. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1497 | Diag(Bases[idx]->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1498 | diag::err_duplicate_base_class) |
Benjamin Kramer | 73ecd70 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1499 | << KnownBase->getType() |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1500 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1501 | |
| 1502 | // Delete the duplicate base class specifier; we're going to |
| 1503 | // overwrite its pointer later. |
Douglas Gregor | b77af8f | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1504 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1505 | |
| 1506 | Invalid = true; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1507 | } else { |
| 1508 | // Okay, add this new base class. |
Benjamin Kramer | 73ecd70 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1509 | KnownBase = Bases[idx]; |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1510 | Bases[NumGoodBases++] = Bases[idx]; |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1511 | if (const RecordType *Record = NewBaseType->getAs<RecordType>()) { |
| 1512 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
| 1513 | if (Class->isInterface() && |
| 1514 | (!RD->isInterface() || |
| 1515 | KnownBase->getAccessSpecifier() != AS_public)) { |
| 1516 | // The Microsoft extension __interface does not permit bases that |
| 1517 | // are not themselves public interfaces. |
| 1518 | Diag(KnownBase->getLocStart(), diag::err_invalid_base_in_interface) |
| 1519 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getName() |
| 1520 | << RD->getSourceRange(); |
| 1521 | Invalid = true; |
| 1522 | } |
| 1523 | if (RD->hasAttr<WeakAttr>()) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1524 | Class->addAttr(WeakAttr::CreateImplicit(Context)); |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1525 | } |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 4a62bdf | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 1530 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1531 | |
| 1532 | // Delete the remaining (good) base class specifiers, since their |
| 1533 | // data has been copied into the CXXRecordDecl. |
| 1534 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | b77af8f | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1535 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1536 | |
| 1537 | return Invalid; |
| 1538 | } |
| 1539 | |
| 1540 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 1541 | /// class, after checking whether there are any duplicate base |
| 1542 | /// classes. |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1543 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, CXXBaseSpecifier **Bases, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1544 | unsigned NumBases) { |
| 1545 | if (!ClassDecl || !Bases || !NumBases) |
| 1546 | return; |
| 1547 | |
| 1548 | AdjustDeclIfTemplate(ClassDecl); |
Robert Wilhelm | e3cea80 | 2013-07-22 05:04:01 +0000 | [diff] [blame] | 1549 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), Bases, NumBases); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1550 | } |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1551 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1552 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1553 | /// derived from the type \p Base. |
| 1554 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1555 | if (!getLangOpts().CPlusPlus) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1556 | return false; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1557 | |
Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1558 | CXXRecordDecl *DerivedRD = Derived->getAsCXXRecordDecl(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1559 | if (!DerivedRD) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1560 | return false; |
| 1561 | |
Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1562 | CXXRecordDecl *BaseRD = Base->getAsCXXRecordDecl(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1563 | if (!BaseRD) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1564 | return false; |
Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1565 | |
| 1566 | // If either the base or the derived type is invalid, don't try to |
| 1567 | // check whether one is derived from the other. |
| 1568 | if (BaseRD->isInvalidDecl() || DerivedRD->isInvalidDecl()) |
| 1569 | return false; |
| 1570 | |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1571 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 1572 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1576 | /// derived from the type \p Base. |
| 1577 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1578 | if (!getLangOpts().CPlusPlus) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1579 | return false; |
| 1580 | |
Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1581 | CXXRecordDecl *DerivedRD = Derived->getAsCXXRecordDecl(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1582 | if (!DerivedRD) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1583 | return false; |
| 1584 | |
Douglas Gregor | 45bb483 | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1585 | CXXRecordDecl *BaseRD = Base->getAsCXXRecordDecl(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1586 | if (!BaseRD) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1587 | return false; |
| 1588 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1589 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 1590 | } |
| 1591 | |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1592 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1593 | CXXCastPath &BasePathArray) { |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1594 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 1595 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 1596 | |
| 1597 | const CXXBasePath &Path = Paths.front(); |
| 1598 | |
| 1599 | // We first go backward and check if we have a virtual base. |
| 1600 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 1601 | // the nearest virtual base. |
| 1602 | unsigned Start = 0; |
| 1603 | for (unsigned I = Path.size(); I != 0; --I) { |
| 1604 | if (Path[I - 1].Base->isVirtual()) { |
| 1605 | Start = I - 1; |
| 1606 | break; |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | // Now add all bases. |
| 1611 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1612 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1615 | /// \brief Determine whether the given base path includes a virtual |
| 1616 | /// base class. |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1617 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 1618 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 1619 | BEnd = BasePath.end(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1620 | B != BEnd; ++B) |
| 1621 | if ((*B)->isVirtual()) |
| 1622 | return true; |
| 1623 | |
| 1624 | return false; |
| 1625 | } |
| 1626 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1627 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 1628 | /// conversion (where Derived and Base are class types) is |
| 1629 | /// well-formed, meaning that the conversion is unambiguous (and |
| 1630 | /// that all of the base classes are accessible). Returns true |
| 1631 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 1632 | /// otherwise. Loc is the location where this routine should point to |
| 1633 | /// if there is an error, and Range is the source range to highlight |
| 1634 | /// if there is an error. |
| 1635 | bool |
| 1636 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1637 | unsigned InaccessibleBaseID, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1638 | unsigned AmbigiousBaseConvID, |
| 1639 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1640 | DeclarationName Name, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1641 | CXXCastPath *BasePath) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1642 | // First, determine whether the path from Derived to Base is |
| 1643 | // ambiguous. This is slightly more expensive than checking whether |
| 1644 | // the Derived to Base conversion exists, because here we need to |
| 1645 | // explore multiple paths to determine if there is an ambiguity. |
| 1646 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1647 | /*DetectVirtual=*/false); |
| 1648 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1649 | assert(DerivationOkay && |
| 1650 | "Can only be used with a derived-to-base conversion"); |
| 1651 | (void)DerivationOkay; |
| 1652 | |
| 1653 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1654 | if (InaccessibleBaseID) { |
| 1655 | // Check that the base class can be accessed. |
| 1656 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 1657 | InaccessibleBaseID)) { |
| 1658 | case AR_inaccessible: |
| 1659 | return true; |
| 1660 | case AR_accessible: |
| 1661 | case AR_dependent: |
| 1662 | case AR_delayed: |
| 1663 | break; |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1664 | } |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1665 | } |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1666 | |
| 1667 | // Build a base path if necessary. |
| 1668 | if (BasePath) |
| 1669 | BuildBasePathArray(Paths, *BasePath); |
| 1670 | return false; |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
David Majnemer | 626032f | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 1673 | if (AmbigiousBaseConvID) { |
| 1674 | // We know that the derived-to-base conversion is ambiguous, and |
| 1675 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 1676 | // search just one more time to compute all of the possible paths so |
| 1677 | // that we can print them out. This is more expensive than any of |
| 1678 | // the previous derived-to-base checks we've done, but at this point |
| 1679 | // performance isn't as much of an issue. |
| 1680 | Paths.clear(); |
| 1681 | Paths.setRecordingPaths(true); |
| 1682 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1683 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 1684 | (void)StillOkay; |
| 1685 | |
| 1686 | // Build up a textual representation of the ambiguous paths, e.g., |
| 1687 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 1688 | // conversions in the diagnostic. We only print one of the paths |
| 1689 | // to each base class subobject. |
| 1690 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1691 | |
| 1692 | Diag(Loc, AmbigiousBaseConvID) |
| 1693 | << Derived << Base << PathDisplayStr << Range << Name; |
| 1694 | } |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1695 | return true; |
| 1696 | } |
| 1697 | |
| 1698 | bool |
| 1699 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1700 | SourceLocation Loc, SourceRange Range, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1701 | CXXCastPath *BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1702 | bool IgnoreAccess) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1703 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1704 | IgnoreAccess ? 0 |
| 1705 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1706 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1707 | Loc, Range, DeclarationName(), |
| 1708 | BasePath); |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | |
| 1712 | /// @brief Builds a string representing ambiguous paths from a |
| 1713 | /// specific derived class to different subobjects of the same base |
| 1714 | /// class. |
| 1715 | /// |
| 1716 | /// This function builds a string that can be used in error messages |
| 1717 | /// to show the different paths that one can take through the |
| 1718 | /// inheritance hierarchy to go from the derived class to different |
| 1719 | /// subobjects of a base class. The result looks something like this: |
| 1720 | /// @code |
| 1721 | /// struct D -> struct B -> struct A |
| 1722 | /// struct D -> struct C -> struct A |
| 1723 | /// @endcode |
| 1724 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 1725 | std::string PathDisplayStr; |
| 1726 | std::set<unsigned> DisplayedPaths; |
| 1727 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1728 | Path != Paths.end(); ++Path) { |
| 1729 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 1730 | // We haven't displayed a path to this particular base |
| 1731 | // class subobject yet. |
| 1732 | PathDisplayStr += "\n "; |
| 1733 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 1734 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 1735 | Element != Path->end(); ++Element) |
| 1736 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | return PathDisplayStr; |
| 1741 | } |
| 1742 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1743 | //===----------------------------------------------------------------------===// |
| 1744 | // C++ class member Handling |
| 1745 | //===----------------------------------------------------------------------===// |
| 1746 | |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1747 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1748 | bool Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 1749 | SourceLocation ASLoc, |
| 1750 | SourceLocation ColonLoc, |
| 1751 | AttributeList *Attrs) { |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1752 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1753 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1754 | ASLoc, ColonLoc); |
| 1755 | CurContext->addHiddenDecl(ASDecl); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1756 | return ProcessAccessDeclAttributeList(ASDecl, Attrs); |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1759 | /// CheckOverrideControl - Check C++11 override control semantics. |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1760 | void Sema::CheckOverrideControl(NamedDecl *D) { |
Richard Smith | 09b031f | 2012-09-06 18:32:18 +0000 | [diff] [blame] | 1761 | if (D->isInvalidDecl()) |
| 1762 | return; |
| 1763 | |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1764 | // We only care about "override" and "final" declarations. |
| 1765 | if (!D->hasAttr<OverrideAttr>() && !D->hasAttr<FinalAttr>()) |
| 1766 | return; |
Anders Carlsson | fd83553 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1767 | |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1768 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
Anders Carlsson | fa8e5d3 | 2011-01-20 06:33:26 +0000 | [diff] [blame] | 1769 | |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1770 | // We can't check dependent instance methods. |
| 1771 | if (MD && MD->isInstance() && |
| 1772 | (MD->getParent()->hasAnyDependentBases() || |
| 1773 | MD->getType()->isDependentType())) |
| 1774 | return; |
| 1775 | |
| 1776 | if (MD && !MD->isVirtual()) { |
| 1777 | // If we have a non-virtual method, check if if hides a virtual method. |
| 1778 | // (In that case, it's most likely the method has the wrong type.) |
| 1779 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| 1780 | FindHiddenVirtualMethods(MD, OverloadedMethods); |
| 1781 | |
| 1782 | if (!OverloadedMethods.empty()) { |
Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1783 | if (OverrideAttr *OA = D->getAttr<OverrideAttr>()) { |
| 1784 | Diag(OA->getLocation(), |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1785 | diag::override_keyword_hides_virtual_member_function) |
| 1786 | << "override" << (OverloadedMethods.size() > 1); |
| 1787 | } else if (FinalAttr *FA = D->getAttr<FinalAttr>()) { |
Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1788 | Diag(FA->getLocation(), |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1789 | diag::override_keyword_hides_virtual_member_function) |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1790 | << (FA->isSpelledAsSealed() ? "sealed" : "final") |
| 1791 | << (OverloadedMethods.size() > 1); |
Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1792 | } |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1793 | NoteHiddenVirtualMethods(MD, OverloadedMethods); |
| 1794 | MD->setInvalidDecl(); |
| 1795 | return; |
| 1796 | } |
| 1797 | // Fall through into the general case diagnostic. |
| 1798 | // FIXME: We might want to attempt typo correction here. |
| 1799 | } |
| 1800 | |
| 1801 | if (!MD || !MD->isVirtual()) { |
| 1802 | if (OverrideAttr *OA = D->getAttr<OverrideAttr>()) { |
| 1803 | Diag(OA->getLocation(), |
| 1804 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1805 | << "override" << FixItHint::CreateRemoval(OA->getLocation()); |
| 1806 | D->dropAttr<OverrideAttr>(); |
| 1807 | } |
| 1808 | if (FinalAttr *FA = D->getAttr<FinalAttr>()) { |
| 1809 | Diag(FA->getLocation(), |
| 1810 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1811 | << (FA->isSpelledAsSealed() ? "sealed" : "final") |
| 1812 | << FixItHint::CreateRemoval(FA->getLocation()); |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 1813 | D->dropAttr<FinalAttr>(); |
Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1814 | } |
Anders Carlsson | fd83553 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1815 | return; |
| 1816 | } |
Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1817 | |
Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1818 | // C++11 [class.virtual]p5: |
| 1819 | // If a virtual function is marked with the virt-specifier override and |
| 1820 | // does not override a member function of a base class, the program is |
| 1821 | // ill-formed. |
| 1822 | bool HasOverriddenMethods = |
| 1823 | MD->begin_overridden_methods() != MD->end_overridden_methods(); |
| 1824 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) |
| 1825 | Diag(MD->getLocation(), diag::err_function_marked_override_not_overriding) |
| 1826 | << MD->getDeclName(); |
Anders Carlsson | fd83553 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1829 | /// CheckIfOverriddenFunctionIsMarkedFinal - Checks whether a virtual member |
Anders Carlsson | 3f610c7 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1830 | /// function overrides a virtual member function marked 'final', according to |
Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1831 | /// C++11 [class.virtual]p4. |
Anders Carlsson | 3f610c7 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1832 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 1833 | const CXXMethodDecl *Old) { |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1834 | FinalAttr *FA = Old->getAttr<FinalAttr>(); |
| 1835 | if (!FA) |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1836 | return false; |
| 1837 | |
| 1838 | Diag(New->getLocation(), diag::err_final_function_overridden) |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 1839 | << New->getDeclName() |
| 1840 | << FA->isSpelledAsSealed(); |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1841 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 1842 | return true; |
Anders Carlsson | 3f610c7 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1845 | static bool InitializationHasSideEffects(const FieldDecl &FD) { |
Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 1846 | const Type *T = FD.getType()->getBaseElementTypeUnsafe(); |
| 1847 | // FIXME: Destruction of ObjC lifetime types has side-effects. |
| 1848 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 1849 | return !RD->isCompleteDefinition() || |
| 1850 | !RD->hasTrivialDefaultConstructor() || |
| 1851 | !RD->hasTrivialDestructor(); |
Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1852 | return false; |
| 1853 | } |
| 1854 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1855 | static AttributeList *getMSPropertyAttr(AttributeList *list) { |
| 1856 | for (AttributeList* it = list; it != 0; it = it->getNext()) |
| 1857 | if (it->isDeclspecPropertyAttribute()) |
| 1858 | return it; |
| 1859 | return 0; |
| 1860 | } |
| 1861 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1862 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 1863 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1864 | /// bitfield width if there is one, 'InitExpr' specifies the initializer if |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1865 | /// one has been parsed, and 'InitStyle' is set if an in-class initializer is |
| 1866 | /// present (but parsing it has been deferred). |
Rafael Espindola | 0a67e2f | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 1867 | NamedDecl * |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1868 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1869 | MultiTemplateParamsArg TemplateParameterLists, |
Richard Trieu | 2bd0401 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 1870 | Expr *BW, const VirtSpecifiers &VS, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1871 | InClassInitStyle InitStyle) { |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1872 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1873 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 1874 | DeclarationName Name = NameInfo.getName(); |
| 1875 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 23ab745 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1876 | |
| 1877 | // For anonymous bitfields, the location should point to the type. |
| 1878 | if (Loc.isInvalid()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1879 | Loc = D.getLocStart(); |
Douglas Gregor | 23ab745 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1880 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1881 | Expr *BitWidth = static_cast<Expr*>(BW); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1882 | |
John McCall | b1cd7da | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1883 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1884 | assert(!DS.isFriendSpecified()); |
| 1885 | |
Richard Smith | cfcdf3a | 2011-06-25 02:28:38 +0000 | [diff] [blame] | 1886 | bool isFunc = D.isDeclarationOfFunction(); |
John McCall | b1cd7da | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1887 | |
John McCall | db632ac | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1888 | if (cast<CXXRecordDecl>(CurContext)->isInterface()) { |
| 1889 | // The Microsoft extension __interface only permits public member functions |
| 1890 | // and prohibits constructors, destructors, operators, non-public member |
| 1891 | // functions, static methods and data members. |
| 1892 | unsigned InvalidDecl; |
| 1893 | bool ShowDeclName = true; |
| 1894 | if (!isFunc) |
| 1895 | InvalidDecl = (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) ? 0 : 1; |
| 1896 | else if (AS != AS_public) |
| 1897 | InvalidDecl = 2; |
| 1898 | else if (DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 1899 | InvalidDecl = 3; |
| 1900 | else switch (Name.getNameKind()) { |
| 1901 | case DeclarationName::CXXConstructorName: |
| 1902 | InvalidDecl = 4; |
| 1903 | ShowDeclName = false; |
| 1904 | break; |
| 1905 | |
| 1906 | case DeclarationName::CXXDestructorName: |
| 1907 | InvalidDecl = 5; |
| 1908 | ShowDeclName = false; |
| 1909 | break; |
| 1910 | |
| 1911 | case DeclarationName::CXXOperatorName: |
| 1912 | case DeclarationName::CXXConversionFunctionName: |
| 1913 | InvalidDecl = 6; |
| 1914 | break; |
| 1915 | |
| 1916 | default: |
| 1917 | InvalidDecl = 0; |
| 1918 | break; |
| 1919 | } |
| 1920 | |
| 1921 | if (InvalidDecl) { |
| 1922 | if (ShowDeclName) |
| 1923 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 1924 | << (InvalidDecl-1) << Name; |
| 1925 | else |
| 1926 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 1927 | << (InvalidDecl-1) << ""; |
| 1928 | return 0; |
| 1929 | } |
| 1930 | } |
| 1931 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1932 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 1933 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1934 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 1935 | // data members and cannot be applied to names declared const or static, |
| 1936 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1937 | switch (DS.getStorageClassSpec()) { |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1938 | case DeclSpec::SCS_unspecified: |
| 1939 | case DeclSpec::SCS_typedef: |
| 1940 | case DeclSpec::SCS_static: |
| 1941 | break; |
| 1942 | case DeclSpec::SCS_mutable: |
| 1943 | if (isFunc) { |
| 1944 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1946 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 1947 | // declarator. Otherwise we could get follow-up errors. |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1948 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1949 | } |
| 1950 | break; |
| 1951 | default: |
| 1952 | Diag(DS.getStorageClassSpecLoc(), |
| 1953 | diag::err_storageclass_invalid_for_member); |
| 1954 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 1955 | break; |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1958 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 1959 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | 1207d31 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 1960 | !isFunc); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1961 | |
David Blaikie | 35506f8 | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1962 | if (DS.isConstexprSpecified() && isInstField) { |
| 1963 | SemaDiagnosticBuilder B = |
| 1964 | Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr_member); |
| 1965 | SourceLocation ConstexprLoc = DS.getConstexprSpecLoc(); |
| 1966 | if (InitStyle == ICIS_NoInit) { |
| 1967 | B << 0 << 0 << FixItHint::CreateReplacement(ConstexprLoc, "const"); |
| 1968 | D.getMutableDeclSpec().ClearConstexprSpec(); |
| 1969 | const char *PrevSpec; |
| 1970 | unsigned DiagID; |
| 1971 | bool Failed = D.getMutableDeclSpec().SetTypeQual(DeclSpec::TQ_const, ConstexprLoc, |
| 1972 | PrevSpec, DiagID, getLangOpts()); |
Matt Beaumont-Gay | efc270d | 2013-01-31 00:08:03 +0000 | [diff] [blame] | 1973 | (void)Failed; |
David Blaikie | 35506f8 | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1974 | assert(!Failed && "Making a constexpr member const shouldn't fail"); |
| 1975 | } else { |
| 1976 | B << 1; |
| 1977 | const char *PrevSpec; |
| 1978 | unsigned DiagID; |
David Blaikie | 35506f8 | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1979 | if (D.getMutableDeclSpec().SetStorageClassSpec( |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1980 | *this, DeclSpec::SCS_static, ConstexprLoc, PrevSpec, DiagID, |
| 1981 | Context.getPrintingPolicy())) { |
Matt Beaumont-Gay | efc270d | 2013-01-31 00:08:03 +0000 | [diff] [blame] | 1982 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_mutable && |
David Blaikie | 35506f8 | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1983 | "This is the only DeclSpec that should fail to be applied"); |
| 1984 | B << 1; |
| 1985 | } else { |
| 1986 | B << 0 << FixItHint::CreateInsertion(ConstexprLoc, "static "); |
| 1987 | isInstField = false; |
| 1988 | } |
| 1989 | } |
| 1990 | } |
| 1991 | |
Rafael Espindola | 0a67e2f | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 1992 | NamedDecl *Member; |
Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1993 | if (isInstField) { |
Douglas Gregor | a007d36 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1994 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Douglas Gregor | bb64afc | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 1995 | |
| 1996 | // Data members must have identifiers for names. |
Benjamin Kramer | 365082d | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1997 | if (!Name.isIdentifier()) { |
Douglas Gregor | bb64afc | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 1998 | Diag(Loc, diag::err_bad_variable_name) |
| 1999 | << Name; |
| 2000 | return 0; |
| 2001 | } |
Douglas Gregor | 7c26c04 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2002 | |
Benjamin Kramer | 365082d | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 2003 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 2004 | |
Douglas Gregor | 7c26c04 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2005 | // Member field could not be with "template" keyword. |
| 2006 | // So TemplateParameterLists should be empty in this case. |
| 2007 | if (TemplateParameterLists.size()) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2008 | TemplateParameterList* TemplateParams = TemplateParameterLists[0]; |
Douglas Gregor | 7c26c04 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2009 | if (TemplateParams->size()) { |
| 2010 | // There is no such thing as a member field template. |
| 2011 | Diag(D.getIdentifierLoc(), diag::err_template_member) |
| 2012 | << II |
| 2013 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 2014 | TemplateParams->getRAngleLoc()); |
| 2015 | } else { |
| 2016 | // There is an extraneous 'template<>' for this member. |
| 2017 | Diag(TemplateParams->getTemplateLoc(), |
| 2018 | diag::err_template_member_noparams) |
| 2019 | << II |
| 2020 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 2021 | TemplateParams->getRAngleLoc()); |
| 2022 | } |
| 2023 | return 0; |
| 2024 | } |
| 2025 | |
Douglas Gregor | a007d36 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 2026 | if (SS.isSet() && !SS.isInvalid()) { |
| 2027 | // The user provided a superfluous scope specifier inside a class |
| 2028 | // definition: |
| 2029 | // |
| 2030 | // class X { |
| 2031 | // int X::member; |
| 2032 | // }; |
Douglas Gregor | b7d17dd | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 2033 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 2034 | diagnoseQualifiedDeclaration(SS, DC, Name, D.getIdentifierLoc()); |
Douglas Gregor | a007d36 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 2035 | else |
| 2036 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 2037 | << Name << SS.getRange(); |
Douglas Gregor | c3ae7c3 | 2011-11-01 22:13:30 +0000 | [diff] [blame] | 2038 | |
Douglas Gregor | a007d36 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 2039 | SS.clear(); |
| 2040 | } |
Douglas Gregor | 7c26c04 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 2041 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 2042 | AttributeList *MSPropertyAttr = |
| 2043 | getMSPropertyAttr(D.getDeclSpec().getAttributes().getList()); |
Eli Friedman | c37dbf7 | 2013-06-28 20:48:34 +0000 | [diff] [blame] | 2044 | if (MSPropertyAttr) { |
| 2045 | Member = HandleMSProperty(S, cast<CXXRecordDecl>(CurContext), Loc, D, |
| 2046 | BitWidth, InitStyle, AS, MSPropertyAttr); |
| 2047 | if (!Member) |
| 2048 | return 0; |
| 2049 | isInstField = false; |
| 2050 | } else { |
| 2051 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, |
| 2052 | BitWidth, InitStyle, AS); |
| 2053 | assert(Member && "HandleField never returns null"); |
| 2054 | } |
| 2055 | } else { |
| 2056 | assert(InitStyle == ICIS_NoInit || D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static); |
| 2057 | |
| 2058 | Member = HandleDeclarator(S, D, TemplateParameterLists); |
| 2059 | if (!Member) |
| 2060 | return 0; |
| 2061 | |
| 2062 | // Non-instance-fields can't have a bitfield. |
| 2063 | if (BitWidth) { |
Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 2064 | if (Member->isInvalidDecl()) { |
| 2065 | // don't emit another diagnostic. |
Douglas Gregor | 212cab3 | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 2066 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 2067 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 2068 | // "static member 'A' cannot be a bit-field" |
| 2069 | Diag(Loc, diag::err_static_not_bitfield) |
| 2070 | << Name << BitWidth->getSourceRange(); |
| 2071 | } else if (isa<TypedefDecl>(Member)) { |
| 2072 | // "typedef member 'x' cannot be a bit-field" |
| 2073 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 2074 | << Name << BitWidth->getSourceRange(); |
| 2075 | } else { |
| 2076 | // A function typedef ("typedef int f(); f a;"). |
| 2077 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 2078 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 2080 | << BitWidth->getSourceRange(); |
Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 2081 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | |
Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 2083 | BitWidth = 0; |
| 2084 | Member->setInvalidDecl(); |
| 2085 | } |
Douglas Gregor | 4261e4c | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 2086 | |
| 2087 | Member->setAccess(AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2088 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2089 | // If we have declared a member function template or static data member |
| 2090 | // template, set the access of the templated declaration as well. |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2091 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 2092 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2093 | else if (VarTemplateDecl *VarTmpl = dyn_cast<VarTemplateDecl>(Member)) |
| 2094 | VarTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 2095 | } |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2096 | |
Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2097 | if (VS.isOverrideSpecified()) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2098 | Member->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context, 0)); |
Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2099 | if (VS.isFinalSpecified()) |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 2100 | Member->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context, |
| 2101 | VS.isFinalSpelledSealed())); |
Anders Carlsson | fd83553 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 2102 | |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2103 | if (VS.getLastLocation().isValid()) { |
| 2104 | // Update the end location of a method that has a virt-specifiers. |
| 2105 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 2106 | MD->setRangeEnd(VS.getLastLocation()); |
| 2107 | } |
Richard Smith | 18f07db | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2108 | |
Anders Carlsson | c87f861 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 2109 | CheckOverrideControl(Member); |
Anders Carlsson | fd83553 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 2110 | |
Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 2111 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2112 | |
Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2113 | if (isInstField) { |
| 2114 | FieldDecl *FD = cast<FieldDecl>(Member); |
| 2115 | FieldCollector->Add(FD); |
| 2116 | |
| 2117 | if (Diags.getDiagnosticLevel(diag::warn_unused_private_field, |
| 2118 | FD->getLocation()) |
| 2119 | != DiagnosticsEngine::Ignored) { |
| 2120 | // Remember all explicit private FieldDecls that have a name, no side |
| 2121 | // effects and are not part of a dependent type declaration. |
| 2122 | if (!FD->isImplicit() && FD->getDeclName() && |
| 2123 | FD->getAccess() == AS_private && |
Daniel Jasper | 429c134 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 2124 | !FD->hasAttr<UnusedAttr>() && |
Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 2125 | !FD->getParent()->isDependentContext() && |
Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2126 | !InitializationHasSideEffects(*FD)) |
| 2127 | UnusedPrivateFields.insert(FD); |
| 2128 | } |
| 2129 | } |
| 2130 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2131 | return Member; |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2134 | namespace { |
| 2135 | class UninitializedFieldVisitor |
| 2136 | : public EvaluatedExprVisitor<UninitializedFieldVisitor> { |
| 2137 | Sema &S; |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2138 | // List of Decls to generate a warning on. Also remove Decls that become |
| 2139 | // initialized. |
Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2140 | llvm::SmallPtrSet<ValueDecl*, 4> &Decls; |
Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2141 | // If non-null, add a note to the warning pointing back to the constructor. |
| 2142 | const CXXConstructorDecl *Constructor; |
Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2143 | public: |
| 2144 | typedef EvaluatedExprVisitor<UninitializedFieldVisitor> Inherited; |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2145 | UninitializedFieldVisitor(Sema &S, |
Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2146 | llvm::SmallPtrSet<ValueDecl*, 4> &Decls, |
Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2147 | const CXXConstructorDecl *Constructor) |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2148 | : Inherited(S.Context), S(S), Decls(Decls), |
| 2149 | Constructor(Constructor) { } |
Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2150 | |
Richard Trieu | fd68777 | 2013-09-16 20:46:50 +0000 | [diff] [blame] | 2151 | void HandleMemberExpr(MemberExpr *ME, bool CheckReferenceOnly) { |
Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2152 | if (isa<EnumConstantDecl>(ME->getMemberDecl())) |
| 2153 | return; |
Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2154 | |
Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2155 | // FieldME is the inner-most MemberExpr that is not an anonymous struct |
| 2156 | // or union. |
| 2157 | MemberExpr *FieldME = ME; |
| 2158 | |
| 2159 | Expr *Base = ME; |
| 2160 | while (isa<MemberExpr>(Base)) { |
| 2161 | ME = cast<MemberExpr>(Base); |
| 2162 | |
| 2163 | if (isa<VarDecl>(ME->getMemberDecl())) |
| 2164 | return; |
| 2165 | |
| 2166 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2167 | if (!FD->isAnonymousStructOrUnion()) |
| 2168 | FieldME = ME; |
| 2169 | |
| 2170 | Base = ME->getBase(); |
| 2171 | } |
| 2172 | |
Richard Trieu | fd68777 | 2013-09-16 20:46:50 +0000 | [diff] [blame] | 2173 | if (!isa<CXXThisExpr>(Base)) |
| 2174 | return; |
| 2175 | |
Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2176 | ValueDecl* FoundVD = FieldME->getMemberDecl(); |
| 2177 | |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2178 | if (!Decls.count(FoundVD)) |
Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2179 | return; |
| 2180 | |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2181 | const bool IsReference = FoundVD->getType()->isReferenceType(); |
Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2182 | |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2183 | // Prevent double warnings on use of unbounded references. |
| 2184 | if (IsReference != CheckReferenceOnly) |
| 2185 | return; |
| 2186 | |
| 2187 | unsigned diag = IsReference |
| 2188 | ? diag::warn_reference_field_is_uninit |
| 2189 | : diag::warn_field_is_uninit; |
| 2190 | S.Diag(FieldME->getExprLoc(), diag) << FoundVD; |
| 2191 | if (Constructor) |
| 2192 | S.Diag(Constructor->getLocation(), |
| 2193 | diag::note_uninit_in_this_constructor) |
| 2194 | << (Constructor->isDefaultConstructor() && Constructor->isImplicit()); |
| 2195 | |
Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | void HandleValue(Expr *E) { |
| 2199 | E = E->IgnoreParens(); |
| 2200 | |
| 2201 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
Richard Trieu | fd68777 | 2013-09-16 20:46:50 +0000 | [diff] [blame] | 2202 | HandleMemberExpr(ME, false /*CheckReferenceOnly*/); |
Nick Lewycky | c363afb | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2203 | return; |
Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 2207 | HandleValue(CO->getTrueExpr()); |
| 2208 | HandleValue(CO->getFalseExpr()); |
| 2209 | return; |
| 2210 | } |
| 2211 | |
| 2212 | if (BinaryConditionalOperator *BCO = |
| 2213 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 2214 | HandleValue(BCO->getCommon()); |
| 2215 | HandleValue(BCO->getFalseExpr()); |
| 2216 | return; |
| 2217 | } |
| 2218 | |
| 2219 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 2220 | switch (BO->getOpcode()) { |
| 2221 | default: |
| 2222 | return; |
| 2223 | case(BO_PtrMemD): |
| 2224 | case(BO_PtrMemI): |
| 2225 | HandleValue(BO->getLHS()); |
| 2226 | return; |
| 2227 | case(BO_Comma): |
| 2228 | HandleValue(BO->getRHS()); |
| 2229 | return; |
| 2230 | } |
| 2231 | } |
| 2232 | } |
| 2233 | |
Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2234 | void VisitMemberExpr(MemberExpr *ME) { |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2235 | // All uses of unbounded reference fields will warn. |
Richard Trieu | fd68777 | 2013-09-16 20:46:50 +0000 | [diff] [blame] | 2236 | HandleMemberExpr(ME, true /*CheckReferenceOnly*/); |
Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2237 | |
| 2238 | Inherited::VisitMemberExpr(ME); |
| 2239 | } |
| 2240 | |
Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2241 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 2242 | if (E->getCastKind() == CK_LValueToRValue) |
| 2243 | HandleValue(E->getSubExpr()); |
| 2244 | |
| 2245 | Inherited::VisitImplicitCastExpr(E); |
| 2246 | } |
| 2247 | |
Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2248 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2249 | if (E->getConstructor()->isCopyConstructor()) |
Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2250 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(E->getArg(0))) |
| 2251 | if (ICE->getCastKind() == CK_NoOp) |
| 2252 | if (MemberExpr *ME = dyn_cast<MemberExpr>(ICE->getSubExpr())) |
Richard Trieu | fd68777 | 2013-09-16 20:46:50 +0000 | [diff] [blame] | 2253 | HandleMemberExpr(ME, false /*CheckReferenceOnly*/); |
Richard Trieu | 1bc22c1 | 2013-09-13 03:20:53 +0000 | [diff] [blame] | 2254 | |
| 2255 | Inherited::VisitCXXConstructExpr(E); |
| 2256 | } |
| 2257 | |
Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2258 | void VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 2259 | Expr *Callee = E->getCallee(); |
| 2260 | if (isa<MemberExpr>(Callee)) |
| 2261 | HandleValue(Callee); |
| 2262 | |
| 2263 | Inherited::VisitCXXMemberCallExpr(E); |
| 2264 | } |
Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2265 | |
| 2266 | void VisitBinaryOperator(BinaryOperator *E) { |
| 2267 | // If a field assignment is detected, remove the field from the |
| 2268 | // uninitiailized field set. |
| 2269 | if (E->getOpcode() == BO_Assign) |
| 2270 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E->getLHS())) |
| 2271 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2272 | if (!FD->getType()->isReferenceType()) |
| 2273 | Decls.erase(FD); |
Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2274 | |
| 2275 | Inherited::VisitBinaryOperator(E); |
| 2276 | } |
Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2277 | }; |
Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2278 | static void CheckInitExprContainsUninitializedFields( |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2279 | Sema &S, Expr *E, llvm::SmallPtrSet<ValueDecl*, 4> &Decls, |
| 2280 | const CXXConstructorDecl *Constructor) { |
| 2281 | if (Decls.size() == 0) |
Richard Trieu | 406e65c | 2013-09-20 03:03:06 +0000 | [diff] [blame] | 2282 | return; |
| 2283 | |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 2284 | if (!E) |
| 2285 | return; |
| 2286 | |
| 2287 | if (CXXDefaultInitExpr *Default = dyn_cast<CXXDefaultInitExpr>(E)) { |
| 2288 | E = Default->getExpr(); |
| 2289 | if (!E) |
| 2290 | return; |
| 2291 | // In class initializers will point to the constructor. |
| 2292 | UninitializedFieldVisitor(S, Decls, Constructor).Visit(E); |
| 2293 | } else { |
| 2294 | UninitializedFieldVisitor(S, Decls, 0).Visit(E); |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 2299 | // foo(foo) |
| 2300 | // where foo is not also a parameter to the constructor. |
| 2301 | // Also diagnose across field uninitialized use such as |
| 2302 | // x(y), y(x) |
| 2303 | // TODO: implement -Wuninitialized and fold this into that framework. |
| 2304 | static void DiagnoseUninitializedFields( |
| 2305 | Sema &SemaRef, const CXXConstructorDecl *Constructor) { |
| 2306 | |
| 2307 | if (SemaRef.getDiagnostics().getDiagnosticLevel(diag::warn_field_is_uninit, |
| 2308 | Constructor->getLocation()) |
| 2309 | == DiagnosticsEngine::Ignored) { |
| 2310 | return; |
| 2311 | } |
| 2312 | |
| 2313 | if (Constructor->isInvalidDecl()) |
| 2314 | return; |
| 2315 | |
| 2316 | const CXXRecordDecl *RD = Constructor->getParent(); |
| 2317 | |
| 2318 | // Holds fields that are uninitialized. |
| 2319 | llvm::SmallPtrSet<ValueDecl*, 4> UninitializedFields; |
| 2320 | |
| 2321 | // At the beginning, all fields are uninitialized. |
| 2322 | for (DeclContext::decl_iterator I = RD->decls_begin(), E = RD->decls_end(); |
| 2323 | I != E; ++I) { |
| 2324 | if (FieldDecl *FD = dyn_cast<FieldDecl>(*I)) { |
| 2325 | UninitializedFields.insert(FD); |
| 2326 | } else if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(*I)) { |
| 2327 | UninitializedFields.insert(IFD->getAnonField()); |
| 2328 | } |
| 2329 | } |
| 2330 | |
| 2331 | for (CXXConstructorDecl::init_const_iterator FieldInit = |
| 2332 | Constructor->init_begin(), |
| 2333 | FieldInitEnd = Constructor->init_end(); |
| 2334 | FieldInit != FieldInitEnd; ++FieldInit) { |
| 2335 | |
| 2336 | Expr *InitExpr = (*FieldInit)->getInit(); |
| 2337 | |
| 2338 | CheckInitExprContainsUninitializedFields( |
| 2339 | SemaRef, InitExpr, UninitializedFields, Constructor); |
| 2340 | |
| 2341 | if (FieldDecl *Field = (*FieldInit)->getAnyMember()) |
| 2342 | UninitializedFields.erase(Field); |
| 2343 | } |
Hans Wennborg | 44fd70a | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2344 | } |
| 2345 | } // namespace |
| 2346 | |
Richard Smith | 7410817 | 2014-01-17 03:11:34 +0000 | [diff] [blame] | 2347 | /// \brief Enter a new C++ default initializer scope. After calling this, the |
| 2348 | /// caller must call \ref ActOnFinishCXXInClassMemberInitializer, even if |
| 2349 | /// parsing or instantiating the initializer failed. |
| 2350 | void Sema::ActOnStartCXXInClassMemberInitializer() { |
| 2351 | // Create a synthetic function scope to represent the call to the constructor |
| 2352 | // that notionally surrounds a use of this initializer. |
| 2353 | PushFunctionScope(); |
| 2354 | } |
| 2355 | |
| 2356 | /// \brief This is invoked after parsing an in-class initializer for a |
| 2357 | /// non-static C++ class member, and after instantiating an in-class initializer |
| 2358 | /// in a class template. Such actions are deferred until the class is complete. |
| 2359 | void Sema::ActOnFinishCXXInClassMemberInitializer(Decl *D, |
| 2360 | SourceLocation InitLoc, |
| 2361 | Expr *InitExpr) { |
| 2362 | // Pop the notional constructor scope we created earlier. |
| 2363 | PopFunctionScopeInfo(0, D); |
| 2364 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2365 | FieldDecl *FD = cast<FieldDecl>(D); |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2366 | assert(FD->getInClassInitStyle() != ICIS_NoInit && |
| 2367 | "must set init style when field is created"); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2368 | |
| 2369 | if (!InitExpr) { |
| 2370 | FD->setInvalidDecl(); |
| 2371 | FD->removeInClassInitializer(); |
| 2372 | return; |
| 2373 | } |
| 2374 | |
Peter Collingbourne | 9f58d7b | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 2375 | if (DiagnoseUnexpandedParameterPack(InitExpr, UPPC_Initializer)) { |
| 2376 | FD->setInvalidDecl(); |
| 2377 | FD->removeInClassInitializer(); |
| 2378 | return; |
| 2379 | } |
| 2380 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2381 | ExprResult Init = InitExpr; |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2382 | if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent()) { |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2383 | InitializedEntity Entity = InitializedEntity::InitializeMember(FD); |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2384 | InitializationKind Kind = FD->getInClassInitStyle() == ICIS_ListInit |
Sebastian Redl | eef474c | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2385 | ? InitializationKind::CreateDirectList(InitExpr->getLocStart()) |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2386 | : InitializationKind::CreateCopy(InitExpr->getLocStart(), InitLoc); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2387 | InitializationSequence Seq(*this, Entity, Kind, InitExpr); |
| 2388 | Init = Seq.Perform(*this, Entity, Kind, InitExpr); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2389 | if (Init.isInvalid()) { |
| 2390 | FD->setInvalidDecl(); |
| 2391 | return; |
| 2392 | } |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2395 | // C++11 [class.base.init]p7: |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2396 | // The initialization of each base and member constitutes a |
| 2397 | // full-expression. |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2398 | Init = ActOnFinishFullExpr(Init.take(), InitLoc); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2399 | if (Init.isInvalid()) { |
| 2400 | FD->setInvalidDecl(); |
| 2401 | return; |
| 2402 | } |
| 2403 | |
| 2404 | InitExpr = Init.release(); |
| 2405 | |
| 2406 | FD->setInClassInitializer(InitExpr); |
| 2407 | } |
| 2408 | |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2409 | /// \brief Find the direct and/or virtual base specifiers that |
| 2410 | /// correspond to the given base type, for use in base initialization |
| 2411 | /// within a constructor. |
| 2412 | static bool FindBaseInitializer(Sema &SemaRef, |
| 2413 | CXXRecordDecl *ClassDecl, |
| 2414 | QualType BaseType, |
| 2415 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 2416 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 2417 | // First, check for a direct base class. |
| 2418 | DirectBaseSpec = 0; |
| 2419 | for (CXXRecordDecl::base_class_const_iterator Base |
| 2420 | = ClassDecl->bases_begin(); |
| 2421 | Base != ClassDecl->bases_end(); ++Base) { |
| 2422 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 2423 | // We found a direct base of this type. That's what we're |
| 2424 | // initializing. |
| 2425 | DirectBaseSpec = &*Base; |
| 2426 | break; |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | // Check for a virtual base class. |
| 2431 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 2432 | // there are no virtual bases. |
| 2433 | VirtualBaseSpec = 0; |
| 2434 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 2435 | // We haven't found a base yet; search the class hierarchy for a |
| 2436 | // virtual base class. |
| 2437 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 2438 | /*DetectVirtual=*/false); |
| 2439 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 2440 | BaseType, Paths)) { |
| 2441 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 2442 | Path != Paths.end(); ++Path) { |
| 2443 | if (Path->back().Base->isVirtual()) { |
| 2444 | VirtualBaseSpec = Path->back().Base; |
| 2445 | break; |
| 2446 | } |
| 2447 | } |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | return DirectBaseSpec || VirtualBaseSpec; |
| 2452 | } |
| 2453 | |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2454 | /// \brief Handle a C++ member initializer using braced-init-list syntax. |
| 2455 | MemInitResult |
| 2456 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
| 2457 | Scope *S, |
| 2458 | CXXScopeSpec &SS, |
| 2459 | IdentifierInfo *MemberOrBase, |
| 2460 | ParsedType TemplateTypeTy, |
David Blaikie | 186a889 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2461 | const DeclSpec &DS, |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2462 | SourceLocation IdLoc, |
| 2463 | Expr *InitList, |
| 2464 | SourceLocation EllipsisLoc) { |
| 2465 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2466 | DS, IdLoc, InitList, |
David Blaikie | 186a889 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2467 | EllipsisLoc); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | /// \brief Handle a C++ member initializer using parentheses syntax. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2471 | MemInitResult |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2472 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2473 | Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2474 | CXXScopeSpec &SS, |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2475 | IdentifierInfo *MemberOrBase, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2476 | ParsedType TemplateTypeTy, |
David Blaikie | 186a889 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2477 | const DeclSpec &DS, |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2478 | SourceLocation IdLoc, |
| 2479 | SourceLocation LParenLoc, |
Dmitri Gribenko | 139474d | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 2480 | ArrayRef<Expr *> Args, |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2481 | SourceLocation RParenLoc, |
| 2482 | SourceLocation EllipsisLoc) { |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2483 | Expr *List = new (Context) ParenListExpr(Context, LParenLoc, |
Dmitri Gribenko | 139474d | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 2484 | Args, RParenLoc); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2485 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2486 | DS, IdLoc, List, EllipsisLoc); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2489 | namespace { |
| 2490 | |
Kaelyn Uhrain | 8811b39 | 2012-01-11 21:17:51 +0000 | [diff] [blame] | 2491 | // Callback to only accept typo corrections that can be a valid C++ member |
| 2492 | // intializer: either a non-static field member or a base class. |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2493 | class MemInitializerValidatorCCC : public CorrectionCandidateCallback { |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2494 | public: |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2495 | explicit MemInitializerValidatorCCC(CXXRecordDecl *ClassDecl) |
| 2496 | : ClassDecl(ClassDecl) {} |
| 2497 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2498 | bool ValidateCandidate(const TypoCorrection &candidate) LLVM_OVERRIDE { |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2499 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 2500 | if (FieldDecl *Member = dyn_cast<FieldDecl>(ND)) |
| 2501 | return Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl); |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2502 | return isa<TypeDecl>(ND); |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2503 | } |
| 2504 | return false; |
| 2505 | } |
| 2506 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2507 | private: |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2508 | CXXRecordDecl *ClassDecl; |
| 2509 | }; |
| 2510 | |
| 2511 | } |
| 2512 | |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2513 | /// \brief Handle a C++ member initializer. |
| 2514 | MemInitResult |
| 2515 | Sema::BuildMemInitializer(Decl *ConstructorD, |
| 2516 | Scope *S, |
| 2517 | CXXScopeSpec &SS, |
| 2518 | IdentifierInfo *MemberOrBase, |
| 2519 | ParsedType TemplateTypeTy, |
David Blaikie | 186a889 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2520 | const DeclSpec &DS, |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2521 | SourceLocation IdLoc, |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2522 | Expr *Init, |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2523 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2524 | if (!ConstructorD) |
| 2525 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | |
Douglas Gregor | c8c277a | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2527 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2528 | |
| 2529 | CXXConstructorDecl *Constructor |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2530 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2531 | if (!Constructor) { |
| 2532 | // The user wrote a constructor initializer on a function that is |
| 2533 | // not a C++ constructor. Ignore the error for now, because we may |
| 2534 | // have more member initializers coming; we'll diagnose it just |
| 2535 | // once in ActOnMemInitializers. |
| 2536 | return true; |
| 2537 | } |
| 2538 | |
| 2539 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2540 | |
| 2541 | // C++ [class.base.init]p2: |
| 2542 | // 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] | 2543 | // constructor's class and, if not found in that scope, are looked |
| 2544 | // up in the scope containing the constructor's definition. |
| 2545 | // [Note: if the constructor's class contains a member with the |
| 2546 | // same name as a direct or virtual base class of the class, a |
| 2547 | // mem-initializer-id naming the member or base class and composed |
| 2548 | // of a single identifier refers to the class member. A |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2549 | // mem-initializer-id for the hidden base class may be specified |
| 2550 | // using a qualified name. ] |
Fariborz Jahanian | c1fc3ec | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2551 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | 302bb66 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2552 | // Look for a member, first. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2553 | DeclContext::lookup_result Result |
Fariborz Jahanian | 302bb66 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2554 | = ClassDecl->lookup(MemberOrBase); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2555 | if (!Result.empty()) { |
Peter Collingbourne | 05156e3 | 2011-10-23 18:59:37 +0000 | [diff] [blame] | 2556 | ValueDecl *Member; |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2557 | if ((Member = dyn_cast<FieldDecl>(Result.front())) || |
| 2558 | (Member = dyn_cast<IndirectFieldDecl>(Result.front()))) { |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2559 | if (EllipsisLoc.isValid()) |
| 2560 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2561 | << MemberOrBase |
| 2562 | << SourceRange(IdLoc, Init->getSourceRange().getEnd()); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2563 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2564 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2565 | } |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2566 | } |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2567 | } |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2568 | // 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] | 2569 | QualType BaseType; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2570 | TypeSourceInfo *TInfo = 0; |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2571 | |
| 2572 | if (TemplateTypeTy) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2573 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
David Blaikie | 186a889 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2574 | } else if (DS.getTypeSpecType() == TST_decltype) { |
| 2575 | BaseType = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2576 | } else { |
| 2577 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 2578 | LookupParsedName(R, S, &SS); |
| 2579 | |
| 2580 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 2581 | if (!TyD) { |
| 2582 | if (R.isAmbiguous()) return true; |
| 2583 | |
John McCall | da6841b | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 2584 | // We don't want access-control diagnostics here. |
| 2585 | R.suppressDiagnostics(); |
| 2586 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2587 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 2588 | bool NotUnknownSpecialization = false; |
| 2589 | DeclContext *DC = computeDeclContext(SS, false); |
| 2590 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 2591 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 2592 | |
| 2593 | if (!NotUnknownSpecialization) { |
| 2594 | // When the scope specifier can refer to a member of an unknown |
| 2595 | // specialization, we take it as a type name. |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 2596 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 2597 | SS.getWithLocInContext(Context), |
| 2598 | *MemberOrBase, IdLoc); |
Douglas Gregor | 281c486 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 2599 | if (BaseType.isNull()) |
| 2600 | return true; |
| 2601 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2602 | R.clear(); |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 2603 | R.setLookupName(MemberOrBase); |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2604 | } |
| 2605 | } |
| 2606 | |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2607 | // If no results were found, try to correct typos. |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2608 | TypoCorrection Corr; |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2609 | MemInitializerValidatorCCC Validator(ClassDecl); |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2610 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2611 | (Corr = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, &SS, |
Kaelyn Uhrain | 4e8942c | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 2612 | Validator, ClassDecl))) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2613 | if (FieldDecl *Member = Corr.getCorrectionDeclAs<FieldDecl>()) { |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2614 | // We have found a non-static data member with a similar |
| 2615 | // name to what was typed; complain and initialize that |
| 2616 | // member. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2617 | diagnoseTypo(Corr, |
| 2618 | PDiag(diag::err_mem_init_not_member_or_class_suggest) |
| 2619 | << MemberOrBase << true); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2620 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2621 | } else if (TypeDecl *Type = Corr.getCorrectionDeclAs<TypeDecl>()) { |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2622 | const CXXBaseSpecifier *DirectBaseSpec; |
| 2623 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 2624 | if (FindBaseInitializer(*this, ClassDecl, |
| 2625 | Context.getTypeDeclType(Type), |
| 2626 | DirectBaseSpec, VirtualBaseSpec)) { |
| 2627 | // We have found a direct or virtual base class with a |
| 2628 | // similar name to what was typed; complain and initialize |
| 2629 | // that base class. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2630 | diagnoseTypo(Corr, |
| 2631 | PDiag(diag::err_mem_init_not_member_or_class_suggest) |
| 2632 | << MemberOrBase << false, |
| 2633 | PDiag() /*Suppress note, we provide our own.*/); |
Douglas Gregor | 43a0857 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2634 | |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2635 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec ? DirectBaseSpec |
| 2636 | : VirtualBaseSpec; |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2637 | Diag(BaseSpec->getLocStart(), |
Douglas Gregor | 43a0857 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2638 | diag::note_base_class_specified_here) |
| 2639 | << BaseSpec->getType() |
| 2640 | << BaseSpec->getSourceRange(); |
| 2641 | |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2642 | TyD = Type; |
| 2643 | } |
| 2644 | } |
| 2645 | } |
| 2646 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2647 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2648 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2649 | << MemberOrBase << SourceRange(IdLoc,Init->getSourceRange().getEnd()); |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2650 | return true; |
| 2651 | } |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2654 | if (BaseType.isNull()) { |
| 2655 | BaseType = Context.getTypeDeclType(TyD); |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2656 | if (SS.isSet()) |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2657 | // FIXME: preserve source range information |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 2658 | BaseType = Context.getElaboratedType(ETK_None, SS.getScopeRep(), |
| 2659 | BaseType); |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2660 | } |
| 2661 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2662 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2663 | if (!TInfo) |
| 2664 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2665 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2666 | return BuildBaseInitializer(BaseType, TInfo, Init, ClassDecl, EllipsisLoc); |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Chandler Carruth | 599deef | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2669 | /// Checks a member initializer expression for cases where reference (or |
| 2670 | /// pointer) members are bound to by-value parameters (or their addresses). |
Chandler Carruth | 599deef | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2671 | static void CheckForDanglingReferenceOrPointer(Sema &S, ValueDecl *Member, |
| 2672 | Expr *Init, |
| 2673 | SourceLocation IdLoc) { |
| 2674 | QualType MemberTy = Member->getType(); |
| 2675 | |
| 2676 | // We only handle pointers and references currently. |
| 2677 | // FIXME: Would this be relevant for ObjC object pointers? Or block pointers? |
| 2678 | if (!MemberTy->isReferenceType() && !MemberTy->isPointerType()) |
| 2679 | return; |
| 2680 | |
| 2681 | const bool IsPointer = MemberTy->isPointerType(); |
| 2682 | if (IsPointer) { |
| 2683 | if (const UnaryOperator *Op |
| 2684 | = dyn_cast<UnaryOperator>(Init->IgnoreParenImpCasts())) { |
| 2685 | // The only case we're worried about with pointers requires taking the |
| 2686 | // address. |
| 2687 | if (Op->getOpcode() != UO_AddrOf) |
| 2688 | return; |
| 2689 | |
| 2690 | Init = Op->getSubExpr(); |
| 2691 | } else { |
| 2692 | // We only handle address-of expression initializers for pointers. |
| 2693 | return; |
| 2694 | } |
| 2695 | } |
| 2696 | |
Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2697 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init->IgnoreParens())) { |
Chandler Carruth | d551d4e | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2698 | // We only warn when referring to a non-reference parameter declaration. |
| 2699 | const ParmVarDecl *Parameter = dyn_cast<ParmVarDecl>(DRE->getDecl()); |
| 2700 | if (!Parameter || Parameter->getType()->isReferenceType()) |
Chandler Carruth | 599deef | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2701 | return; |
| 2702 | |
| 2703 | S.Diag(Init->getExprLoc(), |
| 2704 | IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 2705 | : diag::warn_bind_ref_member_to_parameter) |
| 2706 | << Member << Parameter << Init->getSourceRange(); |
Chandler Carruth | d551d4e | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2707 | } else { |
| 2708 | // Other initializers are fine. |
| 2709 | return; |
Chandler Carruth | 599deef | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2710 | } |
Chandler Carruth | d551d4e | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2711 | |
| 2712 | S.Diag(Member->getLocation(), diag::note_ref_or_ptr_member_declared_here) |
| 2713 | << (unsigned)IsPointer; |
Chandler Carruth | 599deef | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2716 | MemInitResult |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2717 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr *Init, |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2718 | SourceLocation IdLoc) { |
Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2719 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 2720 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 2721 | assert((DirectMember || IndirectMember) && |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2722 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 2723 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2724 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Peter Collingbourne | 9f58d7b | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 2725 | return true; |
| 2726 | |
Douglas Gregor | 266bb5f | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 2727 | if (Member->isInvalidDecl()) |
| 2728 | return true; |
Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2729 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2730 | MultiExprArg Args; |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2731 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2732 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2733 | } else if (InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2734 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2735 | } else { |
| 2736 | // Template instantiation doesn't reconstruct ParenListExprs for us. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2737 | Args = Init; |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2738 | } |
Daniel Jasper | 0baec549 | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2739 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2740 | SourceRange InitRange = Init->getSourceRange(); |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2741 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2742 | if (Member->getType()->isDependentType() || Init->isTypeDependent()) { |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2743 | // Can't check initialization for a member of dependent type or when |
| 2744 | // any of the arguments are type-dependent expressions. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2745 | DiscardCleanupsInEvaluationContext(); |
Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2746 | } else { |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2747 | bool InitList = false; |
| 2748 | if (isa<InitListExpr>(Init)) { |
| 2749 | InitList = true; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2750 | Args = Init; |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2753 | // Initialize the member. |
| 2754 | InitializedEntity MemberEntity = |
| 2755 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 2756 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 2757 | InitializationKind Kind = |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2758 | InitList ? InitializationKind::CreateDirectList(IdLoc) |
| 2759 | : InitializationKind::CreateDirect(IdLoc, InitRange.getBegin(), |
| 2760 | InitRange.getEnd()); |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2761 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2762 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args); |
| 2763 | ExprResult MemberInit = InitSeq.Perform(*this, MemberEntity, Kind, Args, 0); |
Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2764 | if (MemberInit.isInvalid()) |
| 2765 | return true; |
| 2766 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 2767 | CheckForDanglingReferenceOrPointer(*this, Member, MemberInit.get(), IdLoc); |
| 2768 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2769 | // C++11 [class.base.init]p7: |
Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2770 | // The initialization of each base and member constitutes a |
| 2771 | // full-expression. |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2772 | MemberInit = ActOnFinishFullExpr(MemberInit.get(), InitRange.getBegin()); |
Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2773 | if (MemberInit.isInvalid()) |
| 2774 | return true; |
| 2775 | |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2776 | Init = MemberInit.get(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2779 | if (DirectMember) { |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2780 | return new (Context) CXXCtorInitializer(Context, DirectMember, IdLoc, |
| 2781 | InitRange.getBegin(), Init, |
| 2782 | InitRange.getEnd()); |
Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2783 | } else { |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2784 | return new (Context) CXXCtorInitializer(Context, IndirectMember, IdLoc, |
| 2785 | InitRange.getBegin(), Init, |
| 2786 | InitRange.getEnd()); |
Chandler Carruth | d44c310 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2787 | } |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2790 | MemInitResult |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2791 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, Expr *Init, |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2792 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2793 | SourceLocation NameLoc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2794 | if (!LangOpts.CPlusPlus11) |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2795 | return Diag(NameLoc, diag::err_delegating_ctor) |
Alexis Hunt | 4049b8d | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2796 | << TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2797 | Diag(NameLoc, diag::warn_cxx98_compat_delegating_ctor); |
Sebastian Redl | 9cb4be2 | 2011-03-12 13:53:51 +0000 | [diff] [blame] | 2798 | |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2799 | bool InitList = true; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2800 | MultiExprArg Args = Init; |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2801 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 2802 | InitList = false; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2803 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2806 | SourceRange InitRange = Init->getSourceRange(); |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2807 | // Initialize the object. |
| 2808 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 2809 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 2810 | InitializationKind Kind = |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2811 | InitList ? InitializationKind::CreateDirectList(NameLoc) |
| 2812 | : InitializationKind::CreateDirect(NameLoc, InitRange.getBegin(), |
| 2813 | InitRange.getEnd()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2814 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2815 | ExprResult DelegationInit = InitSeq.Perform(*this, DelegationEntity, Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2816 | Args, 0); |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2817 | if (DelegationInit.isInvalid()) |
| 2818 | return true; |
| 2819 | |
Matt Beaumont-Gay | 3e59fac | 2011-11-01 18:10:22 +0000 | [diff] [blame] | 2820 | assert(cast<CXXConstructExpr>(DelegationInit.get())->getConstructor() && |
| 2821 | "Delegating constructor with no target?"); |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2822 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2823 | // C++11 [class.base.init]p7: |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2824 | // The initialization of each base and member constitutes a |
| 2825 | // full-expression. |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2826 | DelegationInit = ActOnFinishFullExpr(DelegationInit.get(), |
| 2827 | InitRange.getBegin()); |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2828 | if (DelegationInit.isInvalid()) |
| 2829 | return true; |
| 2830 | |
Eli Friedman | a9e9ebc | 2012-05-19 23:35:23 +0000 | [diff] [blame] | 2831 | // If we are in a dependent context, template instantiation will |
| 2832 | // perform this type-checking again. Just save the arguments that we |
| 2833 | // received in a ParenListExpr. |
| 2834 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2835 | // of the information that we have about the base |
| 2836 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2837 | // and this approach is far more likely to get the corner cases right. |
| 2838 | if (CurContext->isDependentContext()) |
| 2839 | DelegationInit = Owned(Init); |
| 2840 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2841 | return new (Context) CXXCtorInitializer(Context, TInfo, InitRange.getBegin(), |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2842 | DelegationInit.takeAs<Expr>(), |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2843 | InitRange.getEnd()); |
Alexis Hunt | 4049b8d | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | MemInitResult |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2847 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2848 | Expr *Init, CXXRecordDecl *ClassDecl, |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2849 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2850 | SourceLocation BaseLoc |
| 2851 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2852 | |
Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2853 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 2854 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 2855 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2856 | |
| 2857 | // C++ [class.base.init]p2: |
| 2858 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2859 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2860 | // of that class, the mem-initializer is ill-formed. A |
| 2861 | // mem-initializer-list can initialize a base class using any |
| 2862 | // name that denotes that base class type. |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2863 | bool Dependent = BaseType->isDependentType() || Init->isTypeDependent(); |
Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2864 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2865 | SourceRange InitRange = Init->getSourceRange(); |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2866 | if (EllipsisLoc.isValid()) { |
| 2867 | // This is a pack expansion. |
| 2868 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 2869 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2870 | << SourceRange(BaseLoc, InitRange.getEnd()); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2871 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2872 | EllipsisLoc = SourceLocation(); |
| 2873 | } |
| 2874 | } else { |
| 2875 | // Check for any unexpanded parameter packs. |
| 2876 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 2877 | return true; |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2878 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2879 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2880 | return true; |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2881 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2882 | |
Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2883 | // Check for direct and virtual base classes. |
| 2884 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 2885 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 2886 | if (!Dependent) { |
Alexis Hunt | 4049b8d | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2887 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 2888 | BaseType)) |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2889 | return BuildDelegatingInitializer(BaseTInfo, Init, ClassDecl); |
Alexis Hunt | 4049b8d | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2890 | |
Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2891 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 2892 | VirtualBaseSpec); |
| 2893 | |
| 2894 | // C++ [base.class.init]p2: |
| 2895 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 2896 | // constructor's class or a direct or virtual base of that class, the |
| 2897 | // mem-initializer is ill-formed. |
| 2898 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 2899 | // If the class has any dependent bases, then it's possible that |
| 2900 | // one of those types will resolve to the same type as |
| 2901 | // BaseType. Therefore, just treat this as a dependent base |
| 2902 | // class initialization. FIXME: Should we try to check the |
| 2903 | // initialization anyway? It seems odd. |
| 2904 | if (ClassDecl->hasAnyDependentBases()) |
| 2905 | Dependent = true; |
| 2906 | else |
| 2907 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 2908 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 2909 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2910 | } |
| 2911 | } |
| 2912 | |
| 2913 | if (Dependent) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2914 | DiscardCleanupsInEvaluationContext(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2915 | |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2916 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
| 2917 | /*IsVirtual=*/false, |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2918 | InitRange.getBegin(), Init, |
| 2919 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2920 | } |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2921 | |
| 2922 | // C++ [base.class.init]p2: |
| 2923 | // If a mem-initializer-id is ambiguous because it designates both |
| 2924 | // a direct non-virtual base class and an inherited virtual base |
| 2925 | // class, the mem-initializer is ill-formed. |
| 2926 | if (DirectBaseSpec && VirtualBaseSpec) |
| 2927 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2928 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2929 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2930 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec; |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2931 | if (!BaseSpec) |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2932 | BaseSpec = VirtualBaseSpec; |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2933 | |
| 2934 | // Initialize the base. |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2935 | bool InitList = true; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2936 | MultiExprArg Args = Init; |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2937 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2938 | InitList = false; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2939 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2940 | } |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2941 | |
| 2942 | InitializedEntity BaseEntity = |
| 2943 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
| 2944 | InitializationKind Kind = |
| 2945 | InitList ? InitializationKind::CreateDirectList(BaseLoc) |
| 2946 | : InitializationKind::CreateDirect(BaseLoc, InitRange.getBegin(), |
| 2947 | InitRange.getEnd()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2948 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args); |
| 2949 | ExprResult BaseInit = InitSeq.Perform(*this, BaseEntity, Kind, Args, 0); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2950 | if (BaseInit.isInvalid()) |
| 2951 | return true; |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2952 | |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2953 | // C++11 [class.base.init]p7: |
| 2954 | // The initialization of each base and member constitutes a |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2955 | // full-expression. |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2956 | BaseInit = ActOnFinishFullExpr(BaseInit.get(), InitRange.getBegin()); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2957 | if (BaseInit.isInvalid()) |
| 2958 | return true; |
| 2959 | |
| 2960 | // If we are in a dependent context, template instantiation will |
| 2961 | // perform this type-checking again. Just save the arguments that we |
| 2962 | // received in a ParenListExpr. |
| 2963 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2964 | // of the information that we have about the base |
| 2965 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2966 | // 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] | 2967 | if (CurContext->isDependentContext()) |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2968 | BaseInit = Owned(Init); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2969 | |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2970 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2971 | BaseSpec->isVirtual(), |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2972 | InitRange.getBegin(), |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2973 | BaseInit.takeAs<Expr>(), |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2974 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2977 | // Create a static_cast\<T&&>(expr). |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 2978 | static Expr *CastForMoving(Sema &SemaRef, Expr *E, QualType T = QualType()) { |
| 2979 | if (T.isNull()) T = E->getType(); |
| 2980 | QualType TargetType = SemaRef.BuildReferenceType( |
| 2981 | T, /*SpelledAsLValue*/false, SourceLocation(), DeclarationName()); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2982 | SourceLocation ExprLoc = E->getLocStart(); |
| 2983 | TypeSourceInfo *TargetLoc = SemaRef.Context.getTrivialTypeSourceInfo( |
| 2984 | TargetType, ExprLoc); |
| 2985 | |
| 2986 | return SemaRef.BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
| 2987 | SourceRange(ExprLoc, ExprLoc), |
| 2988 | E->getSourceRange()).take(); |
| 2989 | } |
| 2990 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2991 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 2992 | /// initialize its base or member. |
| 2993 | enum ImplicitInitializerKind { |
| 2994 | IIK_Default, |
| 2995 | IIK_Copy, |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 2996 | IIK_Move, |
| 2997 | IIK_Inherit |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2998 | }; |
| 2999 | |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3000 | static bool |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3001 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3002 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3003 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3004 | bool IsInheritedVirtualBase, |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3005 | CXXCtorInitializer *&CXXBaseInit) { |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3006 | InitializedEntity InitEntity |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3007 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 3008 | IsInheritedVirtualBase); |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3009 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3010 | ExprResult BaseInit; |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3011 | |
| 3012 | switch (ImplicitInitKind) { |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3013 | case IIK_Inherit: { |
| 3014 | const CXXRecordDecl *Inherited = |
| 3015 | Constructor->getInheritedConstructor()->getParent(); |
| 3016 | const CXXRecordDecl *Base = BaseSpec->getType()->getAsCXXRecordDecl(); |
| 3017 | if (Base && Inherited->getCanonicalDecl() == Base->getCanonicalDecl()) { |
| 3018 | // C++11 [class.inhctor]p8: |
| 3019 | // Each expression in the expression-list is of the form |
| 3020 | // static_cast<T&&>(p), where p is the name of the corresponding |
| 3021 | // constructor parameter and T is the declared type of p. |
| 3022 | SmallVector<Expr*, 16> Args; |
| 3023 | for (unsigned I = 0, E = Constructor->getNumParams(); I != E; ++I) { |
| 3024 | ParmVarDecl *PD = Constructor->getParamDecl(I); |
| 3025 | ExprResult ArgExpr = |
| 3026 | SemaRef.BuildDeclRefExpr(PD, PD->getType().getNonReferenceType(), |
| 3027 | VK_LValue, SourceLocation()); |
| 3028 | if (ArgExpr.isInvalid()) |
| 3029 | return true; |
| 3030 | Args.push_back(CastForMoving(SemaRef, ArgExpr.take(), PD->getType())); |
| 3031 | } |
| 3032 | |
| 3033 | InitializationKind InitKind = InitializationKind::CreateDirect( |
| 3034 | Constructor->getLocation(), SourceLocation(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3035 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, Args); |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3036 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, Args); |
| 3037 | break; |
| 3038 | } |
| 3039 | } |
| 3040 | // Fall through. |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3041 | case IIK_Default: { |
| 3042 | InitializationKind InitKind |
| 3043 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3044 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, None); |
| 3045 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, None); |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3046 | break; |
| 3047 | } |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3048 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3049 | case IIK_Move: |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3050 | case IIK_Copy: { |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3051 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3052 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 3053 | QualType ParamType = Param->getType().getNonReferenceType(); |
Eli Friedman | 2dfa793 | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 3054 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3055 | Expr *CopyCtorArg = |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3056 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3057 | SourceLocation(), Param, false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3058 | Constructor->getLocation(), ParamType, |
| 3059 | VK_LValue, 0); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3060 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3061 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(CopyCtorArg)); |
| 3062 | |
Anders Carlsson | af13c7b | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 3063 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 3064 | QualType ArgTy = |
| 3065 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 3066 | ParamType.getQualifiers()); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3067 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3068 | if (Moving) { |
| 3069 | CopyCtorArg = CastForMoving(SemaRef, CopyCtorArg); |
| 3070 | } |
| 3071 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3072 | CXXCastPath BasePath; |
| 3073 | BasePath.push_back(BaseSpec); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3074 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 3075 | CK_UncheckedDerivedToBase, |
Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3076 | Moving ? VK_XValue : VK_LValue, |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3077 | &BasePath).take(); |
Anders Carlsson | af13c7b | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 3078 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3079 | InitializationKind InitKind |
| 3080 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 3081 | SourceLocation(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3082 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, CopyCtorArg); |
| 3083 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, CopyCtorArg); |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3084 | break; |
| 3085 | } |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3086 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3087 | |
Douglas Gregor | a40433a | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3088 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3089 | if (BaseInit.isInvalid()) |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3090 | return true; |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3091 | |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3092 | CXXBaseInit = |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3093 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3094 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 3095 | SourceLocation()), |
| 3096 | BaseSpec->isVirtual(), |
| 3097 | SourceLocation(), |
| 3098 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 3099 | SourceLocation(), |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3100 | SourceLocation()); |
| 3101 | |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3102 | return false; |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3105 | static bool RefersToRValueRef(Expr *MemRef) { |
| 3106 | ValueDecl *Referenced = cast<MemberExpr>(MemRef)->getMemberDecl(); |
| 3107 | return Referenced->getType()->isRValueReferenceType(); |
| 3108 | } |
| 3109 | |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3110 | static bool |
| 3111 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3112 | ImplicitInitializerKind ImplicitInitKind, |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3113 | FieldDecl *Field, IndirectFieldDecl *Indirect, |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3114 | CXXCtorInitializer *&CXXMemberInit) { |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3115 | if (Field->isInvalidDecl()) |
| 3116 | return true; |
| 3117 | |
Chandler Carruth | 9c9286b | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 3118 | SourceLocation Loc = Constructor->getLocation(); |
| 3119 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3120 | if (ImplicitInitKind == IIK_Copy || ImplicitInitKind == IIK_Move) { |
| 3121 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3122 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 3123 | QualType ParamType = Param->getType().getNonReferenceType(); |
John McCall | 1b1a1db | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 3124 | |
| 3125 | // Suppress copying zero-width bitfields. |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3126 | if (Field->isBitField() && Field->getBitWidthValue(SemaRef.Context) == 0) |
| 3127 | return false; |
Douglas Gregor | 10f939c | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3128 | |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3129 | Expr *MemberExprBase = |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3130 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3131 | SourceLocation(), Param, false, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3132 | Loc, ParamType, VK_LValue, 0); |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3133 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3134 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(MemberExprBase)); |
| 3135 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3136 | if (Moving) { |
| 3137 | MemberExprBase = CastForMoving(SemaRef, MemberExprBase); |
| 3138 | } |
| 3139 | |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3140 | // Build a reference to this field within the parameter. |
| 3141 | CXXScopeSpec SS; |
| 3142 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 3143 | Sema::LookupMemberName); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3144 | MemberLookup.addDecl(Indirect ? cast<ValueDecl>(Indirect) |
| 3145 | : cast<ValueDecl>(Field), AS_public); |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3146 | MemberLookup.resolveKind(); |
Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3147 | ExprResult CtorArg |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3148 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3149 | ParamType, Loc, |
| 3150 | /*IsArrow=*/false, |
| 3151 | SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3152 | /*TemplateKWLoc=*/SourceLocation(), |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3153 | /*FirstQualifierInScope=*/0, |
| 3154 | MemberLookup, |
| 3155 | /*TemplateArgs=*/0); |
Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3156 | if (CtorArg.isInvalid()) |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3157 | return true; |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3158 | |
| 3159 | // C++11 [class.copy]p15: |
| 3160 | // - if a member m has rvalue reference type T&&, it is direct-initialized |
| 3161 | // with static_cast<T&&>(x.m); |
Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3162 | if (RefersToRValueRef(CtorArg.get())) { |
| 3163 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3166 | // When the field we are copying is an array, create index variables for |
| 3167 | // each dimension of the array. We use these index variables to subscript |
| 3168 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 3169 | // necessary iteration with these index variables. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3170 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3171 | QualType BaseType = Field->getType(); |
| 3172 | QualType SizeType = SemaRef.Context.getSizeType(); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3173 | bool InitializingArray = false; |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3174 | while (const ConstantArrayType *Array |
| 3175 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3176 | InitializingArray = true; |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3177 | // Create the iteration variable for this array index. |
| 3178 | IdentifierInfo *IterationVarName = 0; |
| 3179 | { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3180 | SmallString<8> Str; |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3181 | llvm::raw_svector_ostream OS(Str); |
| 3182 | OS << "__i" << IndexVariables.size(); |
| 3183 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 3184 | } |
| 3185 | VarDecl *IterationVar |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3186 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, Loc, |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3187 | IterationVarName, SizeType, |
| 3188 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3189 | SC_None); |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3190 | IndexVariables.push_back(IterationVar); |
| 3191 | |
| 3192 | // Create a reference to the iteration variable. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3193 | ExprResult IterationVarRef |
Eli Friedman | 844f945 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 3194 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3195 | assert(!IterationVarRef.isInvalid() && |
| 3196 | "Reference to invented variable cannot fail!"); |
Eli Friedman | 844f945 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 3197 | IterationVarRef = SemaRef.DefaultLvalueConversion(IterationVarRef.take()); |
| 3198 | assert(!IterationVarRef.isInvalid() && |
| 3199 | "Conversion of invented variable cannot fail!"); |
Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3200 | |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3201 | // Subscript the array with this iteration variable. |
Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3202 | CtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CtorArg.take(), Loc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3203 | IterationVarRef.take(), |
Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3204 | Loc); |
| 3205 | if (CtorArg.isInvalid()) |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3206 | return true; |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3207 | |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3208 | BaseType = Array->getElementType(); |
| 3209 | } |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3210 | |
| 3211 | // The array subscript expression is an lvalue, which is wrong for moving. |
| 3212 | if (Moving && InitializingArray) |
Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3213 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3214 | |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3215 | // Construct the entity that we will be initializing. For an array, this |
| 3216 | // will be first element in the array, which may require several levels |
| 3217 | // of array-subscript entities. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3218 | SmallVector<InitializedEntity, 4> Entities; |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3219 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3220 | if (Indirect) |
| 3221 | Entities.push_back(InitializedEntity::InitializeMember(Indirect)); |
| 3222 | else |
| 3223 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3224 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 3225 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 3226 | 0, |
| 3227 | Entities.back())); |
| 3228 | |
| 3229 | // Direct-initialize to use the copy constructor. |
| 3230 | InitializationKind InitKind = |
| 3231 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 3232 | |
Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3233 | Expr *CtorArgE = CtorArg.takeAs<Expr>(); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3234 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, CtorArgE); |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3235 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3236 | ExprResult MemberInit |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3237 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
Sebastian Redl | e9c4e84 | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3238 | MultiExprArg(&CtorArgE, 1)); |
Douglas Gregor | a40433a | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3239 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3240 | if (MemberInit.isInvalid()) |
| 3241 | return true; |
| 3242 | |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3243 | if (Indirect) { |
| 3244 | assert(IndexVariables.size() == 0 && |
| 3245 | "Indirect field improperly initialized"); |
| 3246 | CXXMemberInit |
| 3247 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 3248 | Loc, Loc, |
| 3249 | MemberInit.takeAs<Expr>(), |
| 3250 | Loc); |
| 3251 | } else |
| 3252 | CXXMemberInit = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, |
| 3253 | Loc, MemberInit.takeAs<Expr>(), |
| 3254 | Loc, |
| 3255 | IndexVariables.data(), |
| 3256 | IndexVariables.size()); |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3257 | return false; |
| 3258 | } |
| 3259 | |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3260 | assert((ImplicitInitKind == IIK_Default || ImplicitInitKind == IIK_Inherit) && |
| 3261 | "Unhandled implicit init kind!"); |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3262 | |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3263 | QualType FieldBaseElementType = |
| 3264 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 3265 | |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3266 | if (FieldBaseElementType->isRecordType()) { |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3267 | InitializedEntity InitEntity |
| 3268 | = Indirect? InitializedEntity::InitializeMember(Indirect) |
| 3269 | : InitializedEntity::InitializeMember(Field); |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3270 | InitializationKind InitKind = |
Chandler Carruth | 9c9286b | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 3271 | InitializationKind::CreateDefault(Loc); |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3272 | |
| 3273 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, None); |
| 3274 | ExprResult MemberInit = |
| 3275 | InitSeq.Perform(SemaRef, InitEntity, InitKind, None); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3276 | |
Douglas Gregor | a40433a | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3277 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3278 | if (MemberInit.isInvalid()) |
| 3279 | return true; |
| 3280 | |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3281 | if (Indirect) |
| 3282 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 3283 | Indirect, Loc, |
| 3284 | Loc, |
| 3285 | MemberInit.get(), |
| 3286 | Loc); |
| 3287 | else |
| 3288 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 3289 | Field, Loc, Loc, |
| 3290 | MemberInit.get(), |
| 3291 | Loc); |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3292 | return false; |
| 3293 | } |
Anders Carlsson | dca6be0 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3294 | |
Alexis Hunt | 8b45518 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 3295 | if (!Field->getParent()->isUnion()) { |
| 3296 | if (FieldBaseElementType->isReferenceType()) { |
| 3297 | SemaRef.Diag(Constructor->getLocation(), |
| 3298 | diag::err_uninitialized_member_in_ctor) |
| 3299 | << (int)Constructor->isImplicit() |
| 3300 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 3301 | << 0 << Field->getDeclName(); |
| 3302 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 3303 | return true; |
| 3304 | } |
Anders Carlsson | dca6be0 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3305 | |
Alexis Hunt | 8b45518 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 3306 | if (FieldBaseElementType.isConstQualified()) { |
| 3307 | SemaRef.Diag(Constructor->getLocation(), |
| 3308 | diag::err_uninitialized_member_in_ctor) |
| 3309 | << (int)Constructor->isImplicit() |
| 3310 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 3311 | << 1 << Field->getDeclName(); |
| 3312 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 3313 | return true; |
| 3314 | } |
Anders Carlsson | dca6be0 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3315 | } |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3316 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3317 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3318 | FieldBaseElementType->isObjCRetainableType() && |
| 3319 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_None && |
| 3320 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) { |
Douglas Gregor | 6fa6942 | 2012-07-23 04:23:39 +0000 | [diff] [blame] | 3321 | // ARC: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3322 | // Default-initialize Objective-C pointers to NULL. |
| 3323 | CXXMemberInit |
| 3324 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 3325 | Loc, Loc, |
| 3326 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()), |
| 3327 | Loc); |
| 3328 | return false; |
| 3329 | } |
| 3330 | |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3331 | // Nothing to initialize. |
| 3332 | CXXMemberInit = 0; |
| 3333 | return false; |
| 3334 | } |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3335 | |
| 3336 | namespace { |
| 3337 | struct BaseAndFieldInfo { |
| 3338 | Sema &S; |
| 3339 | CXXConstructorDecl *Ctor; |
| 3340 | bool AnyErrorsInInits; |
| 3341 | ImplicitInitializerKind IIK; |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3342 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3343 | SmallVector<CXXCtorInitializer*, 8> AllToInit; |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3344 | llvm::DenseMap<TagDecl*, FieldDecl*> ActiveUnionMember; |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3345 | |
| 3346 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 3347 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3348 | bool Generated = Ctor->isImplicit() || Ctor->isDefaulted(); |
| 3349 | if (Generated && Ctor->isCopyConstructor()) |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3350 | IIK = IIK_Copy; |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3351 | else if (Generated && Ctor->isMoveConstructor()) |
| 3352 | IIK = IIK_Move; |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3353 | else if (Ctor->getInheritedConstructor()) |
| 3354 | IIK = IIK_Inherit; |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3355 | else |
| 3356 | IIK = IIK_Default; |
| 3357 | } |
Douglas Gregor | 7db3e95 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3358 | |
| 3359 | bool isImplicitCopyOrMove() const { |
| 3360 | switch (IIK) { |
| 3361 | case IIK_Copy: |
| 3362 | case IIK_Move: |
| 3363 | return true; |
| 3364 | |
| 3365 | case IIK_Default: |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3366 | case IIK_Inherit: |
Douglas Gregor | 7db3e95 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3367 | return false; |
| 3368 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 3369 | |
| 3370 | llvm_unreachable("Invalid ImplicitInitializerKind!"); |
Douglas Gregor | 7db3e95 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3371 | } |
Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3372 | |
| 3373 | bool addFieldInitializer(CXXCtorInitializer *Init) { |
| 3374 | AllToInit.push_back(Init); |
| 3375 | |
| 3376 | // Check whether this initializer makes the field "used". |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3377 | if (Init->getInit()->HasSideEffects(S.Context)) |
Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3378 | S.UnusedPrivateFields.remove(Init->getAnyMember()); |
| 3379 | |
| 3380 | return false; |
| 3381 | } |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3382 | |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3383 | bool isInactiveUnionMember(FieldDecl *Field) { |
| 3384 | RecordDecl *Record = Field->getParent(); |
| 3385 | if (!Record->isUnion()) |
| 3386 | return false; |
| 3387 | |
Richard Smith | 8d18385 | 2013-12-10 20:56:03 +0000 | [diff] [blame] | 3388 | if (FieldDecl *Active = |
| 3389 | ActiveUnionMember.lookup(Record->getCanonicalDecl())) |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3390 | return Active != Field->getCanonicalDecl(); |
| 3391 | |
| 3392 | // In an implicit copy or move constructor, ignore any in-class initializer. |
| 3393 | if (isImplicitCopyOrMove()) |
| 3394 | return true; |
| 3395 | |
| 3396 | // If there's no explicit initialization, the field is active only if it |
| 3397 | // has an in-class initializer... |
| 3398 | if (Field->hasInClassInitializer()) |
| 3399 | return false; |
| 3400 | // ... or it's an anonymous struct or union whose class has an in-class |
| 3401 | // initializer. |
| 3402 | if (!Field->isAnonymousStructOrUnion()) |
| 3403 | return true; |
| 3404 | CXXRecordDecl *FieldRD = Field->getType()->getAsCXXRecordDecl(); |
| 3405 | return !FieldRD->hasInClassInitializer(); |
| 3406 | } |
| 3407 | |
| 3408 | /// \brief Determine whether the given field is, or is within, a union member |
| 3409 | /// that is inactive (because there was an initializer given for a different |
| 3410 | /// member of the union, or because the union was not initialized at all). |
| 3411 | bool isWithinInactiveUnionMember(FieldDecl *Field, |
| 3412 | IndirectFieldDecl *Indirect) { |
| 3413 | if (!Indirect) |
| 3414 | return isInactiveUnionMember(Field); |
| 3415 | |
| 3416 | for (IndirectFieldDecl::chain_iterator C = Indirect->chain_begin(), |
| 3417 | CEnd = Indirect->chain_end(); |
| 3418 | C != CEnd; ++C) { |
| 3419 | FieldDecl *Field = dyn_cast<FieldDecl>(*C); |
| 3420 | if (Field && isInactiveUnionMember(Field)) |
Richard Smith | c94ec84 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 3421 | return true; |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3422 | } |
| 3423 | return false; |
| 3424 | } |
| 3425 | }; |
Richard Smith | c94ec84 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
Douglas Gregor | 10f939c | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3428 | /// \brief Determine whether the given type is an incomplete or zero-lenfgth |
| 3429 | /// array type. |
| 3430 | static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) { |
| 3431 | if (T->isIncompleteArrayType()) |
| 3432 | return true; |
| 3433 | |
| 3434 | while (const ConstantArrayType *ArrayT = Context.getAsConstantArrayType(T)) { |
| 3435 | if (!ArrayT->getSize()) |
| 3436 | return true; |
| 3437 | |
| 3438 | T = ArrayT->getElementType(); |
| 3439 | } |
| 3440 | |
| 3441 | return false; |
| 3442 | } |
| 3443 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3444 | static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3445 | FieldDecl *Field, |
| 3446 | IndirectFieldDecl *Indirect = 0) { |
Eli Friedman | b13e64e | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 3447 | if (Field->isInvalidDecl()) |
| 3448 | return false; |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3449 | |
Chandler Carruth | 139e962 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 3450 | // Overwhelmingly common case: we have a direct initializer for this field. |
Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3451 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(Field)) |
| 3452 | return Info.addFieldInitializer(Init); |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3453 | |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3454 | // C++11 [class.base.init]p8: |
| 3455 | // if the entity is a non-static data member that has a |
| 3456 | // brace-or-equal-initializer and either |
| 3457 | // -- the constructor's class is a union and no other variant member of that |
| 3458 | // union is designated by a mem-initializer-id or |
| 3459 | // -- the constructor's class is not a union, and, if the entity is a member |
| 3460 | // of an anonymous union, no other member of that union is designated by |
| 3461 | // a mem-initializer-id, |
| 3462 | // the entity is initialized as specified in [dcl.init]. |
| 3463 | // |
| 3464 | // We also apply the same rules to handle anonymous structs within anonymous |
| 3465 | // unions. |
| 3466 | if (Info.isWithinInactiveUnionMember(Field, Indirect)) |
| 3467 | return false; |
| 3468 | |
Douglas Gregor | 7db3e95 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3469 | if (Field->hasInClassInitializer() && !Info.isImplicitCopyOrMove()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3470 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, |
| 3471 | Info.Ctor->getLocation(), Field); |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3472 | CXXCtorInitializer *Init; |
| 3473 | if (Indirect) |
| 3474 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 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()); |
| 3478 | else |
| 3479 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 3480 | SourceLocation(), |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3481 | SourceLocation(), DIE, |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3482 | SourceLocation()); |
Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3483 | return Info.addFieldInitializer(Init); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
Douglas Gregor | 10f939c | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3486 | // Don't initialize incomplete or zero-length arrays. |
| 3487 | if (isIncompleteOrZeroLengthArrayType(SemaRef.Context, Field->getType())) |
| 3488 | return false; |
| 3489 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3490 | // Don't try to build an implicit initializer if there were semantic |
| 3491 | // errors in any of the initializers (and therefore we might be |
| 3492 | // missing some that the user actually wrote). |
Eli Friedman | b13e64e | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 3493 | if (Info.AnyErrorsInInits) |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3494 | return false; |
| 3495 | |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3496 | CXXCtorInitializer *Init = 0; |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3497 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, |
| 3498 | Indirect, Init)) |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3499 | return true; |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3500 | |
Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3501 | if (!Init) |
| 3502 | return false; |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3503 | |
Richard Smith | 0a8cfc7 | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3504 | return Info.addFieldInitializer(Init); |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3505 | } |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3506 | |
| 3507 | bool |
| 3508 | Sema::SetDelegatingInitializer(CXXConstructorDecl *Constructor, |
| 3509 | CXXCtorInitializer *Initializer) { |
Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3510 | assert(Initializer->isDelegatingInitializer()); |
Alexis Hunt | 5583d56 | 2011-05-03 20:43:02 +0000 | [diff] [blame] | 3511 | Constructor->setNumCtorInitializers(1); |
| 3512 | CXXCtorInitializer **initializer = |
| 3513 | new (Context) CXXCtorInitializer*[1]; |
| 3514 | memcpy(initializer, &Initializer, sizeof (CXXCtorInitializer*)); |
| 3515 | Constructor->setCtorInitializers(initializer); |
| 3516 | |
Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3517 | if (CXXDestructorDecl *Dtor = LookupDestructor(Constructor->getParent())) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3518 | MarkFunctionReferenced(Initializer->getSourceLocation(), Dtor); |
Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3519 | DiagnoseUseOfDecl(Dtor, Initializer->getSourceLocation()); |
| 3520 | } |
| 3521 | |
Alexis Hunt | e262299 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 3522 | DelegatingCtorDecls.push_back(Constructor); |
Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3523 | |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3524 | return false; |
| 3525 | } |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3526 | |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3527 | bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, bool AnyErrors, |
| 3528 | ArrayRef<CXXCtorInitializer *> Initializers) { |
Douglas Gregor | 5223529 | 2011-09-22 23:04:35 +0000 | [diff] [blame] | 3529 | if (Constructor->isDependentContext()) { |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3530 | // Just store the initializers as written, they will be checked during |
| 3531 | // instantiation. |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3532 | if (!Initializers.empty()) { |
| 3533 | Constructor->setNumCtorInitializers(Initializers.size()); |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3534 | CXXCtorInitializer **baseOrMemberInitializers = |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3535 | new (Context) CXXCtorInitializer*[Initializers.size()]; |
| 3536 | memcpy(baseOrMemberInitializers, Initializers.data(), |
| 3537 | Initializers.size() * sizeof(CXXCtorInitializer*)); |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3538 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3539 | } |
Richard Smith | 60f2e1e | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 3540 | |
| 3541 | // Let template instantiation know whether we had errors. |
| 3542 | if (AnyErrors) |
| 3543 | Constructor->setInvalidDecl(); |
| 3544 | |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3545 | return false; |
| 3546 | } |
| 3547 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3548 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3549 | |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3550 | // We need to build the initializer AST according to order of construction |
| 3551 | // and not what user specified in the Initializers list. |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3552 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | c14922f | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 3553 | if (!ClassDecl) |
| 3554 | return true; |
| 3555 | |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3556 | bool HadError = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3557 | |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3558 | for (unsigned i = 0; i < Initializers.size(); i++) { |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3559 | CXXCtorInitializer *Member = Initializers[i]; |
Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3560 | |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3561 | if (Member->isBaseInitializer()) |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3562 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3563 | else { |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3564 | Info.AllBaseFields[Member->getAnyMember()] = Member; |
Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 3565 | |
| 3566 | if (IndirectFieldDecl *F = Member->getIndirectMember()) { |
| 3567 | for (IndirectFieldDecl::chain_iterator C = F->chain_begin(), |
| 3568 | CEnd = F->chain_end(); |
| 3569 | C != CEnd; ++C) { |
| 3570 | FieldDecl *FD = dyn_cast<FieldDecl>(*C); |
| 3571 | if (FD && FD->getParent()->isUnion()) |
| 3572 | Info.ActiveUnionMember.insert(std::make_pair( |
| 3573 | FD->getParent()->getCanonicalDecl(), FD->getCanonicalDecl())); |
| 3574 | } |
| 3575 | } else if (FieldDecl *FD = Member->getMember()) { |
| 3576 | if (FD->getParent()->isUnion()) |
| 3577 | Info.ActiveUnionMember.insert(std::make_pair( |
| 3578 | FD->getParent()->getCanonicalDecl(), FD->getCanonicalDecl())); |
| 3579 | } |
| 3580 | } |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3583 | // Keep track of the direct virtual bases. |
| 3584 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 3585 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 3586 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 3587 | if (I->isVirtual()) |
| 3588 | DirectVBases.insert(I); |
| 3589 | } |
| 3590 | |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3591 | // Push virtual bases before others. |
| 3592 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 3593 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 3594 | |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3595 | if (CXXCtorInitializer *Value |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3596 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3597 | // [class.base.init]p7, per DR257: |
| 3598 | // A mem-initializer where the mem-initializer-id names a virtual base |
| 3599 | // class is ignored during execution of a constructor of any class that |
| 3600 | // is not the most derived class. |
| 3601 | if (ClassDecl->isAbstract()) { |
| 3602 | // FIXME: Provide a fixit to remove the base specifier. This requires |
| 3603 | // tracking the location of the associated comma for a base specifier. |
| 3604 | Diag(Value->getSourceLocation(), diag::warn_abstract_vbase_init_ignored) |
| 3605 | << VBase->getType() << ClassDecl; |
| 3606 | DiagnoseAbstractType(ClassDecl); |
| 3607 | } |
| 3608 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3609 | Info.AllToInit.push_back(Value); |
Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3610 | } else if (!AnyErrors && !ClassDecl->isAbstract()) { |
| 3611 | // [class.base.init]p8, per DR257: |
| 3612 | // If a given [...] base class is not named by a mem-initializer-id |
| 3613 | // [...] and the entity is not a virtual base class of an abstract |
| 3614 | // class, then [...] the entity is default-initialized. |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3615 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3616 | CXXCtorInitializer *CXXBaseInit; |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3617 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3618 | VBase, IsInheritedVirtualBase, |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3619 | CXXBaseInit)) { |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3620 | HadError = true; |
| 3621 | continue; |
| 3622 | } |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3623 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3624 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3625 | } |
| 3626 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3627 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3628 | // Non-virtual bases. |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3629 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3630 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 3631 | // Virtuals are in the virtual base list and already constructed. |
| 3632 | if (Base->isVirtual()) |
| 3633 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3634 | |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3635 | if (CXXCtorInitializer *Value |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3636 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 3637 | Info.AllToInit.push_back(Value); |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3638 | } else if (!AnyErrors) { |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3639 | CXXCtorInitializer *CXXBaseInit; |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3640 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3641 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3642 | CXXBaseInit)) { |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3643 | HadError = true; |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3644 | continue; |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3645 | } |
Fariborz Jahanian | 59a1cd4 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 3646 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3647 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3648 | } |
| 3649 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3650 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3651 | // Fields. |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3652 | for (DeclContext::decl_iterator Mem = ClassDecl->decls_begin(), |
| 3653 | MemEnd = ClassDecl->decls_end(); |
| 3654 | Mem != MemEnd; ++Mem) { |
| 3655 | if (FieldDecl *F = dyn_cast<FieldDecl>(*Mem)) { |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3656 | // C++ [class.bit]p2: |
| 3657 | // A declaration for a bit-field that omits the identifier declares an |
| 3658 | // unnamed bit-field. Unnamed bit-fields are not members and cannot be |
| 3659 | // initialized. |
| 3660 | if (F->isUnnamedBitfield()) |
| 3661 | continue; |
Douglas Gregor | 10f939c | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3662 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3663 | // 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] | 3664 | // handle anonymous struct/union fields based on their individual |
| 3665 | // indirect fields. |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3666 | if (F->isAnonymousStructOrUnion() && !Info.isImplicitCopyOrMove()) |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3667 | continue; |
| 3668 | |
| 3669 | if (CollectFieldInitializer(*this, Info, F)) |
| 3670 | HadError = true; |
Fariborz Jahanian | 1138ba6 | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 3671 | continue; |
| 3672 | } |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3673 | |
| 3674 | // Beyond this point, we only consider default initialization. |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3675 | if (Info.isImplicitCopyOrMove()) |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3676 | continue; |
| 3677 | |
| 3678 | if (IndirectFieldDecl *F = dyn_cast<IndirectFieldDecl>(*Mem)) { |
| 3679 | if (F->getType()->isIncompleteArrayType()) { |
| 3680 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 3681 | "Incomplete array type is not valid"); |
| 3682 | continue; |
| 3683 | } |
| 3684 | |
Douglas Gregor | 493627b | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3685 | // Initialize each field of an anonymous struct individually. |
| 3686 | if (CollectFieldInitializer(*this, Info, F->getAnonField(), F)) |
| 3687 | HadError = true; |
| 3688 | |
| 3689 | continue; |
| 3690 | } |
Fariborz Jahanian | 1138ba6 | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 3691 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3692 | |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3693 | unsigned NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3694 | if (NumInitializers > 0) { |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3695 | Constructor->setNumCtorInitializers(NumInitializers); |
| 3696 | CXXCtorInitializer **baseOrMemberInitializers = |
| 3697 | new (Context) CXXCtorInitializer*[NumInitializers]; |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3698 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3699 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 3700 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Rafael Espindola | 13327bb | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 3701 | |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3702 | // Constructors implicitly reference the base and member |
| 3703 | // destructors. |
| 3704 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 3705 | Constructor->getParent()); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3706 | } |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3707 | |
| 3708 | return HadError; |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
David Blaikie | b61b815 | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3711 | static void PopulateKeysForFields(FieldDecl *Field, SmallVectorImpl<const void*> &IdealInits) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3712 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
David Blaikie | b61b815 | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3713 | const RecordDecl *RD = RT->getDecl(); |
| 3714 | if (RD->isAnonymousStructOrUnion()) { |
| 3715 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3716 | E = RD->field_end(); Field != E; ++Field) |
| 3717 | PopulateKeysForFields(*Field, IdealInits); |
| 3718 | return; |
| 3719 | } |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3720 | } |
David Blaikie | b61b815 | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3721 | IdealInits.push_back(Field); |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3724 | static const void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 3725 | return Context.getCanonicalType(BaseType).getTypePtr(); |
Anders Carlsson | bcec05c | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3728 | static const void *GetKeyForMember(ASTContext &Context, |
| 3729 | CXXCtorInitializer *Member) { |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3730 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3731 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | a942dcd | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 3732 | |
David Blaikie | b61b815 | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3733 | return Member->getAnyMember(); |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3736 | static void DiagnoseBaseOrMemInitializerOrder( |
| 3737 | Sema &SemaRef, const CXXConstructorDecl *Constructor, |
| 3738 | ArrayRef<CXXCtorInitializer *> Inits) { |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3739 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 35d6e3e | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 3740 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3741 | |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3742 | // Don't check initializers order unless the warning is enabled at the |
| 3743 | // location of at least one initializer. |
| 3744 | bool ShouldCheckOrder = false; |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3745 | for (unsigned InitIndex = 0; InitIndex != Inits.size(); ++InitIndex) { |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3746 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3747 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order, |
| 3748 | Init->getSourceLocation()) |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 3749 | != DiagnosticsEngine::Ignored) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3750 | ShouldCheckOrder = true; |
| 3751 | break; |
| 3752 | } |
| 3753 | } |
| 3754 | if (!ShouldCheckOrder) |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3755 | return; |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3756 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3757 | // Build the list of bases and members in the order that they'll |
| 3758 | // actually be initialized. The explicit initializers should be in |
| 3759 | // this same order but may be missing things. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3760 | SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3761 | |
Anders Carlsson | 96b8fc6 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3762 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3763 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3764 | // 1. Virtual bases. |
Anders Carlsson | 96b8fc6 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3765 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3766 | ClassDecl->vbases_begin(), |
| 3767 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3768 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3769 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3770 | // 2. Non-virtual bases. |
Anders Carlsson | 96b8fc6 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3771 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3772 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3773 | if (Base->isVirtual()) |
| 3774 | continue; |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3775 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3776 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3777 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3778 | // 3. Direct fields. |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3779 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3780 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 3781 | if (Field->isUnnamedBitfield()) |
| 3782 | continue; |
| 3783 | |
David Blaikie | b61b815 | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3784 | PopulateKeysForFields(*Field, IdealInitKeys); |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3787 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 3788 | unsigned IdealIndex = 0; |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3789 | |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3790 | CXXCtorInitializer *PrevInit = 0; |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3791 | for (unsigned InitIndex = 0; InitIndex != Inits.size(); ++InitIndex) { |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3792 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3793 | const void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3794 | |
| 3795 | // Scan forward to try to find this initializer in the idealized |
| 3796 | // initializers list. |
| 3797 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3798 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3799 | break; |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3800 | |
| 3801 | // If we didn't find this initializer, it must be because we |
| 3802 | // scanned past it on a previous iteration. That can only |
| 3803 | // happen if we're out of order; emit a warning. |
Douglas Gregor | aabdfcb | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 3804 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3805 | Sema::SemaDiagnosticBuilder D = |
| 3806 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 3807 | diag::warn_initializer_out_of_order); |
| 3808 | |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3809 | if (PrevInit->isAnyMemberInitializer()) |
| 3810 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3811 | else |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3812 | D << 1 << PrevInit->getTypeSourceInfo()->getType(); |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3813 | |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3814 | if (Init->isAnyMemberInitializer()) |
| 3815 | D << 0 << Init->getAnyMember()->getDeclName(); |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3816 | else |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3817 | D << 1 << Init->getTypeSourceInfo()->getType(); |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3818 | |
| 3819 | // Move back to the initializer's location in the ideal list. |
| 3820 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3821 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3822 | break; |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3823 | |
| 3824 | assert(IdealIndex != NumIdealInits && |
| 3825 | "initializer not found in initializer list"); |
Fariborz Jahanian | 341583c | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3826 | } |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3827 | |
| 3828 | PrevInit = Init; |
Fariborz Jahanian | 341583c | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3829 | } |
Anders Carlsson | 75fdaa4 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 3830 | } |
| 3831 | |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3832 | namespace { |
| 3833 | bool CheckRedundantInit(Sema &S, |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3834 | CXXCtorInitializer *Init, |
| 3835 | CXXCtorInitializer *&PrevInit) { |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3836 | if (!PrevInit) { |
| 3837 | PrevInit = Init; |
| 3838 | return false; |
| 3839 | } |
| 3840 | |
Douglas Gregor | ea306a1 | 2013-03-25 23:28:23 +0000 | [diff] [blame] | 3841 | if (FieldDecl *Field = Init->getAnyMember()) |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3842 | S.Diag(Init->getSourceLocation(), |
| 3843 | diag::err_multiple_mem_initialization) |
| 3844 | << Field->getDeclName() |
| 3845 | << Init->getSourceRange(); |
| 3846 | else { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3847 | const Type *BaseClass = Init->getBaseClass(); |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3848 | assert(BaseClass && "neither field nor base"); |
| 3849 | S.Diag(Init->getSourceLocation(), |
| 3850 | diag::err_multiple_base_initialization) |
| 3851 | << QualType(BaseClass, 0) |
| 3852 | << Init->getSourceRange(); |
| 3853 | } |
| 3854 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 3855 | << 0 << PrevInit->getSourceRange(); |
| 3856 | |
| 3857 | return true; |
| 3858 | } |
| 3859 | |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3860 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3861 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 3862 | |
| 3863 | bool CheckRedundantUnionInit(Sema &S, |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3864 | CXXCtorInitializer *Init, |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3865 | RedundantUnionMap &Unions) { |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3866 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3867 | RecordDecl *Parent = Field->getParent(); |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3868 | NamedDecl *Child = Field; |
David Blaikie | 0f65d59 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3869 | |
| 3870 | while (Parent->isAnonymousStructOrUnion() || Parent->isUnion()) { |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3871 | if (Parent->isUnion()) { |
| 3872 | UnionEntry &En = Unions[Parent]; |
| 3873 | if (En.first && En.first != Child) { |
| 3874 | S.Diag(Init->getSourceLocation(), |
| 3875 | diag::err_multiple_mem_union_initialization) |
| 3876 | << Field->getDeclName() |
| 3877 | << Init->getSourceRange(); |
| 3878 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 3879 | << 0 << En.second->getSourceRange(); |
| 3880 | return true; |
David Blaikie | 256ee19 | 2011-11-12 20:54:14 +0000 | [diff] [blame] | 3881 | } |
| 3882 | if (!En.first) { |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3883 | En.first = Child; |
| 3884 | En.second = Init; |
| 3885 | } |
David Blaikie | 0f65d59 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3886 | if (!Parent->isAnonymousStructOrUnion()) |
| 3887 | return false; |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | Child = Parent; |
| 3891 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
David Blaikie | 0f65d59 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3892 | } |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3893 | |
| 3894 | return false; |
| 3895 | } |
| 3896 | } |
| 3897 | |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3898 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3899 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3900 | SourceLocation ColonLoc, |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3901 | ArrayRef<CXXCtorInitializer*> MemInits, |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3902 | bool AnyErrors) { |
| 3903 | if (!ConstructorDecl) |
| 3904 | return; |
| 3905 | |
| 3906 | AdjustDeclIfTemplate(ConstructorDecl); |
| 3907 | |
| 3908 | CXXConstructorDecl *Constructor |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3909 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3910 | |
| 3911 | if (!Constructor) { |
| 3912 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 3913 | return; |
| 3914 | } |
| 3915 | |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3916 | // Mapping for the duplicate initializers check. |
| 3917 | // For member initializers, this is keyed with a FieldDecl*. |
| 3918 | // For base initializers, this is keyed with a Type*. |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3919 | llvm::DenseMap<const void *, CXXCtorInitializer *> Members; |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3920 | |
| 3921 | // Mapping for the inconsistent anonymous-union initializers check. |
| 3922 | RedundantUnionMap MemberUnions; |
| 3923 | |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3924 | bool HadError = false; |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3925 | for (unsigned i = 0; i < MemInits.size(); i++) { |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3926 | CXXCtorInitializer *Init = MemInits[i]; |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3927 | |
Abramo Bagnara | 341d783 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 3928 | // Set the source order index. |
| 3929 | Init->setSourceOrder(i); |
| 3930 | |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3931 | if (Init->isAnyMemberInitializer()) { |
| 3932 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3933 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 3934 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 3935 | HadError = true; |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3936 | } else if (Init->isBaseInitializer()) { |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3937 | const void *Key = |
| 3938 | GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3939 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 3940 | HadError = true; |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3941 | } else { |
| 3942 | assert(Init->isDelegatingInitializer()); |
| 3943 | // This must be the only initializer |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3944 | if (MemInits.size() != 1) { |
Richard Smith | 21f06f0 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 3945 | Diag(Init->getSourceLocation(), |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3946 | diag::err_delegating_initializer_alone) |
Richard Smith | 21f06f0 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 3947 | << Init->getSourceRange() << MemInits[i ? 0 : 1]->getSourceRange(); |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3948 | // We will treat this as being the only initializer. |
Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3949 | } |
Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3950 | SetDelegatingInitializer(Constructor, MemInits[i]); |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3951 | // Return immediately as the initializer is set. |
| 3952 | return; |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3953 | } |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3956 | if (HadError) |
| 3957 | return; |
| 3958 | |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3959 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits); |
Anders Carlsson | 4c8cb01 | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 3960 | |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3961 | SetCtorInitializers(Constructor, AnyErrors, MemInits); |
Richard Trieu | 3a446ff | 2013-09-16 21:54:53 +0000 | [diff] [blame] | 3962 | |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 3963 | DiagnoseUninitializedFields(*this, Constructor); |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3966 | void |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3967 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 3968 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 2010404 | 2011-09-18 12:11:43 +0000 | [diff] [blame] | 3969 | // Ignore dependent contexts. Also ignore unions, since their members never |
| 3970 | // have destructors implicitly called. |
| 3971 | if (ClassDecl->isDependentContext() || ClassDecl->isUnion()) |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3972 | return; |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3973 | |
| 3974 | // FIXME: all the access-control diagnostics are positioned on the |
| 3975 | // field/base declaration. That's probably good; that said, the |
| 3976 | // user might reasonably want to know why the destructor is being |
| 3977 | // emitted, and we currently don't say. |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3978 | |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3979 | // Non-static data members. |
| 3980 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 3981 | E = ClassDecl->field_end(); I != E; ++I) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3982 | FieldDecl *Field = *I; |
Fariborz Jahanian | 16f94c6 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 3983 | if (Field->isInvalidDecl()) |
| 3984 | continue; |
Douglas Gregor | 10f939c | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3985 | |
| 3986 | // Don't destroy incomplete or zero-length arrays. |
| 3987 | if (isIncompleteOrZeroLengthArrayType(Context, Field->getType())) |
| 3988 | continue; |
| 3989 | |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3990 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 3991 | |
| 3992 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 3993 | if (!RT) |
| 3994 | continue; |
| 3995 | |
| 3996 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 93615d9 | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3997 | if (FieldClassDecl->isInvalidDecl()) |
| 3998 | continue; |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3999 | if (FieldClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4000 | continue; |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4001 | // The destructor for an implicit anonymous union member is never invoked. |
| 4002 | if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion()) |
| 4003 | continue; |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4004 | |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4005 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
Matt Beaumont-Gay | 93615d9 | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4006 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4007 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4008 | PDiag(diag::err_access_dtor_field) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4009 | << Field->getDeclName() |
| 4010 | << FieldType); |
| 4011 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4012 | MarkFunctionReferenced(Location, Dtor); |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4013 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4016 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 4017 | |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4018 | // Bases. |
| 4019 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4020 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4021 | // Bases are always records in a well-formed non-dependent class. |
| 4022 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 4023 | |
| 4024 | // Remember direct virtual bases. |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4025 | if (Base->isVirtual()) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4026 | DirectVirtualBases.insert(RT); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4027 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4028 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 93615d9 | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4029 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 4030 | if (BaseClassDecl->isInvalidDecl()) |
| 4031 | continue; |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4032 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4033 | continue; |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4034 | |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4035 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 93615d9 | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4036 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4037 | |
| 4038 | // FIXME: caret should be on the start of the class name |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4039 | CheckDestructorAccess(Base->getLocStart(), Dtor, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4040 | PDiag(diag::err_access_dtor_base) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4041 | << Base->getType() |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 4042 | << Base->getSourceRange(), |
| 4043 | Context.getTypeDeclType(ClassDecl)); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4044 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4045 | MarkFunctionReferenced(Location, Dtor); |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4046 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
| 4049 | // Virtual bases. |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 4050 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 4051 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4052 | |
| 4053 | // Bases are always records in a well-formed non-dependent class. |
John McCall | dd1eca3 | 2012-04-09 21:51:56 +0000 | [diff] [blame] | 4054 | const RecordType *RT = VBase->getType()->castAs<RecordType>(); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4055 | |
| 4056 | // Ignore direct virtual bases. |
| 4057 | if (DirectVirtualBases.count(RT)) |
| 4058 | continue; |
| 4059 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4060 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 93615d9 | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4061 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 4062 | if (BaseClassDecl->isInvalidDecl()) |
| 4063 | continue; |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4064 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 4065 | continue; |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4066 | |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4067 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 93615d9 | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 4068 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
David Majnemer | 626032f | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 4069 | if (CheckDestructorAccess( |
| 4070 | ClassDecl->getLocation(), Dtor, |
| 4071 | PDiag(diag::err_access_dtor_vbase) |
| 4072 | << Context.getTypeDeclType(ClassDecl) << VBase->getType(), |
| 4073 | Context.getTypeDeclType(ClassDecl)) == |
| 4074 | AR_accessible) { |
| 4075 | CheckDerivedToBaseConversion( |
| 4076 | Context.getTypeDeclType(ClassDecl), VBase->getType(), |
| 4077 | diag::err_access_dtor_vbase, 0, ClassDecl->getLocation(), |
| 4078 | SourceRange(), DeclarationName(), 0); |
| 4079 | } |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4080 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4081 | MarkFunctionReferenced(Location, Dtor); |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 4082 | DiagnoseUseOfDecl(Dtor, Location); |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 4083 | } |
| 4084 | } |
| 4085 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4086 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 16094c2 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 4087 | if (!CDtorDecl) |
Fariborz Jahanian | 49c8179 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 4088 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4089 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4090 | if (CXXConstructorDecl *Constructor |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 4091 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) { |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4092 | SetCtorInitializers(Constructor, /*AnyErrors=*/false); |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 4093 | DiagnoseUninitializedFields(*this, Constructor); |
| 4094 | } |
Fariborz Jahanian | 49c8179 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4097 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4098 | unsigned DiagID, AbstractDiagSelID SelID) { |
Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4099 | class NonAbstractTypeDiagnoser : public TypeDiagnoser { |
| 4100 | unsigned DiagID; |
| 4101 | AbstractDiagSelID SelID; |
| 4102 | |
| 4103 | public: |
| 4104 | NonAbstractTypeDiagnoser(unsigned DiagID, AbstractDiagSelID SelID) |
| 4105 | : TypeDiagnoser(DiagID == 0), DiagID(DiagID), SelID(SelID) { } |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 4106 | |
| 4107 | void diagnose(Sema &S, SourceLocation Loc, QualType T) LLVM_OVERRIDE { |
Eli Friedman | 1d4c3cf | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 4108 | if (Suppressed) return; |
Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4109 | if (SelID == -1) |
| 4110 | S.Diag(Loc, DiagID) << T; |
| 4111 | else |
| 4112 | S.Diag(Loc, DiagID) << SelID << T; |
| 4113 | } |
| 4114 | } Diagnoser(DiagID, SelID); |
| 4115 | |
| 4116 | return RequireNonAbstractType(Loc, T, Diagnoser); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
Anders Carlsson | eabf770 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 4119 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4120 | TypeDiagnoser &Diagnoser) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4121 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4122 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4123 | |
Anders Carlsson | eb0c532 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 4124 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4125 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4127 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 4128 | // Find the innermost pointer type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4129 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 4130 | PT = T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4131 | |
Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 4132 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4133 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 4134 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4135 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4136 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4137 | if (!RT) |
| 4138 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4139 | |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4140 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4141 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4142 | // We can't answer whether something is abstract until it has a |
| 4143 | // definition. If it's currently being defined, we'll walk back |
| 4144 | // over all the declarations when we have a full definition. |
| 4145 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 4146 | if (!Def || Def->isBeingDefined()) |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4147 | return false; |
| 4148 | |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4149 | if (!RD->isAbstract()) |
| 4150 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4151 | |
Douglas Gregor | ae29842 | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 4152 | Diagnoser.diagnose(*this, Loc, T); |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4153 | DiagnoseAbstractType(RD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4154 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4155 | return true; |
| 4156 | } |
| 4157 | |
| 4158 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 4159 | // Check if we've already emitted the list of pure virtual functions |
| 4160 | // for this class. |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4161 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4162 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4163 | |
Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 4164 | // If the diagnostic is suppressed, don't emit the notes. We're only |
| 4165 | // going to emit them once, so try to attach them to a diagnostic we're |
| 4166 | // actually going to show. |
| 4167 | if (Diags.isLastDiagnosticIgnored()) |
| 4168 | return; |
| 4169 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4170 | CXXFinalOverriderMap FinalOverriders; |
| 4171 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4172 | |
Anders Carlsson | a2f74f3 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 4173 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 4174 | // more than once. |
| 4175 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 4176 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4177 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 4178 | MEnd = FinalOverriders.end(); |
| 4179 | M != MEnd; |
| 4180 | ++M) { |
| 4181 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 4182 | SOEnd = M->second.end(); |
| 4183 | SO != SOEnd; ++SO) { |
| 4184 | // C++ [class.abstract]p4: |
| 4185 | // A class is abstract if it contains or inherits at least one |
| 4186 | // pure virtual function for which the final overrider is pure |
| 4187 | // virtual. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4188 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4189 | // |
| 4190 | if (SO->second.size() != 1) |
| 4191 | continue; |
| 4192 | |
| 4193 | if (!SO->second.front().Method->isPure()) |
| 4194 | continue; |
| 4195 | |
Anders Carlsson | a2f74f3 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 4196 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 4197 | continue; |
| 4198 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4199 | Diag(SO->second.front().Method->getLocation(), |
| 4200 | diag::note_pure_virtual_function) |
Chandler Carruth | 98e3c56 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 4201 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 4202 | } |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
| 4205 | if (!PureVirtualClassDiagSet) |
| 4206 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 4207 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4210 | namespace { |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4211 | struct AbstractUsageInfo { |
| 4212 | Sema &S; |
| 4213 | CXXRecordDecl *Record; |
| 4214 | CanQualType AbstractType; |
| 4215 | bool Invalid; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4216 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4217 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 4218 | : S(S), Record(Record), |
| 4219 | AbstractType(S.Context.getCanonicalType( |
| 4220 | S.Context.getTypeDeclType(Record))), |
| 4221 | Invalid(false) {} |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4222 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4223 | void DiagnoseAbstractType() { |
| 4224 | if (Invalid) return; |
| 4225 | S.DiagnoseAbstractType(Record); |
| 4226 | Invalid = true; |
| 4227 | } |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4228 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4229 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 4230 | }; |
| 4231 | |
| 4232 | struct CheckAbstractUsage { |
| 4233 | AbstractUsageInfo &Info; |
| 4234 | const NamedDecl *Ctx; |
| 4235 | |
| 4236 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 4237 | : Info(Info), Ctx(Ctx) {} |
| 4238 | |
| 4239 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4240 | switch (TL.getTypeLocClass()) { |
| 4241 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 4242 | #define TYPELOC(CLASS, PARENT) \ |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4243 | case TypeLoc::CLASS: Check(TL.castAs<CLASS##TypeLoc>(), Sel); break; |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4244 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4245 | } |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4246 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4247 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4248 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 4249 | Visit(TL.getReturnLoc(), Sema::AbstractReturnType); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 4250 | for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) { |
| 4251 | if (!TL.getParam(I)) |
Douglas Gregor | 385d3fd | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 4252 | continue; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 4253 | |
| 4254 | TypeSourceInfo *TSI = TL.getParam(I)->getTypeSourceInfo(); |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4255 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4256 | } |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4257 | } |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4258 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4259 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4260 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 4261 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4262 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4263 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4264 | // Visit the type parameters from a permissive context. |
| 4265 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 4266 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 4267 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 4268 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 4269 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 4270 | // TODO: other template argument types? |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4271 | } |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4272 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4273 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4274 | // Visit pointee types from a permissive context. |
| 4275 | #define CheckPolymorphic(Type) \ |
| 4276 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 4277 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 4278 | } |
| 4279 | CheckPolymorphic(PointerTypeLoc) |
| 4280 | CheckPolymorphic(ReferenceTypeLoc) |
| 4281 | CheckPolymorphic(MemberPointerTypeLoc) |
| 4282 | CheckPolymorphic(BlockPointerTypeLoc) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4283 | CheckPolymorphic(AtomicTypeLoc) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4284 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4285 | /// Handle all the types we haven't given a more specific |
| 4286 | /// implementation for above. |
| 4287 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4288 | // Every other kind of type that we haven't called out already |
| 4289 | // that has an inner type is either (1) sugar or (2) contains that |
| 4290 | // inner type in some way as a subobject. |
| 4291 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 4292 | return Visit(Next, Sel); |
| 4293 | |
| 4294 | // If there's no inner type and we're in a permissive context, |
| 4295 | // don't diagnose. |
| 4296 | if (Sel == Sema::AbstractNone) return; |
| 4297 | |
| 4298 | // Check whether the type matches the abstract type. |
| 4299 | QualType T = TL.getType(); |
| 4300 | if (T->isArrayType()) { |
| 4301 | Sel = Sema::AbstractArrayType; |
| 4302 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4303 | } |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4304 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 4305 | if (CT != Info.AbstractType) return; |
| 4306 | |
| 4307 | // It matched; do some magic. |
| 4308 | if (Sel == Sema::AbstractArrayType) { |
| 4309 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 4310 | << T << TL.getSourceRange(); |
| 4311 | } else { |
| 4312 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 4313 | << Sel << T << TL.getSourceRange(); |
| 4314 | } |
| 4315 | Info.DiagnoseAbstractType(); |
| 4316 | } |
| 4317 | }; |
| 4318 | |
| 4319 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 4320 | Sema::AbstractDiagSelID Sel) { |
| 4321 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 4322 | } |
| 4323 | |
| 4324 | } |
| 4325 | |
| 4326 | /// Check for invalid uses of an abstract type in a method declaration. |
| 4327 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 4328 | CXXMethodDecl *MD) { |
| 4329 | // No need to do the check on definitions, which require that |
| 4330 | // the return/param types be complete. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 4331 | if (MD->doesThisDeclarationHaveABody()) |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4332 | return; |
| 4333 | |
| 4334 | // For safety's sake, just ignore it if we don't have type source |
| 4335 | // information. This should never happen for non-implicit methods, |
| 4336 | // but... |
| 4337 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 4338 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 4339 | } |
| 4340 | |
| 4341 | /// Check for invalid uses of an abstract type within a class definition. |
| 4342 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 4343 | CXXRecordDecl *RD) { |
| 4344 | for (CXXRecordDecl::decl_iterator |
| 4345 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 4346 | Decl *D = *I; |
| 4347 | if (D->isImplicit()) continue; |
| 4348 | |
| 4349 | // Methods and method templates. |
| 4350 | if (isa<CXXMethodDecl>(D)) { |
| 4351 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 4352 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 4353 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 4354 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 4355 | |
| 4356 | // Fields and static variables. |
| 4357 | } else if (isa<FieldDecl>(D)) { |
| 4358 | FieldDecl *FD = cast<FieldDecl>(D); |
| 4359 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 4360 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 4361 | } else if (isa<VarDecl>(D)) { |
| 4362 | VarDecl *VD = cast<VarDecl>(D); |
| 4363 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 4364 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 4365 | |
| 4366 | // Nested classes and class templates. |
| 4367 | } else if (isa<CXXRecordDecl>(D)) { |
| 4368 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 4369 | } else if (isa<ClassTemplateDecl>(D)) { |
| 4370 | CheckAbstractClassUsage(Info, |
| 4371 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 4372 | } |
| 4373 | } |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4376 | /// \brief Perform semantic checks on a class definition that has been |
| 4377 | /// completing, introducing implicitly-declared members, checking for |
| 4378 | /// abstract types, etc. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4379 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 4380 | if (!Record) |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4381 | return; |
| 4382 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4383 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 4384 | AbstractUsageInfo Info(*this, Record); |
| 4385 | CheckAbstractClassUsage(Info, Record); |
| 4386 | } |
Douglas Gregor | 454a5b6 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4387 | |
| 4388 | // If this is not an aggregate type and has no user-declared constructor, |
| 4389 | // complain about any non-static data members of reference or const scalar |
| 4390 | // type, since they will never get initializers. |
| 4391 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
Douglas Gregor | fbf19a0 | 2012-02-09 02:20:38 +0000 | [diff] [blame] | 4392 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor() && |
| 4393 | !Record->isLambda()) { |
Douglas Gregor | 454a5b6 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4394 | bool Complained = false; |
| 4395 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 4396 | FEnd = Record->field_end(); |
| 4397 | F != FEnd; ++F) { |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 4398 | if (F->hasInClassInitializer() || F->isUnnamedBitfield()) |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4399 | continue; |
| 4400 | |
Douglas Gregor | 454a5b6 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4401 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 659d7fc | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 4402 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 454a5b6 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4403 | if (!Complained) { |
| 4404 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 4405 | << Record->getTagKind() << Record; |
| 4406 | Complained = true; |
| 4407 | } |
| 4408 | |
| 4409 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 4410 | << F->getType()->isReferenceType() |
| 4411 | << F->getDeclName(); |
| 4412 | } |
| 4413 | } |
| 4414 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4415 | |
Anders Carlsson | e771e76 | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 4416 | if (Record->isDynamicClass() && !Record->isDependentType()) |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4417 | DynamicClasses.push_back(Record); |
Douglas Gregor | 36c22a2 | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4418 | |
| 4419 | if (Record->getIdentifier()) { |
| 4420 | // C++ [class.mem]p13: |
| 4421 | // If T is the name of a class, then each of the following shall have a |
| 4422 | // name different from T: |
| 4423 | // - every member of every anonymous union that is a member of class T. |
| 4424 | // |
| 4425 | // C++ [class.mem]p14: |
| 4426 | // In addition, if class T has a user-declared constructor (12.1), every |
| 4427 | // 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] | 4428 | DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
| 4429 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 4430 | ++I) { |
| 4431 | NamedDecl *D = *I; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4432 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 4433 | isa<IndirectFieldDecl>(D)) { |
| 4434 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 4435 | << D->getDeclName(); |
Douglas Gregor | 36c22a2 | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4436 | break; |
| 4437 | } |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4438 | } |
Douglas Gregor | 36c22a2 | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4439 | } |
Argyrios Kyrtzidis | 7f3986d | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4440 | |
Argyrios Kyrtzidis | 33799ca | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 4441 | // Warn if the class has virtual methods but non-virtual public destructor. |
Douglas Gregor | 0cf82f6 | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 4442 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
Argyrios Kyrtzidis | 7f3986d | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4443 | CXXDestructorDecl *dtor = Record->getDestructor(); |
Argyrios Kyrtzidis | 33799ca | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 4444 | if (!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) |
Argyrios Kyrtzidis | 7f3986d | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4445 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 4446 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 4447 | } |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4448 | |
David Majnemer | a543308 | 2013-10-18 00:33:31 +0000 | [diff] [blame] | 4449 | if (Record->isAbstract()) { |
| 4450 | if (FinalAttr *FA = Record->getAttr<FinalAttr>()) { |
| 4451 | Diag(Record->getLocation(), diag::warn_abstract_final_class) |
| 4452 | << FA->isSpelledAsSealed(); |
| 4453 | DiagnoseAbstractType(Record); |
| 4454 | } |
David Blaikie | 348df50 | 2012-09-21 03:21:07 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4457 | if (!Record->isDependentType()) { |
| 4458 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 4459 | MEnd = Record->method_end(); |
| 4460 | M != MEnd; ++M) { |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4461 | // See if a method overloads virtual methods in a base |
| 4462 | // class without overriding any. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4463 | if (!M->isStatic()) |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 4464 | DiagnoseHiddenVirtualMethods(*M); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4465 | |
| 4466 | // Check whether the explicitly-defaulted special members are valid. |
| 4467 | if (!M->isInvalidDecl() && M->isExplicitlyDefaulted()) |
| 4468 | CheckExplicitlyDefaultedSpecialMember(*M); |
| 4469 | |
| 4470 | // For an explicitly defaulted or deleted special member, we defer |
| 4471 | // determining triviality until the class is complete. That time is now! |
| 4472 | if (!M->isImplicit() && !M->isUserProvided()) { |
| 4473 | CXXSpecialMember CSM = getSpecialMember(*M); |
| 4474 | if (CSM != CXXInvalid) { |
| 4475 | M->setTrivial(SpecialMemberIsTrivial(*M, CSM)); |
| 4476 | |
| 4477 | // Inform the class that we've finished declaring this member. |
| 4478 | Record->finishedDefaultedOrDeletedMember(*M); |
| 4479 | } |
| 4480 | } |
| 4481 | } |
Hans Wennborg | e955e39 | 2013-12-17 17:49:22 +0000 | [diff] [blame] | 4482 | |
| 4483 | if (Record->hasUserDeclaredDestructor()) { |
| 4484 | // The Microsoft ABI requires that we perform the destructor body |
| 4485 | // checks (i.e. operator delete() lookup) in any translataion unit, as |
| 4486 | // any translation unit may need to emit a deleting destructor. |
| 4487 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 4488 | !Record->getDestructor()->isDeleted()) |
| 4489 | CheckDestructor(Record->getDestructor()); |
| 4490 | } |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | // C++11 [dcl.constexpr]p8: A constexpr specifier for a non-static member |
| 4494 | // function that is not a constructor declares that member function to be |
| 4495 | // const. [...] The class of which that function is a member shall be |
| 4496 | // a literal type. |
| 4497 | // |
| 4498 | // If the class has virtual bases, any constexpr members will already have |
| 4499 | // been diagnosed by the checks performed on the member declaration, so |
| 4500 | // suppress this (less useful) diagnostic. |
| 4501 | // |
| 4502 | // We delay this until we know whether an explicitly-defaulted (or deleted) |
| 4503 | // destructor for the class is trivial. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4504 | if (LangOpts.CPlusPlus11 && !Record->isDependentType() && |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4505 | !Record->isLiteral() && !Record->getNumVBases()) { |
| 4506 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 4507 | MEnd = Record->method_end(); |
| 4508 | M != MEnd; ++M) { |
| 4509 | if (M->isConstexpr() && M->isInstance() && !isa<CXXConstructorDecl>(*M)) { |
| 4510 | switch (Record->getTemplateSpecializationKind()) { |
| 4511 | case TSK_ImplicitInstantiation: |
| 4512 | case TSK_ExplicitInstantiationDeclaration: |
| 4513 | case TSK_ExplicitInstantiationDefinition: |
| 4514 | // If a template instantiates to a non-literal type, but its members |
| 4515 | // instantiate to constexpr functions, the template is technically |
| 4516 | // ill-formed, but we allow it for sanity. |
| 4517 | continue; |
| 4518 | |
| 4519 | case TSK_Undeclared: |
| 4520 | case TSK_ExplicitSpecialization: |
| 4521 | RequireLiteralType(M->getLocation(), Context.getRecordType(Record), |
| 4522 | diag::err_constexpr_method_non_literal); |
| 4523 | break; |
| 4524 | } |
| 4525 | |
| 4526 | // Only produce one error per class. |
| 4527 | break; |
| 4528 | } |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4529 | } |
| 4530 | } |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4531 | |
Warren Hunt | 8f8bad7 | 2013-10-11 20:19:00 +0000 | [diff] [blame] | 4532 | // Check to see if we're trying to lay out a struct using the ms_struct |
| 4533 | // attribute that is dynamic. |
| 4534 | if (Record->isMsStruct(Context) && Record->isDynamicClass()) { |
| 4535 | Diag(Record->getLocation(), diag::warn_pragma_ms_struct_failed); |
| 4536 | Record->dropAttr<MsStructAttr>(); |
| 4537 | } |
| 4538 | |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4539 | // Declare inheriting constructors. We do this eagerly here because: |
| 4540 | // - The standard requires an eager diagnostic for conflicting inheriting |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4541 | // constructors from different classes. |
| 4542 | // - The lazy declaration of the other implicit constructors is so as to not |
| 4543 | // waste space and performance on classes that are not meant to be |
| 4544 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4545 | // have inheriting constructors. |
| 4546 | DeclareInheritingConstructors(Record); |
Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4549 | /// Look up the special member function that would be called by a special |
| 4550 | /// member function for a subobject of class type. |
| 4551 | /// |
| 4552 | /// \param Class The class type of the subobject. |
| 4553 | /// \param CSM The kind of special member function. |
| 4554 | /// \param FieldQuals If the subobject is a field, its cv-qualifiers. |
| 4555 | /// \param ConstRHS True if this is a copy operation with a const object |
| 4556 | /// on its RHS, that is, if the argument to the outer special member |
| 4557 | /// function is 'const' and this is not a field marked 'mutable'. |
| 4558 | static Sema::SpecialMemberOverloadResult *lookupCallFromSpecialMember( |
| 4559 | Sema &S, CXXRecordDecl *Class, Sema::CXXSpecialMember CSM, |
| 4560 | unsigned FieldQuals, bool ConstRHS) { |
| 4561 | unsigned LHSQuals = 0; |
| 4562 | if (CSM == Sema::CXXCopyAssignment || CSM == Sema::CXXMoveAssignment) |
| 4563 | LHSQuals = FieldQuals; |
| 4564 | |
| 4565 | unsigned RHSQuals = FieldQuals; |
| 4566 | if (CSM == Sema::CXXDefaultConstructor || CSM == Sema::CXXDestructor) |
| 4567 | RHSQuals = 0; |
| 4568 | else if (ConstRHS) |
| 4569 | RHSQuals |= Qualifiers::Const; |
| 4570 | |
| 4571 | return S.LookupSpecialMember(Class, CSM, |
| 4572 | RHSQuals & Qualifiers::Const, |
| 4573 | RHSQuals & Qualifiers::Volatile, |
| 4574 | false, |
| 4575 | LHSQuals & Qualifiers::Const, |
| 4576 | LHSQuals & Qualifiers::Volatile); |
| 4577 | } |
| 4578 | |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4579 | /// Is the special member function which would be selected to perform the |
| 4580 | /// specified operation on the specified class type a constexpr constructor? |
| 4581 | static bool specialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 4582 | Sema::CXXSpecialMember CSM, |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4583 | unsigned Quals, bool ConstRHS) { |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4584 | Sema::SpecialMemberOverloadResult *SMOR = |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4585 | lookupCallFromSpecialMember(S, ClassDecl, CSM, Quals, ConstRHS); |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4586 | if (!SMOR || !SMOR->getMethod()) |
| 4587 | // A constructor we wouldn't select can't be "involved in initializing" |
| 4588 | // anything. |
| 4589 | return true; |
| 4590 | return SMOR->getMethod()->isConstexpr(); |
| 4591 | } |
| 4592 | |
| 4593 | /// Determine whether the specified special member function would be constexpr |
| 4594 | /// if it were implicitly defined. |
| 4595 | static bool defaultedSpecialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 4596 | Sema::CXXSpecialMember CSM, |
| 4597 | bool ConstArg) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4598 | if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4599 | return false; |
| 4600 | |
| 4601 | // C++11 [dcl.constexpr]p4: |
| 4602 | // In the definition of a constexpr constructor [...] |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4603 | bool Ctor = true; |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4604 | switch (CSM) { |
| 4605 | case Sema::CXXDefaultConstructor: |
Richard Smith | 4086a13 | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4606 | // Since default constructor lookup is essentially trivial (and cannot |
| 4607 | // involve, for instance, template instantiation), we compute whether a |
| 4608 | // defaulted default constructor is constexpr directly within CXXRecordDecl. |
| 4609 | // |
| 4610 | // This is important for performance; we need to know whether the default |
| 4611 | // constructor is constexpr to determine whether the type is a literal type. |
| 4612 | return ClassDecl->defaultedDefaultConstructorIsConstexpr(); |
| 4613 | |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4614 | case Sema::CXXCopyConstructor: |
| 4615 | case Sema::CXXMoveConstructor: |
Richard Smith | 4086a13 | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4616 | // For copy or move constructors, we need to perform overload resolution. |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4617 | break; |
| 4618 | |
| 4619 | case Sema::CXXCopyAssignment: |
| 4620 | case Sema::CXXMoveAssignment: |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4621 | if (!S.getLangOpts().CPlusPlus1y) |
| 4622 | return false; |
| 4623 | // In C++1y, we need to perform overload resolution. |
| 4624 | Ctor = false; |
| 4625 | break; |
| 4626 | |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4627 | case Sema::CXXDestructor: |
| 4628 | case Sema::CXXInvalid: |
| 4629 | return false; |
| 4630 | } |
| 4631 | |
| 4632 | // -- if the class is a non-empty union, or for each non-empty anonymous |
| 4633 | // union member of a non-union class, exactly one non-static data member |
| 4634 | // shall be initialized; [DR1359] |
Richard Smith | 4086a13 | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4635 | // |
| 4636 | // If we squint, this is guaranteed, since exactly one non-static data member |
| 4637 | // will be initialized (if the constructor isn't deleted), we just don't know |
| 4638 | // which one. |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4639 | if (Ctor && ClassDecl->isUnion()) |
Richard Smith | 4086a13 | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4640 | return true; |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4641 | |
| 4642 | // -- the class shall not have any virtual base classes; |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4643 | if (Ctor && ClassDecl->getNumVBases()) |
| 4644 | return false; |
| 4645 | |
| 4646 | // C++1y [class.copy]p26: |
| 4647 | // -- [the class] is a literal type, and |
| 4648 | if (!Ctor && !ClassDecl->isLiteral()) |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4649 | return false; |
| 4650 | |
| 4651 | // -- every constructor involved in initializing [...] base class |
| 4652 | // sub-objects shall be a constexpr constructor; |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4653 | // -- the assignment operator selected to copy/move each direct base |
| 4654 | // class is a constexpr function, and |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4655 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4656 | BEnd = ClassDecl->bases_end(); |
| 4657 | B != BEnd; ++B) { |
| 4658 | const RecordType *BaseType = B->getType()->getAs<RecordType>(); |
| 4659 | if (!BaseType) continue; |
| 4660 | |
| 4661 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4662 | if (!specialMemberIsConstexpr(S, BaseClassDecl, CSM, 0, ConstArg)) |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4663 | return false; |
| 4664 | } |
| 4665 | |
| 4666 | // -- every constructor involved in initializing non-static data members |
| 4667 | // [...] shall be a constexpr constructor; |
| 4668 | // -- every non-static data member and base class sub-object shall be |
| 4669 | // initialized |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4670 | // -- 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] | 4671 | // thereof), the assignment operator selected to copy/move that member is |
| 4672 | // a constexpr function |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4673 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4674 | FEnd = ClassDecl->field_end(); |
| 4675 | F != FEnd; ++F) { |
| 4676 | if (F->isInvalidDecl()) |
| 4677 | continue; |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4678 | QualType BaseType = S.Context.getBaseElementType(F->getType()); |
| 4679 | if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4680 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4681 | if (!specialMemberIsConstexpr(S, FieldRecDecl, CSM, |
| 4682 | BaseType.getCVRQualifiers(), |
| 4683 | ConstArg && !F->isMutable())) |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4684 | return false; |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4685 | } |
| 4686 | } |
| 4687 | |
| 4688 | // All OK, it's constexpr! |
| 4689 | return true; |
| 4690 | } |
| 4691 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4692 | static Sema::ImplicitExceptionSpecification |
| 4693 | computeImplicitExceptionSpec(Sema &S, SourceLocation Loc, CXXMethodDecl *MD) { |
| 4694 | switch (S.getSpecialMember(MD)) { |
| 4695 | case Sema::CXXDefaultConstructor: |
| 4696 | return S.ComputeDefaultedDefaultCtorExceptionSpec(Loc, MD); |
| 4697 | case Sema::CXXCopyConstructor: |
| 4698 | return S.ComputeDefaultedCopyCtorExceptionSpec(MD); |
| 4699 | case Sema::CXXCopyAssignment: |
| 4700 | return S.ComputeDefaultedCopyAssignmentExceptionSpec(MD); |
| 4701 | case Sema::CXXMoveConstructor: |
| 4702 | return S.ComputeDefaultedMoveCtorExceptionSpec(MD); |
| 4703 | case Sema::CXXMoveAssignment: |
| 4704 | return S.ComputeDefaultedMoveAssignmentExceptionSpec(MD); |
| 4705 | case Sema::CXXDestructor: |
| 4706 | return S.ComputeDefaultedDtorExceptionSpec(MD); |
| 4707 | case Sema::CXXInvalid: |
| 4708 | break; |
| 4709 | } |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4710 | assert(cast<CXXConstructorDecl>(MD)->getInheritedConstructor() && |
| 4711 | "only special members have implicit exception specs"); |
| 4712 | return S.ComputeInheritingCtorExceptionSpec(cast<CXXConstructorDecl>(MD)); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4713 | } |
| 4714 | |
Richard Smith | 7f78227 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4715 | static void |
| 4716 | updateExceptionSpec(Sema &S, FunctionDecl *FD, const FunctionProtoType *FPT, |
| 4717 | const Sema::ImplicitExceptionSpecification &ExceptSpec) { |
| 4718 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 4719 | ExceptSpec.getEPI(EPI); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4720 | FD->setType(S.Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4721 | FPT->getParamTypes(), EPI)); |
Richard Smith | 7f78227 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4722 | } |
| 4723 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 4724 | static FunctionProtoType::ExtProtoInfo getImplicitMethodEPI(Sema &S, |
| 4725 | CXXMethodDecl *MD) { |
| 4726 | FunctionProtoType::ExtProtoInfo EPI; |
| 4727 | |
| 4728 | // Build an exception specification pointing back at this member. |
| 4729 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 4730 | EPI.ExceptionSpecDecl = MD; |
| 4731 | |
| 4732 | // Set the calling convention to the default for C++ instance methods. |
| 4733 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv( |
| 4734 | S.Context.getDefaultCallingConvention(/*IsVariadic=*/false, |
| 4735 | /*IsCXXMethod=*/true)); |
| 4736 | return EPI; |
| 4737 | } |
| 4738 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4739 | void Sema::EvaluateImplicitExceptionSpec(SourceLocation Loc, CXXMethodDecl *MD) { |
| 4740 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 4741 | if (FPT->getExceptionSpecType() != EST_Unevaluated) |
| 4742 | return; |
| 4743 | |
Richard Smith | 7f78227 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4744 | // Evaluate the exception specification. |
| 4745 | ImplicitExceptionSpecification ExceptSpec = |
| 4746 | computeImplicitExceptionSpec(*this, Loc, MD); |
| 4747 | |
| 4748 | // Update the type of the special member to use it. |
| 4749 | updateExceptionSpec(*this, MD, FPT, ExceptSpec); |
| 4750 | |
| 4751 | // A user-provided destructor can be defined outside the class. When that |
| 4752 | // happens, be sure to update the exception specification on both |
| 4753 | // declarations. |
| 4754 | const FunctionProtoType *CanonicalFPT = |
| 4755 | MD->getCanonicalDecl()->getType()->castAs<FunctionProtoType>(); |
| 4756 | if (CanonicalFPT->getExceptionSpecType() == EST_Unevaluated) |
| 4757 | updateExceptionSpec(*this, MD->getCanonicalDecl(), |
| 4758 | CanonicalFPT, ExceptSpec); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4761 | void Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD) { |
| 4762 | CXXRecordDecl *RD = MD->getParent(); |
| 4763 | CXXSpecialMember CSM = getSpecialMember(MD); |
Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4764 | |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4765 | assert(MD->isExplicitlyDefaulted() && CSM != CXXInvalid && |
| 4766 | "not an explicitly-defaulted special member"); |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4767 | |
| 4768 | // Whether this was the first-declared instance of the constructor. |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4769 | // This affects whether we implicitly add an exception spec and constexpr. |
Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4770 | bool First = MD == MD->getCanonicalDecl(); |
| 4771 | |
| 4772 | bool HadError = false; |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4773 | |
| 4774 | // C++11 [dcl.fct.def.default]p1: |
| 4775 | // A function that is explicitly defaulted shall |
| 4776 | // -- be a special member function (checked elsewhere), |
| 4777 | // -- have the same type (except for ref-qualifiers, and except that a |
| 4778 | // copy operation can take a non-const reference) as an implicit |
| 4779 | // declaration, and |
| 4780 | // -- not have default arguments. |
| 4781 | unsigned ExpectedParams = 1; |
| 4782 | if (CSM == CXXDefaultConstructor || CSM == CXXDestructor) |
| 4783 | ExpectedParams = 0; |
| 4784 | if (MD->getNumParams() != ExpectedParams) { |
| 4785 | // This also checks for default arguments: a copy or move constructor with a |
| 4786 | // default argument is classified as a default constructor, and assignment |
| 4787 | // operations and destructors can't have default arguments. |
| 4788 | Diag(MD->getLocation(), diag::err_defaulted_special_member_params) |
| 4789 | << CSM << MD->getSourceRange(); |
Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4790 | HadError = true; |
Richard Smith | 50d705b | 2012-12-07 02:10:28 +0000 | [diff] [blame] | 4791 | } else if (MD->isVariadic()) { |
| 4792 | Diag(MD->getLocation(), diag::err_defaulted_special_member_variadic) |
| 4793 | << CSM << MD->getSourceRange(); |
| 4794 | HadError = true; |
Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4795 | } |
| 4796 | |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4797 | const FunctionProtoType *Type = MD->getType()->getAs<FunctionProtoType>(); |
Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4798 | |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4799 | bool CanHaveConstParam = false; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4800 | if (CSM == CXXCopyConstructor) |
Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 4801 | CanHaveConstParam = RD->implicitCopyConstructorHasConstParam(); |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4802 | else if (CSM == CXXCopyAssignment) |
Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 4803 | CanHaveConstParam = RD->implicitCopyAssignmentHasConstParam(); |
Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4804 | |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4805 | QualType ReturnType = Context.VoidTy; |
| 4806 | if (CSM == CXXCopyAssignment || CSM == CXXMoveAssignment) { |
| 4807 | // Check for return type matching. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4808 | ReturnType = Type->getReturnType(); |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4809 | QualType ExpectedReturnType = |
| 4810 | Context.getLValueReferenceType(Context.getTypeDeclType(RD)); |
| 4811 | if (!Context.hasSameType(ReturnType, ExpectedReturnType)) { |
| 4812 | Diag(MD->getLocation(), diag::err_defaulted_special_member_return_type) |
| 4813 | << (CSM == CXXMoveAssignment) << ExpectedReturnType; |
| 4814 | HadError = true; |
| 4815 | } |
| 4816 | |
| 4817 | // A defaulted special member cannot have cv-qualifiers. |
| 4818 | if (Type->getTypeQuals()) { |
| 4819 | Diag(MD->getLocation(), diag::err_defaulted_special_member_quals) |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4820 | << (CSM == CXXMoveAssignment) << getLangOpts().CPlusPlus1y; |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4821 | HadError = true; |
| 4822 | } |
| 4823 | } |
| 4824 | |
| 4825 | // Check for parameter type matching. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4826 | QualType ArgType = ExpectedParams ? Type->getParamType(0) : QualType(); |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4827 | bool HasConstParam = false; |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4828 | if (ExpectedParams && ArgType->isReferenceType()) { |
| 4829 | // Argument must be reference to possibly-const T. |
| 4830 | QualType ReferentType = ArgType->getPointeeType(); |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4831 | HasConstParam = ReferentType.isConstQualified(); |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4832 | |
| 4833 | if (ReferentType.isVolatileQualified()) { |
| 4834 | Diag(MD->getLocation(), |
| 4835 | diag::err_defaulted_special_member_volatile_param) << CSM; |
| 4836 | HadError = true; |
| 4837 | } |
| 4838 | |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4839 | if (HasConstParam && !CanHaveConstParam) { |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4840 | if (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment) { |
| 4841 | Diag(MD->getLocation(), |
| 4842 | diag::err_defaulted_special_member_copy_const_param) |
| 4843 | << (CSM == CXXCopyAssignment); |
| 4844 | // FIXME: Explain why this special member can't be const. |
| 4845 | } else { |
| 4846 | Diag(MD->getLocation(), |
| 4847 | diag::err_defaulted_special_member_move_const_param) |
| 4848 | << (CSM == CXXMoveAssignment); |
| 4849 | } |
| 4850 | HadError = true; |
| 4851 | } |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4852 | } else if (ExpectedParams) { |
| 4853 | // A copy assignment operator can take its argument by value, but a |
| 4854 | // defaulted one cannot. |
| 4855 | assert(CSM == CXXCopyAssignment && "unexpected non-ref argument"); |
Alexis Hunt | 604aeb3 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4856 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_not_ref); |
Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4857 | HadError = true; |
| 4858 | } |
Alexis Hunt | 604aeb3 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4859 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4860 | // C++11 [dcl.fct.def.default]p2: |
| 4861 | // An explicitly-defaulted function may be declared constexpr only if it |
| 4862 | // would have been implicitly declared as constexpr, |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4863 | // Do not apply this rule to members of class templates, since core issue 1358 |
| 4864 | // makes such functions always instantiate to constexpr functions. For |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4865 | // functions which cannot be constexpr (for non-constructors in C++11 and for |
| 4866 | // destructors in C++1y), this is checked elsewhere. |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4867 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, RD, CSM, |
| 4868 | HasConstParam); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4869 | if ((getLangOpts().CPlusPlus1y ? !isa<CXXDestructorDecl>(MD) |
| 4870 | : isa<CXXConstructorDecl>(MD)) && |
| 4871 | MD->isConstexpr() && !Constexpr && |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4872 | MD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) { |
| 4873 | Diag(MD->getLocStart(), diag::err_incorrect_defaulted_constexpr) << CSM; |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4874 | // FIXME: Explain why the special member can't be constexpr. |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4875 | HadError = true; |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4876 | } |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4877 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4878 | // and may have an explicit exception-specification only if it is compatible |
| 4879 | // with the exception-specification on the implicit declaration. |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4880 | if (Type->hasExceptionSpec()) { |
| 4881 | // Delay the check if this is the first declaration of the special member, |
| 4882 | // since we may not have parsed some necessary in-class initializers yet. |
Richard Smith | 3901dfe | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 4883 | if (First) { |
| 4884 | // If the exception specification needs to be instantiated, do so now, |
| 4885 | // before we clobber it with an EST_Unevaluated specification below. |
| 4886 | if (Type->getExceptionSpecType() == EST_Uninstantiated) { |
| 4887 | InstantiateExceptionSpec(MD->getLocStart(), MD); |
| 4888 | Type = MD->getType()->getAs<FunctionProtoType>(); |
| 4889 | } |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4890 | DelayedDefaultedMemberExceptionSpecs.push_back(std::make_pair(MD, Type)); |
Richard Smith | 3901dfe | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 4891 | } else |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4892 | CheckExplicitlyDefaultedMemberExceptionSpec(MD, Type); |
| 4893 | } |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4894 | |
| 4895 | // If a function is explicitly defaulted on its first declaration, |
| 4896 | if (First) { |
| 4897 | // -- it is implicitly considered to be constexpr if the implicit |
| 4898 | // definition would be, |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4899 | MD->setConstexpr(Constexpr); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4900 | |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4901 | // -- it is implicitly considered to have the same exception-specification |
| 4902 | // as if it had been implicitly declared, |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4903 | FunctionProtoType::ExtProtoInfo EPI = Type->getExtProtoInfo(); |
| 4904 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 4905 | EPI.ExceptionSpecDecl = MD; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 4906 | MD->setType(Context.getFunctionType(ReturnType, |
| 4907 | ArrayRef<QualType>(&ArgType, |
| 4908 | ExpectedParams), |
| 4909 | EPI)); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4912 | if (ShouldDeleteSpecialMember(MD, CSM)) { |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4913 | if (First) { |
Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 4914 | SetDeclDeleted(MD, MD->getLocation()); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4915 | } else { |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4916 | // C++11 [dcl.fct.def.default]p4: |
| 4917 | // [For a] user-provided explicitly-defaulted function [...] if such a |
| 4918 | // function is implicitly defined as deleted, the program is ill-formed. |
| 4919 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) << CSM; |
Richard Smith | 566184a | 2014-01-22 20:09:10 +0000 | [diff] [blame] | 4920 | ShouldDeleteSpecialMember(MD, CSM, /*Diagnose*/true); |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4921 | HadError = true; |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4922 | } |
| 4923 | } |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4924 | |
Richard Smith | b9e90b1 | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4925 | if (HadError) |
| 4926 | MD->setInvalidDecl(); |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4929 | /// Check whether the exception specification provided for an |
| 4930 | /// explicitly-defaulted special member matches the exception specification |
| 4931 | /// that would have been generated for an implicit special member, per |
| 4932 | /// C++11 [dcl.fct.def.default]p2. |
| 4933 | void Sema::CheckExplicitlyDefaultedMemberExceptionSpec( |
| 4934 | CXXMethodDecl *MD, const FunctionProtoType *SpecifiedType) { |
| 4935 | // Compute the implicit exception specification. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 4936 | CallingConv CC = Context.getDefaultCallingConvention(/*IsVariadic=*/false, |
| 4937 | /*IsCXXMethod=*/true); |
| 4938 | FunctionProtoType::ExtProtoInfo EPI(CC); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4939 | computeImplicitExceptionSpec(*this, MD->getLocation(), MD).getEPI(EPI); |
| 4940 | const FunctionProtoType *ImplicitType = cast<FunctionProtoType>( |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 4941 | Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4942 | |
| 4943 | // Ensure that it matches. |
| 4944 | CheckEquivalentExceptionSpec( |
| 4945 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
| 4946 | << getSpecialMember(MD), PDiag(), |
| 4947 | ImplicitType, SourceLocation(), |
| 4948 | SpecifiedType, MD->getLocation()); |
| 4949 | } |
| 4950 | |
Alp Toker | ae3a944 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 4951 | void Sema::CheckDelayedMemberExceptionSpecs() { |
| 4952 | SmallVector<std::pair<const CXXDestructorDecl *, const CXXDestructorDecl *>, |
| 4953 | 2> Checks; |
| 4954 | SmallVector<std::pair<CXXMethodDecl *, const FunctionProtoType *>, 2> Specs; |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4955 | |
Alp Toker | ae3a944 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 4956 | std::swap(Checks, DelayedDestructorExceptionSpecChecks); |
| 4957 | std::swap(Specs, DelayedDefaultedMemberExceptionSpecs); |
| 4958 | |
| 4959 | // Perform any deferred checking of exception specifications for virtual |
| 4960 | // destructors. |
| 4961 | for (unsigned i = 0, e = Checks.size(); i != e; ++i) { |
| 4962 | const CXXDestructorDecl *Dtor = Checks[i].first; |
| 4963 | assert(!Dtor->getParent()->isDependentType() && |
| 4964 | "Should not ever add destructors of templates into the list."); |
| 4965 | CheckOverridingFunctionExceptionSpec(Dtor, Checks[i].second); |
| 4966 | } |
| 4967 | |
| 4968 | // Check that any explicitly-defaulted methods have exception specifications |
| 4969 | // compatible with their implicit exception specifications. |
| 4970 | for (unsigned I = 0, N = Specs.size(); I != N; ++I) |
| 4971 | CheckExplicitlyDefaultedMemberExceptionSpec(Specs[I].first, |
| 4972 | Specs[I].second); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4975 | namespace { |
| 4976 | struct SpecialMemberDeletionInfo { |
| 4977 | Sema &S; |
| 4978 | CXXMethodDecl *MD; |
| 4979 | Sema::CXXSpecialMember CSM; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4980 | bool Diagnose; |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4981 | |
| 4982 | // Properties of the special member, computed for convenience. |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4983 | bool IsConstructor, IsAssignment, IsMove, ConstArg; |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4984 | SourceLocation Loc; |
| 4985 | |
| 4986 | bool AllFieldsAreConst; |
| 4987 | |
| 4988 | SpecialMemberDeletionInfo(Sema &S, CXXMethodDecl *MD, |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4989 | Sema::CXXSpecialMember CSM, bool Diagnose) |
| 4990 | : S(S), MD(MD), CSM(CSM), Diagnose(Diagnose), |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4991 | IsConstructor(false), IsAssignment(false), IsMove(false), |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 4992 | ConstArg(false), Loc(MD->getLocation()), |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4993 | AllFieldsAreConst(true) { |
| 4994 | switch (CSM) { |
| 4995 | case Sema::CXXDefaultConstructor: |
| 4996 | case Sema::CXXCopyConstructor: |
| 4997 | IsConstructor = true; |
| 4998 | break; |
| 4999 | case Sema::CXXMoveConstructor: |
| 5000 | IsConstructor = true; |
| 5001 | IsMove = true; |
| 5002 | break; |
| 5003 | case Sema::CXXCopyAssignment: |
| 5004 | IsAssignment = true; |
| 5005 | break; |
| 5006 | case Sema::CXXMoveAssignment: |
| 5007 | IsAssignment = true; |
| 5008 | IsMove = true; |
| 5009 | break; |
| 5010 | case Sema::CXXDestructor: |
| 5011 | break; |
| 5012 | case Sema::CXXInvalid: |
| 5013 | llvm_unreachable("invalid special member kind"); |
| 5014 | } |
| 5015 | |
| 5016 | if (MD->getNumParams()) { |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5017 | if (const ReferenceType *RT = |
| 5018 | MD->getParamDecl(0)->getType()->getAs<ReferenceType>()) |
| 5019 | ConstArg = RT->getPointeeType().isConstQualified(); |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5020 | } |
| 5021 | } |
| 5022 | |
| 5023 | bool inUnion() const { return MD->getParent()->isUnion(); } |
| 5024 | |
| 5025 | /// Look up the corresponding special member in the given class. |
Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5026 | Sema::SpecialMemberOverloadResult *lookupIn(CXXRecordDecl *Class, |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5027 | unsigned Quals, bool IsMutable) { |
| 5028 | return lookupCallFromSpecialMember(S, Class, CSM, Quals, |
| 5029 | ConstArg && !IsMutable); |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5032 | typedef llvm::PointerUnion<CXXBaseSpecifier*, FieldDecl*> Subobject; |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5033 | |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5034 | bool shouldDeleteForBase(CXXBaseSpecifier *Base); |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5035 | bool shouldDeleteForField(FieldDecl *FD); |
| 5036 | bool shouldDeleteForAllConstMembers(); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5037 | |
Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5038 | bool shouldDeleteForClassSubobject(CXXRecordDecl *Class, Subobject Subobj, |
| 5039 | unsigned Quals); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5040 | bool shouldDeleteForSubobjectCall(Subobject Subobj, |
| 5041 | Sema::SpecialMemberOverloadResult *SMOR, |
| 5042 | bool IsDtorCallInCtor); |
John McCall | d427421 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 5043 | |
| 5044 | bool isAccessible(Subobject Subobj, CXXMethodDecl *D); |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5045 | }; |
| 5046 | } |
| 5047 | |
John McCall | d427421 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 5048 | /// Is the given special member inaccessible when used on the given |
| 5049 | /// sub-object. |
| 5050 | bool SpecialMemberDeletionInfo::isAccessible(Subobject Subobj, |
| 5051 | CXXMethodDecl *target) { |
| 5052 | /// If we're operating on a base class, the object type is the |
| 5053 | /// type of this special member. |
| 5054 | QualType objectTy; |
Dmitri Gribenko | 76bb5cabfa | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 5055 | AccessSpecifier access = target->getAccess(); |
John McCall | d427421 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 5056 | if (CXXBaseSpecifier *base = Subobj.dyn_cast<CXXBaseSpecifier*>()) { |
| 5057 | objectTy = S.Context.getTypeDeclType(MD->getParent()); |
| 5058 | access = CXXRecordDecl::MergeAccess(base->getAccessSpecifier(), access); |
| 5059 | |
| 5060 | // If we're operating on a field, the object type is the type of the field. |
| 5061 | } else { |
| 5062 | objectTy = S.Context.getTypeDeclType(target->getParent()); |
| 5063 | } |
| 5064 | |
| 5065 | return S.isSpecialMemberAccessibleForDeletion(target, access, objectTy); |
| 5066 | } |
| 5067 | |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5068 | /// Check whether we should delete a special member due to the implicit |
| 5069 | /// definition containing a call to a special member of a subobject. |
| 5070 | bool SpecialMemberDeletionInfo::shouldDeleteForSubobjectCall( |
| 5071 | Subobject Subobj, Sema::SpecialMemberOverloadResult *SMOR, |
| 5072 | bool IsDtorCallInCtor) { |
| 5073 | CXXMethodDecl *Decl = SMOR->getMethod(); |
| 5074 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
| 5075 | |
| 5076 | int DiagKind = -1; |
| 5077 | |
| 5078 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::NoMemberOrDeleted) |
| 5079 | DiagKind = !Decl ? 0 : 1; |
| 5080 | else if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 5081 | DiagKind = 2; |
John McCall | d427421 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 5082 | else if (!isAccessible(Subobj, Decl)) |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5083 | DiagKind = 3; |
| 5084 | else if (!IsDtorCallInCtor && Field && Field->getParent()->isUnion() && |
| 5085 | !Decl->isTrivial()) { |
| 5086 | // A member of a union must have a trivial corresponding special member. |
| 5087 | // As a weird special case, a destructor call from a union's constructor |
| 5088 | // must be accessible and non-deleted, but need not be trivial. Such a |
| 5089 | // destructor is never actually called, but is semantically checked as |
| 5090 | // if it were. |
| 5091 | DiagKind = 4; |
| 5092 | } |
| 5093 | |
| 5094 | if (DiagKind == -1) |
| 5095 | return false; |
| 5096 | |
| 5097 | if (Diagnose) { |
| 5098 | if (Field) { |
| 5099 | S.Diag(Field->getLocation(), |
| 5100 | diag::note_deleted_special_member_class_subobject) |
| 5101 | << CSM << MD->getParent() << /*IsField*/true |
| 5102 | << Field << DiagKind << IsDtorCallInCtor; |
| 5103 | } else { |
| 5104 | CXXBaseSpecifier *Base = Subobj.get<CXXBaseSpecifier*>(); |
| 5105 | S.Diag(Base->getLocStart(), |
| 5106 | diag::note_deleted_special_member_class_subobject) |
| 5107 | << CSM << MD->getParent() << /*IsField*/false |
| 5108 | << Base->getType() << DiagKind << IsDtorCallInCtor; |
| 5109 | } |
| 5110 | |
| 5111 | if (DiagKind == 1) |
| 5112 | S.NoteDeletedFunction(Decl); |
| 5113 | // FIXME: Explain inaccessibility if DiagKind == 3. |
| 5114 | } |
| 5115 | |
| 5116 | return true; |
| 5117 | } |
| 5118 | |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5119 | /// 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] | 5120 | /// 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] | 5121 | bool SpecialMemberDeletionInfo::shouldDeleteForClassSubobject( |
Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5122 | CXXRecordDecl *Class, Subobject Subobj, unsigned Quals) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5123 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5124 | bool IsMutable = Field && Field->isMutable(); |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5125 | |
| 5126 | // C++11 [class.ctor]p5: |
Richard Smith | 5704fe8 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 5127 | // -- any direct or virtual base class, or non-static data member with no |
| 5128 | // brace-or-equal-initializer, has class type M (or array thereof) and |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5129 | // either M has no default constructor or overload resolution as applied |
| 5130 | // to M's default constructor results in an ambiguity or in a function |
| 5131 | // that is deleted or inaccessible |
| 5132 | // C++11 [class.copy]p11, C++11 [class.copy]p23: |
| 5133 | // -- a direct or virtual base class B that cannot be copied/moved because |
| 5134 | // overload resolution, as applied to B's corresponding special member, |
| 5135 | // results in an ambiguity or a function that is deleted or inaccessible |
| 5136 | // from the defaulted special member |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5137 | // C++11 [class.dtor]p5: |
| 5138 | // -- any direct or virtual base class [...] has a type with a destructor |
| 5139 | // that is deleted or inaccessible |
| 5140 | if (!(CSM == Sema::CXXDefaultConstructor && |
Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5141 | Field && Field->hasInClassInitializer()) && |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5142 | shouldDeleteForSubobjectCall(Subobj, lookupIn(Class, Quals, IsMutable), |
| 5143 | false)) |
Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5144 | return true; |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5145 | |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5146 | // C++11 [class.ctor]p5, C++11 [class.copy]p11: |
| 5147 | // -- any direct or virtual base class or non-static data member has a |
| 5148 | // type with a destructor that is deleted or inaccessible |
| 5149 | if (IsConstructor) { |
| 5150 | Sema::SpecialMemberOverloadResult *SMOR = |
| 5151 | S.LookupSpecialMember(Class, Sema::CXXDestructor, |
| 5152 | false, false, false, false, false); |
| 5153 | if (shouldDeleteForSubobjectCall(Subobj, SMOR, true)) |
| 5154 | return true; |
| 5155 | } |
| 5156 | |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5157 | return false; |
| 5158 | } |
| 5159 | |
| 5160 | /// Check whether we should delete a special member function due to the class |
| 5161 | /// having a particular direct or virtual base class. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5162 | bool SpecialMemberDeletionInfo::shouldDeleteForBase(CXXBaseSpecifier *Base) { |
Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5163 | CXXRecordDecl *BaseClass = Base->getType()->getAsCXXRecordDecl(); |
Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5164 | return shouldDeleteForClassSubobject(BaseClass, Base, 0); |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
| 5167 | /// Check whether we should delete a special member function due to the class |
| 5168 | /// having a particular non-static data member. |
| 5169 | bool SpecialMemberDeletionInfo::shouldDeleteForField(FieldDecl *FD) { |
| 5170 | QualType FieldType = S.Context.getBaseElementType(FD->getType()); |
| 5171 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 5172 | |
| 5173 | if (CSM == Sema::CXXDefaultConstructor) { |
| 5174 | // For a default constructor, all references must be initialized in-class |
| 5175 | // and, if a union, it must have a non-const member. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5176 | if (FieldType->isReferenceType() && !FD->hasInClassInitializer()) { |
| 5177 | if (Diagnose) |
| 5178 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
| 5179 | << MD->getParent() << FD << FieldType << /*Reference*/0; |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5180 | return true; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5181 | } |
Richard Smith | 619ecdc | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 5182 | // C++11 [class.ctor]p5: any non-variant non-static data member of |
| 5183 | // const-qualified type (or array thereof) with no |
| 5184 | // brace-or-equal-initializer does not have a user-provided default |
| 5185 | // constructor. |
| 5186 | if (!inUnion() && FieldType.isConstQualified() && |
| 5187 | !FD->hasInClassInitializer() && |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5188 | (!FieldRecord || !FieldRecord->hasUserProvidedDefaultConstructor())) { |
| 5189 | if (Diagnose) |
| 5190 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
Richard Smith | c5f98f3 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 5191 | << MD->getParent() << FD << FD->getType() << /*Const*/1; |
Richard Smith | 619ecdc | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 5192 | return true; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5193 | } |
| 5194 | |
| 5195 | if (inUnion() && !FieldType.isConstQualified()) |
| 5196 | AllFieldsAreConst = false; |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5197 | } else if (CSM == Sema::CXXCopyConstructor) { |
| 5198 | // For a copy constructor, data members must not be of rvalue reference |
| 5199 | // type. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5200 | if (FieldType->isRValueReferenceType()) { |
| 5201 | if (Diagnose) |
| 5202 | S.Diag(FD->getLocation(), diag::note_deleted_copy_ctor_rvalue_reference) |
| 5203 | << MD->getParent() << FD << FieldType; |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5204 | return true; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5205 | } |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5206 | } else if (IsAssignment) { |
| 5207 | // For an assignment operator, data members must not be of reference type. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5208 | if (FieldType->isReferenceType()) { |
| 5209 | if (Diagnose) |
| 5210 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
| 5211 | << IsMove << MD->getParent() << FD << FieldType << /*Reference*/0; |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5212 | return true; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5213 | } |
| 5214 | if (!FieldRecord && FieldType.isConstQualified()) { |
| 5215 | // C++11 [class.copy]p23: |
| 5216 | // -- a non-static data member of const non-class type (or array thereof) |
| 5217 | if (Diagnose) |
| 5218 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
Richard Smith | c5f98f3 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 5219 | << IsMove << MD->getParent() << FD << FD->getType() << /*Const*/1; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5220 | return true; |
| 5221 | } |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
| 5224 | if (FieldRecord) { |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5225 | // Some additional restrictions exist on the variant members. |
| 5226 | if (!inUnion() && FieldRecord->isUnion() && |
| 5227 | FieldRecord->isAnonymousStructOrUnion()) { |
| 5228 | bool AllVariantFieldsAreConst = true; |
| 5229 | |
Richard Smith | 5704fe8 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 5230 | // FIXME: Handle anonymous unions declared within anonymous unions. |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5231 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 5232 | UE = FieldRecord->field_end(); |
| 5233 | UI != UE; ++UI) { |
| 5234 | QualType UnionFieldType = S.Context.getBaseElementType(UI->getType()); |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5235 | |
| 5236 | if (!UnionFieldType.isConstQualified()) |
| 5237 | AllVariantFieldsAreConst = false; |
| 5238 | |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5239 | CXXRecordDecl *UnionFieldRecord = UnionFieldType->getAsCXXRecordDecl(); |
| 5240 | if (UnionFieldRecord && |
Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5241 | shouldDeleteForClassSubobject(UnionFieldRecord, *UI, |
| 5242 | UnionFieldType.getCVRQualifiers())) |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5243 | return true; |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5244 | } |
| 5245 | |
| 5246 | // At least one member in each anonymous union must be non-const |
Douglas Gregor | 232ee49 | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 5247 | if (CSM == Sema::CXXDefaultConstructor && AllVariantFieldsAreConst && |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5248 | FieldRecord->field_begin() != FieldRecord->field_end()) { |
| 5249 | if (Diagnose) |
| 5250 | S.Diag(FieldRecord->getLocation(), |
| 5251 | diag::note_deleted_default_ctor_all_const) |
| 5252 | << MD->getParent() << /*anonymous union*/1; |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5253 | return true; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5254 | } |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5255 | |
Richard Smith | 5704fe8 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 5256 | // Don't check the implicit member of the anonymous union type. |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5257 | // This is technically non-conformant, but sanity demands it. |
| 5258 | return false; |
| 5259 | } |
| 5260 | |
Richard Smith | af136f8 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 5261 | if (shouldDeleteForClassSubobject(FieldRecord, FD, |
| 5262 | FieldType.getCVRQualifiers())) |
Richard Smith | 5704fe8 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 5263 | return true; |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5264 | } |
| 5265 | |
| 5266 | return false; |
| 5267 | } |
| 5268 | |
| 5269 | /// C++11 [class.ctor] p5: |
| 5270 | /// A defaulted default constructor for a class X is defined as deleted if |
| 5271 | /// X is a union and all of its variant members are of const-qualified type. |
| 5272 | bool SpecialMemberDeletionInfo::shouldDeleteForAllConstMembers() { |
Douglas Gregor | 232ee49 | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 5273 | // This is a silly definition, because it gives an empty union a deleted |
| 5274 | // default constructor. Don't do that. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5275 | if (CSM == Sema::CXXDefaultConstructor && inUnion() && AllFieldsAreConst && |
| 5276 | (MD->getParent()->field_begin() != MD->getParent()->field_end())) { |
| 5277 | if (Diagnose) |
| 5278 | S.Diag(MD->getParent()->getLocation(), |
| 5279 | diag::note_deleted_default_ctor_all_const) |
| 5280 | << MD->getParent() << /*not anonymous union*/0; |
| 5281 | return true; |
| 5282 | } |
| 5283 | return false; |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | /// Determine whether a defaulted special member function should be defined as |
| 5287 | /// deleted, as specified in C++11 [class.ctor]p5, C++11 [class.copy]p11, |
| 5288 | /// C++11 [class.copy]p23, and C++11 [class.dtor]p5. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5289 | bool Sema::ShouldDeleteSpecialMember(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 5290 | bool Diagnose) { |
Richard Smith | f716bb8 | 2012-08-06 02:25:10 +0000 | [diff] [blame] | 5291 | if (MD->isInvalidDecl()) |
| 5292 | return false; |
Alexis Hunt | d6da876 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 5293 | CXXRecordDecl *RD = MD->getParent(); |
Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5294 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5295 | if (!LangOpts.CPlusPlus11 || RD->isInvalidDecl()) |
Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5296 | return false; |
| 5297 | |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5298 | // C++11 [expr.lambda.prim]p19: |
| 5299 | // The closure type associated with a lambda-expression has a |
| 5300 | // deleted (8.4.3) default constructor and a deleted copy |
| 5301 | // assignment operator. |
| 5302 | if (RD->isLambda() && |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5303 | (CSM == CXXDefaultConstructor || CSM == CXXCopyAssignment)) { |
| 5304 | if (Diagnose) |
| 5305 | Diag(RD->getLocation(), diag::note_lambda_decl); |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5306 | return true; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5307 | } |
| 5308 | |
Richard Smith | 6f1e2c6 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 5309 | // For an anonymous struct or union, the copy and assignment special members |
| 5310 | // will never be used, so skip the check. For an anonymous union declared at |
| 5311 | // namespace scope, the constructor and destructor are used. |
| 5312 | if (CSM != CXXDefaultConstructor && CSM != CXXDestructor && |
| 5313 | RD->isAnonymousStructOrUnion()) |
| 5314 | return false; |
| 5315 | |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5316 | // C++11 [class.copy]p7, p18: |
| 5317 | // If the class definition declares a move constructor or move assignment |
| 5318 | // operator, an implicitly declared copy constructor or copy assignment |
| 5319 | // operator is defined as deleted. |
| 5320 | if (MD->isImplicit() && |
| 5321 | (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment)) { |
| 5322 | CXXMethodDecl *UserDeclaredMove = 0; |
| 5323 | |
| 5324 | // In Microsoft mode, a user-declared move only causes the deletion of the |
| 5325 | // corresponding copy operation, not both copy operations. |
| 5326 | if (RD->hasUserDeclaredMoveConstructor() && |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 5327 | (!getLangOpts().MSVCCompat || CSM == CXXCopyConstructor)) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5328 | if (!Diagnose) return true; |
Richard Smith | 1a2532b | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5329 | |
| 5330 | // Find any user-declared move constructor. |
| 5331 | for (CXXRecordDecl::ctor_iterator I = RD->ctor_begin(), |
| 5332 | E = RD->ctor_end(); I != E; ++I) { |
| 5333 | if (I->isMoveConstructor()) { |
| 5334 | UserDeclaredMove = *I; |
| 5335 | break; |
| 5336 | } |
| 5337 | } |
Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5338 | assert(UserDeclaredMove); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5339 | } else if (RD->hasUserDeclaredMoveAssignment() && |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 5340 | (!getLangOpts().MSVCCompat || CSM == CXXCopyAssignment)) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5341 | if (!Diagnose) return true; |
Richard Smith | 1a2532b | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5342 | |
| 5343 | // Find any user-declared move assignment operator. |
| 5344 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 5345 | E = RD->method_end(); I != E; ++I) { |
| 5346 | if (I->isMoveAssignmentOperator()) { |
| 5347 | UserDeclaredMove = *I; |
| 5348 | break; |
| 5349 | } |
| 5350 | } |
Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5351 | assert(UserDeclaredMove); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5352 | } |
| 5353 | |
| 5354 | if (UserDeclaredMove) { |
| 5355 | Diag(UserDeclaredMove->getLocation(), |
| 5356 | diag::note_deleted_copy_user_declared_move) |
Richard Smith | f989e51 | 2012-04-02 21:07:48 +0000 | [diff] [blame] | 5357 | << (CSM == CXXCopyAssignment) << RD |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5358 | << UserDeclaredMove->isMoveAssignmentOperator(); |
| 5359 | return true; |
| 5360 | } |
| 5361 | } |
Alexis Hunt | d6da876 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 5362 | |
Richard Smith | 6f1e2c6 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 5363 | // Do access control from the special member function |
| 5364 | ContextRAII MethodContext(*this, MD); |
| 5365 | |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5366 | // C++11 [class.dtor]p5: |
| 5367 | // -- for a virtual destructor, lookup of the non-array deallocation function |
| 5368 | // 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] | 5369 | if (CSM == CXXDestructor && MD->isVirtual()) { |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5370 | FunctionDecl *OperatorDelete = 0; |
| 5371 | DeclarationName Name = |
| 5372 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| 5373 | if (FindDeallocationFunction(MD->getLocation(), MD->getParent(), Name, |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5374 | OperatorDelete, false)) { |
| 5375 | if (Diagnose) |
| 5376 | Diag(RD->getLocation(), diag::note_deleted_dtor_no_operator_delete); |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5377 | return true; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5378 | } |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5381 | SpecialMemberDeletionInfo SMI(*this, MD, CSM, Diagnose); |
Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5382 | |
Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5383 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5384 | BE = RD->bases_end(); BI != BE; ++BI) |
| 5385 | if (!BI->isVirtual() && |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5386 | SMI.shouldDeleteForBase(BI)) |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5387 | return true; |
Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5388 | |
Richard Smith | d162703 | 2013-07-22 18:06:23 +0000 | [diff] [blame] | 5389 | // Per DR1611, do not consider virtual bases of constructors of abstract |
| 5390 | // classes, since we are not going to construct them. |
Richard Smith | bc46e43 | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 5391 | if (!RD->isAbstract() || !SMI.IsConstructor) { |
| 5392 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 5393 | BE = RD->vbases_end(); |
| 5394 | BI != BE; ++BI) |
| 5395 | if (SMI.shouldDeleteForBase(BI)) |
| 5396 | return true; |
| 5397 | } |
Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5398 | |
| 5399 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5400 | FE = RD->field_end(); FI != FE; ++FI) |
| 5401 | if (!FI->isInvalidDecl() && !FI->isUnnamedBitfield() && |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5402 | SMI.shouldDeleteForField(*FI)) |
Alexis Hunt | a671bca | 2011-05-20 21:43:47 +0000 | [diff] [blame] | 5403 | return true; |
Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5404 | |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5405 | if (SMI.shouldDeleteForAllConstMembers()) |
Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5406 | return true; |
| 5407 | |
| 5408 | return false; |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5409 | } |
| 5410 | |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5411 | /// Perform lookup for a special member of the specified kind, and determine |
| 5412 | /// whether it is trivial. If the triviality can be determined without the |
| 5413 | /// lookup, skip it. This is intended for use when determining whether a |
| 5414 | /// special member of a containing object is trivial, and thus does not ever |
| 5415 | /// perform overload resolution for default constructors. |
| 5416 | /// |
| 5417 | /// If \p Selected is not \c NULL, \c *Selected will be filled in with the |
| 5418 | /// member that was most likely to be intended to be trivial, if any. |
| 5419 | static bool findTrivialSpecialMember(Sema &S, CXXRecordDecl *RD, |
| 5420 | Sema::CXXSpecialMember CSM, unsigned Quals, |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5421 | bool ConstRHS, CXXMethodDecl **Selected) { |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5422 | if (Selected) |
| 5423 | *Selected = 0; |
| 5424 | |
| 5425 | switch (CSM) { |
| 5426 | case Sema::CXXInvalid: |
| 5427 | llvm_unreachable("not a special member"); |
| 5428 | |
| 5429 | case Sema::CXXDefaultConstructor: |
| 5430 | // C++11 [class.ctor]p5: |
| 5431 | // A default constructor is trivial if: |
| 5432 | // - all the [direct subobjects] have trivial default constructors |
| 5433 | // |
| 5434 | // Note, no overload resolution is performed in this case. |
| 5435 | if (RD->hasTrivialDefaultConstructor()) |
| 5436 | return true; |
| 5437 | |
| 5438 | if (Selected) { |
| 5439 | // If there's a default constructor which could have been trivial, dig it |
| 5440 | // out. Otherwise, if there's any user-provided default constructor, point |
| 5441 | // to that as an example of why there's not a trivial one. |
| 5442 | CXXConstructorDecl *DefCtor = 0; |
| 5443 | if (RD->needsImplicitDefaultConstructor()) |
| 5444 | S.DeclareImplicitDefaultConstructor(RD); |
| 5445 | for (CXXRecordDecl::ctor_iterator CI = RD->ctor_begin(), |
| 5446 | CE = RD->ctor_end(); CI != CE; ++CI) { |
| 5447 | if (!CI->isDefaultConstructor()) |
| 5448 | continue; |
| 5449 | DefCtor = *CI; |
| 5450 | if (!DefCtor->isUserProvided()) |
| 5451 | break; |
| 5452 | } |
| 5453 | |
| 5454 | *Selected = DefCtor; |
| 5455 | } |
| 5456 | |
| 5457 | return false; |
| 5458 | |
| 5459 | case Sema::CXXDestructor: |
| 5460 | // C++11 [class.dtor]p5: |
| 5461 | // A destructor is trivial if: |
| 5462 | // - all the direct [subobjects] have trivial destructors |
| 5463 | if (RD->hasTrivialDestructor()) |
| 5464 | return true; |
| 5465 | |
| 5466 | if (Selected) { |
| 5467 | if (RD->needsImplicitDestructor()) |
| 5468 | S.DeclareImplicitDestructor(RD); |
| 5469 | *Selected = RD->getDestructor(); |
| 5470 | } |
| 5471 | |
| 5472 | return false; |
| 5473 | |
| 5474 | case Sema::CXXCopyConstructor: |
| 5475 | // C++11 [class.copy]p12: |
| 5476 | // A copy constructor is trivial if: |
| 5477 | // - the constructor selected to copy each direct [subobject] is trivial |
| 5478 | if (RD->hasTrivialCopyConstructor()) { |
| 5479 | if (Quals == Qualifiers::Const) |
| 5480 | // We must either select the trivial copy constructor or reach an |
| 5481 | // ambiguity; no need to actually perform overload resolution. |
| 5482 | return true; |
| 5483 | } else if (!Selected) { |
| 5484 | return false; |
| 5485 | } |
| 5486 | // In C++98, we are not supposed to perform overload resolution here, but we |
| 5487 | // treat that as a language defect, as suggested on cxx-abi-dev, to treat |
| 5488 | // cases like B as having a non-trivial copy constructor: |
| 5489 | // struct A { template<typename T> A(T&); }; |
| 5490 | // struct B { mutable A a; }; |
| 5491 | goto NeedOverloadResolution; |
| 5492 | |
| 5493 | case Sema::CXXCopyAssignment: |
| 5494 | // C++11 [class.copy]p25: |
| 5495 | // A copy assignment operator is trivial if: |
| 5496 | // - the assignment operator selected to copy each direct [subobject] is |
| 5497 | // trivial |
| 5498 | if (RD->hasTrivialCopyAssignment()) { |
| 5499 | if (Quals == Qualifiers::Const) |
| 5500 | return true; |
| 5501 | } else if (!Selected) { |
| 5502 | return false; |
| 5503 | } |
| 5504 | // In C++98, we are not supposed to perform overload resolution here, but we |
| 5505 | // treat that as a language defect. |
| 5506 | goto NeedOverloadResolution; |
| 5507 | |
| 5508 | case Sema::CXXMoveConstructor: |
| 5509 | case Sema::CXXMoveAssignment: |
| 5510 | NeedOverloadResolution: |
| 5511 | Sema::SpecialMemberOverloadResult *SMOR = |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5512 | lookupCallFromSpecialMember(S, RD, CSM, Quals, ConstRHS); |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5513 | |
| 5514 | // The standard doesn't describe how to behave if the lookup is ambiguous. |
| 5515 | // We treat it as not making the member non-trivial, just like the standard |
| 5516 | // mandates for the default constructor. This should rarely matter, because |
| 5517 | // the member will also be deleted. |
| 5518 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 5519 | return true; |
| 5520 | |
| 5521 | if (!SMOR->getMethod()) { |
| 5522 | assert(SMOR->getKind() == |
| 5523 | Sema::SpecialMemberOverloadResult::NoMemberOrDeleted); |
| 5524 | return false; |
| 5525 | } |
| 5526 | |
| 5527 | // We deliberately don't check if we found a deleted special member. We're |
| 5528 | // not supposed to! |
| 5529 | if (Selected) |
| 5530 | *Selected = SMOR->getMethod(); |
| 5531 | return SMOR->getMethod()->isTrivial(); |
| 5532 | } |
| 5533 | |
| 5534 | llvm_unreachable("unknown special method kind"); |
| 5535 | } |
| 5536 | |
Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 5537 | static CXXConstructorDecl *findUserDeclaredCtor(CXXRecordDecl *RD) { |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5538 | for (CXXRecordDecl::ctor_iterator CI = RD->ctor_begin(), CE = RD->ctor_end(); |
| 5539 | CI != CE; ++CI) |
| 5540 | if (!CI->isImplicit()) |
| 5541 | return *CI; |
| 5542 | |
| 5543 | // Look for constructor templates. |
| 5544 | typedef CXXRecordDecl::specific_decl_iterator<FunctionTemplateDecl> tmpl_iter; |
| 5545 | for (tmpl_iter TI(RD->decls_begin()), TE(RD->decls_end()); TI != TE; ++TI) { |
| 5546 | if (CXXConstructorDecl *CD = |
| 5547 | dyn_cast<CXXConstructorDecl>(TI->getTemplatedDecl())) |
| 5548 | return CD; |
| 5549 | } |
| 5550 | |
| 5551 | return 0; |
| 5552 | } |
| 5553 | |
| 5554 | /// The kind of subobject we are checking for triviality. The values of this |
| 5555 | /// enumeration are used in diagnostics. |
| 5556 | enum TrivialSubobjectKind { |
| 5557 | /// The subobject is a base class. |
| 5558 | TSK_BaseClass, |
| 5559 | /// The subobject is a non-static data member. |
| 5560 | TSK_Field, |
| 5561 | /// The object is actually the complete object. |
| 5562 | TSK_CompleteObject |
| 5563 | }; |
| 5564 | |
| 5565 | /// Check whether the special member selected for a given type would be trivial. |
| 5566 | static bool checkTrivialSubobjectCall(Sema &S, SourceLocation SubobjLoc, |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5567 | QualType SubType, bool ConstRHS, |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5568 | Sema::CXXSpecialMember CSM, |
| 5569 | TrivialSubobjectKind Kind, |
| 5570 | bool Diagnose) { |
| 5571 | CXXRecordDecl *SubRD = SubType->getAsCXXRecordDecl(); |
| 5572 | if (!SubRD) |
| 5573 | return true; |
| 5574 | |
| 5575 | CXXMethodDecl *Selected; |
| 5576 | if (findTrivialSpecialMember(S, SubRD, CSM, SubType.getCVRQualifiers(), |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5577 | ConstRHS, Diagnose ? &Selected : 0)) |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5578 | return true; |
| 5579 | |
| 5580 | if (Diagnose) { |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5581 | if (ConstRHS) |
| 5582 | SubType.addConst(); |
| 5583 | |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5584 | if (!Selected && CSM == Sema::CXXDefaultConstructor) { |
| 5585 | S.Diag(SubobjLoc, diag::note_nontrivial_no_def_ctor) |
| 5586 | << Kind << SubType.getUnqualifiedType(); |
| 5587 | if (CXXConstructorDecl *CD = findUserDeclaredCtor(SubRD)) |
| 5588 | S.Diag(CD->getLocation(), diag::note_user_declared_ctor); |
| 5589 | } else if (!Selected) |
| 5590 | S.Diag(SubobjLoc, diag::note_nontrivial_no_copy) |
| 5591 | << Kind << SubType.getUnqualifiedType() << CSM << SubType; |
| 5592 | else if (Selected->isUserProvided()) { |
| 5593 | if (Kind == TSK_CompleteObject) |
| 5594 | S.Diag(Selected->getLocation(), diag::note_nontrivial_user_provided) |
| 5595 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5596 | else { |
| 5597 | S.Diag(SubobjLoc, diag::note_nontrivial_user_provided) |
| 5598 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5599 | S.Diag(Selected->getLocation(), diag::note_declared_at); |
| 5600 | } |
| 5601 | } else { |
| 5602 | if (Kind != TSK_CompleteObject) |
| 5603 | S.Diag(SubobjLoc, diag::note_nontrivial_subobject) |
| 5604 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5605 | |
| 5606 | // Explain why the defaulted or deleted special member isn't trivial. |
| 5607 | S.SpecialMemberIsTrivial(Selected, CSM, Diagnose); |
| 5608 | } |
| 5609 | } |
| 5610 | |
| 5611 | return false; |
| 5612 | } |
| 5613 | |
| 5614 | /// Check whether the members of a class type allow a special member to be |
| 5615 | /// trivial. |
| 5616 | static bool checkTrivialClassMembers(Sema &S, CXXRecordDecl *RD, |
| 5617 | Sema::CXXSpecialMember CSM, |
| 5618 | bool ConstArg, bool Diagnose) { |
| 5619 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 5620 | FE = RD->field_end(); FI != FE; ++FI) { |
| 5621 | if (FI->isInvalidDecl() || FI->isUnnamedBitfield()) |
| 5622 | continue; |
| 5623 | |
| 5624 | QualType FieldType = S.Context.getBaseElementType(FI->getType()); |
| 5625 | |
| 5626 | // Pretend anonymous struct or union members are members of this class. |
| 5627 | if (FI->isAnonymousStructOrUnion()) { |
| 5628 | if (!checkTrivialClassMembers(S, FieldType->getAsCXXRecordDecl(), |
| 5629 | CSM, ConstArg, Diagnose)) |
| 5630 | return false; |
| 5631 | continue; |
| 5632 | } |
| 5633 | |
| 5634 | // C++11 [class.ctor]p5: |
| 5635 | // A default constructor is trivial if [...] |
| 5636 | // -- no non-static data member of its class has a |
| 5637 | // brace-or-equal-initializer |
| 5638 | if (CSM == Sema::CXXDefaultConstructor && FI->hasInClassInitializer()) { |
| 5639 | if (Diagnose) |
| 5640 | S.Diag(FI->getLocation(), diag::note_nontrivial_in_class_init) << *FI; |
| 5641 | return false; |
| 5642 | } |
| 5643 | |
| 5644 | // Objective C ARC 4.3.5: |
| 5645 | // [...] nontrivally ownership-qualified types are [...] not trivially |
| 5646 | // default constructible, copy constructible, move constructible, copy |
| 5647 | // assignable, move assignable, or destructible [...] |
| 5648 | if (S.getLangOpts().ObjCAutoRefCount && |
| 5649 | FieldType.hasNonTrivialObjCLifetime()) { |
| 5650 | if (Diagnose) |
| 5651 | S.Diag(FI->getLocation(), diag::note_nontrivial_objc_ownership) |
| 5652 | << RD << FieldType.getObjCLifetime(); |
| 5653 | return false; |
| 5654 | } |
| 5655 | |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5656 | bool ConstRHS = ConstArg && !FI->isMutable(); |
| 5657 | if (!checkTrivialSubobjectCall(S, FI->getLocation(), FieldType, ConstRHS, |
| 5658 | CSM, TSK_Field, Diagnose)) |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5659 | return false; |
| 5660 | } |
| 5661 | |
| 5662 | return true; |
| 5663 | } |
| 5664 | |
| 5665 | /// Diagnose why the specified class does not have a trivial special member of |
| 5666 | /// the given kind. |
| 5667 | void Sema::DiagnoseNontrivial(const CXXRecordDecl *RD, CXXSpecialMember CSM) { |
| 5668 | QualType Ty = Context.getRecordType(RD); |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5669 | |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5670 | bool ConstArg = (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment); |
| 5671 | checkTrivialSubobjectCall(*this, RD->getLocation(), Ty, ConstArg, CSM, |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5672 | TSK_CompleteObject, /*Diagnose*/true); |
| 5673 | } |
| 5674 | |
| 5675 | /// Determine whether a defaulted or deleted special member function is trivial, |
| 5676 | /// as specified in C++11 [class.ctor]p5, C++11 [class.copy]p12, |
| 5677 | /// C++11 [class.copy]p25, and C++11 [class.dtor]p5. |
| 5678 | bool Sema::SpecialMemberIsTrivial(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 5679 | bool Diagnose) { |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5680 | assert(!MD->isUserProvided() && CSM != CXXInvalid && "not special enough"); |
| 5681 | |
| 5682 | CXXRecordDecl *RD = MD->getParent(); |
| 5683 | |
| 5684 | bool ConstArg = false; |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5685 | |
Richard Smith | 2002bfe | 2013-11-04 02:02:27 +0000 | [diff] [blame] | 5686 | // C++11 [class.copy]p12, p25: [DR1593] |
| 5687 | // A [special member] is trivial if [...] its parameter-type-list is |
| 5688 | // equivalent to the parameter-type-list of an implicit declaration [...] |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5689 | switch (CSM) { |
| 5690 | case CXXDefaultConstructor: |
| 5691 | case CXXDestructor: |
| 5692 | // Trivial default constructors and destructors cannot have parameters. |
| 5693 | break; |
| 5694 | |
| 5695 | case CXXCopyConstructor: |
| 5696 | case CXXCopyAssignment: { |
| 5697 | // Trivial copy operations always have const, non-volatile parameter types. |
| 5698 | ConstArg = true; |
Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 5699 | const ParmVarDecl *Param0 = MD->getParamDecl(0); |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5700 | const ReferenceType *RT = Param0->getType()->getAs<ReferenceType>(); |
| 5701 | if (!RT || RT->getPointeeType().getCVRQualifiers() != Qualifiers::Const) { |
| 5702 | if (Diagnose) |
| 5703 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 5704 | << Param0->getSourceRange() << Param0->getType() |
| 5705 | << Context.getLValueReferenceType( |
| 5706 | Context.getRecordType(RD).withConst()); |
| 5707 | return false; |
| 5708 | } |
| 5709 | break; |
| 5710 | } |
| 5711 | |
| 5712 | case CXXMoveConstructor: |
| 5713 | case CXXMoveAssignment: { |
| 5714 | // Trivial move operations always have non-cv-qualified parameters. |
Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 5715 | const ParmVarDecl *Param0 = MD->getParamDecl(0); |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5716 | const RValueReferenceType *RT = |
| 5717 | Param0->getType()->getAs<RValueReferenceType>(); |
| 5718 | if (!RT || RT->getPointeeType().getCVRQualifiers()) { |
| 5719 | if (Diagnose) |
| 5720 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 5721 | << Param0->getSourceRange() << Param0->getType() |
| 5722 | << Context.getRValueReferenceType(Context.getRecordType(RD)); |
| 5723 | return false; |
| 5724 | } |
| 5725 | break; |
| 5726 | } |
| 5727 | |
| 5728 | case CXXInvalid: |
| 5729 | llvm_unreachable("not a special member"); |
| 5730 | } |
| 5731 | |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5732 | if (MD->getMinRequiredArguments() < MD->getNumParams()) { |
| 5733 | if (Diagnose) |
| 5734 | Diag(MD->getParamDecl(MD->getMinRequiredArguments())->getLocation(), |
| 5735 | diag::note_nontrivial_default_arg) |
| 5736 | << MD->getParamDecl(MD->getMinRequiredArguments())->getSourceRange(); |
| 5737 | return false; |
| 5738 | } |
| 5739 | if (MD->isVariadic()) { |
| 5740 | if (Diagnose) |
| 5741 | Diag(MD->getLocation(), diag::note_nontrivial_variadic); |
| 5742 | return false; |
| 5743 | } |
| 5744 | |
| 5745 | // C++11 [class.ctor]p5, C++11 [class.dtor]p5: |
| 5746 | // A copy/move [constructor or assignment operator] is trivial if |
| 5747 | // -- the [member] selected to copy/move each direct base class subobject |
| 5748 | // is trivial |
| 5749 | // |
| 5750 | // C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5751 | // A [default constructor or destructor] is trivial if |
| 5752 | // -- all the direct base classes have trivial [default constructors or |
| 5753 | // destructors] |
| 5754 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 5755 | BE = RD->bases_end(); BI != BE; ++BI) |
Richard Smith | 41c35d6 | 2013-11-27 03:39:20 +0000 | [diff] [blame] | 5756 | if (!checkTrivialSubobjectCall(*this, BI->getLocStart(), BI->getType(), |
| 5757 | ConstArg, CSM, TSK_BaseClass, Diagnose)) |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5758 | return false; |
| 5759 | |
| 5760 | // C++11 [class.ctor]p5, C++11 [class.dtor]p5: |
| 5761 | // A copy/move [constructor or assignment operator] for a class X is |
| 5762 | // trivial if |
| 5763 | // -- for each non-static data member of X that is of class type (or array |
| 5764 | // thereof), the constructor selected to copy/move that member is |
| 5765 | // trivial |
| 5766 | // |
| 5767 | // C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5768 | // A [default constructor or destructor] is trivial if |
| 5769 | // -- for all of the non-static data members of its class that are of class |
| 5770 | // type (or array thereof), each such class has a trivial [default |
| 5771 | // constructor or destructor] |
| 5772 | if (!checkTrivialClassMembers(*this, RD, CSM, ConstArg, Diagnose)) |
| 5773 | return false; |
| 5774 | |
| 5775 | // C++11 [class.dtor]p5: |
| 5776 | // A destructor is trivial if [...] |
| 5777 | // -- the destructor is not virtual |
| 5778 | if (CSM == CXXDestructor && MD->isVirtual()) { |
| 5779 | if (Diagnose) |
| 5780 | Diag(MD->getLocation(), diag::note_nontrivial_virtual_dtor) << RD; |
| 5781 | return false; |
| 5782 | } |
| 5783 | |
| 5784 | // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5785 | // A [special member] for class X is trivial if [...] |
| 5786 | // -- class X has no virtual functions and no virtual base classes |
| 5787 | if (CSM != CXXDestructor && MD->getParent()->isDynamicClass()) { |
| 5788 | if (!Diagnose) |
| 5789 | return false; |
| 5790 | |
| 5791 | if (RD->getNumVBases()) { |
| 5792 | // Check for virtual bases. We already know that the corresponding |
| 5793 | // member in all bases is trivial, so vbases must all be direct. |
| 5794 | CXXBaseSpecifier &BS = *RD->vbases_begin(); |
| 5795 | assert(BS.isVirtual()); |
| 5796 | Diag(BS.getLocStart(), diag::note_nontrivial_has_virtual) << RD << 1; |
| 5797 | return false; |
| 5798 | } |
| 5799 | |
| 5800 | // Must have a virtual method. |
| 5801 | for (CXXRecordDecl::method_iterator MI = RD->method_begin(), |
| 5802 | ME = RD->method_end(); MI != ME; ++MI) { |
| 5803 | if (MI->isVirtual()) { |
| 5804 | SourceLocation MLoc = MI->getLocStart(); |
| 5805 | Diag(MLoc, diag::note_nontrivial_has_virtual) << RD << 0; |
| 5806 | return false; |
| 5807 | } |
| 5808 | } |
| 5809 | |
| 5810 | llvm_unreachable("dynamic class with no vbases and no virtual functions"); |
| 5811 | } |
| 5812 | |
| 5813 | // Looks like it's trivial! |
| 5814 | return true; |
| 5815 | } |
| 5816 | |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5817 | /// \brief Data used with FindHiddenVirtualMethod |
Benjamin Kramer | 024e619 | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 5818 | namespace { |
| 5819 | struct FindHiddenVirtualMethodData { |
| 5820 | Sema *S; |
| 5821 | CXXMethodDecl *Method; |
| 5822 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5823 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
Benjamin Kramer | 024e619 | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 5824 | }; |
| 5825 | } |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5826 | |
David Blaikie | 282c92a | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5827 | /// \brief Check whether any most overriden method from MD in Methods |
| 5828 | static bool CheckMostOverridenMethods(const CXXMethodDecl *MD, |
| 5829 | const llvm::SmallPtrSet<const CXXMethodDecl *, 8>& Methods) { |
| 5830 | if (MD->size_overridden_methods() == 0) |
| 5831 | return Methods.count(MD->getCanonicalDecl()); |
| 5832 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 5833 | E = MD->end_overridden_methods(); |
| 5834 | I != E; ++I) |
| 5835 | if (CheckMostOverridenMethods(*I, Methods)) |
| 5836 | return true; |
| 5837 | return false; |
| 5838 | } |
| 5839 | |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5840 | /// \brief Member lookup function that determines whether a given C++ |
| 5841 | /// method overloads virtual methods in a base class without overriding any, |
| 5842 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 5843 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 5844 | CXXBasePath &Path, |
| 5845 | void *UserData) { |
| 5846 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 5847 | |
| 5848 | FindHiddenVirtualMethodData &Data |
| 5849 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 5850 | |
| 5851 | DeclarationName Name = Data.Method->getDeclName(); |
| 5852 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 5853 | |
| 5854 | bool foundSameNameMethod = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5855 | SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5856 | for (Path.Decls = BaseRecord->lookup(Name); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5857 | !Path.Decls.empty(); |
| 5858 | Path.Decls = Path.Decls.slice(1)) { |
| 5859 | NamedDecl *D = Path.Decls.front(); |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5860 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 7dd856a | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 5861 | MD = MD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5862 | foundSameNameMethod = true; |
| 5863 | // Interested only in hidden virtual methods. |
| 5864 | if (!MD->isVirtual()) |
| 5865 | continue; |
| 5866 | // If the method we are checking overrides a method from its base |
| 5867 | // don't warn about the other overloaded methods. |
| 5868 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 5869 | return true; |
| 5870 | // Collect the overload only if its hidden. |
David Blaikie | 282c92a | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5871 | if (!CheckMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods)) |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5872 | overloadedMethods.push_back(MD); |
| 5873 | } |
| 5874 | } |
| 5875 | |
| 5876 | if (foundSameNameMethod) |
| 5877 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 5878 | overloadedMethods.end()); |
| 5879 | return foundSameNameMethod; |
| 5880 | } |
| 5881 | |
David Blaikie | 282c92a | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5882 | /// \brief Add the most overriden methods from MD to Methods |
| 5883 | static void AddMostOverridenMethods(const CXXMethodDecl *MD, |
| 5884 | llvm::SmallPtrSet<const CXXMethodDecl *, 8>& Methods) { |
| 5885 | if (MD->size_overridden_methods() == 0) |
| 5886 | Methods.insert(MD->getCanonicalDecl()); |
| 5887 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 5888 | E = MD->end_overridden_methods(); |
| 5889 | I != E; ++I) |
| 5890 | AddMostOverridenMethods(*I, Methods); |
| 5891 | } |
| 5892 | |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 5893 | /// \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] | 5894 | /// overriding any. |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 5895 | void Sema::FindHiddenVirtualMethods(CXXMethodDecl *MD, |
| 5896 | SmallVectorImpl<CXXMethodDecl*> &OverloadedMethods) { |
Benjamin Kramer | 1458c1c | 2012-05-19 16:03:58 +0000 | [diff] [blame] | 5897 | if (!MD->getDeclName().isIdentifier()) |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5898 | return; |
| 5899 | |
| 5900 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 5901 | /*bool RecordPaths=*/false, |
| 5902 | /*bool DetectVirtual=*/false); |
| 5903 | FindHiddenVirtualMethodData Data; |
| 5904 | Data.Method = MD; |
| 5905 | Data.S = this; |
| 5906 | |
| 5907 | // Keep the base methods that were overriden or introduced in the subclass |
| 5908 | // 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] | 5909 | CXXRecordDecl *DC = MD->getParent(); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5910 | DeclContext::lookup_result R = DC->lookup(MD->getDeclName()); |
| 5911 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 5912 | NamedDecl *ND = *I; |
| 5913 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*I)) |
David Blaikie | 282c92a | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5914 | ND = shad->getTargetDecl(); |
| 5915 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 5916 | AddMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods); |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5917 | } |
| 5918 | |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 5919 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths)) |
| 5920 | OverloadedMethods = Data.OverloadedMethods; |
| 5921 | } |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5922 | |
Eli Friedman | af65120b | 2013-09-05 23:51:03 +0000 | [diff] [blame] | 5923 | void Sema::NoteHiddenVirtualMethods(CXXMethodDecl *MD, |
| 5924 | SmallVectorImpl<CXXMethodDecl*> &OverloadedMethods) { |
| 5925 | for (unsigned i = 0, e = OverloadedMethods.size(); i != e; ++i) { |
| 5926 | CXXMethodDecl *overloadedMD = OverloadedMethods[i]; |
| 5927 | PartialDiagnostic PD = PDiag( |
| 5928 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
| 5929 | HandleFunctionTypeMismatch(PD, MD->getType(), overloadedMD->getType()); |
| 5930 | Diag(overloadedMD->getLocation(), PD); |
| 5931 | } |
| 5932 | } |
| 5933 | |
| 5934 | /// \brief Diagnose methods which overload virtual methods in a base class |
| 5935 | /// without overriding any. |
| 5936 | void Sema::DiagnoseHiddenVirtualMethods(CXXMethodDecl *MD) { |
| 5937 | if (MD->isInvalidDecl()) |
| 5938 | return; |
| 5939 | |
| 5940 | if (Diags.getDiagnosticLevel(diag::warn_overloaded_virtual, |
| 5941 | MD->getLocation()) == DiagnosticsEngine::Ignored) |
| 5942 | return; |
| 5943 | |
| 5944 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| 5945 | FindHiddenVirtualMethods(MD, OverloadedMethods); |
| 5946 | if (!OverloadedMethods.empty()) { |
| 5947 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 5948 | << MD << (OverloadedMethods.size() > 1); |
| 5949 | |
| 5950 | NoteHiddenVirtualMethods(MD, OverloadedMethods); |
Argyrios Kyrtzidis | 7272d9c | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5951 | } |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 5952 | } |
| 5953 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5954 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5955 | Decl *TagDecl, |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5956 | SourceLocation LBrac, |
Douglas Gregor | c48a10d | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 5957 | SourceLocation RBrac, |
| 5958 | AttributeList *AttrList) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5959 | if (!TagDecl) |
| 5960 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5961 | |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5962 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 5963 | |
Rafael Espindola | 06e1b13 | 2012-07-12 04:32:30 +0000 | [diff] [blame] | 5964 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
| 5965 | if (l->getKind() != AttributeList::AT_Visibility) |
| 5966 | continue; |
| 5967 | l->setInvalid(); |
| 5968 | Diag(l->getLoc(), diag::warn_attribute_after_definition_ignored) << |
| 5969 | l->getName(); |
| 5970 | } |
| 5971 | |
David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 5972 | ActOnFields(S, RLoc, TagDecl, llvm::makeArrayRef( |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5973 | // strict aliasing violation! |
| 5974 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 5975 | FieldCollector->getCurNumFields()), LBrac, RBrac, AttrList); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 5976 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5977 | CheckCompletedCXXClass( |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5978 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5979 | } |
| 5980 | |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5981 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 5982 | /// special functions, such as the default constructor, copy |
| 5983 | /// constructor, or destructor, to the given C++ class (C++ |
| 5984 | /// [special]p1). This routine can only be executed just before the |
| 5985 | /// definition of the class is complete. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5986 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5987 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5988 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5989 | |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5990 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5991 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5992 | |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5993 | // If the properties or semantics of the copy constructor couldn't be |
| 5994 | // determined while the class was being declared, force a declaration |
| 5995 | // of it now. |
| 5996 | if (ClassDecl->needsOverloadResolutionForCopyConstructor()) |
| 5997 | DeclareImplicitCopyConstructor(ClassDecl); |
| 5998 | } |
| 5999 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6000 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveConstructor()) { |
Richard Smith | 966c1fb | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 6001 | ++ASTContext::NumImplicitMoveConstructors; |
| 6002 | |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6003 | if (ClassDecl->needsOverloadResolutionForMoveConstructor()) |
| 6004 | DeclareImplicitMoveConstructor(ClassDecl); |
| 6005 | } |
| 6006 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6007 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 6008 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6009 | |
| 6010 | // 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] | 6011 | // 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] | 6012 | // it shows up in the right place in the vtable and that we diagnose |
| 6013 | // problems with the implicit exception specification. |
| 6014 | if (ClassDecl->isDynamicClass() || |
| 6015 | ClassDecl->needsOverloadResolutionForCopyAssignment()) |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6016 | DeclareImplicitCopyAssignment(ClassDecl); |
| 6017 | } |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 6018 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6019 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveAssignment()) { |
Richard Smith | 966c1fb | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 6020 | ++ASTContext::NumImplicitMoveAssignmentOperators; |
| 6021 | |
| 6022 | // Likewise for the move assignment operator. |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6023 | if (ClassDecl->isDynamicClass() || |
| 6024 | ClassDecl->needsOverloadResolutionForMoveAssignment()) |
Richard Smith | 966c1fb | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 6025 | DeclareImplicitMoveAssignment(ClassDecl); |
| 6026 | } |
| 6027 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6028 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 6029 | ++ASTContext::NumImplicitDestructors; |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6030 | |
| 6031 | // 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] | 6032 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 6033 | // shows up in the right place in the vtable and that we diagnose problems |
| 6034 | // with the implicit exception specification. |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 6035 | if (ClassDecl->isDynamicClass() || |
| 6036 | ClassDecl->needsOverloadResolutionForDestructor()) |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6037 | DeclareImplicitDestructor(ClassDecl); |
| 6038 | } |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 6039 | } |
| 6040 | |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6041 | void Sema::ActOnReenterDeclaratorTemplateScope(Scope *S, DeclaratorDecl *D) { |
| 6042 | if (!D) |
| 6043 | return; |
| 6044 | |
| 6045 | int NumParamList = D->getNumTemplateParameterLists(); |
| 6046 | for (int i = 0; i < NumParamList; i++) { |
| 6047 | TemplateParameterList* Params = D->getTemplateParameterList(i); |
| 6048 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 6049 | ParamEnd = Params->end(); |
| 6050 | Param != ParamEnd; ++Param) { |
| 6051 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 6052 | if (Named->getDeclName()) { |
| 6053 | S->AddDecl(Named); |
| 6054 | IdResolver.AddDecl(Named); |
| 6055 | } |
| 6056 | } |
| 6057 | } |
| 6058 | } |
| 6059 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6060 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | e61ef62 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 6061 | if (!D) |
| 6062 | return; |
| 6063 | |
| 6064 | TemplateParameterList *Params = 0; |
| 6065 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 6066 | Params = Template->getTemplateParameters(); |
| 6067 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 6068 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 6069 | Params = PartialSpec->getTemplateParameters(); |
| 6070 | else |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 6071 | return; |
| 6072 | |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 6073 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 6074 | ParamEnd = Params->end(); |
| 6075 | Param != ParamEnd; ++Param) { |
| 6076 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 6077 | if (Named->getDeclName()) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6078 | S->AddDecl(Named); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 6079 | IdResolver.AddDecl(Named); |
| 6080 | } |
| 6081 | } |
| 6082 | } |
| 6083 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6084 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6085 | if (!RecordD) return; |
| 6086 | AdjustDeclIfTemplate(RecordD); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6087 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6088 | PushDeclContext(S, Record); |
| 6089 | } |
| 6090 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6091 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6092 | if (!RecordD) return; |
| 6093 | PopDeclContext(); |
| 6094 | } |
| 6095 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6096 | /// This is used to implement the constant expression evaluation part of the |
| 6097 | /// attribute enable_if extension. There is nothing in standard C++ which would |
| 6098 | /// require reentering parameters. |
| 6099 | void Sema::ActOnReenterCXXMethodParameter(Scope *S, ParmVarDecl *Param) { |
| 6100 | if (!Param) |
| 6101 | return; |
| 6102 | |
| 6103 | S->AddDecl(Param); |
| 6104 | if (Param->getDeclName()) |
| 6105 | IdResolver.AddDecl(Param); |
| 6106 | } |
| 6107 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6108 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 6109 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 6110 | /// parsing those parts of the given Method declaration that could |
| 6111 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 6112 | /// arguments. This action should enter the scope of the given |
| 6113 | /// Method declaration as if we had just parsed the qualified method |
| 6114 | /// name. However, it should not bring the parameters into scope; |
| 6115 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6116 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
| 6119 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 6120 | /// C++ method declaration. We're (re-)introducing the given |
| 6121 | /// function parameter into scope for use in parsing later parts of |
| 6122 | /// the method declaration. For example, we could see an |
| 6123 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6124 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 6125 | if (!ParamD) |
| 6126 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6127 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6128 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6129 | |
| 6130 | // If this parameter has an unparsed default argument, clear it out |
| 6131 | // to make way for the parsed default argument. |
| 6132 | if (Param->hasUnparsedDefaultArg()) |
| 6133 | Param->setDefaultArg(0); |
| 6134 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6135 | S->AddDecl(Param); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6136 | if (Param->getDeclName()) |
| 6137 | IdResolver.AddDecl(Param); |
| 6138 | } |
| 6139 | |
| 6140 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 6141 | /// processing the delayed method declaration for Method. The method |
| 6142 | /// declaration is now considered finished. There may be a separate |
| 6143 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 6144 | /// immediately!) for this method, if it was also defined inside the |
| 6145 | /// class body. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6146 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 6147 | if (!MethodD) |
| 6148 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6149 | |
Douglas Gregor | c8c277a | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 6150 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6151 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6152 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6153 | |
| 6154 | // Now that we have our default arguments, check the constructor |
| 6155 | // again. It could produce additional diagnostics or affect whether |
| 6156 | // the class has implicitly-declared destructors, among other |
| 6157 | // things. |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6158 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 6159 | CheckConstructor(Constructor); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6160 | |
| 6161 | // Check the default arguments, which we may have added. |
| 6162 | if (!Method->isInvalidDecl()) |
| 6163 | CheckCXXDefaultArguments(Method); |
| 6164 | } |
| 6165 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6166 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6167 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6168 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6169 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 6170 | /// will be updated to reflect a well-formed type for the constructor and |
| 6171 | /// returned. |
| 6172 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6173 | StorageClass &SC) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6174 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6175 | |
| 6176 | // C++ [class.ctor]p3: |
| 6177 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 6178 | // constructor can be invoked for a const, volatile or const |
| 6179 | // volatile object. A constructor shall not be declared const, |
| 6180 | // volatile, or const volatile (9.3.2). |
| 6181 | if (isVirtual) { |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6182 | if (!D.isInvalidType()) |
| 6183 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 6184 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 6185 | << SourceRange(D.getIdentifierLoc()); |
| 6186 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6187 | } |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6188 | if (SC == SC_Static) { |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6189 | if (!D.isInvalidType()) |
| 6190 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 6191 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 6192 | << SourceRange(D.getIdentifierLoc()); |
| 6193 | D.setInvalidType(); |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6194 | SC = SC_None; |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6195 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6196 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6197 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6198 | if (FTI.TypeQuals != 0) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6199 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6200 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 6201 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6202 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6203 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 6204 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6205 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6206 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 6207 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6208 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6209 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6210 | |
Douglas Gregor | db9d664 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6211 | // C++0x [class.ctor]p4: |
| 6212 | // A constructor shall not be declared with a ref-qualifier. |
| 6213 | if (FTI.hasRefQualifier()) { |
| 6214 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 6215 | << FTI.RefQualifierIsLValueRef |
| 6216 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 6217 | D.setInvalidType(); |
| 6218 | } |
| 6219 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6220 | // Rebuild the function type "R" without any type qualifiers (in |
| 6221 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6222 | // return type, since constructors don't have return types. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6223 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6224 | if (Proto->getReturnType() == Context.VoidTy && !D.isInvalidType()) |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6225 | return R; |
| 6226 | |
| 6227 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 6228 | EPI.TypeQuals = 0; |
Douglas Gregor | db9d664 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6229 | EPI.RefQualifier = RQ_None; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6230 | |
| 6231 | return Context.getFunctionType(Context.VoidTy, Proto->getParamTypes(), EPI); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6232 | } |
| 6233 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6234 | /// CheckConstructor - Checks a fully-formed constructor for |
| 6235 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 6236 | /// the constructor declarator is invalid. |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6237 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6238 | CXXRecordDecl *ClassDecl |
Douglas Gregor | f4d17c4 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 6239 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 6240 | if (!ClassDecl) |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6241 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6242 | |
| 6243 | // C++ [class.copy]p3: |
| 6244 | // A declaration of a constructor for a class X is ill-formed if |
| 6245 | // its first parameter is of type (optionally cv-qualified) X and |
| 6246 | // either there are no other parameters or else all other |
| 6247 | // parameters have default arguments. |
Douglas Gregor | f4d17c4 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 6248 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6249 | ((Constructor->getNumParams() == 1) || |
| 6250 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 6251 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 6252 | Constructor->getTemplateSpecializationKind() |
| 6253 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6254 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 6255 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 6256 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6257 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | fd42e95 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 6258 | const char *ConstRef |
| 6259 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 6260 | : " const &"; |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 6261 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | fd42e95 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 6262 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 6263 | |
| 6264 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 6265 | // to fix the type. |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6266 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6267 | } |
| 6268 | } |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 6269 | } |
| 6270 | |
John McCall | deb646e | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 6271 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 6272 | /// well-formedness, issuing any diagnostics required. Returns true |
| 6273 | /// on error. |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6274 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6275 | CXXRecordDecl *RD = Destructor->getParent(); |
| 6276 | |
Peter Collingbourne | b289fe6 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 6277 | if (!Destructor->getOperatorDelete() && Destructor->isVirtual()) { |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6278 | SourceLocation Loc; |
| 6279 | |
| 6280 | if (!Destructor->isImplicit()) |
| 6281 | Loc = Destructor->getLocation(); |
| 6282 | else |
| 6283 | Loc = RD->getLocation(); |
| 6284 | |
| 6285 | // If we have a virtual destructor, look up the deallocation function |
| 6286 | FunctionDecl *OperatorDelete = 0; |
| 6287 | DeclarationName Name = |
| 6288 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6289 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6290 | return true; |
Richard Smith | f03bd30 | 2013-12-05 08:30:59 +0000 | [diff] [blame] | 6291 | // If there's no class-specific operator delete, look up the global |
| 6292 | // non-array delete. |
| 6293 | if (!OperatorDelete) |
| 6294 | OperatorDelete = FindUsualDeallocationFunction(Loc, true, Name); |
John McCall | 1e5d75d | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 6295 | |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 6296 | MarkFunctionReferenced(Loc, OperatorDelete); |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6297 | |
| 6298 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6299 | } |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6300 | |
| 6301 | return false; |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6302 | } |
| 6303 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6304 | static inline bool |
Anders Carlsson | 5e96547 | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 6305 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 6306 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 6307 | FTI.ArgInfo[0].Param && |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6308 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 5e96547 | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 6309 | } |
| 6310 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6311 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 6312 | /// the well-formednes of the destructor declarator @p D with type @p |
| 6313 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6314 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 6315 | /// will be updated to reflect a well-formed type for the destructor and |
| 6316 | /// returned. |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6317 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6318 | StorageClass& SC) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6319 | // C++ [class.dtor]p1: |
| 6320 | // [...] A typedef-name that names a class is a class-name |
| 6321 | // (7.1.3); however, a typedef-name that names a class shall not |
| 6322 | // be used as the identifier in the declarator for a destructor |
| 6323 | // declaration. |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6324 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6325 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6326 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6327 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6328 | else if (const TemplateSpecializationType *TST = |
| 6329 | DeclaratorType->getAs<TemplateSpecializationType>()) |
| 6330 | if (TST->isTypeAlias()) |
| 6331 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
| 6332 | << DeclaratorType << 1; |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6333 | |
| 6334 | // C++ [class.dtor]p2: |
| 6335 | // A destructor is used to destroy objects of its class type. A |
| 6336 | // destructor takes no parameters, and no return type can be |
| 6337 | // specified for it (not even void). The address of a destructor |
| 6338 | // shall not be taken. A destructor shall not be static. A |
| 6339 | // destructor can be invoked for a const, volatile or const |
| 6340 | // volatile object. A destructor shall not be declared const, |
| 6341 | // volatile or const volatile (9.3.2). |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6342 | if (SC == SC_Static) { |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6343 | if (!D.isInvalidType()) |
| 6344 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 6345 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6346 | << SourceRange(D.getIdentifierLoc()) |
| 6347 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6348 | |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6349 | SC = SC_None; |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6350 | } |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6351 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6352 | // Destructors don't have return types, but the parser will |
| 6353 | // happily parse something like: |
| 6354 | // |
| 6355 | // class X { |
| 6356 | // float ~X(); |
| 6357 | // }; |
| 6358 | // |
| 6359 | // The return type will be eliminated later. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6360 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 6361 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6362 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6363 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6364 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6365 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6366 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6367 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6368 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6369 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6370 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6371 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6372 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6373 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6374 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6375 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6376 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6377 | } |
| 6378 | |
Douglas Gregor | db9d664 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6379 | // C++0x [class.dtor]p2: |
| 6380 | // A destructor shall not be declared with a ref-qualifier. |
| 6381 | if (FTI.hasRefQualifier()) { |
| 6382 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 6383 | << FTI.RefQualifierIsLValueRef |
| 6384 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 6385 | D.setInvalidType(); |
| 6386 | } |
| 6387 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6388 | // Make sure we don't have any parameters. |
Anders Carlsson | 5e96547 | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 6389 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6390 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 6391 | |
| 6392 | // Delete the parameters. |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6393 | FTI.freeArgs(); |
| 6394 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6395 | } |
| 6396 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6397 | // Make sure the destructor isn't variadic. |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6398 | if (FTI.isVariadic) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6399 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6400 | D.setInvalidType(); |
| 6401 | } |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6402 | |
| 6403 | // Rebuild the function type "R" without any type qualifiers or |
| 6404 | // parameters (in case any of the errors above fired) and with |
| 6405 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6406 | // types. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6407 | if (!D.isInvalidType()) |
| 6408 | return R; |
| 6409 | |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6410 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6411 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 6412 | EPI.Variadic = false; |
| 6413 | EPI.TypeQuals = 0; |
Douglas Gregor | db9d664 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6414 | EPI.RefQualifier = RQ_None; |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 6415 | return Context.getFunctionType(Context.VoidTy, None, EPI); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6418 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 6419 | /// well-formednes of the conversion function declarator @p D with |
| 6420 | /// type @p R. If there are any errors in the declarator, this routine |
| 6421 | /// will emit diagnostics and return true. Otherwise, it will return |
| 6422 | /// false. Either way, the type @p R will be updated to reflect a |
| 6423 | /// well-formed type for the conversion operator. |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6424 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6425 | StorageClass& SC) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6426 | // C++ [class.conv.fct]p1: |
| 6427 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6428 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6429 | // parameter returning conversion-type-id." |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6430 | if (SC == SC_Static) { |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6431 | if (!D.isInvalidType()) |
| 6432 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
Eli Friedman | 600bc24 | 2013-06-20 20:58:02 +0000 | [diff] [blame] | 6433 | << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 6434 | << D.getName().getSourceRange(); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6435 | D.setInvalidType(); |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6436 | SC = SC_None; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6437 | } |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6438 | |
| 6439 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 6440 | |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6441 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6442 | // Conversion functions don't have return types, but the parser will |
| 6443 | // happily parse something like: |
| 6444 | // |
| 6445 | // class X { |
| 6446 | // float operator bool(); |
| 6447 | // }; |
| 6448 | // |
| 6449 | // The return type will be changed later anyway. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6450 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 6451 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6452 | << SourceRange(D.getIdentifierLoc()); |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6453 | D.setInvalidType(); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6454 | } |
| 6455 | |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6456 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 6457 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6458 | // Make sure we don't have any parameters. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6459 | if (Proto->getNumParams() > 0) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6460 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 6461 | |
| 6462 | // Delete the parameters. |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6463 | D.getFunctionTypeInfo().freeArgs(); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6464 | D.setInvalidType(); |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6465 | } else if (Proto->isVariadic()) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6466 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6467 | D.setInvalidType(); |
| 6468 | } |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6469 | |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6470 | // Diagnose "&operator bool()" and other such nonsense. This |
| 6471 | // is actually a gcc extension which we don't support. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6472 | if (Proto->getReturnType() != ConvType) { |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6473 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6474 | << Proto->getReturnType(); |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6475 | D.setInvalidType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6476 | ConvType = Proto->getReturnType(); |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6477 | } |
| 6478 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6479 | // C++ [class.conv.fct]p4: |
| 6480 | // The conversion-type-id shall not represent a function type nor |
| 6481 | // an array type. |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6482 | if (ConvType->isArrayType()) { |
| 6483 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 6484 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6485 | D.setInvalidType(); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6486 | } else if (ConvType->isFunctionType()) { |
| 6487 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 6488 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6489 | D.setInvalidType(); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6490 | } |
| 6491 | |
| 6492 | // Rebuild the function type "R" without any parameters (in case any |
| 6493 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6494 | // return type. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6495 | if (D.isInvalidType()) |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 6496 | R = Context.getFunctionType(ConvType, None, Proto->getExtProtoInfo()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6497 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6498 | // C++0x explicit conversion operators. |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6499 | if (D.getDeclSpec().isExplicitSpecified()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6500 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6501 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6502 | diag::warn_cxx98_compat_explicit_conversion_functions : |
| 6503 | diag::ext_explicit_conversion_functions) |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6504 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6505 | } |
| 6506 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6507 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 6508 | /// the declaration of the given C++ conversion function. This routine |
| 6509 | /// is responsible for recording the conversion function in the C++ |
| 6510 | /// class, if possible. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6511 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6512 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 6513 | |
Douglas Gregor | 4287b37 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 6514 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6515 | |
| 6516 | // Make sure we aren't redeclaring the conversion function. |
| 6517 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6518 | |
| 6519 | // C++ [class.conv.fct]p1: |
| 6520 | // [...] A conversion function is never used to convert a |
| 6521 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 6522 | // same object type (or a reference to it), to a (possibly |
| 6523 | // cv-qualified) base class of that type (or a reference to it), |
| 6524 | // or to (possibly cv-qualified) void. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6525 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 6526 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6527 | QualType ClassType |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6528 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6529 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6530 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | 6309e3d | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 6531 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 6532 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | e47191c | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 6533 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | 6309e3d | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 6534 | else if (ConvType->isRecordType()) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6535 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 6536 | if (ConvType == ClassType) |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 6537 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6538 | << ClassType; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6539 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 6540 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6541 | << ClassType << ConvType; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6542 | } else if (ConvType->isVoidType()) { |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 6543 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6544 | << ClassType << ConvType; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6545 | } |
| 6546 | |
Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 6547 | if (FunctionTemplateDecl *ConversionTemplate |
| 6548 | = Conversion->getDescribedFunctionTemplate()) |
| 6549 | return ConversionTemplate; |
| 6550 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6551 | return Conversion; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6552 | } |
| 6553 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6554 | //===----------------------------------------------------------------------===// |
| 6555 | // Namespace Handling |
| 6556 | //===----------------------------------------------------------------------===// |
| 6557 | |
Richard Smith | 45bb885 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6558 | /// \brief Diagnose a mismatch in 'inline' qualifiers when a namespace is |
| 6559 | /// reopened. |
| 6560 | static void DiagnoseNamespaceInlineMismatch(Sema &S, SourceLocation KeywordLoc, |
| 6561 | SourceLocation Loc, |
| 6562 | IdentifierInfo *II, bool *IsInline, |
| 6563 | NamespaceDecl *PrevNS) { |
| 6564 | assert(*IsInline != PrevNS->isInline()); |
John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 6565 | |
Richard Smith | f501cc3 | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 6566 | // HACK: Work around a bug in libstdc++4.6's <atomic>, where |
| 6567 | // std::__atomic[0,1,2] are defined as non-inline namespaces, then reopened as |
| 6568 | // inline namespaces, with the intention of bringing names into namespace std. |
| 6569 | // |
| 6570 | // We support this just well enough to get that case working; this is not |
| 6571 | // sufficient to support reopening namespaces as inline in general. |
Richard Smith | 45bb885 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6572 | if (*IsInline && II && II->getName().startswith("__atomic") && |
| 6573 | S.getSourceManager().isInSystemHeader(Loc)) { |
Richard Smith | f501cc3 | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 6574 | // Mark all prior declarations of the namespace as inline. |
Richard Smith | 45bb885 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6575 | for (NamespaceDecl *NS = PrevNS->getMostRecentDecl(); NS; |
| 6576 | NS = NS->getPreviousDecl()) |
| 6577 | NS->setInline(*IsInline); |
| 6578 | // Patch up the lookup table for the containing namespace. This isn't really |
| 6579 | // correct, but it's good enough for this particular case. |
| 6580 | for (DeclContext::decl_iterator I = PrevNS->decls_begin(), |
| 6581 | E = PrevNS->decls_end(); I != E; ++I) |
| 6582 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*I)) |
| 6583 | PrevNS->getParent()->makeDeclVisibleInContext(ND); |
| 6584 | return; |
| 6585 | } |
| 6586 | |
| 6587 | if (PrevNS->isInline()) |
| 6588 | // The user probably just forgot the 'inline', so suggest that it |
| 6589 | // be added back. |
| 6590 | S.Diag(Loc, diag::warn_inline_namespace_reopened_noninline) |
| 6591 | << FixItHint::CreateInsertion(KeywordLoc, "inline "); |
| 6592 | else |
| 6593 | S.Diag(Loc, diag::err_inline_namespace_mismatch) |
| 6594 | << IsInline; |
| 6595 | |
| 6596 | S.Diag(PrevNS->getLocation(), diag::note_previous_definition); |
| 6597 | *IsInline = PrevNS->isInline(); |
| 6598 | } |
John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 6599 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6600 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 6601 | /// definition. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6602 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 6603 | SourceLocation InlineLoc, |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6604 | SourceLocation NamespaceLoc, |
John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 6605 | SourceLocation IdentLoc, |
| 6606 | IdentifierInfo *II, |
| 6607 | SourceLocation LBrace, |
| 6608 | AttributeList *AttrList) { |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6609 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 6610 | // For anonymous namespace, take the location of the left brace. |
| 6611 | SourceLocation Loc = II ? IdentLoc : LBrace; |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6612 | bool IsInline = InlineLoc.isValid(); |
Douglas Gregor | 21b3b29 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6613 | bool IsInvalid = false; |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6614 | bool IsStd = false; |
| 6615 | bool AddToKnown = false; |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6616 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 6617 | |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6618 | NamespaceDecl *PrevNS = 0; |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6619 | if (II) { |
| 6620 | // C++ [namespace.def]p2: |
Douglas Gregor | 412c362 | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 6621 | // The identifier in an original-namespace-definition shall not |
| 6622 | // have been previously defined in the declarative region in |
| 6623 | // which the original-namespace-definition appears. The |
| 6624 | // identifier in an original-namespace-definition is the name of |
| 6625 | // the namespace. Subsequently in that declarative region, it is |
| 6626 | // treated as an original-namespace-name. |
| 6627 | // |
| 6628 | // Since namespace names are unique in their scope, and we don't |
Douglas Gregor | b578fbe | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6629 | // look through using directives, just look for any ordinary names. |
| 6630 | |
| 6631 | const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Member | |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6632 | Decl::IDNS_Type | Decl::IDNS_Using | Decl::IDNS_Tag | |
| 6633 | Decl::IDNS_Namespace; |
Douglas Gregor | b578fbe | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6634 | NamedDecl *PrevDecl = 0; |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 6635 | DeclContext::lookup_result R = CurContext->getRedeclContext()->lookup(II); |
| 6636 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 6637 | ++I) { |
| 6638 | if ((*I)->getIdentifierNamespace() & IDNS) { |
| 6639 | PrevDecl = *I; |
Douglas Gregor | b578fbe | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6640 | break; |
| 6641 | } |
| 6642 | } |
| 6643 | |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6644 | PrevNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl); |
| 6645 | |
| 6646 | if (PrevNS) { |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6647 | // This is an extended namespace definition. |
Richard Smith | 45bb885 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6648 | if (IsInline != PrevNS->isInline()) |
| 6649 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, Loc, II, |
| 6650 | &IsInline, PrevNS); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6651 | } else if (PrevDecl) { |
| 6652 | // This is an invalid name redefinition. |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6653 | Diag(Loc, diag::err_redefinition_different_kind) |
| 6654 | << II; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6655 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 21b3b29 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6656 | IsInvalid = true; |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6657 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6658 | } else if (II->isStr("std") && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6659 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 6660 | // This is the first "real" definition of the namespace "std", so update |
| 6661 | // our cache of the "std" namespace to point at this definition. |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6662 | PrevNS = getStdNamespace(); |
| 6663 | IsStd = true; |
| 6664 | AddToKnown = !IsInline; |
| 6665 | } else { |
| 6666 | // We've seen this namespace for the first time. |
| 6667 | AddToKnown = !IsInline; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6668 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6669 | } else { |
John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6670 | // Anonymous namespaces. |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6671 | |
| 6672 | // Determine whether the parent already has an anonymous namespace. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6673 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6674 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6675 | PrevNS = TU->getAnonymousNamespace(); |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6676 | } else { |
| 6677 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6678 | PrevNS = ND->getAnonymousNamespace(); |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6679 | } |
| 6680 | |
Richard Smith | 45bb885 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6681 | if (PrevNS && IsInline != PrevNS->isInline()) |
| 6682 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, NamespaceLoc, II, |
| 6683 | &IsInline, PrevNS); |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6684 | } |
| 6685 | |
| 6686 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, IsInline, |
| 6687 | StartLoc, Loc, II, PrevNS); |
Douglas Gregor | 21b3b29 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6688 | if (IsInvalid) |
| 6689 | Namespc->setInvalidDecl(); |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6690 | |
| 6691 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
Sebastian Redl | b5c2baa | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 6692 | |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6693 | // FIXME: Should we be merging attributes? |
| 6694 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 6695 | PushNamespaceVisibilityAttr(Attr, Loc); |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6696 | |
| 6697 | if (IsStd) |
| 6698 | StdNamespace = Namespc; |
| 6699 | if (AddToKnown) |
| 6700 | KnownNamespaces[Namespc] = false; |
| 6701 | |
| 6702 | if (II) { |
| 6703 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 6704 | } else { |
| 6705 | // Link the anonymous namespace into its parent. |
| 6706 | DeclContext *Parent = CurContext->getRedeclContext(); |
| 6707 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 6708 | TU->setAnonymousNamespace(Namespc); |
| 6709 | } else { |
| 6710 | cast<NamespaceDecl>(Parent)->setAnonymousNamespace(Namespc); |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6711 | } |
John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6712 | |
Douglas Gregor | f9f54ea | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 6713 | CurContext->addDecl(Namespc); |
| 6714 | |
John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6715 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 6716 | // behaves as if it were replaced by |
| 6717 | // namespace unique { /* empty body */ } |
| 6718 | // using namespace unique; |
| 6719 | // namespace unique { namespace-body } |
| 6720 | // where all occurrences of 'unique' in a translation unit are |
| 6721 | // replaced by the same identifier and this identifier differs |
| 6722 | // from all other identifiers in the entire program. |
| 6723 | |
| 6724 | // We just create the namespace with an empty name and then add an |
| 6725 | // implicit using declaration, just like the standard suggests. |
| 6726 | // |
| 6727 | // CodeGen enforces the "universally unique" aspect by giving all |
| 6728 | // declarations semantically contained within an anonymous |
| 6729 | // namespace internal linkage. |
| 6730 | |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6731 | if (!PrevNS) { |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6732 | UsingDirectiveDecl* UD |
Nick Lewycky | 3811582 | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6733 | = UsingDirectiveDecl::Create(Context, Parent, |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6734 | /* 'using' */ LBrace, |
| 6735 | /* 'namespace' */ SourceLocation(), |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 6736 | /* qualifier */ NestedNameSpecifierLoc(), |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6737 | /* identifier */ SourceLocation(), |
| 6738 | Namespc, |
Nick Lewycky | 3811582 | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6739 | /* Ancestor */ Parent); |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6740 | UD->setImplicit(); |
Nick Lewycky | 3811582 | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6741 | Parent->addDecl(UD); |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6742 | } |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6743 | } |
| 6744 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 6745 | ActOnDocumentableDecl(Namespc); |
| 6746 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6747 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 6748 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6749 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 6750 | // for the namespace has the declarations that showed up in that particular |
| 6751 | // namespace definition. |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6752 | PushDeclContext(NamespcScope, Namespc); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6753 | return Namespc; |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6754 | } |
| 6755 | |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6756 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 6757 | /// is a namespace alias, returns the namespace it points to. |
| 6758 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 6759 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 6760 | return AD->getNamespace(); |
| 6761 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 6762 | } |
| 6763 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6764 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 6765 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6766 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6767 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 6768 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6769 | Namespc->setRBraceLoc(RBrace); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6770 | PopDeclContext(); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 6771 | if (Namespc->hasAttr<VisibilityAttr>()) |
Rafael Espindola | 6d65d7b | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 6772 | PopPragmaVisibility(true, RBrace); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6773 | } |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6774 | |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 6775 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 6776 | return cast_or_null<CXXRecordDecl>( |
| 6777 | StdBadAlloc.get(Context.getExternalSource())); |
| 6778 | } |
| 6779 | |
| 6780 | NamespaceDecl *Sema::getStdNamespace() const { |
| 6781 | return cast_or_null<NamespaceDecl>( |
| 6782 | StdNamespace.get(Context.getExternalSource())); |
| 6783 | } |
| 6784 | |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6785 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 6786 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 4f8e173 | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 6787 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6788 | if (!StdNamespace) { |
| 6789 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 6790 | StdNamespace = NamespaceDecl::Create(Context, |
| 6791 | Context.getTranslationUnitDecl(), |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6792 | /*Inline=*/false, |
Abramo Bagnara | b5545be | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6793 | SourceLocation(), SourceLocation(), |
Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6794 | &PP.getIdentifierTable().get("std"), |
| 6795 | /*PrevDecl=*/0); |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 6796 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6797 | } |
| 6798 | |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 6799 | return getStdNamespace(); |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6800 | } |
| 6801 | |
Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6802 | bool Sema::isStdInitializerList(QualType Ty, QualType *Element) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6803 | assert(getLangOpts().CPlusPlus && |
Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6804 | "Looking for std::initializer_list outside of C++."); |
| 6805 | |
| 6806 | // We're looking for implicit instantiations of |
| 6807 | // template <typename E> class std::initializer_list. |
| 6808 | |
| 6809 | if (!StdNamespace) // If we haven't seen namespace std yet, this can't be it. |
| 6810 | return false; |
| 6811 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6812 | ClassTemplateDecl *Template = 0; |
| 6813 | const TemplateArgument *Arguments = 0; |
Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6814 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6815 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6816 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6817 | ClassTemplateSpecializationDecl *Specialization = |
| 6818 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 6819 | if (!Specialization) |
| 6820 | return false; |
Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6821 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6822 | Template = Specialization->getSpecializedTemplate(); |
| 6823 | Arguments = Specialization->getTemplateArgs().data(); |
| 6824 | } else if (const TemplateSpecializationType *TST = |
| 6825 | Ty->getAs<TemplateSpecializationType>()) { |
| 6826 | Template = dyn_cast_or_null<ClassTemplateDecl>( |
| 6827 | TST->getTemplateName().getAsTemplateDecl()); |
| 6828 | Arguments = TST->getArgs(); |
| 6829 | } |
| 6830 | if (!Template) |
| 6831 | return false; |
Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6832 | |
| 6833 | if (!StdInitializerList) { |
| 6834 | // Haven't recognized std::initializer_list yet, maybe this is it. |
| 6835 | CXXRecordDecl *TemplateClass = Template->getTemplatedDecl(); |
| 6836 | if (TemplateClass->getIdentifier() != |
| 6837 | &PP.getIdentifierTable().get("initializer_list") || |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6838 | !getStdNamespace()->InEnclosingNamespaceSetOf( |
| 6839 | TemplateClass->getDeclContext())) |
Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6840 | return false; |
| 6841 | // This is a template called std::initializer_list, but is it the right |
| 6842 | // template? |
| 6843 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6844 | if (Params->getMinRequiredArguments() != 1) |
Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6845 | return false; |
| 6846 | if (!isa<TemplateTypeParmDecl>(Params->getParam(0))) |
| 6847 | return false; |
| 6848 | |
| 6849 | // It's the right template. |
| 6850 | StdInitializerList = Template; |
| 6851 | } |
| 6852 | |
| 6853 | if (Template != StdInitializerList) |
| 6854 | return false; |
| 6855 | |
| 6856 | // This is an instance of std::initializer_list. Find the argument type. |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6857 | if (Element) |
| 6858 | *Element = Arguments[0].getAsType(); |
Sebastian Redl | 2bfa104 | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6859 | return true; |
| 6860 | } |
| 6861 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 6862 | static ClassTemplateDecl *LookupStdInitializerList(Sema &S, SourceLocation Loc){ |
| 6863 | NamespaceDecl *Std = S.getStdNamespace(); |
| 6864 | if (!Std) { |
| 6865 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 6866 | return 0; |
| 6867 | } |
| 6868 | |
| 6869 | LookupResult Result(S, &S.PP.getIdentifierTable().get("initializer_list"), |
| 6870 | Loc, Sema::LookupOrdinaryName); |
| 6871 | if (!S.LookupQualifiedName(Result, Std)) { |
| 6872 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 6873 | return 0; |
| 6874 | } |
| 6875 | ClassTemplateDecl *Template = Result.getAsSingle<ClassTemplateDecl>(); |
| 6876 | if (!Template) { |
| 6877 | Result.suppressDiagnostics(); |
| 6878 | // We found something weird. Complain about the first thing we found. |
| 6879 | NamedDecl *Found = *Result.begin(); |
| 6880 | S.Diag(Found->getLocation(), diag::err_malformed_std_initializer_list); |
| 6881 | return 0; |
| 6882 | } |
| 6883 | |
| 6884 | // We found some template called std::initializer_list. Now verify that it's |
| 6885 | // correct. |
| 6886 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6887 | if (Params->getMinRequiredArguments() != 1 || |
| 6888 | !isa<TemplateTypeParmDecl>(Params->getParam(0))) { |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 6889 | S.Diag(Template->getLocation(), diag::err_malformed_std_initializer_list); |
| 6890 | return 0; |
| 6891 | } |
| 6892 | |
| 6893 | return Template; |
| 6894 | } |
| 6895 | |
| 6896 | QualType Sema::BuildStdInitializerList(QualType Element, SourceLocation Loc) { |
| 6897 | if (!StdInitializerList) { |
| 6898 | StdInitializerList = LookupStdInitializerList(*this, Loc); |
| 6899 | if (!StdInitializerList) |
| 6900 | return QualType(); |
| 6901 | } |
| 6902 | |
| 6903 | TemplateArgumentListInfo Args(Loc, Loc); |
| 6904 | Args.addArgument(TemplateArgumentLoc(TemplateArgument(Element), |
| 6905 | Context.getTrivialTypeSourceInfo(Element, |
| 6906 | Loc))); |
| 6907 | return Context.getCanonicalType( |
| 6908 | CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args)); |
| 6909 | } |
| 6910 | |
Sebastian Redl | be24ec2 | 2012-01-17 22:50:14 +0000 | [diff] [blame] | 6911 | bool Sema::isInitListConstructor(const CXXConstructorDecl* Ctor) { |
| 6912 | // C++ [dcl.init.list]p2: |
| 6913 | // A constructor is an initializer-list constructor if its first parameter |
| 6914 | // is of type std::initializer_list<E> or reference to possibly cv-qualified |
| 6915 | // std::initializer_list<E> for some type E, and either there are no other |
| 6916 | // parameters or else all other parameters have default arguments. |
| 6917 | if (Ctor->getNumParams() < 1 || |
| 6918 | (Ctor->getNumParams() > 1 && !Ctor->getParamDecl(1)->hasDefaultArg())) |
| 6919 | return false; |
| 6920 | |
| 6921 | QualType ArgType = Ctor->getParamDecl(0)->getType(); |
| 6922 | if (const ReferenceType *RT = ArgType->getAs<ReferenceType>()) |
| 6923 | ArgType = RT->getPointeeType().getUnqualifiedType(); |
| 6924 | |
| 6925 | return isStdInitializerList(ArgType, 0); |
| 6926 | } |
| 6927 | |
Douglas Gregor | a172e08 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 6928 | /// \brief Determine whether a using statement is in a context where it will be |
| 6929 | /// apply in all contexts. |
| 6930 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 6931 | switch (CurContext->getDeclKind()) { |
| 6932 | case Decl::TranslationUnit: |
| 6933 | return true; |
| 6934 | case Decl::LinkageSpec: |
| 6935 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 6936 | default: |
| 6937 | return false; |
| 6938 | } |
| 6939 | } |
| 6940 | |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6941 | namespace { |
| 6942 | |
| 6943 | // Callback to only accept typo corrections that are namespaces. |
| 6944 | class NamespaceValidatorCCC : public CorrectionCandidateCallback { |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 6945 | public: |
| 6946 | bool ValidateCandidate(const TypoCorrection &candidate) LLVM_OVERRIDE { |
| 6947 | if (NamedDecl *ND = candidate.getCorrectionDecl()) |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6948 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6949 | return false; |
| 6950 | } |
| 6951 | }; |
| 6952 | |
| 6953 | } |
| 6954 | |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6955 | static bool TryNamespaceTypoCorrection(Sema &S, LookupResult &R, Scope *Sc, |
| 6956 | CXXScopeSpec &SS, |
| 6957 | SourceLocation IdentLoc, |
| 6958 | IdentifierInfo *Ident) { |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6959 | NamespaceValidatorCCC Validator; |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6960 | R.clear(); |
| 6961 | if (TypoCorrection Corrected = S.CorrectTypo(R.getLookupNameInfo(), |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6962 | R.getLookupKind(), Sc, &SS, |
Kaelyn Uhrain | 4e8942c | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 6963 | Validator)) { |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6964 | if (DeclContext *DC = S.computeDeclContext(SS, false)) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6965 | std::string CorrectedStr(Corrected.getAsString(S.getLangOpts())); |
| 6966 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6967 | Ident->getName().equals(CorrectedStr); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6968 | S.diagnoseTypo(Corrected, |
| 6969 | S.PDiag(diag::err_using_directive_member_suggest) |
| 6970 | << Ident << DC << DroppedSpecifier << SS.getRange(), |
| 6971 | S.PDiag(diag::note_namespace_defined_here)); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6972 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6973 | S.diagnoseTypo(Corrected, |
| 6974 | S.PDiag(diag::err_using_directive_suggest) << Ident, |
| 6975 | S.PDiag(diag::note_namespace_defined_here)); |
Kaelyn Uhrain | 10413a4 | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6976 | } |
Kaelyn Uhrain | 2d317ed | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6977 | R.addDecl(Corrected.getCorrectionDecl()); |
| 6978 | return true; |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6979 | } |
| 6980 | return false; |
| 6981 | } |
| 6982 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6983 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6984 | SourceLocation UsingLoc, |
| 6985 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6986 | CXXScopeSpec &SS, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6987 | SourceLocation IdentLoc, |
| 6988 | IdentifierInfo *NamespcName, |
| 6989 | AttributeList *AttrList) { |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6990 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 6991 | assert(NamespcName && "Invalid NamespcName."); |
| 6992 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 6993 | |
| 6994 | // This can only happen along a recovery path. |
| 6995 | while (S->getFlags() & Scope::TemplateParamScope) |
| 6996 | S = S->getParent(); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6997 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6998 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6999 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7000 | NestedNameSpecifier *Qualifier = 0; |
| 7001 | if (SS.isSet()) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7002 | Qualifier = SS.getScopeRep(); |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7003 | |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 7004 | // Lookup namespace name. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7005 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 7006 | LookupParsedName(R, S, &SS); |
| 7007 | if (R.isAmbiguous()) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7008 | return 0; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7009 | |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7010 | if (R.empty()) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 7011 | R.clear(); |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7012 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 7013 | // "std" hasn't been defined yet, for GCC compatibility. |
| 7014 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 7015 | NamespcName->isStr("std")) { |
| 7016 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 4f8e173 | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 7017 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7018 | R.resolveKind(); |
| 7019 | } |
| 7020 | // Otherwise, attempt typo correction. |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 7021 | else TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, NamespcName); |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 7022 | } |
| 7023 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7024 | if (!R.empty()) { |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 7025 | NamedDecl *Named = R.getFoundDecl(); |
| 7026 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 7027 | && "expected namespace decl"); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7028 | // C++ [namespace.udir]p1: |
| 7029 | // A using-directive specifies that the names in the nominated |
| 7030 | // namespace can be used in the scope in which the |
| 7031 | // using-directive appears after the using-directive. During |
| 7032 | // unqualified name lookup (3.4.1), the names appear as if they |
| 7033 | // were declared in the nearest enclosing namespace which |
| 7034 | // contains both the using-directive and the nominated |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 7035 | // namespace. [Note: in this context, "contains" means "contains |
| 7036 | // directly or indirectly". ] |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7037 | |
| 7038 | // Find enclosing context containing both using-directive and |
| 7039 | // nominated namespace. |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 7040 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7041 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 7042 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 7043 | CommonAncestor = CommonAncestor->getParent(); |
| 7044 | |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 7045 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 7046 | SS.getWithLocInContext(Context), |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 7047 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | 96a4bdd | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 7048 | |
Douglas Gregor | a172e08 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 7049 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
Eli Friedman | 5ba37d5 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 7050 | !SourceMgr.isInMainFile(SourceMgr.getExpansionLoc(IdentLoc))) { |
Douglas Gregor | 96a4bdd | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 7051 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 7052 | } |
| 7053 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7054 | PushUsingDirective(S, UDir); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 7055 | } else { |
Chris Lattner | 8dca2e9 | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 7056 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 7057 | } |
| 7058 | |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 7059 | if (UDir) |
| 7060 | ProcessDeclAttributeList(S, UDir, AttrList); |
| 7061 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7062 | return UDir; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7063 | } |
| 7064 | |
| 7065 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 7066 | // If the scope has an associated entity and the using directive is at |
| 7067 | // namespace or translation unit scope, add the UsingDirectiveDecl into |
| 7068 | // its lookup structure so qualified name lookup can find it. |
Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 7069 | DeclContext *Ctx = S->getEntity(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 7070 | if (Ctx && !Ctx->isFunctionOrMethod()) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 7071 | Ctx->addDecl(UDir); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 7072 | else |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 7073 | // Otherwise, it is at block sope. The using-directives will affect lookup |
| 7074 | // only to the end of the scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7075 | S->PushUsingDirective(UDir); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 7076 | } |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7077 | |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7078 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7079 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 7080 | AccessSpecifier AS, |
| 7081 | bool HasUsingKeyword, |
| 7082 | SourceLocation UsingLoc, |
| 7083 | CXXScopeSpec &SS, |
| 7084 | UnqualifiedId &Name, |
| 7085 | AttributeList *AttrList, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7086 | bool HasTypenameKeyword, |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 7087 | SourceLocation TypenameLoc) { |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7088 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7089 | |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7090 | switch (Name.getKind()) { |
Fariborz Jahanian | 7f4427f | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 7091 | case UnqualifiedId::IK_ImplicitSelfParam: |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7092 | case UnqualifiedId::IK_Identifier: |
| 7093 | case UnqualifiedId::IK_OperatorFunctionId: |
Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 7094 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7095 | case UnqualifiedId::IK_ConversionFunctionId: |
| 7096 | break; |
| 7097 | |
| 7098 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 7099 | case UnqualifiedId::IK_ConstructorTemplateId: |
Richard Smith | d494c50 | 2012-04-27 19:33:05 +0000 | [diff] [blame] | 7100 | // C++11 inheriting constructors. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7101 | Diag(Name.getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7102 | getLangOpts().CPlusPlus11 ? |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 7103 | diag::warn_cxx98_compat_using_decl_constructor : |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 7104 | diag::err_using_decl_constructor) |
| 7105 | << SS.getRange(); |
| 7106 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7107 | if (getLangOpts().CPlusPlus11) break; |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7108 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7109 | return 0; |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7110 | |
| 7111 | case UnqualifiedId::IK_DestructorName: |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7112 | Diag(Name.getLocStart(), diag::err_using_decl_destructor) |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7113 | << SS.getRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7114 | return 0; |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7115 | |
| 7116 | case UnqualifiedId::IK_TemplateId: |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 7117 | Diag(Name.getLocStart(), diag::err_using_decl_template_id) |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7118 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7119 | return 0; |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 7120 | } |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7121 | |
| 7122 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 7123 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7124 | if (!TargetName) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7125 | return 0; |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7126 | |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 7127 | // Warn about access declarations. |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 7128 | if (!HasUsingKeyword) { |
Enea Zaffanella | c70b251 | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 7129 | Diag(Name.getLocStart(), |
Richard Smith | f026b60 | 2013-06-13 02:12:17 +0000 | [diff] [blame] | 7130 | getLangOpts().CPlusPlus11 ? diag::err_access_decl |
| 7131 | : diag::warn_access_decl_deprecated) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7132 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 7133 | } |
| 7134 | |
Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 7135 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 7136 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 7137 | return 0; |
| 7138 | |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7139 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7140 | TargetNameInfo, AttrList, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7141 | /* IsInstantiation */ false, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7142 | HasTypenameKeyword, TypenameLoc); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7143 | if (UD) |
| 7144 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7145 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7146 | return UD; |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7147 | } |
| 7148 | |
Douglas Gregor | 1d9ef84 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7149 | /// \brief Determine whether a using declaration considers the given |
| 7150 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 7151 | /// the same entity or are both typedefs of the same type. |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7152 | static bool |
| 7153 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2) { |
| 7154 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) |
Douglas Gregor | 1d9ef84 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7155 | return true; |
Douglas Gregor | 1d9ef84 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7156 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7157 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7158 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) |
Douglas Gregor | 1d9ef84 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7159 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 7160 | TD2->getUnderlyingType()); |
Douglas Gregor | 1d9ef84 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 7161 | |
| 7162 | return false; |
| 7163 | } |
| 7164 | |
| 7165 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7166 | /// Determines whether to create a using shadow decl for a particular |
| 7167 | /// decl, given the set of decls existing prior to this using lookup. |
| 7168 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7169 | const LookupResult &Previous, |
| 7170 | UsingShadowDecl *&PrevShadow) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7171 | // Diagnose finding a decl which is not from a base class of the |
| 7172 | // current class. We do this now because there are cases where this |
| 7173 | // function will silently decide not to build a shadow decl, which |
| 7174 | // will pre-empt further diagnostics. |
| 7175 | // |
| 7176 | // We don't need to do this in C++0x because we do the check once on |
| 7177 | // the qualifier. |
| 7178 | // |
| 7179 | // FIXME: diagnose the following if we care enough: |
| 7180 | // struct A { int foo; }; |
| 7181 | // struct B : A { using A::foo; }; |
| 7182 | // template <class T> struct C : A {}; |
| 7183 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 7184 | // This is invalid (during instantiation) in C++03 because B::foo |
| 7185 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 7186 | // We can't diagnose it immediately because C<T> is an unknown |
| 7187 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 7188 | // to A::foo, which will look well-formed when we instantiate. |
| 7189 | // The right solution is to not collapse the shadow-decl chain. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7190 | if (!getLangOpts().CPlusPlus11 && CurContext->isRecord()) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7191 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 7192 | |
| 7193 | // Handle enums and anonymous structs. |
| 7194 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 7195 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 7196 | while (OrigRec->isAnonymousStructOrUnion()) |
| 7197 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 7198 | |
| 7199 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 7200 | if (OrigDC == CurContext) { |
| 7201 | Diag(Using->getLocation(), |
| 7202 | diag::err_using_decl_nested_name_specifier_is_current_class) |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7203 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7204 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 7205 | return true; |
| 7206 | } |
| 7207 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7208 | Diag(Using->getQualifierLoc().getBeginLoc(), |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7209 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7210 | << Using->getQualifier() |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7211 | << cast<CXXRecordDecl>(CurContext) |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7212 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7213 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 7214 | return true; |
| 7215 | } |
| 7216 | } |
| 7217 | |
| 7218 | if (Previous.empty()) return false; |
| 7219 | |
| 7220 | NamedDecl *Target = Orig; |
| 7221 | if (isa<UsingShadowDecl>(Target)) |
| 7222 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 7223 | |
John McCall | a17e83e | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 7224 | // If the target happens to be one of the previous declarations, we |
| 7225 | // don't have a conflict. |
| 7226 | // |
| 7227 | // FIXME: but we might be increasing its access, in which case we |
| 7228 | // should redeclare it. |
| 7229 | NamedDecl *NonTag = 0, *Tag = 0; |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7230 | bool FoundEquivalentDecl = false; |
John McCall | a17e83e | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 7231 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 7232 | I != E; ++I) { |
| 7233 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7234 | if (IsEquivalentForUsingDecl(Context, D, Target)) { |
| 7235 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(*I)) |
| 7236 | PrevShadow = Shadow; |
| 7237 | FoundEquivalentDecl = true; |
| 7238 | } |
John McCall | a17e83e | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 7239 | |
| 7240 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 7241 | } |
| 7242 | |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7243 | if (FoundEquivalentDecl) |
| 7244 | return false; |
| 7245 | |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 7246 | if (FunctionDecl *FD = Target->getAsFunction()) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7247 | NamedDecl *OldDecl = 0; |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 7248 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7249 | case Ovl_Overload: |
| 7250 | return false; |
| 7251 | |
| 7252 | case Ovl_NonFunction: |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7253 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7254 | break; |
Richard Smith | 1881930 | 2014-02-06 01:31:33 +0000 | [diff] [blame] | 7255 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7256 | // We found a decl with the exact signature. |
| 7257 | case Ovl_Match: |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7258 | // If we're in a record, we want to hide the target, so we |
| 7259 | // return true (without a diagnostic) to tell the caller not to |
| 7260 | // build a shadow decl. |
| 7261 | if (CurContext->isRecord()) |
| 7262 | return true; |
| 7263 | |
| 7264 | // If we're not in a record, this is an error. |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7265 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7266 | break; |
| 7267 | } |
| 7268 | |
| 7269 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 7270 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 7271 | return true; |
| 7272 | } |
| 7273 | |
| 7274 | // Target is not a function. |
| 7275 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7276 | if (isa<TagDecl>(Target)) { |
| 7277 | // No conflict between a tag and a non-tag. |
| 7278 | if (!Tag) return false; |
| 7279 | |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7280 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7281 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 7282 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 7283 | return true; |
| 7284 | } |
| 7285 | |
| 7286 | // No conflict between a tag and a non-tag. |
| 7287 | if (!NonTag) return false; |
| 7288 | |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7289 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7290 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 7291 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 7292 | return true; |
| 7293 | } |
| 7294 | |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7295 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7296 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7297 | UsingDecl *UD, |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7298 | NamedDecl *Orig, |
| 7299 | UsingShadowDecl *PrevDecl) { |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7300 | |
| 7301 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7302 | NamedDecl *Target = Orig; |
| 7303 | if (isa<UsingShadowDecl>(Target)) { |
| 7304 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 7305 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7306 | } |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7307 | |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7308 | UsingShadowDecl *Shadow |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7309 | = UsingShadowDecl::Create(Context, CurContext, |
| 7310 | UD->getLocation(), UD, Target); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7311 | UD->addShadowDecl(Shadow); |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7312 | |
Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 7313 | Shadow->setAccess(UD->getAccess()); |
| 7314 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 7315 | Shadow->setInvalidDecl(); |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7316 | |
| 7317 | Shadow->setPreviousDecl(PrevDecl); |
| 7318 | |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7319 | if (S) |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7320 | PushOnScopeChains(Shadow, S); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7321 | else |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7322 | CurContext->addDecl(Shadow); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7323 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7324 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7325 | return Shadow; |
| 7326 | } |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7327 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7328 | /// Hides a using shadow declaration. This is required by the current |
| 7329 | /// using-decl implementation when a resolvable using declaration in a |
| 7330 | /// class is followed by a declaration which would hide or override |
| 7331 | /// one or more of the using decl's targets; for example: |
| 7332 | /// |
| 7333 | /// struct Base { void foo(int); }; |
| 7334 | /// struct Derived : Base { |
| 7335 | /// using Base::foo; |
| 7336 | /// void foo(int); |
| 7337 | /// }; |
| 7338 | /// |
| 7339 | /// The governing language is C++03 [namespace.udecl]p12: |
| 7340 | /// |
| 7341 | /// When a using-declaration brings names from a base class into a |
| 7342 | /// derived class scope, member functions in the derived class |
| 7343 | /// override and/or hide member functions with the same name and |
| 7344 | /// parameter types in a base class (rather than conflicting). |
| 7345 | /// |
| 7346 | /// There are two ways to implement this: |
| 7347 | /// (1) optimistically create shadow decls when they're not hidden |
| 7348 | /// by existing declarations, or |
| 7349 | /// (2) don't create any shadow decls (or at least don't make them |
| 7350 | /// visible) until we've fully parsed/instantiated the class. |
| 7351 | /// The problem with (1) is that we might have to retroactively remove |
| 7352 | /// a shadow decl, which requires several O(n) operations because the |
| 7353 | /// decl structures are (very reasonably) not designed for removal. |
| 7354 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 7355 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 7356 | if (Shadow->getDeclName().getNameKind() == |
| 7357 | DeclarationName::CXXConversionFunctionName) |
| 7358 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 7359 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7360 | // Remove it from the DeclContext... |
| 7361 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7362 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7363 | // ...and the scope, if applicable... |
| 7364 | if (S) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7365 | S->RemoveDecl(Shadow); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7366 | IdResolver.RemoveDecl(Shadow); |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7367 | } |
| 7368 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7369 | // ...and the using decl. |
| 7370 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 7371 | |
| 7372 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 7373 | // be possible for this to happen, because...? |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7374 | } |
| 7375 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7376 | namespace { |
Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7377 | class UsingValidatorCCC : public CorrectionCandidateCallback { |
| 7378 | public: |
Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 7379 | UsingValidatorCCC(bool HasTypenameKeyword, bool IsInstantiation, |
| 7380 | bool RequireMember) |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7381 | : HasTypenameKeyword(HasTypenameKeyword), |
Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 7382 | IsInstantiation(IsInstantiation), RequireMember(RequireMember) {} |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7383 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7384 | bool ValidateCandidate(const TypoCorrection &Candidate) LLVM_OVERRIDE { |
| 7385 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
| 7386 | |
| 7387 | // Keywords are not valid here. |
| 7388 | if (!ND || isa<NamespaceDecl>(ND)) |
Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7389 | return false; |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7390 | |
Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 7391 | if (RequireMember && !isa<FieldDecl>(ND) && !isa<CXXMethodDecl>(ND) && |
| 7392 | !isa<TypeDecl>(ND)) |
| 7393 | return false; |
| 7394 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7395 | // Completely unqualified names are invalid for a 'using' declaration. |
| 7396 | if (Candidate.WillReplaceSpecifier() && !Candidate.getCorrectionSpecifier()) |
| 7397 | return false; |
| 7398 | |
| 7399 | if (isa<TypeDecl>(ND)) |
| 7400 | return HasTypenameKeyword || !IsInstantiation; |
| 7401 | |
| 7402 | return !HasTypenameKeyword; |
Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7403 | } |
| 7404 | |
| 7405 | private: |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7406 | bool HasTypenameKeyword; |
Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7407 | bool IsInstantiation; |
Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 7408 | bool RequireMember; |
Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7409 | }; |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7410 | } // end anonymous namespace |
Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7411 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7412 | /// Builds a using declaration. |
| 7413 | /// |
| 7414 | /// \param IsInstantiation - Whether this call arises from an |
| 7415 | /// instantiation of an unresolved using declaration. We treat |
| 7416 | /// the lookup differently for these declarations. |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7417 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 7418 | SourceLocation UsingLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7419 | CXXScopeSpec &SS, |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7420 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7421 | AttributeList *AttrList, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7422 | bool IsInstantiation, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7423 | bool HasTypenameKeyword, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7424 | SourceLocation TypenameLoc) { |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7425 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7426 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7427 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 561154d | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 7428 | |
Anders Carlsson | f038fc2 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 7429 | // FIXME: We ignore attributes for now. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7430 | |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7431 | if (SS.isEmpty()) { |
| 7432 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7433 | return 0; |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7434 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7435 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7436 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7437 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7438 | ForRedeclaration); |
| 7439 | Previous.setHideTags(false); |
| 7440 | if (S) { |
| 7441 | LookupName(Previous, S); |
| 7442 | |
| 7443 | // It is really dumb that we have to do this. |
| 7444 | LookupResult::Filter F = Previous.makeFilter(); |
| 7445 | while (F.hasNext()) { |
| 7446 | NamedDecl *D = F.next(); |
| 7447 | if (!isDeclInScope(D, CurContext, S)) |
| 7448 | F.erase(); |
| 7449 | } |
| 7450 | F.done(); |
| 7451 | } else { |
| 7452 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 7453 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 7454 | LookupQualifiedName(Previous, CurContext); |
| 7455 | } |
| 7456 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7457 | // Check for invalid redeclarations. |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7458 | if (CheckUsingDeclRedeclaration(UsingLoc, HasTypenameKeyword, |
| 7459 | SS, IdentLoc, Previous)) |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7460 | return 0; |
| 7461 | |
| 7462 | // Check for bad qualifiers. |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7463 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 7464 | return 0; |
| 7465 | |
John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 7466 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7467 | NamedDecl *D; |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7468 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 7469 | if (!LookupContext) { |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7470 | if (HasTypenameKeyword) { |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7471 | // FIXME: not all declaration name kinds are legal here |
| 7472 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 7473 | UsingLoc, TypenameLoc, |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7474 | QualifierLoc, |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7475 | IdentLoc, NameInfo.getName()); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7476 | } else { |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7477 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 7478 | QualifierLoc, NameInfo); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7479 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7480 | } else { |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7481 | D = UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7482 | NameInfo, HasTypenameKeyword); |
Anders Carlsson | f038fc2 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 7483 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7484 | D->setAccess(AS); |
| 7485 | CurContext->addDecl(D); |
| 7486 | |
| 7487 | if (!LookupContext) return D; |
| 7488 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7489 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7490 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7491 | UD->setInvalidDecl(); |
| 7492 | return UD; |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7493 | } |
| 7494 | |
Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7495 | // The normal rules do not apply to inheriting constructor declarations. |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7496 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7497 | if (CheckInheritingConstructorUsingDecl(UD)) |
Sebastian Redl | c1f8e49 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 7498 | UD->setInvalidDecl(); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7499 | return UD; |
| 7500 | } |
| 7501 | |
| 7502 | // Otherwise, look up the target name. |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7503 | |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7504 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7505 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7506 | // Unlike most lookups, we don't always want to hide tag |
| 7507 | // declarations: tag names are visible through the using declaration |
| 7508 | // even if hidden by ordinary names, *except* in a dependent context |
| 7509 | // where it's important for the sanity of two-phase lookup. |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7510 | if (!IsInstantiation) |
| 7511 | R.setHideTags(false); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7512 | |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 7513 | // For the purposes of this lookup, we have a base object type |
| 7514 | // equal to that of the current context. |
| 7515 | if (CurContext->isRecord()) { |
| 7516 | R.setBaseObjectType( |
| 7517 | Context.getTypeDeclType(cast<CXXRecordDecl>(CurContext))); |
| 7518 | } |
| 7519 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7520 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7521 | |
Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7522 | // Try to correct typos if possible. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7523 | if (R.empty()) { |
Kaelyn Uhrain | 8aa8da8 | 2013-10-19 00:05:00 +0000 | [diff] [blame] | 7524 | UsingValidatorCCC CCC(HasTypenameKeyword, IsInstantiation, |
| 7525 | CurContext->isRecord()); |
Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7526 | if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(), |
| 7527 | R.getLookupKind(), S, &SS, CCC)){ |
| 7528 | // We reject any correction for which ND would be NULL. |
| 7529 | NamedDecl *ND = Corrected.getCorrectionDecl(); |
Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7530 | R.setLookupName(Corrected.getCorrection()); |
| 7531 | R.addDecl(ND); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 7532 | // We reject candidates where DroppedSpecifier == true, hence the |
Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7533 | // literal '0' below. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 7534 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest) |
| 7535 | << NameInfo.getName() << LookupContext << 0 |
| 7536 | << SS.getRange()); |
Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7537 | } else { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 7538 | Diag(IdentLoc, diag::err_no_member) |
Kaelyn Uhrain | 8ec9f5f | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7539 | << NameInfo.getName() << LookupContext << SS.getRange(); |
| 7540 | UD->setInvalidDecl(); |
| 7541 | return UD; |
| 7542 | } |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7543 | } |
| 7544 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7545 | if (R.isAmbiguous()) { |
| 7546 | UD->setInvalidDecl(); |
| 7547 | return UD; |
| 7548 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7549 | |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7550 | if (HasTypenameKeyword) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7551 | // 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] | 7552 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7553 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 7554 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 7555 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 7556 | diag::note_using_decl_target); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7557 | UD->setInvalidDecl(); |
| 7558 | return UD; |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7559 | } |
| 7560 | } else { |
| 7561 | // If we asked for a non-typename and we got a type, error out, |
| 7562 | // but only if this is an instantiation of an unresolved using |
| 7563 | // decl. Otherwise just silently find the type name. |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7564 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7565 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 7566 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7567 | UD->setInvalidDecl(); |
| 7568 | return UD; |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7569 | } |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7570 | } |
| 7571 | |
Anders Carlsson | 5a9c5ac | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 7572 | // C++0x N2914 [namespace.udecl]p6: |
| 7573 | // A using-declaration shall not name a namespace. |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7574 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 5a9c5ac | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 7575 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 7576 | << SS.getRange(); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7577 | UD->setInvalidDecl(); |
| 7578 | return UD; |
Anders Carlsson | 5a9c5ac | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 7579 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7580 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7581 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 7582 | UsingShadowDecl *PrevDecl = 0; |
| 7583 | if (!CheckUsingShadowDecl(UD, *I, Previous, PrevDecl)) |
| 7584 | BuildUsingShadowDecl(S, UD, *I, PrevDecl); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7585 | } |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7586 | |
| 7587 | return UD; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7588 | } |
| 7589 | |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7590 | /// Additional checks for a using declaration referring to a constructor name. |
Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7591 | bool Sema::CheckInheritingConstructorUsingDecl(UsingDecl *UD) { |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7592 | assert(!UD->hasTypename() && "expecting a constructor name"); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7593 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7594 | const Type *SourceType = UD->getQualifier()->getAsType(); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7595 | assert(SourceType && |
| 7596 | "Using decl naming constructor doesn't have type in scope spec."); |
| 7597 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 7598 | |
| 7599 | // Check whether the named type is a direct base class. |
| 7600 | CanQualType CanonicalSourceType = SourceType->getCanonicalTypeUnqualified(); |
| 7601 | CXXRecordDecl::base_class_iterator BaseIt, BaseE; |
| 7602 | for (BaseIt = TargetClass->bases_begin(), BaseE = TargetClass->bases_end(); |
| 7603 | BaseIt != BaseE; ++BaseIt) { |
| 7604 | CanQualType BaseType = BaseIt->getType()->getCanonicalTypeUnqualified(); |
| 7605 | if (CanonicalSourceType == BaseType) |
| 7606 | break; |
Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7607 | if (BaseIt->getType()->isDependentType()) |
| 7608 | break; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7609 | } |
| 7610 | |
| 7611 | if (BaseIt == BaseE) { |
| 7612 | // Did not find SourceType in the bases. |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7613 | Diag(UD->getUsingLoc(), |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7614 | diag::err_using_decl_constructor_not_in_direct_base) |
| 7615 | << UD->getNameInfo().getSourceRange() |
| 7616 | << QualType(SourceType, 0) << TargetClass; |
| 7617 | return true; |
| 7618 | } |
| 7619 | |
Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7620 | if (!CurContext->isDependentContext()) |
| 7621 | BaseIt->setInheritConstructors(); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7622 | |
| 7623 | return false; |
| 7624 | } |
| 7625 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7626 | /// Checks that the given using declaration is not an invalid |
| 7627 | /// redeclaration. Note that this is checking only for the using decl |
| 7628 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 7629 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7630 | bool HasTypenameKeyword, |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7631 | const CXXScopeSpec &SS, |
| 7632 | SourceLocation NameLoc, |
| 7633 | const LookupResult &Prev) { |
| 7634 | // C++03 [namespace.udecl]p8: |
| 7635 | // C++0x [namespace.udecl]p10: |
| 7636 | // A using-declaration is a declaration and can therefore be used |
| 7637 | // repeatedly where (and only where) multiple declarations are |
| 7638 | // allowed. |
Douglas Gregor | 4b718ee | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 7639 | // |
John McCall | 032092f | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 7640 | // That's in non-member contexts. |
| 7641 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7642 | return false; |
| 7643 | |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7644 | NestedNameSpecifier *Qual = SS.getScopeRep(); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7645 | |
| 7646 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 7647 | NamedDecl *D = *I; |
| 7648 | |
| 7649 | bool DTypename; |
| 7650 | NestedNameSpecifier *DQual; |
| 7651 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7652 | DTypename = UD->hasTypename(); |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7653 | DQual = UD->getQualifier(); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7654 | } else if (UnresolvedUsingValueDecl *UD |
| 7655 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 7656 | DTypename = false; |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7657 | DQual = UD->getQualifier(); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7658 | } else if (UnresolvedUsingTypenameDecl *UD |
| 7659 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 7660 | DTypename = true; |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7661 | DQual = UD->getQualifier(); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7662 | } else continue; |
| 7663 | |
| 7664 | // using decls differ if one says 'typename' and the other doesn't. |
| 7665 | // FIXME: non-dependent using decls? |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7666 | if (HasTypenameKeyword != DTypename) continue; |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7667 | |
| 7668 | // using decls differ if they name different scopes (but note that |
| 7669 | // template instantiation can cause this check to trigger when it |
| 7670 | // didn't before instantiation). |
| 7671 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 7672 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 7673 | continue; |
| 7674 | |
| 7675 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7676 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7677 | return true; |
| 7678 | } |
| 7679 | |
| 7680 | return false; |
| 7681 | } |
| 7682 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7683 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7684 | /// Checks that the given nested-name qualifier used in a using decl |
| 7685 | /// in the current context is appropriately related to the current |
| 7686 | /// scope. If an error is found, diagnoses it and returns true. |
| 7687 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 7688 | const CXXScopeSpec &SS, |
| 7689 | SourceLocation NameLoc) { |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7690 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7691 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7692 | if (!CurContext->isRecord()) { |
| 7693 | // C++03 [namespace.udecl]p3: |
| 7694 | // C++0x [namespace.udecl]p8: |
| 7695 | // A using-declaration for a class member shall be a member-declaration. |
| 7696 | |
| 7697 | // If we weren't able to compute a valid scope, it must be a |
| 7698 | // dependent class scope. |
| 7699 | if (!NamedContext || NamedContext->isRecord()) { |
| 7700 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 7701 | << SS.getRange(); |
| 7702 | return true; |
| 7703 | } |
| 7704 | |
| 7705 | // Otherwise, everything is known to be fine. |
| 7706 | return false; |
| 7707 | } |
| 7708 | |
| 7709 | // The current scope is a record. |
| 7710 | |
| 7711 | // If the named context is dependent, we can't decide much. |
| 7712 | if (!NamedContext) { |
| 7713 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 7714 | // nested-name-specifier does not refer to a base class, which is |
| 7715 | // still possible in some cases. |
| 7716 | |
| 7717 | // Otherwise we have to conservatively report that things might be |
| 7718 | // okay. |
| 7719 | return false; |
| 7720 | } |
| 7721 | |
| 7722 | if (!NamedContext->isRecord()) { |
| 7723 | // Ideally this would point at the last name in the specifier, |
| 7724 | // but we don't have that level of source info. |
| 7725 | Diag(SS.getRange().getBegin(), |
| 7726 | diag::err_using_decl_nested_name_specifier_is_not_class) |
Aaron Ballman | 5fe6c80 | 2014-01-03 13:45:46 +0000 | [diff] [blame] | 7727 | << SS.getScopeRep() << SS.getRange(); |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7728 | return true; |
| 7729 | } |
| 7730 | |
Douglas Gregor | 7c84229 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 7731 | if (!NamedContext->isDependentContext() && |
| 7732 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 7733 | return true; |
| 7734 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7735 | if (getLangOpts().CPlusPlus11) { |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7736 | // C++0x [namespace.udecl]p3: |
| 7737 | // In a using-declaration used as a member-declaration, the |
| 7738 | // nested-name-specifier shall name a base class of the class |
| 7739 | // being defined. |
| 7740 | |
| 7741 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 7742 | cast<CXXRecordDecl>(NamedContext))) { |
| 7743 | if (CurContext == NamedContext) { |
| 7744 | Diag(NameLoc, |
| 7745 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 7746 | << SS.getRange(); |
| 7747 | return true; |
| 7748 | } |
| 7749 | |
| 7750 | Diag(SS.getRange().getBegin(), |
| 7751 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7752 | << SS.getScopeRep() |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7753 | << cast<CXXRecordDecl>(CurContext) |
| 7754 | << SS.getRange(); |
| 7755 | return true; |
| 7756 | } |
| 7757 | |
| 7758 | return false; |
| 7759 | } |
| 7760 | |
| 7761 | // C++03 [namespace.udecl]p4: |
| 7762 | // A using-declaration used as a member-declaration shall refer |
| 7763 | // to a member of a base class of the class being defined [etc.]. |
| 7764 | |
| 7765 | // Salient point: SS doesn't have to name a base class as long as |
| 7766 | // lookup only finds members from base classes. Therefore we can |
| 7767 | // diagnose here only if we can prove that that can't happen, |
| 7768 | // i.e. if the class hierarchies provably don't intersect. |
| 7769 | |
| 7770 | // TODO: it would be nice if "definitely valid" results were cached |
| 7771 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 7772 | // need to be repeated. |
| 7773 | |
| 7774 | struct UserData { |
Benjamin Kramer | 3adbe1c | 2012-02-23 16:06:01 +0000 | [diff] [blame] | 7775 | llvm::SmallPtrSet<const CXXRecordDecl*, 4> Bases; |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7776 | |
| 7777 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 7778 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 7779 | Data->Bases.insert(Base); |
| 7780 | return true; |
| 7781 | } |
| 7782 | |
| 7783 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 7784 | return !Class->forallBases(collect, this); |
| 7785 | } |
| 7786 | |
| 7787 | /// Returns true if the base is dependent or is one of the |
| 7788 | /// accumulated base classes. |
| 7789 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 7790 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 7791 | return !Data->Bases.count(Base); |
| 7792 | } |
| 7793 | |
| 7794 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 7795 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 7796 | } |
| 7797 | }; |
| 7798 | |
| 7799 | UserData Data; |
| 7800 | |
| 7801 | // Returns false if we find a dependent base. |
| 7802 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 7803 | return false; |
| 7804 | |
| 7805 | // Returns false if the class has a dependent base or if it or one |
| 7806 | // of its bases is present in the base set of the current context. |
| 7807 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 7808 | return false; |
| 7809 | |
| 7810 | Diag(SS.getRange().getBegin(), |
| 7811 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Aaron Ballman | 4a97967 | 2014-01-03 13:56:08 +0000 | [diff] [blame] | 7812 | << SS.getScopeRep() |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7813 | << cast<CXXRecordDecl>(CurContext) |
| 7814 | << SS.getRange(); |
| 7815 | |
| 7816 | return true; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7817 | } |
| 7818 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7819 | Decl *Sema::ActOnAliasDeclaration(Scope *S, |
| 7820 | AccessSpecifier AS, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7821 | MultiTemplateParamsArg TemplateParamLists, |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7822 | SourceLocation UsingLoc, |
| 7823 | UnqualifiedId &Name, |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 7824 | AttributeList *AttrList, |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7825 | TypeResult Type) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7826 | // Skip up to the relevant declaration scope. |
| 7827 | while (S->getFlags() & Scope::TemplateParamScope) |
| 7828 | S = S->getParent(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7829 | assert((S->getFlags() & Scope::DeclScope) && |
| 7830 | "got alias-declaration outside of declaration scope"); |
| 7831 | |
| 7832 | if (Type.isInvalid()) |
| 7833 | return 0; |
| 7834 | |
| 7835 | bool Invalid = false; |
| 7836 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
| 7837 | TypeSourceInfo *TInfo = 0; |
Nick Lewycky | 82e4780 | 2011-05-02 01:07:19 +0000 | [diff] [blame] | 7838 | GetTypeFromParser(Type.get(), &TInfo); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7839 | |
| 7840 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
| 7841 | return 0; |
| 7842 | |
| 7843 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7844 | UPPC_DeclarationType)) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7845 | Invalid = true; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7846 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 7847 | TInfo->getTypeLoc().getBeginLoc()); |
| 7848 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7849 | |
| 7850 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, ForRedeclaration); |
| 7851 | LookupName(Previous, S); |
| 7852 | |
| 7853 | // Warn about shadowing the name of a template parameter. |
| 7854 | if (Previous.isSingleResult() && |
| 7855 | Previous.getFoundDecl()->isTemplateParameter()) { |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 7856 | DiagnoseTemplateParameterShadow(Name.StartLocation,Previous.getFoundDecl()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7857 | Previous.clear(); |
| 7858 | } |
| 7859 | |
| 7860 | assert(Name.Kind == UnqualifiedId::IK_Identifier && |
| 7861 | "name in alias declaration must be an identifier"); |
| 7862 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 7863 | Name.StartLocation, |
| 7864 | Name.Identifier, TInfo); |
| 7865 | |
| 7866 | NewTD->setAccess(AS); |
| 7867 | |
| 7868 | if (Invalid) |
| 7869 | NewTD->setInvalidDecl(); |
| 7870 | |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 7871 | ProcessDeclAttributeList(S, NewTD, AttrList); |
| 7872 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7873 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 7874 | Invalid |= NewTD->isInvalidDecl(); |
| 7875 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7876 | bool Redeclaration = false; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7877 | |
| 7878 | NamedDecl *NewND; |
| 7879 | if (TemplateParamLists.size()) { |
| 7880 | TypeAliasTemplateDecl *OldDecl = 0; |
| 7881 | TemplateParameterList *OldTemplateParams = 0; |
| 7882 | |
| 7883 | if (TemplateParamLists.size() != 1) { |
| 7884 | Diag(UsingLoc, diag::err_alias_template_extra_headers) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7885 | << SourceRange(TemplateParamLists[1]->getTemplateLoc(), |
| 7886 | TemplateParamLists[TemplateParamLists.size()-1]->getRAngleLoc()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7887 | } |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7888 | TemplateParameterList *TemplateParams = TemplateParamLists[0]; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7889 | |
| 7890 | // Only consider previous declarations in the same scope. |
| 7891 | FilterLookupForScope(Previous, CurContext, S, /*ConsiderLinkage*/false, |
| 7892 | /*ExplicitInstantiationOrSpecialization*/false); |
| 7893 | if (!Previous.empty()) { |
| 7894 | Redeclaration = true; |
| 7895 | |
| 7896 | OldDecl = Previous.getAsSingle<TypeAliasTemplateDecl>(); |
| 7897 | if (!OldDecl && !Invalid) { |
| 7898 | Diag(UsingLoc, diag::err_redefinition_different_kind) |
| 7899 | << Name.Identifier; |
| 7900 | |
| 7901 | NamedDecl *OldD = Previous.getRepresentativeDecl(); |
| 7902 | if (OldD->getLocation().isValid()) |
| 7903 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| 7904 | |
| 7905 | Invalid = true; |
| 7906 | } |
| 7907 | |
| 7908 | if (!Invalid && OldDecl && !OldDecl->isInvalidDecl()) { |
| 7909 | if (TemplateParameterListsAreEqual(TemplateParams, |
| 7910 | OldDecl->getTemplateParameters(), |
| 7911 | /*Complain=*/true, |
| 7912 | TPL_TemplateMatch)) |
| 7913 | OldTemplateParams = OldDecl->getTemplateParameters(); |
| 7914 | else |
| 7915 | Invalid = true; |
| 7916 | |
| 7917 | TypeAliasDecl *OldTD = OldDecl->getTemplatedDecl(); |
| 7918 | if (!Invalid && |
| 7919 | !Context.hasSameType(OldTD->getUnderlyingType(), |
| 7920 | NewTD->getUnderlyingType())) { |
| 7921 | // FIXME: The C++0x standard does not clearly say this is ill-formed, |
| 7922 | // but we can't reasonably accept it. |
| 7923 | Diag(NewTD->getLocation(), diag::err_redefinition_different_typedef) |
| 7924 | << 2 << NewTD->getUnderlyingType() << OldTD->getUnderlyingType(); |
| 7925 | if (OldTD->getLocation().isValid()) |
| 7926 | Diag(OldTD->getLocation(), diag::note_previous_definition); |
| 7927 | Invalid = true; |
| 7928 | } |
| 7929 | } |
| 7930 | } |
| 7931 | |
| 7932 | // Merge any previous default template arguments into our parameters, |
| 7933 | // and check the parameter list. |
| 7934 | if (CheckTemplateParameterList(TemplateParams, OldTemplateParams, |
| 7935 | TPC_TypeAliasTemplate)) |
| 7936 | return 0; |
| 7937 | |
| 7938 | TypeAliasTemplateDecl *NewDecl = |
| 7939 | TypeAliasTemplateDecl::Create(Context, CurContext, UsingLoc, |
| 7940 | Name.Identifier, TemplateParams, |
| 7941 | NewTD); |
| 7942 | |
| 7943 | NewDecl->setAccess(AS); |
| 7944 | |
| 7945 | if (Invalid) |
| 7946 | NewDecl->setInvalidDecl(); |
| 7947 | else if (OldDecl) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 7948 | NewDecl->setPreviousDecl(OldDecl); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7949 | |
| 7950 | NewND = NewDecl; |
| 7951 | } else { |
| 7952 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 7953 | NewND = NewTD; |
| 7954 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7955 | |
| 7956 | if (!Redeclaration) |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7957 | PushOnScopeChains(NewND, S); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7958 | |
Dmitri Gribenko | 7f4b377 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 7959 | ActOnDocumentableDecl(NewND); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7960 | return NewND; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7961 | } |
| 7962 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7963 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 47952ae | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 7964 | SourceLocation NamespaceLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 7965 | SourceLocation AliasLoc, |
| 7966 | IdentifierInfo *Alias, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7967 | CXXScopeSpec &SS, |
Anders Carlsson | 47952ae | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 7968 | SourceLocation IdentLoc, |
| 7969 | IdentifierInfo *Ident) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7970 | |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7971 | // Lookup the namespace name. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7972 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 7973 | LookupParsedName(R, S, &SS); |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7974 | |
Anders Carlsson | dca83c4 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7975 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | 5cf8d67 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 7976 | NamedDecl *PrevDecl |
| 7977 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 7978 | ForRedeclaration); |
| 7979 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 7980 | PrevDecl = 0; |
| 7981 | |
| 7982 | if (PrevDecl) { |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7983 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7984 | // 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] | 7985 | // namespace, so don't create a new one. |
Douglas Gregor | 4667eff | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 7986 | // FIXME: At some point, we'll want to create the (redundant) |
| 7987 | // declaration to maintain better source information. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7988 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | 4667eff | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 7989 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7990 | return 0; |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7991 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7992 | |
Anders Carlsson | dca83c4 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7993 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 7994 | diag::err_redefinition_different_kind; |
| 7995 | Diag(AliasLoc, DiagID) << Alias; |
| 7996 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7997 | return 0; |
Anders Carlsson | dca83c4 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7998 | } |
| 7999 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 8000 | if (R.isAmbiguous()) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8001 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8002 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8003 | if (R.empty()) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 8004 | if (!TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, Ident)) { |
Richard Smith | a974688 | 2012-04-05 23:13:23 +0000 | [diff] [blame] | 8005 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8006 | return 0; |
Douglas Gregor | 9629e9a | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 8007 | } |
Anders Carlsson | ac2c965 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 8008 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8009 | |
Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 8010 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8011 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
Douglas Gregor | c05ba2e | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 8012 | Alias, SS.getWithLocInContext(Context), |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 8013 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8014 | |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 8015 | PushOnScopeChains(AliasDecl, S); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8016 | return AliasDecl; |
Anders Carlsson | 9205d55 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 8017 | } |
| 8018 | |
Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 8019 | Sema::ImplicitExceptionSpecification |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8020 | Sema::ComputeDefaultedDefaultCtorExceptionSpec(SourceLocation Loc, |
| 8021 | CXXMethodDecl *MD) { |
| 8022 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 8023 | |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8024 | // C++ [except.spec]p14: |
| 8025 | // An implicitly declared special member function (Clause 12) shall have an |
| 8026 | // exception-specification. [...] |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8027 | ImplicitExceptionSpecification ExceptSpec(*this); |
Abramo Bagnara | dd8fc04 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 8028 | if (ClassDecl->isInvalidDecl()) |
| 8029 | return ExceptSpec; |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8030 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8031 | // Direct base-class constructors. |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8032 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 8033 | BEnd = ClassDecl->bases_end(); |
| 8034 | B != BEnd; ++B) { |
| 8035 | if (B->isVirtual()) // Handled below. |
| 8036 | continue; |
| 8037 | |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8038 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 8039 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 8040 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 8041 | // If this is a deleted function, add it anyway. This might be conformant |
| 8042 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8043 | if (Constructor) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8044 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8045 | } |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8046 | } |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8047 | |
| 8048 | // Virtual base-class constructors. |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8049 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 8050 | BEnd = ClassDecl->vbases_end(); |
| 8051 | B != BEnd; ++B) { |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8052 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 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) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +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. |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8063 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 8064 | FEnd = ClassDecl->field_end(); |
| 8065 | F != FEnd; ++F) { |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 8066 | if (F->hasInClassInitializer()) { |
| 8067 | if (Expr *E = F->getInClassInitializer()) |
| 8068 | ExceptSpec.CalledExpr(E); |
| 8069 | else if (!F->isInvalidDecl()) |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8070 | // DR1351: |
| 8071 | // If the brace-or-equal-initializer of a non-static data member |
| 8072 | // invokes a defaulted default constructor of its class or of an |
| 8073 | // enclosing class in a potentially evaluated subexpression, the |
| 8074 | // program is ill-formed. |
| 8075 | // |
| 8076 | // This resolution is unworkable: the exception specification of the |
| 8077 | // default constructor can be needed in an unevaluated context, in |
| 8078 | // particular, in the operand of a noexcept-expression, and we can be |
| 8079 | // unable to compute an exception specification for an enclosed class. |
| 8080 | // |
| 8081 | // We do not allow an in-class initializer to require the evaluation |
| 8082 | // of the exception specification for any in-class initializer whose |
| 8083 | // definition is not lexically complete. |
| 8084 | Diag(Loc, diag::err_in_class_initializer_references_def_ctor) << MD; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 8085 | } else if (const RecordType *RecordTy |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8086 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Alexis Hunt | eef8ee0 | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 8087 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 8088 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 8089 | // If this is a deleted function, add it anyway. This might be conformant |
| 8090 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8091 | // In particular, the problem is that this function never gets called. It |
| 8092 | // might just be ill-formed because this function attempts to refer to |
| 8093 | // a deleted function here. |
| 8094 | if (Constructor) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8095 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8096 | } |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8097 | } |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8098 | |
Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 8099 | return ExceptSpec; |
| 8100 | } |
| 8101 | |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8102 | Sema::ImplicitExceptionSpecification |
Richard Smith | b7151b9 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8103 | Sema::ComputeInheritingCtorExceptionSpec(CXXConstructorDecl *CD) { |
| 8104 | CXXRecordDecl *ClassDecl = CD->getParent(); |
| 8105 | |
| 8106 | // C++ [except.spec]p14: |
| 8107 | // An inheriting constructor [...] shall have an exception-specification. [...] |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8108 | ImplicitExceptionSpecification ExceptSpec(*this); |
Richard Smith | b7151b9 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 8109 | if (ClassDecl->isInvalidDecl()) |
| 8110 | return ExceptSpec; |
| 8111 | |
| 8112 | // Inherited constructor. |
| 8113 | const CXXConstructorDecl *InheritedCD = CD->getInheritedConstructor(); |
| 8114 | const CXXRecordDecl *InheritedDecl = InheritedCD->getParent(); |
| 8115 | // FIXME: Copying or moving the parameters could add extra exceptions to the |
| 8116 | // set, as could the default arguments for the inherited constructor. This |
| 8117 | // will be addressed when we implement the resolution of core issue 1351. |
| 8118 | ExceptSpec.CalledDecl(CD->getLocStart(), InheritedCD); |
| 8119 | |
| 8120 | // Direct base-class constructors. |
| 8121 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 8122 | BEnd = ClassDecl->bases_end(); |
| 8123 | B != BEnd; ++B) { |
| 8124 | if (B->isVirtual()) // Handled below. |
| 8125 | continue; |
| 8126 | |
| 8127 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 8128 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 8129 | if (BaseClassDecl == InheritedDecl) |
| 8130 | continue; |
| 8131 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 8132 | if (Constructor) |
| 8133 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
| 8134 | } |
| 8135 | } |
| 8136 | |
| 8137 | // Virtual base-class constructors. |
| 8138 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 8139 | BEnd = ClassDecl->vbases_end(); |
| 8140 | B != BEnd; ++B) { |
| 8141 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 8142 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 8143 | if (BaseClassDecl == InheritedDecl) |
| 8144 | continue; |
| 8145 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 8146 | if (Constructor) |
| 8147 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
| 8148 | } |
| 8149 | } |
| 8150 | |
| 8151 | // Field constructors. |
| 8152 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 8153 | FEnd = ClassDecl->field_end(); |
| 8154 | F != FEnd; ++F) { |
| 8155 | if (F->hasInClassInitializer()) { |
| 8156 | if (Expr *E = F->getInClassInitializer()) |
| 8157 | ExceptSpec.CalledExpr(E); |
| 8158 | else if (!F->isInvalidDecl()) |
| 8159 | Diag(CD->getLocation(), |
| 8160 | diag::err_in_class_initializer_references_def_ctor) << CD; |
| 8161 | } else if (const RecordType *RecordTy |
| 8162 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 8163 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 8164 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 8165 | if (Constructor) |
| 8166 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
| 8167 | } |
| 8168 | } |
| 8169 | |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8170 | return ExceptSpec; |
| 8171 | } |
| 8172 | |
Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8173 | namespace { |
| 8174 | /// RAII object to register a special member as being currently declared. |
| 8175 | struct DeclaringSpecialMember { |
| 8176 | Sema &S; |
| 8177 | Sema::SpecialMemberDecl D; |
| 8178 | bool WasAlreadyBeingDeclared; |
| 8179 | |
| 8180 | DeclaringSpecialMember(Sema &S, CXXRecordDecl *RD, Sema::CXXSpecialMember CSM) |
| 8181 | : S(S), D(RD, CSM) { |
| 8182 | WasAlreadyBeingDeclared = !S.SpecialMembersBeingDeclared.insert(D); |
| 8183 | if (WasAlreadyBeingDeclared) |
| 8184 | // This almost never happens, but if it does, ensure that our cache |
| 8185 | // doesn't contain a stale result. |
| 8186 | S.SpecialMemberCache.clear(); |
| 8187 | |
| 8188 | // FIXME: Register a note to be produced if we encounter an error while |
| 8189 | // declaring the special member. |
| 8190 | } |
| 8191 | ~DeclaringSpecialMember() { |
| 8192 | if (!WasAlreadyBeingDeclared) |
| 8193 | S.SpecialMembersBeingDeclared.erase(D); |
| 8194 | } |
| 8195 | |
| 8196 | /// \brief Are we already trying to declare this special member? |
| 8197 | bool isAlreadyBeingDeclared() const { |
| 8198 | return WasAlreadyBeingDeclared; |
| 8199 | } |
| 8200 | }; |
| 8201 | } |
| 8202 | |
Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 8203 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 8204 | CXXRecordDecl *ClassDecl) { |
| 8205 | // C++ [class.ctor]p5: |
| 8206 | // A default constructor for a class X is a constructor of class X |
| 8207 | // that can be called without an argument. If there is no |
| 8208 | // user-declared constructor for class X, a default constructor is |
| 8209 | // implicitly declared. An implicitly-declared default constructor |
| 8210 | // is an inline public member of its class. |
Richard Smith | 7d125a1 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 8211 | assert(ClassDecl->needsImplicitDefaultConstructor() && |
Alexis Hunt | 6d5b96c | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 8212 | "Should not build implicit default constructor!"); |
| 8213 | |
Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8214 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDefaultConstructor); |
| 8215 | if (DSM.isAlreadyBeingDeclared()) |
| 8216 | return 0; |
| 8217 | |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 8218 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 8219 | CXXDefaultConstructor, |
| 8220 | false); |
| 8221 | |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 8222 | // Create the actual constructor declaration. |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8223 | CanQualType ClassType |
| 8224 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8225 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8226 | DeclarationName Name |
| 8227 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8228 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8229 | CXXConstructorDecl *DefaultCon = CXXConstructorDecl::Create( |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8230 | Context, ClassDecl, ClassLoc, NameInfo, /*Type*/QualType(), /*TInfo=*/0, |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8231 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 8232 | Constexpr); |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8233 | DefaultCon->setAccess(AS_public); |
Alexis Hunt | f92197c | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 8234 | DefaultCon->setDefaulted(); |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8235 | DefaultCon->setImplicit(); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8236 | |
| 8237 | // Build an exception specification pointing back at this constructor. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8238 | FunctionProtoType::ExtProtoInfo EPI = getImplicitMethodEPI(*this, DefaultCon); |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 8239 | DefaultCon->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8240 | |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8241 | // We don't need to use SpecialMemberIsTrivial here; triviality for default |
| 8242 | // constructors is easy to compute. |
| 8243 | DefaultCon->setTrivial(ClassDecl->hasTrivialDefaultConstructor()); |
| 8244 | |
| 8245 | if (ShouldDeleteSpecialMember(DefaultCon, CXXDefaultConstructor)) |
Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 8246 | SetDeclDeleted(DefaultCon, ClassLoc); |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8247 | |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8248 | // Note that we have declared this constructor. |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8249 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8250 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 8251 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 8252 | PushOnScopeChains(DefaultCon, S, false); |
| 8253 | ClassDecl->addDecl(DefaultCon); |
Alexis Hunt | e77a28f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 8254 | |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 8255 | return DefaultCon; |
| 8256 | } |
| 8257 | |
Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 8258 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 8259 | CXXConstructorDecl *Constructor) { |
Alexis Hunt | f92197c | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 8260 | assert((Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 8261 | !Constructor->doesThisDeclarationHaveABody() && |
| 8262 | !Constructor->isDeleted()) && |
Fariborz Jahanian | 18eb69a | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 8263 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8264 | |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 8265 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 8266 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | d7686ef | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 8267 | |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8268 | SynthesizedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 1865342 | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8269 | DiagnosticErrorTrap Trap(Diags); |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 8270 | if (SetCtorInitializers(Constructor, /*AnyErrors=*/false) || |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8271 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 8272 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Alexis Hunt | 80f00ff | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 8273 | << CXXDefaultConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 8274 | Constructor->setInvalidDecl(); |
Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8275 | return; |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 8276 | } |
Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8277 | |
| 8278 | SourceLocation Loc = Constructor->getLocation(); |
Benjamin Kramer | e2a929d | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 8279 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8280 | |
Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 8281 | Constructor->markUsed(Context); |
Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8282 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 8283 | |
| 8284 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8285 | L->CompletedImplicitDefinition(Constructor); |
| 8286 | } |
Richard Trieu | ef64e94 | 2013-10-25 00:56:00 +0000 | [diff] [blame] | 8287 | |
| 8288 | DiagnoseUninitializedFields(*this, Constructor); |
Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 8289 | } |
| 8290 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 8291 | void Sema::ActOnFinishDelayedMemberInitializers(Decl *D) { |
Alp Toker | ae3a944 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 8292 | // Perform any delayed checks on exception specifications. |
| 8293 | CheckDelayedMemberExceptionSpecs(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 8294 | } |
| 8295 | |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8296 | namespace { |
| 8297 | /// Information on inheriting constructors to declare. |
| 8298 | class InheritingConstructorInfo { |
| 8299 | public: |
| 8300 | InheritingConstructorInfo(Sema &SemaRef, CXXRecordDecl *Derived) |
| 8301 | : SemaRef(SemaRef), Derived(Derived) { |
| 8302 | // Mark the constructors that we already have in the derived class. |
| 8303 | // |
| 8304 | // C++11 [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 8305 | // unless there is a user-declared constructor with the same signature in |
| 8306 | // the class where the using-declaration appears. |
| 8307 | visitAll(Derived, &InheritingConstructorInfo::noteDeclaredInDerived); |
| 8308 | } |
| 8309 | |
| 8310 | void inheritAll(CXXRecordDecl *RD) { |
| 8311 | visitAll(RD, &InheritingConstructorInfo::inherit); |
| 8312 | } |
| 8313 | |
| 8314 | private: |
| 8315 | /// Information about an inheriting constructor. |
| 8316 | struct InheritingConstructor { |
| 8317 | InheritingConstructor() |
| 8318 | : DeclaredInDerived(false), BaseCtor(0), DerivedCtor(0) {} |
| 8319 | |
| 8320 | /// If \c true, a constructor with this signature is already declared |
| 8321 | /// in the derived class. |
| 8322 | bool DeclaredInDerived; |
| 8323 | |
| 8324 | /// The constructor which is inherited. |
| 8325 | const CXXConstructorDecl *BaseCtor; |
| 8326 | |
| 8327 | /// The derived constructor we declared. |
| 8328 | CXXConstructorDecl *DerivedCtor; |
| 8329 | }; |
| 8330 | |
| 8331 | /// Inheriting constructors with a given canonical type. There can be at |
| 8332 | /// most one such non-template constructor, and any number of templated |
| 8333 | /// constructors. |
| 8334 | struct InheritingConstructorsForType { |
| 8335 | InheritingConstructor NonTemplate; |
Robert Wilhelm | b869a8f | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 8336 | SmallVector<std::pair<TemplateParameterList *, InheritingConstructor>, 4> |
| 8337 | Templates; |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8338 | |
| 8339 | InheritingConstructor &getEntry(Sema &S, const CXXConstructorDecl *Ctor) { |
| 8340 | if (FunctionTemplateDecl *FTD = Ctor->getDescribedFunctionTemplate()) { |
| 8341 | TemplateParameterList *ParamList = FTD->getTemplateParameters(); |
| 8342 | for (unsigned I = 0, N = Templates.size(); I != N; ++I) |
| 8343 | if (S.TemplateParameterListsAreEqual(ParamList, Templates[I].first, |
| 8344 | false, S.TPL_TemplateMatch)) |
| 8345 | return Templates[I].second; |
| 8346 | Templates.push_back(std::make_pair(ParamList, InheritingConstructor())); |
| 8347 | return Templates.back().second; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8348 | } |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8349 | |
| 8350 | return NonTemplate; |
| 8351 | } |
| 8352 | }; |
| 8353 | |
| 8354 | /// Get or create the inheriting constructor record for a constructor. |
| 8355 | InheritingConstructor &getEntry(const CXXConstructorDecl *Ctor, |
| 8356 | QualType CtorType) { |
| 8357 | return Map[CtorType.getCanonicalType()->castAs<FunctionProtoType>()] |
| 8358 | .getEntry(SemaRef, Ctor); |
| 8359 | } |
| 8360 | |
| 8361 | typedef void (InheritingConstructorInfo::*VisitFn)(const CXXConstructorDecl*); |
| 8362 | |
| 8363 | /// Process all constructors for a class. |
| 8364 | void visitAll(const CXXRecordDecl *RD, VisitFn Callback) { |
| 8365 | for (CXXRecordDecl::ctor_iterator CtorIt = RD->ctor_begin(), |
| 8366 | CtorE = RD->ctor_end(); |
| 8367 | CtorIt != CtorE; ++CtorIt) |
| 8368 | (this->*Callback)(*CtorIt); |
| 8369 | for (CXXRecordDecl::specific_decl_iterator<FunctionTemplateDecl> |
| 8370 | I(RD->decls_begin()), E(RD->decls_end()); |
| 8371 | I != E; ++I) { |
| 8372 | const FunctionDecl *FD = (*I)->getTemplatedDecl(); |
| 8373 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
| 8374 | (this->*Callback)(CD); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8375 | } |
| 8376 | } |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8377 | |
| 8378 | /// Note that a constructor (or constructor template) was declared in Derived. |
| 8379 | void noteDeclaredInDerived(const CXXConstructorDecl *Ctor) { |
| 8380 | getEntry(Ctor, Ctor->getType()).DeclaredInDerived = true; |
| 8381 | } |
| 8382 | |
| 8383 | /// Inherit a single constructor. |
| 8384 | void inherit(const CXXConstructorDecl *Ctor) { |
| 8385 | const FunctionProtoType *CtorType = |
| 8386 | Ctor->getType()->castAs<FunctionProtoType>(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8387 | ArrayRef<QualType> ArgTypes(CtorType->getParamTypes()); |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8388 | FunctionProtoType::ExtProtoInfo EPI = CtorType->getExtProtoInfo(); |
| 8389 | |
| 8390 | SourceLocation UsingLoc = getUsingLoc(Ctor->getParent()); |
| 8391 | |
| 8392 | // Core issue (no number yet): the ellipsis is always discarded. |
| 8393 | if (EPI.Variadic) { |
| 8394 | SemaRef.Diag(UsingLoc, diag::warn_using_decl_constructor_ellipsis); |
| 8395 | SemaRef.Diag(Ctor->getLocation(), |
| 8396 | diag::note_using_decl_constructor_ellipsis); |
| 8397 | EPI.Variadic = false; |
| 8398 | } |
| 8399 | |
| 8400 | // Declare a constructor for each number of parameters. |
| 8401 | // |
| 8402 | // C++11 [class.inhctor]p1: |
| 8403 | // The candidate set of inherited constructors from the class X named in |
| 8404 | // the using-declaration consists of [... modulo defects ...] for each |
| 8405 | // constructor or constructor template of X, the set of constructors or |
| 8406 | // constructor templates that results from omitting any ellipsis parameter |
| 8407 | // specification and successively omitting parameters with a default |
| 8408 | // argument from the end of the parameter-type-list |
Richard Smith | 3c626ed | 2013-04-17 19:00:52 +0000 | [diff] [blame] | 8409 | unsigned MinParams = minParamsToInherit(Ctor); |
| 8410 | unsigned Params = Ctor->getNumParams(); |
| 8411 | if (Params >= MinParams) { |
| 8412 | do |
| 8413 | declareCtor(UsingLoc, Ctor, |
| 8414 | SemaRef.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8415 | Ctor->getReturnType(), ArgTypes.slice(0, Params), EPI)); |
Richard Smith | 3c626ed | 2013-04-17 19:00:52 +0000 | [diff] [blame] | 8416 | while (Params > MinParams && |
| 8417 | Ctor->getParamDecl(--Params)->hasDefaultArg()); |
| 8418 | } |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8419 | } |
| 8420 | |
| 8421 | /// Find the using-declaration which specified that we should inherit the |
| 8422 | /// constructors of \p Base. |
| 8423 | SourceLocation getUsingLoc(const CXXRecordDecl *Base) { |
| 8424 | // No fancy lookup required; just look for the base constructor name |
| 8425 | // directly within the derived class. |
| 8426 | ASTContext &Context = SemaRef.Context; |
| 8427 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
| 8428 | Context.getCanonicalType(Context.getRecordType(Base))); |
| 8429 | DeclContext::lookup_const_result Decls = Derived->lookup(Name); |
| 8430 | return Decls.empty() ? Derived->getLocation() : Decls[0]->getLocation(); |
| 8431 | } |
| 8432 | |
| 8433 | unsigned minParamsToInherit(const CXXConstructorDecl *Ctor) { |
| 8434 | // C++11 [class.inhctor]p3: |
| 8435 | // [F]or each constructor template in the candidate set of inherited |
| 8436 | // constructors, a constructor template is implicitly declared |
| 8437 | if (Ctor->getDescribedFunctionTemplate()) |
| 8438 | return 0; |
| 8439 | |
| 8440 | // For each non-template constructor in the candidate set of inherited |
| 8441 | // constructors other than a constructor having no parameters or a |
| 8442 | // copy/move constructor having a single parameter, a constructor is |
| 8443 | // implicitly declared [...] |
| 8444 | if (Ctor->getNumParams() == 0) |
| 8445 | return 1; |
| 8446 | if (Ctor->isCopyOrMoveConstructor()) |
| 8447 | return 2; |
| 8448 | |
| 8449 | // Per discussion on core reflector, never inherit a constructor which |
| 8450 | // would become a default, copy, or move constructor of Derived either. |
| 8451 | const ParmVarDecl *PD = Ctor->getParamDecl(0); |
| 8452 | const ReferenceType *RT = PD->getType()->getAs<ReferenceType>(); |
| 8453 | return (RT && RT->getPointeeCXXRecordDecl() == Derived) ? 2 : 1; |
| 8454 | } |
| 8455 | |
| 8456 | /// Declare a single inheriting constructor, inheriting the specified |
| 8457 | /// constructor, with the given type. |
| 8458 | void declareCtor(SourceLocation UsingLoc, const CXXConstructorDecl *BaseCtor, |
| 8459 | QualType DerivedType) { |
| 8460 | InheritingConstructor &Entry = getEntry(BaseCtor, DerivedType); |
| 8461 | |
| 8462 | // C++11 [class.inhctor]p3: |
| 8463 | // ... a constructor is implicitly declared with the same constructor |
| 8464 | // characteristics unless there is a user-declared constructor with |
| 8465 | // the same signature in the class where the using-declaration appears |
| 8466 | if (Entry.DeclaredInDerived) |
| 8467 | return; |
| 8468 | |
| 8469 | // C++11 [class.inhctor]p7: |
| 8470 | // If two using-declarations declare inheriting constructors with the |
| 8471 | // same signature, the program is ill-formed |
| 8472 | if (Entry.DerivedCtor) { |
| 8473 | if (BaseCtor->getParent() != Entry.BaseCtor->getParent()) { |
| 8474 | // Only diagnose this once per constructor. |
| 8475 | if (Entry.DerivedCtor->isInvalidDecl()) |
| 8476 | return; |
| 8477 | Entry.DerivedCtor->setInvalidDecl(); |
| 8478 | |
| 8479 | SemaRef.Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 8480 | SemaRef.Diag(BaseCtor->getLocation(), |
| 8481 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 8482 | SemaRef.Diag(Entry.BaseCtor->getLocation(), |
| 8483 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 8484 | SemaRef.Diag(Entry.DerivedCtor->getLocation(), |
| 8485 | diag::note_using_decl_constructor_conflict_previous_using); |
| 8486 | } else { |
| 8487 | // Core issue (no number): if the same inheriting constructor is |
| 8488 | // produced by multiple base class constructors from the same base |
| 8489 | // class, the inheriting constructor is defined as deleted. |
| 8490 | SemaRef.SetDeclDeleted(Entry.DerivedCtor, UsingLoc); |
| 8491 | } |
| 8492 | |
| 8493 | return; |
| 8494 | } |
| 8495 | |
| 8496 | ASTContext &Context = SemaRef.Context; |
| 8497 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
| 8498 | Context.getCanonicalType(Context.getRecordType(Derived))); |
| 8499 | DeclarationNameInfo NameInfo(Name, UsingLoc); |
| 8500 | |
| 8501 | TemplateParameterList *TemplateParams = 0; |
| 8502 | if (const FunctionTemplateDecl *FTD = |
| 8503 | BaseCtor->getDescribedFunctionTemplate()) { |
| 8504 | TemplateParams = FTD->getTemplateParameters(); |
| 8505 | // We're reusing template parameters from a different DeclContext. This |
| 8506 | // is questionable at best, but works out because the template depth in |
| 8507 | // both places is guaranteed to be 0. |
| 8508 | // FIXME: Rebuild the template parameters in the new context, and |
| 8509 | // transform the function type to refer to them. |
| 8510 | } |
| 8511 | |
| 8512 | // Build type source info pointing at the using-declaration. This is |
| 8513 | // required by template instantiation. |
| 8514 | TypeSourceInfo *TInfo = |
| 8515 | Context.getTrivialTypeSourceInfo(DerivedType, UsingLoc); |
| 8516 | FunctionProtoTypeLoc ProtoLoc = |
| 8517 | TInfo->getTypeLoc().IgnoreParens().castAs<FunctionProtoTypeLoc>(); |
| 8518 | |
| 8519 | CXXConstructorDecl *DerivedCtor = CXXConstructorDecl::Create( |
| 8520 | Context, Derived, UsingLoc, NameInfo, DerivedType, |
| 8521 | TInfo, BaseCtor->isExplicit(), /*Inline=*/true, |
| 8522 | /*ImplicitlyDeclared=*/true, /*Constexpr=*/BaseCtor->isConstexpr()); |
| 8523 | |
| 8524 | // Build an unevaluated exception specification for this constructor. |
| 8525 | const FunctionProtoType *FPT = DerivedType->castAs<FunctionProtoType>(); |
| 8526 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8527 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8528 | EPI.ExceptionSpecDecl = DerivedCtor; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8529 | DerivedCtor->setType(Context.getFunctionType(FPT->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8530 | FPT->getParamTypes(), EPI)); |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8531 | |
| 8532 | // Build the parameter declarations. |
| 8533 | SmallVector<ParmVarDecl *, 16> ParamDecls; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8534 | for (unsigned I = 0, N = FPT->getNumParams(); I != N; ++I) { |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8535 | TypeSourceInfo *TInfo = |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8536 | Context.getTrivialTypeSourceInfo(FPT->getParamType(I), UsingLoc); |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8537 | ParmVarDecl *PD = ParmVarDecl::Create( |
| 8538 | Context, DerivedCtor, UsingLoc, UsingLoc, /*IdentifierInfo=*/0, |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8539 | FPT->getParamType(I), TInfo, SC_None, /*DefaultArg=*/0); |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8540 | PD->setScopeInfo(0, I); |
| 8541 | PD->setImplicit(); |
| 8542 | ParamDecls.push_back(PD); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 8543 | ProtoLoc.setParam(I, PD); |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8544 | } |
| 8545 | |
| 8546 | // Set up the new constructor. |
| 8547 | DerivedCtor->setAccess(BaseCtor->getAccess()); |
| 8548 | DerivedCtor->setParams(ParamDecls); |
| 8549 | DerivedCtor->setInheritedConstructor(BaseCtor); |
| 8550 | if (BaseCtor->isDeleted()) |
| 8551 | SemaRef.SetDeclDeleted(DerivedCtor, UsingLoc); |
| 8552 | |
| 8553 | // If this is a constructor template, build the template declaration. |
| 8554 | if (TemplateParams) { |
| 8555 | FunctionTemplateDecl *DerivedTemplate = |
| 8556 | FunctionTemplateDecl::Create(SemaRef.Context, Derived, UsingLoc, Name, |
| 8557 | TemplateParams, DerivedCtor); |
| 8558 | DerivedTemplate->setAccess(BaseCtor->getAccess()); |
| 8559 | DerivedCtor->setDescribedFunctionTemplate(DerivedTemplate); |
| 8560 | Derived->addDecl(DerivedTemplate); |
| 8561 | } else { |
| 8562 | Derived->addDecl(DerivedCtor); |
| 8563 | } |
| 8564 | |
| 8565 | Entry.BaseCtor = BaseCtor; |
| 8566 | Entry.DerivedCtor = DerivedCtor; |
| 8567 | } |
| 8568 | |
| 8569 | Sema &SemaRef; |
| 8570 | CXXRecordDecl *Derived; |
| 8571 | typedef llvm::DenseMap<const Type *, InheritingConstructorsForType> MapType; |
| 8572 | MapType Map; |
| 8573 | }; |
| 8574 | } |
| 8575 | |
| 8576 | void Sema::DeclareInheritingConstructors(CXXRecordDecl *ClassDecl) { |
| 8577 | // Defer declaring the inheriting constructors until the class is |
| 8578 | // instantiated. |
| 8579 | if (ClassDecl->isDependentContext()) |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8580 | return; |
| 8581 | |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8582 | // Find base classes from which we might inherit constructors. |
| 8583 | SmallVector<CXXRecordDecl*, 4> InheritedBases; |
| 8584 | for (CXXRecordDecl::base_class_iterator BaseIt = ClassDecl->bases_begin(), |
| 8585 | BaseE = ClassDecl->bases_end(); |
| 8586 | BaseIt != BaseE; ++BaseIt) |
| 8587 | if (BaseIt->getInheritConstructors()) |
| 8588 | InheritedBases.push_back(BaseIt->getType()->getAsCXXRecordDecl()); |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8589 | |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8590 | // Go no further if we're not inheriting any constructors. |
| 8591 | if (InheritedBases.empty()) |
| 8592 | return; |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8593 | |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8594 | // Declare the inherited constructors. |
| 8595 | InheritingConstructorInfo ICI(*this, ClassDecl); |
| 8596 | for (unsigned I = 0, N = InheritedBases.size(); I != N; ++I) |
| 8597 | ICI.inheritAll(InheritedBases[I]); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8598 | } |
| 8599 | |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8600 | void Sema::DefineInheritingConstructor(SourceLocation CurrentLocation, |
| 8601 | CXXConstructorDecl *Constructor) { |
| 8602 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 8603 | assert(Constructor->getInheritedConstructor() && |
| 8604 | !Constructor->doesThisDeclarationHaveABody() && |
| 8605 | !Constructor->isDeleted()); |
| 8606 | |
| 8607 | SynthesizedFunctionScope Scope(*this, Constructor); |
| 8608 | DiagnosticErrorTrap Trap(Diags); |
| 8609 | if (SetCtorInitializers(Constructor, /*AnyErrors=*/false) || |
| 8610 | Trap.hasErrorOccurred()) { |
| 8611 | Diag(CurrentLocation, diag::note_inhctor_synthesized_at) |
| 8612 | << Context.getTagDeclType(ClassDecl); |
| 8613 | Constructor->setInvalidDecl(); |
| 8614 | return; |
| 8615 | } |
| 8616 | |
| 8617 | SourceLocation Loc = Constructor->getLocation(); |
| 8618 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
| 8619 | |
Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 8620 | Constructor->markUsed(Context); |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8621 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| 8622 | |
| 8623 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8624 | L->CompletedImplicitDefinition(Constructor); |
| 8625 | } |
| 8626 | } |
| 8627 | |
| 8628 | |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8629 | Sema::ImplicitExceptionSpecification |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8630 | Sema::ComputeDefaultedDtorExceptionSpec(CXXMethodDecl *MD) { |
| 8631 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 8632 | |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8633 | // C++ [except.spec]p14: |
| 8634 | // An implicitly declared special member function (Clause 12) shall have |
| 8635 | // an exception-specification. |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8636 | ImplicitExceptionSpecification ExceptSpec(*this); |
Abramo Bagnara | dd8fc04 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 8637 | if (ClassDecl->isInvalidDecl()) |
| 8638 | return ExceptSpec; |
| 8639 | |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8640 | // Direct base-class destructors. |
| 8641 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 8642 | BEnd = ClassDecl->bases_end(); |
| 8643 | B != BEnd; ++B) { |
| 8644 | if (B->isVirtual()) // Handled below. |
| 8645 | continue; |
| 8646 | |
| 8647 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8648 | ExceptSpec.CalledDecl(B->getLocStart(), |
Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8649 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8650 | } |
Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8651 | |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8652 | // Virtual base-class destructors. |
| 8653 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 8654 | BEnd = ClassDecl->vbases_end(); |
| 8655 | B != BEnd; ++B) { |
| 8656 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8657 | ExceptSpec.CalledDecl(B->getLocStart(), |
Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8658 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8659 | } |
Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8660 | |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8661 | // Field destructors. |
| 8662 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 8663 | FEnd = ClassDecl->field_end(); |
| 8664 | F != FEnd; ++F) { |
| 8665 | if (const RecordType *RecordTy |
| 8666 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8667 | ExceptSpec.CalledDecl(F->getLocation(), |
Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8668 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8669 | } |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8670 | |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8671 | return ExceptSpec; |
| 8672 | } |
| 8673 | |
| 8674 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
| 8675 | // C++ [class.dtor]p2: |
| 8676 | // If a class has no user-declared destructor, a destructor is |
| 8677 | // declared implicitly. An implicitly-declared destructor is an |
| 8678 | // inline public member of its class. |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 8679 | assert(ClassDecl->needsImplicitDestructor()); |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8680 | |
Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8681 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDestructor); |
| 8682 | if (DSM.isAlreadyBeingDeclared()) |
| 8683 | return 0; |
| 8684 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8685 | // Create the actual destructor declaration. |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8686 | CanQualType ClassType |
| 8687 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8688 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8689 | DeclarationName Name |
| 8690 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8691 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8692 | CXXDestructorDecl *Destructor |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8693 | = CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
| 8694 | QualType(), 0, /*isInline=*/true, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8695 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8696 | Destructor->setAccess(AS_public); |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8697 | Destructor->setDefaulted(); |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8698 | Destructor->setImplicit(); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8699 | |
| 8700 | // Build an exception specification pointing back at this destructor. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8701 | FunctionProtoType::ExtProtoInfo EPI = getImplicitMethodEPI(*this, Destructor); |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 8702 | Destructor->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8703 | |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8704 | AddOverriddenMethods(ClassDecl, Destructor); |
| 8705 | |
| 8706 | // We don't need to use SpecialMemberIsTrivial here; triviality for |
| 8707 | // destructors is easy to compute. |
| 8708 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
| 8709 | |
| 8710 | if (ShouldDeleteSpecialMember(Destructor, CXXDestructor)) |
Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 8711 | SetDeclDeleted(Destructor, ClassLoc); |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8712 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8713 | // Note that we have declared this destructor. |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8714 | ++ASTContext::NumImplicitDestructorsDeclared; |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8715 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8716 | // Introduce this destructor into its scope. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 8717 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8718 | PushOnScopeChains(Destructor, S, false); |
| 8719 | ClassDecl->addDecl(Destructor); |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8720 | |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8721 | return Destructor; |
| 8722 | } |
| 8723 | |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8724 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | d94105a | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 8725 | CXXDestructorDecl *Destructor) { |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 8726 | assert((Destructor->isDefaulted() && |
Richard Smith | 273c4e9 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8727 | !Destructor->doesThisDeclarationHaveABody() && |
| 8728 | !Destructor->isDeleted()) && |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8729 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 8730 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8731 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8732 | |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8733 | if (Destructor->isInvalidDecl()) |
| 8734 | return; |
| 8735 | |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8736 | SynthesizedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8737 | |
Argyrios Kyrtzidis | 1865342 | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8738 | DiagnosticErrorTrap Trap(Diags); |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 8739 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 8740 | Destructor->getParent()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8741 | |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8742 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 8743 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8744 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 8745 | |
| 8746 | Destructor->setInvalidDecl(); |
| 8747 | return; |
| 8748 | } |
| 8749 | |
Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8750 | SourceLocation Loc = Destructor->getLocation(); |
Benjamin Kramer | e2a929d | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 8751 | Destructor->setBody(new (Context) CompoundStmt(Loc)); |
Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 8752 | Destructor->markUsed(Context); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8753 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 8754 | |
| 8755 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8756 | L->CompletedImplicitDefinition(Destructor); |
| 8757 | } |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8758 | } |
| 8759 | |
Richard Smith | 84973e5 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 8760 | /// \brief Perform any semantic analysis which needs to be delayed until all |
| 8761 | /// pending class member declarations have been parsed. |
| 8762 | void Sema::ActOnFinishCXXMemberDecls() { |
Douglas Gregor | bc0e5c0 | 2013-02-01 04:49:10 +0000 | [diff] [blame] | 8763 | // If the context is an invalid C++ class, just suppress these checks. |
| 8764 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8765 | if (Record->isInvalidDecl()) { |
Alp Toker | ae3a944 | 2013-10-18 05:54:19 +0000 | [diff] [blame] | 8766 | DelayedDefaultedMemberExceptionSpecs.clear(); |
Douglas Gregor | bc0e5c0 | 2013-02-01 04:49:10 +0000 | [diff] [blame] | 8767 | DelayedDestructorExceptionSpecChecks.clear(); |
| 8768 | return; |
| 8769 | } |
| 8770 | } |
Richard Smith | 84973e5 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 8771 | } |
| 8772 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8773 | void Sema::AdjustDestructorExceptionSpec(CXXRecordDecl *ClassDecl, |
| 8774 | CXXDestructorDecl *Destructor) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8775 | assert(getLangOpts().CPlusPlus11 && |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8776 | "adjusting dtor exception specs was introduced in c++11"); |
| 8777 | |
Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8778 | // C++11 [class.dtor]p3: |
| 8779 | // A declaration of a destructor that does not have an exception- |
| 8780 | // specification is implicitly considered to have the same exception- |
| 8781 | // specification as an implicit declaration. |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8782 | const FunctionProtoType *DtorType = Destructor->getType()-> |
Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8783 | getAs<FunctionProtoType>(); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8784 | if (DtorType->hasExceptionSpec()) |
Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8785 | return; |
| 8786 | |
Chandler Carruth | 9a79757 | 2011-09-20 04:55:26 +0000 | [diff] [blame] | 8787 | // Replace the destructor's type, building off the existing one. Fortunately, |
| 8788 | // the only thing of interest in the destructor type is its extended info. |
| 8789 | // The return and arguments are fixed. |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8790 | FunctionProtoType::ExtProtoInfo EPI = DtorType->getExtProtoInfo(); |
| 8791 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8792 | EPI.ExceptionSpecDecl = Destructor; |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 8793 | Destructor->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | 84973e5 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 8794 | |
Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8795 | // FIXME: If the destructor has a body that could throw, and the newly created |
| 8796 | // spec doesn't allow exceptions, we should emit a warning, because this |
| 8797 | // change in behavior can break conforming C++03 programs at runtime. |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8798 | // However, we don't have a body or an exception specification yet, so it |
| 8799 | // needs to be done somewhere else. |
Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8800 | } |
| 8801 | |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8802 | namespace { |
| 8803 | /// \brief An abstract base class for all helper classes used in building the |
| 8804 | // copy/move operators. These classes serve as factory functions and help us |
| 8805 | // avoid using the same Expr* in the AST twice. |
| 8806 | class ExprBuilder { |
| 8807 | ExprBuilder(const ExprBuilder&) LLVM_DELETED_FUNCTION; |
| 8808 | ExprBuilder &operator=(const ExprBuilder&) LLVM_DELETED_FUNCTION; |
| 8809 | |
| 8810 | protected: |
| 8811 | static Expr *assertNotNull(Expr *E) { |
| 8812 | assert(E && "Expression construction must not fail."); |
| 8813 | return E; |
| 8814 | } |
| 8815 | |
| 8816 | public: |
| 8817 | ExprBuilder() {} |
| 8818 | virtual ~ExprBuilder() {} |
| 8819 | |
| 8820 | virtual Expr *build(Sema &S, SourceLocation Loc) const = 0; |
| 8821 | }; |
| 8822 | |
| 8823 | class RefBuilder: public ExprBuilder { |
| 8824 | VarDecl *Var; |
| 8825 | QualType VarType; |
| 8826 | |
| 8827 | public: |
| 8828 | virtual Expr *build(Sema &S, SourceLocation Loc) const LLVM_OVERRIDE { |
| 8829 | return assertNotNull(S.BuildDeclRefExpr(Var, VarType, VK_LValue, Loc).take()); |
| 8830 | } |
| 8831 | |
| 8832 | RefBuilder(VarDecl *Var, QualType VarType) |
| 8833 | : Var(Var), VarType(VarType) {} |
| 8834 | }; |
| 8835 | |
| 8836 | class ThisBuilder: public ExprBuilder { |
| 8837 | public: |
| 8838 | virtual Expr *build(Sema &S, SourceLocation Loc) const LLVM_OVERRIDE { |
| 8839 | return assertNotNull(S.ActOnCXXThis(Loc).takeAs<Expr>()); |
| 8840 | } |
| 8841 | }; |
| 8842 | |
| 8843 | class CastBuilder: public ExprBuilder { |
| 8844 | const ExprBuilder &Builder; |
| 8845 | QualType Type; |
| 8846 | ExprValueKind Kind; |
| 8847 | const CXXCastPath &Path; |
| 8848 | |
| 8849 | public: |
| 8850 | virtual Expr *build(Sema &S, SourceLocation Loc) const LLVM_OVERRIDE { |
| 8851 | return assertNotNull(S.ImpCastExprToType(Builder.build(S, Loc), Type, |
| 8852 | CK_UncheckedDerivedToBase, Kind, |
| 8853 | &Path).take()); |
| 8854 | } |
| 8855 | |
| 8856 | CastBuilder(const ExprBuilder &Builder, QualType Type, ExprValueKind Kind, |
| 8857 | const CXXCastPath &Path) |
| 8858 | : Builder(Builder), Type(Type), Kind(Kind), Path(Path) {} |
| 8859 | }; |
| 8860 | |
| 8861 | class DerefBuilder: public ExprBuilder { |
| 8862 | const ExprBuilder &Builder; |
| 8863 | |
| 8864 | public: |
| 8865 | virtual Expr *build(Sema &S, SourceLocation Loc) const LLVM_OVERRIDE { |
| 8866 | return assertNotNull( |
| 8867 | S.CreateBuiltinUnaryOp(Loc, UO_Deref, Builder.build(S, Loc)).take()); |
| 8868 | } |
| 8869 | |
| 8870 | DerefBuilder(const ExprBuilder &Builder) : Builder(Builder) {} |
| 8871 | }; |
| 8872 | |
| 8873 | class MemberBuilder: public ExprBuilder { |
| 8874 | const ExprBuilder &Builder; |
| 8875 | QualType Type; |
| 8876 | CXXScopeSpec SS; |
| 8877 | bool IsArrow; |
| 8878 | LookupResult &MemberLookup; |
| 8879 | |
| 8880 | public: |
| 8881 | virtual Expr *build(Sema &S, SourceLocation Loc) const LLVM_OVERRIDE { |
| 8882 | return assertNotNull(S.BuildMemberReferenceExpr( |
| 8883 | Builder.build(S, Loc), Type, Loc, IsArrow, SS, SourceLocation(), 0, |
| 8884 | MemberLookup, 0).take()); |
| 8885 | } |
| 8886 | |
| 8887 | MemberBuilder(const ExprBuilder &Builder, QualType Type, bool IsArrow, |
| 8888 | LookupResult &MemberLookup) |
| 8889 | : Builder(Builder), Type(Type), IsArrow(IsArrow), |
| 8890 | MemberLookup(MemberLookup) {} |
| 8891 | }; |
| 8892 | |
| 8893 | class MoveCastBuilder: public ExprBuilder { |
| 8894 | const ExprBuilder &Builder; |
| 8895 | |
| 8896 | public: |
| 8897 | virtual Expr *build(Sema &S, SourceLocation Loc) const LLVM_OVERRIDE { |
| 8898 | return assertNotNull(CastForMoving(S, Builder.build(S, Loc))); |
| 8899 | } |
| 8900 | |
| 8901 | MoveCastBuilder(const ExprBuilder &Builder) : Builder(Builder) {} |
| 8902 | }; |
| 8903 | |
| 8904 | class LvalueConvBuilder: public ExprBuilder { |
| 8905 | const ExprBuilder &Builder; |
| 8906 | |
| 8907 | public: |
| 8908 | virtual Expr *build(Sema &S, SourceLocation Loc) const LLVM_OVERRIDE { |
| 8909 | return assertNotNull( |
| 8910 | S.DefaultLvalueConversion(Builder.build(S, Loc)).take()); |
| 8911 | } |
| 8912 | |
| 8913 | LvalueConvBuilder(const ExprBuilder &Builder) : Builder(Builder) {} |
| 8914 | }; |
| 8915 | |
| 8916 | class SubscriptBuilder: public ExprBuilder { |
| 8917 | const ExprBuilder &Base; |
| 8918 | const ExprBuilder &Index; |
| 8919 | |
| 8920 | public: |
| 8921 | virtual Expr *build(Sema &S, SourceLocation Loc) const |
| 8922 | LLVM_OVERRIDE { |
| 8923 | return assertNotNull(S.CreateBuiltinArraySubscriptExpr( |
| 8924 | Base.build(S, Loc), Loc, Index.build(S, Loc), Loc).take()); |
| 8925 | } |
| 8926 | |
| 8927 | SubscriptBuilder(const ExprBuilder &Base, const ExprBuilder &Index) |
| 8928 | : Base(Base), Index(Index) {} |
| 8929 | }; |
| 8930 | |
| 8931 | } // end anonymous namespace |
| 8932 | |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8933 | /// When generating a defaulted copy or move assignment operator, if a field |
| 8934 | /// should be copied with __builtin_memcpy rather than via explicit assignments, |
| 8935 | /// do so. This optimization only applies for arrays of scalars, and for arrays |
| 8936 | /// of class type where the selected copy/move-assignment operator is trivial. |
| 8937 | static StmtResult |
| 8938 | buildMemcpyForAssignmentOp(Sema &S, SourceLocation Loc, QualType T, |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8939 | const ExprBuilder &ToB, const ExprBuilder &FromB) { |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8940 | // Compute the size of the memory buffer to be copied. |
| 8941 | QualType SizeType = S.Context.getSizeType(); |
| 8942 | llvm::APInt Size(S.Context.getTypeSize(SizeType), |
| 8943 | S.Context.getTypeSizeInChars(T).getQuantity()); |
| 8944 | |
| 8945 | // Take the address of the field references for "from" and "to". We |
| 8946 | // directly construct UnaryOperators here because semantic analysis |
| 8947 | // does not permit us to take the address of an xvalue. |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8948 | Expr *From = FromB.build(S, Loc); |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8949 | From = new (S.Context) UnaryOperator(From, UO_AddrOf, |
| 8950 | S.Context.getPointerType(From->getType()), |
| 8951 | VK_RValue, OK_Ordinary, Loc); |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 8952 | Expr *To = ToB.build(S, Loc); |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8953 | To = new (S.Context) UnaryOperator(To, UO_AddrOf, |
| 8954 | S.Context.getPointerType(To->getType()), |
| 8955 | VK_RValue, OK_Ordinary, Loc); |
| 8956 | |
| 8957 | const Type *E = T->getBaseElementTypeUnsafe(); |
| 8958 | bool NeedsCollectableMemCpy = |
| 8959 | E->isRecordType() && E->getAs<RecordType>()->getDecl()->hasObjectMember(); |
| 8960 | |
| 8961 | // Create a reference to the __builtin_objc_memmove_collectable function |
| 8962 | StringRef MemCpyName = NeedsCollectableMemCpy ? |
| 8963 | "__builtin_objc_memmove_collectable" : |
| 8964 | "__builtin_memcpy"; |
| 8965 | LookupResult R(S, &S.Context.Idents.get(MemCpyName), Loc, |
| 8966 | Sema::LookupOrdinaryName); |
| 8967 | S.LookupName(R, S.TUScope, true); |
| 8968 | |
| 8969 | FunctionDecl *MemCpy = R.getAsSingle<FunctionDecl>(); |
| 8970 | if (!MemCpy) |
| 8971 | // Something went horribly wrong earlier, and we will have complained |
| 8972 | // about it. |
| 8973 | return StmtError(); |
| 8974 | |
| 8975 | ExprResult MemCpyRef = S.BuildDeclRefExpr(MemCpy, S.Context.BuiltinFnTy, |
| 8976 | VK_RValue, Loc, 0); |
| 8977 | assert(MemCpyRef.isUsable() && "Builtin reference cannot fail"); |
| 8978 | |
| 8979 | Expr *CallArgs[] = { |
| 8980 | To, From, IntegerLiteral::Create(S.Context, Size, SizeType, Loc) |
| 8981 | }; |
| 8982 | ExprResult Call = S.ActOnCallExpr(/*Scope=*/0, MemCpyRef.take(), |
| 8983 | Loc, CallArgs, Loc); |
| 8984 | |
| 8985 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 8986 | return S.Owned(Call.takeAs<Stmt>()); |
| 8987 | } |
| 8988 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8989 | /// \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] | 8990 | /// \c To. |
| 8991 | /// |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8992 | /// This routine is used to copy/move the members of a class with an |
| 8993 | /// implicitly-declared copy/move assignment operator. When the entities being |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8994 | /// copied are arrays, this routine builds for loops to copy them. |
| 8995 | /// |
| 8996 | /// \param S The Sema object used for type-checking. |
| 8997 | /// |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8998 | /// \param Loc The location where the implicit copy/move is being generated. |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8999 | /// |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9000 | /// \param T The type of the expressions being copied/moved. Both expressions |
| 9001 | /// must have this type. |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9002 | /// |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9003 | /// \param To The expression we are copying/moving to. |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9004 | /// |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9005 | /// \param From The expression we are copying/moving from. |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9006 | /// |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9007 | /// \param CopyingBaseSubobject Whether we're copying/moving a base subobject. |
Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 9008 | /// Otherwise, it's a non-static member subobject. |
| 9009 | /// |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9010 | /// \param Copying Whether we're copying or moving. |
| 9011 | /// |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9012 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 9013 | /// |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9014 | /// \returns A statement or a loop that copies the expressions, or StmtResult(0) |
| 9015 | /// if a memcpy should be used instead. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9016 | static StmtResult |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9017 | buildSingleCopyAssignRecursively(Sema &S, SourceLocation Loc, QualType T, |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9018 | const ExprBuilder &To, const ExprBuilder &From, |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9019 | bool CopyingBaseSubobject, bool Copying, |
| 9020 | unsigned Depth = 0) { |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9021 | // C++11 [class.copy]p28: |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9022 | // Each subobject is assigned in the manner appropriate to its type: |
| 9023 | // |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9024 | // - if the subobject is of class type, as if by a call to operator= with |
| 9025 | // the subobject as the object expression and the corresponding |
| 9026 | // subobject of x as a single function argument (as if by explicit |
| 9027 | // qualification; that is, ignoring any possible virtual overriding |
| 9028 | // functions in more derived classes); |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9029 | // |
| 9030 | // C++03 [class.copy]p13: |
| 9031 | // - if the subobject is of class type, the copy assignment operator for |
| 9032 | // the class is used (as if by explicit qualification; that is, |
| 9033 | // ignoring any possible virtual overriding functions in more derived |
| 9034 | // classes); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9035 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 9036 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9037 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9038 | // Look for operator=. |
| 9039 | DeclarationName Name |
| 9040 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 9041 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 9042 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9043 | |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9044 | // Prior to C++11, filter out any result that isn't a copy/move-assignment |
| 9045 | // operator. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9046 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9047 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 9048 | while (F.hasNext()) { |
| 9049 | NamedDecl *D = F.next(); |
| 9050 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 9051 | if (Method->isCopyAssignmentOperator() || |
| 9052 | (!Copying && Method->isMoveAssignmentOperator())) |
| 9053 | continue; |
| 9054 | |
| 9055 | F.erase(); |
| 9056 | } |
| 9057 | F.done(); |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 9058 | } |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9059 | |
Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 9060 | // Suppress the protected check (C++ [class.protected]) for each of the |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9061 | // assignment operators we found. This strange dance is required when |
Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 9062 | // we're assigning via a base classes's copy-assignment operator. To |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9063 | // ensure that we're getting the right base class subobject (without |
Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 9064 | // ambiguities), we need to cast "this" to that subobject type; to |
| 9065 | // ensure that we don't go through the virtual call mechanism, we need |
| 9066 | // to qualify the operator= name with the base class (see below). However, |
| 9067 | // this means that if the base class has a protected copy assignment |
| 9068 | // operator, the protected member access check will fail. So, we |
| 9069 | // rewrite "protected" access to "public" access in this case, since we |
| 9070 | // know by construction that we're calling from a derived class. |
| 9071 | if (CopyingBaseSubobject) { |
| 9072 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 9073 | L != LEnd; ++L) { |
| 9074 | if (L.getAccess() == AS_protected) |
| 9075 | L.setAccess(AS_public); |
| 9076 | } |
| 9077 | } |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9078 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9079 | // Create the nested-name-specifier that will be used to qualify the |
| 9080 | // reference to operator=; this is required to suppress the virtual |
| 9081 | // call mechanism. |
| 9082 | CXXScopeSpec SS; |
Manuel Klimek | e716741 | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 9083 | const Type *CanonicalT = S.Context.getCanonicalType(T.getTypePtr()); |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9084 | SS.MakeTrivial(S.Context, |
| 9085 | NestedNameSpecifier::Create(S.Context, 0, false, |
Manuel Klimek | e716741 | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 9086 | CanonicalT), |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 9087 | Loc); |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9088 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9089 | // Create the reference to operator=. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9090 | ExprResult OpEqualRef |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9091 | = S.BuildMemberReferenceExpr(To.build(S, Loc), T, Loc, /*isArrow=*/false, |
| 9092 | SS, /*TemplateKWLoc=*/SourceLocation(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9093 | /*FirstQualifierInScope=*/0, |
| 9094 | OpLookup, |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9095 | /*TemplateArgs=*/0, |
| 9096 | /*SuppressQualifierCheck=*/true); |
| 9097 | if (OpEqualRef.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9098 | return StmtError(); |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9099 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9100 | // Build the call to the assignment operator. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9101 | |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9102 | Expr *FromInst = From.build(S, Loc); |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9103 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 9104 | OpEqualRef.takeAs<Expr>(), |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9105 | Loc, FromInst, Loc); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9106 | if (Call.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9107 | return StmtError(); |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9108 | |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9109 | // If we built a call to a trivial 'operator=' while copying an array, |
| 9110 | // bail out. We'll replace the whole shebang with a memcpy. |
| 9111 | CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Call.get()); |
| 9112 | if (CE && CE->getMethodDecl()->isTrivial() && Depth) |
| 9113 | return StmtResult((Stmt*)0); |
| 9114 | |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9115 | // Convert to an expression-statement, and clean up any produced |
| 9116 | // temporaries. |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 9117 | return S.ActOnExprStmt(Call); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9118 | } |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 9119 | |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9120 | // - if the subobject is of scalar type, the built-in assignment |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9121 | // operator is used. |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9122 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9123 | if (!ArrayTy) { |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9124 | ExprResult Assignment = S.CreateBuiltinBinOp( |
| 9125 | Loc, BO_Assign, To.build(S, Loc), From.build(S, Loc)); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9126 | if (Assignment.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9127 | return StmtError(); |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 9128 | return S.ActOnExprStmt(Assignment); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9129 | } |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9130 | |
| 9131 | // - if the subobject is an array, each element is assigned, in the |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9132 | // manner appropriate to the element type; |
Richard Smith | 52c0b58 | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 9133 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9134 | // Construct a loop over the array bounds, e.g., |
| 9135 | // |
| 9136 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 9137 | // |
| 9138 | // that will copy each of the array elements. |
| 9139 | QualType SizeType = S.Context.getSizeType(); |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9140 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9141 | // Create the iteration variable. |
| 9142 | IdentifierInfo *IterationVarName = 0; |
| 9143 | { |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 9144 | SmallString<8> Str; |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9145 | llvm::raw_svector_ostream OS(Str); |
| 9146 | OS << "__i" << Depth; |
| 9147 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 9148 | } |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9149 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9150 | IterationVarName, SizeType, |
| 9151 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 9152 | SC_None); |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9153 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9154 | // Initialize the iteration variable to zero. |
| 9155 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 9156 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9157 | |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9158 | // Creates a reference to the iteration variable. |
| 9159 | RefBuilder IterationVarRef(IterationVar, SizeType); |
| 9160 | LvalueConvBuilder IterationVarRefRVal(IterationVarRef); |
Eli Friedman | 844f945 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 9161 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9162 | // Create the DeclStmt that holds the iteration variable. |
| 9163 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9164 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9165 | // Subscript the "from" and "to" expressions with the iteration variable. |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9166 | SubscriptBuilder FromIndexCopy(From, IterationVarRefRVal); |
| 9167 | MoveCastBuilder FromIndexMove(FromIndexCopy); |
| 9168 | const ExprBuilder *FromIndex; |
| 9169 | if (Copying) |
| 9170 | FromIndex = &FromIndexCopy; |
| 9171 | else |
| 9172 | FromIndex = &FromIndexMove; |
| 9173 | |
| 9174 | SubscriptBuilder ToIndex(To, IterationVarRefRVal); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9175 | |
| 9176 | // Build the copy/move for an individual element of the array. |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9177 | StmtResult Copy = |
| 9178 | buildSingleCopyAssignRecursively(S, Loc, ArrayTy->getElementType(), |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9179 | ToIndex, *FromIndex, CopyingBaseSubobject, |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9180 | Copying, Depth + 1); |
| 9181 | // Bail out if copying fails or if we determined that we should use memcpy. |
| 9182 | if (Copy.isInvalid() || !Copy.get()) |
| 9183 | return Copy; |
| 9184 | |
| 9185 | // Create the comparison against the array bound. |
| 9186 | llvm::APInt Upper |
| 9187 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
| 9188 | Expr *Comparison |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9189 | = new (S.Context) BinaryOperator(IterationVarRefRVal.build(S, Loc), |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9190 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 9191 | BO_NE, S.Context.BoolTy, |
| 9192 | VK_RValue, OK_Ordinary, Loc, false); |
| 9193 | |
| 9194 | // Create the pre-increment of the iteration variable. |
| 9195 | Expr *Increment |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9196 | = new (S.Context) UnaryOperator(IterationVarRef.build(S, Loc), UO_PreInc, |
| 9197 | SizeType, VK_LValue, OK_Ordinary, Loc); |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9198 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9199 | // Construct the loop that copies all elements of this array. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9200 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9201 | S.MakeFullExpr(Comparison), |
Richard Smith | 945f8d3 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 9202 | 0, S.MakeFullDiscardedValueExpr(Increment), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9203 | Loc, Copy.take()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9204 | } |
| 9205 | |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9206 | static StmtResult |
| 9207 | buildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9208 | const ExprBuilder &To, const ExprBuilder &From, |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9209 | bool CopyingBaseSubobject, bool Copying) { |
| 9210 | // Maybe we should use a memcpy? |
| 9211 | if (T->isArrayType() && !T.isConstQualified() && !T.isVolatileQualified() && |
| 9212 | T.isTriviallyCopyableType(S.Context)) |
| 9213 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 9214 | |
| 9215 | StmtResult Result(buildSingleCopyAssignRecursively(S, Loc, T, To, From, |
| 9216 | CopyingBaseSubobject, |
| 9217 | Copying, 0)); |
| 9218 | |
| 9219 | // If we ended up picking a trivial assignment operator for an array of a |
| 9220 | // non-trivially-copyable class type, just emit a memcpy. |
| 9221 | if (!Result.isInvalid() && !Result.get()) |
| 9222 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 9223 | |
| 9224 | return Result; |
| 9225 | } |
| 9226 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9227 | Sema::ImplicitExceptionSpecification |
| 9228 | Sema::ComputeDefaultedCopyAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 9229 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 9230 | |
| 9231 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 9232 | if (ClassDecl->isInvalidDecl()) |
| 9233 | return ExceptSpec; |
| 9234 | |
| 9235 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 9236 | assert(T->getNumParams() == 1 && "not a copy assignment op"); |
| 9237 | unsigned ArgQuals = |
| 9238 | T->getParamType(0).getNonReferenceType().getCVRQualifiers(); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9239 | |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9240 | // C++ [except.spec]p14: |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9241 | // An implicitly declared special member function (Clause 12) shall have an |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9242 | // exception-specification. [...] |
Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9243 | |
| 9244 | // It is unspecified whether or not an implicit copy assignment operator |
| 9245 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 9246 | // made. As such, this exception specification is effectively unspecified. |
| 9247 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 9248 | // the side of assuming such calls to be made regardless of whether they |
| 9249 | // actually happen. |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9250 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9251 | BaseEnd = ClassDecl->bases_end(); |
| 9252 | Base != BaseEnd; ++Base) { |
Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9253 | if (Base->isVirtual()) |
| 9254 | continue; |
| 9255 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 9256 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9257 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9258 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 9259 | ArgQuals, false, 0)) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9260 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyAssign); |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9261 | } |
Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9262 | |
| 9263 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 9264 | BaseEnd = ClassDecl->vbases_end(); |
| 9265 | Base != BaseEnd; ++Base) { |
| 9266 | CXXRecordDecl *BaseClassDecl |
| 9267 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 9268 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 9269 | ArgQuals, false, 0)) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9270 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyAssign); |
Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9271 | } |
| 9272 | |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9273 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9274 | FieldEnd = ClassDecl->field_end(); |
| 9275 | Field != FieldEnd; |
| 9276 | ++Field) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 9277 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9278 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 9279 | if (CXXMethodDecl *CopyAssign = |
Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9280 | LookupCopyingAssignment(FieldClassDecl, |
| 9281 | ArgQuals | FieldType.getCVRQualifiers(), |
| 9282 | false, 0)) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9283 | ExceptSpec.CalledDecl(Field->getLocation(), CopyAssign); |
Abramo Bagnara | dd8fc04 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 9284 | } |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 9285 | } |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 9286 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9287 | return ExceptSpec; |
Alexis Hunt | 119f365 | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 9288 | } |
| 9289 | |
| 9290 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
| 9291 | // Note: The following rules are largely analoguous to the copy |
| 9292 | // constructor rules. Note that virtual bases are not taken into account |
| 9293 | // for determining the argument type of the operator. Note also that |
| 9294 | // operators taking an object instead of a reference are allowed. |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9295 | assert(ClassDecl->needsImplicitCopyAssignment()); |
Alexis Hunt | 119f365 | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 9296 | |
Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9297 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyAssignment); |
| 9298 | if (DSM.isAlreadyBeingDeclared()) |
| 9299 | return 0; |
| 9300 | |
Alexis Hunt | 119f365 | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 9301 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 9302 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9303 | bool Const = ClassDecl->implicitCopyAssignmentHasConstParam(); |
| 9304 | if (Const) |
Alexis Hunt | 119f365 | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 9305 | ArgType = ArgType.withConst(); |
| 9306 | ArgType = Context.getLValueReferenceType(ArgType); |
| 9307 | |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9308 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 9309 | CXXCopyAssignment, |
| 9310 | Const); |
| 9311 | |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 9312 | // An implicitly-declared copy assignment operator is an inline public |
| 9313 | // member of its class. |
| 9314 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9315 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 9316 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9317 | CXXMethodDecl *CopyAssignment = |
| 9318 | CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
| 9319 | /*TInfo=*/ 0, /*StorageClass=*/ SC_None, |
| 9320 | /*isInline=*/ true, Constexpr, SourceLocation()); |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 9321 | CopyAssignment->setAccess(AS_public); |
Alexis Hunt | b2f2780 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 9322 | CopyAssignment->setDefaulted(); |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 9323 | CopyAssignment->setImplicit(); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9324 | |
| 9325 | // Build an exception specification pointing back at this member. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9326 | FunctionProtoType::ExtProtoInfo EPI = |
| 9327 | getImplicitMethodEPI(*this, CopyAssignment); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9328 | CopyAssignment->setType(Context.getFunctionType(RetType, ArgType, EPI)); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9329 | |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 9330 | // Add the parameter to the operator. |
| 9331 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9332 | ClassLoc, ClassLoc, /*Id=*/0, |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 9333 | ArgType, /*TInfo=*/0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9334 | SC_None, 0); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9335 | CopyAssignment->setParams(FromParam); |
Alexis Hunt | b2f2780 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 9336 | |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9337 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 9338 | |
| 9339 | CopyAssignment->setTrivial( |
| 9340 | ClassDecl->needsOverloadResolutionForCopyAssignment() |
| 9341 | ? SpecialMemberIsTrivial(CopyAssignment, CXXCopyAssignment) |
| 9342 | : ClassDecl->hasTrivialCopyAssignment()); |
| 9343 | |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9344 | if (ShouldDeleteSpecialMember(CopyAssignment, CXXCopyAssignment)) |
Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 9345 | SetDeclDeleted(CopyAssignment, ClassLoc); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9346 | |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9347 | // Note that we have added this copy-assignment operator. |
| 9348 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
| 9349 | |
| 9350 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 9351 | PushOnScopeChains(CopyAssignment, S, false); |
| 9352 | ClassDecl->addDecl(CopyAssignment); |
| 9353 | |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 9354 | return CopyAssignment; |
| 9355 | } |
| 9356 | |
Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9357 | /// Diagnose an implicit copy operation for a class which is odr-used, but |
| 9358 | /// which is deprecated because the class has a user-declared copy constructor, |
| 9359 | /// copy assignment operator, or destructor. |
| 9360 | static void diagnoseDeprecatedCopyOperation(Sema &S, CXXMethodDecl *CopyOp, |
| 9361 | SourceLocation UseLoc) { |
| 9362 | assert(CopyOp->isImplicit()); |
| 9363 | |
| 9364 | CXXRecordDecl *RD = CopyOp->getParent(); |
| 9365 | CXXMethodDecl *UserDeclaredOperation = 0; |
| 9366 | |
| 9367 | // In Microsoft mode, assignment operations don't affect constructors and |
| 9368 | // vice versa. |
| 9369 | if (RD->hasUserDeclaredDestructor()) { |
| 9370 | UserDeclaredOperation = RD->getDestructor(); |
| 9371 | } else if (!isa<CXXConstructorDecl>(CopyOp) && |
| 9372 | RD->hasUserDeclaredCopyConstructor() && |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 9373 | !S.getLangOpts().MSVCCompat) { |
Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9374 | // Find any user-declared copy constructor. |
| 9375 | for (CXXRecordDecl::ctor_iterator I = RD->ctor_begin(), |
| 9376 | E = RD->ctor_end(); I != E; ++I) { |
| 9377 | if (I->isCopyConstructor()) { |
| 9378 | UserDeclaredOperation = *I; |
| 9379 | break; |
| 9380 | } |
| 9381 | } |
| 9382 | assert(UserDeclaredOperation); |
| 9383 | } else if (isa<CXXConstructorDecl>(CopyOp) && |
| 9384 | RD->hasUserDeclaredCopyAssignment() && |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 9385 | !S.getLangOpts().MSVCCompat) { |
Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9386 | // Find any user-declared move assignment operator. |
| 9387 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 9388 | E = RD->method_end(); I != E; ++I) { |
| 9389 | if (I->isCopyAssignmentOperator()) { |
| 9390 | UserDeclaredOperation = *I; |
| 9391 | break; |
| 9392 | } |
| 9393 | } |
| 9394 | assert(UserDeclaredOperation); |
| 9395 | } |
| 9396 | |
| 9397 | if (UserDeclaredOperation) { |
| 9398 | S.Diag(UserDeclaredOperation->getLocation(), |
| 9399 | diag::warn_deprecated_copy_operation) |
| 9400 | << RD << /*copy assignment*/!isa<CXXConstructorDecl>(CopyOp) |
| 9401 | << /*destructor*/isa<CXXDestructorDecl>(UserDeclaredOperation); |
| 9402 | S.Diag(UseLoc, diag::note_member_synthesized_at) |
| 9403 | << (isa<CXXConstructorDecl>(CopyOp) ? Sema::CXXCopyConstructor |
| 9404 | : Sema::CXXCopyAssignment) |
| 9405 | << RD; |
| 9406 | } |
| 9407 | } |
| 9408 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9409 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 9410 | CXXMethodDecl *CopyAssignOperator) { |
Alexis Hunt | b2f2780 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 9411 | assert((CopyAssignOperator->isDefaulted() && |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9412 | CopyAssignOperator->isOverloadedOperator() && |
| 9413 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 273c4e9 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9414 | !CopyAssignOperator->doesThisDeclarationHaveABody() && |
| 9415 | !CopyAssignOperator->isDeleted()) && |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9416 | "DefineImplicitCopyAssignment called for wrong function"); |
| 9417 | |
| 9418 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 9419 | |
| 9420 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 9421 | CopyAssignOperator->setInvalidDecl(); |
| 9422 | return; |
| 9423 | } |
Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9424 | |
| 9425 | // C++11 [class.copy]p18: |
| 9426 | // The [definition of an implicitly declared copy assignment operator] is |
| 9427 | // deprecated if the class has a user-declared copy constructor or a |
| 9428 | // user-declared destructor. |
| 9429 | if (getLangOpts().CPlusPlus11 && CopyAssignOperator->isImplicit()) |
| 9430 | diagnoseDeprecatedCopyOperation(*this, CopyAssignOperator, CurrentLocation); |
| 9431 | |
Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 9432 | CopyAssignOperator->markUsed(Context); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9433 | |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9434 | SynthesizedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 1865342 | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 9435 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9436 | |
| 9437 | // C++0x [class.copy]p30: |
| 9438 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 9439 | // for a non-union class X performs memberwise copy assignment of its |
| 9440 | // subobjects. The direct base classes of X are assigned first, in the |
| 9441 | // order of their declaration in the base-specifier-list, and then the |
| 9442 | // immediate non-static data members of X are assigned, in the order in |
| 9443 | // which they were declared in the class definition. |
| 9444 | |
| 9445 | // The statements that form the synthesized function body. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 9446 | SmallVector<Stmt*, 8> Statements; |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9447 | |
| 9448 | // The parameter for the "other" object, which we are copying from. |
| 9449 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 9450 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 9451 | QualType OtherRefType = Other->getType(); |
| 9452 | if (const LValueReferenceType *OtherRef |
| 9453 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 9454 | OtherRefType = OtherRef->getPointeeType(); |
| 9455 | OtherQuals = OtherRefType.getQualifiers(); |
| 9456 | } |
| 9457 | |
| 9458 | // Our location for everything implicitly-generated. |
| 9459 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 9460 | |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9461 | // Builds a DeclRefExpr for the "other" object. |
| 9462 | RefBuilder OtherRef(Other, OtherRefType); |
| 9463 | |
| 9464 | // Builds the "this" pointer. |
| 9465 | ThisBuilder This; |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9466 | |
| 9467 | // Assign base classes. |
| 9468 | bool Invalid = false; |
| 9469 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9470 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 9471 | // Form the assignment: |
| 9472 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 9473 | QualType BaseType = Base->getType().getUnqualifiedType(); |
Jeffrey Yasskin | 8dfa5f1 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 9474 | if (!BaseType->isRecordType()) { |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9475 | Invalid = true; |
| 9476 | continue; |
| 9477 | } |
| 9478 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9479 | CXXCastPath BasePath; |
| 9480 | BasePath.push_back(Base); |
| 9481 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9482 | // Construct the "from" expression, which is an implicit cast to the |
| 9483 | // appropriately-qualified base type. |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9484 | CastBuilder From(OtherRef, Context.getQualifiedType(BaseType, OtherQuals), |
| 9485 | VK_LValue, BasePath); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9486 | |
| 9487 | // Dereference "this". |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9488 | DerefBuilder DerefThis(This); |
| 9489 | CastBuilder To(DerefThis, |
| 9490 | Context.getCVRQualifiedType( |
| 9491 | BaseType, CopyAssignOperator->getTypeQualifiers()), |
| 9492 | VK_LValue, BasePath); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9493 | |
| 9494 | // Build the copy. |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9495 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, BaseType, |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9496 | To, From, |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9497 | /*CopyingBaseSubobject=*/true, |
| 9498 | /*Copying=*/true); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9499 | if (Copy.isInvalid()) { |
Douglas Gregor | bf1fb44 | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9500 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9501 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 9502 | CopyAssignOperator->setInvalidDecl(); |
| 9503 | return; |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9504 | } |
| 9505 | |
| 9506 | // Success! Record the copy. |
| 9507 | Statements.push_back(Copy.takeAs<Expr>()); |
| 9508 | } |
| 9509 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9510 | // Assign non-static members. |
| 9511 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9512 | FieldEnd = ClassDecl->field_end(); |
| 9513 | Field != FieldEnd; ++Field) { |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 9514 | if (Field->isUnnamedBitfield()) |
| 9515 | continue; |
Eli Friedman | c9817fd | 2013-06-07 01:48:56 +0000 | [diff] [blame] | 9516 | |
| 9517 | if (Field->isInvalidDecl()) { |
| 9518 | Invalid = true; |
| 9519 | continue; |
| 9520 | } |
| 9521 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9522 | // Check for members of reference type; we can't copy those. |
| 9523 | if (Field->getType()->isReferenceType()) { |
| 9524 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9525 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 9526 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | bf1fb44 | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9527 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9528 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9529 | Invalid = true; |
| 9530 | continue; |
| 9531 | } |
| 9532 | |
| 9533 | // Check for members of const-qualified, non-class type. |
| 9534 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 9535 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 9536 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9537 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 9538 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | bf1fb44 | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9539 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9540 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9541 | Invalid = true; |
| 9542 | continue; |
| 9543 | } |
John McCall | 1b1a1db | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 9544 | |
| 9545 | // Suppress assigning zero-width bitfields. |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9546 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 9547 | continue; |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9548 | |
| 9549 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 1138ba6 | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 9550 | if (FieldType->isIncompleteArrayType()) { |
| 9551 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 9552 | "Incomplete array type is not valid"); |
| 9553 | continue; |
| 9554 | } |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9555 | |
| 9556 | // Build references to the field in the object we're copying from and to. |
| 9557 | CXXScopeSpec SS; // Intentionally empty |
| 9558 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 9559 | LookupMemberName); |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 9560 | MemberLookup.addDecl(*Field); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9561 | MemberLookup.resolveKind(); |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9562 | |
| 9563 | MemberBuilder From(OtherRef, OtherRefType, /*IsArrow=*/false, MemberLookup); |
| 9564 | |
| 9565 | MemberBuilder To(This, getCurrentThisType(), /*IsArrow=*/true, MemberLookup); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9566 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9567 | // Build the copy of this field. |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9568 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, FieldType, |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9569 | To, From, |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9570 | /*CopyingBaseSubobject=*/false, |
| 9571 | /*Copying=*/true); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9572 | if (Copy.isInvalid()) { |
Douglas Gregor | bf1fb44 | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9573 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9574 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 9575 | CopyAssignOperator->setInvalidDecl(); |
| 9576 | return; |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9577 | } |
| 9578 | |
| 9579 | // Success! Record the copy. |
| 9580 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 9581 | } |
| 9582 | |
| 9583 | if (!Invalid) { |
| 9584 | // Add a "return *this;" |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9585 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This.build(*this, Loc)); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9586 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9587 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9588 | if (Return.isInvalid()) |
| 9589 | Invalid = true; |
| 9590 | else { |
| 9591 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 9592 | |
| 9593 | if (Trap.hasErrorOccurred()) { |
| 9594 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9595 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 9596 | Invalid = true; |
| 9597 | } |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9598 | } |
| 9599 | } |
| 9600 | |
| 9601 | if (Invalid) { |
| 9602 | CopyAssignOperator->setInvalidDecl(); |
| 9603 | return; |
| 9604 | } |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9605 | |
| 9606 | StmtResult Body; |
| 9607 | { |
| 9608 | CompoundScopeRAII CompoundScope(*this); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9609 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9610 | /*isStmtExpr=*/false); |
| 9611 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 9612 | } |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9613 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 9614 | |
| 9615 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9616 | L->CompletedImplicitDefinition(CopyAssignOperator); |
| 9617 | } |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9618 | } |
| 9619 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9620 | Sema::ImplicitExceptionSpecification |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9621 | Sema::ComputeDefaultedMoveAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 9622 | CXXRecordDecl *ClassDecl = MD->getParent(); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9623 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9624 | ImplicitExceptionSpecification ExceptSpec(*this); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9625 | if (ClassDecl->isInvalidDecl()) |
| 9626 | return ExceptSpec; |
| 9627 | |
| 9628 | // C++0x [except.spec]p14: |
| 9629 | // An implicitly declared special member function (Clause 12) shall have an |
| 9630 | // exception-specification. [...] |
| 9631 | |
| 9632 | // It is unspecified whether or not an implicit move assignment operator |
| 9633 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 9634 | // made. As such, this exception specification is effectively unspecified. |
| 9635 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 9636 | // the side of assuming such calls to be made regardless of whether they |
| 9637 | // actually happen. |
| 9638 | // Note that a move constructor is not implicitly declared when there are |
| 9639 | // virtual bases, but it can still be user-declared and explicitly defaulted. |
| 9640 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9641 | BaseEnd = ClassDecl->bases_end(); |
| 9642 | Base != BaseEnd; ++Base) { |
| 9643 | if (Base->isVirtual()) |
| 9644 | continue; |
| 9645 | |
| 9646 | CXXRecordDecl *BaseClassDecl |
| 9647 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 9648 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9649 | 0, false, 0)) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9650 | ExceptSpec.CalledDecl(Base->getLocStart(), MoveAssign); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9651 | } |
| 9652 | |
| 9653 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 9654 | BaseEnd = ClassDecl->vbases_end(); |
| 9655 | Base != BaseEnd; ++Base) { |
| 9656 | CXXRecordDecl *BaseClassDecl |
| 9657 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 9658 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9659 | 0, false, 0)) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9660 | ExceptSpec.CalledDecl(Base->getLocStart(), MoveAssign); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9661 | } |
| 9662 | |
| 9663 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9664 | FieldEnd = ClassDecl->field_end(); |
| 9665 | Field != FieldEnd; |
| 9666 | ++Field) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 9667 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9668 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9669 | if (CXXMethodDecl *MoveAssign = |
| 9670 | LookupMovingAssignment(FieldClassDecl, |
| 9671 | FieldType.getCVRQualifiers(), |
| 9672 | false, 0)) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9673 | ExceptSpec.CalledDecl(Field->getLocation(), MoveAssign); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9674 | } |
| 9675 | } |
| 9676 | |
| 9677 | return ExceptSpec; |
| 9678 | } |
| 9679 | |
| 9680 | CXXMethodDecl *Sema::DeclareImplicitMoveAssignment(CXXRecordDecl *ClassDecl) { |
Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9681 | assert(ClassDecl->needsImplicitMoveAssignment()); |
| 9682 | |
Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9683 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveAssignment); |
| 9684 | if (DSM.isAlreadyBeingDeclared()) |
| 9685 | return 0; |
| 9686 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9687 | // Note: The following rules are largely analoguous to the move |
| 9688 | // constructor rules. |
| 9689 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9690 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 9691 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 9692 | ArgType = Context.getRValueReferenceType(ArgType); |
| 9693 | |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9694 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 9695 | CXXMoveAssignment, |
| 9696 | false); |
| 9697 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9698 | // An implicitly-declared move assignment operator is an inline public |
| 9699 | // member of its class. |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9700 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 9701 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 9702 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9703 | CXXMethodDecl *MoveAssignment = |
| 9704 | CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
| 9705 | /*TInfo=*/0, /*StorageClass=*/SC_None, |
| 9706 | /*isInline=*/true, Constexpr, SourceLocation()); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9707 | MoveAssignment->setAccess(AS_public); |
| 9708 | MoveAssignment->setDefaulted(); |
| 9709 | MoveAssignment->setImplicit(); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9710 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9711 | // Build an exception specification pointing back at this member. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9712 | FunctionProtoType::ExtProtoInfo EPI = |
| 9713 | getImplicitMethodEPI(*this, MoveAssignment); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9714 | MoveAssignment->setType(Context.getFunctionType(RetType, ArgType, EPI)); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9715 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9716 | // Add the parameter to the operator. |
| 9717 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveAssignment, |
| 9718 | ClassLoc, ClassLoc, /*Id=*/0, |
| 9719 | ArgType, /*TInfo=*/0, |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9720 | SC_None, 0); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9721 | MoveAssignment->setParams(FromParam); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9722 | |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9723 | AddOverriddenMethods(ClassDecl, MoveAssignment); |
| 9724 | |
| 9725 | MoveAssignment->setTrivial( |
| 9726 | ClassDecl->needsOverloadResolutionForMoveAssignment() |
| 9727 | ? SpecialMemberIsTrivial(MoveAssignment, CXXMoveAssignment) |
| 9728 | : ClassDecl->hasTrivialMoveAssignment()); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9729 | |
Richard Smith | d951a1d | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 9730 | if (ShouldDeleteSpecialMember(MoveAssignment, CXXMoveAssignment)) { |
Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 9731 | ClassDecl->setImplicitMoveAssignmentIsDeleted(); |
| 9732 | SetDeclDeleted(MoveAssignment, ClassLoc); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9733 | } |
| 9734 | |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9735 | // Note that we have added this copy-assignment operator. |
| 9736 | ++ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
| 9737 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9738 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 9739 | PushOnScopeChains(MoveAssignment, S, false); |
| 9740 | ClassDecl->addDecl(MoveAssignment); |
| 9741 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9742 | return MoveAssignment; |
| 9743 | } |
| 9744 | |
Richard Smith | b2504bd | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 9745 | /// Check if we're implicitly defining a move assignment operator for a class |
| 9746 | /// with virtual bases. Such a move assignment might move-assign the virtual |
| 9747 | /// base multiple times. |
| 9748 | static void checkMoveAssignmentForRepeatedMove(Sema &S, CXXRecordDecl *Class, |
| 9749 | SourceLocation CurrentLocation) { |
| 9750 | assert(!Class->isDependentContext() && "should not define dependent move"); |
| 9751 | |
| 9752 | // Only a virtual base could get implicitly move-assigned multiple times. |
| 9753 | // Only a non-trivial move assignment can observe this. We only want to |
| 9754 | // diagnose if we implicitly define an assignment operator that assigns |
| 9755 | // two base classes, both of which move-assign the same virtual base. |
| 9756 | if (Class->getNumVBases() == 0 || Class->hasTrivialMoveAssignment() || |
| 9757 | Class->getNumBases() < 2) |
| 9758 | return; |
| 9759 | |
| 9760 | llvm::SmallVector<CXXBaseSpecifier *, 16> Worklist; |
| 9761 | typedef llvm::DenseMap<CXXRecordDecl*, CXXBaseSpecifier*> VBaseMap; |
| 9762 | VBaseMap VBases; |
| 9763 | |
| 9764 | for (CXXRecordDecl::base_class_iterator BI = Class->bases_begin(), |
| 9765 | BE = Class->bases_end(); |
| 9766 | BI != BE; ++BI) { |
| 9767 | Worklist.push_back(&*BI); |
| 9768 | while (!Worklist.empty()) { |
| 9769 | CXXBaseSpecifier *BaseSpec = Worklist.pop_back_val(); |
| 9770 | CXXRecordDecl *Base = BaseSpec->getType()->getAsCXXRecordDecl(); |
| 9771 | |
| 9772 | // If the base has no non-trivial move assignment operators, |
| 9773 | // we don't care about moves from it. |
| 9774 | if (!Base->hasNonTrivialMoveAssignment()) |
| 9775 | continue; |
| 9776 | |
| 9777 | // If there's nothing virtual here, skip it. |
| 9778 | if (!BaseSpec->isVirtual() && !Base->getNumVBases()) |
| 9779 | continue; |
| 9780 | |
| 9781 | // If we're not actually going to call a move assignment for this base, |
| 9782 | // or the selected move assignment is trivial, skip it. |
| 9783 | Sema::SpecialMemberOverloadResult *SMOR = |
| 9784 | S.LookupSpecialMember(Base, Sema::CXXMoveAssignment, |
| 9785 | /*ConstArg*/false, /*VolatileArg*/false, |
| 9786 | /*RValueThis*/true, /*ConstThis*/false, |
| 9787 | /*VolatileThis*/false); |
| 9788 | if (!SMOR->getMethod() || SMOR->getMethod()->isTrivial() || |
| 9789 | !SMOR->getMethod()->isMoveAssignmentOperator()) |
| 9790 | continue; |
| 9791 | |
| 9792 | if (BaseSpec->isVirtual()) { |
| 9793 | // We're going to move-assign this virtual base, and its move |
| 9794 | // assignment operator is not trivial. If this can happen for |
| 9795 | // multiple distinct direct bases of Class, diagnose it. (If it |
| 9796 | // only happens in one base, we'll diagnose it when synthesizing |
| 9797 | // that base class's move assignment operator.) |
| 9798 | CXXBaseSpecifier *&Existing = |
| 9799 | VBases.insert(std::make_pair(Base->getCanonicalDecl(), BI)) |
| 9800 | .first->second; |
| 9801 | if (Existing && Existing != BI) { |
| 9802 | S.Diag(CurrentLocation, diag::warn_vbase_moved_multiple_times) |
| 9803 | << Class << Base; |
| 9804 | S.Diag(Existing->getLocStart(), diag::note_vbase_moved_here) |
| 9805 | << (Base->getCanonicalDecl() == |
| 9806 | Existing->getType()->getAsCXXRecordDecl()->getCanonicalDecl()) |
| 9807 | << Base << Existing->getType() << Existing->getSourceRange(); |
| 9808 | S.Diag(BI->getLocStart(), diag::note_vbase_moved_here) |
| 9809 | << (Base->getCanonicalDecl() == |
| 9810 | BI->getType()->getAsCXXRecordDecl()->getCanonicalDecl()) |
| 9811 | << Base << BI->getType() << BaseSpec->getSourceRange(); |
| 9812 | |
| 9813 | // Only diagnose each vbase once. |
| 9814 | Existing = 0; |
| 9815 | } |
| 9816 | } else { |
| 9817 | // Only walk over bases that have defaulted move assignment operators. |
| 9818 | // We assume that any user-provided move assignment operator handles |
| 9819 | // the multiple-moves-of-vbase case itself somehow. |
| 9820 | if (!SMOR->getMethod()->isDefaulted()) |
| 9821 | continue; |
| 9822 | |
| 9823 | // We're going to move the base classes of Base. Add them to the list. |
| 9824 | for (CXXRecordDecl::base_class_iterator BI = Base->bases_begin(), |
| 9825 | BE = Base->bases_end(); |
| 9826 | BI != BE; ++BI) |
| 9827 | Worklist.push_back(&*BI); |
| 9828 | } |
| 9829 | } |
| 9830 | } |
| 9831 | } |
| 9832 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9833 | void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation, |
| 9834 | CXXMethodDecl *MoveAssignOperator) { |
| 9835 | assert((MoveAssignOperator->isDefaulted() && |
| 9836 | MoveAssignOperator->isOverloadedOperator() && |
| 9837 | MoveAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 273c4e9 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9838 | !MoveAssignOperator->doesThisDeclarationHaveABody() && |
| 9839 | !MoveAssignOperator->isDeleted()) && |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9840 | "DefineImplicitMoveAssignment called for wrong function"); |
| 9841 | |
| 9842 | CXXRecordDecl *ClassDecl = MoveAssignOperator->getParent(); |
| 9843 | |
| 9844 | if (ClassDecl->isInvalidDecl() || MoveAssignOperator->isInvalidDecl()) { |
| 9845 | MoveAssignOperator->setInvalidDecl(); |
| 9846 | return; |
| 9847 | } |
| 9848 | |
Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 9849 | MoveAssignOperator->markUsed(Context); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9850 | |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9851 | SynthesizedFunctionScope Scope(*this, MoveAssignOperator); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9852 | DiagnosticErrorTrap Trap(Diags); |
| 9853 | |
| 9854 | // C++0x [class.copy]p28: |
| 9855 | // The implicitly-defined or move assignment operator for a non-union class |
| 9856 | // X performs memberwise move assignment of its subobjects. The direct base |
| 9857 | // classes of X are assigned first, in the order of their declaration in the |
| 9858 | // base-specifier-list, and then the immediate non-static data members of X |
| 9859 | // are assigned, in the order in which they were declared in the class |
| 9860 | // definition. |
| 9861 | |
Richard Smith | b2504bd | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 9862 | // Issue a warning if our implicit move assignment operator will move |
| 9863 | // from a virtual base more than once. |
| 9864 | checkMoveAssignmentForRepeatedMove(*this, ClassDecl, CurrentLocation); |
Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 9865 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9866 | // The statements that form the synthesized function body. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 9867 | SmallVector<Stmt*, 8> Statements; |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9868 | |
| 9869 | // The parameter for the "other" object, which we are move from. |
| 9870 | ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0); |
| 9871 | QualType OtherRefType = Other->getType()-> |
| 9872 | getAs<RValueReferenceType>()->getPointeeType(); |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 9873 | assert(!OtherRefType.getQualifiers() && |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9874 | "Bad argument type of defaulted move assignment"); |
| 9875 | |
| 9876 | // Our location for everything implicitly-generated. |
| 9877 | SourceLocation Loc = MoveAssignOperator->getLocation(); |
| 9878 | |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9879 | // Builds a reference to the "other" object. |
| 9880 | RefBuilder OtherRef(Other, OtherRefType); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9881 | // Cast to rvalue. |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9882 | MoveCastBuilder MoveOther(OtherRef); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9883 | |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9884 | // Builds the "this" pointer. |
| 9885 | ThisBuilder This; |
Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9886 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9887 | // Assign base classes. |
| 9888 | bool Invalid = false; |
| 9889 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9890 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Richard Smith | b2504bd | 2013-11-04 04:26:14 +0000 | [diff] [blame] | 9891 | // C++11 [class.copy]p28: |
| 9892 | // It is unspecified whether subobjects representing virtual base classes |
| 9893 | // are assigned more than once by the implicitly-defined copy assignment |
| 9894 | // operator. |
| 9895 | // FIXME: Do not assign to a vbase that will be assigned by some other base |
| 9896 | // class. For a move-assignment, this can result in the vbase being moved |
| 9897 | // multiple times. |
| 9898 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9899 | // Form the assignment: |
| 9900 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&&>(other)); |
| 9901 | QualType BaseType = Base->getType().getUnqualifiedType(); |
| 9902 | if (!BaseType->isRecordType()) { |
| 9903 | Invalid = true; |
| 9904 | continue; |
| 9905 | } |
| 9906 | |
| 9907 | CXXCastPath BasePath; |
| 9908 | BasePath.push_back(Base); |
| 9909 | |
| 9910 | // Construct the "from" expression, which is an implicit cast to the |
| 9911 | // appropriately-qualified base type. |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9912 | CastBuilder From(OtherRef, BaseType, VK_XValue, BasePath); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9913 | |
| 9914 | // Dereference "this". |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9915 | DerefBuilder DerefThis(This); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9916 | |
| 9917 | // Implicitly cast "this" to the appropriately-qualified base type. |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9918 | CastBuilder To(DerefThis, |
| 9919 | Context.getCVRQualifiedType( |
| 9920 | BaseType, MoveAssignOperator->getTypeQualifiers()), |
| 9921 | VK_LValue, BasePath); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9922 | |
| 9923 | // Build the move. |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9924 | StmtResult Move = buildSingleCopyAssign(*this, Loc, BaseType, |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9925 | To, From, |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9926 | /*CopyingBaseSubobject=*/true, |
| 9927 | /*Copying=*/false); |
| 9928 | if (Move.isInvalid()) { |
| 9929 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9930 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9931 | MoveAssignOperator->setInvalidDecl(); |
| 9932 | return; |
| 9933 | } |
| 9934 | |
| 9935 | // Success! Record the move. |
| 9936 | Statements.push_back(Move.takeAs<Expr>()); |
| 9937 | } |
| 9938 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9939 | // Assign non-static members. |
| 9940 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9941 | FieldEnd = ClassDecl->field_end(); |
| 9942 | Field != FieldEnd; ++Field) { |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 9943 | if (Field->isUnnamedBitfield()) |
| 9944 | continue; |
| 9945 | |
Eli Friedman | c9817fd | 2013-06-07 01:48:56 +0000 | [diff] [blame] | 9946 | if (Field->isInvalidDecl()) { |
| 9947 | Invalid = true; |
| 9948 | continue; |
| 9949 | } |
| 9950 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9951 | // Check for members of reference type; we can't move those. |
| 9952 | if (Field->getType()->isReferenceType()) { |
| 9953 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9954 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 9955 | Diag(Field->getLocation(), diag::note_declared_at); |
| 9956 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9957 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9958 | Invalid = true; |
| 9959 | continue; |
| 9960 | } |
| 9961 | |
| 9962 | // Check for members of const-qualified, non-class type. |
| 9963 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 9964 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 9965 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9966 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 9967 | Diag(Field->getLocation(), diag::note_declared_at); |
| 9968 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9969 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9970 | Invalid = true; |
| 9971 | continue; |
| 9972 | } |
| 9973 | |
| 9974 | // Suppress assigning zero-width bitfields. |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9975 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 9976 | continue; |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9977 | |
| 9978 | QualType FieldType = Field->getType().getNonReferenceType(); |
| 9979 | if (FieldType->isIncompleteArrayType()) { |
| 9980 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 9981 | "Incomplete array type is not valid"); |
| 9982 | continue; |
| 9983 | } |
| 9984 | |
| 9985 | // 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] | 9986 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 9987 | LookupMemberName); |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 9988 | MemberLookup.addDecl(*Field); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9989 | MemberLookup.resolveKind(); |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9990 | MemberBuilder From(MoveOther, OtherRefType, |
| 9991 | /*IsArrow=*/false, MemberLookup); |
| 9992 | MemberBuilder To(This, getCurrentThisType(), |
| 9993 | /*IsArrow=*/true, MemberLookup); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9994 | |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 9995 | assert(!From.build(*this, Loc)->isLValue() && // could be xvalue or prvalue |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9996 | "Member reference with rvalue base must be rvalue except for reference " |
| 9997 | "members, which aren't allowed for move assignment."); |
| 9998 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9999 | // Build the move of this field. |
Richard Smith | 41ae328 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 10000 | StmtResult Move = buildSingleCopyAssign(*this, Loc, FieldType, |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10001 | To, From, |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10002 | /*CopyingBaseSubobject=*/false, |
| 10003 | /*Copying=*/false); |
| 10004 | if (Move.isInvalid()) { |
| 10005 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10006 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 10007 | MoveAssignOperator->setInvalidDecl(); |
| 10008 | return; |
| 10009 | } |
Richard Smith | 11d1959 | 2012-11-12 23:33:00 +0000 | [diff] [blame] | 10010 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10011 | // Success! Record the copy. |
| 10012 | Statements.push_back(Move.takeAs<Stmt>()); |
| 10013 | } |
| 10014 | |
| 10015 | if (!Invalid) { |
| 10016 | // Add a "return *this;" |
Pavel Labath | 5893498 | 2013-08-30 08:52:28 +0000 | [diff] [blame] | 10017 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This.build(*this, Loc)); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10018 | |
| 10019 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
| 10020 | if (Return.isInvalid()) |
| 10021 | Invalid = true; |
| 10022 | else { |
| 10023 | Statements.push_back(Return.takeAs<Stmt>()); |
| 10024 | |
| 10025 | if (Trap.hasErrorOccurred()) { |
| 10026 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10027 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 10028 | Invalid = true; |
| 10029 | } |
| 10030 | } |
| 10031 | } |
| 10032 | |
| 10033 | if (Invalid) { |
| 10034 | MoveAssignOperator->setInvalidDecl(); |
| 10035 | return; |
| 10036 | } |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 10037 | |
| 10038 | StmtResult Body; |
| 10039 | { |
| 10040 | CompoundScopeRAII CompoundScope(*this); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10041 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 10042 | /*isStmtExpr=*/false); |
| 10043 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 10044 | } |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10045 | MoveAssignOperator->setBody(Body.takeAs<Stmt>()); |
| 10046 | |
| 10047 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10048 | L->CompletedImplicitDefinition(MoveAssignOperator); |
| 10049 | } |
| 10050 | } |
| 10051 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10052 | Sema::ImplicitExceptionSpecification |
| 10053 | Sema::ComputeDefaultedCopyCtorExceptionSpec(CXXMethodDecl *MD) { |
| 10054 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 10055 | |
| 10056 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 10057 | if (ClassDecl->isInvalidDecl()) |
| 10058 | return ExceptSpec; |
| 10059 | |
| 10060 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 10061 | assert(T->getNumParams() >= 1 && "not a copy ctor"); |
| 10062 | unsigned Quals = T->getParamType(0).getNonReferenceType().getCVRQualifiers(); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10063 | |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10064 | // C++ [except.spec]p14: |
| 10065 | // An implicitly declared special member function (Clause 12) shall have an |
| 10066 | // exception-specification. [...] |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10067 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 10068 | BaseEnd = ClassDecl->bases_end(); |
| 10069 | Base != BaseEnd; |
| 10070 | ++Base) { |
| 10071 | // Virtual bases are handled below. |
| 10072 | if (Base->isVirtual()) |
| 10073 | continue; |
| 10074 | |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 10075 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10076 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Alexis Hunt | 899bd44 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 10077 | if (CXXConstructorDecl *CopyConstructor = |
Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 10078 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10079 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyConstructor); |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10080 | } |
| 10081 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 10082 | BaseEnd = ClassDecl->vbases_end(); |
| 10083 | Base != BaseEnd; |
| 10084 | ++Base) { |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 10085 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10086 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Alexis Hunt | 899bd44 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 10087 | if (CXXConstructorDecl *CopyConstructor = |
Alexis Hunt | 491ec60 | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 10088 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10089 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyConstructor); |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10090 | } |
| 10091 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 10092 | FieldEnd = ClassDecl->field_end(); |
| 10093 | Field != FieldEnd; |
| 10094 | ++Field) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 10095 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Alexis Hunt | 899bd44 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 10096 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 10097 | if (CXXConstructorDecl *CopyConstructor = |
Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10098 | LookupCopyingConstructor(FieldClassDecl, |
| 10099 | Quals | FieldType.getCVRQualifiers())) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10100 | ExceptSpec.CalledDecl(Field->getLocation(), CopyConstructor); |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 10101 | } |
| 10102 | } |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 10103 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10104 | return ExceptSpec; |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10105 | } |
| 10106 | |
| 10107 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 10108 | CXXRecordDecl *ClassDecl) { |
| 10109 | // C++ [class.copy]p4: |
| 10110 | // If the class definition does not explicitly declare a copy |
| 10111 | // constructor, one is declared implicitly. |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 10112 | assert(ClassDecl->needsImplicitCopyConstructor()); |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10113 | |
Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 10114 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyConstructor); |
| 10115 | if (DSM.isAlreadyBeingDeclared()) |
| 10116 | return 0; |
| 10117 | |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10118 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 10119 | QualType ArgType = ClassType; |
Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 10120 | bool Const = ClassDecl->implicitCopyConstructorHasConstParam(); |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10121 | if (Const) |
| 10122 | ArgType = ArgType.withConst(); |
| 10123 | ArgType = Context.getLValueReferenceType(ArgType); |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10124 | |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 10125 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 10126 | CXXCopyConstructor, |
| 10127 | Const); |
| 10128 | |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10129 | DeclarationName Name |
| 10130 | = Context.DeclarationNames.getCXXConstructorName( |
| 10131 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10132 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 10133 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10134 | |
| 10135 | // An implicitly-declared copy constructor is an inline public |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10136 | // member of its class. |
| 10137 | CXXConstructorDecl *CopyConstructor = CXXConstructorDecl::Create( |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10138 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/0, |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10139 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 10140 | Constexpr); |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10141 | CopyConstructor->setAccess(AS_public); |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10142 | CopyConstructor->setDefaulted(); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10143 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10144 | // Build an exception specification pointing back at this member. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 10145 | FunctionProtoType::ExtProtoInfo EPI = |
| 10146 | getImplicitMethodEPI(*this, CopyConstructor); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10147 | CopyConstructor->setType( |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 10148 | Context.getFunctionType(Context.VoidTy, ArgType, EPI)); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10149 | |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10150 | // Add the parameter to the constructor. |
| 10151 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10152 | ClassLoc, ClassLoc, |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10153 | /*IdentifierInfo=*/0, |
| 10154 | ArgType, /*TInfo=*/0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 10155 | SC_None, 0); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 10156 | CopyConstructor->setParams(FromParam); |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10157 | |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 10158 | CopyConstructor->setTrivial( |
| 10159 | ClassDecl->needsOverloadResolutionForCopyConstructor() |
| 10160 | ? SpecialMemberIsTrivial(CopyConstructor, CXXCopyConstructor) |
| 10161 | : ClassDecl->hasTrivialCopyConstructor()); |
Alexis Hunt | e77a28f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 10162 | |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 10163 | if (ShouldDeleteSpecialMember(CopyConstructor, CXXCopyConstructor)) |
Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 10164 | SetDeclDeleted(CopyConstructor, ClassLoc); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 10165 | |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 10166 | // Note that we have declared this constructor. |
| 10167 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 10168 | |
| 10169 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 10170 | PushOnScopeChains(CopyConstructor, S, false); |
| 10171 | ClassDecl->addDecl(CopyConstructor); |
| 10172 | |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 10173 | return CopyConstructor; |
| 10174 | } |
| 10175 | |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10176 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10177 | CXXConstructorDecl *CopyConstructor) { |
| 10178 | assert((CopyConstructor->isDefaulted() && |
| 10179 | CopyConstructor->isCopyConstructor() && |
Richard Smith | 273c4e9 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10180 | !CopyConstructor->doesThisDeclarationHaveABody() && |
| 10181 | !CopyConstructor->isDeleted()) && |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10182 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10183 | |
Anders Carlsson | 7a0ffdb | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 10184 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10185 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10186 | |
Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10187 | // C++11 [class.copy]p7: |
Benjamin Kramer | 60509af | 2013-09-09 14:48:42 +0000 | [diff] [blame] | 10188 | // The [definition of an implicitly declared copy constructor] is |
Richard Smith | d577fbb | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 10189 | // deprecated if the class has a user-declared copy assignment operator |
| 10190 | // or a user-declared destructor. |
| 10191 | if (getLangOpts().CPlusPlus11 && CopyConstructor->isImplicit()) |
| 10192 | diagnoseDeprecatedCopyOperation(*this, CopyConstructor, CurrentLocation); |
| 10193 | |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10194 | SynthesizedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 1865342 | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 10195 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10196 | |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 10197 | if (SetCtorInitializers(CopyConstructor, /*AnyErrors=*/false) || |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 10198 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 10199 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 10200 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 10201 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 10202 | } else { |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 10203 | Sema::CompoundScopeRAII CompoundScope(*this); |
Robert Wilhelm | 27b2c9a3 | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 10204 | CopyConstructor->setBody(ActOnCompoundStmt( |
| 10205 | CopyConstructor->getLocation(), CopyConstructor->getLocation(), None, |
| 10206 | /*isStmtExpr=*/ false).takeAs<Stmt>()); |
Anders Carlsson | 53e1ba9 | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 10207 | } |
Robert Wilhelm | 27b2c9a3 | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 10208 | |
Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 10209 | CopyConstructor->markUsed(Context); |
Sebastian Redl | ab238a7 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 10210 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10211 | L->CompletedImplicitDefinition(CopyConstructor); |
| 10212 | } |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10213 | } |
| 10214 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10215 | Sema::ImplicitExceptionSpecification |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10216 | Sema::ComputeDefaultedMoveCtorExceptionSpec(CXXMethodDecl *MD) { |
| 10217 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 10218 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10219 | // C++ [except.spec]p14: |
| 10220 | // An implicitly declared special member function (Clause 12) shall have an |
| 10221 | // exception-specification. [...] |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10222 | ImplicitExceptionSpecification ExceptSpec(*this); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10223 | if (ClassDecl->isInvalidDecl()) |
| 10224 | return ExceptSpec; |
| 10225 | |
| 10226 | // Direct base-class constructors. |
| 10227 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 10228 | BEnd = ClassDecl->bases_end(); |
| 10229 | B != BEnd; ++B) { |
| 10230 | if (B->isVirtual()) // Handled below. |
| 10231 | continue; |
| 10232 | |
| 10233 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 10234 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10235 | CXXConstructorDecl *Constructor = |
| 10236 | LookupMovingConstructor(BaseClassDecl, 0); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10237 | // If this is a deleted function, add it anyway. This might be conformant |
| 10238 | // with the standard. This might not. I'm not sure. It might not matter. |
| 10239 | if (Constructor) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10240 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10241 | } |
| 10242 | } |
| 10243 | |
| 10244 | // Virtual base-class constructors. |
| 10245 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 10246 | BEnd = ClassDecl->vbases_end(); |
| 10247 | B != BEnd; ++B) { |
| 10248 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 10249 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10250 | CXXConstructorDecl *Constructor = |
| 10251 | LookupMovingConstructor(BaseClassDecl, 0); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10252 | // If this is a deleted function, add it anyway. This might be conformant |
| 10253 | // with the standard. This might not. I'm not sure. It might not matter. |
| 10254 | if (Constructor) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10255 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10256 | } |
| 10257 | } |
| 10258 | |
| 10259 | // Field constructors. |
| 10260 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 10261 | FEnd = ClassDecl->field_end(); |
| 10262 | F != FEnd; ++F) { |
Richard Smith | 1c6461e | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 10263 | QualType FieldType = Context.getBaseElementType(F->getType()); |
| 10264 | if (CXXRecordDecl *FieldRecDecl = FieldType->getAsCXXRecordDecl()) { |
| 10265 | CXXConstructorDecl *Constructor = |
| 10266 | LookupMovingConstructor(FieldRecDecl, FieldType.getCVRQualifiers()); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10267 | // If this is a deleted function, add it anyway. This might be conformant |
| 10268 | // with the standard. This might not. I'm not sure. It might not matter. |
| 10269 | // In particular, the problem is that this function never gets called. It |
| 10270 | // might just be ill-formed because this function attempts to refer to |
| 10271 | // a deleted function here. |
| 10272 | if (Constructor) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10273 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10274 | } |
| 10275 | } |
| 10276 | |
| 10277 | return ExceptSpec; |
| 10278 | } |
| 10279 | |
| 10280 | CXXConstructorDecl *Sema::DeclareImplicitMoveConstructor( |
| 10281 | CXXRecordDecl *ClassDecl) { |
Richard Smith | cf8ec8d | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 10282 | assert(ClassDecl->needsImplicitMoveConstructor()); |
| 10283 | |
Richard Smith | 8bf22e5 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 10284 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveConstructor); |
| 10285 | if (DSM.isAlreadyBeingDeclared()) |
| 10286 | return 0; |
| 10287 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10288 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 10289 | QualType ArgType = Context.getRValueReferenceType(ClassType); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10290 | |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 10291 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 10292 | CXXMoveConstructor, |
| 10293 | false); |
| 10294 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10295 | DeclarationName Name |
| 10296 | = Context.DeclarationNames.getCXXConstructorName( |
| 10297 | Context.getCanonicalType(ClassType)); |
| 10298 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 10299 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 10300 | |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 10301 | // C++11 [class.copy]p11: |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10302 | // An implicitly-declared copy/move constructor is an inline public |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10303 | // member of its class. |
| 10304 | CXXConstructorDecl *MoveConstructor = CXXConstructorDecl::Create( |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10305 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/0, |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10306 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | b580009 | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 10307 | Constexpr); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10308 | MoveConstructor->setAccess(AS_public); |
| 10309 | MoveConstructor->setDefaulted(); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 10310 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10311 | // Build an exception specification pointing back at this member. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 10312 | FunctionProtoType::ExtProtoInfo EPI = |
| 10313 | getImplicitMethodEPI(*this, MoveConstructor); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10314 | MoveConstructor->setType( |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 10315 | Context.getFunctionType(Context.VoidTy, ArgType, EPI)); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10316 | |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10317 | // Add the parameter to the constructor. |
| 10318 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveConstructor, |
| 10319 | ClassLoc, ClassLoc, |
| 10320 | /*IdentifierInfo=*/0, |
| 10321 | ArgType, /*TInfo=*/0, |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10322 | SC_None, 0); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 10323 | MoveConstructor->setParams(FromParam); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10324 | |
Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 10325 | MoveConstructor->setTrivial( |
| 10326 | ClassDecl->needsOverloadResolutionForMoveConstructor() |
| 10327 | ? SpecialMemberIsTrivial(MoveConstructor, CXXMoveConstructor) |
| 10328 | : ClassDecl->hasTrivialMoveConstructor()); |
| 10329 | |
Alexis Hunt | 77c1f9f | 2011-10-11 06:43:29 +0000 | [diff] [blame] | 10330 | if (ShouldDeleteSpecialMember(MoveConstructor, CXXMoveConstructor)) { |
Richard Smith | 8b86f2d | 2013-11-04 01:48:18 +0000 | [diff] [blame] | 10331 | ClassDecl->setImplicitMoveConstructorIsDeleted(); |
| 10332 | SetDeclDeleted(MoveConstructor, ClassLoc); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10333 | } |
| 10334 | |
| 10335 | // Note that we have declared this constructor. |
| 10336 | ++ASTContext::NumImplicitMoveConstructorsDeclared; |
| 10337 | |
| 10338 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 10339 | PushOnScopeChains(MoveConstructor, S, false); |
| 10340 | ClassDecl->addDecl(MoveConstructor); |
| 10341 | |
| 10342 | return MoveConstructor; |
| 10343 | } |
| 10344 | |
| 10345 | void Sema::DefineImplicitMoveConstructor(SourceLocation CurrentLocation, |
| 10346 | CXXConstructorDecl *MoveConstructor) { |
| 10347 | assert((MoveConstructor->isDefaulted() && |
| 10348 | MoveConstructor->isMoveConstructor() && |
Richard Smith | 273c4e9 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10349 | !MoveConstructor->doesThisDeclarationHaveABody() && |
| 10350 | !MoveConstructor->isDeleted()) && |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10351 | "DefineImplicitMoveConstructor - call it for implicit move ctor"); |
| 10352 | |
| 10353 | CXXRecordDecl *ClassDecl = MoveConstructor->getParent(); |
| 10354 | assert(ClassDecl && "DefineImplicitMoveConstructor - invalid constructor"); |
| 10355 | |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10356 | SynthesizedFunctionScope Scope(*this, MoveConstructor); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10357 | DiagnosticErrorTrap Trap(Diags); |
| 10358 | |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 10359 | if (SetCtorInitializers(MoveConstructor, /*AnyErrors=*/false) || |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10360 | Trap.hasErrorOccurred()) { |
| 10361 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10362 | << CXXMoveConstructor << Context.getTagDeclType(ClassDecl); |
| 10363 | MoveConstructor->setInvalidDecl(); |
| 10364 | } else { |
Dmitri Gribenko | 800ddf3 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 10365 | Sema::CompoundScopeRAII CompoundScope(*this); |
Robert Wilhelm | 27b2c9a3 | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 10366 | MoveConstructor->setBody(ActOnCompoundStmt( |
| 10367 | MoveConstructor->getLocation(), MoveConstructor->getLocation(), None, |
| 10368 | /*isStmtExpr=*/ false).takeAs<Stmt>()); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10369 | } |
| 10370 | |
Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 10371 | MoveConstructor->markUsed(Context); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10372 | |
| 10373 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10374 | L->CompletedImplicitDefinition(MoveConstructor); |
| 10375 | } |
| 10376 | } |
| 10377 | |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10378 | bool Sema::isImplicitlyDeleted(FunctionDecl *FD) { |
Eli Friedman | ebea0f2 | 2013-07-18 23:29:14 +0000 | [diff] [blame] | 10379 | return FD->isDeleted() && FD->isDefaulted() && isa<CXXMethodDecl>(FD); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10380 | } |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10381 | |
| 10382 | void Sema::DefineImplicitLambdaToFunctionPointerConversion( |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10383 | SourceLocation CurrentLocation, |
| 10384 | CXXConversionDecl *Conv) { |
| 10385 | CXXRecordDecl *Lambda = Conv->getParent(); |
| 10386 | CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator(); |
| 10387 | // If we are defining a specialization of a conversion to function-ptr |
| 10388 | // cache the deduced template arguments for this specialization |
| 10389 | // so that we can use them to retrieve the corresponding call-operator |
| 10390 | // and static-invoker. |
| 10391 | const TemplateArgumentList *DeducedTemplateArgs = 0; |
| 10392 | |
Douglas Gregor | 355efbb | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10393 | |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10394 | // Retrieve the corresponding call-operator specialization. |
| 10395 | if (Lambda->isGenericLambda()) { |
| 10396 | assert(Conv->isFunctionTemplateSpecialization()); |
| 10397 | FunctionTemplateDecl *CallOpTemplate = |
| 10398 | CallOp->getDescribedFunctionTemplate(); |
| 10399 | DeducedTemplateArgs = Conv->getTemplateSpecializationArgs(); |
| 10400 | void *InsertPos = 0; |
| 10401 | FunctionDecl *CallOpSpec = CallOpTemplate->findSpecialization( |
| 10402 | DeducedTemplateArgs->data(), |
| 10403 | DeducedTemplateArgs->size(), |
| 10404 | InsertPos); |
| 10405 | assert(CallOpSpec && |
| 10406 | "Conversion operator must have a corresponding call operator"); |
| 10407 | CallOp = cast<CXXMethodDecl>(CallOpSpec); |
| 10408 | } |
| 10409 | // Mark the call operator referenced (and add to pending instantiations |
| 10410 | // if necessary). |
| 10411 | // For both the conversion and static-invoker template specializations |
| 10412 | // we construct their body's in this function, so no need to add them |
| 10413 | // to the PendingInstantiations. |
| 10414 | MarkFunctionReferenced(CurrentLocation, CallOp); |
| 10415 | |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10416 | SynthesizedFunctionScope Scope(*this, Conv); |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10417 | DiagnosticErrorTrap Trap(Diags); |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10418 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 10419 | // Retrieve the static invoker... |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10420 | CXXMethodDecl *Invoker = Lambda->getLambdaStaticInvoker(); |
| 10421 | // ... and get the corresponding specialization for a generic lambda. |
| 10422 | if (Lambda->isGenericLambda()) { |
| 10423 | assert(DeducedTemplateArgs && |
| 10424 | "Must have deduced template arguments from Conversion Operator"); |
| 10425 | FunctionTemplateDecl *InvokeTemplate = |
| 10426 | Invoker->getDescribedFunctionTemplate(); |
| 10427 | void *InsertPos = 0; |
| 10428 | FunctionDecl *InvokeSpec = InvokeTemplate->findSpecialization( |
| 10429 | DeducedTemplateArgs->data(), |
| 10430 | DeducedTemplateArgs->size(), |
| 10431 | InsertPos); |
| 10432 | assert(InvokeSpec && |
| 10433 | "Must have a corresponding static invoker specialization"); |
| 10434 | Invoker = cast<CXXMethodDecl>(InvokeSpec); |
| 10435 | } |
| 10436 | // Construct the body of the conversion function { return __invoke; }. |
| 10437 | Expr *FunctionRef = BuildDeclRefExpr(Invoker, Invoker->getType(), |
| 10438 | VK_LValue, Conv->getLocation()).take(); |
| 10439 | assert(FunctionRef && "Can't refer to __invoke function?"); |
| 10440 | Stmt *Return = ActOnReturnStmt(Conv->getLocation(), FunctionRef).take(); |
| 10441 | Conv->setBody(new (Context) CompoundStmt(Context, Return, |
| 10442 | Conv->getLocation(), |
| 10443 | Conv->getLocation())); |
| 10444 | |
| 10445 | Conv->markUsed(Context); |
| 10446 | Conv->setReferenced(); |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10447 | |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10448 | // Fill in the __invoke function with a dummy implementation. IR generation |
| 10449 | // will fill in the actual details. |
| 10450 | Invoker->markUsed(Context); |
| 10451 | Invoker->setReferenced(); |
| 10452 | Invoker->setBody(new (Context) CompoundStmt(Conv->getLocation())); |
| 10453 | |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10454 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10455 | L->CompletedImplicitDefinition(Conv); |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10456 | L->CompletedImplicitDefinition(Invoker); |
| 10457 | } |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10458 | } |
| 10459 | |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10460 | |
| 10461 | |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10462 | void Sema::DefineImplicitLambdaToBlockPointerConversion( |
| 10463 | SourceLocation CurrentLocation, |
| 10464 | CXXConversionDecl *Conv) |
| 10465 | { |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 10466 | assert(!Conv->getParent()->isGenericLambda()); |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 10467 | |
Eli Friedman | 276dd18 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 10468 | Conv->markUsed(Context); |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10469 | |
Eli Friedman | eaf3414 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10470 | SynthesizedFunctionScope Scope(*this, Conv); |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10471 | DiagnosticErrorTrap Trap(Diags); |
| 10472 | |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10473 | // Copy-initialize the lambda object as needed to capture it. |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10474 | Expr *This = ActOnCXXThis(CurrentLocation).take(); |
| 10475 | Expr *DerefThis =CreateBuiltinUnaryOp(CurrentLocation, UO_Deref, This).take(); |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10476 | |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 10477 | ExprResult BuildBlock = BuildBlockForLambdaConversion(CurrentLocation, |
| 10478 | Conv->getLocation(), |
| 10479 | Conv, DerefThis); |
| 10480 | |
| 10481 | // If we're not under ARC, make sure we still get the _Block_copy/autorelease |
| 10482 | // behavior. Note that only the general conversion function does this |
| 10483 | // (since it's unusable otherwise); in the case where we inline the |
| 10484 | // block literal, it has block literal lifetime semantics. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10485 | if (!BuildBlock.isInvalid() && !getLangOpts().ObjCAutoRefCount) |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 10486 | BuildBlock = ImplicitCastExpr::Create(Context, BuildBlock.get()->getType(), |
| 10487 | CK_CopyAndAutoreleaseBlockObject, |
| 10488 | BuildBlock.get(), 0, VK_RValue); |
| 10489 | |
| 10490 | if (BuildBlock.isInvalid()) { |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10491 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10492 | Conv->setInvalidDecl(); |
| 10493 | return; |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10494 | } |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10495 | |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10496 | // Create the return statement that returns the block from the conversion |
| 10497 | // function. |
Eli Friedman | 98b01ed | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 10498 | StmtResult Return = ActOnReturnStmt(Conv->getLocation(), BuildBlock.get()); |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10499 | if (Return.isInvalid()) { |
| 10500 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
| 10501 | Conv->setInvalidDecl(); |
| 10502 | return; |
| 10503 | } |
| 10504 | |
| 10505 | // Set the body of the conversion function. |
| 10506 | Stmt *ReturnS = Return.take(); |
Nico Weber | a2a0eb9 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 10507 | Conv->setBody(new (Context) CompoundStmt(Context, ReturnS, |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10508 | Conv->getLocation(), |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10509 | Conv->getLocation())); |
| 10510 | |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10511 | // We're done; notify the mutation listener, if any. |
Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10512 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10513 | L->CompletedImplicitDefinition(Conv); |
| 10514 | } |
| 10515 | } |
| 10516 | |
Douglas Gregor | d2f7007 | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10517 | /// \brief Determine whether the given list arguments contains exactly one |
| 10518 | /// "real" (non-default) argument. |
| 10519 | static bool hasOneRealArgument(MultiExprArg Args) { |
| 10520 | switch (Args.size()) { |
| 10521 | case 0: |
| 10522 | return false; |
| 10523 | |
| 10524 | default: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10525 | if (!Args[1]->isDefaultArgument()) |
Douglas Gregor | d2f7007 | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10526 | return false; |
| 10527 | |
| 10528 | // fall through |
| 10529 | case 1: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10530 | return !Args[0]->isDefaultArgument(); |
Douglas Gregor | d2f7007 | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10531 | } |
| 10532 | |
| 10533 | return false; |
| 10534 | } |
| 10535 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10536 | ExprResult |
Anders Carlsson | 1b4ebfa | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 10537 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10538 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 10539 | MultiExprArg ExprArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10540 | bool HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10541 | bool IsListInitialization, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10542 | bool RequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 10543 | unsigned ConstructKind, |
| 10544 | SourceRange ParenRange) { |
Anders Carlsson | 250aada | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 10545 | bool Elidable = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10546 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 10547 | // C++0x [class.copy]p34: |
| 10548 | // When certain criteria are met, an implementation is allowed to |
| 10549 | // omit the copy/move construction of a class object, even if the |
| 10550 | // copy/move constructor and/or destructor for the object have |
| 10551 | // side effects. [...] |
| 10552 | // - when a temporary class object that has not been bound to a |
| 10553 | // reference (12.2) would be copied/moved to a class object |
| 10554 | // with the same cv-unqualified type, the copy/move operation |
| 10555 | // can be omitted by constructing the temporary object |
| 10556 | // directly into the target of the omitted copy/move |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 10557 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
Douglas Gregor | d2f7007 | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10558 | Constructor->isCopyOrMoveConstructor() && hasOneRealArgument(ExprArgs)) { |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10559 | Expr *SubExpr = ExprArgs[0]; |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 10560 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 250aada | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 10561 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10562 | |
| 10563 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10564 | Elidable, ExprArgs, HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10565 | IsListInitialization, RequiresZeroInit, |
| 10566 | ConstructKind, ParenRange); |
Anders Carlsson | 250aada | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 10567 | } |
| 10568 | |
Fariborz Jahanian | aa890bf | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 10569 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 10570 | /// including handling of its default argument expressions. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10571 | ExprResult |
Anders Carlsson | 1b4ebfa | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 10572 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 10573 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 10574 | MultiExprArg ExprArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10575 | bool HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10576 | bool IsListInitialization, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10577 | bool RequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 10578 | unsigned ConstructKind, |
| 10579 | SourceRange ParenRange) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10580 | MarkFunctionReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 10581 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10582 | Constructor, Elidable, ExprArgs, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10583 | HadMultipleCandidates, |
| 10584 | IsListInitialization, RequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 10585 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 10586 | ParenRange)); |
Fariborz Jahanian | aa890bf | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 10587 | } |
| 10588 | |
John McCall | 03c4848 | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 10589 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10590 | if (VD->isInvalidDecl()) return; |
| 10591 | |
John McCall | 03c4848 | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 10592 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10593 | if (ClassDecl->isInvalidDecl()) return; |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 10594 | if (ClassDecl->hasIrrelevantDestructor()) return; |
Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10595 | if (ClassDecl->isDependentContext()) return; |
John McCall | 47e4093 | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 10596 | |
Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10597 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10598 | MarkFunctionReferenced(VD->getLocation(), Destructor); |
Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10599 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 10600 | PDiag(diag::err_access_dtor_var) |
| 10601 | << VD->getDeclName() |
| 10602 | << VD->getType()); |
Richard Smith | eec915d6 | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 10603 | DiagnoseUseOfDecl(Destructor, VD->getLocation()); |
Anders Carlsson | 98766db | 2011-03-24 01:01:41 +0000 | [diff] [blame] | 10604 | |
Chandler Carruth | 86d17d3 | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10605 | if (!VD->hasGlobalStorage()) return; |
| 10606 | |
| 10607 | // Emit warning for non-trivial dtor in global scope (a real global, |
| 10608 | // class-static, function-static). |
| 10609 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 10610 | |
| 10611 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 10612 | if (!VD->isStaticLocal()) |
| 10613 | Diag(VD->getLocation(), diag::warn_global_destructor); |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10614 | } |
| 10615 | |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10616 | /// \brief Given a constructor and the set of arguments provided for the |
| 10617 | /// constructor, convert the arguments and add any required default arguments |
| 10618 | /// to form a proper call to this constructor. |
| 10619 | /// |
| 10620 | /// \returns true if an error occurred, false otherwise. |
| 10621 | bool |
| 10622 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 10623 | MultiExprArg ArgsPtr, |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 10624 | SourceLocation Loc, |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 10625 | SmallVectorImpl<Expr*> &ConvertedArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 10626 | bool AllowExplicit, |
| 10627 | bool IsListInitialization) { |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10628 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 10629 | unsigned NumArgs = ArgsPtr.size(); |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10630 | Expr **Args = ArgsPtr.data(); |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10631 | |
| 10632 | const FunctionProtoType *Proto |
| 10633 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 10634 | assert(Proto && "Constructor without a prototype?"); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 10635 | unsigned NumParams = Proto->getNumParams(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 10636 | |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10637 | // 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] | 10638 | if (NumArgs < NumParams) |
| 10639 | ConvertedArgs.reserve(NumParams); |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10640 | else |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10641 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10642 | |
| 10643 | VariadicCallType CallType = |
| 10644 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10645 | SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10646 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10647 | Proto, 0, |
| 10648 | llvm::makeArrayRef(Args, NumArgs), |
| 10649 | AllArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 10650 | CallType, AllowExplicit, |
| 10651 | IsListInitialization); |
Benjamin Kramer | 8001f74 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 10652 | ConvertedArgs.append(AllArgs.begin(), AllArgs.end()); |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10653 | |
Dmitri Gribenko | 9c785c2 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10654 | DiagnoseSentinelCalls(Constructor, Loc, AllArgs); |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10655 | |
Dmitri Gribenko | 765396f | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 10656 | CheckConstructorCall(Constructor, |
| 10657 | llvm::makeArrayRef<const Expr *>(AllArgs.data(), |
| 10658 | AllArgs.size()), |
Richard Smith | 55ce352 | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 10659 | Proto, Loc); |
Eli Friedman | ff4b407 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10660 | |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10661 | return Invalid; |
Douglas Gregor | c28b57d | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 10662 | } |
| 10663 | |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10664 | static inline bool |
| 10665 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 10666 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10667 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10668 | if (isa<NamespaceDecl>(DC)) { |
| 10669 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10670 | diag::err_operator_new_delete_declared_in_namespace) |
| 10671 | << FnDecl->getDeclName(); |
| 10672 | } |
| 10673 | |
| 10674 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 10675 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10676 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10677 | diag::err_operator_new_delete_declared_static) |
| 10678 | << FnDecl->getDeclName(); |
| 10679 | } |
| 10680 | |
Anders Carlsson | 60659a8 | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 10681 | return false; |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10682 | } |
| 10683 | |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10684 | static inline bool |
| 10685 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 10686 | CanQualType ExpectedResultType, |
| 10687 | CanQualType ExpectedFirstParamType, |
| 10688 | unsigned DependentParamTypeDiag, |
| 10689 | unsigned InvalidParamTypeDiag) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10690 | QualType ResultType = |
| 10691 | FnDecl->getType()->getAs<FunctionType>()->getReturnType(); |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10692 | |
| 10693 | // Check that the result type is not dependent. |
| 10694 | if (ResultType->isDependentType()) |
| 10695 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10696 | diag::err_operator_new_delete_dependent_result_type) |
| 10697 | << FnDecl->getDeclName() << ExpectedResultType; |
| 10698 | |
| 10699 | // Check that the result type is what we expect. |
| 10700 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 10701 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10702 | diag::err_operator_new_delete_invalid_result_type) |
| 10703 | << FnDecl->getDeclName() << ExpectedResultType; |
| 10704 | |
| 10705 | // A function template must have at least 2 parameters. |
| 10706 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 10707 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10708 | diag::err_operator_new_delete_template_too_few_parameters) |
| 10709 | << FnDecl->getDeclName(); |
| 10710 | |
| 10711 | // The function decl must have at least 1 parameter. |
| 10712 | if (FnDecl->getNumParams() == 0) |
| 10713 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10714 | diag::err_operator_new_delete_too_few_parameters) |
| 10715 | << FnDecl->getDeclName(); |
| 10716 | |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 10717 | // Check the first parameter type is not dependent. |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10718 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 10719 | if (FirstParamType->isDependentType()) |
| 10720 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 10721 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 10722 | |
| 10723 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 684d7bd | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 10724 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10725 | ExpectedFirstParamType) |
| 10726 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 10727 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 10728 | |
| 10729 | return false; |
| 10730 | } |
| 10731 | |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10732 | static bool |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10733 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10734 | // C++ [basic.stc.dynamic.allocation]p1: |
| 10735 | // A program is ill-formed if an allocation function is declared in a |
| 10736 | // namespace scope other than global scope or declared static in global |
| 10737 | // scope. |
| 10738 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 10739 | return true; |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10740 | |
| 10741 | CanQualType SizeTy = |
| 10742 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 10743 | |
| 10744 | // C++ [basic.stc.dynamic.allocation]p1: |
| 10745 | // The return type shall be void*. The first parameter shall have type |
| 10746 | // std::size_t. |
| 10747 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 10748 | SizeTy, |
| 10749 | diag::err_operator_new_dependent_param_type, |
| 10750 | diag::err_operator_new_param_type)) |
| 10751 | return true; |
| 10752 | |
| 10753 | // C++ [basic.stc.dynamic.allocation]p1: |
| 10754 | // The first parameter shall not have an associated default argument. |
| 10755 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 10756 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10757 | diag::err_operator_new_default_arg) |
| 10758 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 10759 | |
| 10760 | return false; |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 10761 | } |
| 10762 | |
| 10763 | static bool |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 10764 | CheckOperatorDeleteDeclaration(Sema &SemaRef, FunctionDecl *FnDecl) { |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10765 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 10766 | // A program is ill-formed if deallocation functions are declared in a |
| 10767 | // namespace scope other than global scope or declared static in global |
| 10768 | // scope. |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10769 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 10770 | return true; |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10771 | |
| 10772 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 10773 | // Each deallocation function shall return void and its first parameter |
| 10774 | // shall be void*. |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10775 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 10776 | SemaRef.Context.VoidPtrTy, |
| 10777 | diag::err_operator_delete_dependent_param_type, |
| 10778 | diag::err_operator_delete_param_type)) |
| 10779 | return true; |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10780 | |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10781 | return false; |
| 10782 | } |
| 10783 | |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10784 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 10785 | /// of this overloaded operator is well-formed. If so, returns false; |
| 10786 | /// otherwise, emits appropriate diagnostics and returns true. |
| 10787 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10788 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10789 | "Expected an overloaded operator declaration"); |
| 10790 | |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10791 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 10792 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10793 | // C++ [over.oper]p5: |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10794 | // The allocation and deallocation functions, operator new, |
| 10795 | // operator new[], operator delete and operator delete[], are |
| 10796 | // described completely in 3.7.3. The attributes and restrictions |
| 10797 | // found in the rest of this subclause do not apply to them unless |
| 10798 | // explicitly stated in 3.7.3. |
Anders Carlsson | f1f4695 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 10799 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10800 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | 4e08894 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 10801 | |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 10802 | if (Op == OO_New || Op == OO_Array_New) |
| 10803 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10804 | |
| 10805 | // C++ [over.oper]p6: |
| 10806 | // An operator function shall either be a non-static member |
| 10807 | // function or be a non-member function and have at least one |
| 10808 | // parameter whose type is a class, a reference to a class, an |
| 10809 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10810 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 10811 | if (MethodDecl->isStatic()) |
| 10812 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10813 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10814 | } else { |
| 10815 | bool ClassOrEnumParam = false; |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10816 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 10817 | ParamEnd = FnDecl->param_end(); |
| 10818 | Param != ParamEnd; ++Param) { |
| 10819 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 173e0b7a | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 10820 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 10821 | ParamType->isEnumeralType()) { |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10822 | ClassOrEnumParam = true; |
| 10823 | break; |
| 10824 | } |
| 10825 | } |
| 10826 | |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10827 | if (!ClassOrEnumParam) |
| 10828 | return Diag(FnDecl->getLocation(), |
Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 10829 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10830 | << FnDecl->getDeclName(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10831 | } |
| 10832 | |
| 10833 | // C++ [over.oper]p8: |
| 10834 | // An operator function cannot have default arguments (8.3.6), |
| 10835 | // except where explicitly stated below. |
| 10836 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10837 | // Only the function-call operator allows default arguments |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10838 | // (C++ [over.call]p1). |
| 10839 | if (Op != OO_Call) { |
| 10840 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 10841 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10842 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10843 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 10844 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10845 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10846 | } |
| 10847 | } |
| 10848 | |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10849 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 10850 | { false, false, false } |
| 10851 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 10852 | , { Unary, Binary, MemberOnly } |
| 10853 | #include "clang/Basic/OperatorKinds.def" |
| 10854 | }; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10855 | |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10856 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 10857 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 10858 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10859 | |
| 10860 | // C++ [over.oper]p8: |
| 10861 | // [...] Operator functions cannot have more or fewer parameters |
| 10862 | // than the number required for the corresponding operator, as |
| 10863 | // described in the rest of this subclause. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10864 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10865 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10866 | if (Op != OO_Call && |
| 10867 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 10868 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 10869 | (NumParams < 1) || (NumParams > 2))) { |
| 10870 | // We have the wrong number of parameters. |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10871 | unsigned ErrorKind; |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10872 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10873 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10874 | } else if (CanBeUnaryOperator) { |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10875 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10876 | } else { |
Chris Lattner | 2b78690 | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 10877 | assert(CanBeBinaryOperator && |
| 10878 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10879 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10880 | } |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10881 | |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10882 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10883 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10884 | } |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 10885 | |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10886 | // Overloaded operators other than operator() cannot be variadic. |
| 10887 | if (Op != OO_Call && |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 10888 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 10889 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10890 | << FnDecl->getDeclName(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10891 | } |
| 10892 | |
| 10893 | // Some operators must be non-static member functions. |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10894 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 10895 | return Diag(FnDecl->getLocation(), |
Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 10896 | diag::err_operator_overload_must_be_member) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10897 | << FnDecl->getDeclName(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10898 | } |
| 10899 | |
| 10900 | // C++ [over.inc]p1: |
| 10901 | // The user-defined function called operator++ implements the |
| 10902 | // prefix and postfix ++ operator. If this function is a member |
| 10903 | // function with no parameters, or a non-member function with one |
| 10904 | // parameter of class or enumeration type, it defines the prefix |
| 10905 | // increment operator ++ for objects of that type. If the function |
| 10906 | // is a member function with one parameter (which shall be of type |
| 10907 | // int) or a non-member function with two parameters (the second |
| 10908 | // of which shall be of type int), it defines the postfix |
| 10909 | // increment operator ++ for objects of that type. |
| 10910 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 10911 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
Richard Smith | 538b52a | 2014-01-30 22:24:05 +0000 | [diff] [blame] | 10912 | QualType ParamType = LastParam->getType(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10913 | |
Richard Smith | 538b52a | 2014-01-30 22:24:05 +0000 | [diff] [blame] | 10914 | if (!ParamType->isSpecificBuiltinType(BuiltinType::Int) && |
| 10915 | !ParamType->isDependentType()) |
Chris Lattner | 2b78690 | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 10916 | return Diag(LastParam->getLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10917 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 10918 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10919 | } |
| 10920 | |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10921 | return false; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10922 | } |
Chris Lattner | 3b024a3 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 10923 | |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10924 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 10925 | /// of this literal operator function is well-formed. If so, returns |
| 10926 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 10927 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
Richard Smith | 5731c75 | 2012-03-10 22:18:57 +0000 | [diff] [blame] | 10928 | if (isa<CXXMethodDecl>(FnDecl)) { |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10929 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 10930 | << FnDecl->getDeclName(); |
| 10931 | return true; |
| 10932 | } |
| 10933 | |
Richard Smith | 72eebee | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10934 | if (FnDecl->isExternC()) { |
| 10935 | Diag(FnDecl->getLocation(), diag::err_literal_operator_extern_c); |
| 10936 | return true; |
| 10937 | } |
| 10938 | |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10939 | bool Valid = false; |
| 10940 | |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 10941 | // This might be the definition of a literal operator template. |
| 10942 | FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate(); |
| 10943 | // This might be a specialization of a literal operator template. |
| 10944 | if (!TpDecl) |
| 10945 | TpDecl = FnDecl->getPrimaryTemplate(); |
| 10946 | |
Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 10947 | // template <char...> type operator "" name() and |
| 10948 | // template <class T, T...> type operator "" name() are the only valid |
| 10949 | // template signatures, and the only valid signatures with no parameters. |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 10950 | if (TpDecl) { |
Richard Smith | 72eebee | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10951 | if (FnDecl->param_size() == 0) { |
Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 10952 | // Must have one or two template parameters |
Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10953 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 10954 | if (Params->size() == 1) { |
| 10955 | NonTypeTemplateParmDecl *PmDecl = |
Richard Smith | ed94302 | 2012-08-03 21:14:57 +0000 | [diff] [blame] | 10956 | dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10957 | |
Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10958 | // The template parameter must be a char parameter pack. |
Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10959 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 10960 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 10961 | Valid = true; |
Richard Smith | b8b41d3 | 2013-10-07 19:57:58 +0000 | [diff] [blame] | 10962 | } else if (Params->size() == 2) { |
| 10963 | TemplateTypeParmDecl *PmType = |
| 10964 | dyn_cast<TemplateTypeParmDecl>(Params->getParam(0)); |
| 10965 | NonTypeTemplateParmDecl *PmArgs = |
| 10966 | dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(1)); |
| 10967 | |
| 10968 | // The second template parameter must be a parameter pack with the |
| 10969 | // first template parameter as its type. |
| 10970 | if (PmType && PmArgs && |
| 10971 | !PmType->isTemplateParameterPack() && |
| 10972 | PmArgs->isTemplateParameterPack()) { |
| 10973 | const TemplateTypeParmType *TArgs = |
| 10974 | PmArgs->getType()->getAs<TemplateTypeParmType>(); |
| 10975 | if (TArgs && TArgs->getDepth() == PmType->getDepth() && |
| 10976 | TArgs->getIndex() == PmType->getIndex()) { |
| 10977 | Valid = true; |
| 10978 | if (ActiveTemplateInstantiations.empty()) |
| 10979 | Diag(FnDecl->getLocation(), |
| 10980 | diag::ext_string_literal_operator_template); |
| 10981 | } |
| 10982 | } |
Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10983 | } |
| 10984 | } |
Richard Smith | 72eebee | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10985 | } else if (FnDecl->param_size()) { |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10986 | // Check the first parameter |
Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10987 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 10988 | |
Richard Smith | 72eebee | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10989 | QualType T = (*Param)->getType().getUnqualifiedType(); |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10990 | |
Alexis Hunt | 079a6f7 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 10991 | // unsigned long long int, long double, and any character type are allowed |
| 10992 | // as the only parameters. |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10993 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 10994 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 10995 | Context.hasSameType(T, Context.CharTy) || |
Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 10996 | Context.hasSameType(T, Context.WideCharTy) || |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10997 | Context.hasSameType(T, Context.Char16Ty) || |
| 10998 | Context.hasSameType(T, Context.Char32Ty)) { |
| 10999 | if (++Param == FnDecl->param_end()) |
| 11000 | Valid = true; |
| 11001 | goto FinishedParams; |
| 11002 | } |
| 11003 | |
Alexis Hunt | 079a6f7 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 11004 | // 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] | 11005 | const PointerType *PT = T->getAs<PointerType>(); |
| 11006 | if (!PT) |
| 11007 | goto FinishedParams; |
| 11008 | T = PT->getPointeeType(); |
Richard Smith | 72eebee | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 11009 | if (!T.isConstQualified() || T.isVolatileQualified()) |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11010 | goto FinishedParams; |
| 11011 | T = T.getUnqualifiedType(); |
| 11012 | |
| 11013 | // Move on to the second parameter; |
| 11014 | ++Param; |
| 11015 | |
| 11016 | // If there is no second parameter, the first must be a const char * |
| 11017 | if (Param == FnDecl->param_end()) { |
| 11018 | if (Context.hasSameType(T, Context.CharTy)) |
| 11019 | Valid = true; |
| 11020 | goto FinishedParams; |
| 11021 | } |
| 11022 | |
| 11023 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 11024 | // are allowed as the first parameter to a two-parameter function |
| 11025 | if (!(Context.hasSameType(T, Context.CharTy) || |
Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 11026 | Context.hasSameType(T, Context.WideCharTy) || |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11027 | Context.hasSameType(T, Context.Char16Ty) || |
| 11028 | Context.hasSameType(T, Context.Char32Ty))) |
| 11029 | goto FinishedParams; |
| 11030 | |
| 11031 | // The second and final parameter must be an std::size_t |
| 11032 | T = (*Param)->getType().getUnqualifiedType(); |
| 11033 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 11034 | ++Param == FnDecl->param_end()) |
| 11035 | Valid = true; |
| 11036 | } |
| 11037 | |
| 11038 | // FIXME: This diagnostic is absolutely terrible. |
| 11039 | FinishedParams: |
| 11040 | if (!Valid) { |
| 11041 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 11042 | << FnDecl->getDeclName(); |
| 11043 | return true; |
| 11044 | } |
| 11045 | |
Richard Smith | 768cecc | 2012-03-09 08:16:22 +0000 | [diff] [blame] | 11046 | // A parameter-declaration-clause containing a default argument is not |
| 11047 | // equivalent to any of the permitted forms. |
| 11048 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 11049 | ParamEnd = FnDecl->param_end(); |
| 11050 | Param != ParamEnd; ++Param) { |
| 11051 | if ((*Param)->hasDefaultArg()) { |
| 11052 | Diag((*Param)->getDefaultArgRange().getBegin(), |
| 11053 | diag::err_literal_operator_default_argument) |
| 11054 | << (*Param)->getDefaultArgRange(); |
| 11055 | break; |
| 11056 | } |
| 11057 | } |
| 11058 | |
Richard Smith | 0df56f4 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 11059 | StringRef LiteralName |
Douglas Gregor | 86325ad | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 11060 | = FnDecl->getDeclName().getCXXLiteralIdentifier()->getName(); |
| 11061 | if (LiteralName[0] != '_') { |
Richard Smith | 0df56f4 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 11062 | // C++11 [usrlit.suffix]p1: |
| 11063 | // Literal suffix identifiers that do not start with an underscore |
| 11064 | // are reserved for future standardization. |
Richard Smith | f4198b7 | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 11065 | Diag(FnDecl->getLocation(), diag::warn_user_literal_reserved) |
| 11066 | << NumericLiteralParser::isValidUDSuffix(getLangOpts(), LiteralName); |
Douglas Gregor | 86325ad | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 11067 | } |
Richard Smith | 0df56f4 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 11068 | |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 11069 | return false; |
| 11070 | } |
| 11071 | |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11072 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 11073 | /// linkage specification, including the language and (if present) |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11074 | /// the '{'. ExternLoc is the location of the 'extern', Lang is the |
| 11075 | /// language string literal. LBraceLoc, if valid, provides the location of |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11076 | /// the '{' brace. Otherwise, this linkage specification does not |
| 11077 | /// have any braces. |
Chris Lattner | 8ea6442 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 11078 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11079 | Expr *LangStr, |
Chris Lattner | 8ea6442 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 11080 | SourceLocation LBraceLoc) { |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11081 | StringLiteral *Lit = cast<StringLiteral>(LangStr); |
| 11082 | if (!Lit->isAscii()) { |
| 11083 | Diag(LangStr->getExprLoc(), diag::err_language_linkage_spec_not_ascii) |
| 11084 | << LangStr->getSourceRange(); |
| 11085 | return 0; |
| 11086 | } |
| 11087 | |
| 11088 | StringRef Lang = Lit->getString(); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11089 | LinkageSpecDecl::LanguageIDs Language; |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11090 | if (Lang == "C") |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11091 | Language = LinkageSpecDecl::lang_c; |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11092 | else if (Lang == "C++") |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11093 | Language = LinkageSpecDecl::lang_cxx; |
| 11094 | else { |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11095 | Diag(LangStr->getExprLoc(), diag::err_language_linkage_spec_unknown) |
| 11096 | << LangStr->getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11097 | return 0; |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11098 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11099 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11100 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11101 | |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11102 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, ExternLoc, |
| 11103 | LangStr->getExprLoc(), Language, |
Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 11104 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11105 | CurContext->addDecl(D); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11106 | PushDeclContext(S, D); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11107 | return D; |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 11108 | } |
| 11109 | |
Abramo Bagnara | ed5b689 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 11110 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11111 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 11112 | /// valid, it's the position of the closing '}' brace in a linkage |
| 11113 | /// specification that uses braces. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11114 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
Abramo Bagnara | 4a8cda8 | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 11115 | Decl *LinkageSpec, |
| 11116 | SourceLocation RBraceLoc) { |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11117 | if (RBraceLoc.isValid()) { |
| 11118 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 11119 | LSDecl->setRBraceLoc(RBraceLoc); |
Abramo Bagnara | 4a8cda8 | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 11120 | } |
Richard Smith | 4ee696d | 2014-02-17 23:25:27 +0000 | [diff] [blame] | 11121 | PopDeclContext(); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 11122 | return LinkageSpec; |
Chris Lattner | 3b024a3 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 11123 | } |
| 11124 | |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 11125 | Decl *Sema::ActOnEmptyDeclaration(Scope *S, |
| 11126 | AttributeList *AttrList, |
| 11127 | SourceLocation SemiLoc) { |
| 11128 | Decl *ED = EmptyDecl::Create(Context, CurContext, SemiLoc); |
| 11129 | // Attribute declarations appertain to empty declaration so we handle |
| 11130 | // them here. |
| 11131 | if (AttrList) |
| 11132 | ProcessDeclAttributeList(S, ED, AttrList); |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 11133 | |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 11134 | CurContext->addDecl(ED); |
| 11135 | return ED; |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 11136 | } |
| 11137 | |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11138 | /// \brief Perform semantic analysis for the variable declaration that |
| 11139 | /// occurs within a C++ catch clause, returning the newly-created |
| 11140 | /// variable. |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11141 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 11142 | TypeSourceInfo *TInfo, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11143 | SourceLocation StartLoc, |
| 11144 | SourceLocation Loc, |
| 11145 | IdentifierInfo *Name) { |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11146 | bool Invalid = false; |
Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 11147 | QualType ExDeclType = TInfo->getType(); |
| 11148 | |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11149 | // Arrays and functions decay. |
| 11150 | if (ExDeclType->isArrayType()) |
| 11151 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 11152 | else if (ExDeclType->isFunctionType()) |
| 11153 | ExDeclType = Context.getPointerType(ExDeclType); |
| 11154 | |
| 11155 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 11156 | // The exception-declaration shall not denote a pointer or reference to an |
| 11157 | // incomplete type, other than [cv] void*. |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 11158 | // N2844 forbids rvalue references. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11159 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 11160 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 11161 | Invalid = true; |
| 11162 | } |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11163 | |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11164 | QualType BaseType = ExDeclType; |
| 11165 | 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] | 11166 | unsigned DK = diag::err_catch_incomplete; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 11167 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11168 | BaseType = Ptr->getPointeeType(); |
| 11169 | Mode = 1; |
Douglas Gregor | 3ecbc3d | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 11170 | DK = diag::err_catch_incomplete_ptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11171 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 11172 | // 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] | 11173 | BaseType = Ref->getPointeeType(); |
| 11174 | Mode = 2; |
Douglas Gregor | 3ecbc3d | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 11175 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11176 | } |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 11177 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | 3ecbc3d | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 11178 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11179 | Invalid = true; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11180 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11181 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11182 | RequireNonAbstractType(Loc, ExDeclType, |
| 11183 | diag::err_abstract_type_in_decl, |
| 11184 | AbstractVariableType)) |
Sebastian Redl | 2f38ba5 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 11185 | Invalid = true; |
| 11186 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 11187 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 11188 | // of ObjC types, and no runtime supports catching ObjC types by value. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11189 | if (!Invalid && getLangOpts().ObjC1) { |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 11190 | QualType T = ExDeclType; |
| 11191 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 11192 | T = RT->getPointeeType(); |
| 11193 | |
| 11194 | if (T->isObjCObjectType()) { |
| 11195 | Diag(Loc, diag::err_objc_object_catch); |
| 11196 | Invalid = true; |
| 11197 | } else if (T->isObjCObjectPointerType()) { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 11198 | // FIXME: should this be a test for macosx-fragile specifically? |
| 11199 | if (getLangOpts().ObjCRuntime.isFragile()) |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 11200 | Diag(Loc, diag::warn_objc_pointer_cxx_catch_fragile); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 11201 | } |
| 11202 | } |
| 11203 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11204 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 11205 | ExDeclType, TInfo, SC_None); |
Douglas Gregor | 3f324d56 | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 11206 | ExDecl->setExceptionVariable(true); |
| 11207 | |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 11208 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11209 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(ExDecl)) |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 11210 | Invalid = true; |
| 11211 | |
Douglas Gregor | 750734c | 2011-07-06 18:14:43 +0000 | [diff] [blame] | 11212 | if (!Invalid && !ExDeclType->isDependentType()) { |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 11213 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
John McCall | eaef89b | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 11214 | // Insulate this from anything else we might currently be parsing. |
| 11215 | EnterExpressionEvaluationContext scope(*this, PotentiallyEvaluated); |
| 11216 | |
Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11217 | // C++ [except.handle]p16: |
Nick Lewycky | 0f29289 | 2013-09-22 10:06:57 +0000 | [diff] [blame] | 11218 | // The object declared in an exception-declaration or, if the |
| 11219 | // exception-declaration does not specify a name, a temporary (12.2) is |
Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11220 | // copy-initialized (8.5) from the exception object. [...] |
| 11221 | // The object is destroyed when the handler exits, after the destruction |
| 11222 | // of any automatic objects initialized within the handler. |
| 11223 | // |
Nick Lewycky | 0f29289 | 2013-09-22 10:06:57 +0000 | [diff] [blame] | 11224 | // We just pretend to initialize the object with itself, then make sure |
Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11225 | // it can be destroyed later. |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 11226 | QualType initType = ExDeclType; |
| 11227 | |
| 11228 | InitializedEntity entity = |
| 11229 | InitializedEntity::InitializeVariable(ExDecl); |
| 11230 | InitializationKind initKind = |
| 11231 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 11232 | |
| 11233 | Expr *opaqueValue = |
| 11234 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 11235 | InitializationSequence sequence(*this, entity, initKind, opaqueValue); |
| 11236 | ExprResult result = sequence.Perform(*this, entity, initKind, opaqueValue); |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 11237 | if (result.isInvalid()) |
Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11238 | Invalid = true; |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 11239 | else { |
| 11240 | // If the constructor used was non-trivial, set this as the |
| 11241 | // "initializer". |
Nick Lewycky | 0f29289 | 2013-09-22 10:06:57 +0000 | [diff] [blame] | 11242 | CXXConstructExpr *construct = result.takeAs<CXXConstructExpr>(); |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 11243 | if (!construct->getConstructor()->isTrivial()) { |
| 11244 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 11245 | ExDecl->setInit(init); |
| 11246 | } |
| 11247 | |
| 11248 | // And make sure it's destructable. |
| 11249 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 11250 | } |
Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 11251 | } |
| 11252 | } |
| 11253 | |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11254 | if (Invalid) |
| 11255 | ExDecl->setInvalidDecl(); |
| 11256 | |
| 11257 | return ExDecl; |
| 11258 | } |
| 11259 | |
| 11260 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 11261 | /// handler. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11262 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11263 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Douglas Gregor | 72772f6 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 11264 | bool Invalid = D.isInvalidType(); |
| 11265 | |
| 11266 | // Check for unexpanded parameter packs. |
Jordan Rose | d03d99d | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 11267 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 11268 | UPPC_ExceptionType)) { |
Douglas Gregor | 72772f6 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 11269 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 11270 | D.getIdentifierLoc()); |
| 11271 | Invalid = true; |
| 11272 | } |
| 11273 | |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11274 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 11275 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 11276 | LookupOrdinaryName, |
| 11277 | ForRedeclaration)) { |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11278 | // The scope should be freshly made just for us. There is just no way |
| 11279 | // it contains any previous declaration. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11280 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11281 | if (PrevDecl->isTemplateParameter()) { |
| 11282 | // Maybe we will complain about the shadowed template parameter. |
| 11283 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Douglas Gregor | f4ef4d2 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 11284 | PrevDecl = 0; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11285 | } |
| 11286 | } |
| 11287 | |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11288 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11289 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 11290 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11291 | Invalid = true; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11292 | } |
| 11293 | |
Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 11294 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11295 | D.getLocStart(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 11296 | D.getIdentifierLoc(), |
| 11297 | D.getIdentifier()); |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 11298 | if (Invalid) |
| 11299 | ExDecl->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11300 | |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11301 | // Add the exception declaration into this scope. |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11302 | if (II) |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 11303 | PushOnScopeChains(ExDecl, S); |
| 11304 | else |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11305 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11306 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 11307 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11308 | return ExDecl; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 11309 | } |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 11310 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 11311 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 11312 | Expr *AssertExpr, |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11313 | Expr *AssertMessageExpr, |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 11314 | SourceLocation RParenLoc) { |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11315 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr); |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 11316 | |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11317 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 11318 | return 0; |
| 11319 | |
| 11320 | return BuildStaticAssertDeclaration(StaticAssertLoc, AssertExpr, |
| 11321 | AssertMessage, RParenLoc, false); |
| 11322 | } |
| 11323 | |
| 11324 | Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
| 11325 | Expr *AssertExpr, |
| 11326 | StringLiteral *AssertMessage, |
| 11327 | SourceLocation RParenLoc, |
| 11328 | bool Failed) { |
| 11329 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent() && |
| 11330 | !Failed) { |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11331 | // In a static_assert-declaration, the constant-expression shall be a |
| 11332 | // constant expression that can be contextually converted to bool. |
| 11333 | ExprResult Converted = PerformContextuallyConvertToBool(AssertExpr); |
| 11334 | if (Converted.isInvalid()) |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11335 | Failed = true; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11336 | |
Richard Smith | 902ca21 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 11337 | llvm::APSInt Cond; |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11338 | if (!Failed && VerifyIntegerConstantExpression(Converted.get(), &Cond, |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 11339 | diag::err_static_assert_expression_is_not_constant, |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 11340 | /*AllowFold=*/false).isInvalid()) |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11341 | Failed = true; |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 11342 | |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11343 | if (!Failed && !Cond) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 11344 | SmallString<256> MsgBuffer; |
Richard Smith | f506eaf | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 11345 | llvm::raw_svector_ostream Msg(MsgBuffer); |
Richard Smith | 235341b | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 11346 | AssertMessage->printPretty(Msg, 0, getPrintingPolicy()); |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 11347 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
Richard Smith | f506eaf | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 11348 | << Msg.str() << AssertExpr->getSourceRange(); |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11349 | Failed = true; |
Richard Smith | f506eaf | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 11350 | } |
Anders Carlsson | 54b2698 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 11351 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11352 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 11353 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 11354 | AssertExpr, AssertMessage, RParenLoc, |
| 11355 | Failed); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11356 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 11357 | CurContext->addDecl(Decl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11358 | return Decl; |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 11359 | } |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11360 | |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11361 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 11362 | /// |
| 11363 | /// \returns A friend declaration that. |
Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11364 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation LocStart, |
Abramo Bagnara | 254b630 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 11365 | SourceLocation FriendLoc, |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11366 | TypeSourceInfo *TSInfo) { |
| 11367 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 11368 | |
| 11369 | QualType T = TSInfo->getType(); |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 11370 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11371 | |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 11372 | // C++03 [class.friend]p2: |
| 11373 | // An elaborated-type-specifier shall be used in a friend declaration |
| 11374 | // for a class.* |
| 11375 | // |
| 11376 | // * The class-key of the elaborated-type-specifier is required. |
| 11377 | if (!ActiveTemplateInstantiations.empty()) { |
| 11378 | // Do not complain about the form of friend template types during |
| 11379 | // template instantiation; we will already have complained when the |
| 11380 | // template was declared. |
Nick Lewycky | 36722d2 | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 11381 | } else { |
| 11382 | if (!T->isElaboratedTypeSpecifier()) { |
| 11383 | // If we evaluated the type to a record type, suggest putting |
| 11384 | // a tag in front. |
| 11385 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 11386 | RecordDecl *RD = RT->getDecl(); |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 11387 | |
Nick Lewycky | 36722d2 | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 11388 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 11389 | |
Nick Lewycky | 36722d2 | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 11390 | Diag(TypeRange.getBegin(), |
| 11391 | getLangOpts().CPlusPlus11 ? |
| 11392 | diag::warn_cxx98_compat_unelaborated_friend_type : |
| 11393 | diag::ext_unelaborated_friend_type) |
| 11394 | << (unsigned) RD->getTagKind() |
| 11395 | << T |
| 11396 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 11397 | InsertionText); |
| 11398 | } else { |
| 11399 | Diag(FriendLoc, |
| 11400 | getLangOpts().CPlusPlus11 ? |
| 11401 | diag::warn_cxx98_compat_nonclass_type_friend : |
| 11402 | diag::ext_nonclass_type_friend) |
| 11403 | << T |
| 11404 | << TypeRange; |
| 11405 | } |
| 11406 | } else if (T->getAs<EnumType>()) { |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 11407 | Diag(FriendLoc, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11408 | getLangOpts().CPlusPlus11 ? |
Nick Lewycky | 36722d2 | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 11409 | diag::warn_cxx98_compat_enum_friend : |
| 11410 | diag::ext_enum_friend) |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11411 | << T |
Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11412 | << TypeRange; |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11413 | } |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11414 | |
Nick Lewycky | 36722d2 | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 11415 | // C++11 [class.friend]p3: |
| 11416 | // A friend declaration that does not declare a function shall have one |
| 11417 | // of the following forms: |
| 11418 | // friend elaborated-type-specifier ; |
| 11419 | // friend simple-type-specifier ; |
| 11420 | // friend typename-specifier ; |
| 11421 | if (getLangOpts().CPlusPlus11 && LocStart != FriendLoc) |
| 11422 | Diag(FriendLoc, diag::err_friend_not_first_in_declaration) << T; |
| 11423 | } |
Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11424 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 11425 | // If the type specifier in a friend declaration designates a (possibly |
Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11426 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 11427 | // the friend declaration is ignored. |
Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11428 | return FriendDecl::Create(Context, CurContext, LocStart, TSInfo, FriendLoc); |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11429 | } |
| 11430 | |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11431 | /// Handle a friend tag declaration where the scope specifier was |
| 11432 | /// templated. |
| 11433 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 11434 | unsigned TagSpec, SourceLocation TagLoc, |
| 11435 | CXXScopeSpec &SS, |
Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 11436 | IdentifierInfo *Name, |
| 11437 | SourceLocation NameLoc, |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11438 | AttributeList *Attr, |
| 11439 | MultiTemplateParamsArg TempParamLists) { |
| 11440 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 11441 | |
| 11442 | bool isExplicitSpecialization = false; |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11443 | bool Invalid = false; |
| 11444 | |
Robert Wilhelm | f2d2e8f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 11445 | if (TemplateParameterList *TemplateParams = |
| 11446 | MatchTemplateParametersToScopeSpecifier( |
| 11447 | TagLoc, NameLoc, SS, TempParamLists, /*friend*/ true, |
| 11448 | isExplicitSpecialization, Invalid)) { |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11449 | if (TemplateParams->size() > 0) { |
| 11450 | // This is a declaration of a class template. |
| 11451 | if (Invalid) |
| 11452 | return 0; |
Abramo Bagnara | 0adf29a | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 11453 | |
Eric Christopher | 6f228b5 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 11454 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 11455 | SS, Name, NameLoc, Attr, |
| 11456 | TemplateParams, AS_public, |
Douglas Gregor | 2820e69 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 11457 | /*ModulePrivateLoc=*/SourceLocation(), |
Eric Christopher | 6f228b5 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 11458 | TempParamLists.size() - 1, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11459 | TempParamLists.data()).take(); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11460 | } else { |
| 11461 | // The "template<>" header is extraneous. |
| 11462 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 11463 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 11464 | isExplicitSpecialization = true; |
| 11465 | } |
| 11466 | } |
| 11467 | |
| 11468 | if (Invalid) return 0; |
| 11469 | |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11470 | bool isAllExplicitSpecializations = true; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 11471 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11472 | if (TempParamLists[I]->size()) { |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11473 | isAllExplicitSpecializations = false; |
| 11474 | break; |
| 11475 | } |
| 11476 | } |
| 11477 | |
| 11478 | // FIXME: don't ignore attributes. |
| 11479 | |
| 11480 | // If it's explicit specializations all the way down, just forget |
| 11481 | // about the template header and build an appropriate non-templated |
| 11482 | // friend. TODO: for source fidelity, remember the headers. |
| 11483 | if (isAllExplicitSpecializations) { |
Douglas Gregor | f65d8ff | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11484 | if (SS.isEmpty()) { |
| 11485 | bool Owned = false; |
| 11486 | bool IsDependent = false; |
| 11487 | return ActOnTag(S, TagSpec, TUK_Friend, TagLoc, SS, Name, NameLoc, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11488 | Attr, AS_public, |
Douglas Gregor | f65d8ff | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11489 | /*ModulePrivateLoc=*/SourceLocation(), |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11490 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 11491 | /*ScopedEnumKWLoc=*/SourceLocation(), |
Douglas Gregor | f65d8ff | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11492 | /*ScopedEnumUsesClassTag=*/false, |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11493 | /*UnderlyingType=*/TypeResult(), |
| 11494 | /*IsTypeSpecifier=*/false); |
Douglas Gregor | f65d8ff | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11495 | } |
Richard Smith | 649c7b06 | 2014-01-08 00:56:48 +0000 | [diff] [blame] | 11496 | |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11497 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11498 | ElaboratedTypeKeyword Keyword |
| 11499 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11500 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
Douglas Gregor | 9cbc22b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 11501 | *Name, NameLoc); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11502 | if (T.isNull()) |
| 11503 | return 0; |
| 11504 | |
| 11505 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 11506 | if (isa<DependentNameType>(T)) { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11507 | DependentNameTypeLoc TL = |
| 11508 | TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 11509 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11510 | TL.setQualifierLoc(QualifierLoc); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11511 | TL.setNameLoc(NameLoc); |
| 11512 | } else { |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11513 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 11514 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 11515 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11516 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(NameLoc); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11517 | } |
| 11518 | |
| 11519 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 11520 | TSI, FriendLoc, TempParamLists); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11521 | Friend->setAccess(AS_public); |
| 11522 | CurContext->addDecl(Friend); |
| 11523 | return Friend; |
| 11524 | } |
Douglas Gregor | f65d8ff | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11525 | |
| 11526 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 11527 | |
| 11528 | |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11529 | |
| 11530 | // Handle the case of a templated-scope friend class. e.g. |
| 11531 | // template <class T> class A<T>::B; |
| 11532 | // FIXME: we don't support these right now. |
Richard Smith | cd556eb | 2013-11-08 18:59:56 +0000 | [diff] [blame] | 11533 | Diag(NameLoc, diag::warn_template_qualified_friend_unsupported) |
| 11534 | << SS.getScopeRep() << SS.getRange() << cast<CXXRecordDecl>(CurContext); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11535 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 11536 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 11537 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11538 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 9033e2b | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 11539 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11540 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11541 | TL.setNameLoc(NameLoc); |
| 11542 | |
| 11543 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
Enea Zaffanella | eb22c87 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 11544 | TSI, FriendLoc, TempParamLists); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11545 | Friend->setAccess(AS_public); |
| 11546 | Friend->setUnsupportedFriend(true); |
| 11547 | CurContext->addDecl(Friend); |
| 11548 | return Friend; |
| 11549 | } |
| 11550 | |
| 11551 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11552 | /// Handle a friend type declaration. This works in tandem with |
| 11553 | /// ActOnTag. |
| 11554 | /// |
| 11555 | /// Notes on friend class templates: |
| 11556 | /// |
| 11557 | /// We generally treat friend class declarations as if they were |
| 11558 | /// declaring a class. So, for example, the elaborated type specifier |
| 11559 | /// in a friend declaration is required to obey the restrictions of a |
| 11560 | /// class-head (i.e. no typedefs in the scope chain), template |
| 11561 | /// parameters are required to match up with simple template-ids, &c. |
| 11562 | /// However, unlike when declaring a template specialization, it's |
| 11563 | /// okay to refer to a template specialization without an empty |
| 11564 | /// template parameter declaration, e.g. |
| 11565 | /// friend class A<T>::B<unsigned>; |
| 11566 | /// We permit this as a special case; if there are any template |
| 11567 | /// parameters present at all, require proper matching, i.e. |
James Dennett | f14a6e5 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 11568 | /// template <> template \<class T> friend class A<int>::B; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11569 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | c9739e3 | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 11570 | MultiTemplateParamsArg TempParams) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11571 | SourceLocation Loc = DS.getLocStart(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11572 | |
| 11573 | assert(DS.isFriendSpecified()); |
| 11574 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 11575 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11576 | // Try to convert the decl specifier to a type. This works for |
| 11577 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 11578 | // for a TUK_Friend. |
Chris Lattner | 1fb66f4 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 11579 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11580 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 11581 | QualType T = TSI->getType(); |
Chris Lattner | 1fb66f4 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 11582 | if (TheDeclarator.isInvalidType()) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11583 | return 0; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11584 | |
Douglas Gregor | 6c110f3 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 11585 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 11586 | return 0; |
| 11587 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11588 | // This is definitely an error in C++98. It's probably meant to |
| 11589 | // be forbidden in C++0x, too, but the specification is just |
| 11590 | // poorly written. |
| 11591 | // |
| 11592 | // The problem is with declarations like the following: |
| 11593 | // template <T> friend A<T>::foo; |
| 11594 | // where deciding whether a class C is a friend or not now hinges |
| 11595 | // on whether there exists an instantiation of A that causes |
| 11596 | // 'foo' to equal C. There are restrictions on class-heads |
| 11597 | // (which we declare (by fiat) elaborated friend declarations to |
| 11598 | // be) that makes this tractable. |
| 11599 | // |
| 11600 | // FIXME: handle "template <> friend class A<T>;", which |
| 11601 | // is possibly well-formed? Who even knows? |
Douglas Gregor | e677daf | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 11602 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11603 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 11604 | << DS.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11605 | return 0; |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11606 | } |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11607 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11608 | // C++98 [class.friend]p1: A friend of a class is a function |
| 11609 | // or class that is not a member of the class . . . |
John McCall | 463e10c | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 11610 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 11611 | // deadline. It's also a very silly restriction that seriously |
| 11612 | // affects inner classes and which nobody else seems to implement; |
| 11613 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 11614 | // |
| 11615 | // But note that we could warn about it: it's always useless to |
| 11616 | // friend one of your own members (it's not, however, worthless to |
| 11617 | // friend a member of an arbitrary specialization of your template). |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11618 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11619 | Decl *D; |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11620 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11621 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11622 | NumTempParamLists, |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11623 | TempParams.data(), |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 11624 | TSI, |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11625 | DS.getFriendSpecLoc()); |
| 11626 | else |
Abramo Bagnara | 254b630 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 11627 | D = CheckFriendTypeDecl(Loc, DS.getFriendSpecLoc(), TSI); |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11628 | |
| 11629 | if (!D) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11630 | return 0; |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11631 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11632 | D->setAccess(AS_public); |
| 11633 | CurContext->addDecl(D); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11634 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11635 | return D; |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11636 | } |
| 11637 | |
Rafael Espindola | 0a67e2f | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 11638 | NamedDecl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, |
| 11639 | MultiTemplateParamsArg TemplateParams) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11640 | const DeclSpec &DS = D.getDeclSpec(); |
| 11641 | |
| 11642 | assert(DS.isFriendSpecified()); |
| 11643 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 11644 | |
| 11645 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11646 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11647 | |
| 11648 | // C++ [class.friend]p1 |
| 11649 | // A friend of a class is a function or class.... |
| 11650 | // Note that this sees through typedefs, which is intended. |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11651 | // It *doesn't* see through dependent types, which is correct |
| 11652 | // according to [temp.arg.type]p3: |
| 11653 | // If a declaration acquires a function type through a |
| 11654 | // type dependent on a template-parameter and this causes |
| 11655 | // a declaration that does not use the syntactic form of a |
| 11656 | // function declarator to have a function type, the program |
| 11657 | // is ill-formed. |
Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11658 | if (!TInfo->getType()->isFunctionType()) { |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11659 | Diag(Loc, diag::err_unexpected_friend); |
| 11660 | |
| 11661 | // It might be worthwhile to try to recover by creating an |
| 11662 | // appropriate declaration. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11663 | return 0; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11664 | } |
| 11665 | |
| 11666 | // C++ [namespace.memdef]p3 |
| 11667 | // - If a friend declaration in a non-local class first declares a |
| 11668 | // class or function, the friend class or function is a member |
| 11669 | // of the innermost enclosing namespace. |
| 11670 | // - The name of the friend is not found by simple name lookup |
| 11671 | // until a matching declaration is provided in that namespace |
| 11672 | // scope (either before or after the class declaration granting |
| 11673 | // friendship). |
| 11674 | // - If a friend function is called, its name may be found by the |
| 11675 | // name lookup that considers functions from namespaces and |
| 11676 | // classes associated with the types of the function arguments. |
| 11677 | // - When looking for a prior declaration of a class or a function |
| 11678 | // declared as a friend, scopes outside the innermost enclosing |
| 11679 | // namespace scope are not considered. |
| 11680 | |
John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11681 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 11682 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 11683 | DeclarationName Name = NameInfo.getName(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11684 | assert(Name); |
| 11685 | |
Douglas Gregor | 6c110f3 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 11686 | // Check for unexpanded parameter packs. |
| 11687 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 11688 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 11689 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 11690 | return 0; |
| 11691 | |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11692 | // The context we found the declaration in, or in which we should |
| 11693 | // create the declaration. |
| 11694 | DeclContext *DC; |
John McCall | ccbc032 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 11695 | Scope *DCScope = S; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 11696 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11697 | ForRedeclaration); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11698 | |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11699 | // There are five cases here. |
| 11700 | // - There's no scope specifier and we're in a local class. Only look |
| 11701 | // for functions declared in the immediately-enclosing block scope. |
| 11702 | // We recover from invalid scope qualifiers as if they just weren't there. |
| 11703 | FunctionDecl *FunctionContainingLocalClass = 0; |
| 11704 | if ((SS.isInvalid() || !SS.isSet()) && |
| 11705 | (FunctionContainingLocalClass = |
| 11706 | cast<CXXRecordDecl>(CurContext)->isLocalClass())) { |
| 11707 | // C++11 [class.friend]p11: |
John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11708 | // If a friend declaration appears in a local class and the name |
| 11709 | // specified is an unqualified name, a prior declaration is |
| 11710 | // looked up without considering scopes that are outside the |
| 11711 | // innermost enclosing non-class scope. For a friend function |
| 11712 | // declaration, if there is no prior declaration, the program is |
| 11713 | // ill-formed. |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11714 | |
| 11715 | // Find the innermost enclosing non-class scope. This is the block |
| 11716 | // scope containing the local class definition (or for a nested class, |
| 11717 | // the outer local class). |
| 11718 | DCScope = S->getFnParent(); |
| 11719 | |
| 11720 | // Look up the function name in the scope. |
| 11721 | Previous.clear(LookupLocalFriendName); |
| 11722 | LookupName(Previous, S, /*AllowBuiltinCreation*/false); |
| 11723 | |
| 11724 | if (!Previous.empty()) { |
| 11725 | // All possible previous declarations must have the same context: |
| 11726 | // either they were declared at block scope or they are members of |
| 11727 | // one of the enclosing local classes. |
| 11728 | DC = Previous.getRepresentativeDecl()->getDeclContext(); |
| 11729 | } else { |
| 11730 | // This is ill-formed, but provide the context that we would have |
| 11731 | // declared the function in, if we were permitted to, for error recovery. |
| 11732 | DC = FunctionContainingLocalClass; |
| 11733 | } |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 11734 | adjustContextForLocalExternDecl(DC); |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11735 | |
| 11736 | // C++ [class.friend]p6: |
| 11737 | // A function can be defined in a friend declaration of a class if and |
| 11738 | // only if the class is a non-local class (9.8), the function name is |
| 11739 | // unqualified, and the function has namespace scope. |
| 11740 | if (D.isFunctionDefinition()) { |
| 11741 | Diag(NameInfo.getBeginLoc(), diag::err_friend_def_in_local_class); |
| 11742 | } |
| 11743 | |
| 11744 | // - There's no scope specifier, in which case we just go to the |
| 11745 | // appropriate scope and look for a function or function template |
| 11746 | // there as appropriate. |
| 11747 | } else if (SS.isInvalid() || !SS.isSet()) { |
| 11748 | // C++11 [namespace.memdef]p3: |
| 11749 | // If the name in a friend declaration is neither qualified nor |
| 11750 | // a template-id and the declaration is a function or an |
| 11751 | // elaborated-type-specifier, the lookup to determine whether |
| 11752 | // the entity has been previously declared shall not consider |
| 11753 | // any scopes outside the innermost enclosing namespace. |
John McCall | f477659 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 11754 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11755 | |
John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11756 | // Find the appropriate context according to the above. |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11757 | DC = CurContext; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11758 | |
Rafael Espindola | 3626b7e | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11759 | // Skip class contexts. If someone can cite chapter and verse |
| 11760 | // for this behavior, that would be nice --- it's what GCC and |
| 11761 | // EDG do, and it seems like a reasonable intent, but the spec |
| 11762 | // really only says that checks for unqualified existing |
| 11763 | // declarations should stop at the nearest enclosing namespace, |
| 11764 | // not that they should only consider the nearest enclosing |
| 11765 | // namespace. |
| 11766 | while (DC->isRecord()) |
| 11767 | DC = DC->getParent(); |
| 11768 | |
| 11769 | DeclContext *LookupDC = DC; |
| 11770 | while (LookupDC->isTransparentContext()) |
| 11771 | LookupDC = LookupDC->getParent(); |
| 11772 | |
| 11773 | while (true) { |
| 11774 | LookupQualifiedName(Previous, LookupDC); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11775 | |
Rafael Espindola | 3626b7e | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11776 | if (!Previous.empty()) { |
| 11777 | DC = LookupDC; |
| 11778 | break; |
John McCall | f477659 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 11779 | } |
Rafael Espindola | 3626b7e | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11780 | |
| 11781 | if (isTemplateId) { |
| 11782 | if (isa<TranslationUnitDecl>(LookupDC)) break; |
| 11783 | } else { |
| 11784 | if (LookupDC->isFileContext()) break; |
| 11785 | } |
| 11786 | LookupDC = LookupDC->getParent(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11787 | } |
| 11788 | |
John McCall | ccbc032 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 11789 | DCScope = getScopeForDeclContext(S, DC); |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11790 | |
John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11791 | // - There's a non-dependent scope specifier, in which case we |
| 11792 | // compute it and do a previous lookup there for a function |
| 11793 | // or function template. |
| 11794 | } else if (!SS.getScopeRep()->isDependent()) { |
| 11795 | DC = computeDeclContext(SS); |
| 11796 | if (!DC) return 0; |
| 11797 | |
| 11798 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 11799 | |
| 11800 | LookupQualifiedName(Previous, DC); |
| 11801 | |
| 11802 | // Ignore things found implicitly in the wrong scope. |
| 11803 | // TODO: better diagnostics for this case. Suggesting the right |
| 11804 | // qualified scope would be nice... |
| 11805 | LookupResult::Filter F = Previous.makeFilter(); |
| 11806 | while (F.hasNext()) { |
| 11807 | NamedDecl *D = F.next(); |
| 11808 | if (!DC->InEnclosingNamespaceSetOf( |
| 11809 | D->getDeclContext()->getRedeclContext())) |
| 11810 | F.erase(); |
| 11811 | } |
| 11812 | F.done(); |
| 11813 | |
| 11814 | if (Previous.empty()) { |
| 11815 | D.setInvalidType(); |
Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11816 | Diag(Loc, diag::err_qualified_friend_not_found) |
| 11817 | << Name << TInfo->getType(); |
John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11818 | return 0; |
| 11819 | } |
| 11820 | |
| 11821 | // C++ [class.friend]p1: A friend of a class is a function or |
| 11822 | // class that is not a member of the class . . . |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 11823 | if (DC->Equals(CurContext)) |
| 11824 | Diag(DS.getFriendSpecLoc(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11825 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 0bf8a492 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 11826 | diag::warn_cxx98_compat_friend_is_member : |
| 11827 | diag::err_friend_is_member); |
Douglas Gregor | 16e6561 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11828 | |
Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11829 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 16e6561 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11830 | // C++ [class.friend]p6: |
| 11831 | // A function can be defined in a friend declaration of a class if and |
| 11832 | // only if the class is a non-local class (9.8), the function name is |
| 11833 | // unqualified, and the function has namespace scope. |
| 11834 | SemaDiagnosticBuilder DB |
| 11835 | = Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def); |
| 11836 | |
| 11837 | DB << SS.getScopeRep(); |
| 11838 | if (DC->isFileContext()) |
| 11839 | DB << FixItHint::CreateRemoval(SS.getRange()); |
| 11840 | SS.clear(); |
| 11841 | } |
John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11842 | |
| 11843 | // - There's a scope specifier that does not match any template |
| 11844 | // parameter lists, in which case we use some arbitrary context, |
| 11845 | // create a method or method template, and wait for instantiation. |
| 11846 | // - There's a scope specifier that does match some template |
| 11847 | // parameter lists, which we don't handle right now. |
| 11848 | } else { |
Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11849 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 16e6561 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11850 | // C++ [class.friend]p6: |
| 11851 | // A function can be defined in a friend declaration of a class if and |
| 11852 | // only if the class is a non-local class (9.8), the function name is |
| 11853 | // unqualified, and the function has namespace scope. |
| 11854 | Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def) |
| 11855 | << SS.getScopeRep(); |
| 11856 | } |
| 11857 | |
John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11858 | DC = CurContext; |
| 11859 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11860 | } |
Douglas Gregor | 16e6561 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11861 | |
John McCall | f7cfb22 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11862 | if (!DC->isRecord()) { |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11863 | // This implies that it has to be an operator or function. |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 11864 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 11865 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 11866 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11867 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 11868 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 11869 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11870 | return 0; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11871 | } |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11872 | } |
Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11873 | |
Douglas Gregor | dd847ba | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 11874 | // FIXME: This is an egregious hack to cope with cases where the scope stack |
| 11875 | // does not contain the declaration context, i.e., in an out-of-line |
| 11876 | // definition of a class. |
| 11877 | Scope FakeDCScope(S, Scope::DeclScope, Diags); |
| 11878 | if (!DCScope) { |
| 11879 | FakeDCScope.setEntity(DC); |
| 11880 | DCScope = &FakeDCScope; |
| 11881 | } |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11882 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 11883 | bool AddToScope = true; |
Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11884 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, TInfo, Previous, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11885 | TemplateParams, AddToScope); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11886 | if (!ND) return 0; |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11887 | |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 11888 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 5ed6e8f | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 11889 | |
Richard Smith | 114394f | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11890 | // If we performed typo correction, we might have added a scope specifier |
| 11891 | // and changed the decl context. |
| 11892 | DC = ND->getDeclContext(); |
| 11893 | |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11894 | // Add the function declaration to the appropriate lookup tables, |
| 11895 | // adjusting the redeclarations list as necessary. We don't |
| 11896 | // want to do this yet if the friending class is dependent. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11897 | // |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11898 | // Also update the scope-based lookup if the target context's |
| 11899 | // lookup context is in lexical scope. |
| 11900 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 11901 | DC = DC->getRedeclContext(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 11902 | DC->makeDeclVisibleInContext(ND); |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11903 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 11904 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11905 | } |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11906 | |
| 11907 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 11908 | D.getIdentifierLoc(), ND, |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11909 | DS.getFriendSpecLoc()); |
John McCall | 75c03bb | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 11910 | FrD->setAccess(AS_public); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11911 | CurContext->addDecl(FrD); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11912 | |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 11913 | if (ND->isInvalidDecl()) { |
John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11914 | FrD->setInvalidDecl(); |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 11915 | } else { |
| 11916 | if (DC->isRecord()) CheckFriendAccess(ND); |
| 11917 | |
John McCall | 2c2eb12 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 11918 | FunctionDecl *FD; |
| 11919 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 11920 | FD = FTD->getTemplatedDecl(); |
| 11921 | else |
| 11922 | FD = cast<FunctionDecl>(ND); |
| 11923 | |
David Majnemer | 502b0ed | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 11924 | // C++11 [dcl.fct.default]p4: If a friend declaration specifies a |
| 11925 | // default argument expression, that declaration shall be a definition |
| 11926 | // and shall be the only declaration of the function or function |
| 11927 | // template in the translation unit. |
| 11928 | if (functionDeclHasDefaultArgument(FD)) { |
| 11929 | if (FunctionDecl *OldFD = FD->getPreviousDecl()) { |
| 11930 | Diag(FD->getLocation(), diag::err_friend_decl_with_def_arg_redeclared); |
| 11931 | Diag(OldFD->getLocation(), diag::note_previous_declaration); |
| 11932 | } else if (!D.isFunctionDefinition()) |
| 11933 | Diag(FD->getLocation(), diag::err_friend_decl_with_def_arg_must_be_def); |
| 11934 | } |
| 11935 | |
John McCall | 2c2eb12 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 11936 | // Mark templated-scope function declarations as unsupported. |
| 11937 | if (FD->getNumTemplateParameterLists()) |
| 11938 | FrD->setUnsupportedFriend(true); |
| 11939 | } |
John McCall | de3fd22 | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11940 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11941 | return ND; |
Anders Carlsson | 3881170 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 11942 | } |
| 11943 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11944 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 11945 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11946 | |
Aaron Ballman | f96361e | 2013-01-16 23:39:10 +0000 | [diff] [blame] | 11947 | FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(Dcl); |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11948 | if (!Fn) { |
| 11949 | Diag(DelLoc, diag::err_deleted_non_function); |
| 11950 | return; |
| 11951 | } |
Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 11952 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 11953 | if (const FunctionDecl *Prev = Fn->getPreviousDecl()) { |
David Blaikie | 3680552 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 11954 | // Don't consider the implicit declaration we generate for explicit |
| 11955 | // specializations. FIXME: Do not generate these implicit declarations. |
Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 11956 | if ((Prev->getTemplateSpecializationKind() != TSK_ExplicitSpecialization || |
| 11957 | Prev->getPreviousDecl()) && |
| 11958 | !Prev->isDefined()) { |
David Blaikie | 3680552 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 11959 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
Richard Smith | bdd1464 | 2014-02-04 01:14:30 +0000 | [diff] [blame] | 11960 | Diag(Prev->getLocation().isInvalid() ? DelLoc : Prev->getLocation(), |
| 11961 | Prev->isImplicit() ? diag::note_previous_implicit_declaration |
| 11962 | : diag::note_previous_declaration); |
David Blaikie | 3680552 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 11963 | } |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11964 | // If the declaration wasn't the first, we delete the function anyway for |
| 11965 | // recovery. |
Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 11966 | Fn = Fn->getCanonicalDecl(); |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11967 | } |
Richard Smith | b4d2a15 | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 11968 | |
| 11969 | if (Fn->isDeleted()) |
| 11970 | return; |
| 11971 | |
| 11972 | // See if we're deleting a function which is already known to override a |
| 11973 | // non-deleted virtual function. |
| 11974 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn)) { |
| 11975 | bool IssuedDiagnostic = false; |
| 11976 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 11977 | E = MD->end_overridden_methods(); |
| 11978 | I != E; ++I) { |
| 11979 | if (!(*MD->begin_overridden_methods())->isDeleted()) { |
| 11980 | if (!IssuedDiagnostic) { |
| 11981 | Diag(DelLoc, diag::err_deleted_override) << MD->getDeclName(); |
| 11982 | IssuedDiagnostic = true; |
| 11983 | } |
| 11984 | Diag((*I)->getLocation(), diag::note_overridden_virtual_function); |
| 11985 | } |
| 11986 | } |
| 11987 | } |
| 11988 | |
Richard Smith | b63b6ee | 2014-01-22 01:43:19 +0000 | [diff] [blame] | 11989 | // C++11 [basic.start.main]p3: |
| 11990 | // A program that defines main as deleted [...] is ill-formed. |
| 11991 | if (Fn->isMain()) |
| 11992 | Diag(DelLoc, diag::err_deleted_main); |
| 11993 | |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 11994 | Fn->setDeletedAsWritten(); |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11995 | } |
Sebastian Redl | 4c01866 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 11996 | |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11997 | void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) { |
Aaron Ballman | f96361e | 2013-01-16 23:39:10 +0000 | [diff] [blame] | 11998 | CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Dcl); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11999 | |
| 12000 | if (MD) { |
Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 12001 | if (MD->getParent()->isDependentType()) { |
| 12002 | MD->setDefaulted(); |
| 12003 | MD->setExplicitlyDefaulted(); |
| 12004 | return; |
| 12005 | } |
| 12006 | |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12007 | CXXSpecialMember Member = getSpecialMember(MD); |
| 12008 | if (Member == CXXInvalid) { |
Eli Friedman | 84c0143e | 2013-07-11 23:55:07 +0000 | [diff] [blame] | 12009 | if (!MD->isInvalidDecl()) |
| 12010 | Diag(DefaultLoc, diag::err_default_special_members); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12011 | return; |
| 12012 | } |
| 12013 | |
| 12014 | MD->setDefaulted(); |
| 12015 | MD->setExplicitlyDefaulted(); |
| 12016 | |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 12017 | // If this definition appears within the record, do the checking when |
| 12018 | // the record is complete. |
| 12019 | const FunctionDecl *Primary = MD; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12020 | if (const FunctionDecl *Pattern = MD->getTemplateInstantiationPattern()) |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 12021 | // Find the uninstantiated declaration that actually had the '= default' |
| 12022 | // on it. |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12023 | Pattern->isDefined(Primary); |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 12024 | |
Richard Smith | 3901dfe | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 12025 | // If the method was defaulted on its first declaration, we will have |
| 12026 | // already performed the checking in CheckCompletedCXXClass. Such a |
| 12027 | // declaration doesn't trigger an implicit definition. |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 12028 | if (Primary == Primary->getCanonicalDecl()) |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12029 | return; |
| 12030 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12031 | CheckExplicitlyDefaultedSpecialMember(MD); |
| 12032 | |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 12033 | // The exception specification is needed because we are defining the |
| 12034 | // function. |
| 12035 | ResolveExceptionSpec(DefaultLoc, |
| 12036 | MD->getType()->castAs<FunctionProtoType>()); |
| 12037 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12038 | if (MD->isInvalidDecl()) |
| 12039 | return; |
| 12040 | |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12041 | switch (Member) { |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12042 | case CXXDefaultConstructor: |
| 12043 | DefineImplicitDefaultConstructor(DefaultLoc, |
| 12044 | cast<CXXConstructorDecl>(MD)); |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 12045 | break; |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12046 | case CXXCopyConstructor: |
| 12047 | DefineImplicitCopyConstructor(DefaultLoc, cast<CXXConstructorDecl>(MD)); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12048 | break; |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12049 | case CXXCopyAssignment: |
| 12050 | DefineImplicitCopyAssignment(DefaultLoc, MD); |
Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 12051 | break; |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12052 | case CXXDestructor: |
| 12053 | DefineImplicitDestructor(DefaultLoc, cast<CXXDestructorDecl>(MD)); |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 12054 | break; |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12055 | case CXXMoveConstructor: |
| 12056 | DefineImplicitMoveConstructor(DefaultLoc, cast<CXXConstructorDecl>(MD)); |
Alexis Hunt | 119c10e | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 12057 | break; |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12058 | case CXXMoveAssignment: |
| 12059 | DefineImplicitMoveAssignment(DefaultLoc, MD); |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 12060 | break; |
Sebastian Redl | 22653ba | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 12061 | case CXXInvalid: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 12062 | llvm_unreachable("Invalid special member."); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 12063 | } |
| 12064 | } else { |
| 12065 | Diag(DefaultLoc, diag::err_default_special_members); |
| 12066 | } |
| 12067 | } |
| 12068 | |
Sebastian Redl | 4c01866 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 12069 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 12070 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Sebastian Redl | 4c01866 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 12071 | Stmt *SubStmt = *CI; |
| 12072 | if (!SubStmt) |
| 12073 | continue; |
| 12074 | if (isa<ReturnStmt>(SubStmt)) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 12075 | Self.Diag(SubStmt->getLocStart(), |
Sebastian Redl | 4c01866 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 12076 | diag::err_return_in_constructor_handler); |
| 12077 | if (!isa<Expr>(SubStmt)) |
| 12078 | SearchForReturnInStmt(Self, SubStmt); |
| 12079 | } |
| 12080 | } |
| 12081 | |
| 12082 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 12083 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 12084 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 12085 | SearchForReturnInStmt(*this, Handler); |
| 12086 | } |
| 12087 | } |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12088 | |
David Blaikie | 68f71a3 | 2013-01-18 23:03:15 +0000 | [diff] [blame] | 12089 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 12090 | const CXXMethodDecl *Old) { |
| 12091 | const FunctionType *NewFT = New->getType()->getAs<FunctionType>(); |
| 12092 | const FunctionType *OldFT = Old->getType()->getAs<FunctionType>(); |
| 12093 | |
| 12094 | CallingConv NewCC = NewFT->getCallConv(), OldCC = OldFT->getCallConv(); |
| 12095 | |
| 12096 | // If the calling conventions match, everything is fine |
| 12097 | if (NewCC == OldCC) |
| 12098 | return false; |
| 12099 | |
Hans Wennborg | 2545efe | 2013-12-11 17:42:11 +0000 | [diff] [blame] | 12100 | // If the calling conventions mismatch because the new function is static, |
| 12101 | // suppress the calling convention mismatch error; the error about static |
| 12102 | // function override (err_static_overrides_virtual from |
| 12103 | // Sema::CheckFunctionDeclaration) is more clear. |
| 12104 | if (New->getStorageClass() == SC_Static) |
| 12105 | return false; |
| 12106 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 12107 | Diag(New->getLocation(), |
| 12108 | diag::err_conflicting_overriding_cc_attributes) |
| 12109 | << New->getDeclName() << New->getType() << Old->getType(); |
| 12110 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 12111 | return true; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 12112 | } |
| 12113 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12114 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12115 | const CXXMethodDecl *Old) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 12116 | QualType NewTy = New->getType()->getAs<FunctionType>()->getReturnType(); |
| 12117 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getReturnType(); |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12118 | |
Chandler Carruth | 284bb2e | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 12119 | if (Context.hasSameType(NewTy, OldTy) || |
| 12120 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12121 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12122 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12123 | // Check if the return types are covariant |
| 12124 | QualType NewClassTy, OldClassTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12125 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12126 | /// Both types must be pointers or references to classes. |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 12127 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 12128 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12129 | NewClassTy = NewPT->getPointeeType(); |
| 12130 | OldClassTy = OldPT->getPointeeType(); |
| 12131 | } |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 12132 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 12133 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 12134 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 12135 | NewClassTy = NewRT->getPointeeType(); |
| 12136 | OldClassTy = OldRT->getPointeeType(); |
| 12137 | } |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12138 | } |
| 12139 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12140 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12141 | // The return types aren't either both pointers or references to a class type. |
| 12142 | if (NewClassTy.isNull()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12143 | Diag(New->getLocation(), |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12144 | diag::err_different_return_type_for_overriding_virtual_function) |
| 12145 | << New->getDeclName() << NewTy << OldTy; |
| 12146 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12147 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12148 | return true; |
| 12149 | } |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12150 | |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 12151 | // C++ [class.virtual]p6: |
| 12152 | // If the return type of D::f differs from the return type of B::f, the |
| 12153 | // class type in the return type of D::f shall be complete at the point of |
| 12154 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | 0c9dd84 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 12155 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 12156 | if (!RT->isBeingDefined() && |
| 12157 | RequireCompleteType(New->getLocation(), NewClassTy, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 12158 | diag::err_covariant_return_incomplete, |
| 12159 | New->getDeclName())) |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 12160 | return true; |
Anders Carlsson | 0c9dd84 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 12161 | } |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 12162 | |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 12163 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12164 | // Check if the new class derives from the old class. |
| 12165 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 12166 | Diag(New->getLocation(), |
| 12167 | diag::err_covariant_return_not_derived) |
| 12168 | << New->getDeclName() << NewTy << OldTy; |
| 12169 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 12170 | return true; |
| 12171 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12172 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12173 | // Check if we the conversion from derived to base is valid. |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 12174 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 12175 | diag::err_covariant_return_inaccessible_base, |
| 12176 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 12177 | // FIXME: Should this point to the return type? |
| 12178 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 12179 | // FIXME: this note won't trigger for delayed access control |
| 12180 | // diagnostics, and it's impossible to get an undelayed error |
| 12181 | // here from access control during the original parse because |
| 12182 | // the ParsingDeclSpec/ParsingDeclarator are still in scope. |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12183 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 12184 | return true; |
| 12185 | } |
| 12186 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12187 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12188 | // The qualifiers of the return types must be the same. |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 12189 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12190 | Diag(New->getLocation(), |
| 12191 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12192 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12193 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 12194 | return true; |
| 12195 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12196 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12197 | |
| 12198 | // The new class type must have the same or less qualifiers as the old type. |
| 12199 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 12200 | Diag(New->getLocation(), |
| 12201 | diag::err_covariant_return_type_class_type_more_qualified) |
| 12202 | << New->getDeclName() << NewTy << OldTy; |
| 12203 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 12204 | return true; |
| 12205 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12206 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 12207 | return false; |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 12208 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12209 | |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 12210 | /// \brief Mark the given method pure. |
| 12211 | /// |
| 12212 | /// \param Method the method to be marked pure. |
| 12213 | /// |
| 12214 | /// \param InitRange the source range that covers the "0" initializer. |
| 12215 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 12216 | SourceLocation EndLoc = InitRange.getEnd(); |
| 12217 | if (EndLoc.isValid()) |
| 12218 | Method->setRangeEnd(EndLoc); |
| 12219 | |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 12220 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 12221 | Method->setPure(); |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 12222 | return false; |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 12223 | } |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 12224 | |
| 12225 | if (!Method->isInvalidDecl()) |
| 12226 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 12227 | << Method->getDeclName() << InitRange; |
| 12228 | return true; |
| 12229 | } |
| 12230 | |
Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 12231 | /// \brief Determine whether the given declaration is a static data member. |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12232 | static bool isStaticDataMember(const Decl *D) { |
| 12233 | if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(D)) |
| 12234 | return Var->isStaticDataMember(); |
| 12235 | |
| 12236 | return false; |
Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 12237 | } |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12238 | |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 12239 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 12240 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 12241 | /// is a fresh scope pushed for just this purpose. |
| 12242 | /// |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12243 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 12244 | /// static data member of class X, names should be looked up in the scope of |
| 12245 | /// class X. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12246 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12247 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | 8e4be0b | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 12248 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12249 | |
Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 12250 | // We will always have a nested name specifier here, but this declaration |
| 12251 | // might not be out of line if the specifier names the current namespace: |
| 12252 | // extern int n; |
| 12253 | // int ::n = 0; |
| 12254 | if (D->isOutOfLine()) |
| 12255 | EnterDeclaratorContext(S, D->getDeclContext()); |
| 12256 | |
Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 12257 | // If we are parsing the initializer for a static data member, push a |
| 12258 | // new expression evaluation context that is associated with this static |
| 12259 | // data member. |
| 12260 | if (isStaticDataMember(D)) |
| 12261 | PushExpressionEvaluationContext(PotentiallyEvaluated, D); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12262 | } |
| 12263 | |
| 12264 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12265 | /// initializer for the out-of-line declaration 'D'. |
| 12266 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12267 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | 8e4be0b | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 12268 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12269 | |
Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 12270 | if (isStaticDataMember(D)) |
Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 12271 | PopExpressionEvaluationContext(); |
Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 12272 | |
Richard Smith | a230224 | 2013-12-05 07:51:02 +0000 | [diff] [blame] | 12273 | if (D->isOutOfLine()) |
| 12274 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 12275 | } |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 12276 | |
| 12277 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 12278 | /// C++ if/switch/while/for statement. |
| 12279 | /// e.g: "if (int x = f()) {...}" |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 12280 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 12281 | // C++ 6.4p2: |
| 12282 | // The declarator shall not specify a function or an array. |
| 12283 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 12284 | // new class or enumeration. |
| 12285 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 12286 | "Parser allowed 'typedef' as storage class of condition decl."); |
Argyrios Kyrtzidis | 8ea7e58 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 12287 | |
| 12288 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 1fe12c9 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 12289 | if (!Dcl) |
| 12290 | return true; |
| 12291 | |
Argyrios Kyrtzidis | 8ea7e58 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 12292 | if (isa<FunctionDecl>(Dcl)) { // The declarator shall not specify a function. |
| 12293 | Diag(Dcl->getLocation(), diag::err_invalid_use_of_function_type) |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 12294 | << D.getSourceRange(); |
Douglas Gregor | 1fe12c9 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 12295 | return true; |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 12296 | } |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 12297 | |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 12298 | return Dcl; |
| 12299 | } |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 12300 | |
Douglas Gregor | 4daf6a3 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 12301 | void Sema::LoadExternalVTableUses() { |
| 12302 | if (!ExternalSource) |
| 12303 | return; |
| 12304 | |
| 12305 | SmallVector<ExternalVTableUse, 4> VTables; |
| 12306 | ExternalSource->ReadUsedVTables(VTables); |
| 12307 | SmallVector<VTableUse, 4> NewUses; |
| 12308 | for (unsigned I = 0, N = VTables.size(); I != N; ++I) { |
| 12309 | llvm::DenseMap<CXXRecordDecl *, bool>::iterator Pos |
| 12310 | = VTablesUsed.find(VTables[I].Record); |
| 12311 | // Even if a definition wasn't required before, it may be required now. |
| 12312 | if (Pos != VTablesUsed.end()) { |
| 12313 | if (!Pos->second && VTables[I].DefinitionRequired) |
| 12314 | Pos->second = true; |
| 12315 | continue; |
| 12316 | } |
| 12317 | |
| 12318 | VTablesUsed[VTables[I].Record] = VTables[I].DefinitionRequired; |
| 12319 | NewUses.push_back(VTableUse(VTables[I].Record, VTables[I].Location)); |
| 12320 | } |
| 12321 | |
| 12322 | VTableUses.insert(VTableUses.begin(), NewUses.begin(), NewUses.end()); |
| 12323 | } |
| 12324 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12325 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 12326 | bool DefinitionRequired) { |
| 12327 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 12328 | // not have a vtable. |
| 12329 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
John McCall | f413f5e | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 12330 | CurContext->isDependentContext() || isUnevaluatedContext()) |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 12331 | return; |
| 12332 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12333 | // Try to insert this class into the map. |
Douglas Gregor | 4daf6a3 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 12334 | LoadExternalVTableUses(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12335 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 12336 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 12337 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 12338 | if (!Pos.second) { |
Daniel Dunbar | 5321776 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 12339 | // If we already had an entry, check to see if we are promoting this vtable |
| 12340 | // to required a definition. If so, we need to reappend to the VTableUses |
| 12341 | // list, since we may have already processed the first entry. |
| 12342 | if (DefinitionRequired && !Pos.first->second) { |
| 12343 | Pos.first->second = true; |
| 12344 | } else { |
| 12345 | // Otherwise, we can early exit. |
| 12346 | return; |
| 12347 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12348 | } |
| 12349 | |
| 12350 | // Local classes need to have their virtual members marked |
| 12351 | // immediately. For all other classes, we mark their virtual members |
| 12352 | // at the end of the translation unit. |
| 12353 | if (Class->isLocalClass()) |
| 12354 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 0547ad3 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 12355 | else |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12356 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | 0c4aad1 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 12357 | } |
| 12358 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12359 | bool Sema::DefineUsedVTables() { |
Douglas Gregor | 4daf6a3 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 12360 | LoadExternalVTableUses(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12361 | if (VTableUses.empty()) |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12362 | return false; |
Chandler Carruth | 88bfa5e | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 12363 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12364 | // Note: The VTableUses vector could grow as a result of marking |
| 12365 | // 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] | 12366 | // time through the loop and prefer indices (which are stable) to |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12367 | // iterators (which are not). |
Douglas Gregor | 9750969 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 12368 | bool DefinedAnything = false; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12369 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | 105ce6d | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 12370 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12371 | if (!Class) |
| 12372 | continue; |
| 12373 | |
| 12374 | SourceLocation Loc = VTableUses[I].second; |
| 12375 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12376 | bool DefineVTable = true; |
| 12377 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12378 | // If this class has a key function, but that key function is |
| 12379 | // defined in another translation unit, we don't need to emit the |
| 12380 | // vtable even though we're using it. |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 12381 | const CXXMethodDecl *KeyFunction = Context.getCurrentKeyFunction(Class); |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 12382 | if (KeyFunction && !KeyFunction->hasBody()) { |
Rafael Espindola | ef7fe1f | 2013-08-26 23:23:21 +0000 | [diff] [blame] | 12383 | // The key function is in another translation unit. |
| 12384 | DefineVTable = false; |
| 12385 | TemplateSpecializationKind TSK = |
| 12386 | KeyFunction->getTemplateSpecializationKind(); |
| 12387 | assert(TSK != TSK_ExplicitInstantiationDefinition && |
| 12388 | TSK != TSK_ImplicitInstantiation && |
| 12389 | "Instantiations don't have key functions"); |
| 12390 | (void)TSK; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12391 | } else if (!KeyFunction) { |
| 12392 | // If we have a class with no key function that is the subject |
| 12393 | // of an explicit instantiation declaration, suppress the |
| 12394 | // vtable; it will live with the explicit instantiation |
| 12395 | // definition. |
| 12396 | bool IsExplicitInstantiationDeclaration |
| 12397 | = Class->getTemplateSpecializationKind() |
| 12398 | == TSK_ExplicitInstantiationDeclaration; |
| 12399 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 12400 | REnd = Class->redecls_end(); |
| 12401 | R != REnd; ++R) { |
| 12402 | TemplateSpecializationKind TSK |
| 12403 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 12404 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 12405 | IsExplicitInstantiationDeclaration = true; |
| 12406 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 12407 | IsExplicitInstantiationDeclaration = false; |
| 12408 | break; |
| 12409 | } |
| 12410 | } |
| 12411 | |
| 12412 | if (IsExplicitInstantiationDeclaration) |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12413 | DefineVTable = false; |
| 12414 | } |
| 12415 | |
| 12416 | // The exception specifications for all virtual members may be needed even |
| 12417 | // if we are not providing an authoritative form of the vtable in this TU. |
| 12418 | // We may choose to emit it available_externally anyway. |
| 12419 | if (!DefineVTable) { |
| 12420 | MarkVirtualMemberExceptionSpecsNeeded(Loc, Class); |
| 12421 | continue; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12422 | } |
| 12423 | |
| 12424 | // Mark all of the virtual members of this class as referenced, so |
| 12425 | // that we can build a vtable. Then, tell the AST consumer that a |
| 12426 | // vtable for this class is required. |
Douglas Gregor | 9750969 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 12427 | DefinedAnything = true; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12428 | MarkVirtualMembersReferenced(Loc, Class); |
| 12429 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 12430 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 12431 | |
| 12432 | // Optionally warn if we're emitting a weak vtable. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 12433 | if (Class->isExternallyVisible() && |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12434 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Douglas Gregor | 34bc6e5 | 2011-09-23 19:04:03 +0000 | [diff] [blame] | 12435 | const FunctionDecl *KeyFunctionDef = 0; |
| 12436 | if (!KeyFunction || |
| 12437 | (KeyFunction->hasBody(KeyFunctionDef) && |
| 12438 | KeyFunctionDef->isInlined())) |
David Blaikie | 72b6120 | 2011-12-09 18:32:50 +0000 | [diff] [blame] | 12439 | Diag(Class->getLocation(), Class->getTemplateSpecializationKind() == |
| 12440 | TSK_ExplicitInstantiationDefinition |
| 12441 | ? diag::warn_weak_template_vtable : diag::warn_weak_vtable) |
| 12442 | << Class; |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12443 | } |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 12444 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12445 | VTableUses.clear(); |
| 12446 | |
Douglas Gregor | 9750969 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 12447 | return DefinedAnything; |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 12448 | } |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12449 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12450 | void Sema::MarkVirtualMemberExceptionSpecsNeeded(SourceLocation Loc, |
| 12451 | const CXXRecordDecl *RD) { |
| 12452 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 12453 | E = RD->method_end(); I != E; ++I) |
| 12454 | if ((*I)->isVirtual() && !(*I)->isPure()) |
| 12455 | ResolveExceptionSpec(Loc, (*I)->getType()->castAs<FunctionProtoType>()); |
| 12456 | } |
| 12457 | |
Rafael Espindola | 5b33408 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 12458 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 12459 | const CXXRecordDecl *RD) { |
Richard Smith | 4ff9ff9 | 2012-07-07 06:59:51 +0000 | [diff] [blame] | 12460 | // Mark all functions which will appear in RD's vtable as used. |
| 12461 | CXXFinalOverriderMap FinalOverriders; |
| 12462 | RD->getFinalOverriders(FinalOverriders); |
| 12463 | for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(), |
| 12464 | E = FinalOverriders.end(); |
| 12465 | I != E; ++I) { |
| 12466 | for (OverridingMethods::const_iterator OI = I->second.begin(), |
| 12467 | OE = I->second.end(); |
| 12468 | OI != OE; ++OI) { |
| 12469 | assert(OI->second.size() > 0 && "no final overrider"); |
| 12470 | CXXMethodDecl *Overrider = OI->second.front().Method; |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12471 | |
Richard Smith | 4ff9ff9 | 2012-07-07 06:59:51 +0000 | [diff] [blame] | 12472 | // C++ [basic.def.odr]p2: |
| 12473 | // [...] A virtual member function is used if it is not pure. [...] |
| 12474 | if (!Overrider->isPure()) |
| 12475 | MarkFunctionReferenced(Loc, Overrider); |
| 12476 | } |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12477 | } |
Rafael Espindola | 5b33408 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 12478 | |
| 12479 | // Only classes that have virtual bases need a VTT. |
| 12480 | if (RD->getNumVBases() == 0) |
| 12481 | return; |
| 12482 | |
| 12483 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 12484 | e = RD->bases_end(); i != e; ++i) { |
| 12485 | const CXXRecordDecl *Base = |
| 12486 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 5b33408 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 12487 | if (Base->getNumVBases() == 0) |
| 12488 | continue; |
| 12489 | MarkVirtualMembersReferenced(Loc, Base); |
| 12490 | } |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12491 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12492 | |
| 12493 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 12494 | /// Objective-C implementation whose ivars need be initialized. |
| 12495 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12496 | if (!getLangOpts().CPlusPlus) |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12497 | return; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 12498 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 12499 | SmallVector<ObjCIvarDecl*, 8> ivars; |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12500 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 12501 | if (ivars.empty()) |
| 12502 | return; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 12503 | SmallVector<CXXCtorInitializer*, 32> AllToInit; |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12504 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 12505 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 12506 | if (Field->isInvalidDecl()) |
| 12507 | continue; |
| 12508 | |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 12509 | CXXCtorInitializer *Member; |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12510 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 12511 | InitializationKind InitKind = |
| 12512 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 12513 | |
| 12514 | InitializationSequence InitSeq(*this, InitEntity, InitKind, None); |
| 12515 | ExprResult MemberInit = |
| 12516 | InitSeq.Perform(*this, InitEntity, InitKind, None); |
Douglas Gregor | a40433a | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 12517 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12518 | // Note, MemberInit could actually come back empty if no initialization |
| 12519 | // is required (e.g., because it would call a trivial default constructor) |
| 12520 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 12521 | continue; |
John McCall | acf0ee5 | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 12522 | |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12523 | Member = |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 12524 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 12525 | SourceLocation(), |
| 12526 | MemberInit.takeAs<Expr>(), |
| 12527 | SourceLocation()); |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12528 | AllToInit.push_back(Member); |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 12529 | |
| 12530 | // Be sure that the destructor is accessible and is marked as referenced. |
| 12531 | if (const RecordType *RecordTy |
| 12532 | = Context.getBaseElementType(Field->getType()) |
| 12533 | ->getAs<RecordType>()) { |
| 12534 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 12535 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 12536 | MarkFunctionReferenced(Field->getLocation(), Destructor); |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 12537 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 12538 | PDiag(diag::err_access_dtor_ivar) |
| 12539 | << Context.getBaseElementType(Field->getType())); |
| 12540 | } |
| 12541 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12542 | } |
| 12543 | ObjCImplementation->setIvarInitializers(Context, |
| 12544 | AllToInit.data(), AllToInit.size()); |
| 12545 | } |
| 12546 | } |
Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 12547 | |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12548 | static |
| 12549 | void DelegatingCycleHelper(CXXConstructorDecl* Ctor, |
| 12550 | llvm::SmallSet<CXXConstructorDecl*, 4> &Valid, |
| 12551 | llvm::SmallSet<CXXConstructorDecl*, 4> &Invalid, |
| 12552 | llvm::SmallSet<CXXConstructorDecl*, 4> &Current, |
| 12553 | Sema &S) { |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12554 | if (Ctor->isInvalidDecl()) |
| 12555 | return; |
| 12556 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12557 | CXXConstructorDecl *Target = Ctor->getTargetConstructor(); |
| 12558 | |
| 12559 | // Target may not be determinable yet, for instance if this is a dependent |
| 12560 | // call in an uninstantiated template. |
| 12561 | if (Target) { |
| 12562 | const FunctionDecl *FNTarget = 0; |
| 12563 | (void)Target->hasBody(FNTarget); |
| 12564 | Target = const_cast<CXXConstructorDecl*>( |
| 12565 | cast_or_null<CXXConstructorDecl>(FNTarget)); |
| 12566 | } |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12567 | |
| 12568 | CXXConstructorDecl *Canonical = Ctor->getCanonicalDecl(), |
| 12569 | // Avoid dereferencing a null pointer here. |
| 12570 | *TCanonical = Target ? Target->getCanonicalDecl() : 0; |
| 12571 | |
| 12572 | if (!Current.insert(Canonical)) |
| 12573 | return; |
| 12574 | |
| 12575 | // We know that beyond here, we aren't chaining into a cycle. |
| 12576 | if (!Target || !Target->isDelegatingConstructor() || |
| 12577 | Target->isInvalidDecl() || Valid.count(TCanonical)) { |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12578 | Valid.insert(Current.begin(), Current.end()); |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12579 | Current.clear(); |
| 12580 | // We've hit a cycle. |
| 12581 | } else if (TCanonical == Canonical || Invalid.count(TCanonical) || |
| 12582 | Current.count(TCanonical)) { |
| 12583 | // If we haven't diagnosed this cycle yet, do so now. |
| 12584 | if (!Invalid.count(TCanonical)) { |
| 12585 | S.Diag((*Ctor->init_begin())->getSourceLocation(), |
Alexis Hunt | e262299 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 12586 | diag::warn_delegating_ctor_cycle) |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12587 | << Ctor; |
| 12588 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12589 | // Don't add a note for a function delegating directly to itself. |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12590 | if (TCanonical != Canonical) |
| 12591 | S.Diag(Target->getLocation(), diag::note_it_delegates_to); |
| 12592 | |
| 12593 | CXXConstructorDecl *C = Target; |
| 12594 | while (C->getCanonicalDecl() != Canonical) { |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12595 | const FunctionDecl *FNTarget = 0; |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12596 | (void)C->getTargetConstructor()->hasBody(FNTarget); |
| 12597 | assert(FNTarget && "Ctor cycle through bodiless function"); |
| 12598 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12599 | C = const_cast<CXXConstructorDecl*>( |
| 12600 | cast<CXXConstructorDecl>(FNTarget)); |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12601 | S.Diag(C->getLocation(), diag::note_which_delegates_to); |
| 12602 | } |
| 12603 | } |
| 12604 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12605 | Invalid.insert(Current.begin(), Current.end()); |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12606 | Current.clear(); |
| 12607 | } else { |
| 12608 | DelegatingCycleHelper(Target, Valid, Invalid, Current, S); |
| 12609 | } |
| 12610 | } |
| 12611 | |
| 12612 | |
Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 12613 | void Sema::CheckDelegatingCtorCycles() { |
| 12614 | llvm::SmallSet<CXXConstructorDecl*, 4> Valid, Invalid, Current; |
| 12615 | |
Douglas Gregor | bae3120 | 2011-07-27 21:57:17 +0000 | [diff] [blame] | 12616 | for (DelegatingCtorDeclsType::iterator |
| 12617 | I = DelegatingCtorDecls.begin(ExternalSource), |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12618 | E = DelegatingCtorDecls.end(); |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12619 | I != E; ++I) |
| 12620 | DelegatingCycleHelper(*I, Valid, Invalid, Current, *this); |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12621 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12622 | for (llvm::SmallSet<CXXConstructorDecl *, 4>::iterator CI = Invalid.begin(), |
| 12623 | CE = Invalid.end(); |
| 12624 | CI != CE; ++CI) |
Alexis Hunt | 27a761d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12625 | (*CI)->setInvalidDecl(); |
Alexis Hunt | 6118d66 | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 12626 | } |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 12627 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12628 | namespace { |
| 12629 | /// \brief AST visitor that finds references to the 'this' expression. |
| 12630 | class FindCXXThisExpr : public RecursiveASTVisitor<FindCXXThisExpr> { |
| 12631 | Sema &S; |
| 12632 | |
| 12633 | public: |
| 12634 | explicit FindCXXThisExpr(Sema &S) : S(S) { } |
| 12635 | |
| 12636 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 12637 | S.Diag(E->getLocation(), diag::err_this_static_member_func) |
| 12638 | << E->isImplicit(); |
| 12639 | return false; |
| 12640 | } |
| 12641 | }; |
| 12642 | } |
| 12643 | |
| 12644 | bool Sema::checkThisInStaticMemberFunctionType(CXXMethodDecl *Method) { |
| 12645 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 12646 | if (!TSInfo) |
| 12647 | return false; |
| 12648 | |
| 12649 | TypeLoc TL = TSInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12650 | FunctionProtoTypeLoc ProtoTL = TL.getAs<FunctionProtoTypeLoc>(); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12651 | if (!ProtoTL) |
| 12652 | return false; |
| 12653 | |
| 12654 | // C++11 [expr.prim.general]p3: |
| 12655 | // [The expression this] shall not appear before the optional |
| 12656 | // cv-qualifier-seq and it shall not appear within the declaration of a |
| 12657 | // static member function (although its type and value category are defined |
| 12658 | // within a static member function as they are within a non-static member |
| 12659 | // function). [ Note: this is because declaration matching does not occur |
NAKAMURA Takumi | 40edb2a | 2012-04-21 09:40:04 +0000 | [diff] [blame] | 12660 | // until the complete declarator is known. - end note ] |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12661 | const FunctionProtoType *Proto = ProtoTL.getTypePtr(); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12662 | FindCXXThisExpr Finder(*this); |
| 12663 | |
| 12664 | // If the return type came after the cv-qualifier-seq, check it now. |
| 12665 | if (Proto->hasTrailingReturn() && |
Alp Toker | 42a16a6 | 2014-01-25 23:51:36 +0000 | [diff] [blame] | 12666 | !Finder.TraverseTypeLoc(ProtoTL.getReturnLoc())) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12667 | return true; |
| 12668 | |
| 12669 | // Check the exception specification. |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12670 | if (checkThisInStaticMemberFunctionExceptionSpec(Method)) |
| 12671 | return true; |
| 12672 | |
| 12673 | return checkThisInStaticMemberFunctionAttributes(Method); |
| 12674 | } |
| 12675 | |
| 12676 | bool Sema::checkThisInStaticMemberFunctionExceptionSpec(CXXMethodDecl *Method) { |
| 12677 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 12678 | if (!TSInfo) |
| 12679 | return false; |
| 12680 | |
| 12681 | TypeLoc TL = TSInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12682 | FunctionProtoTypeLoc ProtoTL = TL.getAs<FunctionProtoTypeLoc>(); |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12683 | if (!ProtoTL) |
| 12684 | return false; |
| 12685 | |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12686 | const FunctionProtoType *Proto = ProtoTL.getTypePtr(); |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12687 | FindCXXThisExpr Finder(*this); |
| 12688 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12689 | switch (Proto->getExceptionSpecType()) { |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 12690 | case EST_Uninstantiated: |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12691 | case EST_Unevaluated: |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12692 | case EST_BasicNoexcept: |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12693 | case EST_DynamicNone: |
| 12694 | case EST_MSAny: |
| 12695 | case EST_None: |
| 12696 | break; |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12697 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12698 | case EST_ComputedNoexcept: |
| 12699 | if (!Finder.TraverseStmt(Proto->getNoexceptExpr())) |
| 12700 | return true; |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12701 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12702 | case EST_Dynamic: |
| 12703 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12704 | EEnd = Proto->exception_end(); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12705 | E != EEnd; ++E) { |
| 12706 | if (!Finder.TraverseType(*E)) |
| 12707 | return true; |
| 12708 | } |
| 12709 | break; |
| 12710 | } |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12711 | |
| 12712 | return false; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12713 | } |
| 12714 | |
| 12715 | bool Sema::checkThisInStaticMemberFunctionAttributes(CXXMethodDecl *Method) { |
| 12716 | FindCXXThisExpr Finder(*this); |
| 12717 | |
| 12718 | // Check attributes. |
| 12719 | for (Decl::attr_iterator A = Method->attr_begin(), AEnd = Method->attr_end(); |
| 12720 | A != AEnd; ++A) { |
| 12721 | // FIXME: This should be emitted by tblgen. |
| 12722 | Expr *Arg = 0; |
| 12723 | ArrayRef<Expr *> Args; |
| 12724 | if (GuardedByAttr *G = dyn_cast<GuardedByAttr>(*A)) |
| 12725 | Arg = G->getArg(); |
| 12726 | else if (PtGuardedByAttr *G = dyn_cast<PtGuardedByAttr>(*A)) |
| 12727 | Arg = G->getArg(); |
| 12728 | else if (AcquiredAfterAttr *AA = dyn_cast<AcquiredAfterAttr>(*A)) |
| 12729 | Args = ArrayRef<Expr *>(AA->args_begin(), AA->args_size()); |
| 12730 | else if (AcquiredBeforeAttr *AB = dyn_cast<AcquiredBeforeAttr>(*A)) |
| 12731 | Args = ArrayRef<Expr *>(AB->args_begin(), AB->args_size()); |
| 12732 | else if (ExclusiveLockFunctionAttr *ELF |
| 12733 | = dyn_cast<ExclusiveLockFunctionAttr>(*A)) |
| 12734 | Args = ArrayRef<Expr *>(ELF->args_begin(), ELF->args_size()); |
| 12735 | else if (SharedLockFunctionAttr *SLF |
| 12736 | = dyn_cast<SharedLockFunctionAttr>(*A)) |
| 12737 | Args = ArrayRef<Expr *>(SLF->args_begin(), SLF->args_size()); |
| 12738 | else if (ExclusiveTrylockFunctionAttr *ETLF |
| 12739 | = dyn_cast<ExclusiveTrylockFunctionAttr>(*A)) { |
| 12740 | Arg = ETLF->getSuccessValue(); |
| 12741 | Args = ArrayRef<Expr *>(ETLF->args_begin(), ETLF->args_size()); |
| 12742 | } else if (SharedTrylockFunctionAttr *STLF |
| 12743 | = dyn_cast<SharedTrylockFunctionAttr>(*A)) { |
| 12744 | Arg = STLF->getSuccessValue(); |
| 12745 | Args = ArrayRef<Expr *>(STLF->args_begin(), STLF->args_size()); |
| 12746 | } else if (UnlockFunctionAttr *UF = dyn_cast<UnlockFunctionAttr>(*A)) |
| 12747 | Args = ArrayRef<Expr *>(UF->args_begin(), UF->args_size()); |
| 12748 | else if (LockReturnedAttr *LR = dyn_cast<LockReturnedAttr>(*A)) |
| 12749 | Arg = LR->getArg(); |
| 12750 | else if (LocksExcludedAttr *LE = dyn_cast<LocksExcludedAttr>(*A)) |
| 12751 | Args = ArrayRef<Expr *>(LE->args_begin(), LE->args_size()); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame^] | 12752 | else if (RequiresCapabilityAttr *RC |
| 12753 | = dyn_cast<RequiresCapabilityAttr>(*A)) |
| 12754 | Args = ArrayRef<Expr *>(RC->args_begin(), RC->args_size()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12755 | |
| 12756 | if (Arg && !Finder.TraverseStmt(Arg)) |
| 12757 | return true; |
| 12758 | |
| 12759 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 12760 | if (!Finder.TraverseStmt(Args[I])) |
| 12761 | return true; |
| 12762 | } |
| 12763 | } |
| 12764 | |
| 12765 | return false; |
| 12766 | } |
| 12767 | |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12768 | void |
| 12769 | Sema::checkExceptionSpecification(ExceptionSpecificationType EST, |
| 12770 | ArrayRef<ParsedType> DynamicExceptions, |
| 12771 | ArrayRef<SourceRange> DynamicExceptionRanges, |
| 12772 | Expr *NoexceptExpr, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 12773 | SmallVectorImpl<QualType> &Exceptions, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12774 | FunctionProtoType::ExtProtoInfo &EPI) { |
| 12775 | Exceptions.clear(); |
| 12776 | EPI.ExceptionSpecType = EST; |
| 12777 | if (EST == EST_Dynamic) { |
| 12778 | Exceptions.reserve(DynamicExceptions.size()); |
| 12779 | for (unsigned ei = 0, ee = DynamicExceptions.size(); ei != ee; ++ei) { |
| 12780 | // FIXME: Preserve type source info. |
| 12781 | QualType ET = GetTypeFromParser(DynamicExceptions[ei]); |
| 12782 | |
| 12783 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 12784 | collectUnexpandedParameterPacks(ET, Unexpanded); |
| 12785 | if (!Unexpanded.empty()) { |
| 12786 | DiagnoseUnexpandedParameterPacks(DynamicExceptionRanges[ei].getBegin(), |
| 12787 | UPPC_ExceptionType, |
| 12788 | Unexpanded); |
| 12789 | continue; |
| 12790 | } |
| 12791 | |
| 12792 | // Check that the type is valid for an exception spec, and |
| 12793 | // drop it if not. |
| 12794 | if (!CheckSpecifiedExceptionType(ET, DynamicExceptionRanges[ei])) |
| 12795 | Exceptions.push_back(ET); |
| 12796 | } |
| 12797 | EPI.NumExceptions = Exceptions.size(); |
| 12798 | EPI.Exceptions = Exceptions.data(); |
| 12799 | return; |
| 12800 | } |
| 12801 | |
| 12802 | if (EST == EST_ComputedNoexcept) { |
| 12803 | // If an error occurred, there's no expression here. |
| 12804 | if (NoexceptExpr) { |
| 12805 | assert((NoexceptExpr->isTypeDependent() || |
| 12806 | NoexceptExpr->getType()->getCanonicalTypeUnqualified() == |
| 12807 | Context.BoolTy) && |
| 12808 | "Parser should have made sure that the expression is boolean"); |
| 12809 | if (NoexceptExpr && DiagnoseUnexpandedParameterPack(NoexceptExpr)) { |
| 12810 | EPI.ExceptionSpecType = EST_BasicNoexcept; |
| 12811 | return; |
| 12812 | } |
| 12813 | |
| 12814 | if (!NoexceptExpr->isValueDependent()) |
| 12815 | NoexceptExpr = VerifyIntegerConstantExpression(NoexceptExpr, 0, |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 12816 | diag::err_noexcept_needs_constant_expression, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12817 | /*AllowFold*/ false).take(); |
| 12818 | EPI.NoexceptExpr = NoexceptExpr; |
| 12819 | } |
| 12820 | return; |
| 12821 | } |
| 12822 | } |
| 12823 | |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 12824 | /// IdentifyCUDATarget - Determine the CUDA compilation target for this function |
| 12825 | Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const FunctionDecl *D) { |
| 12826 | // Implicitly declared functions (e.g. copy constructors) are |
| 12827 | // __host__ __device__ |
| 12828 | if (D->isImplicit()) |
| 12829 | return CFT_HostDevice; |
| 12830 | |
| 12831 | if (D->hasAttr<CUDAGlobalAttr>()) |
| 12832 | return CFT_Global; |
| 12833 | |
| 12834 | if (D->hasAttr<CUDADeviceAttr>()) { |
| 12835 | if (D->hasAttr<CUDAHostAttr>()) |
| 12836 | return CFT_HostDevice; |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12837 | return CFT_Device; |
Peter Collingbourne | 7277fe8 | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 12838 | } |
| 12839 | |
| 12840 | return CFT_Host; |
| 12841 | } |
| 12842 | |
| 12843 | bool Sema::CheckCUDATarget(CUDAFunctionTarget CallerTarget, |
| 12844 | CUDAFunctionTarget CalleeTarget) { |
| 12845 | // CUDA B.1.1 "The __device__ qualifier declares a function that is... |
| 12846 | // Callable from the device only." |
| 12847 | if (CallerTarget == CFT_Host && CalleeTarget == CFT_Device) |
| 12848 | return true; |
| 12849 | |
| 12850 | // CUDA B.1.2 "The __global__ qualifier declares a function that is... |
| 12851 | // Callable from the host only." |
| 12852 | // CUDA B.1.3 "The __host__ qualifier declares a function that is... |
| 12853 | // Callable from the host only." |
| 12854 | if ((CallerTarget == CFT_Device || CallerTarget == CFT_Global) && |
| 12855 | (CalleeTarget == CFT_Host || CalleeTarget == CFT_Global)) |
| 12856 | return true; |
| 12857 | |
| 12858 | if (CallerTarget == CFT_HostDevice && CalleeTarget != CFT_HostDevice) |
| 12859 | return true; |
| 12860 | |
| 12861 | return false; |
| 12862 | } |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 12863 | |
| 12864 | /// HandleMSProperty - Analyze a __delcspec(property) field of a C++ class. |
| 12865 | /// |
| 12866 | MSPropertyDecl *Sema::HandleMSProperty(Scope *S, RecordDecl *Record, |
| 12867 | SourceLocation DeclStart, |
| 12868 | Declarator &D, Expr *BitWidth, |
| 12869 | InClassInitStyle InitStyle, |
| 12870 | AccessSpecifier AS, |
| 12871 | AttributeList *MSPropertyAttr) { |
| 12872 | IdentifierInfo *II = D.getIdentifier(); |
| 12873 | if (!II) { |
| 12874 | Diag(DeclStart, diag::err_anonymous_property); |
| 12875 | return NULL; |
| 12876 | } |
| 12877 | SourceLocation Loc = D.getIdentifierLoc(); |
| 12878 | |
| 12879 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 12880 | QualType T = TInfo->getType(); |
| 12881 | if (getLangOpts().CPlusPlus) { |
| 12882 | CheckExtraCXXDefaultArguments(D); |
| 12883 | |
| 12884 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 12885 | UPPC_DataMemberType)) { |
| 12886 | D.setInvalidType(); |
| 12887 | T = Context.IntTy; |
| 12888 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc); |
| 12889 | } |
| 12890 | } |
| 12891 | |
| 12892 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
| 12893 | |
| 12894 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 12895 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 12896 | diag::err_invalid_thread) |
| 12897 | << DeclSpec::getSpecifierName(TSCS); |
| 12898 | |
| 12899 | // Check to see if this name was declared as a member previously |
| 12900 | NamedDecl *PrevDecl = 0; |
| 12901 | LookupResult Previous(*this, II, Loc, LookupMemberName, ForRedeclaration); |
| 12902 | LookupName(Previous, S); |
| 12903 | switch (Previous.getResultKind()) { |
| 12904 | case LookupResult::Found: |
| 12905 | case LookupResult::FoundUnresolvedValue: |
| 12906 | PrevDecl = Previous.getAsSingle<NamedDecl>(); |
| 12907 | break; |
| 12908 | |
| 12909 | case LookupResult::FoundOverloaded: |
| 12910 | PrevDecl = Previous.getRepresentativeDecl(); |
| 12911 | break; |
| 12912 | |
| 12913 | case LookupResult::NotFound: |
| 12914 | case LookupResult::NotFoundInCurrentInstantiation: |
| 12915 | case LookupResult::Ambiguous: |
| 12916 | break; |
| 12917 | } |
| 12918 | |
| 12919 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 12920 | // Maybe we will complain about the shadowed template parameter. |
| 12921 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 12922 | // Just pretend that we didn't see the previous declaration. |
| 12923 | PrevDecl = 0; |
| 12924 | } |
| 12925 | |
| 12926 | if (PrevDecl && !isDeclInScope(PrevDecl, Record, S)) |
| 12927 | PrevDecl = 0; |
| 12928 | |
| 12929 | SourceLocation TSSL = D.getLocStart(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 12930 | const AttributeList::PropertyData &Data = MSPropertyAttr->getPropertyData(); |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 12931 | MSPropertyDecl *NewPD = MSPropertyDecl::Create( |
| 12932 | Context, Record, Loc, II, T, TInfo, TSSL, Data.GetterId, Data.SetterId); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 12933 | ProcessDeclAttributes(TUScope, NewPD, D); |
| 12934 | NewPD->setAccess(AS); |
| 12935 | |
| 12936 | if (NewPD->isInvalidDecl()) |
| 12937 | Record->setInvalidDecl(); |
| 12938 | |
| 12939 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 12940 | NewPD->setModulePrivate(); |
| 12941 | |
| 12942 | if (NewPD->isInvalidDecl() && PrevDecl) { |
| 12943 | // Don't introduce NewFD into scope; there's already something |
| 12944 | // with the same name in the same scope. |
| 12945 | } else if (II) { |
| 12946 | PushOnScopeChains(NewPD, S); |
| 12947 | } else |
| 12948 | Record->addDecl(NewPD); |
| 12949 | |
| 12950 | return NewPD; |
| 12951 | } |