Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 1 | //===------ SemaDeclCXX.cpp - Semantic Analysis for C++ Declarations ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/CharUnits.h" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclVisitor.h" |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 21 | #include "clang/AST/EvaluatedExprVisitor.h" |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecordLayout.h" |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 30 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/Sema/CXXFieldCollector.h" |
| 32 | #include "clang/Sema/DeclSpec.h" |
| 33 | #include "clang/Sema/Initialization.h" |
| 34 | #include "clang/Sema/Lookup.h" |
| 35 | #include "clang/Sema/ParsedTemplate.h" |
| 36 | #include "clang/Sema/Scope.h" |
| 37 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 40 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 41 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 42 | |
| 43 | using namespace clang; |
| 44 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
| 46 | // CheckDefaultArgumentVisitor |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 49 | namespace { |
| 50 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 51 | /// the default argument of a parameter to determine whether it |
| 52 | /// contains any ill-formed subexpressions. For example, this will |
| 53 | /// diagnose the use of local variables or parameters within the |
| 54 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 55 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 56 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 57 | Expr *DefaultArg; |
| 58 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 60 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 62 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 64 | bool VisitExpr(Expr *Node); |
| 65 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 66 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Douglas Gregor | f0459f8 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 67 | bool VisitLambdaExpr(LambdaExpr *Lambda); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 68 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 70 | /// VisitExpr - Visit all of the children of this expression. |
| 71 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 72 | bool IsInvalid = false; |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 73 | for (Stmt::child_range I = Node->children(); I; ++I) |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 74 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 75 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 78 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 79 | /// determine whether this declaration can be used in the default |
| 80 | /// argument expression. |
| 81 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 82 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 83 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 84 | // C++ [dcl.fct.default]p9 |
| 85 | // Default arguments are evaluated each time the function is |
| 86 | // called. The order of evaluation of function arguments is |
| 87 | // unspecified. Consequently, parameters of a function shall not |
| 88 | // be used in default argument expressions, even if they are not |
| 89 | // evaluated. Parameters of a function declared before a default |
| 90 | // argument expression are in scope and can hide namespace and |
| 91 | // class member names. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 92 | return S->Diag(DRE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 93 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 94 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 95 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 96 | // C++ [dcl.fct.default]p7 |
| 97 | // Local variables shall not be used in default argument |
| 98 | // expressions. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 99 | if (VDecl->isLocalVarDecl()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 100 | return S->Diag(DRE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 101 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 102 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 103 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 104 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 105 | return false; |
| 106 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 107 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 108 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 109 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 110 | // C++ [dcl.fct.default]p8: |
| 111 | // The keyword this shall not be used in a default argument of a |
| 112 | // member function. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 113 | return S->Diag(ThisE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 114 | diag::err_param_default_argument_references_this) |
| 115 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 116 | } |
Douglas Gregor | f0459f8 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 117 | |
| 118 | bool CheckDefaultArgumentVisitor::VisitLambdaExpr(LambdaExpr *Lambda) { |
| 119 | // C++11 [expr.lambda.prim]p13: |
| 120 | // A lambda-expression appearing in a default argument shall not |
| 121 | // implicitly or explicitly capture any entity. |
| 122 | if (Lambda->capture_begin() == Lambda->capture_end()) |
| 123 | return false; |
| 124 | |
| 125 | return S->Diag(Lambda->getLocStart(), |
| 126 | diag::err_lambda_capture_default_arg); |
| 127 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 130 | void Sema::ImplicitExceptionSpecification::CalledDecl(SourceLocation CallLoc, |
| 131 | CXXMethodDecl *Method) { |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 132 | // If we have an MSAny spec already, don't bother. |
| 133 | if (!Method || ComputedEST == EST_MSAny) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 134 | return; |
| 135 | |
| 136 | const FunctionProtoType *Proto |
| 137 | = Method->getType()->getAs<FunctionProtoType>(); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 138 | Proto = Self->ResolveExceptionSpec(CallLoc, Proto); |
| 139 | if (!Proto) |
| 140 | return; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 141 | |
| 142 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
| 143 | |
| 144 | // If this function can throw any exceptions, make a note of that. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 145 | if (EST == EST_MSAny || EST == EST_None) { |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 146 | ClearExceptions(); |
| 147 | ComputedEST = EST; |
| 148 | return; |
| 149 | } |
| 150 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 151 | // FIXME: If the call to this decl is using any of its default arguments, we |
| 152 | // need to search them for potentially-throwing calls. |
| 153 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 154 | // If this function has a basic noexcept, it doesn't affect the outcome. |
| 155 | if (EST == EST_BasicNoexcept) |
| 156 | return; |
| 157 | |
| 158 | // If we have a throw-all spec at this point, ignore the function. |
| 159 | if (ComputedEST == EST_None) |
| 160 | return; |
| 161 | |
| 162 | // If we're still at noexcept(true) and there's a nothrow() callee, |
| 163 | // change to that specification. |
| 164 | if (EST == EST_DynamicNone) { |
| 165 | if (ComputedEST == EST_BasicNoexcept) |
| 166 | ComputedEST = EST_DynamicNone; |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | // Check out noexcept specs. |
| 171 | if (EST == EST_ComputedNoexcept) { |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 172 | FunctionProtoType::NoexceptResult NR = |
| 173 | Proto->getNoexceptSpec(Self->Context); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 174 | assert(NR != FunctionProtoType::NR_NoNoexcept && |
| 175 | "Must have noexcept result for EST_ComputedNoexcept."); |
| 176 | assert(NR != FunctionProtoType::NR_Dependent && |
| 177 | "Should not generate implicit declarations for dependent cases, " |
| 178 | "and don't know how to handle them anyway."); |
| 179 | |
| 180 | // noexcept(false) -> no spec on the new function |
| 181 | if (NR == FunctionProtoType::NR_Throw) { |
| 182 | ClearExceptions(); |
| 183 | ComputedEST = EST_None; |
| 184 | } |
| 185 | // noexcept(true) won't change anything either. |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | assert(EST == EST_Dynamic && "EST case not considered earlier."); |
| 190 | assert(ComputedEST != EST_None && |
| 191 | "Shouldn't collect exceptions when throw-all is guaranteed."); |
| 192 | ComputedEST = EST_Dynamic; |
| 193 | // Record the exceptions in this function's exception specification. |
| 194 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 195 | EEnd = Proto->exception_end(); |
| 196 | E != EEnd; ++E) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 197 | if (ExceptionsSeen.insert(Self->Context.getCanonicalType(*E))) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 198 | Exceptions.push_back(*E); |
| 199 | } |
| 200 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 201 | void Sema::ImplicitExceptionSpecification::CalledExpr(Expr *E) { |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 202 | if (!E || ComputedEST == EST_MSAny) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 203 | return; |
| 204 | |
| 205 | // FIXME: |
| 206 | // |
| 207 | // C++0x [except.spec]p14: |
NAKAMURA Takumi | 4857947 | 2011-06-21 03:19:28 +0000 | [diff] [blame] | 208 | // [An] implicit exception-specification specifies the type-id T if and |
| 209 | // only if T is allowed by the exception-specification of a function directly |
| 210 | // invoked by f's implicit definition; f shall allow all exceptions if any |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 211 | // function it directly invokes allows all exceptions, and f shall allow no |
| 212 | // exceptions if every function it directly invokes allows no exceptions. |
| 213 | // |
| 214 | // Note in particular that if an implicit exception-specification is generated |
| 215 | // for a function containing a throw-expression, that specification can still |
| 216 | // be noexcept(true). |
| 217 | // |
| 218 | // Note also that 'directly invoked' is not defined in the standard, and there |
| 219 | // is no indication that we should only consider potentially-evaluated calls. |
| 220 | // |
| 221 | // Ultimately we should implement the intent of the standard: the exception |
| 222 | // specification should be the set of exceptions which can be thrown by the |
| 223 | // implicit definition. For now, we assume that any non-nothrow expression can |
| 224 | // throw any exception. |
| 225 | |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 226 | if (Self->canThrow(E)) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 227 | ComputedEST = EST_None; |
| 228 | } |
| 229 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 230 | bool |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 231 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | SourceLocation EqualLoc) { |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 233 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 234 | diag::err_typecheck_decl_incomplete_type)) { |
| 235 | Param->setInvalidDecl(); |
| 236 | return true; |
| 237 | } |
| 238 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 239 | // C++ [dcl.fct.default]p5 |
| 240 | // A default argument expression is implicitly converted (clause |
| 241 | // 4) to the parameter type. The default argument expression has |
| 242 | // the same semantic constraints as the initializer expression in |
| 243 | // a declaration of a variable of the parameter type, using the |
| 244 | // copy-initialization semantics (8.5). |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 245 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 246 | Param); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 247 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 248 | EqualLoc); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 249 | InitializationSequence InitSeq(*this, Entity, Kind, &Arg, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 250 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Arg); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 251 | if (Result.isInvalid()) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 252 | return true; |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 253 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 254 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 255 | CheckImplicitConversions(Arg, EqualLoc); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 256 | Arg = MaybeCreateExprWithCleanups(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 258 | // Okay: add the default argument to the parameter |
| 259 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | |
Douglas Gregor | 8cfb7a3 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 261 | // We have already instantiated this parameter; provide each of the |
| 262 | // instantiations with the uninstantiated default argument. |
| 263 | UnparsedDefaultArgInstantiationsMap::iterator InstPos |
| 264 | = UnparsedDefaultArgInstantiations.find(Param); |
| 265 | if (InstPos != UnparsedDefaultArgInstantiations.end()) { |
| 266 | for (unsigned I = 0, N = InstPos->second.size(); I != N; ++I) |
| 267 | InstPos->second[I]->setUninstantiatedDefaultArg(Arg); |
| 268 | |
| 269 | // We're done tracking this parameter's instantiations. |
| 270 | UnparsedDefaultArgInstantiations.erase(InstPos); |
| 271 | } |
| 272 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 273 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 276 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 277 | /// provided for a function parameter is well-formed. If so, attach it |
| 278 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 279 | void |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 280 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 281 | Expr *DefaultArg) { |
| 282 | if (!param || !DefaultArg) |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 283 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 285 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 286 | UnparsedDefaultArgLocs.erase(Param); |
| 287 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 288 | // Default arguments are only permitted in C++ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 289 | if (!getLangOpts().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 290 | Diag(EqualLoc, diag::err_param_default_argument) |
| 291 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 292 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 296 | // Check for unexpanded parameter packs. |
| 297 | if (DiagnoseUnexpandedParameterPack(DefaultArg, UPPC_DefaultArgument)) { |
| 298 | Param->setInvalidDecl(); |
| 299 | return; |
| 300 | } |
| 301 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 302 | // Check that the default argument is well-formed |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 303 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 304 | if (DefaultArgChecker.Visit(DefaultArg)) { |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 305 | Param->setInvalidDecl(); |
| 306 | return; |
| 307 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 309 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 312 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 313 | /// argument for a function parameter, but we can't parse it yet |
| 314 | /// because we're inside a class definition. Note that this default |
| 315 | /// argument will be parsed later. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 316 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 317 | SourceLocation EqualLoc, |
| 318 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 319 | if (!param) |
| 320 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 322 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 323 | if (Param) |
| 324 | Param->setUnparsedDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 326 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 329 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 330 | /// the default argument for the parameter param failed. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 331 | void Sema::ActOnParamDefaultArgumentError(Decl *param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 332 | if (!param) |
| 333 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 335 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 337 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 339 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 342 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 343 | /// arguments in the declarator, which is not a function declaration |
| 344 | /// or definition and therefore is not permitted to have default |
| 345 | /// arguments. This routine should be invoked for every declarator |
| 346 | /// that is not a function declaration or definition. |
| 347 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 348 | // C++ [dcl.fct.default]p3 |
| 349 | // A default argument expression shall be specified only in the |
| 350 | // parameter-declaration-clause of a function declaration or in a |
| 351 | // template-parameter (14.1). It shall not be specified for a |
| 352 | // parameter pack. If it is specified in a |
| 353 | // parameter-declaration-clause, it shall not occur within a |
| 354 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 355 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 356 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 357 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 358 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 359 | ParmVarDecl *Param = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 360 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 361 | if (Param->hasUnparsedDefaultArg()) { |
| 362 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 363 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 364 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 365 | delete Toks; |
| 366 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 367 | } else if (Param->getDefaultArg()) { |
| 368 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 369 | << Param->getDefaultArg()->getSourceRange(); |
| 370 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
Craig Topper | 1a6eac8 | 2012-09-21 04:33:26 +0000 | [diff] [blame] | 377 | /// MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 378 | /// function, once we already know that they have the same |
| 379 | /// type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 380 | /// error, false otherwise. |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 381 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old, |
| 382 | Scope *S) { |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 383 | bool Invalid = false; |
| 384 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 385 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 386 | // For non-template functions, default arguments can be added in |
| 387 | // later declarations of a function in the same |
| 388 | // scope. Declarations in different scopes have completely |
| 389 | // distinct sets of default arguments. That is, declarations in |
| 390 | // inner scopes do not acquire default arguments from |
| 391 | // declarations in outer scopes, and vice versa. In a given |
| 392 | // function declaration, all parameters subsequent to a |
| 393 | // parameter with a default argument shall have default |
| 394 | // arguments supplied in this or previous declarations. A |
| 395 | // default argument shall not be redefined by a later |
| 396 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 397 | // |
| 398 | // C++ [dcl.fct.default]p6: |
| 399 | // Except for member functions of class templates, the default arguments |
| 400 | // in a member function definition that appears outside of the class |
| 401 | // definition are added to the set of default arguments provided by the |
| 402 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 403 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 404 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 405 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 406 | |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 407 | bool OldParamHasDfl = OldParam->hasDefaultArg(); |
| 408 | bool NewParamHasDfl = NewParam->hasDefaultArg(); |
| 409 | |
| 410 | NamedDecl *ND = Old; |
| 411 | if (S && !isDeclInScope(ND, New->getDeclContext(), S)) |
| 412 | // Ignore default parameters of old decl if they are not in |
| 413 | // the same scope. |
| 414 | OldParamHasDfl = false; |
| 415 | |
| 416 | if (OldParamHasDfl && NewParamHasDfl) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 417 | |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 418 | unsigned DiagDefaultParamID = |
| 419 | diag::err_param_default_argument_redefinition; |
| 420 | |
| 421 | // MSVC accepts that default parameters be redefined for member functions |
| 422 | // of template class. The new default parameter's value is ignored. |
| 423 | Invalid = true; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 424 | if (getLangOpts().MicrosoftExt) { |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 425 | CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(New); |
| 426 | if (MD && MD->getParent()->getDescribedClassTemplate()) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 427 | // Merge the old default argument into the new parameter. |
| 428 | NewParam->setHasInheritedDefaultArg(); |
| 429 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 430 | NewParam->setUninstantiatedDefaultArg( |
| 431 | OldParam->getUninstantiatedDefaultArg()); |
| 432 | else |
| 433 | NewParam->setDefaultArg(OldParam->getInit()); |
Francois Pichet | cf320c6 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 434 | DiagDefaultParamID = diag::warn_param_default_argument_redefinition; |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 435 | Invalid = false; |
| 436 | } |
| 437 | } |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 438 | |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 439 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 440 | // hint here. Alternatively, we could walk the type-source information |
| 441 | // for NewParam to find the last source location in the type... but it |
| 442 | // isn't worth the effort right now. This is the kind of test case that |
| 443 | // is hard to get right: |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 444 | // int f(int); |
| 445 | // void g(int (*fp)(int) = f); |
| 446 | // void g(int (*fp)(int) = &f); |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 447 | Diag(NewParam->getLocation(), DiagDefaultParamID) |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 448 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 449 | |
| 450 | // Look for the function declaration where the default argument was |
| 451 | // actually written, which may be a declaration prior to Old. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 452 | for (FunctionDecl *Older = Old->getPreviousDecl(); |
| 453 | Older; Older = Older->getPreviousDecl()) { |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 454 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 455 | break; |
| 456 | |
| 457 | OldParam = Older->getParamDecl(p); |
| 458 | } |
| 459 | |
| 460 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 461 | << OldParam->getDefaultArgRange(); |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 462 | } else if (OldParamHasDfl) { |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 463 | // Merge the old default argument into the new parameter. |
| 464 | // It's important to use getInit() here; getDefaultArg() |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 465 | // strips off any top-level ExprWithCleanups. |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 466 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 467 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 468 | NewParam->setUninstantiatedDefaultArg( |
| 469 | OldParam->getUninstantiatedDefaultArg()); |
| 470 | else |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 471 | NewParam->setDefaultArg(OldParam->getInit()); |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 472 | } else if (NewParamHasDfl) { |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 473 | if (New->getDescribedFunctionTemplate()) { |
| 474 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 475 | Diag(NewParam->getLocation(), |
| 476 | diag::err_param_default_argument_template_redecl) |
| 477 | << NewParam->getDefaultArgRange(); |
| 478 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 479 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 480 | } else if (New->getTemplateSpecializationKind() |
| 481 | != TSK_ImplicitInstantiation && |
| 482 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 483 | // C++ [temp.expr.spec]p21: |
| 484 | // Default function arguments shall not be specified in a declaration |
| 485 | // or a definition for one of the following explicit specializations: |
| 486 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 487 | // - the explicit specialization of a member function template; |
| 488 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 489 | // template where the class template specialization to which the |
| 490 | // member function specialization belongs is implicitly |
| 491 | // instantiated. |
| 492 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 493 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 494 | << New->getDeclName() |
| 495 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 496 | } else if (New->getDeclContext()->isDependentContext()) { |
| 497 | // C++ [dcl.fct.default]p6 (DR217): |
| 498 | // Default arguments for a member function of a class template shall |
| 499 | // be specified on the initial declaration of the member function |
| 500 | // within the class template. |
| 501 | // |
| 502 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 503 | // leads me to the conclusion that one cannot add default function |
| 504 | // arguments for an out-of-line definition of a member function of a |
| 505 | // dependent type. |
| 506 | int WhichKind = 2; |
| 507 | if (CXXRecordDecl *Record |
| 508 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 509 | if (Record->getDescribedClassTemplate()) |
| 510 | WhichKind = 0; |
| 511 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 512 | WhichKind = 1; |
| 513 | else |
| 514 | WhichKind = 2; |
| 515 | } |
| 516 | |
| 517 | Diag(NewParam->getLocation(), |
| 518 | diag::err_param_default_argument_member_template_redecl) |
| 519 | << WhichKind |
| 520 | << NewParam->getDefaultArgRange(); |
| 521 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
Richard Smith | b8abff6 | 2012-11-28 03:45:24 +0000 | [diff] [blame] | 525 | // DR1344: If a default argument is added outside a class definition and that |
| 526 | // default argument makes the function a special member function, the program |
| 527 | // is ill-formed. This can only happen for constructors. |
| 528 | if (isa<CXXConstructorDecl>(New) && |
| 529 | New->getMinRequiredArguments() < Old->getMinRequiredArguments()) { |
| 530 | CXXSpecialMember NewSM = getSpecialMember(cast<CXXMethodDecl>(New)), |
| 531 | OldSM = getSpecialMember(cast<CXXMethodDecl>(Old)); |
| 532 | if (NewSM != OldSM) { |
| 533 | ParmVarDecl *NewParam = New->getParamDecl(New->getMinRequiredArguments()); |
| 534 | assert(NewParam->hasDefaultArg()); |
| 535 | Diag(NewParam->getLocation(), diag::err_default_arg_makes_ctor_special) |
| 536 | << NewParam->getDefaultArgRange() << NewSM; |
| 537 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 538 | } |
| 539 | } |
| 540 | |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 541 | // C++11 [dcl.constexpr]p1: If any declaration of a function or function |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 542 | // template has a constexpr specifier then all its declarations shall |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 543 | // contain the constexpr specifier. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 544 | if (New->isConstexpr() != Old->isConstexpr()) { |
| 545 | Diag(New->getLocation(), diag::err_constexpr_redecl_mismatch) |
| 546 | << New << New->isConstexpr(); |
| 547 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 548 | Invalid = true; |
| 549 | } |
| 550 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 551 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 552 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 553 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 554 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 557 | /// \brief Merge the exception specifications of two variable declarations. |
| 558 | /// |
| 559 | /// This is called when there's a redeclaration of a VarDecl. The function |
| 560 | /// checks if the redeclaration might have an exception specification and |
| 561 | /// validates compatibility and merges the specs if necessary. |
| 562 | void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) { |
| 563 | // Shortcut if exceptions are disabled. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 564 | if (!getLangOpts().CXXExceptions) |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 565 | return; |
| 566 | |
| 567 | assert(Context.hasSameType(New->getType(), Old->getType()) && |
| 568 | "Should only be called if types are otherwise the same."); |
| 569 | |
| 570 | QualType NewType = New->getType(); |
| 571 | QualType OldType = Old->getType(); |
| 572 | |
| 573 | // We're only interested in pointers and references to functions, as well |
| 574 | // as pointers to member functions. |
| 575 | if (const ReferenceType *R = NewType->getAs<ReferenceType>()) { |
| 576 | NewType = R->getPointeeType(); |
| 577 | OldType = OldType->getAs<ReferenceType>()->getPointeeType(); |
| 578 | } else if (const PointerType *P = NewType->getAs<PointerType>()) { |
| 579 | NewType = P->getPointeeType(); |
| 580 | OldType = OldType->getAs<PointerType>()->getPointeeType(); |
| 581 | } else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) { |
| 582 | NewType = M->getPointeeType(); |
| 583 | OldType = OldType->getAs<MemberPointerType>()->getPointeeType(); |
| 584 | } |
| 585 | |
| 586 | if (!NewType->isFunctionProtoType()) |
| 587 | return; |
| 588 | |
| 589 | // There's lots of special cases for functions. For function pointers, system |
| 590 | // libraries are hopefully not as broken so that we don't need these |
| 591 | // workarounds. |
| 592 | if (CheckEquivalentExceptionSpec( |
| 593 | OldType->getAs<FunctionProtoType>(), Old->getLocation(), |
| 594 | NewType->getAs<FunctionProtoType>(), New->getLocation())) { |
| 595 | New->setInvalidDecl(); |
| 596 | } |
| 597 | } |
| 598 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 599 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 600 | /// function declaration are well-formed according to C++ |
| 601 | /// [dcl.fct.default]. |
| 602 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 603 | unsigned NumParams = FD->getNumParams(); |
| 604 | unsigned p; |
| 605 | |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 606 | bool IsLambda = FD->getOverloadedOperator() == OO_Call && |
| 607 | isa<CXXMethodDecl>(FD) && |
| 608 | cast<CXXMethodDecl>(FD)->getParent()->isLambda(); |
| 609 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 610 | // Find first parameter with a default argument |
| 611 | for (p = 0; p < NumParams; ++p) { |
| 612 | ParmVarDecl *Param = FD->getParamDecl(p); |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 613 | if (Param->hasDefaultArg()) { |
| 614 | // C++11 [expr.prim.lambda]p5: |
| 615 | // [...] Default arguments (8.3.6) shall not be specified in the |
| 616 | // parameter-declaration-clause of a lambda-declarator. |
| 617 | // |
| 618 | // FIXME: Core issue 974 strikes this sentence, we only provide an |
| 619 | // extension warning. |
| 620 | if (IsLambda) |
| 621 | Diag(Param->getLocation(), diag::ext_lambda_default_arguments) |
| 622 | << Param->getDefaultArgRange(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 623 | break; |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 624 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | // C++ [dcl.fct.default]p4: |
| 628 | // In a given function declaration, all parameters |
| 629 | // subsequent to a parameter with a default argument shall |
| 630 | // have default arguments supplied in this or previous |
| 631 | // declarations. A default argument shall not be redefined |
| 632 | // by a later declaration (not even to the same value). |
| 633 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 635 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 636 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 637 | if (Param->isInvalidDecl()) |
| 638 | /* We already complained about this parameter. */; |
| 639 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 641 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 642 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 643 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 645 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 647 | LastMissingDefaultArg = p; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | if (LastMissingDefaultArg > 0) { |
| 652 | // Some default arguments were missing. Clear out all of the |
| 653 | // default arguments up to (and including) the last missing |
| 654 | // default argument, so that we leave the function parameters |
| 655 | // in a semantically valid state. |
| 656 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 657 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 658 | if (Param->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 659 | Param->setDefaultArg(0); |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 664 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 665 | // CheckConstexprParameterTypes - Check whether a function's parameter types |
| 666 | // are all literal types. If so, return true. If not, produce a suitable |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 667 | // diagnostic and return false. |
| 668 | static bool CheckConstexprParameterTypes(Sema &SemaRef, |
| 669 | const FunctionDecl *FD) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 670 | unsigned ArgIndex = 0; |
| 671 | const FunctionProtoType *FT = FD->getType()->getAs<FunctionProtoType>(); |
| 672 | for (FunctionProtoType::arg_type_iterator i = FT->arg_type_begin(), |
| 673 | e = FT->arg_type_end(); i != e; ++i, ++ArgIndex) { |
| 674 | const ParmVarDecl *PD = FD->getParamDecl(ArgIndex); |
| 675 | SourceLocation ParamLoc = PD->getLocation(); |
| 676 | if (!(*i)->isDependentType() && |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 677 | SemaRef.RequireLiteralType(ParamLoc, *i, |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 678 | diag::err_constexpr_non_literal_param, |
| 679 | ArgIndex+1, PD->getSourceRange(), |
| 680 | isa<CXXConstructorDecl>(FD))) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 681 | return false; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 682 | } |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 683 | return true; |
| 684 | } |
| 685 | |
| 686 | /// \brief Get diagnostic %select index for tag kind for |
| 687 | /// record diagnostic message. |
| 688 | /// WARNING: Indexes apply to particular diagnostics only! |
| 689 | /// |
| 690 | /// \returns diagnostic %select index. |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 691 | static unsigned getRecordDiagFromTagKind(TagTypeKind Tag) { |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 692 | switch (Tag) { |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 693 | case TTK_Struct: return 0; |
| 694 | case TTK_Interface: return 1; |
| 695 | case TTK_Class: return 2; |
| 696 | default: llvm_unreachable("Invalid tag kind for record diagnostic!"); |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 697 | } |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | // CheckConstexprFunctionDecl - Check whether a function declaration satisfies |
| 701 | // the requirements of a constexpr function definition or a constexpr |
| 702 | // constructor definition. If so, return true. If not, produce appropriate |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 703 | // diagnostics and return false. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 704 | // |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 705 | // This implements C++11 [dcl.constexpr]p3,4, as amended by DR1360. |
| 706 | bool Sema::CheckConstexprFunctionDecl(const FunctionDecl *NewFD) { |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 707 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| 708 | if (MD && MD->isInstance()) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 709 | // C++11 [dcl.constexpr]p4: |
| 710 | // The definition of a constexpr constructor shall satisfy the following |
| 711 | // constraints: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 712 | // - the class shall not have any virtual base classes; |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 713 | const CXXRecordDecl *RD = MD->getParent(); |
| 714 | if (RD->getNumVBases()) { |
| 715 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual_base) |
| 716 | << isa<CXXConstructorDecl>(NewFD) |
| 717 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases(); |
| 718 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 719 | E = RD->vbases_end(); I != E; ++I) |
| 720 | Diag(I->getLocStart(), |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 721 | diag::note_constexpr_virtual_base_here) << I->getSourceRange(); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 722 | return false; |
| 723 | } |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | if (!isa<CXXConstructorDecl>(NewFD)) { |
| 727 | // C++11 [dcl.constexpr]p3: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 728 | // The definition of a constexpr function shall satisfy the following |
| 729 | // constraints: |
| 730 | // - it shall not be virtual; |
| 731 | const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD); |
| 732 | if (Method && Method->isVirtual()) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 733 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 734 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 735 | // If it's not obvious why this function is virtual, find an overridden |
| 736 | // function which uses the 'virtual' keyword. |
| 737 | const CXXMethodDecl *WrittenVirtual = Method; |
| 738 | while (!WrittenVirtual->isVirtualAsWritten()) |
| 739 | WrittenVirtual = *WrittenVirtual->begin_overridden_methods(); |
| 740 | if (WrittenVirtual != Method) |
| 741 | Diag(WrittenVirtual->getLocation(), |
| 742 | diag::note_overridden_virtual_function); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 743 | return false; |
| 744 | } |
| 745 | |
| 746 | // - its return type shall be a literal type; |
| 747 | QualType RT = NewFD->getResultType(); |
| 748 | if (!RT->isDependentType() && |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 749 | RequireLiteralType(NewFD->getLocation(), RT, |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 750 | diag::err_constexpr_non_literal_return)) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 751 | return false; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 754 | // - each of its parameter types shall be a literal type; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 755 | if (!CheckConstexprParameterTypes(*this, NewFD)) |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 756 | return false; |
| 757 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 758 | return true; |
| 759 | } |
| 760 | |
| 761 | /// Check the given declaration statement is legal within a constexpr function |
| 762 | /// body. C++0x [dcl.constexpr]p3,p4. |
| 763 | /// |
| 764 | /// \return true if the body is OK, false if we have diagnosed a problem. |
| 765 | static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl, |
| 766 | DeclStmt *DS) { |
| 767 | // C++0x [dcl.constexpr]p3 and p4: |
| 768 | // The definition of a constexpr function(p3) or constructor(p4) [...] shall |
| 769 | // contain only |
| 770 | for (DeclStmt::decl_iterator DclIt = DS->decl_begin(), |
| 771 | DclEnd = DS->decl_end(); DclIt != DclEnd; ++DclIt) { |
| 772 | switch ((*DclIt)->getKind()) { |
| 773 | case Decl::StaticAssert: |
| 774 | case Decl::Using: |
| 775 | case Decl::UsingShadow: |
| 776 | case Decl::UsingDirective: |
| 777 | case Decl::UnresolvedUsingTypename: |
| 778 | // - static_assert-declarations |
| 779 | // - using-declarations, |
| 780 | // - using-directives, |
| 781 | continue; |
| 782 | |
| 783 | case Decl::Typedef: |
| 784 | case Decl::TypeAlias: { |
| 785 | // - typedef declarations and alias-declarations that do not define |
| 786 | // classes or enumerations, |
| 787 | TypedefNameDecl *TN = cast<TypedefNameDecl>(*DclIt); |
| 788 | if (TN->getUnderlyingType()->isVariablyModifiedType()) { |
| 789 | // Don't allow variably-modified types in constexpr functions. |
| 790 | TypeLoc TL = TN->getTypeSourceInfo()->getTypeLoc(); |
| 791 | SemaRef.Diag(TL.getBeginLoc(), diag::err_constexpr_vla) |
| 792 | << TL.getSourceRange() << TL.getType() |
| 793 | << isa<CXXConstructorDecl>(Dcl); |
| 794 | return false; |
| 795 | } |
| 796 | continue; |
| 797 | } |
| 798 | |
| 799 | case Decl::Enum: |
| 800 | case Decl::CXXRecord: |
| 801 | // As an extension, we allow the declaration (but not the definition) of |
| 802 | // classes and enumerations in all declarations, not just in typedef and |
| 803 | // alias declarations. |
| 804 | if (cast<TagDecl>(*DclIt)->isThisDeclarationADefinition()) { |
| 805 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_type_definition) |
| 806 | << isa<CXXConstructorDecl>(Dcl); |
| 807 | return false; |
| 808 | } |
| 809 | continue; |
| 810 | |
| 811 | case Decl::Var: |
| 812 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_var_declaration) |
| 813 | << isa<CXXConstructorDecl>(Dcl); |
| 814 | return false; |
| 815 | |
| 816 | default: |
| 817 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 818 | << isa<CXXConstructorDecl>(Dcl); |
| 819 | return false; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | return true; |
| 824 | } |
| 825 | |
| 826 | /// Check that the given field is initialized within a constexpr constructor. |
| 827 | /// |
| 828 | /// \param Dcl The constexpr constructor being checked. |
| 829 | /// \param Field The field being checked. This may be a member of an anonymous |
| 830 | /// struct or union nested within the class being checked. |
| 831 | /// \param Inits All declarations, including anonymous struct/union members and |
| 832 | /// indirect members, for which any initialization was provided. |
| 833 | /// \param Diagnosed Set to true if an error is produced. |
| 834 | static void CheckConstexprCtorInitializer(Sema &SemaRef, |
| 835 | const FunctionDecl *Dcl, |
| 836 | FieldDecl *Field, |
| 837 | llvm::SmallSet<Decl*, 16> &Inits, |
| 838 | bool &Diagnosed) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 839 | if (Field->isUnnamedBitfield()) |
| 840 | return; |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 841 | |
| 842 | if (Field->isAnonymousStructOrUnion() && |
| 843 | Field->getType()->getAsCXXRecordDecl()->isEmpty()) |
| 844 | return; |
| 845 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 846 | if (!Inits.count(Field)) { |
| 847 | if (!Diagnosed) { |
| 848 | SemaRef.Diag(Dcl->getLocation(), diag::err_constexpr_ctor_missing_init); |
| 849 | Diagnosed = true; |
| 850 | } |
| 851 | SemaRef.Diag(Field->getLocation(), diag::note_constexpr_ctor_missing_init); |
| 852 | } else if (Field->isAnonymousStructOrUnion()) { |
| 853 | const RecordDecl *RD = Field->getType()->castAs<RecordType>()->getDecl(); |
| 854 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 855 | I != E; ++I) |
| 856 | // If an anonymous union contains an anonymous struct of which any member |
| 857 | // is initialized, all members must be initialized. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 858 | if (!RD->isUnion() || Inits.count(*I)) |
| 859 | CheckConstexprCtorInitializer(SemaRef, Dcl, *I, Inits, Diagnosed); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
| 863 | /// Check the body for the given constexpr function declaration only contains |
| 864 | /// the permitted types of statement. C++11 [dcl.constexpr]p3,p4. |
| 865 | /// |
| 866 | /// \return true if the body is OK, false if we have diagnosed a problem. |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 867 | bool Sema::CheckConstexprFunctionBody(const FunctionDecl *Dcl, Stmt *Body) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 868 | if (isa<CXXTryStmt>(Body)) { |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 869 | // C++11 [dcl.constexpr]p3: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 870 | // The definition of a constexpr function shall satisfy the following |
| 871 | // constraints: [...] |
| 872 | // - its function-body shall be = delete, = default, or a |
| 873 | // compound-statement |
| 874 | // |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 875 | // C++11 [dcl.constexpr]p4: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 876 | // In the definition of a constexpr constructor, [...] |
| 877 | // - its function-body shall not be a function-try-block; |
| 878 | Diag(Body->getLocStart(), diag::err_constexpr_function_try_block) |
| 879 | << isa<CXXConstructorDecl>(Dcl); |
| 880 | return false; |
| 881 | } |
| 882 | |
| 883 | // - its function-body shall be [...] a compound-statement that contains only |
| 884 | CompoundStmt *CompBody = cast<CompoundStmt>(Body); |
| 885 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 886 | SmallVector<SourceLocation, 4> ReturnStmts; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 887 | for (CompoundStmt::body_iterator BodyIt = CompBody->body_begin(), |
| 888 | BodyEnd = CompBody->body_end(); BodyIt != BodyEnd; ++BodyIt) { |
| 889 | switch ((*BodyIt)->getStmtClass()) { |
| 890 | case Stmt::NullStmtClass: |
| 891 | // - null statements, |
| 892 | continue; |
| 893 | |
| 894 | case Stmt::DeclStmtClass: |
| 895 | // - static_assert-declarations |
| 896 | // - using-declarations, |
| 897 | // - using-directives, |
| 898 | // - typedef declarations and alias-declarations that do not define |
| 899 | // classes or enumerations, |
| 900 | if (!CheckConstexprDeclStmt(*this, Dcl, cast<DeclStmt>(*BodyIt))) |
| 901 | return false; |
| 902 | continue; |
| 903 | |
| 904 | case Stmt::ReturnStmtClass: |
| 905 | // - and exactly one return statement; |
| 906 | if (isa<CXXConstructorDecl>(Dcl)) |
| 907 | break; |
| 908 | |
| 909 | ReturnStmts.push_back((*BodyIt)->getLocStart()); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 910 | continue; |
| 911 | |
| 912 | default: |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | Diag((*BodyIt)->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 917 | << isa<CXXConstructorDecl>(Dcl); |
| 918 | return false; |
| 919 | } |
| 920 | |
| 921 | if (const CXXConstructorDecl *Constructor |
| 922 | = dyn_cast<CXXConstructorDecl>(Dcl)) { |
| 923 | const CXXRecordDecl *RD = Constructor->getParent(); |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 924 | // DR1359: |
| 925 | // - every non-variant non-static data member and base class sub-object |
| 926 | // shall be initialized; |
| 927 | // - if the class is a non-empty union, or for each non-empty anonymous |
| 928 | // union member of a non-union class, exactly one non-static data member |
| 929 | // shall be initialized; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 930 | if (RD->isUnion()) { |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 931 | if (Constructor->getNumCtorInitializers() == 0 && !RD->isEmpty()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 932 | Diag(Dcl->getLocation(), diag::err_constexpr_union_ctor_no_init); |
| 933 | return false; |
| 934 | } |
Richard Smith | 6e43375 | 2011-10-10 16:38:04 +0000 | [diff] [blame] | 935 | } else if (!Constructor->isDependentContext() && |
| 936 | !Constructor->isDelegatingConstructor()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 937 | assert(RD->getNumVBases() == 0 && "constexpr ctor with virtual bases"); |
| 938 | |
| 939 | // Skip detailed checking if we have enough initializers, and we would |
| 940 | // allow at most one initializer per member. |
| 941 | bool AnyAnonStructUnionMembers = false; |
| 942 | unsigned Fields = 0; |
| 943 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 944 | E = RD->field_end(); I != E; ++I, ++Fields) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 945 | if (I->isAnonymousStructOrUnion()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 946 | AnyAnonStructUnionMembers = true; |
| 947 | break; |
| 948 | } |
| 949 | } |
| 950 | if (AnyAnonStructUnionMembers || |
| 951 | Constructor->getNumCtorInitializers() != RD->getNumBases() + Fields) { |
| 952 | // Check initialization of non-static data members. Base classes are |
| 953 | // always initialized so do not need to be checked. Dependent bases |
| 954 | // might not have initializers in the member initializer list. |
| 955 | llvm::SmallSet<Decl*, 16> Inits; |
| 956 | for (CXXConstructorDecl::init_const_iterator |
| 957 | I = Constructor->init_begin(), E = Constructor->init_end(); |
| 958 | I != E; ++I) { |
| 959 | if (FieldDecl *FD = (*I)->getMember()) |
| 960 | Inits.insert(FD); |
| 961 | else if (IndirectFieldDecl *ID = (*I)->getIndirectMember()) |
| 962 | Inits.insert(ID->chain_begin(), ID->chain_end()); |
| 963 | } |
| 964 | |
| 965 | bool Diagnosed = false; |
| 966 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 967 | E = RD->field_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 968 | CheckConstexprCtorInitializer(*this, Dcl, *I, Inits, Diagnosed); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 969 | if (Diagnosed) |
| 970 | return false; |
| 971 | } |
| 972 | } |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 973 | } else { |
| 974 | if (ReturnStmts.empty()) { |
| 975 | Diag(Dcl->getLocation(), diag::err_constexpr_body_no_return); |
| 976 | return false; |
| 977 | } |
| 978 | if (ReturnStmts.size() > 1) { |
| 979 | Diag(ReturnStmts.back(), diag::err_constexpr_body_multiple_return); |
| 980 | for (unsigned I = 0; I < ReturnStmts.size() - 1; ++I) |
| 981 | Diag(ReturnStmts[I], diag::note_constexpr_body_previous_return); |
| 982 | return false; |
| 983 | } |
| 984 | } |
| 985 | |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 986 | // C++11 [dcl.constexpr]p5: |
| 987 | // if no function argument values exist such that the function invocation |
| 988 | // substitution would produce a constant expression, the program is |
| 989 | // ill-formed; no diagnostic required. |
| 990 | // C++11 [dcl.constexpr]p3: |
| 991 | // - every constructor call and implicit conversion used in initializing the |
| 992 | // return value shall be one of those allowed in a constant expression. |
| 993 | // C++11 [dcl.constexpr]p4: |
| 994 | // - every constructor involved in initializing non-static data members and |
| 995 | // base class sub-objects shall be a constexpr constructor. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 996 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 997 | if (!Expr::isPotentialConstantExpr(Dcl, Diags)) { |
Richard Smith | afee0ff | 2012-12-09 05:55:43 +0000 | [diff] [blame] | 998 | Diag(Dcl->getLocation(), diag::ext_constexpr_function_never_constant_expr) |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 999 | << isa<CXXConstructorDecl>(Dcl); |
| 1000 | for (size_t I = 0, N = Diags.size(); I != N; ++I) |
| 1001 | Diag(Diags[I].first, Diags[I].second); |
Richard Smith | afee0ff | 2012-12-09 05:55:43 +0000 | [diff] [blame] | 1002 | // Don't return false here: we allow this for compatibility in |
| 1003 | // system headers. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1006 | return true; |
| 1007 | } |
| 1008 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1009 | /// isCurrentClassName - Determine whether the identifier II is the |
| 1010 | /// name of the class type currently being defined. In the case of |
| 1011 | /// nested classes, this will only return true if II is the name of |
| 1012 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1013 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 1014 | const CXXScopeSpec *SS) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1015 | assert(getLangOpts().CPlusPlus && "No class names in C!"); |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 1016 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1017 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1018 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 1019 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1020 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 1021 | } else |
| 1022 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 1023 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 1024 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1025 | return &II == CurDecl->getIdentifier(); |
| 1026 | else |
| 1027 | return false; |
| 1028 | } |
| 1029 | |
Douglas Gregor | 229d47a | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1030 | /// \brief Determine whether the given class is a base class of the given |
| 1031 | /// class, including looking at dependent bases. |
| 1032 | static bool findCircularInheritance(const CXXRecordDecl *Class, |
| 1033 | const CXXRecordDecl *Current) { |
| 1034 | SmallVector<const CXXRecordDecl*, 8> Queue; |
| 1035 | |
| 1036 | Class = Class->getCanonicalDecl(); |
| 1037 | while (true) { |
| 1038 | for (CXXRecordDecl::base_class_const_iterator I = Current->bases_begin(), |
| 1039 | E = Current->bases_end(); |
| 1040 | I != E; ++I) { |
| 1041 | CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 1042 | if (!Base) |
| 1043 | continue; |
| 1044 | |
| 1045 | Base = Base->getDefinition(); |
| 1046 | if (!Base) |
| 1047 | continue; |
| 1048 | |
| 1049 | if (Base->getCanonicalDecl() == Class) |
| 1050 | return true; |
| 1051 | |
| 1052 | Queue.push_back(Base); |
| 1053 | } |
| 1054 | |
| 1055 | if (Queue.empty()) |
| 1056 | return false; |
| 1057 | |
| 1058 | Current = Queue.back(); |
| 1059 | Queue.pop_back(); |
| 1060 | } |
| 1061 | |
| 1062 | return false; |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1066 | /// |
| 1067 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 1068 | /// and returns NULL otherwise. |
| 1069 | CXXBaseSpecifier * |
| 1070 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 1071 | SourceRange SpecifierRange, |
| 1072 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1073 | TypeSourceInfo *TInfo, |
| 1074 | SourceLocation EllipsisLoc) { |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1075 | QualType BaseType = TInfo->getType(); |
| 1076 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1077 | // C++ [class.union]p1: |
| 1078 | // A union shall not have base classes. |
| 1079 | if (Class->isUnion()) { |
| 1080 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 1081 | << SpecifierRange; |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1085 | if (EllipsisLoc.isValid() && |
| 1086 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 1087 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1088 | << TInfo->getTypeLoc().getSourceRange(); |
| 1089 | EllipsisLoc = SourceLocation(); |
| 1090 | } |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1091 | |
| 1092 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
| 1093 | |
| 1094 | if (BaseType->isDependentType()) { |
| 1095 | // Make sure that we don't have circular inheritance among our dependent |
| 1096 | // bases. For non-dependent bases, the check for completeness below handles |
| 1097 | // this. |
| 1098 | if (CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl()) { |
| 1099 | if (BaseDecl->getCanonicalDecl() == Class->getCanonicalDecl() || |
| 1100 | ((BaseDecl = BaseDecl->getDefinition()) && |
Douglas Gregor | 229d47a | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1101 | findCircularInheritance(Class, BaseDecl))) { |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1102 | Diag(BaseLoc, diag::err_circular_inheritance) |
| 1103 | << BaseType << Context.getTypeDeclType(Class); |
| 1104 | |
| 1105 | if (BaseDecl->getCanonicalDecl() != Class->getCanonicalDecl()) |
| 1106 | Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 1107 | << BaseType; |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | } |
| 1112 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1114 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1115 | Access, TInfo, EllipsisLoc); |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1116 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1117 | |
| 1118 | // Base specifiers must be record types. |
| 1119 | if (!BaseType->isRecordType()) { |
| 1120 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | // C++ [class.union]p1: |
| 1125 | // A union shall not be used as a base class. |
| 1126 | if (BaseType->isUnionType()) { |
| 1127 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | // C++ [class.derived]p2: |
| 1132 | // The class-name in a base-specifier shall not be an incompletely |
| 1133 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | if (RequireCompleteType(BaseLoc, BaseType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1135 | diag::err_incomplete_base_class, SpecifierRange)) { |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1136 | Class->setInvalidDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1137 | return 0; |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1138 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1139 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1140 | // If the base class is polymorphic or isn't empty, the new one is/isn't, too. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1141 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1142 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1143 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1144 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1145 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 1146 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 1147 | |
Anders Carlsson | 1d20927 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 1148 | // C++ [class]p3: |
| 1149 | // If a class is marked final and it appears as a base-type-specifier in |
| 1150 | // base-clause, the program is ill-formed. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1151 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 1152 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
| 1153 | << CXXBaseDecl->getDeclName(); |
| 1154 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 1155 | << CXXBaseDecl->getDeclName(); |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1159 | if (BaseDecl->isInvalidDecl()) |
| 1160 | Class->setInvalidDecl(); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1161 | |
| 1162 | // Create the base specifier. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1163 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1164 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1165 | Access, TInfo, EllipsisLoc); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1168 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 1169 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | /// example: |
| 1171 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1172 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1173 | BaseResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1174 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1175 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1176 | ParsedType basetype, SourceLocation BaseLoc, |
| 1177 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1178 | if (!classdecl) |
| 1179 | return true; |
| 1180 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1181 | AdjustDeclIfTemplate(classdecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1182 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 1183 | if (!Class) |
| 1184 | return true; |
| 1185 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1186 | TypeSourceInfo *TInfo = 0; |
| 1187 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1188 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1189 | if (EllipsisLoc.isInvalid() && |
| 1190 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1191 | UPPC_BaseType)) |
| 1192 | return true; |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1193 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1194 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1195 | Virtual, Access, TInfo, |
| 1196 | EllipsisLoc)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1197 | return BaseSpec; |
Douglas Gregor | 8a50fe0 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 1198 | else |
| 1199 | Class->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1201 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1202 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1203 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1204 | /// \brief Performs the actual work of attaching the given base class |
| 1205 | /// specifiers to a C++ class. |
| 1206 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 1207 | unsigned NumBases) { |
| 1208 | if (NumBases == 0) |
| 1209 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1210 | |
| 1211 | // Used to keep track of which base types we have already seen, so |
| 1212 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1213 | // that the key is always the unqualified canonical type of the base |
| 1214 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1215 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 1216 | |
| 1217 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1218 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1219 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1220 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1222 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1223 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1224 | |
| 1225 | CXXBaseSpecifier *&KnownBase = KnownBaseTypes[NewBaseType]; |
| 1226 | if (KnownBase) { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1227 | // C++ [class.mi]p3: |
| 1228 | // A class shall not be specified as a direct base class of a |
| 1229 | // derived class more than once. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1230 | Diag(Bases[idx]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1231 | diag::err_duplicate_base_class) |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1232 | << KnownBase->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1233 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1234 | |
| 1235 | // Delete the duplicate base class specifier; we're going to |
| 1236 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1237 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1238 | |
| 1239 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1240 | } else { |
| 1241 | // Okay, add this new base class. |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1242 | KnownBase = Bases[idx]; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1243 | Bases[NumGoodBases++] = Bases[idx]; |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1244 | if (const RecordType *Record = NewBaseType->getAs<RecordType>()) { |
| 1245 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
| 1246 | if (Class->isInterface() && |
| 1247 | (!RD->isInterface() || |
| 1248 | KnownBase->getAccessSpecifier() != AS_public)) { |
| 1249 | // The Microsoft extension __interface does not permit bases that |
| 1250 | // are not themselves public interfaces. |
| 1251 | Diag(KnownBase->getLocStart(), diag::err_invalid_base_in_interface) |
| 1252 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getName() |
| 1253 | << RD->getSourceRange(); |
| 1254 | Invalid = true; |
| 1255 | } |
| 1256 | if (RD->hasAttr<WeakAttr>()) |
| 1257 | Class->addAttr(::new (Context) WeakAttr(SourceRange(), Context)); |
| 1258 | } |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 1263 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1264 | |
| 1265 | // Delete the remaining (good) base class specifiers, since their |
| 1266 | // data has been copied into the CXXRecordDecl. |
| 1267 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1268 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1269 | |
| 1270 | return Invalid; |
| 1271 | } |
| 1272 | |
| 1273 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 1274 | /// class, after checking whether there are any duplicate base |
| 1275 | /// classes. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1276 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, CXXBaseSpecifier **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1277 | unsigned NumBases) { |
| 1278 | if (!ClassDecl || !Bases || !NumBases) |
| 1279 | return; |
| 1280 | |
| 1281 | AdjustDeclIfTemplate(ClassDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1282 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1283 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1284 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1285 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1286 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 1287 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 1288 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 1289 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 1290 | return ICT->getDecl(); |
| 1291 | else |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1295 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1296 | /// derived from the type \p Base. |
| 1297 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1298 | if (!getLangOpts().CPlusPlus) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1299 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1300 | |
| 1301 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 1302 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1303 | return false; |
| 1304 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1305 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 1306 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1307 | return false; |
| 1308 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1309 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 1310 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1314 | /// derived from the type \p Base. |
| 1315 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1316 | if (!getLangOpts().CPlusPlus) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1317 | return false; |
| 1318 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1319 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 1320 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1321 | return false; |
| 1322 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1323 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 1324 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1325 | return false; |
| 1326 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1327 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 1328 | } |
| 1329 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1330 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1331 | CXXCastPath &BasePathArray) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1332 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 1333 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 1334 | |
| 1335 | const CXXBasePath &Path = Paths.front(); |
| 1336 | |
| 1337 | // We first go backward and check if we have a virtual base. |
| 1338 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 1339 | // the nearest virtual base. |
| 1340 | unsigned Start = 0; |
| 1341 | for (unsigned I = Path.size(); I != 0; --I) { |
| 1342 | if (Path[I - 1].Base->isVirtual()) { |
| 1343 | Start = I - 1; |
| 1344 | break; |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | // Now add all bases. |
| 1349 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1350 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1353 | /// \brief Determine whether the given base path includes a virtual |
| 1354 | /// base class. |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1355 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 1356 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 1357 | BEnd = BasePath.end(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1358 | B != BEnd; ++B) |
| 1359 | if ((*B)->isVirtual()) |
| 1360 | return true; |
| 1361 | |
| 1362 | return false; |
| 1363 | } |
| 1364 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1365 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 1366 | /// conversion (where Derived and Base are class types) is |
| 1367 | /// well-formed, meaning that the conversion is unambiguous (and |
| 1368 | /// that all of the base classes are accessible). Returns true |
| 1369 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 1370 | /// otherwise. Loc is the location where this routine should point to |
| 1371 | /// if there is an error, and Range is the source range to highlight |
| 1372 | /// if there is an error. |
| 1373 | bool |
| 1374 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1375 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1376 | unsigned AmbigiousBaseConvID, |
| 1377 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1378 | DeclarationName Name, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1379 | CXXCastPath *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1380 | // First, determine whether the path from Derived to Base is |
| 1381 | // ambiguous. This is slightly more expensive than checking whether |
| 1382 | // the Derived to Base conversion exists, because here we need to |
| 1383 | // explore multiple paths to determine if there is an ambiguity. |
| 1384 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1385 | /*DetectVirtual=*/false); |
| 1386 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1387 | assert(DerivationOkay && |
| 1388 | "Can only be used with a derived-to-base conversion"); |
| 1389 | (void)DerivationOkay; |
| 1390 | |
| 1391 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1392 | if (InaccessibleBaseID) { |
| 1393 | // Check that the base class can be accessed. |
| 1394 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 1395 | InaccessibleBaseID)) { |
| 1396 | case AR_inaccessible: |
| 1397 | return true; |
| 1398 | case AR_accessible: |
| 1399 | case AR_dependent: |
| 1400 | case AR_delayed: |
| 1401 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1402 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1403 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1404 | |
| 1405 | // Build a base path if necessary. |
| 1406 | if (BasePath) |
| 1407 | BuildBasePathArray(Paths, *BasePath); |
| 1408 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | // We know that the derived-to-base conversion is ambiguous, and |
| 1412 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 1413 | // search just one more time to compute all of the possible paths so |
| 1414 | // that we can print them out. This is more expensive than any of |
| 1415 | // the previous derived-to-base checks we've done, but at this point |
| 1416 | // performance isn't as much of an issue. |
| 1417 | Paths.clear(); |
| 1418 | Paths.setRecordingPaths(true); |
| 1419 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1420 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 1421 | (void)StillOkay; |
| 1422 | |
| 1423 | // Build up a textual representation of the ambiguous paths, e.g., |
| 1424 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 1425 | // conversions in the diagnostic. We only print one of the paths |
| 1426 | // to each base class subobject. |
| 1427 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1428 | |
| 1429 | Diag(Loc, AmbigiousBaseConvID) |
| 1430 | << Derived << Base << PathDisplayStr << Range << Name; |
| 1431 | return true; |
| 1432 | } |
| 1433 | |
| 1434 | bool |
| 1435 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1436 | SourceLocation Loc, SourceRange Range, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1437 | CXXCastPath *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1438 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1439 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1440 | IgnoreAccess ? 0 |
| 1441 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1442 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1443 | Loc, Range, DeclarationName(), |
| 1444 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | |
| 1448 | /// @brief Builds a string representing ambiguous paths from a |
| 1449 | /// specific derived class to different subobjects of the same base |
| 1450 | /// class. |
| 1451 | /// |
| 1452 | /// This function builds a string that can be used in error messages |
| 1453 | /// to show the different paths that one can take through the |
| 1454 | /// inheritance hierarchy to go from the derived class to different |
| 1455 | /// subobjects of a base class. The result looks something like this: |
| 1456 | /// @code |
| 1457 | /// struct D -> struct B -> struct A |
| 1458 | /// struct D -> struct C -> struct A |
| 1459 | /// @endcode |
| 1460 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 1461 | std::string PathDisplayStr; |
| 1462 | std::set<unsigned> DisplayedPaths; |
| 1463 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1464 | Path != Paths.end(); ++Path) { |
| 1465 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 1466 | // We haven't displayed a path to this particular base |
| 1467 | // class subobject yet. |
| 1468 | PathDisplayStr += "\n "; |
| 1469 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 1470 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 1471 | Element != Path->end(); ++Element) |
| 1472 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | return PathDisplayStr; |
| 1477 | } |
| 1478 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1479 | //===----------------------------------------------------------------------===// |
| 1480 | // C++ class member Handling |
| 1481 | //===----------------------------------------------------------------------===// |
| 1482 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1483 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1484 | bool Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 1485 | SourceLocation ASLoc, |
| 1486 | SourceLocation ColonLoc, |
| 1487 | AttributeList *Attrs) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1488 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1489 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1490 | ASLoc, ColonLoc); |
| 1491 | CurContext->addHiddenDecl(ASDecl); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1492 | return ProcessAccessDeclAttributeList(ASDecl, Attrs); |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1495 | /// CheckOverrideControl - Check C++11 override control semantics. |
| 1496 | void Sema::CheckOverrideControl(Decl *D) { |
Richard Smith | cddbc1d | 2012-09-06 18:32:18 +0000 | [diff] [blame] | 1497 | if (D->isInvalidDecl()) |
| 1498 | return; |
| 1499 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1500 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1501 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1502 | // Do we know which functions this declaration might be overriding? |
| 1503 | bool OverridesAreKnown = !MD || |
| 1504 | (!MD->getParent()->hasAnyDependentBases() && |
| 1505 | !MD->getType()->isDependentType()); |
Anders Carlsson | 3ffe183 | 2011-01-20 06:33:26 +0000 | [diff] [blame] | 1506 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1507 | if (!MD || !MD->isVirtual()) { |
| 1508 | if (OverridesAreKnown) { |
| 1509 | if (OverrideAttr *OA = D->getAttr<OverrideAttr>()) { |
| 1510 | Diag(OA->getLocation(), |
| 1511 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1512 | << "override" << FixItHint::CreateRemoval(OA->getLocation()); |
| 1513 | D->dropAttr<OverrideAttr>(); |
| 1514 | } |
| 1515 | if (FinalAttr *FA = D->getAttr<FinalAttr>()) { |
| 1516 | Diag(FA->getLocation(), |
| 1517 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1518 | << "final" << FixItHint::CreateRemoval(FA->getLocation()); |
| 1519 | D->dropAttr<FinalAttr>(); |
| 1520 | } |
| 1521 | } |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1522 | return; |
| 1523 | } |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1524 | |
| 1525 | if (!OverridesAreKnown) |
| 1526 | return; |
| 1527 | |
| 1528 | // C++11 [class.virtual]p5: |
| 1529 | // If a virtual function is marked with the virt-specifier override and |
| 1530 | // does not override a member function of a base class, the program is |
| 1531 | // ill-formed. |
| 1532 | bool HasOverriddenMethods = |
| 1533 | MD->begin_overridden_methods() != MD->end_overridden_methods(); |
| 1534 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) |
| 1535 | Diag(MD->getLocation(), diag::err_function_marked_override_not_overriding) |
| 1536 | << MD->getDeclName(); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1539 | /// CheckIfOverriddenFunctionIsMarkedFinal - Checks whether a virtual member |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1540 | /// function overrides a virtual member function marked 'final', according to |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1541 | /// C++11 [class.virtual]p4. |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1542 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 1543 | const CXXMethodDecl *Old) { |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1544 | if (!Old->hasAttr<FinalAttr>()) |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1545 | return false; |
| 1546 | |
| 1547 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 1548 | << New->getDeclName(); |
| 1549 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 1550 | return true; |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1553 | static bool InitializationHasSideEffects(const FieldDecl &FD) { |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 1554 | const Type *T = FD.getType()->getBaseElementTypeUnsafe(); |
| 1555 | // FIXME: Destruction of ObjC lifetime types has side-effects. |
| 1556 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 1557 | return !RD->isCompleteDefinition() || |
| 1558 | !RD->hasTrivialDefaultConstructor() || |
| 1559 | !RD->hasTrivialDestructor(); |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1560 | return false; |
| 1561 | } |
| 1562 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1563 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 1564 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1565 | /// bitfield width if there is one, 'InitExpr' specifies the initializer if |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1566 | /// one has been parsed, and 'InitStyle' is set if an in-class initializer is |
| 1567 | /// present (but parsing it has been deferred). |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 1568 | NamedDecl * |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1569 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1570 | MultiTemplateParamsArg TemplateParameterLists, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 1571 | Expr *BW, const VirtSpecifiers &VS, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1572 | InClassInitStyle InitStyle) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1573 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1574 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 1575 | DeclarationName Name = NameInfo.getName(); |
| 1576 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1577 | |
| 1578 | // For anonymous bitfields, the location should point to the type. |
| 1579 | if (Loc.isInvalid()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1580 | Loc = D.getLocStart(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1581 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1582 | Expr *BitWidth = static_cast<Expr*>(BW); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1583 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1584 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1585 | assert(!DS.isFriendSpecified()); |
| 1586 | |
Richard Smith | 1ab0d90 | 2011-06-25 02:28:38 +0000 | [diff] [blame] | 1587 | bool isFunc = D.isDeclarationOfFunction(); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1588 | |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1589 | if (cast<CXXRecordDecl>(CurContext)->isInterface()) { |
| 1590 | // The Microsoft extension __interface only permits public member functions |
| 1591 | // and prohibits constructors, destructors, operators, non-public member |
| 1592 | // functions, static methods and data members. |
| 1593 | unsigned InvalidDecl; |
| 1594 | bool ShowDeclName = true; |
| 1595 | if (!isFunc) |
| 1596 | InvalidDecl = (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) ? 0 : 1; |
| 1597 | else if (AS != AS_public) |
| 1598 | InvalidDecl = 2; |
| 1599 | else if (DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 1600 | InvalidDecl = 3; |
| 1601 | else switch (Name.getNameKind()) { |
| 1602 | case DeclarationName::CXXConstructorName: |
| 1603 | InvalidDecl = 4; |
| 1604 | ShowDeclName = false; |
| 1605 | break; |
| 1606 | |
| 1607 | case DeclarationName::CXXDestructorName: |
| 1608 | InvalidDecl = 5; |
| 1609 | ShowDeclName = false; |
| 1610 | break; |
| 1611 | |
| 1612 | case DeclarationName::CXXOperatorName: |
| 1613 | case DeclarationName::CXXConversionFunctionName: |
| 1614 | InvalidDecl = 6; |
| 1615 | break; |
| 1616 | |
| 1617 | default: |
| 1618 | InvalidDecl = 0; |
| 1619 | break; |
| 1620 | } |
| 1621 | |
| 1622 | if (InvalidDecl) { |
| 1623 | if (ShowDeclName) |
| 1624 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 1625 | << (InvalidDecl-1) << Name; |
| 1626 | else |
| 1627 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 1628 | << (InvalidDecl-1) << ""; |
| 1629 | return 0; |
| 1630 | } |
| 1631 | } |
| 1632 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1633 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 1634 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1635 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 1636 | // data members and cannot be applied to names declared const or static, |
| 1637 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1638 | switch (DS.getStorageClassSpec()) { |
| 1639 | case DeclSpec::SCS_unspecified: |
| 1640 | case DeclSpec::SCS_typedef: |
| 1641 | case DeclSpec::SCS_static: |
| 1642 | // FALL THROUGH. |
| 1643 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1644 | case DeclSpec::SCS_mutable: |
| 1645 | if (isFunc) { |
| 1646 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1647 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1648 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1649 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 1651 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 1652 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1653 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1654 | } |
| 1655 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1656 | default: |
| 1657 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1658 | Diag(DS.getStorageClassSpecLoc(), |
| 1659 | diag::err_storageclass_invalid_for_member); |
| 1660 | else |
| 1661 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 1662 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 1663 | } |
| 1664 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1665 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 1666 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 1667 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1668 | |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 1669 | NamedDecl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1670 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1671 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 1672 | |
| 1673 | // Data members must have identifiers for names. |
Benjamin Kramer | c1aa40c | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1674 | if (!Name.isIdentifier()) { |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 1675 | Diag(Loc, diag::err_bad_variable_name) |
| 1676 | << Name; |
| 1677 | return 0; |
| 1678 | } |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1679 | |
Benjamin Kramer | c1aa40c | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1680 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 1681 | |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1682 | // Member field could not be with "template" keyword. |
| 1683 | // So TemplateParameterLists should be empty in this case. |
| 1684 | if (TemplateParameterLists.size()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1685 | TemplateParameterList* TemplateParams = TemplateParameterLists[0]; |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1686 | if (TemplateParams->size()) { |
| 1687 | // There is no such thing as a member field template. |
| 1688 | Diag(D.getIdentifierLoc(), diag::err_template_member) |
| 1689 | << II |
| 1690 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 1691 | TemplateParams->getRAngleLoc()); |
| 1692 | } else { |
| 1693 | // There is an extraneous 'template<>' for this member. |
| 1694 | Diag(TemplateParams->getTemplateLoc(), |
| 1695 | diag::err_template_member_noparams) |
| 1696 | << II |
| 1697 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 1698 | TemplateParams->getRAngleLoc()); |
| 1699 | } |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1703 | if (SS.isSet() && !SS.isInvalid()) { |
| 1704 | // The user provided a superfluous scope specifier inside a class |
| 1705 | // definition: |
| 1706 | // |
| 1707 | // class X { |
| 1708 | // int X::member; |
| 1709 | // }; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 1710 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 1711 | diagnoseQualifiedDeclaration(SS, DC, Name, D.getIdentifierLoc()); |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1712 | else |
| 1713 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 1714 | << Name << SS.getRange(); |
Douglas Gregor | 5d8419c | 2011-11-01 22:13:30 +0000 | [diff] [blame] | 1715 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1716 | SS.clear(); |
| 1717 | } |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1718 | |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1719 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1720 | InitStyle, AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1721 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1722 | } else { |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1723 | assert(InitStyle == ICIS_NoInit); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1724 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1725 | Member = HandleDeclarator(S, D, TemplateParameterLists); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1726 | if (!Member) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1727 | return 0; |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1728 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1729 | |
| 1730 | // Non-instance-fields can't have a bitfield. |
| 1731 | if (BitWidth) { |
| 1732 | if (Member->isInvalidDecl()) { |
| 1733 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 1734 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1735 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 1736 | // "static member 'A' cannot be a bit-field" |
| 1737 | Diag(Loc, diag::err_static_not_bitfield) |
| 1738 | << Name << BitWidth->getSourceRange(); |
| 1739 | } else if (isa<TypedefDecl>(Member)) { |
| 1740 | // "typedef member 'x' cannot be a bit-field" |
| 1741 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 1742 | << Name << BitWidth->getSourceRange(); |
| 1743 | } else { |
| 1744 | // A function typedef ("typedef int f(); f a;"). |
| 1745 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 1746 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1748 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1749 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1751 | BitWidth = 0; |
| 1752 | Member->setInvalidDecl(); |
| 1753 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1754 | |
| 1755 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1757 | // If we have declared a member function template, set the access of the |
| 1758 | // templated declaration as well. |
| 1759 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 1760 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1761 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1762 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1763 | if (VS.isOverrideSpecified()) |
| 1764 | Member->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context)); |
| 1765 | if (VS.isFinalSpecified()) |
| 1766 | Member->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context)); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1767 | |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1768 | if (VS.getLastLocation().isValid()) { |
| 1769 | // Update the end location of a method that has a virt-specifiers. |
| 1770 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 1771 | MD->setRangeEnd(VS.getLastLocation()); |
| 1772 | } |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1773 | |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1774 | CheckOverrideControl(Member); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1775 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1776 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1777 | |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1778 | if (isInstField) { |
| 1779 | FieldDecl *FD = cast<FieldDecl>(Member); |
| 1780 | FieldCollector->Add(FD); |
| 1781 | |
| 1782 | if (Diags.getDiagnosticLevel(diag::warn_unused_private_field, |
| 1783 | FD->getLocation()) |
| 1784 | != DiagnosticsEngine::Ignored) { |
| 1785 | // Remember all explicit private FieldDecls that have a name, no side |
| 1786 | // effects and are not part of a dependent type declaration. |
| 1787 | if (!FD->isImplicit() && FD->getDeclName() && |
| 1788 | FD->getAccess() == AS_private && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1789 | !FD->hasAttr<UnusedAttr>() && |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 1790 | !FD->getParent()->isDependentContext() && |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1791 | !InitializationHasSideEffects(*FD)) |
| 1792 | UnusedPrivateFields.insert(FD); |
| 1793 | } |
| 1794 | } |
| 1795 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1796 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1799 | namespace { |
| 1800 | class UninitializedFieldVisitor |
| 1801 | : public EvaluatedExprVisitor<UninitializedFieldVisitor> { |
| 1802 | Sema &S; |
| 1803 | ValueDecl *VD; |
| 1804 | public: |
| 1805 | typedef EvaluatedExprVisitor<UninitializedFieldVisitor> Inherited; |
| 1806 | UninitializedFieldVisitor(Sema &S, ValueDecl *VD) : Inherited(S.Context), |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 1807 | S(S) { |
| 1808 | if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(VD)) |
| 1809 | this->VD = IFD->getAnonField(); |
| 1810 | else |
| 1811 | this->VD = VD; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | void HandleExpr(Expr *E) { |
| 1815 | if (!E) return; |
| 1816 | |
| 1817 | // Expressions like x(x) sometimes lack the surrounding expressions |
| 1818 | // but need to be checked anyways. |
| 1819 | HandleValue(E); |
| 1820 | Visit(E); |
| 1821 | } |
| 1822 | |
| 1823 | void HandleValue(Expr *E) { |
| 1824 | E = E->IgnoreParens(); |
| 1825 | |
| 1826 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 1827 | if (isa<EnumConstantDecl>(ME->getMemberDecl())) |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 1828 | return; |
| 1829 | |
| 1830 | // FieldME is the inner-most MemberExpr that is not an anonymous struct |
| 1831 | // or union. |
| 1832 | MemberExpr *FieldME = ME; |
| 1833 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1834 | Expr *Base = E; |
| 1835 | while (isa<MemberExpr>(Base)) { |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 1836 | ME = cast<MemberExpr>(Base); |
| 1837 | |
| 1838 | if (isa<VarDecl>(ME->getMemberDecl())) |
| 1839 | return; |
| 1840 | |
| 1841 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 1842 | if (!FD->isAnonymousStructOrUnion()) |
| 1843 | FieldME = ME; |
| 1844 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1845 | Base = ME->getBase(); |
| 1846 | } |
| 1847 | |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 1848 | if (VD == FieldME->getMemberDecl() && isa<CXXThisExpr>(Base)) { |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1849 | unsigned diag = VD->getType()->isReferenceType() |
| 1850 | ? diag::warn_reference_field_is_uninit |
| 1851 | : diag::warn_field_is_uninit; |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 1852 | S.Diag(FieldME->getExprLoc(), diag) << VD; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1853 | } |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 1854 | return; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 1858 | HandleValue(CO->getTrueExpr()); |
| 1859 | HandleValue(CO->getFalseExpr()); |
| 1860 | return; |
| 1861 | } |
| 1862 | |
| 1863 | if (BinaryConditionalOperator *BCO = |
| 1864 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 1865 | HandleValue(BCO->getCommon()); |
| 1866 | HandleValue(BCO->getFalseExpr()); |
| 1867 | return; |
| 1868 | } |
| 1869 | |
| 1870 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 1871 | switch (BO->getOpcode()) { |
| 1872 | default: |
| 1873 | return; |
| 1874 | case(BO_PtrMemD): |
| 1875 | case(BO_PtrMemI): |
| 1876 | HandleValue(BO->getLHS()); |
| 1877 | return; |
| 1878 | case(BO_Comma): |
| 1879 | HandleValue(BO->getRHS()); |
| 1880 | return; |
| 1881 | } |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 1886 | if (E->getCastKind() == CK_LValueToRValue) |
| 1887 | HandleValue(E->getSubExpr()); |
| 1888 | |
| 1889 | Inherited::VisitImplicitCastExpr(E); |
| 1890 | } |
| 1891 | |
| 1892 | void VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 1893 | Expr *Callee = E->getCallee(); |
| 1894 | if (isa<MemberExpr>(Callee)) |
| 1895 | HandleValue(Callee); |
| 1896 | |
| 1897 | Inherited::VisitCXXMemberCallExpr(E); |
| 1898 | } |
| 1899 | }; |
| 1900 | static void CheckInitExprContainsUninitializedFields(Sema &S, Expr *E, |
| 1901 | ValueDecl *VD) { |
| 1902 | UninitializedFieldVisitor(S, VD).HandleExpr(E); |
| 1903 | } |
| 1904 | } // namespace |
| 1905 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1906 | /// ActOnCXXInClassMemberInitializer - This is invoked after parsing an |
Richard Smith | 0ff6f8f | 2011-07-20 00:12:52 +0000 | [diff] [blame] | 1907 | /// in-class initializer for a non-static C++ class member, and after |
| 1908 | /// instantiating an in-class initializer in a class template. Such actions |
| 1909 | /// are deferred until the class is complete. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1910 | void |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1911 | Sema::ActOnCXXInClassMemberInitializer(Decl *D, SourceLocation InitLoc, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1912 | Expr *InitExpr) { |
| 1913 | FieldDecl *FD = cast<FieldDecl>(D); |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1914 | assert(FD->getInClassInitStyle() != ICIS_NoInit && |
| 1915 | "must set init style when field is created"); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1916 | |
| 1917 | if (!InitExpr) { |
| 1918 | FD->setInvalidDecl(); |
| 1919 | FD->removeInClassInitializer(); |
| 1920 | return; |
| 1921 | } |
| 1922 | |
Peter Collingbourne | fef2189 | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 1923 | if (DiagnoseUnexpandedParameterPack(InitExpr, UPPC_Initializer)) { |
| 1924 | FD->setInvalidDecl(); |
| 1925 | FD->removeInClassInitializer(); |
| 1926 | return; |
| 1927 | } |
| 1928 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1929 | if (getDiagnostics().getDiagnosticLevel(diag::warn_field_is_uninit, InitLoc) |
| 1930 | != DiagnosticsEngine::Ignored) { |
| 1931 | CheckInitExprContainsUninitializedFields(*this, InitExpr, FD); |
| 1932 | } |
| 1933 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1934 | ExprResult Init = InitExpr; |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 1935 | if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent()) { |
Sebastian Redl | 772291a | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 1936 | if (isa<InitListExpr>(InitExpr) && isStdInitializerList(FD->getType(), 0)) { |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 1937 | Diag(FD->getLocation(), diag::warn_dangling_std_initializer_list) |
Sebastian Redl | 772291a | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 1938 | << /*at end of ctor*/1 << InitExpr->getSourceRange(); |
| 1939 | } |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 1940 | Expr **Inits = &InitExpr; |
| 1941 | unsigned NumInits = 1; |
| 1942 | InitializedEntity Entity = InitializedEntity::InitializeMember(FD); |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1943 | InitializationKind Kind = FD->getInClassInitStyle() == ICIS_ListInit |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 1944 | ? InitializationKind::CreateDirectList(InitExpr->getLocStart()) |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1945 | : InitializationKind::CreateCopy(InitExpr->getLocStart(), InitLoc); |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 1946 | InitializationSequence Seq(*this, Entity, Kind, Inits, NumInits); |
| 1947 | Init = Seq.Perform(*this, Entity, Kind, MultiExprArg(Inits, NumInits)); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1948 | if (Init.isInvalid()) { |
| 1949 | FD->setInvalidDecl(); |
| 1950 | return; |
| 1951 | } |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 1954 | // C++11 [class.base.init]p7: |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1955 | // The initialization of each base and member constitutes a |
| 1956 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 1957 | Init = ActOnFinishFullExpr(Init.take(), InitLoc); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1958 | if (Init.isInvalid()) { |
| 1959 | FD->setInvalidDecl(); |
| 1960 | return; |
| 1961 | } |
| 1962 | |
| 1963 | InitExpr = Init.release(); |
| 1964 | |
| 1965 | FD->setInClassInitializer(InitExpr); |
| 1966 | } |
| 1967 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1968 | /// \brief Find the direct and/or virtual base specifiers that |
| 1969 | /// correspond to the given base type, for use in base initialization |
| 1970 | /// within a constructor. |
| 1971 | static bool FindBaseInitializer(Sema &SemaRef, |
| 1972 | CXXRecordDecl *ClassDecl, |
| 1973 | QualType BaseType, |
| 1974 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 1975 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 1976 | // First, check for a direct base class. |
| 1977 | DirectBaseSpec = 0; |
| 1978 | for (CXXRecordDecl::base_class_const_iterator Base |
| 1979 | = ClassDecl->bases_begin(); |
| 1980 | Base != ClassDecl->bases_end(); ++Base) { |
| 1981 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 1982 | // We found a direct base of this type. That's what we're |
| 1983 | // initializing. |
| 1984 | DirectBaseSpec = &*Base; |
| 1985 | break; |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | // Check for a virtual base class. |
| 1990 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1991 | // there are no virtual bases. |
| 1992 | VirtualBaseSpec = 0; |
| 1993 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1994 | // We haven't found a base yet; search the class hierarchy for a |
| 1995 | // virtual base class. |
| 1996 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1997 | /*DetectVirtual=*/false); |
| 1998 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1999 | BaseType, Paths)) { |
| 2000 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 2001 | Path != Paths.end(); ++Path) { |
| 2002 | if (Path->back().Base->isVirtual()) { |
| 2003 | VirtualBaseSpec = Path->back().Base; |
| 2004 | break; |
| 2005 | } |
| 2006 | } |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | return DirectBaseSpec || VirtualBaseSpec; |
| 2011 | } |
| 2012 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2013 | /// \brief Handle a C++ member initializer using braced-init-list syntax. |
| 2014 | MemInitResult |
| 2015 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
| 2016 | Scope *S, |
| 2017 | CXXScopeSpec &SS, |
| 2018 | IdentifierInfo *MemberOrBase, |
| 2019 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2020 | const DeclSpec &DS, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2021 | SourceLocation IdLoc, |
| 2022 | Expr *InitList, |
| 2023 | SourceLocation EllipsisLoc) { |
| 2024 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2025 | DS, IdLoc, InitList, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2026 | EllipsisLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | /// \brief Handle a C++ member initializer using parentheses syntax. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2030 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2031 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2032 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2033 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2034 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2035 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2036 | const DeclSpec &DS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2037 | SourceLocation IdLoc, |
| 2038 | SourceLocation LParenLoc, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 2039 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2040 | SourceLocation RParenLoc, |
| 2041 | SourceLocation EllipsisLoc) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2042 | Expr *List = new (Context) ParenListExpr(Context, LParenLoc, |
| 2043 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2044 | RParenLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2045 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2046 | DS, IdLoc, List, EllipsisLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2049 | namespace { |
| 2050 | |
Kaelyn Uhrain | dc98cd0 | 2012-01-11 21:17:51 +0000 | [diff] [blame] | 2051 | // Callback to only accept typo corrections that can be a valid C++ member |
| 2052 | // intializer: either a non-static field member or a base class. |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2053 | class MemInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 2054 | public: |
| 2055 | explicit MemInitializerValidatorCCC(CXXRecordDecl *ClassDecl) |
| 2056 | : ClassDecl(ClassDecl) {} |
| 2057 | |
| 2058 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 2059 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 2060 | if (FieldDecl *Member = dyn_cast<FieldDecl>(ND)) |
| 2061 | return Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl); |
| 2062 | else |
| 2063 | return isa<TypeDecl>(ND); |
| 2064 | } |
| 2065 | return false; |
| 2066 | } |
| 2067 | |
| 2068 | private: |
| 2069 | CXXRecordDecl *ClassDecl; |
| 2070 | }; |
| 2071 | |
| 2072 | } |
| 2073 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2074 | /// \brief Handle a C++ member initializer. |
| 2075 | MemInitResult |
| 2076 | Sema::BuildMemInitializer(Decl *ConstructorD, |
| 2077 | Scope *S, |
| 2078 | CXXScopeSpec &SS, |
| 2079 | IdentifierInfo *MemberOrBase, |
| 2080 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2081 | const DeclSpec &DS, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2082 | SourceLocation IdLoc, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2083 | Expr *Init, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2084 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2085 | if (!ConstructorD) |
| 2086 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2088 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | |
| 2090 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2091 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2092 | if (!Constructor) { |
| 2093 | // The user wrote a constructor initializer on a function that is |
| 2094 | // not a C++ constructor. Ignore the error for now, because we may |
| 2095 | // have more member initializers coming; we'll diagnose it just |
| 2096 | // once in ActOnMemInitializers. |
| 2097 | return true; |
| 2098 | } |
| 2099 | |
| 2100 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2101 | |
| 2102 | // C++ [class.base.init]p2: |
| 2103 | // Names in a mem-initializer-id are looked up in the scope of the |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2104 | // constructor's class and, if not found in that scope, are looked |
| 2105 | // up in the scope containing the constructor's definition. |
| 2106 | // [Note: if the constructor's class contains a member with the |
| 2107 | // same name as a direct or virtual base class of the class, a |
| 2108 | // mem-initializer-id naming the member or base class and composed |
| 2109 | // of a single identifier refers to the class member. A |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2110 | // mem-initializer-id for the hidden base class may be specified |
| 2111 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2112 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2113 | // Look for a member, first. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2115 | = ClassDecl->lookup(MemberOrBase); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2116 | if (!Result.empty()) { |
Peter Collingbourne | dc69be2 | 2011-10-23 18:59:37 +0000 | [diff] [blame] | 2117 | ValueDecl *Member; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2118 | if ((Member = dyn_cast<FieldDecl>(Result.front())) || |
| 2119 | (Member = dyn_cast<IndirectFieldDecl>(Result.front()))) { |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2120 | if (EllipsisLoc.isValid()) |
| 2121 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2122 | << MemberOrBase |
| 2123 | << SourceRange(IdLoc, Init->getSourceRange().getEnd()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2124 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2125 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2126 | } |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2127 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2128 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2129 | // It didn't name a member, so see if it names a class. |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2130 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2131 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2132 | |
| 2133 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2134 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2135 | } else if (DS.getTypeSpecType() == TST_decltype) { |
| 2136 | BaseType = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2137 | } else { |
| 2138 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 2139 | LookupParsedName(R, S, &SS); |
| 2140 | |
| 2141 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 2142 | if (!TyD) { |
| 2143 | if (R.isAmbiguous()) return true; |
| 2144 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 2145 | // We don't want access-control diagnostics here. |
| 2146 | R.suppressDiagnostics(); |
| 2147 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2148 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 2149 | bool NotUnknownSpecialization = false; |
| 2150 | DeclContext *DC = computeDeclContext(SS, false); |
| 2151 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 2152 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 2153 | |
| 2154 | if (!NotUnknownSpecialization) { |
| 2155 | // When the scope specifier can refer to a member of an unknown |
| 2156 | // specialization, we take it as a type name. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 2157 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 2158 | SS.getWithLocInContext(Context), |
| 2159 | *MemberOrBase, IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 2160 | if (BaseType.isNull()) |
| 2161 | return true; |
| 2162 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2163 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 2164 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2165 | } |
| 2166 | } |
| 2167 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2168 | // If no results were found, try to correct typos. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2169 | TypoCorrection Corr; |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2170 | MemInitializerValidatorCCC Validator(ClassDecl); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2171 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2172 | (Corr = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 2173 | Validator, ClassDecl))) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2174 | std::string CorrectedStr(Corr.getAsString(getLangOpts())); |
| 2175 | std::string CorrectedQuotedStr(Corr.getQuoted(getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2176 | if (FieldDecl *Member = Corr.getCorrectionDeclAs<FieldDecl>()) { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2177 | // We have found a non-static data member with a similar |
| 2178 | // name to what was typed; complain and initialize that |
| 2179 | // member. |
| 2180 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 2181 | << MemberOrBase << true << CorrectedQuotedStr |
| 2182 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
| 2183 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 2184 | << CorrectedQuotedStr; |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2185 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2186 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2187 | } else if (TypeDecl *Type = Corr.getCorrectionDeclAs<TypeDecl>()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2188 | const CXXBaseSpecifier *DirectBaseSpec; |
| 2189 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 2190 | if (FindBaseInitializer(*this, ClassDecl, |
| 2191 | Context.getTypeDeclType(Type), |
| 2192 | DirectBaseSpec, VirtualBaseSpec)) { |
| 2193 | // We have found a direct or virtual base class with a |
| 2194 | // similar name to what was typed; complain and initialize |
| 2195 | // that base class. |
| 2196 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2197 | << MemberOrBase << false << CorrectedQuotedStr |
| 2198 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2199 | |
| 2200 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 2201 | : VirtualBaseSpec; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2202 | Diag(BaseSpec->getLocStart(), |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2203 | diag::note_base_class_specified_here) |
| 2204 | << BaseSpec->getType() |
| 2205 | << BaseSpec->getSourceRange(); |
| 2206 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2207 | TyD = Type; |
| 2208 | } |
| 2209 | } |
| 2210 | } |
| 2211 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2212 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2213 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2214 | << MemberOrBase << SourceRange(IdLoc,Init->getSourceRange().getEnd()); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2215 | return true; |
| 2216 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2219 | if (BaseType.isNull()) { |
| 2220 | BaseType = Context.getTypeDeclType(TyD); |
| 2221 | if (SS.isSet()) { |
| 2222 | NestedNameSpecifier *Qualifier = |
| 2223 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2224 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2225 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2226 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2227 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2230 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2231 | if (!TInfo) |
| 2232 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2233 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2234 | return BuildBaseInitializer(BaseType, TInfo, Init, ClassDecl, EllipsisLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2237 | /// Checks a member initializer expression for cases where reference (or |
| 2238 | /// pointer) members are bound to by-value parameters (or their addresses). |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2239 | static void CheckForDanglingReferenceOrPointer(Sema &S, ValueDecl *Member, |
| 2240 | Expr *Init, |
| 2241 | SourceLocation IdLoc) { |
| 2242 | QualType MemberTy = Member->getType(); |
| 2243 | |
| 2244 | // We only handle pointers and references currently. |
| 2245 | // FIXME: Would this be relevant for ObjC object pointers? Or block pointers? |
| 2246 | if (!MemberTy->isReferenceType() && !MemberTy->isPointerType()) |
| 2247 | return; |
| 2248 | |
| 2249 | const bool IsPointer = MemberTy->isPointerType(); |
| 2250 | if (IsPointer) { |
| 2251 | if (const UnaryOperator *Op |
| 2252 | = dyn_cast<UnaryOperator>(Init->IgnoreParenImpCasts())) { |
| 2253 | // The only case we're worried about with pointers requires taking the |
| 2254 | // address. |
| 2255 | if (Op->getOpcode() != UO_AddrOf) |
| 2256 | return; |
| 2257 | |
| 2258 | Init = Op->getSubExpr(); |
| 2259 | } else { |
| 2260 | // We only handle address-of expression initializers for pointers. |
| 2261 | return; |
| 2262 | } |
| 2263 | } |
| 2264 | |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2265 | if (isa<MaterializeTemporaryExpr>(Init->IgnoreParens())) { |
| 2266 | // Taking the address of a temporary will be diagnosed as a hard error. |
| 2267 | if (IsPointer) |
| 2268 | return; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2269 | |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2270 | S.Diag(Init->getExprLoc(), diag::warn_bind_ref_member_to_temporary) |
| 2271 | << Member << Init->getSourceRange(); |
| 2272 | } else if (const DeclRefExpr *DRE |
| 2273 | = dyn_cast<DeclRefExpr>(Init->IgnoreParens())) { |
| 2274 | // We only warn when referring to a non-reference parameter declaration. |
| 2275 | const ParmVarDecl *Parameter = dyn_cast<ParmVarDecl>(DRE->getDecl()); |
| 2276 | if (!Parameter || Parameter->getType()->isReferenceType()) |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2277 | return; |
| 2278 | |
| 2279 | S.Diag(Init->getExprLoc(), |
| 2280 | IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 2281 | : diag::warn_bind_ref_member_to_parameter) |
| 2282 | << Member << Parameter << Init->getSourceRange(); |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2283 | } else { |
| 2284 | // Other initializers are fine. |
| 2285 | return; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2286 | } |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2287 | |
| 2288 | S.Diag(Member->getLocation(), diag::note_ref_or_ptr_member_declared_here) |
| 2289 | << (unsigned)IsPointer; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2292 | MemInitResult |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2293 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr *Init, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2294 | SourceLocation IdLoc) { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2295 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 2296 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 2297 | assert((DirectMember || IndirectMember) && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2298 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 2299 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2300 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Peter Collingbourne | fef2189 | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 2301 | return true; |
| 2302 | |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 2303 | if (Member->isInvalidDecl()) |
| 2304 | return true; |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2305 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2306 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 2307 | // foo(foo) |
| 2308 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 2309 | // TODO: implement -Wuninitialized and fold this into that framework. |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2310 | Expr **Args; |
| 2311 | unsigned NumArgs; |
| 2312 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 2313 | Args = ParenList->getExprs(); |
| 2314 | NumArgs = ParenList->getNumExprs(); |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2315 | } else if (InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2316 | Args = InitList->getInits(); |
| 2317 | NumArgs = InitList->getNumInits(); |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2318 | } else { |
| 2319 | // Template instantiation doesn't reconstruct ParenListExprs for us. |
| 2320 | Args = &Init; |
| 2321 | NumArgs = 1; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2322 | } |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2323 | |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 2324 | if (getDiagnostics().getDiagnosticLevel(diag::warn_field_is_uninit, IdLoc) |
| 2325 | != DiagnosticsEngine::Ignored) |
| 2326 | for (unsigned i = 0; i < NumArgs; ++i) |
| 2327 | // FIXME: Warn about the case when other fields are used before being |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2328 | // initialized. For example, let this field be the i'th field. When |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2329 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 2330 | // fields are used, as they are not yet initialized. |
| 2331 | // Right now we are only handling the case where the i'th field uses |
| 2332 | // itself in its initializer. |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2333 | // Also need to take into account that some fields may be initialized by |
| 2334 | // in-class initializers, see C++11 [class.base.init]p9. |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 2335 | CheckInitExprContainsUninitializedFields(*this, Args[i], Member); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2336 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2337 | SourceRange InitRange = Init->getSourceRange(); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2338 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2339 | if (Member->getType()->isDependentType() || Init->isTypeDependent()) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2340 | // Can't check initialization for a member of dependent type or when |
| 2341 | // any of the arguments are type-dependent expressions. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2342 | DiscardCleanupsInEvaluationContext(); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2343 | } else { |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2344 | bool InitList = false; |
| 2345 | if (isa<InitListExpr>(Init)) { |
| 2346 | InitList = true; |
| 2347 | Args = &Init; |
| 2348 | NumArgs = 1; |
Sebastian Redl | 772291a | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 2349 | |
| 2350 | if (isStdInitializerList(Member->getType(), 0)) { |
| 2351 | Diag(IdLoc, diag::warn_dangling_std_initializer_list) |
| 2352 | << /*at end of ctor*/1 << InitRange; |
| 2353 | } |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2356 | // Initialize the member. |
| 2357 | InitializedEntity MemberEntity = |
| 2358 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 2359 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 2360 | InitializationKind Kind = |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2361 | InitList ? InitializationKind::CreateDirectList(IdLoc) |
| 2362 | : InitializationKind::CreateDirect(IdLoc, InitRange.getBegin(), |
| 2363 | InitRange.getEnd()); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2364 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2365 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 2366 | ExprResult MemberInit = InitSeq.Perform(*this, MemberEntity, Kind, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2367 | MultiExprArg(Args, NumArgs), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2368 | 0); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2369 | if (MemberInit.isInvalid()) |
| 2370 | return true; |
| 2371 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2372 | // C++11 [class.base.init]p7: |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2373 | // The initialization of each base and member constitutes a |
| 2374 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2375 | MemberInit = ActOnFinishFullExpr(MemberInit.get(), InitRange.getBegin()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2376 | if (MemberInit.isInvalid()) |
| 2377 | return true; |
| 2378 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2379 | Init = MemberInit.get(); |
| 2380 | CheckForDanglingReferenceOrPointer(*this, Member, Init, IdLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2383 | if (DirectMember) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2384 | return new (Context) CXXCtorInitializer(Context, DirectMember, IdLoc, |
| 2385 | InitRange.getBegin(), Init, |
| 2386 | InitRange.getEnd()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2387 | } else { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2388 | return new (Context) CXXCtorInitializer(Context, IndirectMember, IdLoc, |
| 2389 | InitRange.getBegin(), Init, |
| 2390 | InitRange.getEnd()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2391 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2394 | MemInitResult |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2395 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, Expr *Init, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2396 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2397 | SourceLocation NameLoc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2398 | if (!LangOpts.CPlusPlus11) |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2399 | return Diag(NameLoc, diag::err_delegating_ctor) |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2400 | << TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2401 | Diag(NameLoc, diag::warn_cxx98_compat_delegating_ctor); |
Sebastian Redl | f9c32eb | 2011-03-12 13:53:51 +0000 | [diff] [blame] | 2402 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2403 | bool InitList = true; |
| 2404 | Expr **Args = &Init; |
| 2405 | unsigned NumArgs = 1; |
| 2406 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 2407 | InitList = false; |
| 2408 | Args = ParenList->getExprs(); |
| 2409 | NumArgs = ParenList->getNumExprs(); |
| 2410 | } |
| 2411 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2412 | SourceRange InitRange = Init->getSourceRange(); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2413 | // Initialize the object. |
| 2414 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 2415 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 2416 | InitializationKind Kind = |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2417 | InitList ? InitializationKind::CreateDirectList(NameLoc) |
| 2418 | : InitializationKind::CreateDirect(NameLoc, InitRange.getBegin(), |
| 2419 | InitRange.getEnd()); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2420 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args, NumArgs); |
| 2421 | ExprResult DelegationInit = InitSeq.Perform(*this, DelegationEntity, Kind, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2422 | MultiExprArg(Args, NumArgs), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2423 | 0); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2424 | if (DelegationInit.isInvalid()) |
| 2425 | return true; |
| 2426 | |
Matt Beaumont-Gay | 2eb0ce3 | 2011-11-01 18:10:22 +0000 | [diff] [blame] | 2427 | assert(cast<CXXConstructExpr>(DelegationInit.get())->getConstructor() && |
| 2428 | "Delegating constructor with no target?"); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2429 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2430 | // C++11 [class.base.init]p7: |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2431 | // The initialization of each base and member constitutes a |
| 2432 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2433 | DelegationInit = ActOnFinishFullExpr(DelegationInit.get(), |
| 2434 | InitRange.getBegin()); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2435 | if (DelegationInit.isInvalid()) |
| 2436 | return true; |
| 2437 | |
Eli Friedman | d21016f | 2012-05-19 23:35:23 +0000 | [diff] [blame] | 2438 | // If we are in a dependent context, template instantiation will |
| 2439 | // perform this type-checking again. Just save the arguments that we |
| 2440 | // received in a ParenListExpr. |
| 2441 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2442 | // of the information that we have about the base |
| 2443 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2444 | // and this approach is far more likely to get the corner cases right. |
| 2445 | if (CurContext->isDependentContext()) |
| 2446 | DelegationInit = Owned(Init); |
| 2447 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2448 | return new (Context) CXXCtorInitializer(Context, TInfo, InitRange.getBegin(), |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2449 | DelegationInit.takeAs<Expr>(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2450 | InitRange.getEnd()); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2454 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2455 | Expr *Init, CXXRecordDecl *ClassDecl, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2456 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2457 | SourceLocation BaseLoc |
| 2458 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2459 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2460 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 2461 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 2462 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2463 | |
| 2464 | // C++ [class.base.init]p2: |
| 2465 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2466 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2467 | // of that class, the mem-initializer is ill-formed. A |
| 2468 | // mem-initializer-list can initialize a base class using any |
| 2469 | // name that denotes that base class type. |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2470 | bool Dependent = BaseType->isDependentType() || Init->isTypeDependent(); |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2471 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2472 | SourceRange InitRange = Init->getSourceRange(); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2473 | if (EllipsisLoc.isValid()) { |
| 2474 | // This is a pack expansion. |
| 2475 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 2476 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2477 | << SourceRange(BaseLoc, InitRange.getEnd()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2478 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2479 | EllipsisLoc = SourceLocation(); |
| 2480 | } |
| 2481 | } else { |
| 2482 | // Check for any unexpanded parameter packs. |
| 2483 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 2484 | return true; |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2485 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2486 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2487 | return true; |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2488 | } |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2489 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2490 | // Check for direct and virtual base classes. |
| 2491 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 2492 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 2493 | if (!Dependent) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2494 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 2495 | BaseType)) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2496 | return BuildDelegatingInitializer(BaseTInfo, Init, ClassDecl); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2497 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2498 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 2499 | VirtualBaseSpec); |
| 2500 | |
| 2501 | // C++ [base.class.init]p2: |
| 2502 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 2503 | // constructor's class or a direct or virtual base of that class, the |
| 2504 | // mem-initializer is ill-formed. |
| 2505 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 2506 | // If the class has any dependent bases, then it's possible that |
| 2507 | // one of those types will resolve to the same type as |
| 2508 | // BaseType. Therefore, just treat this as a dependent base |
| 2509 | // class initialization. FIXME: Should we try to check the |
| 2510 | // initialization anyway? It seems odd. |
| 2511 | if (ClassDecl->hasAnyDependentBases()) |
| 2512 | Dependent = true; |
| 2513 | else |
| 2514 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 2515 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 2516 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | if (Dependent) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2521 | DiscardCleanupsInEvaluationContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2522 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2523 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
| 2524 | /*IsVirtual=*/false, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2525 | InitRange.getBegin(), Init, |
| 2526 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2527 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2528 | |
| 2529 | // C++ [base.class.init]p2: |
| 2530 | // If a mem-initializer-id is ambiguous because it designates both |
| 2531 | // a direct non-virtual base class and an inherited virtual base |
| 2532 | // class, the mem-initializer is ill-formed. |
| 2533 | if (DirectBaseSpec && VirtualBaseSpec) |
| 2534 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2535 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2536 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2537 | CXXBaseSpecifier *BaseSpec = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2538 | if (!BaseSpec) |
| 2539 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 2540 | |
| 2541 | // Initialize the base. |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2542 | bool InitList = true; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2543 | Expr **Args = &Init; |
| 2544 | unsigned NumArgs = 1; |
| 2545 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2546 | InitList = false; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2547 | Args = ParenList->getExprs(); |
| 2548 | NumArgs = ParenList->getNumExprs(); |
| 2549 | } |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2550 | |
| 2551 | InitializedEntity BaseEntity = |
| 2552 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
| 2553 | InitializationKind Kind = |
| 2554 | InitList ? InitializationKind::CreateDirectList(BaseLoc) |
| 2555 | : InitializationKind::CreateDirect(BaseLoc, InitRange.getBegin(), |
| 2556 | InitRange.getEnd()); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2557 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 2558 | ExprResult BaseInit = InitSeq.Perform(*this, BaseEntity, Kind, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2559 | MultiExprArg(Args, NumArgs), 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2560 | if (BaseInit.isInvalid()) |
| 2561 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2562 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2563 | // C++11 [class.base.init]p7: |
| 2564 | // The initialization of each base and member constitutes a |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2565 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2566 | BaseInit = ActOnFinishFullExpr(BaseInit.get(), InitRange.getBegin()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2567 | if (BaseInit.isInvalid()) |
| 2568 | return true; |
| 2569 | |
| 2570 | // If we are in a dependent context, template instantiation will |
| 2571 | // perform this type-checking again. Just save the arguments that we |
| 2572 | // received in a ParenListExpr. |
| 2573 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2574 | // of the information that we have about the base |
| 2575 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2576 | // and this approach is far more likely to get the corner cases right. |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2577 | if (CurContext->isDependentContext()) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2578 | BaseInit = Owned(Init); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2579 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2580 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2581 | BaseSpec->isVirtual(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2582 | InitRange.getBegin(), |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2583 | BaseInit.takeAs<Expr>(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2584 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2587 | // Create a static_cast\<T&&>(expr). |
| 2588 | static Expr *CastForMoving(Sema &SemaRef, Expr *E) { |
| 2589 | QualType ExprType = E->getType(); |
| 2590 | QualType TargetType = SemaRef.Context.getRValueReferenceType(ExprType); |
| 2591 | SourceLocation ExprLoc = E->getLocStart(); |
| 2592 | TypeSourceInfo *TargetLoc = SemaRef.Context.getTrivialTypeSourceInfo( |
| 2593 | TargetType, ExprLoc); |
| 2594 | |
| 2595 | return SemaRef.BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
| 2596 | SourceRange(ExprLoc, ExprLoc), |
| 2597 | E->getSourceRange()).take(); |
| 2598 | } |
| 2599 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2600 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 2601 | /// initialize its base or member. |
| 2602 | enum ImplicitInitializerKind { |
| 2603 | IIK_Default, |
| 2604 | IIK_Copy, |
| 2605 | IIK_Move |
| 2606 | }; |
| 2607 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2608 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2609 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2610 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2611 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2612 | bool IsInheritedVirtualBase, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2613 | CXXCtorInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2614 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2615 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 2616 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2617 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2618 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2619 | |
| 2620 | switch (ImplicitInitKind) { |
| 2621 | case IIK_Default: { |
| 2622 | InitializationKind InitKind |
| 2623 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 2624 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2625 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2626 | break; |
| 2627 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2628 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2629 | case IIK_Move: |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2630 | case IIK_Copy: { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2631 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2632 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 2633 | QualType ParamType = Param->getType().getNonReferenceType(); |
Eli Friedman | cf7c14c | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 2634 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2635 | Expr *CopyCtorArg = |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2636 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2637 | SourceLocation(), Param, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2638 | Constructor->getLocation(), ParamType, |
| 2639 | VK_LValue, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2640 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2641 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(CopyCtorArg)); |
| 2642 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 2643 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 2644 | QualType ArgTy = |
| 2645 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 2646 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2647 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2648 | if (Moving) { |
| 2649 | CopyCtorArg = CastForMoving(SemaRef, CopyCtorArg); |
| 2650 | } |
| 2651 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2652 | CXXCastPath BasePath; |
| 2653 | BasePath.push_back(BaseSpec); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2654 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 2655 | CK_UncheckedDerivedToBase, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2656 | Moving ? VK_XValue : VK_LValue, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2657 | &BasePath).take(); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 2658 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2659 | InitializationKind InitKind |
| 2660 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 2661 | SourceLocation(), SourceLocation()); |
| 2662 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 2663 | &CopyCtorArg, 1); |
| 2664 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2665 | MultiExprArg(&CopyCtorArg, 1)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2666 | break; |
| 2667 | } |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2668 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2669 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2670 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2671 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2672 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2673 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2674 | CXXBaseInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2675 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2676 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 2677 | SourceLocation()), |
| 2678 | BaseSpec->isVirtual(), |
| 2679 | SourceLocation(), |
| 2680 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2681 | SourceLocation(), |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2682 | SourceLocation()); |
| 2683 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2684 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2687 | static bool RefersToRValueRef(Expr *MemRef) { |
| 2688 | ValueDecl *Referenced = cast<MemberExpr>(MemRef)->getMemberDecl(); |
| 2689 | return Referenced->getType()->isRValueReferenceType(); |
| 2690 | } |
| 2691 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2692 | static bool |
| 2693 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2694 | ImplicitInitializerKind ImplicitInitKind, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2695 | FieldDecl *Field, IndirectFieldDecl *Indirect, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2696 | CXXCtorInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2697 | if (Field->isInvalidDecl()) |
| 2698 | return true; |
| 2699 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2700 | SourceLocation Loc = Constructor->getLocation(); |
| 2701 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2702 | if (ImplicitInitKind == IIK_Copy || ImplicitInitKind == IIK_Move) { |
| 2703 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2704 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 2705 | QualType ParamType = Param->getType().getNonReferenceType(); |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 2706 | |
| 2707 | // Suppress copying zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2708 | if (Field->isBitField() && Field->getBitWidthValue(SemaRef.Context) == 0) |
| 2709 | return false; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 2710 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2711 | Expr *MemberExprBase = |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2712 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2713 | SourceLocation(), Param, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2714 | Loc, ParamType, VK_LValue, 0); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2715 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2716 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(MemberExprBase)); |
| 2717 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2718 | if (Moving) { |
| 2719 | MemberExprBase = CastForMoving(SemaRef, MemberExprBase); |
| 2720 | } |
| 2721 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2722 | // Build a reference to this field within the parameter. |
| 2723 | CXXScopeSpec SS; |
| 2724 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 2725 | Sema::LookupMemberName); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2726 | MemberLookup.addDecl(Indirect ? cast<ValueDecl>(Indirect) |
| 2727 | : cast<ValueDecl>(Field), AS_public); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2728 | MemberLookup.resolveKind(); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2729 | ExprResult CtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2730 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2731 | ParamType, Loc, |
| 2732 | /*IsArrow=*/false, |
| 2733 | SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2734 | /*TemplateKWLoc=*/SourceLocation(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2735 | /*FirstQualifierInScope=*/0, |
| 2736 | MemberLookup, |
| 2737 | /*TemplateArgs=*/0); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2738 | if (CtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2739 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2740 | |
| 2741 | // C++11 [class.copy]p15: |
| 2742 | // - if a member m has rvalue reference type T&&, it is direct-initialized |
| 2743 | // with static_cast<T&&>(x.m); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2744 | if (RefersToRValueRef(CtorArg.get())) { |
| 2745 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2748 | // When the field we are copying is an array, create index variables for |
| 2749 | // each dimension of the array. We use these index variables to subscript |
| 2750 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 2751 | // necessary iteration with these index variables. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2752 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2753 | QualType BaseType = Field->getType(); |
| 2754 | QualType SizeType = SemaRef.Context.getSizeType(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2755 | bool InitializingArray = false; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2756 | while (const ConstantArrayType *Array |
| 2757 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2758 | InitializingArray = true; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2759 | // Create the iteration variable for this array index. |
| 2760 | IdentifierInfo *IterationVarName = 0; |
| 2761 | { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2762 | SmallString<8> Str; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2763 | llvm::raw_svector_ostream OS(Str); |
| 2764 | OS << "__i" << IndexVariables.size(); |
| 2765 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 2766 | } |
| 2767 | VarDecl *IterationVar |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2768 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2769 | IterationVarName, SizeType, |
| 2770 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2771 | SC_None, SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2772 | IndexVariables.push_back(IterationVar); |
| 2773 | |
| 2774 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2775 | ExprResult IterationVarRef |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 2776 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2777 | assert(!IterationVarRef.isInvalid() && |
| 2778 | "Reference to invented variable cannot fail!"); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 2779 | IterationVarRef = SemaRef.DefaultLvalueConversion(IterationVarRef.take()); |
| 2780 | assert(!IterationVarRef.isInvalid() && |
| 2781 | "Conversion of invented variable cannot fail!"); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2782 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2783 | // Subscript the array with this iteration variable. |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2784 | CtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CtorArg.take(), Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2785 | IterationVarRef.take(), |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2786 | Loc); |
| 2787 | if (CtorArg.isInvalid()) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2788 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2789 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2790 | BaseType = Array->getElementType(); |
| 2791 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2792 | |
| 2793 | // The array subscript expression is an lvalue, which is wrong for moving. |
| 2794 | if (Moving && InitializingArray) |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2795 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2796 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2797 | // Construct the entity that we will be initializing. For an array, this |
| 2798 | // will be first element in the array, which may require several levels |
| 2799 | // of array-subscript entities. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2800 | SmallVector<InitializedEntity, 4> Entities; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2801 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2802 | if (Indirect) |
| 2803 | Entities.push_back(InitializedEntity::InitializeMember(Indirect)); |
| 2804 | else |
| 2805 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2806 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 2807 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 2808 | 0, |
| 2809 | Entities.back())); |
| 2810 | |
| 2811 | // Direct-initialize to use the copy constructor. |
| 2812 | InitializationKind InitKind = |
| 2813 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 2814 | |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2815 | Expr *CtorArgE = CtorArg.takeAs<Expr>(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2816 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2817 | &CtorArgE, 1); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2818 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2819 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2820 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2821 | MultiExprArg(&CtorArgE, 1)); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2822 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2823 | if (MemberInit.isInvalid()) |
| 2824 | return true; |
| 2825 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2826 | if (Indirect) { |
| 2827 | assert(IndexVariables.size() == 0 && |
| 2828 | "Indirect field improperly initialized"); |
| 2829 | CXXMemberInit |
| 2830 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 2831 | Loc, Loc, |
| 2832 | MemberInit.takeAs<Expr>(), |
| 2833 | Loc); |
| 2834 | } else |
| 2835 | CXXMemberInit = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, |
| 2836 | Loc, MemberInit.takeAs<Expr>(), |
| 2837 | Loc, |
| 2838 | IndexVariables.data(), |
| 2839 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2840 | return false; |
| 2841 | } |
| 2842 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2843 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 2844 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2845 | QualType FieldBaseElementType = |
| 2846 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 2847 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2848 | if (FieldBaseElementType->isRecordType()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2849 | InitializedEntity InitEntity |
| 2850 | = Indirect? InitializedEntity::InitializeMember(Indirect) |
| 2851 | : InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2852 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2853 | InitializationKind::CreateDefault(Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2854 | |
| 2855 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2856 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2857 | InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2858 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2859 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2860 | if (MemberInit.isInvalid()) |
| 2861 | return true; |
| 2862 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2863 | if (Indirect) |
| 2864 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 2865 | Indirect, Loc, |
| 2866 | Loc, |
| 2867 | MemberInit.get(), |
| 2868 | Loc); |
| 2869 | else |
| 2870 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 2871 | Field, Loc, Loc, |
| 2872 | MemberInit.get(), |
| 2873 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2874 | return false; |
| 2875 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2876 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 2877 | if (!Field->getParent()->isUnion()) { |
| 2878 | if (FieldBaseElementType->isReferenceType()) { |
| 2879 | SemaRef.Diag(Constructor->getLocation(), |
| 2880 | diag::err_uninitialized_member_in_ctor) |
| 2881 | << (int)Constructor->isImplicit() |
| 2882 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 2883 | << 0 << Field->getDeclName(); |
| 2884 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 2885 | return true; |
| 2886 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2887 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 2888 | if (FieldBaseElementType.isConstQualified()) { |
| 2889 | SemaRef.Diag(Constructor->getLocation(), |
| 2890 | diag::err_uninitialized_member_in_ctor) |
| 2891 | << (int)Constructor->isImplicit() |
| 2892 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 2893 | << 1 << Field->getDeclName(); |
| 2894 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 2895 | return true; |
| 2896 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2897 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2898 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2899 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2900 | FieldBaseElementType->isObjCRetainableType() && |
| 2901 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_None && |
| 2902 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) { |
Douglas Gregor | 3fe52ff | 2012-07-23 04:23:39 +0000 | [diff] [blame] | 2903 | // ARC: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2904 | // Default-initialize Objective-C pointers to NULL. |
| 2905 | CXXMemberInit |
| 2906 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 2907 | Loc, Loc, |
| 2908 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()), |
| 2909 | Loc); |
| 2910 | return false; |
| 2911 | } |
| 2912 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2913 | // Nothing to initialize. |
| 2914 | CXXMemberInit = 0; |
| 2915 | return false; |
| 2916 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2917 | |
| 2918 | namespace { |
| 2919 | struct BaseAndFieldInfo { |
| 2920 | Sema &S; |
| 2921 | CXXConstructorDecl *Ctor; |
| 2922 | bool AnyErrorsInInits; |
| 2923 | ImplicitInitializerKind IIK; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2924 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2925 | SmallVector<CXXCtorInitializer*, 8> AllToInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2926 | |
| 2927 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 2928 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2929 | bool Generated = Ctor->isImplicit() || Ctor->isDefaulted(); |
| 2930 | if (Generated && Ctor->isCopyConstructor()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2931 | IIK = IIK_Copy; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2932 | else if (Generated && Ctor->isMoveConstructor()) |
| 2933 | IIK = IIK_Move; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2934 | else |
| 2935 | IIK = IIK_Default; |
| 2936 | } |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 2937 | |
| 2938 | bool isImplicitCopyOrMove() const { |
| 2939 | switch (IIK) { |
| 2940 | case IIK_Copy: |
| 2941 | case IIK_Move: |
| 2942 | return true; |
| 2943 | |
| 2944 | case IIK_Default: |
| 2945 | return false; |
| 2946 | } |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2947 | |
| 2948 | llvm_unreachable("Invalid ImplicitInitializerKind!"); |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 2949 | } |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 2950 | |
| 2951 | bool addFieldInitializer(CXXCtorInitializer *Init) { |
| 2952 | AllToInit.push_back(Init); |
| 2953 | |
| 2954 | // Check whether this initializer makes the field "used". |
| 2955 | if (Init->getInit() && Init->getInit()->HasSideEffects(S.Context)) |
| 2956 | S.UnusedPrivateFields.remove(Init->getAnyMember()); |
| 2957 | |
| 2958 | return false; |
| 2959 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2960 | }; |
| 2961 | } |
| 2962 | |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 2963 | /// \brief Determine whether the given indirect field declaration is somewhere |
| 2964 | /// within an anonymous union. |
| 2965 | static bool isWithinAnonymousUnion(IndirectFieldDecl *F) { |
| 2966 | for (IndirectFieldDecl::chain_iterator C = F->chain_begin(), |
| 2967 | CEnd = F->chain_end(); |
| 2968 | C != CEnd; ++C) |
| 2969 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>((*C)->getDeclContext())) |
| 2970 | if (Record->isUnion()) |
| 2971 | return true; |
| 2972 | |
| 2973 | return false; |
| 2974 | } |
| 2975 | |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 2976 | /// \brief Determine whether the given type is an incomplete or zero-lenfgth |
| 2977 | /// array type. |
| 2978 | static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) { |
| 2979 | if (T->isIncompleteArrayType()) |
| 2980 | return true; |
| 2981 | |
| 2982 | while (const ConstantArrayType *ArrayT = Context.getAsConstantArrayType(T)) { |
| 2983 | if (!ArrayT->getSize()) |
| 2984 | return true; |
| 2985 | |
| 2986 | T = ArrayT->getElementType(); |
| 2987 | } |
| 2988 | |
| 2989 | return false; |
| 2990 | } |
| 2991 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2992 | static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2993 | FieldDecl *Field, |
| 2994 | IndirectFieldDecl *Indirect = 0) { |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2995 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2996 | // Overwhelmingly common case: we have a direct initializer for this field. |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 2997 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(Field)) |
| 2998 | return Info.addFieldInitializer(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2999 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3000 | // C++11 [class.base.init]p8: if the entity is a non-static data member that |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3001 | // has a brace-or-equal-initializer, the entity is initialized as specified |
| 3002 | // in [dcl.init]. |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3003 | if (Field->hasInClassInitializer() && !Info.isImplicitCopyOrMove()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3004 | CXXCtorInitializer *Init; |
| 3005 | if (Indirect) |
| 3006 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 3007 | SourceLocation(), |
| 3008 | SourceLocation(), 0, |
| 3009 | SourceLocation()); |
| 3010 | else |
| 3011 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 3012 | SourceLocation(), |
| 3013 | SourceLocation(), 0, |
| 3014 | SourceLocation()); |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3015 | return Info.addFieldInitializer(Init); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
Richard Smith | c115f63 | 2011-09-18 11:14:50 +0000 | [diff] [blame] | 3018 | // Don't build an implicit initializer for union members if none was |
| 3019 | // explicitly specified. |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 3020 | if (Field->getParent()->isUnion() || |
| 3021 | (Indirect && isWithinAnonymousUnion(Indirect))) |
Richard Smith | c115f63 | 2011-09-18 11:14:50 +0000 | [diff] [blame] | 3022 | return false; |
| 3023 | |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3024 | // Don't initialize incomplete or zero-length arrays. |
| 3025 | if (isIncompleteOrZeroLengthArrayType(SemaRef.Context, Field->getType())) |
| 3026 | return false; |
| 3027 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3028 | // Don't try to build an implicit initializer if there were semantic |
| 3029 | // errors in any of the initializers (and therefore we might be |
| 3030 | // missing some that the user actually wrote). |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3031 | if (Info.AnyErrorsInInits || Field->isInvalidDecl()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3032 | return false; |
| 3033 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3034 | CXXCtorInitializer *Init = 0; |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3035 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, |
| 3036 | Indirect, Init)) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3037 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3038 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3039 | if (!Init) |
| 3040 | return false; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3041 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3042 | return Info.addFieldInitializer(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3043 | } |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3044 | |
| 3045 | bool |
| 3046 | Sema::SetDelegatingInitializer(CXXConstructorDecl *Constructor, |
| 3047 | CXXCtorInitializer *Initializer) { |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3048 | assert(Initializer->isDelegatingInitializer()); |
Sean Hunt | 01aacc0 | 2011-05-03 20:43:02 +0000 | [diff] [blame] | 3049 | Constructor->setNumCtorInitializers(1); |
| 3050 | CXXCtorInitializer **initializer = |
| 3051 | new (Context) CXXCtorInitializer*[1]; |
| 3052 | memcpy(initializer, &Initializer, sizeof (CXXCtorInitializer*)); |
| 3053 | Constructor->setCtorInitializers(initializer); |
| 3054 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3055 | if (CXXDestructorDecl *Dtor = LookupDestructor(Constructor->getParent())) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3056 | MarkFunctionReferenced(Initializer->getSourceLocation(), Dtor); |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3057 | DiagnoseUseOfDecl(Dtor, Initializer->getSourceLocation()); |
| 3058 | } |
| 3059 | |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 3060 | DelegatingCtorDecls.push_back(Constructor); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3061 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3062 | return false; |
| 3063 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3064 | |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 3065 | bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, |
| 3066 | CXXCtorInitializer **Initializers, |
| 3067 | unsigned NumInitializers, |
| 3068 | bool AnyErrors) { |
Douglas Gregor | d836c0d | 2011-09-22 23:04:35 +0000 | [diff] [blame] | 3069 | if (Constructor->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3070 | // Just store the initializers as written, they will be checked during |
| 3071 | // instantiation. |
| 3072 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3073 | Constructor->setNumCtorInitializers(NumInitializers); |
| 3074 | CXXCtorInitializer **baseOrMemberInitializers = |
| 3075 | new (Context) CXXCtorInitializer*[NumInitializers]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3076 | memcpy(baseOrMemberInitializers, Initializers, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3077 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 3078 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3079 | } |
Richard Smith | 54b3ba8 | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 3080 | |
| 3081 | // Let template instantiation know whether we had errors. |
| 3082 | if (AnyErrors) |
| 3083 | Constructor->setInvalidDecl(); |
| 3084 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3085 | return false; |
| 3086 | } |
| 3087 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3088 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3089 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3090 | // We need to build the initializer AST according to order of construction |
| 3091 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3092 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 3093 | if (!ClassDecl) |
| 3094 | return true; |
| 3095 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3096 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3097 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3098 | for (unsigned i = 0; i < NumInitializers; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3099 | CXXCtorInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3100 | |
| 3101 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3102 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3103 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3104 | Info.AllBaseFields[Member->getAnyMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3107 | // Keep track of the direct virtual bases. |
| 3108 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 3109 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 3110 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 3111 | if (I->isVirtual()) |
| 3112 | DirectVBases.insert(I); |
| 3113 | } |
| 3114 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3115 | // Push virtual bases before others. |
| 3116 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 3117 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 3118 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3119 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3120 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 3121 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3122 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3123 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3124 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3125 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3126 | VBase, IsInheritedVirtualBase, |
| 3127 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3128 | HadError = true; |
| 3129 | continue; |
| 3130 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3131 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3132 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3133 | } |
| 3134 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3135 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3136 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3137 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3138 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 3139 | // Virtuals are in the virtual base list and already constructed. |
| 3140 | if (Base->isVirtual()) |
| 3141 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3142 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3143 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3144 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 3145 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3146 | } else if (!AnyErrors) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3147 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3148 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3149 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3150 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3151 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3152 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3153 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 3154 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3155 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3156 | } |
| 3157 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3158 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3159 | // Fields. |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3160 | for (DeclContext::decl_iterator Mem = ClassDecl->decls_begin(), |
| 3161 | MemEnd = ClassDecl->decls_end(); |
| 3162 | Mem != MemEnd; ++Mem) { |
| 3163 | if (FieldDecl *F = dyn_cast<FieldDecl>(*Mem)) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3164 | // C++ [class.bit]p2: |
| 3165 | // A declaration for a bit-field that omits the identifier declares an |
| 3166 | // unnamed bit-field. Unnamed bit-fields are not members and cannot be |
| 3167 | // initialized. |
| 3168 | if (F->isUnnamedBitfield()) |
| 3169 | continue; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3170 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3171 | // If we're not generating the implicit copy/move constructor, then we'll |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3172 | // handle anonymous struct/union fields based on their individual |
| 3173 | // indirect fields. |
| 3174 | if (F->isAnonymousStructOrUnion() && Info.IIK == IIK_Default) |
| 3175 | continue; |
| 3176 | |
| 3177 | if (CollectFieldInitializer(*this, Info, F)) |
| 3178 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 3179 | continue; |
| 3180 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3181 | |
| 3182 | // Beyond this point, we only consider default initialization. |
| 3183 | if (Info.IIK != IIK_Default) |
| 3184 | continue; |
| 3185 | |
| 3186 | if (IndirectFieldDecl *F = dyn_cast<IndirectFieldDecl>(*Mem)) { |
| 3187 | if (F->getType()->isIncompleteArrayType()) { |
| 3188 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 3189 | "Incomplete array type is not valid"); |
| 3190 | continue; |
| 3191 | } |
| 3192 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3193 | // Initialize each field of an anonymous struct individually. |
| 3194 | if (CollectFieldInitializer(*this, Info, F->getAnonField(), F)) |
| 3195 | HadError = true; |
| 3196 | |
| 3197 | continue; |
| 3198 | } |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 3199 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3200 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3201 | NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3202 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3203 | Constructor->setNumCtorInitializers(NumInitializers); |
| 3204 | CXXCtorInitializer **baseOrMemberInitializers = |
| 3205 | new (Context) CXXCtorInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3206 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3207 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 3208 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 3209 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3210 | // Constructors implicitly reference the base and member |
| 3211 | // destructors. |
| 3212 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 3213 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3214 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3215 | |
| 3216 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3219 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 3220 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3221 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3222 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 3223 | return static_cast<void *>(RT->getDecl()); |
| 3224 | } |
| 3225 | return static_cast<void *>(Field); |
| 3226 | } |
| 3227 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3228 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3229 | return const_cast<Type*>(Context.getCanonicalType(BaseType).getTypePtr()); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3232 | static void *GetKeyForMember(ASTContext &Context, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3233 | CXXCtorInitializer *Member) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3234 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3235 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 3236 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3237 | // For fields injected into the class via declaration of an anonymous union, |
| 3238 | // use its anonymous union class declaration as the unique key. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3239 | FieldDecl *Field = Member->getAnyMember(); |
| 3240 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3241 | // If the field is a member of an anonymous struct or union, our key |
| 3242 | // is the anonymous record decl that's a direct child of the class. |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 3243 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3244 | if (RD->isAnonymousStructOrUnion()) { |
| 3245 | while (true) { |
| 3246 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 3247 | if (Parent->isAnonymousStructOrUnion()) |
| 3248 | RD = Parent; |
| 3249 | else |
| 3250 | break; |
| 3251 | } |
| 3252 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 3253 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3254 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3255 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 3256 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3259 | static void |
| 3260 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3261 | const CXXConstructorDecl *Constructor, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3262 | CXXCtorInitializer **Inits, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3263 | unsigned NumInits) { |
| 3264 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 3265 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3267 | // Don't check initializers order unless the warning is enabled at the |
| 3268 | // location of at least one initializer. |
| 3269 | bool ShouldCheckOrder = false; |
| 3270 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3271 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3272 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order, |
| 3273 | Init->getSourceLocation()) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 3274 | != DiagnosticsEngine::Ignored) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3275 | ShouldCheckOrder = true; |
| 3276 | break; |
| 3277 | } |
| 3278 | } |
| 3279 | if (!ShouldCheckOrder) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3280 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3281 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3282 | // Build the list of bases and members in the order that they'll |
| 3283 | // actually be initialized. The explicit initializers should be in |
| 3284 | // this same order but may be missing things. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3285 | SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3286 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3287 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3288 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3289 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3290 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3291 | ClassDecl->vbases_begin(), |
| 3292 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3293 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3294 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3295 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3296 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3297 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3298 | if (Base->isVirtual()) |
| 3299 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3300 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3301 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3302 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3303 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3304 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3305 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 3306 | if (Field->isUnnamedBitfield()) |
| 3307 | continue; |
| 3308 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3309 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3312 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 3313 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3314 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3315 | CXXCtorInitializer *PrevInit = 0; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3316 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3317 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3318 | void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3319 | |
| 3320 | // Scan forward to try to find this initializer in the idealized |
| 3321 | // initializers list. |
| 3322 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3323 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3324 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3325 | |
| 3326 | // If we didn't find this initializer, it must be because we |
| 3327 | // scanned past it on a previous iteration. That can only |
| 3328 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 3329 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3330 | Sema::SemaDiagnosticBuilder D = |
| 3331 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 3332 | diag::warn_initializer_out_of_order); |
| 3333 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3334 | if (PrevInit->isAnyMemberInitializer()) |
| 3335 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3336 | else |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3337 | D << 1 << PrevInit->getTypeSourceInfo()->getType(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3338 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3339 | if (Init->isAnyMemberInitializer()) |
| 3340 | D << 0 << Init->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3341 | else |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3342 | D << 1 << Init->getTypeSourceInfo()->getType(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3343 | |
| 3344 | // Move back to the initializer's location in the ideal list. |
| 3345 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3346 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3347 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3348 | |
| 3349 | assert(IdealIndex != NumIdealInits && |
| 3350 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3351 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3352 | |
| 3353 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3354 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3357 | namespace { |
| 3358 | bool CheckRedundantInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3359 | CXXCtorInitializer *Init, |
| 3360 | CXXCtorInitializer *&PrevInit) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3361 | if (!PrevInit) { |
| 3362 | PrevInit = Init; |
| 3363 | return false; |
| 3364 | } |
| 3365 | |
| 3366 | if (FieldDecl *Field = Init->getMember()) |
| 3367 | S.Diag(Init->getSourceLocation(), |
| 3368 | diag::err_multiple_mem_initialization) |
| 3369 | << Field->getDeclName() |
| 3370 | << Init->getSourceRange(); |
| 3371 | else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3372 | const Type *BaseClass = Init->getBaseClass(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3373 | assert(BaseClass && "neither field nor base"); |
| 3374 | S.Diag(Init->getSourceLocation(), |
| 3375 | diag::err_multiple_base_initialization) |
| 3376 | << QualType(BaseClass, 0) |
| 3377 | << Init->getSourceRange(); |
| 3378 | } |
| 3379 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 3380 | << 0 << PrevInit->getSourceRange(); |
| 3381 | |
| 3382 | return true; |
| 3383 | } |
| 3384 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3385 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3386 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 3387 | |
| 3388 | bool CheckRedundantUnionInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3389 | CXXCtorInitializer *Init, |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3390 | RedundantUnionMap &Unions) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3391 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3392 | RecordDecl *Parent = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3393 | NamedDecl *Child = Field; |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3394 | |
| 3395 | while (Parent->isAnonymousStructOrUnion() || Parent->isUnion()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3396 | if (Parent->isUnion()) { |
| 3397 | UnionEntry &En = Unions[Parent]; |
| 3398 | if (En.first && En.first != Child) { |
| 3399 | S.Diag(Init->getSourceLocation(), |
| 3400 | diag::err_multiple_mem_union_initialization) |
| 3401 | << Field->getDeclName() |
| 3402 | << Init->getSourceRange(); |
| 3403 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 3404 | << 0 << En.second->getSourceRange(); |
| 3405 | return true; |
David Blaikie | 5bbe816 | 2011-11-12 20:54:14 +0000 | [diff] [blame] | 3406 | } |
| 3407 | if (!En.first) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3408 | En.first = Child; |
| 3409 | En.second = Init; |
| 3410 | } |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3411 | if (!Parent->isAnonymousStructOrUnion()) |
| 3412 | return false; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | Child = Parent; |
| 3416 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3417 | } |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3418 | |
| 3419 | return false; |
| 3420 | } |
| 3421 | } |
| 3422 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3423 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3424 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3425 | SourceLocation ColonLoc, |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 3426 | CXXCtorInitializer **meminits, |
| 3427 | unsigned NumMemInits, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3428 | bool AnyErrors) { |
| 3429 | if (!ConstructorDecl) |
| 3430 | return; |
| 3431 | |
| 3432 | AdjustDeclIfTemplate(ConstructorDecl); |
| 3433 | |
| 3434 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3435 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3436 | |
| 3437 | if (!Constructor) { |
| 3438 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 3439 | return; |
| 3440 | } |
| 3441 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3442 | CXXCtorInitializer **MemInits = |
| 3443 | reinterpret_cast<CXXCtorInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3444 | |
| 3445 | // Mapping for the duplicate initializers check. |
| 3446 | // For member initializers, this is keyed with a FieldDecl*. |
| 3447 | // For base initializers, this is keyed with a Type*. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3448 | llvm::DenseMap<void*, CXXCtorInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3449 | |
| 3450 | // Mapping for the inconsistent anonymous-union initializers check. |
| 3451 | RedundantUnionMap MemberUnions; |
| 3452 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3453 | bool HadError = false; |
| 3454 | for (unsigned i = 0; i < NumMemInits; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3455 | CXXCtorInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3456 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 3457 | // Set the source order index. |
| 3458 | Init->setSourceOrder(i); |
| 3459 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3460 | if (Init->isAnyMemberInitializer()) { |
| 3461 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3462 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 3463 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 3464 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3465 | } else if (Init->isBaseInitializer()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3466 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 3467 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 3468 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3469 | } else { |
| 3470 | assert(Init->isDelegatingInitializer()); |
| 3471 | // This must be the only initializer |
Richard Smith | a6ddea6 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 3472 | if (NumMemInits != 1) { |
| 3473 | Diag(Init->getSourceLocation(), |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3474 | diag::err_delegating_initializer_alone) |
Richard Smith | a6ddea6 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 3475 | << Init->getSourceRange() << MemInits[i ? 0 : 1]->getSourceRange(); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3476 | // We will treat this as being the only initializer. |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3477 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3478 | SetDelegatingInitializer(Constructor, MemInits[i]); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3479 | // Return immediately as the initializer is set. |
| 3480 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3481 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3484 | if (HadError) |
| 3485 | return; |
| 3486 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3487 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 3488 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3489 | SetCtorInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3492 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3493 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 3494 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 416f63e | 2011-09-18 12:11:43 +0000 | [diff] [blame] | 3495 | // Ignore dependent contexts. Also ignore unions, since their members never |
| 3496 | // have destructors implicitly called. |
| 3497 | if (ClassDecl->isDependentContext() || ClassDecl->isUnion()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3498 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3499 | |
| 3500 | // FIXME: all the access-control diagnostics are positioned on the |
| 3501 | // field/base declaration. That's probably good; that said, the |
| 3502 | // user might reasonably want to know why the destructor is being |
| 3503 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3504 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3505 | // Non-static data members. |
| 3506 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 3507 | E = ClassDecl->field_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3508 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 3509 | if (Field->isInvalidDecl()) |
| 3510 | continue; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3511 | |
| 3512 | // Don't destroy incomplete or zero-length arrays. |
| 3513 | if (isIncompleteOrZeroLengthArrayType(Context, Field->getType())) |
| 3514 | continue; |
| 3515 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3516 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 3517 | |
| 3518 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 3519 | if (!RT) |
| 3520 | continue; |
| 3521 | |
| 3522 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3523 | if (FieldClassDecl->isInvalidDecl()) |
| 3524 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3525 | if (FieldClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3526 | continue; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 3527 | // The destructor for an implicit anonymous union member is never invoked. |
| 3528 | if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion()) |
| 3529 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3530 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3531 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3532 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3533 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3534 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3535 | << Field->getDeclName() |
| 3536 | << FieldType); |
| 3537 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3538 | MarkFunctionReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3539 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3542 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 3543 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3544 | // Bases. |
| 3545 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3546 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3547 | // Bases are always records in a well-formed non-dependent class. |
| 3548 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 3549 | |
| 3550 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3551 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3552 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3553 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3554 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3555 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 3556 | if (BaseClassDecl->isInvalidDecl()) |
| 3557 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3558 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3559 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3560 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3561 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3562 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3563 | |
| 3564 | // FIXME: caret should be on the start of the class name |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3565 | CheckDestructorAccess(Base->getLocStart(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3566 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3567 | << Base->getType() |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 3568 | << Base->getSourceRange(), |
| 3569 | Context.getTypeDeclType(ClassDecl)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3570 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3571 | MarkFunctionReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3572 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3576 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 3577 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3578 | |
| 3579 | // Bases are always records in a well-formed non-dependent class. |
John McCall | 63f5578 | 2012-04-09 21:51:56 +0000 | [diff] [blame] | 3580 | const RecordType *RT = VBase->getType()->castAs<RecordType>(); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3581 | |
| 3582 | // Ignore direct virtual bases. |
| 3583 | if (DirectVirtualBases.count(RT)) |
| 3584 | continue; |
| 3585 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3586 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3587 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 3588 | if (BaseClassDecl->isInvalidDecl()) |
| 3589 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3590 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3591 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3592 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3593 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3594 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3595 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3596 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 63f5578 | 2012-04-09 21:51:56 +0000 | [diff] [blame] | 3597 | << VBase->getType(), |
| 3598 | Context.getTypeDeclType(ClassDecl)); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3599 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3600 | MarkFunctionReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3601 | DiagnoseUseOfDecl(Dtor, Location); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3602 | } |
| 3603 | } |
| 3604 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3605 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 3606 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 3607 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3608 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3609 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3610 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3611 | SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3614 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3615 | unsigned DiagID, AbstractDiagSelID SelID) { |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3616 | class NonAbstractTypeDiagnoser : public TypeDiagnoser { |
| 3617 | unsigned DiagID; |
| 3618 | AbstractDiagSelID SelID; |
| 3619 | |
| 3620 | public: |
| 3621 | NonAbstractTypeDiagnoser(unsigned DiagID, AbstractDiagSelID SelID) |
| 3622 | : TypeDiagnoser(DiagID == 0), DiagID(DiagID), SelID(SelID) { } |
| 3623 | |
| 3624 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
Eli Friedman | 2217f85 | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 3625 | if (Suppressed) return; |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3626 | if (SelID == -1) |
| 3627 | S.Diag(Loc, DiagID) << T; |
| 3628 | else |
| 3629 | S.Diag(Loc, DiagID) << SelID << T; |
| 3630 | } |
| 3631 | } Diagnoser(DiagID, SelID); |
| 3632 | |
| 3633 | return RequireNonAbstractType(Loc, T, Diagnoser); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 3636 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3637 | TypeDiagnoser &Diagnoser) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3638 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3639 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3640 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 3641 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3642 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3643 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3644 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3645 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3646 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3647 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3648 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3649 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3650 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3651 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3652 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3653 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3654 | if (!RT) |
| 3655 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3656 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3657 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3658 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3659 | // We can't answer whether something is abstract until it has a |
| 3660 | // definition. If it's currently being defined, we'll walk back |
| 3661 | // over all the declarations when we have a full definition. |
| 3662 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 3663 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3664 | return false; |
| 3665 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3666 | if (!RD->isAbstract()) |
| 3667 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3668 | |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3669 | Diagnoser.diagnose(*this, Loc, T); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3670 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3671 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3672 | return true; |
| 3673 | } |
| 3674 | |
| 3675 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 3676 | // Check if we've already emitted the list of pure virtual functions |
| 3677 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3678 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3679 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3680 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3681 | CXXFinalOverriderMap FinalOverriders; |
| 3682 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3683 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 3684 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 3685 | // more than once. |
| 3686 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 3687 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3688 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 3689 | MEnd = FinalOverriders.end(); |
| 3690 | M != MEnd; |
| 3691 | ++M) { |
| 3692 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 3693 | SOEnd = M->second.end(); |
| 3694 | SO != SOEnd; ++SO) { |
| 3695 | // C++ [class.abstract]p4: |
| 3696 | // A class is abstract if it contains or inherits at least one |
| 3697 | // pure virtual function for which the final overrider is pure |
| 3698 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3699 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3700 | // |
| 3701 | if (SO->second.size() != 1) |
| 3702 | continue; |
| 3703 | |
| 3704 | if (!SO->second.front().Method->isPure()) |
| 3705 | continue; |
| 3706 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 3707 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 3708 | continue; |
| 3709 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3710 | Diag(SO->second.front().Method->getLocation(), |
| 3711 | diag::note_pure_virtual_function) |
Chandler Carruth | 45f11b7 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 3712 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3713 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
| 3716 | if (!PureVirtualClassDiagSet) |
| 3717 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 3718 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3721 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3722 | struct AbstractUsageInfo { |
| 3723 | Sema &S; |
| 3724 | CXXRecordDecl *Record; |
| 3725 | CanQualType AbstractType; |
| 3726 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3727 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3728 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 3729 | : S(S), Record(Record), |
| 3730 | AbstractType(S.Context.getCanonicalType( |
| 3731 | S.Context.getTypeDeclType(Record))), |
| 3732 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3733 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3734 | void DiagnoseAbstractType() { |
| 3735 | if (Invalid) return; |
| 3736 | S.DiagnoseAbstractType(Record); |
| 3737 | Invalid = true; |
| 3738 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3739 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3740 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 3741 | }; |
| 3742 | |
| 3743 | struct CheckAbstractUsage { |
| 3744 | AbstractUsageInfo &Info; |
| 3745 | const NamedDecl *Ctx; |
| 3746 | |
| 3747 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 3748 | : Info(Info), Ctx(Ctx) {} |
| 3749 | |
| 3750 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3751 | switch (TL.getTypeLocClass()) { |
| 3752 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 3753 | #define TYPELOC(CLASS, PARENT) \ |
| 3754 | case TypeLoc::CLASS: Check(cast<CLASS##TypeLoc>(TL), Sel); break; |
| 3755 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3756 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3757 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3758 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3759 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3760 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 3761 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
Douglas Gregor | 7019186 | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 3762 | if (!TL.getArg(I)) |
| 3763 | continue; |
| 3764 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3765 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 3766 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3767 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3768 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3769 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3770 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3771 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 3772 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3773 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3774 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3775 | // Visit the type parameters from a permissive context. |
| 3776 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 3777 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 3778 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 3779 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 3780 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 3781 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3782 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3783 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3784 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3785 | // Visit pointee types from a permissive context. |
| 3786 | #define CheckPolymorphic(Type) \ |
| 3787 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 3788 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 3789 | } |
| 3790 | CheckPolymorphic(PointerTypeLoc) |
| 3791 | CheckPolymorphic(ReferenceTypeLoc) |
| 3792 | CheckPolymorphic(MemberPointerTypeLoc) |
| 3793 | CheckPolymorphic(BlockPointerTypeLoc) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3794 | CheckPolymorphic(AtomicTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3795 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3796 | /// Handle all the types we haven't given a more specific |
| 3797 | /// implementation for above. |
| 3798 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3799 | // Every other kind of type that we haven't called out already |
| 3800 | // that has an inner type is either (1) sugar or (2) contains that |
| 3801 | // inner type in some way as a subobject. |
| 3802 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 3803 | return Visit(Next, Sel); |
| 3804 | |
| 3805 | // If there's no inner type and we're in a permissive context, |
| 3806 | // don't diagnose. |
| 3807 | if (Sel == Sema::AbstractNone) return; |
| 3808 | |
| 3809 | // Check whether the type matches the abstract type. |
| 3810 | QualType T = TL.getType(); |
| 3811 | if (T->isArrayType()) { |
| 3812 | Sel = Sema::AbstractArrayType; |
| 3813 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3814 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3815 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 3816 | if (CT != Info.AbstractType) return; |
| 3817 | |
| 3818 | // It matched; do some magic. |
| 3819 | if (Sel == Sema::AbstractArrayType) { |
| 3820 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 3821 | << T << TL.getSourceRange(); |
| 3822 | } else { |
| 3823 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 3824 | << Sel << T << TL.getSourceRange(); |
| 3825 | } |
| 3826 | Info.DiagnoseAbstractType(); |
| 3827 | } |
| 3828 | }; |
| 3829 | |
| 3830 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 3831 | Sema::AbstractDiagSelID Sel) { |
| 3832 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 3833 | } |
| 3834 | |
| 3835 | } |
| 3836 | |
| 3837 | /// Check for invalid uses of an abstract type in a method declaration. |
| 3838 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 3839 | CXXMethodDecl *MD) { |
| 3840 | // No need to do the check on definitions, which require that |
| 3841 | // the return/param types be complete. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3842 | if (MD->doesThisDeclarationHaveABody()) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3843 | return; |
| 3844 | |
| 3845 | // For safety's sake, just ignore it if we don't have type source |
| 3846 | // information. This should never happen for non-implicit methods, |
| 3847 | // but... |
| 3848 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 3849 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 3850 | } |
| 3851 | |
| 3852 | /// Check for invalid uses of an abstract type within a class definition. |
| 3853 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 3854 | CXXRecordDecl *RD) { |
| 3855 | for (CXXRecordDecl::decl_iterator |
| 3856 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 3857 | Decl *D = *I; |
| 3858 | if (D->isImplicit()) continue; |
| 3859 | |
| 3860 | // Methods and method templates. |
| 3861 | if (isa<CXXMethodDecl>(D)) { |
| 3862 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 3863 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 3864 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 3865 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 3866 | |
| 3867 | // Fields and static variables. |
| 3868 | } else if (isa<FieldDecl>(D)) { |
| 3869 | FieldDecl *FD = cast<FieldDecl>(D); |
| 3870 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 3871 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 3872 | } else if (isa<VarDecl>(D)) { |
| 3873 | VarDecl *VD = cast<VarDecl>(D); |
| 3874 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 3875 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 3876 | |
| 3877 | // Nested classes and class templates. |
| 3878 | } else if (isa<CXXRecordDecl>(D)) { |
| 3879 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 3880 | } else if (isa<ClassTemplateDecl>(D)) { |
| 3881 | CheckAbstractClassUsage(Info, |
| 3882 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 3883 | } |
| 3884 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3887 | /// \brief Perform semantic checks on a class definition that has been |
| 3888 | /// completing, introducing implicitly-declared members, checking for |
| 3889 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3890 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 3891 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3892 | return; |
| 3893 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3894 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 3895 | AbstractUsageInfo Info(*this, Record); |
| 3896 | CheckAbstractClassUsage(Info, Record); |
| 3897 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3898 | |
| 3899 | // If this is not an aggregate type and has no user-declared constructor, |
| 3900 | // complain about any non-static data members of reference or const scalar |
| 3901 | // type, since they will never get initializers. |
| 3902 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
Douglas Gregor | 5e058eb | 2012-02-09 02:20:38 +0000 | [diff] [blame] | 3903 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor() && |
| 3904 | !Record->isLambda()) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3905 | bool Complained = false; |
| 3906 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 3907 | FEnd = Record->field_end(); |
| 3908 | F != FEnd; ++F) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3909 | if (F->hasInClassInitializer() || F->isUnnamedBitfield()) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3910 | continue; |
| 3911 | |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3912 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 3913 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3914 | if (!Complained) { |
| 3915 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 3916 | << Record->getTagKind() << Record; |
| 3917 | Complained = true; |
| 3918 | } |
| 3919 | |
| 3920 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 3921 | << F->getType()->isReferenceType() |
| 3922 | << F->getDeclName(); |
| 3923 | } |
| 3924 | } |
| 3925 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3926 | |
Anders Carlsson | a5c6c2a | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 3927 | if (Record->isDynamicClass() && !Record->isDependentType()) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3928 | DynamicClasses.push_back(Record); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3929 | |
| 3930 | if (Record->getIdentifier()) { |
| 3931 | // C++ [class.mem]p13: |
| 3932 | // If T is the name of a class, then each of the following shall have a |
| 3933 | // name different from T: |
| 3934 | // - every member of every anonymous union that is a member of class T. |
| 3935 | // |
| 3936 | // C++ [class.mem]p14: |
| 3937 | // In addition, if class T has a user-declared constructor (12.1), every |
| 3938 | // non-static data member of class T shall have a name different from T. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3939 | DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
| 3940 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 3941 | ++I) { |
| 3942 | NamedDecl *D = *I; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3943 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 3944 | isa<IndirectFieldDecl>(D)) { |
| 3945 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 3946 | << D->getDeclName(); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3947 | break; |
| 3948 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3949 | } |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3950 | } |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3951 | |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 3952 | // Warn if the class has virtual methods but non-virtual public destructor. |
Douglas Gregor | f4b793c | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 3953 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3954 | CXXDestructorDecl *dtor = Record->getDestructor(); |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 3955 | if (!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3956 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 3957 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 3958 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3959 | |
David Blaikie | b6b5b97 | 2012-09-21 03:21:07 +0000 | [diff] [blame] | 3960 | if (Record->isAbstract() && Record->hasAttr<FinalAttr>()) { |
| 3961 | Diag(Record->getLocation(), diag::warn_abstract_final_class); |
| 3962 | DiagnoseAbstractType(Record); |
| 3963 | } |
| 3964 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3965 | if (!Record->isDependentType()) { |
| 3966 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 3967 | MEnd = Record->method_end(); |
| 3968 | M != MEnd; ++M) { |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3969 | // See if a method overloads virtual methods in a base |
| 3970 | // class without overriding any. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3971 | if (!M->isStatic()) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3972 | DiagnoseHiddenVirtualMethods(Record, *M); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3973 | |
| 3974 | // Check whether the explicitly-defaulted special members are valid. |
| 3975 | if (!M->isInvalidDecl() && M->isExplicitlyDefaulted()) |
| 3976 | CheckExplicitlyDefaultedSpecialMember(*M); |
| 3977 | |
| 3978 | // For an explicitly defaulted or deleted special member, we defer |
| 3979 | // determining triviality until the class is complete. That time is now! |
| 3980 | if (!M->isImplicit() && !M->isUserProvided()) { |
| 3981 | CXXSpecialMember CSM = getSpecialMember(*M); |
| 3982 | if (CSM != CXXInvalid) { |
| 3983 | M->setTrivial(SpecialMemberIsTrivial(*M, CSM)); |
| 3984 | |
| 3985 | // Inform the class that we've finished declaring this member. |
| 3986 | Record->finishedDefaultedOrDeletedMember(*M); |
| 3987 | } |
| 3988 | } |
| 3989 | } |
| 3990 | } |
| 3991 | |
| 3992 | // C++11 [dcl.constexpr]p8: A constexpr specifier for a non-static member |
| 3993 | // function that is not a constructor declares that member function to be |
| 3994 | // const. [...] The class of which that function is a member shall be |
| 3995 | // a literal type. |
| 3996 | // |
| 3997 | // If the class has virtual bases, any constexpr members will already have |
| 3998 | // been diagnosed by the checks performed on the member declaration, so |
| 3999 | // suppress this (less useful) diagnostic. |
| 4000 | // |
| 4001 | // We delay this until we know whether an explicitly-defaulted (or deleted) |
| 4002 | // destructor for the class is trivial. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4003 | if (LangOpts.CPlusPlus11 && !Record->isDependentType() && |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4004 | !Record->isLiteral() && !Record->getNumVBases()) { |
| 4005 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 4006 | MEnd = Record->method_end(); |
| 4007 | M != MEnd; ++M) { |
| 4008 | if (M->isConstexpr() && M->isInstance() && !isa<CXXConstructorDecl>(*M)) { |
| 4009 | switch (Record->getTemplateSpecializationKind()) { |
| 4010 | case TSK_ImplicitInstantiation: |
| 4011 | case TSK_ExplicitInstantiationDeclaration: |
| 4012 | case TSK_ExplicitInstantiationDefinition: |
| 4013 | // If a template instantiates to a non-literal type, but its members |
| 4014 | // instantiate to constexpr functions, the template is technically |
| 4015 | // ill-formed, but we allow it for sanity. |
| 4016 | continue; |
| 4017 | |
| 4018 | case TSK_Undeclared: |
| 4019 | case TSK_ExplicitSpecialization: |
| 4020 | RequireLiteralType(M->getLocation(), Context.getRecordType(Record), |
| 4021 | diag::err_constexpr_method_non_literal); |
| 4022 | break; |
| 4023 | } |
| 4024 | |
| 4025 | // Only produce one error per class. |
| 4026 | break; |
| 4027 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4028 | } |
| 4029 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4030 | |
| 4031 | // Declare inherited constructors. We do this eagerly here because: |
| 4032 | // - The standard requires an eager diagnostic for conflicting inherited |
| 4033 | // constructors from different classes. |
| 4034 | // - The lazy declaration of the other implicit constructors is so as to not |
| 4035 | // waste space and performance on classes that are not meant to be |
| 4036 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
| 4037 | // have inherited constructors. |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 4038 | DeclareInheritedConstructors(Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4041 | /// Is the special member function which would be selected to perform the |
| 4042 | /// specified operation on the specified class type a constexpr constructor? |
| 4043 | static bool specialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 4044 | Sema::CXXSpecialMember CSM, |
| 4045 | bool ConstArg) { |
| 4046 | Sema::SpecialMemberOverloadResult *SMOR = |
| 4047 | S.LookupSpecialMember(ClassDecl, CSM, ConstArg, |
| 4048 | false, false, false, false); |
| 4049 | if (!SMOR || !SMOR->getMethod()) |
| 4050 | // A constructor we wouldn't select can't be "involved in initializing" |
| 4051 | // anything. |
| 4052 | return true; |
| 4053 | return SMOR->getMethod()->isConstexpr(); |
| 4054 | } |
| 4055 | |
| 4056 | /// Determine whether the specified special member function would be constexpr |
| 4057 | /// if it were implicitly defined. |
| 4058 | static bool defaultedSpecialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 4059 | Sema::CXXSpecialMember CSM, |
| 4060 | bool ConstArg) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4061 | if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4062 | return false; |
| 4063 | |
| 4064 | // C++11 [dcl.constexpr]p4: |
| 4065 | // In the definition of a constexpr constructor [...] |
| 4066 | switch (CSM) { |
| 4067 | case Sema::CXXDefaultConstructor: |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4068 | // Since default constructor lookup is essentially trivial (and cannot |
| 4069 | // involve, for instance, template instantiation), we compute whether a |
| 4070 | // defaulted default constructor is constexpr directly within CXXRecordDecl. |
| 4071 | // |
| 4072 | // This is important for performance; we need to know whether the default |
| 4073 | // constructor is constexpr to determine whether the type is a literal type. |
| 4074 | return ClassDecl->defaultedDefaultConstructorIsConstexpr(); |
| 4075 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4076 | case Sema::CXXCopyConstructor: |
| 4077 | case Sema::CXXMoveConstructor: |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4078 | // For copy or move constructors, we need to perform overload resolution. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4079 | break; |
| 4080 | |
| 4081 | case Sema::CXXCopyAssignment: |
| 4082 | case Sema::CXXMoveAssignment: |
| 4083 | case Sema::CXXDestructor: |
| 4084 | case Sema::CXXInvalid: |
| 4085 | return false; |
| 4086 | } |
| 4087 | |
| 4088 | // -- if the class is a non-empty union, or for each non-empty anonymous |
| 4089 | // union member of a non-union class, exactly one non-static data member |
| 4090 | // shall be initialized; [DR1359] |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4091 | // |
| 4092 | // If we squint, this is guaranteed, since exactly one non-static data member |
| 4093 | // will be initialized (if the constructor isn't deleted), we just don't know |
| 4094 | // which one. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4095 | if (ClassDecl->isUnion()) |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4096 | return true; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4097 | |
| 4098 | // -- the class shall not have any virtual base classes; |
| 4099 | if (ClassDecl->getNumVBases()) |
| 4100 | return false; |
| 4101 | |
| 4102 | // -- every constructor involved in initializing [...] base class |
| 4103 | // sub-objects shall be a constexpr constructor; |
| 4104 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4105 | BEnd = ClassDecl->bases_end(); |
| 4106 | B != BEnd; ++B) { |
| 4107 | const RecordType *BaseType = B->getType()->getAs<RecordType>(); |
| 4108 | if (!BaseType) continue; |
| 4109 | |
| 4110 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4111 | if (!specialMemberIsConstexpr(S, BaseClassDecl, CSM, ConstArg)) |
| 4112 | return false; |
| 4113 | } |
| 4114 | |
| 4115 | // -- every constructor involved in initializing non-static data members |
| 4116 | // [...] shall be a constexpr constructor; |
| 4117 | // -- every non-static data member and base class sub-object shall be |
| 4118 | // initialized |
| 4119 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4120 | FEnd = ClassDecl->field_end(); |
| 4121 | F != FEnd; ++F) { |
| 4122 | if (F->isInvalidDecl()) |
| 4123 | continue; |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4124 | if (const RecordType *RecordTy = |
| 4125 | S.Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4126 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4127 | if (!specialMemberIsConstexpr(S, FieldRecDecl, CSM, ConstArg)) |
| 4128 | return false; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4129 | } |
| 4130 | } |
| 4131 | |
| 4132 | // All OK, it's constexpr! |
| 4133 | return true; |
| 4134 | } |
| 4135 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4136 | static Sema::ImplicitExceptionSpecification |
| 4137 | computeImplicitExceptionSpec(Sema &S, SourceLocation Loc, CXXMethodDecl *MD) { |
| 4138 | switch (S.getSpecialMember(MD)) { |
| 4139 | case Sema::CXXDefaultConstructor: |
| 4140 | return S.ComputeDefaultedDefaultCtorExceptionSpec(Loc, MD); |
| 4141 | case Sema::CXXCopyConstructor: |
| 4142 | return S.ComputeDefaultedCopyCtorExceptionSpec(MD); |
| 4143 | case Sema::CXXCopyAssignment: |
| 4144 | return S.ComputeDefaultedCopyAssignmentExceptionSpec(MD); |
| 4145 | case Sema::CXXMoveConstructor: |
| 4146 | return S.ComputeDefaultedMoveCtorExceptionSpec(MD); |
| 4147 | case Sema::CXXMoveAssignment: |
| 4148 | return S.ComputeDefaultedMoveAssignmentExceptionSpec(MD); |
| 4149 | case Sema::CXXDestructor: |
| 4150 | return S.ComputeDefaultedDtorExceptionSpec(MD); |
| 4151 | case Sema::CXXInvalid: |
| 4152 | break; |
| 4153 | } |
| 4154 | llvm_unreachable("only special members have implicit exception specs"); |
| 4155 | } |
| 4156 | |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4157 | static void |
| 4158 | updateExceptionSpec(Sema &S, FunctionDecl *FD, const FunctionProtoType *FPT, |
| 4159 | const Sema::ImplicitExceptionSpecification &ExceptSpec) { |
| 4160 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 4161 | ExceptSpec.getEPI(EPI); |
| 4162 | const FunctionProtoType *NewFPT = cast<FunctionProtoType>( |
| 4163 | S.Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 4164 | FPT->getNumArgs(), EPI)); |
| 4165 | FD->setType(QualType(NewFPT, 0)); |
| 4166 | } |
| 4167 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4168 | void Sema::EvaluateImplicitExceptionSpec(SourceLocation Loc, CXXMethodDecl *MD) { |
| 4169 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 4170 | if (FPT->getExceptionSpecType() != EST_Unevaluated) |
| 4171 | return; |
| 4172 | |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4173 | // Evaluate the exception specification. |
| 4174 | ImplicitExceptionSpecification ExceptSpec = |
| 4175 | computeImplicitExceptionSpec(*this, Loc, MD); |
| 4176 | |
| 4177 | // Update the type of the special member to use it. |
| 4178 | updateExceptionSpec(*this, MD, FPT, ExceptSpec); |
| 4179 | |
| 4180 | // A user-provided destructor can be defined outside the class. When that |
| 4181 | // happens, be sure to update the exception specification on both |
| 4182 | // declarations. |
| 4183 | const FunctionProtoType *CanonicalFPT = |
| 4184 | MD->getCanonicalDecl()->getType()->castAs<FunctionProtoType>(); |
| 4185 | if (CanonicalFPT->getExceptionSpecType() == EST_Unevaluated) |
| 4186 | updateExceptionSpec(*this, MD->getCanonicalDecl(), |
| 4187 | CanonicalFPT, ExceptSpec); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4190 | void Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD) { |
| 4191 | CXXRecordDecl *RD = MD->getParent(); |
| 4192 | CXXSpecialMember CSM = getSpecialMember(MD); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4193 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4194 | assert(MD->isExplicitlyDefaulted() && CSM != CXXInvalid && |
| 4195 | "not an explicitly-defaulted special member"); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4196 | |
| 4197 | // Whether this was the first-declared instance of the constructor. |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4198 | // This affects whether we implicitly add an exception spec and constexpr. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4199 | bool First = MD == MD->getCanonicalDecl(); |
| 4200 | |
| 4201 | bool HadError = false; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4202 | |
| 4203 | // C++11 [dcl.fct.def.default]p1: |
| 4204 | // A function that is explicitly defaulted shall |
| 4205 | // -- be a special member function (checked elsewhere), |
| 4206 | // -- have the same type (except for ref-qualifiers, and except that a |
| 4207 | // copy operation can take a non-const reference) as an implicit |
| 4208 | // declaration, and |
| 4209 | // -- not have default arguments. |
| 4210 | unsigned ExpectedParams = 1; |
| 4211 | if (CSM == CXXDefaultConstructor || CSM == CXXDestructor) |
| 4212 | ExpectedParams = 0; |
| 4213 | if (MD->getNumParams() != ExpectedParams) { |
| 4214 | // This also checks for default arguments: a copy or move constructor with a |
| 4215 | // default argument is classified as a default constructor, and assignment |
| 4216 | // operations and destructors can't have default arguments. |
| 4217 | Diag(MD->getLocation(), diag::err_defaulted_special_member_params) |
| 4218 | << CSM << MD->getSourceRange(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4219 | HadError = true; |
Richard Smith | 5046439 | 2012-12-07 02:10:28 +0000 | [diff] [blame] | 4220 | } else if (MD->isVariadic()) { |
| 4221 | Diag(MD->getLocation(), diag::err_defaulted_special_member_variadic) |
| 4222 | << CSM << MD->getSourceRange(); |
| 4223 | HadError = true; |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4226 | const FunctionProtoType *Type = MD->getType()->getAs<FunctionProtoType>(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4227 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4228 | bool CanHaveConstParam = false; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4229 | if (CSM == CXXCopyConstructor) |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 4230 | CanHaveConstParam = RD->implicitCopyConstructorHasConstParam(); |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4231 | else if (CSM == CXXCopyAssignment) |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 4232 | CanHaveConstParam = RD->implicitCopyAssignmentHasConstParam(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4233 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4234 | QualType ReturnType = Context.VoidTy; |
| 4235 | if (CSM == CXXCopyAssignment || CSM == CXXMoveAssignment) { |
| 4236 | // Check for return type matching. |
| 4237 | ReturnType = Type->getResultType(); |
| 4238 | QualType ExpectedReturnType = |
| 4239 | Context.getLValueReferenceType(Context.getTypeDeclType(RD)); |
| 4240 | if (!Context.hasSameType(ReturnType, ExpectedReturnType)) { |
| 4241 | Diag(MD->getLocation(), diag::err_defaulted_special_member_return_type) |
| 4242 | << (CSM == CXXMoveAssignment) << ExpectedReturnType; |
| 4243 | HadError = true; |
| 4244 | } |
| 4245 | |
| 4246 | // A defaulted special member cannot have cv-qualifiers. |
| 4247 | if (Type->getTypeQuals()) { |
| 4248 | Diag(MD->getLocation(), diag::err_defaulted_special_member_quals) |
| 4249 | << (CSM == CXXMoveAssignment); |
| 4250 | HadError = true; |
| 4251 | } |
| 4252 | } |
| 4253 | |
| 4254 | // Check for parameter type matching. |
| 4255 | QualType ArgType = ExpectedParams ? Type->getArgType(0) : QualType(); |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4256 | bool HasConstParam = false; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4257 | if (ExpectedParams && ArgType->isReferenceType()) { |
| 4258 | // Argument must be reference to possibly-const T. |
| 4259 | QualType ReferentType = ArgType->getPointeeType(); |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4260 | HasConstParam = ReferentType.isConstQualified(); |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4261 | |
| 4262 | if (ReferentType.isVolatileQualified()) { |
| 4263 | Diag(MD->getLocation(), |
| 4264 | diag::err_defaulted_special_member_volatile_param) << CSM; |
| 4265 | HadError = true; |
| 4266 | } |
| 4267 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4268 | if (HasConstParam && !CanHaveConstParam) { |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4269 | if (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment) { |
| 4270 | Diag(MD->getLocation(), |
| 4271 | diag::err_defaulted_special_member_copy_const_param) |
| 4272 | << (CSM == CXXCopyAssignment); |
| 4273 | // FIXME: Explain why this special member can't be const. |
| 4274 | } else { |
| 4275 | Diag(MD->getLocation(), |
| 4276 | diag::err_defaulted_special_member_move_const_param) |
| 4277 | << (CSM == CXXMoveAssignment); |
| 4278 | } |
| 4279 | HadError = true; |
| 4280 | } |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4281 | } else if (ExpectedParams) { |
| 4282 | // A copy assignment operator can take its argument by value, but a |
| 4283 | // defaulted one cannot. |
| 4284 | assert(CSM == CXXCopyAssignment && "unexpected non-ref argument"); |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4285 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_not_ref); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4286 | HadError = true; |
| 4287 | } |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4288 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4289 | // C++11 [dcl.fct.def.default]p2: |
| 4290 | // An explicitly-defaulted function may be declared constexpr only if it |
| 4291 | // would have been implicitly declared as constexpr, |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4292 | // Do not apply this rule to members of class templates, since core issue 1358 |
| 4293 | // makes such functions always instantiate to constexpr functions. For |
| 4294 | // non-constructors, this is checked elsewhere. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4295 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, RD, CSM, |
| 4296 | HasConstParam); |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4297 | if (isa<CXXConstructorDecl>(MD) && MD->isConstexpr() && !Constexpr && |
| 4298 | MD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) { |
| 4299 | Diag(MD->getLocStart(), diag::err_incorrect_defaulted_constexpr) << CSM; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4300 | // FIXME: Explain why the constructor can't be constexpr. |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4301 | HadError = true; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4302 | } |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4303 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4304 | // and may have an explicit exception-specification only if it is compatible |
| 4305 | // with the exception-specification on the implicit declaration. |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4306 | if (Type->hasExceptionSpec()) { |
| 4307 | // Delay the check if this is the first declaration of the special member, |
| 4308 | // since we may not have parsed some necessary in-class initializers yet. |
| 4309 | if (First) |
| 4310 | DelayedDefaultedMemberExceptionSpecs.push_back(std::make_pair(MD, Type)); |
| 4311 | else |
| 4312 | CheckExplicitlyDefaultedMemberExceptionSpec(MD, Type); |
| 4313 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4314 | |
| 4315 | // If a function is explicitly defaulted on its first declaration, |
| 4316 | if (First) { |
| 4317 | // -- it is implicitly considered to be constexpr if the implicit |
| 4318 | // definition would be, |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4319 | MD->setConstexpr(Constexpr); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4320 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4321 | // -- it is implicitly considered to have the same exception-specification |
| 4322 | // as if it had been implicitly declared, |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4323 | FunctionProtoType::ExtProtoInfo EPI = Type->getExtProtoInfo(); |
| 4324 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 4325 | EPI.ExceptionSpecDecl = MD; |
| 4326 | MD->setType(Context.getFunctionType(ReturnType, &ArgType, |
| 4327 | ExpectedParams, EPI)); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4330 | if (ShouldDeleteSpecialMember(MD, CSM)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4331 | if (First) { |
| 4332 | MD->setDeletedAsWritten(); |
| 4333 | } else { |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4334 | // C++11 [dcl.fct.def.default]p4: |
| 4335 | // [For a] user-provided explicitly-defaulted function [...] if such a |
| 4336 | // function is implicitly defined as deleted, the program is ill-formed. |
| 4337 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) << CSM; |
| 4338 | HadError = true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4339 | } |
| 4340 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4341 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4342 | if (HadError) |
| 4343 | MD->setInvalidDecl(); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4346 | /// Check whether the exception specification provided for an |
| 4347 | /// explicitly-defaulted special member matches the exception specification |
| 4348 | /// that would have been generated for an implicit special member, per |
| 4349 | /// C++11 [dcl.fct.def.default]p2. |
| 4350 | void Sema::CheckExplicitlyDefaultedMemberExceptionSpec( |
| 4351 | CXXMethodDecl *MD, const FunctionProtoType *SpecifiedType) { |
| 4352 | // Compute the implicit exception specification. |
| 4353 | FunctionProtoType::ExtProtoInfo EPI; |
| 4354 | computeImplicitExceptionSpec(*this, MD->getLocation(), MD).getEPI(EPI); |
| 4355 | const FunctionProtoType *ImplicitType = cast<FunctionProtoType>( |
| 4356 | Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 4357 | |
| 4358 | // Ensure that it matches. |
| 4359 | CheckEquivalentExceptionSpec( |
| 4360 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
| 4361 | << getSpecialMember(MD), PDiag(), |
| 4362 | ImplicitType, SourceLocation(), |
| 4363 | SpecifiedType, MD->getLocation()); |
| 4364 | } |
| 4365 | |
| 4366 | void Sema::CheckDelayedExplicitlyDefaultedMemberExceptionSpecs() { |
| 4367 | for (unsigned I = 0, N = DelayedDefaultedMemberExceptionSpecs.size(); |
| 4368 | I != N; ++I) |
| 4369 | CheckExplicitlyDefaultedMemberExceptionSpec( |
| 4370 | DelayedDefaultedMemberExceptionSpecs[I].first, |
| 4371 | DelayedDefaultedMemberExceptionSpecs[I].second); |
| 4372 | |
| 4373 | DelayedDefaultedMemberExceptionSpecs.clear(); |
| 4374 | } |
| 4375 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4376 | namespace { |
| 4377 | struct SpecialMemberDeletionInfo { |
| 4378 | Sema &S; |
| 4379 | CXXMethodDecl *MD; |
| 4380 | Sema::CXXSpecialMember CSM; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4381 | bool Diagnose; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4382 | |
| 4383 | // Properties of the special member, computed for convenience. |
| 4384 | bool IsConstructor, IsAssignment, IsMove, ConstArg, VolatileArg; |
| 4385 | SourceLocation Loc; |
| 4386 | |
| 4387 | bool AllFieldsAreConst; |
| 4388 | |
| 4389 | SpecialMemberDeletionInfo(Sema &S, CXXMethodDecl *MD, |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4390 | Sema::CXXSpecialMember CSM, bool Diagnose) |
| 4391 | : S(S), MD(MD), CSM(CSM), Diagnose(Diagnose), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4392 | IsConstructor(false), IsAssignment(false), IsMove(false), |
| 4393 | ConstArg(false), VolatileArg(false), Loc(MD->getLocation()), |
| 4394 | AllFieldsAreConst(true) { |
| 4395 | switch (CSM) { |
| 4396 | case Sema::CXXDefaultConstructor: |
| 4397 | case Sema::CXXCopyConstructor: |
| 4398 | IsConstructor = true; |
| 4399 | break; |
| 4400 | case Sema::CXXMoveConstructor: |
| 4401 | IsConstructor = true; |
| 4402 | IsMove = true; |
| 4403 | break; |
| 4404 | case Sema::CXXCopyAssignment: |
| 4405 | IsAssignment = true; |
| 4406 | break; |
| 4407 | case Sema::CXXMoveAssignment: |
| 4408 | IsAssignment = true; |
| 4409 | IsMove = true; |
| 4410 | break; |
| 4411 | case Sema::CXXDestructor: |
| 4412 | break; |
| 4413 | case Sema::CXXInvalid: |
| 4414 | llvm_unreachable("invalid special member kind"); |
| 4415 | } |
| 4416 | |
| 4417 | if (MD->getNumParams()) { |
| 4418 | ConstArg = MD->getParamDecl(0)->getType().isConstQualified(); |
| 4419 | VolatileArg = MD->getParamDecl(0)->getType().isVolatileQualified(); |
| 4420 | } |
| 4421 | } |
| 4422 | |
| 4423 | bool inUnion() const { return MD->getParent()->isUnion(); } |
| 4424 | |
| 4425 | /// Look up the corresponding special member in the given class. |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4426 | Sema::SpecialMemberOverloadResult *lookupIn(CXXRecordDecl *Class, |
| 4427 | unsigned Quals) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4428 | unsigned TQ = MD->getTypeQualifiers(); |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4429 | // cv-qualifiers on class members don't affect default ctor / dtor calls. |
| 4430 | if (CSM == Sema::CXXDefaultConstructor || CSM == Sema::CXXDestructor) |
| 4431 | Quals = 0; |
| 4432 | return S.LookupSpecialMember(Class, CSM, |
| 4433 | ConstArg || (Quals & Qualifiers::Const), |
| 4434 | VolatileArg || (Quals & Qualifiers::Volatile), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4435 | MD->getRefQualifier() == RQ_RValue, |
| 4436 | TQ & Qualifiers::Const, |
| 4437 | TQ & Qualifiers::Volatile); |
| 4438 | } |
| 4439 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4440 | typedef llvm::PointerUnion<CXXBaseSpecifier*, FieldDecl*> Subobject; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4441 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4442 | bool shouldDeleteForBase(CXXBaseSpecifier *Base); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4443 | bool shouldDeleteForField(FieldDecl *FD); |
| 4444 | bool shouldDeleteForAllConstMembers(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4445 | |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4446 | bool shouldDeleteForClassSubobject(CXXRecordDecl *Class, Subobject Subobj, |
| 4447 | unsigned Quals); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4448 | bool shouldDeleteForSubobjectCall(Subobject Subobj, |
| 4449 | Sema::SpecialMemberOverloadResult *SMOR, |
| 4450 | bool IsDtorCallInCtor); |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4451 | |
| 4452 | bool isAccessible(Subobject Subobj, CXXMethodDecl *D); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4453 | }; |
| 4454 | } |
| 4455 | |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4456 | /// Is the given special member inaccessible when used on the given |
| 4457 | /// sub-object. |
| 4458 | bool SpecialMemberDeletionInfo::isAccessible(Subobject Subobj, |
| 4459 | CXXMethodDecl *target) { |
| 4460 | /// If we're operating on a base class, the object type is the |
| 4461 | /// type of this special member. |
| 4462 | QualType objectTy; |
Dmitri Gribenko | 1ad23d6 | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 4463 | AccessSpecifier access = target->getAccess(); |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4464 | if (CXXBaseSpecifier *base = Subobj.dyn_cast<CXXBaseSpecifier*>()) { |
| 4465 | objectTy = S.Context.getTypeDeclType(MD->getParent()); |
| 4466 | access = CXXRecordDecl::MergeAccess(base->getAccessSpecifier(), access); |
| 4467 | |
| 4468 | // If we're operating on a field, the object type is the type of the field. |
| 4469 | } else { |
| 4470 | objectTy = S.Context.getTypeDeclType(target->getParent()); |
| 4471 | } |
| 4472 | |
| 4473 | return S.isSpecialMemberAccessibleForDeletion(target, access, objectTy); |
| 4474 | } |
| 4475 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4476 | /// Check whether we should delete a special member due to the implicit |
| 4477 | /// definition containing a call to a special member of a subobject. |
| 4478 | bool SpecialMemberDeletionInfo::shouldDeleteForSubobjectCall( |
| 4479 | Subobject Subobj, Sema::SpecialMemberOverloadResult *SMOR, |
| 4480 | bool IsDtorCallInCtor) { |
| 4481 | CXXMethodDecl *Decl = SMOR->getMethod(); |
| 4482 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
| 4483 | |
| 4484 | int DiagKind = -1; |
| 4485 | |
| 4486 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::NoMemberOrDeleted) |
| 4487 | DiagKind = !Decl ? 0 : 1; |
| 4488 | else if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 4489 | DiagKind = 2; |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4490 | else if (!isAccessible(Subobj, Decl)) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4491 | DiagKind = 3; |
| 4492 | else if (!IsDtorCallInCtor && Field && Field->getParent()->isUnion() && |
| 4493 | !Decl->isTrivial()) { |
| 4494 | // A member of a union must have a trivial corresponding special member. |
| 4495 | // As a weird special case, a destructor call from a union's constructor |
| 4496 | // must be accessible and non-deleted, but need not be trivial. Such a |
| 4497 | // destructor is never actually called, but is semantically checked as |
| 4498 | // if it were. |
| 4499 | DiagKind = 4; |
| 4500 | } |
| 4501 | |
| 4502 | if (DiagKind == -1) |
| 4503 | return false; |
| 4504 | |
| 4505 | if (Diagnose) { |
| 4506 | if (Field) { |
| 4507 | S.Diag(Field->getLocation(), |
| 4508 | diag::note_deleted_special_member_class_subobject) |
| 4509 | << CSM << MD->getParent() << /*IsField*/true |
| 4510 | << Field << DiagKind << IsDtorCallInCtor; |
| 4511 | } else { |
| 4512 | CXXBaseSpecifier *Base = Subobj.get<CXXBaseSpecifier*>(); |
| 4513 | S.Diag(Base->getLocStart(), |
| 4514 | diag::note_deleted_special_member_class_subobject) |
| 4515 | << CSM << MD->getParent() << /*IsField*/false |
| 4516 | << Base->getType() << DiagKind << IsDtorCallInCtor; |
| 4517 | } |
| 4518 | |
| 4519 | if (DiagKind == 1) |
| 4520 | S.NoteDeletedFunction(Decl); |
| 4521 | // FIXME: Explain inaccessibility if DiagKind == 3. |
| 4522 | } |
| 4523 | |
| 4524 | return true; |
| 4525 | } |
| 4526 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4527 | /// Check whether we should delete a special member function due to having a |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4528 | /// direct or virtual base class or non-static data member of class type M. |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4529 | bool SpecialMemberDeletionInfo::shouldDeleteForClassSubobject( |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4530 | CXXRecordDecl *Class, Subobject Subobj, unsigned Quals) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4531 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4532 | |
| 4533 | // C++11 [class.ctor]p5: |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4534 | // -- any direct or virtual base class, or non-static data member with no |
| 4535 | // brace-or-equal-initializer, has class type M (or array thereof) and |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4536 | // either M has no default constructor or overload resolution as applied |
| 4537 | // to M's default constructor results in an ambiguity or in a function |
| 4538 | // that is deleted or inaccessible |
| 4539 | // C++11 [class.copy]p11, C++11 [class.copy]p23: |
| 4540 | // -- a direct or virtual base class B that cannot be copied/moved because |
| 4541 | // overload resolution, as applied to B's corresponding special member, |
| 4542 | // results in an ambiguity or a function that is deleted or inaccessible |
| 4543 | // from the defaulted special member |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4544 | // C++11 [class.dtor]p5: |
| 4545 | // -- any direct or virtual base class [...] has a type with a destructor |
| 4546 | // that is deleted or inaccessible |
| 4547 | if (!(CSM == Sema::CXXDefaultConstructor && |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4548 | Field && Field->hasInClassInitializer()) && |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4549 | shouldDeleteForSubobjectCall(Subobj, lookupIn(Class, Quals), false)) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4550 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4551 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4552 | // C++11 [class.ctor]p5, C++11 [class.copy]p11: |
| 4553 | // -- any direct or virtual base class or non-static data member has a |
| 4554 | // type with a destructor that is deleted or inaccessible |
| 4555 | if (IsConstructor) { |
| 4556 | Sema::SpecialMemberOverloadResult *SMOR = |
| 4557 | S.LookupSpecialMember(Class, Sema::CXXDestructor, |
| 4558 | false, false, false, false, false); |
| 4559 | if (shouldDeleteForSubobjectCall(Subobj, SMOR, true)) |
| 4560 | return true; |
| 4561 | } |
| 4562 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4563 | return false; |
| 4564 | } |
| 4565 | |
| 4566 | /// Check whether we should delete a special member function due to the class |
| 4567 | /// having a particular direct or virtual base class. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4568 | bool SpecialMemberDeletionInfo::shouldDeleteForBase(CXXBaseSpecifier *Base) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4569 | CXXRecordDecl *BaseClass = Base->getType()->getAsCXXRecordDecl(); |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4570 | return shouldDeleteForClassSubobject(BaseClass, Base, 0); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | /// Check whether we should delete a special member function due to the class |
| 4574 | /// having a particular non-static data member. |
| 4575 | bool SpecialMemberDeletionInfo::shouldDeleteForField(FieldDecl *FD) { |
| 4576 | QualType FieldType = S.Context.getBaseElementType(FD->getType()); |
| 4577 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 4578 | |
| 4579 | if (CSM == Sema::CXXDefaultConstructor) { |
| 4580 | // For a default constructor, all references must be initialized in-class |
| 4581 | // and, if a union, it must have a non-const member. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4582 | if (FieldType->isReferenceType() && !FD->hasInClassInitializer()) { |
| 4583 | if (Diagnose) |
| 4584 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
| 4585 | << MD->getParent() << FD << FieldType << /*Reference*/0; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4586 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4587 | } |
Richard Smith | 79363f5 | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 4588 | // C++11 [class.ctor]p5: any non-variant non-static data member of |
| 4589 | // const-qualified type (or array thereof) with no |
| 4590 | // brace-or-equal-initializer does not have a user-provided default |
| 4591 | // constructor. |
| 4592 | if (!inUnion() && FieldType.isConstQualified() && |
| 4593 | !FD->hasInClassInitializer() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4594 | (!FieldRecord || !FieldRecord->hasUserProvidedDefaultConstructor())) { |
| 4595 | if (Diagnose) |
| 4596 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
Richard Smith | a2e76f5 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 4597 | << MD->getParent() << FD << FD->getType() << /*Const*/1; |
Richard Smith | 79363f5 | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 4598 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | if (inUnion() && !FieldType.isConstQualified()) |
| 4602 | AllFieldsAreConst = false; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4603 | } else if (CSM == Sema::CXXCopyConstructor) { |
| 4604 | // For a copy constructor, data members must not be of rvalue reference |
| 4605 | // type. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4606 | if (FieldType->isRValueReferenceType()) { |
| 4607 | if (Diagnose) |
| 4608 | S.Diag(FD->getLocation(), diag::note_deleted_copy_ctor_rvalue_reference) |
| 4609 | << MD->getParent() << FD << FieldType; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4610 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4611 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4612 | } else if (IsAssignment) { |
| 4613 | // For an assignment operator, data members must not be of reference type. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4614 | if (FieldType->isReferenceType()) { |
| 4615 | if (Diagnose) |
| 4616 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
| 4617 | << IsMove << MD->getParent() << FD << FieldType << /*Reference*/0; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4618 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4619 | } |
| 4620 | if (!FieldRecord && FieldType.isConstQualified()) { |
| 4621 | // C++11 [class.copy]p23: |
| 4622 | // -- a non-static data member of const non-class type (or array thereof) |
| 4623 | if (Diagnose) |
| 4624 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
Richard Smith | a2e76f5 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 4625 | << IsMove << MD->getParent() << FD << FD->getType() << /*Const*/1; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4626 | return true; |
| 4627 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | if (FieldRecord) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4631 | // Some additional restrictions exist on the variant members. |
| 4632 | if (!inUnion() && FieldRecord->isUnion() && |
| 4633 | FieldRecord->isAnonymousStructOrUnion()) { |
| 4634 | bool AllVariantFieldsAreConst = true; |
| 4635 | |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4636 | // FIXME: Handle anonymous unions declared within anonymous unions. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4637 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 4638 | UE = FieldRecord->field_end(); |
| 4639 | UI != UE; ++UI) { |
| 4640 | QualType UnionFieldType = S.Context.getBaseElementType(UI->getType()); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4641 | |
| 4642 | if (!UnionFieldType.isConstQualified()) |
| 4643 | AllVariantFieldsAreConst = false; |
| 4644 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4645 | CXXRecordDecl *UnionFieldRecord = UnionFieldType->getAsCXXRecordDecl(); |
| 4646 | if (UnionFieldRecord && |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4647 | shouldDeleteForClassSubobject(UnionFieldRecord, *UI, |
| 4648 | UnionFieldType.getCVRQualifiers())) |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4649 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | // At least one member in each anonymous union must be non-const |
Douglas Gregor | 221c27f | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 4653 | if (CSM == Sema::CXXDefaultConstructor && AllVariantFieldsAreConst && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4654 | FieldRecord->field_begin() != FieldRecord->field_end()) { |
| 4655 | if (Diagnose) |
| 4656 | S.Diag(FieldRecord->getLocation(), |
| 4657 | diag::note_deleted_default_ctor_all_const) |
| 4658 | << MD->getParent() << /*anonymous union*/1; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4659 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4660 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4661 | |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4662 | // Don't check the implicit member of the anonymous union type. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4663 | // This is technically non-conformant, but sanity demands it. |
| 4664 | return false; |
| 4665 | } |
| 4666 | |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4667 | if (shouldDeleteForClassSubobject(FieldRecord, FD, |
| 4668 | FieldType.getCVRQualifiers())) |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4669 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4670 | } |
| 4671 | |
| 4672 | return false; |
| 4673 | } |
| 4674 | |
| 4675 | /// C++11 [class.ctor] p5: |
| 4676 | /// A defaulted default constructor for a class X is defined as deleted if |
| 4677 | /// X is a union and all of its variant members are of const-qualified type. |
| 4678 | bool SpecialMemberDeletionInfo::shouldDeleteForAllConstMembers() { |
Douglas Gregor | 221c27f | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 4679 | // This is a silly definition, because it gives an empty union a deleted |
| 4680 | // default constructor. Don't do that. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4681 | if (CSM == Sema::CXXDefaultConstructor && inUnion() && AllFieldsAreConst && |
| 4682 | (MD->getParent()->field_begin() != MD->getParent()->field_end())) { |
| 4683 | if (Diagnose) |
| 4684 | S.Diag(MD->getParent()->getLocation(), |
| 4685 | diag::note_deleted_default_ctor_all_const) |
| 4686 | << MD->getParent() << /*not anonymous union*/0; |
| 4687 | return true; |
| 4688 | } |
| 4689 | return false; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
| 4692 | /// Determine whether a defaulted special member function should be defined as |
| 4693 | /// deleted, as specified in C++11 [class.ctor]p5, C++11 [class.copy]p11, |
| 4694 | /// C++11 [class.copy]p23, and C++11 [class.dtor]p5. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4695 | bool Sema::ShouldDeleteSpecialMember(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 4696 | bool Diagnose) { |
Richard Smith | eef0029 | 2012-08-06 02:25:10 +0000 | [diff] [blame] | 4697 | if (MD->isInvalidDecl()) |
| 4698 | return false; |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 4699 | CXXRecordDecl *RD = MD->getParent(); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4700 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4701 | if (!LangOpts.CPlusPlus11 || RD->isInvalidDecl()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4702 | return false; |
| 4703 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4704 | // C++11 [expr.lambda.prim]p19: |
| 4705 | // The closure type associated with a lambda-expression has a |
| 4706 | // deleted (8.4.3) default constructor and a deleted copy |
| 4707 | // assignment operator. |
| 4708 | if (RD->isLambda() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4709 | (CSM == CXXDefaultConstructor || CSM == CXXCopyAssignment)) { |
| 4710 | if (Diagnose) |
| 4711 | Diag(RD->getLocation(), diag::note_lambda_decl); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4712 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4713 | } |
| 4714 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 4715 | // For an anonymous struct or union, the copy and assignment special members |
| 4716 | // will never be used, so skip the check. For an anonymous union declared at |
| 4717 | // namespace scope, the constructor and destructor are used. |
| 4718 | if (CSM != CXXDefaultConstructor && CSM != CXXDestructor && |
| 4719 | RD->isAnonymousStructOrUnion()) |
| 4720 | return false; |
| 4721 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4722 | // C++11 [class.copy]p7, p18: |
| 4723 | // If the class definition declares a move constructor or move assignment |
| 4724 | // operator, an implicitly declared copy constructor or copy assignment |
| 4725 | // operator is defined as deleted. |
| 4726 | if (MD->isImplicit() && |
| 4727 | (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment)) { |
| 4728 | CXXMethodDecl *UserDeclaredMove = 0; |
| 4729 | |
| 4730 | // In Microsoft mode, a user-declared move only causes the deletion of the |
| 4731 | // corresponding copy operation, not both copy operations. |
| 4732 | if (RD->hasUserDeclaredMoveConstructor() && |
| 4733 | (!getLangOpts().MicrosoftMode || CSM == CXXCopyConstructor)) { |
| 4734 | if (!Diagnose) return true; |
Richard Smith | 5579865 | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 4735 | |
| 4736 | // Find any user-declared move constructor. |
| 4737 | for (CXXRecordDecl::ctor_iterator I = RD->ctor_begin(), |
| 4738 | E = RD->ctor_end(); I != E; ++I) { |
| 4739 | if (I->isMoveConstructor()) { |
| 4740 | UserDeclaredMove = *I; |
| 4741 | break; |
| 4742 | } |
| 4743 | } |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4744 | assert(UserDeclaredMove); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4745 | } else if (RD->hasUserDeclaredMoveAssignment() && |
| 4746 | (!getLangOpts().MicrosoftMode || CSM == CXXCopyAssignment)) { |
| 4747 | if (!Diagnose) return true; |
Richard Smith | 5579865 | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 4748 | |
| 4749 | // Find any user-declared move assignment operator. |
| 4750 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 4751 | E = RD->method_end(); I != E; ++I) { |
| 4752 | if (I->isMoveAssignmentOperator()) { |
| 4753 | UserDeclaredMove = *I; |
| 4754 | break; |
| 4755 | } |
| 4756 | } |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4757 | assert(UserDeclaredMove); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | if (UserDeclaredMove) { |
| 4761 | Diag(UserDeclaredMove->getLocation(), |
| 4762 | diag::note_deleted_copy_user_declared_move) |
Richard Smith | e6af660 | 2012-04-02 21:07:48 +0000 | [diff] [blame] | 4763 | << (CSM == CXXCopyAssignment) << RD |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4764 | << UserDeclaredMove->isMoveAssignmentOperator(); |
| 4765 | return true; |
| 4766 | } |
| 4767 | } |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 4768 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 4769 | // Do access control from the special member function |
| 4770 | ContextRAII MethodContext(*this, MD); |
| 4771 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4772 | // C++11 [class.dtor]p5: |
| 4773 | // -- for a virtual destructor, lookup of the non-array deallocation function |
| 4774 | // results in an ambiguity or in a function that is deleted or inaccessible |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4775 | if (CSM == CXXDestructor && MD->isVirtual()) { |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4776 | FunctionDecl *OperatorDelete = 0; |
| 4777 | DeclarationName Name = |
| 4778 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| 4779 | if (FindDeallocationFunction(MD->getLocation(), MD->getParent(), Name, |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4780 | OperatorDelete, false)) { |
| 4781 | if (Diagnose) |
| 4782 | Diag(RD->getLocation(), diag::note_deleted_dtor_no_operator_delete); |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4783 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4784 | } |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4785 | } |
| 4786 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4787 | SpecialMemberDeletionInfo SMI(*this, MD, CSM, Diagnose); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4788 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4789 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4790 | BE = RD->bases_end(); BI != BE; ++BI) |
| 4791 | if (!BI->isVirtual() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4792 | SMI.shouldDeleteForBase(BI)) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4793 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4794 | |
| 4795 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4796 | BE = RD->vbases_end(); BI != BE; ++BI) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4797 | if (SMI.shouldDeleteForBase(BI)) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4798 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4799 | |
| 4800 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4801 | FE = RD->field_end(); FI != FE; ++FI) |
| 4802 | if (!FI->isInvalidDecl() && !FI->isUnnamedBitfield() && |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4803 | SMI.shouldDeleteForField(*FI)) |
Sean Hunt | e340682 | 2011-05-20 21:43:47 +0000 | [diff] [blame] | 4804 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4805 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4806 | if (SMI.shouldDeleteForAllConstMembers()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4807 | return true; |
| 4808 | |
| 4809 | return false; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4812 | /// Perform lookup for a special member of the specified kind, and determine |
| 4813 | /// whether it is trivial. If the triviality can be determined without the |
| 4814 | /// lookup, skip it. This is intended for use when determining whether a |
| 4815 | /// special member of a containing object is trivial, and thus does not ever |
| 4816 | /// perform overload resolution for default constructors. |
| 4817 | /// |
| 4818 | /// If \p Selected is not \c NULL, \c *Selected will be filled in with the |
| 4819 | /// member that was most likely to be intended to be trivial, if any. |
| 4820 | static bool findTrivialSpecialMember(Sema &S, CXXRecordDecl *RD, |
| 4821 | Sema::CXXSpecialMember CSM, unsigned Quals, |
| 4822 | CXXMethodDecl **Selected) { |
| 4823 | if (Selected) |
| 4824 | *Selected = 0; |
| 4825 | |
| 4826 | switch (CSM) { |
| 4827 | case Sema::CXXInvalid: |
| 4828 | llvm_unreachable("not a special member"); |
| 4829 | |
| 4830 | case Sema::CXXDefaultConstructor: |
| 4831 | // C++11 [class.ctor]p5: |
| 4832 | // A default constructor is trivial if: |
| 4833 | // - all the [direct subobjects] have trivial default constructors |
| 4834 | // |
| 4835 | // Note, no overload resolution is performed in this case. |
| 4836 | if (RD->hasTrivialDefaultConstructor()) |
| 4837 | return true; |
| 4838 | |
| 4839 | if (Selected) { |
| 4840 | // If there's a default constructor which could have been trivial, dig it |
| 4841 | // out. Otherwise, if there's any user-provided default constructor, point |
| 4842 | // to that as an example of why there's not a trivial one. |
| 4843 | CXXConstructorDecl *DefCtor = 0; |
| 4844 | if (RD->needsImplicitDefaultConstructor()) |
| 4845 | S.DeclareImplicitDefaultConstructor(RD); |
| 4846 | for (CXXRecordDecl::ctor_iterator CI = RD->ctor_begin(), |
| 4847 | CE = RD->ctor_end(); CI != CE; ++CI) { |
| 4848 | if (!CI->isDefaultConstructor()) |
| 4849 | continue; |
| 4850 | DefCtor = *CI; |
| 4851 | if (!DefCtor->isUserProvided()) |
| 4852 | break; |
| 4853 | } |
| 4854 | |
| 4855 | *Selected = DefCtor; |
| 4856 | } |
| 4857 | |
| 4858 | return false; |
| 4859 | |
| 4860 | case Sema::CXXDestructor: |
| 4861 | // C++11 [class.dtor]p5: |
| 4862 | // A destructor is trivial if: |
| 4863 | // - all the direct [subobjects] have trivial destructors |
| 4864 | if (RD->hasTrivialDestructor()) |
| 4865 | return true; |
| 4866 | |
| 4867 | if (Selected) { |
| 4868 | if (RD->needsImplicitDestructor()) |
| 4869 | S.DeclareImplicitDestructor(RD); |
| 4870 | *Selected = RD->getDestructor(); |
| 4871 | } |
| 4872 | |
| 4873 | return false; |
| 4874 | |
| 4875 | case Sema::CXXCopyConstructor: |
| 4876 | // C++11 [class.copy]p12: |
| 4877 | // A copy constructor is trivial if: |
| 4878 | // - the constructor selected to copy each direct [subobject] is trivial |
| 4879 | if (RD->hasTrivialCopyConstructor()) { |
| 4880 | if (Quals == Qualifiers::Const) |
| 4881 | // We must either select the trivial copy constructor or reach an |
| 4882 | // ambiguity; no need to actually perform overload resolution. |
| 4883 | return true; |
| 4884 | } else if (!Selected) { |
| 4885 | return false; |
| 4886 | } |
| 4887 | // In C++98, we are not supposed to perform overload resolution here, but we |
| 4888 | // treat that as a language defect, as suggested on cxx-abi-dev, to treat |
| 4889 | // cases like B as having a non-trivial copy constructor: |
| 4890 | // struct A { template<typename T> A(T&); }; |
| 4891 | // struct B { mutable A a; }; |
| 4892 | goto NeedOverloadResolution; |
| 4893 | |
| 4894 | case Sema::CXXCopyAssignment: |
| 4895 | // C++11 [class.copy]p25: |
| 4896 | // A copy assignment operator is trivial if: |
| 4897 | // - the assignment operator selected to copy each direct [subobject] is |
| 4898 | // trivial |
| 4899 | if (RD->hasTrivialCopyAssignment()) { |
| 4900 | if (Quals == Qualifiers::Const) |
| 4901 | return true; |
| 4902 | } else if (!Selected) { |
| 4903 | return false; |
| 4904 | } |
| 4905 | // In C++98, we are not supposed to perform overload resolution here, but we |
| 4906 | // treat that as a language defect. |
| 4907 | goto NeedOverloadResolution; |
| 4908 | |
| 4909 | case Sema::CXXMoveConstructor: |
| 4910 | case Sema::CXXMoveAssignment: |
| 4911 | NeedOverloadResolution: |
| 4912 | Sema::SpecialMemberOverloadResult *SMOR = |
| 4913 | S.LookupSpecialMember(RD, CSM, |
| 4914 | Quals & Qualifiers::Const, |
| 4915 | Quals & Qualifiers::Volatile, |
| 4916 | /*RValueThis*/false, /*ConstThis*/false, |
| 4917 | /*VolatileThis*/false); |
| 4918 | |
| 4919 | // The standard doesn't describe how to behave if the lookup is ambiguous. |
| 4920 | // We treat it as not making the member non-trivial, just like the standard |
| 4921 | // mandates for the default constructor. This should rarely matter, because |
| 4922 | // the member will also be deleted. |
| 4923 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 4924 | return true; |
| 4925 | |
| 4926 | if (!SMOR->getMethod()) { |
| 4927 | assert(SMOR->getKind() == |
| 4928 | Sema::SpecialMemberOverloadResult::NoMemberOrDeleted); |
| 4929 | return false; |
| 4930 | } |
| 4931 | |
| 4932 | // We deliberately don't check if we found a deleted special member. We're |
| 4933 | // not supposed to! |
| 4934 | if (Selected) |
| 4935 | *Selected = SMOR->getMethod(); |
| 4936 | return SMOR->getMethod()->isTrivial(); |
| 4937 | } |
| 4938 | |
| 4939 | llvm_unreachable("unknown special method kind"); |
| 4940 | } |
| 4941 | |
| 4942 | CXXConstructorDecl *findUserDeclaredCtor(CXXRecordDecl *RD) { |
| 4943 | for (CXXRecordDecl::ctor_iterator CI = RD->ctor_begin(), CE = RD->ctor_end(); |
| 4944 | CI != CE; ++CI) |
| 4945 | if (!CI->isImplicit()) |
| 4946 | return *CI; |
| 4947 | |
| 4948 | // Look for constructor templates. |
| 4949 | typedef CXXRecordDecl::specific_decl_iterator<FunctionTemplateDecl> tmpl_iter; |
| 4950 | for (tmpl_iter TI(RD->decls_begin()), TE(RD->decls_end()); TI != TE; ++TI) { |
| 4951 | if (CXXConstructorDecl *CD = |
| 4952 | dyn_cast<CXXConstructorDecl>(TI->getTemplatedDecl())) |
| 4953 | return CD; |
| 4954 | } |
| 4955 | |
| 4956 | return 0; |
| 4957 | } |
| 4958 | |
| 4959 | /// The kind of subobject we are checking for triviality. The values of this |
| 4960 | /// enumeration are used in diagnostics. |
| 4961 | enum TrivialSubobjectKind { |
| 4962 | /// The subobject is a base class. |
| 4963 | TSK_BaseClass, |
| 4964 | /// The subobject is a non-static data member. |
| 4965 | TSK_Field, |
| 4966 | /// The object is actually the complete object. |
| 4967 | TSK_CompleteObject |
| 4968 | }; |
| 4969 | |
| 4970 | /// Check whether the special member selected for a given type would be trivial. |
| 4971 | static bool checkTrivialSubobjectCall(Sema &S, SourceLocation SubobjLoc, |
| 4972 | QualType SubType, |
| 4973 | Sema::CXXSpecialMember CSM, |
| 4974 | TrivialSubobjectKind Kind, |
| 4975 | bool Diagnose) { |
| 4976 | CXXRecordDecl *SubRD = SubType->getAsCXXRecordDecl(); |
| 4977 | if (!SubRD) |
| 4978 | return true; |
| 4979 | |
| 4980 | CXXMethodDecl *Selected; |
| 4981 | if (findTrivialSpecialMember(S, SubRD, CSM, SubType.getCVRQualifiers(), |
| 4982 | Diagnose ? &Selected : 0)) |
| 4983 | return true; |
| 4984 | |
| 4985 | if (Diagnose) { |
| 4986 | if (!Selected && CSM == Sema::CXXDefaultConstructor) { |
| 4987 | S.Diag(SubobjLoc, diag::note_nontrivial_no_def_ctor) |
| 4988 | << Kind << SubType.getUnqualifiedType(); |
| 4989 | if (CXXConstructorDecl *CD = findUserDeclaredCtor(SubRD)) |
| 4990 | S.Diag(CD->getLocation(), diag::note_user_declared_ctor); |
| 4991 | } else if (!Selected) |
| 4992 | S.Diag(SubobjLoc, diag::note_nontrivial_no_copy) |
| 4993 | << Kind << SubType.getUnqualifiedType() << CSM << SubType; |
| 4994 | else if (Selected->isUserProvided()) { |
| 4995 | if (Kind == TSK_CompleteObject) |
| 4996 | S.Diag(Selected->getLocation(), diag::note_nontrivial_user_provided) |
| 4997 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 4998 | else { |
| 4999 | S.Diag(SubobjLoc, diag::note_nontrivial_user_provided) |
| 5000 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5001 | S.Diag(Selected->getLocation(), diag::note_declared_at); |
| 5002 | } |
| 5003 | } else { |
| 5004 | if (Kind != TSK_CompleteObject) |
| 5005 | S.Diag(SubobjLoc, diag::note_nontrivial_subobject) |
| 5006 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5007 | |
| 5008 | // Explain why the defaulted or deleted special member isn't trivial. |
| 5009 | S.SpecialMemberIsTrivial(Selected, CSM, Diagnose); |
| 5010 | } |
| 5011 | } |
| 5012 | |
| 5013 | return false; |
| 5014 | } |
| 5015 | |
| 5016 | /// Check whether the members of a class type allow a special member to be |
| 5017 | /// trivial. |
| 5018 | static bool checkTrivialClassMembers(Sema &S, CXXRecordDecl *RD, |
| 5019 | Sema::CXXSpecialMember CSM, |
| 5020 | bool ConstArg, bool Diagnose) { |
| 5021 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 5022 | FE = RD->field_end(); FI != FE; ++FI) { |
| 5023 | if (FI->isInvalidDecl() || FI->isUnnamedBitfield()) |
| 5024 | continue; |
| 5025 | |
| 5026 | QualType FieldType = S.Context.getBaseElementType(FI->getType()); |
| 5027 | |
| 5028 | // Pretend anonymous struct or union members are members of this class. |
| 5029 | if (FI->isAnonymousStructOrUnion()) { |
| 5030 | if (!checkTrivialClassMembers(S, FieldType->getAsCXXRecordDecl(), |
| 5031 | CSM, ConstArg, Diagnose)) |
| 5032 | return false; |
| 5033 | continue; |
| 5034 | } |
| 5035 | |
| 5036 | // C++11 [class.ctor]p5: |
| 5037 | // A default constructor is trivial if [...] |
| 5038 | // -- no non-static data member of its class has a |
| 5039 | // brace-or-equal-initializer |
| 5040 | if (CSM == Sema::CXXDefaultConstructor && FI->hasInClassInitializer()) { |
| 5041 | if (Diagnose) |
| 5042 | S.Diag(FI->getLocation(), diag::note_nontrivial_in_class_init) << *FI; |
| 5043 | return false; |
| 5044 | } |
| 5045 | |
| 5046 | // Objective C ARC 4.3.5: |
| 5047 | // [...] nontrivally ownership-qualified types are [...] not trivially |
| 5048 | // default constructible, copy constructible, move constructible, copy |
| 5049 | // assignable, move assignable, or destructible [...] |
| 5050 | if (S.getLangOpts().ObjCAutoRefCount && |
| 5051 | FieldType.hasNonTrivialObjCLifetime()) { |
| 5052 | if (Diagnose) |
| 5053 | S.Diag(FI->getLocation(), diag::note_nontrivial_objc_ownership) |
| 5054 | << RD << FieldType.getObjCLifetime(); |
| 5055 | return false; |
| 5056 | } |
| 5057 | |
| 5058 | if (ConstArg && !FI->isMutable()) |
| 5059 | FieldType.addConst(); |
| 5060 | if (!checkTrivialSubobjectCall(S, FI->getLocation(), FieldType, CSM, |
| 5061 | TSK_Field, Diagnose)) |
| 5062 | return false; |
| 5063 | } |
| 5064 | |
| 5065 | return true; |
| 5066 | } |
| 5067 | |
| 5068 | /// Diagnose why the specified class does not have a trivial special member of |
| 5069 | /// the given kind. |
| 5070 | void Sema::DiagnoseNontrivial(const CXXRecordDecl *RD, CXXSpecialMember CSM) { |
| 5071 | QualType Ty = Context.getRecordType(RD); |
| 5072 | if (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment) |
| 5073 | Ty.addConst(); |
| 5074 | |
| 5075 | checkTrivialSubobjectCall(*this, RD->getLocation(), Ty, CSM, |
| 5076 | TSK_CompleteObject, /*Diagnose*/true); |
| 5077 | } |
| 5078 | |
| 5079 | /// Determine whether a defaulted or deleted special member function is trivial, |
| 5080 | /// as specified in C++11 [class.ctor]p5, C++11 [class.copy]p12, |
| 5081 | /// C++11 [class.copy]p25, and C++11 [class.dtor]p5. |
| 5082 | bool Sema::SpecialMemberIsTrivial(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 5083 | bool Diagnose) { |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5084 | assert(!MD->isUserProvided() && CSM != CXXInvalid && "not special enough"); |
| 5085 | |
| 5086 | CXXRecordDecl *RD = MD->getParent(); |
| 5087 | |
| 5088 | bool ConstArg = false; |
| 5089 | ParmVarDecl *Param0 = MD->getNumParams() ? MD->getParamDecl(0) : 0; |
| 5090 | |
| 5091 | // C++11 [class.copy]p12, p25: |
| 5092 | // A [special member] is trivial if its declared parameter type is the same |
| 5093 | // as if it had been implicitly declared [...] |
| 5094 | switch (CSM) { |
| 5095 | case CXXDefaultConstructor: |
| 5096 | case CXXDestructor: |
| 5097 | // Trivial default constructors and destructors cannot have parameters. |
| 5098 | break; |
| 5099 | |
| 5100 | case CXXCopyConstructor: |
| 5101 | case CXXCopyAssignment: { |
| 5102 | // Trivial copy operations always have const, non-volatile parameter types. |
| 5103 | ConstArg = true; |
| 5104 | const ReferenceType *RT = Param0->getType()->getAs<ReferenceType>(); |
| 5105 | if (!RT || RT->getPointeeType().getCVRQualifiers() != Qualifiers::Const) { |
| 5106 | if (Diagnose) |
| 5107 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 5108 | << Param0->getSourceRange() << Param0->getType() |
| 5109 | << Context.getLValueReferenceType( |
| 5110 | Context.getRecordType(RD).withConst()); |
| 5111 | return false; |
| 5112 | } |
| 5113 | break; |
| 5114 | } |
| 5115 | |
| 5116 | case CXXMoveConstructor: |
| 5117 | case CXXMoveAssignment: { |
| 5118 | // Trivial move operations always have non-cv-qualified parameters. |
| 5119 | const RValueReferenceType *RT = |
| 5120 | Param0->getType()->getAs<RValueReferenceType>(); |
| 5121 | if (!RT || RT->getPointeeType().getCVRQualifiers()) { |
| 5122 | if (Diagnose) |
| 5123 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 5124 | << Param0->getSourceRange() << Param0->getType() |
| 5125 | << Context.getRValueReferenceType(Context.getRecordType(RD)); |
| 5126 | return false; |
| 5127 | } |
| 5128 | break; |
| 5129 | } |
| 5130 | |
| 5131 | case CXXInvalid: |
| 5132 | llvm_unreachable("not a special member"); |
| 5133 | } |
| 5134 | |
| 5135 | // FIXME: We require that the parameter-declaration-clause is equivalent to |
| 5136 | // that of an implicit declaration, not just that the declared parameter type |
| 5137 | // matches, in order to prevent absuridities like a function simultaneously |
| 5138 | // being a trivial copy constructor and a non-trivial default constructor. |
| 5139 | // This issue has not yet been assigned a core issue number. |
| 5140 | if (MD->getMinRequiredArguments() < MD->getNumParams()) { |
| 5141 | if (Diagnose) |
| 5142 | Diag(MD->getParamDecl(MD->getMinRequiredArguments())->getLocation(), |
| 5143 | diag::note_nontrivial_default_arg) |
| 5144 | << MD->getParamDecl(MD->getMinRequiredArguments())->getSourceRange(); |
| 5145 | return false; |
| 5146 | } |
| 5147 | if (MD->isVariadic()) { |
| 5148 | if (Diagnose) |
| 5149 | Diag(MD->getLocation(), diag::note_nontrivial_variadic); |
| 5150 | return false; |
| 5151 | } |
| 5152 | |
| 5153 | // C++11 [class.ctor]p5, C++11 [class.dtor]p5: |
| 5154 | // A copy/move [constructor or assignment operator] is trivial if |
| 5155 | // -- the [member] selected to copy/move each direct base class subobject |
| 5156 | // is trivial |
| 5157 | // |
| 5158 | // C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5159 | // A [default constructor or destructor] is trivial if |
| 5160 | // -- all the direct base classes have trivial [default constructors or |
| 5161 | // destructors] |
| 5162 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 5163 | BE = RD->bases_end(); BI != BE; ++BI) |
| 5164 | if (!checkTrivialSubobjectCall(*this, BI->getLocStart(), |
| 5165 | ConstArg ? BI->getType().withConst() |
| 5166 | : BI->getType(), |
| 5167 | CSM, TSK_BaseClass, Diagnose)) |
| 5168 | return false; |
| 5169 | |
| 5170 | // C++11 [class.ctor]p5, C++11 [class.dtor]p5: |
| 5171 | // A copy/move [constructor or assignment operator] for a class X is |
| 5172 | // trivial if |
| 5173 | // -- for each non-static data member of X that is of class type (or array |
| 5174 | // thereof), the constructor selected to copy/move that member is |
| 5175 | // trivial |
| 5176 | // |
| 5177 | // C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5178 | // A [default constructor or destructor] is trivial if |
| 5179 | // -- for all of the non-static data members of its class that are of class |
| 5180 | // type (or array thereof), each such class has a trivial [default |
| 5181 | // constructor or destructor] |
| 5182 | if (!checkTrivialClassMembers(*this, RD, CSM, ConstArg, Diagnose)) |
| 5183 | return false; |
| 5184 | |
| 5185 | // C++11 [class.dtor]p5: |
| 5186 | // A destructor is trivial if [...] |
| 5187 | // -- the destructor is not virtual |
| 5188 | if (CSM == CXXDestructor && MD->isVirtual()) { |
| 5189 | if (Diagnose) |
| 5190 | Diag(MD->getLocation(), diag::note_nontrivial_virtual_dtor) << RD; |
| 5191 | return false; |
| 5192 | } |
| 5193 | |
| 5194 | // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5195 | // A [special member] for class X is trivial if [...] |
| 5196 | // -- class X has no virtual functions and no virtual base classes |
| 5197 | if (CSM != CXXDestructor && MD->getParent()->isDynamicClass()) { |
| 5198 | if (!Diagnose) |
| 5199 | return false; |
| 5200 | |
| 5201 | if (RD->getNumVBases()) { |
| 5202 | // Check for virtual bases. We already know that the corresponding |
| 5203 | // member in all bases is trivial, so vbases must all be direct. |
| 5204 | CXXBaseSpecifier &BS = *RD->vbases_begin(); |
| 5205 | assert(BS.isVirtual()); |
| 5206 | Diag(BS.getLocStart(), diag::note_nontrivial_has_virtual) << RD << 1; |
| 5207 | return false; |
| 5208 | } |
| 5209 | |
| 5210 | // Must have a virtual method. |
| 5211 | for (CXXRecordDecl::method_iterator MI = RD->method_begin(), |
| 5212 | ME = RD->method_end(); MI != ME; ++MI) { |
| 5213 | if (MI->isVirtual()) { |
| 5214 | SourceLocation MLoc = MI->getLocStart(); |
| 5215 | Diag(MLoc, diag::note_nontrivial_has_virtual) << RD << 0; |
| 5216 | return false; |
| 5217 | } |
| 5218 | } |
| 5219 | |
| 5220 | llvm_unreachable("dynamic class with no vbases and no virtual functions"); |
| 5221 | } |
| 5222 | |
| 5223 | // Looks like it's trivial! |
| 5224 | return true; |
| 5225 | } |
| 5226 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5227 | /// \brief Data used with FindHiddenVirtualMethod |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 5228 | namespace { |
| 5229 | struct FindHiddenVirtualMethodData { |
| 5230 | Sema *S; |
| 5231 | CXXMethodDecl *Method; |
| 5232 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5233 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 5234 | }; |
| 5235 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5236 | |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5237 | /// \brief Check whether any most overriden method from MD in Methods |
| 5238 | static bool CheckMostOverridenMethods(const CXXMethodDecl *MD, |
| 5239 | const llvm::SmallPtrSet<const CXXMethodDecl *, 8>& Methods) { |
| 5240 | if (MD->size_overridden_methods() == 0) |
| 5241 | return Methods.count(MD->getCanonicalDecl()); |
| 5242 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 5243 | E = MD->end_overridden_methods(); |
| 5244 | I != E; ++I) |
| 5245 | if (CheckMostOverridenMethods(*I, Methods)) |
| 5246 | return true; |
| 5247 | return false; |
| 5248 | } |
| 5249 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5250 | /// \brief Member lookup function that determines whether a given C++ |
| 5251 | /// method overloads virtual methods in a base class without overriding any, |
| 5252 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 5253 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 5254 | CXXBasePath &Path, |
| 5255 | void *UserData) { |
| 5256 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 5257 | |
| 5258 | FindHiddenVirtualMethodData &Data |
| 5259 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 5260 | |
| 5261 | DeclarationName Name = Data.Method->getDeclName(); |
| 5262 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 5263 | |
| 5264 | bool foundSameNameMethod = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5265 | SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5266 | for (Path.Decls = BaseRecord->lookup(Name); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5267 | !Path.Decls.empty(); |
| 5268 | Path.Decls = Path.Decls.slice(1)) { |
| 5269 | NamedDecl *D = Path.Decls.front(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5270 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 5271 | MD = MD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5272 | foundSameNameMethod = true; |
| 5273 | // Interested only in hidden virtual methods. |
| 5274 | if (!MD->isVirtual()) |
| 5275 | continue; |
| 5276 | // If the method we are checking overrides a method from its base |
| 5277 | // don't warn about the other overloaded methods. |
| 5278 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 5279 | return true; |
| 5280 | // Collect the overload only if its hidden. |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5281 | if (!CheckMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods)) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5282 | overloadedMethods.push_back(MD); |
| 5283 | } |
| 5284 | } |
| 5285 | |
| 5286 | if (foundSameNameMethod) |
| 5287 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 5288 | overloadedMethods.end()); |
| 5289 | return foundSameNameMethod; |
| 5290 | } |
| 5291 | |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5292 | /// \brief Add the most overriden methods from MD to Methods |
| 5293 | static void AddMostOverridenMethods(const CXXMethodDecl *MD, |
| 5294 | llvm::SmallPtrSet<const CXXMethodDecl *, 8>& Methods) { |
| 5295 | if (MD->size_overridden_methods() == 0) |
| 5296 | Methods.insert(MD->getCanonicalDecl()); |
| 5297 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 5298 | E = MD->end_overridden_methods(); |
| 5299 | I != E; ++I) |
| 5300 | AddMostOverridenMethods(*I, Methods); |
| 5301 | } |
| 5302 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5303 | /// \brief See if a method overloads virtual methods in a base class without |
| 5304 | /// overriding any. |
| 5305 | void Sema::DiagnoseHiddenVirtualMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
| 5306 | if (Diags.getDiagnosticLevel(diag::warn_overloaded_virtual, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5307 | MD->getLocation()) == DiagnosticsEngine::Ignored) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5308 | return; |
Benjamin Kramer | c470442 | 2012-05-19 16:03:58 +0000 | [diff] [blame] | 5309 | if (!MD->getDeclName().isIdentifier()) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5310 | return; |
| 5311 | |
| 5312 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 5313 | /*bool RecordPaths=*/false, |
| 5314 | /*bool DetectVirtual=*/false); |
| 5315 | FindHiddenVirtualMethodData Data; |
| 5316 | Data.Method = MD; |
| 5317 | Data.S = this; |
| 5318 | |
| 5319 | // Keep the base methods that were overriden or introduced in the subclass |
| 5320 | // by 'using' in a set. A base method not in this set is hidden. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5321 | DeclContext::lookup_result R = DC->lookup(MD->getDeclName()); |
| 5322 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 5323 | NamedDecl *ND = *I; |
| 5324 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*I)) |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5325 | ND = shad->getTargetDecl(); |
| 5326 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 5327 | AddMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths) && |
| 5331 | !Data.OverloadedMethods.empty()) { |
| 5332 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 5333 | << MD << (Data.OverloadedMethods.size() > 1); |
| 5334 | |
| 5335 | for (unsigned i = 0, e = Data.OverloadedMethods.size(); i != e; ++i) { |
| 5336 | CXXMethodDecl *overloadedMD = Data.OverloadedMethods[i]; |
| 5337 | Diag(overloadedMD->getLocation(), |
| 5338 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
| 5339 | } |
| 5340 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 5341 | } |
| 5342 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5343 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5344 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5345 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 5346 | SourceLocation RBrac, |
| 5347 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5348 | if (!TagDecl) |
| 5349 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5350 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5351 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 5352 | |
Rafael Espindola | f729ce0 | 2012-07-12 04:32:30 +0000 | [diff] [blame] | 5353 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
| 5354 | if (l->getKind() != AttributeList::AT_Visibility) |
| 5355 | continue; |
| 5356 | l->setInvalid(); |
| 5357 | Diag(l->getLoc(), diag::warn_attribute_after_definition_ignored) << |
| 5358 | l->getName(); |
| 5359 | } |
| 5360 | |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 5361 | ActOnFields(S, RLoc, TagDecl, llvm::makeArrayRef( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5362 | // strict aliasing violation! |
| 5363 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 5364 | FieldCollector->getCurNumFields()), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 5365 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5366 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5367 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5370 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 5371 | /// special functions, such as the default constructor, copy |
| 5372 | /// constructor, or destructor, to the given C++ class (C++ |
| 5373 | /// [special]p1). This routine can only be executed just before the |
| 5374 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5375 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5376 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5377 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5378 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5379 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5380 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5381 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5382 | // If the properties or semantics of the copy constructor couldn't be |
| 5383 | // determined while the class was being declared, force a declaration |
| 5384 | // of it now. |
| 5385 | if (ClassDecl->needsOverloadResolutionForCopyConstructor()) |
| 5386 | DeclareImplicitCopyConstructor(ClassDecl); |
| 5387 | } |
| 5388 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5389 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveConstructor()) { |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 5390 | ++ASTContext::NumImplicitMoveConstructors; |
| 5391 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5392 | if (ClassDecl->needsOverloadResolutionForMoveConstructor()) |
| 5393 | DeclareImplicitMoveConstructor(ClassDecl); |
| 5394 | } |
| 5395 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5396 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 5397 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5398 | |
| 5399 | // If we have a dynamic class, then the copy assignment operator may be |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5400 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5401 | // it shows up in the right place in the vtable and that we diagnose |
| 5402 | // problems with the implicit exception specification. |
| 5403 | if (ClassDecl->isDynamicClass() || |
| 5404 | ClassDecl->needsOverloadResolutionForCopyAssignment()) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5405 | DeclareImplicitCopyAssignment(ClassDecl); |
| 5406 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5407 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5408 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveAssignment()) { |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 5409 | ++ASTContext::NumImplicitMoveAssignmentOperators; |
| 5410 | |
| 5411 | // Likewise for the move assignment operator. |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5412 | if (ClassDecl->isDynamicClass() || |
| 5413 | ClassDecl->needsOverloadResolutionForMoveAssignment()) |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 5414 | DeclareImplicitMoveAssignment(ClassDecl); |
| 5415 | } |
| 5416 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5417 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 5418 | ++ASTContext::NumImplicitDestructors; |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5419 | |
| 5420 | // If we have a dynamic class, then the destructor may be virtual, so we |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5421 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 5422 | // shows up in the right place in the vtable and that we diagnose problems |
| 5423 | // with the implicit exception specification. |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5424 | if (ClassDecl->isDynamicClass() || |
| 5425 | ClassDecl->needsOverloadResolutionForDestructor()) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5426 | DeclareImplicitDestructor(ClassDecl); |
| 5427 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 5430 | void Sema::ActOnReenterDeclaratorTemplateScope(Scope *S, DeclaratorDecl *D) { |
| 5431 | if (!D) |
| 5432 | return; |
| 5433 | |
| 5434 | int NumParamList = D->getNumTemplateParameterLists(); |
| 5435 | for (int i = 0; i < NumParamList; i++) { |
| 5436 | TemplateParameterList* Params = D->getTemplateParameterList(i); |
| 5437 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5438 | ParamEnd = Params->end(); |
| 5439 | Param != ParamEnd; ++Param) { |
| 5440 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 5441 | if (Named->getDeclName()) { |
| 5442 | S->AddDecl(Named); |
| 5443 | IdResolver.AddDecl(Named); |
| 5444 | } |
| 5445 | } |
| 5446 | } |
| 5447 | } |
| 5448 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5449 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 5450 | if (!D) |
| 5451 | return; |
| 5452 | |
| 5453 | TemplateParameterList *Params = 0; |
| 5454 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 5455 | Params = Template->getTemplateParameters(); |
| 5456 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 5457 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 5458 | Params = PartialSpec->getTemplateParameters(); |
| 5459 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 5460 | return; |
| 5461 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 5462 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5463 | ParamEnd = Params->end(); |
| 5464 | Param != ParamEnd; ++Param) { |
| 5465 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 5466 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5467 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 5468 | IdResolver.AddDecl(Named); |
| 5469 | } |
| 5470 | } |
| 5471 | } |
| 5472 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5473 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5474 | if (!RecordD) return; |
| 5475 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5476 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5477 | PushDeclContext(S, Record); |
| 5478 | } |
| 5479 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5480 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5481 | if (!RecordD) return; |
| 5482 | PopDeclContext(); |
| 5483 | } |
| 5484 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5485 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 5486 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 5487 | /// parsing those parts of the given Method declaration that could |
| 5488 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 5489 | /// arguments. This action should enter the scope of the given |
| 5490 | /// Method declaration as if we had just parsed the qualified method |
| 5491 | /// name. However, it should not bring the parameters into scope; |
| 5492 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5493 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5494 | } |
| 5495 | |
| 5496 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 5497 | /// C++ method declaration. We're (re-)introducing the given |
| 5498 | /// function parameter into scope for use in parsing later parts of |
| 5499 | /// the method declaration. For example, we could see an |
| 5500 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5501 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5502 | if (!ParamD) |
| 5503 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5504 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5505 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5506 | |
| 5507 | // If this parameter has an unparsed default argument, clear it out |
| 5508 | // to make way for the parsed default argument. |
| 5509 | if (Param->hasUnparsedDefaultArg()) |
| 5510 | Param->setDefaultArg(0); |
| 5511 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5512 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5513 | if (Param->getDeclName()) |
| 5514 | IdResolver.AddDecl(Param); |
| 5515 | } |
| 5516 | |
| 5517 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 5518 | /// processing the delayed method declaration for Method. The method |
| 5519 | /// declaration is now considered finished. There may be a separate |
| 5520 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 5521 | /// immediately!) for this method, if it was also defined inside the |
| 5522 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5523 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5524 | if (!MethodD) |
| 5525 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5526 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 5527 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5528 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5529 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5530 | |
| 5531 | // Now that we have our default arguments, check the constructor |
| 5532 | // again. It could produce additional diagnostics or affect whether |
| 5533 | // the class has implicitly-declared destructors, among other |
| 5534 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5535 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 5536 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5537 | |
| 5538 | // Check the default arguments, which we may have added. |
| 5539 | if (!Method->isInvalidDecl()) |
| 5540 | CheckCXXDefaultArguments(Method); |
| 5541 | } |
| 5542 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5543 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5544 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5545 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5546 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 5547 | /// will be updated to reflect a well-formed type for the constructor and |
| 5548 | /// returned. |
| 5549 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5550 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5551 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5552 | |
| 5553 | // C++ [class.ctor]p3: |
| 5554 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 5555 | // constructor can be invoked for a const, volatile or const |
| 5556 | // volatile object. A constructor shall not be declared const, |
| 5557 | // volatile, or const volatile (9.3.2). |
| 5558 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5559 | if (!D.isInvalidType()) |
| 5560 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 5561 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 5562 | << SourceRange(D.getIdentifierLoc()); |
| 5563 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5564 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5565 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5566 | if (!D.isInvalidType()) |
| 5567 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 5568 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 5569 | << SourceRange(D.getIdentifierLoc()); |
| 5570 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5571 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5572 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5573 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5574 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5575 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5576 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5577 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 5578 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5579 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5580 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 5581 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5582 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5583 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 5584 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5585 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5586 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5587 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5588 | // C++0x [class.ctor]p4: |
| 5589 | // A constructor shall not be declared with a ref-qualifier. |
| 5590 | if (FTI.hasRefQualifier()) { |
| 5591 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 5592 | << FTI.RefQualifierIsLValueRef |
| 5593 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 5594 | D.setInvalidType(); |
| 5595 | } |
| 5596 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5597 | // Rebuild the function type "R" without any type qualifiers (in |
| 5598 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5599 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5600 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5601 | if (Proto->getResultType() == Context.VoidTy && !D.isInvalidType()) |
| 5602 | return R; |
| 5603 | |
| 5604 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 5605 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5606 | EPI.RefQualifier = RQ_None; |
| 5607 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5608 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5609 | Proto->getNumArgs(), EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5610 | } |
| 5611 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5612 | /// CheckConstructor - Checks a fully-formed constructor for |
| 5613 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 5614 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5615 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5616 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 5617 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 5618 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5619 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5620 | |
| 5621 | // C++ [class.copy]p3: |
| 5622 | // A declaration of a constructor for a class X is ill-formed if |
| 5623 | // its first parameter is of type (optionally cv-qualified) X and |
| 5624 | // either there are no other parameters or else all other |
| 5625 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 5626 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5627 | ((Constructor->getNumParams() == 1) || |
| 5628 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5629 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 5630 | Constructor->getTemplateSpecializationKind() |
| 5631 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5632 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 5633 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 5634 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5635 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 5636 | const char *ConstRef |
| 5637 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 5638 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5639 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 5640 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5641 | |
| 5642 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 5643 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5644 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5645 | } |
| 5646 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5647 | } |
| 5648 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 5649 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 5650 | /// well-formedness, issuing any diagnostics required. Returns true |
| 5651 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5652 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5653 | CXXRecordDecl *RD = Destructor->getParent(); |
| 5654 | |
| 5655 | if (Destructor->isVirtual()) { |
| 5656 | SourceLocation Loc; |
| 5657 | |
| 5658 | if (!Destructor->isImplicit()) |
| 5659 | Loc = Destructor->getLocation(); |
| 5660 | else |
| 5661 | Loc = RD->getLocation(); |
| 5662 | |
| 5663 | // If we have a virtual destructor, look up the deallocation function |
| 5664 | FunctionDecl *OperatorDelete = 0; |
| 5665 | DeclarationName Name = |
| 5666 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5667 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5668 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 5669 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5670 | MarkFunctionReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5671 | |
| 5672 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5673 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5674 | |
| 5675 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5676 | } |
| 5677 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5678 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5679 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 5680 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 5681 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5682 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5683 | } |
| 5684 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5685 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 5686 | /// the well-formednes of the destructor declarator @p D with type @p |
| 5687 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5688 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 5689 | /// will be updated to reflect a well-formed type for the destructor and |
| 5690 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5691 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5692 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5693 | // C++ [class.dtor]p1: |
| 5694 | // [...] A typedef-name that names a class is a class-name |
| 5695 | // (7.1.3); however, a typedef-name that names a class shall not |
| 5696 | // be used as the identifier in the declarator for a destructor |
| 5697 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5698 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5699 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5700 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5701 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5702 | else if (const TemplateSpecializationType *TST = |
| 5703 | DeclaratorType->getAs<TemplateSpecializationType>()) |
| 5704 | if (TST->isTypeAlias()) |
| 5705 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
| 5706 | << DeclaratorType << 1; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5707 | |
| 5708 | // C++ [class.dtor]p2: |
| 5709 | // A destructor is used to destroy objects of its class type. A |
| 5710 | // destructor takes no parameters, and no return type can be |
| 5711 | // specified for it (not even void). The address of a destructor |
| 5712 | // shall not be taken. A destructor shall not be static. A |
| 5713 | // destructor can be invoked for a const, volatile or const |
| 5714 | // volatile object. A destructor shall not be declared const, |
| 5715 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5716 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5717 | if (!D.isInvalidType()) |
| 5718 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 5719 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5720 | << SourceRange(D.getIdentifierLoc()) |
| 5721 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 5722 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5723 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5724 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5725 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5726 | // Destructors don't have return types, but the parser will |
| 5727 | // happily parse something like: |
| 5728 | // |
| 5729 | // class X { |
| 5730 | // float ~X(); |
| 5731 | // }; |
| 5732 | // |
| 5733 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5734 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 5735 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 5736 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5737 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5738 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5739 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5740 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5741 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5742 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 5743 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5744 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5745 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 5746 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5747 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5748 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 5749 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5750 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5751 | } |
| 5752 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5753 | // C++0x [class.dtor]p2: |
| 5754 | // A destructor shall not be declared with a ref-qualifier. |
| 5755 | if (FTI.hasRefQualifier()) { |
| 5756 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 5757 | << FTI.RefQualifierIsLValueRef |
| 5758 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 5759 | D.setInvalidType(); |
| 5760 | } |
| 5761 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5762 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5763 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5764 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 5765 | |
| 5766 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5767 | FTI.freeArgs(); |
| 5768 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5769 | } |
| 5770 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5771 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5772 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5773 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5774 | D.setInvalidType(); |
| 5775 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5776 | |
| 5777 | // Rebuild the function type "R" without any type qualifiers or |
| 5778 | // parameters (in case any of the errors above fired) and with |
| 5779 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5780 | // types. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5781 | if (!D.isInvalidType()) |
| 5782 | return R; |
| 5783 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5784 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5785 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 5786 | EPI.Variadic = false; |
| 5787 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5788 | EPI.RefQualifier = RQ_None; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5789 | return Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5792 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 5793 | /// well-formednes of the conversion function declarator @p D with |
| 5794 | /// type @p R. If there are any errors in the declarator, this routine |
| 5795 | /// will emit diagnostics and return true. Otherwise, it will return |
| 5796 | /// false. Either way, the type @p R will be updated to reflect a |
| 5797 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5798 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5799 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5800 | // C++ [class.conv.fct]p1: |
| 5801 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 5802 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5803 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5804 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5805 | if (!D.isInvalidType()) |
| 5806 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 5807 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 5808 | << SourceRange(D.getIdentifierLoc()); |
| 5809 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5810 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5811 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5812 | |
| 5813 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 5814 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5815 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5816 | // Conversion functions don't have return types, but the parser will |
| 5817 | // happily parse something like: |
| 5818 | // |
| 5819 | // class X { |
| 5820 | // float operator bool(); |
| 5821 | // }; |
| 5822 | // |
| 5823 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5824 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 5825 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 5826 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5827 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5828 | } |
| 5829 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5830 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 5831 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5832 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5833 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5834 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 5835 | |
| 5836 | // Delete the parameters. |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5837 | D.getFunctionTypeInfo().freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5838 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5839 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5840 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5841 | D.setInvalidType(); |
| 5842 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5843 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5844 | // Diagnose "&operator bool()" and other such nonsense. This |
| 5845 | // is actually a gcc extension which we don't support. |
| 5846 | if (Proto->getResultType() != ConvType) { |
| 5847 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 5848 | << Proto->getResultType(); |
| 5849 | D.setInvalidType(); |
| 5850 | ConvType = Proto->getResultType(); |
| 5851 | } |
| 5852 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5853 | // C++ [class.conv.fct]p4: |
| 5854 | // The conversion-type-id shall not represent a function type nor |
| 5855 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5856 | if (ConvType->isArrayType()) { |
| 5857 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 5858 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5859 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5860 | } else if (ConvType->isFunctionType()) { |
| 5861 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 5862 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5863 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5864 | } |
| 5865 | |
| 5866 | // Rebuild the function type "R" without any parameters (in case any |
| 5867 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5868 | // return type. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5869 | if (D.isInvalidType()) |
| 5870 | R = Context.getFunctionType(ConvType, 0, 0, Proto->getExtProtoInfo()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5871 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5872 | // C++0x explicit conversion operators. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5873 | if (D.getDeclSpec().isExplicitSpecified()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5874 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5875 | getLangOpts().CPlusPlus11 ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5876 | diag::warn_cxx98_compat_explicit_conversion_functions : |
| 5877 | diag::ext_explicit_conversion_functions) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5878 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5879 | } |
| 5880 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5881 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 5882 | /// the declaration of the given C++ conversion function. This routine |
| 5883 | /// is responsible for recording the conversion function in the C++ |
| 5884 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5885 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5886 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 5887 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 5888 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5889 | |
| 5890 | // Make sure we aren't redeclaring the conversion function. |
| 5891 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5892 | |
| 5893 | // C++ [class.conv.fct]p1: |
| 5894 | // [...] A conversion function is never used to convert a |
| 5895 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 5896 | // same object type (or a reference to it), to a (possibly |
| 5897 | // cv-qualified) base class of that type (or a reference to it), |
| 5898 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5899 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 5900 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5901 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5902 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5903 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5904 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 5905 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 5906 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 5907 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 5908 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5909 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 5910 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 5911 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5912 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5913 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 5914 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5915 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5916 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 5917 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5918 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5919 | } |
| 5920 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 5921 | if (FunctionTemplateDecl *ConversionTemplate |
| 5922 | = Conversion->getDescribedFunctionTemplate()) |
| 5923 | return ConversionTemplate; |
| 5924 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5925 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5926 | } |
| 5927 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5928 | //===----------------------------------------------------------------------===// |
| 5929 | // Namespace Handling |
| 5930 | //===----------------------------------------------------------------------===// |
| 5931 | |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 5932 | /// \brief Diagnose a mismatch in 'inline' qualifiers when a namespace is |
| 5933 | /// reopened. |
| 5934 | static void DiagnoseNamespaceInlineMismatch(Sema &S, SourceLocation KeywordLoc, |
| 5935 | SourceLocation Loc, |
| 5936 | IdentifierInfo *II, bool *IsInline, |
| 5937 | NamespaceDecl *PrevNS) { |
| 5938 | assert(*IsInline != PrevNS->isInline()); |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 5939 | |
Richard Smith | c969e6a | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 5940 | // HACK: Work around a bug in libstdc++4.6's <atomic>, where |
| 5941 | // std::__atomic[0,1,2] are defined as non-inline namespaces, then reopened as |
| 5942 | // inline namespaces, with the intention of bringing names into namespace std. |
| 5943 | // |
| 5944 | // We support this just well enough to get that case working; this is not |
| 5945 | // sufficient to support reopening namespaces as inline in general. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 5946 | if (*IsInline && II && II->getName().startswith("__atomic") && |
| 5947 | S.getSourceManager().isInSystemHeader(Loc)) { |
Richard Smith | c969e6a | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 5948 | // Mark all prior declarations of the namespace as inline. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 5949 | for (NamespaceDecl *NS = PrevNS->getMostRecentDecl(); NS; |
| 5950 | NS = NS->getPreviousDecl()) |
| 5951 | NS->setInline(*IsInline); |
| 5952 | // Patch up the lookup table for the containing namespace. This isn't really |
| 5953 | // correct, but it's good enough for this particular case. |
| 5954 | for (DeclContext::decl_iterator I = PrevNS->decls_begin(), |
| 5955 | E = PrevNS->decls_end(); I != E; ++I) |
| 5956 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*I)) |
| 5957 | PrevNS->getParent()->makeDeclVisibleInContext(ND); |
| 5958 | return; |
| 5959 | } |
| 5960 | |
| 5961 | if (PrevNS->isInline()) |
| 5962 | // The user probably just forgot the 'inline', so suggest that it |
| 5963 | // be added back. |
| 5964 | S.Diag(Loc, diag::warn_inline_namespace_reopened_noninline) |
| 5965 | << FixItHint::CreateInsertion(KeywordLoc, "inline "); |
| 5966 | else |
| 5967 | S.Diag(Loc, diag::err_inline_namespace_mismatch) |
| 5968 | << IsInline; |
| 5969 | |
| 5970 | S.Diag(PrevNS->getLocation(), diag::note_previous_definition); |
| 5971 | *IsInline = PrevNS->isInline(); |
| 5972 | } |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 5973 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5974 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 5975 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5976 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 5977 | SourceLocation InlineLoc, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5978 | SourceLocation NamespaceLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 5979 | SourceLocation IdentLoc, |
| 5980 | IdentifierInfo *II, |
| 5981 | SourceLocation LBrace, |
| 5982 | AttributeList *AttrList) { |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5983 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 5984 | // For anonymous namespace, take the location of the left brace. |
| 5985 | SourceLocation Loc = II ? IdentLoc : LBrace; |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5986 | bool IsInline = InlineLoc.isValid(); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 5987 | bool IsInvalid = false; |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5988 | bool IsStd = false; |
| 5989 | bool AddToKnown = false; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5990 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 5991 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5992 | NamespaceDecl *PrevNS = 0; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5993 | if (II) { |
| 5994 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 5995 | // The identifier in an original-namespace-definition shall not |
| 5996 | // have been previously defined in the declarative region in |
| 5997 | // which the original-namespace-definition appears. The |
| 5998 | // identifier in an original-namespace-definition is the name of |
| 5999 | // the namespace. Subsequently in that declarative region, it is |
| 6000 | // treated as an original-namespace-name. |
| 6001 | // |
| 6002 | // Since namespace names are unique in their scope, and we don't |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6003 | // look through using directives, just look for any ordinary names. |
| 6004 | |
| 6005 | const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Member | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6006 | Decl::IDNS_Type | Decl::IDNS_Using | Decl::IDNS_Tag | |
| 6007 | Decl::IDNS_Namespace; |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6008 | NamedDecl *PrevDecl = 0; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 6009 | DeclContext::lookup_result R = CurContext->getRedeclContext()->lookup(II); |
| 6010 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 6011 | ++I) { |
| 6012 | if ((*I)->getIdentifierNamespace() & IDNS) { |
| 6013 | PrevDecl = *I; |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6014 | break; |
| 6015 | } |
| 6016 | } |
| 6017 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6018 | PrevNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl); |
| 6019 | |
| 6020 | if (PrevNS) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6021 | // This is an extended namespace definition. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6022 | if (IsInline != PrevNS->isInline()) |
| 6023 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, Loc, II, |
| 6024 | &IsInline, PrevNS); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6025 | } else if (PrevDecl) { |
| 6026 | // This is an invalid name redefinition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6027 | Diag(Loc, diag::err_redefinition_different_kind) |
| 6028 | << II; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6029 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6030 | IsInvalid = true; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6031 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6032 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6033 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 6034 | // This is the first "real" definition of the namespace "std", so update |
| 6035 | // our cache of the "std" namespace to point at this definition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6036 | PrevNS = getStdNamespace(); |
| 6037 | IsStd = true; |
| 6038 | AddToKnown = !IsInline; |
| 6039 | } else { |
| 6040 | // We've seen this namespace for the first time. |
| 6041 | AddToKnown = !IsInline; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6042 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6043 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6044 | // Anonymous namespaces. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6045 | |
| 6046 | // Determine whether the parent already has an anonymous namespace. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6047 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6048 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6049 | PrevNS = TU->getAnonymousNamespace(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6050 | } else { |
| 6051 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6052 | PrevNS = ND->getAnonymousNamespace(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6053 | } |
| 6054 | |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6055 | if (PrevNS && IsInline != PrevNS->isInline()) |
| 6056 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, NamespaceLoc, II, |
| 6057 | &IsInline, PrevNS); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6058 | } |
| 6059 | |
| 6060 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, IsInline, |
| 6061 | StartLoc, Loc, II, PrevNS); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6062 | if (IsInvalid) |
| 6063 | Namespc->setInvalidDecl(); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6064 | |
| 6065 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 6066 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6067 | // FIXME: Should we be merging attributes? |
| 6068 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 6069 | PushNamespaceVisibilityAttr(Attr, Loc); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6070 | |
| 6071 | if (IsStd) |
| 6072 | StdNamespace = Namespc; |
| 6073 | if (AddToKnown) |
| 6074 | KnownNamespaces[Namespc] = false; |
| 6075 | |
| 6076 | if (II) { |
| 6077 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 6078 | } else { |
| 6079 | // Link the anonymous namespace into its parent. |
| 6080 | DeclContext *Parent = CurContext->getRedeclContext(); |
| 6081 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 6082 | TU->setAnonymousNamespace(Namespc); |
| 6083 | } else { |
| 6084 | cast<NamespaceDecl>(Parent)->setAnonymousNamespace(Namespc); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6085 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6086 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 6087 | CurContext->addDecl(Namespc); |
| 6088 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6089 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 6090 | // behaves as if it were replaced by |
| 6091 | // namespace unique { /* empty body */ } |
| 6092 | // using namespace unique; |
| 6093 | // namespace unique { namespace-body } |
| 6094 | // where all occurrences of 'unique' in a translation unit are |
| 6095 | // replaced by the same identifier and this identifier differs |
| 6096 | // from all other identifiers in the entire program. |
| 6097 | |
| 6098 | // We just create the namespace with an empty name and then add an |
| 6099 | // implicit using declaration, just like the standard suggests. |
| 6100 | // |
| 6101 | // CodeGen enforces the "universally unique" aspect by giving all |
| 6102 | // declarations semantically contained within an anonymous |
| 6103 | // namespace internal linkage. |
| 6104 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6105 | if (!PrevNS) { |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6106 | UsingDirectiveDecl* UD |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6107 | = UsingDirectiveDecl::Create(Context, Parent, |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6108 | /* 'using' */ LBrace, |
| 6109 | /* 'namespace' */ SourceLocation(), |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 6110 | /* qualifier */ NestedNameSpecifierLoc(), |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6111 | /* identifier */ SourceLocation(), |
| 6112 | Namespc, |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6113 | /* Ancestor */ Parent); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6114 | UD->setImplicit(); |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6115 | Parent->addDecl(UD); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6116 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 6119 | ActOnDocumentableDecl(Namespc); |
| 6120 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6121 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 6122 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6123 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 6124 | // for the namespace has the declarations that showed up in that particular |
| 6125 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6126 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6127 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6128 | } |
| 6129 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6130 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 6131 | /// is a namespace alias, returns the namespace it points to. |
| 6132 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 6133 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 6134 | return AD->getNamespace(); |
| 6135 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 6136 | } |
| 6137 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6138 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 6139 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6140 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6141 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 6142 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6143 | Namespc->setRBraceLoc(RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6144 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 6145 | if (Namespc->hasAttr<VisibilityAttr>()) |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 6146 | PopPragmaVisibility(true, RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6147 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6148 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 6149 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 6150 | return cast_or_null<CXXRecordDecl>( |
| 6151 | StdBadAlloc.get(Context.getExternalSource())); |
| 6152 | } |
| 6153 | |
| 6154 | NamespaceDecl *Sema::getStdNamespace() const { |
| 6155 | return cast_or_null<NamespaceDecl>( |
| 6156 | StdNamespace.get(Context.getExternalSource())); |
| 6157 | } |
| 6158 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6159 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 6160 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 6161 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6162 | if (!StdNamespace) { |
| 6163 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 6164 | StdNamespace = NamespaceDecl::Create(Context, |
| 6165 | Context.getTranslationUnitDecl(), |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6166 | /*Inline=*/false, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6167 | SourceLocation(), SourceLocation(), |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6168 | &PP.getIdentifierTable().get("std"), |
| 6169 | /*PrevDecl=*/0); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 6170 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6171 | } |
| 6172 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 6173 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6176 | bool Sema::isStdInitializerList(QualType Ty, QualType *Element) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6177 | assert(getLangOpts().CPlusPlus && |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6178 | "Looking for std::initializer_list outside of C++."); |
| 6179 | |
| 6180 | // We're looking for implicit instantiations of |
| 6181 | // template <typename E> class std::initializer_list. |
| 6182 | |
| 6183 | if (!StdNamespace) // If we haven't seen namespace std yet, this can't be it. |
| 6184 | return false; |
| 6185 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6186 | ClassTemplateDecl *Template = 0; |
| 6187 | const TemplateArgument *Arguments = 0; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6188 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6189 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6190 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6191 | ClassTemplateSpecializationDecl *Specialization = |
| 6192 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 6193 | if (!Specialization) |
| 6194 | return false; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6195 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6196 | Template = Specialization->getSpecializedTemplate(); |
| 6197 | Arguments = Specialization->getTemplateArgs().data(); |
| 6198 | } else if (const TemplateSpecializationType *TST = |
| 6199 | Ty->getAs<TemplateSpecializationType>()) { |
| 6200 | Template = dyn_cast_or_null<ClassTemplateDecl>( |
| 6201 | TST->getTemplateName().getAsTemplateDecl()); |
| 6202 | Arguments = TST->getArgs(); |
| 6203 | } |
| 6204 | if (!Template) |
| 6205 | return false; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6206 | |
| 6207 | if (!StdInitializerList) { |
| 6208 | // Haven't recognized std::initializer_list yet, maybe this is it. |
| 6209 | CXXRecordDecl *TemplateClass = Template->getTemplatedDecl(); |
| 6210 | if (TemplateClass->getIdentifier() != |
| 6211 | &PP.getIdentifierTable().get("initializer_list") || |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6212 | !getStdNamespace()->InEnclosingNamespaceSetOf( |
| 6213 | TemplateClass->getDeclContext())) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6214 | return false; |
| 6215 | // This is a template called std::initializer_list, but is it the right |
| 6216 | // template? |
| 6217 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6218 | if (Params->getMinRequiredArguments() != 1) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6219 | return false; |
| 6220 | if (!isa<TemplateTypeParmDecl>(Params->getParam(0))) |
| 6221 | return false; |
| 6222 | |
| 6223 | // It's the right template. |
| 6224 | StdInitializerList = Template; |
| 6225 | } |
| 6226 | |
| 6227 | if (Template != StdInitializerList) |
| 6228 | return false; |
| 6229 | |
| 6230 | // This is an instance of std::initializer_list. Find the argument type. |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6231 | if (Element) |
| 6232 | *Element = Arguments[0].getAsType(); |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6233 | return true; |
| 6234 | } |
| 6235 | |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 6236 | static ClassTemplateDecl *LookupStdInitializerList(Sema &S, SourceLocation Loc){ |
| 6237 | NamespaceDecl *Std = S.getStdNamespace(); |
| 6238 | if (!Std) { |
| 6239 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 6240 | return 0; |
| 6241 | } |
| 6242 | |
| 6243 | LookupResult Result(S, &S.PP.getIdentifierTable().get("initializer_list"), |
| 6244 | Loc, Sema::LookupOrdinaryName); |
| 6245 | if (!S.LookupQualifiedName(Result, Std)) { |
| 6246 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 6247 | return 0; |
| 6248 | } |
| 6249 | ClassTemplateDecl *Template = Result.getAsSingle<ClassTemplateDecl>(); |
| 6250 | if (!Template) { |
| 6251 | Result.suppressDiagnostics(); |
| 6252 | // We found something weird. Complain about the first thing we found. |
| 6253 | NamedDecl *Found = *Result.begin(); |
| 6254 | S.Diag(Found->getLocation(), diag::err_malformed_std_initializer_list); |
| 6255 | return 0; |
| 6256 | } |
| 6257 | |
| 6258 | // We found some template called std::initializer_list. Now verify that it's |
| 6259 | // correct. |
| 6260 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6261 | if (Params->getMinRequiredArguments() != 1 || |
| 6262 | !isa<TemplateTypeParmDecl>(Params->getParam(0))) { |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 6263 | S.Diag(Template->getLocation(), diag::err_malformed_std_initializer_list); |
| 6264 | return 0; |
| 6265 | } |
| 6266 | |
| 6267 | return Template; |
| 6268 | } |
| 6269 | |
| 6270 | QualType Sema::BuildStdInitializerList(QualType Element, SourceLocation Loc) { |
| 6271 | if (!StdInitializerList) { |
| 6272 | StdInitializerList = LookupStdInitializerList(*this, Loc); |
| 6273 | if (!StdInitializerList) |
| 6274 | return QualType(); |
| 6275 | } |
| 6276 | |
| 6277 | TemplateArgumentListInfo Args(Loc, Loc); |
| 6278 | Args.addArgument(TemplateArgumentLoc(TemplateArgument(Element), |
| 6279 | Context.getTrivialTypeSourceInfo(Element, |
| 6280 | Loc))); |
| 6281 | return Context.getCanonicalType( |
| 6282 | CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args)); |
| 6283 | } |
| 6284 | |
Sebastian Redl | 98d3606 | 2012-01-17 22:50:14 +0000 | [diff] [blame] | 6285 | bool Sema::isInitListConstructor(const CXXConstructorDecl* Ctor) { |
| 6286 | // C++ [dcl.init.list]p2: |
| 6287 | // A constructor is an initializer-list constructor if its first parameter |
| 6288 | // is of type std::initializer_list<E> or reference to possibly cv-qualified |
| 6289 | // std::initializer_list<E> for some type E, and either there are no other |
| 6290 | // parameters or else all other parameters have default arguments. |
| 6291 | if (Ctor->getNumParams() < 1 || |
| 6292 | (Ctor->getNumParams() > 1 && !Ctor->getParamDecl(1)->hasDefaultArg())) |
| 6293 | return false; |
| 6294 | |
| 6295 | QualType ArgType = Ctor->getParamDecl(0)->getType(); |
| 6296 | if (const ReferenceType *RT = ArgType->getAs<ReferenceType>()) |
| 6297 | ArgType = RT->getPointeeType().getUnqualifiedType(); |
| 6298 | |
| 6299 | return isStdInitializerList(ArgType, 0); |
| 6300 | } |
| 6301 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 6302 | /// \brief Determine whether a using statement is in a context where it will be |
| 6303 | /// apply in all contexts. |
| 6304 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 6305 | switch (CurContext->getDeclKind()) { |
| 6306 | case Decl::TranslationUnit: |
| 6307 | return true; |
| 6308 | case Decl::LinkageSpec: |
| 6309 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 6310 | default: |
| 6311 | return false; |
| 6312 | } |
| 6313 | } |
| 6314 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6315 | namespace { |
| 6316 | |
| 6317 | // Callback to only accept typo corrections that are namespaces. |
| 6318 | class NamespaceValidatorCCC : public CorrectionCandidateCallback { |
| 6319 | public: |
| 6320 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 6321 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 6322 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 6323 | } |
| 6324 | return false; |
| 6325 | } |
| 6326 | }; |
| 6327 | |
| 6328 | } |
| 6329 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6330 | static bool TryNamespaceTypoCorrection(Sema &S, LookupResult &R, Scope *Sc, |
| 6331 | CXXScopeSpec &SS, |
| 6332 | SourceLocation IdentLoc, |
| 6333 | IdentifierInfo *Ident) { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6334 | NamespaceValidatorCCC Validator; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6335 | R.clear(); |
| 6336 | if (TypoCorrection Corrected = S.CorrectTypo(R.getLookupNameInfo(), |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6337 | R.getLookupKind(), Sc, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 6338 | Validator)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6339 | std::string CorrectedStr(Corrected.getAsString(S.getLangOpts())); |
| 6340 | std::string CorrectedQuotedStr(Corrected.getQuoted(S.getLangOpts())); |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6341 | if (DeclContext *DC = S.computeDeclContext(SS, false)) |
| 6342 | S.Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 6343 | << Ident << DC << CorrectedQuotedStr << SS.getRange() |
David Blaikie | 6952c01 | 2012-10-12 20:00:44 +0000 | [diff] [blame] | 6344 | << FixItHint::CreateReplacement(Corrected.getCorrectionRange(), |
| 6345 | CorrectedStr); |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6346 | else |
| 6347 | S.Diag(IdentLoc, diag::err_using_directive_suggest) |
| 6348 | << Ident << CorrectedQuotedStr |
| 6349 | << FixItHint::CreateReplacement(IdentLoc, CorrectedStr); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6350 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6351 | S.Diag(Corrected.getCorrectionDecl()->getLocation(), |
| 6352 | diag::note_namespace_defined_here) << CorrectedQuotedStr; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6353 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6354 | R.addDecl(Corrected.getCorrectionDecl()); |
| 6355 | return true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6356 | } |
| 6357 | return false; |
| 6358 | } |
| 6359 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6360 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6361 | SourceLocation UsingLoc, |
| 6362 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6363 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6364 | SourceLocation IdentLoc, |
| 6365 | IdentifierInfo *NamespcName, |
| 6366 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6367 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 6368 | assert(NamespcName && "Invalid NamespcName."); |
| 6369 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 6370 | |
| 6371 | // This can only happen along a recovery path. |
| 6372 | while (S->getFlags() & Scope::TemplateParamScope) |
| 6373 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6374 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6375 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6376 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6377 | NestedNameSpecifier *Qualifier = 0; |
| 6378 | if (SS.isSet()) |
| 6379 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6380 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 6381 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6382 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 6383 | LookupParsedName(R, S, &SS); |
| 6384 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6385 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6386 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6387 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6388 | R.clear(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6389 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 6390 | // "std" hasn't been defined yet, for GCC compatibility. |
| 6391 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 6392 | NamespcName->isStr("std")) { |
| 6393 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 6394 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6395 | R.resolveKind(); |
| 6396 | } |
| 6397 | // Otherwise, attempt typo correction. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6398 | else TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6399 | } |
| 6400 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6401 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6402 | NamedDecl *Named = R.getFoundDecl(); |
| 6403 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 6404 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6405 | // C++ [namespace.udir]p1: |
| 6406 | // A using-directive specifies that the names in the nominated |
| 6407 | // namespace can be used in the scope in which the |
| 6408 | // using-directive appears after the using-directive. During |
| 6409 | // unqualified name lookup (3.4.1), the names appear as if they |
| 6410 | // were declared in the nearest enclosing namespace which |
| 6411 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6412 | // namespace. [Note: in this context, "contains" means "contains |
| 6413 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6414 | |
| 6415 | // Find enclosing context containing both using-directive and |
| 6416 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6417 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6418 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 6419 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 6420 | CommonAncestor = CommonAncestor->getParent(); |
| 6421 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6422 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 6423 | SS.getWithLocInContext(Context), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6424 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 6425 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 6426 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 6427 | !SourceMgr.isFromMainFile(SourceMgr.getExpansionLoc(IdentLoc))) { |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 6428 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 6429 | } |
| 6430 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6431 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6432 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 6433 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6434 | } |
| 6435 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6436 | // FIXME: We ignore attributes for now. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6437 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6438 | } |
| 6439 | |
| 6440 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 6441 | // If the scope has an associated entity and the using directive is at |
| 6442 | // namespace or translation unit scope, add the UsingDirectiveDecl into |
| 6443 | // its lookup structure so qualified name lookup can find it. |
| 6444 | DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity()); |
| 6445 | if (Ctx && !Ctx->isFunctionOrMethod()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6446 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6447 | else |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 6448 | // Otherwise, it is at block sope. The using-directives will affect lookup |
| 6449 | // only to the end of the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6450 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6451 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6452 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6453 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6454 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 6455 | AccessSpecifier AS, |
| 6456 | bool HasUsingKeyword, |
| 6457 | SourceLocation UsingLoc, |
| 6458 | CXXScopeSpec &SS, |
| 6459 | UnqualifiedId &Name, |
| 6460 | AttributeList *AttrList, |
| 6461 | bool IsTypeName, |
| 6462 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6463 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6464 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6465 | switch (Name.getKind()) { |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 6466 | case UnqualifiedId::IK_ImplicitSelfParam: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6467 | case UnqualifiedId::IK_Identifier: |
| 6468 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 6469 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6470 | case UnqualifiedId::IK_ConversionFunctionId: |
| 6471 | break; |
| 6472 | |
| 6473 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 6474 | case UnqualifiedId::IK_ConstructorTemplateId: |
Richard Smith | a1366cb | 2012-04-27 19:33:05 +0000 | [diff] [blame] | 6475 | // C++11 inheriting constructors. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6476 | Diag(Name.getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6477 | getLangOpts().CPlusPlus11 ? |
Richard Smith | a1366cb | 2012-04-27 19:33:05 +0000 | [diff] [blame] | 6478 | // FIXME: Produce warn_cxx98_compat_using_decl_constructor |
| 6479 | // instead once inheriting constructors work. |
| 6480 | diag::err_using_decl_constructor_unsupported : |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6481 | diag::err_using_decl_constructor) |
| 6482 | << SS.getRange(); |
| 6483 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6484 | if (getLangOpts().CPlusPlus11) break; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6485 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6486 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6487 | |
| 6488 | case UnqualifiedId::IK_DestructorName: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6489 | Diag(Name.getLocStart(), diag::err_using_decl_destructor) |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6490 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6491 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6492 | |
| 6493 | case UnqualifiedId::IK_TemplateId: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6494 | Diag(Name.getLocStart(), diag::err_using_decl_template_id) |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6495 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6496 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6497 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6498 | |
| 6499 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 6500 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6501 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6502 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6503 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 6504 | // Warn about using declarations. |
| 6505 | // TODO: store that the declaration was written without 'using' and |
| 6506 | // talk about access decls instead of using decls in the |
| 6507 | // diagnostics. |
| 6508 | if (!HasUsingKeyword) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6509 | UsingLoc = Name.getLocStart(); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 6510 | |
| 6511 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6512 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 6513 | } |
| 6514 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 6515 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 6516 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 6517 | return 0; |
| 6518 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6519 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6520 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6521 | /* IsInstantiation */ false, |
| 6522 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6523 | if (UD) |
| 6524 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6525 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6526 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6527 | } |
| 6528 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6529 | /// \brief Determine whether a using declaration considers the given |
| 6530 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 6531 | /// the same entity or are both typedefs of the same type. |
| 6532 | static bool |
| 6533 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 6534 | bool &SuppressRedeclaration) { |
| 6535 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 6536 | SuppressRedeclaration = false; |
| 6537 | return true; |
| 6538 | } |
| 6539 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6540 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
| 6541 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6542 | SuppressRedeclaration = true; |
| 6543 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 6544 | TD2->getUnderlyingType()); |
| 6545 | } |
| 6546 | |
| 6547 | return false; |
| 6548 | } |
| 6549 | |
| 6550 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6551 | /// Determines whether to create a using shadow decl for a particular |
| 6552 | /// decl, given the set of decls existing prior to this using lookup. |
| 6553 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 6554 | const LookupResult &Previous) { |
| 6555 | // Diagnose finding a decl which is not from a base class of the |
| 6556 | // current class. We do this now because there are cases where this |
| 6557 | // function will silently decide not to build a shadow decl, which |
| 6558 | // will pre-empt further diagnostics. |
| 6559 | // |
| 6560 | // We don't need to do this in C++0x because we do the check once on |
| 6561 | // the qualifier. |
| 6562 | // |
| 6563 | // FIXME: diagnose the following if we care enough: |
| 6564 | // struct A { int foo; }; |
| 6565 | // struct B : A { using A::foo; }; |
| 6566 | // template <class T> struct C : A {}; |
| 6567 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 6568 | // This is invalid (during instantiation) in C++03 because B::foo |
| 6569 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 6570 | // We can't diagnose it immediately because C<T> is an unknown |
| 6571 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 6572 | // to A::foo, which will look well-formed when we instantiate. |
| 6573 | // The right solution is to not collapse the shadow-decl chain. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6574 | if (!getLangOpts().CPlusPlus11 && CurContext->isRecord()) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6575 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 6576 | |
| 6577 | // Handle enums and anonymous structs. |
| 6578 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 6579 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 6580 | while (OrigRec->isAnonymousStructOrUnion()) |
| 6581 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 6582 | |
| 6583 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 6584 | if (OrigDC == CurContext) { |
| 6585 | Diag(Using->getLocation(), |
| 6586 | diag::err_using_decl_nested_name_specifier_is_current_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6587 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6588 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 6589 | return true; |
| 6590 | } |
| 6591 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6592 | Diag(Using->getQualifierLoc().getBeginLoc(), |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6593 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6594 | << Using->getQualifier() |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6595 | << cast<CXXRecordDecl>(CurContext) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6596 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6597 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 6598 | return true; |
| 6599 | } |
| 6600 | } |
| 6601 | |
| 6602 | if (Previous.empty()) return false; |
| 6603 | |
| 6604 | NamedDecl *Target = Orig; |
| 6605 | if (isa<UsingShadowDecl>(Target)) |
| 6606 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 6607 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 6608 | // If the target happens to be one of the previous declarations, we |
| 6609 | // don't have a conflict. |
| 6610 | // |
| 6611 | // FIXME: but we might be increasing its access, in which case we |
| 6612 | // should redeclare it. |
| 6613 | NamedDecl *NonTag = 0, *Tag = 0; |
| 6614 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6615 | I != E; ++I) { |
| 6616 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6617 | bool Result; |
| 6618 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 6619 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 6620 | |
| 6621 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 6622 | } |
| 6623 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6624 | if (Target->isFunctionOrFunctionTemplate()) { |
| 6625 | FunctionDecl *FD; |
| 6626 | if (isa<FunctionTemplateDecl>(Target)) |
| 6627 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 6628 | else |
| 6629 | FD = cast<FunctionDecl>(Target); |
| 6630 | |
| 6631 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 6632 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6633 | case Ovl_Overload: |
| 6634 | return false; |
| 6635 | |
| 6636 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6637 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6638 | break; |
| 6639 | |
| 6640 | // We found a decl with the exact signature. |
| 6641 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6642 | // If we're in a record, we want to hide the target, so we |
| 6643 | // return true (without a diagnostic) to tell the caller not to |
| 6644 | // build a shadow decl. |
| 6645 | if (CurContext->isRecord()) |
| 6646 | return true; |
| 6647 | |
| 6648 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6649 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6650 | break; |
| 6651 | } |
| 6652 | |
| 6653 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6654 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 6655 | return true; |
| 6656 | } |
| 6657 | |
| 6658 | // Target is not a function. |
| 6659 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6660 | if (isa<TagDecl>(Target)) { |
| 6661 | // No conflict between a tag and a non-tag. |
| 6662 | if (!Tag) return false; |
| 6663 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6664 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6665 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6666 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 6667 | return true; |
| 6668 | } |
| 6669 | |
| 6670 | // No conflict between a tag and a non-tag. |
| 6671 | if (!NonTag) return false; |
| 6672 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6673 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6674 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6675 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 6676 | return true; |
| 6677 | } |
| 6678 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6679 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6680 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6681 | UsingDecl *UD, |
| 6682 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6683 | |
| 6684 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6685 | NamedDecl *Target = Orig; |
| 6686 | if (isa<UsingShadowDecl>(Target)) { |
| 6687 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 6688 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6689 | } |
| 6690 | |
| 6691 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6692 | = UsingShadowDecl::Create(Context, CurContext, |
| 6693 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6694 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 6695 | |
| 6696 | Shadow->setAccess(UD->getAccess()); |
| 6697 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 6698 | Shadow->setInvalidDecl(); |
| 6699 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6700 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6701 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6702 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6703 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6704 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6705 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6706 | return Shadow; |
| 6707 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6708 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6709 | /// Hides a using shadow declaration. This is required by the current |
| 6710 | /// using-decl implementation when a resolvable using declaration in a |
| 6711 | /// class is followed by a declaration which would hide or override |
| 6712 | /// one or more of the using decl's targets; for example: |
| 6713 | /// |
| 6714 | /// struct Base { void foo(int); }; |
| 6715 | /// struct Derived : Base { |
| 6716 | /// using Base::foo; |
| 6717 | /// void foo(int); |
| 6718 | /// }; |
| 6719 | /// |
| 6720 | /// The governing language is C++03 [namespace.udecl]p12: |
| 6721 | /// |
| 6722 | /// When a using-declaration brings names from a base class into a |
| 6723 | /// derived class scope, member functions in the derived class |
| 6724 | /// override and/or hide member functions with the same name and |
| 6725 | /// parameter types in a base class (rather than conflicting). |
| 6726 | /// |
| 6727 | /// There are two ways to implement this: |
| 6728 | /// (1) optimistically create shadow decls when they're not hidden |
| 6729 | /// by existing declarations, or |
| 6730 | /// (2) don't create any shadow decls (or at least don't make them |
| 6731 | /// visible) until we've fully parsed/instantiated the class. |
| 6732 | /// The problem with (1) is that we might have to retroactively remove |
| 6733 | /// a shadow decl, which requires several O(n) operations because the |
| 6734 | /// decl structures are (very reasonably) not designed for removal. |
| 6735 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 6736 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6737 | if (Shadow->getDeclName().getNameKind() == |
| 6738 | DeclarationName::CXXConversionFunctionName) |
| 6739 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 6740 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6741 | // Remove it from the DeclContext... |
| 6742 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6743 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6744 | // ...and the scope, if applicable... |
| 6745 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6746 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6747 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6748 | } |
| 6749 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6750 | // ...and the using decl. |
| 6751 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 6752 | |
| 6753 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6754 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6755 | } |
| 6756 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6757 | /// Builds a using declaration. |
| 6758 | /// |
| 6759 | /// \param IsInstantiation - Whether this call arises from an |
| 6760 | /// instantiation of an unresolved using declaration. We treat |
| 6761 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6762 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 6763 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6764 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6765 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6766 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6767 | bool IsInstantiation, |
| 6768 | bool IsTypeName, |
| 6769 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6770 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6771 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6772 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 6773 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 6774 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6775 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6776 | if (SS.isEmpty()) { |
| 6777 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6778 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6779 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6780 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6781 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6782 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6783 | ForRedeclaration); |
| 6784 | Previous.setHideTags(false); |
| 6785 | if (S) { |
| 6786 | LookupName(Previous, S); |
| 6787 | |
| 6788 | // It is really dumb that we have to do this. |
| 6789 | LookupResult::Filter F = Previous.makeFilter(); |
| 6790 | while (F.hasNext()) { |
| 6791 | NamedDecl *D = F.next(); |
| 6792 | if (!isDeclInScope(D, CurContext, S)) |
| 6793 | F.erase(); |
| 6794 | } |
| 6795 | F.done(); |
| 6796 | } else { |
| 6797 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 6798 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 6799 | LookupQualifiedName(Previous, CurContext); |
| 6800 | } |
| 6801 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6802 | // Check for invalid redeclarations. |
| 6803 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 6804 | return 0; |
| 6805 | |
| 6806 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6807 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 6808 | return 0; |
| 6809 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 6810 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6811 | NamedDecl *D; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6812 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 6813 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6814 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6815 | // FIXME: not all declaration name kinds are legal here |
| 6816 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 6817 | UsingLoc, TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6818 | QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6819 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6820 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6821 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 6822 | QualifierLoc, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6823 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6824 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6825 | D = UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, |
| 6826 | NameInfo, IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 6827 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6828 | D->setAccess(AS); |
| 6829 | CurContext->addDecl(D); |
| 6830 | |
| 6831 | if (!LookupContext) return D; |
| 6832 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6833 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6834 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6835 | UD->setInvalidDecl(); |
| 6836 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6837 | } |
| 6838 | |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 6839 | // The normal rules do not apply to inheriting constructor declarations. |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6840 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 6841 | if (CheckInheritingConstructorUsingDecl(UD)) |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 6842 | UD->setInvalidDecl(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6843 | return UD; |
| 6844 | } |
| 6845 | |
| 6846 | // Otherwise, look up the target name. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6847 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6848 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6849 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6850 | // Unlike most lookups, we don't always want to hide tag |
| 6851 | // declarations: tag names are visible through the using declaration |
| 6852 | // even if hidden by ordinary names, *except* in a dependent context |
| 6853 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6854 | if (!IsInstantiation) |
| 6855 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6856 | |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 6857 | // For the purposes of this lookup, we have a base object type |
| 6858 | // equal to that of the current context. |
| 6859 | if (CurContext->isRecord()) { |
| 6860 | R.setBaseObjectType( |
| 6861 | Context.getTypeDeclType(cast<CXXRecordDecl>(CurContext))); |
| 6862 | } |
| 6863 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6864 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6865 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6866 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6867 | Diag(IdentLoc, diag::err_no_member) |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6868 | << NameInfo.getName() << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6869 | UD->setInvalidDecl(); |
| 6870 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6871 | } |
| 6872 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6873 | if (R.isAmbiguous()) { |
| 6874 | UD->setInvalidDecl(); |
| 6875 | return UD; |
| 6876 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6877 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6878 | if (IsTypeName) { |
| 6879 | // If we asked for a typename and got a non-type decl, error out. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6880 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6881 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 6882 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 6883 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 6884 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6885 | UD->setInvalidDecl(); |
| 6886 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6887 | } |
| 6888 | } else { |
| 6889 | // If we asked for a non-typename and we got a type, error out, |
| 6890 | // but only if this is an instantiation of an unresolved using |
| 6891 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6892 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6893 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 6894 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6895 | UD->setInvalidDecl(); |
| 6896 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6897 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6898 | } |
| 6899 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 6900 | // C++0x N2914 [namespace.udecl]p6: |
| 6901 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6902 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 6903 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 6904 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6905 | UD->setInvalidDecl(); |
| 6906 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 6907 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6908 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6909 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 6910 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 6911 | BuildUsingShadowDecl(S, UD, *I); |
| 6912 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6913 | |
| 6914 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6915 | } |
| 6916 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6917 | /// Additional checks for a using declaration referring to a constructor name. |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 6918 | bool Sema::CheckInheritingConstructorUsingDecl(UsingDecl *UD) { |
| 6919 | assert(!UD->isTypeName() && "expecting a constructor name"); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6920 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6921 | const Type *SourceType = UD->getQualifier()->getAsType(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6922 | assert(SourceType && |
| 6923 | "Using decl naming constructor doesn't have type in scope spec."); |
| 6924 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 6925 | |
| 6926 | // Check whether the named type is a direct base class. |
| 6927 | CanQualType CanonicalSourceType = SourceType->getCanonicalTypeUnqualified(); |
| 6928 | CXXRecordDecl::base_class_iterator BaseIt, BaseE; |
| 6929 | for (BaseIt = TargetClass->bases_begin(), BaseE = TargetClass->bases_end(); |
| 6930 | BaseIt != BaseE; ++BaseIt) { |
| 6931 | CanQualType BaseType = BaseIt->getType()->getCanonicalTypeUnqualified(); |
| 6932 | if (CanonicalSourceType == BaseType) |
| 6933 | break; |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 6934 | if (BaseIt->getType()->isDependentType()) |
| 6935 | break; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6936 | } |
| 6937 | |
| 6938 | if (BaseIt == BaseE) { |
| 6939 | // Did not find SourceType in the bases. |
| 6940 | Diag(UD->getUsingLocation(), |
| 6941 | diag::err_using_decl_constructor_not_in_direct_base) |
| 6942 | << UD->getNameInfo().getSourceRange() |
| 6943 | << QualType(SourceType, 0) << TargetClass; |
| 6944 | return true; |
| 6945 | } |
| 6946 | |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 6947 | if (!CurContext->isDependentContext()) |
| 6948 | BaseIt->setInheritConstructors(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6949 | |
| 6950 | return false; |
| 6951 | } |
| 6952 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6953 | /// Checks that the given using declaration is not an invalid |
| 6954 | /// redeclaration. Note that this is checking only for the using decl |
| 6955 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 6956 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 6957 | bool isTypeName, |
| 6958 | const CXXScopeSpec &SS, |
| 6959 | SourceLocation NameLoc, |
| 6960 | const LookupResult &Prev) { |
| 6961 | // C++03 [namespace.udecl]p8: |
| 6962 | // C++0x [namespace.udecl]p10: |
| 6963 | // A using-declaration is a declaration and can therefore be used |
| 6964 | // repeatedly where (and only where) multiple declarations are |
| 6965 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 6966 | // |
John McCall | 8a72621 | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 6967 | // That's in non-member contexts. |
| 6968 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6969 | return false; |
| 6970 | |
| 6971 | NestedNameSpecifier *Qual |
| 6972 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 6973 | |
| 6974 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 6975 | NamedDecl *D = *I; |
| 6976 | |
| 6977 | bool DTypename; |
| 6978 | NestedNameSpecifier *DQual; |
| 6979 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 6980 | DTypename = UD->isTypeName(); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6981 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6982 | } else if (UnresolvedUsingValueDecl *UD |
| 6983 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 6984 | DTypename = false; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6985 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6986 | } else if (UnresolvedUsingTypenameDecl *UD |
| 6987 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 6988 | DTypename = true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6989 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6990 | } else continue; |
| 6991 | |
| 6992 | // using decls differ if one says 'typename' and the other doesn't. |
| 6993 | // FIXME: non-dependent using decls? |
| 6994 | if (isTypeName != DTypename) continue; |
| 6995 | |
| 6996 | // using decls differ if they name different scopes (but note that |
| 6997 | // template instantiation can cause this check to trigger when it |
| 6998 | // didn't before instantiation). |
| 6999 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 7000 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 7001 | continue; |
| 7002 | |
| 7003 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7004 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7005 | return true; |
| 7006 | } |
| 7007 | |
| 7008 | return false; |
| 7009 | } |
| 7010 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7011 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7012 | /// Checks that the given nested-name qualifier used in a using decl |
| 7013 | /// in the current context is appropriately related to the current |
| 7014 | /// scope. If an error is found, diagnoses it and returns true. |
| 7015 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 7016 | const CXXScopeSpec &SS, |
| 7017 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7018 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7019 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7020 | if (!CurContext->isRecord()) { |
| 7021 | // C++03 [namespace.udecl]p3: |
| 7022 | // C++0x [namespace.udecl]p8: |
| 7023 | // A using-declaration for a class member shall be a member-declaration. |
| 7024 | |
| 7025 | // If we weren't able to compute a valid scope, it must be a |
| 7026 | // dependent class scope. |
| 7027 | if (!NamedContext || NamedContext->isRecord()) { |
| 7028 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 7029 | << SS.getRange(); |
| 7030 | return true; |
| 7031 | } |
| 7032 | |
| 7033 | // Otherwise, everything is known to be fine. |
| 7034 | return false; |
| 7035 | } |
| 7036 | |
| 7037 | // The current scope is a record. |
| 7038 | |
| 7039 | // If the named context is dependent, we can't decide much. |
| 7040 | if (!NamedContext) { |
| 7041 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 7042 | // nested-name-specifier does not refer to a base class, which is |
| 7043 | // still possible in some cases. |
| 7044 | |
| 7045 | // Otherwise we have to conservatively report that things might be |
| 7046 | // okay. |
| 7047 | return false; |
| 7048 | } |
| 7049 | |
| 7050 | if (!NamedContext->isRecord()) { |
| 7051 | // Ideally this would point at the last name in the specifier, |
| 7052 | // but we don't have that level of source info. |
| 7053 | Diag(SS.getRange().getBegin(), |
| 7054 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 7055 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 7056 | return true; |
| 7057 | } |
| 7058 | |
Douglas Gregor | 6fb0729 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 7059 | if (!NamedContext->isDependentContext() && |
| 7060 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 7061 | return true; |
| 7062 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7063 | if (getLangOpts().CPlusPlus11) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7064 | // C++0x [namespace.udecl]p3: |
| 7065 | // In a using-declaration used as a member-declaration, the |
| 7066 | // nested-name-specifier shall name a base class of the class |
| 7067 | // being defined. |
| 7068 | |
| 7069 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 7070 | cast<CXXRecordDecl>(NamedContext))) { |
| 7071 | if (CurContext == NamedContext) { |
| 7072 | Diag(NameLoc, |
| 7073 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 7074 | << SS.getRange(); |
| 7075 | return true; |
| 7076 | } |
| 7077 | |
| 7078 | Diag(SS.getRange().getBegin(), |
| 7079 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 7080 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 7081 | << cast<CXXRecordDecl>(CurContext) |
| 7082 | << SS.getRange(); |
| 7083 | return true; |
| 7084 | } |
| 7085 | |
| 7086 | return false; |
| 7087 | } |
| 7088 | |
| 7089 | // C++03 [namespace.udecl]p4: |
| 7090 | // A using-declaration used as a member-declaration shall refer |
| 7091 | // to a member of a base class of the class being defined [etc.]. |
| 7092 | |
| 7093 | // Salient point: SS doesn't have to name a base class as long as |
| 7094 | // lookup only finds members from base classes. Therefore we can |
| 7095 | // diagnose here only if we can prove that that can't happen, |
| 7096 | // i.e. if the class hierarchies provably don't intersect. |
| 7097 | |
| 7098 | // TODO: it would be nice if "definitely valid" results were cached |
| 7099 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 7100 | // need to be repeated. |
| 7101 | |
| 7102 | struct UserData { |
Benjamin Kramer | 8c43dcc | 2012-02-23 16:06:01 +0000 | [diff] [blame] | 7103 | llvm::SmallPtrSet<const CXXRecordDecl*, 4> Bases; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7104 | |
| 7105 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 7106 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 7107 | Data->Bases.insert(Base); |
| 7108 | return true; |
| 7109 | } |
| 7110 | |
| 7111 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 7112 | return !Class->forallBases(collect, this); |
| 7113 | } |
| 7114 | |
| 7115 | /// Returns true if the base is dependent or is one of the |
| 7116 | /// accumulated base classes. |
| 7117 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 7118 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 7119 | return !Data->Bases.count(Base); |
| 7120 | } |
| 7121 | |
| 7122 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 7123 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 7124 | } |
| 7125 | }; |
| 7126 | |
| 7127 | UserData Data; |
| 7128 | |
| 7129 | // Returns false if we find a dependent base. |
| 7130 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 7131 | return false; |
| 7132 | |
| 7133 | // Returns false if the class has a dependent base or if it or one |
| 7134 | // of its bases is present in the base set of the current context. |
| 7135 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 7136 | return false; |
| 7137 | |
| 7138 | Diag(SS.getRange().getBegin(), |
| 7139 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 7140 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 7141 | << cast<CXXRecordDecl>(CurContext) |
| 7142 | << SS.getRange(); |
| 7143 | |
| 7144 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7145 | } |
| 7146 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7147 | Decl *Sema::ActOnAliasDeclaration(Scope *S, |
| 7148 | AccessSpecifier AS, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7149 | MultiTemplateParamsArg TemplateParamLists, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7150 | SourceLocation UsingLoc, |
| 7151 | UnqualifiedId &Name, |
| 7152 | TypeResult Type) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7153 | // Skip up to the relevant declaration scope. |
| 7154 | while (S->getFlags() & Scope::TemplateParamScope) |
| 7155 | S = S->getParent(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7156 | assert((S->getFlags() & Scope::DeclScope) && |
| 7157 | "got alias-declaration outside of declaration scope"); |
| 7158 | |
| 7159 | if (Type.isInvalid()) |
| 7160 | return 0; |
| 7161 | |
| 7162 | bool Invalid = false; |
| 7163 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
| 7164 | TypeSourceInfo *TInfo = 0; |
Nick Lewycky | b79bf1d | 2011-05-02 01:07:19 +0000 | [diff] [blame] | 7165 | GetTypeFromParser(Type.get(), &TInfo); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7166 | |
| 7167 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
| 7168 | return 0; |
| 7169 | |
| 7170 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7171 | UPPC_DeclarationType)) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7172 | Invalid = true; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7173 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 7174 | TInfo->getTypeLoc().getBeginLoc()); |
| 7175 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7176 | |
| 7177 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, ForRedeclaration); |
| 7178 | LookupName(Previous, S); |
| 7179 | |
| 7180 | // Warn about shadowing the name of a template parameter. |
| 7181 | if (Previous.isSingleResult() && |
| 7182 | Previous.getFoundDecl()->isTemplateParameter()) { |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 7183 | DiagnoseTemplateParameterShadow(Name.StartLocation,Previous.getFoundDecl()); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7184 | Previous.clear(); |
| 7185 | } |
| 7186 | |
| 7187 | assert(Name.Kind == UnqualifiedId::IK_Identifier && |
| 7188 | "name in alias declaration must be an identifier"); |
| 7189 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 7190 | Name.StartLocation, |
| 7191 | Name.Identifier, TInfo); |
| 7192 | |
| 7193 | NewTD->setAccess(AS); |
| 7194 | |
| 7195 | if (Invalid) |
| 7196 | NewTD->setInvalidDecl(); |
| 7197 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7198 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 7199 | Invalid |= NewTD->isInvalidDecl(); |
| 7200 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7201 | bool Redeclaration = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7202 | |
| 7203 | NamedDecl *NewND; |
| 7204 | if (TemplateParamLists.size()) { |
| 7205 | TypeAliasTemplateDecl *OldDecl = 0; |
| 7206 | TemplateParameterList *OldTemplateParams = 0; |
| 7207 | |
| 7208 | if (TemplateParamLists.size() != 1) { |
| 7209 | Diag(UsingLoc, diag::err_alias_template_extra_headers) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7210 | << SourceRange(TemplateParamLists[1]->getTemplateLoc(), |
| 7211 | TemplateParamLists[TemplateParamLists.size()-1]->getRAngleLoc()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7212 | } |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7213 | TemplateParameterList *TemplateParams = TemplateParamLists[0]; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7214 | |
| 7215 | // Only consider previous declarations in the same scope. |
| 7216 | FilterLookupForScope(Previous, CurContext, S, /*ConsiderLinkage*/false, |
| 7217 | /*ExplicitInstantiationOrSpecialization*/false); |
| 7218 | if (!Previous.empty()) { |
| 7219 | Redeclaration = true; |
| 7220 | |
| 7221 | OldDecl = Previous.getAsSingle<TypeAliasTemplateDecl>(); |
| 7222 | if (!OldDecl && !Invalid) { |
| 7223 | Diag(UsingLoc, diag::err_redefinition_different_kind) |
| 7224 | << Name.Identifier; |
| 7225 | |
| 7226 | NamedDecl *OldD = Previous.getRepresentativeDecl(); |
| 7227 | if (OldD->getLocation().isValid()) |
| 7228 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| 7229 | |
| 7230 | Invalid = true; |
| 7231 | } |
| 7232 | |
| 7233 | if (!Invalid && OldDecl && !OldDecl->isInvalidDecl()) { |
| 7234 | if (TemplateParameterListsAreEqual(TemplateParams, |
| 7235 | OldDecl->getTemplateParameters(), |
| 7236 | /*Complain=*/true, |
| 7237 | TPL_TemplateMatch)) |
| 7238 | OldTemplateParams = OldDecl->getTemplateParameters(); |
| 7239 | else |
| 7240 | Invalid = true; |
| 7241 | |
| 7242 | TypeAliasDecl *OldTD = OldDecl->getTemplatedDecl(); |
| 7243 | if (!Invalid && |
| 7244 | !Context.hasSameType(OldTD->getUnderlyingType(), |
| 7245 | NewTD->getUnderlyingType())) { |
| 7246 | // FIXME: The C++0x standard does not clearly say this is ill-formed, |
| 7247 | // but we can't reasonably accept it. |
| 7248 | Diag(NewTD->getLocation(), diag::err_redefinition_different_typedef) |
| 7249 | << 2 << NewTD->getUnderlyingType() << OldTD->getUnderlyingType(); |
| 7250 | if (OldTD->getLocation().isValid()) |
| 7251 | Diag(OldTD->getLocation(), diag::note_previous_definition); |
| 7252 | Invalid = true; |
| 7253 | } |
| 7254 | } |
| 7255 | } |
| 7256 | |
| 7257 | // Merge any previous default template arguments into our parameters, |
| 7258 | // and check the parameter list. |
| 7259 | if (CheckTemplateParameterList(TemplateParams, OldTemplateParams, |
| 7260 | TPC_TypeAliasTemplate)) |
| 7261 | return 0; |
| 7262 | |
| 7263 | TypeAliasTemplateDecl *NewDecl = |
| 7264 | TypeAliasTemplateDecl::Create(Context, CurContext, UsingLoc, |
| 7265 | Name.Identifier, TemplateParams, |
| 7266 | NewTD); |
| 7267 | |
| 7268 | NewDecl->setAccess(AS); |
| 7269 | |
| 7270 | if (Invalid) |
| 7271 | NewDecl->setInvalidDecl(); |
| 7272 | else if (OldDecl) |
| 7273 | NewDecl->setPreviousDeclaration(OldDecl); |
| 7274 | |
| 7275 | NewND = NewDecl; |
| 7276 | } else { |
| 7277 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 7278 | NewND = NewTD; |
| 7279 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7280 | |
| 7281 | if (!Redeclaration) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7282 | PushOnScopeChains(NewND, S); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7283 | |
Dmitri Gribenko | c27bc80 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 7284 | ActOnDocumentableDecl(NewND); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7285 | return NewND; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7286 | } |
| 7287 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7288 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 7289 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 7290 | SourceLocation AliasLoc, |
| 7291 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7292 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 7293 | SourceLocation IdentLoc, |
| 7294 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7295 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7296 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7297 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 7298 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7299 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7300 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 7301 | NamedDecl *PrevDecl |
| 7302 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 7303 | ForRedeclaration); |
| 7304 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 7305 | PrevDecl = 0; |
| 7306 | |
| 7307 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7308 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7309 | // We already have an alias with the same name that points to the same |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7310 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 7311 | // FIXME: At some point, we'll want to create the (redundant) |
| 7312 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7313 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 7314 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7315 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7316 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7317 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7318 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 7319 | diag::err_redefinition_different_kind; |
| 7320 | Diag(AliasLoc, DiagID) << Alias; |
| 7321 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7322 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7323 | } |
| 7324 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7325 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7326 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7327 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7328 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 7329 | if (!TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, Ident)) { |
Richard Smith | bf9658c | 2012-04-05 23:13:23 +0000 | [diff] [blame] | 7330 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7331 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 7332 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 7333 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7334 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 7335 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7336 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 7337 | Alias, SS.getWithLocInContext(Context), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7338 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7339 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 7340 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7341 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 7342 | } |
| 7343 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 7344 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7345 | Sema::ComputeDefaultedDefaultCtorExceptionSpec(SourceLocation Loc, |
| 7346 | CXXMethodDecl *MD) { |
| 7347 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 7348 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7349 | // C++ [except.spec]p14: |
| 7350 | // An implicitly declared special member function (Clause 12) shall have an |
| 7351 | // exception-specification. [...] |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7352 | ImplicitExceptionSpecification ExceptSpec(*this); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 7353 | if (ClassDecl->isInvalidDecl()) |
| 7354 | return ExceptSpec; |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7355 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7356 | // Direct base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7357 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 7358 | BEnd = ClassDecl->bases_end(); |
| 7359 | B != BEnd; ++B) { |
| 7360 | if (B->isVirtual()) // Handled below. |
| 7361 | continue; |
| 7362 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7363 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 7364 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 7365 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 7366 | // If this is a deleted function, add it anyway. This might be conformant |
| 7367 | // with the standard. This might not. I'm not sure. It might not matter. |
| 7368 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7369 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7370 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7371 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7372 | |
| 7373 | // Virtual base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7374 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 7375 | BEnd = ClassDecl->vbases_end(); |
| 7376 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7377 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 7378 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 7379 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 7380 | // If this is a deleted function, add it anyway. This might be conformant |
| 7381 | // with the standard. This might not. I'm not sure. It might not matter. |
| 7382 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7383 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7384 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7385 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7386 | |
| 7387 | // Field constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7388 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 7389 | FEnd = ClassDecl->field_end(); |
| 7390 | F != FEnd; ++F) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7391 | if (F->hasInClassInitializer()) { |
| 7392 | if (Expr *E = F->getInClassInitializer()) |
| 7393 | ExceptSpec.CalledExpr(E); |
| 7394 | else if (!F->isInvalidDecl()) |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7395 | // DR1351: |
| 7396 | // If the brace-or-equal-initializer of a non-static data member |
| 7397 | // invokes a defaulted default constructor of its class or of an |
| 7398 | // enclosing class in a potentially evaluated subexpression, the |
| 7399 | // program is ill-formed. |
| 7400 | // |
| 7401 | // This resolution is unworkable: the exception specification of the |
| 7402 | // default constructor can be needed in an unevaluated context, in |
| 7403 | // particular, in the operand of a noexcept-expression, and we can be |
| 7404 | // unable to compute an exception specification for an enclosed class. |
| 7405 | // |
| 7406 | // We do not allow an in-class initializer to require the evaluation |
| 7407 | // of the exception specification for any in-class initializer whose |
| 7408 | // definition is not lexically complete. |
| 7409 | Diag(Loc, diag::err_in_class_initializer_references_def_ctor) << MD; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7410 | } else if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7411 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 7412 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 7413 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 7414 | // If this is a deleted function, add it anyway. This might be conformant |
| 7415 | // with the standard. This might not. I'm not sure. It might not matter. |
| 7416 | // In particular, the problem is that this function never gets called. It |
| 7417 | // might just be ill-formed because this function attempts to refer to |
| 7418 | // a deleted function here. |
| 7419 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7420 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7421 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7422 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7423 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 7424 | return ExceptSpec; |
| 7425 | } |
| 7426 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 7427 | namespace { |
| 7428 | /// RAII object to register a special member as being currently declared. |
| 7429 | struct DeclaringSpecialMember { |
| 7430 | Sema &S; |
| 7431 | Sema::SpecialMemberDecl D; |
| 7432 | bool WasAlreadyBeingDeclared; |
| 7433 | |
| 7434 | DeclaringSpecialMember(Sema &S, CXXRecordDecl *RD, Sema::CXXSpecialMember CSM) |
| 7435 | : S(S), D(RD, CSM) { |
| 7436 | WasAlreadyBeingDeclared = !S.SpecialMembersBeingDeclared.insert(D); |
| 7437 | if (WasAlreadyBeingDeclared) |
| 7438 | // This almost never happens, but if it does, ensure that our cache |
| 7439 | // doesn't contain a stale result. |
| 7440 | S.SpecialMemberCache.clear(); |
| 7441 | |
| 7442 | // FIXME: Register a note to be produced if we encounter an error while |
| 7443 | // declaring the special member. |
| 7444 | } |
| 7445 | ~DeclaringSpecialMember() { |
| 7446 | if (!WasAlreadyBeingDeclared) |
| 7447 | S.SpecialMembersBeingDeclared.erase(D); |
| 7448 | } |
| 7449 | |
| 7450 | /// \brief Are we already trying to declare this special member? |
| 7451 | bool isAlreadyBeingDeclared() const { |
| 7452 | return WasAlreadyBeingDeclared; |
| 7453 | } |
| 7454 | }; |
| 7455 | } |
| 7456 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 7457 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 7458 | CXXRecordDecl *ClassDecl) { |
| 7459 | // C++ [class.ctor]p5: |
| 7460 | // A default constructor for a class X is a constructor of class X |
| 7461 | // that can be called without an argument. If there is no |
| 7462 | // user-declared constructor for class X, a default constructor is |
| 7463 | // implicitly declared. An implicitly-declared default constructor |
| 7464 | // is an inline public member of its class. |
Richard Smith | d0adeb6 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 7465 | assert(ClassDecl->needsImplicitDefaultConstructor() && |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 7466 | "Should not build implicit default constructor!"); |
| 7467 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 7468 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDefaultConstructor); |
| 7469 | if (DSM.isAlreadyBeingDeclared()) |
| 7470 | return 0; |
| 7471 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 7472 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 7473 | CXXDefaultConstructor, |
| 7474 | false); |
| 7475 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7476 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7477 | CanQualType ClassType |
| 7478 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7479 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7480 | DeclarationName Name |
| 7481 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7482 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 7483 | CXXConstructorDecl *DefaultCon = CXXConstructorDecl::Create( |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7484 | Context, ClassDecl, ClassLoc, NameInfo, /*Type*/QualType(), /*TInfo=*/0, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 7485 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 7486 | Constexpr); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7487 | DefaultCon->setAccess(AS_public); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 7488 | DefaultCon->setDefaulted(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7489 | DefaultCon->setImplicit(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7490 | |
| 7491 | // Build an exception specification pointing back at this constructor. |
| 7492 | FunctionProtoType::ExtProtoInfo EPI; |
| 7493 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 7494 | EPI.ExceptionSpecDecl = DefaultCon; |
| 7495 | DefaultCon->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 7496 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 7497 | // We don't need to use SpecialMemberIsTrivial here; triviality for default |
| 7498 | // constructors is easy to compute. |
| 7499 | DefaultCon->setTrivial(ClassDecl->hasTrivialDefaultConstructor()); |
| 7500 | |
| 7501 | if (ShouldDeleteSpecialMember(DefaultCon, CXXDefaultConstructor)) |
| 7502 | DefaultCon->setDeletedAsWritten(); |
| 7503 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7504 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7505 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 7506 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 7507 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7508 | PushOnScopeChains(DefaultCon, S, false); |
| 7509 | ClassDecl->addDecl(DefaultCon); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 7510 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7511 | return DefaultCon; |
| 7512 | } |
| 7513 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 7514 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 7515 | CXXConstructorDecl *Constructor) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 7516 | assert((Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 7517 | !Constructor->doesThisDeclarationHaveABody() && |
| 7518 | !Constructor->isDeleted()) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 7519 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7520 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 7521 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 7522 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 7523 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 7524 | SynthesizedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 7525 | DiagnosticErrorTrap Trap(Diags); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 7526 | if (SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7527 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 7528 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 7529 | << CXXDefaultConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 7530 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7531 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 7532 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7533 | |
| 7534 | SourceLocation Loc = Constructor->getLocation(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 7535 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7536 | |
| 7537 | Constructor->setUsed(); |
| 7538 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7539 | |
| 7540 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7541 | L->CompletedImplicitDefinition(Constructor); |
| 7542 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 7543 | } |
| 7544 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7545 | void Sema::ActOnFinishDelayedMemberInitializers(Decl *D) { |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 7546 | // Check that any explicitly-defaulted methods have exception specifications |
| 7547 | // compatible with their implicit exception specifications. |
| 7548 | CheckDelayedExplicitlyDefaultedMemberExceptionSpecs(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7549 | } |
| 7550 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7551 | void Sema::DeclareInheritedConstructors(CXXRecordDecl *ClassDecl) { |
| 7552 | // We start with an initial pass over the base classes to collect those that |
| 7553 | // inherit constructors from. If there are none, we can forgo all further |
| 7554 | // processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7555 | typedef SmallVector<const RecordType *, 4> BasesVector; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7556 | BasesVector BasesToInheritFrom; |
| 7557 | for (CXXRecordDecl::base_class_iterator BaseIt = ClassDecl->bases_begin(), |
| 7558 | BaseE = ClassDecl->bases_end(); |
| 7559 | BaseIt != BaseE; ++BaseIt) { |
| 7560 | if (BaseIt->getInheritConstructors()) { |
| 7561 | QualType Base = BaseIt->getType(); |
| 7562 | if (Base->isDependentType()) { |
| 7563 | // If we inherit constructors from anything that is dependent, just |
| 7564 | // abort processing altogether. We'll get another chance for the |
| 7565 | // instantiations. |
| 7566 | return; |
| 7567 | } |
| 7568 | BasesToInheritFrom.push_back(Base->castAs<RecordType>()); |
| 7569 | } |
| 7570 | } |
| 7571 | if (BasesToInheritFrom.empty()) |
| 7572 | return; |
| 7573 | |
| 7574 | // Now collect the constructors that we already have in the current class. |
| 7575 | // Those take precedence over inherited constructors. |
| 7576 | // C++0x [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 7577 | // unless there is a user-declared constructor with the same signature in |
| 7578 | // the class where the using-declaration appears. |
| 7579 | llvm::SmallSet<const Type *, 8> ExistingConstructors; |
| 7580 | for (CXXRecordDecl::ctor_iterator CtorIt = ClassDecl->ctor_begin(), |
| 7581 | CtorE = ClassDecl->ctor_end(); |
| 7582 | CtorIt != CtorE; ++CtorIt) { |
| 7583 | ExistingConstructors.insert( |
| 7584 | Context.getCanonicalType(CtorIt->getType()).getTypePtr()); |
| 7585 | } |
| 7586 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7587 | DeclarationName CreatedCtorName = |
| 7588 | Context.DeclarationNames.getCXXConstructorName( |
| 7589 | ClassDecl->getTypeForDecl()->getCanonicalTypeUnqualified()); |
| 7590 | |
| 7591 | // Now comes the true work. |
| 7592 | // First, we keep a map from constructor types to the base that introduced |
| 7593 | // them. Needed for finding conflicting constructors. We also keep the |
| 7594 | // actually inserted declarations in there, for pretty diagnostics. |
| 7595 | typedef std::pair<CanQualType, CXXConstructorDecl *> ConstructorInfo; |
| 7596 | typedef llvm::DenseMap<const Type *, ConstructorInfo> ConstructorToSourceMap; |
| 7597 | ConstructorToSourceMap InheritedConstructors; |
| 7598 | for (BasesVector::iterator BaseIt = BasesToInheritFrom.begin(), |
| 7599 | BaseE = BasesToInheritFrom.end(); |
| 7600 | BaseIt != BaseE; ++BaseIt) { |
| 7601 | const RecordType *Base = *BaseIt; |
| 7602 | CanQualType CanonicalBase = Base->getCanonicalTypeUnqualified(); |
| 7603 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(Base->getDecl()); |
| 7604 | for (CXXRecordDecl::ctor_iterator CtorIt = BaseDecl->ctor_begin(), |
| 7605 | CtorE = BaseDecl->ctor_end(); |
| 7606 | CtorIt != CtorE; ++CtorIt) { |
| 7607 | // Find the using declaration for inheriting this base's constructors. |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7608 | // FIXME: Don't perform name lookup just to obtain a source location! |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7609 | DeclarationName Name = |
| 7610 | Context.DeclarationNames.getCXXConstructorName(CanonicalBase); |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7611 | LookupResult Result(*this, Name, SourceLocation(), LookupUsingDeclName); |
| 7612 | LookupQualifiedName(Result, CurContext); |
| 7613 | UsingDecl *UD = Result.getAsSingle<UsingDecl>(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7614 | SourceLocation UsingLoc = UD ? UD->getLocation() : |
| 7615 | ClassDecl->getLocation(); |
| 7616 | |
| 7617 | // C++0x [class.inhctor]p1: The candidate set of inherited constructors |
| 7618 | // from the class X named in the using-declaration consists of actual |
| 7619 | // constructors and notional constructors that result from the |
| 7620 | // transformation of defaulted parameters as follows: |
| 7621 | // - all non-template default constructors of X, and |
| 7622 | // - for each non-template constructor of X that has at least one |
| 7623 | // parameter with a default argument, the set of constructors that |
| 7624 | // results from omitting any ellipsis parameter specification and |
| 7625 | // successively omitting parameters with a default argument from the |
| 7626 | // end of the parameter-type-list. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 7627 | CXXConstructorDecl *BaseCtor = *CtorIt; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7628 | bool CanBeCopyOrMove = BaseCtor->isCopyOrMoveConstructor(); |
| 7629 | const FunctionProtoType *BaseCtorType = |
| 7630 | BaseCtor->getType()->getAs<FunctionProtoType>(); |
| 7631 | |
| 7632 | for (unsigned params = BaseCtor->getMinRequiredArguments(), |
| 7633 | maxParams = BaseCtor->getNumParams(); |
| 7634 | params <= maxParams; ++params) { |
| 7635 | // Skip default constructors. They're never inherited. |
| 7636 | if (params == 0) |
| 7637 | continue; |
| 7638 | // Skip copy and move constructors for the same reason. |
| 7639 | if (CanBeCopyOrMove && params == 1) |
| 7640 | continue; |
| 7641 | |
| 7642 | // Build up a function type for this particular constructor. |
| 7643 | // FIXME: The working paper does not consider that the exception spec |
| 7644 | // for the inheriting constructor might be larger than that of the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7645 | // source. This code doesn't yet, either. When it does, this code will |
| 7646 | // need to be delayed until after exception specifications and in-class |
| 7647 | // member initializers are attached. |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7648 | const Type *NewCtorType; |
| 7649 | if (params == maxParams) |
| 7650 | NewCtorType = BaseCtorType; |
| 7651 | else { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7652 | SmallVector<QualType, 16> Args; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7653 | for (unsigned i = 0; i < params; ++i) { |
| 7654 | Args.push_back(BaseCtorType->getArgType(i)); |
| 7655 | } |
| 7656 | FunctionProtoType::ExtProtoInfo ExtInfo = |
| 7657 | BaseCtorType->getExtProtoInfo(); |
| 7658 | ExtInfo.Variadic = false; |
| 7659 | NewCtorType = Context.getFunctionType(BaseCtorType->getResultType(), |
| 7660 | Args.data(), params, ExtInfo) |
| 7661 | .getTypePtr(); |
| 7662 | } |
| 7663 | const Type *CanonicalNewCtorType = |
| 7664 | Context.getCanonicalType(NewCtorType); |
| 7665 | |
| 7666 | // Now that we have the type, first check if the class already has a |
| 7667 | // constructor with this signature. |
| 7668 | if (ExistingConstructors.count(CanonicalNewCtorType)) |
| 7669 | continue; |
| 7670 | |
| 7671 | // Then we check if we have already declared an inherited constructor |
| 7672 | // with this signature. |
| 7673 | std::pair<ConstructorToSourceMap::iterator, bool> result = |
| 7674 | InheritedConstructors.insert(std::make_pair( |
| 7675 | CanonicalNewCtorType, |
| 7676 | std::make_pair(CanonicalBase, (CXXConstructorDecl*)0))); |
| 7677 | if (!result.second) { |
| 7678 | // Already in the map. If it came from a different class, that's an |
| 7679 | // error. Not if it's from the same. |
| 7680 | CanQualType PreviousBase = result.first->second.first; |
| 7681 | if (CanonicalBase != PreviousBase) { |
| 7682 | const CXXConstructorDecl *PrevCtor = result.first->second.second; |
| 7683 | const CXXConstructorDecl *PrevBaseCtor = |
| 7684 | PrevCtor->getInheritedConstructor(); |
| 7685 | assert(PrevBaseCtor && "Conflicting constructor was not inherited"); |
| 7686 | |
| 7687 | Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 7688 | Diag(BaseCtor->getLocation(), |
| 7689 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 7690 | Diag(PrevBaseCtor->getLocation(), |
| 7691 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 7692 | Diag(PrevCtor->getLocation(), |
| 7693 | diag::note_using_decl_constructor_conflict_previous_using); |
| 7694 | } |
| 7695 | continue; |
| 7696 | } |
| 7697 | |
| 7698 | // OK, we're there, now add the constructor. |
| 7699 | // C++0x [class.inhctor]p8: [...] that would be performed by a |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7700 | // user-written inline constructor [...] |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7701 | DeclarationNameInfo DNI(CreatedCtorName, UsingLoc); |
| 7702 | CXXConstructorDecl *NewCtor = CXXConstructorDecl::Create( |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7703 | Context, ClassDecl, UsingLoc, DNI, QualType(NewCtorType, 0), |
| 7704 | /*TInfo=*/0, BaseCtor->isExplicit(), /*Inline=*/true, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7705 | /*ImplicitlyDeclared=*/true, |
| 7706 | // FIXME: Due to a defect in the standard, we treat inherited |
| 7707 | // constructors as constexpr even if that makes them ill-formed. |
| 7708 | /*Constexpr=*/BaseCtor->isConstexpr()); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7709 | NewCtor->setAccess(BaseCtor->getAccess()); |
| 7710 | |
| 7711 | // Build up the parameter decls and add them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7712 | SmallVector<ParmVarDecl *, 16> ParamDecls; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7713 | for (unsigned i = 0; i < params; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7714 | ParamDecls.push_back(ParmVarDecl::Create(Context, NewCtor, |
| 7715 | UsingLoc, UsingLoc, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7716 | /*IdentifierInfo=*/0, |
| 7717 | BaseCtorType->getArgType(i), |
| 7718 | /*TInfo=*/0, SC_None, |
| 7719 | SC_None, /*DefaultArg=*/0)); |
| 7720 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 7721 | NewCtor->setParams(ParamDecls); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7722 | NewCtor->setInheritedConstructor(BaseCtor); |
| 7723 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7724 | ClassDecl->addDecl(NewCtor); |
| 7725 | result.first->second.second = NewCtor; |
| 7726 | } |
| 7727 | } |
| 7728 | } |
| 7729 | } |
| 7730 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7731 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7732 | Sema::ComputeDefaultedDtorExceptionSpec(CXXMethodDecl *MD) { |
| 7733 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 7734 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7735 | // C++ [except.spec]p14: |
| 7736 | // An implicitly declared special member function (Clause 12) shall have |
| 7737 | // an exception-specification. |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7738 | ImplicitExceptionSpecification ExceptSpec(*this); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 7739 | if (ClassDecl->isInvalidDecl()) |
| 7740 | return ExceptSpec; |
| 7741 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7742 | // Direct base-class destructors. |
| 7743 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 7744 | BEnd = ClassDecl->bases_end(); |
| 7745 | B != BEnd; ++B) { |
| 7746 | if (B->isVirtual()) // Handled below. |
| 7747 | continue; |
| 7748 | |
| 7749 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7750 | ExceptSpec.CalledDecl(B->getLocStart(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7751 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7752 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7753 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7754 | // Virtual base-class destructors. |
| 7755 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 7756 | BEnd = ClassDecl->vbases_end(); |
| 7757 | B != BEnd; ++B) { |
| 7758 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7759 | ExceptSpec.CalledDecl(B->getLocStart(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7760 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7761 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7762 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7763 | // Field destructors. |
| 7764 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 7765 | FEnd = ClassDecl->field_end(); |
| 7766 | F != FEnd; ++F) { |
| 7767 | if (const RecordType *RecordTy |
| 7768 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7769 | ExceptSpec.CalledDecl(F->getLocation(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7770 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7771 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7772 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7773 | return ExceptSpec; |
| 7774 | } |
| 7775 | |
| 7776 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
| 7777 | // C++ [class.dtor]p2: |
| 7778 | // If a class has no user-declared destructor, a destructor is |
| 7779 | // declared implicitly. An implicitly-declared destructor is an |
| 7780 | // inline public member of its class. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 7781 | assert(ClassDecl->needsImplicitDestructor()); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7782 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 7783 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDestructor); |
| 7784 | if (DSM.isAlreadyBeingDeclared()) |
| 7785 | return 0; |
| 7786 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7787 | // Create the actual destructor declaration. |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7788 | CanQualType ClassType |
| 7789 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7790 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7791 | DeclarationName Name |
| 7792 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7793 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7794 | CXXDestructorDecl *Destructor |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7795 | = CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
| 7796 | QualType(), 0, /*isInline=*/true, |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7797 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7798 | Destructor->setAccess(AS_public); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7799 | Destructor->setDefaulted(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7800 | Destructor->setImplicit(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7801 | |
| 7802 | // Build an exception specification pointing back at this destructor. |
| 7803 | FunctionProtoType::ExtProtoInfo EPI; |
| 7804 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 7805 | EPI.ExceptionSpecDecl = Destructor; |
| 7806 | Destructor->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 7807 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 7808 | AddOverriddenMethods(ClassDecl, Destructor); |
| 7809 | |
| 7810 | // We don't need to use SpecialMemberIsTrivial here; triviality for |
| 7811 | // destructors is easy to compute. |
| 7812 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
| 7813 | |
| 7814 | if (ShouldDeleteSpecialMember(Destructor, CXXDestructor)) |
| 7815 | Destructor->setDeletedAsWritten(); |
| 7816 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7817 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7818 | ++ASTContext::NumImplicitDestructorsDeclared; |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7819 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7820 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 7821 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7822 | PushOnScopeChains(Destructor, S, false); |
| 7823 | ClassDecl->addDecl(Destructor); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7824 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7825 | return Destructor; |
| 7826 | } |
| 7827 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7828 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 7829 | CXXDestructorDecl *Destructor) { |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 7830 | assert((Destructor->isDefaulted() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 7831 | !Destructor->doesThisDeclarationHaveABody() && |
| 7832 | !Destructor->isDeleted()) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7833 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7834 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7835 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7836 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7837 | if (Destructor->isInvalidDecl()) |
| 7838 | return; |
| 7839 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 7840 | SynthesizedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7841 | |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 7842 | DiagnosticErrorTrap Trap(Diags); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 7843 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 7844 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7845 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7846 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 7847 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7848 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 7849 | |
| 7850 | Destructor->setInvalidDecl(); |
| 7851 | return; |
| 7852 | } |
| 7853 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7854 | SourceLocation Loc = Destructor->getLocation(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 7855 | Destructor->setBody(new (Context) CompoundStmt(Loc)); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame] | 7856 | Destructor->setImplicitlyDefined(true); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7857 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7858 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7859 | |
| 7860 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7861 | L->CompletedImplicitDefinition(Destructor); |
| 7862 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7863 | } |
| 7864 | |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 7865 | /// \brief Perform any semantic analysis which needs to be delayed until all |
| 7866 | /// pending class member declarations have been parsed. |
| 7867 | void Sema::ActOnFinishCXXMemberDecls() { |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 7868 | // Perform any deferred checking of exception specifications for virtual |
| 7869 | // destructors. |
| 7870 | for (unsigned i = 0, e = DelayedDestructorExceptionSpecChecks.size(); |
| 7871 | i != e; ++i) { |
| 7872 | const CXXDestructorDecl *Dtor = |
| 7873 | DelayedDestructorExceptionSpecChecks[i].first; |
| 7874 | assert(!Dtor->getParent()->isDependentType() && |
| 7875 | "Should not ever add destructors of templates into the list."); |
| 7876 | CheckOverridingFunctionExceptionSpec(Dtor, |
| 7877 | DelayedDestructorExceptionSpecChecks[i].second); |
| 7878 | } |
| 7879 | DelayedDestructorExceptionSpecChecks.clear(); |
| 7880 | } |
| 7881 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7882 | void Sema::AdjustDestructorExceptionSpec(CXXRecordDecl *ClassDecl, |
| 7883 | CXXDestructorDecl *Destructor) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7884 | assert(getLangOpts().CPlusPlus11 && |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7885 | "adjusting dtor exception specs was introduced in c++11"); |
| 7886 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7887 | // C++11 [class.dtor]p3: |
| 7888 | // A declaration of a destructor that does not have an exception- |
| 7889 | // specification is implicitly considered to have the same exception- |
| 7890 | // specification as an implicit declaration. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7891 | const FunctionProtoType *DtorType = Destructor->getType()-> |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7892 | getAs<FunctionProtoType>(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7893 | if (DtorType->hasExceptionSpec()) |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7894 | return; |
| 7895 | |
Chandler Carruth | 3f224b2 | 2011-09-20 04:55:26 +0000 | [diff] [blame] | 7896 | // Replace the destructor's type, building off the existing one. Fortunately, |
| 7897 | // the only thing of interest in the destructor type is its extended info. |
| 7898 | // The return and arguments are fixed. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7899 | FunctionProtoType::ExtProtoInfo EPI = DtorType->getExtProtoInfo(); |
| 7900 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 7901 | EPI.ExceptionSpecDecl = Destructor; |
| 7902 | Destructor->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 7903 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7904 | // FIXME: If the destructor has a body that could throw, and the newly created |
| 7905 | // spec doesn't allow exceptions, we should emit a warning, because this |
| 7906 | // change in behavior can break conforming C++03 programs at runtime. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7907 | // However, we don't have a body or an exception specification yet, so it |
| 7908 | // needs to be done somewhere else. |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7909 | } |
| 7910 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7911 | /// When generating a defaulted copy or move assignment operator, if a field |
| 7912 | /// should be copied with __builtin_memcpy rather than via explicit assignments, |
| 7913 | /// do so. This optimization only applies for arrays of scalars, and for arrays |
| 7914 | /// of class type where the selected copy/move-assignment operator is trivial. |
| 7915 | static StmtResult |
| 7916 | buildMemcpyForAssignmentOp(Sema &S, SourceLocation Loc, QualType T, |
| 7917 | Expr *To, Expr *From) { |
| 7918 | // Compute the size of the memory buffer to be copied. |
| 7919 | QualType SizeType = S.Context.getSizeType(); |
| 7920 | llvm::APInt Size(S.Context.getTypeSize(SizeType), |
| 7921 | S.Context.getTypeSizeInChars(T).getQuantity()); |
| 7922 | |
| 7923 | // Take the address of the field references for "from" and "to". We |
| 7924 | // directly construct UnaryOperators here because semantic analysis |
| 7925 | // does not permit us to take the address of an xvalue. |
| 7926 | From = new (S.Context) UnaryOperator(From, UO_AddrOf, |
| 7927 | S.Context.getPointerType(From->getType()), |
| 7928 | VK_RValue, OK_Ordinary, Loc); |
| 7929 | To = new (S.Context) UnaryOperator(To, UO_AddrOf, |
| 7930 | S.Context.getPointerType(To->getType()), |
| 7931 | VK_RValue, OK_Ordinary, Loc); |
| 7932 | |
| 7933 | const Type *E = T->getBaseElementTypeUnsafe(); |
| 7934 | bool NeedsCollectableMemCpy = |
| 7935 | E->isRecordType() && E->getAs<RecordType>()->getDecl()->hasObjectMember(); |
| 7936 | |
| 7937 | // Create a reference to the __builtin_objc_memmove_collectable function |
| 7938 | StringRef MemCpyName = NeedsCollectableMemCpy ? |
| 7939 | "__builtin_objc_memmove_collectable" : |
| 7940 | "__builtin_memcpy"; |
| 7941 | LookupResult R(S, &S.Context.Idents.get(MemCpyName), Loc, |
| 7942 | Sema::LookupOrdinaryName); |
| 7943 | S.LookupName(R, S.TUScope, true); |
| 7944 | |
| 7945 | FunctionDecl *MemCpy = R.getAsSingle<FunctionDecl>(); |
| 7946 | if (!MemCpy) |
| 7947 | // Something went horribly wrong earlier, and we will have complained |
| 7948 | // about it. |
| 7949 | return StmtError(); |
| 7950 | |
| 7951 | ExprResult MemCpyRef = S.BuildDeclRefExpr(MemCpy, S.Context.BuiltinFnTy, |
| 7952 | VK_RValue, Loc, 0); |
| 7953 | assert(MemCpyRef.isUsable() && "Builtin reference cannot fail"); |
| 7954 | |
| 7955 | Expr *CallArgs[] = { |
| 7956 | To, From, IntegerLiteral::Create(S.Context, Size, SizeType, Loc) |
| 7957 | }; |
| 7958 | ExprResult Call = S.ActOnCallExpr(/*Scope=*/0, MemCpyRef.take(), |
| 7959 | Loc, CallArgs, Loc); |
| 7960 | |
| 7961 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 7962 | return S.Owned(Call.takeAs<Stmt>()); |
| 7963 | } |
| 7964 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7965 | /// \brief Builds a statement that copies/moves the given entity from \p From to |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7966 | /// \c To. |
| 7967 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7968 | /// This routine is used to copy/move the members of a class with an |
| 7969 | /// implicitly-declared copy/move assignment operator. When the entities being |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7970 | /// copied are arrays, this routine builds for loops to copy them. |
| 7971 | /// |
| 7972 | /// \param S The Sema object used for type-checking. |
| 7973 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7974 | /// \param Loc The location where the implicit copy/move is being generated. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7975 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7976 | /// \param T The type of the expressions being copied/moved. Both expressions |
| 7977 | /// must have this type. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7978 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7979 | /// \param To The expression we are copying/moving to. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7980 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7981 | /// \param From The expression we are copying/moving from. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7982 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7983 | /// \param CopyingBaseSubobject Whether we're copying/moving a base subobject. |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 7984 | /// Otherwise, it's a non-static member subobject. |
| 7985 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7986 | /// \param Copying Whether we're copying or moving. |
| 7987 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7988 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 7989 | /// |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7990 | /// \returns A statement or a loop that copies the expressions, or StmtResult(0) |
| 7991 | /// if a memcpy should be used instead. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7992 | static StmtResult |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7993 | buildSingleCopyAssignRecursively(Sema &S, SourceLocation Loc, QualType T, |
| 7994 | Expr *To, Expr *From, |
| 7995 | bool CopyingBaseSubobject, bool Copying, |
| 7996 | unsigned Depth = 0) { |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7997 | // C++11 [class.copy]p28: |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7998 | // Each subobject is assigned in the manner appropriate to its type: |
| 7999 | // |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8000 | // - if the subobject is of class type, as if by a call to operator= with |
| 8001 | // the subobject as the object expression and the corresponding |
| 8002 | // subobject of x as a single function argument (as if by explicit |
| 8003 | // qualification; that is, ignoring any possible virtual overriding |
| 8004 | // functions in more derived classes); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8005 | // |
| 8006 | // C++03 [class.copy]p13: |
| 8007 | // - if the subobject is of class type, the copy assignment operator for |
| 8008 | // the class is used (as if by explicit qualification; that is, |
| 8009 | // ignoring any possible virtual overriding functions in more derived |
| 8010 | // classes); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8011 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 8012 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8013 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8014 | // Look for operator=. |
| 8015 | DeclarationName Name |
| 8016 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 8017 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 8018 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8019 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8020 | // Prior to C++11, filter out any result that isn't a copy/move-assignment |
| 8021 | // operator. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8022 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8023 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 8024 | while (F.hasNext()) { |
| 8025 | NamedDecl *D = F.next(); |
| 8026 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 8027 | if (Method->isCopyAssignmentOperator() || |
| 8028 | (!Copying && Method->isMoveAssignmentOperator())) |
| 8029 | continue; |
| 8030 | |
| 8031 | F.erase(); |
| 8032 | } |
| 8033 | F.done(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 8034 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8035 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 8036 | // Suppress the protected check (C++ [class.protected]) for each of the |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8037 | // assignment operators we found. This strange dance is required when |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 8038 | // we're assigning via a base classes's copy-assignment operator. To |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8039 | // ensure that we're getting the right base class subobject (without |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 8040 | // ambiguities), we need to cast "this" to that subobject type; to |
| 8041 | // ensure that we don't go through the virtual call mechanism, we need |
| 8042 | // to qualify the operator= name with the base class (see below). However, |
| 8043 | // this means that if the base class has a protected copy assignment |
| 8044 | // operator, the protected member access check will fail. So, we |
| 8045 | // rewrite "protected" access to "public" access in this case, since we |
| 8046 | // know by construction that we're calling from a derived class. |
| 8047 | if (CopyingBaseSubobject) { |
| 8048 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 8049 | L != LEnd; ++L) { |
| 8050 | if (L.getAccess() == AS_protected) |
| 8051 | L.setAccess(AS_public); |
| 8052 | } |
| 8053 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8054 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8055 | // Create the nested-name-specifier that will be used to qualify the |
| 8056 | // reference to operator=; this is required to suppress the virtual |
| 8057 | // call mechanism. |
| 8058 | CXXScopeSpec SS; |
Manuel Klimek | 5b6a3dd | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 8059 | const Type *CanonicalT = S.Context.getCanonicalType(T.getTypePtr()); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8060 | SS.MakeTrivial(S.Context, |
| 8061 | NestedNameSpecifier::Create(S.Context, 0, false, |
Manuel Klimek | 5b6a3dd | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 8062 | CanonicalT), |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 8063 | Loc); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8064 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8065 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8066 | ExprResult OpEqualRef |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8067 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8068 | /*TemplateKWLoc=*/SourceLocation(), |
| 8069 | /*FirstQualifierInScope=*/0, |
| 8070 | OpLookup, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8071 | /*TemplateArgs=*/0, |
| 8072 | /*SuppressQualifierCheck=*/true); |
| 8073 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8074 | return StmtError(); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8075 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8076 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8077 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8078 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 8079 | OpEqualRef.takeAs<Expr>(), |
| 8080 | Loc, &From, 1, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8081 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8082 | return StmtError(); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8083 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8084 | // If we built a call to a trivial 'operator=' while copying an array, |
| 8085 | // bail out. We'll replace the whole shebang with a memcpy. |
| 8086 | CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Call.get()); |
| 8087 | if (CE && CE->getMethodDecl()->isTrivial() && Depth) |
| 8088 | return StmtResult((Stmt*)0); |
| 8089 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8090 | // Convert to an expression-statement, and clean up any produced |
| 8091 | // temporaries. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8092 | return S.ActOnExprStmt(Call); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8093 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 8094 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8095 | // - if the subobject is of scalar type, the built-in assignment |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8096 | // operator is used. |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8097 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8098 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8099 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8100 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8101 | return StmtError(); |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8102 | return S.ActOnExprStmt(Assignment); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8103 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8104 | |
| 8105 | // - if the subobject is an array, each element is assigned, in the |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8106 | // manner appropriate to the element type; |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8107 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8108 | // Construct a loop over the array bounds, e.g., |
| 8109 | // |
| 8110 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 8111 | // |
| 8112 | // that will copy each of the array elements. |
| 8113 | QualType SizeType = S.Context.getSizeType(); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8114 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8115 | // Create the iteration variable. |
| 8116 | IdentifierInfo *IterationVarName = 0; |
| 8117 | { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8118 | SmallString<8> Str; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8119 | llvm::raw_svector_ostream OS(Str); |
| 8120 | OS << "__i" << Depth; |
| 8121 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 8122 | } |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8123 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8124 | IterationVarName, SizeType, |
| 8125 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8126 | SC_None, SC_None); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8127 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8128 | // Initialize the iteration variable to zero. |
| 8129 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 8130 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8131 | |
| 8132 | // Create a reference to the iteration variable; we'll use this several |
| 8133 | // times throughout. |
| 8134 | Expr *IterationVarRef |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 8135 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8136 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 8137 | Expr *IterationVarRefRVal = S.DefaultLvalueConversion(IterationVarRef).take(); |
| 8138 | assert(IterationVarRefRVal && "Conversion of invented variable cannot fail!"); |
| 8139 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8140 | // Create the DeclStmt that holds the iteration variable. |
| 8141 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8142 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8143 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8144 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 8145 | IterationVarRefRVal, |
| 8146 | Loc)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8147 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 8148 | IterationVarRefRVal, |
| 8149 | Loc)); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8150 | if (!Copying) // Cast to rvalue |
| 8151 | From = CastForMoving(S, From); |
| 8152 | |
| 8153 | // Build the copy/move for an individual element of the array. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8154 | StmtResult Copy = |
| 8155 | buildSingleCopyAssignRecursively(S, Loc, ArrayTy->getElementType(), |
| 8156 | To, From, CopyingBaseSubobject, |
| 8157 | Copying, Depth + 1); |
| 8158 | // Bail out if copying fails or if we determined that we should use memcpy. |
| 8159 | if (Copy.isInvalid() || !Copy.get()) |
| 8160 | return Copy; |
| 8161 | |
| 8162 | // Create the comparison against the array bound. |
| 8163 | llvm::APInt Upper |
| 8164 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
| 8165 | Expr *Comparison |
| 8166 | = new (S.Context) BinaryOperator(IterationVarRefRVal, |
| 8167 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 8168 | BO_NE, S.Context.BoolTy, |
| 8169 | VK_RValue, OK_Ordinary, Loc, false); |
| 8170 | |
| 8171 | // Create the pre-increment of the iteration variable. |
| 8172 | Expr *Increment |
| 8173 | = new (S.Context) UnaryOperator(IterationVarRef, UO_PreInc, SizeType, |
| 8174 | VK_LValue, OK_Ordinary, Loc); |
| 8175 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8176 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8177 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8178 | S.MakeFullExpr(Comparison), |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8179 | 0, S.MakeFullDiscardedValueExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8180 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8181 | } |
| 8182 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8183 | static StmtResult |
| 8184 | buildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
| 8185 | Expr *To, Expr *From, |
| 8186 | bool CopyingBaseSubobject, bool Copying) { |
| 8187 | // Maybe we should use a memcpy? |
| 8188 | if (T->isArrayType() && !T.isConstQualified() && !T.isVolatileQualified() && |
| 8189 | T.isTriviallyCopyableType(S.Context)) |
| 8190 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 8191 | |
| 8192 | StmtResult Result(buildSingleCopyAssignRecursively(S, Loc, T, To, From, |
| 8193 | CopyingBaseSubobject, |
| 8194 | Copying, 0)); |
| 8195 | |
| 8196 | // If we ended up picking a trivial assignment operator for an array of a |
| 8197 | // non-trivially-copyable class type, just emit a memcpy. |
| 8198 | if (!Result.isInvalid() && !Result.get()) |
| 8199 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 8200 | |
| 8201 | return Result; |
| 8202 | } |
| 8203 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8204 | Sema::ImplicitExceptionSpecification |
| 8205 | Sema::ComputeDefaultedCopyAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 8206 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 8207 | |
| 8208 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 8209 | if (ClassDecl->isInvalidDecl()) |
| 8210 | return ExceptSpec; |
| 8211 | |
| 8212 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
| 8213 | assert(T->getNumArgs() == 1 && "not a copy assignment op"); |
| 8214 | unsigned ArgQuals = T->getArgType(0).getNonReferenceType().getCVRQualifiers(); |
| 8215 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8216 | // C++ [except.spec]p14: |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8217 | // An implicitly declared special member function (Clause 12) shall have an |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8218 | // exception-specification. [...] |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8219 | |
| 8220 | // It is unspecified whether or not an implicit copy assignment operator |
| 8221 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 8222 | // made. As such, this exception specification is effectively unspecified. |
| 8223 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 8224 | // the side of assuming such calls to be made regardless of whether they |
| 8225 | // actually happen. |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8226 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8227 | BaseEnd = ClassDecl->bases_end(); |
| 8228 | Base != BaseEnd; ++Base) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8229 | if (Base->isVirtual()) |
| 8230 | continue; |
| 8231 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 8232 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8233 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8234 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 8235 | ArgQuals, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8236 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyAssign); |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8237 | } |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8238 | |
| 8239 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8240 | BaseEnd = ClassDecl->vbases_end(); |
| 8241 | Base != BaseEnd; ++Base) { |
| 8242 | CXXRecordDecl *BaseClassDecl |
| 8243 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 8244 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 8245 | ArgQuals, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8246 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyAssign); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8247 | } |
| 8248 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8249 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8250 | FieldEnd = ClassDecl->field_end(); |
| 8251 | Field != FieldEnd; |
| 8252 | ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 8253 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8254 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 8255 | if (CXXMethodDecl *CopyAssign = |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8256 | LookupCopyingAssignment(FieldClassDecl, |
| 8257 | ArgQuals | FieldType.getCVRQualifiers(), |
| 8258 | false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8259 | ExceptSpec.CalledDecl(Field->getLocation(), CopyAssign); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 8260 | } |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8261 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8262 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8263 | return ExceptSpec; |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 8264 | } |
| 8265 | |
| 8266 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
| 8267 | // Note: The following rules are largely analoguous to the copy |
| 8268 | // constructor rules. Note that virtual bases are not taken into account |
| 8269 | // for determining the argument type of the operator. Note also that |
| 8270 | // operators taking an object instead of a reference are allowed. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 8271 | assert(ClassDecl->needsImplicitCopyAssignment()); |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 8272 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8273 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyAssignment); |
| 8274 | if (DSM.isAlreadyBeingDeclared()) |
| 8275 | return 0; |
| 8276 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 8277 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 8278 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 8279 | if (ClassDecl->implicitCopyAssignmentHasConstParam()) |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 8280 | ArgType = ArgType.withConst(); |
| 8281 | ArgType = Context.getLValueReferenceType(ArgType); |
| 8282 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8283 | // An implicitly-declared copy assignment operator is an inline public |
| 8284 | // member of its class. |
| 8285 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8286 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 8287 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8288 | CXXMethodDecl *CopyAssignment |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8289 | = CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8290 | /*TInfo=*/0, /*isStatic=*/false, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8291 | /*StorageClassAsWritten=*/SC_None, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 8292 | /*isInline=*/true, /*isConstexpr=*/false, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 8293 | SourceLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8294 | CopyAssignment->setAccess(AS_public); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 8295 | CopyAssignment->setDefaulted(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8296 | CopyAssignment->setImplicit(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8297 | |
| 8298 | // Build an exception specification pointing back at this member. |
| 8299 | FunctionProtoType::ExtProtoInfo EPI; |
| 8300 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8301 | EPI.ExceptionSpecDecl = CopyAssignment; |
| 8302 | CopyAssignment->setType(Context.getFunctionType(RetType, &ArgType, 1, EPI)); |
| 8303 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8304 | // Add the parameter to the operator. |
| 8305 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8306 | ClassLoc, ClassLoc, /*Id=*/0, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8307 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8308 | SC_None, |
| 8309 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8310 | CopyAssignment->setParams(FromParam); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 8311 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8312 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 8313 | |
| 8314 | CopyAssignment->setTrivial( |
| 8315 | ClassDecl->needsOverloadResolutionForCopyAssignment() |
| 8316 | ? SpecialMemberIsTrivial(CopyAssignment, CXXCopyAssignment) |
| 8317 | : ClassDecl->hasTrivialCopyAssignment()); |
| 8318 | |
Nico Weber | afcc96a | 2012-01-23 03:19:29 +0000 | [diff] [blame] | 8319 | // C++0x [class.copy]p19: |
| 8320 | // .... If the class definition does not explicitly declare a copy |
| 8321 | // assignment operator, there is no user-declared move constructor, and |
| 8322 | // there is no user-declared move assignment operator, a copy assignment |
| 8323 | // operator is implicitly declared as defaulted. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8324 | if (ShouldDeleteSpecialMember(CopyAssignment, CXXCopyAssignment)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 8325 | CopyAssignment->setDeletedAsWritten(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8326 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8327 | // Note that we have added this copy-assignment operator. |
| 8328 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
| 8329 | |
| 8330 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 8331 | PushOnScopeChains(CopyAssignment, S, false); |
| 8332 | ClassDecl->addDecl(CopyAssignment); |
| 8333 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8334 | return CopyAssignment; |
| 8335 | } |
| 8336 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8337 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 8338 | CXXMethodDecl *CopyAssignOperator) { |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 8339 | assert((CopyAssignOperator->isDefaulted() && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8340 | CopyAssignOperator->isOverloadedOperator() && |
| 8341 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8342 | !CopyAssignOperator->doesThisDeclarationHaveABody() && |
| 8343 | !CopyAssignOperator->isDeleted()) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8344 | "DefineImplicitCopyAssignment called for wrong function"); |
| 8345 | |
| 8346 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 8347 | |
| 8348 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 8349 | CopyAssignOperator->setInvalidDecl(); |
| 8350 | return; |
| 8351 | } |
| 8352 | |
| 8353 | CopyAssignOperator->setUsed(); |
| 8354 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8355 | SynthesizedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8356 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8357 | |
| 8358 | // C++0x [class.copy]p30: |
| 8359 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 8360 | // for a non-union class X performs memberwise copy assignment of its |
| 8361 | // subobjects. The direct base classes of X are assigned first, in the |
| 8362 | // order of their declaration in the base-specifier-list, and then the |
| 8363 | // immediate non-static data members of X are assigned, in the order in |
| 8364 | // which they were declared in the class definition. |
| 8365 | |
| 8366 | // The statements that form the synthesized function body. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 8367 | SmallVector<Stmt*, 8> Statements; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8368 | |
| 8369 | // The parameter for the "other" object, which we are copying from. |
| 8370 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 8371 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 8372 | QualType OtherRefType = Other->getType(); |
| 8373 | if (const LValueReferenceType *OtherRef |
| 8374 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 8375 | OtherRefType = OtherRef->getPointeeType(); |
| 8376 | OtherQuals = OtherRefType.getQualifiers(); |
| 8377 | } |
| 8378 | |
| 8379 | // Our location for everything implicitly-generated. |
| 8380 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 8381 | |
| 8382 | // Construct a reference to the "other" object. We'll be using this |
| 8383 | // throughout the generated ASTs. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8384 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8385 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 8386 | |
| 8387 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 8388 | // ASTs. |
| 8389 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 8390 | assert(This && "Reference to this cannot fail!"); |
| 8391 | |
| 8392 | // Assign base classes. |
| 8393 | bool Invalid = false; |
| 8394 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8395 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 8396 | // Form the assignment: |
| 8397 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 8398 | QualType BaseType = Base->getType().getUnqualifiedType(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 8399 | if (!BaseType->isRecordType()) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8400 | Invalid = true; |
| 8401 | continue; |
| 8402 | } |
| 8403 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 8404 | CXXCastPath BasePath; |
| 8405 | BasePath.push_back(Base); |
| 8406 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8407 | // Construct the "from" expression, which is an implicit cast to the |
| 8408 | // appropriately-qualified base type. |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 8409 | Expr *From = OtherRef; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8410 | From = ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
| 8411 | CK_UncheckedDerivedToBase, |
| 8412 | VK_LValue, &BasePath).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8413 | |
| 8414 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 8415 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8416 | |
| 8417 | // Implicitly cast "this" to the appropriately-qualified base type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8418 | To = ImpCastExprToType(To.take(), |
| 8419 | Context.getCVRQualifiedType(BaseType, |
| 8420 | CopyAssignOperator->getTypeQualifiers()), |
| 8421 | CK_UncheckedDerivedToBase, |
| 8422 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8423 | |
| 8424 | // Build the copy. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8425 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 8426 | To.get(), From, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8427 | /*CopyingBaseSubobject=*/true, |
| 8428 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8429 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 8430 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8431 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 8432 | CopyAssignOperator->setInvalidDecl(); |
| 8433 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8434 | } |
| 8435 | |
| 8436 | // Success! Record the copy. |
| 8437 | Statements.push_back(Copy.takeAs<Expr>()); |
| 8438 | } |
| 8439 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8440 | // Assign non-static members. |
| 8441 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8442 | FieldEnd = ClassDecl->field_end(); |
| 8443 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 8444 | if (Field->isUnnamedBitfield()) |
| 8445 | continue; |
| 8446 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8447 | // Check for members of reference type; we can't copy those. |
| 8448 | if (Field->getType()->isReferenceType()) { |
| 8449 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 8450 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 8451 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 8452 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8453 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8454 | Invalid = true; |
| 8455 | continue; |
| 8456 | } |
| 8457 | |
| 8458 | // Check for members of const-qualified, non-class type. |
| 8459 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 8460 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 8461 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 8462 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 8463 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 8464 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8465 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8466 | Invalid = true; |
| 8467 | continue; |
| 8468 | } |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 8469 | |
| 8470 | // Suppress assigning zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8471 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 8472 | continue; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8473 | |
| 8474 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 8475 | if (FieldType->isIncompleteArrayType()) { |
| 8476 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 8477 | "Incomplete array type is not valid"); |
| 8478 | continue; |
| 8479 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8480 | |
| 8481 | // Build references to the field in the object we're copying from and to. |
| 8482 | CXXScopeSpec SS; // Intentionally empty |
| 8483 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 8484 | LookupMemberName); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 8485 | MemberLookup.addDecl(*Field); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8486 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8487 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8488 | Loc, /*IsArrow=*/false, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8489 | SS, SourceLocation(), 0, |
| 8490 | MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8491 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8492 | Loc, /*IsArrow=*/true, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8493 | SS, SourceLocation(), 0, |
| 8494 | MemberLookup, 0); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8495 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 8496 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8497 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8498 | // Build the copy of this field. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8499 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, FieldType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8500 | To.get(), From.get(), |
| 8501 | /*CopyingBaseSubobject=*/false, |
| 8502 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8503 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 8504 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8505 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 8506 | CopyAssignOperator->setInvalidDecl(); |
| 8507 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8508 | } |
| 8509 | |
| 8510 | // Success! Record the copy. |
| 8511 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 8512 | } |
| 8513 | |
| 8514 | if (!Invalid) { |
| 8515 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8516 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8517 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8518 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8519 | if (Return.isInvalid()) |
| 8520 | Invalid = true; |
| 8521 | else { |
| 8522 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8523 | |
| 8524 | if (Trap.hasErrorOccurred()) { |
| 8525 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8526 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 8527 | Invalid = true; |
| 8528 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8529 | } |
| 8530 | } |
| 8531 | |
| 8532 | if (Invalid) { |
| 8533 | CopyAssignOperator->setInvalidDecl(); |
| 8534 | return; |
| 8535 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8536 | |
| 8537 | StmtResult Body; |
| 8538 | { |
| 8539 | CompoundScopeRAII CompoundScope(*this); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8540 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8541 | /*isStmtExpr=*/false); |
| 8542 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 8543 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8544 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 8545 | |
| 8546 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8547 | L->CompletedImplicitDefinition(CopyAssignOperator); |
| 8548 | } |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8549 | } |
| 8550 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8551 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8552 | Sema::ComputeDefaultedMoveAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 8553 | CXXRecordDecl *ClassDecl = MD->getParent(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8554 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8555 | ImplicitExceptionSpecification ExceptSpec(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8556 | if (ClassDecl->isInvalidDecl()) |
| 8557 | return ExceptSpec; |
| 8558 | |
| 8559 | // C++0x [except.spec]p14: |
| 8560 | // An implicitly declared special member function (Clause 12) shall have an |
| 8561 | // exception-specification. [...] |
| 8562 | |
| 8563 | // It is unspecified whether or not an implicit move assignment operator |
| 8564 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 8565 | // made. As such, this exception specification is effectively unspecified. |
| 8566 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 8567 | // the side of assuming such calls to be made regardless of whether they |
| 8568 | // actually happen. |
| 8569 | // Note that a move constructor is not implicitly declared when there are |
| 8570 | // virtual bases, but it can still be user-declared and explicitly defaulted. |
| 8571 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8572 | BaseEnd = ClassDecl->bases_end(); |
| 8573 | Base != BaseEnd; ++Base) { |
| 8574 | if (Base->isVirtual()) |
| 8575 | continue; |
| 8576 | |
| 8577 | CXXRecordDecl *BaseClassDecl |
| 8578 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 8579 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8580 | 0, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8581 | ExceptSpec.CalledDecl(Base->getLocStart(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8582 | } |
| 8583 | |
| 8584 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8585 | BaseEnd = ClassDecl->vbases_end(); |
| 8586 | Base != BaseEnd; ++Base) { |
| 8587 | CXXRecordDecl *BaseClassDecl |
| 8588 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 8589 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8590 | 0, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8591 | ExceptSpec.CalledDecl(Base->getLocStart(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8592 | } |
| 8593 | |
| 8594 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8595 | FieldEnd = ClassDecl->field_end(); |
| 8596 | Field != FieldEnd; |
| 8597 | ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 8598 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8599 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8600 | if (CXXMethodDecl *MoveAssign = |
| 8601 | LookupMovingAssignment(FieldClassDecl, |
| 8602 | FieldType.getCVRQualifiers(), |
| 8603 | false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8604 | ExceptSpec.CalledDecl(Field->getLocation(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8605 | } |
| 8606 | } |
| 8607 | |
| 8608 | return ExceptSpec; |
| 8609 | } |
| 8610 | |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8611 | /// Determine whether the class type has any direct or indirect virtual base |
| 8612 | /// classes which have a non-trivial move assignment operator. |
| 8613 | static bool |
| 8614 | hasVirtualBaseWithNonTrivialMoveAssignment(Sema &S, CXXRecordDecl *ClassDecl) { |
| 8615 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8616 | BaseEnd = ClassDecl->vbases_end(); |
| 8617 | Base != BaseEnd; ++Base) { |
| 8618 | CXXRecordDecl *BaseClass = |
| 8619 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 8620 | |
| 8621 | // Try to declare the move assignment. If it would be deleted, then the |
| 8622 | // class does not have a non-trivial move assignment. |
| 8623 | if (BaseClass->needsImplicitMoveAssignment()) |
| 8624 | S.DeclareImplicitMoveAssignment(BaseClass); |
| 8625 | |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 8626 | if (BaseClass->hasNonTrivialMoveAssignment()) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8627 | return true; |
| 8628 | } |
| 8629 | |
| 8630 | return false; |
| 8631 | } |
| 8632 | |
| 8633 | /// Determine whether the given type either has a move constructor or is |
| 8634 | /// trivially copyable. |
| 8635 | static bool |
| 8636 | hasMoveOrIsTriviallyCopyable(Sema &S, QualType Type, bool IsConstructor) { |
| 8637 | Type = S.Context.getBaseElementType(Type); |
| 8638 | |
| 8639 | // FIXME: Technically, non-trivially-copyable non-class types, such as |
| 8640 | // reference types, are supposed to return false here, but that appears |
| 8641 | // to be a standard defect. |
| 8642 | CXXRecordDecl *ClassDecl = Type->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | b5e4ace | 2012-10-10 16:14:06 +0000 | [diff] [blame] | 8643 | if (!ClassDecl || !ClassDecl->getDefinition() || ClassDecl->isInvalidDecl()) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8644 | return true; |
| 8645 | |
| 8646 | if (Type.isTriviallyCopyableType(S.Context)) |
| 8647 | return true; |
| 8648 | |
| 8649 | if (IsConstructor) { |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 8650 | // FIXME: Need this because otherwise hasMoveConstructor isn't guaranteed to |
| 8651 | // give the right answer. |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8652 | if (ClassDecl->needsImplicitMoveConstructor()) |
| 8653 | S.DeclareImplicitMoveConstructor(ClassDecl); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 8654 | return ClassDecl->hasMoveConstructor(); |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8655 | } |
| 8656 | |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 8657 | // FIXME: Need this because otherwise hasMoveAssignment isn't guaranteed to |
| 8658 | // give the right answer. |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8659 | if (ClassDecl->needsImplicitMoveAssignment()) |
| 8660 | S.DeclareImplicitMoveAssignment(ClassDecl); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 8661 | return ClassDecl->hasMoveAssignment(); |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8662 | } |
| 8663 | |
| 8664 | /// Determine whether all non-static data members and direct or virtual bases |
| 8665 | /// of class \p ClassDecl have either a move operation, or are trivially |
| 8666 | /// copyable. |
| 8667 | static bool subobjectsHaveMoveOrTrivialCopy(Sema &S, CXXRecordDecl *ClassDecl, |
| 8668 | bool IsConstructor) { |
| 8669 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8670 | BaseEnd = ClassDecl->bases_end(); |
| 8671 | Base != BaseEnd; ++Base) { |
| 8672 | if (Base->isVirtual()) |
| 8673 | continue; |
| 8674 | |
| 8675 | if (!hasMoveOrIsTriviallyCopyable(S, Base->getType(), IsConstructor)) |
| 8676 | return false; |
| 8677 | } |
| 8678 | |
| 8679 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8680 | BaseEnd = ClassDecl->vbases_end(); |
| 8681 | Base != BaseEnd; ++Base) { |
| 8682 | if (!hasMoveOrIsTriviallyCopyable(S, Base->getType(), IsConstructor)) |
| 8683 | return false; |
| 8684 | } |
| 8685 | |
| 8686 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8687 | FieldEnd = ClassDecl->field_end(); |
| 8688 | Field != FieldEnd; ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 8689 | if (!hasMoveOrIsTriviallyCopyable(S, Field->getType(), IsConstructor)) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8690 | return false; |
| 8691 | } |
| 8692 | |
| 8693 | return true; |
| 8694 | } |
| 8695 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8696 | CXXMethodDecl *Sema::DeclareImplicitMoveAssignment(CXXRecordDecl *ClassDecl) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8697 | // C++11 [class.copy]p20: |
| 8698 | // If the definition of a class X does not explicitly declare a move |
| 8699 | // assignment operator, one will be implicitly declared as defaulted |
| 8700 | // if and only if: |
| 8701 | // |
| 8702 | // - [first 4 bullets] |
| 8703 | assert(ClassDecl->needsImplicitMoveAssignment()); |
| 8704 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8705 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveAssignment); |
| 8706 | if (DSM.isAlreadyBeingDeclared()) |
| 8707 | return 0; |
| 8708 | |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8709 | // [Checked after we build the declaration] |
| 8710 | // - the move assignment operator would not be implicitly defined as |
| 8711 | // deleted, |
| 8712 | |
| 8713 | // [DR1402]: |
| 8714 | // - X has no direct or indirect virtual base class with a non-trivial |
| 8715 | // move assignment operator, and |
| 8716 | // - each of X's non-static data members and direct or virtual base classes |
| 8717 | // has a type that either has a move assignment operator or is trivially |
| 8718 | // copyable. |
| 8719 | if (hasVirtualBaseWithNonTrivialMoveAssignment(*this, ClassDecl) || |
| 8720 | !subobjectsHaveMoveOrTrivialCopy(*this, ClassDecl,/*Constructor*/false)) { |
| 8721 | ClassDecl->setFailedImplicitMoveAssignment(); |
| 8722 | return 0; |
| 8723 | } |
| 8724 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8725 | // Note: The following rules are largely analoguous to the move |
| 8726 | // constructor rules. |
| 8727 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8728 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 8729 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 8730 | ArgType = Context.getRValueReferenceType(ArgType); |
| 8731 | |
| 8732 | // An implicitly-declared move assignment operator is an inline public |
| 8733 | // member of its class. |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8734 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 8735 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 8736 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 8737 | CXXMethodDecl *MoveAssignment |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8738 | = CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8739 | /*TInfo=*/0, /*isStatic=*/false, |
| 8740 | /*StorageClassAsWritten=*/SC_None, |
| 8741 | /*isInline=*/true, |
| 8742 | /*isConstexpr=*/false, |
| 8743 | SourceLocation()); |
| 8744 | MoveAssignment->setAccess(AS_public); |
| 8745 | MoveAssignment->setDefaulted(); |
| 8746 | MoveAssignment->setImplicit(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8747 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8748 | // Build an exception specification pointing back at this member. |
| 8749 | FunctionProtoType::ExtProtoInfo EPI; |
| 8750 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8751 | EPI.ExceptionSpecDecl = MoveAssignment; |
| 8752 | MoveAssignment->setType(Context.getFunctionType(RetType, &ArgType, 1, EPI)); |
| 8753 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8754 | // Add the parameter to the operator. |
| 8755 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveAssignment, |
| 8756 | ClassLoc, ClassLoc, /*Id=*/0, |
| 8757 | ArgType, /*TInfo=*/0, |
| 8758 | SC_None, |
| 8759 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8760 | MoveAssignment->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8761 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8762 | AddOverriddenMethods(ClassDecl, MoveAssignment); |
| 8763 | |
| 8764 | MoveAssignment->setTrivial( |
| 8765 | ClassDecl->needsOverloadResolutionForMoveAssignment() |
| 8766 | ? SpecialMemberIsTrivial(MoveAssignment, CXXMoveAssignment) |
| 8767 | : ClassDecl->hasTrivialMoveAssignment()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8768 | |
| 8769 | // C++0x [class.copy]p9: |
| 8770 | // If the definition of a class X does not explicitly declare a move |
| 8771 | // assignment operator, one will be implicitly declared as defaulted if and |
| 8772 | // only if: |
| 8773 | // [...] |
| 8774 | // - the move assignment operator would not be implicitly defined as |
| 8775 | // deleted. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 8776 | if (ShouldDeleteSpecialMember(MoveAssignment, CXXMoveAssignment)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8777 | // Cache this result so that we don't try to generate this over and over |
| 8778 | // on every lookup, leaking memory and wasting time. |
| 8779 | ClassDecl->setFailedImplicitMoveAssignment(); |
| 8780 | return 0; |
| 8781 | } |
| 8782 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8783 | // Note that we have added this copy-assignment operator. |
| 8784 | ++ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
| 8785 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8786 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 8787 | PushOnScopeChains(MoveAssignment, S, false); |
| 8788 | ClassDecl->addDecl(MoveAssignment); |
| 8789 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8790 | return MoveAssignment; |
| 8791 | } |
| 8792 | |
| 8793 | void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation, |
| 8794 | CXXMethodDecl *MoveAssignOperator) { |
| 8795 | assert((MoveAssignOperator->isDefaulted() && |
| 8796 | MoveAssignOperator->isOverloadedOperator() && |
| 8797 | MoveAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8798 | !MoveAssignOperator->doesThisDeclarationHaveABody() && |
| 8799 | !MoveAssignOperator->isDeleted()) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8800 | "DefineImplicitMoveAssignment called for wrong function"); |
| 8801 | |
| 8802 | CXXRecordDecl *ClassDecl = MoveAssignOperator->getParent(); |
| 8803 | |
| 8804 | if (ClassDecl->isInvalidDecl() || MoveAssignOperator->isInvalidDecl()) { |
| 8805 | MoveAssignOperator->setInvalidDecl(); |
| 8806 | return; |
| 8807 | } |
| 8808 | |
| 8809 | MoveAssignOperator->setUsed(); |
| 8810 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8811 | SynthesizedFunctionScope Scope(*this, MoveAssignOperator); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8812 | DiagnosticErrorTrap Trap(Diags); |
| 8813 | |
| 8814 | // C++0x [class.copy]p28: |
| 8815 | // The implicitly-defined or move assignment operator for a non-union class |
| 8816 | // X performs memberwise move assignment of its subobjects. The direct base |
| 8817 | // classes of X are assigned first, in the order of their declaration in the |
| 8818 | // base-specifier-list, and then the immediate non-static data members of X |
| 8819 | // are assigned, in the order in which they were declared in the class |
| 8820 | // definition. |
| 8821 | |
| 8822 | // The statements that form the synthesized function body. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 8823 | SmallVector<Stmt*, 8> Statements; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8824 | |
| 8825 | // The parameter for the "other" object, which we are move from. |
| 8826 | ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0); |
| 8827 | QualType OtherRefType = Other->getType()-> |
| 8828 | getAs<RValueReferenceType>()->getPointeeType(); |
| 8829 | assert(OtherRefType.getQualifiers() == 0 && |
| 8830 | "Bad argument type of defaulted move assignment"); |
| 8831 | |
| 8832 | // Our location for everything implicitly-generated. |
| 8833 | SourceLocation Loc = MoveAssignOperator->getLocation(); |
| 8834 | |
| 8835 | // Construct a reference to the "other" object. We'll be using this |
| 8836 | // throughout the generated ASTs. |
| 8837 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
| 8838 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 8839 | // Cast to rvalue. |
| 8840 | OtherRef = CastForMoving(*this, OtherRef); |
| 8841 | |
| 8842 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 8843 | // ASTs. |
| 8844 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 8845 | assert(This && "Reference to this cannot fail!"); |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8846 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8847 | // Assign base classes. |
| 8848 | bool Invalid = false; |
| 8849 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8850 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 8851 | // Form the assignment: |
| 8852 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&&>(other)); |
| 8853 | QualType BaseType = Base->getType().getUnqualifiedType(); |
| 8854 | if (!BaseType->isRecordType()) { |
| 8855 | Invalid = true; |
| 8856 | continue; |
| 8857 | } |
| 8858 | |
| 8859 | CXXCastPath BasePath; |
| 8860 | BasePath.push_back(Base); |
| 8861 | |
| 8862 | // Construct the "from" expression, which is an implicit cast to the |
| 8863 | // appropriately-qualified base type. |
| 8864 | Expr *From = OtherRef; |
| 8865 | From = ImpCastExprToType(From, BaseType, CK_UncheckedDerivedToBase, |
Douglas Gregor | b2b5658 | 2011-09-06 16:26:56 +0000 | [diff] [blame] | 8866 | VK_XValue, &BasePath).take(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8867 | |
| 8868 | // Dereference "this". |
| 8869 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
| 8870 | |
| 8871 | // Implicitly cast "this" to the appropriately-qualified base type. |
| 8872 | To = ImpCastExprToType(To.take(), |
| 8873 | Context.getCVRQualifiedType(BaseType, |
| 8874 | MoveAssignOperator->getTypeQualifiers()), |
| 8875 | CK_UncheckedDerivedToBase, |
| 8876 | VK_LValue, &BasePath); |
| 8877 | |
| 8878 | // Build the move. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8879 | StmtResult Move = buildSingleCopyAssign(*this, Loc, BaseType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8880 | To.get(), From, |
| 8881 | /*CopyingBaseSubobject=*/true, |
| 8882 | /*Copying=*/false); |
| 8883 | if (Move.isInvalid()) { |
| 8884 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8885 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8886 | MoveAssignOperator->setInvalidDecl(); |
| 8887 | return; |
| 8888 | } |
| 8889 | |
| 8890 | // Success! Record the move. |
| 8891 | Statements.push_back(Move.takeAs<Expr>()); |
| 8892 | } |
| 8893 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8894 | // Assign non-static members. |
| 8895 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8896 | FieldEnd = ClassDecl->field_end(); |
| 8897 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 8898 | if (Field->isUnnamedBitfield()) |
| 8899 | continue; |
| 8900 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8901 | // Check for members of reference type; we can't move those. |
| 8902 | if (Field->getType()->isReferenceType()) { |
| 8903 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 8904 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 8905 | Diag(Field->getLocation(), diag::note_declared_at); |
| 8906 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8907 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8908 | Invalid = true; |
| 8909 | continue; |
| 8910 | } |
| 8911 | |
| 8912 | // Check for members of const-qualified, non-class type. |
| 8913 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 8914 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 8915 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 8916 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 8917 | Diag(Field->getLocation(), diag::note_declared_at); |
| 8918 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8919 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8920 | Invalid = true; |
| 8921 | continue; |
| 8922 | } |
| 8923 | |
| 8924 | // Suppress assigning zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8925 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 8926 | continue; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8927 | |
| 8928 | QualType FieldType = Field->getType().getNonReferenceType(); |
| 8929 | if (FieldType->isIncompleteArrayType()) { |
| 8930 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 8931 | "Incomplete array type is not valid"); |
| 8932 | continue; |
| 8933 | } |
| 8934 | |
| 8935 | // Build references to the field in the object we're copying from and to. |
| 8936 | CXXScopeSpec SS; // Intentionally empty |
| 8937 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 8938 | LookupMemberName); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 8939 | MemberLookup.addDecl(*Field); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8940 | MemberLookup.resolveKind(); |
| 8941 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
| 8942 | Loc, /*IsArrow=*/false, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8943 | SS, SourceLocation(), 0, |
| 8944 | MemberLookup, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8945 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
| 8946 | Loc, /*IsArrow=*/true, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8947 | SS, SourceLocation(), 0, |
| 8948 | MemberLookup, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8949 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 8950 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 8951 | |
| 8952 | assert(!From.get()->isLValue() && // could be xvalue or prvalue |
| 8953 | "Member reference with rvalue base must be rvalue except for reference " |
| 8954 | "members, which aren't allowed for move assignment."); |
| 8955 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8956 | // Build the move of this field. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8957 | StmtResult Move = buildSingleCopyAssign(*this, Loc, FieldType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8958 | To.get(), From.get(), |
| 8959 | /*CopyingBaseSubobject=*/false, |
| 8960 | /*Copying=*/false); |
| 8961 | if (Move.isInvalid()) { |
| 8962 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8963 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8964 | MoveAssignOperator->setInvalidDecl(); |
| 8965 | return; |
| 8966 | } |
Richard Smith | e7ce709 | 2012-11-12 23:33:00 +0000 | [diff] [blame] | 8967 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8968 | // Success! Record the copy. |
| 8969 | Statements.push_back(Move.takeAs<Stmt>()); |
| 8970 | } |
| 8971 | |
| 8972 | if (!Invalid) { |
| 8973 | // Add a "return *this;" |
| 8974 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
| 8975 | |
| 8976 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
| 8977 | if (Return.isInvalid()) |
| 8978 | Invalid = true; |
| 8979 | else { |
| 8980 | Statements.push_back(Return.takeAs<Stmt>()); |
| 8981 | |
| 8982 | if (Trap.hasErrorOccurred()) { |
| 8983 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8984 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8985 | Invalid = true; |
| 8986 | } |
| 8987 | } |
| 8988 | } |
| 8989 | |
| 8990 | if (Invalid) { |
| 8991 | MoveAssignOperator->setInvalidDecl(); |
| 8992 | return; |
| 8993 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8994 | |
| 8995 | StmtResult Body; |
| 8996 | { |
| 8997 | CompoundScopeRAII CompoundScope(*this); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8998 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8999 | /*isStmtExpr=*/false); |
| 9000 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 9001 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9002 | MoveAssignOperator->setBody(Body.takeAs<Stmt>()); |
| 9003 | |
| 9004 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9005 | L->CompletedImplicitDefinition(MoveAssignOperator); |
| 9006 | } |
| 9007 | } |
| 9008 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9009 | Sema::ImplicitExceptionSpecification |
| 9010 | Sema::ComputeDefaultedCopyCtorExceptionSpec(CXXMethodDecl *MD) { |
| 9011 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 9012 | |
| 9013 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 9014 | if (ClassDecl->isInvalidDecl()) |
| 9015 | return ExceptSpec; |
| 9016 | |
| 9017 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
| 9018 | assert(T->getNumArgs() >= 1 && "not a copy ctor"); |
| 9019 | unsigned Quals = T->getArgType(0).getNonReferenceType().getCVRQualifiers(); |
| 9020 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9021 | // C++ [except.spec]p14: |
| 9022 | // An implicitly declared special member function (Clause 12) shall have an |
| 9023 | // exception-specification. [...] |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9024 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9025 | BaseEnd = ClassDecl->bases_end(); |
| 9026 | Base != BaseEnd; |
| 9027 | ++Base) { |
| 9028 | // Virtual bases are handled below. |
| 9029 | if (Base->isVirtual()) |
| 9030 | continue; |
| 9031 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 9032 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9033 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 9034 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9035 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9036 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9037 | } |
| 9038 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 9039 | BaseEnd = ClassDecl->vbases_end(); |
| 9040 | Base != BaseEnd; |
| 9041 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 9042 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9043 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 9044 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9045 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9046 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9047 | } |
| 9048 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9049 | FieldEnd = ClassDecl->field_end(); |
| 9050 | Field != FieldEnd; |
| 9051 | ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 9052 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 9053 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 9054 | if (CXXConstructorDecl *CopyConstructor = |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9055 | LookupCopyingConstructor(FieldClassDecl, |
| 9056 | Quals | FieldType.getCVRQualifiers())) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9057 | ExceptSpec.CalledDecl(Field->getLocation(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9058 | } |
| 9059 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 9060 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9061 | return ExceptSpec; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9062 | } |
| 9063 | |
| 9064 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 9065 | CXXRecordDecl *ClassDecl) { |
| 9066 | // C++ [class.copy]p4: |
| 9067 | // If the class definition does not explicitly declare a copy |
| 9068 | // constructor, one is declared implicitly. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9069 | assert(ClassDecl->needsImplicitCopyConstructor()); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9070 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9071 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyConstructor); |
| 9072 | if (DSM.isAlreadyBeingDeclared()) |
| 9073 | return 0; |
| 9074 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9075 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 9076 | QualType ArgType = ClassType; |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 9077 | bool Const = ClassDecl->implicitCopyConstructorHasConstParam(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9078 | if (Const) |
| 9079 | ArgType = ArgType.withConst(); |
| 9080 | ArgType = Context.getLValueReferenceType(ArgType); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9081 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 9082 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 9083 | CXXCopyConstructor, |
| 9084 | Const); |
| 9085 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9086 | DeclarationName Name |
| 9087 | = Context.DeclarationNames.getCXXConstructorName( |
| 9088 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9089 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 9090 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9091 | |
| 9092 | // An implicitly-declared copy constructor is an inline public |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9093 | // member of its class. |
| 9094 | CXXConstructorDecl *CopyConstructor = CXXConstructorDecl::Create( |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9095 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/0, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9096 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 9097 | Constexpr); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9098 | CopyConstructor->setAccess(AS_public); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9099 | CopyConstructor->setDefaulted(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9100 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9101 | // Build an exception specification pointing back at this member. |
| 9102 | FunctionProtoType::ExtProtoInfo EPI; |
| 9103 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 9104 | EPI.ExceptionSpecDecl = CopyConstructor; |
| 9105 | CopyConstructor->setType( |
| 9106 | Context.getFunctionType(Context.VoidTy, &ArgType, 1, EPI)); |
| 9107 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9108 | // Add the parameter to the constructor. |
| 9109 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9110 | ClassLoc, ClassLoc, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9111 | /*IdentifierInfo=*/0, |
| 9112 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9113 | SC_None, |
| 9114 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9115 | CopyConstructor->setParams(FromParam); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9116 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9117 | CopyConstructor->setTrivial( |
| 9118 | ClassDecl->needsOverloadResolutionForCopyConstructor() |
| 9119 | ? SpecialMemberIsTrivial(CopyConstructor, CXXCopyConstructor) |
| 9120 | : ClassDecl->hasTrivialCopyConstructor()); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 9121 | |
Nico Weber | afcc96a | 2012-01-23 03:19:29 +0000 | [diff] [blame] | 9122 | // C++11 [class.copy]p8: |
| 9123 | // ... If the class definition does not explicitly declare a copy |
| 9124 | // constructor, there is no user-declared move constructor, and there is no |
| 9125 | // user-declared move assignment operator, a copy constructor is implicitly |
| 9126 | // declared as defaulted. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9127 | if (ShouldDeleteSpecialMember(CopyConstructor, CXXCopyConstructor)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 9128 | CopyConstructor->setDeletedAsWritten(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9129 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9130 | // Note that we have declared this constructor. |
| 9131 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 9132 | |
| 9133 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 9134 | PushOnScopeChains(CopyConstructor, S, false); |
| 9135 | ClassDecl->addDecl(CopyConstructor); |
| 9136 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9137 | return CopyConstructor; |
| 9138 | } |
| 9139 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9140 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9141 | CXXConstructorDecl *CopyConstructor) { |
| 9142 | assert((CopyConstructor->isDefaulted() && |
| 9143 | CopyConstructor->isCopyConstructor() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9144 | !CopyConstructor->doesThisDeclarationHaveABody() && |
| 9145 | !CopyConstructor->isDeleted()) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9146 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9147 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 9148 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9149 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9150 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9151 | SynthesizedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 9152 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9153 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 9154 | if (SetCtorInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 9155 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 9156 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9157 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 9158 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9159 | } else { |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9160 | Sema::CompoundScopeRAII CompoundScope(*this); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9161 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 9162 | CopyConstructor->getLocation(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9163 | MultiStmtArg(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9164 | /*isStmtExpr=*/false) |
| 9165 | .takeAs<Stmt>()); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame] | 9166 | CopyConstructor->setImplicitlyDefined(true); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 9167 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9168 | |
| 9169 | CopyConstructor->setUsed(); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 9170 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9171 | L->CompletedImplicitDefinition(CopyConstructor); |
| 9172 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9173 | } |
| 9174 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9175 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9176 | Sema::ComputeDefaultedMoveCtorExceptionSpec(CXXMethodDecl *MD) { |
| 9177 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 9178 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9179 | // C++ [except.spec]p14: |
| 9180 | // An implicitly declared special member function (Clause 12) shall have an |
| 9181 | // exception-specification. [...] |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9182 | ImplicitExceptionSpecification ExceptSpec(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9183 | if (ClassDecl->isInvalidDecl()) |
| 9184 | return ExceptSpec; |
| 9185 | |
| 9186 | // Direct base-class constructors. |
| 9187 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 9188 | BEnd = ClassDecl->bases_end(); |
| 9189 | B != BEnd; ++B) { |
| 9190 | if (B->isVirtual()) // Handled below. |
| 9191 | continue; |
| 9192 | |
| 9193 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 9194 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9195 | CXXConstructorDecl *Constructor = |
| 9196 | LookupMovingConstructor(BaseClassDecl, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9197 | // If this is a deleted function, add it anyway. This might be conformant |
| 9198 | // with the standard. This might not. I'm not sure. It might not matter. |
| 9199 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9200 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9201 | } |
| 9202 | } |
| 9203 | |
| 9204 | // Virtual base-class constructors. |
| 9205 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 9206 | BEnd = ClassDecl->vbases_end(); |
| 9207 | B != BEnd; ++B) { |
| 9208 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 9209 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9210 | CXXConstructorDecl *Constructor = |
| 9211 | LookupMovingConstructor(BaseClassDecl, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9212 | // If this is a deleted function, add it anyway. This might be conformant |
| 9213 | // with the standard. This might not. I'm not sure. It might not matter. |
| 9214 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9215 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9216 | } |
| 9217 | } |
| 9218 | |
| 9219 | // Field constructors. |
| 9220 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 9221 | FEnd = ClassDecl->field_end(); |
| 9222 | F != FEnd; ++F) { |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9223 | QualType FieldType = Context.getBaseElementType(F->getType()); |
| 9224 | if (CXXRecordDecl *FieldRecDecl = FieldType->getAsCXXRecordDecl()) { |
| 9225 | CXXConstructorDecl *Constructor = |
| 9226 | LookupMovingConstructor(FieldRecDecl, FieldType.getCVRQualifiers()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9227 | // If this is a deleted function, add it anyway. This might be conformant |
| 9228 | // with the standard. This might not. I'm not sure. It might not matter. |
| 9229 | // In particular, the problem is that this function never gets called. It |
| 9230 | // might just be ill-formed because this function attempts to refer to |
| 9231 | // a deleted function here. |
| 9232 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9233 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9234 | } |
| 9235 | } |
| 9236 | |
| 9237 | return ExceptSpec; |
| 9238 | } |
| 9239 | |
| 9240 | CXXConstructorDecl *Sema::DeclareImplicitMoveConstructor( |
| 9241 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9242 | // C++11 [class.copy]p9: |
| 9243 | // If the definition of a class X does not explicitly declare a move |
| 9244 | // constructor, one will be implicitly declared as defaulted if and only if: |
| 9245 | // |
| 9246 | // - [first 4 bullets] |
| 9247 | assert(ClassDecl->needsImplicitMoveConstructor()); |
| 9248 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9249 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveConstructor); |
| 9250 | if (DSM.isAlreadyBeingDeclared()) |
| 9251 | return 0; |
| 9252 | |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9253 | // [Checked after we build the declaration] |
| 9254 | // - the move assignment operator would not be implicitly defined as |
| 9255 | // deleted, |
| 9256 | |
| 9257 | // [DR1402]: |
| 9258 | // - each of X's non-static data members and direct or virtual base classes |
| 9259 | // has a type that either has a move constructor or is trivially copyable. |
| 9260 | if (!subobjectsHaveMoveOrTrivialCopy(*this, ClassDecl, /*Constructor*/true)) { |
| 9261 | ClassDecl->setFailedImplicitMoveConstructor(); |
| 9262 | return 0; |
| 9263 | } |
| 9264 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9265 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 9266 | QualType ArgType = Context.getRValueReferenceType(ClassType); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9267 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 9268 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 9269 | CXXMoveConstructor, |
| 9270 | false); |
| 9271 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9272 | DeclarationName Name |
| 9273 | = Context.DeclarationNames.getCXXConstructorName( |
| 9274 | Context.getCanonicalType(ClassType)); |
| 9275 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 9276 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 9277 | |
| 9278 | // C++0x [class.copy]p11: |
| 9279 | // An implicitly-declared copy/move constructor is an inline public |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9280 | // member of its class. |
| 9281 | CXXConstructorDecl *MoveConstructor = CXXConstructorDecl::Create( |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9282 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/0, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9283 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 9284 | Constexpr); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9285 | MoveConstructor->setAccess(AS_public); |
| 9286 | MoveConstructor->setDefaulted(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9287 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9288 | // Build an exception specification pointing back at this member. |
| 9289 | FunctionProtoType::ExtProtoInfo EPI; |
| 9290 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 9291 | EPI.ExceptionSpecDecl = MoveConstructor; |
| 9292 | MoveConstructor->setType( |
| 9293 | Context.getFunctionType(Context.VoidTy, &ArgType, 1, EPI)); |
| 9294 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9295 | // Add the parameter to the constructor. |
| 9296 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveConstructor, |
| 9297 | ClassLoc, ClassLoc, |
| 9298 | /*IdentifierInfo=*/0, |
| 9299 | ArgType, /*TInfo=*/0, |
| 9300 | SC_None, |
| 9301 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9302 | MoveConstructor->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9303 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9304 | MoveConstructor->setTrivial( |
| 9305 | ClassDecl->needsOverloadResolutionForMoveConstructor() |
| 9306 | ? SpecialMemberIsTrivial(MoveConstructor, CXXMoveConstructor) |
| 9307 | : ClassDecl->hasTrivialMoveConstructor()); |
| 9308 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9309 | // C++0x [class.copy]p9: |
| 9310 | // If the definition of a class X does not explicitly declare a move |
| 9311 | // constructor, one will be implicitly declared as defaulted if and only if: |
| 9312 | // [...] |
| 9313 | // - the move constructor would not be implicitly defined as deleted. |
Sean Hunt | 769bb2d | 2011-10-11 06:43:29 +0000 | [diff] [blame] | 9314 | if (ShouldDeleteSpecialMember(MoveConstructor, CXXMoveConstructor)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9315 | // Cache this result so that we don't try to generate this over and over |
| 9316 | // on every lookup, leaking memory and wasting time. |
| 9317 | ClassDecl->setFailedImplicitMoveConstructor(); |
| 9318 | return 0; |
| 9319 | } |
| 9320 | |
| 9321 | // Note that we have declared this constructor. |
| 9322 | ++ASTContext::NumImplicitMoveConstructorsDeclared; |
| 9323 | |
| 9324 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 9325 | PushOnScopeChains(MoveConstructor, S, false); |
| 9326 | ClassDecl->addDecl(MoveConstructor); |
| 9327 | |
| 9328 | return MoveConstructor; |
| 9329 | } |
| 9330 | |
| 9331 | void Sema::DefineImplicitMoveConstructor(SourceLocation CurrentLocation, |
| 9332 | CXXConstructorDecl *MoveConstructor) { |
| 9333 | assert((MoveConstructor->isDefaulted() && |
| 9334 | MoveConstructor->isMoveConstructor() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9335 | !MoveConstructor->doesThisDeclarationHaveABody() && |
| 9336 | !MoveConstructor->isDeleted()) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9337 | "DefineImplicitMoveConstructor - call it for implicit move ctor"); |
| 9338 | |
| 9339 | CXXRecordDecl *ClassDecl = MoveConstructor->getParent(); |
| 9340 | assert(ClassDecl && "DefineImplicitMoveConstructor - invalid constructor"); |
| 9341 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9342 | SynthesizedFunctionScope Scope(*this, MoveConstructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9343 | DiagnosticErrorTrap Trap(Diags); |
| 9344 | |
| 9345 | if (SetCtorInitializers(MoveConstructor, 0, 0, /*AnyErrors=*/false) || |
| 9346 | Trap.hasErrorOccurred()) { |
| 9347 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9348 | << CXXMoveConstructor << Context.getTagDeclType(ClassDecl); |
| 9349 | MoveConstructor->setInvalidDecl(); |
| 9350 | } else { |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9351 | Sema::CompoundScopeRAII CompoundScope(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9352 | MoveConstructor->setBody(ActOnCompoundStmt(MoveConstructor->getLocation(), |
| 9353 | MoveConstructor->getLocation(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9354 | MultiStmtArg(), |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9355 | /*isStmtExpr=*/false) |
| 9356 | .takeAs<Stmt>()); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame] | 9357 | MoveConstructor->setImplicitlyDefined(true); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9358 | } |
| 9359 | |
| 9360 | MoveConstructor->setUsed(); |
| 9361 | |
| 9362 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9363 | L->CompletedImplicitDefinition(MoveConstructor); |
| 9364 | } |
| 9365 | } |
| 9366 | |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 9367 | bool Sema::isImplicitlyDeleted(FunctionDecl *FD) { |
| 9368 | return FD->isDeleted() && |
| 9369 | (FD->isDefaulted() || FD->isImplicit()) && |
| 9370 | isa<CXXMethodDecl>(FD); |
| 9371 | } |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9372 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 9373 | /// \brief Mark the call operator of the given lambda closure type as "used". |
| 9374 | static void markLambdaCallOperatorUsed(Sema &S, CXXRecordDecl *Lambda) { |
| 9375 | CXXMethodDecl *CallOperator |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9376 | = cast<CXXMethodDecl>( |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 9377 | Lambda->lookup( |
| 9378 | S.Context.DeclarationNames.getCXXOperatorName(OO_Call)).front()); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 9379 | CallOperator->setReferenced(); |
| 9380 | CallOperator->setUsed(); |
| 9381 | } |
| 9382 | |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9383 | void Sema::DefineImplicitLambdaToFunctionPointerConversion( |
| 9384 | SourceLocation CurrentLocation, |
| 9385 | CXXConversionDecl *Conv) |
| 9386 | { |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 9387 | CXXRecordDecl *Lambda = Conv->getParent(); |
| 9388 | |
| 9389 | // Make sure that the lambda call operator is marked used. |
| 9390 | markLambdaCallOperatorUsed(*this, Lambda); |
| 9391 | |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9392 | Conv->setUsed(); |
| 9393 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9394 | SynthesizedFunctionScope Scope(*this, Conv); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9395 | DiagnosticErrorTrap Trap(Diags); |
| 9396 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 9397 | // Return the address of the __invoke function. |
| 9398 | DeclarationName InvokeName = &Context.Idents.get("__invoke"); |
| 9399 | CXXMethodDecl *Invoke |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 9400 | = cast<CXXMethodDecl>(Lambda->lookup(InvokeName).front()); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 9401 | Expr *FunctionRef = BuildDeclRefExpr(Invoke, Invoke->getType(), |
| 9402 | VK_LValue, Conv->getLocation()).take(); |
| 9403 | assert(FunctionRef && "Can't refer to __invoke function?"); |
| 9404 | Stmt *Return = ActOnReturnStmt(Conv->getLocation(), FunctionRef).take(); |
Nico Weber | d36aa35 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 9405 | Conv->setBody(new (Context) CompoundStmt(Context, Return, |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 9406 | Conv->getLocation(), |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9407 | Conv->getLocation())); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 9408 | |
| 9409 | // Fill in the __invoke function with a dummy implementation. IR generation |
| 9410 | // will fill in the actual details. |
| 9411 | Invoke->setUsed(); |
| 9412 | Invoke->setReferenced(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 9413 | Invoke->setBody(new (Context) CompoundStmt(Conv->getLocation())); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9414 | |
| 9415 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9416 | L->CompletedImplicitDefinition(Conv); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 9417 | L->CompletedImplicitDefinition(Invoke); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9418 | } |
| 9419 | } |
| 9420 | |
| 9421 | void Sema::DefineImplicitLambdaToBlockPointerConversion( |
| 9422 | SourceLocation CurrentLocation, |
| 9423 | CXXConversionDecl *Conv) |
| 9424 | { |
| 9425 | Conv->setUsed(); |
| 9426 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9427 | SynthesizedFunctionScope Scope(*this, Conv); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9428 | DiagnosticErrorTrap Trap(Diags); |
| 9429 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9430 | // Copy-initialize the lambda object as needed to capture it. |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9431 | Expr *This = ActOnCXXThis(CurrentLocation).take(); |
| 9432 | Expr *DerefThis =CreateBuiltinUnaryOp(CurrentLocation, UO_Deref, This).take(); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9433 | |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 9434 | ExprResult BuildBlock = BuildBlockForLambdaConversion(CurrentLocation, |
| 9435 | Conv->getLocation(), |
| 9436 | Conv, DerefThis); |
| 9437 | |
| 9438 | // If we're not under ARC, make sure we still get the _Block_copy/autorelease |
| 9439 | // behavior. Note that only the general conversion function does this |
| 9440 | // (since it's unusable otherwise); in the case where we inline the |
| 9441 | // block literal, it has block literal lifetime semantics. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9442 | if (!BuildBlock.isInvalid() && !getLangOpts().ObjCAutoRefCount) |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 9443 | BuildBlock = ImplicitCastExpr::Create(Context, BuildBlock.get()->getType(), |
| 9444 | CK_CopyAndAutoreleaseBlockObject, |
| 9445 | BuildBlock.get(), 0, VK_RValue); |
| 9446 | |
| 9447 | if (BuildBlock.isInvalid()) { |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9448 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9449 | Conv->setInvalidDecl(); |
| 9450 | return; |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9451 | } |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9452 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9453 | // Create the return statement that returns the block from the conversion |
| 9454 | // function. |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 9455 | StmtResult Return = ActOnReturnStmt(Conv->getLocation(), BuildBlock.get()); |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9456 | if (Return.isInvalid()) { |
| 9457 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
| 9458 | Conv->setInvalidDecl(); |
| 9459 | return; |
| 9460 | } |
| 9461 | |
| 9462 | // Set the body of the conversion function. |
| 9463 | Stmt *ReturnS = Return.take(); |
Nico Weber | d36aa35 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 9464 | Conv->setBody(new (Context) CompoundStmt(Context, ReturnS, |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9465 | Conv->getLocation(), |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9466 | Conv->getLocation())); |
| 9467 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9468 | // We're done; notify the mutation listener, if any. |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 9469 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9470 | L->CompletedImplicitDefinition(Conv); |
| 9471 | } |
| 9472 | } |
| 9473 | |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 9474 | /// \brief Determine whether the given list arguments contains exactly one |
| 9475 | /// "real" (non-default) argument. |
| 9476 | static bool hasOneRealArgument(MultiExprArg Args) { |
| 9477 | switch (Args.size()) { |
| 9478 | case 0: |
| 9479 | return false; |
| 9480 | |
| 9481 | default: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9482 | if (!Args[1]->isDefaultArgument()) |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 9483 | return false; |
| 9484 | |
| 9485 | // fall through |
| 9486 | case 1: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9487 | return !Args[0]->isDefaultArgument(); |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 9488 | } |
| 9489 | |
| 9490 | return false; |
| 9491 | } |
| 9492 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9493 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 9494 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9495 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 9496 | MultiExprArg ExprArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9497 | bool HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 9498 | bool IsListInitialization, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9499 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 9500 | unsigned ConstructKind, |
| 9501 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 9502 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9503 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 9504 | // C++0x [class.copy]p34: |
| 9505 | // When certain criteria are met, an implementation is allowed to |
| 9506 | // omit the copy/move construction of a class object, even if the |
| 9507 | // copy/move constructor and/or destructor for the object have |
| 9508 | // side effects. [...] |
| 9509 | // - when a temporary class object that has not been bound to a |
| 9510 | // reference (12.2) would be copied/moved to a class object |
| 9511 | // with the same cv-unqualified type, the copy/move operation |
| 9512 | // can be omitted by constructing the temporary object |
| 9513 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 9514 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 9515 | Constructor->isCopyOrMoveConstructor() && hasOneRealArgument(ExprArgs)) { |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9516 | Expr *SubExpr = ExprArgs[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 9517 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 9518 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9519 | |
| 9520 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9521 | Elidable, ExprArgs, HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 9522 | IsListInitialization, RequiresZeroInit, |
| 9523 | ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 9524 | } |
| 9525 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 9526 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 9527 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9528 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 9529 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 9530 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 9531 | MultiExprArg ExprArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9532 | bool HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 9533 | bool IsListInitialization, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9534 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 9535 | unsigned ConstructKind, |
| 9536 | SourceRange ParenRange) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9537 | MarkFunctionReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 9538 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 9539 | Constructor, Elidable, ExprArgs, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 9540 | HadMultipleCandidates, |
| 9541 | IsListInitialization, RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 9542 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 9543 | ParenRange)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 9544 | } |
| 9545 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 9546 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 9547 | if (VD->isInvalidDecl()) return; |
| 9548 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 9549 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 9550 | if (ClassDecl->isInvalidDecl()) return; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 9551 | if (ClassDecl->hasIrrelevantDestructor()) return; |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 9552 | if (ClassDecl->isDependentContext()) return; |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 9553 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 9554 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9555 | MarkFunctionReferenced(VD->getLocation(), Destructor); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 9556 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 9557 | PDiag(diag::err_access_dtor_var) |
| 9558 | << VD->getDeclName() |
| 9559 | << VD->getType()); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 9560 | DiagnoseUseOfDecl(Destructor, VD->getLocation()); |
Anders Carlsson | 2b32dad | 2011-03-24 01:01:41 +0000 | [diff] [blame] | 9561 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 9562 | if (!VD->hasGlobalStorage()) return; |
| 9563 | |
| 9564 | // Emit warning for non-trivial dtor in global scope (a real global, |
| 9565 | // class-static, function-static). |
| 9566 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 9567 | |
| 9568 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 9569 | if (!VD->isStaticLocal()) |
| 9570 | Diag(VD->getLocation(), diag::warn_global_destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9571 | } |
| 9572 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9573 | /// \brief Given a constructor and the set of arguments provided for the |
| 9574 | /// constructor, convert the arguments and add any required default arguments |
| 9575 | /// to form a proper call to this constructor. |
| 9576 | /// |
| 9577 | /// \returns true if an error occurred, false otherwise. |
| 9578 | bool |
| 9579 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 9580 | MultiExprArg ArgsPtr, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 9581 | SourceLocation Loc, |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 9582 | SmallVectorImpl<Expr*> &ConvertedArgs, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 9583 | bool AllowExplicit) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9584 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 9585 | unsigned NumArgs = ArgsPtr.size(); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9586 | Expr **Args = ArgsPtr.data(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9587 | |
| 9588 | const FunctionProtoType *Proto |
| 9589 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 9590 | assert(Proto && "Constructor without a prototype?"); |
| 9591 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9592 | |
| 9593 | // If too few arguments are available, we'll fill in the rest with defaults. |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9594 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9595 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9596 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9597 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9598 | |
| 9599 | VariadicCallType CallType = |
| 9600 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9601 | SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9602 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 9603 | Proto, 0, Args, NumArgs, AllArgs, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 9604 | CallType, AllowExplicit); |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 9605 | ConvertedArgs.append(AllArgs.begin(), AllArgs.end()); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 9606 | |
| 9607 | DiagnoseSentinelCalls(Constructor, Loc, AllArgs.data(), AllArgs.size()); |
| 9608 | |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 9609 | CheckConstructorCall(Constructor, |
| 9610 | llvm::makeArrayRef<const Expr *>(AllArgs.data(), |
| 9611 | AllArgs.size()), |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 9612 | Proto, Loc); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 9613 | |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9614 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 9615 | } |
| 9616 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9617 | static inline bool |
| 9618 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 9619 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 9620 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9621 | if (isa<NamespaceDecl>(DC)) { |
| 9622 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9623 | diag::err_operator_new_delete_declared_in_namespace) |
| 9624 | << FnDecl->getDeclName(); |
| 9625 | } |
| 9626 | |
| 9627 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9628 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9629 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9630 | diag::err_operator_new_delete_declared_static) |
| 9631 | << FnDecl->getDeclName(); |
| 9632 | } |
| 9633 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 9634 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9635 | } |
| 9636 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9637 | static inline bool |
| 9638 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 9639 | CanQualType ExpectedResultType, |
| 9640 | CanQualType ExpectedFirstParamType, |
| 9641 | unsigned DependentParamTypeDiag, |
| 9642 | unsigned InvalidParamTypeDiag) { |
| 9643 | QualType ResultType = |
| 9644 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 9645 | |
| 9646 | // Check that the result type is not dependent. |
| 9647 | if (ResultType->isDependentType()) |
| 9648 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9649 | diag::err_operator_new_delete_dependent_result_type) |
| 9650 | << FnDecl->getDeclName() << ExpectedResultType; |
| 9651 | |
| 9652 | // Check that the result type is what we expect. |
| 9653 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 9654 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9655 | diag::err_operator_new_delete_invalid_result_type) |
| 9656 | << FnDecl->getDeclName() << ExpectedResultType; |
| 9657 | |
| 9658 | // A function template must have at least 2 parameters. |
| 9659 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 9660 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9661 | diag::err_operator_new_delete_template_too_few_parameters) |
| 9662 | << FnDecl->getDeclName(); |
| 9663 | |
| 9664 | // The function decl must have at least 1 parameter. |
| 9665 | if (FnDecl->getNumParams() == 0) |
| 9666 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9667 | diag::err_operator_new_delete_too_few_parameters) |
| 9668 | << FnDecl->getDeclName(); |
| 9669 | |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 9670 | // Check the first parameter type is not dependent. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9671 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 9672 | if (FirstParamType->isDependentType()) |
| 9673 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 9674 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 9675 | |
| 9676 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 9677 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9678 | ExpectedFirstParamType) |
| 9679 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 9680 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 9681 | |
| 9682 | return false; |
| 9683 | } |
| 9684 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9685 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9686 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9687 | // C++ [basic.stc.dynamic.allocation]p1: |
| 9688 | // A program is ill-formed if an allocation function is declared in a |
| 9689 | // namespace scope other than global scope or declared static in global |
| 9690 | // scope. |
| 9691 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 9692 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9693 | |
| 9694 | CanQualType SizeTy = |
| 9695 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 9696 | |
| 9697 | // C++ [basic.stc.dynamic.allocation]p1: |
| 9698 | // The return type shall be void*. The first parameter shall have type |
| 9699 | // std::size_t. |
| 9700 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 9701 | SizeTy, |
| 9702 | diag::err_operator_new_dependent_param_type, |
| 9703 | diag::err_operator_new_param_type)) |
| 9704 | return true; |
| 9705 | |
| 9706 | // C++ [basic.stc.dynamic.allocation]p1: |
| 9707 | // The first parameter shall not have an associated default argument. |
| 9708 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 9709 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9710 | diag::err_operator_new_default_arg) |
| 9711 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 9712 | |
| 9713 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 9714 | } |
| 9715 | |
| 9716 | static bool |
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 9717 | CheckOperatorDeleteDeclaration(Sema &SemaRef, FunctionDecl *FnDecl) { |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9718 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 9719 | // A program is ill-formed if deallocation functions are declared in a |
| 9720 | // namespace scope other than global scope or declared static in global |
| 9721 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9722 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 9723 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9724 | |
| 9725 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 9726 | // Each deallocation function shall return void and its first parameter |
| 9727 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9728 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 9729 | SemaRef.Context.VoidPtrTy, |
| 9730 | diag::err_operator_delete_dependent_param_type, |
| 9731 | diag::err_operator_delete_param_type)) |
| 9732 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9733 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9734 | return false; |
| 9735 | } |
| 9736 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9737 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 9738 | /// of this overloaded operator is well-formed. If so, returns false; |
| 9739 | /// otherwise, emits appropriate diagnostics and returns true. |
| 9740 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9741 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9742 | "Expected an overloaded operator declaration"); |
| 9743 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9744 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 9745 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9746 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9747 | // The allocation and deallocation functions, operator new, |
| 9748 | // operator new[], operator delete and operator delete[], are |
| 9749 | // described completely in 3.7.3. The attributes and restrictions |
| 9750 | // found in the rest of this subclause do not apply to them unless |
| 9751 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 9752 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9753 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 9754 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 9755 | if (Op == OO_New || Op == OO_Array_New) |
| 9756 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9757 | |
| 9758 | // C++ [over.oper]p6: |
| 9759 | // An operator function shall either be a non-static member |
| 9760 | // function or be a non-member function and have at least one |
| 9761 | // parameter whose type is a class, a reference to a class, an |
| 9762 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9763 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 9764 | if (MethodDecl->isStatic()) |
| 9765 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9766 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9767 | } else { |
| 9768 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9769 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 9770 | ParamEnd = FnDecl->param_end(); |
| 9771 | Param != ParamEnd; ++Param) { |
| 9772 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 9773 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 9774 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9775 | ClassOrEnumParam = true; |
| 9776 | break; |
| 9777 | } |
| 9778 | } |
| 9779 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9780 | if (!ClassOrEnumParam) |
| 9781 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 9782 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9783 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9784 | } |
| 9785 | |
| 9786 | // C++ [over.oper]p8: |
| 9787 | // An operator function cannot have default arguments (8.3.6), |
| 9788 | // except where explicitly stated below. |
| 9789 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9790 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9791 | // (C++ [over.call]p1). |
| 9792 | if (Op != OO_Call) { |
| 9793 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 9794 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9795 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9796 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 9797 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9798 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9799 | } |
| 9800 | } |
| 9801 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9802 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 9803 | { false, false, false } |
| 9804 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 9805 | , { Unary, Binary, MemberOnly } |
| 9806 | #include "clang/Basic/OperatorKinds.def" |
| 9807 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9808 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9809 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 9810 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 9811 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9812 | |
| 9813 | // C++ [over.oper]p8: |
| 9814 | // [...] Operator functions cannot have more or fewer parameters |
| 9815 | // than the number required for the corresponding operator, as |
| 9816 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9817 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9818 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9819 | if (Op != OO_Call && |
| 9820 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 9821 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 9822 | (NumParams < 1) || (NumParams > 2))) { |
| 9823 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9824 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9825 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9826 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9827 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9828 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9829 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 9830 | assert(CanBeBinaryOperator && |
| 9831 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9832 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9833 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9834 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9835 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9836 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9837 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 9838 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9839 | // Overloaded operators other than operator() cannot be variadic. |
| 9840 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 9841 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 9842 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9843 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9844 | } |
| 9845 | |
| 9846 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9847 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 9848 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 9849 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9850 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9851 | } |
| 9852 | |
| 9853 | // C++ [over.inc]p1: |
| 9854 | // The user-defined function called operator++ implements the |
| 9855 | // prefix and postfix ++ operator. If this function is a member |
| 9856 | // function with no parameters, or a non-member function with one |
| 9857 | // parameter of class or enumeration type, it defines the prefix |
| 9858 | // increment operator ++ for objects of that type. If the function |
| 9859 | // is a member function with one parameter (which shall be of type |
| 9860 | // int) or a non-member function with two parameters (the second |
| 9861 | // of which shall be of type int), it defines the postfix |
| 9862 | // increment operator ++ for objects of that type. |
| 9863 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 9864 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 9865 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 9866 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9867 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 9868 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 9869 | if (!ParamIsInt) |
| 9870 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9871 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 9872 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9873 | } |
| 9874 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9875 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9876 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 9877 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9878 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 9879 | /// of this literal operator function is well-formed. If so, returns |
| 9880 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 9881 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
Richard Smith | e5658f0 | 2012-03-10 22:18:57 +0000 | [diff] [blame] | 9882 | if (isa<CXXMethodDecl>(FnDecl)) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9883 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 9884 | << FnDecl->getDeclName(); |
| 9885 | return true; |
| 9886 | } |
| 9887 | |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9888 | if (FnDecl->isExternC()) { |
| 9889 | Diag(FnDecl->getLocation(), diag::err_literal_operator_extern_c); |
| 9890 | return true; |
| 9891 | } |
| 9892 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9893 | bool Valid = false; |
| 9894 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 9895 | // This might be the definition of a literal operator template. |
| 9896 | FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate(); |
| 9897 | // This might be a specialization of a literal operator template. |
| 9898 | if (!TpDecl) |
| 9899 | TpDecl = FnDecl->getPrimaryTemplate(); |
| 9900 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9901 | // template <char...> type operator "" name() is the only valid template |
| 9902 | // signature, and the only valid signature with no parameters. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 9903 | if (TpDecl) { |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9904 | if (FnDecl->param_size() == 0) { |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9905 | // Must have only one template parameter |
| 9906 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 9907 | if (Params->size() == 1) { |
| 9908 | NonTypeTemplateParmDecl *PmDecl = |
Richard Smith | 5295b97 | 2012-08-03 21:14:57 +0000 | [diff] [blame] | 9909 | dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9910 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9911 | // The template parameter must be a char parameter pack. |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9912 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 9913 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 9914 | Valid = true; |
| 9915 | } |
| 9916 | } |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9917 | } else if (FnDecl->param_size()) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9918 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9919 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 9920 | |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9921 | QualType T = (*Param)->getType().getUnqualifiedType(); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9922 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 9923 | // unsigned long long int, long double, and any character type are allowed |
| 9924 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9925 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 9926 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 9927 | Context.hasSameType(T, Context.CharTy) || |
| 9928 | Context.hasSameType(T, Context.WCharTy) || |
| 9929 | Context.hasSameType(T, Context.Char16Ty) || |
| 9930 | Context.hasSameType(T, Context.Char32Ty)) { |
| 9931 | if (++Param == FnDecl->param_end()) |
| 9932 | Valid = true; |
| 9933 | goto FinishedParams; |
| 9934 | } |
| 9935 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 9936 | // Otherwise it must be a pointer to const; let's strip those qualifiers. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9937 | const PointerType *PT = T->getAs<PointerType>(); |
| 9938 | if (!PT) |
| 9939 | goto FinishedParams; |
| 9940 | T = PT->getPointeeType(); |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9941 | if (!T.isConstQualified() || T.isVolatileQualified()) |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9942 | goto FinishedParams; |
| 9943 | T = T.getUnqualifiedType(); |
| 9944 | |
| 9945 | // Move on to the second parameter; |
| 9946 | ++Param; |
| 9947 | |
| 9948 | // If there is no second parameter, the first must be a const char * |
| 9949 | if (Param == FnDecl->param_end()) { |
| 9950 | if (Context.hasSameType(T, Context.CharTy)) |
| 9951 | Valid = true; |
| 9952 | goto FinishedParams; |
| 9953 | } |
| 9954 | |
| 9955 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 9956 | // are allowed as the first parameter to a two-parameter function |
| 9957 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 9958 | Context.hasSameType(T, Context.WCharTy) || |
| 9959 | Context.hasSameType(T, Context.Char16Ty) || |
| 9960 | Context.hasSameType(T, Context.Char32Ty))) |
| 9961 | goto FinishedParams; |
| 9962 | |
| 9963 | // The second and final parameter must be an std::size_t |
| 9964 | T = (*Param)->getType().getUnqualifiedType(); |
| 9965 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 9966 | ++Param == FnDecl->param_end()) |
| 9967 | Valid = true; |
| 9968 | } |
| 9969 | |
| 9970 | // FIXME: This diagnostic is absolutely terrible. |
| 9971 | FinishedParams: |
| 9972 | if (!Valid) { |
| 9973 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 9974 | << FnDecl->getDeclName(); |
| 9975 | return true; |
| 9976 | } |
| 9977 | |
Richard Smith | a9e88b2 | 2012-03-09 08:16:22 +0000 | [diff] [blame] | 9978 | // A parameter-declaration-clause containing a default argument is not |
| 9979 | // equivalent to any of the permitted forms. |
| 9980 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 9981 | ParamEnd = FnDecl->param_end(); |
| 9982 | Param != ParamEnd; ++Param) { |
| 9983 | if ((*Param)->hasDefaultArg()) { |
| 9984 | Diag((*Param)->getDefaultArgRange().getBegin(), |
| 9985 | diag::err_literal_operator_default_argument) |
| 9986 | << (*Param)->getDefaultArgRange(); |
| 9987 | break; |
| 9988 | } |
| 9989 | } |
| 9990 | |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 9991 | StringRef LiteralName |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 9992 | = FnDecl->getDeclName().getCXXLiteralIdentifier()->getName(); |
| 9993 | if (LiteralName[0] != '_') { |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 9994 | // C++11 [usrlit.suffix]p1: |
| 9995 | // Literal suffix identifiers that do not start with an underscore |
| 9996 | // are reserved for future standardization. |
| 9997 | Diag(FnDecl->getLocation(), diag::warn_user_literal_reserved); |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 9998 | } |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 9999 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10000 | return false; |
| 10001 | } |
| 10002 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10003 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 10004 | /// linkage specification, including the language and (if present) |
| 10005 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 10006 | /// the location of the language string literal, which is provided |
| 10007 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 10008 | /// the '{' brace. Otherwise, this linkage specification does not |
| 10009 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 10010 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 10011 | SourceLocation LangLoc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10012 | StringRef Lang, |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 10013 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10014 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 10015 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10016 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 10017 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10018 | Language = LinkageSpecDecl::lang_cxx; |
| 10019 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10020 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10021 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10022 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10023 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10024 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10025 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10026 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10027 | ExternLoc, LangLoc, Language); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 10028 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10029 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10030 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10031 | } |
| 10032 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 10033 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10034 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 10035 | /// valid, it's the position of the closing '}' brace in a linkage |
| 10036 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10037 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 10038 | Decl *LinkageSpec, |
| 10039 | SourceLocation RBraceLoc) { |
| 10040 | if (LinkageSpec) { |
| 10041 | if (RBraceLoc.isValid()) { |
| 10042 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 10043 | LSDecl->setRBraceLoc(RBraceLoc); |
| 10044 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10045 | PopDeclContext(); |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 10046 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10047 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 10048 | } |
| 10049 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10050 | /// \brief Perform semantic analysis for the variable declaration that |
| 10051 | /// occurs within a C++ catch clause, returning the newly-created |
| 10052 | /// variable. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10053 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 10054 | TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10055 | SourceLocation StartLoc, |
| 10056 | SourceLocation Loc, |
| 10057 | IdentifierInfo *Name) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10058 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 10059 | QualType ExDeclType = TInfo->getType(); |
| 10060 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10061 | // Arrays and functions decay. |
| 10062 | if (ExDeclType->isArrayType()) |
| 10063 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 10064 | else if (ExDeclType->isFunctionType()) |
| 10065 | ExDeclType = Context.getPointerType(ExDeclType); |
| 10066 | |
| 10067 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 10068 | // The exception-declaration shall not denote a pointer or reference to an |
| 10069 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 10070 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10071 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 10072 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 10073 | Invalid = true; |
| 10074 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10075 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10076 | QualType BaseType = ExDeclType; |
| 10077 | int Mode = 0; // 0 for direct type, 1 for pointer, 2 for reference |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 10078 | unsigned DK = diag::err_catch_incomplete; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 10079 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10080 | BaseType = Ptr->getPointeeType(); |
| 10081 | Mode = 1; |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 10082 | DK = diag::err_catch_incomplete_ptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10083 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 10084 | // For the purpose of error recovery, we treat rvalue refs like lvalue refs. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10085 | BaseType = Ref->getPointeeType(); |
| 10086 | Mode = 2; |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 10087 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10088 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 10089 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 10090 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10091 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10092 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10093 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10094 | RequireNonAbstractType(Loc, ExDeclType, |
| 10095 | diag::err_abstract_type_in_decl, |
| 10096 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 10097 | Invalid = true; |
| 10098 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 10099 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 10100 | // of ObjC types, and no runtime supports catching ObjC types by value. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10101 | if (!Invalid && getLangOpts().ObjC1) { |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 10102 | QualType T = ExDeclType; |
| 10103 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 10104 | T = RT->getPointeeType(); |
| 10105 | |
| 10106 | if (T->isObjCObjectType()) { |
| 10107 | Diag(Loc, diag::err_objc_object_catch); |
| 10108 | Invalid = true; |
| 10109 | } else if (T->isObjCObjectPointerType()) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 10110 | // FIXME: should this be a test for macosx-fragile specifically? |
| 10111 | if (getLangOpts().ObjCRuntime.isFragile()) |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 10112 | Diag(Loc, diag::warn_objc_pointer_cxx_catch_fragile); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 10113 | } |
| 10114 | } |
| 10115 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10116 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
| 10117 | ExDeclType, TInfo, SC_None, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 10118 | ExDecl->setExceptionVariable(true); |
| 10119 | |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 10120 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10121 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(ExDecl)) |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 10122 | Invalid = true; |
| 10123 | |
Douglas Gregor | c41b878 | 2011-07-06 18:14:43 +0000 | [diff] [blame] | 10124 | if (!Invalid && !ExDeclType->isDependentType()) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 10125 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 10126 | // C++ [except.handle]p16: |
| 10127 | // The object declared in an exception-declaration or, if the |
| 10128 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 10129 | // copy-initialized (8.5) from the exception object. [...] |
| 10130 | // The object is destroyed when the handler exits, after the destruction |
| 10131 | // of any automatic objects initialized within the handler. |
| 10132 | // |
| 10133 | // We just pretend to initialize the object with itself, then make sure |
| 10134 | // it can be destroyed later. |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 10135 | QualType initType = ExDeclType; |
| 10136 | |
| 10137 | InitializedEntity entity = |
| 10138 | InitializedEntity::InitializeVariable(ExDecl); |
| 10139 | InitializationKind initKind = |
| 10140 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 10141 | |
| 10142 | Expr *opaqueValue = |
| 10143 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
| 10144 | InitializationSequence sequence(*this, entity, initKind, &opaqueValue, 1); |
| 10145 | ExprResult result = sequence.Perform(*this, entity, initKind, |
| 10146 | MultiExprArg(&opaqueValue, 1)); |
| 10147 | if (result.isInvalid()) |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 10148 | Invalid = true; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 10149 | else { |
| 10150 | // If the constructor used was non-trivial, set this as the |
| 10151 | // "initializer". |
| 10152 | CXXConstructExpr *construct = cast<CXXConstructExpr>(result.take()); |
| 10153 | if (!construct->getConstructor()->isTrivial()) { |
| 10154 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 10155 | ExDecl->setInit(init); |
| 10156 | } |
| 10157 | |
| 10158 | // And make sure it's destructable. |
| 10159 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 10160 | } |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 10161 | } |
| 10162 | } |
| 10163 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10164 | if (Invalid) |
| 10165 | ExDecl->setInvalidDecl(); |
| 10166 | |
| 10167 | return ExDecl; |
| 10168 | } |
| 10169 | |
| 10170 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 10171 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10172 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 10173 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 10174 | bool Invalid = D.isInvalidType(); |
| 10175 | |
| 10176 | // Check for unexpanded parameter packs. |
| 10177 | if (TInfo && DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 10178 | UPPC_ExceptionType)) { |
| 10179 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 10180 | D.getIdentifierLoc()); |
| 10181 | Invalid = true; |
| 10182 | } |
| 10183 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10184 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 10185 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 10186 | LookupOrdinaryName, |
| 10187 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10188 | // The scope should be freshly made just for us. There is just no way |
| 10189 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10190 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10191 | if (PrevDecl->isTemplateParameter()) { |
| 10192 | // Maybe we will complain about the shadowed template parameter. |
| 10193 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 10194 | PrevDecl = 0; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10195 | } |
| 10196 | } |
| 10197 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 10198 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10199 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 10200 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 10201 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10202 | } |
| 10203 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 10204 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10205 | D.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10206 | D.getIdentifierLoc(), |
| 10207 | D.getIdentifier()); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 10208 | if (Invalid) |
| 10209 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10210 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10211 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10212 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10213 | PushOnScopeChains(ExDecl, S); |
| 10214 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 10215 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10216 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 10217 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10218 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10219 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 10220 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10221 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10222 | Expr *AssertExpr, |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10223 | Expr *AssertMessageExpr, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10224 | SourceLocation RParenLoc) { |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10225 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 10226 | |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10227 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 10228 | return 0; |
| 10229 | |
| 10230 | return BuildStaticAssertDeclaration(StaticAssertLoc, AssertExpr, |
| 10231 | AssertMessage, RParenLoc, false); |
| 10232 | } |
| 10233 | |
| 10234 | Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
| 10235 | Expr *AssertExpr, |
| 10236 | StringLiteral *AssertMessage, |
| 10237 | SourceLocation RParenLoc, |
| 10238 | bool Failed) { |
| 10239 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent() && |
| 10240 | !Failed) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10241 | // In a static_assert-declaration, the constant-expression shall be a |
| 10242 | // constant expression that can be contextually converted to bool. |
| 10243 | ExprResult Converted = PerformContextuallyConvertToBool(AssertExpr); |
| 10244 | if (Converted.isInvalid()) |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10245 | Failed = true; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10246 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10247 | llvm::APSInt Cond; |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10248 | if (!Failed && VerifyIntegerConstantExpression(Converted.get(), &Cond, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10249 | diag::err_static_assert_expression_is_not_constant, |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10250 | /*AllowFold=*/false).isInvalid()) |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10251 | Failed = true; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 10252 | |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10253 | if (!Failed && !Cond) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10254 | SmallString<256> MsgBuffer; |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 10255 | llvm::raw_svector_ostream Msg(MsgBuffer); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 10256 | AssertMessage->printPretty(Msg, 0, getPrintingPolicy()); |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10257 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 10258 | << Msg.str() << AssertExpr->getSourceRange(); |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10259 | Failed = true; |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 10260 | } |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 10261 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10262 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10263 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10264 | AssertExpr, AssertMessage, RParenLoc, |
| 10265 | Failed); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10266 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 10267 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10268 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 10269 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 10270 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10271 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 10272 | /// |
| 10273 | /// \returns A friend declaration that. |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10274 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation LocStart, |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 10275 | SourceLocation FriendLoc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10276 | TypeSourceInfo *TSInfo) { |
| 10277 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 10278 | |
| 10279 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 10280 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10281 | |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10282 | // C++03 [class.friend]p2: |
| 10283 | // An elaborated-type-specifier shall be used in a friend declaration |
| 10284 | // for a class.* |
| 10285 | // |
| 10286 | // * The class-key of the elaborated-type-specifier is required. |
| 10287 | if (!ActiveTemplateInstantiations.empty()) { |
| 10288 | // Do not complain about the form of friend template types during |
| 10289 | // template instantiation; we will already have complained when the |
| 10290 | // template was declared. |
| 10291 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 10292 | // If we evaluated the type to a record type, suggest putting |
| 10293 | // a tag in front. |
| 10294 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 10295 | RecordDecl *RD = RT->getDecl(); |
| 10296 | |
| 10297 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 10298 | |
| 10299 | Diag(TypeRange.getBegin(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10300 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10301 | diag::warn_cxx98_compat_unelaborated_friend_type : |
| 10302 | diag::ext_unelaborated_friend_type) |
| 10303 | << (unsigned) RD->getTagKind() |
| 10304 | << T |
| 10305 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 10306 | InsertionText); |
| 10307 | } else { |
| 10308 | Diag(FriendLoc, |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10309 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10310 | diag::warn_cxx98_compat_nonclass_type_friend : |
| 10311 | diag::ext_nonclass_type_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10312 | << T |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10313 | << TypeRange; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10314 | } |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10315 | } else if (T->getAs<EnumType>()) { |
| 10316 | Diag(FriendLoc, |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10317 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10318 | diag::warn_cxx98_compat_enum_friend : |
| 10319 | diag::ext_enum_friend) |
| 10320 | << T |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10321 | << TypeRange; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10322 | } |
| 10323 | |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10324 | // C++11 [class.friend]p3: |
| 10325 | // A friend declaration that does not declare a function shall have one |
| 10326 | // of the following forms: |
| 10327 | // friend elaborated-type-specifier ; |
| 10328 | // friend simple-type-specifier ; |
| 10329 | // friend typename-specifier ; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10330 | if (getLangOpts().CPlusPlus11 && LocStart != FriendLoc) |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10331 | Diag(FriendLoc, diag::err_friend_not_first_in_declaration) << T; |
| 10332 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 10333 | // If the type specifier in a friend declaration designates a (possibly |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10334 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 10335 | // the friend declaration is ignored. |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10336 | return FriendDecl::Create(Context, CurContext, LocStart, TSInfo, FriendLoc); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10337 | } |
| 10338 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10339 | /// Handle a friend tag declaration where the scope specifier was |
| 10340 | /// templated. |
| 10341 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 10342 | unsigned TagSpec, SourceLocation TagLoc, |
| 10343 | CXXScopeSpec &SS, |
| 10344 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 10345 | AttributeList *Attr, |
| 10346 | MultiTemplateParamsArg TempParamLists) { |
| 10347 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 10348 | |
| 10349 | bool isExplicitSpecialization = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10350 | bool Invalid = false; |
| 10351 | |
| 10352 | if (TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 10353 | = MatchTemplateParametersToScopeSpecifier(TagLoc, NameLoc, SS, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10354 | TempParamLists.data(), |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10355 | TempParamLists.size(), |
| 10356 | /*friend*/ true, |
| 10357 | isExplicitSpecialization, |
| 10358 | Invalid)) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10359 | if (TemplateParams->size() > 0) { |
| 10360 | // This is a declaration of a class template. |
| 10361 | if (Invalid) |
| 10362 | return 0; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 10363 | |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 10364 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 10365 | SS, Name, NameLoc, Attr, |
| 10366 | TemplateParams, AS_public, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 10367 | /*ModulePrivateLoc=*/SourceLocation(), |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 10368 | TempParamLists.size() - 1, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10369 | TempParamLists.data()).take(); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10370 | } else { |
| 10371 | // The "template<>" header is extraneous. |
| 10372 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 10373 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 10374 | isExplicitSpecialization = true; |
| 10375 | } |
| 10376 | } |
| 10377 | |
| 10378 | if (Invalid) return 0; |
| 10379 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10380 | bool isAllExplicitSpecializations = true; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 10381 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10382 | if (TempParamLists[I]->size()) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10383 | isAllExplicitSpecializations = false; |
| 10384 | break; |
| 10385 | } |
| 10386 | } |
| 10387 | |
| 10388 | // FIXME: don't ignore attributes. |
| 10389 | |
| 10390 | // If it's explicit specializations all the way down, just forget |
| 10391 | // about the template header and build an appropriate non-templated |
| 10392 | // friend. TODO: for source fidelity, remember the headers. |
| 10393 | if (isAllExplicitSpecializations) { |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 10394 | if (SS.isEmpty()) { |
| 10395 | bool Owned = false; |
| 10396 | bool IsDependent = false; |
| 10397 | return ActOnTag(S, TagSpec, TUK_Friend, TagLoc, SS, Name, NameLoc, |
| 10398 | Attr, AS_public, |
| 10399 | /*ModulePrivateLoc=*/SourceLocation(), |
| 10400 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 10401 | /*ScopedEnumKWLoc=*/SourceLocation(), |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 10402 | /*ScopedEnumUsesClassTag=*/false, |
| 10403 | /*UnderlyingType=*/TypeResult()); |
| 10404 | } |
| 10405 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 10406 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10407 | ElaboratedTypeKeyword Keyword |
| 10408 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 10409 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 10410 | *Name, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10411 | if (T.isNull()) |
| 10412 | return 0; |
| 10413 | |
| 10414 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 10415 | if (isa<DependentNameType>(T)) { |
| 10416 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 10417 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 10418 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10419 | TL.setNameLoc(NameLoc); |
| 10420 | } else { |
| 10421 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 10422 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 10423 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10424 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(NameLoc); |
| 10425 | } |
| 10426 | |
| 10427 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 10428 | TSI, FriendLoc); |
| 10429 | Friend->setAccess(AS_public); |
| 10430 | CurContext->addDecl(Friend); |
| 10431 | return Friend; |
| 10432 | } |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 10433 | |
| 10434 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 10435 | |
| 10436 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10437 | |
| 10438 | // Handle the case of a templated-scope friend class. e.g. |
| 10439 | // template <class T> class A<T>::B; |
| 10440 | // FIXME: we don't support these right now. |
| 10441 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 10442 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 10443 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 10444 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 10445 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 10446 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10447 | TL.setNameLoc(NameLoc); |
| 10448 | |
| 10449 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 10450 | TSI, FriendLoc); |
| 10451 | Friend->setAccess(AS_public); |
| 10452 | Friend->setUnsupportedFriend(true); |
| 10453 | CurContext->addDecl(Friend); |
| 10454 | return Friend; |
| 10455 | } |
| 10456 | |
| 10457 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10458 | /// Handle a friend type declaration. This works in tandem with |
| 10459 | /// ActOnTag. |
| 10460 | /// |
| 10461 | /// Notes on friend class templates: |
| 10462 | /// |
| 10463 | /// We generally treat friend class declarations as if they were |
| 10464 | /// declaring a class. So, for example, the elaborated type specifier |
| 10465 | /// in a friend declaration is required to obey the restrictions of a |
| 10466 | /// class-head (i.e. no typedefs in the scope chain), template |
| 10467 | /// parameters are required to match up with simple template-ids, &c. |
| 10468 | /// However, unlike when declaring a template specialization, it's |
| 10469 | /// okay to refer to a template specialization without an empty |
| 10470 | /// template parameter declaration, e.g. |
| 10471 | /// friend class A<T>::B<unsigned>; |
| 10472 | /// We permit this as a special case; if there are any template |
| 10473 | /// parameters present at all, require proper matching, i.e. |
James Dennett | ef2b5b3 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 10474 | /// template <> template \<class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10475 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 10476 | MultiTemplateParamsArg TempParams) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10477 | SourceLocation Loc = DS.getLocStart(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10478 | |
| 10479 | assert(DS.isFriendSpecified()); |
| 10480 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 10481 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10482 | // Try to convert the decl specifier to a type. This works for |
| 10483 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 10484 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 10485 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 10486 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 10487 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 10488 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10489 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10490 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 10491 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 10492 | return 0; |
| 10493 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10494 | // This is definitely an error in C++98. It's probably meant to |
| 10495 | // be forbidden in C++0x, too, but the specification is just |
| 10496 | // poorly written. |
| 10497 | // |
| 10498 | // The problem is with declarations like the following: |
| 10499 | // template <T> friend A<T>::foo; |
| 10500 | // where deciding whether a class C is a friend or not now hinges |
| 10501 | // on whether there exists an instantiation of A that causes |
| 10502 | // 'foo' to equal C. There are restrictions on class-heads |
| 10503 | // (which we declare (by fiat) elaborated friend declarations to |
| 10504 | // be) that makes this tractable. |
| 10505 | // |
| 10506 | // FIXME: handle "template <> friend class A<T>;", which |
| 10507 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 10508 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10509 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 10510 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10511 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10512 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10513 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10514 | // C++98 [class.friend]p1: A friend of a class is a function |
| 10515 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 10516 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 10517 | // deadline. It's also a very silly restriction that seriously |
| 10518 | // affects inner classes and which nobody else seems to implement; |
| 10519 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 10520 | // |
| 10521 | // But note that we could warn about it: it's always useless to |
| 10522 | // friend one of your own members (it's not, however, worthless to |
| 10523 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10524 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10525 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10526 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10527 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10528 | NumTempParamLists, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10529 | TempParams.data(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 10530 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10531 | DS.getFriendSpecLoc()); |
| 10532 | else |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 10533 | D = CheckFriendTypeDecl(Loc, DS.getFriendSpecLoc(), TSI); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10534 | |
| 10535 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10536 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10537 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10538 | D->setAccess(AS_public); |
| 10539 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10540 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10541 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10542 | } |
| 10543 | |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 10544 | NamedDecl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, |
| 10545 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10546 | const DeclSpec &DS = D.getDeclSpec(); |
| 10547 | |
| 10548 | assert(DS.isFriendSpecified()); |
| 10549 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 10550 | |
| 10551 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 10552 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10553 | |
| 10554 | // C++ [class.friend]p1 |
| 10555 | // A friend of a class is a function or class.... |
| 10556 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10557 | // It *doesn't* see through dependent types, which is correct |
| 10558 | // according to [temp.arg.type]p3: |
| 10559 | // If a declaration acquires a function type through a |
| 10560 | // type dependent on a template-parameter and this causes |
| 10561 | // a declaration that does not use the syntactic form of a |
| 10562 | // function declarator to have a function type, the program |
| 10563 | // is ill-formed. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10564 | if (!TInfo->getType()->isFunctionType()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10565 | Diag(Loc, diag::err_unexpected_friend); |
| 10566 | |
| 10567 | // It might be worthwhile to try to recover by creating an |
| 10568 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10569 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10570 | } |
| 10571 | |
| 10572 | // C++ [namespace.memdef]p3 |
| 10573 | // - If a friend declaration in a non-local class first declares a |
| 10574 | // class or function, the friend class or function is a member |
| 10575 | // of the innermost enclosing namespace. |
| 10576 | // - The name of the friend is not found by simple name lookup |
| 10577 | // until a matching declaration is provided in that namespace |
| 10578 | // scope (either before or after the class declaration granting |
| 10579 | // friendship). |
| 10580 | // - If a friend function is called, its name may be found by the |
| 10581 | // name lookup that considers functions from namespaces and |
| 10582 | // classes associated with the types of the function arguments. |
| 10583 | // - When looking for a prior declaration of a class or a function |
| 10584 | // declared as a friend, scopes outside the innermost enclosing |
| 10585 | // namespace scope are not considered. |
| 10586 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10587 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10588 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 10589 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10590 | assert(Name); |
| 10591 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 10592 | // Check for unexpanded parameter packs. |
| 10593 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 10594 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 10595 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 10596 | return 0; |
| 10597 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10598 | // The context we found the declaration in, or in which we should |
| 10599 | // create the declaration. |
| 10600 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 10601 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10602 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10603 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10604 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10605 | // FIXME: there are different rules in local classes |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10606 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10607 | // There are four cases here. |
| 10608 | // - There's no scope specifier, in which case we just go to the |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10609 | // appropriate scope and look for a function or function template |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10610 | // there as appropriate. |
| 10611 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 10612 | if (SS.isInvalid() || !SS.isSet()) { |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10613 | // C++0x [namespace.memdef]p3: |
| 10614 | // If the name in a friend declaration is neither qualified nor |
| 10615 | // a template-id and the declaration is a function or an |
| 10616 | // elaborated-type-specifier, the lookup to determine whether |
| 10617 | // the entity has been previously declared shall not consider |
| 10618 | // any scopes outside the innermost enclosing namespace. |
| 10619 | // C++0x [class.friend]p11: |
| 10620 | // If a friend declaration appears in a local class and the name |
| 10621 | // specified is an unqualified name, a prior declaration is |
| 10622 | // looked up without considering scopes that are outside the |
| 10623 | // innermost enclosing non-class scope. For a friend function |
| 10624 | // declaration, if there is no prior declaration, the program is |
| 10625 | // ill-formed. |
| 10626 | bool isLocal = cast<CXXRecordDecl>(CurContext)->isLocalClass(); |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 10627 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10628 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10629 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10630 | DC = CurContext; |
| 10631 | while (true) { |
| 10632 | // Skip class contexts. If someone can cite chapter and verse |
| 10633 | // for this behavior, that would be nice --- it's what GCC and |
| 10634 | // EDG do, and it seems like a reasonable intent, but the spec |
| 10635 | // really only says that checks for unqualified existing |
| 10636 | // declarations should stop at the nearest enclosing namespace, |
| 10637 | // not that they should only consider the nearest enclosing |
| 10638 | // namespace. |
Nick Lewycky | 9c6fde5 | 2012-03-16 19:51:19 +0000 | [diff] [blame] | 10639 | while (DC->isRecord() || DC->isTransparentContext()) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10640 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10641 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10642 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10643 | |
| 10644 | // TODO: decide what we think about using declarations. |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10645 | if (isLocal || !Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10646 | break; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10647 | |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 10648 | if (isTemplateId) { |
| 10649 | if (isa<TranslationUnitDecl>(DC)) break; |
| 10650 | } else { |
| 10651 | if (DC->isFileContext()) break; |
| 10652 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10653 | DC = DC->getParent(); |
| 10654 | } |
| 10655 | |
| 10656 | // C++ [class.friend]p1: A friend of a class is a function or |
| 10657 | // class that is not a member of the class . . . |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10658 | // C++11 changes this for both friend types and functions. |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 10659 | // Most C++ 98 compilers do seem to give an error here, so |
| 10660 | // we do, too. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10661 | if (!Previous.empty() && DC->Equals(CurContext)) |
| 10662 | Diag(DS.getFriendSpecLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10663 | getLangOpts().CPlusPlus11 ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10664 | diag::warn_cxx98_compat_friend_is_member : |
| 10665 | diag::err_friend_is_member); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10666 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 10667 | DCScope = getScopeForDeclContext(S, DC); |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 10668 | |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10669 | // C++ [class.friend]p6: |
| 10670 | // A function can be defined in a friend declaration of a class if and |
| 10671 | // only if the class is a non-local class (9.8), the function name is |
| 10672 | // unqualified, and the function has namespace scope. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10673 | if (isLocal && D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10674 | Diag(NameInfo.getBeginLoc(), diag::err_friend_def_in_local_class); |
| 10675 | } |
| 10676 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10677 | // - There's a non-dependent scope specifier, in which case we |
| 10678 | // compute it and do a previous lookup there for a function |
| 10679 | // or function template. |
| 10680 | } else if (!SS.getScopeRep()->isDependent()) { |
| 10681 | DC = computeDeclContext(SS); |
| 10682 | if (!DC) return 0; |
| 10683 | |
| 10684 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 10685 | |
| 10686 | LookupQualifiedName(Previous, DC); |
| 10687 | |
| 10688 | // Ignore things found implicitly in the wrong scope. |
| 10689 | // TODO: better diagnostics for this case. Suggesting the right |
| 10690 | // qualified scope would be nice... |
| 10691 | LookupResult::Filter F = Previous.makeFilter(); |
| 10692 | while (F.hasNext()) { |
| 10693 | NamedDecl *D = F.next(); |
| 10694 | if (!DC->InEnclosingNamespaceSetOf( |
| 10695 | D->getDeclContext()->getRedeclContext())) |
| 10696 | F.erase(); |
| 10697 | } |
| 10698 | F.done(); |
| 10699 | |
| 10700 | if (Previous.empty()) { |
| 10701 | D.setInvalidType(); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10702 | Diag(Loc, diag::err_qualified_friend_not_found) |
| 10703 | << Name << TInfo->getType(); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10704 | return 0; |
| 10705 | } |
| 10706 | |
| 10707 | // C++ [class.friend]p1: A friend of a class is a function or |
| 10708 | // class that is not a member of the class . . . |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10709 | if (DC->Equals(CurContext)) |
| 10710 | Diag(DS.getFriendSpecLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10711 | getLangOpts().CPlusPlus11 ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10712 | diag::warn_cxx98_compat_friend_is_member : |
| 10713 | diag::err_friend_is_member); |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10714 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10715 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10716 | // C++ [class.friend]p6: |
| 10717 | // A function can be defined in a friend declaration of a class if and |
| 10718 | // only if the class is a non-local class (9.8), the function name is |
| 10719 | // unqualified, and the function has namespace scope. |
| 10720 | SemaDiagnosticBuilder DB |
| 10721 | = Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def); |
| 10722 | |
| 10723 | DB << SS.getScopeRep(); |
| 10724 | if (DC->isFileContext()) |
| 10725 | DB << FixItHint::CreateRemoval(SS.getRange()); |
| 10726 | SS.clear(); |
| 10727 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10728 | |
| 10729 | // - There's a scope specifier that does not match any template |
| 10730 | // parameter lists, in which case we use some arbitrary context, |
| 10731 | // create a method or method template, and wait for instantiation. |
| 10732 | // - There's a scope specifier that does match some template |
| 10733 | // parameter lists, which we don't handle right now. |
| 10734 | } else { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10735 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10736 | // C++ [class.friend]p6: |
| 10737 | // A function can be defined in a friend declaration of a class if and |
| 10738 | // only if the class is a non-local class (9.8), the function name is |
| 10739 | // unqualified, and the function has namespace scope. |
| 10740 | Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def) |
| 10741 | << SS.getScopeRep(); |
| 10742 | } |
| 10743 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10744 | DC = CurContext; |
| 10745 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10746 | } |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10747 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10748 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10749 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 10750 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 10751 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 10752 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10753 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 10754 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 10755 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10756 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10757 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10758 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10759 | |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 10760 | // FIXME: This is an egregious hack to cope with cases where the scope stack |
| 10761 | // does not contain the declaration context, i.e., in an out-of-line |
| 10762 | // definition of a class. |
| 10763 | Scope FakeDCScope(S, Scope::DeclScope, Diags); |
| 10764 | if (!DCScope) { |
| 10765 | FakeDCScope.setEntity(DC); |
| 10766 | DCScope = &FakeDCScope; |
| 10767 | } |
| 10768 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 10769 | bool AddToScope = true; |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10770 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, TInfo, Previous, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10771 | TemplateParams, AddToScope); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10772 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10773 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10774 | assert(ND->getDeclContext() == DC); |
| 10775 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 10776 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10777 | // Add the function declaration to the appropriate lookup tables, |
| 10778 | // adjusting the redeclarations list as necessary. We don't |
| 10779 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10780 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10781 | // Also update the scope-based lookup if the target context's |
| 10782 | // lookup context is in lexical scope. |
| 10783 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10784 | DC = DC->getRedeclContext(); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 10785 | DC->makeDeclVisibleInContext(ND); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10786 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10787 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10788 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10789 | |
| 10790 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10791 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10792 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 10793 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10794 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10795 | |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 10796 | if (ND->isInvalidDecl()) { |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10797 | FrD->setInvalidDecl(); |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 10798 | } else { |
| 10799 | if (DC->isRecord()) CheckFriendAccess(ND); |
| 10800 | |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 10801 | FunctionDecl *FD; |
| 10802 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 10803 | FD = FTD->getTemplatedDecl(); |
| 10804 | else |
| 10805 | FD = cast<FunctionDecl>(ND); |
| 10806 | |
| 10807 | // Mark templated-scope function declarations as unsupported. |
| 10808 | if (FD->getNumTemplateParameterLists()) |
| 10809 | FrD->setUnsupportedFriend(true); |
| 10810 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10811 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10812 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 10813 | } |
| 10814 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10815 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 10816 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10817 | |
Aaron Ballman | afb7ce3 | 2013-01-16 23:39:10 +0000 | [diff] [blame^] | 10818 | FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(Dcl); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 10819 | if (!Fn) { |
| 10820 | Diag(DelLoc, diag::err_deleted_non_function); |
| 10821 | return; |
| 10822 | } |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 10823 | if (const FunctionDecl *Prev = Fn->getPreviousDecl()) { |
David Blaikie | d9cf826 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 10824 | // Don't consider the implicit declaration we generate for explicit |
| 10825 | // specializations. FIXME: Do not generate these implicit declarations. |
David Blaikie | 619ee6a | 2012-06-29 18:00:25 +0000 | [diff] [blame] | 10826 | if ((Prev->getTemplateSpecializationKind() != TSK_ExplicitSpecialization |
| 10827 | || Prev->getPreviousDecl()) && !Prev->isDefined()) { |
David Blaikie | d9cf826 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 10828 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 10829 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 10830 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 10831 | // If the declaration wasn't the first, we delete the function anyway for |
| 10832 | // recovery. |
| 10833 | } |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 10834 | Fn->setDeletedAsWritten(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 10835 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 10836 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10837 | void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) { |
Aaron Ballman | afb7ce3 | 2013-01-16 23:39:10 +0000 | [diff] [blame^] | 10838 | CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Dcl); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10839 | |
| 10840 | if (MD) { |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 10841 | if (MD->getParent()->isDependentType()) { |
| 10842 | MD->setDefaulted(); |
| 10843 | MD->setExplicitlyDefaulted(); |
| 10844 | return; |
| 10845 | } |
| 10846 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10847 | CXXSpecialMember Member = getSpecialMember(MD); |
| 10848 | if (Member == CXXInvalid) { |
| 10849 | Diag(DefaultLoc, diag::err_default_special_members); |
| 10850 | return; |
| 10851 | } |
| 10852 | |
| 10853 | MD->setDefaulted(); |
| 10854 | MD->setExplicitlyDefaulted(); |
| 10855 | |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 10856 | // If this definition appears within the record, do the checking when |
| 10857 | // the record is complete. |
| 10858 | const FunctionDecl *Primary = MD; |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 10859 | if (const FunctionDecl *Pattern = MD->getTemplateInstantiationPattern()) |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 10860 | // Find the uninstantiated declaration that actually had the '= default' |
| 10861 | // on it. |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 10862 | Pattern->isDefined(Primary); |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 10863 | |
| 10864 | if (Primary == Primary->getCanonicalDecl()) |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10865 | return; |
| 10866 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10867 | CheckExplicitlyDefaultedSpecialMember(MD); |
| 10868 | |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 10869 | // The exception specification is needed because we are defining the |
| 10870 | // function. |
| 10871 | ResolveExceptionSpec(DefaultLoc, |
| 10872 | MD->getType()->castAs<FunctionProtoType>()); |
| 10873 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10874 | switch (Member) { |
| 10875 | case CXXDefaultConstructor: { |
| 10876 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10877 | if (!CD->isInvalidDecl()) |
| 10878 | DefineImplicitDefaultConstructor(DefaultLoc, CD); |
| 10879 | break; |
| 10880 | } |
| 10881 | |
| 10882 | case CXXCopyConstructor: { |
| 10883 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10884 | if (!CD->isInvalidDecl()) |
| 10885 | DefineImplicitCopyConstructor(DefaultLoc, CD); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10886 | break; |
| 10887 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10888 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 10889 | case CXXCopyAssignment: { |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 10890 | if (!MD->isInvalidDecl()) |
| 10891 | DefineImplicitCopyAssignment(DefaultLoc, MD); |
| 10892 | break; |
| 10893 | } |
| 10894 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10895 | case CXXDestructor: { |
| 10896 | CXXDestructorDecl *DD = cast<CXXDestructorDecl>(MD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10897 | if (!DD->isInvalidDecl()) |
| 10898 | DefineImplicitDestructor(DefaultLoc, DD); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10899 | break; |
| 10900 | } |
| 10901 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10902 | case CXXMoveConstructor: { |
| 10903 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10904 | if (!CD->isInvalidDecl()) |
| 10905 | DefineImplicitMoveConstructor(DefaultLoc, CD); |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 10906 | break; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10907 | } |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 10908 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10909 | case CXXMoveAssignment: { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10910 | if (!MD->isInvalidDecl()) |
| 10911 | DefineImplicitMoveAssignment(DefaultLoc, MD); |
| 10912 | break; |
| 10913 | } |
| 10914 | |
| 10915 | case CXXInvalid: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10916 | llvm_unreachable("Invalid special member."); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10917 | } |
| 10918 | } else { |
| 10919 | Diag(DefaultLoc, diag::err_default_special_members); |
| 10920 | } |
| 10921 | } |
| 10922 | |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 10923 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 10924 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 10925 | Stmt *SubStmt = *CI; |
| 10926 | if (!SubStmt) |
| 10927 | continue; |
| 10928 | if (isa<ReturnStmt>(SubStmt)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10929 | Self.Diag(SubStmt->getLocStart(), |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 10930 | diag::err_return_in_constructor_handler); |
| 10931 | if (!isa<Expr>(SubStmt)) |
| 10932 | SearchForReturnInStmt(Self, SubStmt); |
| 10933 | } |
| 10934 | } |
| 10935 | |
| 10936 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 10937 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 10938 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 10939 | SearchForReturnInStmt(*this, Handler); |
| 10940 | } |
| 10941 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10942 | |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 10943 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New,
|
| 10944 | const CXXMethodDecl *Old) { |
| 10945 | const FunctionType *NewFT = New->getType()->getAs<FunctionType>(); |
| 10946 | const FunctionType *OldFT = Old->getType()->getAs<FunctionType>(); |
| 10947 | |
| 10948 | CallingConv NewCC = NewFT->getCallConv(), OldCC = OldFT->getCallConv(); |
| 10949 | |
| 10950 | // If the calling conventions match, everything is fine |
| 10951 | if (NewCC == OldCC) |
| 10952 | return false; |
| 10953 | |
| 10954 | // If either of the calling conventions are set to "default", we need to pick |
| 10955 | // something more sensible based on the target. This supports code where the |
| 10956 | // one method explicitly sets thiscall, and another has no explicit calling |
| 10957 | // convention. |
| 10958 | CallingConv Default = |
| 10959 | Context.getTargetInfo().getDefaultCallingConv(TargetInfo::CCMT_Member); |
| 10960 | if (NewCC == CC_Default) |
| 10961 | NewCC = Default; |
| 10962 | if (OldCC == CC_Default) |
| 10963 | OldCC = Default; |
| 10964 | |
| 10965 | // If the calling conventions still don't match, then report the error |
| 10966 | if (NewCC != OldCC) { |
| 10967 | Diag(New->getLocation(),
|
| 10968 | diag::err_conflicting_overriding_cc_attributes)
|
| 10969 | << New->getDeclName() << New->getType() << Old->getType();
|
| 10970 | Diag(Old->getLocation(), diag::note_overridden_virtual_function);
|
| 10971 | return true;
|
| 10972 | } |
| 10973 | |
| 10974 | return false; |
| 10975 | } |
| 10976 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10977 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10978 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 10979 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 10980 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10981 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 10982 | if (Context.hasSameType(NewTy, OldTy) || |
| 10983 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10984 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10985 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10986 | // Check if the return types are covariant |
| 10987 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10988 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10989 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 10990 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 10991 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10992 | NewClassTy = NewPT->getPointeeType(); |
| 10993 | OldClassTy = OldPT->getPointeeType(); |
| 10994 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 10995 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 10996 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 10997 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 10998 | NewClassTy = NewRT->getPointeeType(); |
| 10999 | OldClassTy = OldRT->getPointeeType(); |
| 11000 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11001 | } |
| 11002 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11003 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11004 | // The return types aren't either both pointers or references to a class type. |
| 11005 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11006 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11007 | diag::err_different_return_type_for_overriding_virtual_function) |
| 11008 | << New->getDeclName() << NewTy << OldTy; |
| 11009 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11010 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11011 | return true; |
| 11012 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 11013 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 11014 | // C++ [class.virtual]p6: |
| 11015 | // If the return type of D::f differs from the return type of B::f, the |
| 11016 | // class type in the return type of D::f shall be complete at the point of |
| 11017 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 11018 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 11019 | if (!RT->isBeingDefined() && |
| 11020 | RequireCompleteType(New->getLocation(), NewClassTy, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11021 | diag::err_covariant_return_incomplete, |
| 11022 | New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 11023 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 11024 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 11025 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 11026 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11027 | // Check if the new class derives from the old class. |
| 11028 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 11029 | Diag(New->getLocation(), |
| 11030 | diag::err_covariant_return_not_derived) |
| 11031 | << New->getDeclName() << NewTy << OldTy; |
| 11032 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 11033 | return true; |
| 11034 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11035 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11036 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 11037 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 11038 | diag::err_covariant_return_inaccessible_base, |
| 11039 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 11040 | // FIXME: Should this point to the return type? |
| 11041 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 11042 | // FIXME: this note won't trigger for delayed access control |
| 11043 | // diagnostics, and it's impossible to get an undelayed error |
| 11044 | // here from access control during the original parse because |
| 11045 | // the ParsingDeclSpec/ParsingDeclarator are still in scope. |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11046 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 11047 | return true; |
| 11048 | } |
| 11049 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11050 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11051 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 11052 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11053 | Diag(New->getLocation(), |
| 11054 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 11055 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11056 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 11057 | return true; |
| 11058 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11059 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11060 | |
| 11061 | // The new class type must have the same or less qualifiers as the old type. |
| 11062 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 11063 | Diag(New->getLocation(), |
| 11064 | diag::err_covariant_return_type_class_type_more_qualified) |
| 11065 | << New->getDeclName() << NewTy << OldTy; |
| 11066 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 11067 | return true; |
| 11068 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11069 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11070 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 11071 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11072 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 11073 | /// \brief Mark the given method pure. |
| 11074 | /// |
| 11075 | /// \param Method the method to be marked pure. |
| 11076 | /// |
| 11077 | /// \param InitRange the source range that covers the "0" initializer. |
| 11078 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 11079 | SourceLocation EndLoc = InitRange.getEnd(); |
| 11080 | if (EndLoc.isValid()) |
| 11081 | Method->setRangeEnd(EndLoc); |
| 11082 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 11083 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 11084 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 11085 | return false; |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 11086 | } |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 11087 | |
| 11088 | if (!Method->isInvalidDecl()) |
| 11089 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 11090 | << Method->getDeclName() << InitRange; |
| 11091 | return true; |
| 11092 | } |
| 11093 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 11094 | /// \brief Determine whether the given declaration is a static data member. |
| 11095 | static bool isStaticDataMember(Decl *D) { |
| 11096 | VarDecl *Var = dyn_cast_or_null<VarDecl>(D); |
| 11097 | if (!Var) |
| 11098 | return false; |
| 11099 | |
| 11100 | return Var->isStaticDataMember(); |
| 11101 | } |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 11102 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 11103 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 11104 | /// is a fresh scope pushed for just this purpose. |
| 11105 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11106 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 11107 | /// static data member of class X, names should be looked up in the scope of |
| 11108 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11109 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11110 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 11111 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11112 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 11113 | // We should only get called for declarations with scope specifiers, like: |
| 11114 | // int foo::bar; |
| 11115 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 11116 | EnterDeclaratorContext(S, D->getDeclContext()); |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 11117 | |
| 11118 | // If we are parsing the initializer for a static data member, push a |
| 11119 | // new expression evaluation context that is associated with this static |
| 11120 | // data member. |
| 11121 | if (isStaticDataMember(D)) |
| 11122 | PushExpressionEvaluationContext(PotentiallyEvaluated, D); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11123 | } |
| 11124 | |
| 11125 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11126 | /// initializer for the out-of-line declaration 'D'. |
| 11127 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11128 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 11129 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11130 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 11131 | if (isStaticDataMember(D)) |
| 11132 | PopExpressionEvaluationContext(); |
| 11133 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 11134 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 11135 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11136 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 11137 | |
| 11138 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 11139 | /// C++ if/switch/while/for statement. |
| 11140 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11141 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 11142 | // C++ 6.4p2: |
| 11143 | // The declarator shall not specify a function or an array. |
| 11144 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 11145 | // new class or enumeration. |
| 11146 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 11147 | "Parser allowed 'typedef' as storage class of condition decl."); |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 11148 | |
| 11149 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 11150 | if (!Dcl) |
| 11151 | return true; |
| 11152 | |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 11153 | if (isa<FunctionDecl>(Dcl)) { // The declarator shall not specify a function. |
| 11154 | Diag(Dcl->getLocation(), diag::err_invalid_use_of_function_type) |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 11155 | << D.getSourceRange(); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 11156 | return true; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 11157 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 11158 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 11159 | return Dcl; |
| 11160 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 11161 | |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 11162 | void Sema::LoadExternalVTableUses() { |
| 11163 | if (!ExternalSource) |
| 11164 | return; |
| 11165 | |
| 11166 | SmallVector<ExternalVTableUse, 4> VTables; |
| 11167 | ExternalSource->ReadUsedVTables(VTables); |
| 11168 | SmallVector<VTableUse, 4> NewUses; |
| 11169 | for (unsigned I = 0, N = VTables.size(); I != N; ++I) { |
| 11170 | llvm::DenseMap<CXXRecordDecl *, bool>::iterator Pos |
| 11171 | = VTablesUsed.find(VTables[I].Record); |
| 11172 | // Even if a definition wasn't required before, it may be required now. |
| 11173 | if (Pos != VTablesUsed.end()) { |
| 11174 | if (!Pos->second && VTables[I].DefinitionRequired) |
| 11175 | Pos->second = true; |
| 11176 | continue; |
| 11177 | } |
| 11178 | |
| 11179 | VTablesUsed[VTables[I].Record] = VTables[I].DefinitionRequired; |
| 11180 | NewUses.push_back(VTableUse(VTables[I].Record, VTables[I].Location)); |
| 11181 | } |
| 11182 | |
| 11183 | VTableUses.insert(VTableUses.begin(), NewUses.begin(), NewUses.end()); |
| 11184 | } |
| 11185 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11186 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 11187 | bool DefinitionRequired) { |
| 11188 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 11189 | // not have a vtable. |
| 11190 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 11191 | CurContext->isDependentContext() || |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 11192 | ExprEvalContexts.back().Context == Unevaluated) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 11193 | return; |
| 11194 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11195 | // Try to insert this class into the map. |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 11196 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11197 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 11198 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 11199 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 11200 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 11201 | // If we already had an entry, check to see if we are promoting this vtable |
| 11202 | // to required a definition. If so, we need to reappend to the VTableUses |
| 11203 | // list, since we may have already processed the first entry. |
| 11204 | if (DefinitionRequired && !Pos.first->second) { |
| 11205 | Pos.first->second = true; |
| 11206 | } else { |
| 11207 | // Otherwise, we can early exit. |
| 11208 | return; |
| 11209 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11210 | } |
| 11211 | |
| 11212 | // Local classes need to have their virtual members marked |
| 11213 | // immediately. For all other classes, we mark their virtual members |
| 11214 | // at the end of the translation unit. |
| 11215 | if (Class->isLocalClass()) |
| 11216 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 11217 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11218 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 11219 | } |
| 11220 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11221 | bool Sema::DefineUsedVTables() { |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 11222 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11223 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 11224 | return false; |
Chandler Carruth | aee543a | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 11225 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11226 | // Note: The VTableUses vector could grow as a result of marking |
| 11227 | // the members of a class as "used", so we check the size each |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11228 | // time through the loop and prefer indices (which are stable) to |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11229 | // iterators (which are not). |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 11230 | bool DefinedAnything = false; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11231 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 11232 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11233 | if (!Class) |
| 11234 | continue; |
| 11235 | |
| 11236 | SourceLocation Loc = VTableUses[I].second; |
| 11237 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11238 | bool DefineVTable = true; |
| 11239 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11240 | // If this class has a key function, but that key function is |
| 11241 | // defined in another translation unit, we don't need to emit the |
| 11242 | // vtable even though we're using it. |
| 11243 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 11244 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11245 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 11246 | case TSK_Undeclared: |
| 11247 | case TSK_ExplicitSpecialization: |
| 11248 | case TSK_ExplicitInstantiationDeclaration: |
| 11249 | // The key function is in another translation unit. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11250 | DefineVTable = false; |
| 11251 | break; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11252 | |
| 11253 | case TSK_ExplicitInstantiationDefinition: |
| 11254 | case TSK_ImplicitInstantiation: |
| 11255 | // We will be instantiating the key function. |
| 11256 | break; |
| 11257 | } |
| 11258 | } else if (!KeyFunction) { |
| 11259 | // If we have a class with no key function that is the subject |
| 11260 | // of an explicit instantiation declaration, suppress the |
| 11261 | // vtable; it will live with the explicit instantiation |
| 11262 | // definition. |
| 11263 | bool IsExplicitInstantiationDeclaration |
| 11264 | = Class->getTemplateSpecializationKind() |
| 11265 | == TSK_ExplicitInstantiationDeclaration; |
| 11266 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 11267 | REnd = Class->redecls_end(); |
| 11268 | R != REnd; ++R) { |
| 11269 | TemplateSpecializationKind TSK |
| 11270 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 11271 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 11272 | IsExplicitInstantiationDeclaration = true; |
| 11273 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 11274 | IsExplicitInstantiationDeclaration = false; |
| 11275 | break; |
| 11276 | } |
| 11277 | } |
| 11278 | |
| 11279 | if (IsExplicitInstantiationDeclaration) |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11280 | DefineVTable = false; |
| 11281 | } |
| 11282 | |
| 11283 | // The exception specifications for all virtual members may be needed even |
| 11284 | // if we are not providing an authoritative form of the vtable in this TU. |
| 11285 | // We may choose to emit it available_externally anyway. |
| 11286 | if (!DefineVTable) { |
| 11287 | MarkVirtualMemberExceptionSpecsNeeded(Loc, Class); |
| 11288 | continue; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11289 | } |
| 11290 | |
| 11291 | // Mark all of the virtual members of this class as referenced, so |
| 11292 | // that we can build a vtable. Then, tell the AST consumer that a |
| 11293 | // vtable for this class is required. |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 11294 | DefinedAnything = true; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11295 | MarkVirtualMembersReferenced(Loc, Class); |
| 11296 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 11297 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 11298 | |
| 11299 | // Optionally warn if we're emitting a weak vtable. |
| 11300 | if (Class->getLinkage() == ExternalLinkage && |
| 11301 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Douglas Gregor | a120d01 | 2011-09-23 19:04:03 +0000 | [diff] [blame] | 11302 | const FunctionDecl *KeyFunctionDef = 0; |
| 11303 | if (!KeyFunction || |
| 11304 | (KeyFunction->hasBody(KeyFunctionDef) && |
| 11305 | KeyFunctionDef->isInlined())) |
David Blaikie | 44d95b5 | 2011-12-09 18:32:50 +0000 | [diff] [blame] | 11306 | Diag(Class->getLocation(), Class->getTemplateSpecializationKind() == |
| 11307 | TSK_ExplicitInstantiationDefinition |
| 11308 | ? diag::warn_weak_template_vtable : diag::warn_weak_vtable) |
| 11309 | << Class; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11310 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 11311 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11312 | VTableUses.clear(); |
| 11313 | |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 11314 | return DefinedAnything; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 11315 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 11316 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11317 | void Sema::MarkVirtualMemberExceptionSpecsNeeded(SourceLocation Loc, |
| 11318 | const CXXRecordDecl *RD) { |
| 11319 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 11320 | E = RD->method_end(); I != E; ++I) |
| 11321 | if ((*I)->isVirtual() && !(*I)->isPure()) |
| 11322 | ResolveExceptionSpec(Loc, (*I)->getType()->castAs<FunctionProtoType>()); |
| 11323 | } |
| 11324 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 11325 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 11326 | const CXXRecordDecl *RD) { |
Richard Smith | ff817f7 | 2012-07-07 06:59:51 +0000 | [diff] [blame] | 11327 | // Mark all functions which will appear in RD's vtable as used. |
| 11328 | CXXFinalOverriderMap FinalOverriders; |
| 11329 | RD->getFinalOverriders(FinalOverriders); |
| 11330 | for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(), |
| 11331 | E = FinalOverriders.end(); |
| 11332 | I != E; ++I) { |
| 11333 | for (OverridingMethods::const_iterator OI = I->second.begin(), |
| 11334 | OE = I->second.end(); |
| 11335 | OI != OE; ++OI) { |
| 11336 | assert(OI->second.size() > 0 && "no final overrider"); |
| 11337 | CXXMethodDecl *Overrider = OI->second.front().Method; |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 11338 | |
Richard Smith | ff817f7 | 2012-07-07 06:59:51 +0000 | [diff] [blame] | 11339 | // C++ [basic.def.odr]p2: |
| 11340 | // [...] A virtual member function is used if it is not pure. [...] |
| 11341 | if (!Overrider->isPure()) |
| 11342 | MarkFunctionReferenced(Loc, Overrider); |
| 11343 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 11344 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 11345 | |
| 11346 | // Only classes that have virtual bases need a VTT. |
| 11347 | if (RD->getNumVBases() == 0) |
| 11348 | return; |
| 11349 | |
| 11350 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 11351 | e = RD->bases_end(); i != e; ++i) { |
| 11352 | const CXXRecordDecl *Base = |
| 11353 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 11354 | if (Base->getNumVBases() == 0) |
| 11355 | continue; |
| 11356 | MarkVirtualMembersReferenced(Loc, Base); |
| 11357 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 11358 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 11359 | |
| 11360 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 11361 | /// Objective-C implementation whose ivars need be initialized. |
| 11362 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11363 | if (!getLangOpts().CPlusPlus) |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 11364 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 11365 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 11366 | SmallVector<ObjCIvarDecl*, 8> ivars; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 11367 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 11368 | if (ivars.empty()) |
| 11369 | return; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 11370 | SmallVector<CXXCtorInitializer*, 32> AllToInit; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 11371 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 11372 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 11373 | if (Field->isInvalidDecl()) |
| 11374 | continue; |
| 11375 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 11376 | CXXCtorInitializer *Member; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 11377 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 11378 | InitializationKind InitKind = |
| 11379 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 11380 | |
| 11381 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11382 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 11383 | InitSeq.Perform(*this, InitEntity, InitKind, MultiExprArg()); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 11384 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 11385 | // Note, MemberInit could actually come back empty if no initialization |
| 11386 | // is required (e.g., because it would call a trivial default constructor) |
| 11387 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 11388 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 11389 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 11390 | Member = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 11391 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 11392 | SourceLocation(), |
| 11393 | MemberInit.takeAs<Expr>(), |
| 11394 | SourceLocation()); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 11395 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 11396 | |
| 11397 | // Be sure that the destructor is accessible and is marked as referenced. |
| 11398 | if (const RecordType *RecordTy |
| 11399 | = Context.getBaseElementType(Field->getType()) |
| 11400 | ->getAs<RecordType>()) { |
| 11401 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 11402 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11403 | MarkFunctionReferenced(Field->getLocation(), Destructor); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 11404 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 11405 | PDiag(diag::err_access_dtor_ivar) |
| 11406 | << Context.getBaseElementType(Field->getType())); |
| 11407 | } |
| 11408 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 11409 | } |
| 11410 | ObjCImplementation->setIvarInitializers(Context, |
| 11411 | AllToInit.data(), AllToInit.size()); |
| 11412 | } |
| 11413 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 11414 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 11415 | static |
| 11416 | void DelegatingCycleHelper(CXXConstructorDecl* Ctor, |
| 11417 | llvm::SmallSet<CXXConstructorDecl*, 4> &Valid, |
| 11418 | llvm::SmallSet<CXXConstructorDecl*, 4> &Invalid, |
| 11419 | llvm::SmallSet<CXXConstructorDecl*, 4> &Current, |
| 11420 | Sema &S) { |
| 11421 | llvm::SmallSet<CXXConstructorDecl*, 4>::iterator CI = Current.begin(), |
| 11422 | CE = Current.end(); |
| 11423 | if (Ctor->isInvalidDecl()) |
| 11424 | return; |
| 11425 | |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 11426 | CXXConstructorDecl *Target = Ctor->getTargetConstructor(); |
| 11427 | |
| 11428 | // Target may not be determinable yet, for instance if this is a dependent |
| 11429 | // call in an uninstantiated template. |
| 11430 | if (Target) { |
| 11431 | const FunctionDecl *FNTarget = 0; |
| 11432 | (void)Target->hasBody(FNTarget); |
| 11433 | Target = const_cast<CXXConstructorDecl*>( |
| 11434 | cast_or_null<CXXConstructorDecl>(FNTarget)); |
| 11435 | } |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 11436 | |
| 11437 | CXXConstructorDecl *Canonical = Ctor->getCanonicalDecl(), |
| 11438 | // Avoid dereferencing a null pointer here. |
| 11439 | *TCanonical = Target ? Target->getCanonicalDecl() : 0; |
| 11440 | |
| 11441 | if (!Current.insert(Canonical)) |
| 11442 | return; |
| 11443 | |
| 11444 | // We know that beyond here, we aren't chaining into a cycle. |
| 11445 | if (!Target || !Target->isDelegatingConstructor() || |
| 11446 | Target->isInvalidDecl() || Valid.count(TCanonical)) { |
| 11447 | for (CI = Current.begin(), CE = Current.end(); CI != CE; ++CI) |
| 11448 | Valid.insert(*CI); |
| 11449 | Current.clear(); |
| 11450 | // We've hit a cycle. |
| 11451 | } else if (TCanonical == Canonical || Invalid.count(TCanonical) || |
| 11452 | Current.count(TCanonical)) { |
| 11453 | // If we haven't diagnosed this cycle yet, do so now. |
| 11454 | if (!Invalid.count(TCanonical)) { |
| 11455 | S.Diag((*Ctor->init_begin())->getSourceLocation(), |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 11456 | diag::warn_delegating_ctor_cycle) |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 11457 | << Ctor; |
| 11458 | |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 11459 | // Don't add a note for a function delegating directly to itself. |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 11460 | if (TCanonical != Canonical) |
| 11461 | S.Diag(Target->getLocation(), diag::note_it_delegates_to); |
| 11462 | |
| 11463 | CXXConstructorDecl *C = Target; |
| 11464 | while (C->getCanonicalDecl() != Canonical) { |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 11465 | const FunctionDecl *FNTarget = 0; |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 11466 | (void)C->getTargetConstructor()->hasBody(FNTarget); |
| 11467 | assert(FNTarget && "Ctor cycle through bodiless function"); |
| 11468 | |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 11469 | C = const_cast<CXXConstructorDecl*>( |
| 11470 | cast<CXXConstructorDecl>(FNTarget)); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 11471 | S.Diag(C->getLocation(), diag::note_which_delegates_to); |
| 11472 | } |
| 11473 | } |
| 11474 | |
| 11475 | for (CI = Current.begin(), CE = Current.end(); CI != CE; ++CI) |
| 11476 | Invalid.insert(*CI); |
| 11477 | Current.clear(); |
| 11478 | } else { |
| 11479 | DelegatingCycleHelper(Target, Valid, Invalid, Current, S); |
| 11480 | } |
| 11481 | } |
| 11482 | |
| 11483 | |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 11484 | void Sema::CheckDelegatingCtorCycles() { |
| 11485 | llvm::SmallSet<CXXConstructorDecl*, 4> Valid, Invalid, Current; |
| 11486 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 11487 | llvm::SmallSet<CXXConstructorDecl*, 4>::iterator CI = Current.begin(), |
| 11488 | CE = Current.end(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 11489 | |
Douglas Gregor | 0129b56 | 2011-07-27 21:57:17 +0000 | [diff] [blame] | 11490 | for (DelegatingCtorDeclsType::iterator |
| 11491 | I = DelegatingCtorDecls.begin(ExternalSource), |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 11492 | E = DelegatingCtorDecls.end(); |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 11493 | I != E; ++I) |
| 11494 | DelegatingCycleHelper(*I, Valid, Invalid, Current, *this); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 11495 | |
| 11496 | for (CI = Invalid.begin(), CE = Invalid.end(); CI != CE; ++CI) |
| 11497 | (*CI)->setInvalidDecl(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 11498 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 11499 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11500 | namespace { |
| 11501 | /// \brief AST visitor that finds references to the 'this' expression. |
| 11502 | class FindCXXThisExpr : public RecursiveASTVisitor<FindCXXThisExpr> { |
| 11503 | Sema &S; |
| 11504 | |
| 11505 | public: |
| 11506 | explicit FindCXXThisExpr(Sema &S) : S(S) { } |
| 11507 | |
| 11508 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 11509 | S.Diag(E->getLocation(), diag::err_this_static_member_func) |
| 11510 | << E->isImplicit(); |
| 11511 | return false; |
| 11512 | } |
| 11513 | }; |
| 11514 | } |
| 11515 | |
| 11516 | bool Sema::checkThisInStaticMemberFunctionType(CXXMethodDecl *Method) { |
| 11517 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 11518 | if (!TSInfo) |
| 11519 | return false; |
| 11520 | |
| 11521 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 11522 | FunctionProtoTypeLoc *ProtoTL = dyn_cast<FunctionProtoTypeLoc>(&TL); |
| 11523 | if (!ProtoTL) |
| 11524 | return false; |
| 11525 | |
| 11526 | // C++11 [expr.prim.general]p3: |
| 11527 | // [The expression this] shall not appear before the optional |
| 11528 | // cv-qualifier-seq and it shall not appear within the declaration of a |
| 11529 | // static member function (although its type and value category are defined |
| 11530 | // within a static member function as they are within a non-static member |
| 11531 | // function). [ Note: this is because declaration matching does not occur |
NAKAMURA Takumi | c86d1fd | 2012-04-21 09:40:04 +0000 | [diff] [blame] | 11532 | // until the complete declarator is known. - end note ] |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11533 | const FunctionProtoType *Proto = ProtoTL->getTypePtr(); |
| 11534 | FindCXXThisExpr Finder(*this); |
| 11535 | |
| 11536 | // If the return type came after the cv-qualifier-seq, check it now. |
| 11537 | if (Proto->hasTrailingReturn() && |
| 11538 | !Finder.TraverseTypeLoc(ProtoTL->getResultLoc())) |
| 11539 | return true; |
| 11540 | |
| 11541 | // Check the exception specification. |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11542 | if (checkThisInStaticMemberFunctionExceptionSpec(Method)) |
| 11543 | return true; |
| 11544 | |
| 11545 | return checkThisInStaticMemberFunctionAttributes(Method); |
| 11546 | } |
| 11547 | |
| 11548 | bool Sema::checkThisInStaticMemberFunctionExceptionSpec(CXXMethodDecl *Method) { |
| 11549 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 11550 | if (!TSInfo) |
| 11551 | return false; |
| 11552 | |
| 11553 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 11554 | FunctionProtoTypeLoc *ProtoTL = dyn_cast<FunctionProtoTypeLoc>(&TL); |
| 11555 | if (!ProtoTL) |
| 11556 | return false; |
| 11557 | |
| 11558 | const FunctionProtoType *Proto = ProtoTL->getTypePtr(); |
| 11559 | FindCXXThisExpr Finder(*this); |
| 11560 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11561 | switch (Proto->getExceptionSpecType()) { |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11562 | case EST_Uninstantiated: |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11563 | case EST_Unevaluated: |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11564 | case EST_BasicNoexcept: |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11565 | case EST_DynamicNone: |
| 11566 | case EST_MSAny: |
| 11567 | case EST_None: |
| 11568 | break; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11569 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11570 | case EST_ComputedNoexcept: |
| 11571 | if (!Finder.TraverseStmt(Proto->getNoexceptExpr())) |
| 11572 | return true; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11573 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11574 | case EST_Dynamic: |
| 11575 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11576 | EEnd = Proto->exception_end(); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11577 | E != EEnd; ++E) { |
| 11578 | if (!Finder.TraverseType(*E)) |
| 11579 | return true; |
| 11580 | } |
| 11581 | break; |
| 11582 | } |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11583 | |
| 11584 | return false; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11585 | } |
| 11586 | |
| 11587 | bool Sema::checkThisInStaticMemberFunctionAttributes(CXXMethodDecl *Method) { |
| 11588 | FindCXXThisExpr Finder(*this); |
| 11589 | |
| 11590 | // Check attributes. |
| 11591 | for (Decl::attr_iterator A = Method->attr_begin(), AEnd = Method->attr_end(); |
| 11592 | A != AEnd; ++A) { |
| 11593 | // FIXME: This should be emitted by tblgen. |
| 11594 | Expr *Arg = 0; |
| 11595 | ArrayRef<Expr *> Args; |
| 11596 | if (GuardedByAttr *G = dyn_cast<GuardedByAttr>(*A)) |
| 11597 | Arg = G->getArg(); |
| 11598 | else if (PtGuardedByAttr *G = dyn_cast<PtGuardedByAttr>(*A)) |
| 11599 | Arg = G->getArg(); |
| 11600 | else if (AcquiredAfterAttr *AA = dyn_cast<AcquiredAfterAttr>(*A)) |
| 11601 | Args = ArrayRef<Expr *>(AA->args_begin(), AA->args_size()); |
| 11602 | else if (AcquiredBeforeAttr *AB = dyn_cast<AcquiredBeforeAttr>(*A)) |
| 11603 | Args = ArrayRef<Expr *>(AB->args_begin(), AB->args_size()); |
| 11604 | else if (ExclusiveLockFunctionAttr *ELF |
| 11605 | = dyn_cast<ExclusiveLockFunctionAttr>(*A)) |
| 11606 | Args = ArrayRef<Expr *>(ELF->args_begin(), ELF->args_size()); |
| 11607 | else if (SharedLockFunctionAttr *SLF |
| 11608 | = dyn_cast<SharedLockFunctionAttr>(*A)) |
| 11609 | Args = ArrayRef<Expr *>(SLF->args_begin(), SLF->args_size()); |
| 11610 | else if (ExclusiveTrylockFunctionAttr *ETLF |
| 11611 | = dyn_cast<ExclusiveTrylockFunctionAttr>(*A)) { |
| 11612 | Arg = ETLF->getSuccessValue(); |
| 11613 | Args = ArrayRef<Expr *>(ETLF->args_begin(), ETLF->args_size()); |
| 11614 | } else if (SharedTrylockFunctionAttr *STLF |
| 11615 | = dyn_cast<SharedTrylockFunctionAttr>(*A)) { |
| 11616 | Arg = STLF->getSuccessValue(); |
| 11617 | Args = ArrayRef<Expr *>(STLF->args_begin(), STLF->args_size()); |
| 11618 | } else if (UnlockFunctionAttr *UF = dyn_cast<UnlockFunctionAttr>(*A)) |
| 11619 | Args = ArrayRef<Expr *>(UF->args_begin(), UF->args_size()); |
| 11620 | else if (LockReturnedAttr *LR = dyn_cast<LockReturnedAttr>(*A)) |
| 11621 | Arg = LR->getArg(); |
| 11622 | else if (LocksExcludedAttr *LE = dyn_cast<LocksExcludedAttr>(*A)) |
| 11623 | Args = ArrayRef<Expr *>(LE->args_begin(), LE->args_size()); |
| 11624 | else if (ExclusiveLocksRequiredAttr *ELR |
| 11625 | = dyn_cast<ExclusiveLocksRequiredAttr>(*A)) |
| 11626 | Args = ArrayRef<Expr *>(ELR->args_begin(), ELR->args_size()); |
| 11627 | else if (SharedLocksRequiredAttr *SLR |
| 11628 | = dyn_cast<SharedLocksRequiredAttr>(*A)) |
| 11629 | Args = ArrayRef<Expr *>(SLR->args_begin(), SLR->args_size()); |
| 11630 | |
| 11631 | if (Arg && !Finder.TraverseStmt(Arg)) |
| 11632 | return true; |
| 11633 | |
| 11634 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 11635 | if (!Finder.TraverseStmt(Args[I])) |
| 11636 | return true; |
| 11637 | } |
| 11638 | } |
| 11639 | |
| 11640 | return false; |
| 11641 | } |
| 11642 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11643 | void |
| 11644 | Sema::checkExceptionSpecification(ExceptionSpecificationType EST, |
| 11645 | ArrayRef<ParsedType> DynamicExceptions, |
| 11646 | ArrayRef<SourceRange> DynamicExceptionRanges, |
| 11647 | Expr *NoexceptExpr, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 11648 | SmallVectorImpl<QualType> &Exceptions, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11649 | FunctionProtoType::ExtProtoInfo &EPI) { |
| 11650 | Exceptions.clear(); |
| 11651 | EPI.ExceptionSpecType = EST; |
| 11652 | if (EST == EST_Dynamic) { |
| 11653 | Exceptions.reserve(DynamicExceptions.size()); |
| 11654 | for (unsigned ei = 0, ee = DynamicExceptions.size(); ei != ee; ++ei) { |
| 11655 | // FIXME: Preserve type source info. |
| 11656 | QualType ET = GetTypeFromParser(DynamicExceptions[ei]); |
| 11657 | |
| 11658 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 11659 | collectUnexpandedParameterPacks(ET, Unexpanded); |
| 11660 | if (!Unexpanded.empty()) { |
| 11661 | DiagnoseUnexpandedParameterPacks(DynamicExceptionRanges[ei].getBegin(), |
| 11662 | UPPC_ExceptionType, |
| 11663 | Unexpanded); |
| 11664 | continue; |
| 11665 | } |
| 11666 | |
| 11667 | // Check that the type is valid for an exception spec, and |
| 11668 | // drop it if not. |
| 11669 | if (!CheckSpecifiedExceptionType(ET, DynamicExceptionRanges[ei])) |
| 11670 | Exceptions.push_back(ET); |
| 11671 | } |
| 11672 | EPI.NumExceptions = Exceptions.size(); |
| 11673 | EPI.Exceptions = Exceptions.data(); |
| 11674 | return; |
| 11675 | } |
| 11676 | |
| 11677 | if (EST == EST_ComputedNoexcept) { |
| 11678 | // If an error occurred, there's no expression here. |
| 11679 | if (NoexceptExpr) { |
| 11680 | assert((NoexceptExpr->isTypeDependent() || |
| 11681 | NoexceptExpr->getType()->getCanonicalTypeUnqualified() == |
| 11682 | Context.BoolTy) && |
| 11683 | "Parser should have made sure that the expression is boolean"); |
| 11684 | if (NoexceptExpr && DiagnoseUnexpandedParameterPack(NoexceptExpr)) { |
| 11685 | EPI.ExceptionSpecType = EST_BasicNoexcept; |
| 11686 | return; |
| 11687 | } |
| 11688 | |
| 11689 | if (!NoexceptExpr->isValueDependent()) |
| 11690 | NoexceptExpr = VerifyIntegerConstantExpression(NoexceptExpr, 0, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 11691 | diag::err_noexcept_needs_constant_expression, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11692 | /*AllowFold*/ false).take(); |
| 11693 | EPI.NoexceptExpr = NoexceptExpr; |
| 11694 | } |
| 11695 | return; |
| 11696 | } |
| 11697 | } |
| 11698 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 11699 | /// IdentifyCUDATarget - Determine the CUDA compilation target for this function |
| 11700 | Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const FunctionDecl *D) { |
| 11701 | // Implicitly declared functions (e.g. copy constructors) are |
| 11702 | // __host__ __device__ |
| 11703 | if (D->isImplicit()) |
| 11704 | return CFT_HostDevice; |
| 11705 | |
| 11706 | if (D->hasAttr<CUDAGlobalAttr>()) |
| 11707 | return CFT_Global; |
| 11708 | |
| 11709 | if (D->hasAttr<CUDADeviceAttr>()) { |
| 11710 | if (D->hasAttr<CUDAHostAttr>()) |
| 11711 | return CFT_HostDevice; |
| 11712 | else |
| 11713 | return CFT_Device; |
| 11714 | } |
| 11715 | |
| 11716 | return CFT_Host; |
| 11717 | } |
| 11718 | |
| 11719 | bool Sema::CheckCUDATarget(CUDAFunctionTarget CallerTarget, |
| 11720 | CUDAFunctionTarget CalleeTarget) { |
| 11721 | // CUDA B.1.1 "The __device__ qualifier declares a function that is... |
| 11722 | // Callable from the device only." |
| 11723 | if (CallerTarget == CFT_Host && CalleeTarget == CFT_Device) |
| 11724 | return true; |
| 11725 | |
| 11726 | // CUDA B.1.2 "The __global__ qualifier declares a function that is... |
| 11727 | // Callable from the host only." |
| 11728 | // CUDA B.1.3 "The __host__ qualifier declares a function that is... |
| 11729 | // Callable from the host only." |
| 11730 | if ((CallerTarget == CFT_Device || CallerTarget == CFT_Global) && |
| 11731 | (CalleeTarget == CFT_Host || CalleeTarget == CFT_Global)) |
| 11732 | return true; |
| 11733 | |
| 11734 | if (CallerTarget == CFT_HostDevice && CalleeTarget != CFT_HostDevice) |
| 11735 | return true; |
| 11736 | |
| 11737 | return false; |
| 11738 | } |