Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 1 | //===------ SemaDeclCXX.cpp - Semantic Analysis for C++ Declarations ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 15 | #include "clang/Sema/CXXFieldCollector.h" |
| 16 | #include "clang/Sema/Scope.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Initialization.h" |
| 18 | #include "clang/Sema/Lookup.h" |
Eli Friedman | 7badd24 | 2012-02-09 20:13:14 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 23 | #include "clang/AST/CharUnits.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 24 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclVisitor.h" |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 26 | #include "clang/AST/EvaluatedExprVisitor.h" |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 27 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 28 | #include "clang/AST/RecordLayout.h" |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 29 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 30 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 31 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 32 | #include "clang/AST/TypeOrdering.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 33 | #include "clang/Sema/DeclSpec.h" |
| 34 | #include "clang/Sema/ParsedTemplate.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 35 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 36 | #include "clang/Lex/Preprocessor.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 39 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 40 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 41 | |
| 42 | using namespace clang; |
| 43 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
| 45 | // CheckDefaultArgumentVisitor |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 48 | namespace { |
| 49 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 50 | /// the default argument of a parameter to determine whether it |
| 51 | /// contains any ill-formed subexpressions. For example, this will |
| 52 | /// diagnose the use of local variables or parameters within the |
| 53 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 54 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 55 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 56 | Expr *DefaultArg; |
| 57 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 59 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 61 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 63 | bool VisitExpr(Expr *Node); |
| 64 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 65 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Douglas Gregor | f0459f8 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 66 | bool VisitLambdaExpr(LambdaExpr *Lambda); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 67 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 69 | /// VisitExpr - Visit all of the children of this expression. |
| 70 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 71 | bool IsInvalid = false; |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 72 | for (Stmt::child_range I = Node->children(); I; ++I) |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 73 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 74 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 77 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 78 | /// determine whether this declaration can be used in the default |
| 79 | /// argument expression. |
| 80 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 81 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 82 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 83 | // C++ [dcl.fct.default]p9 |
| 84 | // Default arguments are evaluated each time the function is |
| 85 | // called. The order of evaluation of function arguments is |
| 86 | // unspecified. Consequently, parameters of a function shall not |
| 87 | // be used in default argument expressions, even if they are not |
| 88 | // evaluated. Parameters of a function declared before a default |
| 89 | // argument expression are in scope and can hide namespace and |
| 90 | // class member names. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 91 | return S->Diag(DRE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 92 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 93 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 94 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 95 | // C++ [dcl.fct.default]p7 |
| 96 | // Local variables shall not be used in default argument |
| 97 | // expressions. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 98 | if (VDecl->isLocalVarDecl()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 99 | return S->Diag(DRE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 100 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 101 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 103 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 104 | return false; |
| 105 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 106 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 107 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 108 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 109 | // C++ [dcl.fct.default]p8: |
| 110 | // The keyword this shall not be used in a default argument of a |
| 111 | // member function. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 112 | return S->Diag(ThisE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 113 | diag::err_param_default_argument_references_this) |
| 114 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 115 | } |
Douglas Gregor | f0459f8 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 116 | |
| 117 | bool CheckDefaultArgumentVisitor::VisitLambdaExpr(LambdaExpr *Lambda) { |
| 118 | // C++11 [expr.lambda.prim]p13: |
| 119 | // A lambda-expression appearing in a default argument shall not |
| 120 | // implicitly or explicitly capture any entity. |
| 121 | if (Lambda->capture_begin() == Lambda->capture_end()) |
| 122 | return false; |
| 123 | |
| 124 | return S->Diag(Lambda->getLocStart(), |
| 125 | diag::err_lambda_capture_default_arg); |
| 126 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 129 | void Sema::ImplicitExceptionSpecification::CalledDecl(SourceLocation CallLoc, |
| 130 | CXXMethodDecl *Method) { |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 131 | // If we have an MSAny spec already, don't bother. |
| 132 | if (!Method || ComputedEST == EST_MSAny) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 133 | return; |
| 134 | |
| 135 | const FunctionProtoType *Proto |
| 136 | = Method->getType()->getAs<FunctionProtoType>(); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 137 | Proto = Self->ResolveExceptionSpec(CallLoc, Proto); |
| 138 | if (!Proto) |
| 139 | return; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 140 | |
| 141 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
| 142 | |
| 143 | // If this function can throw any exceptions, make a note of that. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 144 | if (EST == EST_MSAny || EST == EST_None) { |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 145 | ClearExceptions(); |
| 146 | ComputedEST = EST; |
| 147 | return; |
| 148 | } |
| 149 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 150 | // FIXME: If the call to this decl is using any of its default arguments, we |
| 151 | // need to search them for potentially-throwing calls. |
| 152 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 153 | // If this function has a basic noexcept, it doesn't affect the outcome. |
| 154 | if (EST == EST_BasicNoexcept) |
| 155 | return; |
| 156 | |
| 157 | // If we have a throw-all spec at this point, ignore the function. |
| 158 | if (ComputedEST == EST_None) |
| 159 | return; |
| 160 | |
| 161 | // If we're still at noexcept(true) and there's a nothrow() callee, |
| 162 | // change to that specification. |
| 163 | if (EST == EST_DynamicNone) { |
| 164 | if (ComputedEST == EST_BasicNoexcept) |
| 165 | ComputedEST = EST_DynamicNone; |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | // Check out noexcept specs. |
| 170 | if (EST == EST_ComputedNoexcept) { |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 171 | FunctionProtoType::NoexceptResult NR = |
| 172 | Proto->getNoexceptSpec(Self->Context); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 173 | assert(NR != FunctionProtoType::NR_NoNoexcept && |
| 174 | "Must have noexcept result for EST_ComputedNoexcept."); |
| 175 | assert(NR != FunctionProtoType::NR_Dependent && |
| 176 | "Should not generate implicit declarations for dependent cases, " |
| 177 | "and don't know how to handle them anyway."); |
| 178 | |
| 179 | // noexcept(false) -> no spec on the new function |
| 180 | if (NR == FunctionProtoType::NR_Throw) { |
| 181 | ClearExceptions(); |
| 182 | ComputedEST = EST_None; |
| 183 | } |
| 184 | // noexcept(true) won't change anything either. |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | assert(EST == EST_Dynamic && "EST case not considered earlier."); |
| 189 | assert(ComputedEST != EST_None && |
| 190 | "Shouldn't collect exceptions when throw-all is guaranteed."); |
| 191 | ComputedEST = EST_Dynamic; |
| 192 | // Record the exceptions in this function's exception specification. |
| 193 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 194 | EEnd = Proto->exception_end(); |
| 195 | E != EEnd; ++E) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 196 | if (ExceptionsSeen.insert(Self->Context.getCanonicalType(*E))) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 197 | Exceptions.push_back(*E); |
| 198 | } |
| 199 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 200 | void Sema::ImplicitExceptionSpecification::CalledExpr(Expr *E) { |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 201 | if (!E || ComputedEST == EST_MSAny) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 202 | return; |
| 203 | |
| 204 | // FIXME: |
| 205 | // |
| 206 | // C++0x [except.spec]p14: |
NAKAMURA Takumi | 4857947 | 2011-06-21 03:19:28 +0000 | [diff] [blame] | 207 | // [An] implicit exception-specification specifies the type-id T if and |
| 208 | // only if T is allowed by the exception-specification of a function directly |
| 209 | // 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] | 210 | // function it directly invokes allows all exceptions, and f shall allow no |
| 211 | // exceptions if every function it directly invokes allows no exceptions. |
| 212 | // |
| 213 | // Note in particular that if an implicit exception-specification is generated |
| 214 | // for a function containing a throw-expression, that specification can still |
| 215 | // be noexcept(true). |
| 216 | // |
| 217 | // Note also that 'directly invoked' is not defined in the standard, and there |
| 218 | // is no indication that we should only consider potentially-evaluated calls. |
| 219 | // |
| 220 | // Ultimately we should implement the intent of the standard: the exception |
| 221 | // specification should be the set of exceptions which can be thrown by the |
| 222 | // implicit definition. For now, we assume that any non-nothrow expression can |
| 223 | // throw any exception. |
| 224 | |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 225 | if (Self->canThrow(E)) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 226 | ComputedEST = EST_None; |
| 227 | } |
| 228 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 229 | bool |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 230 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | SourceLocation EqualLoc) { |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 232 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 233 | diag::err_typecheck_decl_incomplete_type)) { |
| 234 | Param->setInvalidDecl(); |
| 235 | return true; |
| 236 | } |
| 237 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 238 | // C++ [dcl.fct.default]p5 |
| 239 | // A default argument expression is implicitly converted (clause |
| 240 | // 4) to the parameter type. The default argument expression has |
| 241 | // the same semantic constraints as the initializer expression in |
| 242 | // a declaration of a variable of the parameter type, using the |
| 243 | // copy-initialization semantics (8.5). |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 244 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 245 | Param); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 246 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 247 | EqualLoc); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 248 | InitializationSequence InitSeq(*this, Entity, Kind, &Arg, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 249 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Arg); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 250 | if (Result.isInvalid()) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 251 | return true; |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 252 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 253 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 254 | CheckImplicitConversions(Arg, EqualLoc); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 255 | Arg = MaybeCreateExprWithCleanups(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 257 | // Okay: add the default argument to the parameter |
| 258 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Douglas Gregor | 8cfb7a3 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 260 | // We have already instantiated this parameter; provide each of the |
| 261 | // instantiations with the uninstantiated default argument. |
| 262 | UnparsedDefaultArgInstantiationsMap::iterator InstPos |
| 263 | = UnparsedDefaultArgInstantiations.find(Param); |
| 264 | if (InstPos != UnparsedDefaultArgInstantiations.end()) { |
| 265 | for (unsigned I = 0, N = InstPos->second.size(); I != N; ++I) |
| 266 | InstPos->second[I]->setUninstantiatedDefaultArg(Arg); |
| 267 | |
| 268 | // We're done tracking this parameter's instantiations. |
| 269 | UnparsedDefaultArgInstantiations.erase(InstPos); |
| 270 | } |
| 271 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 272 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 275 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 276 | /// provided for a function parameter is well-formed. If so, attach it |
| 277 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 278 | void |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 279 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 280 | Expr *DefaultArg) { |
| 281 | if (!param || !DefaultArg) |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 282 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 284 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 285 | UnparsedDefaultArgLocs.erase(Param); |
| 286 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 287 | // Default arguments are only permitted in C++ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 288 | if (!getLangOpts().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 289 | Diag(EqualLoc, diag::err_param_default_argument) |
| 290 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 291 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 295 | // Check for unexpanded parameter packs. |
| 296 | if (DiagnoseUnexpandedParameterPack(DefaultArg, UPPC_DefaultArgument)) { |
| 297 | Param->setInvalidDecl(); |
| 298 | return; |
| 299 | } |
| 300 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 301 | // Check that the default argument is well-formed |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 302 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 303 | if (DefaultArgChecker.Visit(DefaultArg)) { |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 304 | Param->setInvalidDecl(); |
| 305 | return; |
| 306 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 308 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 311 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 312 | /// argument for a function parameter, but we can't parse it yet |
| 313 | /// because we're inside a class definition. Note that this default |
| 314 | /// argument will be parsed later. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 315 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 316 | SourceLocation EqualLoc, |
| 317 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 318 | if (!param) |
| 319 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 321 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 322 | if (Param) |
| 323 | Param->setUnparsedDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 325 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 328 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 329 | /// the default argument for the parameter param failed. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 330 | void Sema::ActOnParamDefaultArgumentError(Decl *param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 331 | if (!param) |
| 332 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 334 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 336 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 338 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 341 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 342 | /// arguments in the declarator, which is not a function declaration |
| 343 | /// or definition and therefore is not permitted to have default |
| 344 | /// arguments. This routine should be invoked for every declarator |
| 345 | /// that is not a function declaration or definition. |
| 346 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 347 | // C++ [dcl.fct.default]p3 |
| 348 | // A default argument expression shall be specified only in the |
| 349 | // parameter-declaration-clause of a function declaration or in a |
| 350 | // template-parameter (14.1). It shall not be specified for a |
| 351 | // parameter pack. If it is specified in a |
| 352 | // parameter-declaration-clause, it shall not occur within a |
| 353 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 354 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 355 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 356 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 357 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 358 | ParmVarDecl *Param = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 359 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 360 | if (Param->hasUnparsedDefaultArg()) { |
| 361 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 362 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 363 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 364 | delete Toks; |
| 365 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 366 | } else if (Param->getDefaultArg()) { |
| 367 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 368 | << Param->getDefaultArg()->getSourceRange(); |
| 369 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Craig Topper | 1a6eac8 | 2012-09-21 04:33:26 +0000 | [diff] [blame] | 376 | /// MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 377 | /// function, once we already know that they have the same |
| 378 | /// type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 379 | /// error, false otherwise. |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 380 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old, |
| 381 | Scope *S) { |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 382 | bool Invalid = false; |
| 383 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 384 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 385 | // For non-template functions, default arguments can be added in |
| 386 | // later declarations of a function in the same |
| 387 | // scope. Declarations in different scopes have completely |
| 388 | // distinct sets of default arguments. That is, declarations in |
| 389 | // inner scopes do not acquire default arguments from |
| 390 | // declarations in outer scopes, and vice versa. In a given |
| 391 | // function declaration, all parameters subsequent to a |
| 392 | // parameter with a default argument shall have default |
| 393 | // arguments supplied in this or previous declarations. A |
| 394 | // default argument shall not be redefined by a later |
| 395 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 396 | // |
| 397 | // C++ [dcl.fct.default]p6: |
| 398 | // Except for member functions of class templates, the default arguments |
| 399 | // in a member function definition that appears outside of the class |
| 400 | // definition are added to the set of default arguments provided by the |
| 401 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 402 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 403 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 404 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 405 | |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 406 | bool OldParamHasDfl = OldParam->hasDefaultArg(); |
| 407 | bool NewParamHasDfl = NewParam->hasDefaultArg(); |
| 408 | |
| 409 | NamedDecl *ND = Old; |
| 410 | if (S && !isDeclInScope(ND, New->getDeclContext(), S)) |
| 411 | // Ignore default parameters of old decl if they are not in |
| 412 | // the same scope. |
| 413 | OldParamHasDfl = false; |
| 414 | |
| 415 | if (OldParamHasDfl && NewParamHasDfl) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 416 | |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 417 | unsigned DiagDefaultParamID = |
| 418 | diag::err_param_default_argument_redefinition; |
| 419 | |
| 420 | // MSVC accepts that default parameters be redefined for member functions |
| 421 | // of template class. The new default parameter's value is ignored. |
| 422 | Invalid = true; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 423 | if (getLangOpts().MicrosoftExt) { |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 424 | CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(New); |
| 425 | if (MD && MD->getParent()->getDescribedClassTemplate()) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 426 | // Merge the old default argument into the new parameter. |
| 427 | NewParam->setHasInheritedDefaultArg(); |
| 428 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 429 | NewParam->setUninstantiatedDefaultArg( |
| 430 | OldParam->getUninstantiatedDefaultArg()); |
| 431 | else |
| 432 | NewParam->setDefaultArg(OldParam->getInit()); |
Francois Pichet | cf320c6 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 433 | DiagDefaultParamID = diag::warn_param_default_argument_redefinition; |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 434 | Invalid = false; |
| 435 | } |
| 436 | } |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 437 | |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 438 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 439 | // hint here. Alternatively, we could walk the type-source information |
| 440 | // for NewParam to find the last source location in the type... but it |
| 441 | // isn't worth the effort right now. This is the kind of test case that |
| 442 | // is hard to get right: |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 443 | // int f(int); |
| 444 | // void g(int (*fp)(int) = f); |
| 445 | // void g(int (*fp)(int) = &f); |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 446 | Diag(NewParam->getLocation(), DiagDefaultParamID) |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 447 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 448 | |
| 449 | // Look for the function declaration where the default argument was |
| 450 | // actually written, which may be a declaration prior to Old. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 451 | for (FunctionDecl *Older = Old->getPreviousDecl(); |
| 452 | Older; Older = Older->getPreviousDecl()) { |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 453 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 454 | break; |
| 455 | |
| 456 | OldParam = Older->getParamDecl(p); |
| 457 | } |
| 458 | |
| 459 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 460 | << OldParam->getDefaultArgRange(); |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 461 | } else if (OldParamHasDfl) { |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 462 | // Merge the old default argument into the new parameter. |
| 463 | // It's important to use getInit() here; getDefaultArg() |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 464 | // strips off any top-level ExprWithCleanups. |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 465 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 466 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 467 | NewParam->setUninstantiatedDefaultArg( |
| 468 | OldParam->getUninstantiatedDefaultArg()); |
| 469 | else |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 470 | NewParam->setDefaultArg(OldParam->getInit()); |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 471 | } else if (NewParamHasDfl) { |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 472 | if (New->getDescribedFunctionTemplate()) { |
| 473 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 474 | Diag(NewParam->getLocation(), |
| 475 | diag::err_param_default_argument_template_redecl) |
| 476 | << NewParam->getDefaultArgRange(); |
| 477 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 478 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 479 | } else if (New->getTemplateSpecializationKind() |
| 480 | != TSK_ImplicitInstantiation && |
| 481 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 482 | // C++ [temp.expr.spec]p21: |
| 483 | // Default function arguments shall not be specified in a declaration |
| 484 | // or a definition for one of the following explicit specializations: |
| 485 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 486 | // - the explicit specialization of a member function template; |
| 487 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 488 | // template where the class template specialization to which the |
| 489 | // member function specialization belongs is implicitly |
| 490 | // instantiated. |
| 491 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 492 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 493 | << New->getDeclName() |
| 494 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 495 | } else if (New->getDeclContext()->isDependentContext()) { |
| 496 | // C++ [dcl.fct.default]p6 (DR217): |
| 497 | // Default arguments for a member function of a class template shall |
| 498 | // be specified on the initial declaration of the member function |
| 499 | // within the class template. |
| 500 | // |
| 501 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 502 | // leads me to the conclusion that one cannot add default function |
| 503 | // arguments for an out-of-line definition of a member function of a |
| 504 | // dependent type. |
| 505 | int WhichKind = 2; |
| 506 | if (CXXRecordDecl *Record |
| 507 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 508 | if (Record->getDescribedClassTemplate()) |
| 509 | WhichKind = 0; |
| 510 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 511 | WhichKind = 1; |
| 512 | else |
| 513 | WhichKind = 2; |
| 514 | } |
| 515 | |
| 516 | Diag(NewParam->getLocation(), |
| 517 | diag::err_param_default_argument_member_template_redecl) |
| 518 | << WhichKind |
| 519 | << NewParam->getDefaultArgRange(); |
| 520 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
Richard Smith | b8abff6 | 2012-11-28 03:45:24 +0000 | [diff] [blame] | 524 | // DR1344: If a default argument is added outside a class definition and that |
| 525 | // default argument makes the function a special member function, the program |
| 526 | // is ill-formed. This can only happen for constructors. |
| 527 | if (isa<CXXConstructorDecl>(New) && |
| 528 | New->getMinRequiredArguments() < Old->getMinRequiredArguments()) { |
| 529 | CXXSpecialMember NewSM = getSpecialMember(cast<CXXMethodDecl>(New)), |
| 530 | OldSM = getSpecialMember(cast<CXXMethodDecl>(Old)); |
| 531 | if (NewSM != OldSM) { |
| 532 | ParmVarDecl *NewParam = New->getParamDecl(New->getMinRequiredArguments()); |
| 533 | assert(NewParam->hasDefaultArg()); |
| 534 | Diag(NewParam->getLocation(), diag::err_default_arg_makes_ctor_special) |
| 535 | << NewParam->getDefaultArgRange() << NewSM; |
| 536 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 537 | } |
| 538 | } |
| 539 | |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 540 | // 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] | 541 | // template has a constexpr specifier then all its declarations shall |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 542 | // contain the constexpr specifier. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 543 | if (New->isConstexpr() != Old->isConstexpr()) { |
| 544 | Diag(New->getLocation(), diag::err_constexpr_redecl_mismatch) |
| 545 | << New << New->isConstexpr(); |
| 546 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 547 | Invalid = true; |
| 548 | } |
| 549 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 550 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 551 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 553 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 556 | /// \brief Merge the exception specifications of two variable declarations. |
| 557 | /// |
| 558 | /// This is called when there's a redeclaration of a VarDecl. The function |
| 559 | /// checks if the redeclaration might have an exception specification and |
| 560 | /// validates compatibility and merges the specs if necessary. |
| 561 | void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) { |
| 562 | // Shortcut if exceptions are disabled. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 563 | if (!getLangOpts().CXXExceptions) |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 564 | return; |
| 565 | |
| 566 | assert(Context.hasSameType(New->getType(), Old->getType()) && |
| 567 | "Should only be called if types are otherwise the same."); |
| 568 | |
| 569 | QualType NewType = New->getType(); |
| 570 | QualType OldType = Old->getType(); |
| 571 | |
| 572 | // We're only interested in pointers and references to functions, as well |
| 573 | // as pointers to member functions. |
| 574 | if (const ReferenceType *R = NewType->getAs<ReferenceType>()) { |
| 575 | NewType = R->getPointeeType(); |
| 576 | OldType = OldType->getAs<ReferenceType>()->getPointeeType(); |
| 577 | } else if (const PointerType *P = NewType->getAs<PointerType>()) { |
| 578 | NewType = P->getPointeeType(); |
| 579 | OldType = OldType->getAs<PointerType>()->getPointeeType(); |
| 580 | } else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) { |
| 581 | NewType = M->getPointeeType(); |
| 582 | OldType = OldType->getAs<MemberPointerType>()->getPointeeType(); |
| 583 | } |
| 584 | |
| 585 | if (!NewType->isFunctionProtoType()) |
| 586 | return; |
| 587 | |
| 588 | // There's lots of special cases for functions. For function pointers, system |
| 589 | // libraries are hopefully not as broken so that we don't need these |
| 590 | // workarounds. |
| 591 | if (CheckEquivalentExceptionSpec( |
| 592 | OldType->getAs<FunctionProtoType>(), Old->getLocation(), |
| 593 | NewType->getAs<FunctionProtoType>(), New->getLocation())) { |
| 594 | New->setInvalidDecl(); |
| 595 | } |
| 596 | } |
| 597 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 598 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 599 | /// function declaration are well-formed according to C++ |
| 600 | /// [dcl.fct.default]. |
| 601 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 602 | unsigned NumParams = FD->getNumParams(); |
| 603 | unsigned p; |
| 604 | |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 605 | bool IsLambda = FD->getOverloadedOperator() == OO_Call && |
| 606 | isa<CXXMethodDecl>(FD) && |
| 607 | cast<CXXMethodDecl>(FD)->getParent()->isLambda(); |
| 608 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 609 | // Find first parameter with a default argument |
| 610 | for (p = 0; p < NumParams; ++p) { |
| 611 | ParmVarDecl *Param = FD->getParamDecl(p); |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 612 | if (Param->hasDefaultArg()) { |
| 613 | // C++11 [expr.prim.lambda]p5: |
| 614 | // [...] Default arguments (8.3.6) shall not be specified in the |
| 615 | // parameter-declaration-clause of a lambda-declarator. |
| 616 | // |
| 617 | // FIXME: Core issue 974 strikes this sentence, we only provide an |
| 618 | // extension warning. |
| 619 | if (IsLambda) |
| 620 | Diag(Param->getLocation(), diag::ext_lambda_default_arguments) |
| 621 | << Param->getDefaultArgRange(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 622 | break; |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 623 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | // C++ [dcl.fct.default]p4: |
| 627 | // In a given function declaration, all parameters |
| 628 | // subsequent to a parameter with a default argument shall |
| 629 | // have default arguments supplied in this or previous |
| 630 | // declarations. A default argument shall not be redefined |
| 631 | // by a later declaration (not even to the same value). |
| 632 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 634 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 635 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 636 | if (Param->isInvalidDecl()) |
| 637 | /* We already complained about this parameter. */; |
| 638 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 640 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 641 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 642 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 643 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 644 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 646 | LastMissingDefaultArg = p; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | if (LastMissingDefaultArg > 0) { |
| 651 | // Some default arguments were missing. Clear out all of the |
| 652 | // default arguments up to (and including) the last missing |
| 653 | // default argument, so that we leave the function parameters |
| 654 | // in a semantically valid state. |
| 655 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 656 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 657 | if (Param->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 658 | Param->setDefaultArg(0); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 663 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 664 | // CheckConstexprParameterTypes - Check whether a function's parameter types |
| 665 | // 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] | 666 | // diagnostic and return false. |
| 667 | static bool CheckConstexprParameterTypes(Sema &SemaRef, |
| 668 | const FunctionDecl *FD) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 669 | unsigned ArgIndex = 0; |
| 670 | const FunctionProtoType *FT = FD->getType()->getAs<FunctionProtoType>(); |
| 671 | for (FunctionProtoType::arg_type_iterator i = FT->arg_type_begin(), |
| 672 | e = FT->arg_type_end(); i != e; ++i, ++ArgIndex) { |
| 673 | const ParmVarDecl *PD = FD->getParamDecl(ArgIndex); |
| 674 | SourceLocation ParamLoc = PD->getLocation(); |
| 675 | if (!(*i)->isDependentType() && |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 676 | SemaRef.RequireLiteralType(ParamLoc, *i, |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 677 | diag::err_constexpr_non_literal_param, |
| 678 | ArgIndex+1, PD->getSourceRange(), |
| 679 | isa<CXXConstructorDecl>(FD))) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 680 | return false; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 681 | } |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 682 | return true; |
| 683 | } |
| 684 | |
| 685 | /// \brief Get diagnostic %select index for tag kind for |
| 686 | /// record diagnostic message. |
| 687 | /// WARNING: Indexes apply to particular diagnostics only! |
| 688 | /// |
| 689 | /// \returns diagnostic %select index. |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 690 | static unsigned getRecordDiagFromTagKind(TagTypeKind Tag) { |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 691 | switch (Tag) { |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 692 | case TTK_Struct: return 0; |
| 693 | case TTK_Interface: return 1; |
| 694 | case TTK_Class: return 2; |
| 695 | default: llvm_unreachable("Invalid tag kind for record diagnostic!"); |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 696 | } |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | // CheckConstexprFunctionDecl - Check whether a function declaration satisfies |
| 700 | // the requirements of a constexpr function definition or a constexpr |
| 701 | // constructor definition. If so, return true. If not, produce appropriate |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 702 | // diagnostics and return false. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 703 | // |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 704 | // This implements C++11 [dcl.constexpr]p3,4, as amended by DR1360. |
| 705 | bool Sema::CheckConstexprFunctionDecl(const FunctionDecl *NewFD) { |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 706 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| 707 | if (MD && MD->isInstance()) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 708 | // C++11 [dcl.constexpr]p4: |
| 709 | // The definition of a constexpr constructor shall satisfy the following |
| 710 | // constraints: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 711 | // - the class shall not have any virtual base classes; |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 712 | const CXXRecordDecl *RD = MD->getParent(); |
| 713 | if (RD->getNumVBases()) { |
| 714 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual_base) |
| 715 | << isa<CXXConstructorDecl>(NewFD) |
| 716 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases(); |
| 717 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 718 | E = RD->vbases_end(); I != E; ++I) |
| 719 | Diag(I->getLocStart(), |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 720 | diag::note_constexpr_virtual_base_here) << I->getSourceRange(); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 721 | return false; |
| 722 | } |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | if (!isa<CXXConstructorDecl>(NewFD)) { |
| 726 | // C++11 [dcl.constexpr]p3: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 727 | // The definition of a constexpr function shall satisfy the following |
| 728 | // constraints: |
| 729 | // - it shall not be virtual; |
| 730 | const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD); |
| 731 | if (Method && Method->isVirtual()) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 732 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 733 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 734 | // If it's not obvious why this function is virtual, find an overridden |
| 735 | // function which uses the 'virtual' keyword. |
| 736 | const CXXMethodDecl *WrittenVirtual = Method; |
| 737 | while (!WrittenVirtual->isVirtualAsWritten()) |
| 738 | WrittenVirtual = *WrittenVirtual->begin_overridden_methods(); |
| 739 | if (WrittenVirtual != Method) |
| 740 | Diag(WrittenVirtual->getLocation(), |
| 741 | diag::note_overridden_virtual_function); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 742 | return false; |
| 743 | } |
| 744 | |
| 745 | // - its return type shall be a literal type; |
| 746 | QualType RT = NewFD->getResultType(); |
| 747 | if (!RT->isDependentType() && |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 748 | RequireLiteralType(NewFD->getLocation(), RT, |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 749 | diag::err_constexpr_non_literal_return)) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 750 | return false; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 753 | // - each of its parameter types shall be a literal type; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 754 | if (!CheckConstexprParameterTypes(*this, NewFD)) |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 755 | return false; |
| 756 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 757 | return true; |
| 758 | } |
| 759 | |
| 760 | /// Check the given declaration statement is legal within a constexpr function |
| 761 | /// body. C++0x [dcl.constexpr]p3,p4. |
| 762 | /// |
| 763 | /// \return true if the body is OK, false if we have diagnosed a problem. |
| 764 | static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl, |
| 765 | DeclStmt *DS) { |
| 766 | // C++0x [dcl.constexpr]p3 and p4: |
| 767 | // The definition of a constexpr function(p3) or constructor(p4) [...] shall |
| 768 | // contain only |
| 769 | for (DeclStmt::decl_iterator DclIt = DS->decl_begin(), |
| 770 | DclEnd = DS->decl_end(); DclIt != DclEnd; ++DclIt) { |
| 771 | switch ((*DclIt)->getKind()) { |
| 772 | case Decl::StaticAssert: |
| 773 | case Decl::Using: |
| 774 | case Decl::UsingShadow: |
| 775 | case Decl::UsingDirective: |
| 776 | case Decl::UnresolvedUsingTypename: |
| 777 | // - static_assert-declarations |
| 778 | // - using-declarations, |
| 779 | // - using-directives, |
| 780 | continue; |
| 781 | |
| 782 | case Decl::Typedef: |
| 783 | case Decl::TypeAlias: { |
| 784 | // - typedef declarations and alias-declarations that do not define |
| 785 | // classes or enumerations, |
| 786 | TypedefNameDecl *TN = cast<TypedefNameDecl>(*DclIt); |
| 787 | if (TN->getUnderlyingType()->isVariablyModifiedType()) { |
| 788 | // Don't allow variably-modified types in constexpr functions. |
| 789 | TypeLoc TL = TN->getTypeSourceInfo()->getTypeLoc(); |
| 790 | SemaRef.Diag(TL.getBeginLoc(), diag::err_constexpr_vla) |
| 791 | << TL.getSourceRange() << TL.getType() |
| 792 | << isa<CXXConstructorDecl>(Dcl); |
| 793 | return false; |
| 794 | } |
| 795 | continue; |
| 796 | } |
| 797 | |
| 798 | case Decl::Enum: |
| 799 | case Decl::CXXRecord: |
| 800 | // As an extension, we allow the declaration (but not the definition) of |
| 801 | // classes and enumerations in all declarations, not just in typedef and |
| 802 | // alias declarations. |
| 803 | if (cast<TagDecl>(*DclIt)->isThisDeclarationADefinition()) { |
| 804 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_type_definition) |
| 805 | << isa<CXXConstructorDecl>(Dcl); |
| 806 | return false; |
| 807 | } |
| 808 | continue; |
| 809 | |
| 810 | case Decl::Var: |
| 811 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_var_declaration) |
| 812 | << isa<CXXConstructorDecl>(Dcl); |
| 813 | return false; |
| 814 | |
| 815 | default: |
| 816 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 817 | << isa<CXXConstructorDecl>(Dcl); |
| 818 | return false; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | return true; |
| 823 | } |
| 824 | |
| 825 | /// Check that the given field is initialized within a constexpr constructor. |
| 826 | /// |
| 827 | /// \param Dcl The constexpr constructor being checked. |
| 828 | /// \param Field The field being checked. This may be a member of an anonymous |
| 829 | /// struct or union nested within the class being checked. |
| 830 | /// \param Inits All declarations, including anonymous struct/union members and |
| 831 | /// indirect members, for which any initialization was provided. |
| 832 | /// \param Diagnosed Set to true if an error is produced. |
| 833 | static void CheckConstexprCtorInitializer(Sema &SemaRef, |
| 834 | const FunctionDecl *Dcl, |
| 835 | FieldDecl *Field, |
| 836 | llvm::SmallSet<Decl*, 16> &Inits, |
| 837 | bool &Diagnosed) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 838 | if (Field->isUnnamedBitfield()) |
| 839 | return; |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 840 | |
| 841 | if (Field->isAnonymousStructOrUnion() && |
| 842 | Field->getType()->getAsCXXRecordDecl()->isEmpty()) |
| 843 | return; |
| 844 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 845 | if (!Inits.count(Field)) { |
| 846 | if (!Diagnosed) { |
| 847 | SemaRef.Diag(Dcl->getLocation(), diag::err_constexpr_ctor_missing_init); |
| 848 | Diagnosed = true; |
| 849 | } |
| 850 | SemaRef.Diag(Field->getLocation(), diag::note_constexpr_ctor_missing_init); |
| 851 | } else if (Field->isAnonymousStructOrUnion()) { |
| 852 | const RecordDecl *RD = Field->getType()->castAs<RecordType>()->getDecl(); |
| 853 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 854 | I != E; ++I) |
| 855 | // If an anonymous union contains an anonymous struct of which any member |
| 856 | // is initialized, all members must be initialized. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 857 | if (!RD->isUnion() || Inits.count(*I)) |
| 858 | CheckConstexprCtorInitializer(SemaRef, Dcl, *I, Inits, Diagnosed); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
| 862 | /// Check the body for the given constexpr function declaration only contains |
| 863 | /// the permitted types of statement. C++11 [dcl.constexpr]p3,p4. |
| 864 | /// |
| 865 | /// \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] | 866 | bool Sema::CheckConstexprFunctionBody(const FunctionDecl *Dcl, Stmt *Body) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 867 | if (isa<CXXTryStmt>(Body)) { |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 868 | // C++11 [dcl.constexpr]p3: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 869 | // The definition of a constexpr function shall satisfy the following |
| 870 | // constraints: [...] |
| 871 | // - its function-body shall be = delete, = default, or a |
| 872 | // compound-statement |
| 873 | // |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 874 | // C++11 [dcl.constexpr]p4: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 875 | // In the definition of a constexpr constructor, [...] |
| 876 | // - its function-body shall not be a function-try-block; |
| 877 | Diag(Body->getLocStart(), diag::err_constexpr_function_try_block) |
| 878 | << isa<CXXConstructorDecl>(Dcl); |
| 879 | return false; |
| 880 | } |
| 881 | |
| 882 | // - its function-body shall be [...] a compound-statement that contains only |
| 883 | CompoundStmt *CompBody = cast<CompoundStmt>(Body); |
| 884 | |
| 885 | llvm::SmallVector<SourceLocation, 4> ReturnStmts; |
| 886 | for (CompoundStmt::body_iterator BodyIt = CompBody->body_begin(), |
| 887 | BodyEnd = CompBody->body_end(); BodyIt != BodyEnd; ++BodyIt) { |
| 888 | switch ((*BodyIt)->getStmtClass()) { |
| 889 | case Stmt::NullStmtClass: |
| 890 | // - null statements, |
| 891 | continue; |
| 892 | |
| 893 | case Stmt::DeclStmtClass: |
| 894 | // - static_assert-declarations |
| 895 | // - using-declarations, |
| 896 | // - using-directives, |
| 897 | // - typedef declarations and alias-declarations that do not define |
| 898 | // classes or enumerations, |
| 899 | if (!CheckConstexprDeclStmt(*this, Dcl, cast<DeclStmt>(*BodyIt))) |
| 900 | return false; |
| 901 | continue; |
| 902 | |
| 903 | case Stmt::ReturnStmtClass: |
| 904 | // - and exactly one return statement; |
| 905 | if (isa<CXXConstructorDecl>(Dcl)) |
| 906 | break; |
| 907 | |
| 908 | ReturnStmts.push_back((*BodyIt)->getLocStart()); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 909 | continue; |
| 910 | |
| 911 | default: |
| 912 | break; |
| 913 | } |
| 914 | |
| 915 | Diag((*BodyIt)->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 916 | << isa<CXXConstructorDecl>(Dcl); |
| 917 | return false; |
| 918 | } |
| 919 | |
| 920 | if (const CXXConstructorDecl *Constructor |
| 921 | = dyn_cast<CXXConstructorDecl>(Dcl)) { |
| 922 | const CXXRecordDecl *RD = Constructor->getParent(); |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 923 | // DR1359: |
| 924 | // - every non-variant non-static data member and base class sub-object |
| 925 | // shall be initialized; |
| 926 | // - if the class is a non-empty union, or for each non-empty anonymous |
| 927 | // union member of a non-union class, exactly one non-static data member |
| 928 | // shall be initialized; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 929 | if (RD->isUnion()) { |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 930 | if (Constructor->getNumCtorInitializers() == 0 && !RD->isEmpty()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 931 | Diag(Dcl->getLocation(), diag::err_constexpr_union_ctor_no_init); |
| 932 | return false; |
| 933 | } |
Richard Smith | 6e43375 | 2011-10-10 16:38:04 +0000 | [diff] [blame] | 934 | } else if (!Constructor->isDependentContext() && |
| 935 | !Constructor->isDelegatingConstructor()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 936 | assert(RD->getNumVBases() == 0 && "constexpr ctor with virtual bases"); |
| 937 | |
| 938 | // Skip detailed checking if we have enough initializers, and we would |
| 939 | // allow at most one initializer per member. |
| 940 | bool AnyAnonStructUnionMembers = false; |
| 941 | unsigned Fields = 0; |
| 942 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 943 | E = RD->field_end(); I != E; ++I, ++Fields) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 944 | if (I->isAnonymousStructOrUnion()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 945 | AnyAnonStructUnionMembers = true; |
| 946 | break; |
| 947 | } |
| 948 | } |
| 949 | if (AnyAnonStructUnionMembers || |
| 950 | Constructor->getNumCtorInitializers() != RD->getNumBases() + Fields) { |
| 951 | // Check initialization of non-static data members. Base classes are |
| 952 | // always initialized so do not need to be checked. Dependent bases |
| 953 | // might not have initializers in the member initializer list. |
| 954 | llvm::SmallSet<Decl*, 16> Inits; |
| 955 | for (CXXConstructorDecl::init_const_iterator |
| 956 | I = Constructor->init_begin(), E = Constructor->init_end(); |
| 957 | I != E; ++I) { |
| 958 | if (FieldDecl *FD = (*I)->getMember()) |
| 959 | Inits.insert(FD); |
| 960 | else if (IndirectFieldDecl *ID = (*I)->getIndirectMember()) |
| 961 | Inits.insert(ID->chain_begin(), ID->chain_end()); |
| 962 | } |
| 963 | |
| 964 | bool Diagnosed = false; |
| 965 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 966 | E = RD->field_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 967 | CheckConstexprCtorInitializer(*this, Dcl, *I, Inits, Diagnosed); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 968 | if (Diagnosed) |
| 969 | return false; |
| 970 | } |
| 971 | } |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 972 | } else { |
| 973 | if (ReturnStmts.empty()) { |
| 974 | Diag(Dcl->getLocation(), diag::err_constexpr_body_no_return); |
| 975 | return false; |
| 976 | } |
| 977 | if (ReturnStmts.size() > 1) { |
| 978 | Diag(ReturnStmts.back(), diag::err_constexpr_body_multiple_return); |
| 979 | for (unsigned I = 0; I < ReturnStmts.size() - 1; ++I) |
| 980 | Diag(ReturnStmts[I], diag::note_constexpr_body_previous_return); |
| 981 | return false; |
| 982 | } |
| 983 | } |
| 984 | |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 985 | // C++11 [dcl.constexpr]p5: |
| 986 | // if no function argument values exist such that the function invocation |
| 987 | // substitution would produce a constant expression, the program is |
| 988 | // ill-formed; no diagnostic required. |
| 989 | // C++11 [dcl.constexpr]p3: |
| 990 | // - every constructor call and implicit conversion used in initializing the |
| 991 | // return value shall be one of those allowed in a constant expression. |
| 992 | // C++11 [dcl.constexpr]p4: |
| 993 | // - every constructor involved in initializing non-static data members and |
| 994 | // base class sub-objects shall be a constexpr constructor. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 995 | llvm::SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 996 | if (!Expr::isPotentialConstantExpr(Dcl, Diags)) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 997 | Diag(Dcl->getLocation(), diag::err_constexpr_function_never_constant_expr) |
| 998 | << isa<CXXConstructorDecl>(Dcl); |
| 999 | for (size_t I = 0, N = Diags.size(); I != N; ++I) |
| 1000 | Diag(Diags[I].first, Diags[I].second); |
| 1001 | return false; |
| 1002 | } |
| 1003 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1004 | return true; |
| 1005 | } |
| 1006 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1007 | /// isCurrentClassName - Determine whether the identifier II is the |
| 1008 | /// name of the class type currently being defined. In the case of |
| 1009 | /// nested classes, this will only return true if II is the name of |
| 1010 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1011 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 1012 | const CXXScopeSpec *SS) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1013 | assert(getLangOpts().CPlusPlus && "No class names in C!"); |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 1014 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1015 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1016 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 1017 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1018 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 1019 | } else |
| 1020 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 1021 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 1022 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1023 | return &II == CurDecl->getIdentifier(); |
| 1024 | else |
| 1025 | return false; |
| 1026 | } |
| 1027 | |
Douglas Gregor | 229d47a | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1028 | /// \brief Determine whether the given class is a base class of the given |
| 1029 | /// class, including looking at dependent bases. |
| 1030 | static bool findCircularInheritance(const CXXRecordDecl *Class, |
| 1031 | const CXXRecordDecl *Current) { |
| 1032 | SmallVector<const CXXRecordDecl*, 8> Queue; |
| 1033 | |
| 1034 | Class = Class->getCanonicalDecl(); |
| 1035 | while (true) { |
| 1036 | for (CXXRecordDecl::base_class_const_iterator I = Current->bases_begin(), |
| 1037 | E = Current->bases_end(); |
| 1038 | I != E; ++I) { |
| 1039 | CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 1040 | if (!Base) |
| 1041 | continue; |
| 1042 | |
| 1043 | Base = Base->getDefinition(); |
| 1044 | if (!Base) |
| 1045 | continue; |
| 1046 | |
| 1047 | if (Base->getCanonicalDecl() == Class) |
| 1048 | return true; |
| 1049 | |
| 1050 | Queue.push_back(Base); |
| 1051 | } |
| 1052 | |
| 1053 | if (Queue.empty()) |
| 1054 | return false; |
| 1055 | |
| 1056 | Current = Queue.back(); |
| 1057 | Queue.pop_back(); |
| 1058 | } |
| 1059 | |
| 1060 | return false; |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1064 | /// |
| 1065 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 1066 | /// and returns NULL otherwise. |
| 1067 | CXXBaseSpecifier * |
| 1068 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 1069 | SourceRange SpecifierRange, |
| 1070 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1071 | TypeSourceInfo *TInfo, |
| 1072 | SourceLocation EllipsisLoc) { |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1073 | QualType BaseType = TInfo->getType(); |
| 1074 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1075 | // C++ [class.union]p1: |
| 1076 | // A union shall not have base classes. |
| 1077 | if (Class->isUnion()) { |
| 1078 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 1079 | << SpecifierRange; |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1083 | if (EllipsisLoc.isValid() && |
| 1084 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 1085 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1086 | << TInfo->getTypeLoc().getSourceRange(); |
| 1087 | EllipsisLoc = SourceLocation(); |
| 1088 | } |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1089 | |
| 1090 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
| 1091 | |
| 1092 | if (BaseType->isDependentType()) { |
| 1093 | // Make sure that we don't have circular inheritance among our dependent |
| 1094 | // bases. For non-dependent bases, the check for completeness below handles |
| 1095 | // this. |
| 1096 | if (CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl()) { |
| 1097 | if (BaseDecl->getCanonicalDecl() == Class->getCanonicalDecl() || |
| 1098 | ((BaseDecl = BaseDecl->getDefinition()) && |
Douglas Gregor | 229d47a | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1099 | findCircularInheritance(Class, BaseDecl))) { |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1100 | Diag(BaseLoc, diag::err_circular_inheritance) |
| 1101 | << BaseType << Context.getTypeDeclType(Class); |
| 1102 | |
| 1103 | if (BaseDecl->getCanonicalDecl() != Class->getCanonicalDecl()) |
| 1104 | Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 1105 | << BaseType; |
| 1106 | |
| 1107 | return 0; |
| 1108 | } |
| 1109 | } |
| 1110 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1112 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1113 | Access, TInfo, EllipsisLoc); |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1114 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1115 | |
| 1116 | // Base specifiers must be record types. |
| 1117 | if (!BaseType->isRecordType()) { |
| 1118 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | // C++ [class.union]p1: |
| 1123 | // A union shall not be used as a base class. |
| 1124 | if (BaseType->isUnionType()) { |
| 1125 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | // C++ [class.derived]p2: |
| 1130 | // The class-name in a base-specifier shall not be an incompletely |
| 1131 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | if (RequireCompleteType(BaseLoc, BaseType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1133 | diag::err_incomplete_base_class, SpecifierRange)) { |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1134 | Class->setInvalidDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1135 | return 0; |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1136 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1137 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1138 | // 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] | 1139 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1140 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1141 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1142 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1143 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 1144 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 1145 | |
Anders Carlsson | 1d20927 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 1146 | // C++ [class]p3: |
| 1147 | // If a class is marked final and it appears as a base-type-specifier in |
| 1148 | // base-clause, the program is ill-formed. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1149 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 1150 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
| 1151 | << CXXBaseDecl->getDeclName(); |
| 1152 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 1153 | << CXXBaseDecl->getDeclName(); |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1157 | if (BaseDecl->isInvalidDecl()) |
| 1158 | Class->setInvalidDecl(); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1159 | |
| 1160 | // Create the base specifier. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1161 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1162 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1163 | Access, TInfo, EllipsisLoc); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1166 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 1167 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | /// example: |
| 1169 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1170 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1171 | BaseResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1172 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1173 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1174 | ParsedType basetype, SourceLocation BaseLoc, |
| 1175 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1176 | if (!classdecl) |
| 1177 | return true; |
| 1178 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1179 | AdjustDeclIfTemplate(classdecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1180 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 1181 | if (!Class) |
| 1182 | return true; |
| 1183 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1184 | TypeSourceInfo *TInfo = 0; |
| 1185 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1186 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1187 | if (EllipsisLoc.isInvalid() && |
| 1188 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1189 | UPPC_BaseType)) |
| 1190 | return true; |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1191 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1192 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1193 | Virtual, Access, TInfo, |
| 1194 | EllipsisLoc)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1195 | return BaseSpec; |
Douglas Gregor | 8a50fe0 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 1196 | else |
| 1197 | Class->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1198 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1199 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1200 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1201 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1202 | /// \brief Performs the actual work of attaching the given base class |
| 1203 | /// specifiers to a C++ class. |
| 1204 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 1205 | unsigned NumBases) { |
| 1206 | if (NumBases == 0) |
| 1207 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1208 | |
| 1209 | // Used to keep track of which base types we have already seen, so |
| 1210 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1211 | // that the key is always the unqualified canonical type of the base |
| 1212 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1213 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 1214 | |
| 1215 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1216 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1217 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1218 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1220 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1221 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1222 | |
| 1223 | CXXBaseSpecifier *&KnownBase = KnownBaseTypes[NewBaseType]; |
| 1224 | if (KnownBase) { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1225 | // C++ [class.mi]p3: |
| 1226 | // A class shall not be specified as a direct base class of a |
| 1227 | // derived class more than once. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1228 | Diag(Bases[idx]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1229 | diag::err_duplicate_base_class) |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1230 | << KnownBase->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1231 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1232 | |
| 1233 | // Delete the duplicate base class specifier; we're going to |
| 1234 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1235 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1236 | |
| 1237 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1238 | } else { |
| 1239 | // Okay, add this new base class. |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1240 | KnownBase = Bases[idx]; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1241 | Bases[NumGoodBases++] = Bases[idx]; |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1242 | if (const RecordType *Record = NewBaseType->getAs<RecordType>()) { |
| 1243 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
| 1244 | if (Class->isInterface() && |
| 1245 | (!RD->isInterface() || |
| 1246 | KnownBase->getAccessSpecifier() != AS_public)) { |
| 1247 | // The Microsoft extension __interface does not permit bases that |
| 1248 | // are not themselves public interfaces. |
| 1249 | Diag(KnownBase->getLocStart(), diag::err_invalid_base_in_interface) |
| 1250 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getName() |
| 1251 | << RD->getSourceRange(); |
| 1252 | Invalid = true; |
| 1253 | } |
| 1254 | if (RD->hasAttr<WeakAttr>()) |
| 1255 | Class->addAttr(::new (Context) WeakAttr(SourceRange(), Context)); |
| 1256 | } |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 1261 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1262 | |
| 1263 | // Delete the remaining (good) base class specifiers, since their |
| 1264 | // data has been copied into the CXXRecordDecl. |
| 1265 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1266 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1267 | |
| 1268 | return Invalid; |
| 1269 | } |
| 1270 | |
| 1271 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 1272 | /// class, after checking whether there are any duplicate base |
| 1273 | /// classes. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1274 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, CXXBaseSpecifier **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1275 | unsigned NumBases) { |
| 1276 | if (!ClassDecl || !Bases || !NumBases) |
| 1277 | return; |
| 1278 | |
| 1279 | AdjustDeclIfTemplate(ClassDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1280 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1281 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1282 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1283 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1284 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 1285 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 1286 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 1287 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 1288 | return ICT->getDecl(); |
| 1289 | else |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1293 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1294 | /// derived from the type \p Base. |
| 1295 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1296 | if (!getLangOpts().CPlusPlus) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1297 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1298 | |
| 1299 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 1300 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1301 | return false; |
| 1302 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1303 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 1304 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1305 | return false; |
| 1306 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1307 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 1308 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1312 | /// derived from the type \p Base. |
| 1313 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1314 | if (!getLangOpts().CPlusPlus) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1315 | return false; |
| 1316 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1317 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 1318 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1319 | return false; |
| 1320 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1321 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 1322 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1323 | return false; |
| 1324 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1325 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 1326 | } |
| 1327 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1328 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1329 | CXXCastPath &BasePathArray) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1330 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 1331 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 1332 | |
| 1333 | const CXXBasePath &Path = Paths.front(); |
| 1334 | |
| 1335 | // We first go backward and check if we have a virtual base. |
| 1336 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 1337 | // the nearest virtual base. |
| 1338 | unsigned Start = 0; |
| 1339 | for (unsigned I = Path.size(); I != 0; --I) { |
| 1340 | if (Path[I - 1].Base->isVirtual()) { |
| 1341 | Start = I - 1; |
| 1342 | break; |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | // Now add all bases. |
| 1347 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1348 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1351 | /// \brief Determine whether the given base path includes a virtual |
| 1352 | /// base class. |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1353 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 1354 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 1355 | BEnd = BasePath.end(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1356 | B != BEnd; ++B) |
| 1357 | if ((*B)->isVirtual()) |
| 1358 | return true; |
| 1359 | |
| 1360 | return false; |
| 1361 | } |
| 1362 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1363 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 1364 | /// conversion (where Derived and Base are class types) is |
| 1365 | /// well-formed, meaning that the conversion is unambiguous (and |
| 1366 | /// that all of the base classes are accessible). Returns true |
| 1367 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 1368 | /// otherwise. Loc is the location where this routine should point to |
| 1369 | /// if there is an error, and Range is the source range to highlight |
| 1370 | /// if there is an error. |
| 1371 | bool |
| 1372 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1373 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1374 | unsigned AmbigiousBaseConvID, |
| 1375 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1376 | DeclarationName Name, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1377 | CXXCastPath *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1378 | // First, determine whether the path from Derived to Base is |
| 1379 | // ambiguous. This is slightly more expensive than checking whether |
| 1380 | // the Derived to Base conversion exists, because here we need to |
| 1381 | // explore multiple paths to determine if there is an ambiguity. |
| 1382 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1383 | /*DetectVirtual=*/false); |
| 1384 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1385 | assert(DerivationOkay && |
| 1386 | "Can only be used with a derived-to-base conversion"); |
| 1387 | (void)DerivationOkay; |
| 1388 | |
| 1389 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1390 | if (InaccessibleBaseID) { |
| 1391 | // Check that the base class can be accessed. |
| 1392 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 1393 | InaccessibleBaseID)) { |
| 1394 | case AR_inaccessible: |
| 1395 | return true; |
| 1396 | case AR_accessible: |
| 1397 | case AR_dependent: |
| 1398 | case AR_delayed: |
| 1399 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1400 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1401 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1402 | |
| 1403 | // Build a base path if necessary. |
| 1404 | if (BasePath) |
| 1405 | BuildBasePathArray(Paths, *BasePath); |
| 1406 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | // We know that the derived-to-base conversion is ambiguous, and |
| 1410 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 1411 | // search just one more time to compute all of the possible paths so |
| 1412 | // that we can print them out. This is more expensive than any of |
| 1413 | // the previous derived-to-base checks we've done, but at this point |
| 1414 | // performance isn't as much of an issue. |
| 1415 | Paths.clear(); |
| 1416 | Paths.setRecordingPaths(true); |
| 1417 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1418 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 1419 | (void)StillOkay; |
| 1420 | |
| 1421 | // Build up a textual representation of the ambiguous paths, e.g., |
| 1422 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 1423 | // conversions in the diagnostic. We only print one of the paths |
| 1424 | // to each base class subobject. |
| 1425 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1426 | |
| 1427 | Diag(Loc, AmbigiousBaseConvID) |
| 1428 | << Derived << Base << PathDisplayStr << Range << Name; |
| 1429 | return true; |
| 1430 | } |
| 1431 | |
| 1432 | bool |
| 1433 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1434 | SourceLocation Loc, SourceRange Range, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1435 | CXXCastPath *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1436 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1437 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1438 | IgnoreAccess ? 0 |
| 1439 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1440 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1441 | Loc, Range, DeclarationName(), |
| 1442 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | |
| 1446 | /// @brief Builds a string representing ambiguous paths from a |
| 1447 | /// specific derived class to different subobjects of the same base |
| 1448 | /// class. |
| 1449 | /// |
| 1450 | /// This function builds a string that can be used in error messages |
| 1451 | /// to show the different paths that one can take through the |
| 1452 | /// inheritance hierarchy to go from the derived class to different |
| 1453 | /// subobjects of a base class. The result looks something like this: |
| 1454 | /// @code |
| 1455 | /// struct D -> struct B -> struct A |
| 1456 | /// struct D -> struct C -> struct A |
| 1457 | /// @endcode |
| 1458 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 1459 | std::string PathDisplayStr; |
| 1460 | std::set<unsigned> DisplayedPaths; |
| 1461 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1462 | Path != Paths.end(); ++Path) { |
| 1463 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 1464 | // We haven't displayed a path to this particular base |
| 1465 | // class subobject yet. |
| 1466 | PathDisplayStr += "\n "; |
| 1467 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 1468 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 1469 | Element != Path->end(); ++Element) |
| 1470 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | return PathDisplayStr; |
| 1475 | } |
| 1476 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1477 | //===----------------------------------------------------------------------===// |
| 1478 | // C++ class member Handling |
| 1479 | //===----------------------------------------------------------------------===// |
| 1480 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1481 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1482 | bool Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 1483 | SourceLocation ASLoc, |
| 1484 | SourceLocation ColonLoc, |
| 1485 | AttributeList *Attrs) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1486 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1487 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1488 | ASLoc, ColonLoc); |
| 1489 | CurContext->addHiddenDecl(ASDecl); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1490 | return ProcessAccessDeclAttributeList(ASDecl, Attrs); |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1493 | /// CheckOverrideControl - Check C++11 override control semantics. |
| 1494 | void Sema::CheckOverrideControl(Decl *D) { |
Richard Smith | cddbc1d | 2012-09-06 18:32:18 +0000 | [diff] [blame] | 1495 | if (D->isInvalidDecl()) |
| 1496 | return; |
| 1497 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1498 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1499 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1500 | // Do we know which functions this declaration might be overriding? |
| 1501 | bool OverridesAreKnown = !MD || |
| 1502 | (!MD->getParent()->hasAnyDependentBases() && |
| 1503 | !MD->getType()->isDependentType()); |
Anders Carlsson | 3ffe183 | 2011-01-20 06:33:26 +0000 | [diff] [blame] | 1504 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1505 | if (!MD || !MD->isVirtual()) { |
| 1506 | if (OverridesAreKnown) { |
| 1507 | if (OverrideAttr *OA = D->getAttr<OverrideAttr>()) { |
| 1508 | Diag(OA->getLocation(), |
| 1509 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1510 | << "override" << FixItHint::CreateRemoval(OA->getLocation()); |
| 1511 | D->dropAttr<OverrideAttr>(); |
| 1512 | } |
| 1513 | if (FinalAttr *FA = D->getAttr<FinalAttr>()) { |
| 1514 | Diag(FA->getLocation(), |
| 1515 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1516 | << "final" << FixItHint::CreateRemoval(FA->getLocation()); |
| 1517 | D->dropAttr<FinalAttr>(); |
| 1518 | } |
| 1519 | } |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1520 | return; |
| 1521 | } |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1522 | |
| 1523 | if (!OverridesAreKnown) |
| 1524 | return; |
| 1525 | |
| 1526 | // C++11 [class.virtual]p5: |
| 1527 | // If a virtual function is marked with the virt-specifier override and |
| 1528 | // does not override a member function of a base class, the program is |
| 1529 | // ill-formed. |
| 1530 | bool HasOverriddenMethods = |
| 1531 | MD->begin_overridden_methods() != MD->end_overridden_methods(); |
| 1532 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) |
| 1533 | Diag(MD->getLocation(), diag::err_function_marked_override_not_overriding) |
| 1534 | << MD->getDeclName(); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1537 | /// CheckIfOverriddenFunctionIsMarkedFinal - Checks whether a virtual member |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1538 | /// function overrides a virtual member function marked 'final', according to |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1539 | /// C++11 [class.virtual]p4. |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1540 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 1541 | const CXXMethodDecl *Old) { |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1542 | if (!Old->hasAttr<FinalAttr>()) |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1543 | return false; |
| 1544 | |
| 1545 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 1546 | << New->getDeclName(); |
| 1547 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 1548 | return true; |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1551 | static bool InitializationHasSideEffects(const FieldDecl &FD) { |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 1552 | const Type *T = FD.getType()->getBaseElementTypeUnsafe(); |
| 1553 | // FIXME: Destruction of ObjC lifetime types has side-effects. |
| 1554 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 1555 | return !RD->isCompleteDefinition() || |
| 1556 | !RD->hasTrivialDefaultConstructor() || |
| 1557 | !RD->hasTrivialDestructor(); |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1558 | return false; |
| 1559 | } |
| 1560 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1561 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 1562 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1563 | /// bitfield width if there is one, 'InitExpr' specifies the initializer if |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1564 | /// one has been parsed, and 'InitStyle' is set if an in-class initializer is |
| 1565 | /// present (but parsing it has been deferred). |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1566 | Decl * |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1567 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1568 | MultiTemplateParamsArg TemplateParameterLists, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 1569 | Expr *BW, const VirtSpecifiers &VS, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1570 | InClassInitStyle InitStyle) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1571 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1572 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 1573 | DeclarationName Name = NameInfo.getName(); |
| 1574 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1575 | |
| 1576 | // For anonymous bitfields, the location should point to the type. |
| 1577 | if (Loc.isInvalid()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1578 | Loc = D.getLocStart(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1579 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1580 | Expr *BitWidth = static_cast<Expr*>(BW); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1581 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1582 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1583 | assert(!DS.isFriendSpecified()); |
| 1584 | |
Richard Smith | 1ab0d90 | 2011-06-25 02:28:38 +0000 | [diff] [blame] | 1585 | bool isFunc = D.isDeclarationOfFunction(); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1586 | |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1587 | if (cast<CXXRecordDecl>(CurContext)->isInterface()) { |
| 1588 | // The Microsoft extension __interface only permits public member functions |
| 1589 | // and prohibits constructors, destructors, operators, non-public member |
| 1590 | // functions, static methods and data members. |
| 1591 | unsigned InvalidDecl; |
| 1592 | bool ShowDeclName = true; |
| 1593 | if (!isFunc) |
| 1594 | InvalidDecl = (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) ? 0 : 1; |
| 1595 | else if (AS != AS_public) |
| 1596 | InvalidDecl = 2; |
| 1597 | else if (DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 1598 | InvalidDecl = 3; |
| 1599 | else switch (Name.getNameKind()) { |
| 1600 | case DeclarationName::CXXConstructorName: |
| 1601 | InvalidDecl = 4; |
| 1602 | ShowDeclName = false; |
| 1603 | break; |
| 1604 | |
| 1605 | case DeclarationName::CXXDestructorName: |
| 1606 | InvalidDecl = 5; |
| 1607 | ShowDeclName = false; |
| 1608 | break; |
| 1609 | |
| 1610 | case DeclarationName::CXXOperatorName: |
| 1611 | case DeclarationName::CXXConversionFunctionName: |
| 1612 | InvalidDecl = 6; |
| 1613 | break; |
| 1614 | |
| 1615 | default: |
| 1616 | InvalidDecl = 0; |
| 1617 | break; |
| 1618 | } |
| 1619 | |
| 1620 | if (InvalidDecl) { |
| 1621 | if (ShowDeclName) |
| 1622 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 1623 | << (InvalidDecl-1) << Name; |
| 1624 | else |
| 1625 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 1626 | << (InvalidDecl-1) << ""; |
| 1627 | return 0; |
| 1628 | } |
| 1629 | } |
| 1630 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1631 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 1632 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1633 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 1634 | // data members and cannot be applied to names declared const or static, |
| 1635 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1636 | switch (DS.getStorageClassSpec()) { |
| 1637 | case DeclSpec::SCS_unspecified: |
| 1638 | case DeclSpec::SCS_typedef: |
| 1639 | case DeclSpec::SCS_static: |
| 1640 | // FALL THROUGH. |
| 1641 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1642 | case DeclSpec::SCS_mutable: |
| 1643 | if (isFunc) { |
| 1644 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1645 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1646 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1647 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 1649 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 1650 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1651 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1652 | } |
| 1653 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1654 | default: |
| 1655 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1656 | Diag(DS.getStorageClassSpecLoc(), |
| 1657 | diag::err_storageclass_invalid_for_member); |
| 1658 | else |
| 1659 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 1660 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 1661 | } |
| 1662 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1663 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 1664 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 1665 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1666 | |
| 1667 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1668 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1669 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 1670 | |
| 1671 | // Data members must have identifiers for names. |
Benjamin Kramer | c1aa40c | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1672 | if (!Name.isIdentifier()) { |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 1673 | Diag(Loc, diag::err_bad_variable_name) |
| 1674 | << Name; |
| 1675 | return 0; |
| 1676 | } |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1677 | |
Benjamin Kramer | c1aa40c | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1678 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 1679 | |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1680 | // Member field could not be with "template" keyword. |
| 1681 | // So TemplateParameterLists should be empty in this case. |
| 1682 | if (TemplateParameterLists.size()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1683 | TemplateParameterList* TemplateParams = TemplateParameterLists[0]; |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1684 | if (TemplateParams->size()) { |
| 1685 | // There is no such thing as a member field template. |
| 1686 | Diag(D.getIdentifierLoc(), diag::err_template_member) |
| 1687 | << II |
| 1688 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 1689 | TemplateParams->getRAngleLoc()); |
| 1690 | } else { |
| 1691 | // There is an extraneous 'template<>' for this member. |
| 1692 | Diag(TemplateParams->getTemplateLoc(), |
| 1693 | diag::err_template_member_noparams) |
| 1694 | << II |
| 1695 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 1696 | TemplateParams->getRAngleLoc()); |
| 1697 | } |
| 1698 | return 0; |
| 1699 | } |
| 1700 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1701 | if (SS.isSet() && !SS.isInvalid()) { |
| 1702 | // The user provided a superfluous scope specifier inside a class |
| 1703 | // definition: |
| 1704 | // |
| 1705 | // class X { |
| 1706 | // int X::member; |
| 1707 | // }; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 1708 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 1709 | diagnoseQualifiedDeclaration(SS, DC, Name, D.getIdentifierLoc()); |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1710 | else |
| 1711 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 1712 | << Name << SS.getRange(); |
Douglas Gregor | 5d8419c | 2011-11-01 22:13:30 +0000 | [diff] [blame] | 1713 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1714 | SS.clear(); |
| 1715 | } |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1716 | |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1717 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1718 | InitStyle, AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1719 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1720 | } else { |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1721 | assert(InitStyle == ICIS_NoInit); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1722 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1723 | Member = HandleDeclarator(S, D, TemplateParameterLists); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1724 | if (!Member) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1725 | return 0; |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1726 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1727 | |
| 1728 | // Non-instance-fields can't have a bitfield. |
| 1729 | if (BitWidth) { |
| 1730 | if (Member->isInvalidDecl()) { |
| 1731 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 1732 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1733 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 1734 | // "static member 'A' cannot be a bit-field" |
| 1735 | Diag(Loc, diag::err_static_not_bitfield) |
| 1736 | << Name << BitWidth->getSourceRange(); |
| 1737 | } else if (isa<TypedefDecl>(Member)) { |
| 1738 | // "typedef member 'x' cannot be a bit-field" |
| 1739 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 1740 | << Name << BitWidth->getSourceRange(); |
| 1741 | } else { |
| 1742 | // A function typedef ("typedef int f(); f a;"). |
| 1743 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 1744 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1745 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1746 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1747 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1748 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1749 | BitWidth = 0; |
| 1750 | Member->setInvalidDecl(); |
| 1751 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1752 | |
| 1753 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1755 | // If we have declared a member function template, set the access of the |
| 1756 | // templated declaration as well. |
| 1757 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 1758 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1759 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1760 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1761 | if (VS.isOverrideSpecified()) |
| 1762 | Member->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context)); |
| 1763 | if (VS.isFinalSpecified()) |
| 1764 | Member->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context)); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1765 | |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1766 | if (VS.getLastLocation().isValid()) { |
| 1767 | // Update the end location of a method that has a virt-specifiers. |
| 1768 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 1769 | MD->setRangeEnd(VS.getLastLocation()); |
| 1770 | } |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1771 | |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1772 | CheckOverrideControl(Member); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1773 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1774 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1775 | |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1776 | if (isInstField) { |
| 1777 | FieldDecl *FD = cast<FieldDecl>(Member); |
| 1778 | FieldCollector->Add(FD); |
| 1779 | |
| 1780 | if (Diags.getDiagnosticLevel(diag::warn_unused_private_field, |
| 1781 | FD->getLocation()) |
| 1782 | != DiagnosticsEngine::Ignored) { |
| 1783 | // Remember all explicit private FieldDecls that have a name, no side |
| 1784 | // effects and are not part of a dependent type declaration. |
| 1785 | if (!FD->isImplicit() && FD->getDeclName() && |
| 1786 | FD->getAccess() == AS_private && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1787 | !FD->hasAttr<UnusedAttr>() && |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 1788 | !FD->getParent()->isDependentContext() && |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1789 | !InitializationHasSideEffects(*FD)) |
| 1790 | UnusedPrivateFields.insert(FD); |
| 1791 | } |
| 1792 | } |
| 1793 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1794 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1797 | namespace { |
| 1798 | class UninitializedFieldVisitor |
| 1799 | : public EvaluatedExprVisitor<UninitializedFieldVisitor> { |
| 1800 | Sema &S; |
| 1801 | ValueDecl *VD; |
| 1802 | public: |
| 1803 | typedef EvaluatedExprVisitor<UninitializedFieldVisitor> Inherited; |
| 1804 | UninitializedFieldVisitor(Sema &S, ValueDecl *VD) : Inherited(S.Context), |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 1805 | S(S) { |
| 1806 | if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(VD)) |
| 1807 | this->VD = IFD->getAnonField(); |
| 1808 | else |
| 1809 | this->VD = VD; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | void HandleExpr(Expr *E) { |
| 1813 | if (!E) return; |
| 1814 | |
| 1815 | // Expressions like x(x) sometimes lack the surrounding expressions |
| 1816 | // but need to be checked anyways. |
| 1817 | HandleValue(E); |
| 1818 | Visit(E); |
| 1819 | } |
| 1820 | |
| 1821 | void HandleValue(Expr *E) { |
| 1822 | E = E->IgnoreParens(); |
| 1823 | |
| 1824 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 1825 | if (isa<EnumConstantDecl>(ME->getMemberDecl())) |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 1826 | return; |
| 1827 | |
| 1828 | // FieldME is the inner-most MemberExpr that is not an anonymous struct |
| 1829 | // or union. |
| 1830 | MemberExpr *FieldME = ME; |
| 1831 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1832 | Expr *Base = E; |
| 1833 | while (isa<MemberExpr>(Base)) { |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 1834 | ME = cast<MemberExpr>(Base); |
| 1835 | |
| 1836 | if (isa<VarDecl>(ME->getMemberDecl())) |
| 1837 | return; |
| 1838 | |
| 1839 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 1840 | if (!FD->isAnonymousStructOrUnion()) |
| 1841 | FieldME = ME; |
| 1842 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1843 | Base = ME->getBase(); |
| 1844 | } |
| 1845 | |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 1846 | if (VD == FieldME->getMemberDecl() && isa<CXXThisExpr>(Base)) { |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1847 | unsigned diag = VD->getType()->isReferenceType() |
| 1848 | ? diag::warn_reference_field_is_uninit |
| 1849 | : diag::warn_field_is_uninit; |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 1850 | S.Diag(FieldME->getExprLoc(), diag) << VD; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1851 | } |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 1852 | return; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 1856 | HandleValue(CO->getTrueExpr()); |
| 1857 | HandleValue(CO->getFalseExpr()); |
| 1858 | return; |
| 1859 | } |
| 1860 | |
| 1861 | if (BinaryConditionalOperator *BCO = |
| 1862 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 1863 | HandleValue(BCO->getCommon()); |
| 1864 | HandleValue(BCO->getFalseExpr()); |
| 1865 | return; |
| 1866 | } |
| 1867 | |
| 1868 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 1869 | switch (BO->getOpcode()) { |
| 1870 | default: |
| 1871 | return; |
| 1872 | case(BO_PtrMemD): |
| 1873 | case(BO_PtrMemI): |
| 1874 | HandleValue(BO->getLHS()); |
| 1875 | return; |
| 1876 | case(BO_Comma): |
| 1877 | HandleValue(BO->getRHS()); |
| 1878 | return; |
| 1879 | } |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 1884 | if (E->getCastKind() == CK_LValueToRValue) |
| 1885 | HandleValue(E->getSubExpr()); |
| 1886 | |
| 1887 | Inherited::VisitImplicitCastExpr(E); |
| 1888 | } |
| 1889 | |
| 1890 | void VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 1891 | Expr *Callee = E->getCallee(); |
| 1892 | if (isa<MemberExpr>(Callee)) |
| 1893 | HandleValue(Callee); |
| 1894 | |
| 1895 | Inherited::VisitCXXMemberCallExpr(E); |
| 1896 | } |
| 1897 | }; |
| 1898 | static void CheckInitExprContainsUninitializedFields(Sema &S, Expr *E, |
| 1899 | ValueDecl *VD) { |
| 1900 | UninitializedFieldVisitor(S, VD).HandleExpr(E); |
| 1901 | } |
| 1902 | } // namespace |
| 1903 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1904 | /// ActOnCXXInClassMemberInitializer - This is invoked after parsing an |
Richard Smith | 0ff6f8f | 2011-07-20 00:12:52 +0000 | [diff] [blame] | 1905 | /// in-class initializer for a non-static C++ class member, and after |
| 1906 | /// instantiating an in-class initializer in a class template. Such actions |
| 1907 | /// are deferred until the class is complete. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1908 | void |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1909 | Sema::ActOnCXXInClassMemberInitializer(Decl *D, SourceLocation InitLoc, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1910 | Expr *InitExpr) { |
| 1911 | FieldDecl *FD = cast<FieldDecl>(D); |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1912 | assert(FD->getInClassInitStyle() != ICIS_NoInit && |
| 1913 | "must set init style when field is created"); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1914 | |
| 1915 | if (!InitExpr) { |
| 1916 | FD->setInvalidDecl(); |
| 1917 | FD->removeInClassInitializer(); |
| 1918 | return; |
| 1919 | } |
| 1920 | |
Peter Collingbourne | fef2189 | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 1921 | if (DiagnoseUnexpandedParameterPack(InitExpr, UPPC_Initializer)) { |
| 1922 | FD->setInvalidDecl(); |
| 1923 | FD->removeInClassInitializer(); |
| 1924 | return; |
| 1925 | } |
| 1926 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 1927 | if (getDiagnostics().getDiagnosticLevel(diag::warn_field_is_uninit, InitLoc) |
| 1928 | != DiagnosticsEngine::Ignored) { |
| 1929 | CheckInitExprContainsUninitializedFields(*this, InitExpr, FD); |
| 1930 | } |
| 1931 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1932 | ExprResult Init = InitExpr; |
Douglas Gregor | dd08427 | 2012-09-14 04:20:37 +0000 | [diff] [blame] | 1933 | if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent() && |
| 1934 | !FD->getDeclContext()->isDependentContext()) { |
| 1935 | // Note: We don't type-check when we're in a dependent context, because |
| 1936 | // the initialization-substitution code does not properly handle direct |
| 1937 | // list initialization. We have the same hackaround for ctor-initializers. |
Sebastian Redl | 772291a | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 1938 | if (isa<InitListExpr>(InitExpr) && isStdInitializerList(FD->getType(), 0)) { |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 1939 | Diag(FD->getLocation(), diag::warn_dangling_std_initializer_list) |
Sebastian Redl | 772291a | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 1940 | << /*at end of ctor*/1 << InitExpr->getSourceRange(); |
| 1941 | } |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 1942 | Expr **Inits = &InitExpr; |
| 1943 | unsigned NumInits = 1; |
| 1944 | InitializedEntity Entity = InitializedEntity::InitializeMember(FD); |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1945 | InitializationKind Kind = FD->getInClassInitStyle() == ICIS_ListInit |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 1946 | ? InitializationKind::CreateDirectList(InitExpr->getLocStart()) |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1947 | : InitializationKind::CreateCopy(InitExpr->getLocStart(), InitLoc); |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 1948 | InitializationSequence Seq(*this, Entity, Kind, Inits, NumInits); |
| 1949 | Init = Seq.Perform(*this, Entity, Kind, MultiExprArg(Inits, NumInits)); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1950 | if (Init.isInvalid()) { |
| 1951 | FD->setInvalidDecl(); |
| 1952 | return; |
| 1953 | } |
| 1954 | |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1955 | CheckImplicitConversions(Init.get(), InitLoc); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | // C++0x [class.base.init]p7: |
| 1959 | // The initialization of each base and member constitutes a |
| 1960 | // full-expression. |
| 1961 | Init = MaybeCreateExprWithCleanups(Init); |
| 1962 | if (Init.isInvalid()) { |
| 1963 | FD->setInvalidDecl(); |
| 1964 | return; |
| 1965 | } |
| 1966 | |
| 1967 | InitExpr = Init.release(); |
| 1968 | |
| 1969 | FD->setInClassInitializer(InitExpr); |
| 1970 | } |
| 1971 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1972 | /// \brief Find the direct and/or virtual base specifiers that |
| 1973 | /// correspond to the given base type, for use in base initialization |
| 1974 | /// within a constructor. |
| 1975 | static bool FindBaseInitializer(Sema &SemaRef, |
| 1976 | CXXRecordDecl *ClassDecl, |
| 1977 | QualType BaseType, |
| 1978 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 1979 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 1980 | // First, check for a direct base class. |
| 1981 | DirectBaseSpec = 0; |
| 1982 | for (CXXRecordDecl::base_class_const_iterator Base |
| 1983 | = ClassDecl->bases_begin(); |
| 1984 | Base != ClassDecl->bases_end(); ++Base) { |
| 1985 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 1986 | // We found a direct base of this type. That's what we're |
| 1987 | // initializing. |
| 1988 | DirectBaseSpec = &*Base; |
| 1989 | break; |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | // Check for a virtual base class. |
| 1994 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1995 | // there are no virtual bases. |
| 1996 | VirtualBaseSpec = 0; |
| 1997 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1998 | // We haven't found a base yet; search the class hierarchy for a |
| 1999 | // virtual base class. |
| 2000 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 2001 | /*DetectVirtual=*/false); |
| 2002 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 2003 | BaseType, Paths)) { |
| 2004 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 2005 | Path != Paths.end(); ++Path) { |
| 2006 | if (Path->back().Base->isVirtual()) { |
| 2007 | VirtualBaseSpec = Path->back().Base; |
| 2008 | break; |
| 2009 | } |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | return DirectBaseSpec || VirtualBaseSpec; |
| 2015 | } |
| 2016 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2017 | /// \brief Handle a C++ member initializer using braced-init-list syntax. |
| 2018 | MemInitResult |
| 2019 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
| 2020 | Scope *S, |
| 2021 | CXXScopeSpec &SS, |
| 2022 | IdentifierInfo *MemberOrBase, |
| 2023 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2024 | const DeclSpec &DS, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2025 | SourceLocation IdLoc, |
| 2026 | Expr *InitList, |
| 2027 | SourceLocation EllipsisLoc) { |
| 2028 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2029 | DS, IdLoc, InitList, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2030 | EllipsisLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | /// \brief Handle a C++ member initializer using parentheses syntax. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2034 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2035 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2036 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2037 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2038 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2039 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2040 | const DeclSpec &DS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2041 | SourceLocation IdLoc, |
| 2042 | SourceLocation LParenLoc, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 2043 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2044 | SourceLocation RParenLoc, |
| 2045 | SourceLocation EllipsisLoc) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2046 | Expr *List = new (Context) ParenListExpr(Context, LParenLoc, |
| 2047 | llvm::makeArrayRef(Args, NumArgs), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2048 | RParenLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2049 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2050 | DS, IdLoc, List, EllipsisLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2053 | namespace { |
| 2054 | |
Kaelyn Uhrain | dc98cd0 | 2012-01-11 21:17:51 +0000 | [diff] [blame] | 2055 | // Callback to only accept typo corrections that can be a valid C++ member |
| 2056 | // intializer: either a non-static field member or a base class. |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2057 | class MemInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 2058 | public: |
| 2059 | explicit MemInitializerValidatorCCC(CXXRecordDecl *ClassDecl) |
| 2060 | : ClassDecl(ClassDecl) {} |
| 2061 | |
| 2062 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 2063 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 2064 | if (FieldDecl *Member = dyn_cast<FieldDecl>(ND)) |
| 2065 | return Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl); |
| 2066 | else |
| 2067 | return isa<TypeDecl>(ND); |
| 2068 | } |
| 2069 | return false; |
| 2070 | } |
| 2071 | |
| 2072 | private: |
| 2073 | CXXRecordDecl *ClassDecl; |
| 2074 | }; |
| 2075 | |
| 2076 | } |
| 2077 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2078 | /// \brief Handle a C++ member initializer. |
| 2079 | MemInitResult |
| 2080 | Sema::BuildMemInitializer(Decl *ConstructorD, |
| 2081 | Scope *S, |
| 2082 | CXXScopeSpec &SS, |
| 2083 | IdentifierInfo *MemberOrBase, |
| 2084 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2085 | const DeclSpec &DS, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2086 | SourceLocation IdLoc, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2087 | Expr *Init, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2088 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2089 | if (!ConstructorD) |
| 2090 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2092 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2093 | |
| 2094 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2095 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2096 | if (!Constructor) { |
| 2097 | // The user wrote a constructor initializer on a function that is |
| 2098 | // not a C++ constructor. Ignore the error for now, because we may |
| 2099 | // have more member initializers coming; we'll diagnose it just |
| 2100 | // once in ActOnMemInitializers. |
| 2101 | return true; |
| 2102 | } |
| 2103 | |
| 2104 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2105 | |
| 2106 | // C++ [class.base.init]p2: |
| 2107 | // 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] | 2108 | // constructor's class and, if not found in that scope, are looked |
| 2109 | // up in the scope containing the constructor's definition. |
| 2110 | // [Note: if the constructor's class contains a member with the |
| 2111 | // same name as a direct or virtual base class of the class, a |
| 2112 | // mem-initializer-id naming the member or base class and composed |
| 2113 | // of a single identifier refers to the class member. A |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2114 | // mem-initializer-id for the hidden base class may be specified |
| 2115 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2116 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2117 | // Look for a member, first. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2118 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2119 | = ClassDecl->lookup(MemberOrBase); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2120 | if (Result.first != Result.second) { |
Peter Collingbourne | dc69be2 | 2011-10-23 18:59:37 +0000 | [diff] [blame] | 2121 | ValueDecl *Member; |
| 2122 | if ((Member = dyn_cast<FieldDecl>(*Result.first)) || |
| 2123 | (Member = dyn_cast<IndirectFieldDecl>(*Result.first))) { |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2124 | if (EllipsisLoc.isValid()) |
| 2125 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2126 | << MemberOrBase |
| 2127 | << SourceRange(IdLoc, Init->getSourceRange().getEnd()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2128 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2129 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2130 | } |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2131 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2132 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2133 | // 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] | 2134 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2135 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2136 | |
| 2137 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2138 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2139 | } else if (DS.getTypeSpecType() == TST_decltype) { |
| 2140 | BaseType = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2141 | } else { |
| 2142 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 2143 | LookupParsedName(R, S, &SS); |
| 2144 | |
| 2145 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 2146 | if (!TyD) { |
| 2147 | if (R.isAmbiguous()) return true; |
| 2148 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 2149 | // We don't want access-control diagnostics here. |
| 2150 | R.suppressDiagnostics(); |
| 2151 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2152 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 2153 | bool NotUnknownSpecialization = false; |
| 2154 | DeclContext *DC = computeDeclContext(SS, false); |
| 2155 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 2156 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 2157 | |
| 2158 | if (!NotUnknownSpecialization) { |
| 2159 | // When the scope specifier can refer to a member of an unknown |
| 2160 | // specialization, we take it as a type name. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 2161 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 2162 | SS.getWithLocInContext(Context), |
| 2163 | *MemberOrBase, IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 2164 | if (BaseType.isNull()) |
| 2165 | return true; |
| 2166 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2167 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 2168 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2169 | } |
| 2170 | } |
| 2171 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2172 | // If no results were found, try to correct typos. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2173 | TypoCorrection Corr; |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2174 | MemInitializerValidatorCCC Validator(ClassDecl); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2175 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2176 | (Corr = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 2177 | Validator, ClassDecl))) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2178 | std::string CorrectedStr(Corr.getAsString(getLangOpts())); |
| 2179 | std::string CorrectedQuotedStr(Corr.getQuoted(getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2180 | if (FieldDecl *Member = Corr.getCorrectionDeclAs<FieldDecl>()) { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2181 | // We have found a non-static data member with a similar |
| 2182 | // name to what was typed; complain and initialize that |
| 2183 | // member. |
| 2184 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 2185 | << MemberOrBase << true << CorrectedQuotedStr |
| 2186 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
| 2187 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 2188 | << CorrectedQuotedStr; |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2189 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2190 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2191 | } else if (TypeDecl *Type = Corr.getCorrectionDeclAs<TypeDecl>()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2192 | const CXXBaseSpecifier *DirectBaseSpec; |
| 2193 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 2194 | if (FindBaseInitializer(*this, ClassDecl, |
| 2195 | Context.getTypeDeclType(Type), |
| 2196 | DirectBaseSpec, VirtualBaseSpec)) { |
| 2197 | // We have found a direct or virtual base class with a |
| 2198 | // similar name to what was typed; complain and initialize |
| 2199 | // that base class. |
| 2200 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2201 | << MemberOrBase << false << CorrectedQuotedStr |
| 2202 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2203 | |
| 2204 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 2205 | : VirtualBaseSpec; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2206 | Diag(BaseSpec->getLocStart(), |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2207 | diag::note_base_class_specified_here) |
| 2208 | << BaseSpec->getType() |
| 2209 | << BaseSpec->getSourceRange(); |
| 2210 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2211 | TyD = Type; |
| 2212 | } |
| 2213 | } |
| 2214 | } |
| 2215 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2216 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2217 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2218 | << MemberOrBase << SourceRange(IdLoc,Init->getSourceRange().getEnd()); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2219 | return true; |
| 2220 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2223 | if (BaseType.isNull()) { |
| 2224 | BaseType = Context.getTypeDeclType(TyD); |
| 2225 | if (SS.isSet()) { |
| 2226 | NestedNameSpecifier *Qualifier = |
| 2227 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2228 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2229 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2230 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2231 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2232 | } |
| 2233 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2234 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2235 | if (!TInfo) |
| 2236 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2237 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2238 | return BuildBaseInitializer(BaseType, TInfo, Init, ClassDecl, EllipsisLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2241 | /// Checks a member initializer expression for cases where reference (or |
| 2242 | /// pointer) members are bound to by-value parameters (or their addresses). |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2243 | static void CheckForDanglingReferenceOrPointer(Sema &S, ValueDecl *Member, |
| 2244 | Expr *Init, |
| 2245 | SourceLocation IdLoc) { |
| 2246 | QualType MemberTy = Member->getType(); |
| 2247 | |
| 2248 | // We only handle pointers and references currently. |
| 2249 | // FIXME: Would this be relevant for ObjC object pointers? Or block pointers? |
| 2250 | if (!MemberTy->isReferenceType() && !MemberTy->isPointerType()) |
| 2251 | return; |
| 2252 | |
| 2253 | const bool IsPointer = MemberTy->isPointerType(); |
| 2254 | if (IsPointer) { |
| 2255 | if (const UnaryOperator *Op |
| 2256 | = dyn_cast<UnaryOperator>(Init->IgnoreParenImpCasts())) { |
| 2257 | // The only case we're worried about with pointers requires taking the |
| 2258 | // address. |
| 2259 | if (Op->getOpcode() != UO_AddrOf) |
| 2260 | return; |
| 2261 | |
| 2262 | Init = Op->getSubExpr(); |
| 2263 | } else { |
| 2264 | // We only handle address-of expression initializers for pointers. |
| 2265 | return; |
| 2266 | } |
| 2267 | } |
| 2268 | |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2269 | if (isa<MaterializeTemporaryExpr>(Init->IgnoreParens())) { |
| 2270 | // Taking the address of a temporary will be diagnosed as a hard error. |
| 2271 | if (IsPointer) |
| 2272 | return; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2273 | |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2274 | S.Diag(Init->getExprLoc(), diag::warn_bind_ref_member_to_temporary) |
| 2275 | << Member << Init->getSourceRange(); |
| 2276 | } else if (const DeclRefExpr *DRE |
| 2277 | = dyn_cast<DeclRefExpr>(Init->IgnoreParens())) { |
| 2278 | // We only warn when referring to a non-reference parameter declaration. |
| 2279 | const ParmVarDecl *Parameter = dyn_cast<ParmVarDecl>(DRE->getDecl()); |
| 2280 | if (!Parameter || Parameter->getType()->isReferenceType()) |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2281 | return; |
| 2282 | |
| 2283 | S.Diag(Init->getExprLoc(), |
| 2284 | IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 2285 | : diag::warn_bind_ref_member_to_parameter) |
| 2286 | << Member << Parameter << Init->getSourceRange(); |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2287 | } else { |
| 2288 | // Other initializers are fine. |
| 2289 | return; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2290 | } |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2291 | |
| 2292 | S.Diag(Member->getLocation(), diag::note_ref_or_ptr_member_declared_here) |
| 2293 | << (unsigned)IsPointer; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2296 | MemInitResult |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2297 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr *Init, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2298 | SourceLocation IdLoc) { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2299 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 2300 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 2301 | assert((DirectMember || IndirectMember) && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2302 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 2303 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2304 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Peter Collingbourne | fef2189 | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 2305 | return true; |
| 2306 | |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 2307 | if (Member->isInvalidDecl()) |
| 2308 | return true; |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2309 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2310 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 2311 | // foo(foo) |
| 2312 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 2313 | // TODO: implement -Wuninitialized and fold this into that framework. |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2314 | Expr **Args; |
| 2315 | unsigned NumArgs; |
| 2316 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 2317 | Args = ParenList->getExprs(); |
| 2318 | NumArgs = ParenList->getNumExprs(); |
| 2319 | } else { |
| 2320 | InitListExpr *InitList = cast<InitListExpr>(Init); |
| 2321 | Args = InitList->getInits(); |
| 2322 | NumArgs = InitList->getNumInits(); |
| 2323 | } |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2324 | |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 2325 | if (getDiagnostics().getDiagnosticLevel(diag::warn_field_is_uninit, IdLoc) |
| 2326 | != DiagnosticsEngine::Ignored) |
| 2327 | for (unsigned i = 0; i < NumArgs; ++i) |
| 2328 | // FIXME: Warn about the case when other fields are used before being |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2329 | // initialized. For example, let this field be the i'th field. When |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2330 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 2331 | // fields are used, as they are not yet initialized. |
| 2332 | // Right now we are only handling the case where the i'th field uses |
| 2333 | // itself in its initializer. |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2334 | // Also need to take into account that some fields may be initialized by |
| 2335 | // in-class initializers, see C++11 [class.base.init]p9. |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 2336 | CheckInitExprContainsUninitializedFields(*this, Args[i], Member); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2337 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2338 | SourceRange InitRange = Init->getSourceRange(); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2339 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2340 | if (Member->getType()->isDependentType() || Init->isTypeDependent()) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2341 | // Can't check initialization for a member of dependent type or when |
| 2342 | // any of the arguments are type-dependent expressions. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2343 | DiscardCleanupsInEvaluationContext(); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2344 | } else { |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2345 | bool InitList = false; |
| 2346 | if (isa<InitListExpr>(Init)) { |
| 2347 | InitList = true; |
| 2348 | Args = &Init; |
| 2349 | NumArgs = 1; |
Sebastian Redl | 772291a | 2012-02-19 16:31:05 +0000 | [diff] [blame] | 2350 | |
| 2351 | if (isStdInitializerList(Member->getType(), 0)) { |
| 2352 | Diag(IdLoc, diag::warn_dangling_std_initializer_list) |
| 2353 | << /*at end of ctor*/1 << InitRange; |
| 2354 | } |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2357 | // Initialize the member. |
| 2358 | InitializedEntity MemberEntity = |
| 2359 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 2360 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 2361 | InitializationKind Kind = |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2362 | InitList ? InitializationKind::CreateDirectList(IdLoc) |
| 2363 | : InitializationKind::CreateDirect(IdLoc, InitRange.getBegin(), |
| 2364 | InitRange.getEnd()); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2365 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2366 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 2367 | ExprResult MemberInit = InitSeq.Perform(*this, MemberEntity, Kind, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2368 | MultiExprArg(Args, NumArgs), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2369 | 0); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2370 | if (MemberInit.isInvalid()) |
| 2371 | return true; |
| 2372 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2373 | CheckImplicitConversions(MemberInit.get(), |
| 2374 | InitRange.getBegin()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2375 | |
| 2376 | // C++0x [class.base.init]p7: |
| 2377 | // The initialization of each base and member constitutes a |
| 2378 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2379 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2380 | if (MemberInit.isInvalid()) |
| 2381 | return true; |
| 2382 | |
| 2383 | // If we are in a dependent context, template instantiation will |
| 2384 | // perform this type-checking again. Just save the arguments that we |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2385 | // received. |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2386 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2387 | // of the information that we have about the member |
| 2388 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2389 | // and this approach is far more likely to get the corner cases right. |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2390 | if (CurContext->isDependentContext()) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2391 | // The existing Init will do fine. |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2392 | } else { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2393 | Init = MemberInit.get(); |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2394 | CheckForDanglingReferenceOrPointer(*this, Member, Init, IdLoc); |
| 2395 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2398 | if (DirectMember) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2399 | return new (Context) CXXCtorInitializer(Context, DirectMember, IdLoc, |
| 2400 | InitRange.getBegin(), Init, |
| 2401 | InitRange.getEnd()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2402 | } else { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2403 | return new (Context) CXXCtorInitializer(Context, IndirectMember, IdLoc, |
| 2404 | InitRange.getBegin(), Init, |
| 2405 | InitRange.getEnd()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2406 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2409 | MemInitResult |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2410 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, Expr *Init, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2411 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2412 | SourceLocation NameLoc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2413 | if (!LangOpts.CPlusPlus0x) |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2414 | return Diag(NameLoc, diag::err_delegating_ctor) |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2415 | << TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2416 | Diag(NameLoc, diag::warn_cxx98_compat_delegating_ctor); |
Sebastian Redl | f9c32eb | 2011-03-12 13:53:51 +0000 | [diff] [blame] | 2417 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2418 | bool InitList = true; |
| 2419 | Expr **Args = &Init; |
| 2420 | unsigned NumArgs = 1; |
| 2421 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 2422 | InitList = false; |
| 2423 | Args = ParenList->getExprs(); |
| 2424 | NumArgs = ParenList->getNumExprs(); |
| 2425 | } |
| 2426 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2427 | SourceRange InitRange = Init->getSourceRange(); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2428 | // Initialize the object. |
| 2429 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 2430 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 2431 | InitializationKind Kind = |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2432 | InitList ? InitializationKind::CreateDirectList(NameLoc) |
| 2433 | : InitializationKind::CreateDirect(NameLoc, InitRange.getBegin(), |
| 2434 | InitRange.getEnd()); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2435 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args, NumArgs); |
| 2436 | ExprResult DelegationInit = InitSeq.Perform(*this, DelegationEntity, Kind, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2437 | MultiExprArg(Args, NumArgs), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2438 | 0); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2439 | if (DelegationInit.isInvalid()) |
| 2440 | return true; |
| 2441 | |
Matt Beaumont-Gay | 2eb0ce3 | 2011-11-01 18:10:22 +0000 | [diff] [blame] | 2442 | assert(cast<CXXConstructExpr>(DelegationInit.get())->getConstructor() && |
| 2443 | "Delegating constructor with no target?"); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2444 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2445 | CheckImplicitConversions(DelegationInit.get(), InitRange.getBegin()); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2446 | |
| 2447 | // C++0x [class.base.init]p7: |
| 2448 | // The initialization of each base and member constitutes a |
| 2449 | // full-expression. |
| 2450 | DelegationInit = MaybeCreateExprWithCleanups(DelegationInit); |
| 2451 | if (DelegationInit.isInvalid()) |
| 2452 | return true; |
| 2453 | |
Eli Friedman | d21016f | 2012-05-19 23:35:23 +0000 | [diff] [blame] | 2454 | // If we are in a dependent context, template instantiation will |
| 2455 | // perform this type-checking again. Just save the arguments that we |
| 2456 | // received in a ParenListExpr. |
| 2457 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2458 | // of the information that we have about the base |
| 2459 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2460 | // and this approach is far more likely to get the corner cases right. |
| 2461 | if (CurContext->isDependentContext()) |
| 2462 | DelegationInit = Owned(Init); |
| 2463 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2464 | return new (Context) CXXCtorInitializer(Context, TInfo, InitRange.getBegin(), |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2465 | DelegationInit.takeAs<Expr>(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2466 | InitRange.getEnd()); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2470 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2471 | Expr *Init, CXXRecordDecl *ClassDecl, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2472 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2473 | SourceLocation BaseLoc |
| 2474 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2475 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2476 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 2477 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 2478 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2479 | |
| 2480 | // C++ [class.base.init]p2: |
| 2481 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2482 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2483 | // of that class, the mem-initializer is ill-formed. A |
| 2484 | // mem-initializer-list can initialize a base class using any |
| 2485 | // name that denotes that base class type. |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2486 | bool Dependent = BaseType->isDependentType() || Init->isTypeDependent(); |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2487 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2488 | SourceRange InitRange = Init->getSourceRange(); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2489 | if (EllipsisLoc.isValid()) { |
| 2490 | // This is a pack expansion. |
| 2491 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 2492 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2493 | << SourceRange(BaseLoc, InitRange.getEnd()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2494 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2495 | EllipsisLoc = SourceLocation(); |
| 2496 | } |
| 2497 | } else { |
| 2498 | // Check for any unexpanded parameter packs. |
| 2499 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 2500 | return true; |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2501 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2502 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2503 | return true; |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2504 | } |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2505 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2506 | // Check for direct and virtual base classes. |
| 2507 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 2508 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 2509 | if (!Dependent) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2510 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 2511 | BaseType)) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2512 | return BuildDelegatingInitializer(BaseTInfo, Init, ClassDecl); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2513 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2514 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 2515 | VirtualBaseSpec); |
| 2516 | |
| 2517 | // C++ [base.class.init]p2: |
| 2518 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 2519 | // constructor's class or a direct or virtual base of that class, the |
| 2520 | // mem-initializer is ill-formed. |
| 2521 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 2522 | // If the class has any dependent bases, then it's possible that |
| 2523 | // one of those types will resolve to the same type as |
| 2524 | // BaseType. Therefore, just treat this as a dependent base |
| 2525 | // class initialization. FIXME: Should we try to check the |
| 2526 | // initialization anyway? It seems odd. |
| 2527 | if (ClassDecl->hasAnyDependentBases()) |
| 2528 | Dependent = true; |
| 2529 | else |
| 2530 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 2531 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 2532 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | if (Dependent) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2537 | DiscardCleanupsInEvaluationContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2538 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2539 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
| 2540 | /*IsVirtual=*/false, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2541 | InitRange.getBegin(), Init, |
| 2542 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2543 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2544 | |
| 2545 | // C++ [base.class.init]p2: |
| 2546 | // If a mem-initializer-id is ambiguous because it designates both |
| 2547 | // a direct non-virtual base class and an inherited virtual base |
| 2548 | // class, the mem-initializer is ill-formed. |
| 2549 | if (DirectBaseSpec && VirtualBaseSpec) |
| 2550 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2551 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2552 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2553 | CXXBaseSpecifier *BaseSpec = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2554 | if (!BaseSpec) |
| 2555 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 2556 | |
| 2557 | // Initialize the base. |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2558 | bool InitList = true; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2559 | Expr **Args = &Init; |
| 2560 | unsigned NumArgs = 1; |
| 2561 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2562 | InitList = false; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2563 | Args = ParenList->getExprs(); |
| 2564 | NumArgs = ParenList->getNumExprs(); |
| 2565 | } |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2566 | |
| 2567 | InitializedEntity BaseEntity = |
| 2568 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
| 2569 | InitializationKind Kind = |
| 2570 | InitList ? InitializationKind::CreateDirectList(BaseLoc) |
| 2571 | : InitializationKind::CreateDirect(BaseLoc, InitRange.getBegin(), |
| 2572 | InitRange.getEnd()); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2573 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 2574 | ExprResult BaseInit = InitSeq.Perform(*this, BaseEntity, Kind, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2575 | MultiExprArg(Args, NumArgs), 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2576 | if (BaseInit.isInvalid()) |
| 2577 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2578 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2579 | CheckImplicitConversions(BaseInit.get(), InitRange.getBegin()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2580 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2581 | // C++0x [class.base.init]p7: |
| 2582 | // The initialization of each base and member constitutes a |
| 2583 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2584 | BaseInit = MaybeCreateExprWithCleanups(BaseInit); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2585 | if (BaseInit.isInvalid()) |
| 2586 | return true; |
| 2587 | |
| 2588 | // If we are in a dependent context, template instantiation will |
| 2589 | // perform this type-checking again. Just save the arguments that we |
| 2590 | // received in a ParenListExpr. |
| 2591 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2592 | // of the information that we have about the base |
| 2593 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2594 | // 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] | 2595 | if (CurContext->isDependentContext()) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2596 | BaseInit = Owned(Init); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2597 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2598 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2599 | BaseSpec->isVirtual(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2600 | InitRange.getBegin(), |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2601 | BaseInit.takeAs<Expr>(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2602 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2605 | // Create a static_cast\<T&&>(expr). |
| 2606 | static Expr *CastForMoving(Sema &SemaRef, Expr *E) { |
| 2607 | QualType ExprType = E->getType(); |
| 2608 | QualType TargetType = SemaRef.Context.getRValueReferenceType(ExprType); |
| 2609 | SourceLocation ExprLoc = E->getLocStart(); |
| 2610 | TypeSourceInfo *TargetLoc = SemaRef.Context.getTrivialTypeSourceInfo( |
| 2611 | TargetType, ExprLoc); |
| 2612 | |
| 2613 | return SemaRef.BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
| 2614 | SourceRange(ExprLoc, ExprLoc), |
| 2615 | E->getSourceRange()).take(); |
| 2616 | } |
| 2617 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2618 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 2619 | /// initialize its base or member. |
| 2620 | enum ImplicitInitializerKind { |
| 2621 | IIK_Default, |
| 2622 | IIK_Copy, |
| 2623 | IIK_Move |
| 2624 | }; |
| 2625 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2626 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2627 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2628 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2629 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2630 | bool IsInheritedVirtualBase, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2631 | CXXCtorInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2632 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2633 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 2634 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2635 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2636 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2637 | |
| 2638 | switch (ImplicitInitKind) { |
| 2639 | case IIK_Default: { |
| 2640 | InitializationKind InitKind |
| 2641 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 2642 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2643 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2644 | break; |
| 2645 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2646 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2647 | case IIK_Move: |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2648 | case IIK_Copy: { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2649 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2650 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 2651 | QualType ParamType = Param->getType().getNonReferenceType(); |
Eli Friedman | cf7c14c | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 2652 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2653 | Expr *CopyCtorArg = |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2654 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2655 | SourceLocation(), Param, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2656 | Constructor->getLocation(), ParamType, |
| 2657 | VK_LValue, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2658 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2659 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(CopyCtorArg)); |
| 2660 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 2661 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 2662 | QualType ArgTy = |
| 2663 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 2664 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2665 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2666 | if (Moving) { |
| 2667 | CopyCtorArg = CastForMoving(SemaRef, CopyCtorArg); |
| 2668 | } |
| 2669 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2670 | CXXCastPath BasePath; |
| 2671 | BasePath.push_back(BaseSpec); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2672 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 2673 | CK_UncheckedDerivedToBase, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2674 | Moving ? VK_XValue : VK_LValue, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2675 | &BasePath).take(); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 2676 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2677 | InitializationKind InitKind |
| 2678 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 2679 | SourceLocation(), SourceLocation()); |
| 2680 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 2681 | &CopyCtorArg, 1); |
| 2682 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2683 | MultiExprArg(&CopyCtorArg, 1)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2684 | break; |
| 2685 | } |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2686 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2687 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2688 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2689 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2690 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2691 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2692 | CXXBaseInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2693 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2694 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 2695 | SourceLocation()), |
| 2696 | BaseSpec->isVirtual(), |
| 2697 | SourceLocation(), |
| 2698 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2699 | SourceLocation(), |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2700 | SourceLocation()); |
| 2701 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2702 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2705 | static bool RefersToRValueRef(Expr *MemRef) { |
| 2706 | ValueDecl *Referenced = cast<MemberExpr>(MemRef)->getMemberDecl(); |
| 2707 | return Referenced->getType()->isRValueReferenceType(); |
| 2708 | } |
| 2709 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2710 | static bool |
| 2711 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2712 | ImplicitInitializerKind ImplicitInitKind, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2713 | FieldDecl *Field, IndirectFieldDecl *Indirect, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2714 | CXXCtorInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2715 | if (Field->isInvalidDecl()) |
| 2716 | return true; |
| 2717 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2718 | SourceLocation Loc = Constructor->getLocation(); |
| 2719 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2720 | if (ImplicitInitKind == IIK_Copy || ImplicitInitKind == IIK_Move) { |
| 2721 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2722 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 2723 | QualType ParamType = Param->getType().getNonReferenceType(); |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 2724 | |
| 2725 | // Suppress copying zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2726 | if (Field->isBitField() && Field->getBitWidthValue(SemaRef.Context) == 0) |
| 2727 | return false; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 2728 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2729 | Expr *MemberExprBase = |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2730 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2731 | SourceLocation(), Param, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2732 | Loc, ParamType, VK_LValue, 0); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2733 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2734 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(MemberExprBase)); |
| 2735 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2736 | if (Moving) { |
| 2737 | MemberExprBase = CastForMoving(SemaRef, MemberExprBase); |
| 2738 | } |
| 2739 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2740 | // Build a reference to this field within the parameter. |
| 2741 | CXXScopeSpec SS; |
| 2742 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 2743 | Sema::LookupMemberName); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2744 | MemberLookup.addDecl(Indirect ? cast<ValueDecl>(Indirect) |
| 2745 | : cast<ValueDecl>(Field), AS_public); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2746 | MemberLookup.resolveKind(); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2747 | ExprResult CtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2748 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2749 | ParamType, Loc, |
| 2750 | /*IsArrow=*/false, |
| 2751 | SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2752 | /*TemplateKWLoc=*/SourceLocation(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2753 | /*FirstQualifierInScope=*/0, |
| 2754 | MemberLookup, |
| 2755 | /*TemplateArgs=*/0); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2756 | if (CtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2757 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2758 | |
| 2759 | // C++11 [class.copy]p15: |
| 2760 | // - if a member m has rvalue reference type T&&, it is direct-initialized |
| 2761 | // with static_cast<T&&>(x.m); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2762 | if (RefersToRValueRef(CtorArg.get())) { |
| 2763 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2766 | // When the field we are copying is an array, create index variables for |
| 2767 | // each dimension of the array. We use these index variables to subscript |
| 2768 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 2769 | // necessary iteration with these index variables. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2770 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2771 | QualType BaseType = Field->getType(); |
| 2772 | QualType SizeType = SemaRef.Context.getSizeType(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2773 | bool InitializingArray = false; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2774 | while (const ConstantArrayType *Array |
| 2775 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2776 | InitializingArray = true; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2777 | // Create the iteration variable for this array index. |
| 2778 | IdentifierInfo *IterationVarName = 0; |
| 2779 | { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2780 | SmallString<8> Str; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2781 | llvm::raw_svector_ostream OS(Str); |
| 2782 | OS << "__i" << IndexVariables.size(); |
| 2783 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 2784 | } |
| 2785 | VarDecl *IterationVar |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2786 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2787 | IterationVarName, SizeType, |
| 2788 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2789 | SC_None, SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2790 | IndexVariables.push_back(IterationVar); |
| 2791 | |
| 2792 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2793 | ExprResult IterationVarRef |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 2794 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2795 | assert(!IterationVarRef.isInvalid() && |
| 2796 | "Reference to invented variable cannot fail!"); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 2797 | IterationVarRef = SemaRef.DefaultLvalueConversion(IterationVarRef.take()); |
| 2798 | assert(!IterationVarRef.isInvalid() && |
| 2799 | "Conversion of invented variable cannot fail!"); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2800 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2801 | // Subscript the array with this iteration variable. |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2802 | CtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CtorArg.take(), Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2803 | IterationVarRef.take(), |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2804 | Loc); |
| 2805 | if (CtorArg.isInvalid()) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2806 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2807 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2808 | BaseType = Array->getElementType(); |
| 2809 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2810 | |
| 2811 | // The array subscript expression is an lvalue, which is wrong for moving. |
| 2812 | if (Moving && InitializingArray) |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2813 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2814 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2815 | // Construct the entity that we will be initializing. For an array, this |
| 2816 | // will be first element in the array, which may require several levels |
| 2817 | // of array-subscript entities. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2818 | SmallVector<InitializedEntity, 4> Entities; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2819 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2820 | if (Indirect) |
| 2821 | Entities.push_back(InitializedEntity::InitializeMember(Indirect)); |
| 2822 | else |
| 2823 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2824 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 2825 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 2826 | 0, |
| 2827 | Entities.back())); |
| 2828 | |
| 2829 | // Direct-initialize to use the copy constructor. |
| 2830 | InitializationKind InitKind = |
| 2831 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 2832 | |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2833 | Expr *CtorArgE = CtorArg.takeAs<Expr>(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2834 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2835 | &CtorArgE, 1); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2836 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2837 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2838 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2839 | MultiExprArg(&CtorArgE, 1)); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2840 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2841 | if (MemberInit.isInvalid()) |
| 2842 | return true; |
| 2843 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2844 | if (Indirect) { |
| 2845 | assert(IndexVariables.size() == 0 && |
| 2846 | "Indirect field improperly initialized"); |
| 2847 | CXXMemberInit |
| 2848 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 2849 | Loc, Loc, |
| 2850 | MemberInit.takeAs<Expr>(), |
| 2851 | Loc); |
| 2852 | } else |
| 2853 | CXXMemberInit = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, |
| 2854 | Loc, MemberInit.takeAs<Expr>(), |
| 2855 | Loc, |
| 2856 | IndexVariables.data(), |
| 2857 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2858 | return false; |
| 2859 | } |
| 2860 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2861 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 2862 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2863 | QualType FieldBaseElementType = |
| 2864 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 2865 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2866 | if (FieldBaseElementType->isRecordType()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2867 | InitializedEntity InitEntity |
| 2868 | = Indirect? InitializedEntity::InitializeMember(Indirect) |
| 2869 | : InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2870 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2871 | InitializationKind::CreateDefault(Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2872 | |
| 2873 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2874 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2875 | InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2876 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2877 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2878 | if (MemberInit.isInvalid()) |
| 2879 | return true; |
| 2880 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2881 | if (Indirect) |
| 2882 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 2883 | Indirect, Loc, |
| 2884 | Loc, |
| 2885 | MemberInit.get(), |
| 2886 | Loc); |
| 2887 | else |
| 2888 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 2889 | Field, Loc, Loc, |
| 2890 | MemberInit.get(), |
| 2891 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2892 | return false; |
| 2893 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2894 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 2895 | if (!Field->getParent()->isUnion()) { |
| 2896 | if (FieldBaseElementType->isReferenceType()) { |
| 2897 | SemaRef.Diag(Constructor->getLocation(), |
| 2898 | diag::err_uninitialized_member_in_ctor) |
| 2899 | << (int)Constructor->isImplicit() |
| 2900 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 2901 | << 0 << Field->getDeclName(); |
| 2902 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 2903 | return true; |
| 2904 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2905 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 2906 | if (FieldBaseElementType.isConstQualified()) { |
| 2907 | SemaRef.Diag(Constructor->getLocation(), |
| 2908 | diag::err_uninitialized_member_in_ctor) |
| 2909 | << (int)Constructor->isImplicit() |
| 2910 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 2911 | << 1 << Field->getDeclName(); |
| 2912 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 2913 | return true; |
| 2914 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2915 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2916 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2917 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2918 | FieldBaseElementType->isObjCRetainableType() && |
| 2919 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_None && |
| 2920 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) { |
Douglas Gregor | 3fe52ff | 2012-07-23 04:23:39 +0000 | [diff] [blame] | 2921 | // ARC: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2922 | // Default-initialize Objective-C pointers to NULL. |
| 2923 | CXXMemberInit |
| 2924 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 2925 | Loc, Loc, |
| 2926 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()), |
| 2927 | Loc); |
| 2928 | return false; |
| 2929 | } |
| 2930 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2931 | // Nothing to initialize. |
| 2932 | CXXMemberInit = 0; |
| 2933 | return false; |
| 2934 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2935 | |
| 2936 | namespace { |
| 2937 | struct BaseAndFieldInfo { |
| 2938 | Sema &S; |
| 2939 | CXXConstructorDecl *Ctor; |
| 2940 | bool AnyErrorsInInits; |
| 2941 | ImplicitInitializerKind IIK; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2942 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2943 | SmallVector<CXXCtorInitializer*, 8> AllToInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2944 | |
| 2945 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 2946 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2947 | bool Generated = Ctor->isImplicit() || Ctor->isDefaulted(); |
| 2948 | if (Generated && Ctor->isCopyConstructor()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2949 | IIK = IIK_Copy; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2950 | else if (Generated && Ctor->isMoveConstructor()) |
| 2951 | IIK = IIK_Move; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2952 | else |
| 2953 | IIK = IIK_Default; |
| 2954 | } |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 2955 | |
| 2956 | bool isImplicitCopyOrMove() const { |
| 2957 | switch (IIK) { |
| 2958 | case IIK_Copy: |
| 2959 | case IIK_Move: |
| 2960 | return true; |
| 2961 | |
| 2962 | case IIK_Default: |
| 2963 | return false; |
| 2964 | } |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2965 | |
| 2966 | llvm_unreachable("Invalid ImplicitInitializerKind!"); |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 2967 | } |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 2968 | |
| 2969 | bool addFieldInitializer(CXXCtorInitializer *Init) { |
| 2970 | AllToInit.push_back(Init); |
| 2971 | |
| 2972 | // Check whether this initializer makes the field "used". |
| 2973 | if (Init->getInit() && Init->getInit()->HasSideEffects(S.Context)) |
| 2974 | S.UnusedPrivateFields.remove(Init->getAnyMember()); |
| 2975 | |
| 2976 | return false; |
| 2977 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2978 | }; |
| 2979 | } |
| 2980 | |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 2981 | /// \brief Determine whether the given indirect field declaration is somewhere |
| 2982 | /// within an anonymous union. |
| 2983 | static bool isWithinAnonymousUnion(IndirectFieldDecl *F) { |
| 2984 | for (IndirectFieldDecl::chain_iterator C = F->chain_begin(), |
| 2985 | CEnd = F->chain_end(); |
| 2986 | C != CEnd; ++C) |
| 2987 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>((*C)->getDeclContext())) |
| 2988 | if (Record->isUnion()) |
| 2989 | return true; |
| 2990 | |
| 2991 | return false; |
| 2992 | } |
| 2993 | |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 2994 | /// \brief Determine whether the given type is an incomplete or zero-lenfgth |
| 2995 | /// array type. |
| 2996 | static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) { |
| 2997 | if (T->isIncompleteArrayType()) |
| 2998 | return true; |
| 2999 | |
| 3000 | while (const ConstantArrayType *ArrayT = Context.getAsConstantArrayType(T)) { |
| 3001 | if (!ArrayT->getSize()) |
| 3002 | return true; |
| 3003 | |
| 3004 | T = ArrayT->getElementType(); |
| 3005 | } |
| 3006 | |
| 3007 | return false; |
| 3008 | } |
| 3009 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3010 | static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3011 | FieldDecl *Field, |
| 3012 | IndirectFieldDecl *Indirect = 0) { |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3013 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 3014 | // Overwhelmingly common case: we have a direct initializer for this field. |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3015 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(Field)) |
| 3016 | return Info.addFieldInitializer(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3017 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3018 | // 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] | 3019 | // has a brace-or-equal-initializer, the entity is initialized as specified |
| 3020 | // in [dcl.init]. |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3021 | if (Field->hasInClassInitializer() && !Info.isImplicitCopyOrMove()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3022 | CXXCtorInitializer *Init; |
| 3023 | if (Indirect) |
| 3024 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 3025 | SourceLocation(), |
| 3026 | SourceLocation(), 0, |
| 3027 | SourceLocation()); |
| 3028 | else |
| 3029 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 3030 | SourceLocation(), |
| 3031 | SourceLocation(), 0, |
| 3032 | SourceLocation()); |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3033 | return Info.addFieldInitializer(Init); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
Richard Smith | c115f63 | 2011-09-18 11:14:50 +0000 | [diff] [blame] | 3036 | // Don't build an implicit initializer for union members if none was |
| 3037 | // explicitly specified. |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 3038 | if (Field->getParent()->isUnion() || |
| 3039 | (Indirect && isWithinAnonymousUnion(Indirect))) |
Richard Smith | c115f63 | 2011-09-18 11:14:50 +0000 | [diff] [blame] | 3040 | return false; |
| 3041 | |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3042 | // Don't initialize incomplete or zero-length arrays. |
| 3043 | if (isIncompleteOrZeroLengthArrayType(SemaRef.Context, Field->getType())) |
| 3044 | return false; |
| 3045 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3046 | // Don't try to build an implicit initializer if there were semantic |
| 3047 | // errors in any of the initializers (and therefore we might be |
| 3048 | // missing some that the user actually wrote). |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3049 | if (Info.AnyErrorsInInits || Field->isInvalidDecl()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3050 | return false; |
| 3051 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3052 | CXXCtorInitializer *Init = 0; |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3053 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, |
| 3054 | Indirect, Init)) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3055 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3056 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3057 | if (!Init) |
| 3058 | return false; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3059 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3060 | return Info.addFieldInitializer(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3061 | } |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3062 | |
| 3063 | bool |
| 3064 | Sema::SetDelegatingInitializer(CXXConstructorDecl *Constructor, |
| 3065 | CXXCtorInitializer *Initializer) { |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3066 | assert(Initializer->isDelegatingInitializer()); |
Sean Hunt | 01aacc0 | 2011-05-03 20:43:02 +0000 | [diff] [blame] | 3067 | Constructor->setNumCtorInitializers(1); |
| 3068 | CXXCtorInitializer **initializer = |
| 3069 | new (Context) CXXCtorInitializer*[1]; |
| 3070 | memcpy(initializer, &Initializer, sizeof (CXXCtorInitializer*)); |
| 3071 | Constructor->setCtorInitializers(initializer); |
| 3072 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3073 | if (CXXDestructorDecl *Dtor = LookupDestructor(Constructor->getParent())) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3074 | MarkFunctionReferenced(Initializer->getSourceLocation(), Dtor); |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3075 | DiagnoseUseOfDecl(Dtor, Initializer->getSourceLocation()); |
| 3076 | } |
| 3077 | |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 3078 | DelegatingCtorDecls.push_back(Constructor); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3079 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3080 | return false; |
| 3081 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3082 | |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 3083 | bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, |
| 3084 | CXXCtorInitializer **Initializers, |
| 3085 | unsigned NumInitializers, |
| 3086 | bool AnyErrors) { |
Douglas Gregor | d836c0d | 2011-09-22 23:04:35 +0000 | [diff] [blame] | 3087 | if (Constructor->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3088 | // Just store the initializers as written, they will be checked during |
| 3089 | // instantiation. |
| 3090 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3091 | Constructor->setNumCtorInitializers(NumInitializers); |
| 3092 | CXXCtorInitializer **baseOrMemberInitializers = |
| 3093 | new (Context) CXXCtorInitializer*[NumInitializers]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3094 | memcpy(baseOrMemberInitializers, Initializers, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3095 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 3096 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3097 | } |
Richard Smith | 54b3ba8 | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 3098 | |
| 3099 | // Let template instantiation know whether we had errors. |
| 3100 | if (AnyErrors) |
| 3101 | Constructor->setInvalidDecl(); |
| 3102 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3103 | return false; |
| 3104 | } |
| 3105 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3106 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3107 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3108 | // We need to build the initializer AST according to order of construction |
| 3109 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3110 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 3111 | if (!ClassDecl) |
| 3112 | return true; |
| 3113 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3114 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3115 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3116 | for (unsigned i = 0; i < NumInitializers; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3117 | CXXCtorInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3118 | |
| 3119 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3120 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3121 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3122 | Info.AllBaseFields[Member->getAnyMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3125 | // Keep track of the direct virtual bases. |
| 3126 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 3127 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 3128 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 3129 | if (I->isVirtual()) |
| 3130 | DirectVBases.insert(I); |
| 3131 | } |
| 3132 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3133 | // Push virtual bases before others. |
| 3134 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 3135 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 3136 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3137 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3138 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 3139 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3140 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3141 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3142 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3143 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3144 | VBase, IsInheritedVirtualBase, |
| 3145 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3146 | HadError = true; |
| 3147 | continue; |
| 3148 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3149 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3150 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3151 | } |
| 3152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3153 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3154 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3155 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3156 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 3157 | // Virtuals are in the virtual base list and already constructed. |
| 3158 | if (Base->isVirtual()) |
| 3159 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3160 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3161 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3162 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 3163 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3164 | } else if (!AnyErrors) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3165 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3166 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3167 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3168 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3169 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3170 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3171 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 3172 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3173 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3174 | } |
| 3175 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3176 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3177 | // Fields. |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3178 | for (DeclContext::decl_iterator Mem = ClassDecl->decls_begin(), |
| 3179 | MemEnd = ClassDecl->decls_end(); |
| 3180 | Mem != MemEnd; ++Mem) { |
| 3181 | if (FieldDecl *F = dyn_cast<FieldDecl>(*Mem)) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3182 | // C++ [class.bit]p2: |
| 3183 | // A declaration for a bit-field that omits the identifier declares an |
| 3184 | // unnamed bit-field. Unnamed bit-fields are not members and cannot be |
| 3185 | // initialized. |
| 3186 | if (F->isUnnamedBitfield()) |
| 3187 | continue; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3188 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3189 | // 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] | 3190 | // handle anonymous struct/union fields based on their individual |
| 3191 | // indirect fields. |
| 3192 | if (F->isAnonymousStructOrUnion() && Info.IIK == IIK_Default) |
| 3193 | continue; |
| 3194 | |
| 3195 | if (CollectFieldInitializer(*this, Info, F)) |
| 3196 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 3197 | continue; |
| 3198 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3199 | |
| 3200 | // Beyond this point, we only consider default initialization. |
| 3201 | if (Info.IIK != IIK_Default) |
| 3202 | continue; |
| 3203 | |
| 3204 | if (IndirectFieldDecl *F = dyn_cast<IndirectFieldDecl>(*Mem)) { |
| 3205 | if (F->getType()->isIncompleteArrayType()) { |
| 3206 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 3207 | "Incomplete array type is not valid"); |
| 3208 | continue; |
| 3209 | } |
| 3210 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3211 | // Initialize each field of an anonymous struct individually. |
| 3212 | if (CollectFieldInitializer(*this, Info, F->getAnonField(), F)) |
| 3213 | HadError = true; |
| 3214 | |
| 3215 | continue; |
| 3216 | } |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 3217 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3218 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3219 | NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3220 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3221 | Constructor->setNumCtorInitializers(NumInitializers); |
| 3222 | CXXCtorInitializer **baseOrMemberInitializers = |
| 3223 | new (Context) CXXCtorInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3224 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3225 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 3226 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 3227 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3228 | // Constructors implicitly reference the base and member |
| 3229 | // destructors. |
| 3230 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 3231 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3232 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3233 | |
| 3234 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3237 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 3238 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3239 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3240 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 3241 | return static_cast<void *>(RT->getDecl()); |
| 3242 | } |
| 3243 | return static_cast<void *>(Field); |
| 3244 | } |
| 3245 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3246 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3247 | return const_cast<Type*>(Context.getCanonicalType(BaseType).getTypePtr()); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3250 | static void *GetKeyForMember(ASTContext &Context, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3251 | CXXCtorInitializer *Member) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3252 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3253 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 3254 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3255 | // For fields injected into the class via declaration of an anonymous union, |
| 3256 | // use its anonymous union class declaration as the unique key. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3257 | FieldDecl *Field = Member->getAnyMember(); |
| 3258 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3259 | // If the field is a member of an anonymous struct or union, our key |
| 3260 | // 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] | 3261 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3262 | if (RD->isAnonymousStructOrUnion()) { |
| 3263 | while (true) { |
| 3264 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 3265 | if (Parent->isAnonymousStructOrUnion()) |
| 3266 | RD = Parent; |
| 3267 | else |
| 3268 | break; |
| 3269 | } |
| 3270 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 3271 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3272 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 3274 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3277 | static void |
| 3278 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3279 | const CXXConstructorDecl *Constructor, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3280 | CXXCtorInitializer **Inits, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3281 | unsigned NumInits) { |
| 3282 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 3283 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3284 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3285 | // Don't check initializers order unless the warning is enabled at the |
| 3286 | // location of at least one initializer. |
| 3287 | bool ShouldCheckOrder = false; |
| 3288 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3289 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3290 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order, |
| 3291 | Init->getSourceLocation()) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 3292 | != DiagnosticsEngine::Ignored) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3293 | ShouldCheckOrder = true; |
| 3294 | break; |
| 3295 | } |
| 3296 | } |
| 3297 | if (!ShouldCheckOrder) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3298 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3299 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3300 | // Build the list of bases and members in the order that they'll |
| 3301 | // actually be initialized. The explicit initializers should be in |
| 3302 | // this same order but may be missing things. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3303 | SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3304 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3305 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3306 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3307 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3308 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3309 | ClassDecl->vbases_begin(), |
| 3310 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3311 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3313 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3314 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3315 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3316 | if (Base->isVirtual()) |
| 3317 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3318 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3319 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3321 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3322 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3323 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 3324 | if (Field->isUnnamedBitfield()) |
| 3325 | continue; |
| 3326 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3327 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3330 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 3331 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3332 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3333 | CXXCtorInitializer *PrevInit = 0; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3334 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3335 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3336 | void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3337 | |
| 3338 | // Scan forward to try to find this initializer in the idealized |
| 3339 | // initializers list. |
| 3340 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3341 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3342 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3343 | |
| 3344 | // If we didn't find this initializer, it must be because we |
| 3345 | // scanned past it on a previous iteration. That can only |
| 3346 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 3347 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3348 | Sema::SemaDiagnosticBuilder D = |
| 3349 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 3350 | diag::warn_initializer_out_of_order); |
| 3351 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3352 | if (PrevInit->isAnyMemberInitializer()) |
| 3353 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3354 | else |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3355 | D << 1 << PrevInit->getTypeSourceInfo()->getType(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3356 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3357 | if (Init->isAnyMemberInitializer()) |
| 3358 | D << 0 << Init->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3359 | else |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3360 | D << 1 << Init->getTypeSourceInfo()->getType(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3361 | |
| 3362 | // Move back to the initializer's location in the ideal list. |
| 3363 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3364 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3365 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3366 | |
| 3367 | assert(IdealIndex != NumIdealInits && |
| 3368 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3369 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3370 | |
| 3371 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3372 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3375 | namespace { |
| 3376 | bool CheckRedundantInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3377 | CXXCtorInitializer *Init, |
| 3378 | CXXCtorInitializer *&PrevInit) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3379 | if (!PrevInit) { |
| 3380 | PrevInit = Init; |
| 3381 | return false; |
| 3382 | } |
| 3383 | |
| 3384 | if (FieldDecl *Field = Init->getMember()) |
| 3385 | S.Diag(Init->getSourceLocation(), |
| 3386 | diag::err_multiple_mem_initialization) |
| 3387 | << Field->getDeclName() |
| 3388 | << Init->getSourceRange(); |
| 3389 | else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3390 | const Type *BaseClass = Init->getBaseClass(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3391 | assert(BaseClass && "neither field nor base"); |
| 3392 | S.Diag(Init->getSourceLocation(), |
| 3393 | diag::err_multiple_base_initialization) |
| 3394 | << QualType(BaseClass, 0) |
| 3395 | << Init->getSourceRange(); |
| 3396 | } |
| 3397 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 3398 | << 0 << PrevInit->getSourceRange(); |
| 3399 | |
| 3400 | return true; |
| 3401 | } |
| 3402 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3403 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3404 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 3405 | |
| 3406 | bool CheckRedundantUnionInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3407 | CXXCtorInitializer *Init, |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3408 | RedundantUnionMap &Unions) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3409 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3410 | RecordDecl *Parent = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3411 | NamedDecl *Child = Field; |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3412 | |
| 3413 | while (Parent->isAnonymousStructOrUnion() || Parent->isUnion()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3414 | if (Parent->isUnion()) { |
| 3415 | UnionEntry &En = Unions[Parent]; |
| 3416 | if (En.first && En.first != Child) { |
| 3417 | S.Diag(Init->getSourceLocation(), |
| 3418 | diag::err_multiple_mem_union_initialization) |
| 3419 | << Field->getDeclName() |
| 3420 | << Init->getSourceRange(); |
| 3421 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 3422 | << 0 << En.second->getSourceRange(); |
| 3423 | return true; |
David Blaikie | 5bbe816 | 2011-11-12 20:54:14 +0000 | [diff] [blame] | 3424 | } |
| 3425 | if (!En.first) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3426 | En.first = Child; |
| 3427 | En.second = Init; |
| 3428 | } |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3429 | if (!Parent->isAnonymousStructOrUnion()) |
| 3430 | return false; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | Child = Parent; |
| 3434 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3435 | } |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3436 | |
| 3437 | return false; |
| 3438 | } |
| 3439 | } |
| 3440 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3441 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3442 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3443 | SourceLocation ColonLoc, |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 3444 | CXXCtorInitializer **meminits, |
| 3445 | unsigned NumMemInits, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3446 | bool AnyErrors) { |
| 3447 | if (!ConstructorDecl) |
| 3448 | return; |
| 3449 | |
| 3450 | AdjustDeclIfTemplate(ConstructorDecl); |
| 3451 | |
| 3452 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3453 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3454 | |
| 3455 | if (!Constructor) { |
| 3456 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 3457 | return; |
| 3458 | } |
| 3459 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3460 | CXXCtorInitializer **MemInits = |
| 3461 | reinterpret_cast<CXXCtorInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3462 | |
| 3463 | // Mapping for the duplicate initializers check. |
| 3464 | // For member initializers, this is keyed with a FieldDecl*. |
| 3465 | // For base initializers, this is keyed with a Type*. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3466 | llvm::DenseMap<void*, CXXCtorInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3467 | |
| 3468 | // Mapping for the inconsistent anonymous-union initializers check. |
| 3469 | RedundantUnionMap MemberUnions; |
| 3470 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3471 | bool HadError = false; |
| 3472 | for (unsigned i = 0; i < NumMemInits; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3473 | CXXCtorInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3474 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 3475 | // Set the source order index. |
| 3476 | Init->setSourceOrder(i); |
| 3477 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3478 | if (Init->isAnyMemberInitializer()) { |
| 3479 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3480 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 3481 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 3482 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3483 | } else if (Init->isBaseInitializer()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3484 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 3485 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 3486 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3487 | } else { |
| 3488 | assert(Init->isDelegatingInitializer()); |
| 3489 | // This must be the only initializer |
Richard Smith | a6ddea6 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 3490 | if (NumMemInits != 1) { |
| 3491 | Diag(Init->getSourceLocation(), |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3492 | diag::err_delegating_initializer_alone) |
Richard Smith | a6ddea6 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 3493 | << Init->getSourceRange() << MemInits[i ? 0 : 1]->getSourceRange(); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3494 | // We will treat this as being the only initializer. |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3495 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3496 | SetDelegatingInitializer(Constructor, MemInits[i]); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3497 | // Return immediately as the initializer is set. |
| 3498 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3499 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3502 | if (HadError) |
| 3503 | return; |
| 3504 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3505 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 3506 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3507 | SetCtorInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3510 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3511 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 3512 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 416f63e | 2011-09-18 12:11:43 +0000 | [diff] [blame] | 3513 | // Ignore dependent contexts. Also ignore unions, since their members never |
| 3514 | // have destructors implicitly called. |
| 3515 | if (ClassDecl->isDependentContext() || ClassDecl->isUnion()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3516 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3517 | |
| 3518 | // FIXME: all the access-control diagnostics are positioned on the |
| 3519 | // field/base declaration. That's probably good; that said, the |
| 3520 | // user might reasonably want to know why the destructor is being |
| 3521 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3522 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3523 | // Non-static data members. |
| 3524 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 3525 | E = ClassDecl->field_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3526 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 3527 | if (Field->isInvalidDecl()) |
| 3528 | continue; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3529 | |
| 3530 | // Don't destroy incomplete or zero-length arrays. |
| 3531 | if (isIncompleteOrZeroLengthArrayType(Context, Field->getType())) |
| 3532 | continue; |
| 3533 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3534 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 3535 | |
| 3536 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 3537 | if (!RT) |
| 3538 | continue; |
| 3539 | |
| 3540 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3541 | if (FieldClassDecl->isInvalidDecl()) |
| 3542 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3543 | if (FieldClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3544 | continue; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 3545 | // The destructor for an implicit anonymous union member is never invoked. |
| 3546 | if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion()) |
| 3547 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3548 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3549 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3550 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3551 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3552 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3553 | << Field->getDeclName() |
| 3554 | << FieldType); |
| 3555 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3556 | MarkFunctionReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3557 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3560 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 3561 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3562 | // Bases. |
| 3563 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3564 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3565 | // Bases are always records in a well-formed non-dependent class. |
| 3566 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 3567 | |
| 3568 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3569 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3570 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3571 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3572 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3573 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 3574 | if (BaseClassDecl->isInvalidDecl()) |
| 3575 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3576 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3577 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3578 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3579 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3580 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3581 | |
| 3582 | // FIXME: caret should be on the start of the class name |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3583 | CheckDestructorAccess(Base->getLocStart(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3584 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3585 | << Base->getType() |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 3586 | << Base->getSourceRange(), |
| 3587 | Context.getTypeDeclType(ClassDecl)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3588 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3589 | MarkFunctionReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3590 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3594 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 3595 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3596 | |
| 3597 | // Bases are always records in a well-formed non-dependent class. |
John McCall | 63f5578 | 2012-04-09 21:51:56 +0000 | [diff] [blame] | 3598 | const RecordType *RT = VBase->getType()->castAs<RecordType>(); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3599 | |
| 3600 | // Ignore direct virtual bases. |
| 3601 | if (DirectVirtualBases.count(RT)) |
| 3602 | continue; |
| 3603 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3604 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3605 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 3606 | if (BaseClassDecl->isInvalidDecl()) |
| 3607 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3608 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3609 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3610 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3611 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3612 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3613 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3614 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 63f5578 | 2012-04-09 21:51:56 +0000 | [diff] [blame] | 3615 | << VBase->getType(), |
| 3616 | Context.getTypeDeclType(ClassDecl)); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3617 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3618 | MarkFunctionReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3619 | DiagnoseUseOfDecl(Dtor, Location); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3620 | } |
| 3621 | } |
| 3622 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3623 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 3624 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 3625 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3626 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3627 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3628 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3629 | SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3632 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3633 | unsigned DiagID, AbstractDiagSelID SelID) { |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3634 | class NonAbstractTypeDiagnoser : public TypeDiagnoser { |
| 3635 | unsigned DiagID; |
| 3636 | AbstractDiagSelID SelID; |
| 3637 | |
| 3638 | public: |
| 3639 | NonAbstractTypeDiagnoser(unsigned DiagID, AbstractDiagSelID SelID) |
| 3640 | : TypeDiagnoser(DiagID == 0), DiagID(DiagID), SelID(SelID) { } |
| 3641 | |
| 3642 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
Eli Friedman | 2217f85 | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 3643 | if (Suppressed) return; |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3644 | if (SelID == -1) |
| 3645 | S.Diag(Loc, DiagID) << T; |
| 3646 | else |
| 3647 | S.Diag(Loc, DiagID) << SelID << T; |
| 3648 | } |
| 3649 | } Diagnoser(DiagID, SelID); |
| 3650 | |
| 3651 | return RequireNonAbstractType(Loc, T, Diagnoser); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 3654 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3655 | TypeDiagnoser &Diagnoser) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3656 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3657 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3658 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 3659 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3660 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3661 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3662 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3663 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3664 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3665 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3666 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3667 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3668 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3669 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3670 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3671 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3672 | if (!RT) |
| 3673 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3674 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3675 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3676 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3677 | // We can't answer whether something is abstract until it has a |
| 3678 | // definition. If it's currently being defined, we'll walk back |
| 3679 | // over all the declarations when we have a full definition. |
| 3680 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 3681 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3682 | return false; |
| 3683 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3684 | if (!RD->isAbstract()) |
| 3685 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3686 | |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3687 | Diagnoser.diagnose(*this, Loc, T); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3688 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3689 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3690 | return true; |
| 3691 | } |
| 3692 | |
| 3693 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 3694 | // Check if we've already emitted the list of pure virtual functions |
| 3695 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3696 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3697 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3698 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3699 | CXXFinalOverriderMap FinalOverriders; |
| 3700 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 3702 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 3703 | // more than once. |
| 3704 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 3705 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3706 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 3707 | MEnd = FinalOverriders.end(); |
| 3708 | M != MEnd; |
| 3709 | ++M) { |
| 3710 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 3711 | SOEnd = M->second.end(); |
| 3712 | SO != SOEnd; ++SO) { |
| 3713 | // C++ [class.abstract]p4: |
| 3714 | // A class is abstract if it contains or inherits at least one |
| 3715 | // pure virtual function for which the final overrider is pure |
| 3716 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3717 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3718 | // |
| 3719 | if (SO->second.size() != 1) |
| 3720 | continue; |
| 3721 | |
| 3722 | if (!SO->second.front().Method->isPure()) |
| 3723 | continue; |
| 3724 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 3725 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 3726 | continue; |
| 3727 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3728 | Diag(SO->second.front().Method->getLocation(), |
| 3729 | diag::note_pure_virtual_function) |
Chandler Carruth | 45f11b7 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 3730 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3731 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | if (!PureVirtualClassDiagSet) |
| 3735 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 3736 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3739 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3740 | struct AbstractUsageInfo { |
| 3741 | Sema &S; |
| 3742 | CXXRecordDecl *Record; |
| 3743 | CanQualType AbstractType; |
| 3744 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3745 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3746 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 3747 | : S(S), Record(Record), |
| 3748 | AbstractType(S.Context.getCanonicalType( |
| 3749 | S.Context.getTypeDeclType(Record))), |
| 3750 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3751 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3752 | void DiagnoseAbstractType() { |
| 3753 | if (Invalid) return; |
| 3754 | S.DiagnoseAbstractType(Record); |
| 3755 | Invalid = true; |
| 3756 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3757 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3758 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 3759 | }; |
| 3760 | |
| 3761 | struct CheckAbstractUsage { |
| 3762 | AbstractUsageInfo &Info; |
| 3763 | const NamedDecl *Ctx; |
| 3764 | |
| 3765 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 3766 | : Info(Info), Ctx(Ctx) {} |
| 3767 | |
| 3768 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3769 | switch (TL.getTypeLocClass()) { |
| 3770 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 3771 | #define TYPELOC(CLASS, PARENT) \ |
| 3772 | case TypeLoc::CLASS: Check(cast<CLASS##TypeLoc>(TL), Sel); break; |
| 3773 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3774 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3775 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3776 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3777 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3778 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 3779 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
Douglas Gregor | 7019186 | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 3780 | if (!TL.getArg(I)) |
| 3781 | continue; |
| 3782 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3783 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 3784 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3785 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3786 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3787 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3788 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3789 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 3790 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3791 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3792 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3793 | // Visit the type parameters from a permissive context. |
| 3794 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 3795 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 3796 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 3797 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 3798 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 3799 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3800 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3801 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3802 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3803 | // Visit pointee types from a permissive context. |
| 3804 | #define CheckPolymorphic(Type) \ |
| 3805 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 3806 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 3807 | } |
| 3808 | CheckPolymorphic(PointerTypeLoc) |
| 3809 | CheckPolymorphic(ReferenceTypeLoc) |
| 3810 | CheckPolymorphic(MemberPointerTypeLoc) |
| 3811 | CheckPolymorphic(BlockPointerTypeLoc) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3812 | CheckPolymorphic(AtomicTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3813 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3814 | /// Handle all the types we haven't given a more specific |
| 3815 | /// implementation for above. |
| 3816 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 3817 | // Every other kind of type that we haven't called out already |
| 3818 | // that has an inner type is either (1) sugar or (2) contains that |
| 3819 | // inner type in some way as a subobject. |
| 3820 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 3821 | return Visit(Next, Sel); |
| 3822 | |
| 3823 | // If there's no inner type and we're in a permissive context, |
| 3824 | // don't diagnose. |
| 3825 | if (Sel == Sema::AbstractNone) return; |
| 3826 | |
| 3827 | // Check whether the type matches the abstract type. |
| 3828 | QualType T = TL.getType(); |
| 3829 | if (T->isArrayType()) { |
| 3830 | Sel = Sema::AbstractArrayType; |
| 3831 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3832 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3833 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 3834 | if (CT != Info.AbstractType) return; |
| 3835 | |
| 3836 | // It matched; do some magic. |
| 3837 | if (Sel == Sema::AbstractArrayType) { |
| 3838 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 3839 | << T << TL.getSourceRange(); |
| 3840 | } else { |
| 3841 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 3842 | << Sel << T << TL.getSourceRange(); |
| 3843 | } |
| 3844 | Info.DiagnoseAbstractType(); |
| 3845 | } |
| 3846 | }; |
| 3847 | |
| 3848 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 3849 | Sema::AbstractDiagSelID Sel) { |
| 3850 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 3851 | } |
| 3852 | |
| 3853 | } |
| 3854 | |
| 3855 | /// Check for invalid uses of an abstract type in a method declaration. |
| 3856 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 3857 | CXXMethodDecl *MD) { |
| 3858 | // No need to do the check on definitions, which require that |
| 3859 | // the return/param types be complete. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3860 | if (MD->doesThisDeclarationHaveABody()) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3861 | return; |
| 3862 | |
| 3863 | // For safety's sake, just ignore it if we don't have type source |
| 3864 | // information. This should never happen for non-implicit methods, |
| 3865 | // but... |
| 3866 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 3867 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 3868 | } |
| 3869 | |
| 3870 | /// Check for invalid uses of an abstract type within a class definition. |
| 3871 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 3872 | CXXRecordDecl *RD) { |
| 3873 | for (CXXRecordDecl::decl_iterator |
| 3874 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 3875 | Decl *D = *I; |
| 3876 | if (D->isImplicit()) continue; |
| 3877 | |
| 3878 | // Methods and method templates. |
| 3879 | if (isa<CXXMethodDecl>(D)) { |
| 3880 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 3881 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 3882 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 3883 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 3884 | |
| 3885 | // Fields and static variables. |
| 3886 | } else if (isa<FieldDecl>(D)) { |
| 3887 | FieldDecl *FD = cast<FieldDecl>(D); |
| 3888 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 3889 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 3890 | } else if (isa<VarDecl>(D)) { |
| 3891 | VarDecl *VD = cast<VarDecl>(D); |
| 3892 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 3893 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 3894 | |
| 3895 | // Nested classes and class templates. |
| 3896 | } else if (isa<CXXRecordDecl>(D)) { |
| 3897 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 3898 | } else if (isa<ClassTemplateDecl>(D)) { |
| 3899 | CheckAbstractClassUsage(Info, |
| 3900 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 3901 | } |
| 3902 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3905 | /// \brief Perform semantic checks on a class definition that has been |
| 3906 | /// completing, introducing implicitly-declared members, checking for |
| 3907 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3908 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 3909 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3910 | return; |
| 3911 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3912 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 3913 | AbstractUsageInfo Info(*this, Record); |
| 3914 | CheckAbstractClassUsage(Info, Record); |
| 3915 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3916 | |
| 3917 | // If this is not an aggregate type and has no user-declared constructor, |
| 3918 | // complain about any non-static data members of reference or const scalar |
| 3919 | // type, since they will never get initializers. |
| 3920 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
Douglas Gregor | 5e058eb | 2012-02-09 02:20:38 +0000 | [diff] [blame] | 3921 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor() && |
| 3922 | !Record->isLambda()) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3923 | bool Complained = false; |
| 3924 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 3925 | FEnd = Record->field_end(); |
| 3926 | F != FEnd; ++F) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3927 | if (F->hasInClassInitializer() || F->isUnnamedBitfield()) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3928 | continue; |
| 3929 | |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3930 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 3931 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3932 | if (!Complained) { |
| 3933 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 3934 | << Record->getTagKind() << Record; |
| 3935 | Complained = true; |
| 3936 | } |
| 3937 | |
| 3938 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 3939 | << F->getType()->isReferenceType() |
| 3940 | << F->getDeclName(); |
| 3941 | } |
| 3942 | } |
| 3943 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3944 | |
Anders Carlsson | a5c6c2a | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 3945 | if (Record->isDynamicClass() && !Record->isDependentType()) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3946 | DynamicClasses.push_back(Record); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3947 | |
| 3948 | if (Record->getIdentifier()) { |
| 3949 | // C++ [class.mem]p13: |
| 3950 | // If T is the name of a class, then each of the following shall have a |
| 3951 | // name different from T: |
| 3952 | // - every member of every anonymous union that is a member of class T. |
| 3953 | // |
| 3954 | // C++ [class.mem]p14: |
| 3955 | // In addition, if class T has a user-declared constructor (12.1), every |
| 3956 | // non-static data member of class T shall have a name different from T. |
| 3957 | for (DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3958 | R.first != R.second; ++R.first) { |
| 3959 | NamedDecl *D = *R.first; |
| 3960 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 3961 | isa<IndirectFieldDecl>(D)) { |
| 3962 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 3963 | << D->getDeclName(); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3964 | break; |
| 3965 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3966 | } |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3967 | } |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3968 | |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 3969 | // Warn if the class has virtual methods but non-virtual public destructor. |
Douglas Gregor | f4b793c | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 3970 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3971 | CXXDestructorDecl *dtor = Record->getDestructor(); |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 3972 | if (!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3973 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 3974 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 3975 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3976 | |
David Blaikie | b6b5b97 | 2012-09-21 03:21:07 +0000 | [diff] [blame] | 3977 | if (Record->isAbstract() && Record->hasAttr<FinalAttr>()) { |
| 3978 | Diag(Record->getLocation(), diag::warn_abstract_final_class); |
| 3979 | DiagnoseAbstractType(Record); |
| 3980 | } |
| 3981 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3982 | // See if a method overloads virtual methods in a base |
| 3983 | /// class without overriding any. |
| 3984 | if (!Record->isDependentType()) { |
| 3985 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 3986 | MEnd = Record->method_end(); |
| 3987 | M != MEnd; ++M) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3988 | if (!M->isStatic()) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3989 | DiagnoseHiddenVirtualMethods(Record, *M); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3990 | } |
| 3991 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 3992 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 3993 | // C++0x [dcl.constexpr]p8: A constexpr specifier for a non-static member |
| 3994 | // function that is not a constructor declares that member function to be |
| 3995 | // const. [...] The class of which that function is a member shall be |
| 3996 | // a literal type. |
| 3997 | // |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 3998 | // If the class has virtual bases, any constexpr members will already have |
| 3999 | // been diagnosed by the checks performed on the member declaration, so |
| 4000 | // suppress this (less useful) diagnostic. |
| 4001 | if (LangOpts.CPlusPlus0x && !Record->isDependentType() && |
| 4002 | !Record->isLiteral() && !Record->getNumVBases()) { |
| 4003 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 4004 | MEnd = Record->method_end(); |
| 4005 | M != MEnd; ++M) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4006 | if (M->isConstexpr() && M->isInstance() && !isa<CXXConstructorDecl>(*M)) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4007 | switch (Record->getTemplateSpecializationKind()) { |
| 4008 | case TSK_ImplicitInstantiation: |
| 4009 | case TSK_ExplicitInstantiationDeclaration: |
| 4010 | case TSK_ExplicitInstantiationDefinition: |
| 4011 | // If a template instantiates to a non-literal type, but its members |
| 4012 | // instantiate to constexpr functions, the template is technically |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4013 | // ill-formed, but we allow it for sanity. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4014 | continue; |
| 4015 | |
| 4016 | case TSK_Undeclared: |
| 4017 | case TSK_ExplicitSpecialization: |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4018 | RequireLiteralType(M->getLocation(), Context.getRecordType(Record), |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 4019 | diag::err_constexpr_method_non_literal); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4020 | break; |
| 4021 | } |
| 4022 | |
| 4023 | // Only produce one error per class. |
| 4024 | break; |
| 4025 | } |
| 4026 | } |
| 4027 | } |
| 4028 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4029 | // Declare inherited constructors. We do this eagerly here because: |
| 4030 | // - The standard requires an eager diagnostic for conflicting inherited |
| 4031 | // constructors from different classes. |
| 4032 | // - The lazy declaration of the other implicit constructors is so as to not |
| 4033 | // waste space and performance on classes that are not meant to be |
| 4034 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
| 4035 | // have inherited constructors. |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 4036 | DeclareInheritedConstructors(Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | void Sema::CheckExplicitlyDefaultedMethods(CXXRecordDecl *Record) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4040 | for (CXXRecordDecl::method_iterator MI = Record->method_begin(), |
| 4041 | ME = Record->method_end(); |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4042 | MI != ME; ++MI) |
| 4043 | if (!MI->isInvalidDecl() && MI->isExplicitlyDefaulted()) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4044 | CheckExplicitlyDefaultedSpecialMember(*MI); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4047 | /// Is the special member function which would be selected to perform the |
| 4048 | /// specified operation on the specified class type a constexpr constructor? |
| 4049 | static bool specialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 4050 | Sema::CXXSpecialMember CSM, |
| 4051 | bool ConstArg) { |
| 4052 | Sema::SpecialMemberOverloadResult *SMOR = |
| 4053 | S.LookupSpecialMember(ClassDecl, CSM, ConstArg, |
| 4054 | false, false, false, false); |
| 4055 | if (!SMOR || !SMOR->getMethod()) |
| 4056 | // A constructor we wouldn't select can't be "involved in initializing" |
| 4057 | // anything. |
| 4058 | return true; |
| 4059 | return SMOR->getMethod()->isConstexpr(); |
| 4060 | } |
| 4061 | |
| 4062 | /// Determine whether the specified special member function would be constexpr |
| 4063 | /// if it were implicitly defined. |
| 4064 | static bool defaultedSpecialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 4065 | Sema::CXXSpecialMember CSM, |
| 4066 | bool ConstArg) { |
| 4067 | if (!S.getLangOpts().CPlusPlus0x) |
| 4068 | return false; |
| 4069 | |
| 4070 | // C++11 [dcl.constexpr]p4: |
| 4071 | // In the definition of a constexpr constructor [...] |
| 4072 | switch (CSM) { |
| 4073 | case Sema::CXXDefaultConstructor: |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4074 | // Since default constructor lookup is essentially trivial (and cannot |
| 4075 | // involve, for instance, template instantiation), we compute whether a |
| 4076 | // defaulted default constructor is constexpr directly within CXXRecordDecl. |
| 4077 | // |
| 4078 | // This is important for performance; we need to know whether the default |
| 4079 | // constructor is constexpr to determine whether the type is a literal type. |
| 4080 | return ClassDecl->defaultedDefaultConstructorIsConstexpr(); |
| 4081 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4082 | case Sema::CXXCopyConstructor: |
| 4083 | case Sema::CXXMoveConstructor: |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4084 | // For copy or move constructors, we need to perform overload resolution. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4085 | break; |
| 4086 | |
| 4087 | case Sema::CXXCopyAssignment: |
| 4088 | case Sema::CXXMoveAssignment: |
| 4089 | case Sema::CXXDestructor: |
| 4090 | case Sema::CXXInvalid: |
| 4091 | return false; |
| 4092 | } |
| 4093 | |
| 4094 | // -- if the class is a non-empty union, or for each non-empty anonymous |
| 4095 | // union member of a non-union class, exactly one non-static data member |
| 4096 | // shall be initialized; [DR1359] |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4097 | // |
| 4098 | // If we squint, this is guaranteed, since exactly one non-static data member |
| 4099 | // will be initialized (if the constructor isn't deleted), we just don't know |
| 4100 | // which one. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4101 | if (ClassDecl->isUnion()) |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4102 | return true; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4103 | |
| 4104 | // -- the class shall not have any virtual base classes; |
| 4105 | if (ClassDecl->getNumVBases()) |
| 4106 | return false; |
| 4107 | |
| 4108 | // -- every constructor involved in initializing [...] base class |
| 4109 | // sub-objects shall be a constexpr constructor; |
| 4110 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4111 | BEnd = ClassDecl->bases_end(); |
| 4112 | B != BEnd; ++B) { |
| 4113 | const RecordType *BaseType = B->getType()->getAs<RecordType>(); |
| 4114 | if (!BaseType) continue; |
| 4115 | |
| 4116 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4117 | if (!specialMemberIsConstexpr(S, BaseClassDecl, CSM, ConstArg)) |
| 4118 | return false; |
| 4119 | } |
| 4120 | |
| 4121 | // -- every constructor involved in initializing non-static data members |
| 4122 | // [...] shall be a constexpr constructor; |
| 4123 | // -- every non-static data member and base class sub-object shall be |
| 4124 | // initialized |
| 4125 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4126 | FEnd = ClassDecl->field_end(); |
| 4127 | F != FEnd; ++F) { |
| 4128 | if (F->isInvalidDecl()) |
| 4129 | continue; |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4130 | if (const RecordType *RecordTy = |
| 4131 | S.Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4132 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4133 | if (!specialMemberIsConstexpr(S, FieldRecDecl, CSM, ConstArg)) |
| 4134 | return false; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4135 | } |
| 4136 | } |
| 4137 | |
| 4138 | // All OK, it's constexpr! |
| 4139 | return true; |
| 4140 | } |
| 4141 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4142 | static Sema::ImplicitExceptionSpecification |
| 4143 | computeImplicitExceptionSpec(Sema &S, SourceLocation Loc, CXXMethodDecl *MD) { |
| 4144 | switch (S.getSpecialMember(MD)) { |
| 4145 | case Sema::CXXDefaultConstructor: |
| 4146 | return S.ComputeDefaultedDefaultCtorExceptionSpec(Loc, MD); |
| 4147 | case Sema::CXXCopyConstructor: |
| 4148 | return S.ComputeDefaultedCopyCtorExceptionSpec(MD); |
| 4149 | case Sema::CXXCopyAssignment: |
| 4150 | return S.ComputeDefaultedCopyAssignmentExceptionSpec(MD); |
| 4151 | case Sema::CXXMoveConstructor: |
| 4152 | return S.ComputeDefaultedMoveCtorExceptionSpec(MD); |
| 4153 | case Sema::CXXMoveAssignment: |
| 4154 | return S.ComputeDefaultedMoveAssignmentExceptionSpec(MD); |
| 4155 | case Sema::CXXDestructor: |
| 4156 | return S.ComputeDefaultedDtorExceptionSpec(MD); |
| 4157 | case Sema::CXXInvalid: |
| 4158 | break; |
| 4159 | } |
| 4160 | llvm_unreachable("only special members have implicit exception specs"); |
| 4161 | } |
| 4162 | |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4163 | static void |
| 4164 | updateExceptionSpec(Sema &S, FunctionDecl *FD, const FunctionProtoType *FPT, |
| 4165 | const Sema::ImplicitExceptionSpecification &ExceptSpec) { |
| 4166 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 4167 | ExceptSpec.getEPI(EPI); |
| 4168 | const FunctionProtoType *NewFPT = cast<FunctionProtoType>( |
| 4169 | S.Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 4170 | FPT->getNumArgs(), EPI)); |
| 4171 | FD->setType(QualType(NewFPT, 0)); |
| 4172 | } |
| 4173 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4174 | void Sema::EvaluateImplicitExceptionSpec(SourceLocation Loc, CXXMethodDecl *MD) { |
| 4175 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 4176 | if (FPT->getExceptionSpecType() != EST_Unevaluated) |
| 4177 | return; |
| 4178 | |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4179 | // Evaluate the exception specification. |
| 4180 | ImplicitExceptionSpecification ExceptSpec = |
| 4181 | computeImplicitExceptionSpec(*this, Loc, MD); |
| 4182 | |
| 4183 | // Update the type of the special member to use it. |
| 4184 | updateExceptionSpec(*this, MD, FPT, ExceptSpec); |
| 4185 | |
| 4186 | // A user-provided destructor can be defined outside the class. When that |
| 4187 | // happens, be sure to update the exception specification on both |
| 4188 | // declarations. |
| 4189 | const FunctionProtoType *CanonicalFPT = |
| 4190 | MD->getCanonicalDecl()->getType()->castAs<FunctionProtoType>(); |
| 4191 | if (CanonicalFPT->getExceptionSpecType() == EST_Unevaluated) |
| 4192 | updateExceptionSpec(*this, MD->getCanonicalDecl(), |
| 4193 | CanonicalFPT, ExceptSpec); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4194 | } |
| 4195 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4196 | void Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD) { |
| 4197 | CXXRecordDecl *RD = MD->getParent(); |
| 4198 | CXXSpecialMember CSM = getSpecialMember(MD); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4199 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4200 | assert(MD->isExplicitlyDefaulted() && CSM != CXXInvalid && |
| 4201 | "not an explicitly-defaulted special member"); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4202 | |
| 4203 | // Whether this was the first-declared instance of the constructor. |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4204 | // This affects whether we implicitly add an exception spec and constexpr. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4205 | bool First = MD == MD->getCanonicalDecl(); |
| 4206 | |
| 4207 | bool HadError = false; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4208 | |
| 4209 | // C++11 [dcl.fct.def.default]p1: |
| 4210 | // A function that is explicitly defaulted shall |
| 4211 | // -- be a special member function (checked elsewhere), |
| 4212 | // -- have the same type (except for ref-qualifiers, and except that a |
| 4213 | // copy operation can take a non-const reference) as an implicit |
| 4214 | // declaration, and |
| 4215 | // -- not have default arguments. |
| 4216 | unsigned ExpectedParams = 1; |
| 4217 | if (CSM == CXXDefaultConstructor || CSM == CXXDestructor) |
| 4218 | ExpectedParams = 0; |
| 4219 | if (MD->getNumParams() != ExpectedParams) { |
| 4220 | // This also checks for default arguments: a copy or move constructor with a |
| 4221 | // default argument is classified as a default constructor, and assignment |
| 4222 | // operations and destructors can't have default arguments. |
| 4223 | Diag(MD->getLocation(), diag::err_defaulted_special_member_params) |
| 4224 | << CSM << MD->getSourceRange(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4225 | HadError = true; |
| 4226 | } |
| 4227 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4228 | const FunctionProtoType *Type = MD->getType()->getAs<FunctionProtoType>(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4229 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4230 | // Compute argument constness, constexpr, and triviality. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4231 | bool CanHaveConstParam = false; |
Axel Naumann | 8f411c3 | 2012-09-17 14:26:53 +0000 | [diff] [blame] | 4232 | bool Trivial = false; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4233 | switch (CSM) { |
| 4234 | case CXXDefaultConstructor: |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4235 | Trivial = RD->hasTrivialDefaultConstructor(); |
| 4236 | break; |
| 4237 | case CXXCopyConstructor: |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame^] | 4238 | CanHaveConstParam = RD->implicitCopyConstructorHasConstParam(); |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4239 | Trivial = RD->hasTrivialCopyConstructor(); |
| 4240 | break; |
| 4241 | case CXXCopyAssignment: |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame^] | 4242 | CanHaveConstParam = RD->implicitCopyAssignmentHasConstParam(); |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4243 | Trivial = RD->hasTrivialCopyAssignment(); |
| 4244 | break; |
| 4245 | case CXXMoveConstructor: |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4246 | Trivial = RD->hasTrivialMoveConstructor(); |
| 4247 | break; |
| 4248 | case CXXMoveAssignment: |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4249 | Trivial = RD->hasTrivialMoveAssignment(); |
| 4250 | break; |
| 4251 | case CXXDestructor: |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4252 | Trivial = RD->hasTrivialDestructor(); |
| 4253 | break; |
| 4254 | case CXXInvalid: |
| 4255 | llvm_unreachable("non-special member explicitly defaulted!"); |
| 4256 | } |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4257 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4258 | QualType ReturnType = Context.VoidTy; |
| 4259 | if (CSM == CXXCopyAssignment || CSM == CXXMoveAssignment) { |
| 4260 | // Check for return type matching. |
| 4261 | ReturnType = Type->getResultType(); |
| 4262 | QualType ExpectedReturnType = |
| 4263 | Context.getLValueReferenceType(Context.getTypeDeclType(RD)); |
| 4264 | if (!Context.hasSameType(ReturnType, ExpectedReturnType)) { |
| 4265 | Diag(MD->getLocation(), diag::err_defaulted_special_member_return_type) |
| 4266 | << (CSM == CXXMoveAssignment) << ExpectedReturnType; |
| 4267 | HadError = true; |
| 4268 | } |
| 4269 | |
| 4270 | // A defaulted special member cannot have cv-qualifiers. |
| 4271 | if (Type->getTypeQuals()) { |
| 4272 | Diag(MD->getLocation(), diag::err_defaulted_special_member_quals) |
| 4273 | << (CSM == CXXMoveAssignment); |
| 4274 | HadError = true; |
| 4275 | } |
| 4276 | } |
| 4277 | |
| 4278 | // Check for parameter type matching. |
| 4279 | QualType ArgType = ExpectedParams ? Type->getArgType(0) : QualType(); |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4280 | bool HasConstParam = false; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4281 | if (ExpectedParams && ArgType->isReferenceType()) { |
| 4282 | // Argument must be reference to possibly-const T. |
| 4283 | QualType ReferentType = ArgType->getPointeeType(); |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4284 | HasConstParam = ReferentType.isConstQualified(); |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4285 | |
| 4286 | if (ReferentType.isVolatileQualified()) { |
| 4287 | Diag(MD->getLocation(), |
| 4288 | diag::err_defaulted_special_member_volatile_param) << CSM; |
| 4289 | HadError = true; |
| 4290 | } |
| 4291 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4292 | if (HasConstParam && !CanHaveConstParam) { |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4293 | if (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment) { |
| 4294 | Diag(MD->getLocation(), |
| 4295 | diag::err_defaulted_special_member_copy_const_param) |
| 4296 | << (CSM == CXXCopyAssignment); |
| 4297 | // FIXME: Explain why this special member can't be const. |
| 4298 | } else { |
| 4299 | Diag(MD->getLocation(), |
| 4300 | diag::err_defaulted_special_member_move_const_param) |
| 4301 | << (CSM == CXXMoveAssignment); |
| 4302 | } |
| 4303 | HadError = true; |
| 4304 | } |
| 4305 | |
| 4306 | // If a function is explicitly defaulted on its first declaration, it shall |
| 4307 | // have the same parameter type as if it had been implicitly declared. |
| 4308 | // (Presumably this is to prevent it from being trivial?) |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4309 | if (!HasConstParam && CanHaveConstParam && First) |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4310 | Diag(MD->getLocation(), |
| 4311 | diag::err_defaulted_special_member_copy_non_const_param) |
| 4312 | << (CSM == CXXCopyAssignment); |
| 4313 | } else if (ExpectedParams) { |
| 4314 | // A copy assignment operator can take its argument by value, but a |
| 4315 | // defaulted one cannot. |
| 4316 | assert(CSM == CXXCopyAssignment && "unexpected non-ref argument"); |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4317 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_not_ref); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4318 | HadError = true; |
| 4319 | } |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4320 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4321 | // Rebuild the type with the implicit exception specification added, if we |
| 4322 | // are going to need it. |
| 4323 | const FunctionProtoType *ImplicitType = 0; |
| 4324 | if (First || Type->hasExceptionSpec()) { |
| 4325 | FunctionProtoType::ExtProtoInfo EPI = Type->getExtProtoInfo(); |
| 4326 | computeImplicitExceptionSpec(*this, MD->getLocation(), MD).getEPI(EPI); |
| 4327 | ImplicitType = cast<FunctionProtoType>( |
| 4328 | Context.getFunctionType(ReturnType, &ArgType, ExpectedParams, EPI)); |
| 4329 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4330 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4331 | // C++11 [dcl.fct.def.default]p2: |
| 4332 | // An explicitly-defaulted function may be declared constexpr only if it |
| 4333 | // would have been implicitly declared as constexpr, |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4334 | // Do not apply this rule to members of class templates, since core issue 1358 |
| 4335 | // makes such functions always instantiate to constexpr functions. For |
| 4336 | // non-constructors, this is checked elsewhere. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4337 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, RD, CSM, |
| 4338 | HasConstParam); |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4339 | if (isa<CXXConstructorDecl>(MD) && MD->isConstexpr() && !Constexpr && |
| 4340 | MD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) { |
| 4341 | Diag(MD->getLocStart(), diag::err_incorrect_defaulted_constexpr) << CSM; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4342 | // FIXME: Explain why the constructor can't be constexpr. |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4343 | HadError = true; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4344 | } |
| 4345 | // and may have an explicit exception-specification only if it is compatible |
| 4346 | // with the exception-specification on the implicit declaration. |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4347 | if (Type->hasExceptionSpec() && |
| 4348 | CheckEquivalentExceptionSpec( |
| 4349 | PDiag(diag::err_incorrect_defaulted_exception_spec) << CSM, |
| 4350 | PDiag(), ImplicitType, SourceLocation(), Type, MD->getLocation())) |
| 4351 | HadError = true; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4352 | |
| 4353 | // If a function is explicitly defaulted on its first declaration, |
| 4354 | if (First) { |
| 4355 | // -- it is implicitly considered to be constexpr if the implicit |
| 4356 | // definition would be, |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4357 | MD->setConstexpr(Constexpr); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4358 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4359 | // -- it is implicitly considered to have the same exception-specification |
| 4360 | // as if it had been implicitly declared, |
| 4361 | MD->setType(QualType(ImplicitType, 0)); |
Richard Smith | e653ba2 | 2012-02-26 00:31:33 +0000 | [diff] [blame] | 4362 | |
| 4363 | // Such a function is also trivial if the implicitly-declared function |
| 4364 | // would have been. |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4365 | MD->setTrivial(Trivial); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4368 | if (ShouldDeleteSpecialMember(MD, CSM)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4369 | if (First) { |
| 4370 | MD->setDeletedAsWritten(); |
| 4371 | } else { |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4372 | // C++11 [dcl.fct.def.default]p4: |
| 4373 | // [For a] user-provided explicitly-defaulted function [...] if such a |
| 4374 | // function is implicitly defined as deleted, the program is ill-formed. |
| 4375 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) << CSM; |
| 4376 | HadError = true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4377 | } |
| 4378 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4379 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4380 | if (HadError) |
| 4381 | MD->setInvalidDecl(); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4382 | } |
| 4383 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4384 | namespace { |
| 4385 | struct SpecialMemberDeletionInfo { |
| 4386 | Sema &S; |
| 4387 | CXXMethodDecl *MD; |
| 4388 | Sema::CXXSpecialMember CSM; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4389 | bool Diagnose; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4390 | |
| 4391 | // Properties of the special member, computed for convenience. |
| 4392 | bool IsConstructor, IsAssignment, IsMove, ConstArg, VolatileArg; |
| 4393 | SourceLocation Loc; |
| 4394 | |
| 4395 | bool AllFieldsAreConst; |
| 4396 | |
| 4397 | SpecialMemberDeletionInfo(Sema &S, CXXMethodDecl *MD, |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4398 | Sema::CXXSpecialMember CSM, bool Diagnose) |
| 4399 | : S(S), MD(MD), CSM(CSM), Diagnose(Diagnose), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4400 | IsConstructor(false), IsAssignment(false), IsMove(false), |
| 4401 | ConstArg(false), VolatileArg(false), Loc(MD->getLocation()), |
| 4402 | AllFieldsAreConst(true) { |
| 4403 | switch (CSM) { |
| 4404 | case Sema::CXXDefaultConstructor: |
| 4405 | case Sema::CXXCopyConstructor: |
| 4406 | IsConstructor = true; |
| 4407 | break; |
| 4408 | case Sema::CXXMoveConstructor: |
| 4409 | IsConstructor = true; |
| 4410 | IsMove = true; |
| 4411 | break; |
| 4412 | case Sema::CXXCopyAssignment: |
| 4413 | IsAssignment = true; |
| 4414 | break; |
| 4415 | case Sema::CXXMoveAssignment: |
| 4416 | IsAssignment = true; |
| 4417 | IsMove = true; |
| 4418 | break; |
| 4419 | case Sema::CXXDestructor: |
| 4420 | break; |
| 4421 | case Sema::CXXInvalid: |
| 4422 | llvm_unreachable("invalid special member kind"); |
| 4423 | } |
| 4424 | |
| 4425 | if (MD->getNumParams()) { |
| 4426 | ConstArg = MD->getParamDecl(0)->getType().isConstQualified(); |
| 4427 | VolatileArg = MD->getParamDecl(0)->getType().isVolatileQualified(); |
| 4428 | } |
| 4429 | } |
| 4430 | |
| 4431 | bool inUnion() const { return MD->getParent()->isUnion(); } |
| 4432 | |
| 4433 | /// Look up the corresponding special member in the given class. |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4434 | Sema::SpecialMemberOverloadResult *lookupIn(CXXRecordDecl *Class, |
| 4435 | unsigned Quals) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4436 | unsigned TQ = MD->getTypeQualifiers(); |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4437 | // cv-qualifiers on class members don't affect default ctor / dtor calls. |
| 4438 | if (CSM == Sema::CXXDefaultConstructor || CSM == Sema::CXXDestructor) |
| 4439 | Quals = 0; |
| 4440 | return S.LookupSpecialMember(Class, CSM, |
| 4441 | ConstArg || (Quals & Qualifiers::Const), |
| 4442 | VolatileArg || (Quals & Qualifiers::Volatile), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4443 | MD->getRefQualifier() == RQ_RValue, |
| 4444 | TQ & Qualifiers::Const, |
| 4445 | TQ & Qualifiers::Volatile); |
| 4446 | } |
| 4447 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4448 | typedef llvm::PointerUnion<CXXBaseSpecifier*, FieldDecl*> Subobject; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4449 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4450 | bool shouldDeleteForBase(CXXBaseSpecifier *Base); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4451 | bool shouldDeleteForField(FieldDecl *FD); |
| 4452 | bool shouldDeleteForAllConstMembers(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4453 | |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4454 | bool shouldDeleteForClassSubobject(CXXRecordDecl *Class, Subobject Subobj, |
| 4455 | unsigned Quals); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4456 | bool shouldDeleteForSubobjectCall(Subobject Subobj, |
| 4457 | Sema::SpecialMemberOverloadResult *SMOR, |
| 4458 | bool IsDtorCallInCtor); |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4459 | |
| 4460 | bool isAccessible(Subobject Subobj, CXXMethodDecl *D); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4461 | }; |
| 4462 | } |
| 4463 | |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4464 | /// Is the given special member inaccessible when used on the given |
| 4465 | /// sub-object. |
| 4466 | bool SpecialMemberDeletionInfo::isAccessible(Subobject Subobj, |
| 4467 | CXXMethodDecl *target) { |
| 4468 | /// If we're operating on a base class, the object type is the |
| 4469 | /// type of this special member. |
| 4470 | QualType objectTy; |
Dmitri Gribenko | 1ad23d6 | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 4471 | AccessSpecifier access = target->getAccess(); |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4472 | if (CXXBaseSpecifier *base = Subobj.dyn_cast<CXXBaseSpecifier*>()) { |
| 4473 | objectTy = S.Context.getTypeDeclType(MD->getParent()); |
| 4474 | access = CXXRecordDecl::MergeAccess(base->getAccessSpecifier(), access); |
| 4475 | |
| 4476 | // If we're operating on a field, the object type is the type of the field. |
| 4477 | } else { |
| 4478 | objectTy = S.Context.getTypeDeclType(target->getParent()); |
| 4479 | } |
| 4480 | |
| 4481 | return S.isSpecialMemberAccessibleForDeletion(target, access, objectTy); |
| 4482 | } |
| 4483 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4484 | /// Check whether we should delete a special member due to the implicit |
| 4485 | /// definition containing a call to a special member of a subobject. |
| 4486 | bool SpecialMemberDeletionInfo::shouldDeleteForSubobjectCall( |
| 4487 | Subobject Subobj, Sema::SpecialMemberOverloadResult *SMOR, |
| 4488 | bool IsDtorCallInCtor) { |
| 4489 | CXXMethodDecl *Decl = SMOR->getMethod(); |
| 4490 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
| 4491 | |
| 4492 | int DiagKind = -1; |
| 4493 | |
| 4494 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::NoMemberOrDeleted) |
| 4495 | DiagKind = !Decl ? 0 : 1; |
| 4496 | else if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 4497 | DiagKind = 2; |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4498 | else if (!isAccessible(Subobj, Decl)) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4499 | DiagKind = 3; |
| 4500 | else if (!IsDtorCallInCtor && Field && Field->getParent()->isUnion() && |
| 4501 | !Decl->isTrivial()) { |
| 4502 | // A member of a union must have a trivial corresponding special member. |
| 4503 | // As a weird special case, a destructor call from a union's constructor |
| 4504 | // must be accessible and non-deleted, but need not be trivial. Such a |
| 4505 | // destructor is never actually called, but is semantically checked as |
| 4506 | // if it were. |
| 4507 | DiagKind = 4; |
| 4508 | } |
| 4509 | |
| 4510 | if (DiagKind == -1) |
| 4511 | return false; |
| 4512 | |
| 4513 | if (Diagnose) { |
| 4514 | if (Field) { |
| 4515 | S.Diag(Field->getLocation(), |
| 4516 | diag::note_deleted_special_member_class_subobject) |
| 4517 | << CSM << MD->getParent() << /*IsField*/true |
| 4518 | << Field << DiagKind << IsDtorCallInCtor; |
| 4519 | } else { |
| 4520 | CXXBaseSpecifier *Base = Subobj.get<CXXBaseSpecifier*>(); |
| 4521 | S.Diag(Base->getLocStart(), |
| 4522 | diag::note_deleted_special_member_class_subobject) |
| 4523 | << CSM << MD->getParent() << /*IsField*/false |
| 4524 | << Base->getType() << DiagKind << IsDtorCallInCtor; |
| 4525 | } |
| 4526 | |
| 4527 | if (DiagKind == 1) |
| 4528 | S.NoteDeletedFunction(Decl); |
| 4529 | // FIXME: Explain inaccessibility if DiagKind == 3. |
| 4530 | } |
| 4531 | |
| 4532 | return true; |
| 4533 | } |
| 4534 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4535 | /// 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] | 4536 | /// 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] | 4537 | bool SpecialMemberDeletionInfo::shouldDeleteForClassSubobject( |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4538 | CXXRecordDecl *Class, Subobject Subobj, unsigned Quals) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4539 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4540 | |
| 4541 | // C++11 [class.ctor]p5: |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4542 | // -- any direct or virtual base class, or non-static data member with no |
| 4543 | // brace-or-equal-initializer, has class type M (or array thereof) and |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4544 | // either M has no default constructor or overload resolution as applied |
| 4545 | // to M's default constructor results in an ambiguity or in a function |
| 4546 | // that is deleted or inaccessible |
| 4547 | // C++11 [class.copy]p11, C++11 [class.copy]p23: |
| 4548 | // -- a direct or virtual base class B that cannot be copied/moved because |
| 4549 | // overload resolution, as applied to B's corresponding special member, |
| 4550 | // results in an ambiguity or a function that is deleted or inaccessible |
| 4551 | // from the defaulted special member |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4552 | // C++11 [class.dtor]p5: |
| 4553 | // -- any direct or virtual base class [...] has a type with a destructor |
| 4554 | // that is deleted or inaccessible |
| 4555 | if (!(CSM == Sema::CXXDefaultConstructor && |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4556 | Field && Field->hasInClassInitializer()) && |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4557 | shouldDeleteForSubobjectCall(Subobj, lookupIn(Class, Quals), false)) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4558 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4559 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4560 | // C++11 [class.ctor]p5, C++11 [class.copy]p11: |
| 4561 | // -- any direct or virtual base class or non-static data member has a |
| 4562 | // type with a destructor that is deleted or inaccessible |
| 4563 | if (IsConstructor) { |
| 4564 | Sema::SpecialMemberOverloadResult *SMOR = |
| 4565 | S.LookupSpecialMember(Class, Sema::CXXDestructor, |
| 4566 | false, false, false, false, false); |
| 4567 | if (shouldDeleteForSubobjectCall(Subobj, SMOR, true)) |
| 4568 | return true; |
| 4569 | } |
| 4570 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4571 | return false; |
| 4572 | } |
| 4573 | |
| 4574 | /// Check whether we should delete a special member function due to the class |
| 4575 | /// having a particular direct or virtual base class. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4576 | bool SpecialMemberDeletionInfo::shouldDeleteForBase(CXXBaseSpecifier *Base) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4577 | CXXRecordDecl *BaseClass = Base->getType()->getAsCXXRecordDecl(); |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4578 | return shouldDeleteForClassSubobject(BaseClass, Base, 0); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
| 4581 | /// Check whether we should delete a special member function due to the class |
| 4582 | /// having a particular non-static data member. |
| 4583 | bool SpecialMemberDeletionInfo::shouldDeleteForField(FieldDecl *FD) { |
| 4584 | QualType FieldType = S.Context.getBaseElementType(FD->getType()); |
| 4585 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 4586 | |
| 4587 | if (CSM == Sema::CXXDefaultConstructor) { |
| 4588 | // For a default constructor, all references must be initialized in-class |
| 4589 | // and, if a union, it must have a non-const member. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4590 | if (FieldType->isReferenceType() && !FD->hasInClassInitializer()) { |
| 4591 | if (Diagnose) |
| 4592 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
| 4593 | << MD->getParent() << FD << FieldType << /*Reference*/0; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4594 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4595 | } |
Richard Smith | 79363f5 | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 4596 | // C++11 [class.ctor]p5: any non-variant non-static data member of |
| 4597 | // const-qualified type (or array thereof) with no |
| 4598 | // brace-or-equal-initializer does not have a user-provided default |
| 4599 | // constructor. |
| 4600 | if (!inUnion() && FieldType.isConstQualified() && |
| 4601 | !FD->hasInClassInitializer() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4602 | (!FieldRecord || !FieldRecord->hasUserProvidedDefaultConstructor())) { |
| 4603 | if (Diagnose) |
| 4604 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
Richard Smith | a2e76f5 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 4605 | << MD->getParent() << FD << FD->getType() << /*Const*/1; |
Richard Smith | 79363f5 | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 4606 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | if (inUnion() && !FieldType.isConstQualified()) |
| 4610 | AllFieldsAreConst = false; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4611 | } else if (CSM == Sema::CXXCopyConstructor) { |
| 4612 | // For a copy constructor, data members must not be of rvalue reference |
| 4613 | // type. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4614 | if (FieldType->isRValueReferenceType()) { |
| 4615 | if (Diagnose) |
| 4616 | S.Diag(FD->getLocation(), diag::note_deleted_copy_ctor_rvalue_reference) |
| 4617 | << MD->getParent() << FD << FieldType; |
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 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4620 | } else if (IsAssignment) { |
| 4621 | // For an assignment operator, data members must not be of reference type. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4622 | if (FieldType->isReferenceType()) { |
| 4623 | if (Diagnose) |
| 4624 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
| 4625 | << IsMove << MD->getParent() << FD << FieldType << /*Reference*/0; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4626 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4627 | } |
| 4628 | if (!FieldRecord && FieldType.isConstQualified()) { |
| 4629 | // C++11 [class.copy]p23: |
| 4630 | // -- a non-static data member of const non-class type (or array thereof) |
| 4631 | if (Diagnose) |
| 4632 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
Richard Smith | a2e76f5 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 4633 | << IsMove << MD->getParent() << FD << FD->getType() << /*Const*/1; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4634 | return true; |
| 4635 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | if (FieldRecord) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4639 | // Some additional restrictions exist on the variant members. |
| 4640 | if (!inUnion() && FieldRecord->isUnion() && |
| 4641 | FieldRecord->isAnonymousStructOrUnion()) { |
| 4642 | bool AllVariantFieldsAreConst = true; |
| 4643 | |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4644 | // FIXME: Handle anonymous unions declared within anonymous unions. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4645 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 4646 | UE = FieldRecord->field_end(); |
| 4647 | UI != UE; ++UI) { |
| 4648 | QualType UnionFieldType = S.Context.getBaseElementType(UI->getType()); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4649 | |
| 4650 | if (!UnionFieldType.isConstQualified()) |
| 4651 | AllVariantFieldsAreConst = false; |
| 4652 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4653 | CXXRecordDecl *UnionFieldRecord = UnionFieldType->getAsCXXRecordDecl(); |
| 4654 | if (UnionFieldRecord && |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4655 | shouldDeleteForClassSubobject(UnionFieldRecord, *UI, |
| 4656 | UnionFieldType.getCVRQualifiers())) |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4657 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | // At least one member in each anonymous union must be non-const |
Douglas Gregor | 221c27f | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 4661 | if (CSM == Sema::CXXDefaultConstructor && AllVariantFieldsAreConst && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4662 | FieldRecord->field_begin() != FieldRecord->field_end()) { |
| 4663 | if (Diagnose) |
| 4664 | S.Diag(FieldRecord->getLocation(), |
| 4665 | diag::note_deleted_default_ctor_all_const) |
| 4666 | << MD->getParent() << /*anonymous union*/1; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4667 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4668 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4669 | |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4670 | // Don't check the implicit member of the anonymous union type. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4671 | // This is technically non-conformant, but sanity demands it. |
| 4672 | return false; |
| 4673 | } |
| 4674 | |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4675 | if (shouldDeleteForClassSubobject(FieldRecord, FD, |
| 4676 | FieldType.getCVRQualifiers())) |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4677 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
| 4680 | return false; |
| 4681 | } |
| 4682 | |
| 4683 | /// C++11 [class.ctor] p5: |
| 4684 | /// A defaulted default constructor for a class X is defined as deleted if |
| 4685 | /// X is a union and all of its variant members are of const-qualified type. |
| 4686 | bool SpecialMemberDeletionInfo::shouldDeleteForAllConstMembers() { |
Douglas Gregor | 221c27f | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 4687 | // This is a silly definition, because it gives an empty union a deleted |
| 4688 | // default constructor. Don't do that. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4689 | if (CSM == Sema::CXXDefaultConstructor && inUnion() && AllFieldsAreConst && |
| 4690 | (MD->getParent()->field_begin() != MD->getParent()->field_end())) { |
| 4691 | if (Diagnose) |
| 4692 | S.Diag(MD->getParent()->getLocation(), |
| 4693 | diag::note_deleted_default_ctor_all_const) |
| 4694 | << MD->getParent() << /*not anonymous union*/0; |
| 4695 | return true; |
| 4696 | } |
| 4697 | return false; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | /// Determine whether a defaulted special member function should be defined as |
| 4701 | /// deleted, as specified in C++11 [class.ctor]p5, C++11 [class.copy]p11, |
| 4702 | /// C++11 [class.copy]p23, and C++11 [class.dtor]p5. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4703 | bool Sema::ShouldDeleteSpecialMember(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 4704 | bool Diagnose) { |
Richard Smith | eef0029 | 2012-08-06 02:25:10 +0000 | [diff] [blame] | 4705 | if (MD->isInvalidDecl()) |
| 4706 | return false; |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 4707 | CXXRecordDecl *RD = MD->getParent(); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4708 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 4709 | if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4710 | return false; |
| 4711 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4712 | // C++11 [expr.lambda.prim]p19: |
| 4713 | // The closure type associated with a lambda-expression has a |
| 4714 | // deleted (8.4.3) default constructor and a deleted copy |
| 4715 | // assignment operator. |
| 4716 | if (RD->isLambda() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4717 | (CSM == CXXDefaultConstructor || CSM == CXXCopyAssignment)) { |
| 4718 | if (Diagnose) |
| 4719 | Diag(RD->getLocation(), diag::note_lambda_decl); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4720 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 4723 | // For an anonymous struct or union, the copy and assignment special members |
| 4724 | // will never be used, so skip the check. For an anonymous union declared at |
| 4725 | // namespace scope, the constructor and destructor are used. |
| 4726 | if (CSM != CXXDefaultConstructor && CSM != CXXDestructor && |
| 4727 | RD->isAnonymousStructOrUnion()) |
| 4728 | return false; |
| 4729 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4730 | // C++11 [class.copy]p7, p18: |
| 4731 | // If the class definition declares a move constructor or move assignment |
| 4732 | // operator, an implicitly declared copy constructor or copy assignment |
| 4733 | // operator is defined as deleted. |
| 4734 | if (MD->isImplicit() && |
| 4735 | (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment)) { |
| 4736 | CXXMethodDecl *UserDeclaredMove = 0; |
| 4737 | |
| 4738 | // In Microsoft mode, a user-declared move only causes the deletion of the |
| 4739 | // corresponding copy operation, not both copy operations. |
| 4740 | if (RD->hasUserDeclaredMoveConstructor() && |
| 4741 | (!getLangOpts().MicrosoftMode || CSM == CXXCopyConstructor)) { |
| 4742 | if (!Diagnose) return true; |
| 4743 | UserDeclaredMove = RD->getMoveConstructor(); |
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; |
| 4748 | UserDeclaredMove = RD->getMoveAssignmentOperator(); |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4749 | assert(UserDeclaredMove); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4750 | } |
| 4751 | |
| 4752 | if (UserDeclaredMove) { |
| 4753 | Diag(UserDeclaredMove->getLocation(), |
| 4754 | diag::note_deleted_copy_user_declared_move) |
Richard Smith | e6af660 | 2012-04-02 21:07:48 +0000 | [diff] [blame] | 4755 | << (CSM == CXXCopyAssignment) << RD |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4756 | << UserDeclaredMove->isMoveAssignmentOperator(); |
| 4757 | return true; |
| 4758 | } |
| 4759 | } |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 4760 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 4761 | // Do access control from the special member function |
| 4762 | ContextRAII MethodContext(*this, MD); |
| 4763 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4764 | // C++11 [class.dtor]p5: |
| 4765 | // -- for a virtual destructor, lookup of the non-array deallocation function |
| 4766 | // 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] | 4767 | if (CSM == CXXDestructor && MD->isVirtual()) { |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4768 | FunctionDecl *OperatorDelete = 0; |
| 4769 | DeclarationName Name = |
| 4770 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| 4771 | if (FindDeallocationFunction(MD->getLocation(), MD->getParent(), Name, |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4772 | OperatorDelete, false)) { |
| 4773 | if (Diagnose) |
| 4774 | Diag(RD->getLocation(), diag::note_deleted_dtor_no_operator_delete); |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4775 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4776 | } |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4779 | SpecialMemberDeletionInfo SMI(*this, MD, CSM, Diagnose); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4780 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4781 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4782 | BE = RD->bases_end(); BI != BE; ++BI) |
| 4783 | if (!BI->isVirtual() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4784 | SMI.shouldDeleteForBase(BI)) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4785 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4786 | |
| 4787 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4788 | BE = RD->vbases_end(); BI != BE; ++BI) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4789 | if (SMI.shouldDeleteForBase(BI)) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4790 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4791 | |
| 4792 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4793 | FE = RD->field_end(); FI != FE; ++FI) |
| 4794 | if (!FI->isInvalidDecl() && !FI->isUnnamedBitfield() && |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4795 | SMI.shouldDeleteForField(*FI)) |
Sean Hunt | e340682 | 2011-05-20 21:43:47 +0000 | [diff] [blame] | 4796 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4797 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4798 | if (SMI.shouldDeleteForAllConstMembers()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4799 | return true; |
| 4800 | |
| 4801 | return false; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4802 | } |
| 4803 | |
| 4804 | /// \brief Data used with FindHiddenVirtualMethod |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 4805 | namespace { |
| 4806 | struct FindHiddenVirtualMethodData { |
| 4807 | Sema *S; |
| 4808 | CXXMethodDecl *Method; |
| 4809 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4810 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 4811 | }; |
| 4812 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4813 | |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 4814 | /// \brief Check whether any most overriden method from MD in Methods |
| 4815 | static bool CheckMostOverridenMethods(const CXXMethodDecl *MD, |
| 4816 | const llvm::SmallPtrSet<const CXXMethodDecl *, 8>& Methods) { |
| 4817 | if (MD->size_overridden_methods() == 0) |
| 4818 | return Methods.count(MD->getCanonicalDecl()); |
| 4819 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 4820 | E = MD->end_overridden_methods(); |
| 4821 | I != E; ++I) |
| 4822 | if (CheckMostOverridenMethods(*I, Methods)) |
| 4823 | return true; |
| 4824 | return false; |
| 4825 | } |
| 4826 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4827 | /// \brief Member lookup function that determines whether a given C++ |
| 4828 | /// method overloads virtual methods in a base class without overriding any, |
| 4829 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 4830 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 4831 | CXXBasePath &Path, |
| 4832 | void *UserData) { |
| 4833 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 4834 | |
| 4835 | FindHiddenVirtualMethodData &Data |
| 4836 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 4837 | |
| 4838 | DeclarationName Name = Data.Method->getDeclName(); |
| 4839 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 4840 | |
| 4841 | bool foundSameNameMethod = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4842 | SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4843 | for (Path.Decls = BaseRecord->lookup(Name); |
| 4844 | Path.Decls.first != Path.Decls.second; |
| 4845 | ++Path.Decls.first) { |
| 4846 | NamedDecl *D = *Path.Decls.first; |
| 4847 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 4848 | MD = MD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4849 | foundSameNameMethod = true; |
| 4850 | // Interested only in hidden virtual methods. |
| 4851 | if (!MD->isVirtual()) |
| 4852 | continue; |
| 4853 | // If the method we are checking overrides a method from its base |
| 4854 | // don't warn about the other overloaded methods. |
| 4855 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 4856 | return true; |
| 4857 | // Collect the overload only if its hidden. |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 4858 | if (!CheckMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods)) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4859 | overloadedMethods.push_back(MD); |
| 4860 | } |
| 4861 | } |
| 4862 | |
| 4863 | if (foundSameNameMethod) |
| 4864 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 4865 | overloadedMethods.end()); |
| 4866 | return foundSameNameMethod; |
| 4867 | } |
| 4868 | |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 4869 | /// \brief Add the most overriden methods from MD to Methods |
| 4870 | static void AddMostOverridenMethods(const CXXMethodDecl *MD, |
| 4871 | llvm::SmallPtrSet<const CXXMethodDecl *, 8>& Methods) { |
| 4872 | if (MD->size_overridden_methods() == 0) |
| 4873 | Methods.insert(MD->getCanonicalDecl()); |
| 4874 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 4875 | E = MD->end_overridden_methods(); |
| 4876 | I != E; ++I) |
| 4877 | AddMostOverridenMethods(*I, Methods); |
| 4878 | } |
| 4879 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4880 | /// \brief See if a method overloads virtual methods in a base class without |
| 4881 | /// overriding any. |
| 4882 | void Sema::DiagnoseHiddenVirtualMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
| 4883 | if (Diags.getDiagnosticLevel(diag::warn_overloaded_virtual, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 4884 | MD->getLocation()) == DiagnosticsEngine::Ignored) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4885 | return; |
Benjamin Kramer | c470442 | 2012-05-19 16:03:58 +0000 | [diff] [blame] | 4886 | if (!MD->getDeclName().isIdentifier()) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4887 | return; |
| 4888 | |
| 4889 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 4890 | /*bool RecordPaths=*/false, |
| 4891 | /*bool DetectVirtual=*/false); |
| 4892 | FindHiddenVirtualMethodData Data; |
| 4893 | Data.Method = MD; |
| 4894 | Data.S = this; |
| 4895 | |
| 4896 | // Keep the base methods that were overriden or introduced in the subclass |
| 4897 | // by 'using' in a set. A base method not in this set is hidden. |
| 4898 | for (DeclContext::lookup_result res = DC->lookup(MD->getDeclName()); |
| 4899 | res.first != res.second; ++res.first) { |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 4900 | NamedDecl *ND = *res.first; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4901 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*res.first)) |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 4902 | ND = shad->getTargetDecl(); |
| 4903 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 4904 | AddMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4905 | } |
| 4906 | |
| 4907 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths) && |
| 4908 | !Data.OverloadedMethods.empty()) { |
| 4909 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 4910 | << MD << (Data.OverloadedMethods.size() > 1); |
| 4911 | |
| 4912 | for (unsigned i = 0, e = Data.OverloadedMethods.size(); i != e; ++i) { |
| 4913 | CXXMethodDecl *overloadedMD = Data.OverloadedMethods[i]; |
| 4914 | Diag(overloadedMD->getLocation(), |
| 4915 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
| 4916 | } |
| 4917 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4918 | } |
| 4919 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4920 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4921 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4922 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 4923 | SourceLocation RBrac, |
| 4924 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4925 | if (!TagDecl) |
| 4926 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4927 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4928 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4929 | |
Rafael Espindola | f729ce0 | 2012-07-12 04:32:30 +0000 | [diff] [blame] | 4930 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
| 4931 | if (l->getKind() != AttributeList::AT_Visibility) |
| 4932 | continue; |
| 4933 | l->setInvalid(); |
| 4934 | Diag(l->getLoc(), diag::warn_attribute_after_definition_ignored) << |
| 4935 | l->getName(); |
| 4936 | } |
| 4937 | |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 4938 | ActOnFields(S, RLoc, TagDecl, llvm::makeArrayRef( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4939 | // strict aliasing violation! |
| 4940 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 4941 | FieldCollector->getCurNumFields()), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4942 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4943 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4944 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4945 | } |
| 4946 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4947 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 4948 | /// special functions, such as the default constructor, copy |
| 4949 | /// constructor, or destructor, to the given C++ class (C++ |
| 4950 | /// [special]p1). This routine can only be executed just before the |
| 4951 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4952 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4953 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4954 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4955 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 4956 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 4957 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4958 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4959 | if (getLangOpts().CPlusPlus0x && ClassDecl->needsImplicitMoveConstructor()) |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 4960 | ++ASTContext::NumImplicitMoveConstructors; |
| 4961 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4962 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 4963 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
| 4964 | |
| 4965 | // If we have a dynamic class, then the copy assignment operator may be |
| 4966 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
| 4967 | // it shows up in the right place in the vtable and that we diagnose |
| 4968 | // problems with the implicit exception specification. |
| 4969 | if (ClassDecl->isDynamicClass()) |
| 4970 | DeclareImplicitCopyAssignment(ClassDecl); |
| 4971 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4972 | |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4973 | if (getLangOpts().CPlusPlus0x && ClassDecl->needsImplicitMoveAssignment()) { |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 4974 | ++ASTContext::NumImplicitMoveAssignmentOperators; |
| 4975 | |
| 4976 | // Likewise for the move assignment operator. |
| 4977 | if (ClassDecl->isDynamicClass()) |
| 4978 | DeclareImplicitMoveAssignment(ClassDecl); |
| 4979 | } |
| 4980 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4981 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 4982 | ++ASTContext::NumImplicitDestructors; |
| 4983 | |
| 4984 | // If we have a dynamic class, then the destructor may be virtual, so we |
| 4985 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 4986 | // shows up in the right place in the vtable and that we diagnose problems |
| 4987 | // with the implicit exception specification. |
| 4988 | if (ClassDecl->isDynamicClass()) |
| 4989 | DeclareImplicitDestructor(ClassDecl); |
| 4990 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4991 | } |
| 4992 | |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4993 | void Sema::ActOnReenterDeclaratorTemplateScope(Scope *S, DeclaratorDecl *D) { |
| 4994 | if (!D) |
| 4995 | return; |
| 4996 | |
| 4997 | int NumParamList = D->getNumTemplateParameterLists(); |
| 4998 | for (int i = 0; i < NumParamList; i++) { |
| 4999 | TemplateParameterList* Params = D->getTemplateParameterList(i); |
| 5000 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5001 | ParamEnd = Params->end(); |
| 5002 | Param != ParamEnd; ++Param) { |
| 5003 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 5004 | if (Named->getDeclName()) { |
| 5005 | S->AddDecl(Named); |
| 5006 | IdResolver.AddDecl(Named); |
| 5007 | } |
| 5008 | } |
| 5009 | } |
| 5010 | } |
| 5011 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5012 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 5013 | if (!D) |
| 5014 | return; |
| 5015 | |
| 5016 | TemplateParameterList *Params = 0; |
| 5017 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 5018 | Params = Template->getTemplateParameters(); |
| 5019 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 5020 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 5021 | Params = PartialSpec->getTemplateParameters(); |
| 5022 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 5023 | return; |
| 5024 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 5025 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5026 | ParamEnd = Params->end(); |
| 5027 | Param != ParamEnd; ++Param) { |
| 5028 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 5029 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5030 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 5031 | IdResolver.AddDecl(Named); |
| 5032 | } |
| 5033 | } |
| 5034 | } |
| 5035 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5036 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5037 | if (!RecordD) return; |
| 5038 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5039 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5040 | PushDeclContext(S, Record); |
| 5041 | } |
| 5042 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5043 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5044 | if (!RecordD) return; |
| 5045 | PopDeclContext(); |
| 5046 | } |
| 5047 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5048 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 5049 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 5050 | /// parsing those parts of the given Method declaration that could |
| 5051 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 5052 | /// arguments. This action should enter the scope of the given |
| 5053 | /// Method declaration as if we had just parsed the qualified method |
| 5054 | /// name. However, it should not bring the parameters into scope; |
| 5055 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5056 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 5060 | /// C++ method declaration. We're (re-)introducing the given |
| 5061 | /// function parameter into scope for use in parsing later parts of |
| 5062 | /// the method declaration. For example, we could see an |
| 5063 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5064 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5065 | if (!ParamD) |
| 5066 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5067 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5068 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5069 | |
| 5070 | // If this parameter has an unparsed default argument, clear it out |
| 5071 | // to make way for the parsed default argument. |
| 5072 | if (Param->hasUnparsedDefaultArg()) |
| 5073 | Param->setDefaultArg(0); |
| 5074 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5075 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5076 | if (Param->getDeclName()) |
| 5077 | IdResolver.AddDecl(Param); |
| 5078 | } |
| 5079 | |
| 5080 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 5081 | /// processing the delayed method declaration for Method. The method |
| 5082 | /// declaration is now considered finished. There may be a separate |
| 5083 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 5084 | /// immediately!) for this method, if it was also defined inside the |
| 5085 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5086 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5087 | if (!MethodD) |
| 5088 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5089 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 5090 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5091 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5092 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5093 | |
| 5094 | // Now that we have our default arguments, check the constructor |
| 5095 | // again. It could produce additional diagnostics or affect whether |
| 5096 | // the class has implicitly-declared destructors, among other |
| 5097 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5098 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 5099 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5100 | |
| 5101 | // Check the default arguments, which we may have added. |
| 5102 | if (!Method->isInvalidDecl()) |
| 5103 | CheckCXXDefaultArguments(Method); |
| 5104 | } |
| 5105 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5106 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5107 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5108 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5109 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 5110 | /// will be updated to reflect a well-formed type for the constructor and |
| 5111 | /// returned. |
| 5112 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5113 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5114 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5115 | |
| 5116 | // C++ [class.ctor]p3: |
| 5117 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 5118 | // constructor can be invoked for a const, volatile or const |
| 5119 | // volatile object. A constructor shall not be declared const, |
| 5120 | // volatile, or const volatile (9.3.2). |
| 5121 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5122 | if (!D.isInvalidType()) |
| 5123 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 5124 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 5125 | << SourceRange(D.getIdentifierLoc()); |
| 5126 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5127 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5128 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5129 | if (!D.isInvalidType()) |
| 5130 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 5131 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 5132 | << SourceRange(D.getIdentifierLoc()); |
| 5133 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5134 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5135 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5136 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5137 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5138 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5139 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5140 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 5141 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5142 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5143 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 5144 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5145 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5146 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 5147 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5148 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5149 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5150 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5151 | // C++0x [class.ctor]p4: |
| 5152 | // A constructor shall not be declared with a ref-qualifier. |
| 5153 | if (FTI.hasRefQualifier()) { |
| 5154 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 5155 | << FTI.RefQualifierIsLValueRef |
| 5156 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 5157 | D.setInvalidType(); |
| 5158 | } |
| 5159 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5160 | // Rebuild the function type "R" without any type qualifiers (in |
| 5161 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5162 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5163 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5164 | if (Proto->getResultType() == Context.VoidTy && !D.isInvalidType()) |
| 5165 | return R; |
| 5166 | |
| 5167 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 5168 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5169 | EPI.RefQualifier = RQ_None; |
| 5170 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5171 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5172 | Proto->getNumArgs(), EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5173 | } |
| 5174 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5175 | /// CheckConstructor - Checks a fully-formed constructor for |
| 5176 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 5177 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5178 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5179 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 5180 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 5181 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5182 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5183 | |
| 5184 | // C++ [class.copy]p3: |
| 5185 | // A declaration of a constructor for a class X is ill-formed if |
| 5186 | // its first parameter is of type (optionally cv-qualified) X and |
| 5187 | // either there are no other parameters or else all other |
| 5188 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 5189 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5190 | ((Constructor->getNumParams() == 1) || |
| 5191 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5192 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 5193 | Constructor->getTemplateSpecializationKind() |
| 5194 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5195 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 5196 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 5197 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5198 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 5199 | const char *ConstRef |
| 5200 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 5201 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5202 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 5203 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5204 | |
| 5205 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 5206 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5207 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5208 | } |
| 5209 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5210 | } |
| 5211 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 5212 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 5213 | /// well-formedness, issuing any diagnostics required. Returns true |
| 5214 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5215 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5216 | CXXRecordDecl *RD = Destructor->getParent(); |
| 5217 | |
| 5218 | if (Destructor->isVirtual()) { |
| 5219 | SourceLocation Loc; |
| 5220 | |
| 5221 | if (!Destructor->isImplicit()) |
| 5222 | Loc = Destructor->getLocation(); |
| 5223 | else |
| 5224 | Loc = RD->getLocation(); |
| 5225 | |
| 5226 | // If we have a virtual destructor, look up the deallocation function |
| 5227 | FunctionDecl *OperatorDelete = 0; |
| 5228 | DeclarationName Name = |
| 5229 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5230 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5231 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 5232 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5233 | MarkFunctionReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5234 | |
| 5235 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5236 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5237 | |
| 5238 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5241 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5242 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 5243 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 5244 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5245 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5246 | } |
| 5247 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5248 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 5249 | /// the well-formednes of the destructor declarator @p D with type @p |
| 5250 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5251 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 5252 | /// will be updated to reflect a well-formed type for the destructor and |
| 5253 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5254 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5255 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5256 | // C++ [class.dtor]p1: |
| 5257 | // [...] A typedef-name that names a class is a class-name |
| 5258 | // (7.1.3); however, a typedef-name that names a class shall not |
| 5259 | // be used as the identifier in the declarator for a destructor |
| 5260 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5261 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5262 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5263 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5264 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5265 | else if (const TemplateSpecializationType *TST = |
| 5266 | DeclaratorType->getAs<TemplateSpecializationType>()) |
| 5267 | if (TST->isTypeAlias()) |
| 5268 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
| 5269 | << DeclaratorType << 1; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5270 | |
| 5271 | // C++ [class.dtor]p2: |
| 5272 | // A destructor is used to destroy objects of its class type. A |
| 5273 | // destructor takes no parameters, and no return type can be |
| 5274 | // specified for it (not even void). The address of a destructor |
| 5275 | // shall not be taken. A destructor shall not be static. A |
| 5276 | // destructor can be invoked for a const, volatile or const |
| 5277 | // volatile object. A destructor shall not be declared const, |
| 5278 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5279 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5280 | if (!D.isInvalidType()) |
| 5281 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 5282 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5283 | << SourceRange(D.getIdentifierLoc()) |
| 5284 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 5285 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5286 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5287 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5288 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5289 | // Destructors don't have return types, but the parser will |
| 5290 | // happily parse something like: |
| 5291 | // |
| 5292 | // class X { |
| 5293 | // float ~X(); |
| 5294 | // }; |
| 5295 | // |
| 5296 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5297 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 5298 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 5299 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5300 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5301 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5302 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5303 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5304 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5305 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 5306 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5307 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5308 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 5309 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5310 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5311 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 5312 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5313 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5314 | } |
| 5315 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5316 | // C++0x [class.dtor]p2: |
| 5317 | // A destructor shall not be declared with a ref-qualifier. |
| 5318 | if (FTI.hasRefQualifier()) { |
| 5319 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 5320 | << FTI.RefQualifierIsLValueRef |
| 5321 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 5322 | D.setInvalidType(); |
| 5323 | } |
| 5324 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5325 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5326 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5327 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 5328 | |
| 5329 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5330 | FTI.freeArgs(); |
| 5331 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5332 | } |
| 5333 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5334 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5335 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5336 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5337 | D.setInvalidType(); |
| 5338 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5339 | |
| 5340 | // Rebuild the function type "R" without any type qualifiers or |
| 5341 | // parameters (in case any of the errors above fired) and with |
| 5342 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5343 | // types. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5344 | if (!D.isInvalidType()) |
| 5345 | return R; |
| 5346 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5347 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5348 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 5349 | EPI.Variadic = false; |
| 5350 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5351 | EPI.RefQualifier = RQ_None; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5352 | return Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5355 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 5356 | /// well-formednes of the conversion function declarator @p D with |
| 5357 | /// type @p R. If there are any errors in the declarator, this routine |
| 5358 | /// will emit diagnostics and return true. Otherwise, it will return |
| 5359 | /// false. Either way, the type @p R will be updated to reflect a |
| 5360 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5361 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5362 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5363 | // C++ [class.conv.fct]p1: |
| 5364 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 5365 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5366 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5367 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5368 | if (!D.isInvalidType()) |
| 5369 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 5370 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 5371 | << SourceRange(D.getIdentifierLoc()); |
| 5372 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5373 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5374 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5375 | |
| 5376 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 5377 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5378 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5379 | // Conversion functions don't have return types, but the parser will |
| 5380 | // happily parse something like: |
| 5381 | // |
| 5382 | // class X { |
| 5383 | // float operator bool(); |
| 5384 | // }; |
| 5385 | // |
| 5386 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5387 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 5388 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 5389 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5390 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5391 | } |
| 5392 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5393 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 5394 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5395 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5396 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5397 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 5398 | |
| 5399 | // Delete the parameters. |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5400 | D.getFunctionTypeInfo().freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5401 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5402 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5403 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5404 | D.setInvalidType(); |
| 5405 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5406 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 5407 | // Diagnose "&operator bool()" and other such nonsense. This |
| 5408 | // is actually a gcc extension which we don't support. |
| 5409 | if (Proto->getResultType() != ConvType) { |
| 5410 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 5411 | << Proto->getResultType(); |
| 5412 | D.setInvalidType(); |
| 5413 | ConvType = Proto->getResultType(); |
| 5414 | } |
| 5415 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5416 | // C++ [class.conv.fct]p4: |
| 5417 | // The conversion-type-id shall not represent a function type nor |
| 5418 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5419 | if (ConvType->isArrayType()) { |
| 5420 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 5421 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5422 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5423 | } else if (ConvType->isFunctionType()) { |
| 5424 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 5425 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5426 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | // Rebuild the function type "R" without any parameters (in case any |
| 5430 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5431 | // return type. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5432 | if (D.isInvalidType()) |
| 5433 | R = Context.getFunctionType(ConvType, 0, 0, Proto->getExtProtoInfo()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5434 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5435 | // C++0x explicit conversion operators. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5436 | if (D.getDeclSpec().isExplicitSpecified()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5437 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5438 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5439 | diag::warn_cxx98_compat_explicit_conversion_functions : |
| 5440 | diag::ext_explicit_conversion_functions) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 5441 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5442 | } |
| 5443 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5444 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 5445 | /// the declaration of the given C++ conversion function. This routine |
| 5446 | /// is responsible for recording the conversion function in the C++ |
| 5447 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5448 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5449 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 5450 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 5451 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5452 | |
| 5453 | // Make sure we aren't redeclaring the conversion function. |
| 5454 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5455 | |
| 5456 | // C++ [class.conv.fct]p1: |
| 5457 | // [...] A conversion function is never used to convert a |
| 5458 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 5459 | // same object type (or a reference to it), to a (possibly |
| 5460 | // cv-qualified) base class of that type (or a reference to it), |
| 5461 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5462 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 5463 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5464 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5465 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5466 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5467 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 5468 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 5469 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 5470 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 5471 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5472 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 5473 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 5474 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5475 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5476 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 5477 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5478 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5479 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 5480 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5481 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 5484 | if (FunctionTemplateDecl *ConversionTemplate |
| 5485 | = Conversion->getDescribedFunctionTemplate()) |
| 5486 | return ConversionTemplate; |
| 5487 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5488 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 5489 | } |
| 5490 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5491 | //===----------------------------------------------------------------------===// |
| 5492 | // Namespace Handling |
| 5493 | //===----------------------------------------------------------------------===// |
| 5494 | |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 5495 | /// \brief Diagnose a mismatch in 'inline' qualifiers when a namespace is |
| 5496 | /// reopened. |
| 5497 | static void DiagnoseNamespaceInlineMismatch(Sema &S, SourceLocation KeywordLoc, |
| 5498 | SourceLocation Loc, |
| 5499 | IdentifierInfo *II, bool *IsInline, |
| 5500 | NamespaceDecl *PrevNS) { |
| 5501 | assert(*IsInline != PrevNS->isInline()); |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 5502 | |
Richard Smith | c969e6a | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 5503 | // HACK: Work around a bug in libstdc++4.6's <atomic>, where |
| 5504 | // std::__atomic[0,1,2] are defined as non-inline namespaces, then reopened as |
| 5505 | // inline namespaces, with the intention of bringing names into namespace std. |
| 5506 | // |
| 5507 | // We support this just well enough to get that case working; this is not |
| 5508 | // sufficient to support reopening namespaces as inline in general. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 5509 | if (*IsInline && II && II->getName().startswith("__atomic") && |
| 5510 | S.getSourceManager().isInSystemHeader(Loc)) { |
Richard Smith | c969e6a | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 5511 | // Mark all prior declarations of the namespace as inline. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 5512 | for (NamespaceDecl *NS = PrevNS->getMostRecentDecl(); NS; |
| 5513 | NS = NS->getPreviousDecl()) |
| 5514 | NS->setInline(*IsInline); |
| 5515 | // Patch up the lookup table for the containing namespace. This isn't really |
| 5516 | // correct, but it's good enough for this particular case. |
| 5517 | for (DeclContext::decl_iterator I = PrevNS->decls_begin(), |
| 5518 | E = PrevNS->decls_end(); I != E; ++I) |
| 5519 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*I)) |
| 5520 | PrevNS->getParent()->makeDeclVisibleInContext(ND); |
| 5521 | return; |
| 5522 | } |
| 5523 | |
| 5524 | if (PrevNS->isInline()) |
| 5525 | // The user probably just forgot the 'inline', so suggest that it |
| 5526 | // be added back. |
| 5527 | S.Diag(Loc, diag::warn_inline_namespace_reopened_noninline) |
| 5528 | << FixItHint::CreateInsertion(KeywordLoc, "inline "); |
| 5529 | else |
| 5530 | S.Diag(Loc, diag::err_inline_namespace_mismatch) |
| 5531 | << IsInline; |
| 5532 | |
| 5533 | S.Diag(PrevNS->getLocation(), diag::note_previous_definition); |
| 5534 | *IsInline = PrevNS->isInline(); |
| 5535 | } |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 5536 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5537 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 5538 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5539 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 5540 | SourceLocation InlineLoc, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5541 | SourceLocation NamespaceLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 5542 | SourceLocation IdentLoc, |
| 5543 | IdentifierInfo *II, |
| 5544 | SourceLocation LBrace, |
| 5545 | AttributeList *AttrList) { |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5546 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 5547 | // For anonymous namespace, take the location of the left brace. |
| 5548 | SourceLocation Loc = II ? IdentLoc : LBrace; |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5549 | bool IsInline = InlineLoc.isValid(); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 5550 | bool IsInvalid = false; |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5551 | bool IsStd = false; |
| 5552 | bool AddToKnown = false; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5553 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 5554 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5555 | NamespaceDecl *PrevNS = 0; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5556 | if (II) { |
| 5557 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 5558 | // The identifier in an original-namespace-definition shall not |
| 5559 | // have been previously defined in the declarative region in |
| 5560 | // which the original-namespace-definition appears. The |
| 5561 | // identifier in an original-namespace-definition is the name of |
| 5562 | // the namespace. Subsequently in that declarative region, it is |
| 5563 | // treated as an original-namespace-name. |
| 5564 | // |
| 5565 | // Since namespace names are unique in their scope, and we don't |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 5566 | // look through using directives, just look for any ordinary names. |
| 5567 | |
| 5568 | const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Member | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5569 | Decl::IDNS_Type | Decl::IDNS_Using | Decl::IDNS_Tag | |
| 5570 | Decl::IDNS_Namespace; |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 5571 | NamedDecl *PrevDecl = 0; |
| 5572 | for (DeclContext::lookup_result R |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5573 | = CurContext->getRedeclContext()->lookup(II); |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 5574 | R.first != R.second; ++R.first) { |
| 5575 | if ((*R.first)->getIdentifierNamespace() & IDNS) { |
| 5576 | PrevDecl = *R.first; |
| 5577 | break; |
| 5578 | } |
| 5579 | } |
| 5580 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5581 | PrevNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl); |
| 5582 | |
| 5583 | if (PrevNS) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5584 | // This is an extended namespace definition. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 5585 | if (IsInline != PrevNS->isInline()) |
| 5586 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, Loc, II, |
| 5587 | &IsInline, PrevNS); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5588 | } else if (PrevDecl) { |
| 5589 | // This is an invalid name redefinition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5590 | Diag(Loc, diag::err_redefinition_different_kind) |
| 5591 | << II; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5592 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 5593 | IsInvalid = true; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5594 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5595 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5596 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 5597 | // This is the first "real" definition of the namespace "std", so update |
| 5598 | // our cache of the "std" namespace to point at this definition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5599 | PrevNS = getStdNamespace(); |
| 5600 | IsStd = true; |
| 5601 | AddToKnown = !IsInline; |
| 5602 | } else { |
| 5603 | // We've seen this namespace for the first time. |
| 5604 | AddToKnown = !IsInline; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5605 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5606 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 5607 | // Anonymous namespaces. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5608 | |
| 5609 | // Determine whether the parent already has an anonymous namespace. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5610 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5611 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5612 | PrevNS = TU->getAnonymousNamespace(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5613 | } else { |
| 5614 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5615 | PrevNS = ND->getAnonymousNamespace(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5616 | } |
| 5617 | |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 5618 | if (PrevNS && IsInline != PrevNS->isInline()) |
| 5619 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, NamespaceLoc, II, |
| 5620 | &IsInline, PrevNS); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5621 | } |
| 5622 | |
| 5623 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, IsInline, |
| 5624 | StartLoc, Loc, II, PrevNS); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 5625 | if (IsInvalid) |
| 5626 | Namespc->setInvalidDecl(); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5627 | |
| 5628 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 5629 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5630 | // FIXME: Should we be merging attributes? |
| 5631 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 5632 | PushNamespaceVisibilityAttr(Attr, Loc); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5633 | |
| 5634 | if (IsStd) |
| 5635 | StdNamespace = Namespc; |
| 5636 | if (AddToKnown) |
| 5637 | KnownNamespaces[Namespc] = false; |
| 5638 | |
| 5639 | if (II) { |
| 5640 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 5641 | } else { |
| 5642 | // Link the anonymous namespace into its parent. |
| 5643 | DeclContext *Parent = CurContext->getRedeclContext(); |
| 5644 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 5645 | TU->setAnonymousNamespace(Namespc); |
| 5646 | } else { |
| 5647 | cast<NamespaceDecl>(Parent)->setAnonymousNamespace(Namespc); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5648 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 5649 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 5650 | CurContext->addDecl(Namespc); |
| 5651 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 5652 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 5653 | // behaves as if it were replaced by |
| 5654 | // namespace unique { /* empty body */ } |
| 5655 | // using namespace unique; |
| 5656 | // namespace unique { namespace-body } |
| 5657 | // where all occurrences of 'unique' in a translation unit are |
| 5658 | // replaced by the same identifier and this identifier differs |
| 5659 | // from all other identifiers in the entire program. |
| 5660 | |
| 5661 | // We just create the namespace with an empty name and then add an |
| 5662 | // implicit using declaration, just like the standard suggests. |
| 5663 | // |
| 5664 | // CodeGen enforces the "universally unique" aspect by giving all |
| 5665 | // declarations semantically contained within an anonymous |
| 5666 | // namespace internal linkage. |
| 5667 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5668 | if (!PrevNS) { |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5669 | UsingDirectiveDecl* UD |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 5670 | = UsingDirectiveDecl::Create(Context, Parent, |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5671 | /* 'using' */ LBrace, |
| 5672 | /* 'namespace' */ SourceLocation(), |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 5673 | /* qualifier */ NestedNameSpecifierLoc(), |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5674 | /* identifier */ SourceLocation(), |
| 5675 | Namespc, |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 5676 | /* Ancestor */ Parent); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5677 | UD->setImplicit(); |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 5678 | Parent->addDecl(UD); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 5679 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5680 | } |
| 5681 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 5682 | ActOnDocumentableDecl(Namespc); |
| 5683 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5684 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 5685 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5686 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 5687 | // for the namespace has the declarations that showed up in that particular |
| 5688 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5689 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5690 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5691 | } |
| 5692 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5693 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 5694 | /// is a namespace alias, returns the namespace it points to. |
| 5695 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 5696 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 5697 | return AD->getNamespace(); |
| 5698 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 5699 | } |
| 5700 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5701 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 5702 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5703 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5704 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 5705 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5706 | Namespc->setRBraceLoc(RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5707 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 5708 | if (Namespc->hasAttr<VisibilityAttr>()) |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 5709 | PopPragmaVisibility(true, RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 5710 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5711 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 5712 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 5713 | return cast_or_null<CXXRecordDecl>( |
| 5714 | StdBadAlloc.get(Context.getExternalSource())); |
| 5715 | } |
| 5716 | |
| 5717 | NamespaceDecl *Sema::getStdNamespace() const { |
| 5718 | return cast_or_null<NamespaceDecl>( |
| 5719 | StdNamespace.get(Context.getExternalSource())); |
| 5720 | } |
| 5721 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5722 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 5723 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 5724 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5725 | if (!StdNamespace) { |
| 5726 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 5727 | StdNamespace = NamespaceDecl::Create(Context, |
| 5728 | Context.getTranslationUnitDecl(), |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5729 | /*Inline=*/false, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 5730 | SourceLocation(), SourceLocation(), |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 5731 | &PP.getIdentifierTable().get("std"), |
| 5732 | /*PrevDecl=*/0); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 5733 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5734 | } |
| 5735 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 5736 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5737 | } |
| 5738 | |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5739 | bool Sema::isStdInitializerList(QualType Ty, QualType *Element) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5740 | assert(getLangOpts().CPlusPlus && |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5741 | "Looking for std::initializer_list outside of C++."); |
| 5742 | |
| 5743 | // We're looking for implicit instantiations of |
| 5744 | // template <typename E> class std::initializer_list. |
| 5745 | |
| 5746 | if (!StdNamespace) // If we haven't seen namespace std yet, this can't be it. |
| 5747 | return false; |
| 5748 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 5749 | ClassTemplateDecl *Template = 0; |
| 5750 | const TemplateArgument *Arguments = 0; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5751 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 5752 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5753 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 5754 | ClassTemplateSpecializationDecl *Specialization = |
| 5755 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 5756 | if (!Specialization) |
| 5757 | return false; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5758 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 5759 | Template = Specialization->getSpecializedTemplate(); |
| 5760 | Arguments = Specialization->getTemplateArgs().data(); |
| 5761 | } else if (const TemplateSpecializationType *TST = |
| 5762 | Ty->getAs<TemplateSpecializationType>()) { |
| 5763 | Template = dyn_cast_or_null<ClassTemplateDecl>( |
| 5764 | TST->getTemplateName().getAsTemplateDecl()); |
| 5765 | Arguments = TST->getArgs(); |
| 5766 | } |
| 5767 | if (!Template) |
| 5768 | return false; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5769 | |
| 5770 | if (!StdInitializerList) { |
| 5771 | // Haven't recognized std::initializer_list yet, maybe this is it. |
| 5772 | CXXRecordDecl *TemplateClass = Template->getTemplatedDecl(); |
| 5773 | if (TemplateClass->getIdentifier() != |
| 5774 | &PP.getIdentifierTable().get("initializer_list") || |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 5775 | !getStdNamespace()->InEnclosingNamespaceSetOf( |
| 5776 | TemplateClass->getDeclContext())) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5777 | return false; |
| 5778 | // This is a template called std::initializer_list, but is it the right |
| 5779 | // template? |
| 5780 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 5781 | if (Params->getMinRequiredArguments() != 1) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5782 | return false; |
| 5783 | if (!isa<TemplateTypeParmDecl>(Params->getParam(0))) |
| 5784 | return false; |
| 5785 | |
| 5786 | // It's the right template. |
| 5787 | StdInitializerList = Template; |
| 5788 | } |
| 5789 | |
| 5790 | if (Template != StdInitializerList) |
| 5791 | return false; |
| 5792 | |
| 5793 | // This is an instance of std::initializer_list. Find the argument type. |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 5794 | if (Element) |
| 5795 | *Element = Arguments[0].getAsType(); |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 5796 | return true; |
| 5797 | } |
| 5798 | |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 5799 | static ClassTemplateDecl *LookupStdInitializerList(Sema &S, SourceLocation Loc){ |
| 5800 | NamespaceDecl *Std = S.getStdNamespace(); |
| 5801 | if (!Std) { |
| 5802 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 5803 | return 0; |
| 5804 | } |
| 5805 | |
| 5806 | LookupResult Result(S, &S.PP.getIdentifierTable().get("initializer_list"), |
| 5807 | Loc, Sema::LookupOrdinaryName); |
| 5808 | if (!S.LookupQualifiedName(Result, Std)) { |
| 5809 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 5810 | return 0; |
| 5811 | } |
| 5812 | ClassTemplateDecl *Template = Result.getAsSingle<ClassTemplateDecl>(); |
| 5813 | if (!Template) { |
| 5814 | Result.suppressDiagnostics(); |
| 5815 | // We found something weird. Complain about the first thing we found. |
| 5816 | NamedDecl *Found = *Result.begin(); |
| 5817 | S.Diag(Found->getLocation(), diag::err_malformed_std_initializer_list); |
| 5818 | return 0; |
| 5819 | } |
| 5820 | |
| 5821 | // We found some template called std::initializer_list. Now verify that it's |
| 5822 | // correct. |
| 5823 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 5824 | if (Params->getMinRequiredArguments() != 1 || |
| 5825 | !isa<TemplateTypeParmDecl>(Params->getParam(0))) { |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 5826 | S.Diag(Template->getLocation(), diag::err_malformed_std_initializer_list); |
| 5827 | return 0; |
| 5828 | } |
| 5829 | |
| 5830 | return Template; |
| 5831 | } |
| 5832 | |
| 5833 | QualType Sema::BuildStdInitializerList(QualType Element, SourceLocation Loc) { |
| 5834 | if (!StdInitializerList) { |
| 5835 | StdInitializerList = LookupStdInitializerList(*this, Loc); |
| 5836 | if (!StdInitializerList) |
| 5837 | return QualType(); |
| 5838 | } |
| 5839 | |
| 5840 | TemplateArgumentListInfo Args(Loc, Loc); |
| 5841 | Args.addArgument(TemplateArgumentLoc(TemplateArgument(Element), |
| 5842 | Context.getTrivialTypeSourceInfo(Element, |
| 5843 | Loc))); |
| 5844 | return Context.getCanonicalType( |
| 5845 | CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args)); |
| 5846 | } |
| 5847 | |
Sebastian Redl | 98d3606 | 2012-01-17 22:50:14 +0000 | [diff] [blame] | 5848 | bool Sema::isInitListConstructor(const CXXConstructorDecl* Ctor) { |
| 5849 | // C++ [dcl.init.list]p2: |
| 5850 | // A constructor is an initializer-list constructor if its first parameter |
| 5851 | // is of type std::initializer_list<E> or reference to possibly cv-qualified |
| 5852 | // std::initializer_list<E> for some type E, and either there are no other |
| 5853 | // parameters or else all other parameters have default arguments. |
| 5854 | if (Ctor->getNumParams() < 1 || |
| 5855 | (Ctor->getNumParams() > 1 && !Ctor->getParamDecl(1)->hasDefaultArg())) |
| 5856 | return false; |
| 5857 | |
| 5858 | QualType ArgType = Ctor->getParamDecl(0)->getType(); |
| 5859 | if (const ReferenceType *RT = ArgType->getAs<ReferenceType>()) |
| 5860 | ArgType = RT->getPointeeType().getUnqualifiedType(); |
| 5861 | |
| 5862 | return isStdInitializerList(ArgType, 0); |
| 5863 | } |
| 5864 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 5865 | /// \brief Determine whether a using statement is in a context where it will be |
| 5866 | /// apply in all contexts. |
| 5867 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 5868 | switch (CurContext->getDeclKind()) { |
| 5869 | case Decl::TranslationUnit: |
| 5870 | return true; |
| 5871 | case Decl::LinkageSpec: |
| 5872 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 5873 | default: |
| 5874 | return false; |
| 5875 | } |
| 5876 | } |
| 5877 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5878 | namespace { |
| 5879 | |
| 5880 | // Callback to only accept typo corrections that are namespaces. |
| 5881 | class NamespaceValidatorCCC : public CorrectionCandidateCallback { |
| 5882 | public: |
| 5883 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 5884 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 5885 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
| 5886 | } |
| 5887 | return false; |
| 5888 | } |
| 5889 | }; |
| 5890 | |
| 5891 | } |
| 5892 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5893 | static bool TryNamespaceTypoCorrection(Sema &S, LookupResult &R, Scope *Sc, |
| 5894 | CXXScopeSpec &SS, |
| 5895 | SourceLocation IdentLoc, |
| 5896 | IdentifierInfo *Ident) { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5897 | NamespaceValidatorCCC Validator; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5898 | R.clear(); |
| 5899 | if (TypoCorrection Corrected = S.CorrectTypo(R.getLookupNameInfo(), |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5900 | R.getLookupKind(), Sc, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 5901 | Validator)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5902 | std::string CorrectedStr(Corrected.getAsString(S.getLangOpts())); |
| 5903 | std::string CorrectedQuotedStr(Corrected.getQuoted(S.getLangOpts())); |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5904 | if (DeclContext *DC = S.computeDeclContext(SS, false)) |
| 5905 | S.Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 5906 | << Ident << DC << CorrectedQuotedStr << SS.getRange() |
David Blaikie | 6952c01 | 2012-10-12 20:00:44 +0000 | [diff] [blame] | 5907 | << FixItHint::CreateReplacement(Corrected.getCorrectionRange(), |
| 5908 | CorrectedStr); |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5909 | else |
| 5910 | S.Diag(IdentLoc, diag::err_using_directive_suggest) |
| 5911 | << Ident << CorrectedQuotedStr |
| 5912 | << FixItHint::CreateReplacement(IdentLoc, CorrectedStr); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5913 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5914 | S.Diag(Corrected.getCorrectionDecl()->getLocation(), |
| 5915 | diag::note_namespace_defined_here) << CorrectedQuotedStr; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5916 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 5917 | R.addDecl(Corrected.getCorrectionDecl()); |
| 5918 | return true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5919 | } |
| 5920 | return false; |
| 5921 | } |
| 5922 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5923 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5924 | SourceLocation UsingLoc, |
| 5925 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5926 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5927 | SourceLocation IdentLoc, |
| 5928 | IdentifierInfo *NamespcName, |
| 5929 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5930 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 5931 | assert(NamespcName && "Invalid NamespcName."); |
| 5932 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 5933 | |
| 5934 | // This can only happen along a recovery path. |
| 5935 | while (S->getFlags() & Scope::TemplateParamScope) |
| 5936 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5937 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5938 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5939 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5940 | NestedNameSpecifier *Qualifier = 0; |
| 5941 | if (SS.isSet()) |
| 5942 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5943 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 5944 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5945 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 5946 | LookupParsedName(R, S, &SS); |
| 5947 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5948 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5949 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5950 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5951 | R.clear(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5952 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 5953 | // "std" hasn't been defined yet, for GCC compatibility. |
| 5954 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 5955 | NamespcName->isStr("std")) { |
| 5956 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 5957 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5958 | R.resolveKind(); |
| 5959 | } |
| 5960 | // Otherwise, attempt typo correction. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5961 | else TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 5962 | } |
| 5963 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5964 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5965 | NamedDecl *Named = R.getFoundDecl(); |
| 5966 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 5967 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5968 | // C++ [namespace.udir]p1: |
| 5969 | // A using-directive specifies that the names in the nominated |
| 5970 | // namespace can be used in the scope in which the |
| 5971 | // using-directive appears after the using-directive. During |
| 5972 | // unqualified name lookup (3.4.1), the names appear as if they |
| 5973 | // were declared in the nearest enclosing namespace which |
| 5974 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 5975 | // namespace. [Note: in this context, "contains" means "contains |
| 5976 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5977 | |
| 5978 | // Find enclosing context containing both using-directive and |
| 5979 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5980 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5981 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 5982 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 5983 | CommonAncestor = CommonAncestor->getParent(); |
| 5984 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5985 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 5986 | SS.getWithLocInContext(Context), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 5987 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 5988 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 5989 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 5990 | !SourceMgr.isFromMainFile(SourceMgr.getExpansionLoc(IdentLoc))) { |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 5991 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 5992 | } |
| 5993 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5994 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5995 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 5996 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5997 | } |
| 5998 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5999 | // FIXME: We ignore attributes for now. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6000 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6001 | } |
| 6002 | |
| 6003 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 6004 | // If the scope has an associated entity and the using directive is at |
| 6005 | // namespace or translation unit scope, add the UsingDirectiveDecl into |
| 6006 | // its lookup structure so qualified name lookup can find it. |
| 6007 | DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity()); |
| 6008 | if (Ctx && !Ctx->isFunctionOrMethod()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6009 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6010 | else |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 6011 | // Otherwise, it is at block sope. The using-directives will affect lookup |
| 6012 | // only to the end of the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6013 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6014 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6015 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6016 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6017 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 6018 | AccessSpecifier AS, |
| 6019 | bool HasUsingKeyword, |
| 6020 | SourceLocation UsingLoc, |
| 6021 | CXXScopeSpec &SS, |
| 6022 | UnqualifiedId &Name, |
| 6023 | AttributeList *AttrList, |
| 6024 | bool IsTypeName, |
| 6025 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6026 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6027 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6028 | switch (Name.getKind()) { |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 6029 | case UnqualifiedId::IK_ImplicitSelfParam: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6030 | case UnqualifiedId::IK_Identifier: |
| 6031 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 6032 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6033 | case UnqualifiedId::IK_ConversionFunctionId: |
| 6034 | break; |
| 6035 | |
| 6036 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 6037 | case UnqualifiedId::IK_ConstructorTemplateId: |
Richard Smith | a1366cb | 2012-04-27 19:33:05 +0000 | [diff] [blame] | 6038 | // C++11 inheriting constructors. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6039 | Diag(Name.getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6040 | getLangOpts().CPlusPlus0x ? |
Richard Smith | a1366cb | 2012-04-27 19:33:05 +0000 | [diff] [blame] | 6041 | // FIXME: Produce warn_cxx98_compat_using_decl_constructor |
| 6042 | // instead once inheriting constructors work. |
| 6043 | diag::err_using_decl_constructor_unsupported : |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6044 | diag::err_using_decl_constructor) |
| 6045 | << SS.getRange(); |
| 6046 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6047 | if (getLangOpts().CPlusPlus0x) break; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6048 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6049 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6050 | |
| 6051 | case UnqualifiedId::IK_DestructorName: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6052 | Diag(Name.getLocStart(), diag::err_using_decl_destructor) |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6053 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6054 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6055 | |
| 6056 | case UnqualifiedId::IK_TemplateId: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6057 | Diag(Name.getLocStart(), diag::err_using_decl_template_id) |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6058 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6059 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6060 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6061 | |
| 6062 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 6063 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6064 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6065 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6066 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 6067 | // Warn about using declarations. |
| 6068 | // TODO: store that the declaration was written without 'using' and |
| 6069 | // talk about access decls instead of using decls in the |
| 6070 | // diagnostics. |
| 6071 | if (!HasUsingKeyword) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6072 | UsingLoc = Name.getLocStart(); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 6073 | |
| 6074 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6075 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 6076 | } |
| 6077 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 6078 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 6079 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 6080 | return 0; |
| 6081 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6082 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6083 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6084 | /* IsInstantiation */ false, |
| 6085 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6086 | if (UD) |
| 6087 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6088 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6089 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6090 | } |
| 6091 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6092 | /// \brief Determine whether a using declaration considers the given |
| 6093 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 6094 | /// the same entity or are both typedefs of the same type. |
| 6095 | static bool |
| 6096 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 6097 | bool &SuppressRedeclaration) { |
| 6098 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 6099 | SuppressRedeclaration = false; |
| 6100 | return true; |
| 6101 | } |
| 6102 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6103 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
| 6104 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6105 | SuppressRedeclaration = true; |
| 6106 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 6107 | TD2->getUnderlyingType()); |
| 6108 | } |
| 6109 | |
| 6110 | return false; |
| 6111 | } |
| 6112 | |
| 6113 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6114 | /// Determines whether to create a using shadow decl for a particular |
| 6115 | /// decl, given the set of decls existing prior to this using lookup. |
| 6116 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 6117 | const LookupResult &Previous) { |
| 6118 | // Diagnose finding a decl which is not from a base class of the |
| 6119 | // current class. We do this now because there are cases where this |
| 6120 | // function will silently decide not to build a shadow decl, which |
| 6121 | // will pre-empt further diagnostics. |
| 6122 | // |
| 6123 | // We don't need to do this in C++0x because we do the check once on |
| 6124 | // the qualifier. |
| 6125 | // |
| 6126 | // FIXME: diagnose the following if we care enough: |
| 6127 | // struct A { int foo; }; |
| 6128 | // struct B : A { using A::foo; }; |
| 6129 | // template <class T> struct C : A {}; |
| 6130 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 6131 | // This is invalid (during instantiation) in C++03 because B::foo |
| 6132 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 6133 | // We can't diagnose it immediately because C<T> is an unknown |
| 6134 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 6135 | // to A::foo, which will look well-formed when we instantiate. |
| 6136 | // The right solution is to not collapse the shadow-decl chain. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6137 | if (!getLangOpts().CPlusPlus0x && CurContext->isRecord()) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6138 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 6139 | |
| 6140 | // Handle enums and anonymous structs. |
| 6141 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 6142 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 6143 | while (OrigRec->isAnonymousStructOrUnion()) |
| 6144 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 6145 | |
| 6146 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 6147 | if (OrigDC == CurContext) { |
| 6148 | Diag(Using->getLocation(), |
| 6149 | diag::err_using_decl_nested_name_specifier_is_current_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6150 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6151 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 6152 | return true; |
| 6153 | } |
| 6154 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6155 | Diag(Using->getQualifierLoc().getBeginLoc(), |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6156 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6157 | << Using->getQualifier() |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6158 | << cast<CXXRecordDecl>(CurContext) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6159 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6160 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 6161 | return true; |
| 6162 | } |
| 6163 | } |
| 6164 | |
| 6165 | if (Previous.empty()) return false; |
| 6166 | |
| 6167 | NamedDecl *Target = Orig; |
| 6168 | if (isa<UsingShadowDecl>(Target)) |
| 6169 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 6170 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 6171 | // If the target happens to be one of the previous declarations, we |
| 6172 | // don't have a conflict. |
| 6173 | // |
| 6174 | // FIXME: but we might be increasing its access, in which case we |
| 6175 | // should redeclare it. |
| 6176 | NamedDecl *NonTag = 0, *Tag = 0; |
| 6177 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6178 | I != E; ++I) { |
| 6179 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6180 | bool Result; |
| 6181 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 6182 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 6183 | |
| 6184 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 6185 | } |
| 6186 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6187 | if (Target->isFunctionOrFunctionTemplate()) { |
| 6188 | FunctionDecl *FD; |
| 6189 | if (isa<FunctionTemplateDecl>(Target)) |
| 6190 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 6191 | else |
| 6192 | FD = cast<FunctionDecl>(Target); |
| 6193 | |
| 6194 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 6195 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6196 | case Ovl_Overload: |
| 6197 | return false; |
| 6198 | |
| 6199 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6200 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6201 | break; |
| 6202 | |
| 6203 | // We found a decl with the exact signature. |
| 6204 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6205 | // If we're in a record, we want to hide the target, so we |
| 6206 | // return true (without a diagnostic) to tell the caller not to |
| 6207 | // build a shadow decl. |
| 6208 | if (CurContext->isRecord()) |
| 6209 | return true; |
| 6210 | |
| 6211 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6212 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6213 | break; |
| 6214 | } |
| 6215 | |
| 6216 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6217 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 6218 | return true; |
| 6219 | } |
| 6220 | |
| 6221 | // Target is not a function. |
| 6222 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6223 | if (isa<TagDecl>(Target)) { |
| 6224 | // No conflict between a tag and a non-tag. |
| 6225 | if (!Tag) return false; |
| 6226 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6227 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6228 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6229 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 6230 | return true; |
| 6231 | } |
| 6232 | |
| 6233 | // No conflict between a tag and a non-tag. |
| 6234 | if (!NonTag) return false; |
| 6235 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6236 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6237 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6238 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 6239 | return true; |
| 6240 | } |
| 6241 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6242 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6243 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6244 | UsingDecl *UD, |
| 6245 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6246 | |
| 6247 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6248 | NamedDecl *Target = Orig; |
| 6249 | if (isa<UsingShadowDecl>(Target)) { |
| 6250 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 6251 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6252 | } |
| 6253 | |
| 6254 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6255 | = UsingShadowDecl::Create(Context, CurContext, |
| 6256 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6257 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 6258 | |
| 6259 | Shadow->setAccess(UD->getAccess()); |
| 6260 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 6261 | Shadow->setInvalidDecl(); |
| 6262 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6263 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6264 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6265 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6266 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6267 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6268 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6269 | return Shadow; |
| 6270 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6271 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6272 | /// Hides a using shadow declaration. This is required by the current |
| 6273 | /// using-decl implementation when a resolvable using declaration in a |
| 6274 | /// class is followed by a declaration which would hide or override |
| 6275 | /// one or more of the using decl's targets; for example: |
| 6276 | /// |
| 6277 | /// struct Base { void foo(int); }; |
| 6278 | /// struct Derived : Base { |
| 6279 | /// using Base::foo; |
| 6280 | /// void foo(int); |
| 6281 | /// }; |
| 6282 | /// |
| 6283 | /// The governing language is C++03 [namespace.udecl]p12: |
| 6284 | /// |
| 6285 | /// When a using-declaration brings names from a base class into a |
| 6286 | /// derived class scope, member functions in the derived class |
| 6287 | /// override and/or hide member functions with the same name and |
| 6288 | /// parameter types in a base class (rather than conflicting). |
| 6289 | /// |
| 6290 | /// There are two ways to implement this: |
| 6291 | /// (1) optimistically create shadow decls when they're not hidden |
| 6292 | /// by existing declarations, or |
| 6293 | /// (2) don't create any shadow decls (or at least don't make them |
| 6294 | /// visible) until we've fully parsed/instantiated the class. |
| 6295 | /// The problem with (1) is that we might have to retroactively remove |
| 6296 | /// a shadow decl, which requires several O(n) operations because the |
| 6297 | /// decl structures are (very reasonably) not designed for removal. |
| 6298 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 6299 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6300 | if (Shadow->getDeclName().getNameKind() == |
| 6301 | DeclarationName::CXXConversionFunctionName) |
| 6302 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 6303 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6304 | // Remove it from the DeclContext... |
| 6305 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6306 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6307 | // ...and the scope, if applicable... |
| 6308 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6309 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6310 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6311 | } |
| 6312 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6313 | // ...and the using decl. |
| 6314 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 6315 | |
| 6316 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 6317 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6318 | } |
| 6319 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6320 | /// Builds a using declaration. |
| 6321 | /// |
| 6322 | /// \param IsInstantiation - Whether this call arises from an |
| 6323 | /// instantiation of an unresolved using declaration. We treat |
| 6324 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6325 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 6326 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6327 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6328 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6329 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6330 | bool IsInstantiation, |
| 6331 | bool IsTypeName, |
| 6332 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6333 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6334 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6335 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 6336 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 6337 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6338 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6339 | if (SS.isEmpty()) { |
| 6340 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6341 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6342 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6343 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6344 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6345 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6346 | ForRedeclaration); |
| 6347 | Previous.setHideTags(false); |
| 6348 | if (S) { |
| 6349 | LookupName(Previous, S); |
| 6350 | |
| 6351 | // It is really dumb that we have to do this. |
| 6352 | LookupResult::Filter F = Previous.makeFilter(); |
| 6353 | while (F.hasNext()) { |
| 6354 | NamedDecl *D = F.next(); |
| 6355 | if (!isDeclInScope(D, CurContext, S)) |
| 6356 | F.erase(); |
| 6357 | } |
| 6358 | F.done(); |
| 6359 | } else { |
| 6360 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 6361 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 6362 | LookupQualifiedName(Previous, CurContext); |
| 6363 | } |
| 6364 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6365 | // Check for invalid redeclarations. |
| 6366 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 6367 | return 0; |
| 6368 | |
| 6369 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6370 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 6371 | return 0; |
| 6372 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 6373 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6374 | NamedDecl *D; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6375 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 6376 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6377 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6378 | // FIXME: not all declaration name kinds are legal here |
| 6379 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 6380 | UsingLoc, TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6381 | QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6382 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6383 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6384 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 6385 | QualifierLoc, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6386 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6387 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6388 | D = UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, |
| 6389 | NameInfo, IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 6390 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6391 | D->setAccess(AS); |
| 6392 | CurContext->addDecl(D); |
| 6393 | |
| 6394 | if (!LookupContext) return D; |
| 6395 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6396 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6397 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6398 | UD->setInvalidDecl(); |
| 6399 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6400 | } |
| 6401 | |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 6402 | // The normal rules do not apply to inheriting constructor declarations. |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6403 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 6404 | if (CheckInheritingConstructorUsingDecl(UD)) |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 6405 | UD->setInvalidDecl(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6406 | return UD; |
| 6407 | } |
| 6408 | |
| 6409 | // Otherwise, look up the target name. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6410 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6411 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6412 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6413 | // Unlike most lookups, we don't always want to hide tag |
| 6414 | // declarations: tag names are visible through the using declaration |
| 6415 | // even if hidden by ordinary names, *except* in a dependent context |
| 6416 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6417 | if (!IsInstantiation) |
| 6418 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6419 | |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 6420 | // For the purposes of this lookup, we have a base object type |
| 6421 | // equal to that of the current context. |
| 6422 | if (CurContext->isRecord()) { |
| 6423 | R.setBaseObjectType( |
| 6424 | Context.getTypeDeclType(cast<CXXRecordDecl>(CurContext))); |
| 6425 | } |
| 6426 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6427 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6428 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6429 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6430 | Diag(IdentLoc, diag::err_no_member) |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6431 | << NameInfo.getName() << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6432 | UD->setInvalidDecl(); |
| 6433 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6434 | } |
| 6435 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6436 | if (R.isAmbiguous()) { |
| 6437 | UD->setInvalidDecl(); |
| 6438 | return UD; |
| 6439 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6440 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6441 | if (IsTypeName) { |
| 6442 | // 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] | 6443 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6444 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 6445 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 6446 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 6447 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6448 | UD->setInvalidDecl(); |
| 6449 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6450 | } |
| 6451 | } else { |
| 6452 | // If we asked for a non-typename and we got a type, error out, |
| 6453 | // but only if this is an instantiation of an unresolved using |
| 6454 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6455 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6456 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 6457 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6458 | UD->setInvalidDecl(); |
| 6459 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6460 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 6461 | } |
| 6462 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 6463 | // C++0x N2914 [namespace.udecl]p6: |
| 6464 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6465 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 6466 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 6467 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6468 | UD->setInvalidDecl(); |
| 6469 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 6470 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6471 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6472 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 6473 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 6474 | BuildUsingShadowDecl(S, UD, *I); |
| 6475 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6476 | |
| 6477 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6478 | } |
| 6479 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6480 | /// Additional checks for a using declaration referring to a constructor name. |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 6481 | bool Sema::CheckInheritingConstructorUsingDecl(UsingDecl *UD) { |
| 6482 | assert(!UD->isTypeName() && "expecting a constructor name"); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6483 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6484 | const Type *SourceType = UD->getQualifier()->getAsType(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6485 | assert(SourceType && |
| 6486 | "Using decl naming constructor doesn't have type in scope spec."); |
| 6487 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 6488 | |
| 6489 | // Check whether the named type is a direct base class. |
| 6490 | CanQualType CanonicalSourceType = SourceType->getCanonicalTypeUnqualified(); |
| 6491 | CXXRecordDecl::base_class_iterator BaseIt, BaseE; |
| 6492 | for (BaseIt = TargetClass->bases_begin(), BaseE = TargetClass->bases_end(); |
| 6493 | BaseIt != BaseE; ++BaseIt) { |
| 6494 | CanQualType BaseType = BaseIt->getType()->getCanonicalTypeUnqualified(); |
| 6495 | if (CanonicalSourceType == BaseType) |
| 6496 | break; |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 6497 | if (BaseIt->getType()->isDependentType()) |
| 6498 | break; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | if (BaseIt == BaseE) { |
| 6502 | // Did not find SourceType in the bases. |
| 6503 | Diag(UD->getUsingLocation(), |
| 6504 | diag::err_using_decl_constructor_not_in_direct_base) |
| 6505 | << UD->getNameInfo().getSourceRange() |
| 6506 | << QualType(SourceType, 0) << TargetClass; |
| 6507 | return true; |
| 6508 | } |
| 6509 | |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 6510 | if (!CurContext->isDependentContext()) |
| 6511 | BaseIt->setInheritConstructors(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6512 | |
| 6513 | return false; |
| 6514 | } |
| 6515 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6516 | /// Checks that the given using declaration is not an invalid |
| 6517 | /// redeclaration. Note that this is checking only for the using decl |
| 6518 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 6519 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 6520 | bool isTypeName, |
| 6521 | const CXXScopeSpec &SS, |
| 6522 | SourceLocation NameLoc, |
| 6523 | const LookupResult &Prev) { |
| 6524 | // C++03 [namespace.udecl]p8: |
| 6525 | // C++0x [namespace.udecl]p10: |
| 6526 | // A using-declaration is a declaration and can therefore be used |
| 6527 | // repeatedly where (and only where) multiple declarations are |
| 6528 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 6529 | // |
John McCall | 8a72621 | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 6530 | // That's in non-member contexts. |
| 6531 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6532 | return false; |
| 6533 | |
| 6534 | NestedNameSpecifier *Qual |
| 6535 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 6536 | |
| 6537 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 6538 | NamedDecl *D = *I; |
| 6539 | |
| 6540 | bool DTypename; |
| 6541 | NestedNameSpecifier *DQual; |
| 6542 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 6543 | DTypename = UD->isTypeName(); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6544 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6545 | } else if (UnresolvedUsingValueDecl *UD |
| 6546 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 6547 | DTypename = false; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6548 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6549 | } else if (UnresolvedUsingTypenameDecl *UD |
| 6550 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 6551 | DTypename = true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6552 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6553 | } else continue; |
| 6554 | |
| 6555 | // using decls differ if one says 'typename' and the other doesn't. |
| 6556 | // FIXME: non-dependent using decls? |
| 6557 | if (isTypeName != DTypename) continue; |
| 6558 | |
| 6559 | // using decls differ if they name different scopes (but note that |
| 6560 | // template instantiation can cause this check to trigger when it |
| 6561 | // didn't before instantiation). |
| 6562 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 6563 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 6564 | continue; |
| 6565 | |
| 6566 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6567 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6568 | return true; |
| 6569 | } |
| 6570 | |
| 6571 | return false; |
| 6572 | } |
| 6573 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6574 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6575 | /// Checks that the given nested-name qualifier used in a using decl |
| 6576 | /// in the current context is appropriately related to the current |
| 6577 | /// scope. If an error is found, diagnoses it and returns true. |
| 6578 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 6579 | const CXXScopeSpec &SS, |
| 6580 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6581 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6582 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6583 | if (!CurContext->isRecord()) { |
| 6584 | // C++03 [namespace.udecl]p3: |
| 6585 | // C++0x [namespace.udecl]p8: |
| 6586 | // A using-declaration for a class member shall be a member-declaration. |
| 6587 | |
| 6588 | // If we weren't able to compute a valid scope, it must be a |
| 6589 | // dependent class scope. |
| 6590 | if (!NamedContext || NamedContext->isRecord()) { |
| 6591 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 6592 | << SS.getRange(); |
| 6593 | return true; |
| 6594 | } |
| 6595 | |
| 6596 | // Otherwise, everything is known to be fine. |
| 6597 | return false; |
| 6598 | } |
| 6599 | |
| 6600 | // The current scope is a record. |
| 6601 | |
| 6602 | // If the named context is dependent, we can't decide much. |
| 6603 | if (!NamedContext) { |
| 6604 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 6605 | // nested-name-specifier does not refer to a base class, which is |
| 6606 | // still possible in some cases. |
| 6607 | |
| 6608 | // Otherwise we have to conservatively report that things might be |
| 6609 | // okay. |
| 6610 | return false; |
| 6611 | } |
| 6612 | |
| 6613 | if (!NamedContext->isRecord()) { |
| 6614 | // Ideally this would point at the last name in the specifier, |
| 6615 | // but we don't have that level of source info. |
| 6616 | Diag(SS.getRange().getBegin(), |
| 6617 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 6618 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 6619 | return true; |
| 6620 | } |
| 6621 | |
Douglas Gregor | 6fb0729 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 6622 | if (!NamedContext->isDependentContext() && |
| 6623 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 6624 | return true; |
| 6625 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6626 | if (getLangOpts().CPlusPlus0x) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6627 | // C++0x [namespace.udecl]p3: |
| 6628 | // In a using-declaration used as a member-declaration, the |
| 6629 | // nested-name-specifier shall name a base class of the class |
| 6630 | // being defined. |
| 6631 | |
| 6632 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 6633 | cast<CXXRecordDecl>(NamedContext))) { |
| 6634 | if (CurContext == NamedContext) { |
| 6635 | Diag(NameLoc, |
| 6636 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 6637 | << SS.getRange(); |
| 6638 | return true; |
| 6639 | } |
| 6640 | |
| 6641 | Diag(SS.getRange().getBegin(), |
| 6642 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 6643 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 6644 | << cast<CXXRecordDecl>(CurContext) |
| 6645 | << SS.getRange(); |
| 6646 | return true; |
| 6647 | } |
| 6648 | |
| 6649 | return false; |
| 6650 | } |
| 6651 | |
| 6652 | // C++03 [namespace.udecl]p4: |
| 6653 | // A using-declaration used as a member-declaration shall refer |
| 6654 | // to a member of a base class of the class being defined [etc.]. |
| 6655 | |
| 6656 | // Salient point: SS doesn't have to name a base class as long as |
| 6657 | // lookup only finds members from base classes. Therefore we can |
| 6658 | // diagnose here only if we can prove that that can't happen, |
| 6659 | // i.e. if the class hierarchies provably don't intersect. |
| 6660 | |
| 6661 | // TODO: it would be nice if "definitely valid" results were cached |
| 6662 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 6663 | // need to be repeated. |
| 6664 | |
| 6665 | struct UserData { |
Benjamin Kramer | 8c43dcc | 2012-02-23 16:06:01 +0000 | [diff] [blame] | 6666 | llvm::SmallPtrSet<const CXXRecordDecl*, 4> Bases; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6667 | |
| 6668 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 6669 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 6670 | Data->Bases.insert(Base); |
| 6671 | return true; |
| 6672 | } |
| 6673 | |
| 6674 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 6675 | return !Class->forallBases(collect, this); |
| 6676 | } |
| 6677 | |
| 6678 | /// Returns true if the base is dependent or is one of the |
| 6679 | /// accumulated base classes. |
| 6680 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 6681 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 6682 | return !Data->Bases.count(Base); |
| 6683 | } |
| 6684 | |
| 6685 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 6686 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 6687 | } |
| 6688 | }; |
| 6689 | |
| 6690 | UserData Data; |
| 6691 | |
| 6692 | // Returns false if we find a dependent base. |
| 6693 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 6694 | return false; |
| 6695 | |
| 6696 | // Returns false if the class has a dependent base or if it or one |
| 6697 | // of its bases is present in the base set of the current context. |
| 6698 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 6699 | return false; |
| 6700 | |
| 6701 | Diag(SS.getRange().getBegin(), |
| 6702 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 6703 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 6704 | << cast<CXXRecordDecl>(CurContext) |
| 6705 | << SS.getRange(); |
| 6706 | |
| 6707 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6708 | } |
| 6709 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6710 | Decl *Sema::ActOnAliasDeclaration(Scope *S, |
| 6711 | AccessSpecifier AS, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6712 | MultiTemplateParamsArg TemplateParamLists, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6713 | SourceLocation UsingLoc, |
| 6714 | UnqualifiedId &Name, |
| 6715 | TypeResult Type) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6716 | // Skip up to the relevant declaration scope. |
| 6717 | while (S->getFlags() & Scope::TemplateParamScope) |
| 6718 | S = S->getParent(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6719 | assert((S->getFlags() & Scope::DeclScope) && |
| 6720 | "got alias-declaration outside of declaration scope"); |
| 6721 | |
| 6722 | if (Type.isInvalid()) |
| 6723 | return 0; |
| 6724 | |
| 6725 | bool Invalid = false; |
| 6726 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
| 6727 | TypeSourceInfo *TInfo = 0; |
Nick Lewycky | b79bf1d | 2011-05-02 01:07:19 +0000 | [diff] [blame] | 6728 | GetTypeFromParser(Type.get(), &TInfo); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6729 | |
| 6730 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
| 6731 | return 0; |
| 6732 | |
| 6733 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6734 | UPPC_DeclarationType)) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6735 | Invalid = true; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6736 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 6737 | TInfo->getTypeLoc().getBeginLoc()); |
| 6738 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6739 | |
| 6740 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, ForRedeclaration); |
| 6741 | LookupName(Previous, S); |
| 6742 | |
| 6743 | // Warn about shadowing the name of a template parameter. |
| 6744 | if (Previous.isSingleResult() && |
| 6745 | Previous.getFoundDecl()->isTemplateParameter()) { |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 6746 | DiagnoseTemplateParameterShadow(Name.StartLocation,Previous.getFoundDecl()); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6747 | Previous.clear(); |
| 6748 | } |
| 6749 | |
| 6750 | assert(Name.Kind == UnqualifiedId::IK_Identifier && |
| 6751 | "name in alias declaration must be an identifier"); |
| 6752 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 6753 | Name.StartLocation, |
| 6754 | Name.Identifier, TInfo); |
| 6755 | |
| 6756 | NewTD->setAccess(AS); |
| 6757 | |
| 6758 | if (Invalid) |
| 6759 | NewTD->setInvalidDecl(); |
| 6760 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6761 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 6762 | Invalid |= NewTD->isInvalidDecl(); |
| 6763 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6764 | bool Redeclaration = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6765 | |
| 6766 | NamedDecl *NewND; |
| 6767 | if (TemplateParamLists.size()) { |
| 6768 | TypeAliasTemplateDecl *OldDecl = 0; |
| 6769 | TemplateParameterList *OldTemplateParams = 0; |
| 6770 | |
| 6771 | if (TemplateParamLists.size() != 1) { |
| 6772 | Diag(UsingLoc, diag::err_alias_template_extra_headers) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6773 | << SourceRange(TemplateParamLists[1]->getTemplateLoc(), |
| 6774 | TemplateParamLists[TemplateParamLists.size()-1]->getRAngleLoc()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6775 | } |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6776 | TemplateParameterList *TemplateParams = TemplateParamLists[0]; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6777 | |
| 6778 | // Only consider previous declarations in the same scope. |
| 6779 | FilterLookupForScope(Previous, CurContext, S, /*ConsiderLinkage*/false, |
| 6780 | /*ExplicitInstantiationOrSpecialization*/false); |
| 6781 | if (!Previous.empty()) { |
| 6782 | Redeclaration = true; |
| 6783 | |
| 6784 | OldDecl = Previous.getAsSingle<TypeAliasTemplateDecl>(); |
| 6785 | if (!OldDecl && !Invalid) { |
| 6786 | Diag(UsingLoc, diag::err_redefinition_different_kind) |
| 6787 | << Name.Identifier; |
| 6788 | |
| 6789 | NamedDecl *OldD = Previous.getRepresentativeDecl(); |
| 6790 | if (OldD->getLocation().isValid()) |
| 6791 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| 6792 | |
| 6793 | Invalid = true; |
| 6794 | } |
| 6795 | |
| 6796 | if (!Invalid && OldDecl && !OldDecl->isInvalidDecl()) { |
| 6797 | if (TemplateParameterListsAreEqual(TemplateParams, |
| 6798 | OldDecl->getTemplateParameters(), |
| 6799 | /*Complain=*/true, |
| 6800 | TPL_TemplateMatch)) |
| 6801 | OldTemplateParams = OldDecl->getTemplateParameters(); |
| 6802 | else |
| 6803 | Invalid = true; |
| 6804 | |
| 6805 | TypeAliasDecl *OldTD = OldDecl->getTemplatedDecl(); |
| 6806 | if (!Invalid && |
| 6807 | !Context.hasSameType(OldTD->getUnderlyingType(), |
| 6808 | NewTD->getUnderlyingType())) { |
| 6809 | // FIXME: The C++0x standard does not clearly say this is ill-formed, |
| 6810 | // but we can't reasonably accept it. |
| 6811 | Diag(NewTD->getLocation(), diag::err_redefinition_different_typedef) |
| 6812 | << 2 << NewTD->getUnderlyingType() << OldTD->getUnderlyingType(); |
| 6813 | if (OldTD->getLocation().isValid()) |
| 6814 | Diag(OldTD->getLocation(), diag::note_previous_definition); |
| 6815 | Invalid = true; |
| 6816 | } |
| 6817 | } |
| 6818 | } |
| 6819 | |
| 6820 | // Merge any previous default template arguments into our parameters, |
| 6821 | // and check the parameter list. |
| 6822 | if (CheckTemplateParameterList(TemplateParams, OldTemplateParams, |
| 6823 | TPC_TypeAliasTemplate)) |
| 6824 | return 0; |
| 6825 | |
| 6826 | TypeAliasTemplateDecl *NewDecl = |
| 6827 | TypeAliasTemplateDecl::Create(Context, CurContext, UsingLoc, |
| 6828 | Name.Identifier, TemplateParams, |
| 6829 | NewTD); |
| 6830 | |
| 6831 | NewDecl->setAccess(AS); |
| 6832 | |
| 6833 | if (Invalid) |
| 6834 | NewDecl->setInvalidDecl(); |
| 6835 | else if (OldDecl) |
| 6836 | NewDecl->setPreviousDeclaration(OldDecl); |
| 6837 | |
| 6838 | NewND = NewDecl; |
| 6839 | } else { |
| 6840 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 6841 | NewND = NewTD; |
| 6842 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6843 | |
| 6844 | if (!Redeclaration) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6845 | PushOnScopeChains(NewND, S); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6846 | |
Dmitri Gribenko | c27bc80 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 6847 | ActOnDocumentableDecl(NewND); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 6848 | return NewND; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6849 | } |
| 6850 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6851 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 6852 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6853 | SourceLocation AliasLoc, |
| 6854 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6855 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 6856 | SourceLocation IdentLoc, |
| 6857 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6858 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 6859 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6860 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 6861 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 6862 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 6863 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 6864 | NamedDecl *PrevDecl |
| 6865 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 6866 | ForRedeclaration); |
| 6867 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 6868 | PrevDecl = 0; |
| 6869 | |
| 6870 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 6871 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6872 | // 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] | 6873 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 6874 | // FIXME: At some point, we'll want to create the (redundant) |
| 6875 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6876 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 6877 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6878 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 6879 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6880 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 6881 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 6882 | diag::err_redefinition_different_kind; |
| 6883 | Diag(AliasLoc, DiagID) << Alias; |
| 6884 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6885 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 6886 | } |
| 6887 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6888 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6889 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6890 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6891 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6892 | if (!TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, Ident)) { |
Richard Smith | bf9658c | 2012-04-05 23:13:23 +0000 | [diff] [blame] | 6893 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6894 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 6895 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 6896 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6897 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 6898 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6899 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 6900 | Alias, SS.getWithLocInContext(Context), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6901 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6902 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 6903 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6904 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 6905 | } |
| 6906 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 6907 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 6908 | Sema::ComputeDefaultedDefaultCtorExceptionSpec(SourceLocation Loc, |
| 6909 | CXXMethodDecl *MD) { |
| 6910 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 6911 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6912 | // C++ [except.spec]p14: |
| 6913 | // An implicitly declared special member function (Clause 12) shall have an |
| 6914 | // exception-specification. [...] |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 6915 | ImplicitExceptionSpecification ExceptSpec(*this); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 6916 | if (ClassDecl->isInvalidDecl()) |
| 6917 | return ExceptSpec; |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6918 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6919 | // Direct base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6920 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 6921 | BEnd = ClassDecl->bases_end(); |
| 6922 | B != BEnd; ++B) { |
| 6923 | if (B->isVirtual()) // Handled below. |
| 6924 | continue; |
| 6925 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6926 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 6927 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 6928 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 6929 | // If this is a deleted function, add it anyway. This might be conformant |
| 6930 | // with the standard. This might not. I'm not sure. It might not matter. |
| 6931 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 6932 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6933 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6934 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6935 | |
| 6936 | // Virtual base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6937 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 6938 | BEnd = ClassDecl->vbases_end(); |
| 6939 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6940 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 6941 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 6942 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 6943 | // If this is a deleted function, add it anyway. This might be conformant |
| 6944 | // with the standard. This might not. I'm not sure. It might not matter. |
| 6945 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 6946 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6947 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6948 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6949 | |
| 6950 | // Field constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6951 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 6952 | FEnd = ClassDecl->field_end(); |
| 6953 | F != FEnd; ++F) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 6954 | if (F->hasInClassInitializer()) { |
| 6955 | if (Expr *E = F->getInClassInitializer()) |
| 6956 | ExceptSpec.CalledExpr(E); |
| 6957 | else if (!F->isInvalidDecl()) |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 6958 | // DR1351: |
| 6959 | // If the brace-or-equal-initializer of a non-static data member |
| 6960 | // invokes a defaulted default constructor of its class or of an |
| 6961 | // enclosing class in a potentially evaluated subexpression, the |
| 6962 | // program is ill-formed. |
| 6963 | // |
| 6964 | // This resolution is unworkable: the exception specification of the |
| 6965 | // default constructor can be needed in an unevaluated context, in |
| 6966 | // particular, in the operand of a noexcept-expression, and we can be |
| 6967 | // unable to compute an exception specification for an enclosed class. |
| 6968 | // |
| 6969 | // We do not allow an in-class initializer to require the evaluation |
| 6970 | // of the exception specification for any in-class initializer whose |
| 6971 | // definition is not lexically complete. |
| 6972 | Diag(Loc, diag::err_in_class_initializer_references_def_ctor) << MD; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 6973 | } else if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6974 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 6975 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 6976 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 6977 | // If this is a deleted function, add it anyway. This might be conformant |
| 6978 | // with the standard. This might not. I'm not sure. It might not matter. |
| 6979 | // In particular, the problem is that this function never gets called. It |
| 6980 | // might just be ill-formed because this function attempts to refer to |
| 6981 | // a deleted function here. |
| 6982 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 6983 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6984 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6985 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6986 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 6987 | return ExceptSpec; |
| 6988 | } |
| 6989 | |
| 6990 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 6991 | CXXRecordDecl *ClassDecl) { |
| 6992 | // C++ [class.ctor]p5: |
| 6993 | // A default constructor for a class X is a constructor of class X |
| 6994 | // that can be called without an argument. If there is no |
| 6995 | // user-declared constructor for class X, a default constructor is |
| 6996 | // implicitly declared. An implicitly-declared default constructor |
| 6997 | // is an inline public member of its class. |
Richard Smith | d0adeb6 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 6998 | assert(ClassDecl->needsImplicitDefaultConstructor() && |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 6999 | "Should not build implicit default constructor!"); |
| 7000 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 7001 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 7002 | CXXDefaultConstructor, |
| 7003 | false); |
| 7004 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7005 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7006 | CanQualType ClassType |
| 7007 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7008 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7009 | DeclarationName Name |
| 7010 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7011 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 7012 | CXXConstructorDecl *DefaultCon = CXXConstructorDecl::Create( |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7013 | Context, ClassDecl, ClassLoc, NameInfo, /*Type*/QualType(), /*TInfo=*/0, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 7014 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 7015 | Constexpr); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7016 | DefaultCon->setAccess(AS_public); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 7017 | DefaultCon->setDefaulted(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7018 | DefaultCon->setImplicit(); |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 7019 | DefaultCon->setTrivial(ClassDecl->hasTrivialDefaultConstructor()); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7020 | |
| 7021 | // Build an exception specification pointing back at this constructor. |
| 7022 | FunctionProtoType::ExtProtoInfo EPI; |
| 7023 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 7024 | EPI.ExceptionSpecDecl = DefaultCon; |
| 7025 | DefaultCon->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 7026 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7027 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7028 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
| 7029 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 7030 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7031 | PushOnScopeChains(DefaultCon, S, false); |
| 7032 | ClassDecl->addDecl(DefaultCon); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 7033 | |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 7034 | if (ShouldDeleteSpecialMember(DefaultCon, CXXDefaultConstructor)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 7035 | DefaultCon->setDeletedAsWritten(); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7036 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7037 | return DefaultCon; |
| 7038 | } |
| 7039 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 7040 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 7041 | CXXConstructorDecl *Constructor) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 7042 | assert((Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 7043 | !Constructor->doesThisDeclarationHaveABody() && |
| 7044 | !Constructor->isDeleted()) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 7045 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7046 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 7047 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 7048 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 7049 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 7050 | SynthesizedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 7051 | DiagnosticErrorTrap Trap(Diags); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 7052 | if (SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7053 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 7054 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 7055 | << CXXDefaultConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 7056 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7057 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 7058 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7059 | |
| 7060 | SourceLocation Loc = Constructor->getLocation(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 7061 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7062 | |
| 7063 | Constructor->setUsed(); |
| 7064 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7065 | |
| 7066 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7067 | L->CompletedImplicitDefinition(Constructor); |
| 7068 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 7069 | } |
| 7070 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7071 | void Sema::ActOnFinishDelayedMemberInitializers(Decl *D) { |
| 7072 | if (!D) return; |
| 7073 | AdjustDeclIfTemplate(D); |
| 7074 | |
| 7075 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7076 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7077 | if (!ClassDecl->isDependentType()) |
| 7078 | CheckExplicitlyDefaultedMethods(ClassDecl); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7079 | } |
| 7080 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7081 | void Sema::DeclareInheritedConstructors(CXXRecordDecl *ClassDecl) { |
| 7082 | // We start with an initial pass over the base classes to collect those that |
| 7083 | // inherit constructors from. If there are none, we can forgo all further |
| 7084 | // processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7085 | typedef SmallVector<const RecordType *, 4> BasesVector; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7086 | BasesVector BasesToInheritFrom; |
| 7087 | for (CXXRecordDecl::base_class_iterator BaseIt = ClassDecl->bases_begin(), |
| 7088 | BaseE = ClassDecl->bases_end(); |
| 7089 | BaseIt != BaseE; ++BaseIt) { |
| 7090 | if (BaseIt->getInheritConstructors()) { |
| 7091 | QualType Base = BaseIt->getType(); |
| 7092 | if (Base->isDependentType()) { |
| 7093 | // If we inherit constructors from anything that is dependent, just |
| 7094 | // abort processing altogether. We'll get another chance for the |
| 7095 | // instantiations. |
| 7096 | return; |
| 7097 | } |
| 7098 | BasesToInheritFrom.push_back(Base->castAs<RecordType>()); |
| 7099 | } |
| 7100 | } |
| 7101 | if (BasesToInheritFrom.empty()) |
| 7102 | return; |
| 7103 | |
| 7104 | // Now collect the constructors that we already have in the current class. |
| 7105 | // Those take precedence over inherited constructors. |
| 7106 | // C++0x [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 7107 | // unless there is a user-declared constructor with the same signature in |
| 7108 | // the class where the using-declaration appears. |
| 7109 | llvm::SmallSet<const Type *, 8> ExistingConstructors; |
| 7110 | for (CXXRecordDecl::ctor_iterator CtorIt = ClassDecl->ctor_begin(), |
| 7111 | CtorE = ClassDecl->ctor_end(); |
| 7112 | CtorIt != CtorE; ++CtorIt) { |
| 7113 | ExistingConstructors.insert( |
| 7114 | Context.getCanonicalType(CtorIt->getType()).getTypePtr()); |
| 7115 | } |
| 7116 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7117 | DeclarationName CreatedCtorName = |
| 7118 | Context.DeclarationNames.getCXXConstructorName( |
| 7119 | ClassDecl->getTypeForDecl()->getCanonicalTypeUnqualified()); |
| 7120 | |
| 7121 | // Now comes the true work. |
| 7122 | // First, we keep a map from constructor types to the base that introduced |
| 7123 | // them. Needed for finding conflicting constructors. We also keep the |
| 7124 | // actually inserted declarations in there, for pretty diagnostics. |
| 7125 | typedef std::pair<CanQualType, CXXConstructorDecl *> ConstructorInfo; |
| 7126 | typedef llvm::DenseMap<const Type *, ConstructorInfo> ConstructorToSourceMap; |
| 7127 | ConstructorToSourceMap InheritedConstructors; |
| 7128 | for (BasesVector::iterator BaseIt = BasesToInheritFrom.begin(), |
| 7129 | BaseE = BasesToInheritFrom.end(); |
| 7130 | BaseIt != BaseE; ++BaseIt) { |
| 7131 | const RecordType *Base = *BaseIt; |
| 7132 | CanQualType CanonicalBase = Base->getCanonicalTypeUnqualified(); |
| 7133 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(Base->getDecl()); |
| 7134 | for (CXXRecordDecl::ctor_iterator CtorIt = BaseDecl->ctor_begin(), |
| 7135 | CtorE = BaseDecl->ctor_end(); |
| 7136 | CtorIt != CtorE; ++CtorIt) { |
| 7137 | // Find the using declaration for inheriting this base's constructors. |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7138 | // FIXME: Don't perform name lookup just to obtain a source location! |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7139 | DeclarationName Name = |
| 7140 | Context.DeclarationNames.getCXXConstructorName(CanonicalBase); |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7141 | LookupResult Result(*this, Name, SourceLocation(), LookupUsingDeclName); |
| 7142 | LookupQualifiedName(Result, CurContext); |
| 7143 | UsingDecl *UD = Result.getAsSingle<UsingDecl>(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7144 | SourceLocation UsingLoc = UD ? UD->getLocation() : |
| 7145 | ClassDecl->getLocation(); |
| 7146 | |
| 7147 | // C++0x [class.inhctor]p1: The candidate set of inherited constructors |
| 7148 | // from the class X named in the using-declaration consists of actual |
| 7149 | // constructors and notional constructors that result from the |
| 7150 | // transformation of defaulted parameters as follows: |
| 7151 | // - all non-template default constructors of X, and |
| 7152 | // - for each non-template constructor of X that has at least one |
| 7153 | // parameter with a default argument, the set of constructors that |
| 7154 | // results from omitting any ellipsis parameter specification and |
| 7155 | // successively omitting parameters with a default argument from the |
| 7156 | // end of the parameter-type-list. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 7157 | CXXConstructorDecl *BaseCtor = *CtorIt; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7158 | bool CanBeCopyOrMove = BaseCtor->isCopyOrMoveConstructor(); |
| 7159 | const FunctionProtoType *BaseCtorType = |
| 7160 | BaseCtor->getType()->getAs<FunctionProtoType>(); |
| 7161 | |
| 7162 | for (unsigned params = BaseCtor->getMinRequiredArguments(), |
| 7163 | maxParams = BaseCtor->getNumParams(); |
| 7164 | params <= maxParams; ++params) { |
| 7165 | // Skip default constructors. They're never inherited. |
| 7166 | if (params == 0) |
| 7167 | continue; |
| 7168 | // Skip copy and move constructors for the same reason. |
| 7169 | if (CanBeCopyOrMove && params == 1) |
| 7170 | continue; |
| 7171 | |
| 7172 | // Build up a function type for this particular constructor. |
| 7173 | // FIXME: The working paper does not consider that the exception spec |
| 7174 | // for the inheriting constructor might be larger than that of the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7175 | // source. This code doesn't yet, either. When it does, this code will |
| 7176 | // need to be delayed until after exception specifications and in-class |
| 7177 | // member initializers are attached. |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7178 | const Type *NewCtorType; |
| 7179 | if (params == maxParams) |
| 7180 | NewCtorType = BaseCtorType; |
| 7181 | else { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7182 | SmallVector<QualType, 16> Args; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7183 | for (unsigned i = 0; i < params; ++i) { |
| 7184 | Args.push_back(BaseCtorType->getArgType(i)); |
| 7185 | } |
| 7186 | FunctionProtoType::ExtProtoInfo ExtInfo = |
| 7187 | BaseCtorType->getExtProtoInfo(); |
| 7188 | ExtInfo.Variadic = false; |
| 7189 | NewCtorType = Context.getFunctionType(BaseCtorType->getResultType(), |
| 7190 | Args.data(), params, ExtInfo) |
| 7191 | .getTypePtr(); |
| 7192 | } |
| 7193 | const Type *CanonicalNewCtorType = |
| 7194 | Context.getCanonicalType(NewCtorType); |
| 7195 | |
| 7196 | // Now that we have the type, first check if the class already has a |
| 7197 | // constructor with this signature. |
| 7198 | if (ExistingConstructors.count(CanonicalNewCtorType)) |
| 7199 | continue; |
| 7200 | |
| 7201 | // Then we check if we have already declared an inherited constructor |
| 7202 | // with this signature. |
| 7203 | std::pair<ConstructorToSourceMap::iterator, bool> result = |
| 7204 | InheritedConstructors.insert(std::make_pair( |
| 7205 | CanonicalNewCtorType, |
| 7206 | std::make_pair(CanonicalBase, (CXXConstructorDecl*)0))); |
| 7207 | if (!result.second) { |
| 7208 | // Already in the map. If it came from a different class, that's an |
| 7209 | // error. Not if it's from the same. |
| 7210 | CanQualType PreviousBase = result.first->second.first; |
| 7211 | if (CanonicalBase != PreviousBase) { |
| 7212 | const CXXConstructorDecl *PrevCtor = result.first->second.second; |
| 7213 | const CXXConstructorDecl *PrevBaseCtor = |
| 7214 | PrevCtor->getInheritedConstructor(); |
| 7215 | assert(PrevBaseCtor && "Conflicting constructor was not inherited"); |
| 7216 | |
| 7217 | Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 7218 | Diag(BaseCtor->getLocation(), |
| 7219 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 7220 | Diag(PrevBaseCtor->getLocation(), |
| 7221 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 7222 | Diag(PrevCtor->getLocation(), |
| 7223 | diag::note_using_decl_constructor_conflict_previous_using); |
| 7224 | } |
| 7225 | continue; |
| 7226 | } |
| 7227 | |
| 7228 | // OK, we're there, now add the constructor. |
| 7229 | // C++0x [class.inhctor]p8: [...] that would be performed by a |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7230 | // user-written inline constructor [...] |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7231 | DeclarationNameInfo DNI(CreatedCtorName, UsingLoc); |
| 7232 | CXXConstructorDecl *NewCtor = CXXConstructorDecl::Create( |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7233 | Context, ClassDecl, UsingLoc, DNI, QualType(NewCtorType, 0), |
| 7234 | /*TInfo=*/0, BaseCtor->isExplicit(), /*Inline=*/true, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7235 | /*ImplicitlyDeclared=*/true, |
| 7236 | // FIXME: Due to a defect in the standard, we treat inherited |
| 7237 | // constructors as constexpr even if that makes them ill-formed. |
| 7238 | /*Constexpr=*/BaseCtor->isConstexpr()); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7239 | NewCtor->setAccess(BaseCtor->getAccess()); |
| 7240 | |
| 7241 | // Build up the parameter decls and add them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7242 | SmallVector<ParmVarDecl *, 16> ParamDecls; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7243 | for (unsigned i = 0; i < params; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7244 | ParamDecls.push_back(ParmVarDecl::Create(Context, NewCtor, |
| 7245 | UsingLoc, UsingLoc, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7246 | /*IdentifierInfo=*/0, |
| 7247 | BaseCtorType->getArgType(i), |
| 7248 | /*TInfo=*/0, SC_None, |
| 7249 | SC_None, /*DefaultArg=*/0)); |
| 7250 | } |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 7251 | NewCtor->setParams(ParamDecls); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7252 | NewCtor->setInheritedConstructor(BaseCtor); |
| 7253 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7254 | ClassDecl->addDecl(NewCtor); |
| 7255 | result.first->second.second = NewCtor; |
| 7256 | } |
| 7257 | } |
| 7258 | } |
| 7259 | } |
| 7260 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7261 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7262 | Sema::ComputeDefaultedDtorExceptionSpec(CXXMethodDecl *MD) { |
| 7263 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 7264 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7265 | // C++ [except.spec]p14: |
| 7266 | // An implicitly declared special member function (Clause 12) shall have |
| 7267 | // an exception-specification. |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7268 | ImplicitExceptionSpecification ExceptSpec(*this); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 7269 | if (ClassDecl->isInvalidDecl()) |
| 7270 | return ExceptSpec; |
| 7271 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7272 | // Direct base-class destructors. |
| 7273 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 7274 | BEnd = ClassDecl->bases_end(); |
| 7275 | B != BEnd; ++B) { |
| 7276 | if (B->isVirtual()) // Handled below. |
| 7277 | continue; |
| 7278 | |
| 7279 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7280 | ExceptSpec.CalledDecl(B->getLocStart(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7281 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7282 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7283 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7284 | // Virtual base-class destructors. |
| 7285 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 7286 | BEnd = ClassDecl->vbases_end(); |
| 7287 | B != BEnd; ++B) { |
| 7288 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7289 | ExceptSpec.CalledDecl(B->getLocStart(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7290 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7291 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7292 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7293 | // Field destructors. |
| 7294 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 7295 | FEnd = ClassDecl->field_end(); |
| 7296 | F != FEnd; ++F) { |
| 7297 | if (const RecordType *RecordTy |
| 7298 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7299 | ExceptSpec.CalledDecl(F->getLocation(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7300 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7301 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7302 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7303 | return ExceptSpec; |
| 7304 | } |
| 7305 | |
| 7306 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
| 7307 | // C++ [class.dtor]p2: |
| 7308 | // If a class has no user-declared destructor, a destructor is |
| 7309 | // declared implicitly. An implicitly-declared destructor is an |
| 7310 | // inline public member of its class. |
Richard Smith | d0adeb6 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 7311 | assert(!ClassDecl->hasDeclaredDestructor()); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7312 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7313 | // Create the actual destructor declaration. |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7314 | CanQualType ClassType |
| 7315 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7316 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7317 | DeclarationName Name |
| 7318 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7319 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7320 | CXXDestructorDecl *Destructor |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7321 | = CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
| 7322 | QualType(), 0, /*isInline=*/true, |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7323 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7324 | Destructor->setAccess(AS_public); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7325 | Destructor->setDefaulted(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7326 | Destructor->setImplicit(); |
| 7327 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7328 | |
| 7329 | // Build an exception specification pointing back at this destructor. |
| 7330 | FunctionProtoType::ExtProtoInfo EPI; |
| 7331 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 7332 | EPI.ExceptionSpecDecl = Destructor; |
| 7333 | Destructor->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 7334 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7335 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7336 | ++ASTContext::NumImplicitDestructorsDeclared; |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7337 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7338 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 7339 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 7340 | PushOnScopeChains(Destructor, S, false); |
| 7341 | ClassDecl->addDecl(Destructor); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7342 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 7343 | AddOverriddenMethods(ClassDecl, Destructor); |
| 7344 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 7345 | if (ShouldDeleteSpecialMember(Destructor, CXXDestructor)) |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 7346 | Destructor->setDeletedAsWritten(); |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 7347 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 7348 | return Destructor; |
| 7349 | } |
| 7350 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7351 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 7352 | CXXDestructorDecl *Destructor) { |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 7353 | assert((Destructor->isDefaulted() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 7354 | !Destructor->doesThisDeclarationHaveABody() && |
| 7355 | !Destructor->isDeleted()) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7356 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 7357 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7358 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7359 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7360 | if (Destructor->isInvalidDecl()) |
| 7361 | return; |
| 7362 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 7363 | SynthesizedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7364 | |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 7365 | DiagnosticErrorTrap Trap(Diags); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 7366 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 7367 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7368 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7369 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 7370 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7371 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 7372 | |
| 7373 | Destructor->setInvalidDecl(); |
| 7374 | return; |
| 7375 | } |
| 7376 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7377 | SourceLocation Loc = Destructor->getLocation(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 7378 | Destructor->setBody(new (Context) CompoundStmt(Loc)); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame] | 7379 | Destructor->setImplicitlyDefined(true); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7380 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7381 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7382 | |
| 7383 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7384 | L->CompletedImplicitDefinition(Destructor); |
| 7385 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7386 | } |
| 7387 | |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 7388 | /// \brief Perform any semantic analysis which needs to be delayed until all |
| 7389 | /// pending class member declarations have been parsed. |
| 7390 | void Sema::ActOnFinishCXXMemberDecls() { |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 7391 | // Perform any deferred checking of exception specifications for virtual |
| 7392 | // destructors. |
| 7393 | for (unsigned i = 0, e = DelayedDestructorExceptionSpecChecks.size(); |
| 7394 | i != e; ++i) { |
| 7395 | const CXXDestructorDecl *Dtor = |
| 7396 | DelayedDestructorExceptionSpecChecks[i].first; |
| 7397 | assert(!Dtor->getParent()->isDependentType() && |
| 7398 | "Should not ever add destructors of templates into the list."); |
| 7399 | CheckOverridingFunctionExceptionSpec(Dtor, |
| 7400 | DelayedDestructorExceptionSpecChecks[i].second); |
| 7401 | } |
| 7402 | DelayedDestructorExceptionSpecChecks.clear(); |
| 7403 | } |
| 7404 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7405 | void Sema::AdjustDestructorExceptionSpec(CXXRecordDecl *ClassDecl, |
| 7406 | CXXDestructorDecl *Destructor) { |
| 7407 | assert(getLangOpts().CPlusPlus0x && |
| 7408 | "adjusting dtor exception specs was introduced in c++11"); |
| 7409 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7410 | // C++11 [class.dtor]p3: |
| 7411 | // A declaration of a destructor that does not have an exception- |
| 7412 | // specification is implicitly considered to have the same exception- |
| 7413 | // specification as an implicit declaration. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7414 | const FunctionProtoType *DtorType = Destructor->getType()-> |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7415 | getAs<FunctionProtoType>(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7416 | if (DtorType->hasExceptionSpec()) |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7417 | return; |
| 7418 | |
Chandler Carruth | 3f224b2 | 2011-09-20 04:55:26 +0000 | [diff] [blame] | 7419 | // Replace the destructor's type, building off the existing one. Fortunately, |
| 7420 | // the only thing of interest in the destructor type is its extended info. |
| 7421 | // The return and arguments are fixed. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7422 | FunctionProtoType::ExtProtoInfo EPI = DtorType->getExtProtoInfo(); |
| 7423 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 7424 | EPI.ExceptionSpecDecl = Destructor; |
| 7425 | Destructor->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 7426 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7427 | // FIXME: If the destructor has a body that could throw, and the newly created |
| 7428 | // spec doesn't allow exceptions, we should emit a warning, because this |
| 7429 | // change in behavior can break conforming C++03 programs at runtime. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7430 | // However, we don't have a body or an exception specification yet, so it |
| 7431 | // needs to be done somewhere else. |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 7432 | } |
| 7433 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7434 | /// When generating a defaulted copy or move assignment operator, if a field |
| 7435 | /// should be copied with __builtin_memcpy rather than via explicit assignments, |
| 7436 | /// do so. This optimization only applies for arrays of scalars, and for arrays |
| 7437 | /// of class type where the selected copy/move-assignment operator is trivial. |
| 7438 | static StmtResult |
| 7439 | buildMemcpyForAssignmentOp(Sema &S, SourceLocation Loc, QualType T, |
| 7440 | Expr *To, Expr *From) { |
| 7441 | // Compute the size of the memory buffer to be copied. |
| 7442 | QualType SizeType = S.Context.getSizeType(); |
| 7443 | llvm::APInt Size(S.Context.getTypeSize(SizeType), |
| 7444 | S.Context.getTypeSizeInChars(T).getQuantity()); |
| 7445 | |
| 7446 | // Take the address of the field references for "from" and "to". We |
| 7447 | // directly construct UnaryOperators here because semantic analysis |
| 7448 | // does not permit us to take the address of an xvalue. |
| 7449 | From = new (S.Context) UnaryOperator(From, UO_AddrOf, |
| 7450 | S.Context.getPointerType(From->getType()), |
| 7451 | VK_RValue, OK_Ordinary, Loc); |
| 7452 | To = new (S.Context) UnaryOperator(To, UO_AddrOf, |
| 7453 | S.Context.getPointerType(To->getType()), |
| 7454 | VK_RValue, OK_Ordinary, Loc); |
| 7455 | |
| 7456 | const Type *E = T->getBaseElementTypeUnsafe(); |
| 7457 | bool NeedsCollectableMemCpy = |
| 7458 | E->isRecordType() && E->getAs<RecordType>()->getDecl()->hasObjectMember(); |
| 7459 | |
| 7460 | // Create a reference to the __builtin_objc_memmove_collectable function |
| 7461 | StringRef MemCpyName = NeedsCollectableMemCpy ? |
| 7462 | "__builtin_objc_memmove_collectable" : |
| 7463 | "__builtin_memcpy"; |
| 7464 | LookupResult R(S, &S.Context.Idents.get(MemCpyName), Loc, |
| 7465 | Sema::LookupOrdinaryName); |
| 7466 | S.LookupName(R, S.TUScope, true); |
| 7467 | |
| 7468 | FunctionDecl *MemCpy = R.getAsSingle<FunctionDecl>(); |
| 7469 | if (!MemCpy) |
| 7470 | // Something went horribly wrong earlier, and we will have complained |
| 7471 | // about it. |
| 7472 | return StmtError(); |
| 7473 | |
| 7474 | ExprResult MemCpyRef = S.BuildDeclRefExpr(MemCpy, S.Context.BuiltinFnTy, |
| 7475 | VK_RValue, Loc, 0); |
| 7476 | assert(MemCpyRef.isUsable() && "Builtin reference cannot fail"); |
| 7477 | |
| 7478 | Expr *CallArgs[] = { |
| 7479 | To, From, IntegerLiteral::Create(S.Context, Size, SizeType, Loc) |
| 7480 | }; |
| 7481 | ExprResult Call = S.ActOnCallExpr(/*Scope=*/0, MemCpyRef.take(), |
| 7482 | Loc, CallArgs, Loc); |
| 7483 | |
| 7484 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 7485 | return S.Owned(Call.takeAs<Stmt>()); |
| 7486 | } |
| 7487 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7488 | /// \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] | 7489 | /// \c To. |
| 7490 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7491 | /// This routine is used to copy/move the members of a class with an |
| 7492 | /// implicitly-declared copy/move assignment operator. When the entities being |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7493 | /// copied are arrays, this routine builds for loops to copy them. |
| 7494 | /// |
| 7495 | /// \param S The Sema object used for type-checking. |
| 7496 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7497 | /// \param Loc The location where the implicit copy/move is being generated. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7498 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7499 | /// \param T The type of the expressions being copied/moved. Both expressions |
| 7500 | /// must have this type. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7501 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7502 | /// \param To The expression we are copying/moving to. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7503 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7504 | /// \param From The expression we are copying/moving from. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7505 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7506 | /// \param CopyingBaseSubobject Whether we're copying/moving a base subobject. |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 7507 | /// Otherwise, it's a non-static member subobject. |
| 7508 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7509 | /// \param Copying Whether we're copying or moving. |
| 7510 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7511 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 7512 | /// |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7513 | /// \returns A statement or a loop that copies the expressions, or StmtResult(0) |
| 7514 | /// if a memcpy should be used instead. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7515 | static StmtResult |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7516 | buildSingleCopyAssignRecursively(Sema &S, SourceLocation Loc, QualType T, |
| 7517 | Expr *To, Expr *From, |
| 7518 | bool CopyingBaseSubobject, bool Copying, |
| 7519 | unsigned Depth = 0) { |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7520 | // C++11 [class.copy]p28: |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7521 | // Each subobject is assigned in the manner appropriate to its type: |
| 7522 | // |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7523 | // - if the subobject is of class type, as if by a call to operator= with |
| 7524 | // the subobject as the object expression and the corresponding |
| 7525 | // subobject of x as a single function argument (as if by explicit |
| 7526 | // qualification; that is, ignoring any possible virtual overriding |
| 7527 | // functions in more derived classes); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7528 | // |
| 7529 | // C++03 [class.copy]p13: |
| 7530 | // - if the subobject is of class type, the copy assignment operator for |
| 7531 | // the class is used (as if by explicit qualification; that is, |
| 7532 | // ignoring any possible virtual overriding functions in more derived |
| 7533 | // classes); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7534 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 7535 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7536 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7537 | // Look for operator=. |
| 7538 | DeclarationName Name |
| 7539 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 7540 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 7541 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7542 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7543 | // Prior to C++11, filter out any result that isn't a copy/move-assignment |
| 7544 | // operator. |
| 7545 | if (!S.getLangOpts().CPlusPlus0x) { |
| 7546 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 7547 | while (F.hasNext()) { |
| 7548 | NamedDecl *D = F.next(); |
| 7549 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 7550 | if (Method->isCopyAssignmentOperator() || |
| 7551 | (!Copying && Method->isMoveAssignmentOperator())) |
| 7552 | continue; |
| 7553 | |
| 7554 | F.erase(); |
| 7555 | } |
| 7556 | F.done(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 7557 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7558 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 7559 | // Suppress the protected check (C++ [class.protected]) for each of the |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7560 | // assignment operators we found. This strange dance is required when |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 7561 | // we're assigning via a base classes's copy-assignment operator. To |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7562 | // ensure that we're getting the right base class subobject (without |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 7563 | // ambiguities), we need to cast "this" to that subobject type; to |
| 7564 | // ensure that we don't go through the virtual call mechanism, we need |
| 7565 | // to qualify the operator= name with the base class (see below). However, |
| 7566 | // this means that if the base class has a protected copy assignment |
| 7567 | // operator, the protected member access check will fail. So, we |
| 7568 | // rewrite "protected" access to "public" access in this case, since we |
| 7569 | // know by construction that we're calling from a derived class. |
| 7570 | if (CopyingBaseSubobject) { |
| 7571 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 7572 | L != LEnd; ++L) { |
| 7573 | if (L.getAccess() == AS_protected) |
| 7574 | L.setAccess(AS_public); |
| 7575 | } |
| 7576 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7577 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7578 | // Create the nested-name-specifier that will be used to qualify the |
| 7579 | // reference to operator=; this is required to suppress the virtual |
| 7580 | // call mechanism. |
| 7581 | CXXScopeSpec SS; |
Manuel Klimek | 5b6a3dd | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 7582 | const Type *CanonicalT = S.Context.getCanonicalType(T.getTypePtr()); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7583 | SS.MakeTrivial(S.Context, |
| 7584 | NestedNameSpecifier::Create(S.Context, 0, false, |
Manuel Klimek | 5b6a3dd | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 7585 | CanonicalT), |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 7586 | Loc); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7587 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7588 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7589 | ExprResult OpEqualRef |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7590 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 7591 | /*TemplateKWLoc=*/SourceLocation(), |
| 7592 | /*FirstQualifierInScope=*/0, |
| 7593 | OpLookup, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7594 | /*TemplateArgs=*/0, |
| 7595 | /*SuppressQualifierCheck=*/true); |
| 7596 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7597 | return StmtError(); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7598 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7599 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7600 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7601 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7602 | OpEqualRef.takeAs<Expr>(), |
| 7603 | Loc, &From, 1, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7604 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7605 | return StmtError(); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7606 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7607 | // If we built a call to a trivial 'operator=' while copying an array, |
| 7608 | // bail out. We'll replace the whole shebang with a memcpy. |
| 7609 | CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Call.get()); |
| 7610 | if (CE && CE->getMethodDecl()->isTrivial() && Depth) |
| 7611 | return StmtResult((Stmt*)0); |
| 7612 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7613 | // Convert to an expression-statement, and clean up any produced |
| 7614 | // temporaries. |
| 7615 | return S.ActOnExprStmt(S.MakeFullExpr(Call.take(), Loc)); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7616 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 7617 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7618 | // - if the subobject is of scalar type, the built-in assignment |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7619 | // operator is used. |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7620 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7621 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7622 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7623 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7624 | return StmtError(); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7625 | return S.ActOnExprStmt(S.MakeFullExpr(Assignment.take(), Loc)); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7626 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7627 | |
| 7628 | // - if the subobject is an array, each element is assigned, in the |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7629 | // manner appropriate to the element type; |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 7630 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7631 | // Construct a loop over the array bounds, e.g., |
| 7632 | // |
| 7633 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 7634 | // |
| 7635 | // that will copy each of the array elements. |
| 7636 | QualType SizeType = S.Context.getSizeType(); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7637 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7638 | // Create the iteration variable. |
| 7639 | IdentifierInfo *IterationVarName = 0; |
| 7640 | { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 7641 | SmallString<8> Str; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7642 | llvm::raw_svector_ostream OS(Str); |
| 7643 | OS << "__i" << Depth; |
| 7644 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 7645 | } |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7646 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7647 | IterationVarName, SizeType, |
| 7648 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7649 | SC_None, SC_None); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7650 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7651 | // Initialize the iteration variable to zero. |
| 7652 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 7653 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7654 | |
| 7655 | // Create a reference to the iteration variable; we'll use this several |
| 7656 | // times throughout. |
| 7657 | Expr *IterationVarRef |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 7658 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7659 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 7660 | Expr *IterationVarRefRVal = S.DefaultLvalueConversion(IterationVarRef).take(); |
| 7661 | assert(IterationVarRefRVal && "Conversion of invented variable cannot fail!"); |
| 7662 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7663 | // Create the DeclStmt that holds the iteration variable. |
| 7664 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7665 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7666 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7667 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 7668 | IterationVarRefRVal, |
| 7669 | Loc)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7670 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 7671 | IterationVarRefRVal, |
| 7672 | Loc)); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7673 | if (!Copying) // Cast to rvalue |
| 7674 | From = CastForMoving(S, From); |
| 7675 | |
| 7676 | // Build the copy/move for an individual element of the array. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7677 | StmtResult Copy = |
| 7678 | buildSingleCopyAssignRecursively(S, Loc, ArrayTy->getElementType(), |
| 7679 | To, From, CopyingBaseSubobject, |
| 7680 | Copying, Depth + 1); |
| 7681 | // Bail out if copying fails or if we determined that we should use memcpy. |
| 7682 | if (Copy.isInvalid() || !Copy.get()) |
| 7683 | return Copy; |
| 7684 | |
| 7685 | // Create the comparison against the array bound. |
| 7686 | llvm::APInt Upper |
| 7687 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
| 7688 | Expr *Comparison |
| 7689 | = new (S.Context) BinaryOperator(IterationVarRefRVal, |
| 7690 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 7691 | BO_NE, S.Context.BoolTy, |
| 7692 | VK_RValue, OK_Ordinary, Loc, false); |
| 7693 | |
| 7694 | // Create the pre-increment of the iteration variable. |
| 7695 | Expr *Increment |
| 7696 | = new (S.Context) UnaryOperator(IterationVarRef, UO_PreInc, SizeType, |
| 7697 | VK_LValue, OK_Ordinary, Loc); |
| 7698 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7699 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7700 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7701 | S.MakeFullExpr(Comparison), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7702 | 0, S.MakeFullExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7703 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7704 | } |
| 7705 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7706 | static StmtResult |
| 7707 | buildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
| 7708 | Expr *To, Expr *From, |
| 7709 | bool CopyingBaseSubobject, bool Copying) { |
| 7710 | // Maybe we should use a memcpy? |
| 7711 | if (T->isArrayType() && !T.isConstQualified() && !T.isVolatileQualified() && |
| 7712 | T.isTriviallyCopyableType(S.Context)) |
| 7713 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 7714 | |
| 7715 | StmtResult Result(buildSingleCopyAssignRecursively(S, Loc, T, To, From, |
| 7716 | CopyingBaseSubobject, |
| 7717 | Copying, 0)); |
| 7718 | |
| 7719 | // If we ended up picking a trivial assignment operator for an array of a |
| 7720 | // non-trivially-copyable class type, just emit a memcpy. |
| 7721 | if (!Result.isInvalid() && !Result.get()) |
| 7722 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 7723 | |
| 7724 | return Result; |
| 7725 | } |
| 7726 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7727 | Sema::ImplicitExceptionSpecification |
| 7728 | Sema::ComputeDefaultedCopyAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 7729 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 7730 | |
| 7731 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 7732 | if (ClassDecl->isInvalidDecl()) |
| 7733 | return ExceptSpec; |
| 7734 | |
| 7735 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
| 7736 | assert(T->getNumArgs() == 1 && "not a copy assignment op"); |
| 7737 | unsigned ArgQuals = T->getArgType(0).getNonReferenceType().getCVRQualifiers(); |
| 7738 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7739 | // C++ [except.spec]p14: |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7740 | // An implicitly declared special member function (Clause 12) shall have an |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7741 | // exception-specification. [...] |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7742 | |
| 7743 | // It is unspecified whether or not an implicit copy assignment operator |
| 7744 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 7745 | // made. As such, this exception specification is effectively unspecified. |
| 7746 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 7747 | // the side of assuming such calls to be made regardless of whether they |
| 7748 | // actually happen. |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7749 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7750 | BaseEnd = ClassDecl->bases_end(); |
| 7751 | Base != BaseEnd; ++Base) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7752 | if (Base->isVirtual()) |
| 7753 | continue; |
| 7754 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 7755 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7756 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7757 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 7758 | ArgQuals, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7759 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyAssign); |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7760 | } |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7761 | |
| 7762 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 7763 | BaseEnd = ClassDecl->vbases_end(); |
| 7764 | Base != BaseEnd; ++Base) { |
| 7765 | CXXRecordDecl *BaseClassDecl |
| 7766 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 7767 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 7768 | ArgQuals, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7769 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyAssign); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7770 | } |
| 7771 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7772 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7773 | FieldEnd = ClassDecl->field_end(); |
| 7774 | Field != FieldEnd; |
| 7775 | ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7776 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7777 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 7778 | if (CXXMethodDecl *CopyAssign = |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 7779 | LookupCopyingAssignment(FieldClassDecl, |
| 7780 | ArgQuals | FieldType.getCVRQualifiers(), |
| 7781 | false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7782 | ExceptSpec.CalledDecl(Field->getLocation(), CopyAssign); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 7783 | } |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 7784 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7785 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7786 | return ExceptSpec; |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 7787 | } |
| 7788 | |
| 7789 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
| 7790 | // Note: The following rules are largely analoguous to the copy |
| 7791 | // constructor rules. Note that virtual bases are not taken into account |
| 7792 | // for determining the argument type of the operator. Note also that |
| 7793 | // operators taking an object instead of a reference are allowed. |
Richard Smith | d0adeb6 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 7794 | assert(!ClassDecl->hasDeclaredCopyAssignment()); |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 7795 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 7796 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 7797 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame^] | 7798 | if (ClassDecl->implicitCopyAssignmentHasConstParam()) |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 7799 | ArgType = ArgType.withConst(); |
| 7800 | ArgType = Context.getLValueReferenceType(ArgType); |
| 7801 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7802 | // An implicitly-declared copy assignment operator is an inline public |
| 7803 | // member of its class. |
| 7804 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7805 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 7806 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7807 | CXXMethodDecl *CopyAssignment |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7808 | = CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7809 | /*TInfo=*/0, /*isStatic=*/false, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7810 | /*StorageClassAsWritten=*/SC_None, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 7811 | /*isInline=*/true, /*isConstexpr=*/false, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 7812 | SourceLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7813 | CopyAssignment->setAccess(AS_public); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 7814 | CopyAssignment->setDefaulted(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7815 | CopyAssignment->setImplicit(); |
| 7816 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7817 | |
| 7818 | // Build an exception specification pointing back at this member. |
| 7819 | FunctionProtoType::ExtProtoInfo EPI; |
| 7820 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 7821 | EPI.ExceptionSpecDecl = CopyAssignment; |
| 7822 | CopyAssignment->setType(Context.getFunctionType(RetType, &ArgType, 1, EPI)); |
| 7823 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7824 | // Add the parameter to the operator. |
| 7825 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7826 | ClassLoc, ClassLoc, /*Id=*/0, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7827 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7828 | SC_None, |
| 7829 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 7830 | CopyAssignment->setParams(FromParam); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7831 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 7832 | // Note that we have added this copy-assignment operator. |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 7833 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 7834 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 7835 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 7836 | PushOnScopeChains(CopyAssignment, S, false); |
| 7837 | ClassDecl->addDecl(CopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7838 | |
Nico Weber | afcc96a | 2012-01-23 03:19:29 +0000 | [diff] [blame] | 7839 | // C++0x [class.copy]p19: |
| 7840 | // .... If the class definition does not explicitly declare a copy |
| 7841 | // assignment operator, there is no user-declared move constructor, and |
| 7842 | // there is no user-declared move assignment operator, a copy assignment |
| 7843 | // operator is implicitly declared as defaulted. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 7844 | if (ShouldDeleteSpecialMember(CopyAssignment, CXXCopyAssignment)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 7845 | CopyAssignment->setDeletedAsWritten(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 7846 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 7847 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 7848 | return CopyAssignment; |
| 7849 | } |
| 7850 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7851 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 7852 | CXXMethodDecl *CopyAssignOperator) { |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 7853 | assert((CopyAssignOperator->isDefaulted() && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7854 | CopyAssignOperator->isOverloadedOperator() && |
| 7855 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 7856 | !CopyAssignOperator->doesThisDeclarationHaveABody() && |
| 7857 | !CopyAssignOperator->isDeleted()) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7858 | "DefineImplicitCopyAssignment called for wrong function"); |
| 7859 | |
| 7860 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 7861 | |
| 7862 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 7863 | CopyAssignOperator->setInvalidDecl(); |
| 7864 | return; |
| 7865 | } |
| 7866 | |
| 7867 | CopyAssignOperator->setUsed(); |
| 7868 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 7869 | SynthesizedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 7870 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7871 | |
| 7872 | // C++0x [class.copy]p30: |
| 7873 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 7874 | // for a non-union class X performs memberwise copy assignment of its |
| 7875 | // subobjects. The direct base classes of X are assigned first, in the |
| 7876 | // order of their declaration in the base-specifier-list, and then the |
| 7877 | // immediate non-static data members of X are assigned, in the order in |
| 7878 | // which they were declared in the class definition. |
| 7879 | |
| 7880 | // The statements that form the synthesized function body. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 7881 | SmallVector<Stmt*, 8> Statements; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7882 | |
| 7883 | // The parameter for the "other" object, which we are copying from. |
| 7884 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 7885 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 7886 | QualType OtherRefType = Other->getType(); |
| 7887 | if (const LValueReferenceType *OtherRef |
| 7888 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 7889 | OtherRefType = OtherRef->getPointeeType(); |
| 7890 | OtherQuals = OtherRefType.getQualifiers(); |
| 7891 | } |
| 7892 | |
| 7893 | // Our location for everything implicitly-generated. |
| 7894 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 7895 | |
| 7896 | // Construct a reference to the "other" object. We'll be using this |
| 7897 | // throughout the generated ASTs. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7898 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7899 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 7900 | |
| 7901 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 7902 | // ASTs. |
| 7903 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 7904 | assert(This && "Reference to this cannot fail!"); |
| 7905 | |
| 7906 | // Assign base classes. |
| 7907 | bool Invalid = false; |
| 7908 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7909 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 7910 | // Form the assignment: |
| 7911 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 7912 | QualType BaseType = Base->getType().getUnqualifiedType(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 7913 | if (!BaseType->isRecordType()) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7914 | Invalid = true; |
| 7915 | continue; |
| 7916 | } |
| 7917 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 7918 | CXXCastPath BasePath; |
| 7919 | BasePath.push_back(Base); |
| 7920 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7921 | // Construct the "from" expression, which is an implicit cast to the |
| 7922 | // appropriately-qualified base type. |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 7923 | Expr *From = OtherRef; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7924 | From = ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
| 7925 | CK_UncheckedDerivedToBase, |
| 7926 | VK_LValue, &BasePath).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7927 | |
| 7928 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7929 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7930 | |
| 7931 | // Implicitly cast "this" to the appropriately-qualified base type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7932 | To = ImpCastExprToType(To.take(), |
| 7933 | Context.getCVRQualifiedType(BaseType, |
| 7934 | CopyAssignOperator->getTypeQualifiers()), |
| 7935 | CK_UncheckedDerivedToBase, |
| 7936 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7937 | |
| 7938 | // Build the copy. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 7939 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 7940 | To.get(), From, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 7941 | /*CopyingBaseSubobject=*/true, |
| 7942 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7943 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7944 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7945 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 7946 | CopyAssignOperator->setInvalidDecl(); |
| 7947 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7948 | } |
| 7949 | |
| 7950 | // Success! Record the copy. |
| 7951 | Statements.push_back(Copy.takeAs<Expr>()); |
| 7952 | } |
| 7953 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7954 | // Assign non-static members. |
| 7955 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7956 | FieldEnd = ClassDecl->field_end(); |
| 7957 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 7958 | if (Field->isUnnamedBitfield()) |
| 7959 | continue; |
| 7960 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7961 | // Check for members of reference type; we can't copy those. |
| 7962 | if (Field->getType()->isReferenceType()) { |
| 7963 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 7964 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 7965 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7966 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7967 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7968 | Invalid = true; |
| 7969 | continue; |
| 7970 | } |
| 7971 | |
| 7972 | // Check for members of const-qualified, non-class type. |
| 7973 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 7974 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 7975 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 7976 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 7977 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7978 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7979 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7980 | Invalid = true; |
| 7981 | continue; |
| 7982 | } |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 7983 | |
| 7984 | // Suppress assigning zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 7985 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 7986 | continue; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7987 | |
| 7988 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 7989 | if (FieldType->isIncompleteArrayType()) { |
| 7990 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 7991 | "Incomplete array type is not valid"); |
| 7992 | continue; |
| 7993 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7994 | |
| 7995 | // Build references to the field in the object we're copying from and to. |
| 7996 | CXXScopeSpec SS; // Intentionally empty |
| 7997 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 7998 | LookupMemberName); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 7999 | MemberLookup.addDecl(*Field); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8000 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8001 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8002 | Loc, /*IsArrow=*/false, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8003 | SS, SourceLocation(), 0, |
| 8004 | MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8005 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8006 | Loc, /*IsArrow=*/true, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8007 | SS, SourceLocation(), 0, |
| 8008 | MemberLookup, 0); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8009 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 8010 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8011 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8012 | // Build the copy of this field. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8013 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, FieldType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8014 | To.get(), From.get(), |
| 8015 | /*CopyingBaseSubobject=*/false, |
| 8016 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8017 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 8018 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8019 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 8020 | CopyAssignOperator->setInvalidDecl(); |
| 8021 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8022 | } |
| 8023 | |
| 8024 | // Success! Record the copy. |
| 8025 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 8026 | } |
| 8027 | |
| 8028 | if (!Invalid) { |
| 8029 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8030 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8031 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8032 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8033 | if (Return.isInvalid()) |
| 8034 | Invalid = true; |
| 8035 | else { |
| 8036 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8037 | |
| 8038 | if (Trap.hasErrorOccurred()) { |
| 8039 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8040 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 8041 | Invalid = true; |
| 8042 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8043 | } |
| 8044 | } |
| 8045 | |
| 8046 | if (Invalid) { |
| 8047 | CopyAssignOperator->setInvalidDecl(); |
| 8048 | return; |
| 8049 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8050 | |
| 8051 | StmtResult Body; |
| 8052 | { |
| 8053 | CompoundScopeRAII CompoundScope(*this); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8054 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8055 | /*isStmtExpr=*/false); |
| 8056 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 8057 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8058 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 8059 | |
| 8060 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8061 | L->CompletedImplicitDefinition(CopyAssignOperator); |
| 8062 | } |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8063 | } |
| 8064 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8065 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8066 | Sema::ComputeDefaultedMoveAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 8067 | CXXRecordDecl *ClassDecl = MD->getParent(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8068 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8069 | ImplicitExceptionSpecification ExceptSpec(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8070 | if (ClassDecl->isInvalidDecl()) |
| 8071 | return ExceptSpec; |
| 8072 | |
| 8073 | // C++0x [except.spec]p14: |
| 8074 | // An implicitly declared special member function (Clause 12) shall have an |
| 8075 | // exception-specification. [...] |
| 8076 | |
| 8077 | // It is unspecified whether or not an implicit move assignment operator |
| 8078 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 8079 | // made. As such, this exception specification is effectively unspecified. |
| 8080 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 8081 | // the side of assuming such calls to be made regardless of whether they |
| 8082 | // actually happen. |
| 8083 | // Note that a move constructor is not implicitly declared when there are |
| 8084 | // virtual bases, but it can still be user-declared and explicitly defaulted. |
| 8085 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8086 | BaseEnd = ClassDecl->bases_end(); |
| 8087 | Base != BaseEnd; ++Base) { |
| 8088 | if (Base->isVirtual()) |
| 8089 | continue; |
| 8090 | |
| 8091 | CXXRecordDecl *BaseClassDecl |
| 8092 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 8093 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8094 | 0, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8095 | ExceptSpec.CalledDecl(Base->getLocStart(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8096 | } |
| 8097 | |
| 8098 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8099 | BaseEnd = ClassDecl->vbases_end(); |
| 8100 | Base != BaseEnd; ++Base) { |
| 8101 | CXXRecordDecl *BaseClassDecl |
| 8102 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 8103 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8104 | 0, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8105 | ExceptSpec.CalledDecl(Base->getLocStart(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8106 | } |
| 8107 | |
| 8108 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8109 | FieldEnd = ClassDecl->field_end(); |
| 8110 | Field != FieldEnd; |
| 8111 | ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 8112 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8113 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8114 | if (CXXMethodDecl *MoveAssign = |
| 8115 | LookupMovingAssignment(FieldClassDecl, |
| 8116 | FieldType.getCVRQualifiers(), |
| 8117 | false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8118 | ExceptSpec.CalledDecl(Field->getLocation(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8119 | } |
| 8120 | } |
| 8121 | |
| 8122 | return ExceptSpec; |
| 8123 | } |
| 8124 | |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8125 | /// Determine whether the class type has any direct or indirect virtual base |
| 8126 | /// classes which have a non-trivial move assignment operator. |
| 8127 | static bool |
| 8128 | hasVirtualBaseWithNonTrivialMoveAssignment(Sema &S, CXXRecordDecl *ClassDecl) { |
| 8129 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8130 | BaseEnd = ClassDecl->vbases_end(); |
| 8131 | Base != BaseEnd; ++Base) { |
| 8132 | CXXRecordDecl *BaseClass = |
| 8133 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 8134 | |
| 8135 | // Try to declare the move assignment. If it would be deleted, then the |
| 8136 | // class does not have a non-trivial move assignment. |
| 8137 | if (BaseClass->needsImplicitMoveAssignment()) |
| 8138 | S.DeclareImplicitMoveAssignment(BaseClass); |
| 8139 | |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 8140 | if (BaseClass->hasNonTrivialMoveAssignment()) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8141 | return true; |
| 8142 | } |
| 8143 | |
| 8144 | return false; |
| 8145 | } |
| 8146 | |
| 8147 | /// Determine whether the given type either has a move constructor or is |
| 8148 | /// trivially copyable. |
| 8149 | static bool |
| 8150 | hasMoveOrIsTriviallyCopyable(Sema &S, QualType Type, bool IsConstructor) { |
| 8151 | Type = S.Context.getBaseElementType(Type); |
| 8152 | |
| 8153 | // FIXME: Technically, non-trivially-copyable non-class types, such as |
| 8154 | // reference types, are supposed to return false here, but that appears |
| 8155 | // to be a standard defect. |
| 8156 | CXXRecordDecl *ClassDecl = Type->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | b5e4ace | 2012-10-10 16:14:06 +0000 | [diff] [blame] | 8157 | if (!ClassDecl || !ClassDecl->getDefinition() || ClassDecl->isInvalidDecl()) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8158 | return true; |
| 8159 | |
| 8160 | if (Type.isTriviallyCopyableType(S.Context)) |
| 8161 | return true; |
| 8162 | |
| 8163 | if (IsConstructor) { |
| 8164 | if (ClassDecl->needsImplicitMoveConstructor()) |
| 8165 | S.DeclareImplicitMoveConstructor(ClassDecl); |
| 8166 | return ClassDecl->hasDeclaredMoveConstructor(); |
| 8167 | } |
| 8168 | |
| 8169 | if (ClassDecl->needsImplicitMoveAssignment()) |
| 8170 | S.DeclareImplicitMoveAssignment(ClassDecl); |
| 8171 | return ClassDecl->hasDeclaredMoveAssignment(); |
| 8172 | } |
| 8173 | |
| 8174 | /// Determine whether all non-static data members and direct or virtual bases |
| 8175 | /// of class \p ClassDecl have either a move operation, or are trivially |
| 8176 | /// copyable. |
| 8177 | static bool subobjectsHaveMoveOrTrivialCopy(Sema &S, CXXRecordDecl *ClassDecl, |
| 8178 | bool IsConstructor) { |
| 8179 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8180 | BaseEnd = ClassDecl->bases_end(); |
| 8181 | Base != BaseEnd; ++Base) { |
| 8182 | if (Base->isVirtual()) |
| 8183 | continue; |
| 8184 | |
| 8185 | if (!hasMoveOrIsTriviallyCopyable(S, Base->getType(), IsConstructor)) |
| 8186 | return false; |
| 8187 | } |
| 8188 | |
| 8189 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8190 | BaseEnd = ClassDecl->vbases_end(); |
| 8191 | Base != BaseEnd; ++Base) { |
| 8192 | if (!hasMoveOrIsTriviallyCopyable(S, Base->getType(), IsConstructor)) |
| 8193 | return false; |
| 8194 | } |
| 8195 | |
| 8196 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8197 | FieldEnd = ClassDecl->field_end(); |
| 8198 | Field != FieldEnd; ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 8199 | if (!hasMoveOrIsTriviallyCopyable(S, Field->getType(), IsConstructor)) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8200 | return false; |
| 8201 | } |
| 8202 | |
| 8203 | return true; |
| 8204 | } |
| 8205 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8206 | CXXMethodDecl *Sema::DeclareImplicitMoveAssignment(CXXRecordDecl *ClassDecl) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8207 | // C++11 [class.copy]p20: |
| 8208 | // If the definition of a class X does not explicitly declare a move |
| 8209 | // assignment operator, one will be implicitly declared as defaulted |
| 8210 | // if and only if: |
| 8211 | // |
| 8212 | // - [first 4 bullets] |
| 8213 | assert(ClassDecl->needsImplicitMoveAssignment()); |
| 8214 | |
| 8215 | // [Checked after we build the declaration] |
| 8216 | // - the move assignment operator would not be implicitly defined as |
| 8217 | // deleted, |
| 8218 | |
| 8219 | // [DR1402]: |
| 8220 | // - X has no direct or indirect virtual base class with a non-trivial |
| 8221 | // move assignment operator, and |
| 8222 | // - each of X's non-static data members and direct or virtual base classes |
| 8223 | // has a type that either has a move assignment operator or is trivially |
| 8224 | // copyable. |
| 8225 | if (hasVirtualBaseWithNonTrivialMoveAssignment(*this, ClassDecl) || |
| 8226 | !subobjectsHaveMoveOrTrivialCopy(*this, ClassDecl,/*Constructor*/false)) { |
| 8227 | ClassDecl->setFailedImplicitMoveAssignment(); |
| 8228 | return 0; |
| 8229 | } |
| 8230 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8231 | // Note: The following rules are largely analoguous to the move |
| 8232 | // constructor rules. |
| 8233 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8234 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 8235 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 8236 | ArgType = Context.getRValueReferenceType(ArgType); |
| 8237 | |
| 8238 | // An implicitly-declared move assignment operator is an inline public |
| 8239 | // member of its class. |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8240 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 8241 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 8242 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 8243 | CXXMethodDecl *MoveAssignment |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8244 | = CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8245 | /*TInfo=*/0, /*isStatic=*/false, |
| 8246 | /*StorageClassAsWritten=*/SC_None, |
| 8247 | /*isInline=*/true, |
| 8248 | /*isConstexpr=*/false, |
| 8249 | SourceLocation()); |
| 8250 | MoveAssignment->setAccess(AS_public); |
| 8251 | MoveAssignment->setDefaulted(); |
| 8252 | MoveAssignment->setImplicit(); |
| 8253 | MoveAssignment->setTrivial(ClassDecl->hasTrivialMoveAssignment()); |
| 8254 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8255 | // Build an exception specification pointing back at this member. |
| 8256 | FunctionProtoType::ExtProtoInfo EPI; |
| 8257 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8258 | EPI.ExceptionSpecDecl = MoveAssignment; |
| 8259 | MoveAssignment->setType(Context.getFunctionType(RetType, &ArgType, 1, EPI)); |
| 8260 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8261 | // Add the parameter to the operator. |
| 8262 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveAssignment, |
| 8263 | ClassLoc, ClassLoc, /*Id=*/0, |
| 8264 | ArgType, /*TInfo=*/0, |
| 8265 | SC_None, |
| 8266 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8267 | MoveAssignment->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8268 | |
| 8269 | // Note that we have added this copy-assignment operator. |
| 8270 | ++ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
| 8271 | |
| 8272 | // C++0x [class.copy]p9: |
| 8273 | // If the definition of a class X does not explicitly declare a move |
| 8274 | // assignment operator, one will be implicitly declared as defaulted if and |
| 8275 | // only if: |
| 8276 | // [...] |
| 8277 | // - the move assignment operator would not be implicitly defined as |
| 8278 | // deleted. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 8279 | if (ShouldDeleteSpecialMember(MoveAssignment, CXXMoveAssignment)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8280 | // Cache this result so that we don't try to generate this over and over |
| 8281 | // on every lookup, leaking memory and wasting time. |
| 8282 | ClassDecl->setFailedImplicitMoveAssignment(); |
| 8283 | return 0; |
| 8284 | } |
| 8285 | |
| 8286 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 8287 | PushOnScopeChains(MoveAssignment, S, false); |
| 8288 | ClassDecl->addDecl(MoveAssignment); |
| 8289 | |
| 8290 | AddOverriddenMethods(ClassDecl, MoveAssignment); |
| 8291 | return MoveAssignment; |
| 8292 | } |
| 8293 | |
| 8294 | void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation, |
| 8295 | CXXMethodDecl *MoveAssignOperator) { |
| 8296 | assert((MoveAssignOperator->isDefaulted() && |
| 8297 | MoveAssignOperator->isOverloadedOperator() && |
| 8298 | MoveAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8299 | !MoveAssignOperator->doesThisDeclarationHaveABody() && |
| 8300 | !MoveAssignOperator->isDeleted()) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8301 | "DefineImplicitMoveAssignment called for wrong function"); |
| 8302 | |
| 8303 | CXXRecordDecl *ClassDecl = MoveAssignOperator->getParent(); |
| 8304 | |
| 8305 | if (ClassDecl->isInvalidDecl() || MoveAssignOperator->isInvalidDecl()) { |
| 8306 | MoveAssignOperator->setInvalidDecl(); |
| 8307 | return; |
| 8308 | } |
| 8309 | |
| 8310 | MoveAssignOperator->setUsed(); |
| 8311 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8312 | SynthesizedFunctionScope Scope(*this, MoveAssignOperator); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8313 | DiagnosticErrorTrap Trap(Diags); |
| 8314 | |
| 8315 | // C++0x [class.copy]p28: |
| 8316 | // The implicitly-defined or move assignment operator for a non-union class |
| 8317 | // X performs memberwise move assignment of its subobjects. The direct base |
| 8318 | // classes of X are assigned first, in the order of their declaration in the |
| 8319 | // base-specifier-list, and then the immediate non-static data members of X |
| 8320 | // are assigned, in the order in which they were declared in the class |
| 8321 | // definition. |
| 8322 | |
| 8323 | // The statements that form the synthesized function body. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 8324 | SmallVector<Stmt*, 8> Statements; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8325 | |
| 8326 | // The parameter for the "other" object, which we are move from. |
| 8327 | ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0); |
| 8328 | QualType OtherRefType = Other->getType()-> |
| 8329 | getAs<RValueReferenceType>()->getPointeeType(); |
| 8330 | assert(OtherRefType.getQualifiers() == 0 && |
| 8331 | "Bad argument type of defaulted move assignment"); |
| 8332 | |
| 8333 | // Our location for everything implicitly-generated. |
| 8334 | SourceLocation Loc = MoveAssignOperator->getLocation(); |
| 8335 | |
| 8336 | // Construct a reference to the "other" object. We'll be using this |
| 8337 | // throughout the generated ASTs. |
| 8338 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
| 8339 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 8340 | // Cast to rvalue. |
| 8341 | OtherRef = CastForMoving(*this, OtherRef); |
| 8342 | |
| 8343 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 8344 | // ASTs. |
| 8345 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 8346 | assert(This && "Reference to this cannot fail!"); |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8347 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8348 | // Assign base classes. |
| 8349 | bool Invalid = false; |
| 8350 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8351 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 8352 | // Form the assignment: |
| 8353 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&&>(other)); |
| 8354 | QualType BaseType = Base->getType().getUnqualifiedType(); |
| 8355 | if (!BaseType->isRecordType()) { |
| 8356 | Invalid = true; |
| 8357 | continue; |
| 8358 | } |
| 8359 | |
| 8360 | CXXCastPath BasePath; |
| 8361 | BasePath.push_back(Base); |
| 8362 | |
| 8363 | // Construct the "from" expression, which is an implicit cast to the |
| 8364 | // appropriately-qualified base type. |
| 8365 | Expr *From = OtherRef; |
| 8366 | From = ImpCastExprToType(From, BaseType, CK_UncheckedDerivedToBase, |
Douglas Gregor | b2b5658 | 2011-09-06 16:26:56 +0000 | [diff] [blame] | 8367 | VK_XValue, &BasePath).take(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8368 | |
| 8369 | // Dereference "this". |
| 8370 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
| 8371 | |
| 8372 | // Implicitly cast "this" to the appropriately-qualified base type. |
| 8373 | To = ImpCastExprToType(To.take(), |
| 8374 | Context.getCVRQualifiedType(BaseType, |
| 8375 | MoveAssignOperator->getTypeQualifiers()), |
| 8376 | CK_UncheckedDerivedToBase, |
| 8377 | VK_LValue, &BasePath); |
| 8378 | |
| 8379 | // Build the move. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8380 | StmtResult Move = buildSingleCopyAssign(*this, Loc, BaseType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8381 | To.get(), From, |
| 8382 | /*CopyingBaseSubobject=*/true, |
| 8383 | /*Copying=*/false); |
| 8384 | if (Move.isInvalid()) { |
| 8385 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8386 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8387 | MoveAssignOperator->setInvalidDecl(); |
| 8388 | return; |
| 8389 | } |
| 8390 | |
| 8391 | // Success! Record the move. |
| 8392 | Statements.push_back(Move.takeAs<Expr>()); |
| 8393 | } |
| 8394 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8395 | // Assign non-static members. |
| 8396 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8397 | FieldEnd = ClassDecl->field_end(); |
| 8398 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 8399 | if (Field->isUnnamedBitfield()) |
| 8400 | continue; |
| 8401 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8402 | // Check for members of reference type; we can't move those. |
| 8403 | if (Field->getType()->isReferenceType()) { |
| 8404 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 8405 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 8406 | Diag(Field->getLocation(), diag::note_declared_at); |
| 8407 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8408 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8409 | Invalid = true; |
| 8410 | continue; |
| 8411 | } |
| 8412 | |
| 8413 | // Check for members of const-qualified, non-class type. |
| 8414 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 8415 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 8416 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 8417 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 8418 | Diag(Field->getLocation(), diag::note_declared_at); |
| 8419 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8420 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8421 | Invalid = true; |
| 8422 | continue; |
| 8423 | } |
| 8424 | |
| 8425 | // Suppress assigning zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8426 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 8427 | continue; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8428 | |
| 8429 | QualType FieldType = Field->getType().getNonReferenceType(); |
| 8430 | if (FieldType->isIncompleteArrayType()) { |
| 8431 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 8432 | "Incomplete array type is not valid"); |
| 8433 | continue; |
| 8434 | } |
| 8435 | |
| 8436 | // Build references to the field in the object we're copying from and to. |
| 8437 | CXXScopeSpec SS; // Intentionally empty |
| 8438 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 8439 | LookupMemberName); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 8440 | MemberLookup.addDecl(*Field); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8441 | MemberLookup.resolveKind(); |
| 8442 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
| 8443 | Loc, /*IsArrow=*/false, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8444 | SS, SourceLocation(), 0, |
| 8445 | MemberLookup, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8446 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
| 8447 | Loc, /*IsArrow=*/true, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8448 | SS, SourceLocation(), 0, |
| 8449 | MemberLookup, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8450 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 8451 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 8452 | |
| 8453 | assert(!From.get()->isLValue() && // could be xvalue or prvalue |
| 8454 | "Member reference with rvalue base must be rvalue except for reference " |
| 8455 | "members, which aren't allowed for move assignment."); |
| 8456 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8457 | // Build the move of this field. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8458 | StmtResult Move = buildSingleCopyAssign(*this, Loc, FieldType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8459 | To.get(), From.get(), |
| 8460 | /*CopyingBaseSubobject=*/false, |
| 8461 | /*Copying=*/false); |
| 8462 | if (Move.isInvalid()) { |
| 8463 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8464 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8465 | MoveAssignOperator->setInvalidDecl(); |
| 8466 | return; |
| 8467 | } |
Richard Smith | e7ce709 | 2012-11-12 23:33:00 +0000 | [diff] [blame] | 8468 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8469 | // Success! Record the copy. |
| 8470 | Statements.push_back(Move.takeAs<Stmt>()); |
| 8471 | } |
| 8472 | |
| 8473 | if (!Invalid) { |
| 8474 | // Add a "return *this;" |
| 8475 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
| 8476 | |
| 8477 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
| 8478 | if (Return.isInvalid()) |
| 8479 | Invalid = true; |
| 8480 | else { |
| 8481 | Statements.push_back(Return.takeAs<Stmt>()); |
| 8482 | |
| 8483 | if (Trap.hasErrorOccurred()) { |
| 8484 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8485 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 8486 | Invalid = true; |
| 8487 | } |
| 8488 | } |
| 8489 | } |
| 8490 | |
| 8491 | if (Invalid) { |
| 8492 | MoveAssignOperator->setInvalidDecl(); |
| 8493 | return; |
| 8494 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8495 | |
| 8496 | StmtResult Body; |
| 8497 | { |
| 8498 | CompoundScopeRAII CompoundScope(*this); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8499 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8500 | /*isStmtExpr=*/false); |
| 8501 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 8502 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8503 | MoveAssignOperator->setBody(Body.takeAs<Stmt>()); |
| 8504 | |
| 8505 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8506 | L->CompletedImplicitDefinition(MoveAssignOperator); |
| 8507 | } |
| 8508 | } |
| 8509 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8510 | Sema::ImplicitExceptionSpecification |
| 8511 | Sema::ComputeDefaultedCopyCtorExceptionSpec(CXXMethodDecl *MD) { |
| 8512 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 8513 | |
| 8514 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 8515 | if (ClassDecl->isInvalidDecl()) |
| 8516 | return ExceptSpec; |
| 8517 | |
| 8518 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
| 8519 | assert(T->getNumArgs() >= 1 && "not a copy ctor"); |
| 8520 | unsigned Quals = T->getArgType(0).getNonReferenceType().getCVRQualifiers(); |
| 8521 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8522 | // C++ [except.spec]p14: |
| 8523 | // An implicitly declared special member function (Clause 12) shall have an |
| 8524 | // exception-specification. [...] |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8525 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8526 | BaseEnd = ClassDecl->bases_end(); |
| 8527 | Base != BaseEnd; |
| 8528 | ++Base) { |
| 8529 | // Virtual bases are handled below. |
| 8530 | if (Base->isVirtual()) |
| 8531 | continue; |
| 8532 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8533 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8534 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8535 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8536 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8537 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8538 | } |
| 8539 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8540 | BaseEnd = ClassDecl->vbases_end(); |
| 8541 | Base != BaseEnd; |
| 8542 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8543 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8544 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8545 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8546 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8547 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8548 | } |
| 8549 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8550 | FieldEnd = ClassDecl->field_end(); |
| 8551 | Field != FieldEnd; |
| 8552 | ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 8553 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 8554 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 8555 | if (CXXConstructorDecl *CopyConstructor = |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8556 | LookupCopyingConstructor(FieldClassDecl, |
| 8557 | Quals | FieldType.getCVRQualifiers())) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8558 | ExceptSpec.CalledDecl(Field->getLocation(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 8559 | } |
| 8560 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8561 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8562 | return ExceptSpec; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8563 | } |
| 8564 | |
| 8565 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 8566 | CXXRecordDecl *ClassDecl) { |
| 8567 | // C++ [class.copy]p4: |
| 8568 | // If the class definition does not explicitly declare a copy |
| 8569 | // constructor, one is declared implicitly. |
Richard Smith | d0adeb6 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 8570 | assert(!ClassDecl->hasDeclaredCopyConstructor()); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8571 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8572 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 8573 | QualType ArgType = ClassType; |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame^] | 8574 | bool Const = ClassDecl->implicitCopyConstructorHasConstParam(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8575 | if (Const) |
| 8576 | ArgType = ArgType.withConst(); |
| 8577 | ArgType = Context.getLValueReferenceType(ArgType); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8578 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 8579 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 8580 | CXXCopyConstructor, |
| 8581 | Const); |
| 8582 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8583 | DeclarationName Name |
| 8584 | = Context.DeclarationNames.getCXXConstructorName( |
| 8585 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8586 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 8587 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8588 | |
| 8589 | // An implicitly-declared copy constructor is an inline public |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8590 | // member of its class. |
| 8591 | CXXConstructorDecl *CopyConstructor = CXXConstructorDecl::Create( |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8592 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/0, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8593 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 8594 | Constexpr); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8595 | CopyConstructor->setAccess(AS_public); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8596 | CopyConstructor->setDefaulted(); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8597 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8598 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8599 | // Build an exception specification pointing back at this member. |
| 8600 | FunctionProtoType::ExtProtoInfo EPI; |
| 8601 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8602 | EPI.ExceptionSpecDecl = CopyConstructor; |
| 8603 | CopyConstructor->setType( |
| 8604 | Context.getFunctionType(Context.VoidTy, &ArgType, 1, EPI)); |
| 8605 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8606 | // Note that we have declared this constructor. |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8607 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 8608 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8609 | // Add the parameter to the constructor. |
| 8610 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8611 | ClassLoc, ClassLoc, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8612 | /*IdentifierInfo=*/0, |
| 8613 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8614 | SC_None, |
| 8615 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8616 | CopyConstructor->setParams(FromParam); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8617 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 8618 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 8619 | PushOnScopeChains(CopyConstructor, S, false); |
| 8620 | ClassDecl->addDecl(CopyConstructor); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 8621 | |
Nico Weber | afcc96a | 2012-01-23 03:19:29 +0000 | [diff] [blame] | 8622 | // C++11 [class.copy]p8: |
| 8623 | // ... If the class definition does not explicitly declare a copy |
| 8624 | // constructor, there is no user-declared move constructor, and there is no |
| 8625 | // user-declared move assignment operator, a copy constructor is implicitly |
| 8626 | // declared as defaulted. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8627 | if (ShouldDeleteSpecialMember(CopyConstructor, CXXCopyConstructor)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 8628 | CopyConstructor->setDeletedAsWritten(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8629 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 8630 | return CopyConstructor; |
| 8631 | } |
| 8632 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8633 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8634 | CXXConstructorDecl *CopyConstructor) { |
| 8635 | assert((CopyConstructor->isDefaulted() && |
| 8636 | CopyConstructor->isCopyConstructor() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8637 | !CopyConstructor->doesThisDeclarationHaveABody() && |
| 8638 | !CopyConstructor->isDeleted()) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8639 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8640 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 8641 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8642 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8643 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8644 | SynthesizedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8645 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8646 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 8647 | if (SetCtorInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8648 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 8649 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8650 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 8651 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8652 | } else { |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8653 | Sema::CompoundScopeRAII CompoundScope(*this); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8654 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 8655 | CopyConstructor->getLocation(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 8656 | MultiStmtArg(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8657 | /*isStmtExpr=*/false) |
| 8658 | .takeAs<Stmt>()); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame] | 8659 | CopyConstructor->setImplicitlyDefined(true); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 8660 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 8661 | |
| 8662 | CopyConstructor->setUsed(); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 8663 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8664 | L->CompletedImplicitDefinition(CopyConstructor); |
| 8665 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8666 | } |
| 8667 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8668 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8669 | Sema::ComputeDefaultedMoveCtorExceptionSpec(CXXMethodDecl *MD) { |
| 8670 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 8671 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8672 | // C++ [except.spec]p14: |
| 8673 | // An implicitly declared special member function (Clause 12) shall have an |
| 8674 | // exception-specification. [...] |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8675 | ImplicitExceptionSpecification ExceptSpec(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8676 | if (ClassDecl->isInvalidDecl()) |
| 8677 | return ExceptSpec; |
| 8678 | |
| 8679 | // Direct base-class constructors. |
| 8680 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 8681 | BEnd = ClassDecl->bases_end(); |
| 8682 | B != BEnd; ++B) { |
| 8683 | if (B->isVirtual()) // Handled below. |
| 8684 | continue; |
| 8685 | |
| 8686 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 8687 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8688 | CXXConstructorDecl *Constructor = |
| 8689 | LookupMovingConstructor(BaseClassDecl, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8690 | // If this is a deleted function, add it anyway. This might be conformant |
| 8691 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8692 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8693 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8694 | } |
| 8695 | } |
| 8696 | |
| 8697 | // Virtual base-class constructors. |
| 8698 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 8699 | BEnd = ClassDecl->vbases_end(); |
| 8700 | B != BEnd; ++B) { |
| 8701 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 8702 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8703 | CXXConstructorDecl *Constructor = |
| 8704 | LookupMovingConstructor(BaseClassDecl, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8705 | // If this is a deleted function, add it anyway. This might be conformant |
| 8706 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8707 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8708 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8709 | } |
| 8710 | } |
| 8711 | |
| 8712 | // Field constructors. |
| 8713 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 8714 | FEnd = ClassDecl->field_end(); |
| 8715 | F != FEnd; ++F) { |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8716 | QualType FieldType = Context.getBaseElementType(F->getType()); |
| 8717 | if (CXXRecordDecl *FieldRecDecl = FieldType->getAsCXXRecordDecl()) { |
| 8718 | CXXConstructorDecl *Constructor = |
| 8719 | LookupMovingConstructor(FieldRecDecl, FieldType.getCVRQualifiers()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8720 | // If this is a deleted function, add it anyway. This might be conformant |
| 8721 | // with the standard. This might not. I'm not sure. It might not matter. |
| 8722 | // In particular, the problem is that this function never gets called. It |
| 8723 | // might just be ill-formed because this function attempts to refer to |
| 8724 | // a deleted function here. |
| 8725 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8726 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8727 | } |
| 8728 | } |
| 8729 | |
| 8730 | return ExceptSpec; |
| 8731 | } |
| 8732 | |
| 8733 | CXXConstructorDecl *Sema::DeclareImplicitMoveConstructor( |
| 8734 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 8735 | // C++11 [class.copy]p9: |
| 8736 | // If the definition of a class X does not explicitly declare a move |
| 8737 | // constructor, one will be implicitly declared as defaulted if and only if: |
| 8738 | // |
| 8739 | // - [first 4 bullets] |
| 8740 | assert(ClassDecl->needsImplicitMoveConstructor()); |
| 8741 | |
| 8742 | // [Checked after we build the declaration] |
| 8743 | // - the move assignment operator would not be implicitly defined as |
| 8744 | // deleted, |
| 8745 | |
| 8746 | // [DR1402]: |
| 8747 | // - each of X's non-static data members and direct or virtual base classes |
| 8748 | // has a type that either has a move constructor or is trivially copyable. |
| 8749 | if (!subobjectsHaveMoveOrTrivialCopy(*this, ClassDecl, /*Constructor*/true)) { |
| 8750 | ClassDecl->setFailedImplicitMoveConstructor(); |
| 8751 | return 0; |
| 8752 | } |
| 8753 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8754 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 8755 | QualType ArgType = Context.getRValueReferenceType(ClassType); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8756 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 8757 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 8758 | CXXMoveConstructor, |
| 8759 | false); |
| 8760 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8761 | DeclarationName Name |
| 8762 | = Context.DeclarationNames.getCXXConstructorName( |
| 8763 | Context.getCanonicalType(ClassType)); |
| 8764 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 8765 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 8766 | |
| 8767 | // C++0x [class.copy]p11: |
| 8768 | // An implicitly-declared copy/move constructor is an inline public |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8769 | // member of its class. |
| 8770 | CXXConstructorDecl *MoveConstructor = CXXConstructorDecl::Create( |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8771 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/0, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8772 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 8773 | Constexpr); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8774 | MoveConstructor->setAccess(AS_public); |
| 8775 | MoveConstructor->setDefaulted(); |
| 8776 | MoveConstructor->setTrivial(ClassDecl->hasTrivialMoveConstructor()); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 8777 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8778 | // Build an exception specification pointing back at this member. |
| 8779 | FunctionProtoType::ExtProtoInfo EPI; |
| 8780 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8781 | EPI.ExceptionSpecDecl = MoveConstructor; |
| 8782 | MoveConstructor->setType( |
| 8783 | Context.getFunctionType(Context.VoidTy, &ArgType, 1, EPI)); |
| 8784 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8785 | // Add the parameter to the constructor. |
| 8786 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveConstructor, |
| 8787 | ClassLoc, ClassLoc, |
| 8788 | /*IdentifierInfo=*/0, |
| 8789 | ArgType, /*TInfo=*/0, |
| 8790 | SC_None, |
| 8791 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8792 | MoveConstructor->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8793 | |
| 8794 | // C++0x [class.copy]p9: |
| 8795 | // If the definition of a class X does not explicitly declare a move |
| 8796 | // constructor, one will be implicitly declared as defaulted if and only if: |
| 8797 | // [...] |
| 8798 | // - the move constructor would not be implicitly defined as deleted. |
Sean Hunt | 769bb2d | 2011-10-11 06:43:29 +0000 | [diff] [blame] | 8799 | if (ShouldDeleteSpecialMember(MoveConstructor, CXXMoveConstructor)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8800 | // Cache this result so that we don't try to generate this over and over |
| 8801 | // on every lookup, leaking memory and wasting time. |
| 8802 | ClassDecl->setFailedImplicitMoveConstructor(); |
| 8803 | return 0; |
| 8804 | } |
| 8805 | |
| 8806 | // Note that we have declared this constructor. |
| 8807 | ++ASTContext::NumImplicitMoveConstructorsDeclared; |
| 8808 | |
| 8809 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 8810 | PushOnScopeChains(MoveConstructor, S, false); |
| 8811 | ClassDecl->addDecl(MoveConstructor); |
| 8812 | |
| 8813 | return MoveConstructor; |
| 8814 | } |
| 8815 | |
| 8816 | void Sema::DefineImplicitMoveConstructor(SourceLocation CurrentLocation, |
| 8817 | CXXConstructorDecl *MoveConstructor) { |
| 8818 | assert((MoveConstructor->isDefaulted() && |
| 8819 | MoveConstructor->isMoveConstructor() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8820 | !MoveConstructor->doesThisDeclarationHaveABody() && |
| 8821 | !MoveConstructor->isDeleted()) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8822 | "DefineImplicitMoveConstructor - call it for implicit move ctor"); |
| 8823 | |
| 8824 | CXXRecordDecl *ClassDecl = MoveConstructor->getParent(); |
| 8825 | assert(ClassDecl && "DefineImplicitMoveConstructor - invalid constructor"); |
| 8826 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8827 | SynthesizedFunctionScope Scope(*this, MoveConstructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8828 | DiagnosticErrorTrap Trap(Diags); |
| 8829 | |
| 8830 | if (SetCtorInitializers(MoveConstructor, 0, 0, /*AnyErrors=*/false) || |
| 8831 | Trap.hasErrorOccurred()) { |
| 8832 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8833 | << CXXMoveConstructor << Context.getTagDeclType(ClassDecl); |
| 8834 | MoveConstructor->setInvalidDecl(); |
| 8835 | } else { |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 8836 | Sema::CompoundScopeRAII CompoundScope(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8837 | MoveConstructor->setBody(ActOnCompoundStmt(MoveConstructor->getLocation(), |
| 8838 | MoveConstructor->getLocation(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 8839 | MultiStmtArg(), |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8840 | /*isStmtExpr=*/false) |
| 8841 | .takeAs<Stmt>()); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame] | 8842 | MoveConstructor->setImplicitlyDefined(true); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8843 | } |
| 8844 | |
| 8845 | MoveConstructor->setUsed(); |
| 8846 | |
| 8847 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8848 | L->CompletedImplicitDefinition(MoveConstructor); |
| 8849 | } |
| 8850 | } |
| 8851 | |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 8852 | bool Sema::isImplicitlyDeleted(FunctionDecl *FD) { |
| 8853 | return FD->isDeleted() && |
| 8854 | (FD->isDefaulted() || FD->isImplicit()) && |
| 8855 | isa<CXXMethodDecl>(FD); |
| 8856 | } |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8857 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 8858 | /// \brief Mark the call operator of the given lambda closure type as "used". |
| 8859 | static void markLambdaCallOperatorUsed(Sema &S, CXXRecordDecl *Lambda) { |
| 8860 | CXXMethodDecl *CallOperator |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8861 | = cast<CXXMethodDecl>( |
| 8862 | *Lambda->lookup( |
| 8863 | S.Context.DeclarationNames.getCXXOperatorName(OO_Call)).first); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 8864 | CallOperator->setReferenced(); |
| 8865 | CallOperator->setUsed(); |
| 8866 | } |
| 8867 | |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8868 | void Sema::DefineImplicitLambdaToFunctionPointerConversion( |
| 8869 | SourceLocation CurrentLocation, |
| 8870 | CXXConversionDecl *Conv) |
| 8871 | { |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 8872 | CXXRecordDecl *Lambda = Conv->getParent(); |
| 8873 | |
| 8874 | // Make sure that the lambda call operator is marked used. |
| 8875 | markLambdaCallOperatorUsed(*this, Lambda); |
| 8876 | |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8877 | Conv->setUsed(); |
| 8878 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8879 | SynthesizedFunctionScope Scope(*this, Conv); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8880 | DiagnosticErrorTrap Trap(Diags); |
| 8881 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 8882 | // Return the address of the __invoke function. |
| 8883 | DeclarationName InvokeName = &Context.Idents.get("__invoke"); |
| 8884 | CXXMethodDecl *Invoke |
| 8885 | = cast<CXXMethodDecl>(*Lambda->lookup(InvokeName).first); |
| 8886 | Expr *FunctionRef = BuildDeclRefExpr(Invoke, Invoke->getType(), |
| 8887 | VK_LValue, Conv->getLocation()).take(); |
| 8888 | assert(FunctionRef && "Can't refer to __invoke function?"); |
| 8889 | Stmt *Return = ActOnReturnStmt(Conv->getLocation(), FunctionRef).take(); |
| 8890 | Conv->setBody(new (Context) CompoundStmt(Context, &Return, 1, |
| 8891 | Conv->getLocation(), |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8892 | Conv->getLocation())); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 8893 | |
| 8894 | // Fill in the __invoke function with a dummy implementation. IR generation |
| 8895 | // will fill in the actual details. |
| 8896 | Invoke->setUsed(); |
| 8897 | Invoke->setReferenced(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 8898 | Invoke->setBody(new (Context) CompoundStmt(Conv->getLocation())); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8899 | |
| 8900 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8901 | L->CompletedImplicitDefinition(Conv); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 8902 | L->CompletedImplicitDefinition(Invoke); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8903 | } |
| 8904 | } |
| 8905 | |
| 8906 | void Sema::DefineImplicitLambdaToBlockPointerConversion( |
| 8907 | SourceLocation CurrentLocation, |
| 8908 | CXXConversionDecl *Conv) |
| 8909 | { |
| 8910 | Conv->setUsed(); |
| 8911 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8912 | SynthesizedFunctionScope Scope(*this, Conv); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8913 | DiagnosticErrorTrap Trap(Diags); |
| 8914 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8915 | // Copy-initialize the lambda object as needed to capture it. |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8916 | Expr *This = ActOnCXXThis(CurrentLocation).take(); |
| 8917 | Expr *DerefThis =CreateBuiltinUnaryOp(CurrentLocation, UO_Deref, This).take(); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8918 | |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 8919 | ExprResult BuildBlock = BuildBlockForLambdaConversion(CurrentLocation, |
| 8920 | Conv->getLocation(), |
| 8921 | Conv, DerefThis); |
| 8922 | |
| 8923 | // If we're not under ARC, make sure we still get the _Block_copy/autorelease |
| 8924 | // behavior. Note that only the general conversion function does this |
| 8925 | // (since it's unusable otherwise); in the case where we inline the |
| 8926 | // block literal, it has block literal lifetime semantics. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8927 | if (!BuildBlock.isInvalid() && !getLangOpts().ObjCAutoRefCount) |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 8928 | BuildBlock = ImplicitCastExpr::Create(Context, BuildBlock.get()->getType(), |
| 8929 | CK_CopyAndAutoreleaseBlockObject, |
| 8930 | BuildBlock.get(), 0, VK_RValue); |
| 8931 | |
| 8932 | if (BuildBlock.isInvalid()) { |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8933 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8934 | Conv->setInvalidDecl(); |
| 8935 | return; |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8936 | } |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8937 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8938 | // Create the return statement that returns the block from the conversion |
| 8939 | // function. |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 8940 | StmtResult Return = ActOnReturnStmt(Conv->getLocation(), BuildBlock.get()); |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8941 | if (Return.isInvalid()) { |
| 8942 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
| 8943 | Conv->setInvalidDecl(); |
| 8944 | return; |
| 8945 | } |
| 8946 | |
| 8947 | // Set the body of the conversion function. |
| 8948 | Stmt *ReturnS = Return.take(); |
| 8949 | Conv->setBody(new (Context) CompoundStmt(Context, &ReturnS, 1, |
| 8950 | Conv->getLocation(), |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8951 | Conv->getLocation())); |
| 8952 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 8953 | // We're done; notify the mutation listener, if any. |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 8954 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8955 | L->CompletedImplicitDefinition(Conv); |
| 8956 | } |
| 8957 | } |
| 8958 | |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 8959 | /// \brief Determine whether the given list arguments contains exactly one |
| 8960 | /// "real" (non-default) argument. |
| 8961 | static bool hasOneRealArgument(MultiExprArg Args) { |
| 8962 | switch (Args.size()) { |
| 8963 | case 0: |
| 8964 | return false; |
| 8965 | |
| 8966 | default: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8967 | if (!Args[1]->isDefaultArgument()) |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 8968 | return false; |
| 8969 | |
| 8970 | // fall through |
| 8971 | case 1: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 8972 | return !Args[0]->isDefaultArgument(); |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 8973 | } |
| 8974 | |
| 8975 | return false; |
| 8976 | } |
| 8977 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8978 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 8979 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8980 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 8981 | MultiExprArg ExprArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 8982 | bool HadMultipleCandidates, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8983 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 8984 | unsigned ConstructKind, |
| 8985 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 8986 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8987 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 8988 | // C++0x [class.copy]p34: |
| 8989 | // When certain criteria are met, an implementation is allowed to |
| 8990 | // omit the copy/move construction of a class object, even if the |
| 8991 | // copy/move constructor and/or destructor for the object have |
| 8992 | // side effects. [...] |
| 8993 | // - when a temporary class object that has not been bound to a |
| 8994 | // reference (12.2) would be copied/moved to a class object |
| 8995 | // with the same cv-unqualified type, the copy/move operation |
| 8996 | // can be omitted by constructing the temporary object |
| 8997 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 8998 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 8999 | Constructor->isCopyOrMoveConstructor() && hasOneRealArgument(ExprArgs)) { |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9000 | Expr *SubExpr = ExprArgs[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 9001 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 9002 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9003 | |
| 9004 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9005 | Elidable, ExprArgs, HadMultipleCandidates, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9006 | RequiresZeroInit, ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 9007 | } |
| 9008 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 9009 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 9010 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9011 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 9012 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 9013 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 9014 | MultiExprArg ExprArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9015 | bool HadMultipleCandidates, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9016 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 9017 | unsigned ConstructKind, |
| 9018 | SourceRange ParenRange) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9019 | MarkFunctionReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 9020 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 9021 | Constructor, Elidable, ExprArgs, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 9022 | HadMultipleCandidates, /*FIXME*/false, |
| 9023 | RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 9024 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 9025 | ParenRange)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 9026 | } |
| 9027 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9028 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 9029 | CXXConstructorDecl *Constructor, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9030 | MultiExprArg Exprs, |
| 9031 | bool HadMultipleCandidates) { |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 9032 | // FIXME: Provide the correct paren SourceRange when available. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9033 | ExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 9034 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9035 | Exprs, HadMultipleCandidates, false, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 9036 | CXXConstructExpr::CK_Complete, SourceRange()); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 9037 | if (TempResult.isInvalid()) |
| 9038 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9039 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 9040 | Expr *Temp = TempResult.takeAs<Expr>(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 9041 | CheckImplicitConversions(Temp, VD->getLocation()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9042 | MarkFunctionReferenced(VD->getLocation(), Constructor); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 9043 | Temp = MaybeCreateExprWithCleanups(Temp); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 9044 | VD->setInit(Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9045 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 9046 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 9047 | } |
| 9048 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 9049 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 9050 | if (VD->isInvalidDecl()) return; |
| 9051 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 9052 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 9053 | if (ClassDecl->isInvalidDecl()) return; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 9054 | if (ClassDecl->hasIrrelevantDestructor()) return; |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 9055 | if (ClassDecl->isDependentContext()) return; |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 9056 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 9057 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 9058 | MarkFunctionReferenced(VD->getLocation(), Destructor); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 9059 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 9060 | PDiag(diag::err_access_dtor_var) |
| 9061 | << VD->getDeclName() |
| 9062 | << VD->getType()); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 9063 | DiagnoseUseOfDecl(Destructor, VD->getLocation()); |
Anders Carlsson | 2b32dad | 2011-03-24 01:01:41 +0000 | [diff] [blame] | 9064 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 9065 | if (!VD->hasGlobalStorage()) return; |
| 9066 | |
| 9067 | // Emit warning for non-trivial dtor in global scope (a real global, |
| 9068 | // class-static, function-static). |
| 9069 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 9070 | |
| 9071 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 9072 | if (!VD->isStaticLocal()) |
| 9073 | Diag(VD->getLocation(), diag::warn_global_destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9074 | } |
| 9075 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9076 | /// \brief Given a constructor and the set of arguments provided for the |
| 9077 | /// constructor, convert the arguments and add any required default arguments |
| 9078 | /// to form a proper call to this constructor. |
| 9079 | /// |
| 9080 | /// \returns true if an error occurred, false otherwise. |
| 9081 | bool |
| 9082 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 9083 | MultiExprArg ArgsPtr, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 9084 | SourceLocation Loc, |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 9085 | SmallVectorImpl<Expr*> &ConvertedArgs, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 9086 | bool AllowExplicit) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9087 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 9088 | unsigned NumArgs = ArgsPtr.size(); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9089 | Expr **Args = ArgsPtr.data(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9090 | |
| 9091 | const FunctionProtoType *Proto |
| 9092 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 9093 | assert(Proto && "Constructor without a prototype?"); |
| 9094 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9095 | |
| 9096 | // 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] | 9097 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9098 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9099 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 9100 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9101 | |
| 9102 | VariadicCallType CallType = |
| 9103 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9104 | SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9105 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 9106 | Proto, 0, Args, NumArgs, AllArgs, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 9107 | CallType, AllowExplicit); |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 9108 | ConvertedArgs.append(AllArgs.begin(), AllArgs.end()); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 9109 | |
| 9110 | DiagnoseSentinelCalls(Constructor, Loc, AllArgs.data(), AllArgs.size()); |
| 9111 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 9112 | CheckConstructorCall(Constructor, AllArgs.data(), AllArgs.size(), |
| 9113 | Proto, Loc); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 9114 | |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 9115 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 9116 | } |
| 9117 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9118 | static inline bool |
| 9119 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 9120 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 9121 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9122 | if (isa<NamespaceDecl>(DC)) { |
| 9123 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9124 | diag::err_operator_new_delete_declared_in_namespace) |
| 9125 | << FnDecl->getDeclName(); |
| 9126 | } |
| 9127 | |
| 9128 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9129 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9130 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9131 | diag::err_operator_new_delete_declared_static) |
| 9132 | << FnDecl->getDeclName(); |
| 9133 | } |
| 9134 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 9135 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9136 | } |
| 9137 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9138 | static inline bool |
| 9139 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 9140 | CanQualType ExpectedResultType, |
| 9141 | CanQualType ExpectedFirstParamType, |
| 9142 | unsigned DependentParamTypeDiag, |
| 9143 | unsigned InvalidParamTypeDiag) { |
| 9144 | QualType ResultType = |
| 9145 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 9146 | |
| 9147 | // Check that the result type is not dependent. |
| 9148 | if (ResultType->isDependentType()) |
| 9149 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9150 | diag::err_operator_new_delete_dependent_result_type) |
| 9151 | << FnDecl->getDeclName() << ExpectedResultType; |
| 9152 | |
| 9153 | // Check that the result type is what we expect. |
| 9154 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 9155 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9156 | diag::err_operator_new_delete_invalid_result_type) |
| 9157 | << FnDecl->getDeclName() << ExpectedResultType; |
| 9158 | |
| 9159 | // A function template must have at least 2 parameters. |
| 9160 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 9161 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9162 | diag::err_operator_new_delete_template_too_few_parameters) |
| 9163 | << FnDecl->getDeclName(); |
| 9164 | |
| 9165 | // The function decl must have at least 1 parameter. |
| 9166 | if (FnDecl->getNumParams() == 0) |
| 9167 | return SemaRef.Diag(FnDecl->getLocation(), |
| 9168 | diag::err_operator_new_delete_too_few_parameters) |
| 9169 | << FnDecl->getDeclName(); |
| 9170 | |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 9171 | // Check the first parameter type is not dependent. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9172 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 9173 | if (FirstParamType->isDependentType()) |
| 9174 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 9175 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 9176 | |
| 9177 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 9178 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9179 | ExpectedFirstParamType) |
| 9180 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 9181 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 9182 | |
| 9183 | return false; |
| 9184 | } |
| 9185 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9186 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9187 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9188 | // C++ [basic.stc.dynamic.allocation]p1: |
| 9189 | // A program is ill-formed if an allocation function is declared in a |
| 9190 | // namespace scope other than global scope or declared static in global |
| 9191 | // scope. |
| 9192 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 9193 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9194 | |
| 9195 | CanQualType SizeTy = |
| 9196 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 9197 | |
| 9198 | // C++ [basic.stc.dynamic.allocation]p1: |
| 9199 | // The return type shall be void*. The first parameter shall have type |
| 9200 | // std::size_t. |
| 9201 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 9202 | SizeTy, |
| 9203 | diag::err_operator_new_dependent_param_type, |
| 9204 | diag::err_operator_new_param_type)) |
| 9205 | return true; |
| 9206 | |
| 9207 | // C++ [basic.stc.dynamic.allocation]p1: |
| 9208 | // The first parameter shall not have an associated default argument. |
| 9209 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 9210 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9211 | diag::err_operator_new_default_arg) |
| 9212 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 9213 | |
| 9214 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 9215 | } |
| 9216 | |
| 9217 | static bool |
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 9218 | CheckOperatorDeleteDeclaration(Sema &SemaRef, FunctionDecl *FnDecl) { |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9219 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 9220 | // A program is ill-formed if deallocation functions are declared in a |
| 9221 | // namespace scope other than global scope or declared static in global |
| 9222 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 9223 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 9224 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9225 | |
| 9226 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 9227 | // Each deallocation function shall return void and its first parameter |
| 9228 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9229 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 9230 | SemaRef.Context.VoidPtrTy, |
| 9231 | diag::err_operator_delete_dependent_param_type, |
| 9232 | diag::err_operator_delete_param_type)) |
| 9233 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9234 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9235 | return false; |
| 9236 | } |
| 9237 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9238 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 9239 | /// of this overloaded operator is well-formed. If so, returns false; |
| 9240 | /// otherwise, emits appropriate diagnostics and returns true. |
| 9241 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9242 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9243 | "Expected an overloaded operator declaration"); |
| 9244 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9245 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 9246 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9247 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9248 | // The allocation and deallocation functions, operator new, |
| 9249 | // operator new[], operator delete and operator delete[], are |
| 9250 | // described completely in 3.7.3. The attributes and restrictions |
| 9251 | // found in the rest of this subclause do not apply to them unless |
| 9252 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 9253 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 9254 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 9255 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 9256 | if (Op == OO_New || Op == OO_Array_New) |
| 9257 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9258 | |
| 9259 | // C++ [over.oper]p6: |
| 9260 | // An operator function shall either be a non-static member |
| 9261 | // function or be a non-member function and have at least one |
| 9262 | // parameter whose type is a class, a reference to a class, an |
| 9263 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9264 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 9265 | if (MethodDecl->isStatic()) |
| 9266 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9267 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9268 | } else { |
| 9269 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9270 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 9271 | ParamEnd = FnDecl->param_end(); |
| 9272 | Param != ParamEnd; ++Param) { |
| 9273 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 9274 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 9275 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9276 | ClassOrEnumParam = true; |
| 9277 | break; |
| 9278 | } |
| 9279 | } |
| 9280 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9281 | if (!ClassOrEnumParam) |
| 9282 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 9283 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9284 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9285 | } |
| 9286 | |
| 9287 | // C++ [over.oper]p8: |
| 9288 | // An operator function cannot have default arguments (8.3.6), |
| 9289 | // except where explicitly stated below. |
| 9290 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9291 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9292 | // (C++ [over.call]p1). |
| 9293 | if (Op != OO_Call) { |
| 9294 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 9295 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9296 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9297 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 9298 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 9299 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9300 | } |
| 9301 | } |
| 9302 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9303 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 9304 | { false, false, false } |
| 9305 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 9306 | , { Unary, Binary, MemberOnly } |
| 9307 | #include "clang/Basic/OperatorKinds.def" |
| 9308 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9309 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9310 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 9311 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 9312 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9313 | |
| 9314 | // C++ [over.oper]p8: |
| 9315 | // [...] Operator functions cannot have more or fewer parameters |
| 9316 | // than the number required for the corresponding operator, as |
| 9317 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9318 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9319 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9320 | if (Op != OO_Call && |
| 9321 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 9322 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 9323 | (NumParams < 1) || (NumParams > 2))) { |
| 9324 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9325 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9326 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9327 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9328 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9329 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9330 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 9331 | assert(CanBeBinaryOperator && |
| 9332 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9333 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 9334 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9335 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 9336 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9337 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9338 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 9339 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9340 | // Overloaded operators other than operator() cannot be variadic. |
| 9341 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 9342 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 9343 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9344 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9345 | } |
| 9346 | |
| 9347 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9348 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 9349 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 9350 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 9351 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9352 | } |
| 9353 | |
| 9354 | // C++ [over.inc]p1: |
| 9355 | // The user-defined function called operator++ implements the |
| 9356 | // prefix and postfix ++ operator. If this function is a member |
| 9357 | // function with no parameters, or a non-member function with one |
| 9358 | // parameter of class or enumeration type, it defines the prefix |
| 9359 | // increment operator ++ for objects of that type. If the function |
| 9360 | // is a member function with one parameter (which shall be of type |
| 9361 | // int) or a non-member function with two parameters (the second |
| 9362 | // of which shall be of type int), it defines the postfix |
| 9363 | // increment operator ++ for objects of that type. |
| 9364 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 9365 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 9366 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 9367 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9368 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 9369 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 9370 | if (!ParamIsInt) |
| 9371 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9372 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 9373 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9374 | } |
| 9375 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 9376 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 9377 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 9378 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9379 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 9380 | /// of this literal operator function is well-formed. If so, returns |
| 9381 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 9382 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
Richard Smith | e5658f0 | 2012-03-10 22:18:57 +0000 | [diff] [blame] | 9383 | if (isa<CXXMethodDecl>(FnDecl)) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9384 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 9385 | << FnDecl->getDeclName(); |
| 9386 | return true; |
| 9387 | } |
| 9388 | |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9389 | if (FnDecl->isExternC()) { |
| 9390 | Diag(FnDecl->getLocation(), diag::err_literal_operator_extern_c); |
| 9391 | return true; |
| 9392 | } |
| 9393 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9394 | bool Valid = false; |
| 9395 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 9396 | // This might be the definition of a literal operator template. |
| 9397 | FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate(); |
| 9398 | // This might be a specialization of a literal operator template. |
| 9399 | if (!TpDecl) |
| 9400 | TpDecl = FnDecl->getPrimaryTemplate(); |
| 9401 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9402 | // template <char...> type operator "" name() is the only valid template |
| 9403 | // signature, and the only valid signature with no parameters. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 9404 | if (TpDecl) { |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9405 | if (FnDecl->param_size() == 0) { |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9406 | // Must have only one template parameter |
| 9407 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 9408 | if (Params->size() == 1) { |
| 9409 | NonTypeTemplateParmDecl *PmDecl = |
Richard Smith | 5295b97 | 2012-08-03 21:14:57 +0000 | [diff] [blame] | 9410 | dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9411 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9412 | // The template parameter must be a char parameter pack. |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9413 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 9414 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 9415 | Valid = true; |
| 9416 | } |
| 9417 | } |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9418 | } else if (FnDecl->param_size()) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9419 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 9420 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 9421 | |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9422 | QualType T = (*Param)->getType().getUnqualifiedType(); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9423 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 9424 | // unsigned long long int, long double, and any character type are allowed |
| 9425 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9426 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 9427 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 9428 | Context.hasSameType(T, Context.CharTy) || |
| 9429 | Context.hasSameType(T, Context.WCharTy) || |
| 9430 | Context.hasSameType(T, Context.Char16Ty) || |
| 9431 | Context.hasSameType(T, Context.Char32Ty)) { |
| 9432 | if (++Param == FnDecl->param_end()) |
| 9433 | Valid = true; |
| 9434 | goto FinishedParams; |
| 9435 | } |
| 9436 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 9437 | // 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] | 9438 | const PointerType *PT = T->getAs<PointerType>(); |
| 9439 | if (!PT) |
| 9440 | goto FinishedParams; |
| 9441 | T = PT->getPointeeType(); |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 9442 | if (!T.isConstQualified() || T.isVolatileQualified()) |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9443 | goto FinishedParams; |
| 9444 | T = T.getUnqualifiedType(); |
| 9445 | |
| 9446 | // Move on to the second parameter; |
| 9447 | ++Param; |
| 9448 | |
| 9449 | // If there is no second parameter, the first must be a const char * |
| 9450 | if (Param == FnDecl->param_end()) { |
| 9451 | if (Context.hasSameType(T, Context.CharTy)) |
| 9452 | Valid = true; |
| 9453 | goto FinishedParams; |
| 9454 | } |
| 9455 | |
| 9456 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 9457 | // are allowed as the first parameter to a two-parameter function |
| 9458 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 9459 | Context.hasSameType(T, Context.WCharTy) || |
| 9460 | Context.hasSameType(T, Context.Char16Ty) || |
| 9461 | Context.hasSameType(T, Context.Char32Ty))) |
| 9462 | goto FinishedParams; |
| 9463 | |
| 9464 | // The second and final parameter must be an std::size_t |
| 9465 | T = (*Param)->getType().getUnqualifiedType(); |
| 9466 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 9467 | ++Param == FnDecl->param_end()) |
| 9468 | Valid = true; |
| 9469 | } |
| 9470 | |
| 9471 | // FIXME: This diagnostic is absolutely terrible. |
| 9472 | FinishedParams: |
| 9473 | if (!Valid) { |
| 9474 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 9475 | << FnDecl->getDeclName(); |
| 9476 | return true; |
| 9477 | } |
| 9478 | |
Richard Smith | a9e88b2 | 2012-03-09 08:16:22 +0000 | [diff] [blame] | 9479 | // A parameter-declaration-clause containing a default argument is not |
| 9480 | // equivalent to any of the permitted forms. |
| 9481 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 9482 | ParamEnd = FnDecl->param_end(); |
| 9483 | Param != ParamEnd; ++Param) { |
| 9484 | if ((*Param)->hasDefaultArg()) { |
| 9485 | Diag((*Param)->getDefaultArgRange().getBegin(), |
| 9486 | diag::err_literal_operator_default_argument) |
| 9487 | << (*Param)->getDefaultArgRange(); |
| 9488 | break; |
| 9489 | } |
| 9490 | } |
| 9491 | |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 9492 | StringRef LiteralName |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 9493 | = FnDecl->getDeclName().getCXXLiteralIdentifier()->getName(); |
| 9494 | if (LiteralName[0] != '_') { |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 9495 | // C++11 [usrlit.suffix]p1: |
| 9496 | // Literal suffix identifiers that do not start with an underscore |
| 9497 | // are reserved for future standardization. |
| 9498 | Diag(FnDecl->getLocation(), diag::warn_user_literal_reserved); |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 9499 | } |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 9500 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 9501 | return false; |
| 9502 | } |
| 9503 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9504 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 9505 | /// linkage specification, including the language and (if present) |
| 9506 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 9507 | /// the location of the language string literal, which is provided |
| 9508 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 9509 | /// the '{' brace. Otherwise, this linkage specification does not |
| 9510 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 9511 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 9512 | SourceLocation LangLoc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9513 | StringRef Lang, |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 9514 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9515 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 9516 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9517 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 9518 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9519 | Language = LinkageSpecDecl::lang_cxx; |
| 9520 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9521 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9522 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9523 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9524 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9525 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9526 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9527 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9528 | ExternLoc, LangLoc, Language); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 9529 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9530 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9531 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 9532 | } |
| 9533 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 9534 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9535 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 9536 | /// valid, it's the position of the closing '}' brace in a linkage |
| 9537 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9538 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 9539 | Decl *LinkageSpec, |
| 9540 | SourceLocation RBraceLoc) { |
| 9541 | if (LinkageSpec) { |
| 9542 | if (RBraceLoc.isValid()) { |
| 9543 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 9544 | LSDecl->setRBraceLoc(RBraceLoc); |
| 9545 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9546 | PopDeclContext(); |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 9547 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 9548 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 9549 | } |
| 9550 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9551 | /// \brief Perform semantic analysis for the variable declaration that |
| 9552 | /// occurs within a C++ catch clause, returning the newly-created |
| 9553 | /// variable. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9554 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 9555 | TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9556 | SourceLocation StartLoc, |
| 9557 | SourceLocation Loc, |
| 9558 | IdentifierInfo *Name) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9559 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 9560 | QualType ExDeclType = TInfo->getType(); |
| 9561 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9562 | // Arrays and functions decay. |
| 9563 | if (ExDeclType->isArrayType()) |
| 9564 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 9565 | else if (ExDeclType->isFunctionType()) |
| 9566 | ExDeclType = Context.getPointerType(ExDeclType); |
| 9567 | |
| 9568 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 9569 | // The exception-declaration shall not denote a pointer or reference to an |
| 9570 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 9571 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9572 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 9573 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 9574 | Invalid = true; |
| 9575 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9576 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9577 | QualType BaseType = ExDeclType; |
| 9578 | 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] | 9579 | unsigned DK = diag::err_catch_incomplete; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 9580 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9581 | BaseType = Ptr->getPointeeType(); |
| 9582 | Mode = 1; |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 9583 | DK = diag::err_catch_incomplete_ptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9584 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 9585 | // 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] | 9586 | BaseType = Ref->getPointeeType(); |
| 9587 | Mode = 2; |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 9588 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9589 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 9590 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 9591 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9592 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9593 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9594 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9595 | RequireNonAbstractType(Loc, ExDeclType, |
| 9596 | diag::err_abstract_type_in_decl, |
| 9597 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 9598 | Invalid = true; |
| 9599 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 9600 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 9601 | // of ObjC types, and no runtime supports catching ObjC types by value. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9602 | if (!Invalid && getLangOpts().ObjC1) { |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 9603 | QualType T = ExDeclType; |
| 9604 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 9605 | T = RT->getPointeeType(); |
| 9606 | |
| 9607 | if (T->isObjCObjectType()) { |
| 9608 | Diag(Loc, diag::err_objc_object_catch); |
| 9609 | Invalid = true; |
| 9610 | } else if (T->isObjCObjectPointerType()) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 9611 | // FIXME: should this be a test for macosx-fragile specifically? |
| 9612 | if (getLangOpts().ObjCRuntime.isFragile()) |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 9613 | Diag(Loc, diag::warn_objc_pointer_cxx_catch_fragile); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 9614 | } |
| 9615 | } |
| 9616 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9617 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
| 9618 | ExDeclType, TInfo, SC_None, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 9619 | ExDecl->setExceptionVariable(true); |
| 9620 | |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 9621 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9622 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(ExDecl)) |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 9623 | Invalid = true; |
| 9624 | |
Douglas Gregor | c41b878 | 2011-07-06 18:14:43 +0000 | [diff] [blame] | 9625 | if (!Invalid && !ExDeclType->isDependentType()) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 9626 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 9627 | // C++ [except.handle]p16: |
| 9628 | // The object declared in an exception-declaration or, if the |
| 9629 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 9630 | // copy-initialized (8.5) from the exception object. [...] |
| 9631 | // The object is destroyed when the handler exits, after the destruction |
| 9632 | // of any automatic objects initialized within the handler. |
| 9633 | // |
| 9634 | // We just pretend to initialize the object with itself, then make sure |
| 9635 | // it can be destroyed later. |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 9636 | QualType initType = ExDeclType; |
| 9637 | |
| 9638 | InitializedEntity entity = |
| 9639 | InitializedEntity::InitializeVariable(ExDecl); |
| 9640 | InitializationKind initKind = |
| 9641 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 9642 | |
| 9643 | Expr *opaqueValue = |
| 9644 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
| 9645 | InitializationSequence sequence(*this, entity, initKind, &opaqueValue, 1); |
| 9646 | ExprResult result = sequence.Perform(*this, entity, initKind, |
| 9647 | MultiExprArg(&opaqueValue, 1)); |
| 9648 | if (result.isInvalid()) |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 9649 | Invalid = true; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 9650 | else { |
| 9651 | // If the constructor used was non-trivial, set this as the |
| 9652 | // "initializer". |
| 9653 | CXXConstructExpr *construct = cast<CXXConstructExpr>(result.take()); |
| 9654 | if (!construct->getConstructor()->isTrivial()) { |
| 9655 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 9656 | ExDecl->setInit(init); |
| 9657 | } |
| 9658 | |
| 9659 | // And make sure it's destructable. |
| 9660 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 9661 | } |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 9662 | } |
| 9663 | } |
| 9664 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9665 | if (Invalid) |
| 9666 | ExDecl->setInvalidDecl(); |
| 9667 | |
| 9668 | return ExDecl; |
| 9669 | } |
| 9670 | |
| 9671 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 9672 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9673 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9674 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 9675 | bool Invalid = D.isInvalidType(); |
| 9676 | |
| 9677 | // Check for unexpanded parameter packs. |
| 9678 | if (TInfo && DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 9679 | UPPC_ExceptionType)) { |
| 9680 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 9681 | D.getIdentifierLoc()); |
| 9682 | Invalid = true; |
| 9683 | } |
| 9684 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9685 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 9686 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 9687 | LookupOrdinaryName, |
| 9688 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9689 | // The scope should be freshly made just for us. There is just no way |
| 9690 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9691 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9692 | if (PrevDecl->isTemplateParameter()) { |
| 9693 | // Maybe we will complain about the shadowed template parameter. |
| 9694 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 9695 | PrevDecl = 0; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9696 | } |
| 9697 | } |
| 9698 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 9699 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9700 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 9701 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 9702 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9703 | } |
| 9704 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 9705 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9706 | D.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9707 | D.getIdentifierLoc(), |
| 9708 | D.getIdentifier()); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 9709 | if (Invalid) |
| 9710 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9711 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9712 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9713 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 9714 | PushOnScopeChains(ExDecl, S); |
| 9715 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 9716 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9717 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9718 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9719 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 9720 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 9721 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9722 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9723 | Expr *AssertExpr, |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 9724 | Expr *AssertMessageExpr, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9725 | SourceLocation RParenLoc) { |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 9726 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 9727 | |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 9728 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 9729 | return 0; |
| 9730 | |
| 9731 | return BuildStaticAssertDeclaration(StaticAssertLoc, AssertExpr, |
| 9732 | AssertMessage, RParenLoc, false); |
| 9733 | } |
| 9734 | |
| 9735 | Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
| 9736 | Expr *AssertExpr, |
| 9737 | StringLiteral *AssertMessage, |
| 9738 | SourceLocation RParenLoc, |
| 9739 | bool Failed) { |
| 9740 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent() && |
| 9741 | !Failed) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9742 | // In a static_assert-declaration, the constant-expression shall be a |
| 9743 | // constant expression that can be contextually converted to bool. |
| 9744 | ExprResult Converted = PerformContextuallyConvertToBool(AssertExpr); |
| 9745 | if (Converted.isInvalid()) |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 9746 | Failed = true; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9747 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 9748 | llvm::APSInt Cond; |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 9749 | if (!Failed && VerifyIntegerConstantExpression(Converted.get(), &Cond, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9750 | diag::err_static_assert_expression_is_not_constant, |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9751 | /*AllowFold=*/false).isInvalid()) |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 9752 | Failed = true; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 9753 | |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 9754 | if (!Failed && !Cond) { |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 9755 | llvm::SmallString<256> MsgBuffer; |
| 9756 | llvm::raw_svector_ostream Msg(MsgBuffer); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 9757 | AssertMessage->printPretty(Msg, 0, getPrintingPolicy()); |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9758 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 9759 | << Msg.str() << AssertExpr->getSourceRange(); |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 9760 | Failed = true; |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 9761 | } |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 9762 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9763 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 9764 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 9765 | AssertExpr, AssertMessage, RParenLoc, |
| 9766 | Failed); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9767 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 9768 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9769 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 9770 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 9771 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9772 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 9773 | /// |
| 9774 | /// \returns A friend declaration that. |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 9775 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation LocStart, |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 9776 | SourceLocation FriendLoc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9777 | TypeSourceInfo *TSInfo) { |
| 9778 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 9779 | |
| 9780 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 9781 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9782 | |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 9783 | // C++03 [class.friend]p2: |
| 9784 | // An elaborated-type-specifier shall be used in a friend declaration |
| 9785 | // for a class.* |
| 9786 | // |
| 9787 | // * The class-key of the elaborated-type-specifier is required. |
| 9788 | if (!ActiveTemplateInstantiations.empty()) { |
| 9789 | // Do not complain about the form of friend template types during |
| 9790 | // template instantiation; we will already have complained when the |
| 9791 | // template was declared. |
| 9792 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 9793 | // If we evaluated the type to a record type, suggest putting |
| 9794 | // a tag in front. |
| 9795 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 9796 | RecordDecl *RD = RT->getDecl(); |
| 9797 | |
| 9798 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 9799 | |
| 9800 | Diag(TypeRange.getBegin(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9801 | getLangOpts().CPlusPlus0x ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 9802 | diag::warn_cxx98_compat_unelaborated_friend_type : |
| 9803 | diag::ext_unelaborated_friend_type) |
| 9804 | << (unsigned) RD->getTagKind() |
| 9805 | << T |
| 9806 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 9807 | InsertionText); |
| 9808 | } else { |
| 9809 | Diag(FriendLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9810 | getLangOpts().CPlusPlus0x ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 9811 | diag::warn_cxx98_compat_nonclass_type_friend : |
| 9812 | diag::ext_nonclass_type_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9813 | << T |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 9814 | << TypeRange; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9815 | } |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 9816 | } else if (T->getAs<EnumType>()) { |
| 9817 | Diag(FriendLoc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9818 | getLangOpts().CPlusPlus0x ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 9819 | diag::warn_cxx98_compat_enum_friend : |
| 9820 | diag::ext_enum_friend) |
| 9821 | << T |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 9822 | << TypeRange; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9823 | } |
| 9824 | |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 9825 | // C++11 [class.friend]p3: |
| 9826 | // A friend declaration that does not declare a function shall have one |
| 9827 | // of the following forms: |
| 9828 | // friend elaborated-type-specifier ; |
| 9829 | // friend simple-type-specifier ; |
| 9830 | // friend typename-specifier ; |
| 9831 | if (getLangOpts().CPlusPlus0x && LocStart != FriendLoc) |
| 9832 | Diag(FriendLoc, diag::err_friend_not_first_in_declaration) << T; |
| 9833 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 9834 | // If the type specifier in a friend declaration designates a (possibly |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 9835 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 9836 | // the friend declaration is ignored. |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 9837 | return FriendDecl::Create(Context, CurContext, LocStart, TSInfo, FriendLoc); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 9838 | } |
| 9839 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9840 | /// Handle a friend tag declaration where the scope specifier was |
| 9841 | /// templated. |
| 9842 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 9843 | unsigned TagSpec, SourceLocation TagLoc, |
| 9844 | CXXScopeSpec &SS, |
| 9845 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 9846 | AttributeList *Attr, |
| 9847 | MultiTemplateParamsArg TempParamLists) { |
| 9848 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 9849 | |
| 9850 | bool isExplicitSpecialization = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9851 | bool Invalid = false; |
| 9852 | |
| 9853 | if (TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 9854 | = MatchTemplateParametersToScopeSpecifier(TagLoc, NameLoc, SS, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9855 | TempParamLists.data(), |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9856 | TempParamLists.size(), |
| 9857 | /*friend*/ true, |
| 9858 | isExplicitSpecialization, |
| 9859 | Invalid)) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9860 | if (TemplateParams->size() > 0) { |
| 9861 | // This is a declaration of a class template. |
| 9862 | if (Invalid) |
| 9863 | return 0; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 9864 | |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 9865 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 9866 | SS, Name, NameLoc, Attr, |
| 9867 | TemplateParams, AS_public, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 9868 | /*ModulePrivateLoc=*/SourceLocation(), |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 9869 | TempParamLists.size() - 1, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9870 | TempParamLists.data()).take(); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9871 | } else { |
| 9872 | // The "template<>" header is extraneous. |
| 9873 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 9874 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 9875 | isExplicitSpecialization = true; |
| 9876 | } |
| 9877 | } |
| 9878 | |
| 9879 | if (Invalid) return 0; |
| 9880 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9881 | bool isAllExplicitSpecializations = true; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 9882 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9883 | if (TempParamLists[I]->size()) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9884 | isAllExplicitSpecializations = false; |
| 9885 | break; |
| 9886 | } |
| 9887 | } |
| 9888 | |
| 9889 | // FIXME: don't ignore attributes. |
| 9890 | |
| 9891 | // If it's explicit specializations all the way down, just forget |
| 9892 | // about the template header and build an appropriate non-templated |
| 9893 | // friend. TODO: for source fidelity, remember the headers. |
| 9894 | if (isAllExplicitSpecializations) { |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 9895 | if (SS.isEmpty()) { |
| 9896 | bool Owned = false; |
| 9897 | bool IsDependent = false; |
| 9898 | return ActOnTag(S, TagSpec, TUK_Friend, TagLoc, SS, Name, NameLoc, |
| 9899 | Attr, AS_public, |
| 9900 | /*ModulePrivateLoc=*/SourceLocation(), |
| 9901 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 9902 | /*ScopedEnumKWLoc=*/SourceLocation(), |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 9903 | /*ScopedEnumUsesClassTag=*/false, |
| 9904 | /*UnderlyingType=*/TypeResult()); |
| 9905 | } |
| 9906 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9907 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9908 | ElaboratedTypeKeyword Keyword |
| 9909 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9910 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 9911 | *Name, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9912 | if (T.isNull()) |
| 9913 | return 0; |
| 9914 | |
| 9915 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9916 | if (isa<DependentNameType>(T)) { |
| 9917 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9918 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9919 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9920 | TL.setNameLoc(NameLoc); |
| 9921 | } else { |
| 9922 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9923 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 9924 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9925 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(NameLoc); |
| 9926 | } |
| 9927 | |
| 9928 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 9929 | TSI, FriendLoc); |
| 9930 | Friend->setAccess(AS_public); |
| 9931 | CurContext->addDecl(Friend); |
| 9932 | return Friend; |
| 9933 | } |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 9934 | |
| 9935 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 9936 | |
| 9937 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9938 | |
| 9939 | // Handle the case of a templated-scope friend class. e.g. |
| 9940 | // template <class T> class A<T>::B; |
| 9941 | // FIXME: we don't support these right now. |
| 9942 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 9943 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 9944 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9945 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 9946 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 9947 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 9948 | TL.setNameLoc(NameLoc); |
| 9949 | |
| 9950 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 9951 | TSI, FriendLoc); |
| 9952 | Friend->setAccess(AS_public); |
| 9953 | Friend->setUnsupportedFriend(true); |
| 9954 | CurContext->addDecl(Friend); |
| 9955 | return Friend; |
| 9956 | } |
| 9957 | |
| 9958 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9959 | /// Handle a friend type declaration. This works in tandem with |
| 9960 | /// ActOnTag. |
| 9961 | /// |
| 9962 | /// Notes on friend class templates: |
| 9963 | /// |
| 9964 | /// We generally treat friend class declarations as if they were |
| 9965 | /// declaring a class. So, for example, the elaborated type specifier |
| 9966 | /// in a friend declaration is required to obey the restrictions of a |
| 9967 | /// class-head (i.e. no typedefs in the scope chain), template |
| 9968 | /// parameters are required to match up with simple template-ids, &c. |
| 9969 | /// However, unlike when declaring a template specialization, it's |
| 9970 | /// okay to refer to a template specialization without an empty |
| 9971 | /// template parameter declaration, e.g. |
| 9972 | /// friend class A<T>::B<unsigned>; |
| 9973 | /// We permit this as a special case; if there are any template |
| 9974 | /// parameters present at all, require proper matching, i.e. |
James Dennett | ef2b5b3 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 9975 | /// template <> template \<class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9976 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 9977 | MultiTemplateParamsArg TempParams) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9978 | SourceLocation Loc = DS.getLocStart(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9979 | |
| 9980 | assert(DS.isFriendSpecified()); |
| 9981 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 9982 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9983 | // Try to convert the decl specifier to a type. This works for |
| 9984 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 9985 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 9986 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9987 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 9988 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 9989 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 9990 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 9991 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 9992 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 9993 | return 0; |
| 9994 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 9995 | // This is definitely an error in C++98. It's probably meant to |
| 9996 | // be forbidden in C++0x, too, but the specification is just |
| 9997 | // poorly written. |
| 9998 | // |
| 9999 | // The problem is with declarations like the following: |
| 10000 | // template <T> friend A<T>::foo; |
| 10001 | // where deciding whether a class C is a friend or not now hinges |
| 10002 | // on whether there exists an instantiation of A that causes |
| 10003 | // 'foo' to equal C. There are restrictions on class-heads |
| 10004 | // (which we declare (by fiat) elaborated friend declarations to |
| 10005 | // be) that makes this tractable. |
| 10006 | // |
| 10007 | // FIXME: handle "template <> friend class A<T>;", which |
| 10008 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 10009 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10010 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 10011 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10012 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10013 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10014 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10015 | // C++98 [class.friend]p1: A friend of a class is a function |
| 10016 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 10017 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 10018 | // deadline. It's also a very silly restriction that seriously |
| 10019 | // affects inner classes and which nobody else seems to implement; |
| 10020 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 10021 | // |
| 10022 | // But note that we could warn about it: it's always useless to |
| 10023 | // friend one of your own members (it's not, however, worthless to |
| 10024 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10025 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10026 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10027 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10028 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10029 | NumTempParamLists, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10030 | TempParams.data(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 10031 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10032 | DS.getFriendSpecLoc()); |
| 10033 | else |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 10034 | D = CheckFriendTypeDecl(Loc, DS.getFriendSpecLoc(), TSI); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10035 | |
| 10036 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10037 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10038 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 10039 | D->setAccess(AS_public); |
| 10040 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10041 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10042 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10043 | } |
| 10044 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10045 | Decl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10046 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10047 | const DeclSpec &DS = D.getDeclSpec(); |
| 10048 | |
| 10049 | assert(DS.isFriendSpecified()); |
| 10050 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 10051 | |
| 10052 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 10053 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10054 | |
| 10055 | // C++ [class.friend]p1 |
| 10056 | // A friend of a class is a function or class.... |
| 10057 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10058 | // It *doesn't* see through dependent types, which is correct |
| 10059 | // according to [temp.arg.type]p3: |
| 10060 | // If a declaration acquires a function type through a |
| 10061 | // type dependent on a template-parameter and this causes |
| 10062 | // a declaration that does not use the syntactic form of a |
| 10063 | // function declarator to have a function type, the program |
| 10064 | // is ill-formed. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10065 | if (!TInfo->getType()->isFunctionType()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10066 | Diag(Loc, diag::err_unexpected_friend); |
| 10067 | |
| 10068 | // It might be worthwhile to try to recover by creating an |
| 10069 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10070 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10071 | } |
| 10072 | |
| 10073 | // C++ [namespace.memdef]p3 |
| 10074 | // - If a friend declaration in a non-local class first declares a |
| 10075 | // class or function, the friend class or function is a member |
| 10076 | // of the innermost enclosing namespace. |
| 10077 | // - The name of the friend is not found by simple name lookup |
| 10078 | // until a matching declaration is provided in that namespace |
| 10079 | // scope (either before or after the class declaration granting |
| 10080 | // friendship). |
| 10081 | // - If a friend function is called, its name may be found by the |
| 10082 | // name lookup that considers functions from namespaces and |
| 10083 | // classes associated with the types of the function arguments. |
| 10084 | // - When looking for a prior declaration of a class or a function |
| 10085 | // declared as a friend, scopes outside the innermost enclosing |
| 10086 | // namespace scope are not considered. |
| 10087 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10088 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10089 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 10090 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10091 | assert(Name); |
| 10092 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 10093 | // Check for unexpanded parameter packs. |
| 10094 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 10095 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 10096 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 10097 | return 0; |
| 10098 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10099 | // The context we found the declaration in, or in which we should |
| 10100 | // create the declaration. |
| 10101 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 10102 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 10103 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10104 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10105 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10106 | // FIXME: there are different rules in local classes |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10107 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10108 | // There are four cases here. |
| 10109 | // - 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] | 10110 | // appropriate scope and look for a function or function template |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10111 | // there as appropriate. |
| 10112 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 10113 | if (SS.isInvalid() || !SS.isSet()) { |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10114 | // C++0x [namespace.memdef]p3: |
| 10115 | // If the name in a friend declaration is neither qualified nor |
| 10116 | // a template-id and the declaration is a function or an |
| 10117 | // elaborated-type-specifier, the lookup to determine whether |
| 10118 | // the entity has been previously declared shall not consider |
| 10119 | // any scopes outside the innermost enclosing namespace. |
| 10120 | // C++0x [class.friend]p11: |
| 10121 | // If a friend declaration appears in a local class and the name |
| 10122 | // specified is an unqualified name, a prior declaration is |
| 10123 | // looked up without considering scopes that are outside the |
| 10124 | // innermost enclosing non-class scope. For a friend function |
| 10125 | // declaration, if there is no prior declaration, the program is |
| 10126 | // ill-formed. |
| 10127 | bool isLocal = cast<CXXRecordDecl>(CurContext)->isLocalClass(); |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 10128 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10129 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10130 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10131 | DC = CurContext; |
| 10132 | while (true) { |
| 10133 | // Skip class contexts. If someone can cite chapter and verse |
| 10134 | // for this behavior, that would be nice --- it's what GCC and |
| 10135 | // EDG do, and it seems like a reasonable intent, but the spec |
| 10136 | // really only says that checks for unqualified existing |
| 10137 | // declarations should stop at the nearest enclosing namespace, |
| 10138 | // not that they should only consider the nearest enclosing |
| 10139 | // namespace. |
Nick Lewycky | 9c6fde5 | 2012-03-16 19:51:19 +0000 | [diff] [blame] | 10140 | while (DC->isRecord() || DC->isTransparentContext()) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10141 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10142 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 10143 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10144 | |
| 10145 | // TODO: decide what we think about using declarations. |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10146 | if (isLocal || !Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10147 | break; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10148 | |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 10149 | if (isTemplateId) { |
| 10150 | if (isa<TranslationUnitDecl>(DC)) break; |
| 10151 | } else { |
| 10152 | if (DC->isFileContext()) break; |
| 10153 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10154 | DC = DC->getParent(); |
| 10155 | } |
| 10156 | |
| 10157 | // C++ [class.friend]p1: A friend of a class is a function or |
| 10158 | // class that is not a member of the class . . . |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10159 | // C++11 changes this for both friend types and functions. |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 10160 | // Most C++ 98 compilers do seem to give an error here, so |
| 10161 | // we do, too. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10162 | if (!Previous.empty() && DC->Equals(CurContext)) |
| 10163 | Diag(DS.getFriendSpecLoc(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10164 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10165 | diag::warn_cxx98_compat_friend_is_member : |
| 10166 | diag::err_friend_is_member); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10167 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 10168 | DCScope = getScopeForDeclContext(S, DC); |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 10169 | |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10170 | // C++ [class.friend]p6: |
| 10171 | // A function can be defined in a friend declaration of a class if and |
| 10172 | // only if the class is a non-local class (9.8), the function name is |
| 10173 | // unqualified, and the function has namespace scope. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10174 | if (isLocal && D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10175 | Diag(NameInfo.getBeginLoc(), diag::err_friend_def_in_local_class); |
| 10176 | } |
| 10177 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10178 | // - There's a non-dependent scope specifier, in which case we |
| 10179 | // compute it and do a previous lookup there for a function |
| 10180 | // or function template. |
| 10181 | } else if (!SS.getScopeRep()->isDependent()) { |
| 10182 | DC = computeDeclContext(SS); |
| 10183 | if (!DC) return 0; |
| 10184 | |
| 10185 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 10186 | |
| 10187 | LookupQualifiedName(Previous, DC); |
| 10188 | |
| 10189 | // Ignore things found implicitly in the wrong scope. |
| 10190 | // TODO: better diagnostics for this case. Suggesting the right |
| 10191 | // qualified scope would be nice... |
| 10192 | LookupResult::Filter F = Previous.makeFilter(); |
| 10193 | while (F.hasNext()) { |
| 10194 | NamedDecl *D = F.next(); |
| 10195 | if (!DC->InEnclosingNamespaceSetOf( |
| 10196 | D->getDeclContext()->getRedeclContext())) |
| 10197 | F.erase(); |
| 10198 | } |
| 10199 | F.done(); |
| 10200 | |
| 10201 | if (Previous.empty()) { |
| 10202 | D.setInvalidType(); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10203 | Diag(Loc, diag::err_qualified_friend_not_found) |
| 10204 | << Name << TInfo->getType(); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10205 | return 0; |
| 10206 | } |
| 10207 | |
| 10208 | // C++ [class.friend]p1: A friend of a class is a function or |
| 10209 | // class that is not a member of the class . . . |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10210 | if (DC->Equals(CurContext)) |
| 10211 | Diag(DS.getFriendSpecLoc(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10212 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 10213 | diag::warn_cxx98_compat_friend_is_member : |
| 10214 | diag::err_friend_is_member); |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10215 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10216 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10217 | // C++ [class.friend]p6: |
| 10218 | // A function can be defined in a friend declaration of a class if and |
| 10219 | // only if the class is a non-local class (9.8), the function name is |
| 10220 | // unqualified, and the function has namespace scope. |
| 10221 | SemaDiagnosticBuilder DB |
| 10222 | = Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def); |
| 10223 | |
| 10224 | DB << SS.getScopeRep(); |
| 10225 | if (DC->isFileContext()) |
| 10226 | DB << FixItHint::CreateRemoval(SS.getRange()); |
| 10227 | SS.clear(); |
| 10228 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10229 | |
| 10230 | // - There's a scope specifier that does not match any template |
| 10231 | // parameter lists, in which case we use some arbitrary context, |
| 10232 | // create a method or method template, and wait for instantiation. |
| 10233 | // - There's a scope specifier that does match some template |
| 10234 | // parameter lists, which we don't handle right now. |
| 10235 | } else { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10236 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10237 | // C++ [class.friend]p6: |
| 10238 | // A function can be defined in a friend declaration of a class if and |
| 10239 | // only if the class is a non-local class (9.8), the function name is |
| 10240 | // unqualified, and the function has namespace scope. |
| 10241 | Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def) |
| 10242 | << SS.getScopeRep(); |
| 10243 | } |
| 10244 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10245 | DC = CurContext; |
| 10246 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10247 | } |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 10248 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 10249 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10250 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 10251 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 10252 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 10253 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10254 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 10255 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 10256 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10257 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10258 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10259 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10260 | |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 10261 | // FIXME: This is an egregious hack to cope with cases where the scope stack |
| 10262 | // does not contain the declaration context, i.e., in an out-of-line |
| 10263 | // definition of a class. |
| 10264 | Scope FakeDCScope(S, Scope::DeclScope, Diags); |
| 10265 | if (!DCScope) { |
| 10266 | FakeDCScope.setEntity(DC); |
| 10267 | DCScope = &FakeDCScope; |
| 10268 | } |
| 10269 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 10270 | bool AddToScope = true; |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 10271 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, TInfo, Previous, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10272 | TemplateParams, AddToScope); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10273 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10274 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10275 | assert(ND->getDeclContext() == DC); |
| 10276 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 10277 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10278 | // Add the function declaration to the appropriate lookup tables, |
| 10279 | // adjusting the redeclarations list as necessary. We don't |
| 10280 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10281 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10282 | // Also update the scope-based lookup if the target context's |
| 10283 | // lookup context is in lexical scope. |
| 10284 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10285 | DC = DC->getRedeclContext(); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 10286 | DC->makeDeclVisibleInContext(ND); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10287 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10288 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 10289 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10290 | |
| 10291 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 10292 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10293 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 10294 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 10295 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 10296 | |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 10297 | if (ND->isInvalidDecl()) { |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10298 | FrD->setInvalidDecl(); |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 10299 | } else { |
| 10300 | if (DC->isRecord()) CheckFriendAccess(ND); |
| 10301 | |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 10302 | FunctionDecl *FD; |
| 10303 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 10304 | FD = FTD->getTemplatedDecl(); |
| 10305 | else |
| 10306 | FD = cast<FunctionDecl>(ND); |
| 10307 | |
| 10308 | // Mark templated-scope function declarations as unsupported. |
| 10309 | if (FD->getNumTemplateParameterLists()) |
| 10310 | FrD->setUnsupportedFriend(true); |
| 10311 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 10312 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10313 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 10314 | } |
| 10315 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10316 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 10317 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10318 | |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 10319 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 10320 | if (!Fn) { |
| 10321 | Diag(DelLoc, diag::err_deleted_non_function); |
| 10322 | return; |
| 10323 | } |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 10324 | if (const FunctionDecl *Prev = Fn->getPreviousDecl()) { |
David Blaikie | d9cf826 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 10325 | // Don't consider the implicit declaration we generate for explicit |
| 10326 | // specializations. FIXME: Do not generate these implicit declarations. |
David Blaikie | 619ee6a | 2012-06-29 18:00:25 +0000 | [diff] [blame] | 10327 | if ((Prev->getTemplateSpecializationKind() != TSK_ExplicitSpecialization |
| 10328 | || Prev->getPreviousDecl()) && !Prev->isDefined()) { |
David Blaikie | d9cf826 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 10329 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 10330 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 10331 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 10332 | // If the declaration wasn't the first, we delete the function anyway for |
| 10333 | // recovery. |
| 10334 | } |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 10335 | Fn->setDeletedAsWritten(); |
Richard Smith | e653ba2 | 2012-02-26 00:31:33 +0000 | [diff] [blame] | 10336 | |
| 10337 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Dcl); |
| 10338 | if (!MD) |
| 10339 | return; |
| 10340 | |
| 10341 | // A deleted special member function is trivial if the corresponding |
| 10342 | // implicitly-declared function would have been. |
| 10343 | switch (getSpecialMember(MD)) { |
| 10344 | case CXXInvalid: |
| 10345 | break; |
| 10346 | case CXXDefaultConstructor: |
| 10347 | MD->setTrivial(MD->getParent()->hasTrivialDefaultConstructor()); |
| 10348 | break; |
| 10349 | case CXXCopyConstructor: |
| 10350 | MD->setTrivial(MD->getParent()->hasTrivialCopyConstructor()); |
| 10351 | break; |
| 10352 | case CXXMoveConstructor: |
| 10353 | MD->setTrivial(MD->getParent()->hasTrivialMoveConstructor()); |
| 10354 | break; |
| 10355 | case CXXCopyAssignment: |
| 10356 | MD->setTrivial(MD->getParent()->hasTrivialCopyAssignment()); |
| 10357 | break; |
| 10358 | case CXXMoveAssignment: |
| 10359 | MD->setTrivial(MD->getParent()->hasTrivialMoveAssignment()); |
| 10360 | break; |
| 10361 | case CXXDestructor: |
| 10362 | MD->setTrivial(MD->getParent()->hasTrivialDestructor()); |
| 10363 | break; |
| 10364 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 10365 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 10366 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10367 | void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) { |
| 10368 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Dcl); |
| 10369 | |
| 10370 | if (MD) { |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 10371 | if (MD->getParent()->isDependentType()) { |
| 10372 | MD->setDefaulted(); |
| 10373 | MD->setExplicitlyDefaulted(); |
| 10374 | return; |
| 10375 | } |
| 10376 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10377 | CXXSpecialMember Member = getSpecialMember(MD); |
| 10378 | if (Member == CXXInvalid) { |
| 10379 | Diag(DefaultLoc, diag::err_default_special_members); |
| 10380 | return; |
| 10381 | } |
| 10382 | |
| 10383 | MD->setDefaulted(); |
| 10384 | MD->setExplicitlyDefaulted(); |
| 10385 | |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 10386 | // If this definition appears within the record, do the checking when |
| 10387 | // the record is complete. |
| 10388 | const FunctionDecl *Primary = MD; |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 10389 | if (const FunctionDecl *Pattern = MD->getTemplateInstantiationPattern()) |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 10390 | // Find the uninstantiated declaration that actually had the '= default' |
| 10391 | // on it. |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 10392 | Pattern->isDefined(Primary); |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 10393 | |
| 10394 | if (Primary == Primary->getCanonicalDecl()) |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10395 | return; |
| 10396 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10397 | CheckExplicitlyDefaultedSpecialMember(MD); |
| 10398 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10399 | switch (Member) { |
| 10400 | case CXXDefaultConstructor: { |
| 10401 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10402 | if (!CD->isInvalidDecl()) |
| 10403 | DefineImplicitDefaultConstructor(DefaultLoc, CD); |
| 10404 | break; |
| 10405 | } |
| 10406 | |
| 10407 | case CXXCopyConstructor: { |
| 10408 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10409 | if (!CD->isInvalidDecl()) |
| 10410 | DefineImplicitCopyConstructor(DefaultLoc, CD); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10411 | break; |
| 10412 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10413 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 10414 | case CXXCopyAssignment: { |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 10415 | if (!MD->isInvalidDecl()) |
| 10416 | DefineImplicitCopyAssignment(DefaultLoc, MD); |
| 10417 | break; |
| 10418 | } |
| 10419 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10420 | case CXXDestructor: { |
| 10421 | CXXDestructorDecl *DD = cast<CXXDestructorDecl>(MD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10422 | if (!DD->isInvalidDecl()) |
| 10423 | DefineImplicitDestructor(DefaultLoc, DD); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10424 | break; |
| 10425 | } |
| 10426 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10427 | case CXXMoveConstructor: { |
| 10428 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10429 | if (!CD->isInvalidDecl()) |
| 10430 | DefineImplicitMoveConstructor(DefaultLoc, CD); |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 10431 | break; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10432 | } |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 10433 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10434 | case CXXMoveAssignment: { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10435 | if (!MD->isInvalidDecl()) |
| 10436 | DefineImplicitMoveAssignment(DefaultLoc, MD); |
| 10437 | break; |
| 10438 | } |
| 10439 | |
| 10440 | case CXXInvalid: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10441 | llvm_unreachable("Invalid special member."); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 10442 | } |
| 10443 | } else { |
| 10444 | Diag(DefaultLoc, diag::err_default_special_members); |
| 10445 | } |
| 10446 | } |
| 10447 | |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 10448 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 10449 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 10450 | Stmt *SubStmt = *CI; |
| 10451 | if (!SubStmt) |
| 10452 | continue; |
| 10453 | if (isa<ReturnStmt>(SubStmt)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10454 | Self.Diag(SubStmt->getLocStart(), |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 10455 | diag::err_return_in_constructor_handler); |
| 10456 | if (!isa<Expr>(SubStmt)) |
| 10457 | SearchForReturnInStmt(Self, SubStmt); |
| 10458 | } |
| 10459 | } |
| 10460 | |
| 10461 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 10462 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 10463 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 10464 | SearchForReturnInStmt(*this, Handler); |
| 10465 | } |
| 10466 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10467 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10468 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10469 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 10470 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 10471 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10472 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 10473 | if (Context.hasSameType(NewTy, OldTy) || |
| 10474 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10475 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10476 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10477 | // Check if the return types are covariant |
| 10478 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10479 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10480 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 10481 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 10482 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10483 | NewClassTy = NewPT->getPointeeType(); |
| 10484 | OldClassTy = OldPT->getPointeeType(); |
| 10485 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 10486 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 10487 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 10488 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 10489 | NewClassTy = NewRT->getPointeeType(); |
| 10490 | OldClassTy = OldRT->getPointeeType(); |
| 10491 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10492 | } |
| 10493 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10494 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10495 | // The return types aren't either both pointers or references to a class type. |
| 10496 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10497 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10498 | diag::err_different_return_type_for_overriding_virtual_function) |
| 10499 | << New->getDeclName() << NewTy << OldTy; |
| 10500 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10501 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10502 | return true; |
| 10503 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10504 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 10505 | // C++ [class.virtual]p6: |
| 10506 | // If the return type of D::f differs from the return type of B::f, the |
| 10507 | // class type in the return type of D::f shall be complete at the point of |
| 10508 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 10509 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 10510 | if (!RT->isBeingDefined() && |
| 10511 | RequireCompleteType(New->getLocation(), NewClassTy, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 10512 | diag::err_covariant_return_incomplete, |
| 10513 | New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 10514 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 10515 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 10516 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 10517 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10518 | // Check if the new class derives from the old class. |
| 10519 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 10520 | Diag(New->getLocation(), |
| 10521 | diag::err_covariant_return_not_derived) |
| 10522 | << New->getDeclName() << NewTy << OldTy; |
| 10523 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 10524 | return true; |
| 10525 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10526 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10527 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 10528 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 10529 | diag::err_covariant_return_inaccessible_base, |
| 10530 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 10531 | // FIXME: Should this point to the return type? |
| 10532 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 10533 | // FIXME: this note won't trigger for delayed access control |
| 10534 | // diagnostics, and it's impossible to get an undelayed error |
| 10535 | // here from access control during the original parse because |
| 10536 | // the ParsingDeclSpec/ParsingDeclarator are still in scope. |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10537 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 10538 | return true; |
| 10539 | } |
| 10540 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10541 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10542 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 10543 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10544 | Diag(New->getLocation(), |
| 10545 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10546 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10547 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 10548 | return true; |
| 10549 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10550 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10551 | |
| 10552 | // The new class type must have the same or less qualifiers as the old type. |
| 10553 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 10554 | Diag(New->getLocation(), |
| 10555 | diag::err_covariant_return_type_class_type_more_qualified) |
| 10556 | << New->getDeclName() << NewTy << OldTy; |
| 10557 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 10558 | return true; |
| 10559 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10560 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 10561 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 10562 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10563 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 10564 | /// \brief Mark the given method pure. |
| 10565 | /// |
| 10566 | /// \param Method the method to be marked pure. |
| 10567 | /// |
| 10568 | /// \param InitRange the source range that covers the "0" initializer. |
| 10569 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 10570 | SourceLocation EndLoc = InitRange.getEnd(); |
| 10571 | if (EndLoc.isValid()) |
| 10572 | Method->setRangeEnd(EndLoc); |
| 10573 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 10574 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 10575 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 10576 | return false; |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 10577 | } |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 10578 | |
| 10579 | if (!Method->isInvalidDecl()) |
| 10580 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 10581 | << Method->getDeclName() << InitRange; |
| 10582 | return true; |
| 10583 | } |
| 10584 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 10585 | /// \brief Determine whether the given declaration is a static data member. |
| 10586 | static bool isStaticDataMember(Decl *D) { |
| 10587 | VarDecl *Var = dyn_cast_or_null<VarDecl>(D); |
| 10588 | if (!Var) |
| 10589 | return false; |
| 10590 | |
| 10591 | return Var->isStaticDataMember(); |
| 10592 | } |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 10593 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 10594 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 10595 | /// is a fresh scope pushed for just this purpose. |
| 10596 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10597 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 10598 | /// static data member of class X, names should be looked up in the scope of |
| 10599 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10600 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10601 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 10602 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10603 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 10604 | // We should only get called for declarations with scope specifiers, like: |
| 10605 | // int foo::bar; |
| 10606 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 10607 | EnterDeclaratorContext(S, D->getDeclContext()); |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 10608 | |
| 10609 | // If we are parsing the initializer for a static data member, push a |
| 10610 | // new expression evaluation context that is associated with this static |
| 10611 | // data member. |
| 10612 | if (isStaticDataMember(D)) |
| 10613 | PushExpressionEvaluationContext(PotentiallyEvaluated, D); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10614 | } |
| 10615 | |
| 10616 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10617 | /// initializer for the out-of-line declaration 'D'. |
| 10618 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10619 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 10620 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10621 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 10622 | if (isStaticDataMember(D)) |
| 10623 | PopExpressionEvaluationContext(); |
| 10624 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 10625 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 10626 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 10627 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10628 | |
| 10629 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 10630 | /// C++ if/switch/while/for statement. |
| 10631 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10632 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10633 | // C++ 6.4p2: |
| 10634 | // The declarator shall not specify a function or an array. |
| 10635 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 10636 | // new class or enumeration. |
| 10637 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 10638 | "Parser allowed 'typedef' as storage class of condition decl."); |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 10639 | |
| 10640 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 10641 | if (!Dcl) |
| 10642 | return true; |
| 10643 | |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 10644 | if (isa<FunctionDecl>(Dcl)) { // The declarator shall not specify a function. |
| 10645 | Diag(Dcl->getLocation(), diag::err_invalid_use_of_function_type) |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10646 | << D.getSourceRange(); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 10647 | return true; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10648 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10649 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 10650 | return Dcl; |
| 10651 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 10652 | |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 10653 | void Sema::LoadExternalVTableUses() { |
| 10654 | if (!ExternalSource) |
| 10655 | return; |
| 10656 | |
| 10657 | SmallVector<ExternalVTableUse, 4> VTables; |
| 10658 | ExternalSource->ReadUsedVTables(VTables); |
| 10659 | SmallVector<VTableUse, 4> NewUses; |
| 10660 | for (unsigned I = 0, N = VTables.size(); I != N; ++I) { |
| 10661 | llvm::DenseMap<CXXRecordDecl *, bool>::iterator Pos |
| 10662 | = VTablesUsed.find(VTables[I].Record); |
| 10663 | // Even if a definition wasn't required before, it may be required now. |
| 10664 | if (Pos != VTablesUsed.end()) { |
| 10665 | if (!Pos->second && VTables[I].DefinitionRequired) |
| 10666 | Pos->second = true; |
| 10667 | continue; |
| 10668 | } |
| 10669 | |
| 10670 | VTablesUsed[VTables[I].Record] = VTables[I].DefinitionRequired; |
| 10671 | NewUses.push_back(VTableUse(VTables[I].Record, VTables[I].Location)); |
| 10672 | } |
| 10673 | |
| 10674 | VTableUses.insert(VTableUses.begin(), NewUses.begin(), NewUses.end()); |
| 10675 | } |
| 10676 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10677 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 10678 | bool DefinitionRequired) { |
| 10679 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 10680 | // not have a vtable. |
| 10681 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 10682 | CurContext->isDependentContext() || |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 10683 | ExprEvalContexts.back().Context == Unevaluated) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 10684 | return; |
| 10685 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10686 | // Try to insert this class into the map. |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 10687 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10688 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 10689 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 10690 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 10691 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 10692 | // If we already had an entry, check to see if we are promoting this vtable |
| 10693 | // to required a definition. If so, we need to reappend to the VTableUses |
| 10694 | // list, since we may have already processed the first entry. |
| 10695 | if (DefinitionRequired && !Pos.first->second) { |
| 10696 | Pos.first->second = true; |
| 10697 | } else { |
| 10698 | // Otherwise, we can early exit. |
| 10699 | return; |
| 10700 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10701 | } |
| 10702 | |
| 10703 | // Local classes need to have their virtual members marked |
| 10704 | // immediately. For all other classes, we mark their virtual members |
| 10705 | // at the end of the translation unit. |
| 10706 | if (Class->isLocalClass()) |
| 10707 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 10708 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10709 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 10710 | } |
| 10711 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10712 | bool Sema::DefineUsedVTables() { |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 10713 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10714 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10715 | return false; |
Chandler Carruth | aee543a | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 10716 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10717 | // Note: The VTableUses vector could grow as a result of marking |
| 10718 | // 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] | 10719 | // time through the loop and prefer indices (which are stable) to |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10720 | // iterators (which are not). |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 10721 | bool DefinedAnything = false; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10722 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 10723 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10724 | if (!Class) |
| 10725 | continue; |
| 10726 | |
| 10727 | SourceLocation Loc = VTableUses[I].second; |
| 10728 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10729 | bool DefineVTable = true; |
| 10730 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10731 | // If this class has a key function, but that key function is |
| 10732 | // defined in another translation unit, we don't need to emit the |
| 10733 | // vtable even though we're using it. |
| 10734 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 10735 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10736 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 10737 | case TSK_Undeclared: |
| 10738 | case TSK_ExplicitSpecialization: |
| 10739 | case TSK_ExplicitInstantiationDeclaration: |
| 10740 | // The key function is in another translation unit. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10741 | DefineVTable = false; |
| 10742 | break; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10743 | |
| 10744 | case TSK_ExplicitInstantiationDefinition: |
| 10745 | case TSK_ImplicitInstantiation: |
| 10746 | // We will be instantiating the key function. |
| 10747 | break; |
| 10748 | } |
| 10749 | } else if (!KeyFunction) { |
| 10750 | // If we have a class with no key function that is the subject |
| 10751 | // of an explicit instantiation declaration, suppress the |
| 10752 | // vtable; it will live with the explicit instantiation |
| 10753 | // definition. |
| 10754 | bool IsExplicitInstantiationDeclaration |
| 10755 | = Class->getTemplateSpecializationKind() |
| 10756 | == TSK_ExplicitInstantiationDeclaration; |
| 10757 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 10758 | REnd = Class->redecls_end(); |
| 10759 | R != REnd; ++R) { |
| 10760 | TemplateSpecializationKind TSK |
| 10761 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 10762 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 10763 | IsExplicitInstantiationDeclaration = true; |
| 10764 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 10765 | IsExplicitInstantiationDeclaration = false; |
| 10766 | break; |
| 10767 | } |
| 10768 | } |
| 10769 | |
| 10770 | if (IsExplicitInstantiationDeclaration) |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10771 | DefineVTable = false; |
| 10772 | } |
| 10773 | |
| 10774 | // The exception specifications for all virtual members may be needed even |
| 10775 | // if we are not providing an authoritative form of the vtable in this TU. |
| 10776 | // We may choose to emit it available_externally anyway. |
| 10777 | if (!DefineVTable) { |
| 10778 | MarkVirtualMemberExceptionSpecsNeeded(Loc, Class); |
| 10779 | continue; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10780 | } |
| 10781 | |
| 10782 | // Mark all of the virtual members of this class as referenced, so |
| 10783 | // that we can build a vtable. Then, tell the AST consumer that a |
| 10784 | // vtable for this class is required. |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 10785 | DefinedAnything = true; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10786 | MarkVirtualMembersReferenced(Loc, Class); |
| 10787 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 10788 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 10789 | |
| 10790 | // Optionally warn if we're emitting a weak vtable. |
| 10791 | if (Class->getLinkage() == ExternalLinkage && |
| 10792 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Douglas Gregor | a120d01 | 2011-09-23 19:04:03 +0000 | [diff] [blame] | 10793 | const FunctionDecl *KeyFunctionDef = 0; |
| 10794 | if (!KeyFunction || |
| 10795 | (KeyFunction->hasBody(KeyFunctionDef) && |
| 10796 | KeyFunctionDef->isInlined())) |
David Blaikie | 44d95b5 | 2011-12-09 18:32:50 +0000 | [diff] [blame] | 10797 | Diag(Class->getLocation(), Class->getTemplateSpecializationKind() == |
| 10798 | TSK_ExplicitInstantiationDefinition |
| 10799 | ? diag::warn_weak_template_vtable : diag::warn_weak_vtable) |
| 10800 | << Class; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10801 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 10802 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10803 | VTableUses.clear(); |
| 10804 | |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 10805 | return DefinedAnything; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 10806 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10807 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10808 | void Sema::MarkVirtualMemberExceptionSpecsNeeded(SourceLocation Loc, |
| 10809 | const CXXRecordDecl *RD) { |
| 10810 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 10811 | E = RD->method_end(); I != E; ++I) |
| 10812 | if ((*I)->isVirtual() && !(*I)->isPure()) |
| 10813 | ResolveExceptionSpec(Loc, (*I)->getType()->castAs<FunctionProtoType>()); |
| 10814 | } |
| 10815 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 10816 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 10817 | const CXXRecordDecl *RD) { |
Richard Smith | ff817f7 | 2012-07-07 06:59:51 +0000 | [diff] [blame] | 10818 | // Mark all functions which will appear in RD's vtable as used. |
| 10819 | CXXFinalOverriderMap FinalOverriders; |
| 10820 | RD->getFinalOverriders(FinalOverriders); |
| 10821 | for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(), |
| 10822 | E = FinalOverriders.end(); |
| 10823 | I != E; ++I) { |
| 10824 | for (OverridingMethods::const_iterator OI = I->second.begin(), |
| 10825 | OE = I->second.end(); |
| 10826 | OI != OE; ++OI) { |
| 10827 | assert(OI->second.size() > 0 && "no final overrider"); |
| 10828 | CXXMethodDecl *Overrider = OI->second.front().Method; |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10829 | |
Richard Smith | ff817f7 | 2012-07-07 06:59:51 +0000 | [diff] [blame] | 10830 | // C++ [basic.def.odr]p2: |
| 10831 | // [...] A virtual member function is used if it is not pure. [...] |
| 10832 | if (!Overrider->isPure()) |
| 10833 | MarkFunctionReferenced(Loc, Overrider); |
| 10834 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10835 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 10836 | |
| 10837 | // Only classes that have virtual bases need a VTT. |
| 10838 | if (RD->getNumVBases() == 0) |
| 10839 | return; |
| 10840 | |
| 10841 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 10842 | e = RD->bases_end(); i != e; ++i) { |
| 10843 | const CXXRecordDecl *Base = |
| 10844 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 10845 | if (Base->getNumVBases() == 0) |
| 10846 | continue; |
| 10847 | MarkVirtualMembersReferenced(Loc, Base); |
| 10848 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 10849 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10850 | |
| 10851 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 10852 | /// Objective-C implementation whose ivars need be initialized. |
| 10853 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10854 | if (!getLangOpts().CPlusPlus) |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10855 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 10856 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10857 | SmallVector<ObjCIvarDecl*, 8> ivars; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10858 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 10859 | if (ivars.empty()) |
| 10860 | return; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10861 | SmallVector<CXXCtorInitializer*, 32> AllToInit; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10862 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 10863 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 10864 | if (Field->isInvalidDecl()) |
| 10865 | continue; |
| 10866 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 10867 | CXXCtorInitializer *Member; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10868 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 10869 | InitializationKind InitKind = |
| 10870 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 10871 | |
| 10872 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10873 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 10874 | InitSeq.Perform(*this, InitEntity, InitKind, MultiExprArg()); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 10875 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10876 | // Note, MemberInit could actually come back empty if no initialization |
| 10877 | // is required (e.g., because it would call a trivial default constructor) |
| 10878 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 10879 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 10880 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10881 | Member = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 10882 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 10883 | SourceLocation(), |
| 10884 | MemberInit.takeAs<Expr>(), |
| 10885 | SourceLocation()); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10886 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 10887 | |
| 10888 | // Be sure that the destructor is accessible and is marked as referenced. |
| 10889 | if (const RecordType *RecordTy |
| 10890 | = Context.getBaseElementType(Field->getType()) |
| 10891 | ->getAs<RecordType>()) { |
| 10892 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 10893 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10894 | MarkFunctionReferenced(Field->getLocation(), Destructor); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 10895 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 10896 | PDiag(diag::err_access_dtor_ivar) |
| 10897 | << Context.getBaseElementType(Field->getType())); |
| 10898 | } |
| 10899 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 10900 | } |
| 10901 | ObjCImplementation->setIvarInitializers(Context, |
| 10902 | AllToInit.data(), AllToInit.size()); |
| 10903 | } |
| 10904 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10905 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10906 | static |
| 10907 | void DelegatingCycleHelper(CXXConstructorDecl* Ctor, |
| 10908 | llvm::SmallSet<CXXConstructorDecl*, 4> &Valid, |
| 10909 | llvm::SmallSet<CXXConstructorDecl*, 4> &Invalid, |
| 10910 | llvm::SmallSet<CXXConstructorDecl*, 4> &Current, |
| 10911 | Sema &S) { |
| 10912 | llvm::SmallSet<CXXConstructorDecl*, 4>::iterator CI = Current.begin(), |
| 10913 | CE = Current.end(); |
| 10914 | if (Ctor->isInvalidDecl()) |
| 10915 | return; |
| 10916 | |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 10917 | CXXConstructorDecl *Target = Ctor->getTargetConstructor(); |
| 10918 | |
| 10919 | // Target may not be determinable yet, for instance if this is a dependent |
| 10920 | // call in an uninstantiated template. |
| 10921 | if (Target) { |
| 10922 | const FunctionDecl *FNTarget = 0; |
| 10923 | (void)Target->hasBody(FNTarget); |
| 10924 | Target = const_cast<CXXConstructorDecl*>( |
| 10925 | cast_or_null<CXXConstructorDecl>(FNTarget)); |
| 10926 | } |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10927 | |
| 10928 | CXXConstructorDecl *Canonical = Ctor->getCanonicalDecl(), |
| 10929 | // Avoid dereferencing a null pointer here. |
| 10930 | *TCanonical = Target ? Target->getCanonicalDecl() : 0; |
| 10931 | |
| 10932 | if (!Current.insert(Canonical)) |
| 10933 | return; |
| 10934 | |
| 10935 | // We know that beyond here, we aren't chaining into a cycle. |
| 10936 | if (!Target || !Target->isDelegatingConstructor() || |
| 10937 | Target->isInvalidDecl() || Valid.count(TCanonical)) { |
| 10938 | for (CI = Current.begin(), CE = Current.end(); CI != CE; ++CI) |
| 10939 | Valid.insert(*CI); |
| 10940 | Current.clear(); |
| 10941 | // We've hit a cycle. |
| 10942 | } else if (TCanonical == Canonical || Invalid.count(TCanonical) || |
| 10943 | Current.count(TCanonical)) { |
| 10944 | // If we haven't diagnosed this cycle yet, do so now. |
| 10945 | if (!Invalid.count(TCanonical)) { |
| 10946 | S.Diag((*Ctor->init_begin())->getSourceLocation(), |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 10947 | diag::warn_delegating_ctor_cycle) |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10948 | << Ctor; |
| 10949 | |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 10950 | // Don't add a note for a function delegating directly to itself. |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10951 | if (TCanonical != Canonical) |
| 10952 | S.Diag(Target->getLocation(), diag::note_it_delegates_to); |
| 10953 | |
| 10954 | CXXConstructorDecl *C = Target; |
| 10955 | while (C->getCanonicalDecl() != Canonical) { |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 10956 | const FunctionDecl *FNTarget = 0; |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10957 | (void)C->getTargetConstructor()->hasBody(FNTarget); |
| 10958 | assert(FNTarget && "Ctor cycle through bodiless function"); |
| 10959 | |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 10960 | C = const_cast<CXXConstructorDecl*>( |
| 10961 | cast<CXXConstructorDecl>(FNTarget)); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10962 | S.Diag(C->getLocation(), diag::note_which_delegates_to); |
| 10963 | } |
| 10964 | } |
| 10965 | |
| 10966 | for (CI = Current.begin(), CE = Current.end(); CI != CE; ++CI) |
| 10967 | Invalid.insert(*CI); |
| 10968 | Current.clear(); |
| 10969 | } else { |
| 10970 | DelegatingCycleHelper(Target, Valid, Invalid, Current, S); |
| 10971 | } |
| 10972 | } |
| 10973 | |
| 10974 | |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10975 | void Sema::CheckDelegatingCtorCycles() { |
| 10976 | llvm::SmallSet<CXXConstructorDecl*, 4> Valid, Invalid, Current; |
| 10977 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10978 | llvm::SmallSet<CXXConstructorDecl*, 4>::iterator CI = Current.begin(), |
| 10979 | CE = Current.end(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10980 | |
Douglas Gregor | 0129b56 | 2011-07-27 21:57:17 +0000 | [diff] [blame] | 10981 | for (DelegatingCtorDeclsType::iterator |
| 10982 | I = DelegatingCtorDecls.begin(ExternalSource), |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10983 | E = DelegatingCtorDecls.end(); |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 10984 | I != E; ++I) |
| 10985 | DelegatingCycleHelper(*I, Valid, Invalid, Current, *this); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 10986 | |
| 10987 | for (CI = Invalid.begin(), CE = Invalid.end(); CI != CE; ++CI) |
| 10988 | (*CI)->setInvalidDecl(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 10989 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 10990 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 10991 | namespace { |
| 10992 | /// \brief AST visitor that finds references to the 'this' expression. |
| 10993 | class FindCXXThisExpr : public RecursiveASTVisitor<FindCXXThisExpr> { |
| 10994 | Sema &S; |
| 10995 | |
| 10996 | public: |
| 10997 | explicit FindCXXThisExpr(Sema &S) : S(S) { } |
| 10998 | |
| 10999 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 11000 | S.Diag(E->getLocation(), diag::err_this_static_member_func) |
| 11001 | << E->isImplicit(); |
| 11002 | return false; |
| 11003 | } |
| 11004 | }; |
| 11005 | } |
| 11006 | |
| 11007 | bool Sema::checkThisInStaticMemberFunctionType(CXXMethodDecl *Method) { |
| 11008 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 11009 | if (!TSInfo) |
| 11010 | return false; |
| 11011 | |
| 11012 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 11013 | FunctionProtoTypeLoc *ProtoTL = dyn_cast<FunctionProtoTypeLoc>(&TL); |
| 11014 | if (!ProtoTL) |
| 11015 | return false; |
| 11016 | |
| 11017 | // C++11 [expr.prim.general]p3: |
| 11018 | // [The expression this] shall not appear before the optional |
| 11019 | // cv-qualifier-seq and it shall not appear within the declaration of a |
| 11020 | // static member function (although its type and value category are defined |
| 11021 | // within a static member function as they are within a non-static member |
| 11022 | // function). [ Note: this is because declaration matching does not occur |
NAKAMURA Takumi | c86d1fd | 2012-04-21 09:40:04 +0000 | [diff] [blame] | 11023 | // until the complete declarator is known. - end note ] |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11024 | const FunctionProtoType *Proto = ProtoTL->getTypePtr(); |
| 11025 | FindCXXThisExpr Finder(*this); |
| 11026 | |
| 11027 | // If the return type came after the cv-qualifier-seq, check it now. |
| 11028 | if (Proto->hasTrailingReturn() && |
| 11029 | !Finder.TraverseTypeLoc(ProtoTL->getResultLoc())) |
| 11030 | return true; |
| 11031 | |
| 11032 | // Check the exception specification. |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11033 | if (checkThisInStaticMemberFunctionExceptionSpec(Method)) |
| 11034 | return true; |
| 11035 | |
| 11036 | return checkThisInStaticMemberFunctionAttributes(Method); |
| 11037 | } |
| 11038 | |
| 11039 | bool Sema::checkThisInStaticMemberFunctionExceptionSpec(CXXMethodDecl *Method) { |
| 11040 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 11041 | if (!TSInfo) |
| 11042 | return false; |
| 11043 | |
| 11044 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 11045 | FunctionProtoTypeLoc *ProtoTL = dyn_cast<FunctionProtoTypeLoc>(&TL); |
| 11046 | if (!ProtoTL) |
| 11047 | return false; |
| 11048 | |
| 11049 | const FunctionProtoType *Proto = ProtoTL->getTypePtr(); |
| 11050 | FindCXXThisExpr Finder(*this); |
| 11051 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11052 | switch (Proto->getExceptionSpecType()) { |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 11053 | case EST_Uninstantiated: |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11054 | case EST_Unevaluated: |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11055 | case EST_BasicNoexcept: |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11056 | case EST_DynamicNone: |
| 11057 | case EST_MSAny: |
| 11058 | case EST_None: |
| 11059 | break; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11060 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11061 | case EST_ComputedNoexcept: |
| 11062 | if (!Finder.TraverseStmt(Proto->getNoexceptExpr())) |
| 11063 | return true; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11064 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11065 | case EST_Dynamic: |
| 11066 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11067 | EEnd = Proto->exception_end(); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11068 | E != EEnd; ++E) { |
| 11069 | if (!Finder.TraverseType(*E)) |
| 11070 | return true; |
| 11071 | } |
| 11072 | break; |
| 11073 | } |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11074 | |
| 11075 | return false; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 11076 | } |
| 11077 | |
| 11078 | bool Sema::checkThisInStaticMemberFunctionAttributes(CXXMethodDecl *Method) { |
| 11079 | FindCXXThisExpr Finder(*this); |
| 11080 | |
| 11081 | // Check attributes. |
| 11082 | for (Decl::attr_iterator A = Method->attr_begin(), AEnd = Method->attr_end(); |
| 11083 | A != AEnd; ++A) { |
| 11084 | // FIXME: This should be emitted by tblgen. |
| 11085 | Expr *Arg = 0; |
| 11086 | ArrayRef<Expr *> Args; |
| 11087 | if (GuardedByAttr *G = dyn_cast<GuardedByAttr>(*A)) |
| 11088 | Arg = G->getArg(); |
| 11089 | else if (PtGuardedByAttr *G = dyn_cast<PtGuardedByAttr>(*A)) |
| 11090 | Arg = G->getArg(); |
| 11091 | else if (AcquiredAfterAttr *AA = dyn_cast<AcquiredAfterAttr>(*A)) |
| 11092 | Args = ArrayRef<Expr *>(AA->args_begin(), AA->args_size()); |
| 11093 | else if (AcquiredBeforeAttr *AB = dyn_cast<AcquiredBeforeAttr>(*A)) |
| 11094 | Args = ArrayRef<Expr *>(AB->args_begin(), AB->args_size()); |
| 11095 | else if (ExclusiveLockFunctionAttr *ELF |
| 11096 | = dyn_cast<ExclusiveLockFunctionAttr>(*A)) |
| 11097 | Args = ArrayRef<Expr *>(ELF->args_begin(), ELF->args_size()); |
| 11098 | else if (SharedLockFunctionAttr *SLF |
| 11099 | = dyn_cast<SharedLockFunctionAttr>(*A)) |
| 11100 | Args = ArrayRef<Expr *>(SLF->args_begin(), SLF->args_size()); |
| 11101 | else if (ExclusiveTrylockFunctionAttr *ETLF |
| 11102 | = dyn_cast<ExclusiveTrylockFunctionAttr>(*A)) { |
| 11103 | Arg = ETLF->getSuccessValue(); |
| 11104 | Args = ArrayRef<Expr *>(ETLF->args_begin(), ETLF->args_size()); |
| 11105 | } else if (SharedTrylockFunctionAttr *STLF |
| 11106 | = dyn_cast<SharedTrylockFunctionAttr>(*A)) { |
| 11107 | Arg = STLF->getSuccessValue(); |
| 11108 | Args = ArrayRef<Expr *>(STLF->args_begin(), STLF->args_size()); |
| 11109 | } else if (UnlockFunctionAttr *UF = dyn_cast<UnlockFunctionAttr>(*A)) |
| 11110 | Args = ArrayRef<Expr *>(UF->args_begin(), UF->args_size()); |
| 11111 | else if (LockReturnedAttr *LR = dyn_cast<LockReturnedAttr>(*A)) |
| 11112 | Arg = LR->getArg(); |
| 11113 | else if (LocksExcludedAttr *LE = dyn_cast<LocksExcludedAttr>(*A)) |
| 11114 | Args = ArrayRef<Expr *>(LE->args_begin(), LE->args_size()); |
| 11115 | else if (ExclusiveLocksRequiredAttr *ELR |
| 11116 | = dyn_cast<ExclusiveLocksRequiredAttr>(*A)) |
| 11117 | Args = ArrayRef<Expr *>(ELR->args_begin(), ELR->args_size()); |
| 11118 | else if (SharedLocksRequiredAttr *SLR |
| 11119 | = dyn_cast<SharedLocksRequiredAttr>(*A)) |
| 11120 | Args = ArrayRef<Expr *>(SLR->args_begin(), SLR->args_size()); |
| 11121 | |
| 11122 | if (Arg && !Finder.TraverseStmt(Arg)) |
| 11123 | return true; |
| 11124 | |
| 11125 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 11126 | if (!Finder.TraverseStmt(Args[I])) |
| 11127 | return true; |
| 11128 | } |
| 11129 | } |
| 11130 | |
| 11131 | return false; |
| 11132 | } |
| 11133 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11134 | void |
| 11135 | Sema::checkExceptionSpecification(ExceptionSpecificationType EST, |
| 11136 | ArrayRef<ParsedType> DynamicExceptions, |
| 11137 | ArrayRef<SourceRange> DynamicExceptionRanges, |
| 11138 | Expr *NoexceptExpr, |
| 11139 | llvm::SmallVectorImpl<QualType> &Exceptions, |
| 11140 | FunctionProtoType::ExtProtoInfo &EPI) { |
| 11141 | Exceptions.clear(); |
| 11142 | EPI.ExceptionSpecType = EST; |
| 11143 | if (EST == EST_Dynamic) { |
| 11144 | Exceptions.reserve(DynamicExceptions.size()); |
| 11145 | for (unsigned ei = 0, ee = DynamicExceptions.size(); ei != ee; ++ei) { |
| 11146 | // FIXME: Preserve type source info. |
| 11147 | QualType ET = GetTypeFromParser(DynamicExceptions[ei]); |
| 11148 | |
| 11149 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 11150 | collectUnexpandedParameterPacks(ET, Unexpanded); |
| 11151 | if (!Unexpanded.empty()) { |
| 11152 | DiagnoseUnexpandedParameterPacks(DynamicExceptionRanges[ei].getBegin(), |
| 11153 | UPPC_ExceptionType, |
| 11154 | Unexpanded); |
| 11155 | continue; |
| 11156 | } |
| 11157 | |
| 11158 | // Check that the type is valid for an exception spec, and |
| 11159 | // drop it if not. |
| 11160 | if (!CheckSpecifiedExceptionType(ET, DynamicExceptionRanges[ei])) |
| 11161 | Exceptions.push_back(ET); |
| 11162 | } |
| 11163 | EPI.NumExceptions = Exceptions.size(); |
| 11164 | EPI.Exceptions = Exceptions.data(); |
| 11165 | return; |
| 11166 | } |
| 11167 | |
| 11168 | if (EST == EST_ComputedNoexcept) { |
| 11169 | // If an error occurred, there's no expression here. |
| 11170 | if (NoexceptExpr) { |
| 11171 | assert((NoexceptExpr->isTypeDependent() || |
| 11172 | NoexceptExpr->getType()->getCanonicalTypeUnqualified() == |
| 11173 | Context.BoolTy) && |
| 11174 | "Parser should have made sure that the expression is boolean"); |
| 11175 | if (NoexceptExpr && DiagnoseUnexpandedParameterPack(NoexceptExpr)) { |
| 11176 | EPI.ExceptionSpecType = EST_BasicNoexcept; |
| 11177 | return; |
| 11178 | } |
| 11179 | |
| 11180 | if (!NoexceptExpr->isValueDependent()) |
| 11181 | NoexceptExpr = VerifyIntegerConstantExpression(NoexceptExpr, 0, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 11182 | diag::err_noexcept_needs_constant_expression, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 11183 | /*AllowFold*/ false).take(); |
| 11184 | EPI.NoexceptExpr = NoexceptExpr; |
| 11185 | } |
| 11186 | return; |
| 11187 | } |
| 11188 | } |
| 11189 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 11190 | /// IdentifyCUDATarget - Determine the CUDA compilation target for this function |
| 11191 | Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const FunctionDecl *D) { |
| 11192 | // Implicitly declared functions (e.g. copy constructors) are |
| 11193 | // __host__ __device__ |
| 11194 | if (D->isImplicit()) |
| 11195 | return CFT_HostDevice; |
| 11196 | |
| 11197 | if (D->hasAttr<CUDAGlobalAttr>()) |
| 11198 | return CFT_Global; |
| 11199 | |
| 11200 | if (D->hasAttr<CUDADeviceAttr>()) { |
| 11201 | if (D->hasAttr<CUDAHostAttr>()) |
| 11202 | return CFT_HostDevice; |
| 11203 | else |
| 11204 | return CFT_Device; |
| 11205 | } |
| 11206 | |
| 11207 | return CFT_Host; |
| 11208 | } |
| 11209 | |
| 11210 | bool Sema::CheckCUDATarget(CUDAFunctionTarget CallerTarget, |
| 11211 | CUDAFunctionTarget CalleeTarget) { |
| 11212 | // CUDA B.1.1 "The __device__ qualifier declares a function that is... |
| 11213 | // Callable from the device only." |
| 11214 | if (CallerTarget == CFT_Host && CalleeTarget == CFT_Device) |
| 11215 | return true; |
| 11216 | |
| 11217 | // CUDA B.1.2 "The __global__ qualifier declares a function that is... |
| 11218 | // Callable from the host only." |
| 11219 | // CUDA B.1.3 "The __host__ qualifier declares a function that is... |
| 11220 | // Callable from the host only." |
| 11221 | if ((CallerTarget == CFT_Device || CallerTarget == CFT_Global) && |
| 11222 | (CalleeTarget == CFT_Host || CalleeTarget == CFT_Global)) |
| 11223 | return true; |
| 11224 | |
| 11225 | if (CallerTarget == CFT_HostDevice && CalleeTarget != CFT_HostDevice) |
| 11226 | return true; |
| 11227 | |
| 11228 | return false; |
| 11229 | } |