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) { |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1020 | const CXXMethodDecl *MD = llvm::dyn_cast<CXXMethodDecl>(D); |
| 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 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1082 | bool isFunc = false; |
| 1083 | if (D.isFunctionDeclarator()) |
| 1084 | isFunc = true; |
| 1085 | else if (D.getNumTypeObjects() == 0 && |
| 1086 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1087 | QualType TDType = GetTypeFromParser(DS.getRepAsType()); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1088 | isFunc = TDType->isFunctionType(); |
| 1089 | } |
| 1090 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1091 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 1092 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1093 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 1094 | // data members and cannot be applied to names declared const or static, |
| 1095 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1096 | switch (DS.getStorageClassSpec()) { |
| 1097 | case DeclSpec::SCS_unspecified: |
| 1098 | case DeclSpec::SCS_typedef: |
| 1099 | case DeclSpec::SCS_static: |
| 1100 | // FALL THROUGH. |
| 1101 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1102 | case DeclSpec::SCS_mutable: |
| 1103 | if (isFunc) { |
| 1104 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1105 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1106 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1107 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 1109 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 1110 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1111 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1112 | } |
| 1113 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1114 | default: |
| 1115 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1116 | Diag(DS.getStorageClassSpecLoc(), |
| 1117 | diag::err_storageclass_invalid_for_member); |
| 1118 | else |
| 1119 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 1120 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 1121 | } |
| 1122 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1123 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 1124 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 1125 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1126 | |
| 1127 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1128 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1129 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 1130 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1131 | if (SS.isSet() && !SS.isInvalid()) { |
| 1132 | // The user provided a superfluous scope specifier inside a class |
| 1133 | // definition: |
| 1134 | // |
| 1135 | // class X { |
| 1136 | // int X::member; |
| 1137 | // }; |
| 1138 | DeclContext *DC = 0; |
| 1139 | if ((DC = computeDeclContext(SS, false)) && DC->Equals(CurContext)) |
| 1140 | Diag(D.getIdentifierLoc(), diag::warn_member_extra_qualification) |
| 1141 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
| 1142 | else |
| 1143 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 1144 | << Name << SS.getRange(); |
| 1145 | |
| 1146 | SS.clear(); |
| 1147 | } |
| 1148 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1149 | // FIXME: Check for template parameters! |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 1150 | // FIXME: Check that the name is an identifier! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1151 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1152 | HasDeferredInit, AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1153 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1154 | } else { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1155 | assert(!HasDeferredInit); |
| 1156 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1157 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1158 | if (!Member) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1159 | return 0; |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1160 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1161 | |
| 1162 | // Non-instance-fields can't have a bitfield. |
| 1163 | if (BitWidth) { |
| 1164 | if (Member->isInvalidDecl()) { |
| 1165 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 1166 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1167 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 1168 | // "static member 'A' cannot be a bit-field" |
| 1169 | Diag(Loc, diag::err_static_not_bitfield) |
| 1170 | << Name << BitWidth->getSourceRange(); |
| 1171 | } else if (isa<TypedefDecl>(Member)) { |
| 1172 | // "typedef member 'x' cannot be a bit-field" |
| 1173 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 1174 | << Name << BitWidth->getSourceRange(); |
| 1175 | } else { |
| 1176 | // A function typedef ("typedef int f(); f a;"). |
| 1177 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 1178 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1180 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1181 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1183 | BitWidth = 0; |
| 1184 | Member->setInvalidDecl(); |
| 1185 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1186 | |
| 1187 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1189 | // If we have declared a member function template, set the access of the |
| 1190 | // templated declaration as well. |
| 1191 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 1192 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1193 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1194 | |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1195 | if (VS.isOverrideSpecified()) { |
| 1196 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 1197 | if (!MD || !MD->isVirtual()) { |
| 1198 | Diag(Member->getLocStart(), |
| 1199 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1200 | << "override" << FixItHint::CreateRemoval(VS.getOverrideLoc()); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1201 | } else |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1202 | MD->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context)); |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1203 | } |
| 1204 | if (VS.isFinalSpecified()) { |
| 1205 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 1206 | if (!MD || !MD->isVirtual()) { |
| 1207 | Diag(Member->getLocStart(), |
| 1208 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1209 | << "final" << FixItHint::CreateRemoval(VS.getFinalLoc()); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1210 | } else |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1211 | MD->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context)); |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1212 | } |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1213 | |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1214 | if (VS.getLastLocation().isValid()) { |
| 1215 | // Update the end location of a method that has a virt-specifiers. |
| 1216 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 1217 | MD->setRangeEnd(VS.getLastLocation()); |
| 1218 | } |
| 1219 | |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1220 | CheckOverrideControl(Member); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1221 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1222 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1223 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 1224 | if (Init) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1225 | AddInitializerToDecl(Member, Init, false, |
| 1226 | DS.getTypeSpecType() == DeclSpec::TST_auto); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1227 | else if (DS.getTypeSpecType() == DeclSpec::TST_auto && |
| 1228 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 1229 | // C++0x [dcl.spec.auto]p4: 'auto' can only be used in the type of a static |
| 1230 | // data member if a brace-or-equal-initializer is provided. |
| 1231 | Diag(Loc, diag::err_auto_var_requires_init) |
| 1232 | << Name << cast<ValueDecl>(Member)->getType(); |
| 1233 | Member->setInvalidDecl(); |
| 1234 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1235 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1236 | FinalizeDeclaration(Member); |
| 1237 | |
John McCall | b25b295 | 2011-02-15 07:12:36 +0000 | [diff] [blame] | 1238 | if (isInstField) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1239 | FieldCollector->Add(cast<FieldDecl>(Member)); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1240 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1243 | /// ActOnCXXInClassMemberInitializer - This is invoked after parsing an |
| 1244 | /// in-class initializer for a non-static C++ class member. Such parsing |
| 1245 | /// is deferred until the class is complete. |
| 1246 | void |
| 1247 | Sema::ActOnCXXInClassMemberInitializer(Decl *D, SourceLocation EqualLoc, |
| 1248 | Expr *InitExpr) { |
| 1249 | FieldDecl *FD = cast<FieldDecl>(D); |
| 1250 | |
| 1251 | if (!InitExpr) { |
| 1252 | FD->setInvalidDecl(); |
| 1253 | FD->removeInClassInitializer(); |
| 1254 | return; |
| 1255 | } |
| 1256 | |
| 1257 | ExprResult Init = InitExpr; |
| 1258 | if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent()) { |
| 1259 | // FIXME: if there is no EqualLoc, this is list-initialization. |
| 1260 | Init = PerformCopyInitialization( |
| 1261 | InitializedEntity::InitializeMember(FD), EqualLoc, InitExpr); |
| 1262 | if (Init.isInvalid()) { |
| 1263 | FD->setInvalidDecl(); |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | CheckImplicitConversions(Init.get(), EqualLoc); |
| 1268 | } |
| 1269 | |
| 1270 | // C++0x [class.base.init]p7: |
| 1271 | // The initialization of each base and member constitutes a |
| 1272 | // full-expression. |
| 1273 | Init = MaybeCreateExprWithCleanups(Init); |
| 1274 | if (Init.isInvalid()) { |
| 1275 | FD->setInvalidDecl(); |
| 1276 | return; |
| 1277 | } |
| 1278 | |
| 1279 | InitExpr = Init.release(); |
| 1280 | |
| 1281 | FD->setInClassInitializer(InitExpr); |
| 1282 | } |
| 1283 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1284 | /// \brief Find the direct and/or virtual base specifiers that |
| 1285 | /// correspond to the given base type, for use in base initialization |
| 1286 | /// within a constructor. |
| 1287 | static bool FindBaseInitializer(Sema &SemaRef, |
| 1288 | CXXRecordDecl *ClassDecl, |
| 1289 | QualType BaseType, |
| 1290 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 1291 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 1292 | // First, check for a direct base class. |
| 1293 | DirectBaseSpec = 0; |
| 1294 | for (CXXRecordDecl::base_class_const_iterator Base |
| 1295 | = ClassDecl->bases_begin(); |
| 1296 | Base != ClassDecl->bases_end(); ++Base) { |
| 1297 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 1298 | // We found a direct base of this type. That's what we're |
| 1299 | // initializing. |
| 1300 | DirectBaseSpec = &*Base; |
| 1301 | break; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | // Check for a virtual base class. |
| 1306 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1307 | // there are no virtual bases. |
| 1308 | VirtualBaseSpec = 0; |
| 1309 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1310 | // We haven't found a base yet; search the class hierarchy for a |
| 1311 | // virtual base class. |
| 1312 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1313 | /*DetectVirtual=*/false); |
| 1314 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1315 | BaseType, Paths)) { |
| 1316 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1317 | Path != Paths.end(); ++Path) { |
| 1318 | if (Path->back().Base->isVirtual()) { |
| 1319 | VirtualBaseSpec = Path->back().Base; |
| 1320 | break; |
| 1321 | } |
| 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | return DirectBaseSpec || VirtualBaseSpec; |
| 1327 | } |
| 1328 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1329 | /// ActOnMemInitializer - Handle a C++ member initializer. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1330 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1331 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1332 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1333 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1334 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1335 | ParsedType TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1336 | SourceLocation IdLoc, |
| 1337 | SourceLocation LParenLoc, |
| 1338 | ExprTy **Args, unsigned NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1339 | SourceLocation RParenLoc, |
| 1340 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1341 | if (!ConstructorD) |
| 1342 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1344 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1345 | |
| 1346 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1347 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1348 | if (!Constructor) { |
| 1349 | // The user wrote a constructor initializer on a function that is |
| 1350 | // not a C++ constructor. Ignore the error for now, because we may |
| 1351 | // have more member initializers coming; we'll diagnose it just |
| 1352 | // once in ActOnMemInitializers. |
| 1353 | return true; |
| 1354 | } |
| 1355 | |
| 1356 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1357 | |
| 1358 | // C++ [class.base.init]p2: |
| 1359 | // 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] | 1360 | // constructor's class and, if not found in that scope, are looked |
| 1361 | // up in the scope containing the constructor's definition. |
| 1362 | // [Note: if the constructor's class contains a member with the |
| 1363 | // same name as a direct or virtual base class of the class, a |
| 1364 | // mem-initializer-id naming the member or base class and composed |
| 1365 | // of a single identifier refers to the class member. A |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1366 | // mem-initializer-id for the hidden base class may be specified |
| 1367 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1368 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1369 | // Look for a member, first. |
| 1370 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1372 | = ClassDecl->lookup(MemberOrBase); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1373 | if (Result.first != Result.second) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1374 | Member = dyn_cast<FieldDecl>(*Result.first); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1375 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1376 | if (Member) { |
| 1377 | if (EllipsisLoc.isValid()) |
| 1378 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
| 1379 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1380 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1381 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1382 | LParenLoc, RParenLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1385 | // Handle anonymous union case. |
| 1386 | if (IndirectFieldDecl* IndirectField |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1387 | = dyn_cast<IndirectFieldDecl>(*Result.first)) { |
| 1388 | if (EllipsisLoc.isValid()) |
| 1389 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
| 1390 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1391 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1392 | return BuildMemberInitializer(IndirectField, (Expr**)Args, |
| 1393 | NumArgs, IdLoc, |
| 1394 | LParenLoc, RParenLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1395 | } |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1396 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1397 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1398 | // 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] | 1399 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1400 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1401 | |
| 1402 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1403 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1404 | } else { |
| 1405 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1406 | LookupParsedName(R, S, &SS); |
| 1407 | |
| 1408 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1409 | if (!TyD) { |
| 1410 | if (R.isAmbiguous()) return true; |
| 1411 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1412 | // We don't want access-control diagnostics here. |
| 1413 | R.suppressDiagnostics(); |
| 1414 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1415 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1416 | bool NotUnknownSpecialization = false; |
| 1417 | DeclContext *DC = computeDeclContext(SS, false); |
| 1418 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1419 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1420 | |
| 1421 | if (!NotUnknownSpecialization) { |
| 1422 | // When the scope specifier can refer to a member of an unknown |
| 1423 | // specialization, we take it as a type name. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 1424 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 1425 | SS.getWithLocInContext(Context), |
| 1426 | *MemberOrBase, IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1427 | if (BaseType.isNull()) |
| 1428 | return true; |
| 1429 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1430 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 1431 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1432 | } |
| 1433 | } |
| 1434 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1435 | // If no results were found, try to correct typos. |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1436 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1437 | CorrectTypo(R, S, &SS, ClassDecl, 0, CTC_NoKeywords) && |
| 1438 | R.isSingleResult()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1439 | if (FieldDecl *Member = R.getAsSingle<FieldDecl>()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1440 | if (Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl)) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1441 | // We have found a non-static data member with a similar |
| 1442 | // name to what was typed; complain and initialize that |
| 1443 | // member. |
| 1444 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1445 | << MemberOrBase << true << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1446 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1447 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1448 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 1449 | << Member->getDeclName(); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1450 | |
| 1451 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 1452 | LParenLoc, RParenLoc); |
| 1453 | } |
| 1454 | } else if (TypeDecl *Type = R.getAsSingle<TypeDecl>()) { |
| 1455 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1456 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1457 | if (FindBaseInitializer(*this, ClassDecl, |
| 1458 | Context.getTypeDeclType(Type), |
| 1459 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1460 | // We have found a direct or virtual base class with a |
| 1461 | // similar name to what was typed; complain and initialize |
| 1462 | // that base class. |
| 1463 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1464 | << MemberOrBase << false << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1465 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1466 | R.getLookupName().getAsString()); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1467 | |
| 1468 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1469 | : VirtualBaseSpec; |
| 1470 | Diag(BaseSpec->getSourceRange().getBegin(), |
| 1471 | diag::note_base_class_specified_here) |
| 1472 | << BaseSpec->getType() |
| 1473 | << BaseSpec->getSourceRange(); |
| 1474 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1475 | TyD = Type; |
| 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1480 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1481 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1482 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1483 | return true; |
| 1484 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1487 | if (BaseType.isNull()) { |
| 1488 | BaseType = Context.getTypeDeclType(TyD); |
| 1489 | if (SS.isSet()) { |
| 1490 | NestedNameSpecifier *Qualifier = |
| 1491 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1492 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1493 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1494 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1495 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1496 | } |
| 1497 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1499 | if (!TInfo) |
| 1500 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1501 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1502 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1503 | LParenLoc, RParenLoc, ClassDecl, EllipsisLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1506 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1507 | /// containing the field that is being initialized. Returns true if there is an |
| 1508 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1509 | /// otherwise. |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1510 | static bool InitExprContainsUninitializedFields(const Stmt *S, |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1511 | const ValueDecl *LhsField, |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1512 | SourceLocation *L) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1513 | assert(isa<FieldDecl>(LhsField) || isa<IndirectFieldDecl>(LhsField)); |
| 1514 | |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1515 | if (isa<CallExpr>(S)) { |
| 1516 | // Do not descend into function calls or constructors, as the use |
| 1517 | // of an uninitialized field may be valid. One would have to inspect |
| 1518 | // the contents of the function/ctor to determine if it is safe or not. |
| 1519 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1520 | // may be safe, depending on what the function/ctor does. |
| 1521 | return false; |
| 1522 | } |
| 1523 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 1524 | const NamedDecl *RhsField = ME->getMemberDecl(); |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1525 | |
| 1526 | if (const VarDecl *VD = dyn_cast<VarDecl>(RhsField)) { |
| 1527 | // The member expression points to a static data member. |
| 1528 | assert(VD->isStaticDataMember() && |
| 1529 | "Member points to non-static data member!"); |
Nick Lewycky | edd5911 | 2010-10-06 18:37:39 +0000 | [diff] [blame] | 1530 | (void)VD; |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1531 | return false; |
| 1532 | } |
| 1533 | |
| 1534 | if (isa<EnumConstantDecl>(RhsField)) { |
| 1535 | // The member expression points to an enum. |
| 1536 | return false; |
| 1537 | } |
| 1538 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1539 | if (RhsField == LhsField) { |
| 1540 | // Initializing a field with itself. Throw a warning. |
| 1541 | // But wait; there are exceptions! |
| 1542 | // Exception #1: The field may not belong to this record. |
| 1543 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1544 | const Expr *base = ME->getBase(); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1545 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1546 | // Even though the field matches, it does not belong to this record. |
| 1547 | return false; |
| 1548 | } |
| 1549 | // None of the exceptions triggered; return true to indicate an |
| 1550 | // uninitialized field was used. |
| 1551 | *L = ME->getMemberLoc(); |
| 1552 | return true; |
| 1553 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1554 | } else if (isa<UnaryExprOrTypeTraitExpr>(S)) { |
Argyrios Kyrtzidis | ff8819b | 2010-09-21 10:47:20 +0000 | [diff] [blame] | 1555 | // sizeof/alignof doesn't reference contents, do not warn. |
| 1556 | return false; |
| 1557 | } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(S)) { |
| 1558 | // address-of doesn't reference contents (the pointer may be dereferenced |
| 1559 | // in the same expression but it would be rare; and weird). |
| 1560 | if (UOE->getOpcode() == UO_AddrOf) |
| 1561 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1562 | } |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1563 | for (Stmt::const_child_range it = S->children(); it; ++it) { |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1564 | if (!*it) { |
| 1565 | // An expression such as 'member(arg ?: "")' may trigger this. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1566 | continue; |
| 1567 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1568 | if (InitExprContainsUninitializedFields(*it, LhsField, L)) |
| 1569 | return true; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1570 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1571 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1574 | MemInitResult |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1575 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr **Args, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1576 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1577 | SourceLocation LParenLoc, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1578 | SourceLocation RParenLoc) { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1579 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 1580 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 1581 | assert((DirectMember || IndirectMember) && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1582 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 1583 | |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 1584 | if (Member->isInvalidDecl()) |
| 1585 | return true; |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1586 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1587 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1588 | // foo(foo) |
| 1589 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1590 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1591 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1592 | SourceLocation L; |
| 1593 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1594 | // FIXME: Return true in the case when other fields are used before being |
| 1595 | // uninitialized. For example, let this field be the i'th field. When |
| 1596 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1597 | // fields are used, as they are not yet initialized. |
| 1598 | // Right now we are only handling the case where the i'th field uses |
| 1599 | // itself in its initializer. |
| 1600 | Diag(L, diag::warn_field_is_uninit); |
| 1601 | } |
| 1602 | } |
| 1603 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1604 | bool HasDependentArg = false; |
| 1605 | for (unsigned i = 0; i < NumArgs; i++) |
| 1606 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1607 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1608 | Expr *Init; |
Eli Friedman | 0f2b97d | 2010-07-24 21:19:15 +0000 | [diff] [blame] | 1609 | if (Member->getType()->isDependentType() || HasDependentArg) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1610 | // Can't check initialization for a member of dependent type or when |
| 1611 | // any of the arguments are type-dependent expressions. |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1612 | Init = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame^] | 1613 | RParenLoc, |
| 1614 | Member->getType().getNonReferenceType()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1615 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1616 | DiscardCleanupsInEvaluationContext(); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1617 | } else { |
| 1618 | // Initialize the member. |
| 1619 | InitializedEntity MemberEntity = |
| 1620 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 1621 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 1622 | InitializationKind Kind = |
| 1623 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1624 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1625 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 1626 | |
| 1627 | ExprResult MemberInit = |
| 1628 | InitSeq.Perform(*this, MemberEntity, Kind, |
| 1629 | MultiExprArg(*this, Args, NumArgs), 0); |
| 1630 | if (MemberInit.isInvalid()) |
| 1631 | return true; |
| 1632 | |
| 1633 | CheckImplicitConversions(MemberInit.get(), LParenLoc); |
| 1634 | |
| 1635 | // C++0x [class.base.init]p7: |
| 1636 | // The initialization of each base and member constitutes a |
| 1637 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1638 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1639 | if (MemberInit.isInvalid()) |
| 1640 | return true; |
| 1641 | |
| 1642 | // If we are in a dependent context, template instantiation will |
| 1643 | // perform this type-checking again. Just save the arguments that we |
| 1644 | // received in a ParenListExpr. |
| 1645 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1646 | // of the information that we have about the member |
| 1647 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1648 | // and this approach is far more likely to get the corner cases right. |
| 1649 | if (CurContext->isDependentContext()) |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame^] | 1650 | Init = new (Context) ParenListExpr( |
| 1651 | Context, LParenLoc, Args, NumArgs, RParenLoc, |
| 1652 | Member->getType().getNonReferenceType()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1653 | else |
| 1654 | Init = MemberInit.get(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1657 | if (DirectMember) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1658 | return new (Context) CXXCtorInitializer(Context, DirectMember, |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1659 | IdLoc, LParenLoc, Init, |
| 1660 | RParenLoc); |
| 1661 | } else { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1662 | return new (Context) CXXCtorInitializer(Context, IndirectMember, |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1663 | IdLoc, LParenLoc, Init, |
| 1664 | RParenLoc); |
| 1665 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1668 | MemInitResult |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1669 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, |
| 1670 | Expr **Args, unsigned NumArgs, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1671 | SourceLocation NameLoc, |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1672 | SourceLocation LParenLoc, |
| 1673 | SourceLocation RParenLoc, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1674 | CXXRecordDecl *ClassDecl) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1675 | SourceLocation Loc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1676 | if (!LangOpts.CPlusPlus0x) |
| 1677 | return Diag(Loc, diag::err_delegation_0x_only) |
| 1678 | << TInfo->getTypeLoc().getLocalSourceRange(); |
Sebastian Redl | f9c32eb | 2011-03-12 13:53:51 +0000 | [diff] [blame] | 1679 | |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1680 | // Initialize the object. |
| 1681 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 1682 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 1683 | InitializationKind Kind = |
| 1684 | InitializationKind::CreateDirect(NameLoc, LParenLoc, RParenLoc); |
| 1685 | |
| 1686 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args, NumArgs); |
| 1687 | |
| 1688 | ExprResult DelegationInit = |
| 1689 | InitSeq.Perform(*this, DelegationEntity, Kind, |
| 1690 | MultiExprArg(*this, Args, NumArgs), 0); |
| 1691 | if (DelegationInit.isInvalid()) |
| 1692 | return true; |
| 1693 | |
| 1694 | CXXConstructExpr *ConExpr = cast<CXXConstructExpr>(DelegationInit.get()); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 1695 | CXXConstructorDecl *Constructor |
| 1696 | = ConExpr->getConstructor(); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1697 | assert(Constructor && "Delegating constructor with no target?"); |
| 1698 | |
| 1699 | CheckImplicitConversions(DelegationInit.get(), LParenLoc); |
| 1700 | |
| 1701 | // C++0x [class.base.init]p7: |
| 1702 | // The initialization of each base and member constitutes a |
| 1703 | // full-expression. |
| 1704 | DelegationInit = MaybeCreateExprWithCleanups(DelegationInit); |
| 1705 | if (DelegationInit.isInvalid()) |
| 1706 | return true; |
| 1707 | |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame^] | 1708 | assert(!CurContext->isDependentContext()); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1709 | return new (Context) CXXCtorInitializer(Context, Loc, LParenLoc, Constructor, |
| 1710 | DelegationInit.takeAs<Expr>(), |
| 1711 | RParenLoc); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1715 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1716 | Expr **Args, unsigned NumArgs, |
| 1717 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1718 | CXXRecordDecl *ClassDecl, |
| 1719 | SourceLocation EllipsisLoc) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1720 | bool HasDependentArg = false; |
| 1721 | for (unsigned i = 0; i < NumArgs; i++) |
| 1722 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1723 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1724 | SourceLocation BaseLoc |
| 1725 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1726 | |
| 1727 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 1728 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 1729 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1730 | |
| 1731 | // C++ [class.base.init]p2: |
| 1732 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1733 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1734 | // of that class, the mem-initializer is ill-formed. A |
| 1735 | // mem-initializer-list can initialize a base class using any |
| 1736 | // name that denotes that base class type. |
| 1737 | bool Dependent = BaseType->isDependentType() || HasDependentArg; |
| 1738 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1739 | if (EllipsisLoc.isValid()) { |
| 1740 | // This is a pack expansion. |
| 1741 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 1742 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1743 | << SourceRange(BaseLoc, RParenLoc); |
| 1744 | |
| 1745 | EllipsisLoc = SourceLocation(); |
| 1746 | } |
| 1747 | } else { |
| 1748 | // Check for any unexpanded parameter packs. |
| 1749 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 1750 | return true; |
| 1751 | |
| 1752 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1753 | if (DiagnoseUnexpandedParameterPack(Args[I])) |
| 1754 | return true; |
| 1755 | } |
| 1756 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1757 | // Check for direct and virtual base classes. |
| 1758 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1759 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1760 | if (!Dependent) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1761 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 1762 | BaseType)) |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1763 | return BuildDelegatingInitializer(BaseTInfo, Args, NumArgs, BaseLoc, |
| 1764 | LParenLoc, RParenLoc, ClassDecl); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1765 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1766 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 1767 | VirtualBaseSpec); |
| 1768 | |
| 1769 | // C++ [base.class.init]p2: |
| 1770 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 1771 | // constructor's class or a direct or virtual base of that class, the |
| 1772 | // mem-initializer is ill-formed. |
| 1773 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 1774 | // If the class has any dependent bases, then it's possible that |
| 1775 | // one of those types will resolve to the same type as |
| 1776 | // BaseType. Therefore, just treat this as a dependent base |
| 1777 | // class initialization. FIXME: Should we try to check the |
| 1778 | // initialization anyway? It seems odd. |
| 1779 | if (ClassDecl->hasAnyDependentBases()) |
| 1780 | Dependent = true; |
| 1781 | else |
| 1782 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 1783 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 1784 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | if (Dependent) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1789 | // Can't check initialization for a base of dependent type or when |
| 1790 | // any of the arguments are type-dependent expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1791 | ExprResult BaseInit |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1792 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame^] | 1793 | RParenLoc, BaseType)); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1794 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1795 | DiscardCleanupsInEvaluationContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1796 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1797 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1798 | /*IsVirtual=*/false, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1799 | LParenLoc, |
| 1800 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1801 | RParenLoc, |
| 1802 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1803 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1804 | |
| 1805 | // C++ [base.class.init]p2: |
| 1806 | // If a mem-initializer-id is ambiguous because it designates both |
| 1807 | // a direct non-virtual base class and an inherited virtual base |
| 1808 | // class, the mem-initializer is ill-formed. |
| 1809 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1810 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1811 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1812 | |
| 1813 | CXXBaseSpecifier *BaseSpec |
| 1814 | = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
| 1815 | if (!BaseSpec) |
| 1816 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 1817 | |
| 1818 | // Initialize the base. |
| 1819 | InitializedEntity BaseEntity = |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1820 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1821 | InitializationKind Kind = |
| 1822 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc); |
| 1823 | |
| 1824 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 1825 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1826 | ExprResult BaseInit = |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1827 | InitSeq.Perform(*this, BaseEntity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1828 | MultiExprArg(*this, Args, NumArgs), 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1829 | if (BaseInit.isInvalid()) |
| 1830 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1831 | |
| 1832 | CheckImplicitConversions(BaseInit.get(), LParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1833 | |
| 1834 | // C++0x [class.base.init]p7: |
| 1835 | // The initialization of each base and member constitutes a |
| 1836 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1837 | BaseInit = MaybeCreateExprWithCleanups(BaseInit); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1838 | if (BaseInit.isInvalid()) |
| 1839 | return true; |
| 1840 | |
| 1841 | // If we are in a dependent context, template instantiation will |
| 1842 | // perform this type-checking again. Just save the arguments that we |
| 1843 | // received in a ParenListExpr. |
| 1844 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1845 | // of the information that we have about the base |
| 1846 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1847 | // and this approach is far more likely to get the corner cases right. |
| 1848 | if (CurContext->isDependentContext()) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1849 | ExprResult Init |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1850 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame^] | 1851 | RParenLoc, BaseType)); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1852 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1853 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1854 | LParenLoc, |
| 1855 | Init.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1856 | RParenLoc, |
| 1857 | EllipsisLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1860 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1861 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1862 | LParenLoc, |
| 1863 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1864 | RParenLoc, |
| 1865 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1868 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 1869 | /// initialize its base or member. |
| 1870 | enum ImplicitInitializerKind { |
| 1871 | IIK_Default, |
| 1872 | IIK_Copy, |
| 1873 | IIK_Move |
| 1874 | }; |
| 1875 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1876 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1877 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1878 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1879 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1880 | bool IsInheritedVirtualBase, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1881 | CXXCtorInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1882 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1883 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 1884 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1885 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1886 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1887 | |
| 1888 | switch (ImplicitInitKind) { |
| 1889 | case IIK_Default: { |
| 1890 | InitializationKind InitKind |
| 1891 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 1892 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1893 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1894 | MultiExprArg(SemaRef, 0, 0)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1895 | break; |
| 1896 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1897 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1898 | case IIK_Copy: { |
| 1899 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1900 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1901 | |
| 1902 | Expr *CopyCtorArg = |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1903 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), Param, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1904 | Constructor->getLocation(), ParamType, |
| 1905 | VK_LValue, 0); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1906 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1907 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 1908 | QualType ArgTy = |
| 1909 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 1910 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1911 | |
| 1912 | CXXCastPath BasePath; |
| 1913 | BasePath.push_back(BaseSpec); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1914 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 1915 | CK_UncheckedDerivedToBase, |
| 1916 | VK_LValue, &BasePath).take(); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1917 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1918 | InitializationKind InitKind |
| 1919 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1920 | SourceLocation(), SourceLocation()); |
| 1921 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1922 | &CopyCtorArg, 1); |
| 1923 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1924 | MultiExprArg(&CopyCtorArg, 1)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1925 | break; |
| 1926 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1927 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1928 | case IIK_Move: |
| 1929 | assert(false && "Unhandled initializer kind!"); |
| 1930 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1931 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1932 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1933 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1934 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1935 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1936 | CXXBaseInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1937 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1938 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 1939 | SourceLocation()), |
| 1940 | BaseSpec->isVirtual(), |
| 1941 | SourceLocation(), |
| 1942 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1943 | SourceLocation(), |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1944 | SourceLocation()); |
| 1945 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1946 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1949 | static bool |
| 1950 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1951 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1952 | FieldDecl *Field, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1953 | CXXCtorInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 1954 | if (Field->isInvalidDecl()) |
| 1955 | return true; |
| 1956 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1957 | SourceLocation Loc = Constructor->getLocation(); |
| 1958 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1959 | if (ImplicitInitKind == IIK_Copy) { |
| 1960 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1961 | QualType ParamType = Param->getType().getNonReferenceType(); |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 1962 | |
| 1963 | // Suppress copying zero-width bitfields. |
| 1964 | if (const Expr *Width = Field->getBitWidth()) |
| 1965 | if (Width->EvaluateAsInt(SemaRef.Context) == 0) |
| 1966 | return false; |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1967 | |
| 1968 | Expr *MemberExprBase = |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1969 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), Param, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1970 | Loc, ParamType, VK_LValue, 0); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1971 | |
| 1972 | // Build a reference to this field within the parameter. |
| 1973 | CXXScopeSpec SS; |
| 1974 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 1975 | Sema::LookupMemberName); |
| 1976 | MemberLookup.addDecl(Field, AS_public); |
| 1977 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1978 | ExprResult CopyCtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1979 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1980 | ParamType, Loc, |
| 1981 | /*IsArrow=*/false, |
| 1982 | SS, |
| 1983 | /*FirstQualifierInScope=*/0, |
| 1984 | MemberLookup, |
| 1985 | /*TemplateArgs=*/0); |
| 1986 | if (CopyCtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1987 | return true; |
| 1988 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1989 | // When the field we are copying is an array, create index variables for |
| 1990 | // each dimension of the array. We use these index variables to subscript |
| 1991 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 1992 | // necessary iteration with these index variables. |
| 1993 | llvm::SmallVector<VarDecl *, 4> IndexVariables; |
| 1994 | QualType BaseType = Field->getType(); |
| 1995 | QualType SizeType = SemaRef.Context.getSizeType(); |
| 1996 | while (const ConstantArrayType *Array |
| 1997 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
| 1998 | // Create the iteration variable for this array index. |
| 1999 | IdentifierInfo *IterationVarName = 0; |
| 2000 | { |
| 2001 | llvm::SmallString<8> Str; |
| 2002 | llvm::raw_svector_ostream OS(Str); |
| 2003 | OS << "__i" << IndexVariables.size(); |
| 2004 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 2005 | } |
| 2006 | VarDecl *IterationVar |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2007 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2008 | IterationVarName, SizeType, |
| 2009 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2010 | SC_None, SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2011 | IndexVariables.push_back(IterationVar); |
| 2012 | |
| 2013 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2014 | ExprResult IterationVarRef |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2015 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_RValue, Loc); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2016 | assert(!IterationVarRef.isInvalid() && |
| 2017 | "Reference to invented variable cannot fail!"); |
| 2018 | |
| 2019 | // Subscript the array with this iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2020 | CopyCtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CopyCtorArg.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2021 | Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2022 | IterationVarRef.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2023 | Loc); |
| 2024 | if (CopyCtorArg.isInvalid()) |
| 2025 | return true; |
| 2026 | |
| 2027 | BaseType = Array->getElementType(); |
| 2028 | } |
| 2029 | |
| 2030 | // Construct the entity that we will be initializing. For an array, this |
| 2031 | // will be first element in the array, which may require several levels |
| 2032 | // of array-subscript entities. |
| 2033 | llvm::SmallVector<InitializedEntity, 4> Entities; |
| 2034 | Entities.reserve(1 + IndexVariables.size()); |
| 2035 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
| 2036 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 2037 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 2038 | 0, |
| 2039 | Entities.back())); |
| 2040 | |
| 2041 | // Direct-initialize to use the copy constructor. |
| 2042 | InitializationKind InitKind = |
| 2043 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 2044 | |
| 2045 | Expr *CopyCtorArgE = CopyCtorArg.takeAs<Expr>(); |
| 2046 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
| 2047 | &CopyCtorArgE, 1); |
| 2048 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2049 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2050 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2051 | MultiExprArg(&CopyCtorArgE, 1)); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2052 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2053 | if (MemberInit.isInvalid()) |
| 2054 | return true; |
| 2055 | |
| 2056 | CXXMemberInit |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2057 | = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2058 | MemberInit.takeAs<Expr>(), Loc, |
| 2059 | IndexVariables.data(), |
| 2060 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2061 | return false; |
| 2062 | } |
| 2063 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2064 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 2065 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2066 | QualType FieldBaseElementType = |
| 2067 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 2068 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2069 | if (FieldBaseElementType->isRecordType()) { |
| 2070 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2071 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2072 | InitializationKind::CreateDefault(Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2073 | |
| 2074 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2075 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2076 | InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2077 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2078 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2079 | if (MemberInit.isInvalid()) |
| 2080 | return true; |
| 2081 | |
| 2082 | CXXMemberInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2083 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2084 | Field, Loc, Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2085 | MemberInit.get(), |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2086 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2087 | return false; |
| 2088 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2089 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 2090 | if (!Field->getParent()->isUnion()) { |
| 2091 | if (FieldBaseElementType->isReferenceType()) { |
| 2092 | SemaRef.Diag(Constructor->getLocation(), |
| 2093 | diag::err_uninitialized_member_in_ctor) |
| 2094 | << (int)Constructor->isImplicit() |
| 2095 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 2096 | << 0 << Field->getDeclName(); |
| 2097 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 2098 | return true; |
| 2099 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2100 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 2101 | if (FieldBaseElementType.isConstQualified()) { |
| 2102 | SemaRef.Diag(Constructor->getLocation(), |
| 2103 | diag::err_uninitialized_member_in_ctor) |
| 2104 | << (int)Constructor->isImplicit() |
| 2105 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 2106 | << 1 << Field->getDeclName(); |
| 2107 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 2108 | return true; |
| 2109 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 2110 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2111 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2112 | if (SemaRef.getLangOptions().ObjCAutoRefCount && |
| 2113 | FieldBaseElementType->isObjCRetainableType() && |
| 2114 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_None && |
| 2115 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) { |
| 2116 | // Instant objects: |
| 2117 | // Default-initialize Objective-C pointers to NULL. |
| 2118 | CXXMemberInit |
| 2119 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 2120 | Loc, Loc, |
| 2121 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()), |
| 2122 | Loc); |
| 2123 | return false; |
| 2124 | } |
| 2125 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2126 | // Nothing to initialize. |
| 2127 | CXXMemberInit = 0; |
| 2128 | return false; |
| 2129 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2130 | |
| 2131 | namespace { |
| 2132 | struct BaseAndFieldInfo { |
| 2133 | Sema &S; |
| 2134 | CXXConstructorDecl *Ctor; |
| 2135 | bool AnyErrorsInInits; |
| 2136 | ImplicitInitializerKind IIK; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2137 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
| 2138 | llvm::SmallVector<CXXCtorInitializer*, 8> AllToInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2139 | |
| 2140 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 2141 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
| 2142 | // FIXME: Handle implicit move constructors. |
| 2143 | if (Ctor->isImplicit() && Ctor->isCopyConstructor()) |
| 2144 | IIK = IIK_Copy; |
| 2145 | else |
| 2146 | IIK = IIK_Default; |
| 2147 | } |
| 2148 | }; |
| 2149 | } |
| 2150 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2151 | static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2152 | FieldDecl *Top, FieldDecl *Field) { |
| 2153 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2154 | // Overwhelmingly common case: we have a direct initializer for this field. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2155 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(Field)) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2156 | Info.AllToInit.push_back(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2157 | return false; |
| 2158 | } |
| 2159 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2160 | // C++0x [class.base.init]p8: if the entity is a non-static data member that |
| 2161 | // has a brace-or-equal-initializer, the entity is initialized as specified |
| 2162 | // in [dcl.init]. |
| 2163 | if (Field->hasInClassInitializer()) { |
| 2164 | Info.AllToInit.push_back( |
| 2165 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 2166 | SourceLocation(), |
| 2167 | SourceLocation(), 0, |
| 2168 | SourceLocation())); |
| 2169 | return false; |
| 2170 | } |
| 2171 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2172 | if (Info.IIK == IIK_Default && Field->isAnonymousStructOrUnion()) { |
| 2173 | const RecordType *FieldClassType = Field->getType()->getAs<RecordType>(); |
| 2174 | assert(FieldClassType && "anonymous struct/union without record type"); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2175 | CXXRecordDecl *FieldClassDecl |
| 2176 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2177 | |
| 2178 | // Even though union members never have non-trivial default |
| 2179 | // constructions in C++03, we still build member initializers for aggregate |
| 2180 | // record types which can be union members, and C++0x allows non-trivial |
| 2181 | // default constructors for union members, so we ensure that only one |
| 2182 | // member is initialized for these. |
| 2183 | if (FieldClassDecl->isUnion()) { |
| 2184 | // First check for an explicit initializer for one field. |
| 2185 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 2186 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2187 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(*FA)) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2188 | Info.AllToInit.push_back(Init); |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2189 | |
| 2190 | // Once we've initialized a field of an anonymous union, the union |
| 2191 | // field in the class is also initialized, so exit immediately. |
| 2192 | return false; |
Argyrios Kyrtzidis | 881b36c | 2010-08-16 17:27:13 +0000 | [diff] [blame] | 2193 | } else if ((*FA)->isAnonymousStructOrUnion()) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2194 | if (CollectFieldInitializer(SemaRef, Info, Top, *FA)) |
Argyrios Kyrtzidis | 881b36c | 2010-08-16 17:27:13 +0000 | [diff] [blame] | 2195 | return true; |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | // Fallthrough and construct a default initializer for the union as |
| 2200 | // a whole, which can call its default constructor if such a thing exists |
| 2201 | // (C++0x perhaps). FIXME: It's not clear that this is the correct |
| 2202 | // behavior going forward with C++0x, when anonymous unions there are |
| 2203 | // finalized, we should revisit this. |
| 2204 | } else { |
| 2205 | // For structs, we simply descend through to initialize all members where |
| 2206 | // necessary. |
| 2207 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 2208 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2209 | if (CollectFieldInitializer(SemaRef, Info, Top, *FA)) |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2210 | return true; |
| 2211 | } |
| 2212 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | // Don't try to build an implicit initializer if there were semantic |
| 2216 | // errors in any of the initializers (and therefore we might be |
| 2217 | // missing some that the user actually wrote). |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2218 | if (Info.AnyErrorsInInits || Field->isInvalidDecl()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2219 | return false; |
| 2220 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2221 | CXXCtorInitializer *Init = 0; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2222 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, Init)) |
| 2223 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2224 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2225 | if (Init) |
| 2226 | Info.AllToInit.push_back(Init); |
| 2227 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2228 | return false; |
| 2229 | } |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2230 | |
| 2231 | bool |
| 2232 | Sema::SetDelegatingInitializer(CXXConstructorDecl *Constructor, |
| 2233 | CXXCtorInitializer *Initializer) { |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 2234 | assert(Initializer->isDelegatingInitializer()); |
Sean Hunt | 01aacc0 | 2011-05-03 20:43:02 +0000 | [diff] [blame] | 2235 | Constructor->setNumCtorInitializers(1); |
| 2236 | CXXCtorInitializer **initializer = |
| 2237 | new (Context) CXXCtorInitializer*[1]; |
| 2238 | memcpy(initializer, &Initializer, sizeof (CXXCtorInitializer*)); |
| 2239 | Constructor->setCtorInitializers(initializer); |
| 2240 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2241 | if (CXXDestructorDecl *Dtor = LookupDestructor(Constructor->getParent())) { |
| 2242 | MarkDeclarationReferenced(Initializer->getSourceLocation(), Dtor); |
| 2243 | DiagnoseUseOfDecl(Dtor, Initializer->getSourceLocation()); |
| 2244 | } |
| 2245 | |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 2246 | DelegatingCtorDecls.push_back(Constructor); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 2247 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2248 | return false; |
| 2249 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2250 | |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 2251 | bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, |
| 2252 | CXXCtorInitializer **Initializers, |
| 2253 | unsigned NumInitializers, |
| 2254 | bool AnyErrors) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2255 | if (Constructor->getDeclContext()->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2256 | // Just store the initializers as written, they will be checked during |
| 2257 | // instantiation. |
| 2258 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2259 | Constructor->setNumCtorInitializers(NumInitializers); |
| 2260 | CXXCtorInitializer **baseOrMemberInitializers = |
| 2261 | new (Context) CXXCtorInitializer*[NumInitializers]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2262 | memcpy(baseOrMemberInitializers, Initializers, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2263 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 2264 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | return false; |
| 2268 | } |
| 2269 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2270 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2271 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2272 | // We need to build the initializer AST according to order of construction |
| 2273 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2274 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 2275 | if (!ClassDecl) |
| 2276 | return true; |
| 2277 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2278 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2279 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2280 | for (unsigned i = 0; i < NumInitializers; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2281 | CXXCtorInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2282 | |
| 2283 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2284 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2285 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2286 | Info.AllBaseFields[Member->getAnyMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2289 | // Keep track of the direct virtual bases. |
| 2290 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 2291 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 2292 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 2293 | if (I->isVirtual()) |
| 2294 | DirectVBases.insert(I); |
| 2295 | } |
| 2296 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2297 | // Push virtual bases before others. |
| 2298 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2299 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 2300 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2301 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2302 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 2303 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2304 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2305 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2306 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2307 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2308 | VBase, IsInheritedVirtualBase, |
| 2309 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2310 | HadError = true; |
| 2311 | continue; |
| 2312 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2313 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2314 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2315 | } |
| 2316 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2317 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2318 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2319 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2320 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 2321 | // Virtuals are in the virtual base list and already constructed. |
| 2322 | if (Base->isVirtual()) |
| 2323 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2325 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2326 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 2327 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2328 | } else if (!AnyErrors) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2329 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2330 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2331 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2332 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2333 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2334 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2335 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 2336 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2337 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2338 | } |
| 2339 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2341 | // Fields. |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2342 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 2343 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 2344 | if ((*Field)->getType()->isIncompleteArrayType()) { |
| 2345 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 2346 | "Incomplete array type is not valid"); |
| 2347 | continue; |
| 2348 | } |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2349 | if (CollectFieldInitializer(*this, Info, *Field, *Field)) |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2350 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 2351 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2352 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2353 | NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2354 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2355 | Constructor->setNumCtorInitializers(NumInitializers); |
| 2356 | CXXCtorInitializer **baseOrMemberInitializers = |
| 2357 | new (Context) CXXCtorInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2358 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2359 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 2360 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 2361 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2362 | // Constructors implicitly reference the base and member |
| 2363 | // destructors. |
| 2364 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 2365 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2366 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2367 | |
| 2368 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2371 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 2372 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2373 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2374 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 2375 | return static_cast<void *>(RT->getDecl()); |
| 2376 | } |
| 2377 | return static_cast<void *>(Field); |
| 2378 | } |
| 2379 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2380 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2381 | return const_cast<Type*>(Context.getCanonicalType(BaseType).getTypePtr()); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2384 | static void *GetKeyForMember(ASTContext &Context, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2385 | CXXCtorInitializer *Member) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2386 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2387 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 2388 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2389 | // For fields injected into the class via declaration of an anonymous union, |
| 2390 | // use its anonymous union class declaration as the unique key. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2391 | FieldDecl *Field = Member->getAnyMember(); |
| 2392 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2393 | // If the field is a member of an anonymous struct or union, our key |
| 2394 | // 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] | 2395 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2396 | if (RD->isAnonymousStructOrUnion()) { |
| 2397 | while (true) { |
| 2398 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 2399 | if (Parent->isAnonymousStructOrUnion()) |
| 2400 | RD = Parent; |
| 2401 | else |
| 2402 | break; |
| 2403 | } |
| 2404 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 2405 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2406 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2407 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 2408 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2411 | static void |
| 2412 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2413 | const CXXConstructorDecl *Constructor, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2414 | CXXCtorInitializer **Inits, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2415 | unsigned NumInits) { |
| 2416 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 2417 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 2419 | // Don't check initializers order unless the warning is enabled at the |
| 2420 | // location of at least one initializer. |
| 2421 | bool ShouldCheckOrder = false; |
| 2422 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2423 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 2424 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order, |
| 2425 | Init->getSourceLocation()) |
| 2426 | != Diagnostic::Ignored) { |
| 2427 | ShouldCheckOrder = true; |
| 2428 | break; |
| 2429 | } |
| 2430 | } |
| 2431 | if (!ShouldCheckOrder) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2432 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2433 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2434 | // Build the list of bases and members in the order that they'll |
| 2435 | // actually be initialized. The explicit initializers should be in |
| 2436 | // this same order but may be missing things. |
| 2437 | llvm::SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2438 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2439 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2440 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2441 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2442 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2443 | ClassDecl->vbases_begin(), |
| 2444 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2445 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2446 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2447 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2448 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2449 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2450 | if (Base->isVirtual()) |
| 2451 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2452 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2453 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2454 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2455 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2456 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2457 | E = ClassDecl->field_end(); Field != E; ++Field) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2458 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2459 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2460 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 2461 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2462 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2463 | CXXCtorInitializer *PrevInit = 0; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2464 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2465 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2466 | void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2467 | |
| 2468 | // Scan forward to try to find this initializer in the idealized |
| 2469 | // initializers list. |
| 2470 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2471 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2472 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2473 | |
| 2474 | // If we didn't find this initializer, it must be because we |
| 2475 | // scanned past it on a previous iteration. That can only |
| 2476 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 2477 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2478 | Sema::SemaDiagnosticBuilder D = |
| 2479 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 2480 | diag::warn_initializer_out_of_order); |
| 2481 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2482 | if (PrevInit->isAnyMemberInitializer()) |
| 2483 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2484 | else |
| 2485 | D << 1 << PrevInit->getBaseClassInfo()->getType(); |
| 2486 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2487 | if (Init->isAnyMemberInitializer()) |
| 2488 | D << 0 << Init->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2489 | else |
| 2490 | D << 1 << Init->getBaseClassInfo()->getType(); |
| 2491 | |
| 2492 | // Move back to the initializer's location in the ideal list. |
| 2493 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2494 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2495 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2496 | |
| 2497 | assert(IdealIndex != NumIdealInits && |
| 2498 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2499 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2500 | |
| 2501 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2502 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2505 | namespace { |
| 2506 | bool CheckRedundantInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2507 | CXXCtorInitializer *Init, |
| 2508 | CXXCtorInitializer *&PrevInit) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2509 | if (!PrevInit) { |
| 2510 | PrevInit = Init; |
| 2511 | return false; |
| 2512 | } |
| 2513 | |
| 2514 | if (FieldDecl *Field = Init->getMember()) |
| 2515 | S.Diag(Init->getSourceLocation(), |
| 2516 | diag::err_multiple_mem_initialization) |
| 2517 | << Field->getDeclName() |
| 2518 | << Init->getSourceRange(); |
| 2519 | else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2520 | const Type *BaseClass = Init->getBaseClass(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2521 | assert(BaseClass && "neither field nor base"); |
| 2522 | S.Diag(Init->getSourceLocation(), |
| 2523 | diag::err_multiple_base_initialization) |
| 2524 | << QualType(BaseClass, 0) |
| 2525 | << Init->getSourceRange(); |
| 2526 | } |
| 2527 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 2528 | << 0 << PrevInit->getSourceRange(); |
| 2529 | |
| 2530 | return true; |
| 2531 | } |
| 2532 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2533 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2534 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 2535 | |
| 2536 | bool CheckRedundantUnionInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2537 | CXXCtorInitializer *Init, |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2538 | RedundantUnionMap &Unions) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2539 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2540 | RecordDecl *Parent = Field->getParent(); |
| 2541 | if (!Parent->isAnonymousStructOrUnion()) |
| 2542 | return false; |
| 2543 | |
| 2544 | NamedDecl *Child = Field; |
| 2545 | do { |
| 2546 | if (Parent->isUnion()) { |
| 2547 | UnionEntry &En = Unions[Parent]; |
| 2548 | if (En.first && En.first != Child) { |
| 2549 | S.Diag(Init->getSourceLocation(), |
| 2550 | diag::err_multiple_mem_union_initialization) |
| 2551 | << Field->getDeclName() |
| 2552 | << Init->getSourceRange(); |
| 2553 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 2554 | << 0 << En.second->getSourceRange(); |
| 2555 | return true; |
| 2556 | } else if (!En.first) { |
| 2557 | En.first = Child; |
| 2558 | En.second = Init; |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | Child = Parent; |
| 2563 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 2564 | } while (Parent->isAnonymousStructOrUnion()); |
| 2565 | |
| 2566 | return false; |
| 2567 | } |
| 2568 | } |
| 2569 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2570 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2571 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2572 | SourceLocation ColonLoc, |
| 2573 | MemInitTy **meminits, unsigned NumMemInits, |
| 2574 | bool AnyErrors) { |
| 2575 | if (!ConstructorDecl) |
| 2576 | return; |
| 2577 | |
| 2578 | AdjustDeclIfTemplate(ConstructorDecl); |
| 2579 | |
| 2580 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2581 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2582 | |
| 2583 | if (!Constructor) { |
| 2584 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 2585 | return; |
| 2586 | } |
| 2587 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2588 | CXXCtorInitializer **MemInits = |
| 2589 | reinterpret_cast<CXXCtorInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2590 | |
| 2591 | // Mapping for the duplicate initializers check. |
| 2592 | // For member initializers, this is keyed with a FieldDecl*. |
| 2593 | // For base initializers, this is keyed with a Type*. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2594 | llvm::DenseMap<void*, CXXCtorInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2595 | |
| 2596 | // Mapping for the inconsistent anonymous-union initializers check. |
| 2597 | RedundantUnionMap MemberUnions; |
| 2598 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2599 | bool HadError = false; |
| 2600 | for (unsigned i = 0; i < NumMemInits; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2601 | CXXCtorInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2602 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 2603 | // Set the source order index. |
| 2604 | Init->setSourceOrder(i); |
| 2605 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2606 | if (Init->isAnyMemberInitializer()) { |
| 2607 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2608 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 2609 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 2610 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2611 | } else if (Init->isBaseInitializer()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2612 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 2613 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 2614 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2615 | } else { |
| 2616 | assert(Init->isDelegatingInitializer()); |
| 2617 | // This must be the only initializer |
| 2618 | if (i != 0 || NumMemInits > 1) { |
| 2619 | Diag(MemInits[0]->getSourceLocation(), |
| 2620 | diag::err_delegating_initializer_alone) |
| 2621 | << MemInits[0]->getSourceRange(); |
| 2622 | HadError = true; |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2623 | // We will treat this as being the only initializer. |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2624 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 2625 | SetDelegatingInitializer(Constructor, MemInits[i]); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2626 | // Return immediately as the initializer is set. |
| 2627 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2628 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2631 | if (HadError) |
| 2632 | return; |
| 2633 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2634 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2635 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2636 | SetCtorInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2639 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2640 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 2641 | CXXRecordDecl *ClassDecl) { |
| 2642 | // Ignore dependent contexts. |
| 2643 | if (ClassDecl->isDependentContext()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2644 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2645 | |
| 2646 | // FIXME: all the access-control diagnostics are positioned on the |
| 2647 | // field/base declaration. That's probably good; that said, the |
| 2648 | // user might reasonably want to know why the destructor is being |
| 2649 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2650 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2651 | // Non-static data members. |
| 2652 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 2653 | E = ClassDecl->field_end(); I != E; ++I) { |
| 2654 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 2655 | if (Field->isInvalidDecl()) |
| 2656 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2657 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 2658 | |
| 2659 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 2660 | if (!RT) |
| 2661 | continue; |
| 2662 | |
| 2663 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2664 | if (FieldClassDecl->isInvalidDecl()) |
| 2665 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2666 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2667 | continue; |
| 2668 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2669 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2670 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2671 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2672 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2673 | << Field->getDeclName() |
| 2674 | << FieldType); |
| 2675 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2676 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2679 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 2680 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2681 | // Bases. |
| 2682 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2683 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2684 | // Bases are always records in a well-formed non-dependent class. |
| 2685 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 2686 | |
| 2687 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2688 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2689 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2690 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2691 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2692 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 2693 | if (BaseClassDecl->isInvalidDecl()) |
| 2694 | continue; |
| 2695 | // Ignore trivial destructors. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2696 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2697 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2698 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2699 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2700 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2701 | |
| 2702 | // FIXME: caret should be on the start of the class name |
| 2703 | CheckDestructorAccess(Base->getSourceRange().getBegin(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2704 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2705 | << Base->getType() |
| 2706 | << Base->getSourceRange()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2707 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2708 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2712 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2713 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2714 | |
| 2715 | // Bases are always records in a well-formed non-dependent class. |
| 2716 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 2717 | |
| 2718 | // Ignore direct virtual bases. |
| 2719 | if (DirectVirtualBases.count(RT)) |
| 2720 | continue; |
| 2721 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2722 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2723 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 2724 | if (BaseClassDecl->isInvalidDecl()) |
| 2725 | continue; |
| 2726 | // Ignore trivial destructors. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2727 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2728 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2729 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2730 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2731 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2732 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2733 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2734 | << VBase->getType()); |
| 2735 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2736 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2737 | } |
| 2738 | } |
| 2739 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2740 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2741 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2742 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2743 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2744 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2745 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2746 | SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2749 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2750 | unsigned DiagID, AbstractDiagSelID SelID) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2751 | if (SelID == -1) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2752 | return RequireNonAbstractType(Loc, T, PDiag(DiagID)); |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2753 | else |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2754 | return RequireNonAbstractType(Loc, T, PDiag(DiagID) << SelID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2757 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2758 | const PartialDiagnostic &PD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2759 | if (!getLangOptions().CPlusPlus) |
| 2760 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2761 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 2762 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2763 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2765 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2766 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2767 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2768 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2770 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2771 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2772 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2773 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2774 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2775 | if (!RT) |
| 2776 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2777 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2778 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2779 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2780 | // We can't answer whether something is abstract until it has a |
| 2781 | // definition. If it's currently being defined, we'll walk back |
| 2782 | // over all the declarations when we have a full definition. |
| 2783 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 2784 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2785 | return false; |
| 2786 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2787 | if (!RD->isAbstract()) |
| 2788 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2790 | Diag(Loc, PD) << RD->getDeclName(); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2791 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2793 | return true; |
| 2794 | } |
| 2795 | |
| 2796 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 2797 | // Check if we've already emitted the list of pure virtual functions |
| 2798 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2799 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2800 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2801 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2802 | CXXFinalOverriderMap FinalOverriders; |
| 2803 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2805 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 2806 | // more than once. |
| 2807 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 2808 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2809 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2810 | MEnd = FinalOverriders.end(); |
| 2811 | M != MEnd; |
| 2812 | ++M) { |
| 2813 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2814 | SOEnd = M->second.end(); |
| 2815 | SO != SOEnd; ++SO) { |
| 2816 | // C++ [class.abstract]p4: |
| 2817 | // A class is abstract if it contains or inherits at least one |
| 2818 | // pure virtual function for which the final overrider is pure |
| 2819 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2820 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2821 | // |
| 2822 | if (SO->second.size() != 1) |
| 2823 | continue; |
| 2824 | |
| 2825 | if (!SO->second.front().Method->isPure()) |
| 2826 | continue; |
| 2827 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2828 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 2829 | continue; |
| 2830 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2831 | Diag(SO->second.front().Method->getLocation(), |
| 2832 | diag::note_pure_virtual_function) |
Chandler Carruth | 45f11b7 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 2833 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2834 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | if (!PureVirtualClassDiagSet) |
| 2838 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 2839 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2842 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2843 | struct AbstractUsageInfo { |
| 2844 | Sema &S; |
| 2845 | CXXRecordDecl *Record; |
| 2846 | CanQualType AbstractType; |
| 2847 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2849 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 2850 | : S(S), Record(Record), |
| 2851 | AbstractType(S.Context.getCanonicalType( |
| 2852 | S.Context.getTypeDeclType(Record))), |
| 2853 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2854 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2855 | void DiagnoseAbstractType() { |
| 2856 | if (Invalid) return; |
| 2857 | S.DiagnoseAbstractType(Record); |
| 2858 | Invalid = true; |
| 2859 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2860 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2861 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 2862 | }; |
| 2863 | |
| 2864 | struct CheckAbstractUsage { |
| 2865 | AbstractUsageInfo &Info; |
| 2866 | const NamedDecl *Ctx; |
| 2867 | |
| 2868 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 2869 | : Info(Info), Ctx(Ctx) {} |
| 2870 | |
| 2871 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2872 | switch (TL.getTypeLocClass()) { |
| 2873 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 2874 | #define TYPELOC(CLASS, PARENT) \ |
| 2875 | case TypeLoc::CLASS: Check(cast<CLASS##TypeLoc>(TL), Sel); break; |
| 2876 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2877 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2878 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2879 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2880 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2881 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 2882 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
Douglas Gregor | 7019186 | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 2883 | if (!TL.getArg(I)) |
| 2884 | continue; |
| 2885 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2886 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 2887 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2888 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2889 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2890 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2891 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2892 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 2893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2894 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2895 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2896 | // Visit the type parameters from a permissive context. |
| 2897 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2898 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 2899 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 2900 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 2901 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 2902 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2903 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2904 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2905 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2906 | // Visit pointee types from a permissive context. |
| 2907 | #define CheckPolymorphic(Type) \ |
| 2908 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 2909 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 2910 | } |
| 2911 | CheckPolymorphic(PointerTypeLoc) |
| 2912 | CheckPolymorphic(ReferenceTypeLoc) |
| 2913 | CheckPolymorphic(MemberPointerTypeLoc) |
| 2914 | CheckPolymorphic(BlockPointerTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2915 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2916 | /// Handle all the types we haven't given a more specific |
| 2917 | /// implementation for above. |
| 2918 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2919 | // Every other kind of type that we haven't called out already |
| 2920 | // that has an inner type is either (1) sugar or (2) contains that |
| 2921 | // inner type in some way as a subobject. |
| 2922 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 2923 | return Visit(Next, Sel); |
| 2924 | |
| 2925 | // If there's no inner type and we're in a permissive context, |
| 2926 | // don't diagnose. |
| 2927 | if (Sel == Sema::AbstractNone) return; |
| 2928 | |
| 2929 | // Check whether the type matches the abstract type. |
| 2930 | QualType T = TL.getType(); |
| 2931 | if (T->isArrayType()) { |
| 2932 | Sel = Sema::AbstractArrayType; |
| 2933 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2934 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2935 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 2936 | if (CT != Info.AbstractType) return; |
| 2937 | |
| 2938 | // It matched; do some magic. |
| 2939 | if (Sel == Sema::AbstractArrayType) { |
| 2940 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 2941 | << T << TL.getSourceRange(); |
| 2942 | } else { |
| 2943 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 2944 | << Sel << T << TL.getSourceRange(); |
| 2945 | } |
| 2946 | Info.DiagnoseAbstractType(); |
| 2947 | } |
| 2948 | }; |
| 2949 | |
| 2950 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 2951 | Sema::AbstractDiagSelID Sel) { |
| 2952 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 2953 | } |
| 2954 | |
| 2955 | } |
| 2956 | |
| 2957 | /// Check for invalid uses of an abstract type in a method declaration. |
| 2958 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2959 | CXXMethodDecl *MD) { |
| 2960 | // No need to do the check on definitions, which require that |
| 2961 | // the return/param types be complete. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2962 | if (MD->doesThisDeclarationHaveABody()) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2963 | return; |
| 2964 | |
| 2965 | // For safety's sake, just ignore it if we don't have type source |
| 2966 | // information. This should never happen for non-implicit methods, |
| 2967 | // but... |
| 2968 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 2969 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 2970 | } |
| 2971 | |
| 2972 | /// Check for invalid uses of an abstract type within a class definition. |
| 2973 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2974 | CXXRecordDecl *RD) { |
| 2975 | for (CXXRecordDecl::decl_iterator |
| 2976 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 2977 | Decl *D = *I; |
| 2978 | if (D->isImplicit()) continue; |
| 2979 | |
| 2980 | // Methods and method templates. |
| 2981 | if (isa<CXXMethodDecl>(D)) { |
| 2982 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 2983 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 2984 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 2985 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 2986 | |
| 2987 | // Fields and static variables. |
| 2988 | } else if (isa<FieldDecl>(D)) { |
| 2989 | FieldDecl *FD = cast<FieldDecl>(D); |
| 2990 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 2991 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 2992 | } else if (isa<VarDecl>(D)) { |
| 2993 | VarDecl *VD = cast<VarDecl>(D); |
| 2994 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 2995 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 2996 | |
| 2997 | // Nested classes and class templates. |
| 2998 | } else if (isa<CXXRecordDecl>(D)) { |
| 2999 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 3000 | } else if (isa<ClassTemplateDecl>(D)) { |
| 3001 | CheckAbstractClassUsage(Info, |
| 3002 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 3003 | } |
| 3004 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3007 | /// \brief Perform semantic checks on a class definition that has been |
| 3008 | /// completing, introducing implicitly-declared members, checking for |
| 3009 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3010 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 3011 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3012 | return; |
| 3013 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3014 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 3015 | AbstractUsageInfo Info(*this, Record); |
| 3016 | CheckAbstractClassUsage(Info, Record); |
| 3017 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3018 | |
| 3019 | // If this is not an aggregate type and has no user-declared constructor, |
| 3020 | // complain about any non-static data members of reference or const scalar |
| 3021 | // type, since they will never get initializers. |
| 3022 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 3023 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor()) { |
| 3024 | bool Complained = false; |
| 3025 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 3026 | FEnd = Record->field_end(); |
| 3027 | F != FEnd; ++F) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3028 | if (F->hasInClassInitializer()) |
| 3029 | continue; |
| 3030 | |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3031 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 3032 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 3033 | if (!Complained) { |
| 3034 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 3035 | << Record->getTagKind() << Record; |
| 3036 | Complained = true; |
| 3037 | } |
| 3038 | |
| 3039 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 3040 | << F->getType()->isReferenceType() |
| 3041 | << F->getDeclName(); |
| 3042 | } |
| 3043 | } |
| 3044 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3045 | |
Anders Carlsson | a5c6c2a | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 3046 | if (Record->isDynamicClass() && !Record->isDependentType()) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 3047 | DynamicClasses.push_back(Record); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3048 | |
| 3049 | if (Record->getIdentifier()) { |
| 3050 | // C++ [class.mem]p13: |
| 3051 | // If T is the name of a class, then each of the following shall have a |
| 3052 | // name different from T: |
| 3053 | // - every member of every anonymous union that is a member of class T. |
| 3054 | // |
| 3055 | // C++ [class.mem]p14: |
| 3056 | // In addition, if class T has a user-declared constructor (12.1), every |
| 3057 | // non-static data member of class T shall have a name different from T. |
| 3058 | for (DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3059 | R.first != R.second; ++R.first) { |
| 3060 | NamedDecl *D = *R.first; |
| 3061 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 3062 | isa<IndirectFieldDecl>(D)) { |
| 3063 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 3064 | << D->getDeclName(); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3065 | break; |
| 3066 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3067 | } |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 3068 | } |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3069 | |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 3070 | // Warn if the class has virtual methods but non-virtual public destructor. |
Douglas Gregor | f4b793c | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 3071 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3072 | CXXDestructorDecl *dtor = Record->getDestructor(); |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 3073 | if (!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 3074 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 3075 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 3076 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3077 | |
| 3078 | // See if a method overloads virtual methods in a base |
| 3079 | /// class without overriding any. |
| 3080 | if (!Record->isDependentType()) { |
| 3081 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 3082 | MEnd = Record->method_end(); |
| 3083 | M != MEnd; ++M) { |
Argyrios Kyrtzidis | 0266aa3 | 2011-03-03 22:58:57 +0000 | [diff] [blame] | 3084 | if (!(*M)->isStatic()) |
| 3085 | DiagnoseHiddenVirtualMethods(Record, *M); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3086 | } |
| 3087 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 3088 | |
| 3089 | // Declare inherited constructors. We do this eagerly here because: |
| 3090 | // - The standard requires an eager diagnostic for conflicting inherited |
| 3091 | // constructors from different classes. |
| 3092 | // - The lazy declaration of the other implicit constructors is so as to not |
| 3093 | // waste space and performance on classes that are not meant to be |
| 3094 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
| 3095 | // have inherited constructors. |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 3096 | DeclareInheritedConstructors(Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3097 | |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 3098 | if (!Record->isDependentType()) |
| 3099 | CheckExplicitlyDefaultedMethods(Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | void Sema::CheckExplicitlyDefaultedMethods(CXXRecordDecl *Record) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3103 | for (CXXRecordDecl::method_iterator MI = Record->method_begin(), |
| 3104 | ME = Record->method_end(); |
| 3105 | MI != ME; ++MI) { |
| 3106 | if (!MI->isInvalidDecl() && MI->isExplicitlyDefaulted()) { |
| 3107 | switch (getSpecialMember(*MI)) { |
| 3108 | case CXXDefaultConstructor: |
| 3109 | CheckExplicitlyDefaultedDefaultConstructor( |
| 3110 | cast<CXXConstructorDecl>(*MI)); |
| 3111 | break; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3112 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3113 | case CXXDestructor: |
| 3114 | CheckExplicitlyDefaultedDestructor(cast<CXXDestructorDecl>(*MI)); |
| 3115 | break; |
| 3116 | |
| 3117 | case CXXCopyConstructor: |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3118 | CheckExplicitlyDefaultedCopyConstructor(cast<CXXConstructorDecl>(*MI)); |
| 3119 | break; |
| 3120 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3121 | case CXXCopyAssignment: |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3122 | CheckExplicitlyDefaultedCopyAssignment(*MI); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3123 | break; |
| 3124 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3125 | case CXXMoveConstructor: |
| 3126 | case CXXMoveAssignment: |
| 3127 | Diag(MI->getLocation(), diag::err_defaulted_move_unsupported); |
| 3128 | break; |
| 3129 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3130 | default: |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3131 | // FIXME: Do moves once they exist |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3132 | llvm_unreachable("non-special member explicitly defaulted!"); |
| 3133 | } |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3134 | } |
| 3135 | } |
| 3136 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | void Sema::CheckExplicitlyDefaultedDefaultConstructor(CXXConstructorDecl *CD) { |
| 3140 | assert(CD->isExplicitlyDefaulted() && CD->isDefaultConstructor()); |
| 3141 | |
| 3142 | // Whether this was the first-declared instance of the constructor. |
| 3143 | // This affects whether we implicitly add an exception spec (and, eventually, |
| 3144 | // constexpr). It is also ill-formed to explicitly default a constructor such |
| 3145 | // that it would be deleted. (C++0x [decl.fct.def.default]) |
| 3146 | bool First = CD == CD->getCanonicalDecl(); |
| 3147 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3148 | bool HadError = false; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3149 | if (CD->getNumParams() != 0) { |
| 3150 | Diag(CD->getLocation(), diag::err_defaulted_default_ctor_params) |
| 3151 | << CD->getSourceRange(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3152 | HadError = true; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | ImplicitExceptionSpecification Spec |
| 3156 | = ComputeDefaultedDefaultCtorExceptionSpec(CD->getParent()); |
| 3157 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3158 | if (EPI.ExceptionSpecType == EST_Delayed) { |
| 3159 | // Exception specification depends on some deferred part of the class. We'll |
| 3160 | // try again when the class's definition has been fully processed. |
| 3161 | return; |
| 3162 | } |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3163 | const FunctionProtoType *CtorType = CD->getType()->getAs<FunctionProtoType>(), |
| 3164 | *ExceptionType = Context.getFunctionType( |
| 3165 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3166 | |
| 3167 | if (CtorType->hasExceptionSpec()) { |
| 3168 | if (CheckEquivalentExceptionSpec( |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3169 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3170 | << CXXDefaultConstructor, |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3171 | PDiag(), |
| 3172 | ExceptionType, SourceLocation(), |
| 3173 | CtorType, CD->getLocation())) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3174 | HadError = true; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3175 | } |
| 3176 | } else if (First) { |
| 3177 | // We set the declaration to have the computed exception spec here. |
| 3178 | // We know there are no parameters. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3179 | EPI.ExtInfo = CtorType->getExtInfo(); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3180 | CD->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 3181 | } |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3182 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3183 | if (HadError) { |
| 3184 | CD->setInvalidDecl(); |
| 3185 | return; |
| 3186 | } |
| 3187 | |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3188 | if (ShouldDeleteDefaultConstructor(CD)) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3189 | if (First) { |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3190 | CD->setDeletedAsWritten(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3191 | } else { |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3192 | Diag(CD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3193 | << CXXDefaultConstructor; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3194 | CD->setInvalidDecl(); |
| 3195 | } |
| 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | void Sema::CheckExplicitlyDefaultedCopyConstructor(CXXConstructorDecl *CD) { |
| 3200 | assert(CD->isExplicitlyDefaulted() && CD->isCopyConstructor()); |
| 3201 | |
| 3202 | // Whether this was the first-declared instance of the constructor. |
| 3203 | bool First = CD == CD->getCanonicalDecl(); |
| 3204 | |
| 3205 | bool HadError = false; |
| 3206 | if (CD->getNumParams() != 1) { |
| 3207 | Diag(CD->getLocation(), diag::err_defaulted_copy_ctor_params) |
| 3208 | << CD->getSourceRange(); |
| 3209 | HadError = true; |
| 3210 | } |
| 3211 | |
| 3212 | ImplicitExceptionSpecification Spec(Context); |
| 3213 | bool Const; |
| 3214 | llvm::tie(Spec, Const) = |
| 3215 | ComputeDefaultedCopyCtorExceptionSpecAndConst(CD->getParent()); |
| 3216 | |
| 3217 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 3218 | const FunctionProtoType *CtorType = CD->getType()->getAs<FunctionProtoType>(), |
| 3219 | *ExceptionType = Context.getFunctionType( |
| 3220 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3221 | |
| 3222 | // Check for parameter type matching. |
| 3223 | // This is a copy ctor so we know it's a cv-qualified reference to T. |
| 3224 | QualType ArgType = CtorType->getArgType(0); |
| 3225 | if (ArgType->getPointeeType().isVolatileQualified()) { |
| 3226 | Diag(CD->getLocation(), diag::err_defaulted_copy_ctor_volatile_param); |
| 3227 | HadError = true; |
| 3228 | } |
| 3229 | if (ArgType->getPointeeType().isConstQualified() && !Const) { |
| 3230 | Diag(CD->getLocation(), diag::err_defaulted_copy_ctor_const_param); |
| 3231 | HadError = true; |
| 3232 | } |
| 3233 | |
| 3234 | if (CtorType->hasExceptionSpec()) { |
| 3235 | if (CheckEquivalentExceptionSpec( |
| 3236 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3237 | << CXXCopyConstructor, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3238 | PDiag(), |
| 3239 | ExceptionType, SourceLocation(), |
| 3240 | CtorType, CD->getLocation())) { |
| 3241 | HadError = true; |
| 3242 | } |
| 3243 | } else if (First) { |
| 3244 | // We set the declaration to have the computed exception spec here. |
| 3245 | // We duplicate the one parameter type. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3246 | EPI.ExtInfo = CtorType->getExtInfo(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3247 | CD->setType(Context.getFunctionType(Context.VoidTy, &ArgType, 1, EPI)); |
| 3248 | } |
| 3249 | |
| 3250 | if (HadError) { |
| 3251 | CD->setInvalidDecl(); |
| 3252 | return; |
| 3253 | } |
| 3254 | |
| 3255 | if (ShouldDeleteCopyConstructor(CD)) { |
| 3256 | if (First) { |
| 3257 | CD->setDeletedAsWritten(); |
| 3258 | } else { |
| 3259 | Diag(CD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3260 | << CXXCopyConstructor; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3261 | CD->setInvalidDecl(); |
| 3262 | } |
Sean Hunt | ca46d13 | 2011-05-12 03:51:48 +0000 | [diff] [blame] | 3263 | } |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3264 | } |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 3265 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3266 | void Sema::CheckExplicitlyDefaultedCopyAssignment(CXXMethodDecl *MD) { |
| 3267 | assert(MD->isExplicitlyDefaulted()); |
| 3268 | |
| 3269 | // Whether this was the first-declared instance of the operator |
| 3270 | bool First = MD == MD->getCanonicalDecl(); |
| 3271 | |
| 3272 | bool HadError = false; |
| 3273 | if (MD->getNumParams() != 1) { |
| 3274 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_params) |
| 3275 | << MD->getSourceRange(); |
| 3276 | HadError = true; |
| 3277 | } |
| 3278 | |
| 3279 | QualType ReturnType = |
| 3280 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 3281 | if (!ReturnType->isLValueReferenceType() || |
| 3282 | !Context.hasSameType( |
| 3283 | Context.getCanonicalType(ReturnType->getPointeeType()), |
| 3284 | Context.getCanonicalType(Context.getTypeDeclType(MD->getParent())))) { |
| 3285 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_return_type); |
| 3286 | HadError = true; |
| 3287 | } |
| 3288 | |
| 3289 | ImplicitExceptionSpecification Spec(Context); |
| 3290 | bool Const; |
| 3291 | llvm::tie(Spec, Const) = |
| 3292 | ComputeDefaultedCopyCtorExceptionSpecAndConst(MD->getParent()); |
| 3293 | |
| 3294 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 3295 | const FunctionProtoType *OperType = MD->getType()->getAs<FunctionProtoType>(), |
| 3296 | *ExceptionType = Context.getFunctionType( |
| 3297 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3298 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3299 | QualType ArgType = OperType->getArgType(0); |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 3300 | if (!ArgType->isReferenceType()) { |
| 3301 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_not_ref); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3302 | HadError = true; |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 3303 | } else { |
| 3304 | if (ArgType->getPointeeType().isVolatileQualified()) { |
| 3305 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_volatile_param); |
| 3306 | HadError = true; |
| 3307 | } |
| 3308 | if (ArgType->getPointeeType().isConstQualified() && !Const) { |
| 3309 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_const_param); |
| 3310 | HadError = true; |
| 3311 | } |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3312 | } |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 3313 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3314 | if (OperType->getTypeQuals()) { |
| 3315 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_quals); |
| 3316 | HadError = true; |
| 3317 | } |
| 3318 | |
| 3319 | if (OperType->hasExceptionSpec()) { |
| 3320 | if (CheckEquivalentExceptionSpec( |
| 3321 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3322 | << CXXCopyAssignment, |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3323 | PDiag(), |
| 3324 | ExceptionType, SourceLocation(), |
| 3325 | OperType, MD->getLocation())) { |
| 3326 | HadError = true; |
| 3327 | } |
| 3328 | } else if (First) { |
| 3329 | // We set the declaration to have the computed exception spec here. |
| 3330 | // We duplicate the one parameter type. |
| 3331 | EPI.RefQualifier = OperType->getRefQualifier(); |
| 3332 | EPI.ExtInfo = OperType->getExtInfo(); |
| 3333 | MD->setType(Context.getFunctionType(ReturnType, &ArgType, 1, EPI)); |
| 3334 | } |
| 3335 | |
| 3336 | if (HadError) { |
| 3337 | MD->setInvalidDecl(); |
| 3338 | return; |
| 3339 | } |
| 3340 | |
| 3341 | if (ShouldDeleteCopyAssignmentOperator(MD)) { |
| 3342 | if (First) { |
| 3343 | MD->setDeletedAsWritten(); |
| 3344 | } else { |
| 3345 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3346 | << CXXCopyAssignment; |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3347 | MD->setInvalidDecl(); |
| 3348 | } |
| 3349 | } |
| 3350 | } |
| 3351 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3352 | void Sema::CheckExplicitlyDefaultedDestructor(CXXDestructorDecl *DD) { |
| 3353 | assert(DD->isExplicitlyDefaulted()); |
| 3354 | |
| 3355 | // Whether this was the first-declared instance of the destructor. |
| 3356 | bool First = DD == DD->getCanonicalDecl(); |
| 3357 | |
| 3358 | ImplicitExceptionSpecification Spec |
| 3359 | = ComputeDefaultedDtorExceptionSpec(DD->getParent()); |
| 3360 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 3361 | const FunctionProtoType *DtorType = DD->getType()->getAs<FunctionProtoType>(), |
| 3362 | *ExceptionType = Context.getFunctionType( |
| 3363 | Context.VoidTy, 0, 0, EPI)->getAs<FunctionProtoType>(); |
| 3364 | |
| 3365 | if (DtorType->hasExceptionSpec()) { |
| 3366 | if (CheckEquivalentExceptionSpec( |
| 3367 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3368 | << CXXDestructor, |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3369 | PDiag(), |
| 3370 | ExceptionType, SourceLocation(), |
| 3371 | DtorType, DD->getLocation())) { |
| 3372 | DD->setInvalidDecl(); |
| 3373 | return; |
| 3374 | } |
| 3375 | } else if (First) { |
| 3376 | // We set the declaration to have the computed exception spec here. |
| 3377 | // There are no parameters. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3378 | EPI.ExtInfo = DtorType->getExtInfo(); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3379 | DD->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 3380 | } |
| 3381 | |
| 3382 | if (ShouldDeleteDestructor(DD)) { |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3383 | if (First) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3384 | DD->setDeletedAsWritten(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3385 | } else { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3386 | Diag(DD->getLocation(), diag::err_out_of_line_default_deletes) |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 3387 | << CXXDestructor; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3388 | DD->setInvalidDecl(); |
| 3389 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3390 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3393 | bool Sema::ShouldDeleteDefaultConstructor(CXXConstructorDecl *CD) { |
| 3394 | CXXRecordDecl *RD = CD->getParent(); |
| 3395 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
| 3396 | if (!LangOpts.CPlusPlus0x) |
| 3397 | return false; |
| 3398 | |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3399 | SourceLocation Loc = CD->getLocation(); |
| 3400 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3401 | // Do access control from the constructor |
| 3402 | ContextRAII CtorContext(*this, CD); |
| 3403 | |
| 3404 | bool Union = RD->isUnion(); |
| 3405 | bool AllConst = true; |
| 3406 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3407 | // We do this because we should never actually use an anonymous |
| 3408 | // union's constructor. |
| 3409 | if (Union && RD->isAnonymousStructOrUnion()) |
| 3410 | return false; |
| 3411 | |
| 3412 | // FIXME: We should put some diagnostic logic right into this function. |
| 3413 | |
| 3414 | // C++0x [class.ctor]/5 |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3415 | // A defaulted default constructor for class X is defined as deleted if: |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3416 | |
| 3417 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 3418 | BE = RD->bases_end(); |
| 3419 | BI != BE; ++BI) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3420 | // We'll handle this one later |
| 3421 | if (BI->isVirtual()) |
| 3422 | continue; |
| 3423 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3424 | CXXRecordDecl *BaseDecl = BI->getType()->getAsCXXRecordDecl(); |
| 3425 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3426 | |
| 3427 | // -- any [direct base class] has a type with a destructor that is |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3428 | // deleted or inaccessible from the defaulted default constructor |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3429 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3430 | if (BaseDtor->isDeleted()) |
| 3431 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3432 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3433 | AR_accessible) |
| 3434 | return true; |
| 3435 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3436 | // -- any [direct base class either] has no default constructor or |
| 3437 | // overload resolution as applied to [its] default constructor |
| 3438 | // results in an ambiguity or in a function that is deleted or |
| 3439 | // inaccessible from the defaulted default constructor |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3440 | CXXConstructorDecl *BaseDefault = LookupDefaultConstructor(BaseDecl); |
| 3441 | if (!BaseDefault || BaseDefault->isDeleted()) |
| 3442 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3443 | |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3444 | if (CheckConstructorAccess(Loc, BaseDefault, BaseDefault->getAccess(), |
| 3445 | PDiag()) != AR_accessible) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3446 | return true; |
| 3447 | } |
| 3448 | |
| 3449 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 3450 | BE = RD->vbases_end(); |
| 3451 | BI != BE; ++BI) { |
| 3452 | CXXRecordDecl *BaseDecl = BI->getType()->getAsCXXRecordDecl(); |
| 3453 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3454 | |
| 3455 | // -- any [virtual base class] has a type with a destructor that is |
| 3456 | // delete or inaccessible from the defaulted default constructor |
| 3457 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3458 | if (BaseDtor->isDeleted()) |
| 3459 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3460 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3461 | AR_accessible) |
| 3462 | return true; |
| 3463 | |
| 3464 | // -- any [virtual base class either] has no default constructor or |
| 3465 | // overload resolution as applied to [its] default constructor |
| 3466 | // results in an ambiguity or in a function that is deleted or |
| 3467 | // inaccessible from the defaulted default constructor |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3468 | CXXConstructorDecl *BaseDefault = LookupDefaultConstructor(BaseDecl); |
| 3469 | if (!BaseDefault || BaseDefault->isDeleted()) |
| 3470 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3471 | |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3472 | if (CheckConstructorAccess(Loc, BaseDefault, BaseDefault->getAccess(), |
| 3473 | PDiag()) != AR_accessible) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3474 | return true; |
| 3475 | } |
| 3476 | |
| 3477 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 3478 | FE = RD->field_end(); |
| 3479 | FI != FE; ++FI) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3480 | if (FI->isInvalidDecl()) |
| 3481 | continue; |
| 3482 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3483 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 3484 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3485 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3486 | // -- any non-static data member with no brace-or-equal-initializer is of |
| 3487 | // reference type |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3488 | if (FieldType->isReferenceType() && !FI->hasInClassInitializer()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3489 | return true; |
| 3490 | |
| 3491 | // -- X is a union and all its variant members are of const-qualified type |
| 3492 | // (or array thereof) |
| 3493 | if (Union && !FieldType.isConstQualified()) |
| 3494 | AllConst = false; |
| 3495 | |
| 3496 | if (FieldRecord) { |
| 3497 | // -- X is a union-like class that has a variant member with a non-trivial |
| 3498 | // default constructor |
| 3499 | if (Union && !FieldRecord->hasTrivialDefaultConstructor()) |
| 3500 | return true; |
| 3501 | |
| 3502 | CXXDestructorDecl *FieldDtor = LookupDestructor(FieldRecord); |
| 3503 | if (FieldDtor->isDeleted()) |
| 3504 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3505 | if (CheckDestructorAccess(Loc, FieldDtor, PDiag()) != |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3506 | AR_accessible) |
| 3507 | return true; |
| 3508 | |
| 3509 | // -- any non-variant non-static data member of const-qualified type (or |
| 3510 | // array thereof) with no brace-or-equal-initializer does not have a |
| 3511 | // user-provided default constructor |
| 3512 | if (FieldType.isConstQualified() && |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3513 | !FI->hasInClassInitializer() && |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3514 | !FieldRecord->hasUserProvidedDefaultConstructor()) |
| 3515 | return true; |
| 3516 | |
| 3517 | if (!Union && FieldRecord->isUnion() && |
| 3518 | FieldRecord->isAnonymousStructOrUnion()) { |
| 3519 | // We're okay to reuse AllConst here since we only care about the |
| 3520 | // value otherwise if we're in a union. |
| 3521 | AllConst = true; |
| 3522 | |
| 3523 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 3524 | UE = FieldRecord->field_end(); |
| 3525 | UI != UE; ++UI) { |
| 3526 | QualType UnionFieldType = Context.getBaseElementType(UI->getType()); |
| 3527 | CXXRecordDecl *UnionFieldRecord = |
| 3528 | UnionFieldType->getAsCXXRecordDecl(); |
| 3529 | |
| 3530 | if (!UnionFieldType.isConstQualified()) |
| 3531 | AllConst = false; |
| 3532 | |
| 3533 | if (UnionFieldRecord && |
| 3534 | !UnionFieldRecord->hasTrivialDefaultConstructor()) |
| 3535 | return true; |
| 3536 | } |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 3537 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3538 | if (AllConst) |
| 3539 | return true; |
| 3540 | |
| 3541 | // Don't try to initialize the anonymous union |
Sean Hunt | a6bff2c | 2011-05-11 22:50:12 +0000 | [diff] [blame] | 3542 | // This is technically non-conformant, but sanity demands it. |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3543 | continue; |
| 3544 | } |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3545 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3546 | // -- any non-static data member with no brace-or-equal-initializer has |
| 3547 | // class type M (or array thereof) and either M has no default |
| 3548 | // constructor or overload resolution as applied to M's default |
| 3549 | // constructor results in an ambiguity or in a function that is deleted |
| 3550 | // or inaccessible from the defaulted default constructor. |
| 3551 | if (!FI->hasInClassInitializer()) { |
| 3552 | CXXConstructorDecl *FieldDefault = LookupDefaultConstructor(FieldRecord); |
| 3553 | if (!FieldDefault || FieldDefault->isDeleted()) |
| 3554 | return true; |
| 3555 | if (CheckConstructorAccess(Loc, FieldDefault, FieldDefault->getAccess(), |
| 3556 | PDiag()) != AR_accessible) |
| 3557 | return true; |
| 3558 | } |
| 3559 | } else if (!Union && FieldType.isConstQualified() && |
| 3560 | !FI->hasInClassInitializer()) { |
Sean Hunt | e340682 | 2011-05-20 21:43:47 +0000 | [diff] [blame] | 3561 | // -- any non-variant non-static data member of const-qualified type (or |
| 3562 | // array thereof) with no brace-or-equal-initializer does not have a |
| 3563 | // user-provided default constructor |
| 3564 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3565 | } |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 3566 | } |
| 3567 | |
| 3568 | if (Union && AllConst) |
| 3569 | return true; |
| 3570 | |
| 3571 | return false; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3572 | } |
| 3573 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3574 | bool Sema::ShouldDeleteCopyConstructor(CXXConstructorDecl *CD) { |
Sean Hunt | 493ff72 | 2011-05-18 20:57:13 +0000 | [diff] [blame] | 3575 | CXXRecordDecl *RD = CD->getParent(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3576 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
| 3577 | if (!LangOpts.CPlusPlus0x) |
| 3578 | return false; |
| 3579 | |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3580 | SourceLocation Loc = CD->getLocation(); |
| 3581 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3582 | // Do access control from the constructor |
| 3583 | ContextRAII CtorContext(*this, CD); |
| 3584 | |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3585 | bool Union = RD->isUnion(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3586 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3587 | assert(!CD->getParamDecl(0)->getType()->getPointeeType().isNull() && |
| 3588 | "copy assignment arg has no pointee type"); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3589 | unsigned ArgQuals = |
| 3590 | CD->getParamDecl(0)->getType()->getPointeeType().isConstQualified() ? |
| 3591 | Qualifiers::Const : 0; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3592 | |
| 3593 | // We do this because we should never actually use an anonymous |
| 3594 | // union's constructor. |
| 3595 | if (Union && RD->isAnonymousStructOrUnion()) |
| 3596 | return false; |
| 3597 | |
| 3598 | // FIXME: We should put some diagnostic logic right into this function. |
| 3599 | |
| 3600 | // C++0x [class.copy]/11 |
| 3601 | // A defaulted [copy] constructor for class X is defined as delete if X has: |
| 3602 | |
| 3603 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 3604 | BE = RD->bases_end(); |
| 3605 | BI != BE; ++BI) { |
| 3606 | // We'll handle this one later |
| 3607 | if (BI->isVirtual()) |
| 3608 | continue; |
| 3609 | |
| 3610 | QualType BaseType = BI->getType(); |
| 3611 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 3612 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3613 | |
| 3614 | // -- any [direct base class] of a type with a destructor that is deleted or |
| 3615 | // inaccessible from the defaulted constructor |
| 3616 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3617 | if (BaseDtor->isDeleted()) |
| 3618 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3619 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3620 | AR_accessible) |
| 3621 | return true; |
| 3622 | |
| 3623 | // -- a [direct base class] B that cannot be [copied] because overload |
| 3624 | // resolution, as applied to B's [copy] constructor, results in an |
| 3625 | // ambiguity or a function that is deleted or inaccessible from the |
| 3626 | // defaulted constructor |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3627 | CXXConstructorDecl *BaseCtor = LookupCopyingConstructor(BaseDecl, ArgQuals); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3628 | if (!BaseCtor || BaseCtor->isDeleted()) |
| 3629 | return true; |
| 3630 | if (CheckConstructorAccess(Loc, BaseCtor, BaseCtor->getAccess(), PDiag()) != |
| 3631 | AR_accessible) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3632 | return true; |
| 3633 | } |
| 3634 | |
| 3635 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 3636 | BE = RD->vbases_end(); |
| 3637 | BI != BE; ++BI) { |
| 3638 | QualType BaseType = BI->getType(); |
| 3639 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 3640 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3641 | |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 3642 | // -- 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] | 3643 | // inaccessible from the defaulted constructor |
| 3644 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3645 | if (BaseDtor->isDeleted()) |
| 3646 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3647 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3648 | AR_accessible) |
| 3649 | return true; |
| 3650 | |
| 3651 | // -- a [virtual base class] B that cannot be [copied] because overload |
| 3652 | // resolution, as applied to B's [copy] constructor, results in an |
| 3653 | // ambiguity or a function that is deleted or inaccessible from the |
| 3654 | // defaulted constructor |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3655 | CXXConstructorDecl *BaseCtor = LookupCopyingConstructor(BaseDecl, ArgQuals); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3656 | if (!BaseCtor || BaseCtor->isDeleted()) |
| 3657 | return true; |
| 3658 | if (CheckConstructorAccess(Loc, BaseCtor, BaseCtor->getAccess(), PDiag()) != |
| 3659 | AR_accessible) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3660 | return true; |
| 3661 | } |
| 3662 | |
| 3663 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 3664 | FE = RD->field_end(); |
| 3665 | FI != FE; ++FI) { |
| 3666 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 3667 | |
| 3668 | // -- for a copy constructor, a non-static data member of rvalue reference |
| 3669 | // type |
| 3670 | if (FieldType->isRValueReferenceType()) |
| 3671 | return true; |
| 3672 | |
| 3673 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 3674 | |
| 3675 | if (FieldRecord) { |
| 3676 | // This is an anonymous union |
| 3677 | if (FieldRecord->isUnion() && FieldRecord->isAnonymousStructOrUnion()) { |
| 3678 | // Anonymous unions inside unions do not variant members create |
| 3679 | if (!Union) { |
| 3680 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 3681 | UE = FieldRecord->field_end(); |
| 3682 | UI != UE; ++UI) { |
| 3683 | QualType UnionFieldType = Context.getBaseElementType(UI->getType()); |
| 3684 | CXXRecordDecl *UnionFieldRecord = |
| 3685 | UnionFieldType->getAsCXXRecordDecl(); |
| 3686 | |
| 3687 | // -- a variant member with a non-trivial [copy] constructor and X |
| 3688 | // is a union-like class |
| 3689 | if (UnionFieldRecord && |
| 3690 | !UnionFieldRecord->hasTrivialCopyConstructor()) |
| 3691 | return true; |
| 3692 | } |
| 3693 | } |
| 3694 | |
| 3695 | // Don't try to initalize an anonymous union |
| 3696 | continue; |
| 3697 | } else { |
| 3698 | // -- a variant member with a non-trivial [copy] constructor and X is a |
| 3699 | // union-like class |
| 3700 | if (Union && !FieldRecord->hasTrivialCopyConstructor()) |
| 3701 | return true; |
| 3702 | |
| 3703 | // -- any [non-static data member] of a type with a destructor that is |
| 3704 | // deleted or inaccessible from the defaulted constructor |
| 3705 | CXXDestructorDecl *FieldDtor = LookupDestructor(FieldRecord); |
| 3706 | if (FieldDtor->isDeleted()) |
| 3707 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3708 | if (CheckDestructorAccess(Loc, FieldDtor, PDiag()) != |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3709 | AR_accessible) |
| 3710 | return true; |
| 3711 | } |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3712 | |
| 3713 | // -- a [non-static data member of class type (or array thereof)] B that |
| 3714 | // cannot be [copied] because overload resolution, as applied to B's |
| 3715 | // [copy] constructor, results in an ambiguity or a function that is |
| 3716 | // deleted or inaccessible from the defaulted constructor |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3717 | CXXConstructorDecl *FieldCtor = LookupCopyingConstructor(FieldRecord, |
| 3718 | ArgQuals); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 3719 | if (!FieldCtor || FieldCtor->isDeleted()) |
| 3720 | return true; |
| 3721 | if (CheckConstructorAccess(Loc, FieldCtor, FieldCtor->getAccess(), |
| 3722 | PDiag()) != AR_accessible) |
| 3723 | return true; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3724 | } |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | return false; |
| 3728 | } |
| 3729 | |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3730 | bool Sema::ShouldDeleteCopyAssignmentOperator(CXXMethodDecl *MD) { |
| 3731 | CXXRecordDecl *RD = MD->getParent(); |
| 3732 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
| 3733 | if (!LangOpts.CPlusPlus0x) |
| 3734 | return false; |
| 3735 | |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3736 | SourceLocation Loc = MD->getLocation(); |
| 3737 | |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3738 | // Do access control from the constructor |
| 3739 | ContextRAII MethodContext(*this, MD); |
| 3740 | |
| 3741 | bool Union = RD->isUnion(); |
| 3742 | |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3743 | unsigned ArgQuals = |
| 3744 | MD->getParamDecl(0)->getType()->getPointeeType().isConstQualified() ? |
| 3745 | Qualifiers::Const : 0; |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3746 | |
| 3747 | // We do this because we should never actually use an anonymous |
| 3748 | // union's constructor. |
| 3749 | if (Union && RD->isAnonymousStructOrUnion()) |
| 3750 | return false; |
| 3751 | |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3752 | // FIXME: We should put some diagnostic logic right into this function. |
| 3753 | |
| 3754 | // C++0x [class.copy]/11 |
| 3755 | // A defaulted [copy] assignment operator for class X is defined as deleted |
| 3756 | // if X has: |
| 3757 | |
| 3758 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 3759 | BE = RD->bases_end(); |
| 3760 | BI != BE; ++BI) { |
| 3761 | // We'll handle this one later |
| 3762 | if (BI->isVirtual()) |
| 3763 | continue; |
| 3764 | |
| 3765 | QualType BaseType = BI->getType(); |
| 3766 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 3767 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3768 | |
| 3769 | // -- a [direct base class] B that cannot be [copied] because overload |
| 3770 | // resolution, as applied to B's [copy] assignment operator, results in |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3771 | // an ambiguity or a function that is deleted or inaccessible from the |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3772 | // assignment operator |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3773 | CXXMethodDecl *CopyOper = LookupCopyingAssignment(BaseDecl, ArgQuals, false, |
| 3774 | 0); |
| 3775 | if (!CopyOper || CopyOper->isDeleted()) |
| 3776 | return true; |
| 3777 | if (CheckDirectMemberAccess(Loc, CopyOper, PDiag()) != AR_accessible) |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3778 | return true; |
| 3779 | } |
| 3780 | |
| 3781 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 3782 | BE = RD->vbases_end(); |
| 3783 | BI != BE; ++BI) { |
| 3784 | QualType BaseType = BI->getType(); |
| 3785 | CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl(); |
| 3786 | assert(BaseDecl && "base isn't a CXXRecordDecl"); |
| 3787 | |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3788 | // -- a [virtual base class] B that cannot be [copied] because overload |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3789 | // resolution, as applied to B's [copy] assignment operator, results in |
| 3790 | // an ambiguity or a function that is deleted or inaccessible from the |
| 3791 | // assignment operator |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3792 | CXXMethodDecl *CopyOper = LookupCopyingAssignment(BaseDecl, ArgQuals, false, |
| 3793 | 0); |
| 3794 | if (!CopyOper || CopyOper->isDeleted()) |
| 3795 | return true; |
| 3796 | if (CheckDirectMemberAccess(Loc, CopyOper, PDiag()) != AR_accessible) |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3797 | return true; |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 3801 | FE = RD->field_end(); |
| 3802 | FI != FE; ++FI) { |
| 3803 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 3804 | |
| 3805 | // -- a non-static data member of reference type |
| 3806 | if (FieldType->isReferenceType()) |
| 3807 | return true; |
| 3808 | |
| 3809 | // -- a non-static data member of const non-class type (or array thereof) |
| 3810 | if (FieldType.isConstQualified() && !FieldType->isRecordType()) |
| 3811 | return true; |
| 3812 | |
| 3813 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 3814 | |
| 3815 | if (FieldRecord) { |
| 3816 | // This is an anonymous union |
| 3817 | if (FieldRecord->isUnion() && FieldRecord->isAnonymousStructOrUnion()) { |
| 3818 | // Anonymous unions inside unions do not variant members create |
| 3819 | if (!Union) { |
| 3820 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 3821 | UE = FieldRecord->field_end(); |
| 3822 | UI != UE; ++UI) { |
| 3823 | QualType UnionFieldType = Context.getBaseElementType(UI->getType()); |
| 3824 | CXXRecordDecl *UnionFieldRecord = |
| 3825 | UnionFieldType->getAsCXXRecordDecl(); |
| 3826 | |
| 3827 | // -- a variant member with a non-trivial [copy] assignment operator |
| 3828 | // and X is a union-like class |
| 3829 | if (UnionFieldRecord && |
| 3830 | !UnionFieldRecord->hasTrivialCopyAssignment()) |
| 3831 | return true; |
| 3832 | } |
| 3833 | } |
| 3834 | |
| 3835 | // Don't try to initalize an anonymous union |
| 3836 | continue; |
| 3837 | // -- a variant member with a non-trivial [copy] assignment operator |
| 3838 | // and X is a union-like class |
| 3839 | } else if (Union && !FieldRecord->hasTrivialCopyAssignment()) { |
| 3840 | return true; |
| 3841 | } |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3842 | |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 3843 | CXXMethodDecl *CopyOper = LookupCopyingAssignment(FieldRecord, ArgQuals, |
| 3844 | false, 0); |
| 3845 | if (!CopyOper || CopyOper->isDeleted()) |
| 3846 | return false; |
| 3847 | if (CheckDirectMemberAccess(Loc, CopyOper, PDiag()) != AR_accessible) |
| 3848 | return false; |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 3849 | } |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | return false; |
| 3853 | } |
| 3854 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3855 | bool Sema::ShouldDeleteDestructor(CXXDestructorDecl *DD) { |
| 3856 | CXXRecordDecl *RD = DD->getParent(); |
| 3857 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
| 3858 | if (!LangOpts.CPlusPlus0x) |
| 3859 | return false; |
| 3860 | |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3861 | SourceLocation Loc = DD->getLocation(); |
| 3862 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3863 | // Do access control from the destructor |
| 3864 | ContextRAII CtorContext(*this, DD); |
| 3865 | |
| 3866 | bool Union = RD->isUnion(); |
| 3867 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 3868 | // We do this because we should never actually use an anonymous |
| 3869 | // union's destructor. |
| 3870 | if (Union && RD->isAnonymousStructOrUnion()) |
| 3871 | return false; |
| 3872 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3873 | // C++0x [class.dtor]p5 |
| 3874 | // A defaulted destructor for a class X is defined as deleted if: |
| 3875 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 3876 | BE = RD->bases_end(); |
| 3877 | BI != BE; ++BI) { |
| 3878 | // We'll handle this one later |
| 3879 | if (BI->isVirtual()) |
| 3880 | continue; |
| 3881 | |
| 3882 | CXXRecordDecl *BaseDecl = BI->getType()->getAsCXXRecordDecl(); |
| 3883 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3884 | assert(BaseDtor && "base has no destructor"); |
| 3885 | |
| 3886 | // -- any direct or virtual base class has a deleted destructor or |
| 3887 | // a destructor that is inaccessible from the defaulted destructor |
| 3888 | if (BaseDtor->isDeleted()) |
| 3889 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3890 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3891 | AR_accessible) |
| 3892 | return true; |
| 3893 | } |
| 3894 | |
| 3895 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 3896 | BE = RD->vbases_end(); |
| 3897 | BI != BE; ++BI) { |
| 3898 | CXXRecordDecl *BaseDecl = BI->getType()->getAsCXXRecordDecl(); |
| 3899 | CXXDestructorDecl *BaseDtor = LookupDestructor(BaseDecl); |
| 3900 | assert(BaseDtor && "base has no destructor"); |
| 3901 | |
| 3902 | // -- any direct or virtual base class has a deleted destructor or |
| 3903 | // a destructor that is inaccessible from the defaulted destructor |
| 3904 | if (BaseDtor->isDeleted()) |
| 3905 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3906 | if (CheckDestructorAccess(Loc, BaseDtor, PDiag()) != |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3907 | AR_accessible) |
| 3908 | return true; |
| 3909 | } |
| 3910 | |
| 3911 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 3912 | FE = RD->field_end(); |
| 3913 | FI != FE; ++FI) { |
| 3914 | QualType FieldType = Context.getBaseElementType(FI->getType()); |
| 3915 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 3916 | if (FieldRecord) { |
| 3917 | if (FieldRecord->isUnion() && FieldRecord->isAnonymousStructOrUnion()) { |
| 3918 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 3919 | UE = FieldRecord->field_end(); |
| 3920 | UI != UE; ++UI) { |
| 3921 | QualType UnionFieldType = Context.getBaseElementType(FI->getType()); |
| 3922 | CXXRecordDecl *UnionFieldRecord = |
| 3923 | UnionFieldType->getAsCXXRecordDecl(); |
| 3924 | |
| 3925 | // -- X is a union-like class that has a variant member with a non- |
| 3926 | // trivial destructor. |
| 3927 | if (UnionFieldRecord && !UnionFieldRecord->hasTrivialDestructor()) |
| 3928 | return true; |
| 3929 | } |
| 3930 | // Technically we are supposed to do this next check unconditionally. |
| 3931 | // But that makes absolutely no sense. |
| 3932 | } else { |
| 3933 | CXXDestructorDecl *FieldDtor = LookupDestructor(FieldRecord); |
| 3934 | |
| 3935 | // -- any of the non-static data members has class type M (or array |
| 3936 | // thereof) and M has a deleted destructor or a destructor that is |
| 3937 | // inaccessible from the defaulted destructor |
| 3938 | if (FieldDtor->isDeleted()) |
| 3939 | return true; |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3940 | if (CheckDestructorAccess(Loc, FieldDtor, PDiag()) != |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3941 | AR_accessible) |
| 3942 | return true; |
| 3943 | |
| 3944 | // -- X is a union-like class that has a variant member with a non- |
| 3945 | // trivial destructor. |
| 3946 | if (Union && !FieldDtor->isTrivial()) |
| 3947 | return true; |
| 3948 | } |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | if (DD->isVirtual()) { |
| 3953 | FunctionDecl *OperatorDelete = 0; |
| 3954 | DeclarationName Name = |
| 3955 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 3956 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete, |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 3957 | false)) |
| 3958 | return true; |
| 3959 | } |
| 3960 | |
| 3961 | |
| 3962 | return false; |
| 3963 | } |
| 3964 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3965 | /// \brief Data used with FindHiddenVirtualMethod |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 3966 | namespace { |
| 3967 | struct FindHiddenVirtualMethodData { |
| 3968 | Sema *S; |
| 3969 | CXXMethodDecl *Method; |
| 3970 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
| 3971 | llvm::SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| 3972 | }; |
| 3973 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3974 | |
| 3975 | /// \brief Member lookup function that determines whether a given C++ |
| 3976 | /// method overloads virtual methods in a base class without overriding any, |
| 3977 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 3978 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 3979 | CXXBasePath &Path, |
| 3980 | void *UserData) { |
| 3981 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 3982 | |
| 3983 | FindHiddenVirtualMethodData &Data |
| 3984 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 3985 | |
| 3986 | DeclarationName Name = Data.Method->getDeclName(); |
| 3987 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 3988 | |
| 3989 | bool foundSameNameMethod = false; |
| 3990 | llvm::SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
| 3991 | for (Path.Decls = BaseRecord->lookup(Name); |
| 3992 | Path.Decls.first != Path.Decls.second; |
| 3993 | ++Path.Decls.first) { |
| 3994 | NamedDecl *D = *Path.Decls.first; |
| 3995 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 3996 | MD = MD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 3997 | foundSameNameMethod = true; |
| 3998 | // Interested only in hidden virtual methods. |
| 3999 | if (!MD->isVirtual()) |
| 4000 | continue; |
| 4001 | // If the method we are checking overrides a method from its base |
| 4002 | // don't warn about the other overloaded methods. |
| 4003 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 4004 | return true; |
| 4005 | // Collect the overload only if its hidden. |
| 4006 | if (!Data.OverridenAndUsingBaseMethods.count(MD)) |
| 4007 | overloadedMethods.push_back(MD); |
| 4008 | } |
| 4009 | } |
| 4010 | |
| 4011 | if (foundSameNameMethod) |
| 4012 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 4013 | overloadedMethods.end()); |
| 4014 | return foundSameNameMethod; |
| 4015 | } |
| 4016 | |
| 4017 | /// \brief See if a method overloads virtual methods in a base class without |
| 4018 | /// overriding any. |
| 4019 | void Sema::DiagnoseHiddenVirtualMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
| 4020 | if (Diags.getDiagnosticLevel(diag::warn_overloaded_virtual, |
| 4021 | MD->getLocation()) == Diagnostic::Ignored) |
| 4022 | return; |
| 4023 | if (MD->getDeclName().getNameKind() != DeclarationName::Identifier) |
| 4024 | return; |
| 4025 | |
| 4026 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 4027 | /*bool RecordPaths=*/false, |
| 4028 | /*bool DetectVirtual=*/false); |
| 4029 | FindHiddenVirtualMethodData Data; |
| 4030 | Data.Method = MD; |
| 4031 | Data.S = this; |
| 4032 | |
| 4033 | // Keep the base methods that were overriden or introduced in the subclass |
| 4034 | // by 'using' in a set. A base method not in this set is hidden. |
| 4035 | for (DeclContext::lookup_result res = DC->lookup(MD->getDeclName()); |
| 4036 | res.first != res.second; ++res.first) { |
| 4037 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*res.first)) |
| 4038 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 4039 | E = MD->end_overridden_methods(); |
| 4040 | I != E; ++I) |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 4041 | Data.OverridenAndUsingBaseMethods.insert((*I)->getCanonicalDecl()); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4042 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*res.first)) |
| 4043 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(shad->getTargetDecl())) |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 4044 | Data.OverridenAndUsingBaseMethods.insert(MD->getCanonicalDecl()); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths) && |
| 4048 | !Data.OverloadedMethods.empty()) { |
| 4049 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 4050 | << MD << (Data.OverloadedMethods.size() > 1); |
| 4051 | |
| 4052 | for (unsigned i = 0, e = Data.OverloadedMethods.size(); i != e; ++i) { |
| 4053 | CXXMethodDecl *overloadedMD = Data.OverloadedMethods[i]; |
| 4054 | Diag(overloadedMD->getLocation(), |
| 4055 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
| 4056 | } |
| 4057 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4060 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4061 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4062 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 4063 | SourceLocation RBrac, |
| 4064 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4065 | if (!TagDecl) |
| 4066 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4067 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4068 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4069 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4070 | ActOnFields(S, RLoc, TagDecl, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4071 | // strict aliasing violation! |
| 4072 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 4073 | FieldCollector->getCurNumFields(), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4074 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4075 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4076 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4079 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 4080 | /// special functions, such as the default constructor, copy |
| 4081 | /// constructor, or destructor, to the given C++ class (C++ |
| 4082 | /// [special]p1). This routine can only be executed just before the |
| 4083 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4084 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4085 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4086 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4087 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 4088 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 4089 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4090 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4091 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 4092 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
| 4093 | |
| 4094 | // If we have a dynamic class, then the copy assignment operator may be |
| 4095 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
| 4096 | // it shows up in the right place in the vtable and that we diagnose |
| 4097 | // problems with the implicit exception specification. |
| 4098 | if (ClassDecl->isDynamicClass()) |
| 4099 | DeclareImplicitCopyAssignment(ClassDecl); |
| 4100 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4101 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4102 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 4103 | ++ASTContext::NumImplicitDestructors; |
| 4104 | |
| 4105 | // If we have a dynamic class, then the destructor may be virtual, so we |
| 4106 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 4107 | // shows up in the right place in the vtable and that we diagnose problems |
| 4108 | // with the implicit exception specification. |
| 4109 | if (ClassDecl->isDynamicClass()) |
| 4110 | DeclareImplicitDestructor(ClassDecl); |
| 4111 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4114 | void Sema::ActOnReenterDeclaratorTemplateScope(Scope *S, DeclaratorDecl *D) { |
| 4115 | if (!D) |
| 4116 | return; |
| 4117 | |
| 4118 | int NumParamList = D->getNumTemplateParameterLists(); |
| 4119 | for (int i = 0; i < NumParamList; i++) { |
| 4120 | TemplateParameterList* Params = D->getTemplateParameterList(i); |
| 4121 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 4122 | ParamEnd = Params->end(); |
| 4123 | Param != ParamEnd; ++Param) { |
| 4124 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 4125 | if (Named->getDeclName()) { |
| 4126 | S->AddDecl(Named); |
| 4127 | IdResolver.AddDecl(Named); |
| 4128 | } |
| 4129 | } |
| 4130 | } |
| 4131 | } |
| 4132 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4133 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 4134 | if (!D) |
| 4135 | return; |
| 4136 | |
| 4137 | TemplateParameterList *Params = 0; |
| 4138 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 4139 | Params = Template->getTemplateParameters(); |
| 4140 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 4141 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 4142 | Params = PartialSpec->getTemplateParameters(); |
| 4143 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 4144 | return; |
| 4145 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 4146 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 4147 | ParamEnd = Params->end(); |
| 4148 | Param != ParamEnd; ++Param) { |
| 4149 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 4150 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4151 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 4152 | IdResolver.AddDecl(Named); |
| 4153 | } |
| 4154 | } |
| 4155 | } |
| 4156 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4157 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 4158 | if (!RecordD) return; |
| 4159 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4160 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 4161 | PushDeclContext(S, Record); |
| 4162 | } |
| 4163 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4164 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 4165 | if (!RecordD) return; |
| 4166 | PopDeclContext(); |
| 4167 | } |
| 4168 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4169 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 4170 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 4171 | /// parsing those parts of the given Method declaration that could |
| 4172 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 4173 | /// arguments. This action should enter the scope of the given |
| 4174 | /// Method declaration as if we had just parsed the qualified method |
| 4175 | /// name. However, it should not bring the parameters into scope; |
| 4176 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4177 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 4181 | /// C++ method declaration. We're (re-)introducing the given |
| 4182 | /// function parameter into scope for use in parsing later parts of |
| 4183 | /// the method declaration. For example, we could see an |
| 4184 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4185 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4186 | if (!ParamD) |
| 4187 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4188 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4189 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4190 | |
| 4191 | // If this parameter has an unparsed default argument, clear it out |
| 4192 | // to make way for the parsed default argument. |
| 4193 | if (Param->hasUnparsedDefaultArg()) |
| 4194 | Param->setDefaultArg(0); |
| 4195 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4196 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4197 | if (Param->getDeclName()) |
| 4198 | IdResolver.AddDecl(Param); |
| 4199 | } |
| 4200 | |
| 4201 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 4202 | /// processing the delayed method declaration for Method. The method |
| 4203 | /// declaration is now considered finished. There may be a separate |
| 4204 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 4205 | /// immediately!) for this method, if it was also defined inside the |
| 4206 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4207 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 4208 | if (!MethodD) |
| 4209 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4210 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4211 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4212 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4213 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4214 | |
| 4215 | // Now that we have our default arguments, check the constructor |
| 4216 | // again. It could produce additional diagnostics or affect whether |
| 4217 | // the class has implicitly-declared destructors, among other |
| 4218 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4219 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 4220 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4221 | |
| 4222 | // Check the default arguments, which we may have added. |
| 4223 | if (!Method->isInvalidDecl()) |
| 4224 | CheckCXXDefaultArguments(Method); |
| 4225 | } |
| 4226 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4227 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4228 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4229 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4230 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 4231 | /// will be updated to reflect a well-formed type for the constructor and |
| 4232 | /// returned. |
| 4233 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4234 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4235 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4236 | |
| 4237 | // C++ [class.ctor]p3: |
| 4238 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 4239 | // constructor can be invoked for a const, volatile or const |
| 4240 | // volatile object. A constructor shall not be declared const, |
| 4241 | // volatile, or const volatile (9.3.2). |
| 4242 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4243 | if (!D.isInvalidType()) |
| 4244 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 4245 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 4246 | << SourceRange(D.getIdentifierLoc()); |
| 4247 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4248 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4249 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4250 | if (!D.isInvalidType()) |
| 4251 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 4252 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 4253 | << SourceRange(D.getIdentifierLoc()); |
| 4254 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4255 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4256 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4257 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4258 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4259 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4260 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4261 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 4262 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4263 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4264 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 4265 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4266 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4267 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 4268 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4269 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4270 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4271 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 4272 | // C++0x [class.ctor]p4: |
| 4273 | // A constructor shall not be declared with a ref-qualifier. |
| 4274 | if (FTI.hasRefQualifier()) { |
| 4275 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 4276 | << FTI.RefQualifierIsLValueRef |
| 4277 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 4278 | D.setInvalidType(); |
| 4279 | } |
| 4280 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4281 | // Rebuild the function type "R" without any type qualifiers (in |
| 4282 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4283 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4284 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4285 | if (Proto->getResultType() == Context.VoidTy && !D.isInvalidType()) |
| 4286 | return R; |
| 4287 | |
| 4288 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 4289 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 4290 | EPI.RefQualifier = RQ_None; |
| 4291 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4292 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4293 | Proto->getNumArgs(), EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4296 | /// CheckConstructor - Checks a fully-formed constructor for |
| 4297 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 4298 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4299 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4300 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 4301 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 4302 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4303 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4304 | |
| 4305 | // C++ [class.copy]p3: |
| 4306 | // A declaration of a constructor for a class X is ill-formed if |
| 4307 | // its first parameter is of type (optionally cv-qualified) X and |
| 4308 | // either there are no other parameters or else all other |
| 4309 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 4310 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4311 | ((Constructor->getNumParams() == 1) || |
| 4312 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4313 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 4314 | Constructor->getTemplateSpecializationKind() |
| 4315 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4316 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 4317 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 4318 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4319 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 4320 | const char *ConstRef |
| 4321 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 4322 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4323 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 4324 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 4325 | |
| 4326 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 4327 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4328 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4329 | } |
| 4330 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 4333 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 4334 | /// well-formedness, issuing any diagnostics required. Returns true |
| 4335 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 4336 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4337 | CXXRecordDecl *RD = Destructor->getParent(); |
| 4338 | |
| 4339 | if (Destructor->isVirtual()) { |
| 4340 | SourceLocation Loc; |
| 4341 | |
| 4342 | if (!Destructor->isImplicit()) |
| 4343 | Loc = Destructor->getLocation(); |
| 4344 | else |
| 4345 | Loc = RD->getLocation(); |
| 4346 | |
| 4347 | // If we have a virtual destructor, look up the deallocation function |
| 4348 | FunctionDecl *OperatorDelete = 0; |
| 4349 | DeclarationName Name = |
| 4350 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 4351 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4352 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 4353 | |
| 4354 | MarkDeclarationReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4355 | |
| 4356 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4357 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4358 | |
| 4359 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4360 | } |
| 4361 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 4363 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 4364 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 4365 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4366 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4369 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 4370 | /// the well-formednes of the destructor declarator @p D with type @p |
| 4371 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4372 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 4373 | /// will be updated to reflect a well-formed type for the destructor and |
| 4374 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4375 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4376 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4377 | // C++ [class.dtor]p1: |
| 4378 | // [...] A typedef-name that names a class is a class-name |
| 4379 | // (7.1.3); however, a typedef-name that names a class shall not |
| 4380 | // be used as the identifier in the declarator for a destructor |
| 4381 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4382 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4383 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4384 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4385 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4386 | else if (const TemplateSpecializationType *TST = |
| 4387 | DeclaratorType->getAs<TemplateSpecializationType>()) |
| 4388 | if (TST->isTypeAlias()) |
| 4389 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
| 4390 | << DeclaratorType << 1; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4391 | |
| 4392 | // C++ [class.dtor]p2: |
| 4393 | // A destructor is used to destroy objects of its class type. A |
| 4394 | // destructor takes no parameters, and no return type can be |
| 4395 | // specified for it (not even void). The address of a destructor |
| 4396 | // shall not be taken. A destructor shall not be static. A |
| 4397 | // destructor can be invoked for a const, volatile or const |
| 4398 | // volatile object. A destructor shall not be declared const, |
| 4399 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4400 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4401 | if (!D.isInvalidType()) |
| 4402 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 4403 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4404 | << SourceRange(D.getIdentifierLoc()) |
| 4405 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 4406 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4407 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4408 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4409 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4410 | // Destructors don't have return types, but the parser will |
| 4411 | // happily parse something like: |
| 4412 | // |
| 4413 | // class X { |
| 4414 | // float ~X(); |
| 4415 | // }; |
| 4416 | // |
| 4417 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4418 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 4419 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 4420 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4422 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4423 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4424 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4425 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4426 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 4427 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4428 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4429 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 4430 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4431 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4432 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 4433 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4434 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 4437 | // C++0x [class.dtor]p2: |
| 4438 | // A destructor shall not be declared with a ref-qualifier. |
| 4439 | if (FTI.hasRefQualifier()) { |
| 4440 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 4441 | << FTI.RefQualifierIsLValueRef |
| 4442 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 4443 | D.setInvalidType(); |
| 4444 | } |
| 4445 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4446 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 4447 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4448 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 4449 | |
| 4450 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4451 | FTI.freeArgs(); |
| 4452 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4455 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4456 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4457 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 4458 | D.setInvalidType(); |
| 4459 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4460 | |
| 4461 | // Rebuild the function type "R" without any type qualifiers or |
| 4462 | // parameters (in case any of the errors above fired) and with |
| 4463 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4464 | // types. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4465 | if (!D.isInvalidType()) |
| 4466 | return R; |
| 4467 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 4468 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4469 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 4470 | EPI.Variadic = false; |
| 4471 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 4472 | EPI.RefQualifier = RQ_None; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4473 | return Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4476 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 4477 | /// well-formednes of the conversion function declarator @p D with |
| 4478 | /// type @p R. If there are any errors in the declarator, this routine |
| 4479 | /// will emit diagnostics and return true. Otherwise, it will return |
| 4480 | /// false. Either way, the type @p R will be updated to reflect a |
| 4481 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4482 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4483 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4484 | // C++ [class.conv.fct]p1: |
| 4485 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4486 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4487 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4488 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4489 | if (!D.isInvalidType()) |
| 4490 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 4491 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 4492 | << SourceRange(D.getIdentifierLoc()); |
| 4493 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4494 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4495 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 4496 | |
| 4497 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 4498 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4499 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4500 | // Conversion functions don't have return types, but the parser will |
| 4501 | // happily parse something like: |
| 4502 | // |
| 4503 | // class X { |
| 4504 | // float operator bool(); |
| 4505 | // }; |
| 4506 | // |
| 4507 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4508 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 4509 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 4510 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 4511 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 4514 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 4515 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4516 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 4517 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4518 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 4519 | |
| 4520 | // Delete the parameters. |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4521 | D.getFunctionTypeInfo().freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4522 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 4523 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4524 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4525 | D.setInvalidType(); |
| 4526 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4527 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 4528 | // Diagnose "&operator bool()" and other such nonsense. This |
| 4529 | // is actually a gcc extension which we don't support. |
| 4530 | if (Proto->getResultType() != ConvType) { |
| 4531 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 4532 | << Proto->getResultType(); |
| 4533 | D.setInvalidType(); |
| 4534 | ConvType = Proto->getResultType(); |
| 4535 | } |
| 4536 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4537 | // C++ [class.conv.fct]p4: |
| 4538 | // The conversion-type-id shall not represent a function type nor |
| 4539 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4540 | if (ConvType->isArrayType()) { |
| 4541 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 4542 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4543 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4544 | } else if (ConvType->isFunctionType()) { |
| 4545 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 4546 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 4547 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
| 4550 | // Rebuild the function type "R" without any parameters (in case any |
| 4551 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4552 | // return type. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4553 | if (D.isInvalidType()) |
| 4554 | R = Context.getFunctionType(ConvType, 0, 0, Proto->getExtProtoInfo()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4555 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4556 | // C++0x explicit conversion operators. |
| 4557 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4558 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4559 | diag::warn_explicit_conversion_functions) |
| 4560 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4563 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 4564 | /// the declaration of the given C++ conversion function. This routine |
| 4565 | /// is responsible for recording the conversion function in the C++ |
| 4566 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4567 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4568 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 4569 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 4570 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4571 | |
| 4572 | // Make sure we aren't redeclaring the conversion function. |
| 4573 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4574 | |
| 4575 | // C++ [class.conv.fct]p1: |
| 4576 | // [...] A conversion function is never used to convert a |
| 4577 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 4578 | // same object type (or a reference to it), to a (possibly |
| 4579 | // cv-qualified) base class of that type (or a reference to it), |
| 4580 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4581 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 4582 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4583 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4584 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4585 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4586 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 4587 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 4588 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 4589 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 4590 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4591 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 4592 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 4593 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4594 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4595 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 4596 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4597 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4598 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 4599 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4600 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 4603 | if (FunctionTemplateDecl *ConversionTemplate |
| 4604 | = Conversion->getDescribedFunctionTemplate()) |
| 4605 | return ConversionTemplate; |
| 4606 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4607 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4610 | //===----------------------------------------------------------------------===// |
| 4611 | // Namespace Handling |
| 4612 | //===----------------------------------------------------------------------===// |
| 4613 | |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 4614 | |
| 4615 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4616 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 4617 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4618 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 4619 | SourceLocation InlineLoc, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 4620 | SourceLocation NamespaceLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 4621 | SourceLocation IdentLoc, |
| 4622 | IdentifierInfo *II, |
| 4623 | SourceLocation LBrace, |
| 4624 | AttributeList *AttrList) { |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 4625 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 4626 | // For anonymous namespace, take the location of the left brace. |
| 4627 | SourceLocation Loc = II ? IdentLoc : LBrace; |
Douglas Gregor | 21e09b6 | 2010-08-19 20:55:47 +0000 | [diff] [blame] | 4628 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 4629 | StartLoc, Loc, II); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 4630 | Namespc->setInline(InlineLoc.isValid()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4631 | |
| 4632 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 4633 | |
Anders Carlsson | 2a3503d | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 4634 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 4635 | |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 4636 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
| 4637 | PushNamespaceVisibilityAttr(Attr); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 4638 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4639 | if (II) { |
| 4640 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 4641 | // The identifier in an original-namespace-definition shall not |
| 4642 | // have been previously defined in the declarative region in |
| 4643 | // which the original-namespace-definition appears. The |
| 4644 | // identifier in an original-namespace-definition is the name of |
| 4645 | // the namespace. Subsequently in that declarative region, it is |
| 4646 | // treated as an original-namespace-name. |
| 4647 | // |
| 4648 | // Since namespace names are unique in their scope, and we don't |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 4649 | // look through using directives, just look for any ordinary names. |
| 4650 | |
| 4651 | const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Member | |
| 4652 | Decl::IDNS_Type | Decl::IDNS_Using | Decl::IDNS_Tag | |
| 4653 | Decl::IDNS_Namespace; |
| 4654 | NamedDecl *PrevDecl = 0; |
| 4655 | for (DeclContext::lookup_result R |
| 4656 | = CurContext->getRedeclContext()->lookup(II); |
| 4657 | R.first != R.second; ++R.first) { |
| 4658 | if ((*R.first)->getIdentifierNamespace() & IDNS) { |
| 4659 | PrevDecl = *R.first; |
| 4660 | break; |
| 4661 | } |
| 4662 | } |
| 4663 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4664 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 4665 | // This is an extended namespace definition. |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 4666 | if (Namespc->isInline() != OrigNS->isInline()) { |
| 4667 | // inline-ness must match |
Douglas Gregor | b7ec906 | 2011-05-20 15:48:31 +0000 | [diff] [blame] | 4668 | if (OrigNS->isInline()) { |
| 4669 | // The user probably just forgot the 'inline', so suggest that it |
| 4670 | // be added back. |
| 4671 | Diag(Namespc->getLocation(), |
| 4672 | diag::warn_inline_namespace_reopened_noninline) |
| 4673 | << FixItHint::CreateInsertion(NamespaceLoc, "inline "); |
| 4674 | } else { |
| 4675 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 4676 | << Namespc->isInline(); |
| 4677 | } |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 4678 | Diag(OrigNS->getLocation(), diag::note_previous_definition); |
Douglas Gregor | b7ec906 | 2011-05-20 15:48:31 +0000 | [diff] [blame] | 4679 | |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 4680 | // Recover by ignoring the new namespace's inline status. |
| 4681 | Namespc->setInline(OrigNS->isInline()); |
| 4682 | } |
| 4683 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4684 | // Attach this namespace decl to the chain of extended namespace |
| 4685 | // definitions. |
| 4686 | OrigNS->setNextNamespace(Namespc); |
| 4687 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4688 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4689 | // Remove the previous declaration from the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4690 | if (DeclRegionScope->isDeclScope(OrigNS)) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 4691 | IdResolver.RemoveDecl(OrigNS); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4692 | DeclRegionScope->RemoveDecl(OrigNS); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4693 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4694 | } else if (PrevDecl) { |
| 4695 | // This is an invalid name redefinition. |
| 4696 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 4697 | << Namespc->getDeclName(); |
| 4698 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 4699 | Namespc->setInvalidDecl(); |
| 4700 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 4701 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4702 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 4703 | // This is the first "real" definition of the namespace "std", so update |
| 4704 | // our cache of the "std" namespace to point at this definition. |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 4705 | if (NamespaceDecl *StdNS = getStdNamespace()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 4706 | // We had already defined a dummy namespace "std". Link this new |
| 4707 | // namespace definition to the dummy namespace "std". |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 4708 | StdNS->setNextNamespace(Namespc); |
| 4709 | StdNS->setLocation(IdentLoc); |
| 4710 | Namespc->setOriginalNamespace(StdNS->getOriginalNamespace()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | // Make our StdNamespace cache point at the first real definition of the |
| 4714 | // "std" namespace. |
| 4715 | StdNamespace = Namespc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4716 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4717 | |
| 4718 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 4719 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 4720 | // Anonymous namespaces. |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 4721 | assert(Namespc->isAnonymousNamespace()); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 4722 | |
| 4723 | // Link the anonymous namespace into its parent. |
| 4724 | NamespaceDecl *PrevDecl; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4725 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 4726 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 4727 | PrevDecl = TU->getAnonymousNamespace(); |
| 4728 | TU->setAnonymousNamespace(Namespc); |
| 4729 | } else { |
| 4730 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 4731 | PrevDecl = ND->getAnonymousNamespace(); |
| 4732 | ND->setAnonymousNamespace(Namespc); |
| 4733 | } |
| 4734 | |
| 4735 | // Link the anonymous namespace with its previous declaration. |
| 4736 | if (PrevDecl) { |
| 4737 | assert(PrevDecl->isAnonymousNamespace()); |
| 4738 | assert(!PrevDecl->getNextNamespace()); |
| 4739 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 4740 | PrevDecl->setNextNamespace(Namespc); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 4741 | |
| 4742 | if (Namespc->isInline() != PrevDecl->isInline()) { |
| 4743 | // inline-ness must match |
| 4744 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 4745 | << Namespc->isInline(); |
| 4746 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 4747 | Namespc->setInvalidDecl(); |
| 4748 | // Recover by ignoring the new namespace's inline status. |
| 4749 | Namespc->setInline(PrevDecl->isInline()); |
| 4750 | } |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 4751 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 4752 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 4753 | CurContext->addDecl(Namespc); |
| 4754 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 4755 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 4756 | // behaves as if it were replaced by |
| 4757 | // namespace unique { /* empty body */ } |
| 4758 | // using namespace unique; |
| 4759 | // namespace unique { namespace-body } |
| 4760 | // where all occurrences of 'unique' in a translation unit are |
| 4761 | // replaced by the same identifier and this identifier differs |
| 4762 | // from all other identifiers in the entire program. |
| 4763 | |
| 4764 | // We just create the namespace with an empty name and then add an |
| 4765 | // implicit using declaration, just like the standard suggests. |
| 4766 | // |
| 4767 | // CodeGen enforces the "universally unique" aspect by giving all |
| 4768 | // declarations semantically contained within an anonymous |
| 4769 | // namespace internal linkage. |
| 4770 | |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 4771 | if (!PrevDecl) { |
| 4772 | UsingDirectiveDecl* UD |
| 4773 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 4774 | /* 'using' */ LBrace, |
| 4775 | /* 'namespace' */ SourceLocation(), |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 4776 | /* qualifier */ NestedNameSpecifierLoc(), |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 4777 | /* identifier */ SourceLocation(), |
| 4778 | Namespc, |
| 4779 | /* Ancestor */ CurContext); |
| 4780 | UD->setImplicit(); |
| 4781 | CurContext->addDecl(UD); |
| 4782 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 4786 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4787 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 4788 | // for the namespace has the declarations that showed up in that particular |
| 4789 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4790 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4791 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 4794 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 4795 | /// is a namespace alias, returns the namespace it points to. |
| 4796 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 4797 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 4798 | return AD->getNamespace(); |
| 4799 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 4800 | } |
| 4801 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4802 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 4803 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4804 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4805 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 4806 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 4807 | Namespc->setRBraceLoc(RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4808 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 4809 | if (Namespc->hasAttr<VisibilityAttr>()) |
| 4810 | PopPragmaVisibility(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 4811 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4812 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 4813 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 4814 | return cast_or_null<CXXRecordDecl>( |
| 4815 | StdBadAlloc.get(Context.getExternalSource())); |
| 4816 | } |
| 4817 | |
| 4818 | NamespaceDecl *Sema::getStdNamespace() const { |
| 4819 | return cast_or_null<NamespaceDecl>( |
| 4820 | StdNamespace.get(Context.getExternalSource())); |
| 4821 | } |
| 4822 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4823 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 4824 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 4825 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4826 | if (!StdNamespace) { |
| 4827 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 4828 | StdNamespace = NamespaceDecl::Create(Context, |
| 4829 | Context.getTranslationUnitDecl(), |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 4830 | SourceLocation(), SourceLocation(), |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4831 | &PP.getIdentifierTable().get("std")); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 4832 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 4835 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4836 | } |
| 4837 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 4838 | /// \brief Determine whether a using statement is in a context where it will be |
| 4839 | /// apply in all contexts. |
| 4840 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 4841 | switch (CurContext->getDeclKind()) { |
| 4842 | case Decl::TranslationUnit: |
| 4843 | return true; |
| 4844 | case Decl::LinkageSpec: |
| 4845 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 4846 | default: |
| 4847 | return false; |
| 4848 | } |
| 4849 | } |
| 4850 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4851 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4852 | SourceLocation UsingLoc, |
| 4853 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4854 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4855 | SourceLocation IdentLoc, |
| 4856 | IdentifierInfo *NamespcName, |
| 4857 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 4858 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 4859 | assert(NamespcName && "Invalid NamespcName."); |
| 4860 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 4861 | |
| 4862 | // This can only happen along a recovery path. |
| 4863 | while (S->getFlags() & Scope::TemplateParamScope) |
| 4864 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4865 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 4866 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4867 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4868 | NestedNameSpecifier *Qualifier = 0; |
| 4869 | if (SS.isSet()) |
| 4870 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 4871 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 4872 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4873 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 4874 | LookupParsedName(R, S, &SS); |
| 4875 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4876 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4877 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4878 | if (R.empty()) { |
| 4879 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 4880 | // "std" hasn't been defined yet, for GCC compatibility. |
| 4881 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 4882 | NamespcName->isStr("std")) { |
| 4883 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 4884 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4885 | R.resolveKind(); |
| 4886 | } |
| 4887 | // Otherwise, attempt typo correction. |
| 4888 | else if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 4889 | CTC_NoKeywords, 0)) { |
| 4890 | if (R.getAsSingle<NamespaceDecl>() || |
| 4891 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 4892 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 4893 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 4894 | << NamespcName << DC << Corrected << SS.getRange() |
| 4895 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4896 | else |
| 4897 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 4898 | << NamespcName << Corrected |
| 4899 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4900 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 4901 | << Corrected; |
| 4902 | |
| 4903 | NamespcName = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 4904 | } else { |
| 4905 | R.clear(); |
| 4906 | R.setLookupName(NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 4907 | } |
| 4908 | } |
| 4909 | } |
| 4910 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4911 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 4912 | NamedDecl *Named = R.getFoundDecl(); |
| 4913 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 4914 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4915 | // C++ [namespace.udir]p1: |
| 4916 | // A using-directive specifies that the names in the nominated |
| 4917 | // namespace can be used in the scope in which the |
| 4918 | // using-directive appears after the using-directive. During |
| 4919 | // unqualified name lookup (3.4.1), the names appear as if they |
| 4920 | // were declared in the nearest enclosing namespace which |
| 4921 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4922 | // namespace. [Note: in this context, "contains" means "contains |
| 4923 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4924 | |
| 4925 | // Find enclosing context containing both using-directive and |
| 4926 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 4927 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4928 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 4929 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 4930 | CommonAncestor = CommonAncestor->getParent(); |
| 4931 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 4932 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 4933 | SS.getWithLocInContext(Context), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 4934 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 4935 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 4936 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
Nico Weber | 2166948 | 2011-04-02 19:45:15 +0000 | [diff] [blame] | 4937 | !SourceMgr.isFromMainFile(SourceMgr.getInstantiationLoc(IdentLoc))) { |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 4938 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 4939 | } |
| 4940 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4941 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 4942 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 4943 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 4944 | } |
| 4945 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4946 | // FIXME: We ignore attributes for now. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4947 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
| 4950 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 4951 | // If scope has associated entity, then using directive is at namespace |
| 4952 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 4953 | // it's lookup structure. |
| 4954 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4955 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 4956 | else |
| 4957 | // Otherwise it is block-sope. using-directives will affect lookup |
| 4958 | // only to the end of scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4959 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 4960 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4961 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 4962 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4963 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 4964 | AccessSpecifier AS, |
| 4965 | bool HasUsingKeyword, |
| 4966 | SourceLocation UsingLoc, |
| 4967 | CXXScopeSpec &SS, |
| 4968 | UnqualifiedId &Name, |
| 4969 | AttributeList *AttrList, |
| 4970 | bool IsTypeName, |
| 4971 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 4972 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4973 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 4974 | switch (Name.getKind()) { |
| 4975 | case UnqualifiedId::IK_Identifier: |
| 4976 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 4977 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 4978 | case UnqualifiedId::IK_ConversionFunctionId: |
| 4979 | break; |
| 4980 | |
| 4981 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4982 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4983 | // C++0x inherited constructors. |
| 4984 | if (getLangOptions().CPlusPlus0x) break; |
| 4985 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 4986 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 4987 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4988 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 4989 | |
| 4990 | case UnqualifiedId::IK_DestructorName: |
| 4991 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 4992 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4993 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 4994 | |
| 4995 | case UnqualifiedId::IK_TemplateId: |
| 4996 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 4997 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4998 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 4999 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5000 | |
| 5001 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 5002 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5003 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5004 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5005 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 5006 | // Warn about using declarations. |
| 5007 | // TODO: store that the declaration was written without 'using' and |
| 5008 | // talk about access decls instead of using decls in the |
| 5009 | // diagnostics. |
| 5010 | if (!HasUsingKeyword) { |
| 5011 | UsingLoc = Name.getSourceRange().getBegin(); |
| 5012 | |
| 5013 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5014 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 5017 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 5018 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 5019 | return 0; |
| 5020 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5021 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5022 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5023 | /* IsInstantiation */ false, |
| 5024 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5025 | if (UD) |
| 5026 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5027 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5028 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 5031 | /// \brief Determine whether a using declaration considers the given |
| 5032 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 5033 | /// the same entity or are both typedefs of the same type. |
| 5034 | static bool |
| 5035 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 5036 | bool &SuppressRedeclaration) { |
| 5037 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 5038 | SuppressRedeclaration = false; |
| 5039 | return true; |
| 5040 | } |
| 5041 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5042 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
| 5043 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 5044 | SuppressRedeclaration = true; |
| 5045 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 5046 | TD2->getUnderlyingType()); |
| 5047 | } |
| 5048 | |
| 5049 | return false; |
| 5050 | } |
| 5051 | |
| 5052 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5053 | /// Determines whether to create a using shadow decl for a particular |
| 5054 | /// decl, given the set of decls existing prior to this using lookup. |
| 5055 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 5056 | const LookupResult &Previous) { |
| 5057 | // Diagnose finding a decl which is not from a base class of the |
| 5058 | // current class. We do this now because there are cases where this |
| 5059 | // function will silently decide not to build a shadow decl, which |
| 5060 | // will pre-empt further diagnostics. |
| 5061 | // |
| 5062 | // We don't need to do this in C++0x because we do the check once on |
| 5063 | // the qualifier. |
| 5064 | // |
| 5065 | // FIXME: diagnose the following if we care enough: |
| 5066 | // struct A { int foo; }; |
| 5067 | // struct B : A { using A::foo; }; |
| 5068 | // template <class T> struct C : A {}; |
| 5069 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 5070 | // This is invalid (during instantiation) in C++03 because B::foo |
| 5071 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 5072 | // We can't diagnose it immediately because C<T> is an unknown |
| 5073 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 5074 | // to A::foo, which will look well-formed when we instantiate. |
| 5075 | // The right solution is to not collapse the shadow-decl chain. |
| 5076 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 5077 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 5078 | |
| 5079 | // Handle enums and anonymous structs. |
| 5080 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 5081 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 5082 | while (OrigRec->isAnonymousStructOrUnion()) |
| 5083 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 5084 | |
| 5085 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 5086 | if (OrigDC == CurContext) { |
| 5087 | Diag(Using->getLocation(), |
| 5088 | diag::err_using_decl_nested_name_specifier_is_current_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5089 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5090 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 5091 | return true; |
| 5092 | } |
| 5093 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5094 | Diag(Using->getQualifierLoc().getBeginLoc(), |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5095 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5096 | << Using->getQualifier() |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5097 | << cast<CXXRecordDecl>(CurContext) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5098 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5099 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 5100 | return true; |
| 5101 | } |
| 5102 | } |
| 5103 | |
| 5104 | if (Previous.empty()) return false; |
| 5105 | |
| 5106 | NamedDecl *Target = Orig; |
| 5107 | if (isa<UsingShadowDecl>(Target)) |
| 5108 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 5109 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 5110 | // If the target happens to be one of the previous declarations, we |
| 5111 | // don't have a conflict. |
| 5112 | // |
| 5113 | // FIXME: but we might be increasing its access, in which case we |
| 5114 | // should redeclare it. |
| 5115 | NamedDecl *NonTag = 0, *Tag = 0; |
| 5116 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5117 | I != E; ++I) { |
| 5118 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 5119 | bool Result; |
| 5120 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 5121 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 5122 | |
| 5123 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 5124 | } |
| 5125 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5126 | if (Target->isFunctionOrFunctionTemplate()) { |
| 5127 | FunctionDecl *FD; |
| 5128 | if (isa<FunctionTemplateDecl>(Target)) |
| 5129 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 5130 | else |
| 5131 | FD = cast<FunctionDecl>(Target); |
| 5132 | |
| 5133 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 5134 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5135 | case Ovl_Overload: |
| 5136 | return false; |
| 5137 | |
| 5138 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5139 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5140 | break; |
| 5141 | |
| 5142 | // We found a decl with the exact signature. |
| 5143 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5144 | // If we're in a record, we want to hide the target, so we |
| 5145 | // return true (without a diagnostic) to tell the caller not to |
| 5146 | // build a shadow decl. |
| 5147 | if (CurContext->isRecord()) |
| 5148 | return true; |
| 5149 | |
| 5150 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5151 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5152 | break; |
| 5153 | } |
| 5154 | |
| 5155 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 5156 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 5157 | return true; |
| 5158 | } |
| 5159 | |
| 5160 | // Target is not a function. |
| 5161 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5162 | if (isa<TagDecl>(Target)) { |
| 5163 | // No conflict between a tag and a non-tag. |
| 5164 | if (!Tag) return false; |
| 5165 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5166 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5167 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 5168 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 5169 | return true; |
| 5170 | } |
| 5171 | |
| 5172 | // No conflict between a tag and a non-tag. |
| 5173 | if (!NonTag) return false; |
| 5174 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5175 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5176 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 5177 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 5178 | return true; |
| 5179 | } |
| 5180 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5181 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5182 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5183 | UsingDecl *UD, |
| 5184 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5185 | |
| 5186 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5187 | NamedDecl *Target = Orig; |
| 5188 | if (isa<UsingShadowDecl>(Target)) { |
| 5189 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 5190 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5191 | } |
| 5192 | |
| 5193 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5194 | = UsingShadowDecl::Create(Context, CurContext, |
| 5195 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5196 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 5197 | |
| 5198 | Shadow->setAccess(UD->getAccess()); |
| 5199 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 5200 | Shadow->setInvalidDecl(); |
| 5201 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5202 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5203 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5204 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5205 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5206 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5207 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5208 | return Shadow; |
| 5209 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5210 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5211 | /// Hides a using shadow declaration. This is required by the current |
| 5212 | /// using-decl implementation when a resolvable using declaration in a |
| 5213 | /// class is followed by a declaration which would hide or override |
| 5214 | /// one or more of the using decl's targets; for example: |
| 5215 | /// |
| 5216 | /// struct Base { void foo(int); }; |
| 5217 | /// struct Derived : Base { |
| 5218 | /// using Base::foo; |
| 5219 | /// void foo(int); |
| 5220 | /// }; |
| 5221 | /// |
| 5222 | /// The governing language is C++03 [namespace.udecl]p12: |
| 5223 | /// |
| 5224 | /// When a using-declaration brings names from a base class into a |
| 5225 | /// derived class scope, member functions in the derived class |
| 5226 | /// override and/or hide member functions with the same name and |
| 5227 | /// parameter types in a base class (rather than conflicting). |
| 5228 | /// |
| 5229 | /// There are two ways to implement this: |
| 5230 | /// (1) optimistically create shadow decls when they're not hidden |
| 5231 | /// by existing declarations, or |
| 5232 | /// (2) don't create any shadow decls (or at least don't make them |
| 5233 | /// visible) until we've fully parsed/instantiated the class. |
| 5234 | /// The problem with (1) is that we might have to retroactively remove |
| 5235 | /// a shadow decl, which requires several O(n) operations because the |
| 5236 | /// decl structures are (very reasonably) not designed for removal. |
| 5237 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 5238 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 5239 | if (Shadow->getDeclName().getNameKind() == |
| 5240 | DeclarationName::CXXConversionFunctionName) |
| 5241 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 5242 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5243 | // Remove it from the DeclContext... |
| 5244 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5245 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5246 | // ...and the scope, if applicable... |
| 5247 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5248 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5249 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5250 | } |
| 5251 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5252 | // ...and the using decl. |
| 5253 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 5254 | |
| 5255 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 5256 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5257 | } |
| 5258 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5259 | /// Builds a using declaration. |
| 5260 | /// |
| 5261 | /// \param IsInstantiation - Whether this call arises from an |
| 5262 | /// instantiation of an unresolved using declaration. We treat |
| 5263 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5264 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 5265 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5266 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5267 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5268 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5269 | bool IsInstantiation, |
| 5270 | bool IsTypeName, |
| 5271 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5272 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5273 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5274 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 5275 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 5276 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5277 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 5278 | if (SS.isEmpty()) { |
| 5279 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 5280 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 5281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5282 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5283 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5284 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5285 | ForRedeclaration); |
| 5286 | Previous.setHideTags(false); |
| 5287 | if (S) { |
| 5288 | LookupName(Previous, S); |
| 5289 | |
| 5290 | // It is really dumb that we have to do this. |
| 5291 | LookupResult::Filter F = Previous.makeFilter(); |
| 5292 | while (F.hasNext()) { |
| 5293 | NamedDecl *D = F.next(); |
| 5294 | if (!isDeclInScope(D, CurContext, S)) |
| 5295 | F.erase(); |
| 5296 | } |
| 5297 | F.done(); |
| 5298 | } else { |
| 5299 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 5300 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 5301 | LookupQualifiedName(Previous, CurContext); |
| 5302 | } |
| 5303 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5304 | // Check for invalid redeclarations. |
| 5305 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 5306 | return 0; |
| 5307 | |
| 5308 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5309 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 5310 | return 0; |
| 5311 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 5312 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5313 | NamedDecl *D; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5314 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 5315 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5316 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5317 | // FIXME: not all declaration name kinds are legal here |
| 5318 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 5319 | UsingLoc, TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5320 | QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5321 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5322 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5323 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 5324 | QualifierLoc, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5325 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5326 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5327 | D = UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, |
| 5328 | NameInfo, IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 5329 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5330 | D->setAccess(AS); |
| 5331 | CurContext->addDecl(D); |
| 5332 | |
| 5333 | if (!LookupContext) return D; |
| 5334 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5335 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5336 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5337 | UD->setInvalidDecl(); |
| 5338 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 5339 | } |
| 5340 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5341 | // Constructor inheriting using decls get special treatment. |
| 5342 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 5343 | if (CheckInheritedConstructorUsingDecl(UD)) |
| 5344 | UD->setInvalidDecl(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5345 | return UD; |
| 5346 | } |
| 5347 | |
| 5348 | // Otherwise, look up the target name. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5349 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5350 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Francois Pichet | b2ee830 | 2011-05-23 03:43:44 +0000 | [diff] [blame] | 5351 | R.setUsingDeclaration(true); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5352 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5353 | // Unlike most lookups, we don't always want to hide tag |
| 5354 | // declarations: tag names are visible through the using declaration |
| 5355 | // even if hidden by ordinary names, *except* in a dependent context |
| 5356 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5357 | if (!IsInstantiation) |
| 5358 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5359 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5360 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5361 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5362 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5363 | Diag(IdentLoc, diag::err_no_member) |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 5364 | << NameInfo.getName() << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5365 | UD->setInvalidDecl(); |
| 5366 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5367 | } |
| 5368 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5369 | if (R.isAmbiguous()) { |
| 5370 | UD->setInvalidDecl(); |
| 5371 | return UD; |
| 5372 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5373 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5374 | if (IsTypeName) { |
| 5375 | // 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] | 5376 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5377 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 5378 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 5379 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 5380 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5381 | UD->setInvalidDecl(); |
| 5382 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5383 | } |
| 5384 | } else { |
| 5385 | // If we asked for a non-typename and we got a type, error out, |
| 5386 | // but only if this is an instantiation of an unresolved using |
| 5387 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5388 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5389 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 5390 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5391 | UD->setInvalidDecl(); |
| 5392 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5393 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 5394 | } |
| 5395 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 5396 | // C++0x N2914 [namespace.udecl]p6: |
| 5397 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5398 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 5399 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 5400 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5401 | UD->setInvalidDecl(); |
| 5402 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 5403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5404 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5405 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 5406 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 5407 | BuildUsingShadowDecl(S, UD, *I); |
| 5408 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 5409 | |
| 5410 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5413 | /// Additional checks for a using declaration referring to a constructor name. |
| 5414 | bool Sema::CheckInheritedConstructorUsingDecl(UsingDecl *UD) { |
| 5415 | if (UD->isTypeName()) { |
| 5416 | // FIXME: Cannot specify typename when specifying constructor |
| 5417 | return true; |
| 5418 | } |
| 5419 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5420 | const Type *SourceType = UD->getQualifier()->getAsType(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5421 | assert(SourceType && |
| 5422 | "Using decl naming constructor doesn't have type in scope spec."); |
| 5423 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 5424 | |
| 5425 | // Check whether the named type is a direct base class. |
| 5426 | CanQualType CanonicalSourceType = SourceType->getCanonicalTypeUnqualified(); |
| 5427 | CXXRecordDecl::base_class_iterator BaseIt, BaseE; |
| 5428 | for (BaseIt = TargetClass->bases_begin(), BaseE = TargetClass->bases_end(); |
| 5429 | BaseIt != BaseE; ++BaseIt) { |
| 5430 | CanQualType BaseType = BaseIt->getType()->getCanonicalTypeUnqualified(); |
| 5431 | if (CanonicalSourceType == BaseType) |
| 5432 | break; |
| 5433 | } |
| 5434 | |
| 5435 | if (BaseIt == BaseE) { |
| 5436 | // Did not find SourceType in the bases. |
| 5437 | Diag(UD->getUsingLocation(), |
| 5438 | diag::err_using_decl_constructor_not_in_direct_base) |
| 5439 | << UD->getNameInfo().getSourceRange() |
| 5440 | << QualType(SourceType, 0) << TargetClass; |
| 5441 | return true; |
| 5442 | } |
| 5443 | |
| 5444 | BaseIt->setInheritConstructors(); |
| 5445 | |
| 5446 | return false; |
| 5447 | } |
| 5448 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5449 | /// Checks that the given using declaration is not an invalid |
| 5450 | /// redeclaration. Note that this is checking only for the using decl |
| 5451 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 5452 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 5453 | bool isTypeName, |
| 5454 | const CXXScopeSpec &SS, |
| 5455 | SourceLocation NameLoc, |
| 5456 | const LookupResult &Prev) { |
| 5457 | // C++03 [namespace.udecl]p8: |
| 5458 | // C++0x [namespace.udecl]p10: |
| 5459 | // A using-declaration is a declaration and can therefore be used |
| 5460 | // repeatedly where (and only where) multiple declarations are |
| 5461 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 5462 | // |
John McCall | 8a72621 | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 5463 | // That's in non-member contexts. |
| 5464 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5465 | return false; |
| 5466 | |
| 5467 | NestedNameSpecifier *Qual |
| 5468 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 5469 | |
| 5470 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 5471 | NamedDecl *D = *I; |
| 5472 | |
| 5473 | bool DTypename; |
| 5474 | NestedNameSpecifier *DQual; |
| 5475 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 5476 | DTypename = UD->isTypeName(); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5477 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5478 | } else if (UnresolvedUsingValueDecl *UD |
| 5479 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 5480 | DTypename = false; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5481 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5482 | } else if (UnresolvedUsingTypenameDecl *UD |
| 5483 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 5484 | DTypename = true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 5485 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5486 | } else continue; |
| 5487 | |
| 5488 | // using decls differ if one says 'typename' and the other doesn't. |
| 5489 | // FIXME: non-dependent using decls? |
| 5490 | if (isTypeName != DTypename) continue; |
| 5491 | |
| 5492 | // using decls differ if they name different scopes (but note that |
| 5493 | // template instantiation can cause this check to trigger when it |
| 5494 | // didn't before instantiation). |
| 5495 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 5496 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 5497 | continue; |
| 5498 | |
| 5499 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 5500 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 5501 | return true; |
| 5502 | } |
| 5503 | |
| 5504 | return false; |
| 5505 | } |
| 5506 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5507 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5508 | /// Checks that the given nested-name qualifier used in a using decl |
| 5509 | /// in the current context is appropriately related to the current |
| 5510 | /// scope. If an error is found, diagnoses it and returns true. |
| 5511 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 5512 | const CXXScopeSpec &SS, |
| 5513 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5514 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5515 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5516 | if (!CurContext->isRecord()) { |
| 5517 | // C++03 [namespace.udecl]p3: |
| 5518 | // C++0x [namespace.udecl]p8: |
| 5519 | // A using-declaration for a class member shall be a member-declaration. |
| 5520 | |
| 5521 | // If we weren't able to compute a valid scope, it must be a |
| 5522 | // dependent class scope. |
| 5523 | if (!NamedContext || NamedContext->isRecord()) { |
| 5524 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 5525 | << SS.getRange(); |
| 5526 | return true; |
| 5527 | } |
| 5528 | |
| 5529 | // Otherwise, everything is known to be fine. |
| 5530 | return false; |
| 5531 | } |
| 5532 | |
| 5533 | // The current scope is a record. |
| 5534 | |
| 5535 | // If the named context is dependent, we can't decide much. |
| 5536 | if (!NamedContext) { |
| 5537 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 5538 | // nested-name-specifier does not refer to a base class, which is |
| 5539 | // still possible in some cases. |
| 5540 | |
| 5541 | // Otherwise we have to conservatively report that things might be |
| 5542 | // okay. |
| 5543 | return false; |
| 5544 | } |
| 5545 | |
| 5546 | if (!NamedContext->isRecord()) { |
| 5547 | // Ideally this would point at the last name in the specifier, |
| 5548 | // but we don't have that level of source info. |
| 5549 | Diag(SS.getRange().getBegin(), |
| 5550 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 5551 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 5552 | return true; |
| 5553 | } |
| 5554 | |
Douglas Gregor | 6fb0729 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 5555 | if (!NamedContext->isDependentContext() && |
| 5556 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 5557 | return true; |
| 5558 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 5559 | if (getLangOptions().CPlusPlus0x) { |
| 5560 | // C++0x [namespace.udecl]p3: |
| 5561 | // In a using-declaration used as a member-declaration, the |
| 5562 | // nested-name-specifier shall name a base class of the class |
| 5563 | // being defined. |
| 5564 | |
| 5565 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 5566 | cast<CXXRecordDecl>(NamedContext))) { |
| 5567 | if (CurContext == NamedContext) { |
| 5568 | Diag(NameLoc, |
| 5569 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 5570 | << SS.getRange(); |
| 5571 | return true; |
| 5572 | } |
| 5573 | |
| 5574 | Diag(SS.getRange().getBegin(), |
| 5575 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 5576 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 5577 | << cast<CXXRecordDecl>(CurContext) |
| 5578 | << SS.getRange(); |
| 5579 | return true; |
| 5580 | } |
| 5581 | |
| 5582 | return false; |
| 5583 | } |
| 5584 | |
| 5585 | // C++03 [namespace.udecl]p4: |
| 5586 | // A using-declaration used as a member-declaration shall refer |
| 5587 | // to a member of a base class of the class being defined [etc.]. |
| 5588 | |
| 5589 | // Salient point: SS doesn't have to name a base class as long as |
| 5590 | // lookup only finds members from base classes. Therefore we can |
| 5591 | // diagnose here only if we can prove that that can't happen, |
| 5592 | // i.e. if the class hierarchies provably don't intersect. |
| 5593 | |
| 5594 | // TODO: it would be nice if "definitely valid" results were cached |
| 5595 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 5596 | // need to be repeated. |
| 5597 | |
| 5598 | struct UserData { |
| 5599 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 5600 | |
| 5601 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 5602 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 5603 | Data->Bases.insert(Base); |
| 5604 | return true; |
| 5605 | } |
| 5606 | |
| 5607 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 5608 | return !Class->forallBases(collect, this); |
| 5609 | } |
| 5610 | |
| 5611 | /// Returns true if the base is dependent or is one of the |
| 5612 | /// accumulated base classes. |
| 5613 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 5614 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 5615 | return !Data->Bases.count(Base); |
| 5616 | } |
| 5617 | |
| 5618 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 5619 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 5620 | } |
| 5621 | }; |
| 5622 | |
| 5623 | UserData Data; |
| 5624 | |
| 5625 | // Returns false if we find a dependent base. |
| 5626 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 5627 | return false; |
| 5628 | |
| 5629 | // Returns false if the class has a dependent base or if it or one |
| 5630 | // of its bases is present in the base set of the current context. |
| 5631 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 5632 | return false; |
| 5633 | |
| 5634 | Diag(SS.getRange().getBegin(), |
| 5635 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 5636 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 5637 | << cast<CXXRecordDecl>(CurContext) |
| 5638 | << SS.getRange(); |
| 5639 | |
| 5640 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5641 | } |
| 5642 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5643 | Decl *Sema::ActOnAliasDeclaration(Scope *S, |
| 5644 | AccessSpecifier AS, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5645 | MultiTemplateParamsArg TemplateParamLists, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5646 | SourceLocation UsingLoc, |
| 5647 | UnqualifiedId &Name, |
| 5648 | TypeResult Type) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5649 | // Skip up to the relevant declaration scope. |
| 5650 | while (S->getFlags() & Scope::TemplateParamScope) |
| 5651 | S = S->getParent(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5652 | assert((S->getFlags() & Scope::DeclScope) && |
| 5653 | "got alias-declaration outside of declaration scope"); |
| 5654 | |
| 5655 | if (Type.isInvalid()) |
| 5656 | return 0; |
| 5657 | |
| 5658 | bool Invalid = false; |
| 5659 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
| 5660 | TypeSourceInfo *TInfo = 0; |
Nick Lewycky | b79bf1d | 2011-05-02 01:07:19 +0000 | [diff] [blame] | 5661 | GetTypeFromParser(Type.get(), &TInfo); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5662 | |
| 5663 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
| 5664 | return 0; |
| 5665 | |
| 5666 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5667 | UPPC_DeclarationType)) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5668 | Invalid = true; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5669 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 5670 | TInfo->getTypeLoc().getBeginLoc()); |
| 5671 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5672 | |
| 5673 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, ForRedeclaration); |
| 5674 | LookupName(Previous, S); |
| 5675 | |
| 5676 | // Warn about shadowing the name of a template parameter. |
| 5677 | if (Previous.isSingleResult() && |
| 5678 | Previous.getFoundDecl()->isTemplateParameter()) { |
| 5679 | if (DiagnoseTemplateParameterShadow(Name.StartLocation, |
| 5680 | Previous.getFoundDecl())) |
| 5681 | Invalid = true; |
| 5682 | Previous.clear(); |
| 5683 | } |
| 5684 | |
| 5685 | assert(Name.Kind == UnqualifiedId::IK_Identifier && |
| 5686 | "name in alias declaration must be an identifier"); |
| 5687 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 5688 | Name.StartLocation, |
| 5689 | Name.Identifier, TInfo); |
| 5690 | |
| 5691 | NewTD->setAccess(AS); |
| 5692 | |
| 5693 | if (Invalid) |
| 5694 | NewTD->setInvalidDecl(); |
| 5695 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5696 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 5697 | Invalid |= NewTD->isInvalidDecl(); |
| 5698 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5699 | bool Redeclaration = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5700 | |
| 5701 | NamedDecl *NewND; |
| 5702 | if (TemplateParamLists.size()) { |
| 5703 | TypeAliasTemplateDecl *OldDecl = 0; |
| 5704 | TemplateParameterList *OldTemplateParams = 0; |
| 5705 | |
| 5706 | if (TemplateParamLists.size() != 1) { |
| 5707 | Diag(UsingLoc, diag::err_alias_template_extra_headers) |
| 5708 | << SourceRange(TemplateParamLists.get()[1]->getTemplateLoc(), |
| 5709 | TemplateParamLists.get()[TemplateParamLists.size()-1]->getRAngleLoc()); |
| 5710 | } |
| 5711 | TemplateParameterList *TemplateParams = TemplateParamLists.get()[0]; |
| 5712 | |
| 5713 | // Only consider previous declarations in the same scope. |
| 5714 | FilterLookupForScope(Previous, CurContext, S, /*ConsiderLinkage*/false, |
| 5715 | /*ExplicitInstantiationOrSpecialization*/false); |
| 5716 | if (!Previous.empty()) { |
| 5717 | Redeclaration = true; |
| 5718 | |
| 5719 | OldDecl = Previous.getAsSingle<TypeAliasTemplateDecl>(); |
| 5720 | if (!OldDecl && !Invalid) { |
| 5721 | Diag(UsingLoc, diag::err_redefinition_different_kind) |
| 5722 | << Name.Identifier; |
| 5723 | |
| 5724 | NamedDecl *OldD = Previous.getRepresentativeDecl(); |
| 5725 | if (OldD->getLocation().isValid()) |
| 5726 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| 5727 | |
| 5728 | Invalid = true; |
| 5729 | } |
| 5730 | |
| 5731 | if (!Invalid && OldDecl && !OldDecl->isInvalidDecl()) { |
| 5732 | if (TemplateParameterListsAreEqual(TemplateParams, |
| 5733 | OldDecl->getTemplateParameters(), |
| 5734 | /*Complain=*/true, |
| 5735 | TPL_TemplateMatch)) |
| 5736 | OldTemplateParams = OldDecl->getTemplateParameters(); |
| 5737 | else |
| 5738 | Invalid = true; |
| 5739 | |
| 5740 | TypeAliasDecl *OldTD = OldDecl->getTemplatedDecl(); |
| 5741 | if (!Invalid && |
| 5742 | !Context.hasSameType(OldTD->getUnderlyingType(), |
| 5743 | NewTD->getUnderlyingType())) { |
| 5744 | // FIXME: The C++0x standard does not clearly say this is ill-formed, |
| 5745 | // but we can't reasonably accept it. |
| 5746 | Diag(NewTD->getLocation(), diag::err_redefinition_different_typedef) |
| 5747 | << 2 << NewTD->getUnderlyingType() << OldTD->getUnderlyingType(); |
| 5748 | if (OldTD->getLocation().isValid()) |
| 5749 | Diag(OldTD->getLocation(), diag::note_previous_definition); |
| 5750 | Invalid = true; |
| 5751 | } |
| 5752 | } |
| 5753 | } |
| 5754 | |
| 5755 | // Merge any previous default template arguments into our parameters, |
| 5756 | // and check the parameter list. |
| 5757 | if (CheckTemplateParameterList(TemplateParams, OldTemplateParams, |
| 5758 | TPC_TypeAliasTemplate)) |
| 5759 | return 0; |
| 5760 | |
| 5761 | TypeAliasTemplateDecl *NewDecl = |
| 5762 | TypeAliasTemplateDecl::Create(Context, CurContext, UsingLoc, |
| 5763 | Name.Identifier, TemplateParams, |
| 5764 | NewTD); |
| 5765 | |
| 5766 | NewDecl->setAccess(AS); |
| 5767 | |
| 5768 | if (Invalid) |
| 5769 | NewDecl->setInvalidDecl(); |
| 5770 | else if (OldDecl) |
| 5771 | NewDecl->setPreviousDeclaration(OldDecl); |
| 5772 | |
| 5773 | NewND = NewDecl; |
| 5774 | } else { |
| 5775 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 5776 | NewND = NewTD; |
| 5777 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5778 | |
| 5779 | if (!Redeclaration) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5780 | PushOnScopeChains(NewND, S); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5781 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5782 | return NewND; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5785 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 5786 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5787 | SourceLocation AliasLoc, |
| 5788 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5789 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 5790 | SourceLocation IdentLoc, |
| 5791 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5792 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 5793 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5794 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 5795 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 5796 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 5797 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 5798 | NamedDecl *PrevDecl |
| 5799 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 5800 | ForRedeclaration); |
| 5801 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 5802 | PrevDecl = 0; |
| 5803 | |
| 5804 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 5805 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5806 | // 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] | 5807 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 5808 | // FIXME: At some point, we'll want to create the (redundant) |
| 5809 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5810 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 5811 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5812 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 5813 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5814 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 5815 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 5816 | diag::err_redefinition_different_kind; |
| 5817 | Diag(AliasLoc, DiagID) << Alias; |
| 5818 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5819 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 5820 | } |
| 5821 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5822 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5823 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5824 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5825 | if (R.empty()) { |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 5826 | if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 5827 | CTC_NoKeywords, 0)) { |
| 5828 | if (R.getAsSingle<NamespaceDecl>() || |
| 5829 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 5830 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 5831 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 5832 | << Ident << DC << Corrected << SS.getRange() |
| 5833 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 5834 | else |
| 5835 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 5836 | << Ident << Corrected |
| 5837 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 5838 | |
| 5839 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 5840 | << Corrected; |
| 5841 | |
| 5842 | Ident = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 5843 | } else { |
| 5844 | R.clear(); |
| 5845 | R.setLookupName(Ident); |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 5846 | } |
| 5847 | } |
| 5848 | |
| 5849 | if (R.empty()) { |
| 5850 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5851 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 5852 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 5853 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5854 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 5855 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5856 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 5857 | Alias, SS.getWithLocInContext(Context), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5858 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5859 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 5860 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5861 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 5862 | } |
| 5863 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5864 | namespace { |
| 5865 | /// \brief Scoped object used to handle the state changes required in Sema |
| 5866 | /// to implicitly define the body of a C++ member function; |
| 5867 | class ImplicitlyDefinedFunctionScope { |
| 5868 | Sema &S; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5869 | Sema::ContextRAII SavedContext; |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5870 | |
| 5871 | public: |
| 5872 | ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method) |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5873 | : S(S), SavedContext(S, Method) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5874 | { |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5875 | S.PushFunctionScope(); |
| 5876 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 5877 | } |
| 5878 | |
| 5879 | ~ImplicitlyDefinedFunctionScope() { |
| 5880 | S.PopExpressionEvaluationContext(); |
| 5881 | S.PopFunctionOrBlockScope(); |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5882 | } |
| 5883 | }; |
| 5884 | } |
| 5885 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 5886 | Sema::ImplicitExceptionSpecification |
| 5887 | Sema::ComputeDefaultedDefaultCtorExceptionSpec(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5888 | // C++ [except.spec]p14: |
| 5889 | // An implicitly declared special member function (Clause 12) shall have an |
| 5890 | // exception-specification. [...] |
| 5891 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 5892 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 5893 | // Direct base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5894 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 5895 | BEnd = ClassDecl->bases_end(); |
| 5896 | B != BEnd; ++B) { |
| 5897 | if (B->isVirtual()) // Handled below. |
| 5898 | continue; |
| 5899 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5900 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 5901 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 5902 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 5903 | // If this is a deleted function, add it anyway. This might be conformant |
| 5904 | // with the standard. This might not. I'm not sure. It might not matter. |
| 5905 | if (Constructor) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5906 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5907 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5908 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 5909 | |
| 5910 | // Virtual base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5911 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 5912 | BEnd = ClassDecl->vbases_end(); |
| 5913 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5914 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 5915 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 5916 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 5917 | // If this is a deleted function, add it anyway. This might be conformant |
| 5918 | // with the standard. This might not. I'm not sure. It might not matter. |
| 5919 | if (Constructor) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5920 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5921 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5922 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 5923 | |
| 5924 | // Field constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5925 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 5926 | FEnd = ClassDecl->field_end(); |
| 5927 | F != FEnd; ++F) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 5928 | if (F->hasInClassInitializer()) { |
| 5929 | if (Expr *E = F->getInClassInitializer()) |
| 5930 | ExceptSpec.CalledExpr(E); |
| 5931 | else if (!F->isInvalidDecl()) |
| 5932 | ExceptSpec.SetDelayed(); |
| 5933 | } else if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5934 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 5935 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 5936 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 5937 | // If this is a deleted function, add it anyway. This might be conformant |
| 5938 | // with the standard. This might not. I'm not sure. It might not matter. |
| 5939 | // In particular, the problem is that this function never gets called. It |
| 5940 | // might just be ill-formed because this function attempts to refer to |
| 5941 | // a deleted function here. |
| 5942 | if (Constructor) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5943 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5944 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5945 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5946 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 5947 | return ExceptSpec; |
| 5948 | } |
| 5949 | |
| 5950 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 5951 | CXXRecordDecl *ClassDecl) { |
| 5952 | // C++ [class.ctor]p5: |
| 5953 | // A default constructor for a class X is a constructor of class X |
| 5954 | // that can be called without an argument. If there is no |
| 5955 | // user-declared constructor for class X, a default constructor is |
| 5956 | // implicitly declared. An implicitly-declared default constructor |
| 5957 | // is an inline public member of its class. |
| 5958 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
| 5959 | "Should not build implicit default constructor!"); |
| 5960 | |
| 5961 | ImplicitExceptionSpecification Spec = |
| 5962 | ComputeDefaultedDefaultCtorExceptionSpec(ClassDecl); |
| 5963 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 5964 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 5965 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5966 | CanQualType ClassType |
| 5967 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5968 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5969 | DeclarationName Name |
| 5970 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5971 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5972 | CXXConstructorDecl *DefaultCon |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5973 | = CXXConstructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5974 | Context.getFunctionType(Context.VoidTy, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5975 | 0, 0, EPI), |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5976 | /*TInfo=*/0, |
| 5977 | /*isExplicit=*/false, |
| 5978 | /*isInline=*/true, |
Sean Hunt | 5f802e5 | 2011-05-06 00:11:07 +0000 | [diff] [blame] | 5979 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5980 | DefaultCon->setAccess(AS_public); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 5981 | DefaultCon->setDefaulted(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5982 | DefaultCon->setImplicit(); |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 5983 | DefaultCon->setTrivial(ClassDecl->hasTrivialDefaultConstructor()); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5984 | |
| 5985 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5986 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
| 5987 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5988 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5989 | PushOnScopeChains(DefaultCon, S, false); |
| 5990 | ClassDecl->addDecl(DefaultCon); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 5991 | |
| 5992 | if (ShouldDeleteDefaultConstructor(DefaultCon)) |
| 5993 | DefaultCon->setDeletedAsWritten(); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5994 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5995 | return DefaultCon; |
| 5996 | } |
| 5997 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 5998 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 5999 | CXXConstructorDecl *Constructor) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 6000 | assert((Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 6001 | !Constructor->doesThisDeclarationHaveABody() && |
| 6002 | !Constructor->isDeleted()) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 6003 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6004 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 6005 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 6006 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 6007 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6008 | ImplicitlyDefinedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 6009 | DiagnosticErrorTrap Trap(Diags); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 6010 | if (SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 6011 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6012 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 6013 | << CXXDefaultConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 6014 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 6015 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 6016 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 6017 | |
| 6018 | SourceLocation Loc = Constructor->getLocation(); |
| 6019 | Constructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 6020 | |
| 6021 | Constructor->setUsed(); |
| 6022 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 6023 | |
| 6024 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 6025 | L->CompletedImplicitDefinition(Constructor); |
| 6026 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 6027 | } |
| 6028 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 6029 | /// Get any existing defaulted default constructor for the given class. Do not |
| 6030 | /// implicitly define one if it does not exist. |
| 6031 | static CXXConstructorDecl *getDefaultedDefaultConstructorUnsafe(Sema &Self, |
| 6032 | CXXRecordDecl *D) { |
| 6033 | ASTContext &Context = Self.Context; |
| 6034 | QualType ClassType = Context.getTypeDeclType(D); |
| 6035 | DeclarationName ConstructorName |
| 6036 | = Context.DeclarationNames.getCXXConstructorName( |
| 6037 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
| 6038 | |
| 6039 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 6040 | for (llvm::tie(Con, ConEnd) = D->lookup(ConstructorName); |
| 6041 | Con != ConEnd; ++Con) { |
| 6042 | // A function template cannot be defaulted. |
| 6043 | if (isa<FunctionTemplateDecl>(*Con)) |
| 6044 | continue; |
| 6045 | |
| 6046 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 6047 | if (Constructor->isDefaultConstructor()) |
| 6048 | return Constructor->isDefaulted() ? Constructor : 0; |
| 6049 | } |
| 6050 | return 0; |
| 6051 | } |
| 6052 | |
| 6053 | void Sema::ActOnFinishDelayedMemberInitializers(Decl *D) { |
| 6054 | if (!D) return; |
| 6055 | AdjustDeclIfTemplate(D); |
| 6056 | |
| 6057 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D); |
| 6058 | CXXConstructorDecl *CtorDecl |
| 6059 | = getDefaultedDefaultConstructorUnsafe(*this, ClassDecl); |
| 6060 | |
| 6061 | if (!CtorDecl) return; |
| 6062 | |
| 6063 | // Compute the exception specification for the default constructor. |
| 6064 | const FunctionProtoType *CtorTy = |
| 6065 | CtorDecl->getType()->castAs<FunctionProtoType>(); |
| 6066 | if (CtorTy->getExceptionSpecType() == EST_Delayed) { |
| 6067 | ImplicitExceptionSpecification Spec = |
| 6068 | ComputeDefaultedDefaultCtorExceptionSpec(ClassDecl); |
| 6069 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 6070 | assert(EPI.ExceptionSpecType != EST_Delayed); |
| 6071 | |
| 6072 | CtorDecl->setType(Context.getFunctionType(Context.VoidTy, 0, 0, EPI)); |
| 6073 | } |
| 6074 | |
| 6075 | // If the default constructor is explicitly defaulted, checking the exception |
| 6076 | // specification is deferred until now. |
| 6077 | if (!CtorDecl->isInvalidDecl() && CtorDecl->isExplicitlyDefaulted() && |
| 6078 | !ClassDecl->isDependentType()) |
| 6079 | CheckExplicitlyDefaultedDefaultConstructor(CtorDecl); |
| 6080 | } |
| 6081 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6082 | void Sema::DeclareInheritedConstructors(CXXRecordDecl *ClassDecl) { |
| 6083 | // We start with an initial pass over the base classes to collect those that |
| 6084 | // inherit constructors from. If there are none, we can forgo all further |
| 6085 | // processing. |
| 6086 | typedef llvm::SmallVector<const RecordType *, 4> BasesVector; |
| 6087 | BasesVector BasesToInheritFrom; |
| 6088 | for (CXXRecordDecl::base_class_iterator BaseIt = ClassDecl->bases_begin(), |
| 6089 | BaseE = ClassDecl->bases_end(); |
| 6090 | BaseIt != BaseE; ++BaseIt) { |
| 6091 | if (BaseIt->getInheritConstructors()) { |
| 6092 | QualType Base = BaseIt->getType(); |
| 6093 | if (Base->isDependentType()) { |
| 6094 | // If we inherit constructors from anything that is dependent, just |
| 6095 | // abort processing altogether. We'll get another chance for the |
| 6096 | // instantiations. |
| 6097 | return; |
| 6098 | } |
| 6099 | BasesToInheritFrom.push_back(Base->castAs<RecordType>()); |
| 6100 | } |
| 6101 | } |
| 6102 | if (BasesToInheritFrom.empty()) |
| 6103 | return; |
| 6104 | |
| 6105 | // Now collect the constructors that we already have in the current class. |
| 6106 | // Those take precedence over inherited constructors. |
| 6107 | // C++0x [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 6108 | // unless there is a user-declared constructor with the same signature in |
| 6109 | // the class where the using-declaration appears. |
| 6110 | llvm::SmallSet<const Type *, 8> ExistingConstructors; |
| 6111 | for (CXXRecordDecl::ctor_iterator CtorIt = ClassDecl->ctor_begin(), |
| 6112 | CtorE = ClassDecl->ctor_end(); |
| 6113 | CtorIt != CtorE; ++CtorIt) { |
| 6114 | ExistingConstructors.insert( |
| 6115 | Context.getCanonicalType(CtorIt->getType()).getTypePtr()); |
| 6116 | } |
| 6117 | |
| 6118 | Scope *S = getScopeForContext(ClassDecl); |
| 6119 | DeclarationName CreatedCtorName = |
| 6120 | Context.DeclarationNames.getCXXConstructorName( |
| 6121 | ClassDecl->getTypeForDecl()->getCanonicalTypeUnqualified()); |
| 6122 | |
| 6123 | // Now comes the true work. |
| 6124 | // First, we keep a map from constructor types to the base that introduced |
| 6125 | // them. Needed for finding conflicting constructors. We also keep the |
| 6126 | // actually inserted declarations in there, for pretty diagnostics. |
| 6127 | typedef std::pair<CanQualType, CXXConstructorDecl *> ConstructorInfo; |
| 6128 | typedef llvm::DenseMap<const Type *, ConstructorInfo> ConstructorToSourceMap; |
| 6129 | ConstructorToSourceMap InheritedConstructors; |
| 6130 | for (BasesVector::iterator BaseIt = BasesToInheritFrom.begin(), |
| 6131 | BaseE = BasesToInheritFrom.end(); |
| 6132 | BaseIt != BaseE; ++BaseIt) { |
| 6133 | const RecordType *Base = *BaseIt; |
| 6134 | CanQualType CanonicalBase = Base->getCanonicalTypeUnqualified(); |
| 6135 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(Base->getDecl()); |
| 6136 | for (CXXRecordDecl::ctor_iterator CtorIt = BaseDecl->ctor_begin(), |
| 6137 | CtorE = BaseDecl->ctor_end(); |
| 6138 | CtorIt != CtorE; ++CtorIt) { |
| 6139 | // Find the using declaration for inheriting this base's constructors. |
| 6140 | DeclarationName Name = |
| 6141 | Context.DeclarationNames.getCXXConstructorName(CanonicalBase); |
| 6142 | UsingDecl *UD = dyn_cast_or_null<UsingDecl>( |
| 6143 | LookupSingleName(S, Name,SourceLocation(), LookupUsingDeclName)); |
| 6144 | SourceLocation UsingLoc = UD ? UD->getLocation() : |
| 6145 | ClassDecl->getLocation(); |
| 6146 | |
| 6147 | // C++0x [class.inhctor]p1: The candidate set of inherited constructors |
| 6148 | // from the class X named in the using-declaration consists of actual |
| 6149 | // constructors and notional constructors that result from the |
| 6150 | // transformation of defaulted parameters as follows: |
| 6151 | // - all non-template default constructors of X, and |
| 6152 | // - for each non-template constructor of X that has at least one |
| 6153 | // parameter with a default argument, the set of constructors that |
| 6154 | // results from omitting any ellipsis parameter specification and |
| 6155 | // successively omitting parameters with a default argument from the |
| 6156 | // end of the parameter-type-list. |
| 6157 | CXXConstructorDecl *BaseCtor = *CtorIt; |
| 6158 | bool CanBeCopyOrMove = BaseCtor->isCopyOrMoveConstructor(); |
| 6159 | const FunctionProtoType *BaseCtorType = |
| 6160 | BaseCtor->getType()->getAs<FunctionProtoType>(); |
| 6161 | |
| 6162 | for (unsigned params = BaseCtor->getMinRequiredArguments(), |
| 6163 | maxParams = BaseCtor->getNumParams(); |
| 6164 | params <= maxParams; ++params) { |
| 6165 | // Skip default constructors. They're never inherited. |
| 6166 | if (params == 0) |
| 6167 | continue; |
| 6168 | // Skip copy and move constructors for the same reason. |
| 6169 | if (CanBeCopyOrMove && params == 1) |
| 6170 | continue; |
| 6171 | |
| 6172 | // Build up a function type for this particular constructor. |
| 6173 | // FIXME: The working paper does not consider that the exception spec |
| 6174 | // for the inheriting constructor might be larger than that of the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 6175 | // source. This code doesn't yet, either. When it does, this code will |
| 6176 | // need to be delayed until after exception specifications and in-class |
| 6177 | // member initializers are attached. |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6178 | const Type *NewCtorType; |
| 6179 | if (params == maxParams) |
| 6180 | NewCtorType = BaseCtorType; |
| 6181 | else { |
| 6182 | llvm::SmallVector<QualType, 16> Args; |
| 6183 | for (unsigned i = 0; i < params; ++i) { |
| 6184 | Args.push_back(BaseCtorType->getArgType(i)); |
| 6185 | } |
| 6186 | FunctionProtoType::ExtProtoInfo ExtInfo = |
| 6187 | BaseCtorType->getExtProtoInfo(); |
| 6188 | ExtInfo.Variadic = false; |
| 6189 | NewCtorType = Context.getFunctionType(BaseCtorType->getResultType(), |
| 6190 | Args.data(), params, ExtInfo) |
| 6191 | .getTypePtr(); |
| 6192 | } |
| 6193 | const Type *CanonicalNewCtorType = |
| 6194 | Context.getCanonicalType(NewCtorType); |
| 6195 | |
| 6196 | // Now that we have the type, first check if the class already has a |
| 6197 | // constructor with this signature. |
| 6198 | if (ExistingConstructors.count(CanonicalNewCtorType)) |
| 6199 | continue; |
| 6200 | |
| 6201 | // Then we check if we have already declared an inherited constructor |
| 6202 | // with this signature. |
| 6203 | std::pair<ConstructorToSourceMap::iterator, bool> result = |
| 6204 | InheritedConstructors.insert(std::make_pair( |
| 6205 | CanonicalNewCtorType, |
| 6206 | std::make_pair(CanonicalBase, (CXXConstructorDecl*)0))); |
| 6207 | if (!result.second) { |
| 6208 | // Already in the map. If it came from a different class, that's an |
| 6209 | // error. Not if it's from the same. |
| 6210 | CanQualType PreviousBase = result.first->second.first; |
| 6211 | if (CanonicalBase != PreviousBase) { |
| 6212 | const CXXConstructorDecl *PrevCtor = result.first->second.second; |
| 6213 | const CXXConstructorDecl *PrevBaseCtor = |
| 6214 | PrevCtor->getInheritedConstructor(); |
| 6215 | assert(PrevBaseCtor && "Conflicting constructor was not inherited"); |
| 6216 | |
| 6217 | Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 6218 | Diag(BaseCtor->getLocation(), |
| 6219 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 6220 | Diag(PrevBaseCtor->getLocation(), |
| 6221 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 6222 | Diag(PrevCtor->getLocation(), |
| 6223 | diag::note_using_decl_constructor_conflict_previous_using); |
| 6224 | } |
| 6225 | continue; |
| 6226 | } |
| 6227 | |
| 6228 | // OK, we're there, now add the constructor. |
| 6229 | // C++0x [class.inhctor]p8: [...] that would be performed by a |
| 6230 | // user-writtern inline constructor [...] |
| 6231 | DeclarationNameInfo DNI(CreatedCtorName, UsingLoc); |
| 6232 | CXXConstructorDecl *NewCtor = CXXConstructorDecl::Create( |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6233 | Context, ClassDecl, UsingLoc, DNI, QualType(NewCtorType, 0), |
| 6234 | /*TInfo=*/0, BaseCtor->isExplicit(), /*Inline=*/true, |
Sean Hunt | 5f802e5 | 2011-05-06 00:11:07 +0000 | [diff] [blame] | 6235 | /*ImplicitlyDeclared=*/true); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6236 | NewCtor->setAccess(BaseCtor->getAccess()); |
| 6237 | |
| 6238 | // Build up the parameter decls and add them. |
| 6239 | llvm::SmallVector<ParmVarDecl *, 16> ParamDecls; |
| 6240 | for (unsigned i = 0; i < params; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6241 | ParamDecls.push_back(ParmVarDecl::Create(Context, NewCtor, |
| 6242 | UsingLoc, UsingLoc, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 6243 | /*IdentifierInfo=*/0, |
| 6244 | BaseCtorType->getArgType(i), |
| 6245 | /*TInfo=*/0, SC_None, |
| 6246 | SC_None, /*DefaultArg=*/0)); |
| 6247 | } |
| 6248 | NewCtor->setParams(ParamDecls.data(), ParamDecls.size()); |
| 6249 | NewCtor->setInheritedConstructor(BaseCtor); |
| 6250 | |
| 6251 | PushOnScopeChains(NewCtor, S, false); |
| 6252 | ClassDecl->addDecl(NewCtor); |
| 6253 | result.first->second.second = NewCtor; |
| 6254 | } |
| 6255 | } |
| 6256 | } |
| 6257 | } |
| 6258 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6259 | Sema::ImplicitExceptionSpecification |
| 6260 | Sema::ComputeDefaultedDtorExceptionSpec(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6261 | // C++ [except.spec]p14: |
| 6262 | // An implicitly declared special member function (Clause 12) shall have |
| 6263 | // an exception-specification. |
| 6264 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 6265 | |
| 6266 | // Direct base-class destructors. |
| 6267 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 6268 | BEnd = ClassDecl->bases_end(); |
| 6269 | B != BEnd; ++B) { |
| 6270 | if (B->isVirtual()) // Handled below. |
| 6271 | continue; |
| 6272 | |
| 6273 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 6274 | ExceptSpec.CalledDecl( |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6275 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6276 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6277 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6278 | // Virtual base-class destructors. |
| 6279 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 6280 | BEnd = ClassDecl->vbases_end(); |
| 6281 | B != BEnd; ++B) { |
| 6282 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 6283 | ExceptSpec.CalledDecl( |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6284 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6285 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6286 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6287 | // Field destructors. |
| 6288 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 6289 | FEnd = ClassDecl->field_end(); |
| 6290 | F != FEnd; ++F) { |
| 6291 | if (const RecordType *RecordTy |
| 6292 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
| 6293 | ExceptSpec.CalledDecl( |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6294 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6295 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6296 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6297 | return ExceptSpec; |
| 6298 | } |
| 6299 | |
| 6300 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
| 6301 | // C++ [class.dtor]p2: |
| 6302 | // If a class has no user-declared destructor, a destructor is |
| 6303 | // declared implicitly. An implicitly-declared destructor is an |
| 6304 | // inline public member of its class. |
| 6305 | |
| 6306 | ImplicitExceptionSpecification Spec = |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6307 | ComputeDefaultedDtorExceptionSpec(ClassDecl); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6308 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 6309 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6310 | // Create the actual destructor declaration. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6311 | QualType Ty = Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6312 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6313 | CanQualType ClassType |
| 6314 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6315 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6316 | DeclarationName Name |
| 6317 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6318 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6319 | CXXDestructorDecl *Destructor |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6320 | = CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, Ty, 0, |
| 6321 | /*isInline=*/true, |
| 6322 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6323 | Destructor->setAccess(AS_public); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6324 | Destructor->setDefaulted(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6325 | Destructor->setImplicit(); |
| 6326 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6327 | |
| 6328 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6329 | ++ASTContext::NumImplicitDestructorsDeclared; |
| 6330 | |
| 6331 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 6332 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6333 | PushOnScopeChains(Destructor, S, false); |
| 6334 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6335 | |
| 6336 | // This could be uniqued if it ever proves significant. |
| 6337 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 6338 | |
| 6339 | if (ShouldDeleteDestructor(Destructor)) |
| 6340 | Destructor->setDeletedAsWritten(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6341 | |
| 6342 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 6343 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 6344 | return Destructor; |
| 6345 | } |
| 6346 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6347 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 6348 | CXXDestructorDecl *Destructor) { |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 6349 | assert((Destructor->isDefaulted() && |
| 6350 | !Destructor->doesThisDeclarationHaveABody()) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6351 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 6352 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6353 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6354 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 6355 | if (Destructor->isInvalidDecl()) |
| 6356 | return; |
| 6357 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6358 | ImplicitlyDefinedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6359 | |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 6360 | DiagnosticErrorTrap Trap(Diags); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 6361 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 6362 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6363 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 6364 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 6365 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 6366 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 6367 | |
| 6368 | Destructor->setInvalidDecl(); |
| 6369 | return; |
| 6370 | } |
| 6371 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 6372 | SourceLocation Loc = Destructor->getLocation(); |
| 6373 | Destructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 6374 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6375 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6376 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 6377 | |
| 6378 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 6379 | L->CompletedImplicitDefinition(Destructor); |
| 6380 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6381 | } |
| 6382 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 6383 | void Sema::AdjustDestructorExceptionSpec(CXXRecordDecl *classDecl, |
| 6384 | CXXDestructorDecl *destructor) { |
| 6385 | // C++11 [class.dtor]p3: |
| 6386 | // A declaration of a destructor that does not have an exception- |
| 6387 | // specification is implicitly considered to have the same exception- |
| 6388 | // specification as an implicit declaration. |
| 6389 | const FunctionProtoType *dtorType = destructor->getType()-> |
| 6390 | getAs<FunctionProtoType>(); |
| 6391 | if (dtorType->hasExceptionSpec()) |
| 6392 | return; |
| 6393 | |
| 6394 | ImplicitExceptionSpecification exceptSpec = |
| 6395 | ComputeDefaultedDtorExceptionSpec(classDecl); |
| 6396 | |
| 6397 | // Replace the destructor's type. |
| 6398 | FunctionProtoType::ExtProtoInfo epi; |
| 6399 | epi.ExceptionSpecType = exceptSpec.getExceptionSpecType(); |
| 6400 | epi.NumExceptions = exceptSpec.size(); |
| 6401 | epi.Exceptions = exceptSpec.data(); |
| 6402 | QualType ty = Context.getFunctionType(Context.VoidTy, 0, 0, epi); |
| 6403 | |
| 6404 | destructor->setType(ty); |
| 6405 | |
| 6406 | // FIXME: If the destructor has a body that could throw, and the newly created |
| 6407 | // spec doesn't allow exceptions, we should emit a warning, because this |
| 6408 | // change in behavior can break conforming C++03 programs at runtime. |
| 6409 | // However, we don't have a body yet, so it needs to be done somewhere else. |
| 6410 | } |
| 6411 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6412 | /// \brief Builds a statement that copies the given entity from \p From to |
| 6413 | /// \c To. |
| 6414 | /// |
| 6415 | /// This routine is used to copy the members of a class with an |
| 6416 | /// implicitly-declared copy assignment operator. When the entities being |
| 6417 | /// copied are arrays, this routine builds for loops to copy them. |
| 6418 | /// |
| 6419 | /// \param S The Sema object used for type-checking. |
| 6420 | /// |
| 6421 | /// \param Loc The location where the implicit copy is being generated. |
| 6422 | /// |
| 6423 | /// \param T The type of the expressions being copied. Both expressions must |
| 6424 | /// have this type. |
| 6425 | /// |
| 6426 | /// \param To The expression we are copying to. |
| 6427 | /// |
| 6428 | /// \param From The expression we are copying from. |
| 6429 | /// |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 6430 | /// \param CopyingBaseSubobject Whether we're copying a base subobject. |
| 6431 | /// Otherwise, it's a non-static member subobject. |
| 6432 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6433 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 6434 | /// |
| 6435 | /// \returns A statement or a loop that copies the expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6436 | static StmtResult |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6437 | BuildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6438 | Expr *To, Expr *From, |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 6439 | bool CopyingBaseSubobject, unsigned Depth = 0) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6440 | // C++0x [class.copy]p30: |
| 6441 | // Each subobject is assigned in the manner appropriate to its type: |
| 6442 | // |
| 6443 | // - if the subobject is of class type, the copy assignment operator |
| 6444 | // for the class is used (as if by explicit qualification; that is, |
| 6445 | // ignoring any possible virtual overriding functions in more derived |
| 6446 | // classes); |
| 6447 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 6448 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 6449 | |
| 6450 | // Look for operator=. |
| 6451 | DeclarationName Name |
| 6452 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 6453 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 6454 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| 6455 | |
| 6456 | // Filter out any result that isn't a copy-assignment operator. |
| 6457 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 6458 | while (F.hasNext()) { |
| 6459 | NamedDecl *D = F.next(); |
| 6460 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 6461 | if (Method->isCopyAssignmentOperator()) |
| 6462 | continue; |
| 6463 | |
| 6464 | F.erase(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 6465 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6466 | F.done(); |
| 6467 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 6468 | // Suppress the protected check (C++ [class.protected]) for each of the |
| 6469 | // assignment operators we found. This strange dance is required when |
| 6470 | // we're assigning via a base classes's copy-assignment operator. To |
| 6471 | // ensure that we're getting the right base class subobject (without |
| 6472 | // ambiguities), we need to cast "this" to that subobject type; to |
| 6473 | // ensure that we don't go through the virtual call mechanism, we need |
| 6474 | // to qualify the operator= name with the base class (see below). However, |
| 6475 | // this means that if the base class has a protected copy assignment |
| 6476 | // operator, the protected member access check will fail. So, we |
| 6477 | // rewrite "protected" access to "public" access in this case, since we |
| 6478 | // know by construction that we're calling from a derived class. |
| 6479 | if (CopyingBaseSubobject) { |
| 6480 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 6481 | L != LEnd; ++L) { |
| 6482 | if (L.getAccess() == AS_protected) |
| 6483 | L.setAccess(AS_public); |
| 6484 | } |
| 6485 | } |
| 6486 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6487 | // Create the nested-name-specifier that will be used to qualify the |
| 6488 | // reference to operator=; this is required to suppress the virtual |
| 6489 | // call mechanism. |
| 6490 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 6491 | SS.MakeTrivial(S.Context, |
| 6492 | NestedNameSpecifier::Create(S.Context, 0, false, |
| 6493 | T.getTypePtr()), |
| 6494 | Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6495 | |
| 6496 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6497 | ExprResult OpEqualRef |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6498 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6499 | /*FirstQualifierInScope=*/0, OpLookup, |
| 6500 | /*TemplateArgs=*/0, |
| 6501 | /*SuppressQualifierCheck=*/true); |
| 6502 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6503 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6504 | |
| 6505 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6506 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6507 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 6508 | OpEqualRef.takeAs<Expr>(), |
| 6509 | Loc, &From, 1, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6510 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6511 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6512 | |
| 6513 | return S.Owned(Call.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 6514 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 6515 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6516 | // - if the subobject is of scalar type, the built-in assignment |
| 6517 | // operator is used. |
| 6518 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| 6519 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6520 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6521 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6522 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6523 | |
| 6524 | return S.Owned(Assignment.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 6525 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6526 | |
| 6527 | // - if the subobject is an array, each element is assigned, in the |
| 6528 | // manner appropriate to the element type; |
| 6529 | |
| 6530 | // Construct a loop over the array bounds, e.g., |
| 6531 | // |
| 6532 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 6533 | // |
| 6534 | // that will copy each of the array elements. |
| 6535 | QualType SizeType = S.Context.getSizeType(); |
| 6536 | |
| 6537 | // Create the iteration variable. |
| 6538 | IdentifierInfo *IterationVarName = 0; |
| 6539 | { |
| 6540 | llvm::SmallString<8> Str; |
| 6541 | llvm::raw_svector_ostream OS(Str); |
| 6542 | OS << "__i" << Depth; |
| 6543 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 6544 | } |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6545 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6546 | IterationVarName, SizeType, |
| 6547 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6548 | SC_None, SC_None); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6549 | |
| 6550 | // Initialize the iteration variable to zero. |
| 6551 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 6552 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6553 | |
| 6554 | // Create a reference to the iteration variable; we'll use this several |
| 6555 | // times throughout. |
| 6556 | Expr *IterationVarRef |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6557 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_RValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6558 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
| 6559 | |
| 6560 | // Create the DeclStmt that holds the iteration variable. |
| 6561 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| 6562 | |
| 6563 | // Create the comparison against the array bound. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6564 | llvm::APInt Upper |
| 6565 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6566 | Expr *Comparison |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 6567 | = new (S.Context) BinaryOperator(IterationVarRef, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6568 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 6569 | BO_NE, S.Context.BoolTy, |
| 6570 | VK_RValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6571 | |
| 6572 | // Create the pre-increment of the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6573 | Expr *Increment |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6574 | = new (S.Context) UnaryOperator(IterationVarRef, UO_PreInc, SizeType, |
| 6575 | VK_LValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6576 | |
| 6577 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6578 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
| 6579 | IterationVarRef, Loc)); |
| 6580 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
| 6581 | IterationVarRef, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6582 | |
| 6583 | // Build the copy for an individual element of the array. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6584 | StmtResult Copy = BuildSingleCopyAssign(S, Loc, ArrayTy->getElementType(), |
| 6585 | To, From, CopyingBaseSubobject, |
| 6586 | Depth + 1); |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 6587 | if (Copy.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6588 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6589 | |
| 6590 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6591 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6592 | S.MakeFullExpr(Comparison), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6593 | 0, S.MakeFullExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6594 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 6595 | } |
| 6596 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 6597 | std::pair<Sema::ImplicitExceptionSpecification, bool> |
| 6598 | Sema::ComputeDefaultedCopyAssignmentExceptionSpecAndConst( |
| 6599 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6600 | // C++ [class.copy]p10: |
| 6601 | // If the class definition does not explicitly declare a copy |
| 6602 | // assignment operator, one is declared implicitly. |
| 6603 | // The implicitly-defined copy assignment operator for a class X |
| 6604 | // will have the form |
| 6605 | // |
| 6606 | // X& X::operator=(const X&) |
| 6607 | // |
| 6608 | // if |
| 6609 | bool HasConstCopyAssignment = true; |
| 6610 | |
| 6611 | // -- each direct base class B of X has a copy assignment operator |
| 6612 | // whose parameter is of type const B&, const volatile B& or B, |
| 6613 | // and |
| 6614 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 6615 | BaseEnd = ClassDecl->bases_end(); |
| 6616 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6617 | // We'll handle this below |
| 6618 | if (LangOpts.CPlusPlus0x && Base->isVirtual()) |
| 6619 | continue; |
| 6620 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6621 | assert(!Base->getType()->isDependentType() && |
| 6622 | "Cannot generate implicit members for class with dependent bases."); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6623 | CXXRecordDecl *BaseClassDecl = Base->getType()->getAsCXXRecordDecl(); |
| 6624 | LookupCopyingAssignment(BaseClassDecl, Qualifiers::Const, false, 0, |
| 6625 | &HasConstCopyAssignment); |
| 6626 | } |
| 6627 | |
| 6628 | // In C++0x, the above citation has "or virtual added" |
| 6629 | if (LangOpts.CPlusPlus0x) { |
| 6630 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 6631 | BaseEnd = ClassDecl->vbases_end(); |
| 6632 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
| 6633 | assert(!Base->getType()->isDependentType() && |
| 6634 | "Cannot generate implicit members for class with dependent bases."); |
| 6635 | CXXRecordDecl *BaseClassDecl = Base->getType()->getAsCXXRecordDecl(); |
| 6636 | LookupCopyingAssignment(BaseClassDecl, Qualifiers::Const, false, 0, |
| 6637 | &HasConstCopyAssignment); |
| 6638 | } |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6639 | } |
| 6640 | |
| 6641 | // -- for all the nonstatic data members of X that are of a class |
| 6642 | // type M (or array thereof), each such class type has a copy |
| 6643 | // assignment operator whose parameter is of type const M&, |
| 6644 | // const volatile M& or M. |
| 6645 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 6646 | FieldEnd = ClassDecl->field_end(); |
| 6647 | HasConstCopyAssignment && Field != FieldEnd; |
| 6648 | ++Field) { |
| 6649 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6650 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 6651 | LookupCopyingAssignment(FieldClassDecl, Qualifiers::Const, false, 0, |
| 6652 | &HasConstCopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6653 | } |
| 6654 | } |
| 6655 | |
| 6656 | // Otherwise, the implicitly declared copy assignment operator will |
| 6657 | // have the form |
| 6658 | // |
| 6659 | // X& X::operator=(X&) |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6660 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6661 | // C++ [except.spec]p14: |
| 6662 | // An implicitly declared special member function (Clause 12) shall have an |
| 6663 | // exception-specification. [...] |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6664 | |
| 6665 | // It is unspecified whether or not an implicit copy assignment operator |
| 6666 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 6667 | // made. As such, this exception specification is effectively unspecified. |
| 6668 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 6669 | // the side of assuming such calls to be made regardless of whether they |
| 6670 | // actually happen. |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6671 | ImplicitExceptionSpecification ExceptSpec(Context); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6672 | unsigned ArgQuals = HasConstCopyAssignment ? Qualifiers::Const : 0; |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6673 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 6674 | BaseEnd = ClassDecl->bases_end(); |
| 6675 | Base != BaseEnd; ++Base) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6676 | if (Base->isVirtual()) |
| 6677 | continue; |
| 6678 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6679 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6680 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6681 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 6682 | ArgQuals, false, 0)) |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6683 | ExceptSpec.CalledDecl(CopyAssign); |
| 6684 | } |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6685 | |
| 6686 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 6687 | BaseEnd = ClassDecl->vbases_end(); |
| 6688 | Base != BaseEnd; ++Base) { |
| 6689 | CXXRecordDecl *BaseClassDecl |
| 6690 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 6691 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 6692 | ArgQuals, false, 0)) |
| 6693 | ExceptSpec.CalledDecl(CopyAssign); |
| 6694 | } |
| 6695 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6696 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 6697 | FieldEnd = ClassDecl->field_end(); |
| 6698 | Field != FieldEnd; |
| 6699 | ++Field) { |
| 6700 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 6701 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 6702 | if (CXXMethodDecl *CopyAssign = |
| 6703 | LookupCopyingAssignment(FieldClassDecl, ArgQuals, false, 0)) |
| 6704 | ExceptSpec.CalledDecl(CopyAssign); |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 6705 | } |
| 6706 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6707 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 6708 | return std::make_pair(ExceptSpec, HasConstCopyAssignment); |
| 6709 | } |
| 6710 | |
| 6711 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
| 6712 | // Note: The following rules are largely analoguous to the copy |
| 6713 | // constructor rules. Note that virtual bases are not taken into account |
| 6714 | // for determining the argument type of the operator. Note also that |
| 6715 | // operators taking an object instead of a reference are allowed. |
| 6716 | |
| 6717 | ImplicitExceptionSpecification Spec(Context); |
| 6718 | bool Const; |
| 6719 | llvm::tie(Spec, Const) = |
| 6720 | ComputeDefaultedCopyAssignmentExceptionSpecAndConst(ClassDecl); |
| 6721 | |
| 6722 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 6723 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 6724 | if (Const) |
| 6725 | ArgType = ArgType.withConst(); |
| 6726 | ArgType = Context.getLValueReferenceType(ArgType); |
| 6727 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6728 | // An implicitly-declared copy assignment operator is an inline public |
| 6729 | // member of its class. |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 6730 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6731 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6732 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 6733 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6734 | CXXMethodDecl *CopyAssignment |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6735 | = CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6736 | Context.getFunctionType(RetType, &ArgType, 1, EPI), |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6737 | /*TInfo=*/0, /*isStatic=*/false, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6738 | /*StorageClassAsWritten=*/SC_None, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 6739 | /*isInline=*/true, |
| 6740 | SourceLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6741 | CopyAssignment->setAccess(AS_public); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 6742 | CopyAssignment->setDefaulted(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6743 | CopyAssignment->setImplicit(); |
| 6744 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6745 | |
| 6746 | // Add the parameter to the operator. |
| 6747 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6748 | ClassLoc, ClassLoc, /*Id=*/0, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6749 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6750 | SC_None, |
| 6751 | SC_None, 0); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6752 | CopyAssignment->setParams(&FromParam, 1); |
| 6753 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6754 | // Note that we have added this copy-assignment operator. |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6755 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 6756 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 6757 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 6758 | PushOnScopeChains(CopyAssignment, S, false); |
| 6759 | ClassDecl->addDecl(CopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6760 | |
Sean Hunt | 1ccbc54 | 2011-06-22 01:05:13 +0000 | [diff] [blame] | 6761 | // C++0x [class.copy]p18: |
| 6762 | // ... If the class definition declares a move constructor or move |
| 6763 | // assignment operator, the implicitly declared copy assignment operator is |
| 6764 | // defined as deleted; ... |
| 6765 | if (ClassDecl->hasUserDeclaredMoveConstructor() || |
| 6766 | ClassDecl->hasUserDeclaredMoveAssignment() || |
| 6767 | ShouldDeleteCopyAssignmentOperator(CopyAssignment)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 6768 | CopyAssignment->setDeletedAsWritten(); |
| 6769 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 6770 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 6771 | return CopyAssignment; |
| 6772 | } |
| 6773 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6774 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 6775 | CXXMethodDecl *CopyAssignOperator) { |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 6776 | assert((CopyAssignOperator->isDefaulted() && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6777 | CopyAssignOperator->isOverloadedOperator() && |
| 6778 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 6779 | !CopyAssignOperator->doesThisDeclarationHaveABody()) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6780 | "DefineImplicitCopyAssignment called for wrong function"); |
| 6781 | |
| 6782 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 6783 | |
| 6784 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 6785 | CopyAssignOperator->setInvalidDecl(); |
| 6786 | return; |
| 6787 | } |
| 6788 | |
| 6789 | CopyAssignOperator->setUsed(); |
| 6790 | |
| 6791 | ImplicitlyDefinedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 6792 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6793 | |
| 6794 | // C++0x [class.copy]p30: |
| 6795 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 6796 | // for a non-union class X performs memberwise copy assignment of its |
| 6797 | // subobjects. The direct base classes of X are assigned first, in the |
| 6798 | // order of their declaration in the base-specifier-list, and then the |
| 6799 | // immediate non-static data members of X are assigned, in the order in |
| 6800 | // which they were declared in the class definition. |
| 6801 | |
| 6802 | // The statements that form the synthesized function body. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 6803 | ASTOwningVector<Stmt*> Statements(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6804 | |
| 6805 | // The parameter for the "other" object, which we are copying from. |
| 6806 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 6807 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 6808 | QualType OtherRefType = Other->getType(); |
| 6809 | if (const LValueReferenceType *OtherRef |
| 6810 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 6811 | OtherRefType = OtherRef->getPointeeType(); |
| 6812 | OtherQuals = OtherRefType.getQualifiers(); |
| 6813 | } |
| 6814 | |
| 6815 | // Our location for everything implicitly-generated. |
| 6816 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 6817 | |
| 6818 | // Construct a reference to the "other" object. We'll be using this |
| 6819 | // throughout the generated ASTs. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6820 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6821 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 6822 | |
| 6823 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 6824 | // ASTs. |
| 6825 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 6826 | assert(This && "Reference to this cannot fail!"); |
| 6827 | |
| 6828 | // Assign base classes. |
| 6829 | bool Invalid = false; |
| 6830 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 6831 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 6832 | // Form the assignment: |
| 6833 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 6834 | QualType BaseType = Base->getType().getUnqualifiedType(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 6835 | if (!BaseType->isRecordType()) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6836 | Invalid = true; |
| 6837 | continue; |
| 6838 | } |
| 6839 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 6840 | CXXCastPath BasePath; |
| 6841 | BasePath.push_back(Base); |
| 6842 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6843 | // Construct the "from" expression, which is an implicit cast to the |
| 6844 | // appropriately-qualified base type. |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 6845 | Expr *From = OtherRef; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6846 | From = ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
| 6847 | CK_UncheckedDerivedToBase, |
| 6848 | VK_LValue, &BasePath).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6849 | |
| 6850 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6851 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6852 | |
| 6853 | // Implicitly cast "this" to the appropriately-qualified base type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6854 | To = ImpCastExprToType(To.take(), |
| 6855 | Context.getCVRQualifiedType(BaseType, |
| 6856 | CopyAssignOperator->getTypeQualifiers()), |
| 6857 | CK_UncheckedDerivedToBase, |
| 6858 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6859 | |
| 6860 | // Build the copy. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6861 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6862 | To.get(), From, |
| 6863 | /*CopyingBaseSubobject=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6864 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 6865 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 6866 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 6867 | CopyAssignOperator->setInvalidDecl(); |
| 6868 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6869 | } |
| 6870 | |
| 6871 | // Success! Record the copy. |
| 6872 | Statements.push_back(Copy.takeAs<Expr>()); |
| 6873 | } |
| 6874 | |
| 6875 | // \brief Reference to the __builtin_memcpy function. |
| 6876 | Expr *BuiltinMemCpyRef = 0; |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 6877 | // \brief Reference to the __builtin_objc_memmove_collectable function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6878 | Expr *CollectableMemCpyRef = 0; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6879 | |
| 6880 | // Assign non-static members. |
| 6881 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 6882 | FieldEnd = ClassDecl->field_end(); |
| 6883 | Field != FieldEnd; ++Field) { |
| 6884 | // Check for members of reference type; we can't copy those. |
| 6885 | if (Field->getType()->isReferenceType()) { |
| 6886 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 6887 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 6888 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 6889 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 6890 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6891 | Invalid = true; |
| 6892 | continue; |
| 6893 | } |
| 6894 | |
| 6895 | // Check for members of const-qualified, non-class type. |
| 6896 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 6897 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 6898 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 6899 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 6900 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 6901 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 6902 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6903 | Invalid = true; |
| 6904 | continue; |
| 6905 | } |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 6906 | |
| 6907 | // Suppress assigning zero-width bitfields. |
| 6908 | if (const Expr *Width = Field->getBitWidth()) |
| 6909 | if (Width->EvaluateAsInt(Context) == 0) |
| 6910 | continue; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6911 | |
| 6912 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 6913 | if (FieldType->isIncompleteArrayType()) { |
| 6914 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 6915 | "Incomplete array type is not valid"); |
| 6916 | continue; |
| 6917 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6918 | |
| 6919 | // Build references to the field in the object we're copying from and to. |
| 6920 | CXXScopeSpec SS; // Intentionally empty |
| 6921 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 6922 | LookupMemberName); |
| 6923 | MemberLookup.addDecl(*Field); |
| 6924 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6925 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6926 | Loc, /*IsArrow=*/false, |
| 6927 | SS, 0, MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6928 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6929 | Loc, /*IsArrow=*/true, |
| 6930 | SS, 0, MemberLookup, 0); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6931 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 6932 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 6933 | |
| 6934 | // If the field should be copied with __builtin_memcpy rather than via |
| 6935 | // explicit assignments, do so. This optimization only applies for arrays |
| 6936 | // of scalars and arrays of class type with trivial copy-assignment |
| 6937 | // operators. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6938 | if (FieldType->isArrayType() && |
| 6939 | BaseType.hasTrivialCopyAssignment(Context)) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6940 | // Compute the size of the memory buffer to be copied. |
| 6941 | QualType SizeType = Context.getSizeType(); |
| 6942 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 6943 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 6944 | for (const ConstantArrayType *Array |
| 6945 | = Context.getAsConstantArrayType(FieldType); |
| 6946 | Array; |
| 6947 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 6948 | llvm::APInt ArraySize |
| 6949 | = Array->getSize().zextOrTrunc(Size.getBitWidth()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6950 | Size *= ArraySize; |
| 6951 | } |
| 6952 | |
| 6953 | // Take the address of the field references for "from" and "to". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6954 | From = CreateBuiltinUnaryOp(Loc, UO_AddrOf, From.get()); |
| 6955 | To = CreateBuiltinUnaryOp(Loc, UO_AddrOf, To.get()); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6956 | |
| 6957 | bool NeedsCollectableMemCpy = |
| 6958 | (BaseType->isRecordType() && |
| 6959 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()); |
| 6960 | |
| 6961 | if (NeedsCollectableMemCpy) { |
| 6962 | if (!CollectableMemCpyRef) { |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 6963 | // Create a reference to the __builtin_objc_memmove_collectable function. |
| 6964 | LookupResult R(*this, |
| 6965 | &Context.Idents.get("__builtin_objc_memmove_collectable"), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6966 | Loc, LookupOrdinaryName); |
| 6967 | LookupName(R, TUScope, true); |
| 6968 | |
| 6969 | FunctionDecl *CollectableMemCpy = R.getAsSingle<FunctionDecl>(); |
| 6970 | if (!CollectableMemCpy) { |
| 6971 | // Something went horribly wrong earlier, and we will have |
| 6972 | // complained about it. |
| 6973 | Invalid = true; |
| 6974 | continue; |
| 6975 | } |
| 6976 | |
| 6977 | CollectableMemCpyRef = BuildDeclRefExpr(CollectableMemCpy, |
| 6978 | CollectableMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6979 | VK_LValue, Loc, 0).take(); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6980 | assert(CollectableMemCpyRef && "Builtin reference cannot fail"); |
| 6981 | } |
| 6982 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6983 | // Create a reference to the __builtin_memcpy builtin function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6984 | else if (!BuiltinMemCpyRef) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 6985 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 6986 | LookupOrdinaryName); |
| 6987 | LookupName(R, TUScope, true); |
| 6988 | |
| 6989 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 6990 | if (!BuiltinMemCpy) { |
| 6991 | // Something went horribly wrong earlier, and we will have complained |
| 6992 | // about it. |
| 6993 | Invalid = true; |
| 6994 | continue; |
| 6995 | } |
| 6996 | |
| 6997 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 6998 | BuiltinMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6999 | VK_LValue, Loc, 0).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7000 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 7001 | } |
| 7002 | |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 7003 | ASTOwningVector<Expr*> CallArgs(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7004 | CallArgs.push_back(To.takeAs<Expr>()); |
| 7005 | CallArgs.push_back(From.takeAs<Expr>()); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 7006 | CallArgs.push_back(IntegerLiteral::Create(Context, Size, SizeType, Loc)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7007 | ExprResult Call = ExprError(); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7008 | if (NeedsCollectableMemCpy) |
| 7009 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7010 | CollectableMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7011 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7012 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7013 | else |
| 7014 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7015 | BuiltinMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7016 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 7017 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 7018 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7019 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 7020 | Statements.push_back(Call.takeAs<Expr>()); |
| 7021 | continue; |
| 7022 | } |
| 7023 | |
| 7024 | // Build the copy of this field. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7025 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, FieldType, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7026 | To.get(), From.get(), |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 7027 | /*CopyingBaseSubobject=*/false); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7028 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 7029 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7030 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 7031 | CopyAssignOperator->setInvalidDecl(); |
| 7032 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7033 | } |
| 7034 | |
| 7035 | // Success! Record the copy. |
| 7036 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 7037 | } |
| 7038 | |
| 7039 | if (!Invalid) { |
| 7040 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7041 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7042 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7043 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7044 | if (Return.isInvalid()) |
| 7045 | Invalid = true; |
| 7046 | else { |
| 7047 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7048 | |
| 7049 | if (Trap.hasErrorOccurred()) { |
| 7050 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 7051 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 7052 | Invalid = true; |
| 7053 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7054 | } |
| 7055 | } |
| 7056 | |
| 7057 | if (Invalid) { |
| 7058 | CopyAssignOperator->setInvalidDecl(); |
| 7059 | return; |
| 7060 | } |
| 7061 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7062 | StmtResult Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 7063 | /*isStmtExpr=*/false); |
| 7064 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 7065 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7066 | |
| 7067 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7068 | L->CompletedImplicitDefinition(CopyAssignOperator); |
| 7069 | } |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7070 | } |
| 7071 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7072 | std::pair<Sema::ImplicitExceptionSpecification, bool> |
| 7073 | Sema::ComputeDefaultedCopyCtorExceptionSpecAndConst(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7074 | // C++ [class.copy]p5: |
| 7075 | // The implicitly-declared copy constructor for a class X will |
| 7076 | // have the form |
| 7077 | // |
| 7078 | // X::X(const X&) |
| 7079 | // |
| 7080 | // if |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 7081 | // FIXME: It ought to be possible to store this on the record. |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7082 | bool HasConstCopyConstructor = true; |
| 7083 | |
| 7084 | // -- each direct or virtual base class B of X has a copy |
| 7085 | // constructor whose first parameter is of type const B& or |
| 7086 | // const volatile B&, and |
| 7087 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7088 | BaseEnd = ClassDecl->bases_end(); |
| 7089 | HasConstCopyConstructor && Base != BaseEnd; |
| 7090 | ++Base) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 7091 | // Virtual bases are handled below. |
| 7092 | if (Base->isVirtual()) |
| 7093 | continue; |
| 7094 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7095 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 7096 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7097 | LookupCopyingConstructor(BaseClassDecl, Qualifiers::Const, |
| 7098 | &HasConstCopyConstructor); |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 7099 | } |
| 7100 | |
| 7101 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 7102 | BaseEnd = ClassDecl->vbases_end(); |
| 7103 | HasConstCopyConstructor && Base != BaseEnd; |
| 7104 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7105 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7106 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7107 | LookupCopyingConstructor(BaseClassDecl, Qualifiers::Const, |
| 7108 | &HasConstCopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7109 | } |
| 7110 | |
| 7111 | // -- for all the nonstatic data members of X that are of a |
| 7112 | // class type M (or array thereof), each such class type |
| 7113 | // has a copy constructor whose first parameter is of type |
| 7114 | // const M& or const volatile M&. |
| 7115 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7116 | FieldEnd = ClassDecl->field_end(); |
| 7117 | HasConstCopyConstructor && Field != FieldEnd; |
| 7118 | ++Field) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 7119 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 7120 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7121 | LookupCopyingConstructor(FieldClassDecl, Qualifiers::Const, |
| 7122 | &HasConstCopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7123 | } |
| 7124 | } |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7125 | // Otherwise, the implicitly declared copy constructor will have |
| 7126 | // the form |
| 7127 | // |
| 7128 | // X::X(X&) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7129 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 7130 | // C++ [except.spec]p14: |
| 7131 | // An implicitly declared special member function (Clause 12) shall have an |
| 7132 | // exception-specification. [...] |
| 7133 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 7134 | unsigned Quals = HasConstCopyConstructor? Qualifiers::Const : 0; |
| 7135 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 7136 | BaseEnd = ClassDecl->bases_end(); |
| 7137 | Base != BaseEnd; |
| 7138 | ++Base) { |
| 7139 | // Virtual bases are handled below. |
| 7140 | if (Base->isVirtual()) |
| 7141 | continue; |
| 7142 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7143 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 7144 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 7145 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7146 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 7147 | ExceptSpec.CalledDecl(CopyConstructor); |
| 7148 | } |
| 7149 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 7150 | BaseEnd = ClassDecl->vbases_end(); |
| 7151 | Base != BaseEnd; |
| 7152 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7153 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 7154 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 7155 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7156 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 7157 | ExceptSpec.CalledDecl(CopyConstructor); |
| 7158 | } |
| 7159 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 7160 | FieldEnd = ClassDecl->field_end(); |
| 7161 | Field != FieldEnd; |
| 7162 | ++Field) { |
| 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()) { |
| 7165 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 7166 | LookupCopyingConstructor(FieldClassDecl, Quals)) |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 7167 | ExceptSpec.CalledDecl(CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 7168 | } |
| 7169 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7170 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7171 | return std::make_pair(ExceptSpec, HasConstCopyConstructor); |
| 7172 | } |
| 7173 | |
| 7174 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 7175 | CXXRecordDecl *ClassDecl) { |
| 7176 | // C++ [class.copy]p4: |
| 7177 | // If the class definition does not explicitly declare a copy |
| 7178 | // constructor, one is declared implicitly. |
| 7179 | |
| 7180 | ImplicitExceptionSpecification Spec(Context); |
| 7181 | bool Const; |
| 7182 | llvm::tie(Spec, Const) = |
| 7183 | ComputeDefaultedCopyCtorExceptionSpecAndConst(ClassDecl); |
| 7184 | |
| 7185 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 7186 | QualType ArgType = ClassType; |
| 7187 | if (Const) |
| 7188 | ArgType = ArgType.withConst(); |
| 7189 | ArgType = Context.getLValueReferenceType(ArgType); |
| 7190 | |
| 7191 | FunctionProtoType::ExtProtoInfo EPI = Spec.getEPI(); |
| 7192 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7193 | DeclarationName Name |
| 7194 | = Context.DeclarationNames.getCXXConstructorName( |
| 7195 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7196 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 7197 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7198 | |
| 7199 | // An implicitly-declared copy constructor is an inline public |
| 7200 | // member of its class. |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7201 | CXXConstructorDecl *CopyConstructor |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7202 | = CXXConstructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7203 | Context.getFunctionType(Context.VoidTy, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7204 | &ArgType, 1, EPI), |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7205 | /*TInfo=*/0, |
| 7206 | /*isExplicit=*/false, |
| 7207 | /*isInline=*/true, |
Sean Hunt | 5f802e5 | 2011-05-06 00:11:07 +0000 | [diff] [blame] | 7208 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7209 | CopyConstructor->setAccess(AS_public); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7210 | CopyConstructor->setDefaulted(); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7211 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
| 7212 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7213 | // Note that we have declared this constructor. |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7214 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 7215 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7216 | // Add the parameter to the constructor. |
| 7217 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7218 | ClassLoc, ClassLoc, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7219 | /*IdentifierInfo=*/0, |
| 7220 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7221 | SC_None, |
| 7222 | SC_None, 0); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7223 | CopyConstructor->setParams(&FromParam, 1); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7224 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 7225 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 7226 | PushOnScopeChains(CopyConstructor, S, false); |
| 7227 | ClassDecl->addDecl(CopyConstructor); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 7228 | |
Sean Hunt | 1ccbc54 | 2011-06-22 01:05:13 +0000 | [diff] [blame] | 7229 | // C++0x [class.copy]p7: |
| 7230 | // ... If the class definition declares a move constructor or move |
| 7231 | // assignment operator, the implicitly declared constructor is defined as |
| 7232 | // deleted; ... |
| 7233 | if (ClassDecl->hasUserDeclaredMoveConstructor() || |
| 7234 | ClassDecl->hasUserDeclaredMoveAssignment() || |
| 7235 | ShouldDeleteCopyConstructor(CopyConstructor)) |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 7236 | CopyConstructor->setDeletedAsWritten(); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 7237 | |
| 7238 | return CopyConstructor; |
| 7239 | } |
| 7240 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7241 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 7242 | CXXConstructorDecl *CopyConstructor) { |
| 7243 | assert((CopyConstructor->isDefaulted() && |
| 7244 | CopyConstructor->isCopyConstructor() && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 7245 | !CopyConstructor->doesThisDeclarationHaveABody()) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7246 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7247 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 7248 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7249 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7250 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 7251 | ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 7252 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7253 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 7254 | if (SetCtorInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7255 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 7256 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 7257 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 7258 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 7259 | } else { |
| 7260 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 7261 | CopyConstructor->getLocation(), |
| 7262 | MultiStmtArg(*this, 0, 0), |
| 7263 | /*isStmtExpr=*/false) |
| 7264 | .takeAs<Stmt>()); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 7265 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 7266 | |
| 7267 | CopyConstructor->setUsed(); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7268 | |
| 7269 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7270 | L->CompletedImplicitDefinition(CopyConstructor); |
| 7271 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7272 | } |
| 7273 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7274 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 7275 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7276 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7277 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7278 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7279 | unsigned ConstructKind, |
| 7280 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 7281 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7282 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 7283 | // C++0x [class.copy]p34: |
| 7284 | // When certain criteria are met, an implementation is allowed to |
| 7285 | // omit the copy/move construction of a class object, even if the |
| 7286 | // copy/move constructor and/or destructor for the object have |
| 7287 | // side effects. [...] |
| 7288 | // - when a temporary class object that has not been bound to a |
| 7289 | // reference (12.2) would be copied/moved to a class object |
| 7290 | // with the same cv-unqualified type, the copy/move operation |
| 7291 | // can be omitted by constructing the temporary object |
| 7292 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 7293 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
Douglas Gregor | 70a21de | 2011-01-27 23:24:55 +0000 | [diff] [blame] | 7294 | Constructor->isCopyOrMoveConstructor() && ExprArgs.size() >= 1) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 7295 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 7296 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 7297 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7298 | |
| 7299 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7300 | Elidable, move(ExprArgs), RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7301 | ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 7302 | } |
| 7303 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 7304 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 7305 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7306 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 7307 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 7308 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7309 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 7310 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7311 | unsigned ConstructKind, |
| 7312 | SourceRange ParenRange) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 7313 | unsigned NumExprs = ExprArgs.size(); |
| 7314 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7315 | |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 7316 | for (specific_attr_iterator<NonNullAttr> |
| 7317 | i = Constructor->specific_attr_begin<NonNullAttr>(), |
| 7318 | e = Constructor->specific_attr_end<NonNullAttr>(); i != e; ++i) { |
| 7319 | const NonNullAttr *NonNull = *i; |
| 7320 | CheckNonNullArguments(NonNull, ExprArgs.get(), ConstructLoc); |
| 7321 | } |
| 7322 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 7323 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 7324 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 7325 | Constructor, Elidable, Exprs, NumExprs, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 7326 | RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7327 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 7328 | ParenRange)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 7329 | } |
| 7330 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7331 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 7332 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 7333 | MultiExprArg Exprs) { |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7334 | // FIXME: Provide the correct paren SourceRange when available. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7335 | ExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 7336 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 7337 | move(Exprs), false, CXXConstructExpr::CK_Complete, |
| 7338 | SourceRange()); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 7339 | if (TempResult.isInvalid()) |
| 7340 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7341 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 7342 | Expr *Temp = TempResult.takeAs<Expr>(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7343 | CheckImplicitConversions(Temp, VD->getLocation()); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7344 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 7345 | Temp = MaybeCreateExprWithCleanups(Temp); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 7346 | VD->setInit(Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7347 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 7348 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 7349 | } |
| 7350 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 7351 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 7352 | if (VD->isInvalidDecl()) return; |
| 7353 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 7354 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 7355 | if (ClassDecl->isInvalidDecl()) return; |
| 7356 | if (ClassDecl->hasTrivialDestructor()) return; |
| 7357 | if (ClassDecl->isDependentContext()) return; |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 7358 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 7359 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
| 7360 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
| 7361 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 7362 | PDiag(diag::err_access_dtor_var) |
| 7363 | << VD->getDeclName() |
| 7364 | << VD->getType()); |
Anders Carlsson | 2b32dad | 2011-03-24 01:01:41 +0000 | [diff] [blame] | 7365 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 7366 | if (!VD->hasGlobalStorage()) return; |
| 7367 | |
| 7368 | // Emit warning for non-trivial dtor in global scope (a real global, |
| 7369 | // class-static, function-static). |
| 7370 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 7371 | |
| 7372 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 7373 | if (!VD->isStaticLocal()) |
| 7374 | Diag(VD->getLocation(), diag::warn_global_destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7375 | } |
| 7376 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7377 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7378 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 7379 | /// e.g: "int x(1);" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7380 | void Sema::AddCXXDirectInitializerToDecl(Decl *RealDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 7381 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 7382 | MultiExprArg Exprs, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7383 | SourceLocation RParenLoc, |
| 7384 | bool TypeMayContainAuto) { |
Daniel Dunbar | 5184626 | 2009-12-24 19:19:26 +0000 | [diff] [blame] | 7385 | assert(Exprs.size() != 0 && Exprs.get() && "missing expressions"); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7386 | |
| 7387 | // If there is no declaration, there was an error parsing it. Just ignore |
| 7388 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 7389 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7390 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7391 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7392 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 7393 | if (!VDecl) { |
| 7394 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 7395 | RealDecl->setInvalidDecl(); |
| 7396 | return; |
| 7397 | } |
| 7398 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7399 | // C++0x [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 7400 | if (TypeMayContainAuto && VDecl->getType()->getContainedAutoType()) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7401 | // FIXME: n3225 doesn't actually seem to indicate this is ill-formed |
| 7402 | if (Exprs.size() > 1) { |
| 7403 | Diag(Exprs.get()[1]->getSourceRange().getBegin(), |
| 7404 | diag::err_auto_var_init_multiple_expressions) |
| 7405 | << VDecl->getDeclName() << VDecl->getType() |
| 7406 | << VDecl->getSourceRange(); |
| 7407 | RealDecl->setInvalidDecl(); |
| 7408 | return; |
| 7409 | } |
| 7410 | |
| 7411 | Expr *Init = Exprs.get()[0]; |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 7412 | TypeSourceInfo *DeducedType = 0; |
| 7413 | if (!DeduceAutoType(VDecl->getTypeSourceInfo(), Init, DeducedType)) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7414 | Diag(VDecl->getLocation(), diag::err_auto_var_deduction_failure) |
| 7415 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 7416 | << Init->getSourceRange(); |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 7417 | if (!DeducedType) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7418 | RealDecl->setInvalidDecl(); |
| 7419 | return; |
| 7420 | } |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 7421 | VDecl->setTypeSourceInfo(DeducedType); |
| 7422 | VDecl->setType(DeducedType->getType()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7423 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7424 | // In ARC, infer lifetime. |
| 7425 | if (getLangOptions().ObjCAutoRefCount && inferObjCARCLifetime(VDecl)) |
| 7426 | VDecl->setInvalidDecl(); |
| 7427 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 7428 | // If this is a redeclaration, check that the type we just deduced matches |
| 7429 | // the previously declared type. |
| 7430 | if (VarDecl *Old = VDecl->getPreviousDeclaration()) |
| 7431 | MergeVarDeclTypes(VDecl, Old); |
| 7432 | } |
| 7433 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 7434 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 7435 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7436 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 7437 | // |
| 7438 | // Clients that want to distinguish between the two forms, can check for |
| 7439 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 7440 | // A major benefit is that clients that don't particularly care about which |
| 7441 | // exactly form was it (like the CodeGen) can handle both cases without |
| 7442 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 7443 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7444 | // C++ 8.5p11: |
| 7445 | // The form of initialization (using parentheses or '=') is generally |
| 7446 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 7447 | // class type. |
| 7448 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 7449 | if (!VDecl->getType()->isDependentType() && |
| 7450 | RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 7451 | diag::err_typecheck_decl_incomplete_type)) { |
| 7452 | VDecl->setInvalidDecl(); |
| 7453 | return; |
| 7454 | } |
| 7455 | |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 7456 | // The variable can not have an abstract class type. |
| 7457 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 7458 | diag::err_abstract_type_in_decl, |
| 7459 | AbstractVariableType)) |
| 7460 | VDecl->setInvalidDecl(); |
| 7461 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 7462 | const VarDecl *Def; |
| 7463 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 7464 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| 7465 | << VDecl->getDeclName(); |
| 7466 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 7467 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 7468 | return; |
| 7469 | } |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 7470 | |
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 7471 | // C++ [class.static.data]p4 |
| 7472 | // If a static data member is of const integral or const |
| 7473 | // enumeration type, its declaration in the class definition can |
| 7474 | // specify a constant-initializer which shall be an integral |
| 7475 | // constant expression (5.19). In that case, the member can appear |
| 7476 | // in integral constant expressions. The member shall still be |
| 7477 | // defined in a namespace scope if it is used in the program and the |
| 7478 | // namespace scope definition shall not contain an initializer. |
| 7479 | // |
| 7480 | // We already performed a redefinition check above, but for static |
| 7481 | // data members we also need to check whether there was an in-class |
| 7482 | // declaration with an initializer. |
| 7483 | const VarDecl* PrevInit = 0; |
| 7484 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { |
| 7485 | Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName(); |
| 7486 | Diag(PrevInit->getLocation(), diag::note_previous_definition); |
| 7487 | return; |
| 7488 | } |
| 7489 | |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 7490 | bool IsDependent = false; |
| 7491 | for (unsigned I = 0, N = Exprs.size(); I != N; ++I) { |
| 7492 | if (DiagnoseUnexpandedParameterPack(Exprs.get()[I], UPPC_Expression)) { |
| 7493 | VDecl->setInvalidDecl(); |
| 7494 | return; |
| 7495 | } |
| 7496 | |
| 7497 | if (Exprs.get()[I]->isTypeDependent()) |
| 7498 | IsDependent = true; |
| 7499 | } |
| 7500 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 7501 | // If either the declaration has a dependent type or if any of the |
| 7502 | // expressions is type-dependent, we represent the initialization |
| 7503 | // via a ParenListExpr for later use during template instantiation. |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 7504 | if (VDecl->getType()->isDependentType() || IsDependent) { |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 7505 | // Let clients know that initialization was done with a direct initializer. |
| 7506 | VDecl->setCXXDirectInitializer(true); |
| 7507 | |
| 7508 | // Store the initialization expressions as a ParenListExpr. |
| 7509 | unsigned NumExprs = Exprs.size(); |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame^] | 7510 | VDecl->setInit(new (Context) ParenListExpr( |
| 7511 | Context, LParenLoc, (Expr **)Exprs.release(), NumExprs, RParenLoc, |
| 7512 | VDecl->getType().getNonReferenceType())); |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 7513 | return; |
| 7514 | } |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 7515 | |
| 7516 | // Capture the variable that is being initialized and the style of |
| 7517 | // initialization. |
| 7518 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 7519 | |
| 7520 | // FIXME: Poor source location information. |
| 7521 | InitializationKind Kind |
| 7522 | = InitializationKind::CreateDirect(VDecl->getLocation(), |
| 7523 | LParenLoc, RParenLoc); |
| 7524 | |
| 7525 | InitializationSequence InitSeq(*this, Entity, Kind, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7526 | Exprs.get(), Exprs.size()); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7527 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(Exprs)); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 7528 | if (Result.isInvalid()) { |
| 7529 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7530 | return; |
| 7531 | } |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7532 | |
| 7533 | CheckImplicitConversions(Result.get(), LParenLoc); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 7534 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 7535 | Result = MaybeCreateExprWithCleanups(Result); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 7536 | VDecl->setInit(Result.takeAs<Expr>()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7537 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 7538 | |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 7539 | CheckCompleteVariableDeclaration(VDecl); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 7540 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 7541 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 7542 | /// \brief Given a constructor and the set of arguments provided for the |
| 7543 | /// constructor, convert the arguments and add any required default arguments |
| 7544 | /// to form a proper call to this constructor. |
| 7545 | /// |
| 7546 | /// \returns true if an error occurred, false otherwise. |
| 7547 | bool |
| 7548 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 7549 | MultiExprArg ArgsPtr, |
| 7550 | SourceLocation Loc, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 7551 | ASTOwningVector<Expr*> &ConvertedArgs) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 7552 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 7553 | unsigned NumArgs = ArgsPtr.size(); |
| 7554 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 7555 | |
| 7556 | const FunctionProtoType *Proto |
| 7557 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 7558 | assert(Proto && "Constructor without a prototype?"); |
| 7559 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 7560 | |
| 7561 | // 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] | 7562 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 7563 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 7564 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 7565 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 7566 | |
| 7567 | VariadicCallType CallType = |
| 7568 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 7569 | llvm::SmallVector<Expr *, 8> AllArgs; |
| 7570 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 7571 | Proto, 0, Args, NumArgs, AllArgs, |
| 7572 | CallType); |
| 7573 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 7574 | ConvertedArgs.push_back(AllArgs[i]); |
| 7575 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 7576 | } |
| 7577 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 7578 | static inline bool |
| 7579 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 7580 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7581 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 7582 | if (isa<NamespaceDecl>(DC)) { |
| 7583 | return SemaRef.Diag(FnDecl->getLocation(), |
| 7584 | diag::err_operator_new_delete_declared_in_namespace) |
| 7585 | << FnDecl->getDeclName(); |
| 7586 | } |
| 7587 | |
| 7588 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7589 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 7590 | return SemaRef.Diag(FnDecl->getLocation(), |
| 7591 | diag::err_operator_new_delete_declared_static) |
| 7592 | << FnDecl->getDeclName(); |
| 7593 | } |
| 7594 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 7595 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 7596 | } |
| 7597 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7598 | static inline bool |
| 7599 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 7600 | CanQualType ExpectedResultType, |
| 7601 | CanQualType ExpectedFirstParamType, |
| 7602 | unsigned DependentParamTypeDiag, |
| 7603 | unsigned InvalidParamTypeDiag) { |
| 7604 | QualType ResultType = |
| 7605 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 7606 | |
| 7607 | // Check that the result type is not dependent. |
| 7608 | if (ResultType->isDependentType()) |
| 7609 | return SemaRef.Diag(FnDecl->getLocation(), |
| 7610 | diag::err_operator_new_delete_dependent_result_type) |
| 7611 | << FnDecl->getDeclName() << ExpectedResultType; |
| 7612 | |
| 7613 | // Check that the result type is what we expect. |
| 7614 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 7615 | return SemaRef.Diag(FnDecl->getLocation(), |
| 7616 | diag::err_operator_new_delete_invalid_result_type) |
| 7617 | << FnDecl->getDeclName() << ExpectedResultType; |
| 7618 | |
| 7619 | // A function template must have at least 2 parameters. |
| 7620 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 7621 | return SemaRef.Diag(FnDecl->getLocation(), |
| 7622 | diag::err_operator_new_delete_template_too_few_parameters) |
| 7623 | << FnDecl->getDeclName(); |
| 7624 | |
| 7625 | // The function decl must have at least 1 parameter. |
| 7626 | if (FnDecl->getNumParams() == 0) |
| 7627 | return SemaRef.Diag(FnDecl->getLocation(), |
| 7628 | diag::err_operator_new_delete_too_few_parameters) |
| 7629 | << FnDecl->getDeclName(); |
| 7630 | |
| 7631 | // Check the the first parameter type is not dependent. |
| 7632 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 7633 | if (FirstParamType->isDependentType()) |
| 7634 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 7635 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 7636 | |
| 7637 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 7638 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7639 | ExpectedFirstParamType) |
| 7640 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 7641 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 7642 | |
| 7643 | return false; |
| 7644 | } |
| 7645 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 7646 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7647 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 7648 | // C++ [basic.stc.dynamic.allocation]p1: |
| 7649 | // A program is ill-formed if an allocation function is declared in a |
| 7650 | // namespace scope other than global scope or declared static in global |
| 7651 | // scope. |
| 7652 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 7653 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7654 | |
| 7655 | CanQualType SizeTy = |
| 7656 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 7657 | |
| 7658 | // C++ [basic.stc.dynamic.allocation]p1: |
| 7659 | // The return type shall be void*. The first parameter shall have type |
| 7660 | // std::size_t. |
| 7661 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 7662 | SizeTy, |
| 7663 | diag::err_operator_new_dependent_param_type, |
| 7664 | diag::err_operator_new_param_type)) |
| 7665 | return true; |
| 7666 | |
| 7667 | // C++ [basic.stc.dynamic.allocation]p1: |
| 7668 | // The first parameter shall not have an associated default argument. |
| 7669 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 7670 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7671 | diag::err_operator_new_default_arg) |
| 7672 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 7673 | |
| 7674 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 7675 | } |
| 7676 | |
| 7677 | static bool |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 7678 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 7679 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 7680 | // A program is ill-formed if deallocation functions are declared in a |
| 7681 | // namespace scope other than global scope or declared static in global |
| 7682 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 7683 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 7684 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 7685 | |
| 7686 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 7687 | // Each deallocation function shall return void and its first parameter |
| 7688 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7689 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 7690 | SemaRef.Context.VoidPtrTy, |
| 7691 | diag::err_operator_delete_dependent_param_type, |
| 7692 | diag::err_operator_delete_param_type)) |
| 7693 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 7694 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 7695 | return false; |
| 7696 | } |
| 7697 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7698 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 7699 | /// of this overloaded operator is well-formed. If so, returns false; |
| 7700 | /// otherwise, emits appropriate diagnostics and returns true. |
| 7701 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7702 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7703 | "Expected an overloaded operator declaration"); |
| 7704 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7705 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 7706 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7707 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7708 | // The allocation and deallocation functions, operator new, |
| 7709 | // operator new[], operator delete and operator delete[], are |
| 7710 | // described completely in 3.7.3. The attributes and restrictions |
| 7711 | // found in the rest of this subclause do not apply to them unless |
| 7712 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 7713 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 7714 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 7715 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 7716 | if (Op == OO_New || Op == OO_Array_New) |
| 7717 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7718 | |
| 7719 | // C++ [over.oper]p6: |
| 7720 | // An operator function shall either be a non-static member |
| 7721 | // function or be a non-member function and have at least one |
| 7722 | // parameter whose type is a class, a reference to a class, an |
| 7723 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7724 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 7725 | if (MethodDecl->isStatic()) |
| 7726 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 7727 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7728 | } else { |
| 7729 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7730 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 7731 | ParamEnd = FnDecl->param_end(); |
| 7732 | Param != ParamEnd; ++Param) { |
| 7733 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 7734 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 7735 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7736 | ClassOrEnumParam = true; |
| 7737 | break; |
| 7738 | } |
| 7739 | } |
| 7740 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7741 | if (!ClassOrEnumParam) |
| 7742 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 7743 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 7744 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7745 | } |
| 7746 | |
| 7747 | // C++ [over.oper]p8: |
| 7748 | // An operator function cannot have default arguments (8.3.6), |
| 7749 | // except where explicitly stated below. |
| 7750 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7751 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7752 | // (C++ [over.call]p1). |
| 7753 | if (Op != OO_Call) { |
| 7754 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 7755 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7756 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7757 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 7758 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 7759 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7760 | } |
| 7761 | } |
| 7762 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 7763 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 7764 | { false, false, false } |
| 7765 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 7766 | , { Unary, Binary, MemberOnly } |
| 7767 | #include "clang/Basic/OperatorKinds.def" |
| 7768 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7769 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 7770 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 7771 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 7772 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7773 | |
| 7774 | // C++ [over.oper]p8: |
| 7775 | // [...] Operator functions cannot have more or fewer parameters |
| 7776 | // than the number required for the corresponding operator, as |
| 7777 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7778 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7779 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7780 | if (Op != OO_Call && |
| 7781 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 7782 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 7783 | (NumParams < 1) || (NumParams > 2))) { |
| 7784 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 7785 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 7786 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 7787 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 7788 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 7789 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 7790 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 7791 | assert(CanBeBinaryOperator && |
| 7792 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 7793 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 7794 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7795 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 7796 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 7797 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7798 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 7799 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7800 | // Overloaded operators other than operator() cannot be variadic. |
| 7801 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7802 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 7803 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 7804 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7805 | } |
| 7806 | |
| 7807 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7808 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 7809 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 7810 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 7811 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7812 | } |
| 7813 | |
| 7814 | // C++ [over.inc]p1: |
| 7815 | // The user-defined function called operator++ implements the |
| 7816 | // prefix and postfix ++ operator. If this function is a member |
| 7817 | // function with no parameters, or a non-member function with one |
| 7818 | // parameter of class or enumeration type, it defines the prefix |
| 7819 | // increment operator ++ for objects of that type. If the function |
| 7820 | // is a member function with one parameter (which shall be of type |
| 7821 | // int) or a non-member function with two parameters (the second |
| 7822 | // of which shall be of type int), it defines the postfix |
| 7823 | // increment operator ++ for objects of that type. |
| 7824 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 7825 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 7826 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7827 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7828 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 7829 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 7830 | if (!ParamIsInt) |
| 7831 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7832 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7833 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7834 | } |
| 7835 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 7836 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 7837 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 7838 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7839 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 7840 | /// of this literal operator function is well-formed. If so, returns |
| 7841 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 7842 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 7843 | DeclContext *DC = FnDecl->getDeclContext(); |
| 7844 | Decl::Kind Kind = DC->getDeclKind(); |
| 7845 | if (Kind != Decl::TranslationUnit && Kind != Decl::Namespace && |
| 7846 | Kind != Decl::LinkageSpec) { |
| 7847 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 7848 | << FnDecl->getDeclName(); |
| 7849 | return true; |
| 7850 | } |
| 7851 | |
| 7852 | bool Valid = false; |
| 7853 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 7854 | // template <char...> type operator "" name() is the only valid template |
| 7855 | // signature, and the only valid signature with no parameters. |
| 7856 | if (FnDecl->param_size() == 0) { |
| 7857 | if (FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate()) { |
| 7858 | // Must have only one template parameter |
| 7859 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 7860 | if (Params->size() == 1) { |
| 7861 | NonTypeTemplateParmDecl *PmDecl = |
| 7862 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7863 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 7864 | // The template parameter must be a char parameter pack. |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 7865 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 7866 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 7867 | Valid = true; |
| 7868 | } |
| 7869 | } |
| 7870 | } else { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7871 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 7872 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 7873 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7874 | QualType T = (*Param)->getType(); |
| 7875 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 7876 | // unsigned long long int, long double, and any character type are allowed |
| 7877 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 7878 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 7879 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 7880 | Context.hasSameType(T, Context.CharTy) || |
| 7881 | Context.hasSameType(T, Context.WCharTy) || |
| 7882 | Context.hasSameType(T, Context.Char16Ty) || |
| 7883 | Context.hasSameType(T, Context.Char32Ty)) { |
| 7884 | if (++Param == FnDecl->param_end()) |
| 7885 | Valid = true; |
| 7886 | goto FinishedParams; |
| 7887 | } |
| 7888 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 7889 | // 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] | 7890 | const PointerType *PT = T->getAs<PointerType>(); |
| 7891 | if (!PT) |
| 7892 | goto FinishedParams; |
| 7893 | T = PT->getPointeeType(); |
| 7894 | if (!T.isConstQualified()) |
| 7895 | goto FinishedParams; |
| 7896 | T = T.getUnqualifiedType(); |
| 7897 | |
| 7898 | // Move on to the second parameter; |
| 7899 | ++Param; |
| 7900 | |
| 7901 | // If there is no second parameter, the first must be a const char * |
| 7902 | if (Param == FnDecl->param_end()) { |
| 7903 | if (Context.hasSameType(T, Context.CharTy)) |
| 7904 | Valid = true; |
| 7905 | goto FinishedParams; |
| 7906 | } |
| 7907 | |
| 7908 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 7909 | // are allowed as the first parameter to a two-parameter function |
| 7910 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 7911 | Context.hasSameType(T, Context.WCharTy) || |
| 7912 | Context.hasSameType(T, Context.Char16Ty) || |
| 7913 | Context.hasSameType(T, Context.Char32Ty))) |
| 7914 | goto FinishedParams; |
| 7915 | |
| 7916 | // The second and final parameter must be an std::size_t |
| 7917 | T = (*Param)->getType().getUnqualifiedType(); |
| 7918 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 7919 | ++Param == FnDecl->param_end()) |
| 7920 | Valid = true; |
| 7921 | } |
| 7922 | |
| 7923 | // FIXME: This diagnostic is absolutely terrible. |
| 7924 | FinishedParams: |
| 7925 | if (!Valid) { |
| 7926 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 7927 | << FnDecl->getDeclName(); |
| 7928 | return true; |
| 7929 | } |
| 7930 | |
| 7931 | return false; |
| 7932 | } |
| 7933 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 7934 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 7935 | /// linkage specification, including the language and (if present) |
| 7936 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 7937 | /// the location of the language string literal, which is provided |
| 7938 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 7939 | /// the '{' brace. Otherwise, this linkage specification does not |
| 7940 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 7941 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 7942 | SourceLocation LangLoc, |
| 7943 | llvm::StringRef Lang, |
| 7944 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 7945 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 7946 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 7947 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 7948 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 7949 | Language = LinkageSpecDecl::lang_cxx; |
| 7950 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 7951 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7952 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 7953 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7954 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 7955 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7956 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 7957 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 7958 | ExternLoc, LangLoc, Language); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 7959 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 7960 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7961 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 7962 | } |
| 7963 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 7964 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 7965 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 7966 | /// valid, it's the position of the closing '}' brace in a linkage |
| 7967 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7968 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 7969 | Decl *LinkageSpec, |
| 7970 | SourceLocation RBraceLoc) { |
| 7971 | if (LinkageSpec) { |
| 7972 | if (RBraceLoc.isValid()) { |
| 7973 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 7974 | LSDecl->setRBraceLoc(RBraceLoc); |
| 7975 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 7976 | PopDeclContext(); |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 7977 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 7978 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 7979 | } |
| 7980 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 7981 | /// \brief Perform semantic analysis for the variable declaration that |
| 7982 | /// occurs within a C++ catch clause, returning the newly-created |
| 7983 | /// variable. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7984 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 7985 | TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7986 | SourceLocation StartLoc, |
| 7987 | SourceLocation Loc, |
| 7988 | IdentifierInfo *Name) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 7989 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 7990 | QualType ExDeclType = TInfo->getType(); |
| 7991 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 7992 | // Arrays and functions decay. |
| 7993 | if (ExDeclType->isArrayType()) |
| 7994 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 7995 | else if (ExDeclType->isFunctionType()) |
| 7996 | ExDeclType = Context.getPointerType(ExDeclType); |
| 7997 | |
| 7998 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 7999 | // The exception-declaration shall not denote a pointer or reference to an |
| 8000 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 8001 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8002 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 8003 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 8004 | Invalid = true; |
| 8005 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 8006 | |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 8007 | // GCC allows catching pointers and references to incomplete types |
| 8008 | // as an extension; so do we, but we warn by default. |
| 8009 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8010 | QualType BaseType = ExDeclType; |
| 8011 | 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] | 8012 | unsigned DK = diag::err_catch_incomplete; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 8013 | bool IncompleteCatchIsInvalid = true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 8014 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8015 | BaseType = Ptr->getPointeeType(); |
| 8016 | Mode = 1; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 8017 | DK = diag::ext_catch_incomplete_ptr; |
| 8018 | IncompleteCatchIsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8019 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 8020 | // 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] | 8021 | BaseType = Ref->getPointeeType(); |
| 8022 | Mode = 2; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 8023 | DK = diag::ext_catch_incomplete_ref; |
| 8024 | IncompleteCatchIsInvalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8025 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 8026 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 8027 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK) && |
| 8028 | IncompleteCatchIsInvalid) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8029 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8030 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8031 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 8032 | RequireNonAbstractType(Loc, ExDeclType, |
| 8033 | diag::err_abstract_type_in_decl, |
| 8034 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 8035 | Invalid = true; |
| 8036 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 8037 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 8038 | // of ObjC types, and no runtime supports catching ObjC types by value. |
| 8039 | if (!Invalid && getLangOptions().ObjC1) { |
| 8040 | QualType T = ExDeclType; |
| 8041 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 8042 | T = RT->getPointeeType(); |
| 8043 | |
| 8044 | if (T->isObjCObjectType()) { |
| 8045 | Diag(Loc, diag::err_objc_object_catch); |
| 8046 | Invalid = true; |
| 8047 | } else if (T->isObjCObjectPointerType()) { |
David Chisnall | 80558d2 | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 8048 | if (!getLangOptions().ObjCNonFragileABI) { |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 8049 | Diag(Loc, diag::err_objc_pointer_cxx_catch_fragile); |
| 8050 | Invalid = true; |
| 8051 | } |
| 8052 | } |
| 8053 | } |
| 8054 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8055 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
| 8056 | ExDeclType, TInfo, SC_None, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 8057 | ExDecl->setExceptionVariable(true); |
| 8058 | |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 8059 | if (!Invalid) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 8060 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 8061 | // C++ [except.handle]p16: |
| 8062 | // The object declared in an exception-declaration or, if the |
| 8063 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 8064 | // copy-initialized (8.5) from the exception object. [...] |
| 8065 | // The object is destroyed when the handler exits, after the destruction |
| 8066 | // of any automatic objects initialized within the handler. |
| 8067 | // |
| 8068 | // We just pretend to initialize the object with itself, then make sure |
| 8069 | // it can be destroyed later. |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 8070 | QualType initType = ExDeclType; |
| 8071 | |
| 8072 | InitializedEntity entity = |
| 8073 | InitializedEntity::InitializeVariable(ExDecl); |
| 8074 | InitializationKind initKind = |
| 8075 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 8076 | |
| 8077 | Expr *opaqueValue = |
| 8078 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
| 8079 | InitializationSequence sequence(*this, entity, initKind, &opaqueValue, 1); |
| 8080 | ExprResult result = sequence.Perform(*this, entity, initKind, |
| 8081 | MultiExprArg(&opaqueValue, 1)); |
| 8082 | if (result.isInvalid()) |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 8083 | Invalid = true; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 8084 | else { |
| 8085 | // If the constructor used was non-trivial, set this as the |
| 8086 | // "initializer". |
| 8087 | CXXConstructExpr *construct = cast<CXXConstructExpr>(result.take()); |
| 8088 | if (!construct->getConstructor()->isTrivial()) { |
| 8089 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 8090 | ExDecl->setInit(init); |
| 8091 | } |
| 8092 | |
| 8093 | // And make sure it's destructable. |
| 8094 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 8095 | } |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 8096 | } |
| 8097 | } |
| 8098 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 8099 | if (Invalid) |
| 8100 | ExDecl->setInvalidDecl(); |
| 8101 | |
| 8102 | return ExDecl; |
| 8103 | } |
| 8104 | |
| 8105 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 8106 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8107 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8108 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 8109 | bool Invalid = D.isInvalidType(); |
| 8110 | |
| 8111 | // Check for unexpanded parameter packs. |
| 8112 | if (TInfo && DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 8113 | UPPC_ExceptionType)) { |
| 8114 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 8115 | D.getIdentifierLoc()); |
| 8116 | Invalid = true; |
| 8117 | } |
| 8118 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8119 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 8120 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 8121 | LookupOrdinaryName, |
| 8122 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8123 | // The scope should be freshly made just for us. There is just no way |
| 8124 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8125 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8126 | if (PrevDecl->isTemplateParameter()) { |
| 8127 | // Maybe we will complain about the shadowed template parameter. |
| 8128 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8129 | } |
| 8130 | } |
| 8131 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 8132 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8133 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 8134 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 8135 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8136 | } |
| 8137 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 8138 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8139 | D.getSourceRange().getBegin(), |
| 8140 | D.getIdentifierLoc(), |
| 8141 | D.getIdentifier()); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 8142 | if (Invalid) |
| 8143 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8144 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8145 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8146 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 8147 | PushOnScopeChains(ExDecl, S); |
| 8148 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 8149 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8150 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 8151 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8152 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 8153 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 8154 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 8155 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8156 | Expr *AssertExpr, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 8157 | Expr *AssertMessageExpr_, |
| 8158 | SourceLocation RParenLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8159 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr_); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 8160 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 8161 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 8162 | llvm::APSInt Value(32); |
| 8163 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 8164 | Diag(StaticAssertLoc, |
| 8165 | diag::err_static_assert_expression_is_not_constant) << |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 8166 | AssertExpr->getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8167 | return 0; |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 8168 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 8169 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 8170 | if (Value == 0) { |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 8171 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 8172 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 8173 | } |
| 8174 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8175 | |
Douglas Gregor | 399ad97 | 2010-12-15 23:55:21 +0000 | [diff] [blame] | 8176 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 8177 | return 0; |
| 8178 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 8179 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
| 8180 | AssertExpr, AssertMessage, RParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8181 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 8182 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8183 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 8184 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 8185 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8186 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 8187 | /// |
| 8188 | /// \returns A friend declaration that. |
| 8189 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation FriendLoc, |
| 8190 | TypeSourceInfo *TSInfo) { |
| 8191 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 8192 | |
| 8193 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8194 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8195 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 8196 | if (!getLangOptions().CPlusPlus0x) { |
| 8197 | // C++03 [class.friend]p2: |
| 8198 | // An elaborated-type-specifier shall be used in a friend declaration |
| 8199 | // for a class.* |
| 8200 | // |
| 8201 | // * The class-key of the elaborated-type-specifier is required. |
| 8202 | if (!ActiveTemplateInstantiations.empty()) { |
| 8203 | // Do not complain about the form of friend template types during |
| 8204 | // template instantiation; we will already have complained when the |
| 8205 | // template was declared. |
| 8206 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 8207 | // If we evaluated the type to a record type, suggest putting |
| 8208 | // a tag in front. |
| 8209 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 8210 | RecordDecl *RD = RT->getDecl(); |
| 8211 | |
| 8212 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 8213 | |
| 8214 | Diag(TypeRange.getBegin(), diag::ext_unelaborated_friend_type) |
| 8215 | << (unsigned) RD->getTagKind() |
| 8216 | << T |
| 8217 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 8218 | InsertionText); |
| 8219 | } else { |
| 8220 | Diag(FriendLoc, diag::ext_nonclass_type_friend) |
| 8221 | << T |
| 8222 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
| 8223 | } |
| 8224 | } else if (T->getAs<EnumType>()) { |
| 8225 | Diag(FriendLoc, diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8226 | << T |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8227 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8228 | } |
| 8229 | } |
| 8230 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 8231 | // C++0x [class.friend]p3: |
| 8232 | // If the type specifier in a friend declaration designates a (possibly |
| 8233 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 8234 | // the friend declaration is ignored. |
| 8235 | |
| 8236 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 8237 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8238 | |
| 8239 | return FriendDecl::Create(Context, CurContext, FriendLoc, TSInfo, FriendLoc); |
| 8240 | } |
| 8241 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8242 | /// Handle a friend tag declaration where the scope specifier was |
| 8243 | /// templated. |
| 8244 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 8245 | unsigned TagSpec, SourceLocation TagLoc, |
| 8246 | CXXScopeSpec &SS, |
| 8247 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 8248 | AttributeList *Attr, |
| 8249 | MultiTemplateParamsArg TempParamLists) { |
| 8250 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 8251 | |
| 8252 | bool isExplicitSpecialization = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8253 | bool Invalid = false; |
| 8254 | |
| 8255 | if (TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 8256 | = MatchTemplateParametersToScopeSpecifier(TagLoc, NameLoc, SS, |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8257 | TempParamLists.get(), |
| 8258 | TempParamLists.size(), |
| 8259 | /*friend*/ true, |
| 8260 | isExplicitSpecialization, |
| 8261 | Invalid)) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8262 | if (TemplateParams->size() > 0) { |
| 8263 | // This is a declaration of a class template. |
| 8264 | if (Invalid) |
| 8265 | return 0; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 8266 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8267 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 8268 | SS, Name, NameLoc, Attr, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 8269 | TemplateParams, AS_public, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 8270 | TempParamLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 8271 | (TemplateParameterList**) TempParamLists.release()).take(); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8272 | } else { |
| 8273 | // The "template<>" header is extraneous. |
| 8274 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 8275 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 8276 | isExplicitSpecialization = true; |
| 8277 | } |
| 8278 | } |
| 8279 | |
| 8280 | if (Invalid) return 0; |
| 8281 | |
| 8282 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 8283 | |
| 8284 | bool isAllExplicitSpecializations = true; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 8285 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8286 | if (TempParamLists.get()[I]->size()) { |
| 8287 | isAllExplicitSpecializations = false; |
| 8288 | break; |
| 8289 | } |
| 8290 | } |
| 8291 | |
| 8292 | // FIXME: don't ignore attributes. |
| 8293 | |
| 8294 | // If it's explicit specializations all the way down, just forget |
| 8295 | // about the template header and build an appropriate non-templated |
| 8296 | // friend. TODO: for source fidelity, remember the headers. |
| 8297 | if (isAllExplicitSpecializations) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8298 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8299 | ElaboratedTypeKeyword Keyword |
| 8300 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8301 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 8302 | *Name, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8303 | if (T.isNull()) |
| 8304 | return 0; |
| 8305 | |
| 8306 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8307 | if (isa<DependentNameType>(T)) { |
| 8308 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 8309 | TL.setKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8310 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8311 | TL.setNameLoc(NameLoc); |
| 8312 | } else { |
| 8313 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
| 8314 | TL.setKeywordLoc(TagLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 8315 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8316 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(NameLoc); |
| 8317 | } |
| 8318 | |
| 8319 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 8320 | TSI, FriendLoc); |
| 8321 | Friend->setAccess(AS_public); |
| 8322 | CurContext->addDecl(Friend); |
| 8323 | return Friend; |
| 8324 | } |
| 8325 | |
| 8326 | // Handle the case of a templated-scope friend class. e.g. |
| 8327 | // template <class T> class A<T>::B; |
| 8328 | // FIXME: we don't support these right now. |
| 8329 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 8330 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 8331 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 8332 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 8333 | TL.setKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 8334 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 8335 | TL.setNameLoc(NameLoc); |
| 8336 | |
| 8337 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 8338 | TSI, FriendLoc); |
| 8339 | Friend->setAccess(AS_public); |
| 8340 | Friend->setUnsupportedFriend(true); |
| 8341 | CurContext->addDecl(Friend); |
| 8342 | return Friend; |
| 8343 | } |
| 8344 | |
| 8345 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8346 | /// Handle a friend type declaration. This works in tandem with |
| 8347 | /// ActOnTag. |
| 8348 | /// |
| 8349 | /// Notes on friend class templates: |
| 8350 | /// |
| 8351 | /// We generally treat friend class declarations as if they were |
| 8352 | /// declaring a class. So, for example, the elaborated type specifier |
| 8353 | /// in a friend declaration is required to obey the restrictions of a |
| 8354 | /// class-head (i.e. no typedefs in the scope chain), template |
| 8355 | /// parameters are required to match up with simple template-ids, &c. |
| 8356 | /// However, unlike when declaring a template specialization, it's |
| 8357 | /// okay to refer to a template specialization without an empty |
| 8358 | /// template parameter declaration, e.g. |
| 8359 | /// friend class A<T>::B<unsigned>; |
| 8360 | /// We permit this as a special case; if there are any template |
| 8361 | /// parameters present at all, require proper matching, i.e. |
| 8362 | /// template <> template <class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8363 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 8364 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8365 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8366 | |
| 8367 | assert(DS.isFriendSpecified()); |
| 8368 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 8369 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8370 | // Try to convert the decl specifier to a type. This works for |
| 8371 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 8372 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 8373 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8374 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 8375 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 8376 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8377 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8378 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 8379 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 8380 | return 0; |
| 8381 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8382 | // This is definitely an error in C++98. It's probably meant to |
| 8383 | // be forbidden in C++0x, too, but the specification is just |
| 8384 | // poorly written. |
| 8385 | // |
| 8386 | // The problem is with declarations like the following: |
| 8387 | // template <T> friend A<T>::foo; |
| 8388 | // where deciding whether a class C is a friend or not now hinges |
| 8389 | // on whether there exists an instantiation of A that causes |
| 8390 | // 'foo' to equal C. There are restrictions on class-heads |
| 8391 | // (which we declare (by fiat) elaborated friend declarations to |
| 8392 | // be) that makes this tractable. |
| 8393 | // |
| 8394 | // FIXME: handle "template <> friend class A<T>;", which |
| 8395 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 8396 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8397 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 8398 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8399 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8400 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8401 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8402 | // C++98 [class.friend]p1: A friend of a class is a function |
| 8403 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 8404 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 8405 | // deadline. It's also a very silly restriction that seriously |
| 8406 | // affects inner classes and which nobody else seems to implement; |
| 8407 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 8408 | // |
| 8409 | // But note that we could warn about it: it's always useless to |
| 8410 | // friend one of your own members (it's not, however, worthless to |
| 8411 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8412 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8413 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8414 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8415 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8416 | NumTempParamLists, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 8417 | TempParams.release(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 8418 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8419 | DS.getFriendSpecLoc()); |
| 8420 | else |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8421 | D = CheckFriendTypeDecl(DS.getFriendSpecLoc(), TSI); |
| 8422 | |
| 8423 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8424 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 8425 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 8426 | D->setAccess(AS_public); |
| 8427 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8428 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8429 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8430 | } |
| 8431 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8432 | Decl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, bool IsDefinition, |
| 8433 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8434 | const DeclSpec &DS = D.getDeclSpec(); |
| 8435 | |
| 8436 | assert(DS.isFriendSpecified()); |
| 8437 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 8438 | |
| 8439 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8440 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 8441 | QualType T = TInfo->getType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8442 | |
| 8443 | // C++ [class.friend]p1 |
| 8444 | // A friend of a class is a function or class.... |
| 8445 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8446 | // It *doesn't* see through dependent types, which is correct |
| 8447 | // according to [temp.arg.type]p3: |
| 8448 | // If a declaration acquires a function type through a |
| 8449 | // type dependent on a template-parameter and this causes |
| 8450 | // a declaration that does not use the syntactic form of a |
| 8451 | // function declarator to have a function type, the program |
| 8452 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8453 | if (!T->isFunctionType()) { |
| 8454 | Diag(Loc, diag::err_unexpected_friend); |
| 8455 | |
| 8456 | // It might be worthwhile to try to recover by creating an |
| 8457 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8458 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8459 | } |
| 8460 | |
| 8461 | // C++ [namespace.memdef]p3 |
| 8462 | // - If a friend declaration in a non-local class first declares a |
| 8463 | // class or function, the friend class or function is a member |
| 8464 | // of the innermost enclosing namespace. |
| 8465 | // - The name of the friend is not found by simple name lookup |
| 8466 | // until a matching declaration is provided in that namespace |
| 8467 | // scope (either before or after the class declaration granting |
| 8468 | // friendship). |
| 8469 | // - If a friend function is called, its name may be found by the |
| 8470 | // name lookup that considers functions from namespaces and |
| 8471 | // classes associated with the types of the function arguments. |
| 8472 | // - When looking for a prior declaration of a class or a function |
| 8473 | // declared as a friend, scopes outside the innermost enclosing |
| 8474 | // namespace scope are not considered. |
| 8475 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8476 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8477 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 8478 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8479 | assert(Name); |
| 8480 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 8481 | // Check for unexpanded parameter packs. |
| 8482 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 8483 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 8484 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 8485 | return 0; |
| 8486 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8487 | // The context we found the declaration in, or in which we should |
| 8488 | // create the declaration. |
| 8489 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 8490 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 8491 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8492 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8493 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8494 | // FIXME: there are different rules in local classes |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8495 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8496 | // There are four cases here. |
| 8497 | // - 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] | 8498 | // appropriate scope and look for a function or function template |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8499 | // there as appropriate. |
| 8500 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 8501 | if (SS.isInvalid() || !SS.isSet()) { |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 8502 | // C++0x [namespace.memdef]p3: |
| 8503 | // If the name in a friend declaration is neither qualified nor |
| 8504 | // a template-id and the declaration is a function or an |
| 8505 | // elaborated-type-specifier, the lookup to determine whether |
| 8506 | // the entity has been previously declared shall not consider |
| 8507 | // any scopes outside the innermost enclosing namespace. |
| 8508 | // C++0x [class.friend]p11: |
| 8509 | // If a friend declaration appears in a local class and the name |
| 8510 | // specified is an unqualified name, a prior declaration is |
| 8511 | // looked up without considering scopes that are outside the |
| 8512 | // innermost enclosing non-class scope. For a friend function |
| 8513 | // declaration, if there is no prior declaration, the program is |
| 8514 | // ill-formed. |
| 8515 | bool isLocal = cast<CXXRecordDecl>(CurContext)->isLocalClass(); |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 8516 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8517 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 8518 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8519 | DC = CurContext; |
| 8520 | while (true) { |
| 8521 | // Skip class contexts. If someone can cite chapter and verse |
| 8522 | // for this behavior, that would be nice --- it's what GCC and |
| 8523 | // EDG do, and it seems like a reasonable intent, but the spec |
| 8524 | // really only says that checks for unqualified existing |
| 8525 | // declarations should stop at the nearest enclosing namespace, |
| 8526 | // not that they should only consider the nearest enclosing |
| 8527 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 8528 | while (DC->isRecord()) |
| 8529 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8530 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8531 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8532 | |
| 8533 | // TODO: decide what we think about using declarations. |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 8534 | if (isLocal || !Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8535 | break; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 8536 | |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 8537 | if (isTemplateId) { |
| 8538 | if (isa<TranslationUnitDecl>(DC)) break; |
| 8539 | } else { |
| 8540 | if (DC->isFileContext()) break; |
| 8541 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8542 | DC = DC->getParent(); |
| 8543 | } |
| 8544 | |
| 8545 | // C++ [class.friend]p1: A friend of a class is a function or |
| 8546 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 8547 | // C++0x changes this for both friend types and functions. |
| 8548 | // Most C++ 98 compilers do seem to give an error here, so |
| 8549 | // we do, too. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 8550 | if (!Previous.empty() && DC->Equals(CurContext) |
| 8551 | && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8552 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8553 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 8554 | DCScope = getScopeForDeclContext(S, DC); |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 8555 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8556 | // - There's a non-dependent scope specifier, in which case we |
| 8557 | // compute it and do a previous lookup there for a function |
| 8558 | // or function template. |
| 8559 | } else if (!SS.getScopeRep()->isDependent()) { |
| 8560 | DC = computeDeclContext(SS); |
| 8561 | if (!DC) return 0; |
| 8562 | |
| 8563 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 8564 | |
| 8565 | LookupQualifiedName(Previous, DC); |
| 8566 | |
| 8567 | // Ignore things found implicitly in the wrong scope. |
| 8568 | // TODO: better diagnostics for this case. Suggesting the right |
| 8569 | // qualified scope would be nice... |
| 8570 | LookupResult::Filter F = Previous.makeFilter(); |
| 8571 | while (F.hasNext()) { |
| 8572 | NamedDecl *D = F.next(); |
| 8573 | if (!DC->InEnclosingNamespaceSetOf( |
| 8574 | D->getDeclContext()->getRedeclContext())) |
| 8575 | F.erase(); |
| 8576 | } |
| 8577 | F.done(); |
| 8578 | |
| 8579 | if (Previous.empty()) { |
| 8580 | D.setInvalidType(); |
| 8581 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 8582 | return 0; |
| 8583 | } |
| 8584 | |
| 8585 | // C++ [class.friend]p1: A friend of a class is a function or |
| 8586 | // class that is not a member of the class . . . |
| 8587 | if (DC->Equals(CurContext)) |
| 8588 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 8589 | |
| 8590 | // - There's a scope specifier that does not match any template |
| 8591 | // parameter lists, in which case we use some arbitrary context, |
| 8592 | // create a method or method template, and wait for instantiation. |
| 8593 | // - There's a scope specifier that does match some template |
| 8594 | // parameter lists, which we don't handle right now. |
| 8595 | } else { |
| 8596 | DC = CurContext; |
| 8597 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8598 | } |
| 8599 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 8600 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8601 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8602 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 8603 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 8604 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8605 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 8606 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 8607 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8608 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8609 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8610 | } |
| 8611 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 8612 | bool Redeclaration = false; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 8613 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, T, TInfo, Previous, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 8614 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 8615 | IsDefinition, |
| 8616 | Redeclaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8617 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 8618 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 8619 | assert(ND->getDeclContext() == DC); |
| 8620 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 8621 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 8622 | // Add the function declaration to the appropriate lookup tables, |
| 8623 | // adjusting the redeclarations list as necessary. We don't |
| 8624 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8625 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 8626 | // Also update the scope-based lookup if the target context's |
| 8627 | // lookup context is in lexical scope. |
| 8628 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 8629 | DC = DC->getRedeclContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 8630 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 8631 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 8632 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 8633 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8634 | |
| 8635 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 8636 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8637 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 8638 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 8639 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 8640 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8641 | if (ND->isInvalidDecl()) |
| 8642 | FrD->setInvalidDecl(); |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 8643 | else { |
| 8644 | FunctionDecl *FD; |
| 8645 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 8646 | FD = FTD->getTemplatedDecl(); |
| 8647 | else |
| 8648 | FD = cast<FunctionDecl>(ND); |
| 8649 | |
| 8650 | // Mark templated-scope function declarations as unsupported. |
| 8651 | if (FD->getNumTemplateParameterLists()) |
| 8652 | FrD->setUnsupportedFriend(true); |
| 8653 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 8654 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8655 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 8656 | } |
| 8657 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8658 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 8659 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8660 | |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 8661 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 8662 | if (!Fn) { |
| 8663 | Diag(DelLoc, diag::err_deleted_non_function); |
| 8664 | return; |
| 8665 | } |
| 8666 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 8667 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 8668 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 8669 | // If the declaration wasn't the first, we delete the function anyway for |
| 8670 | // recovery. |
| 8671 | } |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 8672 | Fn->setDeletedAsWritten(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 8673 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 8674 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 8675 | void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) { |
| 8676 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Dcl); |
| 8677 | |
| 8678 | if (MD) { |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 8679 | if (MD->getParent()->isDependentType()) { |
| 8680 | MD->setDefaulted(); |
| 8681 | MD->setExplicitlyDefaulted(); |
| 8682 | return; |
| 8683 | } |
| 8684 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 8685 | CXXSpecialMember Member = getSpecialMember(MD); |
| 8686 | if (Member == CXXInvalid) { |
| 8687 | Diag(DefaultLoc, diag::err_default_special_members); |
| 8688 | return; |
| 8689 | } |
| 8690 | |
| 8691 | MD->setDefaulted(); |
| 8692 | MD->setExplicitlyDefaulted(); |
| 8693 | |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 8694 | // If this definition appears within the record, do the checking when |
| 8695 | // the record is complete. |
| 8696 | const FunctionDecl *Primary = MD; |
| 8697 | if (MD->getTemplatedKind() != FunctionDecl::TK_NonTemplate) |
| 8698 | // Find the uninstantiated declaration that actually had the '= default' |
| 8699 | // on it. |
| 8700 | MD->getTemplateInstantiationPattern()->isDefined(Primary); |
| 8701 | |
| 8702 | if (Primary == Primary->getCanonicalDecl()) |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 8703 | return; |
| 8704 | |
| 8705 | switch (Member) { |
| 8706 | case CXXDefaultConstructor: { |
| 8707 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
| 8708 | CheckExplicitlyDefaultedDefaultConstructor(CD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8709 | if (!CD->isInvalidDecl()) |
| 8710 | DefineImplicitDefaultConstructor(DefaultLoc, CD); |
| 8711 | break; |
| 8712 | } |
| 8713 | |
| 8714 | case CXXCopyConstructor: { |
| 8715 | CXXConstructorDecl *CD = cast<CXXConstructorDecl>(MD); |
| 8716 | CheckExplicitlyDefaultedCopyConstructor(CD); |
| 8717 | if (!CD->isInvalidDecl()) |
| 8718 | DefineImplicitCopyConstructor(DefaultLoc, CD); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 8719 | break; |
| 8720 | } |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8721 | |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 8722 | case CXXCopyAssignment: { |
| 8723 | CheckExplicitlyDefaultedCopyAssignment(MD); |
| 8724 | if (!MD->isInvalidDecl()) |
| 8725 | DefineImplicitCopyAssignment(DefaultLoc, MD); |
| 8726 | break; |
| 8727 | } |
| 8728 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8729 | case CXXDestructor: { |
| 8730 | CXXDestructorDecl *DD = cast<CXXDestructorDecl>(MD); |
| 8731 | CheckExplicitlyDefaultedDestructor(DD); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 8732 | if (!DD->isInvalidDecl()) |
| 8733 | DefineImplicitDestructor(DefaultLoc, DD); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8734 | break; |
| 8735 | } |
| 8736 | |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 8737 | case CXXMoveConstructor: |
| 8738 | case CXXMoveAssignment: |
| 8739 | Diag(Dcl->getLocation(), diag::err_defaulted_move_unsupported); |
| 8740 | break; |
| 8741 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 8742 | default: |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 8743 | // FIXME: Do the rest once we have move functions |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 8744 | break; |
| 8745 | } |
| 8746 | } else { |
| 8747 | Diag(DefaultLoc, diag::err_default_special_members); |
| 8748 | } |
| 8749 | } |
| 8750 | |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 8751 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 8752 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 8753 | Stmt *SubStmt = *CI; |
| 8754 | if (!SubStmt) |
| 8755 | continue; |
| 8756 | if (isa<ReturnStmt>(SubStmt)) |
| 8757 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 8758 | diag::err_return_in_constructor_handler); |
| 8759 | if (!isa<Expr>(SubStmt)) |
| 8760 | SearchForReturnInStmt(Self, SubStmt); |
| 8761 | } |
| 8762 | } |
| 8763 | |
| 8764 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 8765 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 8766 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 8767 | SearchForReturnInStmt(*this, Handler); |
| 8768 | } |
| 8769 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8770 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8771 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8772 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 8773 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 8774 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8775 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 8776 | if (Context.hasSameType(NewTy, OldTy) || |
| 8777 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8778 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8779 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8780 | // Check if the return types are covariant |
| 8781 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8782 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8783 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 8784 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 8785 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8786 | NewClassTy = NewPT->getPointeeType(); |
| 8787 | OldClassTy = OldPT->getPointeeType(); |
| 8788 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 8789 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 8790 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 8791 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 8792 | NewClassTy = NewRT->getPointeeType(); |
| 8793 | OldClassTy = OldRT->getPointeeType(); |
| 8794 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8795 | } |
| 8796 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8797 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8798 | // The return types aren't either both pointers or references to a class type. |
| 8799 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8800 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8801 | diag::err_different_return_type_for_overriding_virtual_function) |
| 8802 | << New->getDeclName() << NewTy << OldTy; |
| 8803 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8804 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8805 | return true; |
| 8806 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8807 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 8808 | // C++ [class.virtual]p6: |
| 8809 | // If the return type of D::f differs from the return type of B::f, the |
| 8810 | // class type in the return type of D::f shall be complete at the point of |
| 8811 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 8812 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 8813 | if (!RT->isBeingDefined() && |
| 8814 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 8815 | PDiag(diag::err_covariant_return_incomplete) |
| 8816 | << New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 8817 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 8818 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 8819 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 8820 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8821 | // Check if the new class derives from the old class. |
| 8822 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 8823 | Diag(New->getLocation(), |
| 8824 | diag::err_covariant_return_not_derived) |
| 8825 | << New->getDeclName() << NewTy << OldTy; |
| 8826 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 8827 | return true; |
| 8828 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8829 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8830 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 8831 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 8832 | diag::err_covariant_return_inaccessible_base, |
| 8833 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 8834 | // FIXME: Should this point to the return type? |
| 8835 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 8836 | // FIXME: this note won't trigger for delayed access control |
| 8837 | // diagnostics, and it's impossible to get an undelayed error |
| 8838 | // here from access control during the original parse because |
| 8839 | // the ParsingDeclSpec/ParsingDeclarator are still in scope. |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8840 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 8841 | return true; |
| 8842 | } |
| 8843 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8844 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8845 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 8846 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8847 | Diag(New->getLocation(), |
| 8848 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8849 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8850 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 8851 | return true; |
| 8852 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8853 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8854 | |
| 8855 | // The new class type must have the same or less qualifiers as the old type. |
| 8856 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 8857 | Diag(New->getLocation(), |
| 8858 | diag::err_covariant_return_type_class_type_more_qualified) |
| 8859 | << New->getDeclName() << NewTy << OldTy; |
| 8860 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 8861 | return true; |
| 8862 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8863 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 8864 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 8865 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8866 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8867 | /// \brief Mark the given method pure. |
| 8868 | /// |
| 8869 | /// \param Method the method to be marked pure. |
| 8870 | /// |
| 8871 | /// \param InitRange the source range that covers the "0" initializer. |
| 8872 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8873 | SourceLocation EndLoc = InitRange.getEnd(); |
| 8874 | if (EndLoc.isValid()) |
| 8875 | Method->setRangeEnd(EndLoc); |
| 8876 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8877 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 8878 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8879 | return false; |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8880 | } |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 8881 | |
| 8882 | if (!Method->isInvalidDecl()) |
| 8883 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 8884 | << Method->getDeclName() << InitRange; |
| 8885 | return true; |
| 8886 | } |
| 8887 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 8888 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 8889 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 8890 | /// is a fresh scope pushed for just this purpose. |
| 8891 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8892 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 8893 | /// static data member of class X, names should be looked up in the scope of |
| 8894 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8895 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8896 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 8897 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8898 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 8899 | // We should only get called for declarations with scope specifiers, like: |
| 8900 | // int foo::bar; |
| 8901 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 8902 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8903 | } |
| 8904 | |
| 8905 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8906 | /// initializer for the out-of-line declaration 'D'. |
| 8907 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8908 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 8909 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8910 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 8911 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 8912 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 8913 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 8914 | |
| 8915 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 8916 | /// C++ if/switch/while/for statement. |
| 8917 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8918 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 8919 | // C++ 6.4p2: |
| 8920 | // The declarator shall not specify a function or an array. |
| 8921 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 8922 | // new class or enumeration. |
| 8923 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 8924 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 8925 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 8926 | TagDecl *OwnedTag = 0; |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8927 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag); |
| 8928 | QualType Ty = TInfo->getType(); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 8929 | |
| 8930 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 8931 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 8932 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 8933 | Diag(D.getIdentifierLoc(), diag::err_invalid_use_of_function_type) |
| 8934 | << D.getSourceRange(); |
| 8935 | return DeclResult(); |
| 8936 | } else if (OwnedTag && OwnedTag->isDefinition()) { |
| 8937 | // The type-specifier-seq shall not declare a new class or enumeration. |
| 8938 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition); |
| 8939 | } |
| 8940 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 8941 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 8942 | if (!Dcl) |
| 8943 | return DeclResult(); |
| 8944 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 8945 | return Dcl; |
| 8946 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 8947 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8948 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 8949 | bool DefinitionRequired) { |
| 8950 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 8951 | // not have a vtable. |
| 8952 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 8953 | CurContext->isDependentContext() || |
| 8954 | ExprEvalContexts.back().Context == Unevaluated) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 8955 | return; |
| 8956 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8957 | // Try to insert this class into the map. |
| 8958 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 8959 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 8960 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 8961 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 8962 | // If we already had an entry, check to see if we are promoting this vtable |
| 8963 | // to required a definition. If so, we need to reappend to the VTableUses |
| 8964 | // list, since we may have already processed the first entry. |
| 8965 | if (DefinitionRequired && !Pos.first->second) { |
| 8966 | Pos.first->second = true; |
| 8967 | } else { |
| 8968 | // Otherwise, we can early exit. |
| 8969 | return; |
| 8970 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8971 | } |
| 8972 | |
| 8973 | // Local classes need to have their virtual members marked |
| 8974 | // immediately. For all other classes, we mark their virtual members |
| 8975 | // at the end of the translation unit. |
| 8976 | if (Class->isLocalClass()) |
| 8977 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 8978 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8979 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 8980 | } |
| 8981 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8982 | bool Sema::DefineUsedVTables() { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8983 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 8984 | return false; |
Chandler Carruth | aee543a | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 8985 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8986 | // Note: The VTableUses vector could grow as a result of marking |
| 8987 | // the members of a class as "used", so we check the size each |
| 8988 | // time through the loop and prefer indices (with are stable) to |
| 8989 | // iterators (which are not). |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 8990 | bool DefinedAnything = false; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8991 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 8992 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8993 | if (!Class) |
| 8994 | continue; |
| 8995 | |
| 8996 | SourceLocation Loc = VTableUses[I].second; |
| 8997 | |
| 8998 | // If this class has a key function, but that key function is |
| 8999 | // defined in another translation unit, we don't need to emit the |
| 9000 | // vtable even though we're using it. |
| 9001 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 9002 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9003 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 9004 | case TSK_Undeclared: |
| 9005 | case TSK_ExplicitSpecialization: |
| 9006 | case TSK_ExplicitInstantiationDeclaration: |
| 9007 | // The key function is in another translation unit. |
| 9008 | continue; |
| 9009 | |
| 9010 | case TSK_ExplicitInstantiationDefinition: |
| 9011 | case TSK_ImplicitInstantiation: |
| 9012 | // We will be instantiating the key function. |
| 9013 | break; |
| 9014 | } |
| 9015 | } else if (!KeyFunction) { |
| 9016 | // If we have a class with no key function that is the subject |
| 9017 | // of an explicit instantiation declaration, suppress the |
| 9018 | // vtable; it will live with the explicit instantiation |
| 9019 | // definition. |
| 9020 | bool IsExplicitInstantiationDeclaration |
| 9021 | = Class->getTemplateSpecializationKind() |
| 9022 | == TSK_ExplicitInstantiationDeclaration; |
| 9023 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 9024 | REnd = Class->redecls_end(); |
| 9025 | R != REnd; ++R) { |
| 9026 | TemplateSpecializationKind TSK |
| 9027 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 9028 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 9029 | IsExplicitInstantiationDeclaration = true; |
| 9030 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 9031 | IsExplicitInstantiationDeclaration = false; |
| 9032 | break; |
| 9033 | } |
| 9034 | } |
| 9035 | |
| 9036 | if (IsExplicitInstantiationDeclaration) |
| 9037 | continue; |
| 9038 | } |
| 9039 | |
| 9040 | // Mark all of the virtual members of this class as referenced, so |
| 9041 | // that we can build a vtable. Then, tell the AST consumer that a |
| 9042 | // vtable for this class is required. |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 9043 | DefinedAnything = true; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9044 | MarkVirtualMembersReferenced(Loc, Class); |
| 9045 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 9046 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 9047 | |
| 9048 | // Optionally warn if we're emitting a weak vtable. |
| 9049 | if (Class->getLinkage() == ExternalLinkage && |
| 9050 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 9051 | if (!KeyFunction || (KeyFunction->hasBody() && KeyFunction->isInlined())) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9052 | Diag(Class->getLocation(), diag::warn_weak_vtable) << Class; |
| 9053 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 9054 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9055 | VTableUses.clear(); |
| 9056 | |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 9057 | return DefinedAnything; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 9058 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 9059 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 9060 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 9061 | const CXXRecordDecl *RD) { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 9062 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 9063 | e = RD->method_end(); i != e; ++i) { |
| 9064 | CXXMethodDecl *MD = *i; |
| 9065 | |
| 9066 | // C++ [basic.def.odr]p2: |
| 9067 | // [...] A virtual member function is used if it is not pure. [...] |
| 9068 | if (MD->isVirtual() && !MD->isPure()) |
| 9069 | MarkDeclarationReferenced(Loc, MD); |
| 9070 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 9071 | |
| 9072 | // Only classes that have virtual bases need a VTT. |
| 9073 | if (RD->getNumVBases() == 0) |
| 9074 | return; |
| 9075 | |
| 9076 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 9077 | e = RD->bases_end(); i != e; ++i) { |
| 9078 | const CXXRecordDecl *Base = |
| 9079 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 9080 | if (Base->getNumVBases() == 0) |
| 9081 | continue; |
| 9082 | MarkVirtualMembersReferenced(Loc, Base); |
| 9083 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 9084 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9085 | |
| 9086 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 9087 | /// Objective-C implementation whose ivars need be initialized. |
| 9088 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 9089 | if (!getLangOptions().CPlusPlus) |
| 9090 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 9091 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9092 | llvm::SmallVector<ObjCIvarDecl*, 8> ivars; |
| 9093 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 9094 | if (ivars.empty()) |
| 9095 | return; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 9096 | llvm::SmallVector<CXXCtorInitializer*, 32> AllToInit; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9097 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 9098 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 9099 | if (Field->isInvalidDecl()) |
| 9100 | continue; |
| 9101 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 9102 | CXXCtorInitializer *Member; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9103 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 9104 | InitializationKind InitKind = |
| 9105 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 9106 | |
| 9107 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9108 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 9109 | InitSeq.Perform(*this, InitEntity, InitKind, MultiExprArg()); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 9110 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9111 | // Note, MemberInit could actually come back empty if no initialization |
| 9112 | // is required (e.g., because it would call a trivial default constructor) |
| 9113 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 9114 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 9115 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9116 | Member = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 9117 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 9118 | SourceLocation(), |
| 9119 | MemberInit.takeAs<Expr>(), |
| 9120 | SourceLocation()); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9121 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 9122 | |
| 9123 | // Be sure that the destructor is accessible and is marked as referenced. |
| 9124 | if (const RecordType *RecordTy |
| 9125 | = Context.getBaseElementType(Field->getType()) |
| 9126 | ->getAs<RecordType>()) { |
| 9127 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 9128 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 9129 | MarkDeclarationReferenced(Field->getLocation(), Destructor); |
| 9130 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 9131 | PDiag(diag::err_access_dtor_ivar) |
| 9132 | << Context.getBaseElementType(Field->getType())); |
| 9133 | } |
| 9134 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 9135 | } |
| 9136 | ObjCImplementation->setIvarInitializers(Context, |
| 9137 | AllToInit.data(), AllToInit.size()); |
| 9138 | } |
| 9139 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 9140 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 9141 | static |
| 9142 | void DelegatingCycleHelper(CXXConstructorDecl* Ctor, |
| 9143 | llvm::SmallSet<CXXConstructorDecl*, 4> &Valid, |
| 9144 | llvm::SmallSet<CXXConstructorDecl*, 4> &Invalid, |
| 9145 | llvm::SmallSet<CXXConstructorDecl*, 4> &Current, |
| 9146 | Sema &S) { |
| 9147 | llvm::SmallSet<CXXConstructorDecl*, 4>::iterator CI = Current.begin(), |
| 9148 | CE = Current.end(); |
| 9149 | if (Ctor->isInvalidDecl()) |
| 9150 | return; |
| 9151 | |
| 9152 | const FunctionDecl *FNTarget = 0; |
| 9153 | CXXConstructorDecl *Target; |
| 9154 | |
| 9155 | // We ignore the result here since if we don't have a body, Target will be |
| 9156 | // null below. |
| 9157 | (void)Ctor->getTargetConstructor()->hasBody(FNTarget); |
| 9158 | Target |
| 9159 | = const_cast<CXXConstructorDecl*>(cast_or_null<CXXConstructorDecl>(FNTarget)); |
| 9160 | |
| 9161 | CXXConstructorDecl *Canonical = Ctor->getCanonicalDecl(), |
| 9162 | // Avoid dereferencing a null pointer here. |
| 9163 | *TCanonical = Target ? Target->getCanonicalDecl() : 0; |
| 9164 | |
| 9165 | if (!Current.insert(Canonical)) |
| 9166 | return; |
| 9167 | |
| 9168 | // We know that beyond here, we aren't chaining into a cycle. |
| 9169 | if (!Target || !Target->isDelegatingConstructor() || |
| 9170 | Target->isInvalidDecl() || Valid.count(TCanonical)) { |
| 9171 | for (CI = Current.begin(), CE = Current.end(); CI != CE; ++CI) |
| 9172 | Valid.insert(*CI); |
| 9173 | Current.clear(); |
| 9174 | // We've hit a cycle. |
| 9175 | } else if (TCanonical == Canonical || Invalid.count(TCanonical) || |
| 9176 | Current.count(TCanonical)) { |
| 9177 | // If we haven't diagnosed this cycle yet, do so now. |
| 9178 | if (!Invalid.count(TCanonical)) { |
| 9179 | S.Diag((*Ctor->init_begin())->getSourceLocation(), |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 9180 | diag::warn_delegating_ctor_cycle) |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 9181 | << Ctor; |
| 9182 | |
| 9183 | // Don't add a note for a function delegating directo to itself. |
| 9184 | if (TCanonical != Canonical) |
| 9185 | S.Diag(Target->getLocation(), diag::note_it_delegates_to); |
| 9186 | |
| 9187 | CXXConstructorDecl *C = Target; |
| 9188 | while (C->getCanonicalDecl() != Canonical) { |
| 9189 | (void)C->getTargetConstructor()->hasBody(FNTarget); |
| 9190 | assert(FNTarget && "Ctor cycle through bodiless function"); |
| 9191 | |
| 9192 | C |
| 9193 | = const_cast<CXXConstructorDecl*>(cast<CXXConstructorDecl>(FNTarget)); |
| 9194 | S.Diag(C->getLocation(), diag::note_which_delegates_to); |
| 9195 | } |
| 9196 | } |
| 9197 | |
| 9198 | for (CI = Current.begin(), CE = Current.end(); CI != CE; ++CI) |
| 9199 | Invalid.insert(*CI); |
| 9200 | Current.clear(); |
| 9201 | } else { |
| 9202 | DelegatingCycleHelper(Target, Valid, Invalid, Current, S); |
| 9203 | } |
| 9204 | } |
| 9205 | |
| 9206 | |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 9207 | void Sema::CheckDelegatingCtorCycles() { |
| 9208 | llvm::SmallSet<CXXConstructorDecl*, 4> Valid, Invalid, Current; |
| 9209 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 9210 | llvm::SmallSet<CXXConstructorDecl*, 4>::iterator CI = Current.begin(), |
| 9211 | CE = Current.end(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 9212 | |
| 9213 | for (llvm::SmallVector<CXXConstructorDecl*, 4>::iterator |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 9214 | I = DelegatingCtorDecls.begin(), |
| 9215 | E = DelegatingCtorDecls.end(); |
| 9216 | I != E; ++I) { |
| 9217 | DelegatingCycleHelper(*I, Valid, Invalid, Current, *this); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 9218 | } |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 9219 | |
| 9220 | for (CI = Invalid.begin(), CE = Invalid.end(); CI != CE; ++CI) |
| 9221 | (*CI)->setInvalidDecl(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 9222 | } |