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" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 22 | #include "clang/AST/CharUnits.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 23 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclVisitor.h" |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 26 | #include "clang/AST/RecordLayout.h" |
| 27 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 29 | #include "clang/AST/TypeOrdering.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 30 | #include "clang/Sema/DeclSpec.h" |
| 31 | #include "clang/Sema/ParsedTemplate.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 32 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 33 | #include "clang/Lex/Preprocessor.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 36 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 37 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace clang; |
| 40 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | // CheckDefaultArgumentVisitor |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 47 | /// the default argument of a parameter to determine whether it |
| 48 | /// contains any ill-formed subexpressions. For example, this will |
| 49 | /// diagnose the use of local variables or parameters within the |
| 50 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 51 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 52 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 53 | Expr *DefaultArg; |
| 54 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 56 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 58 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 60 | bool VisitExpr(Expr *Node); |
| 61 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 62 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 63 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 65 | /// VisitExpr - Visit all of the children of this expression. |
| 66 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 67 | bool IsInvalid = false; |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 68 | for (Stmt::child_range I = Node->children(); I; ++I) |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 69 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 70 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 73 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 74 | /// determine whether this declaration can be used in the default |
| 75 | /// argument expression. |
| 76 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 77 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 78 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 79 | // C++ [dcl.fct.default]p9 |
| 80 | // Default arguments are evaluated each time the function is |
| 81 | // called. The order of evaluation of function arguments is |
| 82 | // unspecified. Consequently, parameters of a function shall not |
| 83 | // be used in default argument expressions, even if they are not |
| 84 | // evaluated. Parameters of a function declared before a default |
| 85 | // argument expression are in scope and can hide namespace and |
| 86 | // class member names. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 88 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 89 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 90 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 91 | // C++ [dcl.fct.default]p7 |
| 92 | // Local variables shall not be used in default argument |
| 93 | // expressions. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 94 | if (VDecl->isLocalVarDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 96 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 97 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 98 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 99 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 100 | return false; |
| 101 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 102 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 103 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 104 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 105 | // C++ [dcl.fct.default]p8: |
| 106 | // The keyword this shall not be used in a default argument of a |
| 107 | // member function. |
| 108 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 109 | diag::err_param_default_argument_references_this) |
| 110 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 111 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 114 | void Sema::ImplicitExceptionSpecification::CalledDecl(CXXMethodDecl *Method) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 115 | assert(Context && "ImplicitExceptionSpecification without an ASTContext"); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 116 | // If we have an MSAny or unknown spec already, don't bother. |
| 117 | if (!Method || ComputedEST == EST_MSAny || ComputedEST == EST_Delayed) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 118 | return; |
| 119 | |
| 120 | const FunctionProtoType *Proto |
| 121 | = Method->getType()->getAs<FunctionProtoType>(); |
| 122 | |
| 123 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
| 124 | |
| 125 | // If this function can throw any exceptions, make a note of that. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 126 | if (EST == EST_Delayed || EST == EST_MSAny || EST == EST_None) { |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 127 | ClearExceptions(); |
| 128 | ComputedEST = EST; |
| 129 | return; |
| 130 | } |
| 131 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 132 | // FIXME: If the call to this decl is using any of its default arguments, we |
| 133 | // need to search them for potentially-throwing calls. |
| 134 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 135 | // If this function has a basic noexcept, it doesn't affect the outcome. |
| 136 | if (EST == EST_BasicNoexcept) |
| 137 | return; |
| 138 | |
| 139 | // If we have a throw-all spec at this point, ignore the function. |
| 140 | if (ComputedEST == EST_None) |
| 141 | return; |
| 142 | |
| 143 | // If we're still at noexcept(true) and there's a nothrow() callee, |
| 144 | // change to that specification. |
| 145 | if (EST == EST_DynamicNone) { |
| 146 | if (ComputedEST == EST_BasicNoexcept) |
| 147 | ComputedEST = EST_DynamicNone; |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | // Check out noexcept specs. |
| 152 | if (EST == EST_ComputedNoexcept) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 153 | FunctionProtoType::NoexceptResult NR = Proto->getNoexceptSpec(*Context); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 154 | assert(NR != FunctionProtoType::NR_NoNoexcept && |
| 155 | "Must have noexcept result for EST_ComputedNoexcept."); |
| 156 | assert(NR != FunctionProtoType::NR_Dependent && |
| 157 | "Should not generate implicit declarations for dependent cases, " |
| 158 | "and don't know how to handle them anyway."); |
| 159 | |
| 160 | // noexcept(false) -> no spec on the new function |
| 161 | if (NR == FunctionProtoType::NR_Throw) { |
| 162 | ClearExceptions(); |
| 163 | ComputedEST = EST_None; |
| 164 | } |
| 165 | // noexcept(true) won't change anything either. |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | assert(EST == EST_Dynamic && "EST case not considered earlier."); |
| 170 | assert(ComputedEST != EST_None && |
| 171 | "Shouldn't collect exceptions when throw-all is guaranteed."); |
| 172 | ComputedEST = EST_Dynamic; |
| 173 | // Record the exceptions in this function's exception specification. |
| 174 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 175 | EEnd = Proto->exception_end(); |
| 176 | E != EEnd; ++E) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 177 | if (ExceptionsSeen.insert(Context->getCanonicalType(*E))) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 178 | Exceptions.push_back(*E); |
| 179 | } |
| 180 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 181 | void Sema::ImplicitExceptionSpecification::CalledExpr(Expr *E) { |
| 182 | if (!E || ComputedEST == EST_MSAny || ComputedEST == EST_Delayed) |
| 183 | return; |
| 184 | |
| 185 | // FIXME: |
| 186 | // |
| 187 | // C++0x [except.spec]p14: |
NAKAMURA Takumi | 4857947 | 2011-06-21 03:19:28 +0000 | [diff] [blame] | 188 | // [An] implicit exception-specification specifies the type-id T if and |
| 189 | // only if T is allowed by the exception-specification of a function directly |
| 190 | // 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] | 191 | // function it directly invokes allows all exceptions, and f shall allow no |
| 192 | // exceptions if every function it directly invokes allows no exceptions. |
| 193 | // |
| 194 | // Note in particular that if an implicit exception-specification is generated |
| 195 | // for a function containing a throw-expression, that specification can still |
| 196 | // be noexcept(true). |
| 197 | // |
| 198 | // Note also that 'directly invoked' is not defined in the standard, and there |
| 199 | // is no indication that we should only consider potentially-evaluated calls. |
| 200 | // |
| 201 | // Ultimately we should implement the intent of the standard: the exception |
| 202 | // specification should be the set of exceptions which can be thrown by the |
| 203 | // implicit definition. For now, we assume that any non-nothrow expression can |
| 204 | // throw any exception. |
| 205 | |
| 206 | if (E->CanThrow(*Context)) |
| 207 | ComputedEST = EST_None; |
| 208 | } |
| 209 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 210 | bool |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 211 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | SourceLocation EqualLoc) { |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 213 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 214 | diag::err_typecheck_decl_incomplete_type)) { |
| 215 | Param->setInvalidDecl(); |
| 216 | return true; |
| 217 | } |
| 218 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 219 | // C++ [dcl.fct.default]p5 |
| 220 | // A default argument expression is implicitly converted (clause |
| 221 | // 4) to the parameter type. The default argument expression has |
| 222 | // the same semantic constraints as the initializer expression in |
| 223 | // a declaration of a variable of the parameter type, using the |
| 224 | // copy-initialization semantics (8.5). |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 225 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 226 | Param); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 227 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 228 | EqualLoc); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 229 | InitializationSequence InitSeq(*this, Entity, Kind, &Arg, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 230 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
Nico Weber | 6bb4dcb | 2010-11-28 22:53:37 +0000 | [diff] [blame] | 231 | MultiExprArg(*this, &Arg, 1)); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 232 | if (Result.isInvalid()) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 233 | return true; |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 234 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 235 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 236 | CheckImplicitConversions(Arg, EqualLoc); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 237 | Arg = MaybeCreateExprWithCleanups(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 239 | // Okay: add the default argument to the parameter |
| 240 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Douglas Gregor | 8cfb7a3 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 242 | // We have already instantiated this parameter; provide each of the |
| 243 | // instantiations with the uninstantiated default argument. |
| 244 | UnparsedDefaultArgInstantiationsMap::iterator InstPos |
| 245 | = UnparsedDefaultArgInstantiations.find(Param); |
| 246 | if (InstPos != UnparsedDefaultArgInstantiations.end()) { |
| 247 | for (unsigned I = 0, N = InstPos->second.size(); I != N; ++I) |
| 248 | InstPos->second[I]->setUninstantiatedDefaultArg(Arg); |
| 249 | |
| 250 | // We're done tracking this parameter's instantiations. |
| 251 | UnparsedDefaultArgInstantiations.erase(InstPos); |
| 252 | } |
| 253 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 254 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 257 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 258 | /// provided for a function parameter is well-formed. If so, attach it |
| 259 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 260 | void |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 261 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 262 | Expr *DefaultArg) { |
| 263 | if (!param || !DefaultArg) |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 264 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 266 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 267 | UnparsedDefaultArgLocs.erase(Param); |
| 268 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 269 | // Default arguments are only permitted in C++ |
| 270 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 271 | Diag(EqualLoc, diag::err_param_default_argument) |
| 272 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 273 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 277 | // Check for unexpanded parameter packs. |
| 278 | if (DiagnoseUnexpandedParameterPack(DefaultArg, UPPC_DefaultArgument)) { |
| 279 | Param->setInvalidDecl(); |
| 280 | return; |
| 281 | } |
| 282 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 283 | // Check that the default argument is well-formed |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 284 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 285 | if (DefaultArgChecker.Visit(DefaultArg)) { |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 286 | Param->setInvalidDecl(); |
| 287 | return; |
| 288 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 290 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 293 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 294 | /// argument for a function parameter, but we can't parse it yet |
| 295 | /// because we're inside a class definition. Note that this default |
| 296 | /// argument will be parsed later. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 297 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 298 | SourceLocation EqualLoc, |
| 299 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 300 | if (!param) |
| 301 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 303 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 304 | if (Param) |
| 305 | Param->setUnparsedDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 307 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 310 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 311 | /// the default argument for the parameter param failed. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 312 | void Sema::ActOnParamDefaultArgumentError(Decl *param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 313 | if (!param) |
| 314 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 316 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 318 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 320 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 323 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 324 | /// arguments in the declarator, which is not a function declaration |
| 325 | /// or definition and therefore is not permitted to have default |
| 326 | /// arguments. This routine should be invoked for every declarator |
| 327 | /// that is not a function declaration or definition. |
| 328 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 329 | // C++ [dcl.fct.default]p3 |
| 330 | // A default argument expression shall be specified only in the |
| 331 | // parameter-declaration-clause of a function declaration or in a |
| 332 | // template-parameter (14.1). It shall not be specified for a |
| 333 | // parameter pack. If it is specified in a |
| 334 | // parameter-declaration-clause, it shall not occur within a |
| 335 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 336 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 337 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 338 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 339 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 340 | ParmVarDecl *Param = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 341 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 342 | if (Param->hasUnparsedDefaultArg()) { |
| 343 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 344 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 345 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 346 | delete Toks; |
| 347 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 348 | } else if (Param->getDefaultArg()) { |
| 349 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 350 | << Param->getDefaultArg()->getSourceRange(); |
| 351 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 358 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 359 | // function, once we already know that they have the same |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 360 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 361 | // error, false otherwise. |
| 362 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 363 | bool Invalid = false; |
| 364 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 365 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 366 | // For non-template functions, default arguments can be added in |
| 367 | // later declarations of a function in the same |
| 368 | // scope. Declarations in different scopes have completely |
| 369 | // distinct sets of default arguments. That is, declarations in |
| 370 | // inner scopes do not acquire default arguments from |
| 371 | // declarations in outer scopes, and vice versa. In a given |
| 372 | // function declaration, all parameters subsequent to a |
| 373 | // parameter with a default argument shall have default |
| 374 | // arguments supplied in this or previous declarations. A |
| 375 | // default argument shall not be redefined by a later |
| 376 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 377 | // |
| 378 | // C++ [dcl.fct.default]p6: |
| 379 | // Except for member functions of class templates, the default arguments |
| 380 | // in a member function definition that appears outside of the class |
| 381 | // definition are added to the set of default arguments provided by the |
| 382 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 383 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 384 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 385 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 386 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 387 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 388 | |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 389 | unsigned DiagDefaultParamID = |
| 390 | diag::err_param_default_argument_redefinition; |
| 391 | |
| 392 | // MSVC accepts that default parameters be redefined for member functions |
| 393 | // of template class. The new default parameter's value is ignored. |
| 394 | Invalid = true; |
| 395 | if (getLangOptions().Microsoft) { |
| 396 | CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(New); |
| 397 | if (MD && MD->getParent()->getDescribedClassTemplate()) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 398 | // Merge the old default argument into the new parameter. |
| 399 | NewParam->setHasInheritedDefaultArg(); |
| 400 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 401 | NewParam->setUninstantiatedDefaultArg( |
| 402 | OldParam->getUninstantiatedDefaultArg()); |
| 403 | else |
| 404 | NewParam->setDefaultArg(OldParam->getInit()); |
Francois Pichet | cf320c6 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 405 | DiagDefaultParamID = diag::warn_param_default_argument_redefinition; |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 406 | Invalid = false; |
| 407 | } |
| 408 | } |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 409 | |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 410 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 411 | // hint here. Alternatively, we could walk the type-source information |
| 412 | // for NewParam to find the last source location in the type... but it |
| 413 | // isn't worth the effort right now. This is the kind of test case that |
| 414 | // is hard to get right: |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 415 | // int f(int); |
| 416 | // void g(int (*fp)(int) = f); |
| 417 | // void g(int (*fp)(int) = &f); |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 418 | Diag(NewParam->getLocation(), DiagDefaultParamID) |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 419 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 420 | |
| 421 | // Look for the function declaration where the default argument was |
| 422 | // actually written, which may be a declaration prior to Old. |
| 423 | for (FunctionDecl *Older = Old->getPreviousDeclaration(); |
| 424 | Older; Older = Older->getPreviousDeclaration()) { |
| 425 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 426 | break; |
| 427 | |
| 428 | OldParam = Older->getParamDecl(p); |
| 429 | } |
| 430 | |
| 431 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 432 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 433 | } else if (OldParam->hasDefaultArg()) { |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 434 | // Merge the old default argument into the new parameter. |
| 435 | // It's important to use getInit() here; getDefaultArg() |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 436 | // strips off any top-level ExprWithCleanups. |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 437 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 438 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 439 | NewParam->setUninstantiatedDefaultArg( |
| 440 | OldParam->getUninstantiatedDefaultArg()); |
| 441 | else |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 442 | NewParam->setDefaultArg(OldParam->getInit()); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 443 | } else if (NewParam->hasDefaultArg()) { |
| 444 | if (New->getDescribedFunctionTemplate()) { |
| 445 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 446 | Diag(NewParam->getLocation(), |
| 447 | diag::err_param_default_argument_template_redecl) |
| 448 | << NewParam->getDefaultArgRange(); |
| 449 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 450 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 451 | } else if (New->getTemplateSpecializationKind() |
| 452 | != TSK_ImplicitInstantiation && |
| 453 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 454 | // C++ [temp.expr.spec]p21: |
| 455 | // Default function arguments shall not be specified in a declaration |
| 456 | // or a definition for one of the following explicit specializations: |
| 457 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 458 | // - the explicit specialization of a member function template; |
| 459 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 460 | // template where the class template specialization to which the |
| 461 | // member function specialization belongs is implicitly |
| 462 | // instantiated. |
| 463 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 464 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 465 | << New->getDeclName() |
| 466 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 467 | } else if (New->getDeclContext()->isDependentContext()) { |
| 468 | // C++ [dcl.fct.default]p6 (DR217): |
| 469 | // Default arguments for a member function of a class template shall |
| 470 | // be specified on the initial declaration of the member function |
| 471 | // within the class template. |
| 472 | // |
| 473 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 474 | // leads me to the conclusion that one cannot add default function |
| 475 | // arguments for an out-of-line definition of a member function of a |
| 476 | // dependent type. |
| 477 | int WhichKind = 2; |
| 478 | if (CXXRecordDecl *Record |
| 479 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 480 | if (Record->getDescribedClassTemplate()) |
| 481 | WhichKind = 0; |
| 482 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 483 | WhichKind = 1; |
| 484 | else |
| 485 | WhichKind = 2; |
| 486 | } |
| 487 | |
| 488 | Diag(NewParam->getLocation(), |
| 489 | diag::err_param_default_argument_member_template_redecl) |
| 490 | << WhichKind |
| 491 | << NewParam->getDefaultArgRange(); |
Sean Hunt | 9ae60d5 | 2011-05-26 01:26:05 +0000 | [diff] [blame] | 492 | } else if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(New)) { |
| 493 | CXXSpecialMember NewSM = getSpecialMember(Ctor), |
| 494 | OldSM = getSpecialMember(cast<CXXConstructorDecl>(Old)); |
| 495 | if (NewSM != OldSM) { |
| 496 | Diag(NewParam->getLocation(),diag::warn_default_arg_makes_ctor_special) |
| 497 | << NewParam->getDefaultArgRange() << NewSM; |
| 498 | Diag(Old->getLocation(), diag::note_previous_declaration_special) |
| 499 | << OldSM; |
| 500 | } |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 501 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 505 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 506 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 507 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 508 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 511 | /// \brief Merge the exception specifications of two variable declarations. |
| 512 | /// |
| 513 | /// This is called when there's a redeclaration of a VarDecl. The function |
| 514 | /// checks if the redeclaration might have an exception specification and |
| 515 | /// validates compatibility and merges the specs if necessary. |
| 516 | void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) { |
| 517 | // Shortcut if exceptions are disabled. |
| 518 | if (!getLangOptions().CXXExceptions) |
| 519 | return; |
| 520 | |
| 521 | assert(Context.hasSameType(New->getType(), Old->getType()) && |
| 522 | "Should only be called if types are otherwise the same."); |
| 523 | |
| 524 | QualType NewType = New->getType(); |
| 525 | QualType OldType = Old->getType(); |
| 526 | |
| 527 | // We're only interested in pointers and references to functions, as well |
| 528 | // as pointers to member functions. |
| 529 | if (const ReferenceType *R = NewType->getAs<ReferenceType>()) { |
| 530 | NewType = R->getPointeeType(); |
| 531 | OldType = OldType->getAs<ReferenceType>()->getPointeeType(); |
| 532 | } else if (const PointerType *P = NewType->getAs<PointerType>()) { |
| 533 | NewType = P->getPointeeType(); |
| 534 | OldType = OldType->getAs<PointerType>()->getPointeeType(); |
| 535 | } else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) { |
| 536 | NewType = M->getPointeeType(); |
| 537 | OldType = OldType->getAs<MemberPointerType>()->getPointeeType(); |
| 538 | } |
| 539 | |
| 540 | if (!NewType->isFunctionProtoType()) |
| 541 | return; |
| 542 | |
| 543 | // There's lots of special cases for functions. For function pointers, system |
| 544 | // libraries are hopefully not as broken so that we don't need these |
| 545 | // workarounds. |
| 546 | if (CheckEquivalentExceptionSpec( |
| 547 | OldType->getAs<FunctionProtoType>(), Old->getLocation(), |
| 548 | NewType->getAs<FunctionProtoType>(), New->getLocation())) { |
| 549 | New->setInvalidDecl(); |
| 550 | } |
| 551 | } |
| 552 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 553 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 554 | /// function declaration are well-formed according to C++ |
| 555 | /// [dcl.fct.default]. |
| 556 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 557 | unsigned NumParams = FD->getNumParams(); |
| 558 | unsigned p; |
| 559 | |
| 560 | // Find first parameter with a default argument |
| 561 | for (p = 0; p < NumParams; ++p) { |
| 562 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 563 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 564 | break; |
| 565 | } |
| 566 | |
| 567 | // C++ [dcl.fct.default]p4: |
| 568 | // In a given function declaration, all parameters |
| 569 | // subsequent to a parameter with a default argument shall |
| 570 | // have default arguments supplied in this or previous |
| 571 | // declarations. A default argument shall not be redefined |
| 572 | // by a later declaration (not even to the same value). |
| 573 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 575 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 576 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 577 | if (Param->isInvalidDecl()) |
| 578 | /* We already complained about this parameter. */; |
| 579 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 581 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 582 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 583 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 585 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 587 | LastMissingDefaultArg = p; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | if (LastMissingDefaultArg > 0) { |
| 592 | // Some default arguments were missing. Clear out all of the |
| 593 | // default arguments up to (and including) the last missing |
| 594 | // default argument, so that we leave the function parameters |
| 595 | // in a semantically valid state. |
| 596 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 597 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 598 | if (Param->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 599 | Param->setDefaultArg(0); |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 605 | /// isCurrentClassName - Determine whether the identifier II is the |
| 606 | /// name of the class type currently being defined. In the case of |
| 607 | /// nested classes, this will only return true if II is the name of |
| 608 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 609 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 610 | const CXXScopeSpec *SS) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 611 | assert(getLangOptions().CPlusPlus && "No class names in C!"); |
| 612 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 613 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 614 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 615 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 616 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 617 | } else |
| 618 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 619 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 620 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 621 | return &II == CurDecl->getIdentifier(); |
| 622 | else |
| 623 | return false; |
| 624 | } |
| 625 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 627 | /// |
| 628 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 629 | /// and returns NULL otherwise. |
| 630 | CXXBaseSpecifier * |
| 631 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 632 | SourceRange SpecifierRange, |
| 633 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 634 | TypeSourceInfo *TInfo, |
| 635 | SourceLocation EllipsisLoc) { |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 636 | QualType BaseType = TInfo->getType(); |
| 637 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 638 | // C++ [class.union]p1: |
| 639 | // A union shall not have base classes. |
| 640 | if (Class->isUnion()) { |
| 641 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 642 | << SpecifierRange; |
| 643 | return 0; |
| 644 | } |
| 645 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 646 | if (EllipsisLoc.isValid() && |
| 647 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 648 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 649 | << TInfo->getTypeLoc().getSourceRange(); |
| 650 | EllipsisLoc = SourceLocation(); |
| 651 | } |
| 652 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 653 | if (BaseType->isDependentType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 655 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 656 | Access, TInfo, EllipsisLoc); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 657 | |
| 658 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 659 | |
| 660 | // Base specifiers must be record types. |
| 661 | if (!BaseType->isRecordType()) { |
| 662 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | // C++ [class.union]p1: |
| 667 | // A union shall not be used as a base class. |
| 668 | if (BaseType->isUnionType()) { |
| 669 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | // C++ [class.derived]p2: |
| 674 | // The class-name in a base-specifier shall not be an incompletely |
| 675 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 677 | PDiag(diag::err_incomplete_base_class) |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 678 | << SpecifierRange)) { |
| 679 | Class->setInvalidDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 680 | return 0; |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 681 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 682 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 683 | // 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] | 684 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 685 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 686 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 687 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 688 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 689 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 690 | |
Anders Carlsson | 1d20927 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 691 | // C++ [class]p3: |
| 692 | // If a class is marked final and it appears as a base-type-specifier in |
| 693 | // base-clause, the program is ill-formed. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 694 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 695 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
| 696 | << CXXBaseDecl->getDeclName(); |
| 697 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 698 | << CXXBaseDecl->getDeclName(); |
| 699 | return 0; |
| 700 | } |
| 701 | |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 702 | if (BaseDecl->isInvalidDecl()) |
| 703 | Class->setInvalidDecl(); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 704 | |
| 705 | // Create the base specifier. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 706 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 707 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 708 | Access, TInfo, EllipsisLoc); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 711 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 712 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | /// example: |
| 714 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 715 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 716 | BaseResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 717 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 718 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 719 | ParsedType basetype, SourceLocation BaseLoc, |
| 720 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 721 | if (!classdecl) |
| 722 | return true; |
| 723 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 724 | AdjustDeclIfTemplate(classdecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 725 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 726 | if (!Class) |
| 727 | return true; |
| 728 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 729 | TypeSourceInfo *TInfo = 0; |
| 730 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 731 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 732 | if (EllipsisLoc.isInvalid() && |
| 733 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 734 | UPPC_BaseType)) |
| 735 | return true; |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 736 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 737 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 738 | Virtual, Access, TInfo, |
| 739 | EllipsisLoc)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 740 | return BaseSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 742 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 743 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 744 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 745 | /// \brief Performs the actual work of attaching the given base class |
| 746 | /// specifiers to a C++ class. |
| 747 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 748 | unsigned NumBases) { |
| 749 | if (NumBases == 0) |
| 750 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 751 | |
| 752 | // Used to keep track of which base types we have already seen, so |
| 753 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 754 | // that the key is always the unqualified canonical type of the base |
| 755 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 756 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 757 | |
| 758 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 759 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 760 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 761 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 763 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 764 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 765 | if (KnownBaseTypes[NewBaseType]) { |
| 766 | // C++ [class.mi]p3: |
| 767 | // A class shall not be specified as a direct base class of a |
| 768 | // derived class more than once. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 769 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 770 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 771 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 772 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 773 | |
| 774 | // Delete the duplicate base class specifier; we're going to |
| 775 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 776 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 777 | |
| 778 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 779 | } else { |
| 780 | // Okay, add this new base class. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 781 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 782 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
| 786 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 787 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 788 | |
| 789 | // Delete the remaining (good) base class specifiers, since their |
| 790 | // data has been copied into the CXXRecordDecl. |
| 791 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 792 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 793 | |
| 794 | return Invalid; |
| 795 | } |
| 796 | |
| 797 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 798 | /// class, after checking whether there are any duplicate base |
| 799 | /// classes. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 800 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, BaseTy **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 801 | unsigned NumBases) { |
| 802 | if (!ClassDecl || !Bases || !NumBases) |
| 803 | return; |
| 804 | |
| 805 | AdjustDeclIfTemplate(ClassDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 806 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 807 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 808 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 809 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 810 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 811 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 812 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 813 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 814 | return ICT->getDecl(); |
| 815 | else |
| 816 | return 0; |
| 817 | } |
| 818 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 819 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 820 | /// derived from the type \p Base. |
| 821 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 822 | if (!getLangOptions().CPlusPlus) |
| 823 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 824 | |
| 825 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 826 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 827 | return false; |
| 828 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 829 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 830 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 831 | return false; |
| 832 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 833 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 834 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 838 | /// derived from the type \p Base. |
| 839 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 840 | if (!getLangOptions().CPlusPlus) |
| 841 | return false; |
| 842 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 843 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 844 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 845 | return false; |
| 846 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 847 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 848 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 849 | return false; |
| 850 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 851 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 852 | } |
| 853 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 854 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 855 | CXXCastPath &BasePathArray) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 856 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 857 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 858 | |
| 859 | const CXXBasePath &Path = Paths.front(); |
| 860 | |
| 861 | // We first go backward and check if we have a virtual base. |
| 862 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 863 | // the nearest virtual base. |
| 864 | unsigned Start = 0; |
| 865 | for (unsigned I = Path.size(); I != 0; --I) { |
| 866 | if (Path[I - 1].Base->isVirtual()) { |
| 867 | Start = I - 1; |
| 868 | break; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | // Now add all bases. |
| 873 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 874 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 877 | /// \brief Determine whether the given base path includes a virtual |
| 878 | /// base class. |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 879 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 880 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 881 | BEnd = BasePath.end(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 882 | B != BEnd; ++B) |
| 883 | if ((*B)->isVirtual()) |
| 884 | return true; |
| 885 | |
| 886 | return false; |
| 887 | } |
| 888 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 889 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 890 | /// conversion (where Derived and Base are class types) is |
| 891 | /// well-formed, meaning that the conversion is unambiguous (and |
| 892 | /// that all of the base classes are accessible). Returns true |
| 893 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 894 | /// otherwise. Loc is the location where this routine should point to |
| 895 | /// if there is an error, and Range is the source range to highlight |
| 896 | /// if there is an error. |
| 897 | bool |
| 898 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 899 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 900 | unsigned AmbigiousBaseConvID, |
| 901 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 902 | DeclarationName Name, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 903 | CXXCastPath *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 904 | // First, determine whether the path from Derived to Base is |
| 905 | // ambiguous. This is slightly more expensive than checking whether |
| 906 | // the Derived to Base conversion exists, because here we need to |
| 907 | // explore multiple paths to determine if there is an ambiguity. |
| 908 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 909 | /*DetectVirtual=*/false); |
| 910 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 911 | assert(DerivationOkay && |
| 912 | "Can only be used with a derived-to-base conversion"); |
| 913 | (void)DerivationOkay; |
| 914 | |
| 915 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 916 | if (InaccessibleBaseID) { |
| 917 | // Check that the base class can be accessed. |
| 918 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 919 | InaccessibleBaseID)) { |
| 920 | case AR_inaccessible: |
| 921 | return true; |
| 922 | case AR_accessible: |
| 923 | case AR_dependent: |
| 924 | case AR_delayed: |
| 925 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 926 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 927 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 928 | |
| 929 | // Build a base path if necessary. |
| 930 | if (BasePath) |
| 931 | BuildBasePathArray(Paths, *BasePath); |
| 932 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | // We know that the derived-to-base conversion is ambiguous, and |
| 936 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 937 | // search just one more time to compute all of the possible paths so |
| 938 | // that we can print them out. This is more expensive than any of |
| 939 | // the previous derived-to-base checks we've done, but at this point |
| 940 | // performance isn't as much of an issue. |
| 941 | Paths.clear(); |
| 942 | Paths.setRecordingPaths(true); |
| 943 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 944 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 945 | (void)StillOkay; |
| 946 | |
| 947 | // Build up a textual representation of the ambiguous paths, e.g., |
| 948 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 949 | // conversions in the diagnostic. We only print one of the paths |
| 950 | // to each base class subobject. |
| 951 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 952 | |
| 953 | Diag(Loc, AmbigiousBaseConvID) |
| 954 | << Derived << Base << PathDisplayStr << Range << Name; |
| 955 | return true; |
| 956 | } |
| 957 | |
| 958 | bool |
| 959 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 960 | SourceLocation Loc, SourceRange Range, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 961 | CXXCastPath *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 962 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 963 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 964 | IgnoreAccess ? 0 |
| 965 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 966 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 967 | Loc, Range, DeclarationName(), |
| 968 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | |
| 972 | /// @brief Builds a string representing ambiguous paths from a |
| 973 | /// specific derived class to different subobjects of the same base |
| 974 | /// class. |
| 975 | /// |
| 976 | /// This function builds a string that can be used in error messages |
| 977 | /// to show the different paths that one can take through the |
| 978 | /// inheritance hierarchy to go from the derived class to different |
| 979 | /// subobjects of a base class. The result looks something like this: |
| 980 | /// @code |
| 981 | /// struct D -> struct B -> struct A |
| 982 | /// struct D -> struct C -> struct A |
| 983 | /// @endcode |
| 984 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 985 | std::string PathDisplayStr; |
| 986 | std::set<unsigned> DisplayedPaths; |
| 987 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 988 | Path != Paths.end(); ++Path) { |
| 989 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 990 | // We haven't displayed a path to this particular base |
| 991 | // class subobject yet. |
| 992 | PathDisplayStr += "\n "; |
| 993 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 994 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 995 | Element != Path->end(); ++Element) |
| 996 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | return PathDisplayStr; |
| 1001 | } |
| 1002 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1003 | //===----------------------------------------------------------------------===// |
| 1004 | // C++ class member Handling |
| 1005 | //===----------------------------------------------------------------------===// |
| 1006 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1007 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1008 | Decl *Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 1009 | SourceLocation ASLoc, |
| 1010 | SourceLocation ColonLoc) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1011 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1012 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1013 | ASLoc, ColonLoc); |
| 1014 | CurContext->addHiddenDecl(ASDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1015 | return ASDecl; |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1018 | /// CheckOverrideControl - Check C++0x override control semantics. |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1019 | void Sema::CheckOverrideControl(const Decl *D) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1020 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1021 | if (!MD || !MD->isVirtual()) |
| 1022 | return; |
| 1023 | |
Anders Carlsson | 3ffe183 | 2011-01-20 06:33:26 +0000 | [diff] [blame] | 1024 | if (MD->isDependentContext()) |
| 1025 | return; |
| 1026 | |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1027 | // C++0x [class.virtual]p3: |
| 1028 | // If a virtual function is marked with the virt-specifier override and does |
| 1029 | // not override a member function of a base class, |
| 1030 | // the program is ill-formed. |
| 1031 | bool HasOverriddenMethods = |
| 1032 | MD->begin_overridden_methods() != MD->end_overridden_methods(); |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1033 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) { |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1034 | Diag(MD->getLocation(), |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1035 | diag::err_function_marked_override_not_overriding) |
| 1036 | << MD->getDeclName(); |
| 1037 | return; |
| 1038 | } |
| 1039 | } |
| 1040 | |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1041 | /// CheckIfOverriddenFunctionIsMarkedFinal - Checks whether a virtual member |
| 1042 | /// function overrides a virtual member function marked 'final', according to |
| 1043 | /// C++0x [class.virtual]p3. |
| 1044 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 1045 | const CXXMethodDecl *Old) { |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1046 | if (!Old->hasAttr<FinalAttr>()) |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1047 | return false; |
| 1048 | |
| 1049 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 1050 | << New->getDeclName(); |
| 1051 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 1052 | return true; |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1055 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 1056 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1057 | /// bitfield width if there is one, 'InitExpr' specifies the initializer if |
| 1058 | /// one has been parsed, and 'HasDeferredInit' is true if an initializer is |
| 1059 | /// present but parsing it has been deferred. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1060 | Decl * |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1061 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1062 | MultiTemplateParamsArg TemplateParameterLists, |
Anders Carlsson | 69a8735 | 2011-01-20 03:57:25 +0000 | [diff] [blame] | 1063 | ExprTy *BW, const VirtSpecifiers &VS, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1064 | ExprTy *InitExpr, bool HasDeferredInit, |
| 1065 | bool IsDefinition) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1066 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1067 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 1068 | DeclarationName Name = NameInfo.getName(); |
| 1069 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1070 | |
| 1071 | // For anonymous bitfields, the location should point to the type. |
| 1072 | if (Loc.isInvalid()) |
| 1073 | Loc = D.getSourceRange().getBegin(); |
| 1074 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1075 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 1076 | Expr *Init = static_cast<Expr*>(InitExpr); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1077 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1078 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1079 | assert(!DS.isFriendSpecified()); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1080 | assert(!Init || !HasDeferredInit); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1081 | |
Richard Smith | 1ab0d90 | 2011-06-25 02:28:38 +0000 | [diff] [blame] | 1082 | bool isFunc = D.isDeclarationOfFunction(); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1083 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1084 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 1085 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1086 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 1087 | // data members and cannot be applied to names declared const or static, |
| 1088 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1089 | switch (DS.getStorageClassSpec()) { |
| 1090 | case DeclSpec::SCS_unspecified: |
| 1091 | case DeclSpec::SCS_typedef: |
| 1092 | case DeclSpec::SCS_static: |
| 1093 | // FALL THROUGH. |
| 1094 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1095 | case DeclSpec::SCS_mutable: |
| 1096 | if (isFunc) { |
| 1097 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1098 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1099 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1100 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 1102 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 1103 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1104 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1105 | } |
| 1106 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1107 | default: |
| 1108 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1109 | Diag(DS.getStorageClassSpecLoc(), |
| 1110 | diag::err_storageclass_invalid_for_member); |
| 1111 | else |
| 1112 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 1113 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 1114 | } |
| 1115 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1116 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 1117 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 1118 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1119 | |
| 1120 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1121 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1122 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 1123 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1124 | if (SS.isSet() && !SS.isInvalid()) { |
| 1125 | // The user provided a superfluous scope specifier inside a class |
| 1126 | // definition: |
| 1127 | // |
| 1128 | // class X { |
| 1129 | // int X::member; |
| 1130 | // }; |
| 1131 | DeclContext *DC = 0; |
| 1132 | if ((DC = computeDeclContext(SS, false)) && DC->Equals(CurContext)) |
| 1133 | Diag(D.getIdentifierLoc(), diag::warn_member_extra_qualification) |
| 1134 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
| 1135 | else |
| 1136 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 1137 | << Name << SS.getRange(); |
| 1138 | |
| 1139 | SS.clear(); |
| 1140 | } |
| 1141 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1142 | // FIXME: Check for template parameters! |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 1143 | // FIXME: Check that the name is an identifier! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1144 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1145 | HasDeferredInit, AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1146 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1147 | } else { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1148 | assert(!HasDeferredInit); |
| 1149 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1150 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1151 | if (!Member) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1152 | return 0; |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1153 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1154 | |
| 1155 | // Non-instance-fields can't have a bitfield. |
| 1156 | if (BitWidth) { |
| 1157 | if (Member->isInvalidDecl()) { |
| 1158 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 1159 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1160 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 1161 | // "static member 'A' cannot be a bit-field" |
| 1162 | Diag(Loc, diag::err_static_not_bitfield) |
| 1163 | << Name << BitWidth->getSourceRange(); |
| 1164 | } else if (isa<TypedefDecl>(Member)) { |
| 1165 | // "typedef member 'x' cannot be a bit-field" |
| 1166 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 1167 | << Name << BitWidth->getSourceRange(); |
| 1168 | } else { |
| 1169 | // A function typedef ("typedef int f(); f a;"). |
| 1170 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 1171 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1172 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1173 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1174 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1176 | BitWidth = 0; |
| 1177 | Member->setInvalidDecl(); |
| 1178 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1179 | |
| 1180 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1182 | // If we have declared a member function template, set the access of the |
| 1183 | // templated declaration as well. |
| 1184 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 1185 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1186 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1187 | |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1188 | if (VS.isOverrideSpecified()) { |
| 1189 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 1190 | if (!MD || !MD->isVirtual()) { |
| 1191 | Diag(Member->getLocStart(), |
| 1192 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1193 | << "override" << FixItHint::CreateRemoval(VS.getOverrideLoc()); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1194 | } else |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1195 | MD->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context)); |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1196 | } |
| 1197 | if (VS.isFinalSpecified()) { |
| 1198 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 1199 | if (!MD || !MD->isVirtual()) { |
| 1200 | Diag(Member->getLocStart(), |
| 1201 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1202 | << "final" << FixItHint::CreateRemoval(VS.getFinalLoc()); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1203 | } else |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1204 | MD->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context)); |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1205 | } |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1206 | |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1207 | if (VS.getLastLocation().isValid()) { |
| 1208 | // Update the end location of a method that has a virt-specifiers. |
| 1209 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 1210 | MD->setRangeEnd(VS.getLastLocation()); |
| 1211 | } |
| 1212 | |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1213 | CheckOverrideControl(Member); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1214 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1215 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1216 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 1217 | if (Init) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1218 | AddInitializerToDecl(Member, Init, false, |
| 1219 | DS.getTypeSpecType() == DeclSpec::TST_auto); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1220 | else if (DS.getTypeSpecType() == DeclSpec::TST_auto && |
| 1221 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 1222 | // C++0x [dcl.spec.auto]p4: 'auto' can only be used in the type of a static |
| 1223 | // data member if a brace-or-equal-initializer is provided. |
| 1224 | Diag(Loc, diag::err_auto_var_requires_init) |
| 1225 | << Name << cast<ValueDecl>(Member)->getType(); |
| 1226 | Member->setInvalidDecl(); |
| 1227 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1228 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1229 | FinalizeDeclaration(Member); |
| 1230 | |
John McCall | b25b295 | 2011-02-15 07:12:36 +0000 | [diff] [blame] | 1231 | if (isInstField) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1232 | FieldCollector->Add(cast<FieldDecl>(Member)); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1233 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1236 | /// ActOnCXXInClassMemberInitializer - This is invoked after parsing an |
Richard Smith | 0ff6f8f | 2011-07-20 00:12:52 +0000 | [diff] [blame] | 1237 | /// in-class initializer for a non-static C++ class member, and after |
| 1238 | /// instantiating an in-class initializer in a class template. Such actions |
| 1239 | /// are deferred until the class is complete. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1240 | void |
| 1241 | Sema::ActOnCXXInClassMemberInitializer(Decl *D, SourceLocation EqualLoc, |
| 1242 | Expr *InitExpr) { |
| 1243 | FieldDecl *FD = cast<FieldDecl>(D); |
| 1244 | |
| 1245 | if (!InitExpr) { |
| 1246 | FD->setInvalidDecl(); |
| 1247 | FD->removeInClassInitializer(); |
| 1248 | return; |
| 1249 | } |
| 1250 | |
| 1251 | ExprResult Init = InitExpr; |
| 1252 | if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent()) { |
| 1253 | // FIXME: if there is no EqualLoc, this is list-initialization. |
| 1254 | Init = PerformCopyInitialization( |
| 1255 | InitializedEntity::InitializeMember(FD), EqualLoc, InitExpr); |
| 1256 | if (Init.isInvalid()) { |
| 1257 | FD->setInvalidDecl(); |
| 1258 | return; |
| 1259 | } |
| 1260 | |
| 1261 | CheckImplicitConversions(Init.get(), EqualLoc); |
| 1262 | } |
| 1263 | |
| 1264 | // C++0x [class.base.init]p7: |
| 1265 | // The initialization of each base and member constitutes a |
| 1266 | // full-expression. |
| 1267 | Init = MaybeCreateExprWithCleanups(Init); |
| 1268 | if (Init.isInvalid()) { |
| 1269 | FD->setInvalidDecl(); |
| 1270 | return; |
| 1271 | } |
| 1272 | |
| 1273 | InitExpr = Init.release(); |
| 1274 | |
| 1275 | FD->setInClassInitializer(InitExpr); |
| 1276 | } |
| 1277 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1278 | /// \brief Find the direct and/or virtual base specifiers that |
| 1279 | /// correspond to the given base type, for use in base initialization |
| 1280 | /// within a constructor. |
| 1281 | static bool FindBaseInitializer(Sema &SemaRef, |
| 1282 | CXXRecordDecl *ClassDecl, |
| 1283 | QualType BaseType, |
| 1284 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 1285 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 1286 | // First, check for a direct base class. |
| 1287 | DirectBaseSpec = 0; |
| 1288 | for (CXXRecordDecl::base_class_const_iterator Base |
| 1289 | = ClassDecl->bases_begin(); |
| 1290 | Base != ClassDecl->bases_end(); ++Base) { |
| 1291 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 1292 | // We found a direct base of this type. That's what we're |
| 1293 | // initializing. |
| 1294 | DirectBaseSpec = &*Base; |
| 1295 | break; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | // Check for a virtual base class. |
| 1300 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1301 | // there are no virtual bases. |
| 1302 | VirtualBaseSpec = 0; |
| 1303 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1304 | // We haven't found a base yet; search the class hierarchy for a |
| 1305 | // virtual base class. |
| 1306 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1307 | /*DetectVirtual=*/false); |
| 1308 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1309 | BaseType, Paths)) { |
| 1310 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1311 | Path != Paths.end(); ++Path) { |
| 1312 | if (Path->back().Base->isVirtual()) { |
| 1313 | VirtualBaseSpec = Path->back().Base; |
| 1314 | break; |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | return DirectBaseSpec || VirtualBaseSpec; |
| 1321 | } |
| 1322 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1323 | /// ActOnMemInitializer - Handle a C++ member initializer. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1324 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1325 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1326 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1327 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1328 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1329 | ParsedType TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1330 | SourceLocation IdLoc, |
| 1331 | SourceLocation LParenLoc, |
| 1332 | ExprTy **Args, unsigned NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1333 | SourceLocation RParenLoc, |
| 1334 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1335 | if (!ConstructorD) |
| 1336 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1338 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1339 | |
| 1340 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1341 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1342 | if (!Constructor) { |
| 1343 | // The user wrote a constructor initializer on a function that is |
| 1344 | // not a C++ constructor. Ignore the error for now, because we may |
| 1345 | // have more member initializers coming; we'll diagnose it just |
| 1346 | // once in ActOnMemInitializers. |
| 1347 | return true; |
| 1348 | } |
| 1349 | |
| 1350 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1351 | |
| 1352 | // C++ [class.base.init]p2: |
| 1353 | // 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] | 1354 | // constructor's class and, if not found in that scope, are looked |
| 1355 | // up in the scope containing the constructor's definition. |
| 1356 | // [Note: if the constructor's class contains a member with the |
| 1357 | // same name as a direct or virtual base class of the class, a |
| 1358 | // mem-initializer-id naming the member or base class and composed |
| 1359 | // of a single identifier refers to the class member. A |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1360 | // mem-initializer-id for the hidden base class may be specified |
| 1361 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1362 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1363 | // Look for a member, first. |
| 1364 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1366 | = ClassDecl->lookup(MemberOrBase); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1367 | if (Result.first != Result.second) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1368 | Member = dyn_cast<FieldDecl>(*Result.first); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1369 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1370 | if (Member) { |
| 1371 | if (EllipsisLoc.isValid()) |
| 1372 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
| 1373 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1374 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1375 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1376 | LParenLoc, RParenLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1379 | // Handle anonymous union case. |
| 1380 | if (IndirectFieldDecl* IndirectField |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1381 | = dyn_cast<IndirectFieldDecl>(*Result.first)) { |
| 1382 | if (EllipsisLoc.isValid()) |
| 1383 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
| 1384 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1385 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1386 | return BuildMemberInitializer(IndirectField, (Expr**)Args, |
| 1387 | NumArgs, IdLoc, |
| 1388 | LParenLoc, RParenLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1389 | } |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1390 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1391 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1392 | // 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] | 1393 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1394 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1395 | |
| 1396 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1397 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1398 | } else { |
| 1399 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1400 | LookupParsedName(R, S, &SS); |
| 1401 | |
| 1402 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1403 | if (!TyD) { |
| 1404 | if (R.isAmbiguous()) return true; |
| 1405 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1406 | // We don't want access-control diagnostics here. |
| 1407 | R.suppressDiagnostics(); |
| 1408 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1409 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1410 | bool NotUnknownSpecialization = false; |
| 1411 | DeclContext *DC = computeDeclContext(SS, false); |
| 1412 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1413 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1414 | |
| 1415 | if (!NotUnknownSpecialization) { |
| 1416 | // When the scope specifier can refer to a member of an unknown |
| 1417 | // specialization, we take it as a type name. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 1418 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 1419 | SS.getWithLocInContext(Context), |
| 1420 | *MemberOrBase, IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1421 | if (BaseType.isNull()) |
| 1422 | return true; |
| 1423 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1424 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 1425 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1429 | // If no results were found, try to correct typos. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1430 | TypoCorrection Corr; |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1431 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1432 | (Corr = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, &SS, |
| 1433 | ClassDecl, false, CTC_NoKeywords))) { |
| 1434 | std::string CorrectedStr(Corr.getAsString(getLangOptions())); |
| 1435 | std::string CorrectedQuotedStr(Corr.getQuoted(getLangOptions())); |
| 1436 | if (FieldDecl *Member = Corr.getCorrectionDeclAs<FieldDecl>()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1437 | if (Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl)) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1438 | // We have found a non-static data member with a similar |
| 1439 | // name to what was typed; complain and initialize that |
| 1440 | // member. |
| 1441 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1442 | << MemberOrBase << true << CorrectedQuotedStr |
| 1443 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1444 | Diag(Member->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1445 | << CorrectedQuotedStr; |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1446 | |
| 1447 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 1448 | LParenLoc, RParenLoc); |
| 1449 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1450 | } else if (TypeDecl *Type = Corr.getCorrectionDeclAs<TypeDecl>()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1451 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1452 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1453 | if (FindBaseInitializer(*this, ClassDecl, |
| 1454 | Context.getTypeDeclType(Type), |
| 1455 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1456 | // We have found a direct or virtual base class with a |
| 1457 | // similar name to what was typed; complain and initialize |
| 1458 | // that base class. |
| 1459 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1460 | << MemberOrBase << false << CorrectedQuotedStr |
| 1461 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1462 | |
| 1463 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1464 | : VirtualBaseSpec; |
| 1465 | Diag(BaseSpec->getSourceRange().getBegin(), |
| 1466 | diag::note_base_class_specified_here) |
| 1467 | << BaseSpec->getType() |
| 1468 | << BaseSpec->getSourceRange(); |
| 1469 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1470 | TyD = Type; |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1475 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1476 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1477 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1478 | return true; |
| 1479 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1482 | if (BaseType.isNull()) { |
| 1483 | BaseType = Context.getTypeDeclType(TyD); |
| 1484 | if (SS.isSet()) { |
| 1485 | NestedNameSpecifier *Qualifier = |
| 1486 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1487 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1488 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1489 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1490 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1491 | } |
| 1492 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1494 | if (!TInfo) |
| 1495 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1496 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1497 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1498 | LParenLoc, RParenLoc, ClassDecl, EllipsisLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1501 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1502 | /// containing the field that is being initialized. Returns true if there is an |
| 1503 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1504 | /// otherwise. |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1505 | static bool InitExprContainsUninitializedFields(const Stmt *S, |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1506 | const ValueDecl *LhsField, |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1507 | SourceLocation *L) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1508 | assert(isa<FieldDecl>(LhsField) || isa<IndirectFieldDecl>(LhsField)); |
| 1509 | |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1510 | if (isa<CallExpr>(S)) { |
| 1511 | // Do not descend into function calls or constructors, as the use |
| 1512 | // of an uninitialized field may be valid. One would have to inspect |
| 1513 | // the contents of the function/ctor to determine if it is safe or not. |
| 1514 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1515 | // may be safe, depending on what the function/ctor does. |
| 1516 | return false; |
| 1517 | } |
| 1518 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 1519 | const NamedDecl *RhsField = ME->getMemberDecl(); |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1520 | |
| 1521 | if (const VarDecl *VD = dyn_cast<VarDecl>(RhsField)) { |
| 1522 | // The member expression points to a static data member. |
| 1523 | assert(VD->isStaticDataMember() && |
| 1524 | "Member points to non-static data member!"); |
Nick Lewycky | edd5911 | 2010-10-06 18:37:39 +0000 | [diff] [blame] | 1525 | (void)VD; |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1526 | return false; |
| 1527 | } |
| 1528 | |
| 1529 | if (isa<EnumConstantDecl>(RhsField)) { |
| 1530 | // The member expression points to an enum. |
| 1531 | return false; |
| 1532 | } |
| 1533 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1534 | if (RhsField == LhsField) { |
| 1535 | // Initializing a field with itself. Throw a warning. |
| 1536 | // But wait; there are exceptions! |
| 1537 | // Exception #1: The field may not belong to this record. |
| 1538 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1539 | const Expr *base = ME->getBase(); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1540 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1541 | // Even though the field matches, it does not belong to this record. |
| 1542 | return false; |
| 1543 | } |
| 1544 | // None of the exceptions triggered; return true to indicate an |
| 1545 | // uninitialized field was used. |
| 1546 | *L = ME->getMemberLoc(); |
| 1547 | return true; |
| 1548 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1549 | } else if (isa<UnaryExprOrTypeTraitExpr>(S)) { |
Argyrios Kyrtzidis | ff8819b | 2010-09-21 10:47:20 +0000 | [diff] [blame] | 1550 | // sizeof/alignof doesn't reference contents, do not warn. |
| 1551 | return false; |
| 1552 | } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(S)) { |
| 1553 | // address-of doesn't reference contents (the pointer may be dereferenced |
| 1554 | // in the same expression but it would be rare; and weird). |
| 1555 | if (UOE->getOpcode() == UO_AddrOf) |
| 1556 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1557 | } |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1558 | for (Stmt::const_child_range it = S->children(); it; ++it) { |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1559 | if (!*it) { |
| 1560 | // An expression such as 'member(arg ?: "")' may trigger this. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1561 | continue; |
| 1562 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1563 | if (InitExprContainsUninitializedFields(*it, LhsField, L)) |
| 1564 | return true; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1565 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1566 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1569 | MemInitResult |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1570 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr **Args, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1571 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1572 | SourceLocation LParenLoc, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1573 | SourceLocation RParenLoc) { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1574 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 1575 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 1576 | assert((DirectMember || IndirectMember) && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1577 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 1578 | |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 1579 | if (Member->isInvalidDecl()) |
| 1580 | return true; |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1581 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1582 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1583 | // foo(foo) |
| 1584 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1585 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1586 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1587 | SourceLocation L; |
| 1588 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1589 | // FIXME: Return true in the case when other fields are used before being |
| 1590 | // uninitialized. For example, let this field be the i'th field. When |
| 1591 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1592 | // fields are used, as they are not yet initialized. |
| 1593 | // Right now we are only handling the case where the i'th field uses |
| 1594 | // itself in its initializer. |
| 1595 | Diag(L, diag::warn_field_is_uninit); |
| 1596 | } |
| 1597 | } |
| 1598 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1599 | bool HasDependentArg = false; |
| 1600 | for (unsigned i = 0; i < NumArgs; i++) |
| 1601 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1602 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1603 | Expr *Init; |
Eli Friedman | 0f2b97d | 2010-07-24 21:19:15 +0000 | [diff] [blame] | 1604 | if (Member->getType()->isDependentType() || HasDependentArg) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1605 | // Can't check initialization for a member of dependent type or when |
| 1606 | // any of the arguments are type-dependent expressions. |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1607 | Init = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 1608 | RParenLoc, |
| 1609 | Member->getType().getNonReferenceType()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1610 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1611 | DiscardCleanupsInEvaluationContext(); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1612 | } else { |
| 1613 | // Initialize the member. |
| 1614 | InitializedEntity MemberEntity = |
| 1615 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 1616 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 1617 | InitializationKind Kind = |
| 1618 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1619 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1620 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 1621 | |
| 1622 | ExprResult MemberInit = |
| 1623 | InitSeq.Perform(*this, MemberEntity, Kind, |
| 1624 | MultiExprArg(*this, Args, NumArgs), 0); |
| 1625 | if (MemberInit.isInvalid()) |
| 1626 | return true; |
| 1627 | |
| 1628 | CheckImplicitConversions(MemberInit.get(), LParenLoc); |
| 1629 | |
| 1630 | // C++0x [class.base.init]p7: |
| 1631 | // The initialization of each base and member constitutes a |
| 1632 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1633 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1634 | if (MemberInit.isInvalid()) |
| 1635 | return true; |
| 1636 | |
| 1637 | // If we are in a dependent context, template instantiation will |
| 1638 | // perform this type-checking again. Just save the arguments that we |
| 1639 | // received in a ParenListExpr. |
| 1640 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1641 | // of the information that we have about the member |
| 1642 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1643 | // and this approach is far more likely to get the corner cases right. |
| 1644 | if (CurContext->isDependentContext()) |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 1645 | Init = new (Context) ParenListExpr( |
| 1646 | Context, LParenLoc, Args, NumArgs, RParenLoc, |
| 1647 | Member->getType().getNonReferenceType()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1648 | else |
| 1649 | Init = MemberInit.get(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1652 | if (DirectMember) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1653 | return new (Context) CXXCtorInitializer(Context, DirectMember, |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1654 | IdLoc, LParenLoc, Init, |
| 1655 | RParenLoc); |
| 1656 | } else { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1657 | return new (Context) CXXCtorInitializer(Context, IndirectMember, |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1658 | IdLoc, LParenLoc, Init, |
| 1659 | RParenLoc); |
| 1660 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1663 | MemInitResult |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1664 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, |
| 1665 | Expr **Args, unsigned NumArgs, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1666 | SourceLocation NameLoc, |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1667 | SourceLocation LParenLoc, |
| 1668 | SourceLocation RParenLoc, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1669 | CXXRecordDecl *ClassDecl) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1670 | SourceLocation Loc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1671 | if (!LangOpts.CPlusPlus0x) |
| 1672 | return Diag(Loc, diag::err_delegation_0x_only) |
| 1673 | << TInfo->getTypeLoc().getLocalSourceRange(); |
Sebastian Redl | f9c32eb | 2011-03-12 13:53:51 +0000 | [diff] [blame] | 1674 | |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1675 | // Initialize the object. |
| 1676 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 1677 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 1678 | InitializationKind Kind = |
| 1679 | InitializationKind::CreateDirect(NameLoc, LParenLoc, RParenLoc); |
| 1680 | |
| 1681 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args, NumArgs); |
| 1682 | |
| 1683 | ExprResult DelegationInit = |
| 1684 | InitSeq.Perform(*this, DelegationEntity, Kind, |
| 1685 | MultiExprArg(*this, Args, NumArgs), 0); |
| 1686 | if (DelegationInit.isInvalid()) |
| 1687 | return true; |
| 1688 | |
| 1689 | CXXConstructExpr *ConExpr = cast<CXXConstructExpr>(DelegationInit.get()); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 1690 | CXXConstructorDecl *Constructor |
| 1691 | = ConExpr->getConstructor(); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1692 | assert(Constructor && "Delegating constructor with no target?"); |
| 1693 | |
| 1694 | CheckImplicitConversions(DelegationInit.get(), LParenLoc); |
| 1695 | |
| 1696 | // C++0x [class.base.init]p7: |
| 1697 | // The initialization of each base and member constitutes a |
| 1698 | // full-expression. |
| 1699 | DelegationInit = MaybeCreateExprWithCleanups(DelegationInit); |
| 1700 | if (DelegationInit.isInvalid()) |
| 1701 | return true; |
| 1702 | |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 1703 | assert(!CurContext->isDependentContext()); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1704 | return new (Context) CXXCtorInitializer(Context, Loc, LParenLoc, Constructor, |
| 1705 | DelegationInit.takeAs<Expr>(), |
| 1706 | RParenLoc); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1710 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1711 | Expr **Args, unsigned NumArgs, |
| 1712 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1713 | CXXRecordDecl *ClassDecl, |
| 1714 | SourceLocation EllipsisLoc) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1715 | bool HasDependentArg = false; |
| 1716 | for (unsigned i = 0; i < NumArgs; i++) |
| 1717 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1718 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1719 | SourceLocation BaseLoc |
| 1720 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1721 | |
| 1722 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 1723 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 1724 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1725 | |
| 1726 | // C++ [class.base.init]p2: |
| 1727 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1728 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1729 | // of that class, the mem-initializer is ill-formed. A |
| 1730 | // mem-initializer-list can initialize a base class using any |
| 1731 | // name that denotes that base class type. |
| 1732 | bool Dependent = BaseType->isDependentType() || HasDependentArg; |
| 1733 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1734 | if (EllipsisLoc.isValid()) { |
| 1735 | // This is a pack expansion. |
| 1736 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 1737 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1738 | << SourceRange(BaseLoc, RParenLoc); |
| 1739 | |
| 1740 | EllipsisLoc = SourceLocation(); |
| 1741 | } |
| 1742 | } else { |
| 1743 | // Check for any unexpanded parameter packs. |
| 1744 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 1745 | return true; |
| 1746 | |
| 1747 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1748 | if (DiagnoseUnexpandedParameterPack(Args[I])) |
| 1749 | return true; |
| 1750 | } |
| 1751 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1752 | // Check for direct and virtual base classes. |
| 1753 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1754 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1755 | if (!Dependent) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1756 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 1757 | BaseType)) |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1758 | return BuildDelegatingInitializer(BaseTInfo, Args, NumArgs, BaseLoc, |
| 1759 | LParenLoc, RParenLoc, ClassDecl); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1760 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1761 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 1762 | VirtualBaseSpec); |
| 1763 | |
| 1764 | // C++ [base.class.init]p2: |
| 1765 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 1766 | // constructor's class or a direct or virtual base of that class, the |
| 1767 | // mem-initializer is ill-formed. |
| 1768 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 1769 | // If the class has any dependent bases, then it's possible that |
| 1770 | // one of those types will resolve to the same type as |
| 1771 | // BaseType. Therefore, just treat this as a dependent base |
| 1772 | // class initialization. FIXME: Should we try to check the |
| 1773 | // initialization anyway? It seems odd. |
| 1774 | if (ClassDecl->hasAnyDependentBases()) |
| 1775 | Dependent = true; |
| 1776 | else |
| 1777 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 1778 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 1779 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | if (Dependent) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1784 | // Can't check initialization for a base of dependent type or when |
| 1785 | // any of the arguments are type-dependent expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1786 | ExprResult BaseInit |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1787 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 1788 | RParenLoc, BaseType)); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1789 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1790 | DiscardCleanupsInEvaluationContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1792 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1793 | /*IsVirtual=*/false, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1794 | LParenLoc, |
| 1795 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1796 | RParenLoc, |
| 1797 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1798 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1799 | |
| 1800 | // C++ [base.class.init]p2: |
| 1801 | // If a mem-initializer-id is ambiguous because it designates both |
| 1802 | // a direct non-virtual base class and an inherited virtual base |
| 1803 | // class, the mem-initializer is ill-formed. |
| 1804 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1805 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1806 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1807 | |
| 1808 | CXXBaseSpecifier *BaseSpec |
| 1809 | = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
| 1810 | if (!BaseSpec) |
| 1811 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 1812 | |
| 1813 | // Initialize the base. |
| 1814 | InitializedEntity BaseEntity = |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1815 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1816 | InitializationKind Kind = |
| 1817 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc); |
| 1818 | |
| 1819 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 1820 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1821 | ExprResult BaseInit = |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1822 | InitSeq.Perform(*this, BaseEntity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1823 | MultiExprArg(*this, Args, NumArgs), 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1824 | if (BaseInit.isInvalid()) |
| 1825 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1826 | |
| 1827 | CheckImplicitConversions(BaseInit.get(), LParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1828 | |
| 1829 | // C++0x [class.base.init]p7: |
| 1830 | // The initialization of each base and member constitutes a |
| 1831 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1832 | BaseInit = MaybeCreateExprWithCleanups(BaseInit); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1833 | if (BaseInit.isInvalid()) |
| 1834 | return true; |
| 1835 | |
| 1836 | // If we are in a dependent context, template instantiation will |
| 1837 | // perform this type-checking again. Just save the arguments that we |
| 1838 | // received in a ParenListExpr. |
| 1839 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1840 | // of the information that we have about the base |
| 1841 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1842 | // and this approach is far more likely to get the corner cases right. |
| 1843 | if (CurContext->isDependentContext()) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1844 | ExprResult Init |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1845 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 1846 | RParenLoc, BaseType)); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1847 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1848 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1849 | LParenLoc, |
| 1850 | Init.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1851 | RParenLoc, |
| 1852 | EllipsisLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1855 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1856 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1857 | LParenLoc, |
| 1858 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1859 | RParenLoc, |
| 1860 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1863 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 1864 | /// initialize its base or member. |
| 1865 | enum ImplicitInitializerKind { |
| 1866 | IIK_Default, |
| 1867 | IIK_Copy, |
| 1868 | IIK_Move |
| 1869 | }; |
| 1870 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1871 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1872 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1873 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1874 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1875 | bool IsInheritedVirtualBase, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1876 | CXXCtorInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1877 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1878 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 1879 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1880 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1881 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1882 | |
| 1883 | switch (ImplicitInitKind) { |
| 1884 | case IIK_Default: { |
| 1885 | InitializationKind InitKind |
| 1886 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 1887 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1888 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1889 | MultiExprArg(SemaRef, 0, 0)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1890 | break; |
| 1891 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1892 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1893 | case IIK_Copy: { |
| 1894 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1895 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1896 | |
| 1897 | Expr *CopyCtorArg = |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1898 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), Param, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1899 | Constructor->getLocation(), ParamType, |
| 1900 | VK_LValue, 0); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1901 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1902 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 1903 | QualType ArgTy = |
| 1904 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 1905 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1906 | |
| 1907 | CXXCastPath BasePath; |
| 1908 | BasePath.push_back(BaseSpec); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1909 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 1910 | CK_UncheckedDerivedToBase, |
| 1911 | VK_LValue, &BasePath).take(); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1912 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1913 | InitializationKind InitKind |
| 1914 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1915 | SourceLocation(), SourceLocation()); |
| 1916 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1917 | &CopyCtorArg, 1); |
| 1918 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1919 | MultiExprArg(&CopyCtorArg, 1)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1920 | break; |
| 1921 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1922 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1923 | case IIK_Move: |
| 1924 | assert(false && "Unhandled initializer kind!"); |
| 1925 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1926 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1927 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1928 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1929 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1930 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1931 | CXXBaseInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1932 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1933 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 1934 | SourceLocation()), |
| 1935 | BaseSpec->isVirtual(), |
| 1936 | SourceLocation(), |
| 1937 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1938 | SourceLocation(), |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1939 | SourceLocation()); |
| 1940 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1941 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1944 | static bool |
| 1945 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1946 | ImplicitInitializerKind ImplicitInitKind, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 1947 | FieldDecl *Field, IndirectFieldDecl *Indirect, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1948 | CXXCtorInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 1949 | if (Field->isInvalidDecl()) |
| 1950 | return true; |
| 1951 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1952 | SourceLocation Loc = Constructor->getLocation(); |
| 1953 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1954 | if (ImplicitInitKind == IIK_Copy) { |
| 1955 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1956 | QualType ParamType = Param->getType().getNonReferenceType(); |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 1957 | |
| 1958 | // Suppress copying zero-width bitfields. |
| 1959 | if (const Expr *Width = Field->getBitWidth()) |
| 1960 | if (Width->EvaluateAsInt(SemaRef.Context) == 0) |
| 1961 | return false; |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1962 | |
| 1963 | Expr *MemberExprBase = |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1964 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), Param, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1965 | Loc, ParamType, VK_LValue, 0); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1966 | |
| 1967 | // Build a reference to this field within the parameter. |
| 1968 | CXXScopeSpec SS; |
| 1969 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 1970 | Sema::LookupMemberName); |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 1971 | MemberLookup.addDecl(Indirect? cast<ValueDecl>(Indirect) : cast<ValueDecl>(Field), AS_public); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1972 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1973 | ExprResult CopyCtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1974 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1975 | ParamType, Loc, |
| 1976 | /*IsArrow=*/false, |
| 1977 | SS, |
| 1978 | /*FirstQualifierInScope=*/0, |
| 1979 | MemberLookup, |
| 1980 | /*TemplateArgs=*/0); |
| 1981 | if (CopyCtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1982 | return true; |
| 1983 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1984 | // When the field we are copying is an array, create index variables for |
| 1985 | // each dimension of the array. We use these index variables to subscript |
| 1986 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 1987 | // necessary iteration with these index variables. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1988 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1989 | QualType BaseType = Field->getType(); |
| 1990 | QualType SizeType = SemaRef.Context.getSizeType(); |
| 1991 | while (const ConstantArrayType *Array |
| 1992 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
| 1993 | // Create the iteration variable for this array index. |
| 1994 | IdentifierInfo *IterationVarName = 0; |
| 1995 | { |
| 1996 | llvm::SmallString<8> Str; |
| 1997 | llvm::raw_svector_ostream OS(Str); |
| 1998 | OS << "__i" << IndexVariables.size(); |
| 1999 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 2000 | } |
| 2001 | VarDecl *IterationVar |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2002 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2003 | IterationVarName, SizeType, |
| 2004 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2005 | SC_None, SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2006 | IndexVariables.push_back(IterationVar); |
| 2007 | |
| 2008 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2009 | ExprResult IterationVarRef |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2010 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_RValue, Loc); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2011 | assert(!IterationVarRef.isInvalid() && |
| 2012 | "Reference to invented variable cannot fail!"); |
| 2013 | |
| 2014 | // Subscript the array with this iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2015 | CopyCtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CopyCtorArg.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2016 | Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2017 | IterationVarRef.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2018 | Loc); |
| 2019 | if (CopyCtorArg.isInvalid()) |
| 2020 | return true; |
| 2021 | |
| 2022 | BaseType = Array->getElementType(); |
| 2023 | } |
| 2024 | |
| 2025 | // Construct the entity that we will be initializing. For an array, this |
| 2026 | // will be first element in the array, which may require several levels |
| 2027 | // of array-subscript entities. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2028 | SmallVector<InitializedEntity, 4> Entities; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2029 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2030 | if (Indirect) |
| 2031 | Entities.push_back(InitializedEntity::InitializeMember(Indirect)); |
| 2032 | else |
| 2033 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2034 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 2035 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 2036 | 0, |
| 2037 | Entities.back())); |
| 2038 | |
| 2039 | // Direct-initialize to use the copy constructor. |
| 2040 | InitializationKind InitKind = |
| 2041 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 2042 | |
| 2043 | Expr *CopyCtorArgE = CopyCtorArg.takeAs<Expr>(); |
| 2044 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
| 2045 | &CopyCtorArgE, 1); |
| 2046 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2047 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2048 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2049 | MultiExprArg(&CopyCtorArgE, 1)); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2050 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2051 | if (MemberInit.isInvalid()) |
| 2052 | return true; |
| 2053 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2054 | if (Indirect) { |
| 2055 | assert(IndexVariables.size() == 0 && |
| 2056 | "Indirect field improperly initialized"); |
| 2057 | CXXMemberInit |
| 2058 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 2059 | Loc, Loc, |
| 2060 | MemberInit.takeAs<Expr>(), |
| 2061 | Loc); |
| 2062 | } else |
| 2063 | CXXMemberInit = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, |
| 2064 | Loc, MemberInit.takeAs<Expr>(), |
| 2065 | Loc, |
| 2066 | IndexVariables.data(), |
| 2067 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2068 | return false; |
| 2069 | } |
| 2070 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2071 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 2072 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2073 | QualType FieldBaseElementType = |
| 2074 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 2075 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2076 | if (FieldBaseElementType->isRecordType()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2077 | InitializedEntity InitEntity |
| 2078 | = Indirect? InitializedEntity::InitializeMember(Indirect) |
| 2079 | : InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2080 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2081 | InitializationKind::CreateDefault(Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2082 | |
| 2083 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2084 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2085 | InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2086 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2087 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2088 | if (MemberInit.isInvalid()) |
| 2089 | return true; |
| 2090 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2091 | if (Indirect) |
| 2092 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 2093 | Indirect, Loc, |
| 2094 | Loc, |
| 2095 | MemberInit.get(), |
| 2096 | Loc); |
| 2097 | else |
| 2098 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 2099 | Field, Loc, Loc, |
| 2100 | MemberInit.get(), |
| 2101 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2102 | return false; |
| 2103 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2104 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 2105 | if (!Field->getParent()->isUnion()) { |
| 2106 | if (FieldBaseElementType->isReferenceType()) { |
| 2107 | SemaRef.Diag(Constructor->getLocation(), |
| 2108 | diag::err_uninitialized_member_in_ctor) |
| 2109 | << (int)Constructor->isImplicit() |
| 2110 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 2111 | << 0 << Field->getDeclName(); |
| 2112 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 2113 | return true; |
| 2114 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2115 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 2116 | if (FieldBaseElementType.isConstQualified()) { |
| 2117 | SemaRef.Diag(Constructor->getLocation(), |
| 2118 | diag::err_uninitialized_member_in_ctor) |
| 2119 | << (int)Constructor->isImplicit() |
| 2120 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 2121 | << 1 << Field->getDeclName(); |
| 2122 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 2123 | return true; |
| 2124 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2125 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2126 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2127 | if (SemaRef.getLangOptions().ObjCAutoRefCount && |
| 2128 | FieldBaseElementType->isObjCRetainableType() && |
| 2129 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_None && |
| 2130 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) { |
| 2131 | // Instant objects: |
| 2132 | // Default-initialize Objective-C pointers to NULL. |
| 2133 | CXXMemberInit |
| 2134 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 2135 | Loc, Loc, |
| 2136 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()), |
| 2137 | Loc); |
| 2138 | return false; |
| 2139 | } |
| 2140 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2141 | // Nothing to initialize. |
| 2142 | CXXMemberInit = 0; |
| 2143 | return false; |
| 2144 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2145 | |
| 2146 | namespace { |
| 2147 | struct BaseAndFieldInfo { |
| 2148 | Sema &S; |
| 2149 | CXXConstructorDecl *Ctor; |
| 2150 | bool AnyErrorsInInits; |
| 2151 | ImplicitInitializerKind IIK; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2152 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2153 | SmallVector<CXXCtorInitializer*, 8> AllToInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2154 | |
| 2155 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 2156 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
| 2157 | // FIXME: Handle implicit move constructors. |
Douglas Gregor | 136da1c | 2011-08-10 16:51:53 +0000 | [diff] [blame] | 2158 | if ((Ctor->isImplicit() || Ctor->isDefaulted()) && |
| 2159 | Ctor->isCopyConstructor()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2160 | IIK = IIK_Copy; |
| 2161 | else |
| 2162 | IIK = IIK_Default; |
| 2163 | } |
| 2164 | }; |
| 2165 | } |
| 2166 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2167 | static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2168 | FieldDecl *Field, |
| 2169 | IndirectFieldDecl *Indirect = 0) { |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2170 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2171 | // Overwhelmingly common case: we have a direct initializer for this field. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2172 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(Field)) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2173 | Info.AllToInit.push_back(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2174 | return false; |
| 2175 | } |
| 2176 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2177 | // C++0x [class.base.init]p8: if the entity is a non-static data member that |
| 2178 | // has a brace-or-equal-initializer, the entity is initialized as specified |
| 2179 | // in [dcl.init]. |
| 2180 | if (Field->hasInClassInitializer()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2181 | CXXCtorInitializer *Init; |
| 2182 | if (Indirect) |
| 2183 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 2184 | SourceLocation(), |
| 2185 | SourceLocation(), 0, |
| 2186 | SourceLocation()); |
| 2187 | else |
| 2188 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 2189 | SourceLocation(), |
| 2190 | SourceLocation(), 0, |
| 2191 | SourceLocation()); |
| 2192 | Info.AllToInit.push_back(Init); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2193 | return false; |
| 2194 | } |
| 2195 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2196 | // Don't try to build an implicit initializer if there were semantic |
| 2197 | // errors in any of the initializers (and therefore we might be |
| 2198 | // missing some that the user actually wrote). |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2199 | if (Info.AnyErrorsInInits || Field->isInvalidDecl()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2200 | return false; |
| 2201 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2202 | CXXCtorInitializer *Init = 0; |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2203 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, |
| 2204 | Indirect, Init)) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2205 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2206 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2207 | if (Init) |
| 2208 | Info.AllToInit.push_back(Init); |
| 2209 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2210 | return false; |
| 2211 | } |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2212 | |
| 2213 | bool |
| 2214 | Sema::SetDelegatingInitializer(CXXConstructorDecl *Constructor, |
| 2215 | CXXCtorInitializer *Initializer) { |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 2216 | assert(Initializer->isDelegatingInitializer()); |
Sean Hunt | 01aacc0 | 2011-05-03 20:43:02 +0000 | [diff] [blame] | 2217 | Constructor->setNumCtorInitializers(1); |
| 2218 | CXXCtorInitializer **initializer = |
| 2219 | new (Context) CXXCtorInitializer*[1]; |
| 2220 | memcpy(initializer, &Initializer, sizeof (CXXCtorInitializer*)); |
| 2221 | Constructor->setCtorInitializers(initializer); |
| 2222 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2223 | if (CXXDestructorDecl *Dtor = LookupDestructor(Constructor->getParent())) { |
| 2224 | MarkDeclarationReferenced(Initializer->getSourceLocation(), Dtor); |
| 2225 | DiagnoseUseOfDecl(Dtor, Initializer->getSourceLocation()); |
| 2226 | } |
| 2227 | |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 2228 | DelegatingCtorDecls.push_back(Constructor); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 2229 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2230 | return false; |
| 2231 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2232 | |
| 2233 | /// \brief Determine whether the given indirect field declaration is somewhere |
| 2234 | /// within an anonymous union. |
| 2235 | static bool isWithinAnonymousUnion(IndirectFieldDecl *F) { |
| 2236 | for (IndirectFieldDecl::chain_iterator C = F->chain_begin(), |
| 2237 | CEnd = F->chain_end(); |
| 2238 | C != CEnd; ++C) |
| 2239 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>((*C)->getDeclContext())) |
| 2240 | if (Record->isUnion()) |
| 2241 | return true; |
| 2242 | |
| 2243 | return false; |
| 2244 | } |
| 2245 | |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 2246 | bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, |
| 2247 | CXXCtorInitializer **Initializers, |
| 2248 | unsigned NumInitializers, |
| 2249 | bool AnyErrors) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2250 | if (Constructor->getDeclContext()->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2251 | // Just store the initializers as written, they will be checked during |
| 2252 | // instantiation. |
| 2253 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2254 | Constructor->setNumCtorInitializers(NumInitializers); |
| 2255 | CXXCtorInitializer **baseOrMemberInitializers = |
| 2256 | new (Context) CXXCtorInitializer*[NumInitializers]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2257 | memcpy(baseOrMemberInitializers, Initializers, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2258 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 2259 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | return false; |
| 2263 | } |
| 2264 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2265 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2266 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2267 | // We need to build the initializer AST according to order of construction |
| 2268 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2269 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 2270 | if (!ClassDecl) |
| 2271 | return true; |
| 2272 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2273 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2274 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2275 | for (unsigned i = 0; i < NumInitializers; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2276 | CXXCtorInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2277 | |
| 2278 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2279 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2280 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2281 | Info.AllBaseFields[Member->getAnyMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2284 | // Keep track of the direct virtual bases. |
| 2285 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 2286 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 2287 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 2288 | if (I->isVirtual()) |
| 2289 | DirectVBases.insert(I); |
| 2290 | } |
| 2291 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2292 | // Push virtual bases before others. |
| 2293 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2294 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 2295 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2296 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2297 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 2298 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2299 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2300 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2301 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2302 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2303 | VBase, IsInheritedVirtualBase, |
| 2304 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2305 | HadError = true; |
| 2306 | continue; |
| 2307 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2308 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2309 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2310 | } |
| 2311 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2313 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2314 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2315 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 2316 | // Virtuals are in the virtual base list and already constructed. |
| 2317 | if (Base->isVirtual()) |
| 2318 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2319 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2320 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2321 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 2322 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2323 | } else if (!AnyErrors) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2324 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2325 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2326 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2327 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2328 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2329 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2330 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 2331 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2332 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2333 | } |
| 2334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2336 | // Fields. |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2337 | for (DeclContext::decl_iterator Mem = ClassDecl->decls_begin(), |
| 2338 | MemEnd = ClassDecl->decls_end(); |
| 2339 | Mem != MemEnd; ++Mem) { |
| 2340 | if (FieldDecl *F = dyn_cast<FieldDecl>(*Mem)) { |
| 2341 | if (F->getType()->isIncompleteArrayType()) { |
| 2342 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 2343 | "Incomplete array type is not valid"); |
| 2344 | continue; |
| 2345 | } |
| 2346 | |
| 2347 | // If we're not generating the implicit copy constructor, then we'll |
| 2348 | // handle anonymous struct/union fields based on their individual |
| 2349 | // indirect fields. |
| 2350 | if (F->isAnonymousStructOrUnion() && Info.IIK == IIK_Default) |
| 2351 | continue; |
| 2352 | |
| 2353 | if (CollectFieldInitializer(*this, Info, F)) |
| 2354 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 2355 | continue; |
| 2356 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2357 | |
| 2358 | // Beyond this point, we only consider default initialization. |
| 2359 | if (Info.IIK != IIK_Default) |
| 2360 | continue; |
| 2361 | |
| 2362 | if (IndirectFieldDecl *F = dyn_cast<IndirectFieldDecl>(*Mem)) { |
| 2363 | if (F->getType()->isIncompleteArrayType()) { |
| 2364 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 2365 | "Incomplete array type is not valid"); |
| 2366 | continue; |
| 2367 | } |
| 2368 | |
| 2369 | // If this field is somewhere within an anonymous union, we only |
| 2370 | // initialize it if there's an explicit initializer. |
| 2371 | if (isWithinAnonymousUnion(F)) { |
| 2372 | if (CXXCtorInitializer *Init |
| 2373 | = Info.AllBaseFields.lookup(F->getAnonField())) { |
| 2374 | Info.AllToInit.push_back(Init); |
| 2375 | } |
| 2376 | |
| 2377 | continue; |
| 2378 | } |
| 2379 | |
| 2380 | // Initialize each field of an anonymous struct individually. |
| 2381 | if (CollectFieldInitializer(*this, Info, F->getAnonField(), F)) |
| 2382 | HadError = true; |
| 2383 | |
| 2384 | continue; |
| 2385 | } |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 2386 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2387 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2388 | NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2389 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2390 | Constructor->setNumCtorInitializers(NumInitializers); |
| 2391 | CXXCtorInitializer **baseOrMemberInitializers = |
| 2392 | new (Context) CXXCtorInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2393 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2394 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 2395 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 2396 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2397 | // Constructors implicitly reference the base and member |
| 2398 | // destructors. |
| 2399 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 2400 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2401 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2402 | |
| 2403 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2406 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 2407 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2408 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2409 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 2410 | return static_cast<void *>(RT->getDecl()); |
| 2411 | } |
| 2412 | return static_cast<void *>(Field); |
| 2413 | } |
| 2414 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2415 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2416 | return const_cast<Type*>(Context.getCanonicalType(BaseType).getTypePtr()); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2419 | static void *GetKeyForMember(ASTContext &Context, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2420 | CXXCtorInitializer *Member) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2421 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2422 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 2423 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2424 | // For fields injected into the class via declaration of an anonymous union, |
| 2425 | // use its anonymous union class declaration as the unique key. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2426 | FieldDecl *Field = Member->getAnyMember(); |
| 2427 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2428 | // If the field is a member of an anonymous struct or union, our key |
| 2429 | // 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] | 2430 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2431 | if (RD->isAnonymousStructOrUnion()) { |
| 2432 | while (true) { |
| 2433 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 2434 | if (Parent->isAnonymousStructOrUnion()) |
| 2435 | RD = Parent; |
| 2436 | else |
| 2437 | break; |
| 2438 | } |
| 2439 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 2440 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2441 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2442 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 2443 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2446 | static void |
| 2447 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2448 | const CXXConstructorDecl *Constructor, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2449 | CXXCtorInitializer **Inits, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2450 | unsigned NumInits) { |
| 2451 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 2452 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2453 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 2454 | // Don't check initializers order unless the warning is enabled at the |
| 2455 | // location of at least one initializer. |
| 2456 | bool ShouldCheckOrder = false; |
| 2457 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2458 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 2459 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order, |
| 2460 | Init->getSourceLocation()) |
| 2461 | != Diagnostic::Ignored) { |
| 2462 | ShouldCheckOrder = true; |
| 2463 | break; |
| 2464 | } |
| 2465 | } |
| 2466 | if (!ShouldCheckOrder) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2467 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2468 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2469 | // Build the list of bases and members in the order that they'll |
| 2470 | // actually be initialized. The explicit initializers should be in |
| 2471 | // this same order but may be missing things. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2472 | SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2473 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2474 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2475 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2476 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2477 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2478 | ClassDecl->vbases_begin(), |
| 2479 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2480 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2482 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2483 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2484 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2485 | if (Base->isVirtual()) |
| 2486 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2487 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2489 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2490 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2491 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2492 | E = ClassDecl->field_end(); Field != E; ++Field) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2493 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2494 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2495 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 2496 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2497 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2498 | CXXCtorInitializer *PrevInit = 0; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2499 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2500 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2501 | void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2502 | |
| 2503 | // Scan forward to try to find this initializer in the idealized |
| 2504 | // initializers list. |
| 2505 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2506 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2507 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2508 | |
| 2509 | // If we didn't find this initializer, it must be because we |
| 2510 | // scanned past it on a previous iteration. That can only |
| 2511 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 2512 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2513 | Sema::SemaDiagnosticBuilder D = |
| 2514 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 2515 | diag::warn_initializer_out_of_order); |
| 2516 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2517 | if (PrevInit->isAnyMemberInitializer()) |
| 2518 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2519 | else |
| 2520 | D << 1 << PrevInit->getBaseClassInfo()->getType(); |
| 2521 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2522 | if (Init->isAnyMemberInitializer()) |
| 2523 | D << 0 << Init->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2524 | else |
| 2525 | D << 1 << Init->getBaseClassInfo()->getType(); |
| 2526 | |
| 2527 | // Move back to the initializer's location in the ideal list. |
| 2528 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2529 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2530 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2531 | |
| 2532 | assert(IdealIndex != NumIdealInits && |
| 2533 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2534 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2535 | |
| 2536 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2537 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2540 | namespace { |
| 2541 | bool CheckRedundantInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2542 | CXXCtorInitializer *Init, |
| 2543 | CXXCtorInitializer *&PrevInit) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2544 | if (!PrevInit) { |
| 2545 | PrevInit = Init; |
| 2546 | return false; |
| 2547 | } |
| 2548 | |
| 2549 | if (FieldDecl *Field = Init->getMember()) |
| 2550 | S.Diag(Init->getSourceLocation(), |
| 2551 | diag::err_multiple_mem_initialization) |
| 2552 | << Field->getDeclName() |
| 2553 | << Init->getSourceRange(); |
| 2554 | else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2555 | const Type *BaseClass = Init->getBaseClass(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2556 | assert(BaseClass && "neither field nor base"); |
| 2557 | S.Diag(Init->getSourceLocation(), |
| 2558 | diag::err_multiple_base_initialization) |
| 2559 | << QualType(BaseClass, 0) |
| 2560 | << Init->getSourceRange(); |
| 2561 | } |
| 2562 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 2563 | << 0 << PrevInit->getSourceRange(); |
| 2564 | |
| 2565 | return true; |
| 2566 | } |
| 2567 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2568 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2569 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 2570 | |
| 2571 | bool CheckRedundantUnionInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2572 | CXXCtorInitializer *Init, |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2573 | RedundantUnionMap &Unions) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2574 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2575 | RecordDecl *Parent = Field->getParent(); |
| 2576 | if (!Parent->isAnonymousStructOrUnion()) |
| 2577 | return false; |
| 2578 | |
| 2579 | NamedDecl *Child = Field; |
| 2580 | do { |
| 2581 | if (Parent->isUnion()) { |
| 2582 | UnionEntry &En = Unions[Parent]; |
| 2583 | if (En.first && En.first != Child) { |
| 2584 | S.Diag(Init->getSourceLocation(), |
| 2585 | diag::err_multiple_mem_union_initialization) |
| 2586 | << Field->getDeclName() |
| 2587 | << Init->getSourceRange(); |
| 2588 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 2589 | << 0 << En.second->getSourceRange(); |
| 2590 | return true; |
| 2591 | } else if (!En.first) { |
| 2592 | En.first = Child; |
| 2593 | En.second = Init; |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | Child = Parent; |
| 2598 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 2599 | } while (Parent->isAnonymousStructOrUnion()); |
| 2600 | |
| 2601 | return false; |
| 2602 | } |
| 2603 | } |
| 2604 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2605 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2606 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2607 | SourceLocation ColonLoc, |
| 2608 | MemInitTy **meminits, unsigned NumMemInits, |
| 2609 | bool AnyErrors) { |
| 2610 | if (!ConstructorDecl) |
| 2611 | return; |
| 2612 | |
| 2613 | AdjustDeclIfTemplate(ConstructorDecl); |
| 2614 | |
| 2615 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2616 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2617 | |
| 2618 | if (!Constructor) { |
| 2619 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 2620 | return; |
| 2621 | } |
| 2622 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2623 | CXXCtorInitializer **MemInits = |
| 2624 | reinterpret_cast<CXXCtorInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2625 | |
| 2626 | // Mapping for the duplicate initializers check. |
| 2627 | // For member initializers, this is keyed with a FieldDecl*. |
| 2628 | // For base initializers, this is keyed with a Type*. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2629 | llvm::DenseMap<void*, CXXCtorInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2630 | |
| 2631 | // Mapping for the inconsistent anonymous-union initializers check. |
| 2632 | RedundantUnionMap MemberUnions; |
| 2633 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2634 | bool HadError = false; |
| 2635 | for (unsigned i = 0; i < NumMemInits; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2636 | CXXCtorInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2637 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 2638 | // Set the source order index. |
| 2639 | Init->setSourceOrder(i); |
| 2640 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2641 | if (Init->isAnyMemberInitializer()) { |
| 2642 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2643 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 2644 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 2645 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2646 | } else if (Init->isBaseInitializer()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2647 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 2648 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 2649 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2650 | } else { |
| 2651 | assert(Init->isDelegatingInitializer()); |
| 2652 | // This must be the only initializer |
| 2653 | if (i != 0 || NumMemInits > 1) { |
| 2654 | Diag(MemInits[0]->getSourceLocation(), |
| 2655 | diag::err_delegating_initializer_alone) |
| 2656 | << MemInits[0]->getSourceRange(); |
| 2657 | HadError = true; |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2658 | // We will treat this as being the only initializer. |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2659 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 2660 | SetDelegatingInitializer(Constructor, MemInits[i]); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2661 | // Return immediately as the initializer is set. |
| 2662 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2663 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2666 | if (HadError) |
| 2667 | return; |
| 2668 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2669 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2670 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2671 | SetCtorInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2674 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2675 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 2676 | CXXRecordDecl *ClassDecl) { |
| 2677 | // Ignore dependent contexts. |
| 2678 | if (ClassDecl->isDependentContext()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2679 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2680 | |
| 2681 | // FIXME: all the access-control diagnostics are positioned on the |
| 2682 | // field/base declaration. That's probably good; that said, the |
| 2683 | // user might reasonably want to know why the destructor is being |
| 2684 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2685 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2686 | // Non-static data members. |
| 2687 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 2688 | E = ClassDecl->field_end(); I != E; ++I) { |
| 2689 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 2690 | if (Field->isInvalidDecl()) |
| 2691 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2692 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 2693 | |
| 2694 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 2695 | if (!RT) |
| 2696 | continue; |
| 2697 | |
| 2698 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2699 | if (FieldClassDecl->isInvalidDecl()) |
| 2700 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2701 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2702 | continue; |
| 2703 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2704 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2705 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2706 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2707 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2708 | << Field->getDeclName() |
| 2709 | << FieldType); |
| 2710 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2711 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2714 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 2715 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2716 | // Bases. |
| 2717 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2718 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2719 | // Bases are always records in a well-formed non-dependent class. |
| 2720 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 2721 | |
| 2722 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2723 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2724 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2725 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2726 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2727 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 2728 | if (BaseClassDecl->isInvalidDecl()) |
| 2729 | continue; |
| 2730 | // Ignore trivial destructors. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2731 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2732 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2733 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2734 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2735 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2736 | |
| 2737 | // FIXME: caret should be on the start of the class name |
| 2738 | CheckDestructorAccess(Base->getSourceRange().getBegin(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2739 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2740 | << Base->getType() |
| 2741 | << Base->getSourceRange()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2742 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2743 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2747 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2748 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2749 | |
| 2750 | // Bases are always records in a well-formed non-dependent class. |
| 2751 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 2752 | |
| 2753 | // Ignore direct virtual bases. |
| 2754 | if (DirectVirtualBases.count(RT)) |
| 2755 | continue; |
| 2756 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2757 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2758 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 2759 | if (BaseClassDecl->isInvalidDecl()) |
| 2760 | continue; |
| 2761 | // Ignore trivial destructors. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2762 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2763 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2764 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2765 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2766 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2767 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2768 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2769 | << VBase->getType()); |
| 2770 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2771 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2772 | } |
| 2773 | } |
| 2774 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2775 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2776 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2777 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2778 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2779 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2780 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2781 | SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2784 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2785 | unsigned DiagID, AbstractDiagSelID SelID) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2786 | if (SelID == -1) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2787 | return RequireNonAbstractType(Loc, T, PDiag(DiagID)); |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2788 | else |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2789 | return RequireNonAbstractType(Loc, T, PDiag(DiagID) << SelID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2792 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2793 | const PartialDiagnostic &PD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2794 | if (!getLangOptions().CPlusPlus) |
| 2795 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2796 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 2797 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2798 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2800 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2801 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2802 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2803 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2805 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2806 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2807 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2808 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2809 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2810 | if (!RT) |
| 2811 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2813 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2814 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2815 | // We can't answer whether something is abstract until it has a |
| 2816 | // definition. If it's currently being defined, we'll walk back |
| 2817 | // over all the declarations when we have a full definition. |
| 2818 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 2819 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2820 | return false; |
| 2821 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2822 | if (!RD->isAbstract()) |
| 2823 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2824 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2825 | Diag(Loc, PD) << RD->getDeclName(); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2826 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2828 | return true; |
| 2829 | } |
| 2830 | |
| 2831 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 2832 | // Check if we've already emitted the list of pure virtual functions |
| 2833 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2834 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2835 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2836 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2837 | CXXFinalOverriderMap FinalOverriders; |
| 2838 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2839 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2840 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 2841 | // more than once. |
| 2842 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 2843 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2844 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2845 | MEnd = FinalOverriders.end(); |
| 2846 | M != MEnd; |
| 2847 | ++M) { |
| 2848 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2849 | SOEnd = M->second.end(); |
| 2850 | SO != SOEnd; ++SO) { |
| 2851 | // C++ [class.abstract]p4: |
| 2852 | // A class is abstract if it contains or inherits at least one |
| 2853 | // pure virtual function for which the final overrider is pure |
| 2854 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2856 | // |
| 2857 | if (SO->second.size() != 1) |
| 2858 | continue; |
| 2859 | |
| 2860 | if (!SO->second.front().Method->isPure()) |
| 2861 | continue; |
| 2862 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2863 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 2864 | continue; |
| 2865 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2866 | Diag(SO->second.front().Method->getLocation(), |
| 2867 | diag::note_pure_virtual_function) |
Chandler Carruth | 45f11b7 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 2868 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2869 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | if (!PureVirtualClassDiagSet) |
| 2873 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 2874 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2877 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2878 | struct AbstractUsageInfo { |
| 2879 | Sema &S; |
| 2880 | CXXRecordDecl *Record; |
| 2881 | CanQualType AbstractType; |
| 2882 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2883 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2884 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 2885 | : S(S), Record(Record), |
| 2886 | AbstractType(S.Context.getCanonicalType( |
| 2887 | S.Context.getTypeDeclType(Record))), |
| 2888 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2889 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2890 | void DiagnoseAbstractType() { |
| 2891 | if (Invalid) return; |
| 2892 | S.DiagnoseAbstractType(Record); |
| 2893 | Invalid = true; |
| 2894 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2895 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2896 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 2897 | }; |
| 2898 | |
| 2899 | struct CheckAbstractUsage { |
| 2900 | AbstractUsageInfo &Info; |
| 2901 | const NamedDecl *Ctx; |
| 2902 | |
| 2903 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 2904 | : Info(Info), Ctx(Ctx) {} |
| 2905 | |
| 2906 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2907 | switch (TL.getTypeLocClass()) { |
| 2908 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 2909 | #define TYPELOC(CLASS, PARENT) \ |
| 2910 | case TypeLoc::CLASS: Check(cast<CLASS##TypeLoc>(TL), Sel); break; |
| 2911 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2912 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2913 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2914 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2915 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2916 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 2917 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
Douglas Gregor | 7019186 | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 2918 | if (!TL.getArg(I)) |
| 2919 | continue; |
| 2920 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2921 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 2922 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2923 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2924 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2925 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2926 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2927 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 2928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2929 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2930 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2931 | // Visit the type parameters from a permissive context. |
| 2932 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2933 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 2934 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 2935 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 2936 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 2937 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2938 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2939 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2940 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2941 | // Visit pointee types from a permissive context. |
| 2942 | #define CheckPolymorphic(Type) \ |
| 2943 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 2944 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 2945 | } |
| 2946 | CheckPolymorphic(PointerTypeLoc) |
| 2947 | CheckPolymorphic(ReferenceTypeLoc) |
| 2948 | CheckPolymorphic(MemberPointerTypeLoc) |
| 2949 | CheckPolymorphic(BlockPointerTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2950 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2951 | /// Handle all the types we haven't given a more specific |
| 2952 | /// implementation for above. |
| 2953 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2954 | // Every other kind of type that we haven't called out already |
| 2955 | // that has an inner type is either (1) sugar or (2) contains that |
| 2956 | // inner type in some way as a subobject. |
| 2957 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 2958 | return Visit(Next, Sel); |
| 2959 | |
| 2960 | // If there's no inner type and we're in a permissive context, |
| 2961 | // don't diagnose. |
| 2962 | if (Sel == Sema::AbstractNone) return; |
| 2963 | |
| 2964 | // Check whether the type matches the abstract type. |
| 2965 | QualType T = TL.getType(); |
| 2966 | if (T->isArrayType()) { |
| 2967 | Sel = Sema::AbstractArrayType; |
| 2968 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2969 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2970 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 2971 | if (CT != Info.AbstractType) return; |
| 2972 | |
| 2973 | // It matched; do some magic. |
| 2974 | if (Sel == Sema::AbstractArrayType) { |
| 2975 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 2976 | << T << TL.getSourceRange(); |
| 2977 | } else { |
| 2978 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 2979 | << Sel << T << TL.getSourceRange(); |
| 2980 | } |
| 2981 | Info.DiagnoseAbstractType(); |
| 2982 | } |
| 2983 | }; |
| 2984 | |
| 2985 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 2986 | Sema::AbstractDiagSelID Sel) { |
| 2987 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 2988 | } |
| 2989 | |
| 2990 | } |
| 2991 | |
| 2992 | /// Check for invalid uses of an abstract type in a method declaration. |
| 2993 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2994 | CXXMethodDecl *MD) { |
| 2995 | // No need to do the check on definitions, which require that |
| 2996 | // the return/param types be complete. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2997 | if (MD->doesThisDeclarationHaveABody()) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2998 | return; |
| 2999 | |
| 3000 | // For safety's sake, just ignore it if we don't have type source |
| 3001 | // information. This should never happen for non-implicit methods, |
| 3002 | // but... |
| 3003 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 3004 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 3005 | } |
| 3006 | |
| 3007 | /// Check for invalid uses of an abstract type within a class definition. |
| 3008 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 3009 | CXXRecordDecl *RD) { |
| 3010 | for (CXXRecordDecl::decl_iterator |
| 3011 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 3012 | Decl *D = *I; |
| 3013 | if (D->isImplicit()) continue; |
| 3014 | |
| 3015 | // Methods and method templates. |
| 3016 | if (isa<CXXMethodDecl>(D)) { |
| 3017 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 3018 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 3019 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 3020 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 3021 | |
| 3022 | // Fields and static variables. |
| 3023 | } else if (isa<FieldDecl>(D)) { |
| 3024 | FieldDecl *FD = cast<FieldDecl>(D); |
| 3025 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 3026 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 3027 | } else if (isa<VarDecl>(D)) { |
| 3028 | VarDecl *VD = cast<VarDecl>(D); |
| 3029 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 3030 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 3031 | |
| 3032 | // Nested classes and class templates. |
| 3033 | } else if (isa<CXXRecordDecl>(D)) { |
| 3034 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 3035 | } else if (isa<ClassTemplateDecl>(D)) { |
| 3036 | CheckAbstractClassUsage(Info, |
| 3037 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 3038 | } |
| 3039 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3042 | /// \brief Perform semantic checks on a class definition that has been |
| 3043 | /// completing, introducing implicitly-declared members, checking for |
| 3044 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3045 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 3046 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3047 | return; |
| 3048 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3049 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 3050 | AbstractUsageInfo Info(*this, Record); |
| 3051 | CheckAbstractClassUsage(Info, Record); |
| 3052 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3053 | |
| 3054 | // If this is not an aggregate type and has no user-declared constructor, |
| 3055 | // complain about any non-static data members of reference or const scalar |
| 3056 | // type, since they will never get initializers. |
| 3057 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 3058 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor()) { |
| 3059 | bool Complained = false; |
| 3060 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 3061 | FEnd = Record->field_end(); |
| 3062 | F != FEnd; ++F) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3063 | if (F->hasInClassInitializer()) |
| 3064 | continue; |
| 3065 | |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3066 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 3067 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3068 | if (!Complained) { |
| 3069 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 3070 | << Record->getTagKind() << Record; |
| 3071 | Complained = true; |
| 3072 | } |
| 3073 | |
| 3074 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 3075 | << F->getType()->isReferenceType() |
| 3076 | << F->getDeclName(); |
| 3077 | } |
| 3078 | } |
| 3079 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3080 | |
Anders Carlsson | a5c6c2a | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 3081 | if (Record->isDynamicClass() && !Record->isDependentType()) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3082 | DynamicClasses.push_back(Record); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3083 | |
| 3084 | if (Record->getIdentifier()) { |
| 3085 | // C++ [class.mem]p13: |
| 3086 | // If T is the name of a class, then each of the following shall have a |
| 3087 | // name different from T: |
| 3088 | // - every member of every anonymous union that is a member of class T. |
| 3089 | // |
| 3090 | // C++ [class.mem]p14: |
| 3091 | // In addition, if class T has a user-declared constructor (12.1), every |
| 3092 | // non-static data member of class T shall have a name different from T. |
| 3093 | for (DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3094 | R.first != R.second; ++R.first) { |
| 3095 | NamedDecl *D = *R.first; |
| 3096 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 3097 | isa<IndirectFieldDecl>(D)) { |
| 3098 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 3099 | << D->getDeclName(); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3100 | break; |
| 3101 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3102 | } |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3103 | } |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3104 | |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 3105 | // Warn if the class has virtual methods but non-virtual public destructor. |
Douglas Gregor | f4b793c | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 3106 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3107 | CXXDestructorDecl *dtor = Record->getDestructor(); |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 3108 | if (!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3109 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 3110 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 3111 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3112 | |
| 3113 | // See if a method overloads virtual methods in a base |
| 3114 | /// class without overriding any. |
| 3115 | if (!Record->isDependentType()) { |
| 3116 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 3117 | MEnd = Record->method_end(); |
| 3118 | M != MEnd; ++M) { |
Argyrios Kyrtzidis | 0266aa3 | 2011-03-03 22:58:57 +0000 | [diff] [blame] | 3119 | if (!(*M)->isStatic()) |
| 3120 | DiagnoseHiddenVirtualMethods(Record, *M); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3121 | } |
| 3122 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 3123 | |
| 3124 | // Declare inherited constructors. We do this eagerly here because: |
| 3125 | // - The standard requires an eager diagnostic for conflicting inherited |
| 3126 | // constructors from different classes. |
| 3127 | // - The lazy declaration of the other implicit constructors is so as to not |
| 3128 | // waste space and performance on classes that are not meant to be |
| 3129 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
| 3130 | // have inherited constructors. |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 3131 | DeclareInheritedConstructors(Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3132 | |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 3133 | if (!Record->isDependentType()) |
| 3134 | CheckExplicitlyDefaultedMethods(Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | void Sema::CheckExplicitlyDefaultedMethods(CXXRecordDecl *Record) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3138 | for (CXXRecordDecl::method_iterator MI = Record->method_begin(), |
| 3139 | ME = Record->method_end(); |
| 3140 | MI != ME; ++MI) { |
| 3141 | if (!MI->isInvalidDecl() && MI->isExplicitlyDefaulted()) { |
| 3142 | switch (getSpecialMember(*MI)) { |
| 3143 | case CXXDefaultConstructor: |
| 3144 | CheckExplicitlyDefaultedDefaultConstructor( |
| 3145 | cast<CXXConstructorDecl>(*MI)); |
| 3146 | break; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3147 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3148 | case CXXDestructor: |
| 3149 | CheckExplicitlyDefaultedDestructor(cast<CXXDestructorDecl>(*MI)); |
| 3150 | break; |
| 3151 | |
| 3152 | case CXXCopyConstructor: |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3153 | CheckExplicitlyDefaultedCopyConstructor(cast<CXXConstructorDecl>(*MI)); |
| 3154 | break; |
| 3155 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3156 | case CXXCopyAssignment: |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3157 | CheckExplicitlyDefaultedCopyAssignment(*MI); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3158 | break; |
| 3159 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3160 | case CXXMoveConstructor: |
| 3161 | case CXXMoveAssignment: |
| 3162 | Diag(MI->getLocation(), diag::err_defaulted_move_unsupported); |
| 3163 | break; |
| 3164 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3165 | default: |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3166 | // FIXME: Do moves once they exist |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3167 | llvm_unreachable("non-special member explicitly defaulted!"); |
| 3168 | } |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3169 | } |
| 3170 | } |
| 3171 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | void Sema::CheckExplicitlyDefaultedDefaultConstructor(CXXConstructorDecl *CD) { |
| 3175 | assert(CD->isExplicitlyDefaulted() && CD->isDefaultConstructor()); |
| 3176 | |
| 3177 | // Whether this was the first-declared instance of the constructor. |
| 3178 | // This affects whether we implicitly add an exception spec (and, eventually, |
| 3179 | // constexpr). It is also ill-formed to explicitly default a constructor such |
| 3180 | // that it would be deleted. (C++0x [decl.fct.def.default]) |
| 3181 | bool First = CD == CD->getCanonicalDecl(); |
| 3182 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3183 | bool HadError = false; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3184 | if (CD->getNumParams() != 0) { |
| 3185 | Diag(CD->getLocation(), diag::err_defaulted_default_ctor_params) |
| 3186 | << CD->getSourceRange(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3187 | HadError = true; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | ImplicitExceptionSpecification Spec |
| 3191 | = ComputeDefaultedDefaultCtorExceptionSpec(CD->getParent()); |
| 3192 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3193 | if (EPI.ExceptionSpecType == EST_Delayed) { |
| 3194 | // Exception specification depends on some deferred part of the class. We'll |
| 3195 | // try again when the class's definition has been fully processed. |
| 3196 | return; |
| 3197 | } |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3198 | const FunctionProtoType *CtorType = CD->getType()->getAs<FunctionProtoType>(), |
| 3199 | *ExceptionType = Context.getFunctionType( |
| 3200 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3201 | |
| 3202 | if (CtorType->hasExceptionSpec()) { |
| 3203 | if (CheckEquivalentExceptionSpec( |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3204 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3205 | << CXXDefaultConstructor, |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3206 | PDiag(), |
| 3207 | ExceptionType, SourceLocation(), |
| 3208 | CtorType, CD->getLocation())) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3209 | HadError = true; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3210 | } |
| 3211 | } else if (First) { |
| 3212 | // We set the declaration to have the computed exception spec here. |
| 3213 | // We know there are no parameters. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3214 | EPI.ExtInfo = CtorType->getExtInfo(); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3215 | CD->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 3216 | } |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3217 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3218 | if (HadError) { |
| 3219 | CD->setInvalidDecl(); |
| 3220 | return; |
| 3221 | } |
| 3222 | |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3223 | if (ShouldDeleteDefaultConstructor(CD)) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3224 | if (First) { |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3225 | CD->setDeletedAsWritten(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3226 | } else { |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3227 | Diag(CD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3228 | << CXXDefaultConstructor; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3229 | CD->setInvalidDecl(); |
| 3230 | } |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | void Sema::CheckExplicitlyDefaultedCopyConstructor(CXXConstructorDecl *CD) { |
| 3235 | assert(CD->isExplicitlyDefaulted() && CD->isCopyConstructor()); |
| 3236 | |
| 3237 | // Whether this was the first-declared instance of the constructor. |
| 3238 | bool First = CD == CD->getCanonicalDecl(); |
| 3239 | |
| 3240 | bool HadError = false; |
| 3241 | if (CD->getNumParams() != 1) { |
| 3242 | Diag(CD->getLocation(), diag::err_defaulted_copy_ctor_params) |
| 3243 | << CD->getSourceRange(); |
| 3244 | HadError = true; |
| 3245 | } |
| 3246 | |
| 3247 | ImplicitExceptionSpecification Spec(Context); |
| 3248 | bool Const; |
| 3249 | llvm::tie(Spec, Const) = |
| 3250 | ComputeDefaultedCopyCtorExceptionSpecAndConst(CD->getParent()); |
| 3251 | |
| 3252 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 3253 | const FunctionProtoType *CtorType = CD->getType()->getAs<FunctionProtoType>(), |
| 3254 | *ExceptionType = Context.getFunctionType( |
| 3255 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3256 | |
| 3257 | // Check for parameter type matching. |
| 3258 | // This is a copy ctor so we know it's a cv-qualified reference to T. |
| 3259 | QualType ArgType = CtorType->getArgType(0); |
| 3260 | if (ArgType->getPointeeType().isVolatileQualified()) { |
| 3261 | Diag(CD->getLocation(), diag::err_defaulted_copy_ctor_volatile_param); |
| 3262 | HadError = true; |
| 3263 | } |
| 3264 | if (ArgType->getPointeeType().isConstQualified() && !Const) { |
| 3265 | Diag(CD->getLocation(), diag::err_defaulted_copy_ctor_const_param); |
| 3266 | HadError = true; |
| 3267 | } |
| 3268 | |
| 3269 | if (CtorType->hasExceptionSpec()) { |
| 3270 | if (CheckEquivalentExceptionSpec( |
| 3271 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3272 | << CXXCopyConstructor, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3273 | PDiag(), |
| 3274 | ExceptionType, SourceLocation(), |
| 3275 | CtorType, CD->getLocation())) { |
| 3276 | HadError = true; |
| 3277 | } |
| 3278 | } else if (First) { |
| 3279 | // We set the declaration to have the computed exception spec here. |
| 3280 | // We duplicate the one parameter type. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3281 | EPI.ExtInfo = CtorType->getExtInfo(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3282 | CD->setType(Context.getFunctionType(Context.VoidTy, &ArgType, 1, EPI)); |
| 3283 | } |
| 3284 | |
| 3285 | if (HadError) { |
| 3286 | CD->setInvalidDecl(); |
| 3287 | return; |
| 3288 | } |
| 3289 | |
| 3290 | if (ShouldDeleteCopyConstructor(CD)) { |
| 3291 | if (First) { |
| 3292 | CD->setDeletedAsWritten(); |
| 3293 | } else { |
| 3294 | Diag(CD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3295 | << CXXCopyConstructor; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3296 | CD->setInvalidDecl(); |
| 3297 | } |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3298 | } |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3299 | } |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3300 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3301 | void Sema::CheckExplicitlyDefaultedCopyAssignment(CXXMethodDecl *MD) { |
| 3302 | assert(MD->isExplicitlyDefaulted()); |
| 3303 | |
| 3304 | // Whether this was the first-declared instance of the operator |
| 3305 | bool First = MD == MD->getCanonicalDecl(); |
| 3306 | |
| 3307 | bool HadError = false; |
| 3308 | if (MD->getNumParams() != 1) { |
| 3309 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_params) |
| 3310 | << MD->getSourceRange(); |
| 3311 | HadError = true; |
| 3312 | } |
| 3313 | |
| 3314 | QualType ReturnType = |
| 3315 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 3316 | if (!ReturnType->isLValueReferenceType() || |
| 3317 | !Context.hasSameType( |
| 3318 | Context.getCanonicalType(ReturnType->getPointeeType()), |
| 3319 | Context.getCanonicalType(Context.getTypeDeclType(MD->getParent())))) { |
| 3320 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_return_type); |
| 3321 | HadError = true; |
| 3322 | } |
| 3323 | |
| 3324 | ImplicitExceptionSpecification Spec(Context); |
| 3325 | bool Const; |
| 3326 | llvm::tie(Spec, Const) = |
| 3327 | ComputeDefaultedCopyCtorExceptionSpecAndConst(MD->getParent()); |
| 3328 | |
| 3329 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 3330 | const FunctionProtoType *OperType = MD->getType()->getAs<FunctionProtoType>(), |
| 3331 | *ExceptionType = Context.getFunctionType( |
| 3332 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3333 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3334 | QualType ArgType = OperType->getArgType(0); |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 3335 | if (!ArgType->isReferenceType()) { |
| 3336 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_not_ref); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3337 | HadError = true; |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 3338 | } else { |
| 3339 | if (ArgType->getPointeeType().isVolatileQualified()) { |
| 3340 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_volatile_param); |
| 3341 | HadError = true; |
| 3342 | } |
| 3343 | if (ArgType->getPointeeType().isConstQualified() && !Const) { |
| 3344 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_const_param); |
| 3345 | HadError = true; |
| 3346 | } |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3347 | } |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 3348 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3349 | if (OperType->getTypeQuals()) { |
| 3350 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_quals); |
| 3351 | HadError = true; |
| 3352 | } |
| 3353 | |
| 3354 | if (OperType->hasExceptionSpec()) { |
| 3355 | if (CheckEquivalentExceptionSpec( |
| 3356 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3357 | << CXXCopyAssignment, |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3358 | PDiag(), |
| 3359 | ExceptionType, SourceLocation(), |
| 3360 | OperType, MD->getLocation())) { |
| 3361 | HadError = true; |
| 3362 | } |
| 3363 | } else if (First) { |
| 3364 | // We set the declaration to have the computed exception spec here. |
| 3365 | // We duplicate the one parameter type. |
| 3366 | EPI.RefQualifier = OperType->getRefQualifier(); |
| 3367 | EPI.ExtInfo = OperType->getExtInfo(); |
| 3368 | MD->setType(Context.getFunctionType(ReturnType, &ArgType, 1, EPI)); |
| 3369 | } |
| 3370 | |
| 3371 | if (HadError) { |
| 3372 | MD->setInvalidDecl(); |
| 3373 | return; |
| 3374 | } |
| 3375 | |
| 3376 | if (ShouldDeleteCopyAssignmentOperator(MD)) { |
| 3377 | if (First) { |
| 3378 | MD->setDeletedAsWritten(); |
| 3379 | } else { |
| 3380 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3381 | << CXXCopyAssignment; |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3382 | MD->setInvalidDecl(); |
| 3383 | } |
| 3384 | } |
| 3385 | } |
| 3386 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3387 | void Sema::CheckExplicitlyDefaultedDestructor(CXXDestructorDecl *DD) { |
| 3388 | assert(DD->isExplicitlyDefaulted()); |
| 3389 | |
| 3390 | // Whether this was the first-declared instance of the destructor. |
| 3391 | bool First = DD == DD->getCanonicalDecl(); |
| 3392 | |
| 3393 | ImplicitExceptionSpecification Spec |
| 3394 | = ComputeDefaultedDtorExceptionSpec(DD->getParent()); |
| 3395 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 3396 | const FunctionProtoType *DtorType = DD->getType()->getAs<FunctionProtoType>(), |
| 3397 | *ExceptionType = Context.getFunctionType( |
| 3398 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3399 | |
| 3400 | if (DtorType->hasExceptionSpec()) { |
| 3401 | if (CheckEquivalentExceptionSpec( |
| 3402 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3403 | << CXXDestructor, |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3404 | PDiag(), |
| 3405 | ExceptionType, SourceLocation(), |
| 3406 | DtorType, DD->getLocation())) { |
| 3407 | DD->setInvalidDecl(); |
| 3408 | return; |
| 3409 | } |
| 3410 | } else if (First) { |
| 3411 | // We set the declaration to have the computed exception spec here. |
| 3412 | // There are no parameters. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3413 | EPI.ExtInfo = DtorType->getExtInfo(); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3414 | DD->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 3415 | } |
| 3416 | |
| 3417 | if (ShouldDeleteDestructor(DD)) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3418 | if (First) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3419 | DD->setDeletedAsWritten(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3420 | } else { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3421 | Diag(DD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3422 | << CXXDestructor; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3423 | DD->setInvalidDecl(); |
| 3424 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3425 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3428 | bool Sema::ShouldDeleteDefaultConstructor(CXXConstructorDecl *CD) { |
| 3429 | CXXRecordDecl *RD = CD->getParent(); |
| 3430 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 3431 | if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3432 | return false; |
| 3433 | |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3434 | SourceLocation Loc = CD->getLocation(); |
| 3435 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3436 | // Do access control from the constructor |
| 3437 | ContextRAII CtorContext(*this, CD); |
| 3438 | |
| 3439 | bool Union = RD->isUnion(); |
| 3440 | bool AllConst = true; |
| 3441 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3442 | // We do this because we should never actually use an anonymous |
| 3443 | // union's constructor. |
| 3444 | if (Union && RD->isAnonymousStructOrUnion()) |
| 3445 | return false; |
| 3446 | |
| 3447 | // FIXME: We should put some diagnostic logic right into this function. |
| 3448 | |
| 3449 | // C++0x [class.ctor]/5 |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3450 | // A defaulted default constructor for class X is defined as deleted if: |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3451 | |
| 3452 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 3453 | BE = RD->bases_end(); |
| 3454 | BI != BE; ++BI) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3455 | // We'll handle this one later |
| 3456 | if (BI->isVirtual()) |
| 3457 | continue; |
| 3458 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3459 | CXXRecordDecl *BaseDecl = BI->getType()->getAsCXXRecordDecl(); |
| 3460 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3461 | |
| 3462 | // -- any [direct base class] has a type with a destructor that is |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3463 | // deleted or inaccessible from the defaulted default constructor |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3464 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3465 | if (BaseDtor->isDeleted()) |
| 3466 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3467 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3468 | AR_accessible) |
| 3469 | return true; |
| 3470 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3471 | // -- any [direct base class either] has no default constructor or |
| 3472 | // overload resolution as applied to [its] default constructor |
| 3473 | // results in an ambiguity or in a function that is deleted or |
| 3474 | // inaccessible from the defaulted default constructor |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3475 | CXXConstructorDecl *BaseDefault = LookupDefaultConstructor(BaseDecl); |
| 3476 | if (!BaseDefault || BaseDefault->isDeleted()) |
| 3477 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3478 | |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3479 | if (CheckConstructorAccess(Loc, BaseDefault, BaseDefault->getAccess(), |
| 3480 | PDiag()) != AR_accessible) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3481 | return true; |
| 3482 | } |
| 3483 | |
| 3484 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 3485 | BE = RD->vbases_end(); |
| 3486 | BI != BE; ++BI) { |
| 3487 | CXXRecordDecl *BaseDecl = BI->getType()->getAsCXXRecordDecl(); |
| 3488 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3489 | |
| 3490 | // -- any [virtual base class] has a type with a destructor that is |
| 3491 | // delete or inaccessible from the defaulted default constructor |
| 3492 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3493 | if (BaseDtor->isDeleted()) |
| 3494 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3495 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3496 | AR_accessible) |
| 3497 | return true; |
| 3498 | |
| 3499 | // -- any [virtual base class either] has no default constructor or |
| 3500 | // overload resolution as applied to [its] default constructor |
| 3501 | // results in an ambiguity or in a function that is deleted or |
| 3502 | // inaccessible from the defaulted default constructor |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3503 | CXXConstructorDecl *BaseDefault = LookupDefaultConstructor(BaseDecl); |
| 3504 | if (!BaseDefault || BaseDefault->isDeleted()) |
| 3505 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3506 | |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3507 | if (CheckConstructorAccess(Loc, BaseDefault, BaseDefault->getAccess(), |
| 3508 | PDiag()) != AR_accessible) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3509 | return true; |
| 3510 | } |
| 3511 | |
| 3512 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 3513 | FE = RD->field_end(); |
| 3514 | FI != FE; ++FI) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3515 | if (FI->isInvalidDecl()) |
| 3516 | continue; |
| 3517 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3518 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 3519 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3520 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3521 | // -- any non-static data member with no brace-or-equal-initializer is of |
| 3522 | // reference type |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3523 | if (FieldType->isReferenceType() && !FI->hasInClassInitializer()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3524 | return true; |
| 3525 | |
| 3526 | // -- X is a union and all its variant members are of const-qualified type |
| 3527 | // (or array thereof) |
| 3528 | if (Union && !FieldType.isConstQualified()) |
| 3529 | AllConst = false; |
| 3530 | |
| 3531 | if (FieldRecord) { |
| 3532 | // -- X is a union-like class that has a variant member with a non-trivial |
| 3533 | // default constructor |
| 3534 | if (Union && !FieldRecord->hasTrivialDefaultConstructor()) |
| 3535 | return true; |
| 3536 | |
| 3537 | CXXDestructorDecl *FieldDtor = LookupDestructor(FieldRecord); |
| 3538 | if (FieldDtor->isDeleted()) |
| 3539 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3540 | if (CheckDestructorAccess(Loc, FieldDtor, PDiag()) != |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3541 | AR_accessible) |
| 3542 | return true; |
| 3543 | |
| 3544 | // -- any non-variant non-static data member of const-qualified type (or |
| 3545 | // array thereof) with no brace-or-equal-initializer does not have a |
| 3546 | // user-provided default constructor |
| 3547 | if (FieldType.isConstQualified() && |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3548 | !FI->hasInClassInitializer() && |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3549 | !FieldRecord->hasUserProvidedDefaultConstructor()) |
| 3550 | return true; |
| 3551 | |
| 3552 | if (!Union && FieldRecord->isUnion() && |
| 3553 | FieldRecord->isAnonymousStructOrUnion()) { |
| 3554 | // We're okay to reuse AllConst here since we only care about the |
| 3555 | // value otherwise if we're in a union. |
| 3556 | AllConst = true; |
| 3557 | |
| 3558 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 3559 | UE = FieldRecord->field_end(); |
| 3560 | UI != UE; ++UI) { |
| 3561 | QualType UnionFieldType = Context.getBaseElementType(UI->getType()); |
| 3562 | CXXRecordDecl *UnionFieldRecord = |
| 3563 | UnionFieldType->getAsCXXRecordDecl(); |
| 3564 | |
| 3565 | if (!UnionFieldType.isConstQualified()) |
| 3566 | AllConst = false; |
| 3567 | |
| 3568 | if (UnionFieldRecord && |
| 3569 | !UnionFieldRecord->hasTrivialDefaultConstructor()) |
| 3570 | return true; |
| 3571 | } |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 3572 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3573 | if (AllConst) |
| 3574 | return true; |
| 3575 | |
| 3576 | // Don't try to initialize the anonymous union |
Sean Hunt | a6bff2c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 3577 | // This is technically non-conformant, but sanity demands it. |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3578 | continue; |
| 3579 | } |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3580 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3581 | // -- any non-static data member with no brace-or-equal-initializer has |
| 3582 | // class type M (or array thereof) and either M has no default |
| 3583 | // constructor or overload resolution as applied to M's default |
| 3584 | // constructor results in an ambiguity or in a function that is deleted |
| 3585 | // or inaccessible from the defaulted default constructor. |
| 3586 | if (!FI->hasInClassInitializer()) { |
| 3587 | CXXConstructorDecl *FieldDefault = LookupDefaultConstructor(FieldRecord); |
| 3588 | if (!FieldDefault || FieldDefault->isDeleted()) |
| 3589 | return true; |
| 3590 | if (CheckConstructorAccess(Loc, FieldDefault, FieldDefault->getAccess(), |
| 3591 | PDiag()) != AR_accessible) |
| 3592 | return true; |
| 3593 | } |
| 3594 | } else if (!Union && FieldType.isConstQualified() && |
| 3595 | !FI->hasInClassInitializer()) { |
Sean Hunt | e340682 | 2011-05-20 21:43:47 +0000 | [diff] [blame] | 3596 | // -- any non-variant non-static data member of const-qualified type (or |
| 3597 | // array thereof) with no brace-or-equal-initializer does not have a |
| 3598 | // user-provided default constructor |
| 3599 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3600 | } |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | if (Union && AllConst) |
| 3604 | return true; |
| 3605 | |
| 3606 | return false; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3609 | bool Sema::ShouldDeleteCopyConstructor(CXXConstructorDecl *CD) { |
Sean Hunt | 493ff72 | 2011-05-18 20:57:13 +0000 | [diff] [blame] | 3610 | CXXRecordDecl *RD = CD->getParent(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3611 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 3612 | if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl()) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3613 | return false; |
| 3614 | |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3615 | SourceLocation Loc = CD->getLocation(); |
| 3616 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3617 | // Do access control from the constructor |
| 3618 | ContextRAII CtorContext(*this, CD); |
| 3619 | |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3620 | bool Union = RD->isUnion(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3621 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3622 | assert(!CD->getParamDecl(0)->getType()->getPointeeType().isNull() && |
| 3623 | "copy assignment arg has no pointee type"); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3624 | unsigned ArgQuals = |
| 3625 | CD->getParamDecl(0)->getType()->getPointeeType().isConstQualified() ? |
| 3626 | Qualifiers::Const : 0; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3627 | |
| 3628 | // We do this because we should never actually use an anonymous |
| 3629 | // union's constructor. |
| 3630 | if (Union && RD->isAnonymousStructOrUnion()) |
| 3631 | return false; |
| 3632 | |
| 3633 | // FIXME: We should put some diagnostic logic right into this function. |
| 3634 | |
| 3635 | // C++0x [class.copy]/11 |
| 3636 | // A defaulted [copy] constructor for class X is defined as delete if X has: |
| 3637 | |
| 3638 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 3639 | BE = RD->bases_end(); |
| 3640 | BI != BE; ++BI) { |
| 3641 | // We'll handle this one later |
| 3642 | if (BI->isVirtual()) |
| 3643 | continue; |
| 3644 | |
| 3645 | QualType BaseType = BI->getType(); |
| 3646 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 3647 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3648 | |
| 3649 | // -- any [direct base class] of a type with a destructor that is deleted or |
| 3650 | // inaccessible from the defaulted constructor |
| 3651 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3652 | if (BaseDtor->isDeleted()) |
| 3653 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3654 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3655 | AR_accessible) |
| 3656 | return true; |
| 3657 | |
| 3658 | // -- a [direct base class] B that cannot be [copied] because overload |
| 3659 | // resolution, as applied to B's [copy] constructor, results in an |
| 3660 | // ambiguity or a function that is deleted or inaccessible from the |
| 3661 | // defaulted constructor |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3662 | CXXConstructorDecl *BaseCtor = LookupCopyingConstructor(BaseDecl, ArgQuals); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3663 | if (!BaseCtor || BaseCtor->isDeleted()) |
| 3664 | return true; |
| 3665 | if (CheckConstructorAccess(Loc, BaseCtor, BaseCtor->getAccess(), PDiag()) != |
| 3666 | AR_accessible) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3667 | return true; |
| 3668 | } |
| 3669 | |
| 3670 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 3671 | BE = RD->vbases_end(); |
| 3672 | BI != BE; ++BI) { |
| 3673 | QualType BaseType = BI->getType(); |
| 3674 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 3675 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3676 | |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3677 | // -- any [virtual base class] of a type with a destructor that is deleted or |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3678 | // inaccessible from the defaulted constructor |
| 3679 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3680 | if (BaseDtor->isDeleted()) |
| 3681 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3682 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3683 | AR_accessible) |
| 3684 | return true; |
| 3685 | |
| 3686 | // -- a [virtual base class] B that cannot be [copied] because overload |
| 3687 | // resolution, as applied to B's [copy] constructor, results in an |
| 3688 | // ambiguity or a function that is deleted or inaccessible from the |
| 3689 | // defaulted constructor |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3690 | CXXConstructorDecl *BaseCtor = LookupCopyingConstructor(BaseDecl, ArgQuals); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3691 | if (!BaseCtor || BaseCtor->isDeleted()) |
| 3692 | return true; |
| 3693 | if (CheckConstructorAccess(Loc, BaseCtor, BaseCtor->getAccess(), PDiag()) != |
| 3694 | AR_accessible) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3695 | return true; |
| 3696 | } |
| 3697 | |
| 3698 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 3699 | FE = RD->field_end(); |
| 3700 | FI != FE; ++FI) { |
| 3701 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 3702 | |
| 3703 | // -- for a copy constructor, a non-static data member of rvalue reference |
| 3704 | // type |
| 3705 | if (FieldType->isRValueReferenceType()) |
| 3706 | return true; |
| 3707 | |
| 3708 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 3709 | |
| 3710 | if (FieldRecord) { |
| 3711 | // This is an anonymous union |
| 3712 | if (FieldRecord->isUnion() && FieldRecord->isAnonymousStructOrUnion()) { |
| 3713 | // Anonymous unions inside unions do not variant members create |
| 3714 | if (!Union) { |
| 3715 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 3716 | UE = FieldRecord->field_end(); |
| 3717 | UI != UE; ++UI) { |
| 3718 | QualType UnionFieldType = Context.getBaseElementType(UI->getType()); |
| 3719 | CXXRecordDecl *UnionFieldRecord = |
| 3720 | UnionFieldType->getAsCXXRecordDecl(); |
| 3721 | |
| 3722 | // -- a variant member with a non-trivial [copy] constructor and X |
| 3723 | // is a union-like class |
| 3724 | if (UnionFieldRecord && |
| 3725 | !UnionFieldRecord->hasTrivialCopyConstructor()) |
| 3726 | return true; |
| 3727 | } |
| 3728 | } |
| 3729 | |
| 3730 | // Don't try to initalize an anonymous union |
| 3731 | continue; |
| 3732 | } else { |
| 3733 | // -- a variant member with a non-trivial [copy] constructor and X is a |
| 3734 | // union-like class |
| 3735 | if (Union && !FieldRecord->hasTrivialCopyConstructor()) |
| 3736 | return true; |
| 3737 | |
| 3738 | // -- any [non-static data member] of a type with a destructor that is |
| 3739 | // deleted or inaccessible from the defaulted constructor |
| 3740 | CXXDestructorDecl *FieldDtor = LookupDestructor(FieldRecord); |
| 3741 | if (FieldDtor->isDeleted()) |
| 3742 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3743 | if (CheckDestructorAccess(Loc, FieldDtor, PDiag()) != |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3744 | AR_accessible) |
| 3745 | return true; |
| 3746 | } |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3747 | |
| 3748 | // -- a [non-static data member of class type (or array thereof)] B that |
| 3749 | // cannot be [copied] because overload resolution, as applied to B's |
| 3750 | // [copy] constructor, results in an ambiguity or a function that is |
| 3751 | // deleted or inaccessible from the defaulted constructor |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3752 | CXXConstructorDecl *FieldCtor = LookupCopyingConstructor(FieldRecord, |
| 3753 | ArgQuals); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3754 | if (!FieldCtor || FieldCtor->isDeleted()) |
| 3755 | return true; |
| 3756 | if (CheckConstructorAccess(Loc, FieldCtor, FieldCtor->getAccess(), |
| 3757 | PDiag()) != AR_accessible) |
| 3758 | return true; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3759 | } |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | return false; |
| 3763 | } |
| 3764 | |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3765 | bool Sema::ShouldDeleteCopyAssignmentOperator(CXXMethodDecl *MD) { |
| 3766 | CXXRecordDecl *RD = MD->getParent(); |
| 3767 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 3768 | if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl()) |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3769 | return false; |
| 3770 | |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3771 | SourceLocation Loc = MD->getLocation(); |
| 3772 | |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3773 | // Do access control from the constructor |
| 3774 | ContextRAII MethodContext(*this, MD); |
| 3775 | |
| 3776 | bool Union = RD->isUnion(); |
| 3777 | |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3778 | unsigned ArgQuals = |
| 3779 | MD->getParamDecl(0)->getType()->getPointeeType().isConstQualified() ? |
| 3780 | Qualifiers::Const : 0; |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3781 | |
| 3782 | // We do this because we should never actually use an anonymous |
| 3783 | // union's constructor. |
| 3784 | if (Union && RD->isAnonymousStructOrUnion()) |
| 3785 | return false; |
| 3786 | |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3787 | // FIXME: We should put some diagnostic logic right into this function. |
| 3788 | |
| 3789 | // C++0x [class.copy]/11 |
| 3790 | // A defaulted [copy] assignment operator for class X is defined as deleted |
| 3791 | // if X has: |
| 3792 | |
| 3793 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 3794 | BE = RD->bases_end(); |
| 3795 | BI != BE; ++BI) { |
| 3796 | // We'll handle this one later |
| 3797 | if (BI->isVirtual()) |
| 3798 | continue; |
| 3799 | |
| 3800 | QualType BaseType = BI->getType(); |
| 3801 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 3802 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3803 | |
| 3804 | // -- a [direct base class] B that cannot be [copied] because overload |
| 3805 | // resolution, as applied to B's [copy] assignment operator, results in |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3806 | // an ambiguity or a function that is deleted or inaccessible from the |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3807 | // assignment operator |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3808 | CXXMethodDecl *CopyOper = LookupCopyingAssignment(BaseDecl, ArgQuals, false, |
| 3809 | 0); |
| 3810 | if (!CopyOper || CopyOper->isDeleted()) |
| 3811 | return true; |
| 3812 | if (CheckDirectMemberAccess(Loc, CopyOper, PDiag()) != AR_accessible) |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3813 | return true; |
| 3814 | } |
| 3815 | |
| 3816 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 3817 | BE = RD->vbases_end(); |
| 3818 | BI != BE; ++BI) { |
| 3819 | QualType BaseType = BI->getType(); |
| 3820 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 3821 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3822 | |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3823 | // -- a [virtual base class] B that cannot be [copied] because overload |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3824 | // resolution, as applied to B's [copy] assignment operator, results in |
| 3825 | // an ambiguity or a function that is deleted or inaccessible from the |
| 3826 | // assignment operator |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3827 | CXXMethodDecl *CopyOper = LookupCopyingAssignment(BaseDecl, ArgQuals, false, |
| 3828 | 0); |
| 3829 | if (!CopyOper || CopyOper->isDeleted()) |
| 3830 | return true; |
| 3831 | if (CheckDirectMemberAccess(Loc, CopyOper, PDiag()) != AR_accessible) |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3832 | return true; |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 3836 | FE = RD->field_end(); |
| 3837 | FI != FE; ++FI) { |
| 3838 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 3839 | |
| 3840 | // -- a non-static data member of reference type |
| 3841 | if (FieldType->isReferenceType()) |
| 3842 | return true; |
| 3843 | |
| 3844 | // -- a non-static data member of const non-class type (or array thereof) |
| 3845 | if (FieldType.isConstQualified() && !FieldType->isRecordType()) |
| 3846 | return true; |
| 3847 | |
| 3848 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 3849 | |
| 3850 | if (FieldRecord) { |
| 3851 | // This is an anonymous union |
| 3852 | if (FieldRecord->isUnion() && FieldRecord->isAnonymousStructOrUnion()) { |
| 3853 | // Anonymous unions inside unions do not variant members create |
| 3854 | if (!Union) { |
| 3855 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 3856 | UE = FieldRecord->field_end(); |
| 3857 | UI != UE; ++UI) { |
| 3858 | QualType UnionFieldType = Context.getBaseElementType(UI->getType()); |
| 3859 | CXXRecordDecl *UnionFieldRecord = |
| 3860 | UnionFieldType->getAsCXXRecordDecl(); |
| 3861 | |
| 3862 | // -- a variant member with a non-trivial [copy] assignment operator |
| 3863 | // and X is a union-like class |
| 3864 | if (UnionFieldRecord && |
| 3865 | !UnionFieldRecord->hasTrivialCopyAssignment()) |
| 3866 | return true; |
| 3867 | } |
| 3868 | } |
| 3869 | |
| 3870 | // Don't try to initalize an anonymous union |
| 3871 | continue; |
| 3872 | // -- a variant member with a non-trivial [copy] assignment operator |
| 3873 | // and X is a union-like class |
| 3874 | } else if (Union && !FieldRecord->hasTrivialCopyAssignment()) { |
| 3875 | return true; |
| 3876 | } |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3877 | |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3878 | CXXMethodDecl *CopyOper = LookupCopyingAssignment(FieldRecord, ArgQuals, |
| 3879 | false, 0); |
| 3880 | if (!CopyOper || CopyOper->isDeleted()) |
| 3881 | return false; |
| 3882 | if (CheckDirectMemberAccess(Loc, CopyOper, PDiag()) != AR_accessible) |
| 3883 | return false; |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3884 | } |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | return false; |
| 3888 | } |
| 3889 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3890 | bool Sema::ShouldDeleteDestructor(CXXDestructorDecl *DD) { |
| 3891 | CXXRecordDecl *RD = DD->getParent(); |
| 3892 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 3893 | if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl()) |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3894 | return false; |
| 3895 | |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3896 | SourceLocation Loc = DD->getLocation(); |
| 3897 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3898 | // Do access control from the destructor |
| 3899 | ContextRAII CtorContext(*this, DD); |
| 3900 | |
| 3901 | bool Union = RD->isUnion(); |
| 3902 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3903 | // We do this because we should never actually use an anonymous |
| 3904 | // union's destructor. |
| 3905 | if (Union && RD->isAnonymousStructOrUnion()) |
| 3906 | return false; |
| 3907 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3908 | // C++0x [class.dtor]p5 |
| 3909 | // A defaulted destructor for a class X is defined as deleted if: |
| 3910 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 3911 | BE = RD->bases_end(); |
| 3912 | BI != BE; ++BI) { |
| 3913 | // We'll handle this one later |
| 3914 | if (BI->isVirtual()) |
| 3915 | continue; |
| 3916 | |
| 3917 | CXXRecordDecl *BaseDecl = BI->getType()->getAsCXXRecordDecl(); |
| 3918 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3919 | assert(BaseDtor && "base has no destructor"); |
| 3920 | |
| 3921 | // -- any direct or virtual base class has a deleted destructor or |
| 3922 | // a destructor that is inaccessible from the defaulted destructor |
| 3923 | if (BaseDtor->isDeleted()) |
| 3924 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3925 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3926 | AR_accessible) |
| 3927 | return true; |
| 3928 | } |
| 3929 | |
| 3930 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 3931 | BE = RD->vbases_end(); |
| 3932 | BI != BE; ++BI) { |
| 3933 | CXXRecordDecl *BaseDecl = BI->getType()->getAsCXXRecordDecl(); |
| 3934 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3935 | assert(BaseDtor && "base has no destructor"); |
| 3936 | |
| 3937 | // -- any direct or virtual base class has a deleted destructor or |
| 3938 | // a destructor that is inaccessible from the defaulted destructor |
| 3939 | if (BaseDtor->isDeleted()) |
| 3940 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3941 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3942 | AR_accessible) |
| 3943 | return true; |
| 3944 | } |
| 3945 | |
| 3946 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 3947 | FE = RD->field_end(); |
| 3948 | FI != FE; ++FI) { |
| 3949 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 3950 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 3951 | if (FieldRecord) { |
| 3952 | if (FieldRecord->isUnion() && FieldRecord->isAnonymousStructOrUnion()) { |
| 3953 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 3954 | UE = FieldRecord->field_end(); |
| 3955 | UI != UE; ++UI) { |
| 3956 | QualType UnionFieldType = Context.getBaseElementType(FI->getType()); |
| 3957 | CXXRecordDecl *UnionFieldRecord = |
| 3958 | UnionFieldType->getAsCXXRecordDecl(); |
| 3959 | |
| 3960 | // -- X is a union-like class that has a variant member with a non- |
| 3961 | // trivial destructor. |
| 3962 | if (UnionFieldRecord && !UnionFieldRecord->hasTrivialDestructor()) |
| 3963 | return true; |
| 3964 | } |
| 3965 | // Technically we are supposed to do this next check unconditionally. |
| 3966 | // But that makes absolutely no sense. |
| 3967 | } else { |
| 3968 | CXXDestructorDecl *FieldDtor = LookupDestructor(FieldRecord); |
| 3969 | |
| 3970 | // -- any of the non-static data members has class type M (or array |
| 3971 | // thereof) and M has a deleted destructor or a destructor that is |
| 3972 | // inaccessible from the defaulted destructor |
| 3973 | if (FieldDtor->isDeleted()) |
| 3974 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3975 | if (CheckDestructorAccess(Loc, FieldDtor, PDiag()) != |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3976 | AR_accessible) |
| 3977 | return true; |
| 3978 | |
| 3979 | // -- X is a union-like class that has a variant member with a non- |
| 3980 | // trivial destructor. |
| 3981 | if (Union && !FieldDtor->isTrivial()) |
| 3982 | return true; |
| 3983 | } |
| 3984 | } |
| 3985 | } |
| 3986 | |
| 3987 | if (DD->isVirtual()) { |
| 3988 | FunctionDecl *OperatorDelete = 0; |
| 3989 | DeclarationName Name = |
| 3990 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3991 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete, |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3992 | false)) |
| 3993 | return true; |
| 3994 | } |
| 3995 | |
| 3996 | |
| 3997 | return false; |
| 3998 | } |
| 3999 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4000 | /// \brief Data used with FindHiddenVirtualMethod |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 4001 | namespace { |
| 4002 | struct FindHiddenVirtualMethodData { |
| 4003 | Sema *S; |
| 4004 | CXXMethodDecl *Method; |
| 4005 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4006 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 4007 | }; |
| 4008 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4009 | |
| 4010 | /// \brief Member lookup function that determines whether a given C++ |
| 4011 | /// method overloads virtual methods in a base class without overriding any, |
| 4012 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 4013 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 4014 | CXXBasePath &Path, |
| 4015 | void *UserData) { |
| 4016 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 4017 | |
| 4018 | FindHiddenVirtualMethodData &Data |
| 4019 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 4020 | |
| 4021 | DeclarationName Name = Data.Method->getDeclName(); |
| 4022 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 4023 | |
| 4024 | bool foundSameNameMethod = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4025 | SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4026 | for (Path.Decls = BaseRecord->lookup(Name); |
| 4027 | Path.Decls.first != Path.Decls.second; |
| 4028 | ++Path.Decls.first) { |
| 4029 | NamedDecl *D = *Path.Decls.first; |
| 4030 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 4031 | MD = MD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4032 | foundSameNameMethod = true; |
| 4033 | // Interested only in hidden virtual methods. |
| 4034 | if (!MD->isVirtual()) |
| 4035 | continue; |
| 4036 | // If the method we are checking overrides a method from its base |
| 4037 | // don't warn about the other overloaded methods. |
| 4038 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 4039 | return true; |
| 4040 | // Collect the overload only if its hidden. |
| 4041 | if (!Data.OverridenAndUsingBaseMethods.count(MD)) |
| 4042 | overloadedMethods.push_back(MD); |
| 4043 | } |
| 4044 | } |
| 4045 | |
| 4046 | if (foundSameNameMethod) |
| 4047 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 4048 | overloadedMethods.end()); |
| 4049 | return foundSameNameMethod; |
| 4050 | } |
| 4051 | |
| 4052 | /// \brief See if a method overloads virtual methods in a base class without |
| 4053 | /// overriding any. |
| 4054 | void Sema::DiagnoseHiddenVirtualMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
| 4055 | if (Diags.getDiagnosticLevel(diag::warn_overloaded_virtual, |
| 4056 | MD->getLocation()) == Diagnostic::Ignored) |
| 4057 | return; |
| 4058 | if (MD->getDeclName().getNameKind() != DeclarationName::Identifier) |
| 4059 | return; |
| 4060 | |
| 4061 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 4062 | /*bool RecordPaths=*/false, |
| 4063 | /*bool DetectVirtual=*/false); |
| 4064 | FindHiddenVirtualMethodData Data; |
| 4065 | Data.Method = MD; |
| 4066 | Data.S = this; |
| 4067 | |
| 4068 | // Keep the base methods that were overriden or introduced in the subclass |
| 4069 | // by 'using' in a set. A base method not in this set is hidden. |
| 4070 | for (DeclContext::lookup_result res = DC->lookup(MD->getDeclName()); |
| 4071 | res.first != res.second; ++res.first) { |
| 4072 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*res.first)) |
| 4073 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 4074 | E = MD->end_overridden_methods(); |
| 4075 | I != E; ++I) |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 4076 | Data.OverridenAndUsingBaseMethods.insert((*I)->getCanonicalDecl()); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4077 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*res.first)) |
| 4078 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(shad->getTargetDecl())) |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 4079 | Data.OverridenAndUsingBaseMethods.insert(MD->getCanonicalDecl()); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths) && |
| 4083 | !Data.OverloadedMethods.empty()) { |
| 4084 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 4085 | << MD << (Data.OverloadedMethods.size() > 1); |
| 4086 | |
| 4087 | for (unsigned i = 0, e = Data.OverloadedMethods.size(); i != e; ++i) { |
| 4088 | CXXMethodDecl *overloadedMD = Data.OverloadedMethods[i]; |
| 4089 | Diag(overloadedMD->getLocation(), |
| 4090 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
| 4091 | } |
| 4092 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4095 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4096 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4097 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 4098 | SourceLocation RBrac, |
| 4099 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4100 | if (!TagDecl) |
| 4101 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4102 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4103 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4104 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4105 | ActOnFields(S, RLoc, TagDecl, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4106 | // strict aliasing violation! |
| 4107 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 4108 | FieldCollector->getCurNumFields(), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4109 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4110 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4111 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4114 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 4115 | /// special functions, such as the default constructor, copy |
| 4116 | /// constructor, or destructor, to the given C++ class (C++ |
| 4117 | /// [special]p1). This routine can only be executed just before the |
| 4118 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4119 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4120 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4121 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4122 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 4123 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 4124 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4125 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4126 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 4127 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
| 4128 | |
| 4129 | // If we have a dynamic class, then the copy assignment operator may be |
| 4130 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
| 4131 | // it shows up in the right place in the vtable and that we diagnose |
| 4132 | // problems with the implicit exception specification. |
| 4133 | if (ClassDecl->isDynamicClass()) |
| 4134 | DeclareImplicitCopyAssignment(ClassDecl); |
| 4135 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4136 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4137 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 4138 | ++ASTContext::NumImplicitDestructors; |
| 4139 | |
| 4140 | // If we have a dynamic class, then the destructor may be virtual, so we |
| 4141 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 4142 | // shows up in the right place in the vtable and that we diagnose problems |
| 4143 | // with the implicit exception specification. |
| 4144 | if (ClassDecl->isDynamicClass()) |
| 4145 | DeclareImplicitDestructor(ClassDecl); |
| 4146 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4149 | void Sema::ActOnReenterDeclaratorTemplateScope(Scope *S, DeclaratorDecl *D) { |
| 4150 | if (!D) |
| 4151 | return; |
| 4152 | |
| 4153 | int NumParamList = D->getNumTemplateParameterLists(); |
| 4154 | for (int i = 0; i < NumParamList; i++) { |
| 4155 | TemplateParameterList* Params = D->getTemplateParameterList(i); |
| 4156 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 4157 | ParamEnd = Params->end(); |
| 4158 | Param != ParamEnd; ++Param) { |
| 4159 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 4160 | if (Named->getDeclName()) { |
| 4161 | S->AddDecl(Named); |
| 4162 | IdResolver.AddDecl(Named); |
| 4163 | } |
| 4164 | } |
| 4165 | } |
| 4166 | } |
| 4167 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4168 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 4169 | if (!D) |
| 4170 | return; |
| 4171 | |
| 4172 | TemplateParameterList *Params = 0; |
| 4173 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 4174 | Params = Template->getTemplateParameters(); |
| 4175 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 4176 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 4177 | Params = PartialSpec->getTemplateParameters(); |
| 4178 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 4179 | return; |
| 4180 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 4181 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 4182 | ParamEnd = Params->end(); |
| 4183 | Param != ParamEnd; ++Param) { |
| 4184 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 4185 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4186 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 4187 | IdResolver.AddDecl(Named); |
| 4188 | } |
| 4189 | } |
| 4190 | } |
| 4191 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4192 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 4193 | if (!RecordD) return; |
| 4194 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4195 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 4196 | PushDeclContext(S, Record); |
| 4197 | } |
| 4198 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4199 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 4200 | if (!RecordD) return; |
| 4201 | PopDeclContext(); |
| 4202 | } |
| 4203 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4204 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 4205 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 4206 | /// parsing those parts of the given Method declaration that could |
| 4207 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 4208 | /// arguments. This action should enter the scope of the given |
| 4209 | /// Method declaration as if we had just parsed the qualified method |
| 4210 | /// name. However, it should not bring the parameters into scope; |
| 4211 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4212 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
| 4215 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 4216 | /// C++ method declaration. We're (re-)introducing the given |
| 4217 | /// function parameter into scope for use in parsing later parts of |
| 4218 | /// the method declaration. For example, we could see an |
| 4219 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4220 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4221 | if (!ParamD) |
| 4222 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4223 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4224 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4225 | |
| 4226 | // If this parameter has an unparsed default argument, clear it out |
| 4227 | // to make way for the parsed default argument. |
| 4228 | if (Param->hasUnparsedDefaultArg()) |
| 4229 | Param->setDefaultArg(0); |
| 4230 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4231 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4232 | if (Param->getDeclName()) |
| 4233 | IdResolver.AddDecl(Param); |
| 4234 | } |
| 4235 | |
| 4236 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 4237 | /// processing the delayed method declaration for Method. The method |
| 4238 | /// declaration is now considered finished. There may be a separate |
| 4239 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 4240 | /// immediately!) for this method, if it was also defined inside the |
| 4241 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4242 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4243 | if (!MethodD) |
| 4244 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4245 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4246 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4247 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4248 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4249 | |
| 4250 | // Now that we have our default arguments, check the constructor |
| 4251 | // again. It could produce additional diagnostics or affect whether |
| 4252 | // the class has implicitly-declared destructors, among other |
| 4253 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4254 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 4255 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4256 | |
| 4257 | // Check the default arguments, which we may have added. |
| 4258 | if (!Method->isInvalidDecl()) |
| 4259 | CheckCXXDefaultArguments(Method); |
| 4260 | } |
| 4261 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4262 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4263 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4264 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4265 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 4266 | /// will be updated to reflect a well-formed type for the constructor and |
| 4267 | /// returned. |
| 4268 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4269 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4270 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4271 | |
| 4272 | // C++ [class.ctor]p3: |
| 4273 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 4274 | // constructor can be invoked for a const, volatile or const |
| 4275 | // volatile object. A constructor shall not be declared const, |
| 4276 | // volatile, or const volatile (9.3.2). |
| 4277 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4278 | if (!D.isInvalidType()) |
| 4279 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 4280 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 4281 | << SourceRange(D.getIdentifierLoc()); |
| 4282 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4283 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4284 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4285 | if (!D.isInvalidType()) |
| 4286 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 4287 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 4288 | << SourceRange(D.getIdentifierLoc()); |
| 4289 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4290 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4291 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4292 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4293 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4294 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4295 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4296 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 4297 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4298 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4299 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 4300 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4301 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4302 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 4303 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4304 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4305 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4306 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 4307 | // C++0x [class.ctor]p4: |
| 4308 | // A constructor shall not be declared with a ref-qualifier. |
| 4309 | if (FTI.hasRefQualifier()) { |
| 4310 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 4311 | << FTI.RefQualifierIsLValueRef |
| 4312 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 4313 | D.setInvalidType(); |
| 4314 | } |
| 4315 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4316 | // Rebuild the function type "R" without any type qualifiers (in |
| 4317 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4318 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4319 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4320 | if (Proto->getResultType() == Context.VoidTy && !D.isInvalidType()) |
| 4321 | return R; |
| 4322 | |
| 4323 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 4324 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 4325 | EPI.RefQualifier = RQ_None; |
| 4326 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4327 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4328 | Proto->getNumArgs(), EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4331 | /// CheckConstructor - Checks a fully-formed constructor for |
| 4332 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 4333 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4334 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4335 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 4336 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 4337 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4338 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4339 | |
| 4340 | // C++ [class.copy]p3: |
| 4341 | // A declaration of a constructor for a class X is ill-formed if |
| 4342 | // its first parameter is of type (optionally cv-qualified) X and |
| 4343 | // either there are no other parameters or else all other |
| 4344 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 4345 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4346 | ((Constructor->getNumParams() == 1) || |
| 4347 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4348 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 4349 | Constructor->getTemplateSpecializationKind() |
| 4350 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4351 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 4352 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 4353 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4354 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 4355 | const char *ConstRef |
| 4356 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 4357 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4358 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 4359 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4360 | |
| 4361 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 4362 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4363 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4364 | } |
| 4365 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 4368 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 4369 | /// well-formedness, issuing any diagnostics required. Returns true |
| 4370 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 4371 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4372 | CXXRecordDecl *RD = Destructor->getParent(); |
| 4373 | |
| 4374 | if (Destructor->isVirtual()) { |
| 4375 | SourceLocation Loc; |
| 4376 | |
| 4377 | if (!Destructor->isImplicit()) |
| 4378 | Loc = Destructor->getLocation(); |
| 4379 | else |
| 4380 | Loc = RD->getLocation(); |
| 4381 | |
| 4382 | // If we have a virtual destructor, look up the deallocation function |
| 4383 | FunctionDecl *OperatorDelete = 0; |
| 4384 | DeclarationName Name = |
| 4385 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 4386 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4387 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 4388 | |
| 4389 | MarkDeclarationReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4390 | |
| 4391 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4392 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4393 | |
| 4394 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4397 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 4398 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 4399 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 4400 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4401 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4404 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 4405 | /// the well-formednes of the destructor declarator @p D with type @p |
| 4406 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4407 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 4408 | /// will be updated to reflect a well-formed type for the destructor and |
| 4409 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4410 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4411 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4412 | // C++ [class.dtor]p1: |
| 4413 | // [...] A typedef-name that names a class is a class-name |
| 4414 | // (7.1.3); however, a typedef-name that names a class shall not |
| 4415 | // be used as the identifier in the declarator for a destructor |
| 4416 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4417 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4418 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4419 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4420 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4421 | else if (const TemplateSpecializationType *TST = |
| 4422 | DeclaratorType->getAs<TemplateSpecializationType>()) |
| 4423 | if (TST->isTypeAlias()) |
| 4424 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
| 4425 | << DeclaratorType << 1; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4426 | |
| 4427 | // C++ [class.dtor]p2: |
| 4428 | // A destructor is used to destroy objects of its class type. A |
| 4429 | // destructor takes no parameters, and no return type can be |
| 4430 | // specified for it (not even void). The address of a destructor |
| 4431 | // shall not be taken. A destructor shall not be static. A |
| 4432 | // destructor can be invoked for a const, volatile or const |
| 4433 | // volatile object. A destructor shall not be declared const, |
| 4434 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4435 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4436 | if (!D.isInvalidType()) |
| 4437 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 4438 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4439 | << SourceRange(D.getIdentifierLoc()) |
| 4440 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 4441 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4442 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4443 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4444 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4445 | // Destructors don't have return types, but the parser will |
| 4446 | // happily parse something like: |
| 4447 | // |
| 4448 | // class X { |
| 4449 | // float ~X(); |
| 4450 | // }; |
| 4451 | // |
| 4452 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4453 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 4454 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 4455 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4456 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4457 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4458 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4459 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4460 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4461 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 4462 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4463 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4464 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 4465 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4466 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4467 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 4468 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4469 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 4472 | // C++0x [class.dtor]p2: |
| 4473 | // A destructor shall not be declared with a ref-qualifier. |
| 4474 | if (FTI.hasRefQualifier()) { |
| 4475 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 4476 | << FTI.RefQualifierIsLValueRef |
| 4477 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 4478 | D.setInvalidType(); |
| 4479 | } |
| 4480 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4481 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 4482 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4483 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 4484 | |
| 4485 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4486 | FTI.freeArgs(); |
| 4487 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4488 | } |
| 4489 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4490 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4491 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4492 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4493 | D.setInvalidType(); |
| 4494 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4495 | |
| 4496 | // Rebuild the function type "R" without any type qualifiers or |
| 4497 | // parameters (in case any of the errors above fired) and with |
| 4498 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4499 | // types. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4500 | if (!D.isInvalidType()) |
| 4501 | return R; |
| 4502 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4503 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4504 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 4505 | EPI.Variadic = false; |
| 4506 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 4507 | EPI.RefQualifier = RQ_None; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4508 | return Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4511 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 4512 | /// well-formednes of the conversion function declarator @p D with |
| 4513 | /// type @p R. If there are any errors in the declarator, this routine |
| 4514 | /// will emit diagnostics and return true. Otherwise, it will return |
| 4515 | /// false. Either way, the type @p R will be updated to reflect a |
| 4516 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4517 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4518 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4519 | // C++ [class.conv.fct]p1: |
| 4520 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4521 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4522 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4523 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4524 | if (!D.isInvalidType()) |
| 4525 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 4526 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 4527 | << SourceRange(D.getIdentifierLoc()); |
| 4528 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4529 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4530 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 4531 | |
| 4532 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 4533 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4534 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4535 | // Conversion functions don't have return types, but the parser will |
| 4536 | // happily parse something like: |
| 4537 | // |
| 4538 | // class X { |
| 4539 | // float operator bool(); |
| 4540 | // }; |
| 4541 | // |
| 4542 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4543 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 4544 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 4545 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 4546 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 4549 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 4550 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4551 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 4552 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4553 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 4554 | |
| 4555 | // Delete the parameters. |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4556 | D.getFunctionTypeInfo().freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4557 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 4558 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4559 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4560 | D.setInvalidType(); |
| 4561 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4562 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 4563 | // Diagnose "&operator bool()" and other such nonsense. This |
| 4564 | // is actually a gcc extension which we don't support. |
| 4565 | if (Proto->getResultType() != ConvType) { |
| 4566 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 4567 | << Proto->getResultType(); |
| 4568 | D.setInvalidType(); |
| 4569 | ConvType = Proto->getResultType(); |
| 4570 | } |
| 4571 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4572 | // C++ [class.conv.fct]p4: |
| 4573 | // The conversion-type-id shall not represent a function type nor |
| 4574 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4575 | if (ConvType->isArrayType()) { |
| 4576 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 4577 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4578 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4579 | } else if (ConvType->isFunctionType()) { |
| 4580 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 4581 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4582 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | // Rebuild the function type "R" without any parameters (in case any |
| 4586 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4587 | // return type. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4588 | if (D.isInvalidType()) |
| 4589 | R = Context.getFunctionType(ConvType, 0, 0, Proto->getExtProtoInfo()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4590 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4591 | // C++0x explicit conversion operators. |
| 4592 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4593 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4594 | diag::warn_explicit_conversion_functions) |
| 4595 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4598 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 4599 | /// the declaration of the given C++ conversion function. This routine |
| 4600 | /// is responsible for recording the conversion function in the C++ |
| 4601 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4602 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4603 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 4604 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 4605 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4606 | |
| 4607 | // Make sure we aren't redeclaring the conversion function. |
| 4608 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4609 | |
| 4610 | // C++ [class.conv.fct]p1: |
| 4611 | // [...] A conversion function is never used to convert a |
| 4612 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 4613 | // same object type (or a reference to it), to a (possibly |
| 4614 | // cv-qualified) base class of that type (or a reference to it), |
| 4615 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4616 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 4617 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4618 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4619 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4620 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4621 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 4622 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 4623 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 4624 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 4625 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4626 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 4627 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 4628 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4629 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4630 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 4631 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4632 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4633 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 4634 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4635 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 4638 | if (FunctionTemplateDecl *ConversionTemplate |
| 4639 | = Conversion->getDescribedFunctionTemplate()) |
| 4640 | return ConversionTemplate; |
| 4641 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4642 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4643 | } |
| 4644 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4645 | //===----------------------------------------------------------------------===// |
| 4646 | // Namespace Handling |
| 4647 | //===----------------------------------------------------------------------===// |
| 4648 | |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 4649 | |
| 4650 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4651 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 4652 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4653 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 4654 | SourceLocation InlineLoc, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 4655 | SourceLocation NamespaceLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 4656 | SourceLocation IdentLoc, |
| 4657 | IdentifierInfo *II, |
| 4658 | SourceLocation LBrace, |
| 4659 | AttributeList *AttrList) { |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 4660 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 4661 | // For anonymous namespace, take the location of the left brace. |
| 4662 | SourceLocation Loc = II ? IdentLoc : LBrace; |
Douglas Gregor | 21e09b6 | 2010-08-19 20:55:47 +0000 | [diff] [blame] | 4663 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 4664 | StartLoc, Loc, II); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 4665 | Namespc->setInline(InlineLoc.isValid()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4666 | |
| 4667 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 4668 | |
Anders Carlsson | 2a3503d | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 4669 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 4670 | |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 4671 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
| 4672 | PushNamespaceVisibilityAttr(Attr); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 4673 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4674 | if (II) { |
| 4675 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 4676 | // The identifier in an original-namespace-definition shall not |
| 4677 | // have been previously defined in the declarative region in |
| 4678 | // which the original-namespace-definition appears. The |
| 4679 | // identifier in an original-namespace-definition is the name of |
| 4680 | // the namespace. Subsequently in that declarative region, it is |
| 4681 | // treated as an original-namespace-name. |
| 4682 | // |
| 4683 | // Since namespace names are unique in their scope, and we don't |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 4684 | // look through using directives, just look for any ordinary names. |
| 4685 | |
| 4686 | const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Member | |
| 4687 | Decl::IDNS_Type | Decl::IDNS_Using | Decl::IDNS_Tag | |
| 4688 | Decl::IDNS_Namespace; |
| 4689 | NamedDecl *PrevDecl = 0; |
| 4690 | for (DeclContext::lookup_result R |
| 4691 | = CurContext->getRedeclContext()->lookup(II); |
| 4692 | R.first != R.second; ++R.first) { |
| 4693 | if ((*R.first)->getIdentifierNamespace() & IDNS) { |
| 4694 | PrevDecl = *R.first; |
| 4695 | break; |
| 4696 | } |
| 4697 | } |
| 4698 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4699 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 4700 | // This is an extended namespace definition. |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 4701 | if (Namespc->isInline() != OrigNS->isInline()) { |
| 4702 | // inline-ness must match |
Douglas Gregor | b7ec906 | 2011-05-20 15:48:31 +0000 | [diff] [blame] | 4703 | if (OrigNS->isInline()) { |
| 4704 | // The user probably just forgot the 'inline', so suggest that it |
| 4705 | // be added back. |
| 4706 | Diag(Namespc->getLocation(), |
| 4707 | diag::warn_inline_namespace_reopened_noninline) |
| 4708 | << FixItHint::CreateInsertion(NamespaceLoc, "inline "); |
| 4709 | } else { |
| 4710 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 4711 | << Namespc->isInline(); |
| 4712 | } |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 4713 | Diag(OrigNS->getLocation(), diag::note_previous_definition); |
Douglas Gregor | b7ec906 | 2011-05-20 15:48:31 +0000 | [diff] [blame] | 4714 | |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 4715 | // Recover by ignoring the new namespace's inline status. |
| 4716 | Namespc->setInline(OrigNS->isInline()); |
| 4717 | } |
| 4718 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4719 | // Attach this namespace decl to the chain of extended namespace |
| 4720 | // definitions. |
| 4721 | OrigNS->setNextNamespace(Namespc); |
| 4722 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4723 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4724 | // Remove the previous declaration from the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4725 | if (DeclRegionScope->isDeclScope(OrigNS)) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 4726 | IdResolver.RemoveDecl(OrigNS); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4727 | DeclRegionScope->RemoveDecl(OrigNS); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4728 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4729 | } else if (PrevDecl) { |
| 4730 | // This is an invalid name redefinition. |
| 4731 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 4732 | << Namespc->getDeclName(); |
| 4733 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 4734 | Namespc->setInvalidDecl(); |
| 4735 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 4736 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4737 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 4738 | // This is the first "real" definition of the namespace "std", so update |
| 4739 | // our cache of the "std" namespace to point at this definition. |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 4740 | if (NamespaceDecl *StdNS = getStdNamespace()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 4741 | // We had already defined a dummy namespace "std". Link this new |
| 4742 | // namespace definition to the dummy namespace "std". |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 4743 | StdNS->setNextNamespace(Namespc); |
| 4744 | StdNS->setLocation(IdentLoc); |
| 4745 | Namespc->setOriginalNamespace(StdNS->getOriginalNamespace()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | // Make our StdNamespace cache point at the first real definition of the |
| 4749 | // "std" namespace. |
| 4750 | StdNamespace = Namespc; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4751 | |
| 4752 | // Add this instance of "std" to the set of known namespaces |
| 4753 | KnownNamespaces[Namespc] = false; |
| 4754 | } else if (!Namespc->isInline()) { |
| 4755 | // Since this is an "original" namespace, add it to the known set of |
| 4756 | // namespaces if it is not an inline namespace. |
| 4757 | KnownNamespaces[Namespc] = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4758 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4759 | |
| 4760 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 4761 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 4762 | // Anonymous namespaces. |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 4763 | assert(Namespc->isAnonymousNamespace()); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 4764 | |
| 4765 | // Link the anonymous namespace into its parent. |
| 4766 | NamespaceDecl *PrevDecl; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4767 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 4768 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 4769 | PrevDecl = TU->getAnonymousNamespace(); |
| 4770 | TU->setAnonymousNamespace(Namespc); |
| 4771 | } else { |
| 4772 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 4773 | PrevDecl = ND->getAnonymousNamespace(); |
| 4774 | ND->setAnonymousNamespace(Namespc); |
| 4775 | } |
| 4776 | |
| 4777 | // Link the anonymous namespace with its previous declaration. |
| 4778 | if (PrevDecl) { |
| 4779 | assert(PrevDecl->isAnonymousNamespace()); |
| 4780 | assert(!PrevDecl->getNextNamespace()); |
| 4781 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 4782 | PrevDecl->setNextNamespace(Namespc); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 4783 | |
| 4784 | if (Namespc->isInline() != PrevDecl->isInline()) { |
| 4785 | // inline-ness must match |
| 4786 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 4787 | << Namespc->isInline(); |
| 4788 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 4789 | Namespc->setInvalidDecl(); |
| 4790 | // Recover by ignoring the new namespace's inline status. |
| 4791 | Namespc->setInline(PrevDecl->isInline()); |
| 4792 | } |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 4793 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 4794 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 4795 | CurContext->addDecl(Namespc); |
| 4796 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 4797 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 4798 | // behaves as if it were replaced by |
| 4799 | // namespace unique { /* empty body */ } |
| 4800 | // using namespace unique; |
| 4801 | // namespace unique { namespace-body } |
| 4802 | // where all occurrences of 'unique' in a translation unit are |
| 4803 | // replaced by the same identifier and this identifier differs |
| 4804 | // from all other identifiers in the entire program. |
| 4805 | |
| 4806 | // We just create the namespace with an empty name and then add an |
| 4807 | // implicit using declaration, just like the standard suggests. |
| 4808 | // |
| 4809 | // CodeGen enforces the "universally unique" aspect by giving all |
| 4810 | // declarations semantically contained within an anonymous |
| 4811 | // namespace internal linkage. |
| 4812 | |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 4813 | if (!PrevDecl) { |
| 4814 | UsingDirectiveDecl* UD |
| 4815 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 4816 | /* 'using' */ LBrace, |
| 4817 | /* 'namespace' */ SourceLocation(), |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 4818 | /* qualifier */ NestedNameSpecifierLoc(), |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 4819 | /* identifier */ SourceLocation(), |
| 4820 | Namespc, |
| 4821 | /* Ancestor */ CurContext); |
| 4822 | UD->setImplicit(); |
| 4823 | CurContext->addDecl(UD); |
| 4824 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
| 4827 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 4828 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4829 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 4830 | // for the namespace has the declarations that showed up in that particular |
| 4831 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4832 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4833 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 4836 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 4837 | /// is a namespace alias, returns the namespace it points to. |
| 4838 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 4839 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 4840 | return AD->getNamespace(); |
| 4841 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 4842 | } |
| 4843 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4844 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 4845 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4846 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4847 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 4848 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 4849 | Namespc->setRBraceLoc(RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4850 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 4851 | if (Namespc->hasAttr<VisibilityAttr>()) |
| 4852 | PopPragmaVisibility(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4853 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4854 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 4855 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 4856 | return cast_or_null<CXXRecordDecl>( |
| 4857 | StdBadAlloc.get(Context.getExternalSource())); |
| 4858 | } |
| 4859 | |
| 4860 | NamespaceDecl *Sema::getStdNamespace() const { |
| 4861 | return cast_or_null<NamespaceDecl>( |
| 4862 | StdNamespace.get(Context.getExternalSource())); |
| 4863 | } |
| 4864 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4865 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 4866 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 4867 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4868 | if (!StdNamespace) { |
| 4869 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 4870 | StdNamespace = NamespaceDecl::Create(Context, |
| 4871 | Context.getTranslationUnitDecl(), |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 4872 | SourceLocation(), SourceLocation(), |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4873 | &PP.getIdentifierTable().get("std")); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 4874 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 4877 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4878 | } |
| 4879 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 4880 | /// \brief Determine whether a using statement is in a context where it will be |
| 4881 | /// apply in all contexts. |
| 4882 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 4883 | switch (CurContext->getDeclKind()) { |
| 4884 | case Decl::TranslationUnit: |
| 4885 | return true; |
| 4886 | case Decl::LinkageSpec: |
| 4887 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 4888 | default: |
| 4889 | return false; |
| 4890 | } |
| 4891 | } |
| 4892 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4893 | static bool TryNamespaceTypoCorrection(Sema &S, LookupResult &R, Scope *Sc, |
| 4894 | CXXScopeSpec &SS, |
| 4895 | SourceLocation IdentLoc, |
| 4896 | IdentifierInfo *Ident) { |
| 4897 | R.clear(); |
| 4898 | if (TypoCorrection Corrected = S.CorrectTypo(R.getLookupNameInfo(), |
| 4899 | R.getLookupKind(), Sc, &SS, NULL, |
| 4900 | false, S.CTC_NoKeywords, NULL)) { |
| 4901 | if (Corrected.getCorrectionDeclAs<NamespaceDecl>() || |
| 4902 | Corrected.getCorrectionDeclAs<NamespaceAliasDecl>()) { |
| 4903 | std::string CorrectedStr(Corrected.getAsString(S.getLangOptions())); |
| 4904 | std::string CorrectedQuotedStr(Corrected.getQuoted(S.getLangOptions())); |
| 4905 | if (DeclContext *DC = S.computeDeclContext(SS, false)) |
| 4906 | S.Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 4907 | << Ident << DC << CorrectedQuotedStr << SS.getRange() |
| 4908 | << FixItHint::CreateReplacement(IdentLoc, CorrectedStr); |
| 4909 | else |
| 4910 | S.Diag(IdentLoc, diag::err_using_directive_suggest) |
| 4911 | << Ident << CorrectedQuotedStr |
| 4912 | << FixItHint::CreateReplacement(IdentLoc, CorrectedStr); |
| 4913 | |
| 4914 | S.Diag(Corrected.getCorrectionDecl()->getLocation(), |
| 4915 | diag::note_namespace_defined_here) << CorrectedQuotedStr; |
| 4916 | |
| 4917 | Ident = Corrected.getCorrectionAsIdentifierInfo(); |
| 4918 | R.addDecl(Corrected.getCorrectionDecl()); |
| 4919 | return true; |
| 4920 | } |
| 4921 | R.setLookupName(Ident); |
| 4922 | } |
| 4923 | return false; |
| 4924 | } |
| 4925 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4926 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4927 | SourceLocation UsingLoc, |
| 4928 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4929 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4930 | SourceLocation IdentLoc, |
| 4931 | IdentifierInfo *NamespcName, |
| 4932 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 4933 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 4934 | assert(NamespcName && "Invalid NamespcName."); |
| 4935 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 4936 | |
| 4937 | // This can only happen along a recovery path. |
| 4938 | while (S->getFlags() & Scope::TemplateParamScope) |
| 4939 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4940 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 4941 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4942 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4943 | NestedNameSpecifier *Qualifier = 0; |
| 4944 | if (SS.isSet()) |
| 4945 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 4946 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 4947 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4948 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 4949 | LookupParsedName(R, S, &SS); |
| 4950 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4951 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4952 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4953 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4954 | R.clear(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4955 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 4956 | // "std" hasn't been defined yet, for GCC compatibility. |
| 4957 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 4958 | NamespcName->isStr("std")) { |
| 4959 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 4960 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4961 | R.resolveKind(); |
| 4962 | } |
| 4963 | // Otherwise, attempt typo correction. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 4964 | else TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4967 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 4968 | NamedDecl *Named = R.getFoundDecl(); |
| 4969 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 4970 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4971 | // C++ [namespace.udir]p1: |
| 4972 | // A using-directive specifies that the names in the nominated |
| 4973 | // namespace can be used in the scope in which the |
| 4974 | // using-directive appears after the using-directive. During |
| 4975 | // unqualified name lookup (3.4.1), the names appear as if they |
| 4976 | // were declared in the nearest enclosing namespace which |
| 4977 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4978 | // namespace. [Note: in this context, "contains" means "contains |
| 4979 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4980 | |
| 4981 | // Find enclosing context containing both using-directive and |
| 4982 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 4983 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4984 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 4985 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 4986 | CommonAncestor = CommonAncestor->getParent(); |
| 4987 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 4988 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 4989 | SS.getWithLocInContext(Context), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 4990 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 4991 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 4992 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 4993 | !SourceMgr.isFromMainFile(SourceMgr.getExpansionLoc(IdentLoc))) { |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 4994 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 4995 | } |
| 4996 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4997 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 4998 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 4999 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5002 | // FIXME: We ignore attributes for now. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5003 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
| 5006 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 5007 | // If scope has associated entity, then using directive is at namespace |
| 5008 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 5009 | // it's lookup structure. |
| 5010 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5011 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 5012 | else |
| 5013 | // Otherwise it is block-sope. using-directives will affect lookup |
| 5014 | // only to the end of scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5015 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 5016 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5017 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5018 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5019 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 5020 | AccessSpecifier AS, |
| 5021 | bool HasUsingKeyword, |
| 5022 | SourceLocation UsingLoc, |
| 5023 | CXXScopeSpec &SS, |
| 5024 | UnqualifiedId &Name, |
| 5025 | AttributeList *AttrList, |
| 5026 | bool IsTypeName, |
| 5027 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5028 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5029 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5030 | switch (Name.getKind()) { |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 5031 | case UnqualifiedId::IK_ImplicitSelfParam: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5032 | case UnqualifiedId::IK_Identifier: |
| 5033 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 5034 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5035 | case UnqualifiedId::IK_ConversionFunctionId: |
| 5036 | break; |
| 5037 | |
| 5038 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 5039 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5040 | // C++0x inherited constructors. |
| 5041 | if (getLangOptions().CPlusPlus0x) break; |
| 5042 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5043 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 5044 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5045 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5046 | |
| 5047 | case UnqualifiedId::IK_DestructorName: |
| 5048 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 5049 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5050 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5051 | |
| 5052 | case UnqualifiedId::IK_TemplateId: |
| 5053 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 5054 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5055 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 5056 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5057 | |
| 5058 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 5059 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5060 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5061 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5062 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 5063 | // Warn about using declarations. |
| 5064 | // TODO: store that the declaration was written without 'using' and |
| 5065 | // talk about access decls instead of using decls in the |
| 5066 | // diagnostics. |
| 5067 | if (!HasUsingKeyword) { |
| 5068 | UsingLoc = Name.getSourceRange().getBegin(); |
| 5069 | |
| 5070 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5071 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 5072 | } |
| 5073 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 5074 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 5075 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 5076 | return 0; |
| 5077 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5078 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5079 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5080 | /* IsInstantiation */ false, |
| 5081 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5082 | if (UD) |
| 5083 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5084 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5085 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 5088 | /// \brief Determine whether a using declaration considers the given |
| 5089 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 5090 | /// the same entity or are both typedefs of the same type. |
| 5091 | static bool |
| 5092 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 5093 | bool &SuppressRedeclaration) { |
| 5094 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 5095 | SuppressRedeclaration = false; |
| 5096 | return true; |
| 5097 | } |
| 5098 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5099 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
| 5100 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 5101 | SuppressRedeclaration = true; |
| 5102 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 5103 | TD2->getUnderlyingType()); |
| 5104 | } |
| 5105 | |
| 5106 | return false; |
| 5107 | } |
| 5108 | |
| 5109 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5110 | /// Determines whether to create a using shadow decl for a particular |
| 5111 | /// decl, given the set of decls existing prior to this using lookup. |
| 5112 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 5113 | const LookupResult &Previous) { |
| 5114 | // Diagnose finding a decl which is not from a base class of the |
| 5115 | // current class. We do this now because there are cases where this |
| 5116 | // function will silently decide not to build a shadow decl, which |
| 5117 | // will pre-empt further diagnostics. |
| 5118 | // |
| 5119 | // We don't need to do this in C++0x because we do the check once on |
| 5120 | // the qualifier. |
| 5121 | // |
| 5122 | // FIXME: diagnose the following if we care enough: |
| 5123 | // struct A { int foo; }; |
| 5124 | // struct B : A { using A::foo; }; |
| 5125 | // template <class T> struct C : A {}; |
| 5126 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 5127 | // This is invalid (during instantiation) in C++03 because B::foo |
| 5128 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 5129 | // We can't diagnose it immediately because C<T> is an unknown |
| 5130 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 5131 | // to A::foo, which will look well-formed when we instantiate. |
| 5132 | // The right solution is to not collapse the shadow-decl chain. |
| 5133 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 5134 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 5135 | |
| 5136 | // Handle enums and anonymous structs. |
| 5137 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 5138 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 5139 | while (OrigRec->isAnonymousStructOrUnion()) |
| 5140 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 5141 | |
| 5142 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 5143 | if (OrigDC == CurContext) { |
| 5144 | Diag(Using->getLocation(), |
| 5145 | diag::err_using_decl_nested_name_specifier_is_current_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5146 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5147 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 5148 | return true; |
| 5149 | } |
| 5150 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5151 | Diag(Using->getQualifierLoc().getBeginLoc(), |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5152 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5153 | << Using->getQualifier() |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5154 | << cast<CXXRecordDecl>(CurContext) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5155 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5156 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 5157 | return true; |
| 5158 | } |
| 5159 | } |
| 5160 | |
| 5161 | if (Previous.empty()) return false; |
| 5162 | |
| 5163 | NamedDecl *Target = Orig; |
| 5164 | if (isa<UsingShadowDecl>(Target)) |
| 5165 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 5166 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 5167 | // If the target happens to be one of the previous declarations, we |
| 5168 | // don't have a conflict. |
| 5169 | // |
| 5170 | // FIXME: but we might be increasing its access, in which case we |
| 5171 | // should redeclare it. |
| 5172 | NamedDecl *NonTag = 0, *Tag = 0; |
| 5173 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5174 | I != E; ++I) { |
| 5175 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 5176 | bool Result; |
| 5177 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 5178 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 5179 | |
| 5180 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 5181 | } |
| 5182 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5183 | if (Target->isFunctionOrFunctionTemplate()) { |
| 5184 | FunctionDecl *FD; |
| 5185 | if (isa<FunctionTemplateDecl>(Target)) |
| 5186 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 5187 | else |
| 5188 | FD = cast<FunctionDecl>(Target); |
| 5189 | |
| 5190 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 5191 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5192 | case Ovl_Overload: |
| 5193 | return false; |
| 5194 | |
| 5195 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5196 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5197 | break; |
| 5198 | |
| 5199 | // We found a decl with the exact signature. |
| 5200 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5201 | // If we're in a record, we want to hide the target, so we |
| 5202 | // return true (without a diagnostic) to tell the caller not to |
| 5203 | // build a shadow decl. |
| 5204 | if (CurContext->isRecord()) |
| 5205 | return true; |
| 5206 | |
| 5207 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5208 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5209 | break; |
| 5210 | } |
| 5211 | |
| 5212 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 5213 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 5214 | return true; |
| 5215 | } |
| 5216 | |
| 5217 | // Target is not a function. |
| 5218 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5219 | if (isa<TagDecl>(Target)) { |
| 5220 | // No conflict between a tag and a non-tag. |
| 5221 | if (!Tag) return false; |
| 5222 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5223 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5224 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 5225 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 5226 | return true; |
| 5227 | } |
| 5228 | |
| 5229 | // No conflict between a tag and a non-tag. |
| 5230 | if (!NonTag) return false; |
| 5231 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5232 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5233 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 5234 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 5235 | return true; |
| 5236 | } |
| 5237 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5238 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5239 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5240 | UsingDecl *UD, |
| 5241 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5242 | |
| 5243 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5244 | NamedDecl *Target = Orig; |
| 5245 | if (isa<UsingShadowDecl>(Target)) { |
| 5246 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 5247 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5251 | = UsingShadowDecl::Create(Context, CurContext, |
| 5252 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5253 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 5254 | |
| 5255 | Shadow->setAccess(UD->getAccess()); |
| 5256 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 5257 | Shadow->setInvalidDecl(); |
| 5258 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5259 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5260 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5261 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5262 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5263 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5264 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5265 | return Shadow; |
| 5266 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5267 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5268 | /// Hides a using shadow declaration. This is required by the current |
| 5269 | /// using-decl implementation when a resolvable using declaration in a |
| 5270 | /// class is followed by a declaration which would hide or override |
| 5271 | /// one or more of the using decl's targets; for example: |
| 5272 | /// |
| 5273 | /// struct Base { void foo(int); }; |
| 5274 | /// struct Derived : Base { |
| 5275 | /// using Base::foo; |
| 5276 | /// void foo(int); |
| 5277 | /// }; |
| 5278 | /// |
| 5279 | /// The governing language is C++03 [namespace.udecl]p12: |
| 5280 | /// |
| 5281 | /// When a using-declaration brings names from a base class into a |
| 5282 | /// derived class scope, member functions in the derived class |
| 5283 | /// override and/or hide member functions with the same name and |
| 5284 | /// parameter types in a base class (rather than conflicting). |
| 5285 | /// |
| 5286 | /// There are two ways to implement this: |
| 5287 | /// (1) optimistically create shadow decls when they're not hidden |
| 5288 | /// by existing declarations, or |
| 5289 | /// (2) don't create any shadow decls (or at least don't make them |
| 5290 | /// visible) until we've fully parsed/instantiated the class. |
| 5291 | /// The problem with (1) is that we might have to retroactively remove |
| 5292 | /// a shadow decl, which requires several O(n) operations because the |
| 5293 | /// decl structures are (very reasonably) not designed for removal. |
| 5294 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 5295 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 5296 | if (Shadow->getDeclName().getNameKind() == |
| 5297 | DeclarationName::CXXConversionFunctionName) |
| 5298 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 5299 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5300 | // Remove it from the DeclContext... |
| 5301 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5302 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5303 | // ...and the scope, if applicable... |
| 5304 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5305 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5306 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5307 | } |
| 5308 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5309 | // ...and the using decl. |
| 5310 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 5311 | |
| 5312 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 5313 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5314 | } |
| 5315 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5316 | /// Builds a using declaration. |
| 5317 | /// |
| 5318 | /// \param IsInstantiation - Whether this call arises from an |
| 5319 | /// instantiation of an unresolved using declaration. We treat |
| 5320 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5321 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 5322 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5323 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5324 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5325 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5326 | bool IsInstantiation, |
| 5327 | bool IsTypeName, |
| 5328 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5329 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5330 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5331 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 5332 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 5333 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5334 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 5335 | if (SS.isEmpty()) { |
| 5336 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5337 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 5338 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5339 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5340 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5341 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5342 | ForRedeclaration); |
| 5343 | Previous.setHideTags(false); |
| 5344 | if (S) { |
| 5345 | LookupName(Previous, S); |
| 5346 | |
| 5347 | // It is really dumb that we have to do this. |
| 5348 | LookupResult::Filter F = Previous.makeFilter(); |
| 5349 | while (F.hasNext()) { |
| 5350 | NamedDecl *D = F.next(); |
| 5351 | if (!isDeclInScope(D, CurContext, S)) |
| 5352 | F.erase(); |
| 5353 | } |
| 5354 | F.done(); |
| 5355 | } else { |
| 5356 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 5357 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 5358 | LookupQualifiedName(Previous, CurContext); |
| 5359 | } |
| 5360 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5361 | // Check for invalid redeclarations. |
| 5362 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 5363 | return 0; |
| 5364 | |
| 5365 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5366 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 5367 | return 0; |
| 5368 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 5369 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5370 | NamedDecl *D; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5371 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 5372 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5373 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5374 | // FIXME: not all declaration name kinds are legal here |
| 5375 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 5376 | UsingLoc, TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5377 | QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5378 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5379 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5380 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 5381 | QualifierLoc, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5382 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5383 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5384 | D = UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, |
| 5385 | NameInfo, IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 5386 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5387 | D->setAccess(AS); |
| 5388 | CurContext->addDecl(D); |
| 5389 | |
| 5390 | if (!LookupContext) return D; |
| 5391 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5392 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5393 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5394 | UD->setInvalidDecl(); |
| 5395 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 5396 | } |
| 5397 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5398 | // Constructor inheriting using decls get special treatment. |
| 5399 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 5400 | if (CheckInheritedConstructorUsingDecl(UD)) |
| 5401 | UD->setInvalidDecl(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5402 | return UD; |
| 5403 | } |
| 5404 | |
| 5405 | // Otherwise, look up the target name. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5406 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5407 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Francois Pichet | b2ee830 | 2011-05-23 03:43:44 +0000 | [diff] [blame] | 5408 | R.setUsingDeclaration(true); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5409 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5410 | // Unlike most lookups, we don't always want to hide tag |
| 5411 | // declarations: tag names are visible through the using declaration |
| 5412 | // even if hidden by ordinary names, *except* in a dependent context |
| 5413 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5414 | if (!IsInstantiation) |
| 5415 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5416 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5417 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5418 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5419 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5420 | Diag(IdentLoc, diag::err_no_member) |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5421 | << NameInfo.getName() << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5422 | UD->setInvalidDecl(); |
| 5423 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5424 | } |
| 5425 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5426 | if (R.isAmbiguous()) { |
| 5427 | UD->setInvalidDecl(); |
| 5428 | return UD; |
| 5429 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5430 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5431 | if (IsTypeName) { |
| 5432 | // 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] | 5433 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5434 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 5435 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 5436 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 5437 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5438 | UD->setInvalidDecl(); |
| 5439 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5440 | } |
| 5441 | } else { |
| 5442 | // If we asked for a non-typename and we got a type, error out, |
| 5443 | // but only if this is an instantiation of an unresolved using |
| 5444 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5445 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5446 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 5447 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5448 | UD->setInvalidDecl(); |
| 5449 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5450 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 5451 | } |
| 5452 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 5453 | // C++0x N2914 [namespace.udecl]p6: |
| 5454 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5455 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 5456 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 5457 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5458 | UD->setInvalidDecl(); |
| 5459 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 5460 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5461 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5462 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 5463 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 5464 | BuildUsingShadowDecl(S, UD, *I); |
| 5465 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5466 | |
| 5467 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5468 | } |
| 5469 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5470 | /// Additional checks for a using declaration referring to a constructor name. |
| 5471 | bool Sema::CheckInheritedConstructorUsingDecl(UsingDecl *UD) { |
| 5472 | if (UD->isTypeName()) { |
| 5473 | // FIXME: Cannot specify typename when specifying constructor |
| 5474 | return true; |
| 5475 | } |
| 5476 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5477 | const Type *SourceType = UD->getQualifier()->getAsType(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5478 | assert(SourceType && |
| 5479 | "Using decl naming constructor doesn't have type in scope spec."); |
| 5480 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 5481 | |
| 5482 | // Check whether the named type is a direct base class. |
| 5483 | CanQualType CanonicalSourceType = SourceType->getCanonicalTypeUnqualified(); |
| 5484 | CXXRecordDecl::base_class_iterator BaseIt, BaseE; |
| 5485 | for (BaseIt = TargetClass->bases_begin(), BaseE = TargetClass->bases_end(); |
| 5486 | BaseIt != BaseE; ++BaseIt) { |
| 5487 | CanQualType BaseType = BaseIt->getType()->getCanonicalTypeUnqualified(); |
| 5488 | if (CanonicalSourceType == BaseType) |
| 5489 | break; |
| 5490 | } |
| 5491 | |
| 5492 | if (BaseIt == BaseE) { |
| 5493 | // Did not find SourceType in the bases. |
| 5494 | Diag(UD->getUsingLocation(), |
| 5495 | diag::err_using_decl_constructor_not_in_direct_base) |
| 5496 | << UD->getNameInfo().getSourceRange() |
| 5497 | << QualType(SourceType, 0) << TargetClass; |
| 5498 | return true; |
| 5499 | } |
| 5500 | |
| 5501 | BaseIt->setInheritConstructors(); |
| 5502 | |
| 5503 | return false; |
| 5504 | } |
| 5505 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5506 | /// Checks that the given using declaration is not an invalid |
| 5507 | /// redeclaration. Note that this is checking only for the using decl |
| 5508 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 5509 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 5510 | bool isTypeName, |
| 5511 | const CXXScopeSpec &SS, |
| 5512 | SourceLocation NameLoc, |
| 5513 | const LookupResult &Prev) { |
| 5514 | // C++03 [namespace.udecl]p8: |
| 5515 | // C++0x [namespace.udecl]p10: |
| 5516 | // A using-declaration is a declaration and can therefore be used |
| 5517 | // repeatedly where (and only where) multiple declarations are |
| 5518 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 5519 | // |
John McCall | 8a72621 | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 5520 | // That's in non-member contexts. |
| 5521 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5522 | return false; |
| 5523 | |
| 5524 | NestedNameSpecifier *Qual |
| 5525 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 5526 | |
| 5527 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 5528 | NamedDecl *D = *I; |
| 5529 | |
| 5530 | bool DTypename; |
| 5531 | NestedNameSpecifier *DQual; |
| 5532 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 5533 | DTypename = UD->isTypeName(); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5534 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5535 | } else if (UnresolvedUsingValueDecl *UD |
| 5536 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 5537 | DTypename = false; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5538 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5539 | } else if (UnresolvedUsingTypenameDecl *UD |
| 5540 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 5541 | DTypename = true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5542 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5543 | } else continue; |
| 5544 | |
| 5545 | // using decls differ if one says 'typename' and the other doesn't. |
| 5546 | // FIXME: non-dependent using decls? |
| 5547 | if (isTypeName != DTypename) continue; |
| 5548 | |
| 5549 | // using decls differ if they name different scopes (but note that |
| 5550 | // template instantiation can cause this check to trigger when it |
| 5551 | // didn't before instantiation). |
| 5552 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 5553 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 5554 | continue; |
| 5555 | |
| 5556 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5557 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5558 | return true; |
| 5559 | } |
| 5560 | |
| 5561 | return false; |
| 5562 | } |
| 5563 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5564 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5565 | /// Checks that the given nested-name qualifier used in a using decl |
| 5566 | /// in the current context is appropriately related to the current |
| 5567 | /// scope. If an error is found, diagnoses it and returns true. |
| 5568 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 5569 | const CXXScopeSpec &SS, |
| 5570 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5571 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5572 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5573 | if (!CurContext->isRecord()) { |
| 5574 | // C++03 [namespace.udecl]p3: |
| 5575 | // C++0x [namespace.udecl]p8: |
| 5576 | // A using-declaration for a class member shall be a member-declaration. |
| 5577 | |
| 5578 | // If we weren't able to compute a valid scope, it must be a |
| 5579 | // dependent class scope. |
| 5580 | if (!NamedContext || NamedContext->isRecord()) { |
| 5581 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 5582 | << SS.getRange(); |
| 5583 | return true; |
| 5584 | } |
| 5585 | |
| 5586 | // Otherwise, everything is known to be fine. |
| 5587 | return false; |
| 5588 | } |
| 5589 | |
| 5590 | // The current scope is a record. |
| 5591 | |
| 5592 | // If the named context is dependent, we can't decide much. |
| 5593 | if (!NamedContext) { |
| 5594 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 5595 | // nested-name-specifier does not refer to a base class, which is |
| 5596 | // still possible in some cases. |
| 5597 | |
| 5598 | // Otherwise we have to conservatively report that things might be |
| 5599 | // okay. |
| 5600 | return false; |
| 5601 | } |
| 5602 | |
| 5603 | if (!NamedContext->isRecord()) { |
| 5604 | // Ideally this would point at the last name in the specifier, |
| 5605 | // but we don't have that level of source info. |
| 5606 | Diag(SS.getRange().getBegin(), |
| 5607 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 5608 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 5609 | return true; |
| 5610 | } |
| 5611 | |
Douglas Gregor | 6fb0729 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 5612 | if (!NamedContext->isDependentContext() && |
| 5613 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 5614 | return true; |
| 5615 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5616 | if (getLangOptions().CPlusPlus0x) { |
| 5617 | // C++0x [namespace.udecl]p3: |
| 5618 | // In a using-declaration used as a member-declaration, the |
| 5619 | // nested-name-specifier shall name a base class of the class |
| 5620 | // being defined. |
| 5621 | |
| 5622 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 5623 | cast<CXXRecordDecl>(NamedContext))) { |
| 5624 | if (CurContext == NamedContext) { |
| 5625 | Diag(NameLoc, |
| 5626 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 5627 | << SS.getRange(); |
| 5628 | return true; |
| 5629 | } |
| 5630 | |
| 5631 | Diag(SS.getRange().getBegin(), |
| 5632 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 5633 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 5634 | << cast<CXXRecordDecl>(CurContext) |
| 5635 | << SS.getRange(); |
| 5636 | return true; |
| 5637 | } |
| 5638 | |
| 5639 | return false; |
| 5640 | } |
| 5641 | |
| 5642 | // C++03 [namespace.udecl]p4: |
| 5643 | // A using-declaration used as a member-declaration shall refer |
| 5644 | // to a member of a base class of the class being defined [etc.]. |
| 5645 | |
| 5646 | // Salient point: SS doesn't have to name a base class as long as |
| 5647 | // lookup only finds members from base classes. Therefore we can |
| 5648 | // diagnose here only if we can prove that that can't happen, |
| 5649 | // i.e. if the class hierarchies provably don't intersect. |
| 5650 | |
| 5651 | // TODO: it would be nice if "definitely valid" results were cached |
| 5652 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 5653 | // need to be repeated. |
| 5654 | |
| 5655 | struct UserData { |
| 5656 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 5657 | |
| 5658 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 5659 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 5660 | Data->Bases.insert(Base); |
| 5661 | return true; |
| 5662 | } |
| 5663 | |
| 5664 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 5665 | return !Class->forallBases(collect, this); |
| 5666 | } |
| 5667 | |
| 5668 | /// Returns true if the base is dependent or is one of the |
| 5669 | /// accumulated base classes. |
| 5670 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 5671 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 5672 | return !Data->Bases.count(Base); |
| 5673 | } |
| 5674 | |
| 5675 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 5676 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 5677 | } |
| 5678 | }; |
| 5679 | |
| 5680 | UserData Data; |
| 5681 | |
| 5682 | // Returns false if we find a dependent base. |
| 5683 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 5684 | return false; |
| 5685 | |
| 5686 | // Returns false if the class has a dependent base or if it or one |
| 5687 | // of its bases is present in the base set of the current context. |
| 5688 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 5689 | return false; |
| 5690 | |
| 5691 | Diag(SS.getRange().getBegin(), |
| 5692 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 5693 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 5694 | << cast<CXXRecordDecl>(CurContext) |
| 5695 | << SS.getRange(); |
| 5696 | |
| 5697 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5698 | } |
| 5699 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5700 | Decl *Sema::ActOnAliasDeclaration(Scope *S, |
| 5701 | AccessSpecifier AS, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5702 | MultiTemplateParamsArg TemplateParamLists, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5703 | SourceLocation UsingLoc, |
| 5704 | UnqualifiedId &Name, |
| 5705 | TypeResult Type) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5706 | // Skip up to the relevant declaration scope. |
| 5707 | while (S->getFlags() & Scope::TemplateParamScope) |
| 5708 | S = S->getParent(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5709 | assert((S->getFlags() & Scope::DeclScope) && |
| 5710 | "got alias-declaration outside of declaration scope"); |
| 5711 | |
| 5712 | if (Type.isInvalid()) |
| 5713 | return 0; |
| 5714 | |
| 5715 | bool Invalid = false; |
| 5716 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
| 5717 | TypeSourceInfo *TInfo = 0; |
Nick Lewycky | b79bf1d | 2011-05-02 01:07:19 +0000 | [diff] [blame] | 5718 | GetTypeFromParser(Type.get(), &TInfo); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5719 | |
| 5720 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
| 5721 | return 0; |
| 5722 | |
| 5723 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5724 | UPPC_DeclarationType)) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5725 | Invalid = true; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5726 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 5727 | TInfo->getTypeLoc().getBeginLoc()); |
| 5728 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5729 | |
| 5730 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, ForRedeclaration); |
| 5731 | LookupName(Previous, S); |
| 5732 | |
| 5733 | // Warn about shadowing the name of a template parameter. |
| 5734 | if (Previous.isSingleResult() && |
| 5735 | Previous.getFoundDecl()->isTemplateParameter()) { |
| 5736 | if (DiagnoseTemplateParameterShadow(Name.StartLocation, |
| 5737 | Previous.getFoundDecl())) |
| 5738 | Invalid = true; |
| 5739 | Previous.clear(); |
| 5740 | } |
| 5741 | |
| 5742 | assert(Name.Kind == UnqualifiedId::IK_Identifier && |
| 5743 | "name in alias declaration must be an identifier"); |
| 5744 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 5745 | Name.StartLocation, |
| 5746 | Name.Identifier, TInfo); |
| 5747 | |
| 5748 | NewTD->setAccess(AS); |
| 5749 | |
| 5750 | if (Invalid) |
| 5751 | NewTD->setInvalidDecl(); |
| 5752 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5753 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 5754 | Invalid |= NewTD->isInvalidDecl(); |
| 5755 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5756 | bool Redeclaration = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5757 | |
| 5758 | NamedDecl *NewND; |
| 5759 | if (TemplateParamLists.size()) { |
| 5760 | TypeAliasTemplateDecl *OldDecl = 0; |
| 5761 | TemplateParameterList *OldTemplateParams = 0; |
| 5762 | |
| 5763 | if (TemplateParamLists.size() != 1) { |
| 5764 | Diag(UsingLoc, diag::err_alias_template_extra_headers) |
| 5765 | << SourceRange(TemplateParamLists.get()[1]->getTemplateLoc(), |
| 5766 | TemplateParamLists.get()[TemplateParamLists.size()-1]->getRAngleLoc()); |
| 5767 | } |
| 5768 | TemplateParameterList *TemplateParams = TemplateParamLists.get()[0]; |
| 5769 | |
| 5770 | // Only consider previous declarations in the same scope. |
| 5771 | FilterLookupForScope(Previous, CurContext, S, /*ConsiderLinkage*/false, |
| 5772 | /*ExplicitInstantiationOrSpecialization*/false); |
| 5773 | if (!Previous.empty()) { |
| 5774 | Redeclaration = true; |
| 5775 | |
| 5776 | OldDecl = Previous.getAsSingle<TypeAliasTemplateDecl>(); |
| 5777 | if (!OldDecl && !Invalid) { |
| 5778 | Diag(UsingLoc, diag::err_redefinition_different_kind) |
| 5779 | << Name.Identifier; |
| 5780 | |
| 5781 | NamedDecl *OldD = Previous.getRepresentativeDecl(); |
| 5782 | if (OldD->getLocation().isValid()) |
| 5783 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| 5784 | |
| 5785 | Invalid = true; |
| 5786 | } |
| 5787 | |
| 5788 | if (!Invalid && OldDecl && !OldDecl->isInvalidDecl()) { |
| 5789 | if (TemplateParameterListsAreEqual(TemplateParams, |
| 5790 | OldDecl->getTemplateParameters(), |
| 5791 | /*Complain=*/true, |
| 5792 | TPL_TemplateMatch)) |
| 5793 | OldTemplateParams = OldDecl->getTemplateParameters(); |
| 5794 | else |
| 5795 | Invalid = true; |
| 5796 | |
| 5797 | TypeAliasDecl *OldTD = OldDecl->getTemplatedDecl(); |
| 5798 | if (!Invalid && |
| 5799 | !Context.hasSameType(OldTD->getUnderlyingType(), |
| 5800 | NewTD->getUnderlyingType())) { |
| 5801 | // FIXME: The C++0x standard does not clearly say this is ill-formed, |
| 5802 | // but we can't reasonably accept it. |
| 5803 | Diag(NewTD->getLocation(), diag::err_redefinition_different_typedef) |
| 5804 | << 2 << NewTD->getUnderlyingType() << OldTD->getUnderlyingType(); |
| 5805 | if (OldTD->getLocation().isValid()) |
| 5806 | Diag(OldTD->getLocation(), diag::note_previous_definition); |
| 5807 | Invalid = true; |
| 5808 | } |
| 5809 | } |
| 5810 | } |
| 5811 | |
| 5812 | // Merge any previous default template arguments into our parameters, |
| 5813 | // and check the parameter list. |
| 5814 | if (CheckTemplateParameterList(TemplateParams, OldTemplateParams, |
| 5815 | TPC_TypeAliasTemplate)) |
| 5816 | return 0; |
| 5817 | |
| 5818 | TypeAliasTemplateDecl *NewDecl = |
| 5819 | TypeAliasTemplateDecl::Create(Context, CurContext, UsingLoc, |
| 5820 | Name.Identifier, TemplateParams, |
| 5821 | NewTD); |
| 5822 | |
| 5823 | NewDecl->setAccess(AS); |
| 5824 | |
| 5825 | if (Invalid) |
| 5826 | NewDecl->setInvalidDecl(); |
| 5827 | else if (OldDecl) |
| 5828 | NewDecl->setPreviousDeclaration(OldDecl); |
| 5829 | |
| 5830 | NewND = NewDecl; |
| 5831 | } else { |
| 5832 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 5833 | NewND = NewTD; |
| 5834 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5835 | |
| 5836 | if (!Redeclaration) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5837 | PushOnScopeChains(NewND, S); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5838 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5839 | return NewND; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5840 | } |
| 5841 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5842 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 5843 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5844 | SourceLocation AliasLoc, |
| 5845 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5846 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 5847 | SourceLocation IdentLoc, |
| 5848 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5849 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 5850 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5851 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 5852 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 5853 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 5854 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 5855 | NamedDecl *PrevDecl |
| 5856 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 5857 | ForRedeclaration); |
| 5858 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 5859 | PrevDecl = 0; |
| 5860 | |
| 5861 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 5862 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5863 | // 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] | 5864 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 5865 | // FIXME: At some point, we'll want to create the (redundant) |
| 5866 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5867 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 5868 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5869 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 5870 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5871 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 5872 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 5873 | diag::err_redefinition_different_kind; |
| 5874 | Diag(AliasLoc, DiagID) << Alias; |
| 5875 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5876 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 5877 | } |
| 5878 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5879 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5880 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5881 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5882 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 5883 | if (!TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, Ident)) { |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 5884 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5885 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 5886 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 5887 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5888 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 5889 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5890 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 5891 | Alias, SS.getWithLocInContext(Context), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5892 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5893 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 5894 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5895 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 5896 | } |
| 5897 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5898 | namespace { |
| 5899 | /// \brief Scoped object used to handle the state changes required in Sema |
| 5900 | /// to implicitly define the body of a C++ member function; |
| 5901 | class ImplicitlyDefinedFunctionScope { |
| 5902 | Sema &S; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5903 | Sema::ContextRAII SavedContext; |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5904 | |
| 5905 | public: |
| 5906 | ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method) |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5907 | : S(S), SavedContext(S, Method) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5908 | { |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5909 | S.PushFunctionScope(); |
| 5910 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 5911 | } |
| 5912 | |
| 5913 | ~ImplicitlyDefinedFunctionScope() { |
| 5914 | S.PopExpressionEvaluationContext(); |
| 5915 | S.PopFunctionOrBlockScope(); |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5916 | } |
| 5917 | }; |
| 5918 | } |
| 5919 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 5920 | Sema::ImplicitExceptionSpecification |
| 5921 | Sema::ComputeDefaultedDefaultCtorExceptionSpec(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5922 | // C++ [except.spec]p14: |
| 5923 | // An implicitly declared special member function (Clause 12) shall have an |
| 5924 | // exception-specification. [...] |
| 5925 | ImplicitExceptionSpecification ExceptSpec(Context); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 5926 | if (ClassDecl->isInvalidDecl()) |
| 5927 | return ExceptSpec; |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5928 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 5929 | // Direct base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5930 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 5931 | BEnd = ClassDecl->bases_end(); |
| 5932 | B != BEnd; ++B) { |
| 5933 | if (B->isVirtual()) // Handled below. |
| 5934 | continue; |
| 5935 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5936 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 5937 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 5938 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 5939 | // If this is a deleted function, add it anyway. This might be conformant |
| 5940 | // with the standard. This might not. I'm not sure. It might not matter. |
| 5941 | if (Constructor) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5942 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5943 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5944 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 5945 | |
| 5946 | // Virtual base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5947 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 5948 | BEnd = ClassDecl->vbases_end(); |
| 5949 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5950 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 5951 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 5952 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 5953 | // If this is a deleted function, add it anyway. This might be conformant |
| 5954 | // with the standard. This might not. I'm not sure. It might not matter. |
| 5955 | if (Constructor) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5956 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5957 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5958 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 5959 | |
| 5960 | // Field constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5961 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 5962 | FEnd = ClassDecl->field_end(); |
| 5963 | F != FEnd; ++F) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 5964 | if (F->hasInClassInitializer()) { |
| 5965 | if (Expr *E = F->getInClassInitializer()) |
| 5966 | ExceptSpec.CalledExpr(E); |
| 5967 | else if (!F->isInvalidDecl()) |
| 5968 | ExceptSpec.SetDelayed(); |
| 5969 | } else if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5970 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 5971 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 5972 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 5973 | // If this is a deleted function, add it anyway. This might be conformant |
| 5974 | // with the standard. This might not. I'm not sure. It might not matter. |
| 5975 | // In particular, the problem is that this function never gets called. It |
| 5976 | // might just be ill-formed because this function attempts to refer to |
| 5977 | // a deleted function here. |
| 5978 | if (Constructor) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5979 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5980 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5981 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5982 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 5983 | return ExceptSpec; |
| 5984 | } |
| 5985 | |
| 5986 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 5987 | CXXRecordDecl *ClassDecl) { |
| 5988 | // C++ [class.ctor]p5: |
| 5989 | // A default constructor for a class X is a constructor of class X |
| 5990 | // that can be called without an argument. If there is no |
| 5991 | // user-declared constructor for class X, a default constructor is |
| 5992 | // implicitly declared. An implicitly-declared default constructor |
| 5993 | // is an inline public member of its class. |
| 5994 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
| 5995 | "Should not build implicit default constructor!"); |
| 5996 | |
| 5997 | ImplicitExceptionSpecification Spec = |
| 5998 | ComputeDefaultedDefaultCtorExceptionSpec(ClassDecl); |
| 5999 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 6000 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 6001 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6002 | CanQualType ClassType |
| 6003 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6004 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6005 | DeclarationName Name |
| 6006 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6007 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6008 | CXXConstructorDecl *DefaultCon |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6009 | = CXXConstructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6010 | Context.getFunctionType(Context.VoidTy, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6011 | 0, 0, EPI), |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6012 | /*TInfo=*/0, |
| 6013 | /*isExplicit=*/false, |
| 6014 | /*isInline=*/true, |
Sean Hunt | 5f802e5 | 2011-05-06 00:11:07 +0000 | [diff] [blame] | 6015 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6016 | DefaultCon->setAccess(AS_public); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 6017 | DefaultCon->setDefaulted(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6018 | DefaultCon->setImplicit(); |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 6019 | DefaultCon->setTrivial(ClassDecl->hasTrivialDefaultConstructor()); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6020 | |
| 6021 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6022 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
| 6023 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 6024 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6025 | PushOnScopeChains(DefaultCon, S, false); |
| 6026 | ClassDecl->addDecl(DefaultCon); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 6027 | |
| 6028 | if (ShouldDeleteDefaultConstructor(DefaultCon)) |
| 6029 | DefaultCon->setDeletedAsWritten(); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 6030 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 6031 | return DefaultCon; |
| 6032 | } |
| 6033 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 6034 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 6035 | CXXConstructorDecl *Constructor) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 6036 | assert((Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 6037 | !Constructor->doesThisDeclarationHaveABody() && |
| 6038 | !Constructor->isDeleted()) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 6039 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6040 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 6041 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 6042 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 6043 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6044 | ImplicitlyDefinedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 6045 | DiagnosticErrorTrap Trap(Diags); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 6046 | if (SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 6047 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6048 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 6049 | << CXXDefaultConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 6050 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 6051 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 6052 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 6053 | |
| 6054 | SourceLocation Loc = Constructor->getLocation(); |
| 6055 | Constructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 6056 | |
| 6057 | Constructor->setUsed(); |
| 6058 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 6059 | |
| 6060 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 6061 | L->CompletedImplicitDefinition(Constructor); |
| 6062 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 6063 | } |
| 6064 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 6065 | /// Get any existing defaulted default constructor for the given class. Do not |
| 6066 | /// implicitly define one if it does not exist. |
| 6067 | static CXXConstructorDecl *getDefaultedDefaultConstructorUnsafe(Sema &Self, |
| 6068 | CXXRecordDecl *D) { |
| 6069 | ASTContext &Context = Self.Context; |
| 6070 | QualType ClassType = Context.getTypeDeclType(D); |
| 6071 | DeclarationName ConstructorName |
| 6072 | = Context.DeclarationNames.getCXXConstructorName( |
| 6073 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
| 6074 | |
| 6075 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 6076 | for (llvm::tie(Con, ConEnd) = D->lookup(ConstructorName); |
| 6077 | Con != ConEnd; ++Con) { |
| 6078 | // A function template cannot be defaulted. |
| 6079 | if (isa<FunctionTemplateDecl>(*Con)) |
| 6080 | continue; |
| 6081 | |
| 6082 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 6083 | if (Constructor->isDefaultConstructor()) |
| 6084 | return Constructor->isDefaulted() ? Constructor : 0; |
| 6085 | } |
| 6086 | return 0; |
| 6087 | } |
| 6088 | |
| 6089 | void Sema::ActOnFinishDelayedMemberInitializers(Decl *D) { |
| 6090 | if (!D) return; |
| 6091 | AdjustDeclIfTemplate(D); |
| 6092 | |
| 6093 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D); |
| 6094 | CXXConstructorDecl *CtorDecl |
| 6095 | = getDefaultedDefaultConstructorUnsafe(*this, ClassDecl); |
| 6096 | |
| 6097 | if (!CtorDecl) return; |
| 6098 | |
| 6099 | // Compute the exception specification for the default constructor. |
| 6100 | const FunctionProtoType *CtorTy = |
| 6101 | CtorDecl->getType()->castAs<FunctionProtoType>(); |
| 6102 | if (CtorTy->getExceptionSpecType() == EST_Delayed) { |
| 6103 | ImplicitExceptionSpecification Spec = |
| 6104 | ComputeDefaultedDefaultCtorExceptionSpec(ClassDecl); |
| 6105 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 6106 | assert(EPI.ExceptionSpecType != EST_Delayed); |
| 6107 | |
| 6108 | CtorDecl->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 6109 | } |
| 6110 | |
| 6111 | // If the default constructor is explicitly defaulted, checking the exception |
| 6112 | // specification is deferred until now. |
| 6113 | if (!CtorDecl->isInvalidDecl() && CtorDecl->isExplicitlyDefaulted() && |
| 6114 | !ClassDecl->isDependentType()) |
| 6115 | CheckExplicitlyDefaultedDefaultConstructor(CtorDecl); |
| 6116 | } |
| 6117 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6118 | void Sema::DeclareInheritedConstructors(CXXRecordDecl *ClassDecl) { |
| 6119 | // We start with an initial pass over the base classes to collect those that |
| 6120 | // inherit constructors from. If there are none, we can forgo all further |
| 6121 | // processing. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6122 | typedef SmallVector<const RecordType *, 4> BasesVector; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6123 | BasesVector BasesToInheritFrom; |
| 6124 | for (CXXRecordDecl::base_class_iterator BaseIt = ClassDecl->bases_begin(), |
| 6125 | BaseE = ClassDecl->bases_end(); |
| 6126 | BaseIt != BaseE; ++BaseIt) { |
| 6127 | if (BaseIt->getInheritConstructors()) { |
| 6128 | QualType Base = BaseIt->getType(); |
| 6129 | if (Base->isDependentType()) { |
| 6130 | // If we inherit constructors from anything that is dependent, just |
| 6131 | // abort processing altogether. We'll get another chance for the |
| 6132 | // instantiations. |
| 6133 | return; |
| 6134 | } |
| 6135 | BasesToInheritFrom.push_back(Base->castAs<RecordType>()); |
| 6136 | } |
| 6137 | } |
| 6138 | if (BasesToInheritFrom.empty()) |
| 6139 | return; |
| 6140 | |
| 6141 | // Now collect the constructors that we already have in the current class. |
| 6142 | // Those take precedence over inherited constructors. |
| 6143 | // C++0x [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 6144 | // unless there is a user-declared constructor with the same signature in |
| 6145 | // the class where the using-declaration appears. |
| 6146 | llvm::SmallSet<const Type *, 8> ExistingConstructors; |
| 6147 | for (CXXRecordDecl::ctor_iterator CtorIt = ClassDecl->ctor_begin(), |
| 6148 | CtorE = ClassDecl->ctor_end(); |
| 6149 | CtorIt != CtorE; ++CtorIt) { |
| 6150 | ExistingConstructors.insert( |
| 6151 | Context.getCanonicalType(CtorIt->getType()).getTypePtr()); |
| 6152 | } |
| 6153 | |
| 6154 | Scope *S = getScopeForContext(ClassDecl); |
| 6155 | DeclarationName CreatedCtorName = |
| 6156 | Context.DeclarationNames.getCXXConstructorName( |
| 6157 | ClassDecl->getTypeForDecl()->getCanonicalTypeUnqualified()); |
| 6158 | |
| 6159 | // Now comes the true work. |
| 6160 | // First, we keep a map from constructor types to the base that introduced |
| 6161 | // them. Needed for finding conflicting constructors. We also keep the |
| 6162 | // actually inserted declarations in there, for pretty diagnostics. |
| 6163 | typedef std::pair<CanQualType, CXXConstructorDecl *> ConstructorInfo; |
| 6164 | typedef llvm::DenseMap<const Type *, ConstructorInfo> ConstructorToSourceMap; |
| 6165 | ConstructorToSourceMap InheritedConstructors; |
| 6166 | for (BasesVector::iterator BaseIt = BasesToInheritFrom.begin(), |
| 6167 | BaseE = BasesToInheritFrom.end(); |
| 6168 | BaseIt != BaseE; ++BaseIt) { |
| 6169 | const RecordType *Base = *BaseIt; |
| 6170 | CanQualType CanonicalBase = Base->getCanonicalTypeUnqualified(); |
| 6171 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(Base->getDecl()); |
| 6172 | for (CXXRecordDecl::ctor_iterator CtorIt = BaseDecl->ctor_begin(), |
| 6173 | CtorE = BaseDecl->ctor_end(); |
| 6174 | CtorIt != CtorE; ++CtorIt) { |
| 6175 | // Find the using declaration for inheriting this base's constructors. |
| 6176 | DeclarationName Name = |
| 6177 | Context.DeclarationNames.getCXXConstructorName(CanonicalBase); |
| 6178 | UsingDecl *UD = dyn_cast_or_null<UsingDecl>( |
| 6179 | LookupSingleName(S, Name,SourceLocation(), LookupUsingDeclName)); |
| 6180 | SourceLocation UsingLoc = UD ? UD->getLocation() : |
| 6181 | ClassDecl->getLocation(); |
| 6182 | |
| 6183 | // C++0x [class.inhctor]p1: The candidate set of inherited constructors |
| 6184 | // from the class X named in the using-declaration consists of actual |
| 6185 | // constructors and notional constructors that result from the |
| 6186 | // transformation of defaulted parameters as follows: |
| 6187 | // - all non-template default constructors of X, and |
| 6188 | // - for each non-template constructor of X that has at least one |
| 6189 | // parameter with a default argument, the set of constructors that |
| 6190 | // results from omitting any ellipsis parameter specification and |
| 6191 | // successively omitting parameters with a default argument from the |
| 6192 | // end of the parameter-type-list. |
| 6193 | CXXConstructorDecl *BaseCtor = *CtorIt; |
| 6194 | bool CanBeCopyOrMove = BaseCtor->isCopyOrMoveConstructor(); |
| 6195 | const FunctionProtoType *BaseCtorType = |
| 6196 | BaseCtor->getType()->getAs<FunctionProtoType>(); |
| 6197 | |
| 6198 | for (unsigned params = BaseCtor->getMinRequiredArguments(), |
| 6199 | maxParams = BaseCtor->getNumParams(); |
| 6200 | params <= maxParams; ++params) { |
| 6201 | // Skip default constructors. They're never inherited. |
| 6202 | if (params == 0) |
| 6203 | continue; |
| 6204 | // Skip copy and move constructors for the same reason. |
| 6205 | if (CanBeCopyOrMove && params == 1) |
| 6206 | continue; |
| 6207 | |
| 6208 | // Build up a function type for this particular constructor. |
| 6209 | // FIXME: The working paper does not consider that the exception spec |
| 6210 | // for the inheriting constructor might be larger than that of the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 6211 | // source. This code doesn't yet, either. When it does, this code will |
| 6212 | // need to be delayed until after exception specifications and in-class |
| 6213 | // member initializers are attached. |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6214 | const Type *NewCtorType; |
| 6215 | if (params == maxParams) |
| 6216 | NewCtorType = BaseCtorType; |
| 6217 | else { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6218 | SmallVector<QualType, 16> Args; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6219 | for (unsigned i = 0; i < params; ++i) { |
| 6220 | Args.push_back(BaseCtorType->getArgType(i)); |
| 6221 | } |
| 6222 | FunctionProtoType::ExtProtoInfo ExtInfo = |
| 6223 | BaseCtorType->getExtProtoInfo(); |
| 6224 | ExtInfo.Variadic = false; |
| 6225 | NewCtorType = Context.getFunctionType(BaseCtorType->getResultType(), |
| 6226 | Args.data(), params, ExtInfo) |
| 6227 | .getTypePtr(); |
| 6228 | } |
| 6229 | const Type *CanonicalNewCtorType = |
| 6230 | Context.getCanonicalType(NewCtorType); |
| 6231 | |
| 6232 | // Now that we have the type, first check if the class already has a |
| 6233 | // constructor with this signature. |
| 6234 | if (ExistingConstructors.count(CanonicalNewCtorType)) |
| 6235 | continue; |
| 6236 | |
| 6237 | // Then we check if we have already declared an inherited constructor |
| 6238 | // with this signature. |
| 6239 | std::pair<ConstructorToSourceMap::iterator, bool> result = |
| 6240 | InheritedConstructors.insert(std::make_pair( |
| 6241 | CanonicalNewCtorType, |
| 6242 | std::make_pair(CanonicalBase, (CXXConstructorDecl*)0))); |
| 6243 | if (!result.second) { |
| 6244 | // Already in the map. If it came from a different class, that's an |
| 6245 | // error. Not if it's from the same. |
| 6246 | CanQualType PreviousBase = result.first->second.first; |
| 6247 | if (CanonicalBase != PreviousBase) { |
| 6248 | const CXXConstructorDecl *PrevCtor = result.first->second.second; |
| 6249 | const CXXConstructorDecl *PrevBaseCtor = |
| 6250 | PrevCtor->getInheritedConstructor(); |
| 6251 | assert(PrevBaseCtor && "Conflicting constructor was not inherited"); |
| 6252 | |
| 6253 | Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 6254 | Diag(BaseCtor->getLocation(), |
| 6255 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 6256 | Diag(PrevBaseCtor->getLocation(), |
| 6257 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 6258 | Diag(PrevCtor->getLocation(), |
| 6259 | diag::note_using_decl_constructor_conflict_previous_using); |
| 6260 | } |
| 6261 | continue; |
| 6262 | } |
| 6263 | |
| 6264 | // OK, we're there, now add the constructor. |
| 6265 | // C++0x [class.inhctor]p8: [...] that would be performed by a |
| 6266 | // user-writtern inline constructor [...] |
| 6267 | DeclarationNameInfo DNI(CreatedCtorName, UsingLoc); |
| 6268 | CXXConstructorDecl *NewCtor = CXXConstructorDecl::Create( |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6269 | Context, ClassDecl, UsingLoc, DNI, QualType(NewCtorType, 0), |
| 6270 | /*TInfo=*/0, BaseCtor->isExplicit(), /*Inline=*/true, |
Sean Hunt | 5f802e5 | 2011-05-06 00:11:07 +0000 | [diff] [blame] | 6271 | /*ImplicitlyDeclared=*/true); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6272 | NewCtor->setAccess(BaseCtor->getAccess()); |
| 6273 | |
| 6274 | // Build up the parameter decls and add them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6275 | SmallVector<ParmVarDecl *, 16> ParamDecls; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6276 | for (unsigned i = 0; i < params; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6277 | ParamDecls.push_back(ParmVarDecl::Create(Context, NewCtor, |
| 6278 | UsingLoc, UsingLoc, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6279 | /*IdentifierInfo=*/0, |
| 6280 | BaseCtorType->getArgType(i), |
| 6281 | /*TInfo=*/0, SC_None, |
| 6282 | SC_None, /*DefaultArg=*/0)); |
| 6283 | } |
| 6284 | NewCtor->setParams(ParamDecls.data(), ParamDecls.size()); |
| 6285 | NewCtor->setInheritedConstructor(BaseCtor); |
| 6286 | |
| 6287 | PushOnScopeChains(NewCtor, S, false); |
| 6288 | ClassDecl->addDecl(NewCtor); |
| 6289 | result.first->second.second = NewCtor; |
| 6290 | } |
| 6291 | } |
| 6292 | } |
| 6293 | } |
| 6294 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6295 | Sema::ImplicitExceptionSpecification |
| 6296 | Sema::ComputeDefaultedDtorExceptionSpec(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6297 | // C++ [except.spec]p14: |
| 6298 | // An implicitly declared special member function (Clause 12) shall have |
| 6299 | // an exception-specification. |
| 6300 | ImplicitExceptionSpecification ExceptSpec(Context); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 6301 | if (ClassDecl->isInvalidDecl()) |
| 6302 | return ExceptSpec; |
| 6303 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6304 | // Direct base-class destructors. |
| 6305 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 6306 | BEnd = ClassDecl->bases_end(); |
| 6307 | B != BEnd; ++B) { |
| 6308 | if (B->isVirtual()) // Handled below. |
| 6309 | continue; |
| 6310 | |
| 6311 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 6312 | ExceptSpec.CalledDecl( |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6313 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6314 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6315 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6316 | // Virtual base-class destructors. |
| 6317 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 6318 | BEnd = ClassDecl->vbases_end(); |
| 6319 | B != BEnd; ++B) { |
| 6320 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 6321 | ExceptSpec.CalledDecl( |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6322 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6323 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6324 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6325 | // Field destructors. |
| 6326 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 6327 | FEnd = ClassDecl->field_end(); |
| 6328 | F != FEnd; ++F) { |
| 6329 | if (const RecordType *RecordTy |
| 6330 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
| 6331 | ExceptSpec.CalledDecl( |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6332 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6333 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6334 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6335 | return ExceptSpec; |
| 6336 | } |
| 6337 | |
| 6338 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
| 6339 | // C++ [class.dtor]p2: |
| 6340 | // If a class has no user-declared destructor, a destructor is |
| 6341 | // declared implicitly. An implicitly-declared destructor is an |
| 6342 | // inline public member of its class. |
| 6343 | |
| 6344 | ImplicitExceptionSpecification Spec = |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6345 | ComputeDefaultedDtorExceptionSpec(ClassDecl); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6346 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 6347 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6348 | // Create the actual destructor declaration. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6349 | QualType Ty = Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6350 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6351 | CanQualType ClassType |
| 6352 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6353 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6354 | DeclarationName Name |
| 6355 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6356 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6357 | CXXDestructorDecl *Destructor |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6358 | = CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, Ty, 0, |
| 6359 | /*isInline=*/true, |
| 6360 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6361 | Destructor->setAccess(AS_public); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6362 | Destructor->setDefaulted(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6363 | Destructor->setImplicit(); |
| 6364 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6365 | |
| 6366 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6367 | ++ASTContext::NumImplicitDestructorsDeclared; |
| 6368 | |
| 6369 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 6370 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6371 | PushOnScopeChains(Destructor, S, false); |
| 6372 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6373 | |
| 6374 | // This could be uniqued if it ever proves significant. |
| 6375 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6376 | |
| 6377 | if (ShouldDeleteDestructor(Destructor)) |
| 6378 | Destructor->setDeletedAsWritten(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6379 | |
| 6380 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6381 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6382 | return Destructor; |
| 6383 | } |
| 6384 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6385 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 6386 | CXXDestructorDecl *Destructor) { |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 6387 | assert((Destructor->isDefaulted() && |
| 6388 | !Destructor->doesThisDeclarationHaveABody()) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6389 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6390 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6391 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6392 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 6393 | if (Destructor->isInvalidDecl()) |
| 6394 | return; |
| 6395 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6396 | ImplicitlyDefinedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6397 | |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 6398 | DiagnosticErrorTrap Trap(Diags); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 6399 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 6400 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6401 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 6402 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6403 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 6404 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 6405 | |
| 6406 | Destructor->setInvalidDecl(); |
| 6407 | return; |
| 6408 | } |
| 6409 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 6410 | SourceLocation Loc = Destructor->getLocation(); |
| 6411 | Destructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 6412 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6413 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6414 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 6415 | |
| 6416 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 6417 | L->CompletedImplicitDefinition(Destructor); |
| 6418 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6419 | } |
| 6420 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6421 | void Sema::AdjustDestructorExceptionSpec(CXXRecordDecl *classDecl, |
| 6422 | CXXDestructorDecl *destructor) { |
| 6423 | // C++11 [class.dtor]p3: |
| 6424 | // A declaration of a destructor that does not have an exception- |
| 6425 | // specification is implicitly considered to have the same exception- |
| 6426 | // specification as an implicit declaration. |
| 6427 | const FunctionProtoType *dtorType = destructor->getType()-> |
| 6428 | getAs<FunctionProtoType>(); |
| 6429 | if (dtorType->hasExceptionSpec()) |
| 6430 | return; |
| 6431 | |
| 6432 | ImplicitExceptionSpecification exceptSpec = |
| 6433 | ComputeDefaultedDtorExceptionSpec(classDecl); |
| 6434 | |
| 6435 | // Replace the destructor's type. |
| 6436 | FunctionProtoType::ExtProtoInfo epi; |
| 6437 | epi.ExceptionSpecType = exceptSpec.getExceptionSpecType(); |
| 6438 | epi.NumExceptions = exceptSpec.size(); |
| 6439 | epi.Exceptions = exceptSpec.data(); |
| 6440 | QualType ty = Context.getFunctionType(Context.VoidTy, 0, 0, epi); |
| 6441 | |
| 6442 | destructor->setType(ty); |
| 6443 | |
| 6444 | // FIXME: If the destructor has a body that could throw, and the newly created |
| 6445 | // spec doesn't allow exceptions, we should emit a warning, because this |
| 6446 | // change in behavior can break conforming C++03 programs at runtime. |
| 6447 | // However, we don't have a body yet, so it needs to be done somewhere else. |
| 6448 | } |
| 6449 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6450 | /// \brief Builds a statement that copies the given entity from \p From to |
| 6451 | /// \c To. |
| 6452 | /// |
| 6453 | /// This routine is used to copy the members of a class with an |
| 6454 | /// implicitly-declared copy assignment operator. When the entities being |
| 6455 | /// copied are arrays, this routine builds for loops to copy them. |
| 6456 | /// |
| 6457 | /// \param S The Sema object used for type-checking. |
| 6458 | /// |
| 6459 | /// \param Loc The location where the implicit copy is being generated. |
| 6460 | /// |
| 6461 | /// \param T The type of the expressions being copied. Both expressions must |
| 6462 | /// have this type. |
| 6463 | /// |
| 6464 | /// \param To The expression we are copying to. |
| 6465 | /// |
| 6466 | /// \param From The expression we are copying from. |
| 6467 | /// |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 6468 | /// \param CopyingBaseSubobject Whether we're copying a base subobject. |
| 6469 | /// Otherwise, it's a non-static member subobject. |
| 6470 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6471 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 6472 | /// |
| 6473 | /// \returns A statement or a loop that copies the expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6474 | static StmtResult |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6475 | BuildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6476 | Expr *To, Expr *From, |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 6477 | bool CopyingBaseSubobject, unsigned Depth = 0) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6478 | // C++0x [class.copy]p30: |
| 6479 | // Each subobject is assigned in the manner appropriate to its type: |
| 6480 | // |
| 6481 | // - if the subobject is of class type, the copy assignment operator |
| 6482 | // for the class is used (as if by explicit qualification; that is, |
| 6483 | // ignoring any possible virtual overriding functions in more derived |
| 6484 | // classes); |
| 6485 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 6486 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 6487 | |
| 6488 | // Look for operator=. |
| 6489 | DeclarationName Name |
| 6490 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 6491 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 6492 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| 6493 | |
| 6494 | // Filter out any result that isn't a copy-assignment operator. |
| 6495 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 6496 | while (F.hasNext()) { |
| 6497 | NamedDecl *D = F.next(); |
| 6498 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 6499 | if (Method->isCopyAssignmentOperator()) |
| 6500 | continue; |
| 6501 | |
| 6502 | F.erase(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 6503 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6504 | F.done(); |
| 6505 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 6506 | // Suppress the protected check (C++ [class.protected]) for each of the |
| 6507 | // assignment operators we found. This strange dance is required when |
| 6508 | // we're assigning via a base classes's copy-assignment operator. To |
| 6509 | // ensure that we're getting the right base class subobject (without |
| 6510 | // ambiguities), we need to cast "this" to that subobject type; to |
| 6511 | // ensure that we don't go through the virtual call mechanism, we need |
| 6512 | // to qualify the operator= name with the base class (see below). However, |
| 6513 | // this means that if the base class has a protected copy assignment |
| 6514 | // operator, the protected member access check will fail. So, we |
| 6515 | // rewrite "protected" access to "public" access in this case, since we |
| 6516 | // know by construction that we're calling from a derived class. |
| 6517 | if (CopyingBaseSubobject) { |
| 6518 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 6519 | L != LEnd; ++L) { |
| 6520 | if (L.getAccess() == AS_protected) |
| 6521 | L.setAccess(AS_public); |
| 6522 | } |
| 6523 | } |
| 6524 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6525 | // Create the nested-name-specifier that will be used to qualify the |
| 6526 | // reference to operator=; this is required to suppress the virtual |
| 6527 | // call mechanism. |
| 6528 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 6529 | SS.MakeTrivial(S.Context, |
| 6530 | NestedNameSpecifier::Create(S.Context, 0, false, |
| 6531 | T.getTypePtr()), |
| 6532 | Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6533 | |
| 6534 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6535 | ExprResult OpEqualRef |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6536 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6537 | /*FirstQualifierInScope=*/0, OpLookup, |
| 6538 | /*TemplateArgs=*/0, |
| 6539 | /*SuppressQualifierCheck=*/true); |
| 6540 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6541 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6542 | |
| 6543 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6544 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6545 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 6546 | OpEqualRef.takeAs<Expr>(), |
| 6547 | Loc, &From, 1, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6548 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6549 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6550 | |
| 6551 | return S.Owned(Call.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 6552 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 6553 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6554 | // - if the subobject is of scalar type, the built-in assignment |
| 6555 | // operator is used. |
| 6556 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| 6557 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6558 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6559 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6560 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6561 | |
| 6562 | return S.Owned(Assignment.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 6563 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6564 | |
| 6565 | // - if the subobject is an array, each element is assigned, in the |
| 6566 | // manner appropriate to the element type; |
| 6567 | |
| 6568 | // Construct a loop over the array bounds, e.g., |
| 6569 | // |
| 6570 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 6571 | // |
| 6572 | // that will copy each of the array elements. |
| 6573 | QualType SizeType = S.Context.getSizeType(); |
| 6574 | |
| 6575 | // Create the iteration variable. |
| 6576 | IdentifierInfo *IterationVarName = 0; |
| 6577 | { |
| 6578 | llvm::SmallString<8> Str; |
| 6579 | llvm::raw_svector_ostream OS(Str); |
| 6580 | OS << "__i" << Depth; |
| 6581 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 6582 | } |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6583 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6584 | IterationVarName, SizeType, |
| 6585 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6586 | SC_None, SC_None); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6587 | |
| 6588 | // Initialize the iteration variable to zero. |
| 6589 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 6590 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6591 | |
| 6592 | // Create a reference to the iteration variable; we'll use this several |
| 6593 | // times throughout. |
| 6594 | Expr *IterationVarRef |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6595 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_RValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6596 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
| 6597 | |
| 6598 | // Create the DeclStmt that holds the iteration variable. |
| 6599 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| 6600 | |
| 6601 | // Create the comparison against the array bound. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6602 | llvm::APInt Upper |
| 6603 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6604 | Expr *Comparison |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 6605 | = new (S.Context) BinaryOperator(IterationVarRef, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6606 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 6607 | BO_NE, S.Context.BoolTy, |
| 6608 | VK_RValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6609 | |
| 6610 | // Create the pre-increment of the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6611 | Expr *Increment |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6612 | = new (S.Context) UnaryOperator(IterationVarRef, UO_PreInc, SizeType, |
| 6613 | VK_LValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6614 | |
| 6615 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6616 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
| 6617 | IterationVarRef, Loc)); |
| 6618 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
| 6619 | IterationVarRef, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6620 | |
| 6621 | // Build the copy for an individual element of the array. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6622 | StmtResult Copy = BuildSingleCopyAssign(S, Loc, ArrayTy->getElementType(), |
| 6623 | To, From, CopyingBaseSubobject, |
| 6624 | Depth + 1); |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 6625 | if (Copy.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6626 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6627 | |
| 6628 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6629 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6630 | S.MakeFullExpr(Comparison), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6631 | 0, S.MakeFullExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6632 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 6633 | } |
| 6634 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 6635 | std::pair<Sema::ImplicitExceptionSpecification, bool> |
| 6636 | Sema::ComputeDefaultedCopyAssignmentExceptionSpecAndConst( |
| 6637 | CXXRecordDecl *ClassDecl) { |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 6638 | if (ClassDecl->isInvalidDecl()) |
| 6639 | return std::make_pair(ImplicitExceptionSpecification(Context), false); |
| 6640 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6641 | // C++ [class.copy]p10: |
| 6642 | // If the class definition does not explicitly declare a copy |
| 6643 | // assignment operator, one is declared implicitly. |
| 6644 | // The implicitly-defined copy assignment operator for a class X |
| 6645 | // will have the form |
| 6646 | // |
| 6647 | // X& X::operator=(const X&) |
| 6648 | // |
| 6649 | // if |
| 6650 | bool HasConstCopyAssignment = true; |
| 6651 | |
| 6652 | // -- each direct base class B of X has a copy assignment operator |
| 6653 | // whose parameter is of type const B&, const volatile B& or B, |
| 6654 | // and |
| 6655 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 6656 | BaseEnd = ClassDecl->bases_end(); |
| 6657 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6658 | // We'll handle this below |
| 6659 | if (LangOpts.CPlusPlus0x && Base->isVirtual()) |
| 6660 | continue; |
| 6661 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6662 | assert(!Base->getType()->isDependentType() && |
| 6663 | "Cannot generate implicit members for class with dependent bases."); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6664 | CXXRecordDecl *BaseClassDecl = Base->getType()->getAsCXXRecordDecl(); |
| 6665 | LookupCopyingAssignment(BaseClassDecl, Qualifiers::Const, false, 0, |
| 6666 | &HasConstCopyAssignment); |
| 6667 | } |
| 6668 | |
| 6669 | // In C++0x, the above citation has "or virtual added" |
| 6670 | if (LangOpts.CPlusPlus0x) { |
| 6671 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 6672 | BaseEnd = ClassDecl->vbases_end(); |
| 6673 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
| 6674 | assert(!Base->getType()->isDependentType() && |
| 6675 | "Cannot generate implicit members for class with dependent bases."); |
| 6676 | CXXRecordDecl *BaseClassDecl = Base->getType()->getAsCXXRecordDecl(); |
| 6677 | LookupCopyingAssignment(BaseClassDecl, Qualifiers::Const, false, 0, |
| 6678 | &HasConstCopyAssignment); |
| 6679 | } |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6680 | } |
| 6681 | |
| 6682 | // -- for all the nonstatic data members of X that are of a class |
| 6683 | // type M (or array thereof), each such class type has a copy |
| 6684 | // assignment operator whose parameter is of type const M&, |
| 6685 | // const volatile M& or M. |
| 6686 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 6687 | FieldEnd = ClassDecl->field_end(); |
| 6688 | HasConstCopyAssignment && Field != FieldEnd; |
| 6689 | ++Field) { |
| 6690 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6691 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 6692 | LookupCopyingAssignment(FieldClassDecl, Qualifiers::Const, false, 0, |
| 6693 | &HasConstCopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6694 | } |
| 6695 | } |
| 6696 | |
| 6697 | // Otherwise, the implicitly declared copy assignment operator will |
| 6698 | // have the form |
| 6699 | // |
| 6700 | // X& X::operator=(X&) |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6701 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6702 | // C++ [except.spec]p14: |
| 6703 | // An implicitly declared special member function (Clause 12) shall have an |
| 6704 | // exception-specification. [...] |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6705 | |
| 6706 | // It is unspecified whether or not an implicit copy assignment operator |
| 6707 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 6708 | // made. As such, this exception specification is effectively unspecified. |
| 6709 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 6710 | // the side of assuming such calls to be made regardless of whether they |
| 6711 | // actually happen. |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6712 | ImplicitExceptionSpecification ExceptSpec(Context); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6713 | unsigned ArgQuals = HasConstCopyAssignment ? Qualifiers::Const : 0; |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6714 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 6715 | BaseEnd = ClassDecl->bases_end(); |
| 6716 | Base != BaseEnd; ++Base) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6717 | if (Base->isVirtual()) |
| 6718 | continue; |
| 6719 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6720 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6721 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6722 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 6723 | ArgQuals, false, 0)) |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6724 | ExceptSpec.CalledDecl(CopyAssign); |
| 6725 | } |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6726 | |
| 6727 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 6728 | BaseEnd = ClassDecl->vbases_end(); |
| 6729 | Base != BaseEnd; ++Base) { |
| 6730 | CXXRecordDecl *BaseClassDecl |
| 6731 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 6732 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 6733 | ArgQuals, false, 0)) |
| 6734 | ExceptSpec.CalledDecl(CopyAssign); |
| 6735 | } |
| 6736 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6737 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 6738 | FieldEnd = ClassDecl->field_end(); |
| 6739 | Field != FieldEnd; |
| 6740 | ++Field) { |
| 6741 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6742 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 6743 | if (CXXMethodDecl *CopyAssign = |
| 6744 | LookupCopyingAssignment(FieldClassDecl, ArgQuals, false, 0)) |
| 6745 | ExceptSpec.CalledDecl(CopyAssign); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 6746 | } |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6747 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6748 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 6749 | return std::make_pair(ExceptSpec, HasConstCopyAssignment); |
| 6750 | } |
| 6751 | |
| 6752 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
| 6753 | // Note: The following rules are largely analoguous to the copy |
| 6754 | // constructor rules. Note that virtual bases are not taken into account |
| 6755 | // for determining the argument type of the operator. Note also that |
| 6756 | // operators taking an object instead of a reference are allowed. |
| 6757 | |
| 6758 | ImplicitExceptionSpecification Spec(Context); |
| 6759 | bool Const; |
| 6760 | llvm::tie(Spec, Const) = |
| 6761 | ComputeDefaultedCopyAssignmentExceptionSpecAndConst(ClassDecl); |
| 6762 | |
| 6763 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 6764 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 6765 | if (Const) |
| 6766 | ArgType = ArgType.withConst(); |
| 6767 | ArgType = Context.getLValueReferenceType(ArgType); |
| 6768 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6769 | // An implicitly-declared copy assignment operator is an inline public |
| 6770 | // member of its class. |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 6771 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6772 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6773 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 6774 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6775 | CXXMethodDecl *CopyAssignment |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6776 | = CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6777 | Context.getFunctionType(RetType, &ArgType, 1, EPI), |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6778 | /*TInfo=*/0, /*isStatic=*/false, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6779 | /*StorageClassAsWritten=*/SC_None, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 6780 | /*isInline=*/true, |
| 6781 | SourceLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6782 | CopyAssignment->setAccess(AS_public); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 6783 | CopyAssignment->setDefaulted(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6784 | CopyAssignment->setImplicit(); |
| 6785 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6786 | |
| 6787 | // Add the parameter to the operator. |
| 6788 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6789 | ClassLoc, ClassLoc, /*Id=*/0, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6790 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6791 | SC_None, |
| 6792 | SC_None, 0); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6793 | CopyAssignment->setParams(&FromParam, 1); |
| 6794 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6795 | // Note that we have added this copy-assignment operator. |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6796 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 6797 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 6798 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6799 | PushOnScopeChains(CopyAssignment, S, false); |
| 6800 | ClassDecl->addDecl(CopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6801 | |
Sean Hunt | 1ccbc54 | 2011-06-22 01:05:13 +0000 | [diff] [blame] | 6802 | // C++0x [class.copy]p18: |
| 6803 | // ... If the class definition declares a move constructor or move |
| 6804 | // assignment operator, the implicitly declared copy assignment operator is |
| 6805 | // defined as deleted; ... |
| 6806 | if (ClassDecl->hasUserDeclaredMoveConstructor() || |
| 6807 | ClassDecl->hasUserDeclaredMoveAssignment() || |
| 6808 | ShouldDeleteCopyAssignmentOperator(CopyAssignment)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 6809 | CopyAssignment->setDeletedAsWritten(); |
| 6810 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6811 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 6812 | return CopyAssignment; |
| 6813 | } |
| 6814 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6815 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 6816 | CXXMethodDecl *CopyAssignOperator) { |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 6817 | assert((CopyAssignOperator->isDefaulted() && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6818 | CopyAssignOperator->isOverloadedOperator() && |
| 6819 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 6820 | !CopyAssignOperator->doesThisDeclarationHaveABody()) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6821 | "DefineImplicitCopyAssignment called for wrong function"); |
| 6822 | |
| 6823 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 6824 | |
| 6825 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 6826 | CopyAssignOperator->setInvalidDecl(); |
| 6827 | return; |
| 6828 | } |
| 6829 | |
| 6830 | CopyAssignOperator->setUsed(); |
| 6831 | |
| 6832 | ImplicitlyDefinedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 6833 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6834 | |
| 6835 | // C++0x [class.copy]p30: |
| 6836 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 6837 | // for a non-union class X performs memberwise copy assignment of its |
| 6838 | // subobjects. The direct base classes of X are assigned first, in the |
| 6839 | // order of their declaration in the base-specifier-list, and then the |
| 6840 | // immediate non-static data members of X are assigned, in the order in |
| 6841 | // which they were declared in the class definition. |
| 6842 | |
| 6843 | // The statements that form the synthesized function body. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 6844 | ASTOwningVector<Stmt*> Statements(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6845 | |
| 6846 | // The parameter for the "other" object, which we are copying from. |
| 6847 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 6848 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 6849 | QualType OtherRefType = Other->getType(); |
| 6850 | if (const LValueReferenceType *OtherRef |
| 6851 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 6852 | OtherRefType = OtherRef->getPointeeType(); |
| 6853 | OtherQuals = OtherRefType.getQualifiers(); |
| 6854 | } |
| 6855 | |
| 6856 | // Our location for everything implicitly-generated. |
| 6857 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 6858 | |
| 6859 | // Construct a reference to the "other" object. We'll be using this |
| 6860 | // throughout the generated ASTs. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6861 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6862 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 6863 | |
| 6864 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 6865 | // ASTs. |
| 6866 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 6867 | assert(This && "Reference to this cannot fail!"); |
| 6868 | |
| 6869 | // Assign base classes. |
| 6870 | bool Invalid = false; |
| 6871 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 6872 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 6873 | // Form the assignment: |
| 6874 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 6875 | QualType BaseType = Base->getType().getUnqualifiedType(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 6876 | if (!BaseType->isRecordType()) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6877 | Invalid = true; |
| 6878 | continue; |
| 6879 | } |
| 6880 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 6881 | CXXCastPath BasePath; |
| 6882 | BasePath.push_back(Base); |
| 6883 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6884 | // Construct the "from" expression, which is an implicit cast to the |
| 6885 | // appropriately-qualified base type. |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 6886 | Expr *From = OtherRef; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6887 | From = ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
| 6888 | CK_UncheckedDerivedToBase, |
| 6889 | VK_LValue, &BasePath).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6890 | |
| 6891 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6892 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6893 | |
| 6894 | // Implicitly cast "this" to the appropriately-qualified base type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6895 | To = ImpCastExprToType(To.take(), |
| 6896 | Context.getCVRQualifiedType(BaseType, |
| 6897 | CopyAssignOperator->getTypeQualifiers()), |
| 6898 | CK_UncheckedDerivedToBase, |
| 6899 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6900 | |
| 6901 | // Build the copy. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6902 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6903 | To.get(), From, |
| 6904 | /*CopyingBaseSubobject=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6905 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 6906 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 6907 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 6908 | CopyAssignOperator->setInvalidDecl(); |
| 6909 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6910 | } |
| 6911 | |
| 6912 | // Success! Record the copy. |
| 6913 | Statements.push_back(Copy.takeAs<Expr>()); |
| 6914 | } |
| 6915 | |
| 6916 | // \brief Reference to the __builtin_memcpy function. |
| 6917 | Expr *BuiltinMemCpyRef = 0; |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 6918 | // \brief Reference to the __builtin_objc_memmove_collectable function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6919 | Expr *CollectableMemCpyRef = 0; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6920 | |
| 6921 | // Assign non-static members. |
| 6922 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 6923 | FieldEnd = ClassDecl->field_end(); |
| 6924 | Field != FieldEnd; ++Field) { |
| 6925 | // Check for members of reference type; we can't copy those. |
| 6926 | if (Field->getType()->isReferenceType()) { |
| 6927 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 6928 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 6929 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 6930 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 6931 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6932 | Invalid = true; |
| 6933 | continue; |
| 6934 | } |
| 6935 | |
| 6936 | // Check for members of const-qualified, non-class type. |
| 6937 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 6938 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 6939 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 6940 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 6941 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 6942 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 6943 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6944 | Invalid = true; |
| 6945 | continue; |
| 6946 | } |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 6947 | |
| 6948 | // Suppress assigning zero-width bitfields. |
| 6949 | if (const Expr *Width = Field->getBitWidth()) |
| 6950 | if (Width->EvaluateAsInt(Context) == 0) |
| 6951 | continue; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6952 | |
| 6953 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 6954 | if (FieldType->isIncompleteArrayType()) { |
| 6955 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 6956 | "Incomplete array type is not valid"); |
| 6957 | continue; |
| 6958 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6959 | |
| 6960 | // Build references to the field in the object we're copying from and to. |
| 6961 | CXXScopeSpec SS; // Intentionally empty |
| 6962 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 6963 | LookupMemberName); |
| 6964 | MemberLookup.addDecl(*Field); |
| 6965 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6966 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6967 | Loc, /*IsArrow=*/false, |
| 6968 | SS, 0, MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6969 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6970 | Loc, /*IsArrow=*/true, |
| 6971 | SS, 0, MemberLookup, 0); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6972 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 6973 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 6974 | |
| 6975 | // If the field should be copied with __builtin_memcpy rather than via |
| 6976 | // explicit assignments, do so. This optimization only applies for arrays |
| 6977 | // of scalars and arrays of class type with trivial copy-assignment |
| 6978 | // operators. |
Fariborz Jahanian | 6b167f4 | 2011-08-09 00:26:11 +0000 | [diff] [blame] | 6979 | if (FieldType->isArrayType() && !FieldType.isVolatileQualified() |
| 6980 | && BaseType.hasTrivialCopyAssignment(Context)) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6981 | // Compute the size of the memory buffer to be copied. |
| 6982 | QualType SizeType = Context.getSizeType(); |
| 6983 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 6984 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 6985 | for (const ConstantArrayType *Array |
| 6986 | = Context.getAsConstantArrayType(FieldType); |
| 6987 | Array; |
| 6988 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6989 | llvm::APInt ArraySize |
| 6990 | = Array->getSize().zextOrTrunc(Size.getBitWidth()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6991 | Size *= ArraySize; |
| 6992 | } |
| 6993 | |
| 6994 | // Take the address of the field references for "from" and "to". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6995 | From = CreateBuiltinUnaryOp(Loc, UO_AddrOf, From.get()); |
| 6996 | To = CreateBuiltinUnaryOp(Loc, UO_AddrOf, To.get()); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6997 | |
| 6998 | bool NeedsCollectableMemCpy = |
| 6999 | (BaseType->isRecordType() && |
| 7000 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()); |
| 7001 | |
| 7002 | if (NeedsCollectableMemCpy) { |
| 7003 | if (!CollectableMemCpyRef) { |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 7004 | // Create a reference to the __builtin_objc_memmove_collectable function. |
| 7005 | LookupResult R(*this, |
| 7006 | &Context.Idents.get("__builtin_objc_memmove_collectable"), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7007 | Loc, LookupOrdinaryName); |
| 7008 | LookupName(R, TUScope, true); |
| 7009 | |
| 7010 | FunctionDecl *CollectableMemCpy = R.getAsSingle<FunctionDecl>(); |
| 7011 | if (!CollectableMemCpy) { |
| 7012 | // Something went horribly wrong earlier, and we will have |
| 7013 | // complained about it. |
| 7014 | Invalid = true; |
| 7015 | continue; |
| 7016 | } |
| 7017 | |
| 7018 | CollectableMemCpyRef = BuildDeclRefExpr(CollectableMemCpy, |
| 7019 | CollectableMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7020 | VK_LValue, Loc, 0).take(); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7021 | assert(CollectableMemCpyRef && "Builtin reference cannot fail"); |
| 7022 | } |
| 7023 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7024 | // Create a reference to the __builtin_memcpy builtin function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 7025 | else if (!BuiltinMemCpyRef) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7026 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 7027 | LookupOrdinaryName); |
| 7028 | LookupName(R, TUScope, true); |
| 7029 | |
| 7030 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 7031 | if (!BuiltinMemCpy) { |
| 7032 | // Something went horribly wrong earlier, and we will have complained |
| 7033 | // about it. |
| 7034 | Invalid = true; |
| 7035 | continue; |
| 7036 | } |
| 7037 | |
| 7038 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 7039 | BuiltinMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7040 | VK_LValue, Loc, 0).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7041 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 7042 | } |
| 7043 | |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 7044 | ASTOwningVector<Expr*> CallArgs(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7045 | CallArgs.push_back(To.takeAs<Expr>()); |
| 7046 | CallArgs.push_back(From.takeAs<Expr>()); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 7047 | CallArgs.push_back(IntegerLiteral::Create(Context, Size, SizeType, Loc)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7048 | ExprResult Call = ExprError(); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7049 | if (NeedsCollectableMemCpy) |
| 7050 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7051 | CollectableMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7052 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7053 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7054 | else |
| 7055 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7056 | BuiltinMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7057 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7058 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7059 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7060 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 7061 | Statements.push_back(Call.takeAs<Expr>()); |
| 7062 | continue; |
| 7063 | } |
| 7064 | |
| 7065 | // Build the copy of this field. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7066 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, FieldType, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7067 | To.get(), From.get(), |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 7068 | /*CopyingBaseSubobject=*/false); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7069 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7070 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7071 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 7072 | CopyAssignOperator->setInvalidDecl(); |
| 7073 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7074 | } |
| 7075 | |
| 7076 | // Success! Record the copy. |
| 7077 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 7078 | } |
| 7079 | |
| 7080 | if (!Invalid) { |
| 7081 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7082 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7083 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7084 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7085 | if (Return.isInvalid()) |
| 7086 | Invalid = true; |
| 7087 | else { |
| 7088 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7089 | |
| 7090 | if (Trap.hasErrorOccurred()) { |
| 7091 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7092 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 7093 | Invalid = true; |
| 7094 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7095 | } |
| 7096 | } |
| 7097 | |
| 7098 | if (Invalid) { |
| 7099 | CopyAssignOperator->setInvalidDecl(); |
| 7100 | return; |
| 7101 | } |
| 7102 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7103 | StmtResult Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7104 | /*isStmtExpr=*/false); |
| 7105 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 7106 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7107 | |
| 7108 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7109 | L->CompletedImplicitDefinition(CopyAssignOperator); |
| 7110 | } |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7113 | std::pair<Sema::ImplicitExceptionSpecification, bool> |
| 7114 | Sema::ComputeDefaultedCopyCtorExceptionSpecAndConst(CXXRecordDecl *ClassDecl) { |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 7115 | if (ClassDecl->isInvalidDecl()) |
| 7116 | return std::make_pair(ImplicitExceptionSpecification(Context), false); |
| 7117 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7118 | // C++ [class.copy]p5: |
| 7119 | // The implicitly-declared copy constructor for a class X will |
| 7120 | // have the form |
| 7121 | // |
| 7122 | // X::X(const X&) |
| 7123 | // |
| 7124 | // if |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 7125 | // FIXME: It ought to be possible to store this on the record. |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7126 | bool HasConstCopyConstructor = true; |
| 7127 | |
| 7128 | // -- each direct or virtual base class B of X has a copy |
| 7129 | // constructor whose first parameter is of type const B& or |
| 7130 | // const volatile B&, and |
| 7131 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7132 | BaseEnd = ClassDecl->bases_end(); |
| 7133 | HasConstCopyConstructor && Base != BaseEnd; |
| 7134 | ++Base) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 7135 | // Virtual bases are handled below. |
| 7136 | if (Base->isVirtual()) |
| 7137 | continue; |
| 7138 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7139 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 7140 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7141 | LookupCopyingConstructor(BaseClassDecl, Qualifiers::Const, |
| 7142 | &HasConstCopyConstructor); |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 7143 | } |
| 7144 | |
| 7145 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 7146 | BaseEnd = ClassDecl->vbases_end(); |
| 7147 | HasConstCopyConstructor && Base != BaseEnd; |
| 7148 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7149 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7150 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7151 | LookupCopyingConstructor(BaseClassDecl, Qualifiers::Const, |
| 7152 | &HasConstCopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7153 | } |
| 7154 | |
| 7155 | // -- for all the nonstatic data members of X that are of a |
| 7156 | // class type M (or array thereof), each such class type |
| 7157 | // has a copy constructor whose first parameter is of type |
| 7158 | // const M& or const volatile M&. |
| 7159 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7160 | FieldEnd = ClassDecl->field_end(); |
| 7161 | HasConstCopyConstructor && Field != FieldEnd; |
| 7162 | ++Field) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 7163 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 7164 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7165 | LookupCopyingConstructor(FieldClassDecl, Qualifiers::Const, |
| 7166 | &HasConstCopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7167 | } |
| 7168 | } |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7169 | // Otherwise, the implicitly declared copy constructor will have |
| 7170 | // the form |
| 7171 | // |
| 7172 | // X::X(X&) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7173 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 7174 | // C++ [except.spec]p14: |
| 7175 | // An implicitly declared special member function (Clause 12) shall have an |
| 7176 | // exception-specification. [...] |
| 7177 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 7178 | unsigned Quals = HasConstCopyConstructor? Qualifiers::Const : 0; |
| 7179 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7180 | BaseEnd = ClassDecl->bases_end(); |
| 7181 | Base != BaseEnd; |
| 7182 | ++Base) { |
| 7183 | // Virtual bases are handled below. |
| 7184 | if (Base->isVirtual()) |
| 7185 | continue; |
| 7186 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7187 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 7188 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 7189 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7190 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 7191 | ExceptSpec.CalledDecl(CopyConstructor); |
| 7192 | } |
| 7193 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 7194 | BaseEnd = ClassDecl->vbases_end(); |
| 7195 | Base != BaseEnd; |
| 7196 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7197 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 7198 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 7199 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7200 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 7201 | ExceptSpec.CalledDecl(CopyConstructor); |
| 7202 | } |
| 7203 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7204 | FieldEnd = ClassDecl->field_end(); |
| 7205 | Field != FieldEnd; |
| 7206 | ++Field) { |
| 7207 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 7208 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 7209 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7210 | LookupCopyingConstructor(FieldClassDecl, Quals)) |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 7211 | ExceptSpec.CalledDecl(CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 7212 | } |
| 7213 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7214 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7215 | return std::make_pair(ExceptSpec, HasConstCopyConstructor); |
| 7216 | } |
| 7217 | |
| 7218 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 7219 | CXXRecordDecl *ClassDecl) { |
| 7220 | // C++ [class.copy]p4: |
| 7221 | // If the class definition does not explicitly declare a copy |
| 7222 | // constructor, one is declared implicitly. |
| 7223 | |
| 7224 | ImplicitExceptionSpecification Spec(Context); |
| 7225 | bool Const; |
| 7226 | llvm::tie(Spec, Const) = |
| 7227 | ComputeDefaultedCopyCtorExceptionSpecAndConst(ClassDecl); |
| 7228 | |
| 7229 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 7230 | QualType ArgType = ClassType; |
| 7231 | if (Const) |
| 7232 | ArgType = ArgType.withConst(); |
| 7233 | ArgType = Context.getLValueReferenceType(ArgType); |
| 7234 | |
| 7235 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 7236 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7237 | DeclarationName Name |
| 7238 | = Context.DeclarationNames.getCXXConstructorName( |
| 7239 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7240 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 7241 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7242 | |
| 7243 | // An implicitly-declared copy constructor is an inline public |
| 7244 | // member of its class. |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7245 | CXXConstructorDecl *CopyConstructor |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7246 | = CXXConstructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7247 | Context.getFunctionType(Context.VoidTy, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7248 | &ArgType, 1, EPI), |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7249 | /*TInfo=*/0, |
| 7250 | /*isExplicit=*/false, |
| 7251 | /*isInline=*/true, |
Sean Hunt | 5f802e5 | 2011-05-06 00:11:07 +0000 | [diff] [blame] | 7252 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7253 | CopyConstructor->setAccess(AS_public); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7254 | CopyConstructor->setDefaulted(); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7255 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
| 7256 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7257 | // Note that we have declared this constructor. |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7258 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 7259 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7260 | // Add the parameter to the constructor. |
| 7261 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7262 | ClassLoc, ClassLoc, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7263 | /*IdentifierInfo=*/0, |
| 7264 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7265 | SC_None, |
| 7266 | SC_None, 0); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7267 | CopyConstructor->setParams(&FromParam, 1); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7268 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 7269 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7270 | PushOnScopeChains(CopyConstructor, S, false); |
| 7271 | ClassDecl->addDecl(CopyConstructor); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 7272 | |
Sean Hunt | 1ccbc54 | 2011-06-22 01:05:13 +0000 | [diff] [blame] | 7273 | // C++0x [class.copy]p7: |
| 7274 | // ... If the class definition declares a move constructor or move |
| 7275 | // assignment operator, the implicitly declared constructor is defined as |
| 7276 | // deleted; ... |
| 7277 | if (ClassDecl->hasUserDeclaredMoveConstructor() || |
| 7278 | ClassDecl->hasUserDeclaredMoveAssignment() || |
| 7279 | ShouldDeleteCopyConstructor(CopyConstructor)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 7280 | CopyConstructor->setDeletedAsWritten(); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7281 | |
| 7282 | return CopyConstructor; |
| 7283 | } |
| 7284 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7285 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7286 | CXXConstructorDecl *CopyConstructor) { |
| 7287 | assert((CopyConstructor->isDefaulted() && |
| 7288 | CopyConstructor->isCopyConstructor() && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 7289 | !CopyConstructor->doesThisDeclarationHaveABody()) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7290 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7291 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 7292 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7293 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7294 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 7295 | ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 7296 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7297 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 7298 | if (SetCtorInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7299 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 7300 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 7301 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 7302 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 7303 | } else { |
| 7304 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 7305 | CopyConstructor->getLocation(), |
| 7306 | MultiStmtArg(*this, 0, 0), |
| 7307 | /*isStmtExpr=*/false) |
| 7308 | .takeAs<Stmt>()); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 7309 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 7310 | |
| 7311 | CopyConstructor->setUsed(); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7312 | |
| 7313 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7314 | L->CompletedImplicitDefinition(CopyConstructor); |
| 7315 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7316 | } |
| 7317 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7318 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 7319 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7320 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7321 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7322 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7323 | unsigned ConstructKind, |
| 7324 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 7325 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7326 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 7327 | // C++0x [class.copy]p34: |
| 7328 | // When certain criteria are met, an implementation is allowed to |
| 7329 | // omit the copy/move construction of a class object, even if the |
| 7330 | // copy/move constructor and/or destructor for the object have |
| 7331 | // side effects. [...] |
| 7332 | // - when a temporary class object that has not been bound to a |
| 7333 | // reference (12.2) would be copied/moved to a class object |
| 7334 | // with the same cv-unqualified type, the copy/move operation |
| 7335 | // can be omitted by constructing the temporary object |
| 7336 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 7337 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
Douglas Gregor | 70a21de | 2011-01-27 23:24:55 +0000 | [diff] [blame] | 7338 | Constructor->isCopyOrMoveConstructor() && ExprArgs.size() >= 1) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 7339 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 7340 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 7341 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7342 | |
| 7343 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7344 | Elidable, move(ExprArgs), RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7345 | ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 7346 | } |
| 7347 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 7348 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 7349 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7350 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 7351 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 7352 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7353 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7354 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7355 | unsigned ConstructKind, |
| 7356 | SourceRange ParenRange) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 7357 | unsigned NumExprs = ExprArgs.size(); |
| 7358 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7359 | |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 7360 | for (specific_attr_iterator<NonNullAttr> |
| 7361 | i = Constructor->specific_attr_begin<NonNullAttr>(), |
| 7362 | e = Constructor->specific_attr_end<NonNullAttr>(); i != e; ++i) { |
| 7363 | const NonNullAttr *NonNull = *i; |
| 7364 | CheckNonNullArguments(NonNull, ExprArgs.get(), ConstructLoc); |
| 7365 | } |
| 7366 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 7367 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 7368 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7369 | Constructor, Elidable, Exprs, NumExprs, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 7370 | RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7371 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 7372 | ParenRange)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 7373 | } |
| 7374 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7375 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 7376 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 7377 | MultiExprArg Exprs) { |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7378 | // FIXME: Provide the correct paren SourceRange when available. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7379 | ExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 7380 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7381 | move(Exprs), false, CXXConstructExpr::CK_Complete, |
| 7382 | SourceRange()); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 7383 | if (TempResult.isInvalid()) |
| 7384 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7385 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 7386 | Expr *Temp = TempResult.takeAs<Expr>(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7387 | CheckImplicitConversions(Temp, VD->getLocation()); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7388 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 7389 | Temp = MaybeCreateExprWithCleanups(Temp); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 7390 | VD->setInit(Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7391 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 7392 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 7393 | } |
| 7394 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 7395 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 7396 | if (VD->isInvalidDecl()) return; |
| 7397 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 7398 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 7399 | if (ClassDecl->isInvalidDecl()) return; |
| 7400 | if (ClassDecl->hasTrivialDestructor()) return; |
| 7401 | if (ClassDecl->isDependentContext()) return; |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 7402 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 7403 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
| 7404 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
| 7405 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 7406 | PDiag(diag::err_access_dtor_var) |
| 7407 | << VD->getDeclName() |
| 7408 | << VD->getType()); |
Anders Carlsson | 2b32dad | 2011-03-24 01:01:41 +0000 | [diff] [blame] | 7409 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 7410 | if (!VD->hasGlobalStorage()) return; |
| 7411 | |
| 7412 | // Emit warning for non-trivial dtor in global scope (a real global, |
| 7413 | // class-static, function-static). |
| 7414 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 7415 | |
| 7416 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 7417 | if (!VD->isStaticLocal()) |
| 7418 | Diag(VD->getLocation(), diag::warn_global_destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7419 | } |
| 7420 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7421 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7422 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 7423 | /// e.g: "int x(1);" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7424 | void Sema::AddCXXDirectInitializerToDecl(Decl *RealDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 7425 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 7426 | MultiExprArg Exprs, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7427 | SourceLocation RParenLoc, |
| 7428 | bool TypeMayContainAuto) { |
Daniel Dunbar | 5184626 | 2009-12-24 19:19:26 +0000 | [diff] [blame] | 7429 | assert(Exprs.size() != 0 && Exprs.get() && "missing expressions"); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7430 | |
| 7431 | // If there is no declaration, there was an error parsing it. Just ignore |
| 7432 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 7433 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7434 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7435 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7436 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 7437 | if (!VDecl) { |
| 7438 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 7439 | RealDecl->setInvalidDecl(); |
| 7440 | return; |
| 7441 | } |
| 7442 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7443 | // C++0x [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 7444 | if (TypeMayContainAuto && VDecl->getType()->getContainedAutoType()) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7445 | // FIXME: n3225 doesn't actually seem to indicate this is ill-formed |
| 7446 | if (Exprs.size() > 1) { |
| 7447 | Diag(Exprs.get()[1]->getSourceRange().getBegin(), |
| 7448 | diag::err_auto_var_init_multiple_expressions) |
| 7449 | << VDecl->getDeclName() << VDecl->getType() |
| 7450 | << VDecl->getSourceRange(); |
| 7451 | RealDecl->setInvalidDecl(); |
| 7452 | return; |
| 7453 | } |
| 7454 | |
| 7455 | Expr *Init = Exprs.get()[0]; |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 7456 | TypeSourceInfo *DeducedType = 0; |
| 7457 | if (!DeduceAutoType(VDecl->getTypeSourceInfo(), Init, DeducedType)) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7458 | Diag(VDecl->getLocation(), diag::err_auto_var_deduction_failure) |
| 7459 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 7460 | << Init->getSourceRange(); |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 7461 | if (!DeducedType) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7462 | RealDecl->setInvalidDecl(); |
| 7463 | return; |
| 7464 | } |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 7465 | VDecl->setTypeSourceInfo(DeducedType); |
| 7466 | VDecl->setType(DeducedType->getType()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7467 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7468 | // In ARC, infer lifetime. |
| 7469 | if (getLangOptions().ObjCAutoRefCount && inferObjCARCLifetime(VDecl)) |
| 7470 | VDecl->setInvalidDecl(); |
| 7471 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7472 | // If this is a redeclaration, check that the type we just deduced matches |
| 7473 | // the previously declared type. |
| 7474 | if (VarDecl *Old = VDecl->getPreviousDeclaration()) |
| 7475 | MergeVarDeclTypes(VDecl, Old); |
| 7476 | } |
| 7477 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 7478 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 7479 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7480 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 7481 | // |
| 7482 | // Clients that want to distinguish between the two forms, can check for |
| 7483 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 7484 | // A major benefit is that clients that don't particularly care about which |
| 7485 | // exactly form was it (like the CodeGen) can handle both cases without |
| 7486 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 7487 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7488 | // C++ 8.5p11: |
| 7489 | // The form of initialization (using parentheses or '=') is generally |
| 7490 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 7491 | // class type. |
| 7492 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 7493 | if (!VDecl->getType()->isDependentType() && |
| 7494 | RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 7495 | diag::err_typecheck_decl_incomplete_type)) { |
| 7496 | VDecl->setInvalidDecl(); |
| 7497 | return; |
| 7498 | } |
| 7499 | |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 7500 | // The variable can not have an abstract class type. |
| 7501 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 7502 | diag::err_abstract_type_in_decl, |
| 7503 | AbstractVariableType)) |
| 7504 | VDecl->setInvalidDecl(); |
| 7505 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 7506 | const VarDecl *Def; |
| 7507 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 7508 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| 7509 | << VDecl->getDeclName(); |
| 7510 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 7511 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 7512 | return; |
| 7513 | } |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 7514 | |
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 7515 | // C++ [class.static.data]p4 |
| 7516 | // If a static data member is of const integral or const |
| 7517 | // enumeration type, its declaration in the class definition can |
| 7518 | // specify a constant-initializer which shall be an integral |
| 7519 | // constant expression (5.19). In that case, the member can appear |
| 7520 | // in integral constant expressions. The member shall still be |
| 7521 | // defined in a namespace scope if it is used in the program and the |
| 7522 | // namespace scope definition shall not contain an initializer. |
| 7523 | // |
| 7524 | // We already performed a redefinition check above, but for static |
| 7525 | // data members we also need to check whether there was an in-class |
| 7526 | // declaration with an initializer. |
| 7527 | const VarDecl* PrevInit = 0; |
| 7528 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { |
| 7529 | Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName(); |
| 7530 | Diag(PrevInit->getLocation(), diag::note_previous_definition); |
| 7531 | return; |
| 7532 | } |
| 7533 | |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 7534 | bool IsDependent = false; |
| 7535 | for (unsigned I = 0, N = Exprs.size(); I != N; ++I) { |
| 7536 | if (DiagnoseUnexpandedParameterPack(Exprs.get()[I], UPPC_Expression)) { |
| 7537 | VDecl->setInvalidDecl(); |
| 7538 | return; |
| 7539 | } |
| 7540 | |
| 7541 | if (Exprs.get()[I]->isTypeDependent()) |
| 7542 | IsDependent = true; |
| 7543 | } |
| 7544 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 7545 | // If either the declaration has a dependent type or if any of the |
| 7546 | // expressions is type-dependent, we represent the initialization |
| 7547 | // via a ParenListExpr for later use during template instantiation. |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 7548 | if (VDecl->getType()->isDependentType() || IsDependent) { |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 7549 | // Let clients know that initialization was done with a direct initializer. |
| 7550 | VDecl->setCXXDirectInitializer(true); |
| 7551 | |
| 7552 | // Store the initialization expressions as a ParenListExpr. |
| 7553 | unsigned NumExprs = Exprs.size(); |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 7554 | VDecl->setInit(new (Context) ParenListExpr( |
| 7555 | Context, LParenLoc, (Expr **)Exprs.release(), NumExprs, RParenLoc, |
| 7556 | VDecl->getType().getNonReferenceType())); |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 7557 | return; |
| 7558 | } |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 7559 | |
| 7560 | // Capture the variable that is being initialized and the style of |
| 7561 | // initialization. |
| 7562 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 7563 | |
| 7564 | // FIXME: Poor source location information. |
| 7565 | InitializationKind Kind |
| 7566 | = InitializationKind::CreateDirect(VDecl->getLocation(), |
| 7567 | LParenLoc, RParenLoc); |
| 7568 | |
| 7569 | InitializationSequence InitSeq(*this, Entity, Kind, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7570 | Exprs.get(), Exprs.size()); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7571 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(Exprs)); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 7572 | if (Result.isInvalid()) { |
| 7573 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7574 | return; |
| 7575 | } |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7576 | |
| 7577 | CheckImplicitConversions(Result.get(), LParenLoc); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 7578 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 7579 | Result = MaybeCreateExprWithCleanups(Result); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 7580 | VDecl->setInit(Result.takeAs<Expr>()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7581 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 7582 | |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 7583 | CheckCompleteVariableDeclaration(VDecl); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7584 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 7585 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 7586 | /// \brief Given a constructor and the set of arguments provided for the |
| 7587 | /// constructor, convert the arguments and add any required default arguments |
| 7588 | /// to form a proper call to this constructor. |
| 7589 | /// |
| 7590 | /// \returns true if an error occurred, false otherwise. |
| 7591 | bool |
| 7592 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 7593 | MultiExprArg ArgsPtr, |
| 7594 | SourceLocation Loc, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 7595 | ASTOwningVector<Expr*> &ConvertedArgs) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 7596 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 7597 | unsigned NumArgs = ArgsPtr.size(); |
| 7598 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 7599 | |
| 7600 | const FunctionProtoType *Proto |
| 7601 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 7602 | assert(Proto && "Constructor without a prototype?"); |
| 7603 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 7604 | |
| 7605 | // 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] | 7606 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 7607 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 7608 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 7609 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 7610 | |
| 7611 | VariadicCallType CallType = |
| 7612 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7613 | SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 7614 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 7615 | Proto, 0, Args, NumArgs, AllArgs, |
| 7616 | CallType); |
| 7617 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 7618 | ConvertedArgs.push_back(AllArgs[i]); |
| 7619 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 7620 | } |
| 7621 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 7622 | static inline bool |
| 7623 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 7624 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7625 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 7626 | if (isa<NamespaceDecl>(DC)) { |
| 7627 | return SemaRef.Diag(FnDecl->getLocation(), |
| 7628 | diag::err_operator_new_delete_declared_in_namespace) |
| 7629 | << FnDecl->getDeclName(); |
| 7630 | } |
| 7631 | |
| 7632 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7633 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 7634 | return SemaRef.Diag(FnDecl->getLocation(), |
| 7635 | diag::err_operator_new_delete_declared_static) |
| 7636 | << FnDecl->getDeclName(); |
| 7637 | } |
| 7638 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 7639 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 7640 | } |
| 7641 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7642 | static inline bool |
| 7643 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 7644 | CanQualType ExpectedResultType, |
| 7645 | CanQualType ExpectedFirstParamType, |
| 7646 | unsigned DependentParamTypeDiag, |
| 7647 | unsigned InvalidParamTypeDiag) { |
| 7648 | QualType ResultType = |
| 7649 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 7650 | |
| 7651 | // Check that the result type is not dependent. |
| 7652 | if (ResultType->isDependentType()) |
| 7653 | return SemaRef.Diag(FnDecl->getLocation(), |
| 7654 | diag::err_operator_new_delete_dependent_result_type) |
| 7655 | << FnDecl->getDeclName() << ExpectedResultType; |
| 7656 | |
| 7657 | // Check that the result type is what we expect. |
| 7658 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 7659 | return SemaRef.Diag(FnDecl->getLocation(), |
| 7660 | diag::err_operator_new_delete_invalid_result_type) |
| 7661 | << FnDecl->getDeclName() << ExpectedResultType; |
| 7662 | |
| 7663 | // A function template must have at least 2 parameters. |
| 7664 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 7665 | return SemaRef.Diag(FnDecl->getLocation(), |
| 7666 | diag::err_operator_new_delete_template_too_few_parameters) |
| 7667 | << FnDecl->getDeclName(); |
| 7668 | |
| 7669 | // The function decl must have at least 1 parameter. |
| 7670 | if (FnDecl->getNumParams() == 0) |
| 7671 | return SemaRef.Diag(FnDecl->getLocation(), |
| 7672 | diag::err_operator_new_delete_too_few_parameters) |
| 7673 | << FnDecl->getDeclName(); |
| 7674 | |
| 7675 | // Check the the first parameter type is not dependent. |
| 7676 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 7677 | if (FirstParamType->isDependentType()) |
| 7678 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 7679 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 7680 | |
| 7681 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 7682 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7683 | ExpectedFirstParamType) |
| 7684 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 7685 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 7686 | |
| 7687 | return false; |
| 7688 | } |
| 7689 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 7690 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7691 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 7692 | // C++ [basic.stc.dynamic.allocation]p1: |
| 7693 | // A program is ill-formed if an allocation function is declared in a |
| 7694 | // namespace scope other than global scope or declared static in global |
| 7695 | // scope. |
| 7696 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 7697 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7698 | |
| 7699 | CanQualType SizeTy = |
| 7700 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 7701 | |
| 7702 | // C++ [basic.stc.dynamic.allocation]p1: |
| 7703 | // The return type shall be void*. The first parameter shall have type |
| 7704 | // std::size_t. |
| 7705 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 7706 | SizeTy, |
| 7707 | diag::err_operator_new_dependent_param_type, |
| 7708 | diag::err_operator_new_param_type)) |
| 7709 | return true; |
| 7710 | |
| 7711 | // C++ [basic.stc.dynamic.allocation]p1: |
| 7712 | // The first parameter shall not have an associated default argument. |
| 7713 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 7714 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7715 | diag::err_operator_new_default_arg) |
| 7716 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 7717 | |
| 7718 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 7719 | } |
| 7720 | |
| 7721 | static bool |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 7722 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 7723 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 7724 | // A program is ill-formed if deallocation functions are declared in a |
| 7725 | // namespace scope other than global scope or declared static in global |
| 7726 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 7727 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 7728 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 7729 | |
| 7730 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 7731 | // Each deallocation function shall return void and its first parameter |
| 7732 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7733 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 7734 | SemaRef.Context.VoidPtrTy, |
| 7735 | diag::err_operator_delete_dependent_param_type, |
| 7736 | diag::err_operator_delete_param_type)) |
| 7737 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 7738 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 7739 | return false; |
| 7740 | } |
| 7741 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7742 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 7743 | /// of this overloaded operator is well-formed. If so, returns false; |
| 7744 | /// otherwise, emits appropriate diagnostics and returns true. |
| 7745 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7746 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7747 | "Expected an overloaded operator declaration"); |
| 7748 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7749 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 7750 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7751 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7752 | // The allocation and deallocation functions, operator new, |
| 7753 | // operator new[], operator delete and operator delete[], are |
| 7754 | // described completely in 3.7.3. The attributes and restrictions |
| 7755 | // found in the rest of this subclause do not apply to them unless |
| 7756 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 7757 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 7758 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 7759 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 7760 | if (Op == OO_New || Op == OO_Array_New) |
| 7761 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7762 | |
| 7763 | // C++ [over.oper]p6: |
| 7764 | // An operator function shall either be a non-static member |
| 7765 | // function or be a non-member function and have at least one |
| 7766 | // parameter whose type is a class, a reference to a class, an |
| 7767 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7768 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 7769 | if (MethodDecl->isStatic()) |
| 7770 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 7771 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7772 | } else { |
| 7773 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7774 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 7775 | ParamEnd = FnDecl->param_end(); |
| 7776 | Param != ParamEnd; ++Param) { |
| 7777 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 7778 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 7779 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7780 | ClassOrEnumParam = true; |
| 7781 | break; |
| 7782 | } |
| 7783 | } |
| 7784 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7785 | if (!ClassOrEnumParam) |
| 7786 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 7787 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 7788 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7789 | } |
| 7790 | |
| 7791 | // C++ [over.oper]p8: |
| 7792 | // An operator function cannot have default arguments (8.3.6), |
| 7793 | // except where explicitly stated below. |
| 7794 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7795 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7796 | // (C++ [over.call]p1). |
| 7797 | if (Op != OO_Call) { |
| 7798 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 7799 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7800 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7801 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 7802 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7803 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7804 | } |
| 7805 | } |
| 7806 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 7807 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 7808 | { false, false, false } |
| 7809 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 7810 | , { Unary, Binary, MemberOnly } |
| 7811 | #include "clang/Basic/OperatorKinds.def" |
| 7812 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7813 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 7814 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 7815 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 7816 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7817 | |
| 7818 | // C++ [over.oper]p8: |
| 7819 | // [...] Operator functions cannot have more or fewer parameters |
| 7820 | // than the number required for the corresponding operator, as |
| 7821 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7822 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7823 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7824 | if (Op != OO_Call && |
| 7825 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 7826 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 7827 | (NumParams < 1) || (NumParams > 2))) { |
| 7828 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 7829 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 7830 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 7831 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 7832 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 7833 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 7834 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 7835 | assert(CanBeBinaryOperator && |
| 7836 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 7837 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 7838 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7839 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 7840 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 7841 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7842 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 7843 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7844 | // Overloaded operators other than operator() cannot be variadic. |
| 7845 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7846 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 7847 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 7848 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7849 | } |
| 7850 | |
| 7851 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7852 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 7853 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 7854 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 7855 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7856 | } |
| 7857 | |
| 7858 | // C++ [over.inc]p1: |
| 7859 | // The user-defined function called operator++ implements the |
| 7860 | // prefix and postfix ++ operator. If this function is a member |
| 7861 | // function with no parameters, or a non-member function with one |
| 7862 | // parameter of class or enumeration type, it defines the prefix |
| 7863 | // increment operator ++ for objects of that type. If the function |
| 7864 | // is a member function with one parameter (which shall be of type |
| 7865 | // int) or a non-member function with two parameters (the second |
| 7866 | // of which shall be of type int), it defines the postfix |
| 7867 | // increment operator ++ for objects of that type. |
| 7868 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 7869 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 7870 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7871 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7872 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 7873 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 7874 | if (!ParamIsInt) |
| 7875 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7876 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7877 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7878 | } |
| 7879 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7880 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7881 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 7882 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7883 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 7884 | /// of this literal operator function is well-formed. If so, returns |
| 7885 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 7886 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 7887 | DeclContext *DC = FnDecl->getDeclContext(); |
| 7888 | Decl::Kind Kind = DC->getDeclKind(); |
| 7889 | if (Kind != Decl::TranslationUnit && Kind != Decl::Namespace && |
| 7890 | Kind != Decl::LinkageSpec) { |
| 7891 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 7892 | << FnDecl->getDeclName(); |
| 7893 | return true; |
| 7894 | } |
| 7895 | |
| 7896 | bool Valid = false; |
| 7897 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 7898 | // template <char...> type operator "" name() is the only valid template |
| 7899 | // signature, and the only valid signature with no parameters. |
| 7900 | if (FnDecl->param_size() == 0) { |
| 7901 | if (FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate()) { |
| 7902 | // Must have only one template parameter |
| 7903 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 7904 | if (Params->size() == 1) { |
| 7905 | NonTypeTemplateParmDecl *PmDecl = |
| 7906 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7907 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 7908 | // The template parameter must be a char parameter pack. |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 7909 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 7910 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 7911 | Valid = true; |
| 7912 | } |
| 7913 | } |
| 7914 | } else { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7915 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 7916 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 7917 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7918 | QualType T = (*Param)->getType(); |
| 7919 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 7920 | // unsigned long long int, long double, and any character type are allowed |
| 7921 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7922 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 7923 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 7924 | Context.hasSameType(T, Context.CharTy) || |
| 7925 | Context.hasSameType(T, Context.WCharTy) || |
| 7926 | Context.hasSameType(T, Context.Char16Ty) || |
| 7927 | Context.hasSameType(T, Context.Char32Ty)) { |
| 7928 | if (++Param == FnDecl->param_end()) |
| 7929 | Valid = true; |
| 7930 | goto FinishedParams; |
| 7931 | } |
| 7932 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 7933 | // 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] | 7934 | const PointerType *PT = T->getAs<PointerType>(); |
| 7935 | if (!PT) |
| 7936 | goto FinishedParams; |
| 7937 | T = PT->getPointeeType(); |
| 7938 | if (!T.isConstQualified()) |
| 7939 | goto FinishedParams; |
| 7940 | T = T.getUnqualifiedType(); |
| 7941 | |
| 7942 | // Move on to the second parameter; |
| 7943 | ++Param; |
| 7944 | |
| 7945 | // If there is no second parameter, the first must be a const char * |
| 7946 | if (Param == FnDecl->param_end()) { |
| 7947 | if (Context.hasSameType(T, Context.CharTy)) |
| 7948 | Valid = true; |
| 7949 | goto FinishedParams; |
| 7950 | } |
| 7951 | |
| 7952 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 7953 | // are allowed as the first parameter to a two-parameter function |
| 7954 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 7955 | Context.hasSameType(T, Context.WCharTy) || |
| 7956 | Context.hasSameType(T, Context.Char16Ty) || |
| 7957 | Context.hasSameType(T, Context.Char32Ty))) |
| 7958 | goto FinishedParams; |
| 7959 | |
| 7960 | // The second and final parameter must be an std::size_t |
| 7961 | T = (*Param)->getType().getUnqualifiedType(); |
| 7962 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 7963 | ++Param == FnDecl->param_end()) |
| 7964 | Valid = true; |
| 7965 | } |
| 7966 | |
| 7967 | // FIXME: This diagnostic is absolutely terrible. |
| 7968 | FinishedParams: |
| 7969 | if (!Valid) { |
| 7970 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 7971 | << FnDecl->getDeclName(); |
| 7972 | return true; |
| 7973 | } |
| 7974 | |
| 7975 | return false; |
| 7976 | } |
| 7977 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 7978 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 7979 | /// linkage specification, including the language and (if present) |
| 7980 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 7981 | /// the location of the language string literal, which is provided |
| 7982 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 7983 | /// the '{' brace. Otherwise, this linkage specification does not |
| 7984 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 7985 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 7986 | SourceLocation LangLoc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7987 | StringRef Lang, |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 7988 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 7989 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 7990 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 7991 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 7992 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 7993 | Language = LinkageSpecDecl::lang_cxx; |
| 7994 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 7995 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7996 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 7997 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7998 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 7999 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8000 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 8001 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 8002 | ExternLoc, LangLoc, Language); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 8003 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 8004 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8005 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 8006 | } |
| 8007 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 8008 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 8009 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 8010 | /// valid, it's the position of the closing '}' brace in a linkage |
| 8011 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8012 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 8013 | Decl *LinkageSpec, |
| 8014 | SourceLocation RBraceLoc) { |
| 8015 | if (LinkageSpec) { |
| 8016 | if (RBraceLoc.isValid()) { |
| 8017 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 8018 | LSDecl->setRBraceLoc(RBraceLoc); |
| 8019 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 8020 | PopDeclContext(); |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 8021 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 8022 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 8023 | } |
| 8024 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 8025 | /// \brief Perform semantic analysis for the variable declaration that |
| 8026 | /// occurs within a C++ catch clause, returning the newly-created |
| 8027 | /// variable. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8028 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 8029 | TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8030 | SourceLocation StartLoc, |
| 8031 | SourceLocation Loc, |
| 8032 | IdentifierInfo *Name) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 8033 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 8034 | QualType ExDeclType = TInfo->getType(); |
| 8035 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8036 | // Arrays and functions decay. |
| 8037 | if (ExDeclType->isArrayType()) |
| 8038 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 8039 | else if (ExDeclType->isFunctionType()) |
| 8040 | ExDeclType = Context.getPointerType(ExDeclType); |
| 8041 | |
| 8042 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 8043 | // The exception-declaration shall not denote a pointer or reference to an |
| 8044 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 8045 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8046 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 8047 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 8048 | Invalid = true; |
| 8049 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 8050 | |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 8051 | // GCC allows catching pointers and references to incomplete types |
| 8052 | // as an extension; so do we, but we warn by default. |
| 8053 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8054 | QualType BaseType = ExDeclType; |
| 8055 | 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] | 8056 | unsigned DK = diag::err_catch_incomplete; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 8057 | bool IncompleteCatchIsInvalid = true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 8058 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8059 | BaseType = Ptr->getPointeeType(); |
| 8060 | Mode = 1; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 8061 | DK = diag::ext_catch_incomplete_ptr; |
| 8062 | IncompleteCatchIsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8063 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 8064 | // 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] | 8065 | BaseType = Ref->getPointeeType(); |
| 8066 | Mode = 2; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 8067 | DK = diag::ext_catch_incomplete_ref; |
| 8068 | IncompleteCatchIsInvalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8069 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 8070 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 8071 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK) && |
| 8072 | IncompleteCatchIsInvalid) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8073 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8074 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8075 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 8076 | RequireNonAbstractType(Loc, ExDeclType, |
| 8077 | diag::err_abstract_type_in_decl, |
| 8078 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 8079 | Invalid = true; |
| 8080 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 8081 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 8082 | // of ObjC types, and no runtime supports catching ObjC types by value. |
| 8083 | if (!Invalid && getLangOptions().ObjC1) { |
| 8084 | QualType T = ExDeclType; |
| 8085 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 8086 | T = RT->getPointeeType(); |
| 8087 | |
| 8088 | if (T->isObjCObjectType()) { |
| 8089 | Diag(Loc, diag::err_objc_object_catch); |
| 8090 | Invalid = true; |
| 8091 | } else if (T->isObjCObjectPointerType()) { |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 8092 | if (!getLangOptions().ObjCNonFragileABI) |
| 8093 | Diag(Loc, diag::warn_objc_pointer_cxx_catch_fragile); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 8094 | } |
| 8095 | } |
| 8096 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8097 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
| 8098 | ExDeclType, TInfo, SC_None, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 8099 | ExDecl->setExceptionVariable(true); |
| 8100 | |
Douglas Gregor | c41b878 | 2011-07-06 18:14:43 +0000 | [diff] [blame] | 8101 | if (!Invalid && !ExDeclType->isDependentType()) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 8102 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 8103 | // C++ [except.handle]p16: |
| 8104 | // The object declared in an exception-declaration or, if the |
| 8105 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 8106 | // copy-initialized (8.5) from the exception object. [...] |
| 8107 | // The object is destroyed when the handler exits, after the destruction |
| 8108 | // of any automatic objects initialized within the handler. |
| 8109 | // |
| 8110 | // We just pretend to initialize the object with itself, then make sure |
| 8111 | // it can be destroyed later. |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 8112 | QualType initType = ExDeclType; |
| 8113 | |
| 8114 | InitializedEntity entity = |
| 8115 | InitializedEntity::InitializeVariable(ExDecl); |
| 8116 | InitializationKind initKind = |
| 8117 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 8118 | |
| 8119 | Expr *opaqueValue = |
| 8120 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
| 8121 | InitializationSequence sequence(*this, entity, initKind, &opaqueValue, 1); |
| 8122 | ExprResult result = sequence.Perform(*this, entity, initKind, |
| 8123 | MultiExprArg(&opaqueValue, 1)); |
| 8124 | if (result.isInvalid()) |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 8125 | Invalid = true; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 8126 | else { |
| 8127 | // If the constructor used was non-trivial, set this as the |
| 8128 | // "initializer". |
| 8129 | CXXConstructExpr *construct = cast<CXXConstructExpr>(result.take()); |
| 8130 | if (!construct->getConstructor()->isTrivial()) { |
| 8131 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 8132 | ExDecl->setInit(init); |
| 8133 | } |
| 8134 | |
| 8135 | // And make sure it's destructable. |
| 8136 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 8137 | } |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 8138 | } |
| 8139 | } |
| 8140 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 8141 | if (Invalid) |
| 8142 | ExDecl->setInvalidDecl(); |
| 8143 | |
| 8144 | return ExDecl; |
| 8145 | } |
| 8146 | |
| 8147 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 8148 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8149 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8150 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 8151 | bool Invalid = D.isInvalidType(); |
| 8152 | |
| 8153 | // Check for unexpanded parameter packs. |
| 8154 | if (TInfo && DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 8155 | UPPC_ExceptionType)) { |
| 8156 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 8157 | D.getIdentifierLoc()); |
| 8158 | Invalid = true; |
| 8159 | } |
| 8160 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8161 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 8162 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 8163 | LookupOrdinaryName, |
| 8164 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8165 | // The scope should be freshly made just for us. There is just no way |
| 8166 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8167 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8168 | if (PrevDecl->isTemplateParameter()) { |
| 8169 | // Maybe we will complain about the shadowed template parameter. |
| 8170 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8171 | } |
| 8172 | } |
| 8173 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 8174 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8175 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 8176 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 8177 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8178 | } |
| 8179 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 8180 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8181 | D.getSourceRange().getBegin(), |
| 8182 | D.getIdentifierLoc(), |
| 8183 | D.getIdentifier()); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 8184 | if (Invalid) |
| 8185 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8186 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8187 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8188 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 8189 | PushOnScopeChains(ExDecl, S); |
| 8190 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 8191 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8192 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 8193 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8194 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8195 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 8196 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 8197 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8198 | Expr *AssertExpr, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 8199 | Expr *AssertMessageExpr_, |
| 8200 | SourceLocation RParenLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8201 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr_); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 8202 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 8203 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 8204 | llvm::APSInt Value(32); |
| 8205 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 8206 | Diag(StaticAssertLoc, |
| 8207 | diag::err_static_assert_expression_is_not_constant) << |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 8208 | AssertExpr->getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8209 | return 0; |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 8210 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 8211 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 8212 | if (Value == 0) { |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 8213 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 8214 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 8215 | } |
| 8216 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8217 | |
Douglas Gregor | 399ad97 | 2010-12-15 23:55:21 +0000 | [diff] [blame] | 8218 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 8219 | return 0; |
| 8220 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 8221 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
| 8222 | AssertExpr, AssertMessage, RParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8223 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 8224 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8225 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 8226 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 8227 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8228 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 8229 | /// |
| 8230 | /// \returns A friend declaration that. |
| 8231 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation FriendLoc, |
| 8232 | TypeSourceInfo *TSInfo) { |
| 8233 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 8234 | |
| 8235 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8236 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8237 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 8238 | if (!getLangOptions().CPlusPlus0x) { |
| 8239 | // C++03 [class.friend]p2: |
| 8240 | // An elaborated-type-specifier shall be used in a friend declaration |
| 8241 | // for a class.* |
| 8242 | // |
| 8243 | // * The class-key of the elaborated-type-specifier is required. |
| 8244 | if (!ActiveTemplateInstantiations.empty()) { |
| 8245 | // Do not complain about the form of friend template types during |
| 8246 | // template instantiation; we will already have complained when the |
| 8247 | // template was declared. |
| 8248 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 8249 | // If we evaluated the type to a record type, suggest putting |
| 8250 | // a tag in front. |
| 8251 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 8252 | RecordDecl *RD = RT->getDecl(); |
| 8253 | |
| 8254 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 8255 | |
| 8256 | Diag(TypeRange.getBegin(), diag::ext_unelaborated_friend_type) |
| 8257 | << (unsigned) RD->getTagKind() |
| 8258 | << T |
| 8259 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 8260 | InsertionText); |
| 8261 | } else { |
| 8262 | Diag(FriendLoc, diag::ext_nonclass_type_friend) |
| 8263 | << T |
| 8264 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
| 8265 | } |
| 8266 | } else if (T->getAs<EnumType>()) { |
| 8267 | Diag(FriendLoc, diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8268 | << T |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8269 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8270 | } |
| 8271 | } |
| 8272 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 8273 | // C++0x [class.friend]p3: |
| 8274 | // If the type specifier in a friend declaration designates a (possibly |
| 8275 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 8276 | // the friend declaration is ignored. |
| 8277 | |
| 8278 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 8279 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8280 | |
| 8281 | return FriendDecl::Create(Context, CurContext, FriendLoc, TSInfo, FriendLoc); |
| 8282 | } |
| 8283 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8284 | /// Handle a friend tag declaration where the scope specifier was |
| 8285 | /// templated. |
| 8286 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 8287 | unsigned TagSpec, SourceLocation TagLoc, |
| 8288 | CXXScopeSpec &SS, |
| 8289 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 8290 | AttributeList *Attr, |
| 8291 | MultiTemplateParamsArg TempParamLists) { |
| 8292 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 8293 | |
| 8294 | bool isExplicitSpecialization = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8295 | bool Invalid = false; |
| 8296 | |
| 8297 | if (TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 8298 | = MatchTemplateParametersToScopeSpecifier(TagLoc, NameLoc, SS, |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8299 | TempParamLists.get(), |
| 8300 | TempParamLists.size(), |
| 8301 | /*friend*/ true, |
| 8302 | isExplicitSpecialization, |
| 8303 | Invalid)) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8304 | if (TemplateParams->size() > 0) { |
| 8305 | // This is a declaration of a class template. |
| 8306 | if (Invalid) |
| 8307 | return 0; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 8308 | |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 8309 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 8310 | SS, Name, NameLoc, Attr, |
| 8311 | TemplateParams, AS_public, |
| 8312 | TempParamLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 8313 | (TemplateParameterList**) TempParamLists.release()).take(); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8314 | } else { |
| 8315 | // The "template<>" header is extraneous. |
| 8316 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 8317 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 8318 | isExplicitSpecialization = true; |
| 8319 | } |
| 8320 | } |
| 8321 | |
| 8322 | if (Invalid) return 0; |
| 8323 | |
| 8324 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 8325 | |
| 8326 | bool isAllExplicitSpecializations = true; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 8327 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8328 | if (TempParamLists.get()[I]->size()) { |
| 8329 | isAllExplicitSpecializations = false; |
| 8330 | break; |
| 8331 | } |
| 8332 | } |
| 8333 | |
| 8334 | // FIXME: don't ignore attributes. |
| 8335 | |
| 8336 | // If it's explicit specializations all the way down, just forget |
| 8337 | // about the template header and build an appropriate non-templated |
| 8338 | // friend. TODO: for source fidelity, remember the headers. |
| 8339 | if (isAllExplicitSpecializations) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8340 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8341 | ElaboratedTypeKeyword Keyword |
| 8342 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8343 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8344 | *Name, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8345 | if (T.isNull()) |
| 8346 | return 0; |
| 8347 | |
| 8348 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8349 | if (isa<DependentNameType>(T)) { |
| 8350 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 8351 | TL.setKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8352 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8353 | TL.setNameLoc(NameLoc); |
| 8354 | } else { |
| 8355 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
| 8356 | TL.setKeywordLoc(TagLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8357 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8358 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(NameLoc); |
| 8359 | } |
| 8360 | |
| 8361 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 8362 | TSI, FriendLoc); |
| 8363 | Friend->setAccess(AS_public); |
| 8364 | CurContext->addDecl(Friend); |
| 8365 | return Friend; |
| 8366 | } |
| 8367 | |
| 8368 | // Handle the case of a templated-scope friend class. e.g. |
| 8369 | // template <class T> class A<T>::B; |
| 8370 | // FIXME: we don't support these right now. |
| 8371 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 8372 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 8373 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8374 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 8375 | TL.setKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8376 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8377 | TL.setNameLoc(NameLoc); |
| 8378 | |
| 8379 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 8380 | TSI, FriendLoc); |
| 8381 | Friend->setAccess(AS_public); |
| 8382 | Friend->setUnsupportedFriend(true); |
| 8383 | CurContext->addDecl(Friend); |
| 8384 | return Friend; |
| 8385 | } |
| 8386 | |
| 8387 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8388 | /// Handle a friend type declaration. This works in tandem with |
| 8389 | /// ActOnTag. |
| 8390 | /// |
| 8391 | /// Notes on friend class templates: |
| 8392 | /// |
| 8393 | /// We generally treat friend class declarations as if they were |
| 8394 | /// declaring a class. So, for example, the elaborated type specifier |
| 8395 | /// in a friend declaration is required to obey the restrictions of a |
| 8396 | /// class-head (i.e. no typedefs in the scope chain), template |
| 8397 | /// parameters are required to match up with simple template-ids, &c. |
| 8398 | /// However, unlike when declaring a template specialization, it's |
| 8399 | /// okay to refer to a template specialization without an empty |
| 8400 | /// template parameter declaration, e.g. |
| 8401 | /// friend class A<T>::B<unsigned>; |
| 8402 | /// We permit this as a special case; if there are any template |
| 8403 | /// parameters present at all, require proper matching, i.e. |
| 8404 | /// template <> template <class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8405 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 8406 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8407 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8408 | |
| 8409 | assert(DS.isFriendSpecified()); |
| 8410 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 8411 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8412 | // Try to convert the decl specifier to a type. This works for |
| 8413 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 8414 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 8415 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8416 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 8417 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 8418 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8419 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8420 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 8421 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 8422 | return 0; |
| 8423 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8424 | // This is definitely an error in C++98. It's probably meant to |
| 8425 | // be forbidden in C++0x, too, but the specification is just |
| 8426 | // poorly written. |
| 8427 | // |
| 8428 | // The problem is with declarations like the following: |
| 8429 | // template <T> friend A<T>::foo; |
| 8430 | // where deciding whether a class C is a friend or not now hinges |
| 8431 | // on whether there exists an instantiation of A that causes |
| 8432 | // 'foo' to equal C. There are restrictions on class-heads |
| 8433 | // (which we declare (by fiat) elaborated friend declarations to |
| 8434 | // be) that makes this tractable. |
| 8435 | // |
| 8436 | // FIXME: handle "template <> friend class A<T>;", which |
| 8437 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 8438 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8439 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 8440 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8441 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8442 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8443 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8444 | // C++98 [class.friend]p1: A friend of a class is a function |
| 8445 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 8446 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 8447 | // deadline. It's also a very silly restriction that seriously |
| 8448 | // affects inner classes and which nobody else seems to implement; |
| 8449 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 8450 | // |
| 8451 | // But note that we could warn about it: it's always useless to |
| 8452 | // friend one of your own members (it's not, however, worthless to |
| 8453 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8454 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8455 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8456 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8457 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8458 | NumTempParamLists, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 8459 | TempParams.release(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 8460 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8461 | DS.getFriendSpecLoc()); |
| 8462 | else |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8463 | D = CheckFriendTypeDecl(DS.getFriendSpecLoc(), TSI); |
| 8464 | |
| 8465 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8466 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8467 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8468 | D->setAccess(AS_public); |
| 8469 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8470 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8471 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8472 | } |
| 8473 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8474 | Decl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, bool IsDefinition, |
| 8475 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8476 | const DeclSpec &DS = D.getDeclSpec(); |
| 8477 | |
| 8478 | assert(DS.isFriendSpecified()); |
| 8479 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 8480 | |
| 8481 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8482 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 8483 | QualType T = TInfo->getType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8484 | |
| 8485 | // C++ [class.friend]p1 |
| 8486 | // A friend of a class is a function or class.... |
| 8487 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8488 | // It *doesn't* see through dependent types, which is correct |
| 8489 | // according to [temp.arg.type]p3: |
| 8490 | // If a declaration acquires a function type through a |
| 8491 | // type dependent on a template-parameter and this causes |
| 8492 | // a declaration that does not use the syntactic form of a |
| 8493 | // function declarator to have a function type, the program |
| 8494 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8495 | if (!T->isFunctionType()) { |
| 8496 | Diag(Loc, diag::err_unexpected_friend); |
| 8497 | |
| 8498 | // It might be worthwhile to try to recover by creating an |
| 8499 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8500 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8501 | } |
| 8502 | |
| 8503 | // C++ [namespace.memdef]p3 |
| 8504 | // - If a friend declaration in a non-local class first declares a |
| 8505 | // class or function, the friend class or function is a member |
| 8506 | // of the innermost enclosing namespace. |
| 8507 | // - The name of the friend is not found by simple name lookup |
| 8508 | // until a matching declaration is provided in that namespace |
| 8509 | // scope (either before or after the class declaration granting |
| 8510 | // friendship). |
| 8511 | // - If a friend function is called, its name may be found by the |
| 8512 | // name lookup that considers functions from namespaces and |
| 8513 | // classes associated with the types of the function arguments. |
| 8514 | // - When looking for a prior declaration of a class or a function |
| 8515 | // declared as a friend, scopes outside the innermost enclosing |
| 8516 | // namespace scope are not considered. |
| 8517 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8518 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8519 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 8520 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8521 | assert(Name); |
| 8522 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 8523 | // Check for unexpanded parameter packs. |
| 8524 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 8525 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 8526 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 8527 | return 0; |
| 8528 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8529 | // The context we found the declaration in, or in which we should |
| 8530 | // create the declaration. |
| 8531 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 8532 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8533 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8534 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8535 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8536 | // FIXME: there are different rules in local classes |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8537 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8538 | // There are four cases here. |
| 8539 | // - 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] | 8540 | // appropriate scope and look for a function or function template |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8541 | // there as appropriate. |
| 8542 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 8543 | if (SS.isInvalid() || !SS.isSet()) { |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 8544 | // C++0x [namespace.memdef]p3: |
| 8545 | // If the name in a friend declaration is neither qualified nor |
| 8546 | // a template-id and the declaration is a function or an |
| 8547 | // elaborated-type-specifier, the lookup to determine whether |
| 8548 | // the entity has been previously declared shall not consider |
| 8549 | // any scopes outside the innermost enclosing namespace. |
| 8550 | // C++0x [class.friend]p11: |
| 8551 | // If a friend declaration appears in a local class and the name |
| 8552 | // specified is an unqualified name, a prior declaration is |
| 8553 | // looked up without considering scopes that are outside the |
| 8554 | // innermost enclosing non-class scope. For a friend function |
| 8555 | // declaration, if there is no prior declaration, the program is |
| 8556 | // ill-formed. |
| 8557 | bool isLocal = cast<CXXRecordDecl>(CurContext)->isLocalClass(); |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 8558 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8559 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 8560 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8561 | DC = CurContext; |
| 8562 | while (true) { |
| 8563 | // Skip class contexts. If someone can cite chapter and verse |
| 8564 | // for this behavior, that would be nice --- it's what GCC and |
| 8565 | // EDG do, and it seems like a reasonable intent, but the spec |
| 8566 | // really only says that checks for unqualified existing |
| 8567 | // declarations should stop at the nearest enclosing namespace, |
| 8568 | // not that they should only consider the nearest enclosing |
| 8569 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 8570 | while (DC->isRecord()) |
| 8571 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8572 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8573 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8574 | |
| 8575 | // TODO: decide what we think about using declarations. |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 8576 | if (isLocal || !Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8577 | break; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 8578 | |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 8579 | if (isTemplateId) { |
| 8580 | if (isa<TranslationUnitDecl>(DC)) break; |
| 8581 | } else { |
| 8582 | if (DC->isFileContext()) break; |
| 8583 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8584 | DC = DC->getParent(); |
| 8585 | } |
| 8586 | |
| 8587 | // C++ [class.friend]p1: A friend of a class is a function or |
| 8588 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 8589 | // C++0x changes this for both friend types and functions. |
| 8590 | // Most C++ 98 compilers do seem to give an error here, so |
| 8591 | // we do, too. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8592 | if (!Previous.empty() && DC->Equals(CurContext) |
| 8593 | && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8594 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8595 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 8596 | DCScope = getScopeForDeclContext(S, DC); |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 8597 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8598 | // - There's a non-dependent scope specifier, in which case we |
| 8599 | // compute it and do a previous lookup there for a function |
| 8600 | // or function template. |
| 8601 | } else if (!SS.getScopeRep()->isDependent()) { |
| 8602 | DC = computeDeclContext(SS); |
| 8603 | if (!DC) return 0; |
| 8604 | |
| 8605 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 8606 | |
| 8607 | LookupQualifiedName(Previous, DC); |
| 8608 | |
| 8609 | // Ignore things found implicitly in the wrong scope. |
| 8610 | // TODO: better diagnostics for this case. Suggesting the right |
| 8611 | // qualified scope would be nice... |
| 8612 | LookupResult::Filter F = Previous.makeFilter(); |
| 8613 | while (F.hasNext()) { |
| 8614 | NamedDecl *D = F.next(); |
| 8615 | if (!DC->InEnclosingNamespaceSetOf( |
| 8616 | D->getDeclContext()->getRedeclContext())) |
| 8617 | F.erase(); |
| 8618 | } |
| 8619 | F.done(); |
| 8620 | |
| 8621 | if (Previous.empty()) { |
| 8622 | D.setInvalidType(); |
| 8623 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 8624 | return 0; |
| 8625 | } |
| 8626 | |
| 8627 | // C++ [class.friend]p1: A friend of a class is a function or |
| 8628 | // class that is not a member of the class . . . |
| 8629 | if (DC->Equals(CurContext)) |
| 8630 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 8631 | |
| 8632 | // - There's a scope specifier that does not match any template |
| 8633 | // parameter lists, in which case we use some arbitrary context, |
| 8634 | // create a method or method template, and wait for instantiation. |
| 8635 | // - There's a scope specifier that does match some template |
| 8636 | // parameter lists, which we don't handle right now. |
| 8637 | } else { |
| 8638 | DC = CurContext; |
| 8639 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8640 | } |
| 8641 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 8642 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8643 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8644 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 8645 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 8646 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8647 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8648 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 8649 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8650 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8651 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8652 | } |
| 8653 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 8654 | bool Redeclaration = false; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 8655 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, T, TInfo, Previous, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 8656 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 8657 | IsDefinition, |
| 8658 | Redeclaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8659 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 8660 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 8661 | assert(ND->getDeclContext() == DC); |
| 8662 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 8663 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 8664 | // Add the function declaration to the appropriate lookup tables, |
| 8665 | // adjusting the redeclarations list as necessary. We don't |
| 8666 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8667 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 8668 | // Also update the scope-based lookup if the target context's |
| 8669 | // lookup context is in lexical scope. |
| 8670 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8671 | DC = DC->getRedeclContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 8672 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 8673 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 8674 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 8675 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8676 | |
| 8677 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 8678 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8679 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 8680 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8681 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8682 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8683 | if (ND->isInvalidDecl()) |
| 8684 | FrD->setInvalidDecl(); |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 8685 | else { |
| 8686 | FunctionDecl *FD; |
| 8687 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 8688 | FD = FTD->getTemplatedDecl(); |
| 8689 | else |
| 8690 | FD = cast<FunctionDecl>(ND); |
| 8691 | |
| 8692 | // Mark templated-scope function declarations as unsupported. |
| 8693 | if (FD->getNumTemplateParameterLists()) |
| 8694 | FrD->setUnsupportedFriend(true); |
| 8695 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8696 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8697 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 8698 | } |
| 8699 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8700 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 8701 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8702 | |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 8703 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 8704 | if (!Fn) { |
| 8705 | Diag(DelLoc, diag::err_deleted_non_function); |
| 8706 | return; |
| 8707 | } |
| 8708 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 8709 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 8710 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 8711 | // If the declaration wasn't the first, we delete the function anyway for |
| 8712 | // recovery. |
| 8713 | } |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 8714 | Fn->setDeletedAsWritten(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 8715 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 8716 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 8717 | void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) { |
| 8718 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Dcl); |
| 8719 | |
| 8720 | if (MD) { |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 8721 | if (MD->getParent()->isDependentType()) { |
| 8722 | MD->setDefaulted(); |
| 8723 | MD->setExplicitlyDefaulted(); |
| 8724 | return; |
| 8725 | } |
| 8726 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 8727 | CXXSpecialMember Member = getSpecialMember(MD); |
| 8728 | if (Member == CXXInvalid) { |
| 8729 | Diag(DefaultLoc, diag::err_default_special_members); |
| 8730 | return; |
| 8731 | } |
| 8732 | |
| 8733 | MD->setDefaulted(); |
| 8734 | MD->setExplicitlyDefaulted(); |
| 8735 | |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 8736 | // If this definition appears within the record, do the checking when |
| 8737 | // the record is complete. |
| 8738 | const FunctionDecl *Primary = MD; |
| 8739 | if (MD->getTemplatedKind() != FunctionDecl::TK_NonTemplate) |
| 8740 | // Find the uninstantiated declaration that actually had the '= default' |
| 8741 | // on it. |
| 8742 | MD->getTemplateInstantiationPattern()->isDefined(Primary); |
| 8743 | |
| 8744 | if (Primary == Primary->getCanonicalDecl()) |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 8745 | return; |
| 8746 | |
| 8747 | switch (Member) { |
| 8748 | case CXXDefaultConstructor: { |
| 8749 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
| 8750 | CheckExplicitlyDefaultedDefaultConstructor(CD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8751 | if (!CD->isInvalidDecl()) |
| 8752 | DefineImplicitDefaultConstructor(DefaultLoc, CD); |
| 8753 | break; |
| 8754 | } |
| 8755 | |
| 8756 | case CXXCopyConstructor: { |
| 8757 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
| 8758 | CheckExplicitlyDefaultedCopyConstructor(CD); |
| 8759 | if (!CD->isInvalidDecl()) |
| 8760 | DefineImplicitCopyConstructor(DefaultLoc, CD); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 8761 | break; |
| 8762 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8763 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 8764 | case CXXCopyAssignment: { |
| 8765 | CheckExplicitlyDefaultedCopyAssignment(MD); |
| 8766 | if (!MD->isInvalidDecl()) |
| 8767 | DefineImplicitCopyAssignment(DefaultLoc, MD); |
| 8768 | break; |
| 8769 | } |
| 8770 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8771 | case CXXDestructor: { |
| 8772 | CXXDestructorDecl *DD = cast<CXXDestructorDecl>(MD); |
| 8773 | CheckExplicitlyDefaultedDestructor(DD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8774 | if (!DD->isInvalidDecl()) |
| 8775 | DefineImplicitDestructor(DefaultLoc, DD); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8776 | break; |
| 8777 | } |
| 8778 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8779 | case CXXMoveConstructor: |
| 8780 | case CXXMoveAssignment: |
| 8781 | Diag(Dcl->getLocation(), diag::err_defaulted_move_unsupported); |
| 8782 | break; |
| 8783 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 8784 | default: |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 8785 | // FIXME: Do the rest once we have move functions |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 8786 | break; |
| 8787 | } |
| 8788 | } else { |
| 8789 | Diag(DefaultLoc, diag::err_default_special_members); |
| 8790 | } |
| 8791 | } |
| 8792 | |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 8793 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 8794 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 8795 | Stmt *SubStmt = *CI; |
| 8796 | if (!SubStmt) |
| 8797 | continue; |
| 8798 | if (isa<ReturnStmt>(SubStmt)) |
| 8799 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 8800 | diag::err_return_in_constructor_handler); |
| 8801 | if (!isa<Expr>(SubStmt)) |
| 8802 | SearchForReturnInStmt(Self, SubStmt); |
| 8803 | } |
| 8804 | } |
| 8805 | |
| 8806 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 8807 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 8808 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 8809 | SearchForReturnInStmt(*this, Handler); |
| 8810 | } |
| 8811 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8812 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8813 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8814 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 8815 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 8816 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8817 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 8818 | if (Context.hasSameType(NewTy, OldTy) || |
| 8819 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8820 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8821 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8822 | // Check if the return types are covariant |
| 8823 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8824 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8825 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 8826 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 8827 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8828 | NewClassTy = NewPT->getPointeeType(); |
| 8829 | OldClassTy = OldPT->getPointeeType(); |
| 8830 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 8831 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 8832 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 8833 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 8834 | NewClassTy = NewRT->getPointeeType(); |
| 8835 | OldClassTy = OldRT->getPointeeType(); |
| 8836 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8837 | } |
| 8838 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8839 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8840 | // The return types aren't either both pointers or references to a class type. |
| 8841 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8842 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8843 | diag::err_different_return_type_for_overriding_virtual_function) |
| 8844 | << New->getDeclName() << NewTy << OldTy; |
| 8845 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8846 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8847 | return true; |
| 8848 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8849 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 8850 | // C++ [class.virtual]p6: |
| 8851 | // If the return type of D::f differs from the return type of B::f, the |
| 8852 | // class type in the return type of D::f shall be complete at the point of |
| 8853 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 8854 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 8855 | if (!RT->isBeingDefined() && |
| 8856 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 8857 | PDiag(diag::err_covariant_return_incomplete) |
| 8858 | << New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 8859 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 8860 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 8861 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 8862 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8863 | // Check if the new class derives from the old class. |
| 8864 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 8865 | Diag(New->getLocation(), |
| 8866 | diag::err_covariant_return_not_derived) |
| 8867 | << New->getDeclName() << NewTy << OldTy; |
| 8868 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 8869 | return true; |
| 8870 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8871 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8872 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 8873 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 8874 | diag::err_covariant_return_inaccessible_base, |
| 8875 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 8876 | // FIXME: Should this point to the return type? |
| 8877 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 8878 | // FIXME: this note won't trigger for delayed access control |
| 8879 | // diagnostics, and it's impossible to get an undelayed error |
| 8880 | // here from access control during the original parse because |
| 8881 | // the ParsingDeclSpec/ParsingDeclarator are still in scope. |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8882 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 8883 | return true; |
| 8884 | } |
| 8885 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8886 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8887 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 8888 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8889 | Diag(New->getLocation(), |
| 8890 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8891 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8892 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 8893 | return true; |
| 8894 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8895 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8896 | |
| 8897 | // The new class type must have the same or less qualifiers as the old type. |
| 8898 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 8899 | Diag(New->getLocation(), |
| 8900 | diag::err_covariant_return_type_class_type_more_qualified) |
| 8901 | << New->getDeclName() << NewTy << OldTy; |
| 8902 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 8903 | return true; |
| 8904 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8905 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8906 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8907 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8908 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8909 | /// \brief Mark the given method pure. |
| 8910 | /// |
| 8911 | /// \param Method the method to be marked pure. |
| 8912 | /// |
| 8913 | /// \param InitRange the source range that covers the "0" initializer. |
| 8914 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8915 | SourceLocation EndLoc = InitRange.getEnd(); |
| 8916 | if (EndLoc.isValid()) |
| 8917 | Method->setRangeEnd(EndLoc); |
| 8918 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8919 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 8920 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8921 | return false; |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8922 | } |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8923 | |
| 8924 | if (!Method->isInvalidDecl()) |
| 8925 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 8926 | << Method->getDeclName() << InitRange; |
| 8927 | return true; |
| 8928 | } |
| 8929 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 8930 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 8931 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 8932 | /// is a fresh scope pushed for just this purpose. |
| 8933 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8934 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 8935 | /// static data member of class X, names should be looked up in the scope of |
| 8936 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8937 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8938 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 8939 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8940 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 8941 | // We should only get called for declarations with scope specifiers, like: |
| 8942 | // int foo::bar; |
| 8943 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 8944 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8945 | } |
| 8946 | |
| 8947 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8948 | /// initializer for the out-of-line declaration 'D'. |
| 8949 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8950 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 8951 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8952 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 8953 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 8954 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8955 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 8956 | |
| 8957 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 8958 | /// C++ if/switch/while/for statement. |
| 8959 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8960 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 8961 | // C++ 6.4p2: |
| 8962 | // The declarator shall not specify a function or an array. |
| 8963 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 8964 | // new class or enumeration. |
| 8965 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 8966 | "Parser allowed 'typedef' as storage class of condition decl."); |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 8967 | |
| 8968 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 8969 | if (!Dcl) |
| 8970 | return true; |
| 8971 | |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 8972 | if (isa<FunctionDecl>(Dcl)) { // The declarator shall not specify a function. |
| 8973 | Diag(Dcl->getLocation(), diag::err_invalid_use_of_function_type) |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 8974 | << D.getSourceRange(); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 8975 | return true; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 8976 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 8977 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 8978 | return Dcl; |
| 8979 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 8980 | |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 8981 | void Sema::LoadExternalVTableUses() { |
| 8982 | if (!ExternalSource) |
| 8983 | return; |
| 8984 | |
| 8985 | SmallVector<ExternalVTableUse, 4> VTables; |
| 8986 | ExternalSource->ReadUsedVTables(VTables); |
| 8987 | SmallVector<VTableUse, 4> NewUses; |
| 8988 | for (unsigned I = 0, N = VTables.size(); I != N; ++I) { |
| 8989 | llvm::DenseMap<CXXRecordDecl *, bool>::iterator Pos |
| 8990 | = VTablesUsed.find(VTables[I].Record); |
| 8991 | // Even if a definition wasn't required before, it may be required now. |
| 8992 | if (Pos != VTablesUsed.end()) { |
| 8993 | if (!Pos->second && VTables[I].DefinitionRequired) |
| 8994 | Pos->second = true; |
| 8995 | continue; |
| 8996 | } |
| 8997 | |
| 8998 | VTablesUsed[VTables[I].Record] = VTables[I].DefinitionRequired; |
| 8999 | NewUses.push_back(VTableUse(VTables[I].Record, VTables[I].Location)); |
| 9000 | } |
| 9001 | |
| 9002 | VTableUses.insert(VTableUses.begin(), NewUses.begin(), NewUses.end()); |
| 9003 | } |
| 9004 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9005 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 9006 | bool DefinitionRequired) { |
| 9007 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 9008 | // not have a vtable. |
| 9009 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 9010 | CurContext->isDependentContext() || |
| 9011 | ExprEvalContexts.back().Context == Unevaluated) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 9012 | return; |
| 9013 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9014 | // Try to insert this class into the map. |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 9015 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9016 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 9017 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 9018 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 9019 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 9020 | // If we already had an entry, check to see if we are promoting this vtable |
| 9021 | // to required a definition. If so, we need to reappend to the VTableUses |
| 9022 | // list, since we may have already processed the first entry. |
| 9023 | if (DefinitionRequired && !Pos.first->second) { |
| 9024 | Pos.first->second = true; |
| 9025 | } else { |
| 9026 | // Otherwise, we can early exit. |
| 9027 | return; |
| 9028 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9029 | } |
| 9030 | |
| 9031 | // Local classes need to have their virtual members marked |
| 9032 | // immediately. For all other classes, we mark their virtual members |
| 9033 | // at the end of the translation unit. |
| 9034 | if (Class->isLocalClass()) |
| 9035 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 9036 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9037 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 9038 | } |
| 9039 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9040 | bool Sema::DefineUsedVTables() { |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 9041 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9042 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 9043 | return false; |
Chandler Carruth | aee543a | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 9044 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9045 | // Note: The VTableUses vector could grow as a result of marking |
| 9046 | // the members of a class as "used", so we check the size each |
| 9047 | // time through the loop and prefer indices (with are stable) to |
| 9048 | // iterators (which are not). |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 9049 | bool DefinedAnything = false; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9050 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 9051 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9052 | if (!Class) |
| 9053 | continue; |
| 9054 | |
| 9055 | SourceLocation Loc = VTableUses[I].second; |
| 9056 | |
| 9057 | // If this class has a key function, but that key function is |
| 9058 | // defined in another translation unit, we don't need to emit the |
| 9059 | // vtable even though we're using it. |
| 9060 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 9061 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9062 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 9063 | case TSK_Undeclared: |
| 9064 | case TSK_ExplicitSpecialization: |
| 9065 | case TSK_ExplicitInstantiationDeclaration: |
| 9066 | // The key function is in another translation unit. |
| 9067 | continue; |
| 9068 | |
| 9069 | case TSK_ExplicitInstantiationDefinition: |
| 9070 | case TSK_ImplicitInstantiation: |
| 9071 | // We will be instantiating the key function. |
| 9072 | break; |
| 9073 | } |
| 9074 | } else if (!KeyFunction) { |
| 9075 | // If we have a class with no key function that is the subject |
| 9076 | // of an explicit instantiation declaration, suppress the |
| 9077 | // vtable; it will live with the explicit instantiation |
| 9078 | // definition. |
| 9079 | bool IsExplicitInstantiationDeclaration |
| 9080 | = Class->getTemplateSpecializationKind() |
| 9081 | == TSK_ExplicitInstantiationDeclaration; |
| 9082 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 9083 | REnd = Class->redecls_end(); |
| 9084 | R != REnd; ++R) { |
| 9085 | TemplateSpecializationKind TSK |
| 9086 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 9087 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 9088 | IsExplicitInstantiationDeclaration = true; |
| 9089 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 9090 | IsExplicitInstantiationDeclaration = false; |
| 9091 | break; |
| 9092 | } |
| 9093 | } |
| 9094 | |
| 9095 | if (IsExplicitInstantiationDeclaration) |
| 9096 | continue; |
| 9097 | } |
| 9098 | |
| 9099 | // Mark all of the virtual members of this class as referenced, so |
| 9100 | // that we can build a vtable. Then, tell the AST consumer that a |
| 9101 | // vtable for this class is required. |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 9102 | DefinedAnything = true; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9103 | MarkVirtualMembersReferenced(Loc, Class); |
| 9104 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 9105 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 9106 | |
| 9107 | // Optionally warn if we're emitting a weak vtable. |
| 9108 | if (Class->getLinkage() == ExternalLinkage && |
| 9109 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 9110 | if (!KeyFunction || (KeyFunction->hasBody() && KeyFunction->isInlined())) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9111 | Diag(Class->getLocation(), diag::warn_weak_vtable) << Class; |
| 9112 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 9113 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9114 | VTableUses.clear(); |
| 9115 | |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 9116 | return DefinedAnything; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 9117 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 9118 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 9119 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 9120 | const CXXRecordDecl *RD) { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 9121 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 9122 | e = RD->method_end(); i != e; ++i) { |
| 9123 | CXXMethodDecl *MD = *i; |
| 9124 | |
| 9125 | // C++ [basic.def.odr]p2: |
| 9126 | // [...] A virtual member function is used if it is not pure. [...] |
| 9127 | if (MD->isVirtual() && !MD->isPure()) |
| 9128 | MarkDeclarationReferenced(Loc, MD); |
| 9129 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 9130 | |
| 9131 | // Only classes that have virtual bases need a VTT. |
| 9132 | if (RD->getNumVBases() == 0) |
| 9133 | return; |
| 9134 | |
| 9135 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 9136 | e = RD->bases_end(); i != e; ++i) { |
| 9137 | const CXXRecordDecl *Base = |
| 9138 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 9139 | if (Base->getNumVBases() == 0) |
| 9140 | continue; |
| 9141 | MarkVirtualMembersReferenced(Loc, Base); |
| 9142 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 9143 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9144 | |
| 9145 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 9146 | /// Objective-C implementation whose ivars need be initialized. |
| 9147 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 9148 | if (!getLangOptions().CPlusPlus) |
| 9149 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 9150 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9151 | SmallVector<ObjCIvarDecl*, 8> ivars; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9152 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 9153 | if (ivars.empty()) |
| 9154 | return; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9155 | SmallVector<CXXCtorInitializer*, 32> AllToInit; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9156 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 9157 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 9158 | if (Field->isInvalidDecl()) |
| 9159 | continue; |
| 9160 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 9161 | CXXCtorInitializer *Member; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9162 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 9163 | InitializationKind InitKind = |
| 9164 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 9165 | |
| 9166 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9167 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9168 | InitSeq.Perform(*this, InitEntity, InitKind, MultiExprArg()); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 9169 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9170 | // Note, MemberInit could actually come back empty if no initialization |
| 9171 | // is required (e.g., because it would call a trivial default constructor) |
| 9172 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 9173 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 9174 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9175 | Member = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 9176 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 9177 | SourceLocation(), |
| 9178 | MemberInit.takeAs<Expr>(), |
| 9179 | SourceLocation()); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9180 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 9181 | |
| 9182 | // Be sure that the destructor is accessible and is marked as referenced. |
| 9183 | if (const RecordType *RecordTy |
| 9184 | = Context.getBaseElementType(Field->getType()) |
| 9185 | ->getAs<RecordType>()) { |
| 9186 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 9187 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 9188 | MarkDeclarationReferenced(Field->getLocation(), Destructor); |
| 9189 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 9190 | PDiag(diag::err_access_dtor_ivar) |
| 9191 | << Context.getBaseElementType(Field->getType())); |
| 9192 | } |
| 9193 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9194 | } |
| 9195 | ObjCImplementation->setIvarInitializers(Context, |
| 9196 | AllToInit.data(), AllToInit.size()); |
| 9197 | } |
| 9198 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 9199 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 9200 | static |
| 9201 | void DelegatingCycleHelper(CXXConstructorDecl* Ctor, |
| 9202 | llvm::SmallSet<CXXConstructorDecl*, 4> &Valid, |
| 9203 | llvm::SmallSet<CXXConstructorDecl*, 4> &Invalid, |
| 9204 | llvm::SmallSet<CXXConstructorDecl*, 4> &Current, |
| 9205 | Sema &S) { |
| 9206 | llvm::SmallSet<CXXConstructorDecl*, 4>::iterator CI = Current.begin(), |
| 9207 | CE = Current.end(); |
| 9208 | if (Ctor->isInvalidDecl()) |
| 9209 | return; |
| 9210 | |
| 9211 | const FunctionDecl *FNTarget = 0; |
| 9212 | CXXConstructorDecl *Target; |
| 9213 | |
| 9214 | // We ignore the result here since if we don't have a body, Target will be |
| 9215 | // null below. |
| 9216 | (void)Ctor->getTargetConstructor()->hasBody(FNTarget); |
| 9217 | Target |
| 9218 | = const_cast<CXXConstructorDecl*>(cast_or_null<CXXConstructorDecl>(FNTarget)); |
| 9219 | |
| 9220 | CXXConstructorDecl *Canonical = Ctor->getCanonicalDecl(), |
| 9221 | // Avoid dereferencing a null pointer here. |
| 9222 | *TCanonical = Target ? Target->getCanonicalDecl() : 0; |
| 9223 | |
| 9224 | if (!Current.insert(Canonical)) |
| 9225 | return; |
| 9226 | |
| 9227 | // We know that beyond here, we aren't chaining into a cycle. |
| 9228 | if (!Target || !Target->isDelegatingConstructor() || |
| 9229 | Target->isInvalidDecl() || Valid.count(TCanonical)) { |
| 9230 | for (CI = Current.begin(), CE = Current.end(); CI != CE; ++CI) |
| 9231 | Valid.insert(*CI); |
| 9232 | Current.clear(); |
| 9233 | // We've hit a cycle. |
| 9234 | } else if (TCanonical == Canonical || Invalid.count(TCanonical) || |
| 9235 | Current.count(TCanonical)) { |
| 9236 | // If we haven't diagnosed this cycle yet, do so now. |
| 9237 | if (!Invalid.count(TCanonical)) { |
| 9238 | S.Diag((*Ctor->init_begin())->getSourceLocation(), |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 9239 | diag::warn_delegating_ctor_cycle) |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 9240 | << Ctor; |
| 9241 | |
| 9242 | // Don't add a note for a function delegating directo to itself. |
| 9243 | if (TCanonical != Canonical) |
| 9244 | S.Diag(Target->getLocation(), diag::note_it_delegates_to); |
| 9245 | |
| 9246 | CXXConstructorDecl *C = Target; |
| 9247 | while (C->getCanonicalDecl() != Canonical) { |
| 9248 | (void)C->getTargetConstructor()->hasBody(FNTarget); |
| 9249 | assert(FNTarget && "Ctor cycle through bodiless function"); |
| 9250 | |
| 9251 | C |
| 9252 | = const_cast<CXXConstructorDecl*>(cast<CXXConstructorDecl>(FNTarget)); |
| 9253 | S.Diag(C->getLocation(), diag::note_which_delegates_to); |
| 9254 | } |
| 9255 | } |
| 9256 | |
| 9257 | for (CI = Current.begin(), CE = Current.end(); CI != CE; ++CI) |
| 9258 | Invalid.insert(*CI); |
| 9259 | Current.clear(); |
| 9260 | } else { |
| 9261 | DelegatingCycleHelper(Target, Valid, Invalid, Current, S); |
| 9262 | } |
| 9263 | } |
| 9264 | |
| 9265 | |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 9266 | void Sema::CheckDelegatingCtorCycles() { |
| 9267 | llvm::SmallSet<CXXConstructorDecl*, 4> Valid, Invalid, Current; |
| 9268 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 9269 | llvm::SmallSet<CXXConstructorDecl*, 4>::iterator CI = Current.begin(), |
| 9270 | CE = Current.end(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 9271 | |
Douglas Gregor | 0129b56 | 2011-07-27 21:57:17 +0000 | [diff] [blame] | 9272 | for (DelegatingCtorDeclsType::iterator |
| 9273 | I = DelegatingCtorDecls.begin(ExternalSource), |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 9274 | E = DelegatingCtorDecls.end(); |
| 9275 | I != E; ++I) { |
| 9276 | DelegatingCycleHelper(*I, Valid, Invalid, Current, *this); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 9277 | } |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 9278 | |
| 9279 | for (CI = Invalid.begin(), CE = Invalid.end(); CI != CE; ++CI) |
| 9280 | (*CI)->setInvalidDecl(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 9281 | } |