Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 1 | //===------ SemaDeclCXX.cpp - Semantic Analysis for C++ Declarations ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/CharUnits.h" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclVisitor.h" |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 21 | #include "clang/AST/EvaluatedExprVisitor.h" |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecordLayout.h" |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Richard Smith | 4ac537b | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 30 | #include "clang/Lex/LiteralSupport.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 32 | #include "clang/Sema/CXXFieldCollector.h" |
| 33 | #include "clang/Sema/DeclSpec.h" |
| 34 | #include "clang/Sema/Initialization.h" |
| 35 | #include "clang/Sema/Lookup.h" |
| 36 | #include "clang/Sema/ParsedTemplate.h" |
| 37 | #include "clang/Sema/Scope.h" |
| 38 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 41 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 42 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 43 | |
| 44 | using namespace clang; |
| 45 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | // CheckDefaultArgumentVisitor |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 50 | namespace { |
| 51 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 52 | /// the default argument of a parameter to determine whether it |
| 53 | /// contains any ill-formed subexpressions. For example, this will |
| 54 | /// diagnose the use of local variables or parameters within the |
| 55 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 56 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 57 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 58 | Expr *DefaultArg; |
| 59 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 61 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 63 | : DefaultArg(defarg), S(s) {} |
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 | bool VisitExpr(Expr *Node); |
| 66 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 67 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Douglas Gregor | f0459f8 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 68 | bool VisitLambdaExpr(LambdaExpr *Lambda); |
John McCall | 045d252 | 2013-04-09 01:56:28 +0000 | [diff] [blame] | 69 | bool VisitPseudoObjectExpr(PseudoObjectExpr *POE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 70 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 72 | /// VisitExpr - Visit all of the children of this expression. |
| 73 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 74 | bool IsInvalid = false; |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 75 | for (Stmt::child_range I = Node->children(); I; ++I) |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 76 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 77 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 80 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 81 | /// determine whether this declaration can be used in the default |
| 82 | /// argument expression. |
| 83 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 84 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 85 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 86 | // C++ [dcl.fct.default]p9 |
| 87 | // Default arguments are evaluated each time the function is |
| 88 | // called. The order of evaluation of function arguments is |
| 89 | // unspecified. Consequently, parameters of a function shall not |
| 90 | // be used in default argument expressions, even if they are not |
| 91 | // evaluated. Parameters of a function declared before a default |
| 92 | // argument expression are in scope and can hide namespace and |
| 93 | // class member names. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 94 | return S->Diag(DRE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 95 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 96 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 97 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 98 | // C++ [dcl.fct.default]p7 |
| 99 | // Local variables shall not be used in default argument |
| 100 | // expressions. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 101 | if (VDecl->isLocalVarDecl()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 102 | return S->Diag(DRE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 103 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 104 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 105 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 106 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 107 | return false; |
| 108 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 109 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 110 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 111 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 112 | // C++ [dcl.fct.default]p8: |
| 113 | // The keyword this shall not be used in a default argument of a |
| 114 | // member function. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 115 | return S->Diag(ThisE->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 116 | diag::err_param_default_argument_references_this) |
| 117 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 118 | } |
Douglas Gregor | f0459f8 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 119 | |
John McCall | 045d252 | 2013-04-09 01:56:28 +0000 | [diff] [blame] | 120 | bool CheckDefaultArgumentVisitor::VisitPseudoObjectExpr(PseudoObjectExpr *POE) { |
| 121 | bool Invalid = false; |
| 122 | for (PseudoObjectExpr::semantics_iterator |
| 123 | i = POE->semantics_begin(), e = POE->semantics_end(); i != e; ++i) { |
| 124 | Expr *E = *i; |
| 125 | |
| 126 | // Look through bindings. |
| 127 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) { |
| 128 | E = OVE->getSourceExpr(); |
| 129 | assert(E && "pseudo-object binding without source expression?"); |
| 130 | } |
| 131 | |
| 132 | Invalid |= Visit(E); |
| 133 | } |
| 134 | return Invalid; |
| 135 | } |
| 136 | |
Douglas Gregor | f0459f8 | 2012-02-10 23:30:22 +0000 | [diff] [blame] | 137 | bool CheckDefaultArgumentVisitor::VisitLambdaExpr(LambdaExpr *Lambda) { |
| 138 | // C++11 [expr.lambda.prim]p13: |
| 139 | // A lambda-expression appearing in a default argument shall not |
| 140 | // implicitly or explicitly capture any entity. |
| 141 | if (Lambda->capture_begin() == Lambda->capture_end()) |
| 142 | return false; |
| 143 | |
| 144 | return S->Diag(Lambda->getLocStart(), |
| 145 | diag::err_lambda_capture_default_arg); |
| 146 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 149 | void |
| 150 | Sema::ImplicitExceptionSpecification::CalledDecl(SourceLocation CallLoc, |
| 151 | const CXXMethodDecl *Method) { |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 152 | // If we have an MSAny spec already, don't bother. |
| 153 | if (!Method || ComputedEST == EST_MSAny) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 154 | return; |
| 155 | |
| 156 | const FunctionProtoType *Proto |
| 157 | = Method->getType()->getAs<FunctionProtoType>(); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 158 | Proto = Self->ResolveExceptionSpec(CallLoc, Proto); |
| 159 | if (!Proto) |
| 160 | return; |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 161 | |
| 162 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
| 163 | |
| 164 | // If this function can throw any exceptions, make a note of that. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 165 | if (EST == EST_MSAny || EST == EST_None) { |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 166 | ClearExceptions(); |
| 167 | ComputedEST = EST; |
| 168 | return; |
| 169 | } |
| 170 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 171 | // FIXME: If the call to this decl is using any of its default arguments, we |
| 172 | // need to search them for potentially-throwing calls. |
| 173 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 174 | // If this function has a basic noexcept, it doesn't affect the outcome. |
| 175 | if (EST == EST_BasicNoexcept) |
| 176 | return; |
| 177 | |
| 178 | // If we have a throw-all spec at this point, ignore the function. |
| 179 | if (ComputedEST == EST_None) |
| 180 | return; |
| 181 | |
| 182 | // If we're still at noexcept(true) and there's a nothrow() callee, |
| 183 | // change to that specification. |
| 184 | if (EST == EST_DynamicNone) { |
| 185 | if (ComputedEST == EST_BasicNoexcept) |
| 186 | ComputedEST = EST_DynamicNone; |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | // Check out noexcept specs. |
| 191 | if (EST == EST_ComputedNoexcept) { |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 192 | FunctionProtoType::NoexceptResult NR = |
| 193 | Proto->getNoexceptSpec(Self->Context); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 194 | assert(NR != FunctionProtoType::NR_NoNoexcept && |
| 195 | "Must have noexcept result for EST_ComputedNoexcept."); |
| 196 | assert(NR != FunctionProtoType::NR_Dependent && |
| 197 | "Should not generate implicit declarations for dependent cases, " |
| 198 | "and don't know how to handle them anyway."); |
| 199 | |
| 200 | // noexcept(false) -> no spec on the new function |
| 201 | if (NR == FunctionProtoType::NR_Throw) { |
| 202 | ClearExceptions(); |
| 203 | ComputedEST = EST_None; |
| 204 | } |
| 205 | // noexcept(true) won't change anything either. |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | assert(EST == EST_Dynamic && "EST case not considered earlier."); |
| 210 | assert(ComputedEST != EST_None && |
| 211 | "Shouldn't collect exceptions when throw-all is guaranteed."); |
| 212 | ComputedEST = EST_Dynamic; |
| 213 | // Record the exceptions in this function's exception specification. |
| 214 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 215 | EEnd = Proto->exception_end(); |
| 216 | E != EEnd; ++E) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 217 | if (ExceptionsSeen.insert(Self->Context.getCanonicalType(*E))) |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 218 | Exceptions.push_back(*E); |
| 219 | } |
| 220 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 221 | void Sema::ImplicitExceptionSpecification::CalledExpr(Expr *E) { |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 222 | if (!E || ComputedEST == EST_MSAny) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 223 | return; |
| 224 | |
| 225 | // FIXME: |
| 226 | // |
| 227 | // C++0x [except.spec]p14: |
NAKAMURA Takumi | 4857947 | 2011-06-21 03:19:28 +0000 | [diff] [blame] | 228 | // [An] implicit exception-specification specifies the type-id T if and |
| 229 | // only if T is allowed by the exception-specification of a function directly |
| 230 | // 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] | 231 | // function it directly invokes allows all exceptions, and f shall allow no |
| 232 | // exceptions if every function it directly invokes allows no exceptions. |
| 233 | // |
| 234 | // Note in particular that if an implicit exception-specification is generated |
| 235 | // for a function containing a throw-expression, that specification can still |
| 236 | // be noexcept(true). |
| 237 | // |
| 238 | // Note also that 'directly invoked' is not defined in the standard, and there |
| 239 | // is no indication that we should only consider potentially-evaluated calls. |
| 240 | // |
| 241 | // Ultimately we should implement the intent of the standard: the exception |
| 242 | // specification should be the set of exceptions which can be thrown by the |
| 243 | // implicit definition. For now, we assume that any non-nothrow expression can |
| 244 | // throw any exception. |
| 245 | |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 246 | if (Self->canThrow(E)) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 247 | ComputedEST = EST_None; |
| 248 | } |
| 249 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 250 | bool |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 251 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | SourceLocation EqualLoc) { |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 253 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 254 | diag::err_typecheck_decl_incomplete_type)) { |
| 255 | Param->setInvalidDecl(); |
| 256 | return true; |
| 257 | } |
| 258 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 259 | // C++ [dcl.fct.default]p5 |
| 260 | // A default argument expression is implicitly converted (clause |
| 261 | // 4) to the parameter type. The default argument expression has |
| 262 | // the same semantic constraints as the initializer expression in |
| 263 | // a declaration of a variable of the parameter type, using the |
| 264 | // copy-initialization semantics (8.5). |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 265 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 266 | Param); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 267 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 268 | EqualLoc); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 269 | InitializationSequence InitSeq(*this, Entity, Kind, Arg); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 270 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Arg); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 271 | if (Result.isInvalid()) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 272 | return true; |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 273 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 274 | |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 275 | CheckCompletedExpr(Arg, EqualLoc); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 276 | Arg = MaybeCreateExprWithCleanups(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 278 | // Okay: add the default argument to the parameter |
| 279 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | 8cfb7a3 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 281 | // We have already instantiated this parameter; provide each of the |
| 282 | // instantiations with the uninstantiated default argument. |
| 283 | UnparsedDefaultArgInstantiationsMap::iterator InstPos |
| 284 | = UnparsedDefaultArgInstantiations.find(Param); |
| 285 | if (InstPos != UnparsedDefaultArgInstantiations.end()) { |
| 286 | for (unsigned I = 0, N = InstPos->second.size(); I != N; ++I) |
| 287 | InstPos->second[I]->setUninstantiatedDefaultArg(Arg); |
| 288 | |
| 289 | // We're done tracking this parameter's instantiations. |
| 290 | UnparsedDefaultArgInstantiations.erase(InstPos); |
| 291 | } |
| 292 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 293 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 296 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 297 | /// provided for a function parameter is well-formed. If so, attach it |
| 298 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 299 | void |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 300 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 301 | Expr *DefaultArg) { |
| 302 | if (!param || !DefaultArg) |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 303 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 305 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 306 | UnparsedDefaultArgLocs.erase(Param); |
| 307 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 308 | // Default arguments are only permitted in C++ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 309 | if (!getLangOpts().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 310 | Diag(EqualLoc, diag::err_param_default_argument) |
| 311 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 312 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 316 | // Check for unexpanded parameter packs. |
| 317 | if (DiagnoseUnexpandedParameterPack(DefaultArg, UPPC_DefaultArgument)) { |
| 318 | Param->setInvalidDecl(); |
| 319 | return; |
| 320 | } |
| 321 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 322 | // Check that the default argument is well-formed |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 323 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 324 | if (DefaultArgChecker.Visit(DefaultArg)) { |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 325 | Param->setInvalidDecl(); |
| 326 | return; |
| 327 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 329 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 332 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 333 | /// argument for a function parameter, but we can't parse it yet |
| 334 | /// because we're inside a class definition. Note that this default |
| 335 | /// argument will be parsed later. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 336 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 337 | SourceLocation EqualLoc, |
| 338 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 339 | if (!param) |
| 340 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 342 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 343 | if (Param) |
| 344 | Param->setUnparsedDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 346 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 349 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 350 | /// the default argument for the parameter param failed. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 351 | void Sema::ActOnParamDefaultArgumentError(Decl *param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 352 | if (!param) |
| 353 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 355 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 357 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 359 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 362 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 363 | /// arguments in the declarator, which is not a function declaration |
| 364 | /// or definition and therefore is not permitted to have default |
| 365 | /// arguments. This routine should be invoked for every declarator |
| 366 | /// that is not a function declaration or definition. |
| 367 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 368 | // C++ [dcl.fct.default]p3 |
| 369 | // A default argument expression shall be specified only in the |
| 370 | // parameter-declaration-clause of a function declaration or in a |
| 371 | // template-parameter (14.1). It shall not be specified for a |
| 372 | // parameter pack. If it is specified in a |
| 373 | // parameter-declaration-clause, it shall not occur within a |
| 374 | // declarator or abstract-declarator of a parameter-declaration. |
Richard Smith | 3cdbbdc | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 375 | bool MightBeFunction = D.isFunctionDeclarationContext(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 376 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 377 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 378 | if (chunk.Kind == DeclaratorChunk::Function) { |
Richard Smith | 3cdbbdc | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 379 | if (MightBeFunction) { |
| 380 | // This is a function declaration. It can have default arguments, but |
| 381 | // keep looking in case its return type is a function type with default |
| 382 | // arguments. |
| 383 | MightBeFunction = false; |
| 384 | continue; |
| 385 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 386 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 387 | ParmVarDecl *Param = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 388 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 389 | if (Param->hasUnparsedDefaultArg()) { |
| 390 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 391 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
Richard Smith | 3cdbbdc | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 392 | << SourceRange((*Toks)[1].getLocation(), |
| 393 | Toks->back().getLocation()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 394 | delete Toks; |
| 395 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 396 | } else if (Param->getDefaultArg()) { |
| 397 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 398 | << Param->getDefaultArg()->getSourceRange(); |
| 399 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
Richard Smith | 3cdbbdc | 2013-03-06 01:37:38 +0000 | [diff] [blame] | 402 | } else if (chunk.Kind != DeclaratorChunk::Paren) { |
| 403 | MightBeFunction = false; |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
David Majnemer | f6a144f | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 408 | static bool functionDeclHasDefaultArgument(const FunctionDecl *FD) { |
| 409 | for (unsigned NumParams = FD->getNumParams(); NumParams > 0; --NumParams) { |
| 410 | const ParmVarDecl *PVD = FD->getParamDecl(NumParams-1); |
| 411 | if (!PVD->hasDefaultArg()) |
| 412 | return false; |
| 413 | if (!PVD->hasInheritedDefaultArg()) |
| 414 | return true; |
| 415 | } |
| 416 | return false; |
| 417 | } |
| 418 | |
Craig Topper | 1a6eac8 | 2012-09-21 04:33:26 +0000 | [diff] [blame] | 419 | /// MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 420 | /// function, once we already know that they have the same |
| 421 | /// type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 422 | /// error, false otherwise. |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 423 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old, |
| 424 | Scope *S) { |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 425 | bool Invalid = false; |
| 426 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 427 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 428 | // For non-template functions, default arguments can be added in |
| 429 | // later declarations of a function in the same |
| 430 | // scope. Declarations in different scopes have completely |
| 431 | // distinct sets of default arguments. That is, declarations in |
| 432 | // inner scopes do not acquire default arguments from |
| 433 | // declarations in outer scopes, and vice versa. In a given |
| 434 | // function declaration, all parameters subsequent to a |
| 435 | // parameter with a default argument shall have default |
| 436 | // arguments supplied in this or previous declarations. A |
| 437 | // default argument shall not be redefined by a later |
| 438 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 439 | // |
| 440 | // C++ [dcl.fct.default]p6: |
| 441 | // Except for member functions of class templates, the default arguments |
| 442 | // in a member function definition that appears outside of the class |
| 443 | // definition are added to the set of default arguments provided by the |
| 444 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 445 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 446 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 447 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 448 | |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 449 | bool OldParamHasDfl = OldParam->hasDefaultArg(); |
| 450 | bool NewParamHasDfl = NewParam->hasDefaultArg(); |
| 451 | |
| 452 | NamedDecl *ND = Old; |
| 453 | if (S && !isDeclInScope(ND, New->getDeclContext(), S)) |
| 454 | // Ignore default parameters of old decl if they are not in |
| 455 | // the same scope. |
| 456 | OldParamHasDfl = false; |
| 457 | |
| 458 | if (OldParamHasDfl && NewParamHasDfl) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 459 | |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 460 | unsigned DiagDefaultParamID = |
| 461 | diag::err_param_default_argument_redefinition; |
| 462 | |
| 463 | // MSVC accepts that default parameters be redefined for member functions |
| 464 | // of template class. The new default parameter's value is ignored. |
| 465 | Invalid = true; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 466 | if (getLangOpts().MicrosoftExt) { |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 467 | CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(New); |
| 468 | if (MD && MD->getParent()->getDescribedClassTemplate()) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 469 | // Merge the old default argument into the new parameter. |
| 470 | NewParam->setHasInheritedDefaultArg(); |
| 471 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 472 | NewParam->setUninstantiatedDefaultArg( |
| 473 | OldParam->getUninstantiatedDefaultArg()); |
| 474 | else |
| 475 | NewParam->setDefaultArg(OldParam->getInit()); |
Francois Pichet | cf320c6 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 476 | DiagDefaultParamID = diag::warn_param_default_argument_redefinition; |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 477 | Invalid = false; |
| 478 | } |
| 479 | } |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 480 | |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 481 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 482 | // hint here. Alternatively, we could walk the type-source information |
| 483 | // for NewParam to find the last source location in the type... but it |
| 484 | // isn't worth the effort right now. This is the kind of test case that |
| 485 | // is hard to get right: |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 486 | // int f(int); |
| 487 | // void g(int (*fp)(int) = f); |
| 488 | // void g(int (*fp)(int) = &f); |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 489 | Diag(NewParam->getLocation(), DiagDefaultParamID) |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 490 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 491 | |
| 492 | // Look for the function declaration where the default argument was |
| 493 | // actually written, which may be a declaration prior to Old. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 494 | for (FunctionDecl *Older = Old->getPreviousDecl(); |
| 495 | Older; Older = Older->getPreviousDecl()) { |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 496 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 497 | break; |
| 498 | |
| 499 | OldParam = Older->getParamDecl(p); |
| 500 | } |
| 501 | |
| 502 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 503 | << OldParam->getDefaultArgRange(); |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 504 | } else if (OldParamHasDfl) { |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 505 | // Merge the old default argument into the new parameter. |
| 506 | // It's important to use getInit() here; getDefaultArg() |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 507 | // strips off any top-level ExprWithCleanups. |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 508 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 509 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 510 | NewParam->setUninstantiatedDefaultArg( |
| 511 | OldParam->getUninstantiatedDefaultArg()); |
| 512 | else |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 513 | NewParam->setDefaultArg(OldParam->getInit()); |
James Molloy | 9cda03f | 2012-03-13 08:55:35 +0000 | [diff] [blame] | 514 | } else if (NewParamHasDfl) { |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 515 | if (New->getDescribedFunctionTemplate()) { |
| 516 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 517 | Diag(NewParam->getLocation(), |
| 518 | diag::err_param_default_argument_template_redecl) |
| 519 | << NewParam->getDefaultArgRange(); |
| 520 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 521 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 522 | } else if (New->getTemplateSpecializationKind() |
| 523 | != TSK_ImplicitInstantiation && |
| 524 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 525 | // C++ [temp.expr.spec]p21: |
| 526 | // Default function arguments shall not be specified in a declaration |
| 527 | // or a definition for one of the following explicit specializations: |
| 528 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 529 | // - the explicit specialization of a member function template; |
| 530 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 531 | // template where the class template specialization to which the |
| 532 | // member function specialization belongs is implicitly |
| 533 | // instantiated. |
| 534 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 535 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 536 | << New->getDeclName() |
| 537 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 538 | } else if (New->getDeclContext()->isDependentContext()) { |
| 539 | // C++ [dcl.fct.default]p6 (DR217): |
| 540 | // Default arguments for a member function of a class template shall |
| 541 | // be specified on the initial declaration of the member function |
| 542 | // within the class template. |
| 543 | // |
| 544 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 545 | // leads me to the conclusion that one cannot add default function |
| 546 | // arguments for an out-of-line definition of a member function of a |
| 547 | // dependent type. |
| 548 | int WhichKind = 2; |
| 549 | if (CXXRecordDecl *Record |
| 550 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 551 | if (Record->getDescribedClassTemplate()) |
| 552 | WhichKind = 0; |
| 553 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 554 | WhichKind = 1; |
| 555 | else |
| 556 | WhichKind = 2; |
| 557 | } |
| 558 | |
| 559 | Diag(NewParam->getLocation(), |
| 560 | diag::err_param_default_argument_member_template_redecl) |
| 561 | << WhichKind |
| 562 | << NewParam->getDefaultArgRange(); |
| 563 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
Richard Smith | b8abff6 | 2012-11-28 03:45:24 +0000 | [diff] [blame] | 567 | // DR1344: If a default argument is added outside a class definition and that |
| 568 | // default argument makes the function a special member function, the program |
| 569 | // is ill-formed. This can only happen for constructors. |
| 570 | if (isa<CXXConstructorDecl>(New) && |
| 571 | New->getMinRequiredArguments() < Old->getMinRequiredArguments()) { |
| 572 | CXXSpecialMember NewSM = getSpecialMember(cast<CXXMethodDecl>(New)), |
| 573 | OldSM = getSpecialMember(cast<CXXMethodDecl>(Old)); |
| 574 | if (NewSM != OldSM) { |
| 575 | ParmVarDecl *NewParam = New->getParamDecl(New->getMinRequiredArguments()); |
| 576 | assert(NewParam->hasDefaultArg()); |
| 577 | Diag(NewParam->getLocation(), diag::err_default_arg_makes_ctor_special) |
| 578 | << NewParam->getDefaultArgRange() << NewSM; |
| 579 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 580 | } |
| 581 | } |
| 582 | |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 583 | // C++11 [dcl.constexpr]p1: If any declaration of a function or function |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 584 | // template has a constexpr specifier then all its declarations shall |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 585 | // contain the constexpr specifier. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 586 | if (New->isConstexpr() != Old->isConstexpr()) { |
| 587 | Diag(New->getLocation(), diag::err_constexpr_redecl_mismatch) |
| 588 | << New << New->isConstexpr(); |
| 589 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 590 | Invalid = true; |
| 591 | } |
| 592 | |
David Majnemer | f6a144f | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 593 | // C++11 [dcl.fct.default]p4: If a friend declaration specifies a default |
NAKAMURA Takumi | fd527a4 | 2013-07-17 17:57:52 +0000 | [diff] [blame] | 594 | // argument expression, that declaration shall be a definition and shall be |
David Majnemer | f6a144f | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 595 | // the only declaration of the function or function template in the |
| 596 | // translation unit. |
| 597 | if (Old->getFriendObjectKind() == Decl::FOK_Undeclared && |
| 598 | functionDeclHasDefaultArgument(Old)) { |
| 599 | Diag(New->getLocation(), diag::err_friend_decl_with_def_arg_redeclared); |
| 600 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 601 | Invalid = true; |
| 602 | } |
| 603 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 604 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 605 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 607 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 610 | /// \brief Merge the exception specifications of two variable declarations. |
| 611 | /// |
| 612 | /// This is called when there's a redeclaration of a VarDecl. The function |
| 613 | /// checks if the redeclaration might have an exception specification and |
| 614 | /// validates compatibility and merges the specs if necessary. |
| 615 | void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) { |
| 616 | // Shortcut if exceptions are disabled. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 617 | if (!getLangOpts().CXXExceptions) |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 618 | return; |
| 619 | |
| 620 | assert(Context.hasSameType(New->getType(), Old->getType()) && |
| 621 | "Should only be called if types are otherwise the same."); |
| 622 | |
| 623 | QualType NewType = New->getType(); |
| 624 | QualType OldType = Old->getType(); |
| 625 | |
| 626 | // We're only interested in pointers and references to functions, as well |
| 627 | // as pointers to member functions. |
| 628 | if (const ReferenceType *R = NewType->getAs<ReferenceType>()) { |
| 629 | NewType = R->getPointeeType(); |
| 630 | OldType = OldType->getAs<ReferenceType>()->getPointeeType(); |
| 631 | } else if (const PointerType *P = NewType->getAs<PointerType>()) { |
| 632 | NewType = P->getPointeeType(); |
| 633 | OldType = OldType->getAs<PointerType>()->getPointeeType(); |
| 634 | } else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) { |
| 635 | NewType = M->getPointeeType(); |
| 636 | OldType = OldType->getAs<MemberPointerType>()->getPointeeType(); |
| 637 | } |
| 638 | |
| 639 | if (!NewType->isFunctionProtoType()) |
| 640 | return; |
| 641 | |
| 642 | // There's lots of special cases for functions. For function pointers, system |
| 643 | // libraries are hopefully not as broken so that we don't need these |
| 644 | // workarounds. |
| 645 | if (CheckEquivalentExceptionSpec( |
| 646 | OldType->getAs<FunctionProtoType>(), Old->getLocation(), |
| 647 | NewType->getAs<FunctionProtoType>(), New->getLocation())) { |
| 648 | New->setInvalidDecl(); |
| 649 | } |
| 650 | } |
| 651 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 652 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 653 | /// function declaration are well-formed according to C++ |
| 654 | /// [dcl.fct.default]. |
| 655 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 656 | unsigned NumParams = FD->getNumParams(); |
| 657 | unsigned p; |
| 658 | |
| 659 | // Find first parameter with a default argument |
| 660 | for (p = 0; p < NumParams; ++p) { |
| 661 | ParmVarDecl *Param = FD->getParamDecl(p); |
Richard Smith | 7974c60 | 2013-04-17 16:25:20 +0000 | [diff] [blame] | 662 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 663 | break; |
| 664 | } |
| 665 | |
| 666 | // C++ [dcl.fct.default]p4: |
| 667 | // In a given function declaration, all parameters |
| 668 | // subsequent to a parameter with a default argument shall |
| 669 | // have default arguments supplied in this or previous |
| 670 | // declarations. A default argument shall not be redefined |
| 671 | // by a later declaration (not even to the same value). |
| 672 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 674 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 675 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 676 | if (Param->isInvalidDecl()) |
| 677 | /* We already complained about this parameter. */; |
| 678 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 680 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 681 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 682 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 683 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 684 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 686 | LastMissingDefaultArg = p; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | if (LastMissingDefaultArg > 0) { |
| 691 | // Some default arguments were missing. Clear out all of the |
| 692 | // default arguments up to (and including) the last missing |
| 693 | // default argument, so that we leave the function parameters |
| 694 | // in a semantically valid state. |
| 695 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 696 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 697 | if (Param->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 698 | Param->setDefaultArg(0); |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 703 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 704 | // CheckConstexprParameterTypes - Check whether a function's parameter types |
| 705 | // are all literal types. If so, return true. If not, produce a suitable |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 706 | // diagnostic and return false. |
| 707 | static bool CheckConstexprParameterTypes(Sema &SemaRef, |
| 708 | const FunctionDecl *FD) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 709 | unsigned ArgIndex = 0; |
| 710 | const FunctionProtoType *FT = FD->getType()->getAs<FunctionProtoType>(); |
| 711 | for (FunctionProtoType::arg_type_iterator i = FT->arg_type_begin(), |
| 712 | e = FT->arg_type_end(); i != e; ++i, ++ArgIndex) { |
| 713 | const ParmVarDecl *PD = FD->getParamDecl(ArgIndex); |
| 714 | SourceLocation ParamLoc = PD->getLocation(); |
| 715 | if (!(*i)->isDependentType() && |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 716 | SemaRef.RequireLiteralType(ParamLoc, *i, |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 717 | diag::err_constexpr_non_literal_param, |
| 718 | ArgIndex+1, PD->getSourceRange(), |
| 719 | isa<CXXConstructorDecl>(FD))) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 720 | return false; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 721 | } |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 722 | return true; |
| 723 | } |
| 724 | |
| 725 | /// \brief Get diagnostic %select index for tag kind for |
| 726 | /// record diagnostic message. |
| 727 | /// WARNING: Indexes apply to particular diagnostics only! |
| 728 | /// |
| 729 | /// \returns diagnostic %select index. |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 730 | static unsigned getRecordDiagFromTagKind(TagTypeKind Tag) { |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 731 | switch (Tag) { |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 732 | case TTK_Struct: return 0; |
| 733 | case TTK_Interface: return 1; |
| 734 | case TTK_Class: return 2; |
| 735 | default: llvm_unreachable("Invalid tag kind for record diagnostic!"); |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 736 | } |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | // CheckConstexprFunctionDecl - Check whether a function declaration satisfies |
| 740 | // the requirements of a constexpr function definition or a constexpr |
| 741 | // constructor definition. If so, return true. If not, produce appropriate |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 742 | // diagnostics and return false. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 743 | // |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 744 | // This implements C++11 [dcl.constexpr]p3,4, as amended by DR1360. |
| 745 | bool Sema::CheckConstexprFunctionDecl(const FunctionDecl *NewFD) { |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 746 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD); |
| 747 | if (MD && MD->isInstance()) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 748 | // C++11 [dcl.constexpr]p4: |
| 749 | // The definition of a constexpr constructor shall satisfy the following |
| 750 | // constraints: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 751 | // - the class shall not have any virtual base classes; |
Joao Matos | 17d35c3 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 752 | const CXXRecordDecl *RD = MD->getParent(); |
| 753 | if (RD->getNumVBases()) { |
| 754 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual_base) |
| 755 | << isa<CXXConstructorDecl>(NewFD) |
| 756 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases(); |
| 757 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 758 | E = RD->vbases_end(); I != E; ++I) |
| 759 | Diag(I->getLocStart(), |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 760 | diag::note_constexpr_virtual_base_here) << I->getSourceRange(); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 761 | return false; |
| 762 | } |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | if (!isa<CXXConstructorDecl>(NewFD)) { |
| 766 | // C++11 [dcl.constexpr]p3: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 767 | // The definition of a constexpr function shall satisfy the following |
| 768 | // constraints: |
| 769 | // - it shall not be virtual; |
| 770 | const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD); |
| 771 | if (Method && Method->isVirtual()) { |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 772 | Diag(NewFD->getLocation(), diag::err_constexpr_virtual); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 773 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 774 | // If it's not obvious why this function is virtual, find an overridden |
| 775 | // function which uses the 'virtual' keyword. |
| 776 | const CXXMethodDecl *WrittenVirtual = Method; |
| 777 | while (!WrittenVirtual->isVirtualAsWritten()) |
| 778 | WrittenVirtual = *WrittenVirtual->begin_overridden_methods(); |
| 779 | if (WrittenVirtual != Method) |
| 780 | Diag(WrittenVirtual->getLocation(), |
| 781 | diag::note_overridden_virtual_function); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 782 | return false; |
| 783 | } |
| 784 | |
| 785 | // - its return type shall be a literal type; |
| 786 | QualType RT = NewFD->getResultType(); |
| 787 | if (!RT->isDependentType() && |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 788 | RequireLiteralType(NewFD->getLocation(), RT, |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 789 | diag::err_constexpr_non_literal_return)) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 790 | return false; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 793 | // - each of its parameter types shall be a literal type; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 794 | if (!CheckConstexprParameterTypes(*this, NewFD)) |
Richard Smith | 3534050 | 2012-01-13 04:54:00 +0000 | [diff] [blame] | 795 | return false; |
| 796 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 797 | return true; |
| 798 | } |
| 799 | |
| 800 | /// Check the given declaration statement is legal within a constexpr function |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 801 | /// body. C++11 [dcl.constexpr]p3,p4, and C++1y [dcl.constexpr]p3. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 802 | /// |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 803 | /// \return true if the body is OK (maybe only as an extension), false if we |
| 804 | /// have diagnosed a problem. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 805 | static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl, |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 806 | DeclStmt *DS, SourceLocation &Cxx1yLoc) { |
| 807 | // C++11 [dcl.constexpr]p3 and p4: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 808 | // The definition of a constexpr function(p3) or constructor(p4) [...] shall |
| 809 | // contain only |
| 810 | for (DeclStmt::decl_iterator DclIt = DS->decl_begin(), |
| 811 | DclEnd = DS->decl_end(); DclIt != DclEnd; ++DclIt) { |
| 812 | switch ((*DclIt)->getKind()) { |
| 813 | case Decl::StaticAssert: |
| 814 | case Decl::Using: |
| 815 | case Decl::UsingShadow: |
| 816 | case Decl::UsingDirective: |
| 817 | case Decl::UnresolvedUsingTypename: |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 818 | case Decl::UnresolvedUsingValue: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 819 | // - static_assert-declarations |
| 820 | // - using-declarations, |
| 821 | // - using-directives, |
| 822 | continue; |
| 823 | |
| 824 | case Decl::Typedef: |
| 825 | case Decl::TypeAlias: { |
| 826 | // - typedef declarations and alias-declarations that do not define |
| 827 | // classes or enumerations, |
| 828 | TypedefNameDecl *TN = cast<TypedefNameDecl>(*DclIt); |
| 829 | if (TN->getUnderlyingType()->isVariablyModifiedType()) { |
| 830 | // Don't allow variably-modified types in constexpr functions. |
| 831 | TypeLoc TL = TN->getTypeSourceInfo()->getTypeLoc(); |
| 832 | SemaRef.Diag(TL.getBeginLoc(), diag::err_constexpr_vla) |
| 833 | << TL.getSourceRange() << TL.getType() |
| 834 | << isa<CXXConstructorDecl>(Dcl); |
| 835 | return false; |
| 836 | } |
| 837 | continue; |
| 838 | } |
| 839 | |
| 840 | case Decl::Enum: |
| 841 | case Decl::CXXRecord: |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 842 | // C++1y allows types to be defined, not just declared. |
| 843 | if (cast<TagDecl>(*DclIt)->isThisDeclarationADefinition()) |
| 844 | SemaRef.Diag(DS->getLocStart(), |
| 845 | SemaRef.getLangOpts().CPlusPlus1y |
| 846 | ? diag::warn_cxx11_compat_constexpr_type_definition |
| 847 | : diag::ext_constexpr_type_definition) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 848 | << isa<CXXConstructorDecl>(Dcl); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 849 | continue; |
| 850 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 851 | case Decl::EnumConstant: |
| 852 | case Decl::IndirectField: |
| 853 | case Decl::ParmVar: |
| 854 | // These can only appear with other declarations which are banned in |
| 855 | // C++11 and permitted in C++1y, so ignore them. |
| 856 | continue; |
| 857 | |
| 858 | case Decl::Var: { |
| 859 | // C++1y [dcl.constexpr]p3 allows anything except: |
| 860 | // a definition of a variable of non-literal type or of static or |
| 861 | // thread storage duration or for which no initialization is performed. |
| 862 | VarDecl *VD = cast<VarDecl>(*DclIt); |
| 863 | if (VD->isThisDeclarationADefinition()) { |
| 864 | if (VD->isStaticLocal()) { |
| 865 | SemaRef.Diag(VD->getLocation(), |
| 866 | diag::err_constexpr_local_var_static) |
| 867 | << isa<CXXConstructorDecl>(Dcl) |
| 868 | << (VD->getTLSKind() == VarDecl::TLS_Dynamic); |
| 869 | return false; |
| 870 | } |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 871 | if (!VD->getType()->isDependentType() && |
| 872 | SemaRef.RequireLiteralType( |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 873 | VD->getLocation(), VD->getType(), |
| 874 | diag::err_constexpr_local_var_non_literal_type, |
| 875 | isa<CXXConstructorDecl>(Dcl))) |
| 876 | return false; |
| 877 | if (!VD->hasInit()) { |
| 878 | SemaRef.Diag(VD->getLocation(), |
| 879 | diag::err_constexpr_local_var_no_init) |
| 880 | << isa<CXXConstructorDecl>(Dcl); |
| 881 | return false; |
| 882 | } |
| 883 | } |
| 884 | SemaRef.Diag(VD->getLocation(), |
| 885 | SemaRef.getLangOpts().CPlusPlus1y |
| 886 | ? diag::warn_cxx11_compat_constexpr_local_var |
| 887 | : diag::ext_constexpr_local_var) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 888 | << isa<CXXConstructorDecl>(Dcl); |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 889 | continue; |
| 890 | } |
| 891 | |
| 892 | case Decl::NamespaceAlias: |
| 893 | case Decl::Function: |
| 894 | // These are disallowed in C++11 and permitted in C++1y. Allow them |
| 895 | // everywhere as an extension. |
| 896 | if (!Cxx1yLoc.isValid()) |
| 897 | Cxx1yLoc = DS->getLocStart(); |
| 898 | continue; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 899 | |
| 900 | default: |
| 901 | SemaRef.Diag(DS->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 902 | << isa<CXXConstructorDecl>(Dcl); |
| 903 | return false; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | return true; |
| 908 | } |
| 909 | |
| 910 | /// Check that the given field is initialized within a constexpr constructor. |
| 911 | /// |
| 912 | /// \param Dcl The constexpr constructor being checked. |
| 913 | /// \param Field The field being checked. This may be a member of an anonymous |
| 914 | /// struct or union nested within the class being checked. |
| 915 | /// \param Inits All declarations, including anonymous struct/union members and |
| 916 | /// indirect members, for which any initialization was provided. |
| 917 | /// \param Diagnosed Set to true if an error is produced. |
| 918 | static void CheckConstexprCtorInitializer(Sema &SemaRef, |
| 919 | const FunctionDecl *Dcl, |
| 920 | FieldDecl *Field, |
| 921 | llvm::SmallSet<Decl*, 16> &Inits, |
| 922 | bool &Diagnosed) { |
Eli Friedman | 5fb478b | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 923 | if (Field->isInvalidDecl()) |
| 924 | return; |
| 925 | |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 926 | if (Field->isUnnamedBitfield()) |
| 927 | return; |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 928 | |
| 929 | if (Field->isAnonymousStructOrUnion() && |
| 930 | Field->getType()->getAsCXXRecordDecl()->isEmpty()) |
| 931 | return; |
| 932 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 933 | if (!Inits.count(Field)) { |
| 934 | if (!Diagnosed) { |
| 935 | SemaRef.Diag(Dcl->getLocation(), diag::err_constexpr_ctor_missing_init); |
| 936 | Diagnosed = true; |
| 937 | } |
| 938 | SemaRef.Diag(Field->getLocation(), diag::note_constexpr_ctor_missing_init); |
| 939 | } else if (Field->isAnonymousStructOrUnion()) { |
| 940 | const RecordDecl *RD = Field->getType()->castAs<RecordType>()->getDecl(); |
| 941 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 942 | I != E; ++I) |
| 943 | // If an anonymous union contains an anonymous struct of which any member |
| 944 | // is initialized, all members must be initialized. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 945 | if (!RD->isUnion() || Inits.count(*I)) |
| 946 | CheckConstexprCtorInitializer(SemaRef, Dcl, *I, Inits, Diagnosed); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 947 | } |
| 948 | } |
| 949 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 950 | /// Check the provided statement is allowed in a constexpr function |
| 951 | /// definition. |
| 952 | static bool |
| 953 | CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S, |
| 954 | llvm::SmallVectorImpl<SourceLocation> &ReturnStmts, |
| 955 | SourceLocation &Cxx1yLoc) { |
| 956 | // - its function-body shall be [...] a compound-statement that contains only |
| 957 | switch (S->getStmtClass()) { |
| 958 | case Stmt::NullStmtClass: |
| 959 | // - null statements, |
| 960 | return true; |
| 961 | |
| 962 | case Stmt::DeclStmtClass: |
| 963 | // - static_assert-declarations |
| 964 | // - using-declarations, |
| 965 | // - using-directives, |
| 966 | // - typedef declarations and alias-declarations that do not define |
| 967 | // classes or enumerations, |
| 968 | if (!CheckConstexprDeclStmt(SemaRef, Dcl, cast<DeclStmt>(S), Cxx1yLoc)) |
| 969 | return false; |
| 970 | return true; |
| 971 | |
| 972 | case Stmt::ReturnStmtClass: |
| 973 | // - and exactly one return statement; |
| 974 | if (isa<CXXConstructorDecl>(Dcl)) { |
| 975 | // C++1y allows return statements in constexpr constructors. |
| 976 | if (!Cxx1yLoc.isValid()) |
| 977 | Cxx1yLoc = S->getLocStart(); |
| 978 | return true; |
| 979 | } |
| 980 | |
| 981 | ReturnStmts.push_back(S->getLocStart()); |
| 982 | return true; |
| 983 | |
| 984 | case Stmt::CompoundStmtClass: { |
| 985 | // C++1y allows compound-statements. |
| 986 | if (!Cxx1yLoc.isValid()) |
| 987 | Cxx1yLoc = S->getLocStart(); |
| 988 | |
| 989 | CompoundStmt *CompStmt = cast<CompoundStmt>(S); |
| 990 | for (CompoundStmt::body_iterator BodyIt = CompStmt->body_begin(), |
| 991 | BodyEnd = CompStmt->body_end(); BodyIt != BodyEnd; ++BodyIt) { |
| 992 | if (!CheckConstexprFunctionStmt(SemaRef, Dcl, *BodyIt, ReturnStmts, |
| 993 | Cxx1yLoc)) |
| 994 | return false; |
| 995 | } |
| 996 | return true; |
| 997 | } |
| 998 | |
| 999 | case Stmt::AttributedStmtClass: |
| 1000 | if (!Cxx1yLoc.isValid()) |
| 1001 | Cxx1yLoc = S->getLocStart(); |
| 1002 | return true; |
| 1003 | |
| 1004 | case Stmt::IfStmtClass: { |
| 1005 | // C++1y allows if-statements. |
| 1006 | if (!Cxx1yLoc.isValid()) |
| 1007 | Cxx1yLoc = S->getLocStart(); |
| 1008 | |
| 1009 | IfStmt *If = cast<IfStmt>(S); |
| 1010 | if (!CheckConstexprFunctionStmt(SemaRef, Dcl, If->getThen(), ReturnStmts, |
| 1011 | Cxx1yLoc)) |
| 1012 | return false; |
| 1013 | if (If->getElse() && |
| 1014 | !CheckConstexprFunctionStmt(SemaRef, Dcl, If->getElse(), ReturnStmts, |
| 1015 | Cxx1yLoc)) |
| 1016 | return false; |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
| 1020 | case Stmt::WhileStmtClass: |
| 1021 | case Stmt::DoStmtClass: |
| 1022 | case Stmt::ForStmtClass: |
| 1023 | case Stmt::CXXForRangeStmtClass: |
| 1024 | case Stmt::ContinueStmtClass: |
| 1025 | // C++1y allows all of these. We don't allow them as extensions in C++11, |
| 1026 | // because they don't make sense without variable mutation. |
| 1027 | if (!SemaRef.getLangOpts().CPlusPlus1y) |
| 1028 | break; |
| 1029 | if (!Cxx1yLoc.isValid()) |
| 1030 | Cxx1yLoc = S->getLocStart(); |
| 1031 | for (Stmt::child_range Children = S->children(); Children; ++Children) |
| 1032 | if (*Children && |
| 1033 | !CheckConstexprFunctionStmt(SemaRef, Dcl, *Children, ReturnStmts, |
| 1034 | Cxx1yLoc)) |
| 1035 | return false; |
| 1036 | return true; |
| 1037 | |
| 1038 | case Stmt::SwitchStmtClass: |
| 1039 | case Stmt::CaseStmtClass: |
| 1040 | case Stmt::DefaultStmtClass: |
| 1041 | case Stmt::BreakStmtClass: |
| 1042 | // C++1y allows switch-statements, and since they don't need variable |
| 1043 | // mutation, we can reasonably allow them in C++11 as an extension. |
| 1044 | if (!Cxx1yLoc.isValid()) |
| 1045 | Cxx1yLoc = S->getLocStart(); |
| 1046 | for (Stmt::child_range Children = S->children(); Children; ++Children) |
| 1047 | if (*Children && |
| 1048 | !CheckConstexprFunctionStmt(SemaRef, Dcl, *Children, ReturnStmts, |
| 1049 | Cxx1yLoc)) |
| 1050 | return false; |
| 1051 | return true; |
| 1052 | |
| 1053 | default: |
| 1054 | if (!isa<Expr>(S)) |
| 1055 | break; |
| 1056 | |
| 1057 | // C++1y allows expression-statements. |
| 1058 | if (!Cxx1yLoc.isValid()) |
| 1059 | Cxx1yLoc = S->getLocStart(); |
| 1060 | return true; |
| 1061 | } |
| 1062 | |
| 1063 | SemaRef.Diag(S->getLocStart(), diag::err_constexpr_body_invalid_stmt) |
| 1064 | << isa<CXXConstructorDecl>(Dcl); |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1068 | /// Check the body for the given constexpr function declaration only contains |
| 1069 | /// the permitted types of statement. C++11 [dcl.constexpr]p3,p4. |
| 1070 | /// |
| 1071 | /// \return true if the body is OK, false if we have diagnosed a problem. |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1072 | bool Sema::CheckConstexprFunctionBody(const FunctionDecl *Dcl, Stmt *Body) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1073 | if (isa<CXXTryStmt>(Body)) { |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 1074 | // C++11 [dcl.constexpr]p3: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1075 | // The definition of a constexpr function shall satisfy the following |
| 1076 | // constraints: [...] |
| 1077 | // - its function-body shall be = delete, = default, or a |
| 1078 | // compound-statement |
| 1079 | // |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 1080 | // C++11 [dcl.constexpr]p4: |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1081 | // In the definition of a constexpr constructor, [...] |
| 1082 | // - its function-body shall not be a function-try-block; |
| 1083 | Diag(Body->getLocStart(), diag::err_constexpr_function_try_block) |
| 1084 | << isa<CXXConstructorDecl>(Dcl); |
| 1085 | return false; |
| 1086 | } |
| 1087 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1088 | SmallVector<SourceLocation, 4> ReturnStmts; |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1089 | |
| 1090 | // - its function-body shall be [...] a compound-statement that contains only |
| 1091 | // [... list of cases ...] |
| 1092 | CompoundStmt *CompBody = cast<CompoundStmt>(Body); |
| 1093 | SourceLocation Cxx1yLoc; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1094 | for (CompoundStmt::body_iterator BodyIt = CompBody->body_begin(), |
| 1095 | BodyEnd = CompBody->body_end(); BodyIt != BodyEnd; ++BodyIt) { |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1096 | if (!CheckConstexprFunctionStmt(*this, Dcl, *BodyIt, ReturnStmts, Cxx1yLoc)) |
| 1097 | return false; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1100 | if (Cxx1yLoc.isValid()) |
| 1101 | Diag(Cxx1yLoc, |
| 1102 | getLangOpts().CPlusPlus1y |
| 1103 | ? diag::warn_cxx11_compat_constexpr_body_invalid_stmt |
| 1104 | : diag::ext_constexpr_body_invalid_stmt) |
| 1105 | << isa<CXXConstructorDecl>(Dcl); |
| 1106 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1107 | if (const CXXConstructorDecl *Constructor |
| 1108 | = dyn_cast<CXXConstructorDecl>(Dcl)) { |
| 1109 | const CXXRecordDecl *RD = Constructor->getParent(); |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 1110 | // DR1359: |
| 1111 | // - every non-variant non-static data member and base class sub-object |
| 1112 | // shall be initialized; |
| 1113 | // - if the class is a non-empty union, or for each non-empty anonymous |
| 1114 | // union member of a non-union class, exactly one non-static data member |
| 1115 | // shall be initialized; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1116 | if (RD->isUnion()) { |
Richard Smith | 30ecfad | 2012-02-09 06:40:58 +0000 | [diff] [blame] | 1117 | if (Constructor->getNumCtorInitializers() == 0 && !RD->isEmpty()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1118 | Diag(Dcl->getLocation(), diag::err_constexpr_union_ctor_no_init); |
| 1119 | return false; |
| 1120 | } |
Richard Smith | 6e43375 | 2011-10-10 16:38:04 +0000 | [diff] [blame] | 1121 | } else if (!Constructor->isDependentContext() && |
| 1122 | !Constructor->isDelegatingConstructor()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1123 | assert(RD->getNumVBases() == 0 && "constexpr ctor with virtual bases"); |
| 1124 | |
| 1125 | // Skip detailed checking if we have enough initializers, and we would |
| 1126 | // allow at most one initializer per member. |
| 1127 | bool AnyAnonStructUnionMembers = false; |
| 1128 | unsigned Fields = 0; |
| 1129 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 1130 | E = RD->field_end(); I != E; ++I, ++Fields) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1131 | if (I->isAnonymousStructOrUnion()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1132 | AnyAnonStructUnionMembers = true; |
| 1133 | break; |
| 1134 | } |
| 1135 | } |
| 1136 | if (AnyAnonStructUnionMembers || |
| 1137 | Constructor->getNumCtorInitializers() != RD->getNumBases() + Fields) { |
| 1138 | // Check initialization of non-static data members. Base classes are |
| 1139 | // always initialized so do not need to be checked. Dependent bases |
| 1140 | // might not have initializers in the member initializer list. |
| 1141 | llvm::SmallSet<Decl*, 16> Inits; |
| 1142 | for (CXXConstructorDecl::init_const_iterator |
| 1143 | I = Constructor->init_begin(), E = Constructor->init_end(); |
| 1144 | I != E; ++I) { |
| 1145 | if (FieldDecl *FD = (*I)->getMember()) |
| 1146 | Inits.insert(FD); |
| 1147 | else if (IndirectFieldDecl *ID = (*I)->getIndirectMember()) |
| 1148 | Inits.insert(ID->chain_begin(), ID->chain_end()); |
| 1149 | } |
| 1150 | |
| 1151 | bool Diagnosed = false; |
| 1152 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 1153 | E = RD->field_end(); I != E; ++I) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1154 | CheckConstexprCtorInitializer(*this, Dcl, *I, Inits, Diagnosed); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1155 | if (Diagnosed) |
| 1156 | return false; |
| 1157 | } |
| 1158 | } |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1159 | } else { |
| 1160 | if (ReturnStmts.empty()) { |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1161 | // C++1y doesn't require constexpr functions to contain a 'return' |
| 1162 | // statement. We still do, unless the return type is void, because |
| 1163 | // otherwise if there's no return statement, the function cannot |
| 1164 | // be used in a core constant expression. |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1165 | bool OK = getLangOpts().CPlusPlus1y && Dcl->getResultType()->isVoidType(); |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1166 | Diag(Dcl->getLocation(), |
Richard Smith | bebf5b1 | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1167 | OK ? diag::warn_cxx11_compat_constexpr_body_no_return |
| 1168 | : diag::err_constexpr_body_no_return); |
| 1169 | return OK; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1170 | } |
| 1171 | if (ReturnStmts.size() > 1) { |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1172 | Diag(ReturnStmts.back(), |
| 1173 | getLangOpts().CPlusPlus1y |
| 1174 | ? diag::warn_cxx11_compat_constexpr_body_multiple_return |
| 1175 | : diag::ext_constexpr_body_multiple_return); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1176 | for (unsigned I = 0; I < ReturnStmts.size() - 1; ++I) |
| 1177 | Diag(ReturnStmts[I], diag::note_constexpr_body_previous_return); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 1181 | // C++11 [dcl.constexpr]p5: |
| 1182 | // if no function argument values exist such that the function invocation |
| 1183 | // substitution would produce a constant expression, the program is |
| 1184 | // ill-formed; no diagnostic required. |
| 1185 | // C++11 [dcl.constexpr]p3: |
| 1186 | // - every constructor call and implicit conversion used in initializing the |
| 1187 | // return value shall be one of those allowed in a constant expression. |
| 1188 | // C++11 [dcl.constexpr]p4: |
| 1189 | // - every constructor involved in initializing non-static data members and |
| 1190 | // base class sub-objects shall be a constexpr constructor. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1191 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1192 | if (!Expr::isPotentialConstantExpr(Dcl, Diags)) { |
Richard Smith | afee0ff | 2012-12-09 05:55:43 +0000 | [diff] [blame] | 1193 | Diag(Dcl->getLocation(), diag::ext_constexpr_function_never_constant_expr) |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1194 | << isa<CXXConstructorDecl>(Dcl); |
| 1195 | for (size_t I = 0, N = Diags.size(); I != N; ++I) |
| 1196 | Diag(Diags[I].first, Diags[I].second); |
Richard Smith | afee0ff | 2012-12-09 05:55:43 +0000 | [diff] [blame] | 1197 | // Don't return false here: we allow this for compatibility in |
| 1198 | // system headers. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1201 | return true; |
| 1202 | } |
| 1203 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1204 | /// isCurrentClassName - Determine whether the identifier II is the |
| 1205 | /// name of the class type currently being defined. In the case of |
| 1206 | /// nested classes, this will only return true if II is the name of |
| 1207 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1208 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 1209 | const CXXScopeSpec *SS) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1210 | assert(getLangOpts().CPlusPlus && "No class names in C!"); |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 1211 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1212 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1213 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 1214 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 1215 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 1216 | } else |
| 1217 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 1218 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 1219 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1220 | return &II == CurDecl->getIdentifier(); |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 1221 | return false; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Douglas Gregor | 229d47a | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1224 | /// \brief Determine whether the given class is a base class of the given |
| 1225 | /// class, including looking at dependent bases. |
| 1226 | static bool findCircularInheritance(const CXXRecordDecl *Class, |
| 1227 | const CXXRecordDecl *Current) { |
| 1228 | SmallVector<const CXXRecordDecl*, 8> Queue; |
| 1229 | |
| 1230 | Class = Class->getCanonicalDecl(); |
| 1231 | while (true) { |
| 1232 | for (CXXRecordDecl::base_class_const_iterator I = Current->bases_begin(), |
| 1233 | E = Current->bases_end(); |
| 1234 | I != E; ++I) { |
| 1235 | CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 1236 | if (!Base) |
| 1237 | continue; |
| 1238 | |
| 1239 | Base = Base->getDefinition(); |
| 1240 | if (!Base) |
| 1241 | continue; |
| 1242 | |
| 1243 | if (Base->getCanonicalDecl() == Class) |
| 1244 | return true; |
| 1245 | |
| 1246 | Queue.push_back(Base); |
| 1247 | } |
| 1248 | |
| 1249 | if (Queue.empty()) |
| 1250 | return false; |
| 1251 | |
| 1252 | Current = Queue.back(); |
| 1253 | Queue.pop_back(); |
| 1254 | } |
| 1255 | |
| 1256 | return false; |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1260 | /// |
| 1261 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 1262 | /// and returns NULL otherwise. |
| 1263 | CXXBaseSpecifier * |
| 1264 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 1265 | SourceRange SpecifierRange, |
| 1266 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1267 | TypeSourceInfo *TInfo, |
| 1268 | SourceLocation EllipsisLoc) { |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1269 | QualType BaseType = TInfo->getType(); |
| 1270 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1271 | // C++ [class.union]p1: |
| 1272 | // A union shall not have base classes. |
| 1273 | if (Class->isUnion()) { |
| 1274 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 1275 | << SpecifierRange; |
| 1276 | return 0; |
| 1277 | } |
| 1278 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1279 | if (EllipsisLoc.isValid() && |
| 1280 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 1281 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1282 | << TInfo->getTypeLoc().getSourceRange(); |
| 1283 | EllipsisLoc = SourceLocation(); |
| 1284 | } |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1285 | |
| 1286 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
| 1287 | |
| 1288 | if (BaseType->isDependentType()) { |
| 1289 | // Make sure that we don't have circular inheritance among our dependent |
| 1290 | // bases. For non-dependent bases, the check for completeness below handles |
| 1291 | // this. |
| 1292 | if (CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl()) { |
| 1293 | if (BaseDecl->getCanonicalDecl() == Class->getCanonicalDecl() || |
| 1294 | ((BaseDecl = BaseDecl->getDefinition()) && |
Douglas Gregor | 229d47a | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1295 | findCircularInheritance(Class, BaseDecl))) { |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1296 | Diag(BaseLoc, diag::err_circular_inheritance) |
| 1297 | << BaseType << Context.getTypeDeclType(Class); |
| 1298 | |
| 1299 | if (BaseDecl->getCanonicalDecl() != Class->getCanonicalDecl()) |
| 1300 | Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 1301 | << BaseType; |
| 1302 | |
| 1303 | return 0; |
| 1304 | } |
| 1305 | } |
| 1306 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1308 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1309 | Access, TInfo, EllipsisLoc); |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1310 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1311 | |
| 1312 | // Base specifiers must be record types. |
| 1313 | if (!BaseType->isRecordType()) { |
| 1314 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | // C++ [class.union]p1: |
| 1319 | // A union shall not be used as a base class. |
| 1320 | if (BaseType->isUnionType()) { |
| 1321 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
| 1325 | // C++ [class.derived]p2: |
| 1326 | // The class-name in a base-specifier shall not be an incompletely |
| 1327 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | if (RequireCompleteType(BaseLoc, BaseType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1329 | diag::err_incomplete_base_class, SpecifierRange)) { |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1330 | Class->setInvalidDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1331 | return 0; |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1332 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1333 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1334 | // 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] | 1335 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1336 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1337 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1338 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
David Majnemer | 2f68669 | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 1339 | CXXRecordDecl *CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1340 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 1341 | |
Anders Carlsson | 1d20927 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 1342 | // C++ [class]p3: |
| 1343 | // If a class is marked final and it appears as a base-type-specifier in |
| 1344 | // base-clause, the program is ill-formed. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1345 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 1346 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
| 1347 | << CXXBaseDecl->getDeclName(); |
| 1348 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 1349 | << CXXBaseDecl->getDeclName(); |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1353 | if (BaseDecl->isInvalidDecl()) |
| 1354 | Class->setInvalidDecl(); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Create the base specifier. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1357 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1358 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1359 | Access, TInfo, EllipsisLoc); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1362 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 1363 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | /// example: |
| 1365 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1366 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1367 | BaseResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1368 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1369 | ParsedAttributes &Attributes, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1370 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1371 | ParsedType basetype, SourceLocation BaseLoc, |
| 1372 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1373 | if (!classdecl) |
| 1374 | return true; |
| 1375 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1376 | AdjustDeclIfTemplate(classdecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1377 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 1378 | if (!Class) |
| 1379 | return true; |
| 1380 | |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1381 | // We do not support any C++11 attributes on base-specifiers yet. |
| 1382 | // Diagnose any attributes we see. |
| 1383 | if (!Attributes.empty()) { |
| 1384 | for (AttributeList *Attr = Attributes.getList(); Attr; |
| 1385 | Attr = Attr->getNext()) { |
| 1386 | if (Attr->isInvalid() || |
| 1387 | Attr->getKind() == AttributeList::IgnoredAttribute) |
| 1388 | continue; |
| 1389 | Diag(Attr->getLoc(), |
| 1390 | Attr->getKind() == AttributeList::UnknownAttribute |
| 1391 | ? diag::warn_unknown_attribute_ignored |
| 1392 | : diag::err_base_specifier_attribute) |
| 1393 | << Attr->getName(); |
| 1394 | } |
| 1395 | } |
| 1396 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1397 | TypeSourceInfo *TInfo = 0; |
| 1398 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1399 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1400 | if (EllipsisLoc.isInvalid() && |
| 1401 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1402 | UPPC_BaseType)) |
| 1403 | return true; |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1404 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1405 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1406 | Virtual, Access, TInfo, |
| 1407 | EllipsisLoc)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1408 | return BaseSpec; |
Douglas Gregor | 8a50fe0 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 1409 | else |
| 1410 | Class->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1412 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1413 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1414 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1415 | /// \brief Performs the actual work of attaching the given base class |
| 1416 | /// specifiers to a C++ class. |
| 1417 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 1418 | unsigned NumBases) { |
| 1419 | if (NumBases == 0) |
| 1420 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1421 | |
| 1422 | // Used to keep track of which base types we have already seen, so |
| 1423 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1424 | // that the key is always the unqualified canonical type of the base |
| 1425 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1426 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 1427 | |
| 1428 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1429 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1430 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1431 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1433 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1434 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1435 | |
| 1436 | CXXBaseSpecifier *&KnownBase = KnownBaseTypes[NewBaseType]; |
| 1437 | if (KnownBase) { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1438 | // C++ [class.mi]p3: |
| 1439 | // A class shall not be specified as a direct base class of a |
| 1440 | // derived class more than once. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1441 | Diag(Bases[idx]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1442 | diag::err_duplicate_base_class) |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1443 | << KnownBase->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1444 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1445 | |
| 1446 | // Delete the duplicate base class specifier; we're going to |
| 1447 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1448 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1449 | |
| 1450 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1451 | } else { |
| 1452 | // Okay, add this new base class. |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1453 | KnownBase = Bases[idx]; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1454 | Bases[NumGoodBases++] = Bases[idx]; |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1455 | if (const RecordType *Record = NewBaseType->getAs<RecordType>()) { |
| 1456 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
| 1457 | if (Class->isInterface() && |
| 1458 | (!RD->isInterface() || |
| 1459 | KnownBase->getAccessSpecifier() != AS_public)) { |
| 1460 | // The Microsoft extension __interface does not permit bases that |
| 1461 | // are not themselves public interfaces. |
| 1462 | Diag(KnownBase->getLocStart(), diag::err_invalid_base_in_interface) |
| 1463 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getName() |
| 1464 | << RD->getSourceRange(); |
| 1465 | Invalid = true; |
| 1466 | } |
| 1467 | if (RD->hasAttr<WeakAttr>()) |
| 1468 | Class->addAttr(::new (Context) WeakAttr(SourceRange(), Context)); |
| 1469 | } |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 1474 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1475 | |
| 1476 | // Delete the remaining (good) base class specifiers, since their |
| 1477 | // data has been copied into the CXXRecordDecl. |
| 1478 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1479 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1480 | |
| 1481 | return Invalid; |
| 1482 | } |
| 1483 | |
| 1484 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 1485 | /// class, after checking whether there are any duplicate base |
| 1486 | /// classes. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1487 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, CXXBaseSpecifier **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1488 | unsigned NumBases) { |
| 1489 | if (!ClassDecl || !Bases || !NumBases) |
| 1490 | return; |
| 1491 | |
| 1492 | AdjustDeclIfTemplate(ClassDecl); |
Robert Wilhelm | 0d317a0 | 2013-07-22 05:04:01 +0000 | [diff] [blame] | 1493 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), Bases, NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1494 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1495 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1496 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1497 | /// derived from the type \p Base. |
| 1498 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1499 | if (!getLangOpts().CPlusPlus) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1500 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1501 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1502 | CXXRecordDecl *DerivedRD = Derived->getAsCXXRecordDecl(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1503 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1504 | return false; |
| 1505 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1506 | CXXRecordDecl *BaseRD = Base->getAsCXXRecordDecl(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1507 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1508 | return false; |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1509 | |
| 1510 | // If either the base or the derived type is invalid, don't try to |
| 1511 | // check whether one is derived from the other. |
| 1512 | if (BaseRD->isInvalidDecl() || DerivedRD->isInvalidDecl()) |
| 1513 | return false; |
| 1514 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1515 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 1516 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1520 | /// derived from the type \p Base. |
| 1521 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1522 | if (!getLangOpts().CPlusPlus) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1523 | return false; |
| 1524 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1525 | CXXRecordDecl *DerivedRD = Derived->getAsCXXRecordDecl(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1526 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1527 | return false; |
| 1528 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1529 | CXXRecordDecl *BaseRD = Base->getAsCXXRecordDecl(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1530 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1531 | return false; |
| 1532 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1533 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 1534 | } |
| 1535 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1536 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1537 | CXXCastPath &BasePathArray) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1538 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 1539 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 1540 | |
| 1541 | const CXXBasePath &Path = Paths.front(); |
| 1542 | |
| 1543 | // We first go backward and check if we have a virtual base. |
| 1544 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 1545 | // the nearest virtual base. |
| 1546 | unsigned Start = 0; |
| 1547 | for (unsigned I = Path.size(); I != 0; --I) { |
| 1548 | if (Path[I - 1].Base->isVirtual()) { |
| 1549 | Start = I - 1; |
| 1550 | break; |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | // Now add all bases. |
| 1555 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1556 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1559 | /// \brief Determine whether the given base path includes a virtual |
| 1560 | /// base class. |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1561 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 1562 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 1563 | BEnd = BasePath.end(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1564 | B != BEnd; ++B) |
| 1565 | if ((*B)->isVirtual()) |
| 1566 | return true; |
| 1567 | |
| 1568 | return false; |
| 1569 | } |
| 1570 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1571 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 1572 | /// conversion (where Derived and Base are class types) is |
| 1573 | /// well-formed, meaning that the conversion is unambiguous (and |
| 1574 | /// that all of the base classes are accessible). Returns true |
| 1575 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 1576 | /// otherwise. Loc is the location where this routine should point to |
| 1577 | /// if there is an error, and Range is the source range to highlight |
| 1578 | /// if there is an error. |
| 1579 | bool |
| 1580 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1581 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1582 | unsigned AmbigiousBaseConvID, |
| 1583 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1584 | DeclarationName Name, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1585 | CXXCastPath *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1586 | // First, determine whether the path from Derived to Base is |
| 1587 | // ambiguous. This is slightly more expensive than checking whether |
| 1588 | // the Derived to Base conversion exists, because here we need to |
| 1589 | // explore multiple paths to determine if there is an ambiguity. |
| 1590 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1591 | /*DetectVirtual=*/false); |
| 1592 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1593 | assert(DerivationOkay && |
| 1594 | "Can only be used with a derived-to-base conversion"); |
| 1595 | (void)DerivationOkay; |
| 1596 | |
| 1597 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1598 | if (InaccessibleBaseID) { |
| 1599 | // Check that the base class can be accessed. |
| 1600 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 1601 | InaccessibleBaseID)) { |
| 1602 | case AR_inaccessible: |
| 1603 | return true; |
| 1604 | case AR_accessible: |
| 1605 | case AR_dependent: |
| 1606 | case AR_delayed: |
| 1607 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1608 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1609 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1610 | |
| 1611 | // Build a base path if necessary. |
| 1612 | if (BasePath) |
| 1613 | BuildBasePathArray(Paths, *BasePath); |
| 1614 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
David Majnemer | 2f68669 | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 1617 | if (AmbigiousBaseConvID) { |
| 1618 | // We know that the derived-to-base conversion is ambiguous, and |
| 1619 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 1620 | // search just one more time to compute all of the possible paths so |
| 1621 | // that we can print them out. This is more expensive than any of |
| 1622 | // the previous derived-to-base checks we've done, but at this point |
| 1623 | // performance isn't as much of an issue. |
| 1624 | Paths.clear(); |
| 1625 | Paths.setRecordingPaths(true); |
| 1626 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1627 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 1628 | (void)StillOkay; |
| 1629 | |
| 1630 | // Build up a textual representation of the ambiguous paths, e.g., |
| 1631 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 1632 | // conversions in the diagnostic. We only print one of the paths |
| 1633 | // to each base class subobject. |
| 1634 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1635 | |
| 1636 | Diag(Loc, AmbigiousBaseConvID) |
| 1637 | << Derived << Base << PathDisplayStr << Range << Name; |
| 1638 | } |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1639 | return true; |
| 1640 | } |
| 1641 | |
| 1642 | bool |
| 1643 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1644 | SourceLocation Loc, SourceRange Range, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1645 | CXXCastPath *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1646 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1647 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1648 | IgnoreAccess ? 0 |
| 1649 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1650 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1651 | Loc, Range, DeclarationName(), |
| 1652 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | |
| 1656 | /// @brief Builds a string representing ambiguous paths from a |
| 1657 | /// specific derived class to different subobjects of the same base |
| 1658 | /// class. |
| 1659 | /// |
| 1660 | /// This function builds a string that can be used in error messages |
| 1661 | /// to show the different paths that one can take through the |
| 1662 | /// inheritance hierarchy to go from the derived class to different |
| 1663 | /// subobjects of a base class. The result looks something like this: |
| 1664 | /// @code |
| 1665 | /// struct D -> struct B -> struct A |
| 1666 | /// struct D -> struct C -> struct A |
| 1667 | /// @endcode |
| 1668 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 1669 | std::string PathDisplayStr; |
| 1670 | std::set<unsigned> DisplayedPaths; |
| 1671 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1672 | Path != Paths.end(); ++Path) { |
| 1673 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 1674 | // We haven't displayed a path to this particular base |
| 1675 | // class subobject yet. |
| 1676 | PathDisplayStr += "\n "; |
| 1677 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 1678 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 1679 | Element != Path->end(); ++Element) |
| 1680 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | return PathDisplayStr; |
| 1685 | } |
| 1686 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1687 | //===----------------------------------------------------------------------===// |
| 1688 | // C++ class member Handling |
| 1689 | //===----------------------------------------------------------------------===// |
| 1690 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1691 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1692 | bool Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 1693 | SourceLocation ASLoc, |
| 1694 | SourceLocation ColonLoc, |
| 1695 | AttributeList *Attrs) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1696 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1697 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1698 | ASLoc, ColonLoc); |
| 1699 | CurContext->addHiddenDecl(ASDecl); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1700 | return ProcessAccessDeclAttributeList(ASDecl, Attrs); |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1703 | /// CheckOverrideControl - Check C++11 override control semantics. |
| 1704 | void Sema::CheckOverrideControl(Decl *D) { |
Richard Smith | cddbc1d | 2012-09-06 18:32:18 +0000 | [diff] [blame] | 1705 | if (D->isInvalidDecl()) |
| 1706 | return; |
| 1707 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1708 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1709 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1710 | // Do we know which functions this declaration might be overriding? |
| 1711 | bool OverridesAreKnown = !MD || |
| 1712 | (!MD->getParent()->hasAnyDependentBases() && |
| 1713 | !MD->getType()->isDependentType()); |
Anders Carlsson | 3ffe183 | 2011-01-20 06:33:26 +0000 | [diff] [blame] | 1714 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1715 | if (!MD || !MD->isVirtual()) { |
| 1716 | if (OverridesAreKnown) { |
| 1717 | if (OverrideAttr *OA = D->getAttr<OverrideAttr>()) { |
| 1718 | Diag(OA->getLocation(), |
| 1719 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1720 | << "override" << FixItHint::CreateRemoval(OA->getLocation()); |
| 1721 | D->dropAttr<OverrideAttr>(); |
| 1722 | } |
| 1723 | if (FinalAttr *FA = D->getAttr<FinalAttr>()) { |
| 1724 | Diag(FA->getLocation(), |
| 1725 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1726 | << "final" << FixItHint::CreateRemoval(FA->getLocation()); |
| 1727 | D->dropAttr<FinalAttr>(); |
| 1728 | } |
| 1729 | } |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1730 | return; |
| 1731 | } |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1732 | |
| 1733 | if (!OverridesAreKnown) |
| 1734 | return; |
| 1735 | |
| 1736 | // C++11 [class.virtual]p5: |
| 1737 | // If a virtual function is marked with the virt-specifier override and |
| 1738 | // does not override a member function of a base class, the program is |
| 1739 | // ill-formed. |
| 1740 | bool HasOverriddenMethods = |
| 1741 | MD->begin_overridden_methods() != MD->end_overridden_methods(); |
| 1742 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) |
| 1743 | Diag(MD->getLocation(), diag::err_function_marked_override_not_overriding) |
| 1744 | << MD->getDeclName(); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1747 | /// CheckIfOverriddenFunctionIsMarkedFinal - Checks whether a virtual member |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1748 | /// function overrides a virtual member function marked 'final', according to |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1749 | /// C++11 [class.virtual]p4. |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1750 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 1751 | const CXXMethodDecl *Old) { |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1752 | if (!Old->hasAttr<FinalAttr>()) |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1753 | return false; |
| 1754 | |
| 1755 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 1756 | << New->getDeclName(); |
| 1757 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 1758 | return true; |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1761 | static bool InitializationHasSideEffects(const FieldDecl &FD) { |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 1762 | const Type *T = FD.getType()->getBaseElementTypeUnsafe(); |
| 1763 | // FIXME: Destruction of ObjC lifetime types has side-effects. |
| 1764 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 1765 | return !RD->isCompleteDefinition() || |
| 1766 | !RD->hasTrivialDefaultConstructor() || |
| 1767 | !RD->hasTrivialDestructor(); |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1768 | return false; |
| 1769 | } |
| 1770 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1771 | static AttributeList *getMSPropertyAttr(AttributeList *list) { |
| 1772 | for (AttributeList* it = list; it != 0; it = it->getNext()) |
| 1773 | if (it->isDeclspecPropertyAttribute()) |
| 1774 | return it; |
| 1775 | return 0; |
| 1776 | } |
| 1777 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1778 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 1779 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1780 | /// bitfield width if there is one, 'InitExpr' specifies the initializer if |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1781 | /// one has been parsed, and 'InitStyle' is set if an in-class initializer is |
| 1782 | /// present (but parsing it has been deferred). |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 1783 | NamedDecl * |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1784 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1785 | MultiTemplateParamsArg TemplateParameterLists, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 1786 | Expr *BW, const VirtSpecifiers &VS, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1787 | InClassInitStyle InitStyle) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1788 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1789 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 1790 | DeclarationName Name = NameInfo.getName(); |
| 1791 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1792 | |
| 1793 | // For anonymous bitfields, the location should point to the type. |
| 1794 | if (Loc.isInvalid()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1795 | Loc = D.getLocStart(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1796 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1797 | Expr *BitWidth = static_cast<Expr*>(BW); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1798 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1799 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1800 | assert(!DS.isFriendSpecified()); |
| 1801 | |
Richard Smith | 1ab0d90 | 2011-06-25 02:28:38 +0000 | [diff] [blame] | 1802 | bool isFunc = D.isDeclarationOfFunction(); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1803 | |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1804 | if (cast<CXXRecordDecl>(CurContext)->isInterface()) { |
| 1805 | // The Microsoft extension __interface only permits public member functions |
| 1806 | // and prohibits constructors, destructors, operators, non-public member |
| 1807 | // functions, static methods and data members. |
| 1808 | unsigned InvalidDecl; |
| 1809 | bool ShowDeclName = true; |
| 1810 | if (!isFunc) |
| 1811 | InvalidDecl = (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) ? 0 : 1; |
| 1812 | else if (AS != AS_public) |
| 1813 | InvalidDecl = 2; |
| 1814 | else if (DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 1815 | InvalidDecl = 3; |
| 1816 | else switch (Name.getNameKind()) { |
| 1817 | case DeclarationName::CXXConstructorName: |
| 1818 | InvalidDecl = 4; |
| 1819 | ShowDeclName = false; |
| 1820 | break; |
| 1821 | |
| 1822 | case DeclarationName::CXXDestructorName: |
| 1823 | InvalidDecl = 5; |
| 1824 | ShowDeclName = false; |
| 1825 | break; |
| 1826 | |
| 1827 | case DeclarationName::CXXOperatorName: |
| 1828 | case DeclarationName::CXXConversionFunctionName: |
| 1829 | InvalidDecl = 6; |
| 1830 | break; |
| 1831 | |
| 1832 | default: |
| 1833 | InvalidDecl = 0; |
| 1834 | break; |
| 1835 | } |
| 1836 | |
| 1837 | if (InvalidDecl) { |
| 1838 | if (ShowDeclName) |
| 1839 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 1840 | << (InvalidDecl-1) << Name; |
| 1841 | else |
| 1842 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 1843 | << (InvalidDecl-1) << ""; |
| 1844 | return 0; |
| 1845 | } |
| 1846 | } |
| 1847 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1848 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 1849 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1850 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 1851 | // data members and cannot be applied to names declared const or static, |
| 1852 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1853 | switch (DS.getStorageClassSpec()) { |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1854 | case DeclSpec::SCS_unspecified: |
| 1855 | case DeclSpec::SCS_typedef: |
| 1856 | case DeclSpec::SCS_static: |
| 1857 | break; |
| 1858 | case DeclSpec::SCS_mutable: |
| 1859 | if (isFunc) { |
| 1860 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1862 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 1863 | // declarator. Otherwise we could get follow-up errors. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1864 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1865 | } |
| 1866 | break; |
| 1867 | default: |
| 1868 | Diag(DS.getStorageClassSpecLoc(), |
| 1869 | diag::err_storageclass_invalid_for_member); |
| 1870 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 1871 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1874 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 1875 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 1876 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1877 | |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1878 | if (DS.isConstexprSpecified() && isInstField) { |
| 1879 | SemaDiagnosticBuilder B = |
| 1880 | Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr_member); |
| 1881 | SourceLocation ConstexprLoc = DS.getConstexprSpecLoc(); |
| 1882 | if (InitStyle == ICIS_NoInit) { |
| 1883 | B << 0 << 0 << FixItHint::CreateReplacement(ConstexprLoc, "const"); |
| 1884 | D.getMutableDeclSpec().ClearConstexprSpec(); |
| 1885 | const char *PrevSpec; |
| 1886 | unsigned DiagID; |
| 1887 | bool Failed = D.getMutableDeclSpec().SetTypeQual(DeclSpec::TQ_const, ConstexprLoc, |
| 1888 | PrevSpec, DiagID, getLangOpts()); |
Matt Beaumont-Gay | 3e55e3e | 2013-01-31 00:08:03 +0000 | [diff] [blame] | 1889 | (void)Failed; |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1890 | assert(!Failed && "Making a constexpr member const shouldn't fail"); |
| 1891 | } else { |
| 1892 | B << 1; |
| 1893 | const char *PrevSpec; |
| 1894 | unsigned DiagID; |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1895 | if (D.getMutableDeclSpec().SetStorageClassSpec( |
| 1896 | *this, DeclSpec::SCS_static, ConstexprLoc, PrevSpec, DiagID)) { |
Matt Beaumont-Gay | 3e55e3e | 2013-01-31 00:08:03 +0000 | [diff] [blame] | 1897 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_mutable && |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1898 | "This is the only DeclSpec that should fail to be applied"); |
| 1899 | B << 1; |
| 1900 | } else { |
| 1901 | B << 0 << FixItHint::CreateInsertion(ConstexprLoc, "static "); |
| 1902 | isInstField = false; |
| 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 1907 | NamedDecl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1908 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1909 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 1910 | |
| 1911 | // Data members must have identifiers for names. |
Benjamin Kramer | c1aa40c | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1912 | if (!Name.isIdentifier()) { |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 1913 | Diag(Loc, diag::err_bad_variable_name) |
| 1914 | << Name; |
| 1915 | return 0; |
| 1916 | } |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1917 | |
Benjamin Kramer | c1aa40c | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1918 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 1919 | |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1920 | // Member field could not be with "template" keyword. |
| 1921 | // So TemplateParameterLists should be empty in this case. |
| 1922 | if (TemplateParameterLists.size()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1923 | TemplateParameterList* TemplateParams = TemplateParameterLists[0]; |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1924 | if (TemplateParams->size()) { |
| 1925 | // There is no such thing as a member field template. |
| 1926 | Diag(D.getIdentifierLoc(), diag::err_template_member) |
| 1927 | << II |
| 1928 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 1929 | TemplateParams->getRAngleLoc()); |
| 1930 | } else { |
| 1931 | // There is an extraneous 'template<>' for this member. |
| 1932 | Diag(TemplateParams->getTemplateLoc(), |
| 1933 | diag::err_template_member_noparams) |
| 1934 | << II |
| 1935 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 1936 | TemplateParams->getRAngleLoc()); |
| 1937 | } |
| 1938 | return 0; |
| 1939 | } |
| 1940 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1941 | if (SS.isSet() && !SS.isInvalid()) { |
| 1942 | // The user provided a superfluous scope specifier inside a class |
| 1943 | // definition: |
| 1944 | // |
| 1945 | // class X { |
| 1946 | // int X::member; |
| 1947 | // }; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 1948 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 1949 | diagnoseQualifiedDeclaration(SS, DC, Name, D.getIdentifierLoc()); |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1950 | else |
| 1951 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 1952 | << Name << SS.getRange(); |
Douglas Gregor | 5d8419c | 2011-11-01 22:13:30 +0000 | [diff] [blame] | 1953 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1954 | SS.clear(); |
| 1955 | } |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1956 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1957 | AttributeList *MSPropertyAttr = |
| 1958 | getMSPropertyAttr(D.getDeclSpec().getAttributes().getList()); |
Eli Friedman | b26f012 | 2013-06-28 20:48:34 +0000 | [diff] [blame] | 1959 | if (MSPropertyAttr) { |
| 1960 | Member = HandleMSProperty(S, cast<CXXRecordDecl>(CurContext), Loc, D, |
| 1961 | BitWidth, InitStyle, AS, MSPropertyAttr); |
| 1962 | if (!Member) |
| 1963 | return 0; |
| 1964 | isInstField = false; |
| 1965 | } else { |
| 1966 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, |
| 1967 | BitWidth, InitStyle, AS); |
| 1968 | assert(Member && "HandleField never returns null"); |
| 1969 | } |
| 1970 | } else { |
| 1971 | assert(InitStyle == ICIS_NoInit || D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static); |
| 1972 | |
| 1973 | Member = HandleDeclarator(S, D, TemplateParameterLists); |
| 1974 | if (!Member) |
| 1975 | return 0; |
| 1976 | |
| 1977 | // Non-instance-fields can't have a bitfield. |
| 1978 | if (BitWidth) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1979 | if (Member->isInvalidDecl()) { |
| 1980 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 1981 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1982 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 1983 | // "static member 'A' cannot be a bit-field" |
| 1984 | Diag(Loc, diag::err_static_not_bitfield) |
| 1985 | << Name << BitWidth->getSourceRange(); |
| 1986 | } else if (isa<TypedefDecl>(Member)) { |
| 1987 | // "typedef member 'x' cannot be a bit-field" |
| 1988 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 1989 | << Name << BitWidth->getSourceRange(); |
| 1990 | } else { |
| 1991 | // A function typedef ("typedef int f(); f a;"). |
| 1992 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 1993 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1995 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1996 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1998 | BitWidth = 0; |
| 1999 | Member->setInvalidDecl(); |
| 2000 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 2001 | |
| 2002 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame^] | 2004 | // If we have declared a member function template or static data member |
| 2005 | // template, set the access of the templated declaration as well. |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2006 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 2007 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame^] | 2008 | else if (VarTemplateDecl *VarTmpl = dyn_cast<VarTemplateDecl>(Member)) |
| 2009 | VarTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 2010 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2011 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2012 | if (VS.isOverrideSpecified()) |
| 2013 | Member->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context)); |
| 2014 | if (VS.isFinalSpecified()) |
| 2015 | Member->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context)); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 2016 | |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2017 | if (VS.getLastLocation().isValid()) { |
| 2018 | // Update the end location of a method that has a virt-specifiers. |
| 2019 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 2020 | MD->setRangeEnd(VS.getLastLocation()); |
| 2021 | } |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2022 | |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 2023 | CheckOverrideControl(Member); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 2024 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 2025 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2026 | |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2027 | if (isInstField) { |
| 2028 | FieldDecl *FD = cast<FieldDecl>(Member); |
| 2029 | FieldCollector->Add(FD); |
| 2030 | |
| 2031 | if (Diags.getDiagnosticLevel(diag::warn_unused_private_field, |
| 2032 | FD->getLocation()) |
| 2033 | != DiagnosticsEngine::Ignored) { |
| 2034 | // Remember all explicit private FieldDecls that have a name, no side |
| 2035 | // effects and are not part of a dependent type declaration. |
| 2036 | if (!FD->isImplicit() && FD->getDeclName() && |
| 2037 | FD->getAccess() == AS_private && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 2038 | !FD->hasAttr<UnusedAttr>() && |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 2039 | !FD->getParent()->isDependentContext() && |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2040 | !InitializationHasSideEffects(*FD)) |
| 2041 | UnusedPrivateFields.insert(FD); |
| 2042 | } |
| 2043 | } |
| 2044 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2045 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2048 | namespace { |
| 2049 | class UninitializedFieldVisitor |
| 2050 | : public EvaluatedExprVisitor<UninitializedFieldVisitor> { |
| 2051 | Sema &S; |
| 2052 | ValueDecl *VD; |
| 2053 | public: |
| 2054 | typedef EvaluatedExprVisitor<UninitializedFieldVisitor> Inherited; |
| 2055 | UninitializedFieldVisitor(Sema &S, ValueDecl *VD) : Inherited(S.Context), |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2056 | S(S) { |
| 2057 | if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(VD)) |
| 2058 | this->VD = IFD->getAnonField(); |
| 2059 | else |
| 2060 | this->VD = VD; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | void HandleExpr(Expr *E) { |
| 2064 | if (!E) return; |
| 2065 | |
| 2066 | // Expressions like x(x) sometimes lack the surrounding expressions |
| 2067 | // but need to be checked anyways. |
| 2068 | HandleValue(E); |
| 2069 | Visit(E); |
| 2070 | } |
| 2071 | |
| 2072 | void HandleValue(Expr *E) { |
| 2073 | E = E->IgnoreParens(); |
| 2074 | |
| 2075 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 2076 | if (isa<EnumConstantDecl>(ME->getMemberDecl())) |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2077 | return; |
| 2078 | |
| 2079 | // FieldME is the inner-most MemberExpr that is not an anonymous struct |
| 2080 | // or union. |
| 2081 | MemberExpr *FieldME = ME; |
| 2082 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2083 | Expr *Base = E; |
| 2084 | while (isa<MemberExpr>(Base)) { |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2085 | ME = cast<MemberExpr>(Base); |
| 2086 | |
| 2087 | if (isa<VarDecl>(ME->getMemberDecl())) |
| 2088 | return; |
| 2089 | |
| 2090 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2091 | if (!FD->isAnonymousStructOrUnion()) |
| 2092 | FieldME = ME; |
| 2093 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2094 | Base = ME->getBase(); |
| 2095 | } |
| 2096 | |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2097 | if (VD == FieldME->getMemberDecl() && isa<CXXThisExpr>(Base)) { |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2098 | unsigned diag = VD->getType()->isReferenceType() |
| 2099 | ? diag::warn_reference_field_is_uninit |
| 2100 | : diag::warn_field_is_uninit; |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2101 | S.Diag(FieldME->getExprLoc(), diag) << VD; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2102 | } |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2103 | return; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 2107 | HandleValue(CO->getTrueExpr()); |
| 2108 | HandleValue(CO->getFalseExpr()); |
| 2109 | return; |
| 2110 | } |
| 2111 | |
| 2112 | if (BinaryConditionalOperator *BCO = |
| 2113 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 2114 | HandleValue(BCO->getCommon()); |
| 2115 | HandleValue(BCO->getFalseExpr()); |
| 2116 | return; |
| 2117 | } |
| 2118 | |
| 2119 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 2120 | switch (BO->getOpcode()) { |
| 2121 | default: |
| 2122 | return; |
| 2123 | case(BO_PtrMemD): |
| 2124 | case(BO_PtrMemI): |
| 2125 | HandleValue(BO->getLHS()); |
| 2126 | return; |
| 2127 | case(BO_Comma): |
| 2128 | HandleValue(BO->getRHS()); |
| 2129 | return; |
| 2130 | } |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 2135 | if (E->getCastKind() == CK_LValueToRValue) |
| 2136 | HandleValue(E->getSubExpr()); |
| 2137 | |
| 2138 | Inherited::VisitImplicitCastExpr(E); |
| 2139 | } |
| 2140 | |
| 2141 | void VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 2142 | Expr *Callee = E->getCallee(); |
| 2143 | if (isa<MemberExpr>(Callee)) |
| 2144 | HandleValue(Callee); |
| 2145 | |
| 2146 | Inherited::VisitCXXMemberCallExpr(E); |
| 2147 | } |
| 2148 | }; |
| 2149 | static void CheckInitExprContainsUninitializedFields(Sema &S, Expr *E, |
| 2150 | ValueDecl *VD) { |
| 2151 | UninitializedFieldVisitor(S, VD).HandleExpr(E); |
| 2152 | } |
| 2153 | } // namespace |
| 2154 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2155 | /// ActOnCXXInClassMemberInitializer - This is invoked after parsing an |
Richard Smith | 0ff6f8f | 2011-07-20 00:12:52 +0000 | [diff] [blame] | 2156 | /// in-class initializer for a non-static C++ class member, and after |
| 2157 | /// instantiating an in-class initializer in a class template. Such actions |
| 2158 | /// are deferred until the class is complete. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2159 | void |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2160 | Sema::ActOnCXXInClassMemberInitializer(Decl *D, SourceLocation InitLoc, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2161 | Expr *InitExpr) { |
| 2162 | FieldDecl *FD = cast<FieldDecl>(D); |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2163 | assert(FD->getInClassInitStyle() != ICIS_NoInit && |
| 2164 | "must set init style when field is created"); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2165 | |
| 2166 | if (!InitExpr) { |
| 2167 | FD->setInvalidDecl(); |
| 2168 | FD->removeInClassInitializer(); |
| 2169 | return; |
| 2170 | } |
| 2171 | |
Peter Collingbourne | fef2189 | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 2172 | if (DiagnoseUnexpandedParameterPack(InitExpr, UPPC_Initializer)) { |
| 2173 | FD->setInvalidDecl(); |
| 2174 | FD->removeInClassInitializer(); |
| 2175 | return; |
| 2176 | } |
| 2177 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2178 | if (getDiagnostics().getDiagnosticLevel(diag::warn_field_is_uninit, InitLoc) |
| 2179 | != DiagnosticsEngine::Ignored) { |
| 2180 | CheckInitExprContainsUninitializedFields(*this, InitExpr, FD); |
| 2181 | } |
| 2182 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2183 | ExprResult Init = InitExpr; |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2184 | if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent()) { |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2185 | InitializedEntity Entity = InitializedEntity::InitializeMember(FD); |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2186 | InitializationKind Kind = FD->getInClassInitStyle() == ICIS_ListInit |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2187 | ? InitializationKind::CreateDirectList(InitExpr->getLocStart()) |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2188 | : InitializationKind::CreateCopy(InitExpr->getLocStart(), InitLoc); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2189 | InitializationSequence Seq(*this, Entity, Kind, InitExpr); |
| 2190 | Init = Seq.Perform(*this, Entity, Kind, InitExpr); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2191 | if (Init.isInvalid()) { |
| 2192 | FD->setInvalidDecl(); |
| 2193 | return; |
| 2194 | } |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2197 | // C++11 [class.base.init]p7: |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2198 | // The initialization of each base and member constitutes a |
| 2199 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2200 | Init = ActOnFinishFullExpr(Init.take(), InitLoc); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2201 | if (Init.isInvalid()) { |
| 2202 | FD->setInvalidDecl(); |
| 2203 | return; |
| 2204 | } |
| 2205 | |
| 2206 | InitExpr = Init.release(); |
| 2207 | |
| 2208 | FD->setInClassInitializer(InitExpr); |
| 2209 | } |
| 2210 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2211 | /// \brief Find the direct and/or virtual base specifiers that |
| 2212 | /// correspond to the given base type, for use in base initialization |
| 2213 | /// within a constructor. |
| 2214 | static bool FindBaseInitializer(Sema &SemaRef, |
| 2215 | CXXRecordDecl *ClassDecl, |
| 2216 | QualType BaseType, |
| 2217 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 2218 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 2219 | // First, check for a direct base class. |
| 2220 | DirectBaseSpec = 0; |
| 2221 | for (CXXRecordDecl::base_class_const_iterator Base |
| 2222 | = ClassDecl->bases_begin(); |
| 2223 | Base != ClassDecl->bases_end(); ++Base) { |
| 2224 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 2225 | // We found a direct base of this type. That's what we're |
| 2226 | // initializing. |
| 2227 | DirectBaseSpec = &*Base; |
| 2228 | break; |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | // Check for a virtual base class. |
| 2233 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 2234 | // there are no virtual bases. |
| 2235 | VirtualBaseSpec = 0; |
| 2236 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 2237 | // We haven't found a base yet; search the class hierarchy for a |
| 2238 | // virtual base class. |
| 2239 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 2240 | /*DetectVirtual=*/false); |
| 2241 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 2242 | BaseType, Paths)) { |
| 2243 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 2244 | Path != Paths.end(); ++Path) { |
| 2245 | if (Path->back().Base->isVirtual()) { |
| 2246 | VirtualBaseSpec = Path->back().Base; |
| 2247 | break; |
| 2248 | } |
| 2249 | } |
| 2250 | } |
| 2251 | } |
| 2252 | |
| 2253 | return DirectBaseSpec || VirtualBaseSpec; |
| 2254 | } |
| 2255 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2256 | /// \brief Handle a C++ member initializer using braced-init-list syntax. |
| 2257 | MemInitResult |
| 2258 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
| 2259 | Scope *S, |
| 2260 | CXXScopeSpec &SS, |
| 2261 | IdentifierInfo *MemberOrBase, |
| 2262 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2263 | const DeclSpec &DS, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2264 | SourceLocation IdLoc, |
| 2265 | Expr *InitList, |
| 2266 | SourceLocation EllipsisLoc) { |
| 2267 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2268 | DS, IdLoc, InitList, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2269 | EllipsisLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | /// \brief Handle a C++ member initializer using parentheses syntax. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2273 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2274 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2275 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2276 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2277 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2278 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2279 | const DeclSpec &DS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2280 | SourceLocation IdLoc, |
| 2281 | SourceLocation LParenLoc, |
Dmitri Gribenko | a36bbac | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 2282 | ArrayRef<Expr *> Args, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2283 | SourceLocation RParenLoc, |
| 2284 | SourceLocation EllipsisLoc) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2285 | Expr *List = new (Context) ParenListExpr(Context, LParenLoc, |
Dmitri Gribenko | a36bbac | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 2286 | Args, RParenLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2287 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2288 | DS, IdLoc, List, EllipsisLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2291 | namespace { |
| 2292 | |
Kaelyn Uhrain | dc98cd0 | 2012-01-11 21:17:51 +0000 | [diff] [blame] | 2293 | // Callback to only accept typo corrections that can be a valid C++ member |
| 2294 | // intializer: either a non-static field member or a base class. |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2295 | class MemInitializerValidatorCCC : public CorrectionCandidateCallback { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2296 | public: |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2297 | explicit MemInitializerValidatorCCC(CXXRecordDecl *ClassDecl) |
| 2298 | : ClassDecl(ClassDecl) {} |
| 2299 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2300 | bool ValidateCandidate(const TypoCorrection &candidate) LLVM_OVERRIDE { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2301 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 2302 | if (FieldDecl *Member = dyn_cast<FieldDecl>(ND)) |
| 2303 | return Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl); |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2304 | return isa<TypeDecl>(ND); |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2305 | } |
| 2306 | return false; |
| 2307 | } |
| 2308 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2309 | private: |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2310 | CXXRecordDecl *ClassDecl; |
| 2311 | }; |
| 2312 | |
| 2313 | } |
| 2314 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2315 | /// \brief Handle a C++ member initializer. |
| 2316 | MemInitResult |
| 2317 | Sema::BuildMemInitializer(Decl *ConstructorD, |
| 2318 | Scope *S, |
| 2319 | CXXScopeSpec &SS, |
| 2320 | IdentifierInfo *MemberOrBase, |
| 2321 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2322 | const DeclSpec &DS, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2323 | SourceLocation IdLoc, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2324 | Expr *Init, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2325 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2326 | if (!ConstructorD) |
| 2327 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2328 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2329 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2330 | |
| 2331 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2332 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2333 | if (!Constructor) { |
| 2334 | // The user wrote a constructor initializer on a function that is |
| 2335 | // not a C++ constructor. Ignore the error for now, because we may |
| 2336 | // have more member initializers coming; we'll diagnose it just |
| 2337 | // once in ActOnMemInitializers. |
| 2338 | return true; |
| 2339 | } |
| 2340 | |
| 2341 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2342 | |
| 2343 | // C++ [class.base.init]p2: |
| 2344 | // 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] | 2345 | // constructor's class and, if not found in that scope, are looked |
| 2346 | // up in the scope containing the constructor's definition. |
| 2347 | // [Note: if the constructor's class contains a member with the |
| 2348 | // same name as a direct or virtual base class of the class, a |
| 2349 | // mem-initializer-id naming the member or base class and composed |
| 2350 | // of a single identifier refers to the class member. A |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2351 | // mem-initializer-id for the hidden base class may be specified |
| 2352 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2353 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2354 | // Look for a member, first. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2356 | = ClassDecl->lookup(MemberOrBase); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2357 | if (!Result.empty()) { |
Peter Collingbourne | dc69be2 | 2011-10-23 18:59:37 +0000 | [diff] [blame] | 2358 | ValueDecl *Member; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2359 | if ((Member = dyn_cast<FieldDecl>(Result.front())) || |
| 2360 | (Member = dyn_cast<IndirectFieldDecl>(Result.front()))) { |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2361 | if (EllipsisLoc.isValid()) |
| 2362 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2363 | << MemberOrBase |
| 2364 | << SourceRange(IdLoc, Init->getSourceRange().getEnd()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2365 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2366 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2367 | } |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2368 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2369 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2370 | // 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] | 2371 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2372 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2373 | |
| 2374 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2375 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2376 | } else if (DS.getTypeSpecType() == TST_decltype) { |
| 2377 | BaseType = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2378 | } else { |
| 2379 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 2380 | LookupParsedName(R, S, &SS); |
| 2381 | |
| 2382 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 2383 | if (!TyD) { |
| 2384 | if (R.isAmbiguous()) return true; |
| 2385 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 2386 | // We don't want access-control diagnostics here. |
| 2387 | R.suppressDiagnostics(); |
| 2388 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2389 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 2390 | bool NotUnknownSpecialization = false; |
| 2391 | DeclContext *DC = computeDeclContext(SS, false); |
| 2392 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 2393 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 2394 | |
| 2395 | if (!NotUnknownSpecialization) { |
| 2396 | // When the scope specifier can refer to a member of an unknown |
| 2397 | // specialization, we take it as a type name. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 2398 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 2399 | SS.getWithLocInContext(Context), |
| 2400 | *MemberOrBase, IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 2401 | if (BaseType.isNull()) |
| 2402 | return true; |
| 2403 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2404 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 2405 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2409 | // If no results were found, try to correct typos. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2410 | TypoCorrection Corr; |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2411 | MemInitializerValidatorCCC Validator(ClassDecl); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2412 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2413 | (Corr = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 2414 | Validator, ClassDecl))) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2415 | std::string CorrectedStr(Corr.getAsString(getLangOpts())); |
| 2416 | std::string CorrectedQuotedStr(Corr.getQuoted(getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2417 | if (FieldDecl *Member = Corr.getCorrectionDeclAs<FieldDecl>()) { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2418 | // We have found a non-static data member with a similar |
| 2419 | // name to what was typed; complain and initialize that |
| 2420 | // member. |
| 2421 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 2422 | << MemberOrBase << true << CorrectedQuotedStr |
| 2423 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
| 2424 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 2425 | << CorrectedQuotedStr; |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2426 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2427 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2428 | } else if (TypeDecl *Type = Corr.getCorrectionDeclAs<TypeDecl>()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2429 | const CXXBaseSpecifier *DirectBaseSpec; |
| 2430 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 2431 | if (FindBaseInitializer(*this, ClassDecl, |
| 2432 | Context.getTypeDeclType(Type), |
| 2433 | DirectBaseSpec, VirtualBaseSpec)) { |
| 2434 | // We have found a direct or virtual base class with a |
| 2435 | // similar name to what was typed; complain and initialize |
| 2436 | // that base class. |
| 2437 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2438 | << MemberOrBase << false << CorrectedQuotedStr |
| 2439 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2440 | |
| 2441 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 2442 | : VirtualBaseSpec; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2443 | Diag(BaseSpec->getLocStart(), |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2444 | diag::note_base_class_specified_here) |
| 2445 | << BaseSpec->getType() |
| 2446 | << BaseSpec->getSourceRange(); |
| 2447 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2448 | TyD = Type; |
| 2449 | } |
| 2450 | } |
| 2451 | } |
| 2452 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2453 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2454 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2455 | << MemberOrBase << SourceRange(IdLoc,Init->getSourceRange().getEnd()); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2456 | return true; |
| 2457 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2460 | if (BaseType.isNull()) { |
| 2461 | BaseType = Context.getTypeDeclType(TyD); |
| 2462 | if (SS.isSet()) { |
| 2463 | NestedNameSpecifier *Qualifier = |
| 2464 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2465 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2466 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2467 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2468 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2469 | } |
| 2470 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2471 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2472 | if (!TInfo) |
| 2473 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2474 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2475 | return BuildBaseInitializer(BaseType, TInfo, Init, ClassDecl, EllipsisLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2478 | /// Checks a member initializer expression for cases where reference (or |
| 2479 | /// pointer) members are bound to by-value parameters (or their addresses). |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2480 | static void CheckForDanglingReferenceOrPointer(Sema &S, ValueDecl *Member, |
| 2481 | Expr *Init, |
| 2482 | SourceLocation IdLoc) { |
| 2483 | QualType MemberTy = Member->getType(); |
| 2484 | |
| 2485 | // We only handle pointers and references currently. |
| 2486 | // FIXME: Would this be relevant for ObjC object pointers? Or block pointers? |
| 2487 | if (!MemberTy->isReferenceType() && !MemberTy->isPointerType()) |
| 2488 | return; |
| 2489 | |
| 2490 | const bool IsPointer = MemberTy->isPointerType(); |
| 2491 | if (IsPointer) { |
| 2492 | if (const UnaryOperator *Op |
| 2493 | = dyn_cast<UnaryOperator>(Init->IgnoreParenImpCasts())) { |
| 2494 | // The only case we're worried about with pointers requires taking the |
| 2495 | // address. |
| 2496 | if (Op->getOpcode() != UO_AddrOf) |
| 2497 | return; |
| 2498 | |
| 2499 | Init = Op->getSubExpr(); |
| 2500 | } else { |
| 2501 | // We only handle address-of expression initializers for pointers. |
| 2502 | return; |
| 2503 | } |
| 2504 | } |
| 2505 | |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2506 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init->IgnoreParens())) { |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2507 | // We only warn when referring to a non-reference parameter declaration. |
| 2508 | const ParmVarDecl *Parameter = dyn_cast<ParmVarDecl>(DRE->getDecl()); |
| 2509 | if (!Parameter || Parameter->getType()->isReferenceType()) |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2510 | return; |
| 2511 | |
| 2512 | S.Diag(Init->getExprLoc(), |
| 2513 | IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 2514 | : diag::warn_bind_ref_member_to_parameter) |
| 2515 | << Member << Parameter << Init->getSourceRange(); |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2516 | } else { |
| 2517 | // Other initializers are fine. |
| 2518 | return; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2519 | } |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2520 | |
| 2521 | S.Diag(Member->getLocation(), diag::note_ref_or_ptr_member_declared_here) |
| 2522 | << (unsigned)IsPointer; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2525 | MemInitResult |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2526 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr *Init, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2527 | SourceLocation IdLoc) { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2528 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 2529 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 2530 | assert((DirectMember || IndirectMember) && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2531 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 2532 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2533 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Peter Collingbourne | fef2189 | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 2534 | return true; |
| 2535 | |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 2536 | if (Member->isInvalidDecl()) |
| 2537 | return true; |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2538 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2539 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 2540 | // foo(foo) |
| 2541 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 2542 | // TODO: implement -Wuninitialized and fold this into that framework. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2543 | MultiExprArg Args; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2544 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2545 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2546 | } else if (InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2547 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2548 | } else { |
| 2549 | // Template instantiation doesn't reconstruct ParenListExprs for us. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2550 | Args = Init; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2551 | } |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2552 | |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 2553 | if (getDiagnostics().getDiagnosticLevel(diag::warn_field_is_uninit, IdLoc) |
| 2554 | != DiagnosticsEngine::Ignored) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2555 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 2556 | // FIXME: Warn about the case when other fields are used before being |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2557 | // initialized. For example, let this field be the i'th field. When |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2558 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 2559 | // fields are used, as they are not yet initialized. |
| 2560 | // Right now we are only handling the case where the i'th field uses |
| 2561 | // itself in its initializer. |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2562 | // Also need to take into account that some fields may be initialized by |
| 2563 | // in-class initializers, see C++11 [class.base.init]p9. |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 2564 | CheckInitExprContainsUninitializedFields(*this, Args[i], Member); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2565 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2566 | SourceRange InitRange = Init->getSourceRange(); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2567 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2568 | if (Member->getType()->isDependentType() || Init->isTypeDependent()) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2569 | // Can't check initialization for a member of dependent type or when |
| 2570 | // any of the arguments are type-dependent expressions. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2571 | DiscardCleanupsInEvaluationContext(); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2572 | } else { |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2573 | bool InitList = false; |
| 2574 | if (isa<InitListExpr>(Init)) { |
| 2575 | InitList = true; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2576 | Args = Init; |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2579 | // Initialize the member. |
| 2580 | InitializedEntity MemberEntity = |
| 2581 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 2582 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 2583 | InitializationKind Kind = |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2584 | InitList ? InitializationKind::CreateDirectList(IdLoc) |
| 2585 | : InitializationKind::CreateDirect(IdLoc, InitRange.getBegin(), |
| 2586 | InitRange.getEnd()); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2587 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2588 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args); |
| 2589 | ExprResult MemberInit = InitSeq.Perform(*this, MemberEntity, Kind, Args, 0); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2590 | if (MemberInit.isInvalid()) |
| 2591 | return true; |
| 2592 | |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 2593 | CheckForDanglingReferenceOrPointer(*this, Member, MemberInit.get(), IdLoc); |
| 2594 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2595 | // C++11 [class.base.init]p7: |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2596 | // The initialization of each base and member constitutes a |
| 2597 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2598 | MemberInit = ActOnFinishFullExpr(MemberInit.get(), InitRange.getBegin()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2599 | if (MemberInit.isInvalid()) |
| 2600 | return true; |
| 2601 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2602 | Init = MemberInit.get(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2605 | if (DirectMember) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2606 | return new (Context) CXXCtorInitializer(Context, DirectMember, IdLoc, |
| 2607 | InitRange.getBegin(), Init, |
| 2608 | InitRange.getEnd()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2609 | } else { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2610 | return new (Context) CXXCtorInitializer(Context, IndirectMember, IdLoc, |
| 2611 | InitRange.getBegin(), Init, |
| 2612 | InitRange.getEnd()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2613 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2616 | MemInitResult |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2617 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, Expr *Init, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2618 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2619 | SourceLocation NameLoc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2620 | if (!LangOpts.CPlusPlus11) |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2621 | return Diag(NameLoc, diag::err_delegating_ctor) |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2622 | << TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2623 | Diag(NameLoc, diag::warn_cxx98_compat_delegating_ctor); |
Sebastian Redl | f9c32eb | 2011-03-12 13:53:51 +0000 | [diff] [blame] | 2624 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2625 | bool InitList = true; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2626 | MultiExprArg Args = Init; |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2627 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 2628 | InitList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2629 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2632 | SourceRange InitRange = Init->getSourceRange(); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2633 | // Initialize the object. |
| 2634 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 2635 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 2636 | InitializationKind Kind = |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2637 | InitList ? InitializationKind::CreateDirectList(NameLoc) |
| 2638 | : InitializationKind::CreateDirect(NameLoc, InitRange.getBegin(), |
| 2639 | InitRange.getEnd()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2640 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2641 | ExprResult DelegationInit = InitSeq.Perform(*this, DelegationEntity, Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2642 | Args, 0); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2643 | if (DelegationInit.isInvalid()) |
| 2644 | return true; |
| 2645 | |
Matt Beaumont-Gay | 2eb0ce3 | 2011-11-01 18:10:22 +0000 | [diff] [blame] | 2646 | assert(cast<CXXConstructExpr>(DelegationInit.get())->getConstructor() && |
| 2647 | "Delegating constructor with no target?"); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2648 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2649 | // C++11 [class.base.init]p7: |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2650 | // The initialization of each base and member constitutes a |
| 2651 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2652 | DelegationInit = ActOnFinishFullExpr(DelegationInit.get(), |
| 2653 | InitRange.getBegin()); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2654 | if (DelegationInit.isInvalid()) |
| 2655 | return true; |
| 2656 | |
Eli Friedman | d21016f | 2012-05-19 23:35:23 +0000 | [diff] [blame] | 2657 | // If we are in a dependent context, template instantiation will |
| 2658 | // perform this type-checking again. Just save the arguments that we |
| 2659 | // received in a ParenListExpr. |
| 2660 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2661 | // of the information that we have about the base |
| 2662 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2663 | // and this approach is far more likely to get the corner cases right. |
| 2664 | if (CurContext->isDependentContext()) |
| 2665 | DelegationInit = Owned(Init); |
| 2666 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2667 | return new (Context) CXXCtorInitializer(Context, TInfo, InitRange.getBegin(), |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2668 | DelegationInit.takeAs<Expr>(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2669 | InitRange.getEnd()); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2673 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2674 | Expr *Init, CXXRecordDecl *ClassDecl, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2675 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2676 | SourceLocation BaseLoc |
| 2677 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2678 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2679 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 2680 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 2681 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2682 | |
| 2683 | // C++ [class.base.init]p2: |
| 2684 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2685 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2686 | // of that class, the mem-initializer is ill-formed. A |
| 2687 | // mem-initializer-list can initialize a base class using any |
| 2688 | // name that denotes that base class type. |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2689 | bool Dependent = BaseType->isDependentType() || Init->isTypeDependent(); |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2690 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2691 | SourceRange InitRange = Init->getSourceRange(); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2692 | if (EllipsisLoc.isValid()) { |
| 2693 | // This is a pack expansion. |
| 2694 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 2695 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2696 | << SourceRange(BaseLoc, InitRange.getEnd()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2697 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2698 | EllipsisLoc = SourceLocation(); |
| 2699 | } |
| 2700 | } else { |
| 2701 | // Check for any unexpanded parameter packs. |
| 2702 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 2703 | return true; |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2704 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2705 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2706 | return true; |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2707 | } |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2708 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2709 | // Check for direct and virtual base classes. |
| 2710 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 2711 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 2712 | if (!Dependent) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2713 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 2714 | BaseType)) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2715 | return BuildDelegatingInitializer(BaseTInfo, Init, ClassDecl); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2716 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2717 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 2718 | VirtualBaseSpec); |
| 2719 | |
| 2720 | // C++ [base.class.init]p2: |
| 2721 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 2722 | // constructor's class or a direct or virtual base of that class, the |
| 2723 | // mem-initializer is ill-formed. |
| 2724 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 2725 | // If the class has any dependent bases, then it's possible that |
| 2726 | // one of those types will resolve to the same type as |
| 2727 | // BaseType. Therefore, just treat this as a dependent base |
| 2728 | // class initialization. FIXME: Should we try to check the |
| 2729 | // initialization anyway? It seems odd. |
| 2730 | if (ClassDecl->hasAnyDependentBases()) |
| 2731 | Dependent = true; |
| 2732 | else |
| 2733 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 2734 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 2735 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2736 | } |
| 2737 | } |
| 2738 | |
| 2739 | if (Dependent) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2740 | DiscardCleanupsInEvaluationContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2741 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2742 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
| 2743 | /*IsVirtual=*/false, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2744 | InitRange.getBegin(), Init, |
| 2745 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2746 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2747 | |
| 2748 | // C++ [base.class.init]p2: |
| 2749 | // If a mem-initializer-id is ambiguous because it designates both |
| 2750 | // a direct non-virtual base class and an inherited virtual base |
| 2751 | // class, the mem-initializer is ill-formed. |
| 2752 | if (DirectBaseSpec && VirtualBaseSpec) |
| 2753 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2754 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2755 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2756 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2757 | if (!BaseSpec) |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2758 | BaseSpec = VirtualBaseSpec; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2759 | |
| 2760 | // Initialize the base. |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2761 | bool InitList = true; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2762 | MultiExprArg Args = Init; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2763 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2764 | InitList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2765 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2766 | } |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2767 | |
| 2768 | InitializedEntity BaseEntity = |
| 2769 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
| 2770 | InitializationKind Kind = |
| 2771 | InitList ? InitializationKind::CreateDirectList(BaseLoc) |
| 2772 | : InitializationKind::CreateDirect(BaseLoc, InitRange.getBegin(), |
| 2773 | InitRange.getEnd()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2774 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args); |
| 2775 | ExprResult BaseInit = InitSeq.Perform(*this, BaseEntity, Kind, Args, 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2776 | if (BaseInit.isInvalid()) |
| 2777 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2778 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2779 | // C++11 [class.base.init]p7: |
| 2780 | // The initialization of each base and member constitutes a |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2781 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2782 | BaseInit = ActOnFinishFullExpr(BaseInit.get(), InitRange.getBegin()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2783 | if (BaseInit.isInvalid()) |
| 2784 | return true; |
| 2785 | |
| 2786 | // If we are in a dependent context, template instantiation will |
| 2787 | // perform this type-checking again. Just save the arguments that we |
| 2788 | // received in a ParenListExpr. |
| 2789 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2790 | // of the information that we have about the base |
| 2791 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2792 | // and this approach is far more likely to get the corner cases right. |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2793 | if (CurContext->isDependentContext()) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2794 | BaseInit = Owned(Init); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2795 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2796 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2797 | BaseSpec->isVirtual(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2798 | InitRange.getBegin(), |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2799 | BaseInit.takeAs<Expr>(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2800 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2803 | // Create a static_cast\<T&&>(expr). |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 2804 | static Expr *CastForMoving(Sema &SemaRef, Expr *E, QualType T = QualType()) { |
| 2805 | if (T.isNull()) T = E->getType(); |
| 2806 | QualType TargetType = SemaRef.BuildReferenceType( |
| 2807 | T, /*SpelledAsLValue*/false, SourceLocation(), DeclarationName()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2808 | SourceLocation ExprLoc = E->getLocStart(); |
| 2809 | TypeSourceInfo *TargetLoc = SemaRef.Context.getTrivialTypeSourceInfo( |
| 2810 | TargetType, ExprLoc); |
| 2811 | |
| 2812 | return SemaRef.BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
| 2813 | SourceRange(ExprLoc, ExprLoc), |
| 2814 | E->getSourceRange()).take(); |
| 2815 | } |
| 2816 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2817 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 2818 | /// initialize its base or member. |
| 2819 | enum ImplicitInitializerKind { |
| 2820 | IIK_Default, |
| 2821 | IIK_Copy, |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 2822 | IIK_Move, |
| 2823 | IIK_Inherit |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2824 | }; |
| 2825 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2826 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2827 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2828 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2829 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2830 | bool IsInheritedVirtualBase, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2831 | CXXCtorInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2832 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2833 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 2834 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2835 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2836 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2837 | |
| 2838 | switch (ImplicitInitKind) { |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 2839 | case IIK_Inherit: { |
| 2840 | const CXXRecordDecl *Inherited = |
| 2841 | Constructor->getInheritedConstructor()->getParent(); |
| 2842 | const CXXRecordDecl *Base = BaseSpec->getType()->getAsCXXRecordDecl(); |
| 2843 | if (Base && Inherited->getCanonicalDecl() == Base->getCanonicalDecl()) { |
| 2844 | // C++11 [class.inhctor]p8: |
| 2845 | // Each expression in the expression-list is of the form |
| 2846 | // static_cast<T&&>(p), where p is the name of the corresponding |
| 2847 | // constructor parameter and T is the declared type of p. |
| 2848 | SmallVector<Expr*, 16> Args; |
| 2849 | for (unsigned I = 0, E = Constructor->getNumParams(); I != E; ++I) { |
| 2850 | ParmVarDecl *PD = Constructor->getParamDecl(I); |
| 2851 | ExprResult ArgExpr = |
| 2852 | SemaRef.BuildDeclRefExpr(PD, PD->getType().getNonReferenceType(), |
| 2853 | VK_LValue, SourceLocation()); |
| 2854 | if (ArgExpr.isInvalid()) |
| 2855 | return true; |
| 2856 | Args.push_back(CastForMoving(SemaRef, ArgExpr.take(), PD->getType())); |
| 2857 | } |
| 2858 | |
| 2859 | InitializationKind InitKind = InitializationKind::CreateDirect( |
| 2860 | Constructor->getLocation(), SourceLocation(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2861 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, Args); |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 2862 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, Args); |
| 2863 | break; |
| 2864 | } |
| 2865 | } |
| 2866 | // Fall through. |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2867 | case IIK_Default: { |
| 2868 | InitializationKind InitKind |
| 2869 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2870 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, None); |
| 2871 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, None); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2872 | break; |
| 2873 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2874 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2875 | case IIK_Move: |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2876 | case IIK_Copy: { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2877 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2878 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 2879 | QualType ParamType = Param->getType().getNonReferenceType(); |
Eli Friedman | cf7c14c | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 2880 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2881 | Expr *CopyCtorArg = |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2882 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2883 | SourceLocation(), Param, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2884 | Constructor->getLocation(), ParamType, |
| 2885 | VK_LValue, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2886 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2887 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(CopyCtorArg)); |
| 2888 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 2889 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 2890 | QualType ArgTy = |
| 2891 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 2892 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2893 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2894 | if (Moving) { |
| 2895 | CopyCtorArg = CastForMoving(SemaRef, CopyCtorArg); |
| 2896 | } |
| 2897 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2898 | CXXCastPath BasePath; |
| 2899 | BasePath.push_back(BaseSpec); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2900 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 2901 | CK_UncheckedDerivedToBase, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2902 | Moving ? VK_XValue : VK_LValue, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2903 | &BasePath).take(); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 2904 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2905 | InitializationKind InitKind |
| 2906 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 2907 | SourceLocation(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2908 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, CopyCtorArg); |
| 2909 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, CopyCtorArg); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2910 | break; |
| 2911 | } |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2912 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2913 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2914 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2915 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2916 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2917 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2918 | CXXBaseInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2919 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2920 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 2921 | SourceLocation()), |
| 2922 | BaseSpec->isVirtual(), |
| 2923 | SourceLocation(), |
| 2924 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2925 | SourceLocation(), |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2926 | SourceLocation()); |
| 2927 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2928 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2931 | static bool RefersToRValueRef(Expr *MemRef) { |
| 2932 | ValueDecl *Referenced = cast<MemberExpr>(MemRef)->getMemberDecl(); |
| 2933 | return Referenced->getType()->isRValueReferenceType(); |
| 2934 | } |
| 2935 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2936 | static bool |
| 2937 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2938 | ImplicitInitializerKind ImplicitInitKind, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2939 | FieldDecl *Field, IndirectFieldDecl *Indirect, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2940 | CXXCtorInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2941 | if (Field->isInvalidDecl()) |
| 2942 | return true; |
| 2943 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2944 | SourceLocation Loc = Constructor->getLocation(); |
| 2945 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2946 | if (ImplicitInitKind == IIK_Copy || ImplicitInitKind == IIK_Move) { |
| 2947 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2948 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 2949 | QualType ParamType = Param->getType().getNonReferenceType(); |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 2950 | |
| 2951 | // Suppress copying zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2952 | if (Field->isBitField() && Field->getBitWidthValue(SemaRef.Context) == 0) |
| 2953 | return false; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 2954 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2955 | Expr *MemberExprBase = |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2956 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2957 | SourceLocation(), Param, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2958 | Loc, ParamType, VK_LValue, 0); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2959 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2960 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(MemberExprBase)); |
| 2961 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2962 | if (Moving) { |
| 2963 | MemberExprBase = CastForMoving(SemaRef, MemberExprBase); |
| 2964 | } |
| 2965 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2966 | // Build a reference to this field within the parameter. |
| 2967 | CXXScopeSpec SS; |
| 2968 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 2969 | Sema::LookupMemberName); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2970 | MemberLookup.addDecl(Indirect ? cast<ValueDecl>(Indirect) |
| 2971 | : cast<ValueDecl>(Field), AS_public); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2972 | MemberLookup.resolveKind(); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2973 | ExprResult CtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2974 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2975 | ParamType, Loc, |
| 2976 | /*IsArrow=*/false, |
| 2977 | SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2978 | /*TemplateKWLoc=*/SourceLocation(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2979 | /*FirstQualifierInScope=*/0, |
| 2980 | MemberLookup, |
| 2981 | /*TemplateArgs=*/0); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2982 | if (CtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2983 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2984 | |
| 2985 | // C++11 [class.copy]p15: |
| 2986 | // - if a member m has rvalue reference type T&&, it is direct-initialized |
| 2987 | // with static_cast<T&&>(x.m); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2988 | if (RefersToRValueRef(CtorArg.get())) { |
| 2989 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2992 | // When the field we are copying is an array, create index variables for |
| 2993 | // each dimension of the array. We use these index variables to subscript |
| 2994 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 2995 | // necessary iteration with these index variables. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2996 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2997 | QualType BaseType = Field->getType(); |
| 2998 | QualType SizeType = SemaRef.Context.getSizeType(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2999 | bool InitializingArray = false; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3000 | while (const ConstantArrayType *Array |
| 3001 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3002 | InitializingArray = true; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3003 | // Create the iteration variable for this array index. |
| 3004 | IdentifierInfo *IterationVarName = 0; |
| 3005 | { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3006 | SmallString<8> Str; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3007 | llvm::raw_svector_ostream OS(Str); |
| 3008 | OS << "__i" << IndexVariables.size(); |
| 3009 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 3010 | } |
| 3011 | VarDecl *IterationVar |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3012 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3013 | IterationVarName, SizeType, |
| 3014 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3015 | SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3016 | IndexVariables.push_back(IterationVar); |
| 3017 | |
| 3018 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3019 | ExprResult IterationVarRef |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 3020 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3021 | assert(!IterationVarRef.isInvalid() && |
| 3022 | "Reference to invented variable cannot fail!"); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 3023 | IterationVarRef = SemaRef.DefaultLvalueConversion(IterationVarRef.take()); |
| 3024 | assert(!IterationVarRef.isInvalid() && |
| 3025 | "Conversion of invented variable cannot fail!"); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3026 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3027 | // Subscript the array with this iteration variable. |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3028 | CtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CtorArg.take(), Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3029 | IterationVarRef.take(), |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3030 | Loc); |
| 3031 | if (CtorArg.isInvalid()) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3032 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3033 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3034 | BaseType = Array->getElementType(); |
| 3035 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3036 | |
| 3037 | // The array subscript expression is an lvalue, which is wrong for moving. |
| 3038 | if (Moving && InitializingArray) |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3039 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3040 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3041 | // Construct the entity that we will be initializing. For an array, this |
| 3042 | // will be first element in the array, which may require several levels |
| 3043 | // of array-subscript entities. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3044 | SmallVector<InitializedEntity, 4> Entities; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3045 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3046 | if (Indirect) |
| 3047 | Entities.push_back(InitializedEntity::InitializeMember(Indirect)); |
| 3048 | else |
| 3049 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3050 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 3051 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 3052 | 0, |
| 3053 | Entities.back())); |
| 3054 | |
| 3055 | // Direct-initialize to use the copy constructor. |
| 3056 | InitializationKind InitKind = |
| 3057 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 3058 | |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3059 | Expr *CtorArgE = CtorArg.takeAs<Expr>(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3060 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, CtorArgE); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3061 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3062 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3063 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3064 | MultiExprArg(&CtorArgE, 1)); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3065 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3066 | if (MemberInit.isInvalid()) |
| 3067 | return true; |
| 3068 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3069 | if (Indirect) { |
| 3070 | assert(IndexVariables.size() == 0 && |
| 3071 | "Indirect field improperly initialized"); |
| 3072 | CXXMemberInit |
| 3073 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 3074 | Loc, Loc, |
| 3075 | MemberInit.takeAs<Expr>(), |
| 3076 | Loc); |
| 3077 | } else |
| 3078 | CXXMemberInit = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, |
| 3079 | Loc, MemberInit.takeAs<Expr>(), |
| 3080 | Loc, |
| 3081 | IndexVariables.data(), |
| 3082 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3083 | return false; |
| 3084 | } |
| 3085 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3086 | assert((ImplicitInitKind == IIK_Default || ImplicitInitKind == IIK_Inherit) && |
| 3087 | "Unhandled implicit init kind!"); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3088 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3089 | QualType FieldBaseElementType = |
| 3090 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 3091 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3092 | if (FieldBaseElementType->isRecordType()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3093 | InitializedEntity InitEntity |
| 3094 | = Indirect? InitializedEntity::InitializeMember(Indirect) |
| 3095 | : InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3096 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 3097 | InitializationKind::CreateDefault(Loc); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3098 | |
| 3099 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, None); |
| 3100 | ExprResult MemberInit = |
| 3101 | InitSeq.Perform(SemaRef, InitEntity, InitKind, None); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3102 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3103 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3104 | if (MemberInit.isInvalid()) |
| 3105 | return true; |
| 3106 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3107 | if (Indirect) |
| 3108 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 3109 | Indirect, Loc, |
| 3110 | Loc, |
| 3111 | MemberInit.get(), |
| 3112 | Loc); |
| 3113 | else |
| 3114 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 3115 | Field, Loc, Loc, |
| 3116 | MemberInit.get(), |
| 3117 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3118 | return false; |
| 3119 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3120 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 3121 | if (!Field->getParent()->isUnion()) { |
| 3122 | if (FieldBaseElementType->isReferenceType()) { |
| 3123 | SemaRef.Diag(Constructor->getLocation(), |
| 3124 | diag::err_uninitialized_member_in_ctor) |
| 3125 | << (int)Constructor->isImplicit() |
| 3126 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 3127 | << 0 << Field->getDeclName(); |
| 3128 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 3129 | return true; |
| 3130 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3131 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 3132 | if (FieldBaseElementType.isConstQualified()) { |
| 3133 | SemaRef.Diag(Constructor->getLocation(), |
| 3134 | diag::err_uninitialized_member_in_ctor) |
| 3135 | << (int)Constructor->isImplicit() |
| 3136 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 3137 | << 1 << Field->getDeclName(); |
| 3138 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 3139 | return true; |
| 3140 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3141 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3142 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3143 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3144 | FieldBaseElementType->isObjCRetainableType() && |
| 3145 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_None && |
| 3146 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) { |
Douglas Gregor | 3fe52ff | 2012-07-23 04:23:39 +0000 | [diff] [blame] | 3147 | // ARC: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3148 | // Default-initialize Objective-C pointers to NULL. |
| 3149 | CXXMemberInit |
| 3150 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 3151 | Loc, Loc, |
| 3152 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()), |
| 3153 | Loc); |
| 3154 | return false; |
| 3155 | } |
| 3156 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3157 | // Nothing to initialize. |
| 3158 | CXXMemberInit = 0; |
| 3159 | return false; |
| 3160 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3161 | |
| 3162 | namespace { |
| 3163 | struct BaseAndFieldInfo { |
| 3164 | Sema &S; |
| 3165 | CXXConstructorDecl *Ctor; |
| 3166 | bool AnyErrorsInInits; |
| 3167 | ImplicitInitializerKind IIK; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3168 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3169 | SmallVector<CXXCtorInitializer*, 8> AllToInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3170 | |
| 3171 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 3172 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3173 | bool Generated = Ctor->isImplicit() || Ctor->isDefaulted(); |
| 3174 | if (Generated && Ctor->isCopyConstructor()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3175 | IIK = IIK_Copy; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3176 | else if (Generated && Ctor->isMoveConstructor()) |
| 3177 | IIK = IIK_Move; |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3178 | else if (Ctor->getInheritedConstructor()) |
| 3179 | IIK = IIK_Inherit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3180 | else |
| 3181 | IIK = IIK_Default; |
| 3182 | } |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3183 | |
| 3184 | bool isImplicitCopyOrMove() const { |
| 3185 | switch (IIK) { |
| 3186 | case IIK_Copy: |
| 3187 | case IIK_Move: |
| 3188 | return true; |
| 3189 | |
| 3190 | case IIK_Default: |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3191 | case IIK_Inherit: |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3192 | return false; |
| 3193 | } |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 3194 | |
| 3195 | llvm_unreachable("Invalid ImplicitInitializerKind!"); |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3196 | } |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3197 | |
| 3198 | bool addFieldInitializer(CXXCtorInitializer *Init) { |
| 3199 | AllToInit.push_back(Init); |
| 3200 | |
| 3201 | // Check whether this initializer makes the field "used". |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3202 | if (Init->getInit()->HasSideEffects(S.Context)) |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3203 | S.UnusedPrivateFields.remove(Init->getAnyMember()); |
| 3204 | |
| 3205 | return false; |
| 3206 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3207 | }; |
| 3208 | } |
| 3209 | |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 3210 | /// \brief Determine whether the given indirect field declaration is somewhere |
| 3211 | /// within an anonymous union. |
| 3212 | static bool isWithinAnonymousUnion(IndirectFieldDecl *F) { |
| 3213 | for (IndirectFieldDecl::chain_iterator C = F->chain_begin(), |
| 3214 | CEnd = F->chain_end(); |
| 3215 | C != CEnd; ++C) |
| 3216 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>((*C)->getDeclContext())) |
| 3217 | if (Record->isUnion()) |
| 3218 | return true; |
| 3219 | |
| 3220 | return false; |
| 3221 | } |
| 3222 | |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3223 | /// \brief Determine whether the given type is an incomplete or zero-lenfgth |
| 3224 | /// array type. |
| 3225 | static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) { |
| 3226 | if (T->isIncompleteArrayType()) |
| 3227 | return true; |
| 3228 | |
| 3229 | while (const ConstantArrayType *ArrayT = Context.getAsConstantArrayType(T)) { |
| 3230 | if (!ArrayT->getSize()) |
| 3231 | return true; |
| 3232 | |
| 3233 | T = ArrayT->getElementType(); |
| 3234 | } |
| 3235 | |
| 3236 | return false; |
| 3237 | } |
| 3238 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3239 | static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3240 | FieldDecl *Field, |
| 3241 | IndirectFieldDecl *Indirect = 0) { |
Eli Friedman | 5fb478b | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 3242 | if (Field->isInvalidDecl()) |
| 3243 | return false; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3244 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 3245 | // Overwhelmingly common case: we have a direct initializer for this field. |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3246 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(Field)) |
| 3247 | return Info.addFieldInitializer(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3248 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3249 | // C++11 [class.base.init]p8: if the entity is a non-static data member that |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3250 | // has a brace-or-equal-initializer, the entity is initialized as specified |
| 3251 | // in [dcl.init]. |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3252 | if (Field->hasInClassInitializer() && !Info.isImplicitCopyOrMove()) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3253 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, |
| 3254 | Info.Ctor->getLocation(), Field); |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3255 | CXXCtorInitializer *Init; |
| 3256 | if (Indirect) |
| 3257 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 3258 | SourceLocation(), |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3259 | SourceLocation(), DIE, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3260 | SourceLocation()); |
| 3261 | else |
| 3262 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 3263 | SourceLocation(), |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3264 | SourceLocation(), DIE, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3265 | SourceLocation()); |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3266 | return Info.addFieldInitializer(Init); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3267 | } |
| 3268 | |
Richard Smith | c115f63 | 2011-09-18 11:14:50 +0000 | [diff] [blame] | 3269 | // Don't build an implicit initializer for union members if none was |
| 3270 | // explicitly specified. |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 3271 | if (Field->getParent()->isUnion() || |
| 3272 | (Indirect && isWithinAnonymousUnion(Indirect))) |
Richard Smith | c115f63 | 2011-09-18 11:14:50 +0000 | [diff] [blame] | 3273 | return false; |
| 3274 | |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3275 | // Don't initialize incomplete or zero-length arrays. |
| 3276 | if (isIncompleteOrZeroLengthArrayType(SemaRef.Context, Field->getType())) |
| 3277 | return false; |
| 3278 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3279 | // Don't try to build an implicit initializer if there were semantic |
| 3280 | // errors in any of the initializers (and therefore we might be |
| 3281 | // missing some that the user actually wrote). |
Eli Friedman | 5fb478b | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 3282 | if (Info.AnyErrorsInInits) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3283 | return false; |
| 3284 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3285 | CXXCtorInitializer *Init = 0; |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3286 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, |
| 3287 | Indirect, Init)) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3288 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3289 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3290 | if (!Init) |
| 3291 | return false; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3292 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3293 | return Info.addFieldInitializer(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3294 | } |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3295 | |
| 3296 | bool |
| 3297 | Sema::SetDelegatingInitializer(CXXConstructorDecl *Constructor, |
| 3298 | CXXCtorInitializer *Initializer) { |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3299 | assert(Initializer->isDelegatingInitializer()); |
Sean Hunt | 01aacc0 | 2011-05-03 20:43:02 +0000 | [diff] [blame] | 3300 | Constructor->setNumCtorInitializers(1); |
| 3301 | CXXCtorInitializer **initializer = |
| 3302 | new (Context) CXXCtorInitializer*[1]; |
| 3303 | memcpy(initializer, &Initializer, sizeof (CXXCtorInitializer*)); |
| 3304 | Constructor->setCtorInitializers(initializer); |
| 3305 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3306 | if (CXXDestructorDecl *Dtor = LookupDestructor(Constructor->getParent())) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3307 | MarkFunctionReferenced(Initializer->getSourceLocation(), Dtor); |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3308 | DiagnoseUseOfDecl(Dtor, Initializer->getSourceLocation()); |
| 3309 | } |
| 3310 | |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 3311 | DelegatingCtorDecls.push_back(Constructor); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3312 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3313 | return false; |
| 3314 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3315 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3316 | bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, bool AnyErrors, |
| 3317 | ArrayRef<CXXCtorInitializer *> Initializers) { |
Douglas Gregor | d836c0d | 2011-09-22 23:04:35 +0000 | [diff] [blame] | 3318 | if (Constructor->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3319 | // Just store the initializers as written, they will be checked during |
| 3320 | // instantiation. |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3321 | if (!Initializers.empty()) { |
| 3322 | Constructor->setNumCtorInitializers(Initializers.size()); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3323 | CXXCtorInitializer **baseOrMemberInitializers = |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3324 | new (Context) CXXCtorInitializer*[Initializers.size()]; |
| 3325 | memcpy(baseOrMemberInitializers, Initializers.data(), |
| 3326 | Initializers.size() * sizeof(CXXCtorInitializer*)); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3327 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3328 | } |
Richard Smith | 54b3ba8 | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 3329 | |
| 3330 | // Let template instantiation know whether we had errors. |
| 3331 | if (AnyErrors) |
| 3332 | Constructor->setInvalidDecl(); |
| 3333 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3334 | return false; |
| 3335 | } |
| 3336 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3337 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3338 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3339 | // We need to build the initializer AST according to order of construction |
| 3340 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3341 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 3342 | if (!ClassDecl) |
| 3343 | return true; |
| 3344 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3345 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3346 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3347 | for (unsigned i = 0; i < Initializers.size(); i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3348 | CXXCtorInitializer *Member = Initializers[i]; |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3349 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3350 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3351 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3352 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3353 | Info.AllBaseFields[Member->getAnyMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3356 | // Keep track of the direct virtual bases. |
| 3357 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 3358 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 3359 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 3360 | if (I->isVirtual()) |
| 3361 | DirectVBases.insert(I); |
| 3362 | } |
| 3363 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3364 | // Push virtual bases before others. |
| 3365 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 3366 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 3367 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3368 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3369 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3370 | // [class.base.init]p7, per DR257: |
| 3371 | // A mem-initializer where the mem-initializer-id names a virtual base |
| 3372 | // class is ignored during execution of a constructor of any class that |
| 3373 | // is not the most derived class. |
| 3374 | if (ClassDecl->isAbstract()) { |
| 3375 | // FIXME: Provide a fixit to remove the base specifier. This requires |
| 3376 | // tracking the location of the associated comma for a base specifier. |
| 3377 | Diag(Value->getSourceLocation(), diag::warn_abstract_vbase_init_ignored) |
| 3378 | << VBase->getType() << ClassDecl; |
| 3379 | DiagnoseAbstractType(ClassDecl); |
| 3380 | } |
| 3381 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3382 | Info.AllToInit.push_back(Value); |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3383 | } else if (!AnyErrors && !ClassDecl->isAbstract()) { |
| 3384 | // [class.base.init]p8, per DR257: |
| 3385 | // If a given [...] base class is not named by a mem-initializer-id |
| 3386 | // [...] and the entity is not a virtual base class of an abstract |
| 3387 | // class, then [...] the entity is default-initialized. |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3388 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3389 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3390 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3391 | VBase, IsInheritedVirtualBase, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3392 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3393 | HadError = true; |
| 3394 | continue; |
| 3395 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3396 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3397 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3398 | } |
| 3399 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3401 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3402 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3403 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 3404 | // Virtuals are in the virtual base list and already constructed. |
| 3405 | if (Base->isVirtual()) |
| 3406 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3407 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3408 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3409 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 3410 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3411 | } else if (!AnyErrors) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3412 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3413 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3414 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3415 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3416 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3417 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3418 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 3419 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3420 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3421 | } |
| 3422 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3423 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3424 | // Fields. |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3425 | for (DeclContext::decl_iterator Mem = ClassDecl->decls_begin(), |
| 3426 | MemEnd = ClassDecl->decls_end(); |
| 3427 | Mem != MemEnd; ++Mem) { |
| 3428 | if (FieldDecl *F = dyn_cast<FieldDecl>(*Mem)) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3429 | // C++ [class.bit]p2: |
| 3430 | // A declaration for a bit-field that omits the identifier declares an |
| 3431 | // unnamed bit-field. Unnamed bit-fields are not members and cannot be |
| 3432 | // initialized. |
| 3433 | if (F->isUnnamedBitfield()) |
| 3434 | continue; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3435 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3436 | // If we're not generating the implicit copy/move constructor, then we'll |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3437 | // handle anonymous struct/union fields based on their individual |
| 3438 | // indirect fields. |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3439 | if (F->isAnonymousStructOrUnion() && !Info.isImplicitCopyOrMove()) |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3440 | continue; |
| 3441 | |
| 3442 | if (CollectFieldInitializer(*this, Info, F)) |
| 3443 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 3444 | continue; |
| 3445 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3446 | |
| 3447 | // Beyond this point, we only consider default initialization. |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3448 | if (Info.isImplicitCopyOrMove()) |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3449 | continue; |
| 3450 | |
| 3451 | if (IndirectFieldDecl *F = dyn_cast<IndirectFieldDecl>(*Mem)) { |
| 3452 | if (F->getType()->isIncompleteArrayType()) { |
| 3453 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 3454 | "Incomplete array type is not valid"); |
| 3455 | continue; |
| 3456 | } |
| 3457 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3458 | // Initialize each field of an anonymous struct individually. |
| 3459 | if (CollectFieldInitializer(*this, Info, F->getAnonField(), F)) |
| 3460 | HadError = true; |
| 3461 | |
| 3462 | continue; |
| 3463 | } |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 3464 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3465 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3466 | unsigned NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3467 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3468 | Constructor->setNumCtorInitializers(NumInitializers); |
| 3469 | CXXCtorInitializer **baseOrMemberInitializers = |
| 3470 | new (Context) CXXCtorInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3471 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3472 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 3473 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 3474 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3475 | // Constructors implicitly reference the base and member |
| 3476 | // destructors. |
| 3477 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 3478 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3479 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3480 | |
| 3481 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3484 | static void PopulateKeysForFields(FieldDecl *Field, SmallVectorImpl<const void*> &IdealInits) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3485 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3486 | const RecordDecl *RD = RT->getDecl(); |
| 3487 | if (RD->isAnonymousStructOrUnion()) { |
| 3488 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3489 | E = RD->field_end(); Field != E; ++Field) |
| 3490 | PopulateKeysForFields(*Field, IdealInits); |
| 3491 | return; |
| 3492 | } |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3493 | } |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3494 | IdealInits.push_back(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3497 | static const void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 3498 | return Context.getCanonicalType(BaseType).getTypePtr(); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3501 | static const void *GetKeyForMember(ASTContext &Context, |
| 3502 | CXXCtorInitializer *Member) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3503 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3504 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 3505 | |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3506 | return Member->getAnyMember(); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3509 | static void DiagnoseBaseOrMemInitializerOrder( |
| 3510 | Sema &SemaRef, const CXXConstructorDecl *Constructor, |
| 3511 | ArrayRef<CXXCtorInitializer *> Inits) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3512 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 3513 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3514 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3515 | // Don't check initializers order unless the warning is enabled at the |
| 3516 | // location of at least one initializer. |
| 3517 | bool ShouldCheckOrder = false; |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3518 | for (unsigned InitIndex = 0; InitIndex != Inits.size(); ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3519 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3520 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order, |
| 3521 | Init->getSourceLocation()) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 3522 | != DiagnosticsEngine::Ignored) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3523 | ShouldCheckOrder = true; |
| 3524 | break; |
| 3525 | } |
| 3526 | } |
| 3527 | if (!ShouldCheckOrder) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3528 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3529 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3530 | // Build the list of bases and members in the order that they'll |
| 3531 | // actually be initialized. The explicit initializers should be in |
| 3532 | // this same order but may be missing things. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3533 | SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3534 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3535 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3536 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3537 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3538 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3539 | ClassDecl->vbases_begin(), |
| 3540 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3541 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3542 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3543 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3544 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3545 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3546 | if (Base->isVirtual()) |
| 3547 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3548 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3549 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3550 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3551 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3552 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3553 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 3554 | if (Field->isUnnamedBitfield()) |
| 3555 | continue; |
| 3556 | |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3557 | PopulateKeysForFields(*Field, IdealInitKeys); |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3560 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 3561 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3562 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3563 | CXXCtorInitializer *PrevInit = 0; |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3564 | for (unsigned InitIndex = 0; InitIndex != Inits.size(); ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3565 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3566 | const void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3567 | |
| 3568 | // Scan forward to try to find this initializer in the idealized |
| 3569 | // initializers list. |
| 3570 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3571 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3572 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3573 | |
| 3574 | // If we didn't find this initializer, it must be because we |
| 3575 | // scanned past it on a previous iteration. That can only |
| 3576 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 3577 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3578 | Sema::SemaDiagnosticBuilder D = |
| 3579 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 3580 | diag::warn_initializer_out_of_order); |
| 3581 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3582 | if (PrevInit->isAnyMemberInitializer()) |
| 3583 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3584 | else |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3585 | D << 1 << PrevInit->getTypeSourceInfo()->getType(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3586 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3587 | if (Init->isAnyMemberInitializer()) |
| 3588 | D << 0 << Init->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3589 | else |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3590 | D << 1 << Init->getTypeSourceInfo()->getType(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3591 | |
| 3592 | // Move back to the initializer's location in the ideal list. |
| 3593 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3594 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3595 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3596 | |
| 3597 | assert(IdealIndex != NumIdealInits && |
| 3598 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3599 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3600 | |
| 3601 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3602 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3605 | namespace { |
| 3606 | bool CheckRedundantInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3607 | CXXCtorInitializer *Init, |
| 3608 | CXXCtorInitializer *&PrevInit) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3609 | if (!PrevInit) { |
| 3610 | PrevInit = Init; |
| 3611 | return false; |
| 3612 | } |
| 3613 | |
Douglas Gregor | dc392c1 | 2013-03-25 23:28:23 +0000 | [diff] [blame] | 3614 | if (FieldDecl *Field = Init->getAnyMember()) |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3615 | S.Diag(Init->getSourceLocation(), |
| 3616 | diag::err_multiple_mem_initialization) |
| 3617 | << Field->getDeclName() |
| 3618 | << Init->getSourceRange(); |
| 3619 | else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3620 | const Type *BaseClass = Init->getBaseClass(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3621 | assert(BaseClass && "neither field nor base"); |
| 3622 | S.Diag(Init->getSourceLocation(), |
| 3623 | diag::err_multiple_base_initialization) |
| 3624 | << QualType(BaseClass, 0) |
| 3625 | << Init->getSourceRange(); |
| 3626 | } |
| 3627 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 3628 | << 0 << PrevInit->getSourceRange(); |
| 3629 | |
| 3630 | return true; |
| 3631 | } |
| 3632 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3633 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3634 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 3635 | |
| 3636 | bool CheckRedundantUnionInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3637 | CXXCtorInitializer *Init, |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3638 | RedundantUnionMap &Unions) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3639 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3640 | RecordDecl *Parent = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3641 | NamedDecl *Child = Field; |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3642 | |
| 3643 | while (Parent->isAnonymousStructOrUnion() || Parent->isUnion()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3644 | if (Parent->isUnion()) { |
| 3645 | UnionEntry &En = Unions[Parent]; |
| 3646 | if (En.first && En.first != Child) { |
| 3647 | S.Diag(Init->getSourceLocation(), |
| 3648 | diag::err_multiple_mem_union_initialization) |
| 3649 | << Field->getDeclName() |
| 3650 | << Init->getSourceRange(); |
| 3651 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 3652 | << 0 << En.second->getSourceRange(); |
| 3653 | return true; |
David Blaikie | 5bbe816 | 2011-11-12 20:54:14 +0000 | [diff] [blame] | 3654 | } |
| 3655 | if (!En.first) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3656 | En.first = Child; |
| 3657 | En.second = Init; |
| 3658 | } |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3659 | if (!Parent->isAnonymousStructOrUnion()) |
| 3660 | return false; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3661 | } |
| 3662 | |
| 3663 | Child = Parent; |
| 3664 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3665 | } |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3666 | |
| 3667 | return false; |
| 3668 | } |
| 3669 | } |
| 3670 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3671 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3672 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3673 | SourceLocation ColonLoc, |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3674 | ArrayRef<CXXCtorInitializer*> MemInits, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3675 | bool AnyErrors) { |
| 3676 | if (!ConstructorDecl) |
| 3677 | return; |
| 3678 | |
| 3679 | AdjustDeclIfTemplate(ConstructorDecl); |
| 3680 | |
| 3681 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3682 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3683 | |
| 3684 | if (!Constructor) { |
| 3685 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 3686 | return; |
| 3687 | } |
| 3688 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3689 | // Mapping for the duplicate initializers check. |
| 3690 | // For member initializers, this is keyed with a FieldDecl*. |
| 3691 | // For base initializers, this is keyed with a Type*. |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3692 | llvm::DenseMap<const void *, CXXCtorInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3693 | |
| 3694 | // Mapping for the inconsistent anonymous-union initializers check. |
| 3695 | RedundantUnionMap MemberUnions; |
| 3696 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3697 | bool HadError = false; |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3698 | for (unsigned i = 0; i < MemInits.size(); i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3699 | CXXCtorInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3700 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 3701 | // Set the source order index. |
| 3702 | Init->setSourceOrder(i); |
| 3703 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3704 | if (Init->isAnyMemberInitializer()) { |
| 3705 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3706 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 3707 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 3708 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3709 | } else if (Init->isBaseInitializer()) { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3710 | const void *Key = |
| 3711 | GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3712 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 3713 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3714 | } else { |
| 3715 | assert(Init->isDelegatingInitializer()); |
| 3716 | // This must be the only initializer |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3717 | if (MemInits.size() != 1) { |
Richard Smith | a6ddea6 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 3718 | Diag(Init->getSourceLocation(), |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3719 | diag::err_delegating_initializer_alone) |
Richard Smith | a6ddea6 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 3720 | << Init->getSourceRange() << MemInits[i ? 0 : 1]->getSourceRange(); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3721 | // We will treat this as being the only initializer. |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3722 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3723 | SetDelegatingInitializer(Constructor, MemInits[i]); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3724 | // Return immediately as the initializer is set. |
| 3725 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3726 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3729 | if (HadError) |
| 3730 | return; |
| 3731 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3732 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 3733 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3734 | SetCtorInitializers(Constructor, AnyErrors, MemInits); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3737 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3738 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 3739 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 416f63e | 2011-09-18 12:11:43 +0000 | [diff] [blame] | 3740 | // Ignore dependent contexts. Also ignore unions, since their members never |
| 3741 | // have destructors implicitly called. |
| 3742 | if (ClassDecl->isDependentContext() || ClassDecl->isUnion()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3743 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3744 | |
| 3745 | // FIXME: all the access-control diagnostics are positioned on the |
| 3746 | // field/base declaration. That's probably good; that said, the |
| 3747 | // user might reasonably want to know why the destructor is being |
| 3748 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3749 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3750 | // Non-static data members. |
| 3751 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 3752 | E = ClassDecl->field_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3753 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 3754 | if (Field->isInvalidDecl()) |
| 3755 | continue; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3756 | |
| 3757 | // Don't destroy incomplete or zero-length arrays. |
| 3758 | if (isIncompleteOrZeroLengthArrayType(Context, Field->getType())) |
| 3759 | continue; |
| 3760 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3761 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 3762 | |
| 3763 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 3764 | if (!RT) |
| 3765 | continue; |
| 3766 | |
| 3767 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3768 | if (FieldClassDecl->isInvalidDecl()) |
| 3769 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3770 | if (FieldClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3771 | continue; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 3772 | // The destructor for an implicit anonymous union member is never invoked. |
| 3773 | if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion()) |
| 3774 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3775 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3776 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3777 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3778 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3779 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3780 | << Field->getDeclName() |
| 3781 | << FieldType); |
| 3782 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3783 | MarkFunctionReferenced(Location, Dtor); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3784 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3787 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 3788 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3789 | // Bases. |
| 3790 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3791 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3792 | // Bases are always records in a well-formed non-dependent class. |
| 3793 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 3794 | |
| 3795 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3796 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3797 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3798 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3799 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3800 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 3801 | if (BaseClassDecl->isInvalidDecl()) |
| 3802 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3803 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3804 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3805 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3806 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3807 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3808 | |
| 3809 | // FIXME: caret should be on the start of the class name |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3810 | CheckDestructorAccess(Base->getLocStart(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3811 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3812 | << Base->getType() |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 3813 | << Base->getSourceRange(), |
| 3814 | Context.getTypeDeclType(ClassDecl)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3815 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3816 | MarkFunctionReferenced(Location, Dtor); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3817 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3821 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 3822 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3823 | |
| 3824 | // Bases are always records in a well-formed non-dependent class. |
John McCall | 63f5578 | 2012-04-09 21:51:56 +0000 | [diff] [blame] | 3825 | const RecordType *RT = VBase->getType()->castAs<RecordType>(); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3826 | |
| 3827 | // Ignore direct virtual bases. |
| 3828 | if (DirectVirtualBases.count(RT)) |
| 3829 | continue; |
| 3830 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3831 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3832 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 3833 | if (BaseClassDecl->isInvalidDecl()) |
| 3834 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3835 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3836 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3837 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3838 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3839 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
David Majnemer | 2f68669 | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 3840 | if (CheckDestructorAccess( |
| 3841 | ClassDecl->getLocation(), Dtor, |
| 3842 | PDiag(diag::err_access_dtor_vbase) |
| 3843 | << Context.getTypeDeclType(ClassDecl) << VBase->getType(), |
| 3844 | Context.getTypeDeclType(ClassDecl)) == |
| 3845 | AR_accessible) { |
| 3846 | CheckDerivedToBaseConversion( |
| 3847 | Context.getTypeDeclType(ClassDecl), VBase->getType(), |
| 3848 | diag::err_access_dtor_vbase, 0, ClassDecl->getLocation(), |
| 3849 | SourceRange(), DeclarationName(), 0); |
| 3850 | } |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3851 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3852 | MarkFunctionReferenced(Location, Dtor); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3853 | DiagnoseUseOfDecl(Dtor, Location); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3854 | } |
| 3855 | } |
| 3856 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3857 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 3858 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 3859 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3860 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3861 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3862 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3863 | SetCtorInitializers(Constructor, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 3864 | } |
| 3865 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3866 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3867 | unsigned DiagID, AbstractDiagSelID SelID) { |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3868 | class NonAbstractTypeDiagnoser : public TypeDiagnoser { |
| 3869 | unsigned DiagID; |
| 3870 | AbstractDiagSelID SelID; |
| 3871 | |
| 3872 | public: |
| 3873 | NonAbstractTypeDiagnoser(unsigned DiagID, AbstractDiagSelID SelID) |
| 3874 | : TypeDiagnoser(DiagID == 0), DiagID(DiagID), SelID(SelID) { } |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3875 | |
| 3876 | void diagnose(Sema &S, SourceLocation Loc, QualType T) LLVM_OVERRIDE { |
Eli Friedman | 2217f85 | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 3877 | if (Suppressed) return; |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3878 | if (SelID == -1) |
| 3879 | S.Diag(Loc, DiagID) << T; |
| 3880 | else |
| 3881 | S.Diag(Loc, DiagID) << SelID << T; |
| 3882 | } |
| 3883 | } Diagnoser(DiagID, SelID); |
| 3884 | |
| 3885 | return RequireNonAbstractType(Loc, T, Diagnoser); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3886 | } |
| 3887 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 3888 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3889 | TypeDiagnoser &Diagnoser) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3890 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3891 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 3893 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3894 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3895 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3896 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3897 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3898 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3899 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3900 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3901 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3902 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3903 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3904 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3905 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3906 | if (!RT) |
| 3907 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3908 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3909 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3910 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3911 | // We can't answer whether something is abstract until it has a |
| 3912 | // definition. If it's currently being defined, we'll walk back |
| 3913 | // over all the declarations when we have a full definition. |
| 3914 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 3915 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3916 | return false; |
| 3917 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3918 | if (!RD->isAbstract()) |
| 3919 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3920 | |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3921 | Diagnoser.diagnose(*this, Loc, T); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3922 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3923 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3924 | return true; |
| 3925 | } |
| 3926 | |
| 3927 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 3928 | // Check if we've already emitted the list of pure virtual functions |
| 3929 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3930 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3931 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3932 | |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3933 | // If the diagnostic is suppressed, don't emit the notes. We're only |
| 3934 | // going to emit them once, so try to attach them to a diagnostic we're |
| 3935 | // actually going to show. |
| 3936 | if (Diags.isLastDiagnosticIgnored()) |
| 3937 | return; |
| 3938 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3939 | CXXFinalOverriderMap FinalOverriders; |
| 3940 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3941 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 3942 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 3943 | // more than once. |
| 3944 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 3945 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3946 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 3947 | MEnd = FinalOverriders.end(); |
| 3948 | M != MEnd; |
| 3949 | ++M) { |
| 3950 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 3951 | SOEnd = M->second.end(); |
| 3952 | SO != SOEnd; ++SO) { |
| 3953 | // C++ [class.abstract]p4: |
| 3954 | // A class is abstract if it contains or inherits at least one |
| 3955 | // pure virtual function for which the final overrider is pure |
| 3956 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3957 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3958 | // |
| 3959 | if (SO->second.size() != 1) |
| 3960 | continue; |
| 3961 | |
| 3962 | if (!SO->second.front().Method->isPure()) |
| 3963 | continue; |
| 3964 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 3965 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 3966 | continue; |
| 3967 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3968 | Diag(SO->second.front().Method->getLocation(), |
| 3969 | diag::note_pure_virtual_function) |
Chandler Carruth | 45f11b7 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 3970 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3971 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
| 3974 | if (!PureVirtualClassDiagSet) |
| 3975 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 3976 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3979 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3980 | struct AbstractUsageInfo { |
| 3981 | Sema &S; |
| 3982 | CXXRecordDecl *Record; |
| 3983 | CanQualType AbstractType; |
| 3984 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3985 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3986 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 3987 | : S(S), Record(Record), |
| 3988 | AbstractType(S.Context.getCanonicalType( |
| 3989 | S.Context.getTypeDeclType(Record))), |
| 3990 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3991 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3992 | void DiagnoseAbstractType() { |
| 3993 | if (Invalid) return; |
| 3994 | S.DiagnoseAbstractType(Record); |
| 3995 | Invalid = true; |
| 3996 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3997 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3998 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 3999 | }; |
| 4000 | |
| 4001 | struct CheckAbstractUsage { |
| 4002 | AbstractUsageInfo &Info; |
| 4003 | const NamedDecl *Ctx; |
| 4004 | |
| 4005 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 4006 | : Info(Info), Ctx(Ctx) {} |
| 4007 | |
| 4008 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4009 | switch (TL.getTypeLocClass()) { |
| 4010 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 4011 | #define TYPELOC(CLASS, PARENT) \ |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4012 | case TypeLoc::CLASS: Check(TL.castAs<CLASS##TypeLoc>(), Sel); break; |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4013 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4014 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4015 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4016 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4017 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4018 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 4019 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
Douglas Gregor | 7019186 | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 4020 | if (!TL.getArg(I)) |
| 4021 | continue; |
| 4022 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4023 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 4024 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4025 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4026 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4027 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4028 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4029 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 4030 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4031 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4032 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4033 | // Visit the type parameters from a permissive context. |
| 4034 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 4035 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 4036 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 4037 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 4038 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 4039 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4040 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4041 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4042 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4043 | // Visit pointee types from a permissive context. |
| 4044 | #define CheckPolymorphic(Type) \ |
| 4045 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 4046 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 4047 | } |
| 4048 | CheckPolymorphic(PointerTypeLoc) |
| 4049 | CheckPolymorphic(ReferenceTypeLoc) |
| 4050 | CheckPolymorphic(MemberPointerTypeLoc) |
| 4051 | CheckPolymorphic(BlockPointerTypeLoc) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4052 | CheckPolymorphic(AtomicTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4053 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4054 | /// Handle all the types we haven't given a more specific |
| 4055 | /// implementation for above. |
| 4056 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4057 | // Every other kind of type that we haven't called out already |
| 4058 | // that has an inner type is either (1) sugar or (2) contains that |
| 4059 | // inner type in some way as a subobject. |
| 4060 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 4061 | return Visit(Next, Sel); |
| 4062 | |
| 4063 | // If there's no inner type and we're in a permissive context, |
| 4064 | // don't diagnose. |
| 4065 | if (Sel == Sema::AbstractNone) return; |
| 4066 | |
| 4067 | // Check whether the type matches the abstract type. |
| 4068 | QualType T = TL.getType(); |
| 4069 | if (T->isArrayType()) { |
| 4070 | Sel = Sema::AbstractArrayType; |
| 4071 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4072 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4073 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 4074 | if (CT != Info.AbstractType) return; |
| 4075 | |
| 4076 | // It matched; do some magic. |
| 4077 | if (Sel == Sema::AbstractArrayType) { |
| 4078 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 4079 | << T << TL.getSourceRange(); |
| 4080 | } else { |
| 4081 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 4082 | << Sel << T << TL.getSourceRange(); |
| 4083 | } |
| 4084 | Info.DiagnoseAbstractType(); |
| 4085 | } |
| 4086 | }; |
| 4087 | |
| 4088 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 4089 | Sema::AbstractDiagSelID Sel) { |
| 4090 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 4091 | } |
| 4092 | |
| 4093 | } |
| 4094 | |
| 4095 | /// Check for invalid uses of an abstract type in a method declaration. |
| 4096 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 4097 | CXXMethodDecl *MD) { |
| 4098 | // No need to do the check on definitions, which require that |
| 4099 | // the return/param types be complete. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 4100 | if (MD->doesThisDeclarationHaveABody()) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4101 | return; |
| 4102 | |
| 4103 | // For safety's sake, just ignore it if we don't have type source |
| 4104 | // information. This should never happen for non-implicit methods, |
| 4105 | // but... |
| 4106 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 4107 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 4108 | } |
| 4109 | |
| 4110 | /// Check for invalid uses of an abstract type within a class definition. |
| 4111 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 4112 | CXXRecordDecl *RD) { |
| 4113 | for (CXXRecordDecl::decl_iterator |
| 4114 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 4115 | Decl *D = *I; |
| 4116 | if (D->isImplicit()) continue; |
| 4117 | |
| 4118 | // Methods and method templates. |
| 4119 | if (isa<CXXMethodDecl>(D)) { |
| 4120 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 4121 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 4122 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 4123 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 4124 | |
| 4125 | // Fields and static variables. |
| 4126 | } else if (isa<FieldDecl>(D)) { |
| 4127 | FieldDecl *FD = cast<FieldDecl>(D); |
| 4128 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 4129 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 4130 | } else if (isa<VarDecl>(D)) { |
| 4131 | VarDecl *VD = cast<VarDecl>(D); |
| 4132 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 4133 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 4134 | |
| 4135 | // Nested classes and class templates. |
| 4136 | } else if (isa<CXXRecordDecl>(D)) { |
| 4137 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 4138 | } else if (isa<ClassTemplateDecl>(D)) { |
| 4139 | CheckAbstractClassUsage(Info, |
| 4140 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 4141 | } |
| 4142 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4145 | /// \brief Perform semantic checks on a class definition that has been |
| 4146 | /// completing, introducing implicitly-declared members, checking for |
| 4147 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4148 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 4149 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4150 | return; |
| 4151 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4152 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 4153 | AbstractUsageInfo Info(*this, Record); |
| 4154 | CheckAbstractClassUsage(Info, Record); |
| 4155 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4156 | |
| 4157 | // If this is not an aggregate type and has no user-declared constructor, |
| 4158 | // complain about any non-static data members of reference or const scalar |
| 4159 | // type, since they will never get initializers. |
| 4160 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
Douglas Gregor | 5e058eb | 2012-02-09 02:20:38 +0000 | [diff] [blame] | 4161 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor() && |
| 4162 | !Record->isLambda()) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4163 | bool Complained = false; |
| 4164 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 4165 | FEnd = Record->field_end(); |
| 4166 | F != FEnd; ++F) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 4167 | if (F->hasInClassInitializer() || F->isUnnamedBitfield()) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4168 | continue; |
| 4169 | |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4170 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 4171 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4172 | if (!Complained) { |
| 4173 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 4174 | << Record->getTagKind() << Record; |
| 4175 | Complained = true; |
| 4176 | } |
| 4177 | |
| 4178 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 4179 | << F->getType()->isReferenceType() |
| 4180 | << F->getDeclName(); |
| 4181 | } |
| 4182 | } |
| 4183 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4184 | |
Anders Carlsson | a5c6c2a | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 4185 | if (Record->isDynamicClass() && !Record->isDependentType()) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4186 | DynamicClasses.push_back(Record); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4187 | |
| 4188 | if (Record->getIdentifier()) { |
| 4189 | // C++ [class.mem]p13: |
| 4190 | // If T is the name of a class, then each of the following shall have a |
| 4191 | // name different from T: |
| 4192 | // - every member of every anonymous union that is a member of class T. |
| 4193 | // |
| 4194 | // C++ [class.mem]p14: |
| 4195 | // In addition, if class T has a user-declared constructor (12.1), every |
| 4196 | // non-static data member of class T shall have a name different from T. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4197 | DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
| 4198 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 4199 | ++I) { |
| 4200 | NamedDecl *D = *I; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4201 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 4202 | isa<IndirectFieldDecl>(D)) { |
| 4203 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 4204 | << D->getDeclName(); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4205 | break; |
| 4206 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4207 | } |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4208 | } |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4209 | |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 4210 | // Warn if the class has virtual methods but non-virtual public destructor. |
Douglas Gregor | f4b793c | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 4211 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4212 | CXXDestructorDecl *dtor = Record->getDestructor(); |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 4213 | if (!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4214 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 4215 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 4216 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4217 | |
David Blaikie | b6b5b97 | 2012-09-21 03:21:07 +0000 | [diff] [blame] | 4218 | if (Record->isAbstract() && Record->hasAttr<FinalAttr>()) { |
| 4219 | Diag(Record->getLocation(), diag::warn_abstract_final_class); |
| 4220 | DiagnoseAbstractType(Record); |
| 4221 | } |
| 4222 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4223 | if (!Record->isDependentType()) { |
| 4224 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 4225 | MEnd = Record->method_end(); |
| 4226 | M != MEnd; ++M) { |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4227 | // See if a method overloads virtual methods in a base |
| 4228 | // class without overriding any. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4229 | if (!M->isStatic()) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4230 | DiagnoseHiddenVirtualMethods(Record, *M); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4231 | |
| 4232 | // Check whether the explicitly-defaulted special members are valid. |
| 4233 | if (!M->isInvalidDecl() && M->isExplicitlyDefaulted()) |
| 4234 | CheckExplicitlyDefaultedSpecialMember(*M); |
| 4235 | |
| 4236 | // For an explicitly defaulted or deleted special member, we defer |
| 4237 | // determining triviality until the class is complete. That time is now! |
| 4238 | if (!M->isImplicit() && !M->isUserProvided()) { |
| 4239 | CXXSpecialMember CSM = getSpecialMember(*M); |
| 4240 | if (CSM != CXXInvalid) { |
| 4241 | M->setTrivial(SpecialMemberIsTrivial(*M, CSM)); |
| 4242 | |
| 4243 | // Inform the class that we've finished declaring this member. |
| 4244 | Record->finishedDefaultedOrDeletedMember(*M); |
| 4245 | } |
| 4246 | } |
| 4247 | } |
| 4248 | } |
| 4249 | |
| 4250 | // C++11 [dcl.constexpr]p8: A constexpr specifier for a non-static member |
| 4251 | // function that is not a constructor declares that member function to be |
| 4252 | // const. [...] The class of which that function is a member shall be |
| 4253 | // a literal type. |
| 4254 | // |
| 4255 | // If the class has virtual bases, any constexpr members will already have |
| 4256 | // been diagnosed by the checks performed on the member declaration, so |
| 4257 | // suppress this (less useful) diagnostic. |
| 4258 | // |
| 4259 | // We delay this until we know whether an explicitly-defaulted (or deleted) |
| 4260 | // destructor for the class is trivial. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4261 | if (LangOpts.CPlusPlus11 && !Record->isDependentType() && |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4262 | !Record->isLiteral() && !Record->getNumVBases()) { |
| 4263 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 4264 | MEnd = Record->method_end(); |
| 4265 | M != MEnd; ++M) { |
| 4266 | if (M->isConstexpr() && M->isInstance() && !isa<CXXConstructorDecl>(*M)) { |
| 4267 | switch (Record->getTemplateSpecializationKind()) { |
| 4268 | case TSK_ImplicitInstantiation: |
| 4269 | case TSK_ExplicitInstantiationDeclaration: |
| 4270 | case TSK_ExplicitInstantiationDefinition: |
| 4271 | // If a template instantiates to a non-literal type, but its members |
| 4272 | // instantiate to constexpr functions, the template is technically |
| 4273 | // ill-formed, but we allow it for sanity. |
| 4274 | continue; |
| 4275 | |
| 4276 | case TSK_Undeclared: |
| 4277 | case TSK_ExplicitSpecialization: |
| 4278 | RequireLiteralType(M->getLocation(), Context.getRecordType(Record), |
| 4279 | diag::err_constexpr_method_non_literal); |
| 4280 | break; |
| 4281 | } |
| 4282 | |
| 4283 | // Only produce one error per class. |
| 4284 | break; |
| 4285 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4286 | } |
| 4287 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4288 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4289 | // Declare inheriting constructors. We do this eagerly here because: |
| 4290 | // - The standard requires an eager diagnostic for conflicting inheriting |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4291 | // constructors from different classes. |
| 4292 | // - The lazy declaration of the other implicit constructors is so as to not |
| 4293 | // waste space and performance on classes that are not meant to be |
| 4294 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4295 | // have inheriting constructors. |
| 4296 | DeclareInheritingConstructors(Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4297 | } |
| 4298 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4299 | /// Is the special member function which would be selected to perform the |
| 4300 | /// specified operation on the specified class type a constexpr constructor? |
| 4301 | static bool specialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 4302 | Sema::CXXSpecialMember CSM, |
| 4303 | bool ConstArg) { |
| 4304 | Sema::SpecialMemberOverloadResult *SMOR = |
| 4305 | S.LookupSpecialMember(ClassDecl, CSM, ConstArg, |
| 4306 | false, false, false, false); |
| 4307 | if (!SMOR || !SMOR->getMethod()) |
| 4308 | // A constructor we wouldn't select can't be "involved in initializing" |
| 4309 | // anything. |
| 4310 | return true; |
| 4311 | return SMOR->getMethod()->isConstexpr(); |
| 4312 | } |
| 4313 | |
| 4314 | /// Determine whether the specified special member function would be constexpr |
| 4315 | /// if it were implicitly defined. |
| 4316 | static bool defaultedSpecialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 4317 | Sema::CXXSpecialMember CSM, |
| 4318 | bool ConstArg) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4319 | if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4320 | return false; |
| 4321 | |
| 4322 | // C++11 [dcl.constexpr]p4: |
| 4323 | // In the definition of a constexpr constructor [...] |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4324 | bool Ctor = true; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4325 | switch (CSM) { |
| 4326 | case Sema::CXXDefaultConstructor: |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4327 | // Since default constructor lookup is essentially trivial (and cannot |
| 4328 | // involve, for instance, template instantiation), we compute whether a |
| 4329 | // defaulted default constructor is constexpr directly within CXXRecordDecl. |
| 4330 | // |
| 4331 | // This is important for performance; we need to know whether the default |
| 4332 | // constructor is constexpr to determine whether the type is a literal type. |
| 4333 | return ClassDecl->defaultedDefaultConstructorIsConstexpr(); |
| 4334 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4335 | case Sema::CXXCopyConstructor: |
| 4336 | case Sema::CXXMoveConstructor: |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4337 | // For copy or move constructors, we need to perform overload resolution. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4338 | break; |
| 4339 | |
| 4340 | case Sema::CXXCopyAssignment: |
| 4341 | case Sema::CXXMoveAssignment: |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4342 | if (!S.getLangOpts().CPlusPlus1y) |
| 4343 | return false; |
| 4344 | // In C++1y, we need to perform overload resolution. |
| 4345 | Ctor = false; |
| 4346 | break; |
| 4347 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4348 | case Sema::CXXDestructor: |
| 4349 | case Sema::CXXInvalid: |
| 4350 | return false; |
| 4351 | } |
| 4352 | |
| 4353 | // -- if the class is a non-empty union, or for each non-empty anonymous |
| 4354 | // union member of a non-union class, exactly one non-static data member |
| 4355 | // shall be initialized; [DR1359] |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4356 | // |
| 4357 | // If we squint, this is guaranteed, since exactly one non-static data member |
| 4358 | // will be initialized (if the constructor isn't deleted), we just don't know |
| 4359 | // which one. |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4360 | if (Ctor && ClassDecl->isUnion()) |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4361 | return true; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4362 | |
| 4363 | // -- the class shall not have any virtual base classes; |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4364 | if (Ctor && ClassDecl->getNumVBases()) |
| 4365 | return false; |
| 4366 | |
| 4367 | // C++1y [class.copy]p26: |
| 4368 | // -- [the class] is a literal type, and |
| 4369 | if (!Ctor && !ClassDecl->isLiteral()) |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4370 | return false; |
| 4371 | |
| 4372 | // -- every constructor involved in initializing [...] base class |
| 4373 | // sub-objects shall be a constexpr constructor; |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4374 | // -- the assignment operator selected to copy/move each direct base |
| 4375 | // class is a constexpr function, and |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4376 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4377 | BEnd = ClassDecl->bases_end(); |
| 4378 | B != BEnd; ++B) { |
| 4379 | const RecordType *BaseType = B->getType()->getAs<RecordType>(); |
| 4380 | if (!BaseType) continue; |
| 4381 | |
| 4382 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4383 | if (!specialMemberIsConstexpr(S, BaseClassDecl, CSM, ConstArg)) |
| 4384 | return false; |
| 4385 | } |
| 4386 | |
| 4387 | // -- every constructor involved in initializing non-static data members |
| 4388 | // [...] shall be a constexpr constructor; |
| 4389 | // -- every non-static data member and base class sub-object shall be |
| 4390 | // initialized |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4391 | // -- for each non-stastic data member of X that is of class type (or array |
| 4392 | // thereof), the assignment operator selected to copy/move that member is |
| 4393 | // a constexpr function |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4394 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4395 | FEnd = ClassDecl->field_end(); |
| 4396 | F != FEnd; ++F) { |
| 4397 | if (F->isInvalidDecl()) |
| 4398 | continue; |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4399 | if (const RecordType *RecordTy = |
| 4400 | S.Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4401 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4402 | if (!specialMemberIsConstexpr(S, FieldRecDecl, CSM, ConstArg)) |
| 4403 | return false; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | // All OK, it's constexpr! |
| 4408 | return true; |
| 4409 | } |
| 4410 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4411 | static Sema::ImplicitExceptionSpecification |
| 4412 | computeImplicitExceptionSpec(Sema &S, SourceLocation Loc, CXXMethodDecl *MD) { |
| 4413 | switch (S.getSpecialMember(MD)) { |
| 4414 | case Sema::CXXDefaultConstructor: |
| 4415 | return S.ComputeDefaultedDefaultCtorExceptionSpec(Loc, MD); |
| 4416 | case Sema::CXXCopyConstructor: |
| 4417 | return S.ComputeDefaultedCopyCtorExceptionSpec(MD); |
| 4418 | case Sema::CXXCopyAssignment: |
| 4419 | return S.ComputeDefaultedCopyAssignmentExceptionSpec(MD); |
| 4420 | case Sema::CXXMoveConstructor: |
| 4421 | return S.ComputeDefaultedMoveCtorExceptionSpec(MD); |
| 4422 | case Sema::CXXMoveAssignment: |
| 4423 | return S.ComputeDefaultedMoveAssignmentExceptionSpec(MD); |
| 4424 | case Sema::CXXDestructor: |
| 4425 | return S.ComputeDefaultedDtorExceptionSpec(MD); |
| 4426 | case Sema::CXXInvalid: |
| 4427 | break; |
| 4428 | } |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4429 | assert(cast<CXXConstructorDecl>(MD)->getInheritedConstructor() && |
| 4430 | "only special members have implicit exception specs"); |
| 4431 | return S.ComputeInheritingCtorExceptionSpec(cast<CXXConstructorDecl>(MD)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4434 | static void |
| 4435 | updateExceptionSpec(Sema &S, FunctionDecl *FD, const FunctionProtoType *FPT, |
| 4436 | const Sema::ImplicitExceptionSpecification &ExceptSpec) { |
| 4437 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 4438 | ExceptSpec.getEPI(EPI); |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 4439 | FD->setType(S.Context.getFunctionType(FPT->getResultType(), |
| 4440 | FPT->getArgTypes(), EPI)); |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4441 | } |
| 4442 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4443 | void Sema::EvaluateImplicitExceptionSpec(SourceLocation Loc, CXXMethodDecl *MD) { |
| 4444 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 4445 | if (FPT->getExceptionSpecType() != EST_Unevaluated) |
| 4446 | return; |
| 4447 | |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4448 | // Evaluate the exception specification. |
| 4449 | ImplicitExceptionSpecification ExceptSpec = |
| 4450 | computeImplicitExceptionSpec(*this, Loc, MD); |
| 4451 | |
| 4452 | // Update the type of the special member to use it. |
| 4453 | updateExceptionSpec(*this, MD, FPT, ExceptSpec); |
| 4454 | |
| 4455 | // A user-provided destructor can be defined outside the class. When that |
| 4456 | // happens, be sure to update the exception specification on both |
| 4457 | // declarations. |
| 4458 | const FunctionProtoType *CanonicalFPT = |
| 4459 | MD->getCanonicalDecl()->getType()->castAs<FunctionProtoType>(); |
| 4460 | if (CanonicalFPT->getExceptionSpecType() == EST_Unevaluated) |
| 4461 | updateExceptionSpec(*this, MD->getCanonicalDecl(), |
| 4462 | CanonicalFPT, ExceptSpec); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4465 | void Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD) { |
| 4466 | CXXRecordDecl *RD = MD->getParent(); |
| 4467 | CXXSpecialMember CSM = getSpecialMember(MD); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4468 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4469 | assert(MD->isExplicitlyDefaulted() && CSM != CXXInvalid && |
| 4470 | "not an explicitly-defaulted special member"); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4471 | |
| 4472 | // Whether this was the first-declared instance of the constructor. |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4473 | // This affects whether we implicitly add an exception spec and constexpr. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4474 | bool First = MD == MD->getCanonicalDecl(); |
| 4475 | |
| 4476 | bool HadError = false; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4477 | |
| 4478 | // C++11 [dcl.fct.def.default]p1: |
| 4479 | // A function that is explicitly defaulted shall |
| 4480 | // -- be a special member function (checked elsewhere), |
| 4481 | // -- have the same type (except for ref-qualifiers, and except that a |
| 4482 | // copy operation can take a non-const reference) as an implicit |
| 4483 | // declaration, and |
| 4484 | // -- not have default arguments. |
| 4485 | unsigned ExpectedParams = 1; |
| 4486 | if (CSM == CXXDefaultConstructor || CSM == CXXDestructor) |
| 4487 | ExpectedParams = 0; |
| 4488 | if (MD->getNumParams() != ExpectedParams) { |
| 4489 | // This also checks for default arguments: a copy or move constructor with a |
| 4490 | // default argument is classified as a default constructor, and assignment |
| 4491 | // operations and destructors can't have default arguments. |
| 4492 | Diag(MD->getLocation(), diag::err_defaulted_special_member_params) |
| 4493 | << CSM << MD->getSourceRange(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4494 | HadError = true; |
Richard Smith | 5046439 | 2012-12-07 02:10:28 +0000 | [diff] [blame] | 4495 | } else if (MD->isVariadic()) { |
| 4496 | Diag(MD->getLocation(), diag::err_defaulted_special_member_variadic) |
| 4497 | << CSM << MD->getSourceRange(); |
| 4498 | HadError = true; |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4501 | const FunctionProtoType *Type = MD->getType()->getAs<FunctionProtoType>(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4502 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4503 | bool CanHaveConstParam = false; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4504 | if (CSM == CXXCopyConstructor) |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 4505 | CanHaveConstParam = RD->implicitCopyConstructorHasConstParam(); |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4506 | else if (CSM == CXXCopyAssignment) |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 4507 | CanHaveConstParam = RD->implicitCopyAssignmentHasConstParam(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4508 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4509 | QualType ReturnType = Context.VoidTy; |
| 4510 | if (CSM == CXXCopyAssignment || CSM == CXXMoveAssignment) { |
| 4511 | // Check for return type matching. |
| 4512 | ReturnType = Type->getResultType(); |
| 4513 | QualType ExpectedReturnType = |
| 4514 | Context.getLValueReferenceType(Context.getTypeDeclType(RD)); |
| 4515 | if (!Context.hasSameType(ReturnType, ExpectedReturnType)) { |
| 4516 | Diag(MD->getLocation(), diag::err_defaulted_special_member_return_type) |
| 4517 | << (CSM == CXXMoveAssignment) << ExpectedReturnType; |
| 4518 | HadError = true; |
| 4519 | } |
| 4520 | |
| 4521 | // A defaulted special member cannot have cv-qualifiers. |
| 4522 | if (Type->getTypeQuals()) { |
| 4523 | Diag(MD->getLocation(), diag::err_defaulted_special_member_quals) |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4524 | << (CSM == CXXMoveAssignment) << getLangOpts().CPlusPlus1y; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4525 | HadError = true; |
| 4526 | } |
| 4527 | } |
| 4528 | |
| 4529 | // Check for parameter type matching. |
| 4530 | QualType ArgType = ExpectedParams ? Type->getArgType(0) : QualType(); |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4531 | bool HasConstParam = false; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4532 | if (ExpectedParams && ArgType->isReferenceType()) { |
| 4533 | // Argument must be reference to possibly-const T. |
| 4534 | QualType ReferentType = ArgType->getPointeeType(); |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4535 | HasConstParam = ReferentType.isConstQualified(); |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4536 | |
| 4537 | if (ReferentType.isVolatileQualified()) { |
| 4538 | Diag(MD->getLocation(), |
| 4539 | diag::err_defaulted_special_member_volatile_param) << CSM; |
| 4540 | HadError = true; |
| 4541 | } |
| 4542 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4543 | if (HasConstParam && !CanHaveConstParam) { |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4544 | if (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment) { |
| 4545 | Diag(MD->getLocation(), |
| 4546 | diag::err_defaulted_special_member_copy_const_param) |
| 4547 | << (CSM == CXXCopyAssignment); |
| 4548 | // FIXME: Explain why this special member can't be const. |
| 4549 | } else { |
| 4550 | Diag(MD->getLocation(), |
| 4551 | diag::err_defaulted_special_member_move_const_param) |
| 4552 | << (CSM == CXXMoveAssignment); |
| 4553 | } |
| 4554 | HadError = true; |
| 4555 | } |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4556 | } else if (ExpectedParams) { |
| 4557 | // A copy assignment operator can take its argument by value, but a |
| 4558 | // defaulted one cannot. |
| 4559 | assert(CSM == CXXCopyAssignment && "unexpected non-ref argument"); |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4560 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_not_ref); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4561 | HadError = true; |
| 4562 | } |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4563 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4564 | // C++11 [dcl.fct.def.default]p2: |
| 4565 | // An explicitly-defaulted function may be declared constexpr only if it |
| 4566 | // would have been implicitly declared as constexpr, |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4567 | // Do not apply this rule to members of class templates, since core issue 1358 |
| 4568 | // makes such functions always instantiate to constexpr functions. For |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4569 | // functions which cannot be constexpr (for non-constructors in C++11 and for |
| 4570 | // destructors in C++1y), this is checked elsewhere. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4571 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, RD, CSM, |
| 4572 | HasConstParam); |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4573 | if ((getLangOpts().CPlusPlus1y ? !isa<CXXDestructorDecl>(MD) |
| 4574 | : isa<CXXConstructorDecl>(MD)) && |
| 4575 | MD->isConstexpr() && !Constexpr && |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4576 | MD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) { |
| 4577 | Diag(MD->getLocStart(), diag::err_incorrect_defaulted_constexpr) << CSM; |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4578 | // FIXME: Explain why the special member can't be constexpr. |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4579 | HadError = true; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4580 | } |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4581 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4582 | // and may have an explicit exception-specification only if it is compatible |
| 4583 | // with the exception-specification on the implicit declaration. |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4584 | if (Type->hasExceptionSpec()) { |
| 4585 | // Delay the check if this is the first declaration of the special member, |
| 4586 | // since we may not have parsed some necessary in-class initializers yet. |
Richard Smith | 12fef49 | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 4587 | if (First) { |
| 4588 | // If the exception specification needs to be instantiated, do so now, |
| 4589 | // before we clobber it with an EST_Unevaluated specification below. |
| 4590 | if (Type->getExceptionSpecType() == EST_Uninstantiated) { |
| 4591 | InstantiateExceptionSpec(MD->getLocStart(), MD); |
| 4592 | Type = MD->getType()->getAs<FunctionProtoType>(); |
| 4593 | } |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4594 | DelayedDefaultedMemberExceptionSpecs.push_back(std::make_pair(MD, Type)); |
Richard Smith | 12fef49 | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 4595 | } else |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4596 | CheckExplicitlyDefaultedMemberExceptionSpec(MD, Type); |
| 4597 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4598 | |
| 4599 | // If a function is explicitly defaulted on its first declaration, |
| 4600 | if (First) { |
| 4601 | // -- it is implicitly considered to be constexpr if the implicit |
| 4602 | // definition would be, |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4603 | MD->setConstexpr(Constexpr); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4604 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4605 | // -- it is implicitly considered to have the same exception-specification |
| 4606 | // as if it had been implicitly declared, |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4607 | FunctionProtoType::ExtProtoInfo EPI = Type->getExtProtoInfo(); |
| 4608 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 4609 | EPI.ExceptionSpecDecl = MD; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 4610 | MD->setType(Context.getFunctionType(ReturnType, |
| 4611 | ArrayRef<QualType>(&ArgType, |
| 4612 | ExpectedParams), |
| 4613 | EPI)); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4616 | if (ShouldDeleteSpecialMember(MD, CSM)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4617 | if (First) { |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 4618 | SetDeclDeleted(MD, MD->getLocation()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4619 | } else { |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4620 | // C++11 [dcl.fct.def.default]p4: |
| 4621 | // [For a] user-provided explicitly-defaulted function [...] if such a |
| 4622 | // function is implicitly defined as deleted, the program is ill-formed. |
| 4623 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) << CSM; |
| 4624 | HadError = true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4625 | } |
| 4626 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4627 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4628 | if (HadError) |
| 4629 | MD->setInvalidDecl(); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4632 | /// Check whether the exception specification provided for an |
| 4633 | /// explicitly-defaulted special member matches the exception specification |
| 4634 | /// that would have been generated for an implicit special member, per |
| 4635 | /// C++11 [dcl.fct.def.default]p2. |
| 4636 | void Sema::CheckExplicitlyDefaultedMemberExceptionSpec( |
| 4637 | CXXMethodDecl *MD, const FunctionProtoType *SpecifiedType) { |
| 4638 | // Compute the implicit exception specification. |
| 4639 | FunctionProtoType::ExtProtoInfo EPI; |
| 4640 | computeImplicitExceptionSpec(*this, MD->getLocation(), MD).getEPI(EPI); |
| 4641 | const FunctionProtoType *ImplicitType = cast<FunctionProtoType>( |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 4642 | Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4643 | |
| 4644 | // Ensure that it matches. |
| 4645 | CheckEquivalentExceptionSpec( |
| 4646 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
| 4647 | << getSpecialMember(MD), PDiag(), |
| 4648 | ImplicitType, SourceLocation(), |
| 4649 | SpecifiedType, MD->getLocation()); |
| 4650 | } |
| 4651 | |
| 4652 | void Sema::CheckDelayedExplicitlyDefaultedMemberExceptionSpecs() { |
| 4653 | for (unsigned I = 0, N = DelayedDefaultedMemberExceptionSpecs.size(); |
| 4654 | I != N; ++I) |
| 4655 | CheckExplicitlyDefaultedMemberExceptionSpec( |
| 4656 | DelayedDefaultedMemberExceptionSpecs[I].first, |
| 4657 | DelayedDefaultedMemberExceptionSpecs[I].second); |
| 4658 | |
| 4659 | DelayedDefaultedMemberExceptionSpecs.clear(); |
| 4660 | } |
| 4661 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4662 | namespace { |
| 4663 | struct SpecialMemberDeletionInfo { |
| 4664 | Sema &S; |
| 4665 | CXXMethodDecl *MD; |
| 4666 | Sema::CXXSpecialMember CSM; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4667 | bool Diagnose; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4668 | |
| 4669 | // Properties of the special member, computed for convenience. |
| 4670 | bool IsConstructor, IsAssignment, IsMove, ConstArg, VolatileArg; |
| 4671 | SourceLocation Loc; |
| 4672 | |
| 4673 | bool AllFieldsAreConst; |
| 4674 | |
| 4675 | SpecialMemberDeletionInfo(Sema &S, CXXMethodDecl *MD, |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4676 | Sema::CXXSpecialMember CSM, bool Diagnose) |
| 4677 | : S(S), MD(MD), CSM(CSM), Diagnose(Diagnose), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4678 | IsConstructor(false), IsAssignment(false), IsMove(false), |
| 4679 | ConstArg(false), VolatileArg(false), Loc(MD->getLocation()), |
| 4680 | AllFieldsAreConst(true) { |
| 4681 | switch (CSM) { |
| 4682 | case Sema::CXXDefaultConstructor: |
| 4683 | case Sema::CXXCopyConstructor: |
| 4684 | IsConstructor = true; |
| 4685 | break; |
| 4686 | case Sema::CXXMoveConstructor: |
| 4687 | IsConstructor = true; |
| 4688 | IsMove = true; |
| 4689 | break; |
| 4690 | case Sema::CXXCopyAssignment: |
| 4691 | IsAssignment = true; |
| 4692 | break; |
| 4693 | case Sema::CXXMoveAssignment: |
| 4694 | IsAssignment = true; |
| 4695 | IsMove = true; |
| 4696 | break; |
| 4697 | case Sema::CXXDestructor: |
| 4698 | break; |
| 4699 | case Sema::CXXInvalid: |
| 4700 | llvm_unreachable("invalid special member kind"); |
| 4701 | } |
| 4702 | |
| 4703 | if (MD->getNumParams()) { |
| 4704 | ConstArg = MD->getParamDecl(0)->getType().isConstQualified(); |
| 4705 | VolatileArg = MD->getParamDecl(0)->getType().isVolatileQualified(); |
| 4706 | } |
| 4707 | } |
| 4708 | |
| 4709 | bool inUnion() const { return MD->getParent()->isUnion(); } |
| 4710 | |
| 4711 | /// Look up the corresponding special member in the given class. |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4712 | Sema::SpecialMemberOverloadResult *lookupIn(CXXRecordDecl *Class, |
| 4713 | unsigned Quals) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4714 | unsigned TQ = MD->getTypeQualifiers(); |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4715 | // cv-qualifiers on class members don't affect default ctor / dtor calls. |
| 4716 | if (CSM == Sema::CXXDefaultConstructor || CSM == Sema::CXXDestructor) |
| 4717 | Quals = 0; |
| 4718 | return S.LookupSpecialMember(Class, CSM, |
| 4719 | ConstArg || (Quals & Qualifiers::Const), |
| 4720 | VolatileArg || (Quals & Qualifiers::Volatile), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4721 | MD->getRefQualifier() == RQ_RValue, |
| 4722 | TQ & Qualifiers::Const, |
| 4723 | TQ & Qualifiers::Volatile); |
| 4724 | } |
| 4725 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4726 | typedef llvm::PointerUnion<CXXBaseSpecifier*, FieldDecl*> Subobject; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4727 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4728 | bool shouldDeleteForBase(CXXBaseSpecifier *Base); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4729 | bool shouldDeleteForField(FieldDecl *FD); |
| 4730 | bool shouldDeleteForAllConstMembers(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4731 | |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4732 | bool shouldDeleteForClassSubobject(CXXRecordDecl *Class, Subobject Subobj, |
| 4733 | unsigned Quals); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4734 | bool shouldDeleteForSubobjectCall(Subobject Subobj, |
| 4735 | Sema::SpecialMemberOverloadResult *SMOR, |
| 4736 | bool IsDtorCallInCtor); |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4737 | |
| 4738 | bool isAccessible(Subobject Subobj, CXXMethodDecl *D); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4739 | }; |
| 4740 | } |
| 4741 | |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4742 | /// Is the given special member inaccessible when used on the given |
| 4743 | /// sub-object. |
| 4744 | bool SpecialMemberDeletionInfo::isAccessible(Subobject Subobj, |
| 4745 | CXXMethodDecl *target) { |
| 4746 | /// If we're operating on a base class, the object type is the |
| 4747 | /// type of this special member. |
| 4748 | QualType objectTy; |
Dmitri Gribenko | 1ad23d6 | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 4749 | AccessSpecifier access = target->getAccess(); |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4750 | if (CXXBaseSpecifier *base = Subobj.dyn_cast<CXXBaseSpecifier*>()) { |
| 4751 | objectTy = S.Context.getTypeDeclType(MD->getParent()); |
| 4752 | access = CXXRecordDecl::MergeAccess(base->getAccessSpecifier(), access); |
| 4753 | |
| 4754 | // If we're operating on a field, the object type is the type of the field. |
| 4755 | } else { |
| 4756 | objectTy = S.Context.getTypeDeclType(target->getParent()); |
| 4757 | } |
| 4758 | |
| 4759 | return S.isSpecialMemberAccessibleForDeletion(target, access, objectTy); |
| 4760 | } |
| 4761 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4762 | /// Check whether we should delete a special member due to the implicit |
| 4763 | /// definition containing a call to a special member of a subobject. |
| 4764 | bool SpecialMemberDeletionInfo::shouldDeleteForSubobjectCall( |
| 4765 | Subobject Subobj, Sema::SpecialMemberOverloadResult *SMOR, |
| 4766 | bool IsDtorCallInCtor) { |
| 4767 | CXXMethodDecl *Decl = SMOR->getMethod(); |
| 4768 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
| 4769 | |
| 4770 | int DiagKind = -1; |
| 4771 | |
| 4772 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::NoMemberOrDeleted) |
| 4773 | DiagKind = !Decl ? 0 : 1; |
| 4774 | else if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 4775 | DiagKind = 2; |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4776 | else if (!isAccessible(Subobj, Decl)) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4777 | DiagKind = 3; |
| 4778 | else if (!IsDtorCallInCtor && Field && Field->getParent()->isUnion() && |
| 4779 | !Decl->isTrivial()) { |
| 4780 | // A member of a union must have a trivial corresponding special member. |
| 4781 | // As a weird special case, a destructor call from a union's constructor |
| 4782 | // must be accessible and non-deleted, but need not be trivial. Such a |
| 4783 | // destructor is never actually called, but is semantically checked as |
| 4784 | // if it were. |
| 4785 | DiagKind = 4; |
| 4786 | } |
| 4787 | |
| 4788 | if (DiagKind == -1) |
| 4789 | return false; |
| 4790 | |
| 4791 | if (Diagnose) { |
| 4792 | if (Field) { |
| 4793 | S.Diag(Field->getLocation(), |
| 4794 | diag::note_deleted_special_member_class_subobject) |
| 4795 | << CSM << MD->getParent() << /*IsField*/true |
| 4796 | << Field << DiagKind << IsDtorCallInCtor; |
| 4797 | } else { |
| 4798 | CXXBaseSpecifier *Base = Subobj.get<CXXBaseSpecifier*>(); |
| 4799 | S.Diag(Base->getLocStart(), |
| 4800 | diag::note_deleted_special_member_class_subobject) |
| 4801 | << CSM << MD->getParent() << /*IsField*/false |
| 4802 | << Base->getType() << DiagKind << IsDtorCallInCtor; |
| 4803 | } |
| 4804 | |
| 4805 | if (DiagKind == 1) |
| 4806 | S.NoteDeletedFunction(Decl); |
| 4807 | // FIXME: Explain inaccessibility if DiagKind == 3. |
| 4808 | } |
| 4809 | |
| 4810 | return true; |
| 4811 | } |
| 4812 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4813 | /// Check whether we should delete a special member function due to having a |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4814 | /// direct or virtual base class or non-static data member of class type M. |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4815 | bool SpecialMemberDeletionInfo::shouldDeleteForClassSubobject( |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4816 | CXXRecordDecl *Class, Subobject Subobj, unsigned Quals) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4817 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4818 | |
| 4819 | // C++11 [class.ctor]p5: |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4820 | // -- any direct or virtual base class, or non-static data member with no |
| 4821 | // brace-or-equal-initializer, has class type M (or array thereof) and |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4822 | // either M has no default constructor or overload resolution as applied |
| 4823 | // to M's default constructor results in an ambiguity or in a function |
| 4824 | // that is deleted or inaccessible |
| 4825 | // C++11 [class.copy]p11, C++11 [class.copy]p23: |
| 4826 | // -- a direct or virtual base class B that cannot be copied/moved because |
| 4827 | // overload resolution, as applied to B's corresponding special member, |
| 4828 | // results in an ambiguity or a function that is deleted or inaccessible |
| 4829 | // from the defaulted special member |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4830 | // C++11 [class.dtor]p5: |
| 4831 | // -- any direct or virtual base class [...] has a type with a destructor |
| 4832 | // that is deleted or inaccessible |
| 4833 | if (!(CSM == Sema::CXXDefaultConstructor && |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4834 | Field && Field->hasInClassInitializer()) && |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4835 | shouldDeleteForSubobjectCall(Subobj, lookupIn(Class, Quals), false)) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4836 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4837 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4838 | // C++11 [class.ctor]p5, C++11 [class.copy]p11: |
| 4839 | // -- any direct or virtual base class or non-static data member has a |
| 4840 | // type with a destructor that is deleted or inaccessible |
| 4841 | if (IsConstructor) { |
| 4842 | Sema::SpecialMemberOverloadResult *SMOR = |
| 4843 | S.LookupSpecialMember(Class, Sema::CXXDestructor, |
| 4844 | false, false, false, false, false); |
| 4845 | if (shouldDeleteForSubobjectCall(Subobj, SMOR, true)) |
| 4846 | return true; |
| 4847 | } |
| 4848 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4849 | return false; |
| 4850 | } |
| 4851 | |
| 4852 | /// Check whether we should delete a special member function due to the class |
| 4853 | /// having a particular direct or virtual base class. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4854 | bool SpecialMemberDeletionInfo::shouldDeleteForBase(CXXBaseSpecifier *Base) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4855 | CXXRecordDecl *BaseClass = Base->getType()->getAsCXXRecordDecl(); |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4856 | return shouldDeleteForClassSubobject(BaseClass, Base, 0); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
| 4859 | /// Check whether we should delete a special member function due to the class |
| 4860 | /// having a particular non-static data member. |
| 4861 | bool SpecialMemberDeletionInfo::shouldDeleteForField(FieldDecl *FD) { |
| 4862 | QualType FieldType = S.Context.getBaseElementType(FD->getType()); |
| 4863 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 4864 | |
| 4865 | if (CSM == Sema::CXXDefaultConstructor) { |
| 4866 | // For a default constructor, all references must be initialized in-class |
| 4867 | // and, if a union, it must have a non-const member. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4868 | if (FieldType->isReferenceType() && !FD->hasInClassInitializer()) { |
| 4869 | if (Diagnose) |
| 4870 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
| 4871 | << MD->getParent() << FD << FieldType << /*Reference*/0; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4872 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4873 | } |
Richard Smith | 79363f5 | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 4874 | // C++11 [class.ctor]p5: any non-variant non-static data member of |
| 4875 | // const-qualified type (or array thereof) with no |
| 4876 | // brace-or-equal-initializer does not have a user-provided default |
| 4877 | // constructor. |
| 4878 | if (!inUnion() && FieldType.isConstQualified() && |
| 4879 | !FD->hasInClassInitializer() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4880 | (!FieldRecord || !FieldRecord->hasUserProvidedDefaultConstructor())) { |
| 4881 | if (Diagnose) |
| 4882 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
Richard Smith | a2e76f5 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 4883 | << MD->getParent() << FD << FD->getType() << /*Const*/1; |
Richard Smith | 79363f5 | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 4884 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
| 4887 | if (inUnion() && !FieldType.isConstQualified()) |
| 4888 | AllFieldsAreConst = false; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4889 | } else if (CSM == Sema::CXXCopyConstructor) { |
| 4890 | // For a copy constructor, data members must not be of rvalue reference |
| 4891 | // type. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4892 | if (FieldType->isRValueReferenceType()) { |
| 4893 | if (Diagnose) |
| 4894 | S.Diag(FD->getLocation(), diag::note_deleted_copy_ctor_rvalue_reference) |
| 4895 | << MD->getParent() << FD << FieldType; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4896 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4897 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4898 | } else if (IsAssignment) { |
| 4899 | // For an assignment operator, data members must not be of reference type. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4900 | if (FieldType->isReferenceType()) { |
| 4901 | if (Diagnose) |
| 4902 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
| 4903 | << IsMove << MD->getParent() << FD << FieldType << /*Reference*/0; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4904 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4905 | } |
| 4906 | if (!FieldRecord && FieldType.isConstQualified()) { |
| 4907 | // C++11 [class.copy]p23: |
| 4908 | // -- a non-static data member of const non-class type (or array thereof) |
| 4909 | if (Diagnose) |
| 4910 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
Richard Smith | a2e76f5 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 4911 | << IsMove << MD->getParent() << FD << FD->getType() << /*Const*/1; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4912 | return true; |
| 4913 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | if (FieldRecord) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4917 | // Some additional restrictions exist on the variant members. |
| 4918 | if (!inUnion() && FieldRecord->isUnion() && |
| 4919 | FieldRecord->isAnonymousStructOrUnion()) { |
| 4920 | bool AllVariantFieldsAreConst = true; |
| 4921 | |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4922 | // FIXME: Handle anonymous unions declared within anonymous unions. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4923 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 4924 | UE = FieldRecord->field_end(); |
| 4925 | UI != UE; ++UI) { |
| 4926 | QualType UnionFieldType = S.Context.getBaseElementType(UI->getType()); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4927 | |
| 4928 | if (!UnionFieldType.isConstQualified()) |
| 4929 | AllVariantFieldsAreConst = false; |
| 4930 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4931 | CXXRecordDecl *UnionFieldRecord = UnionFieldType->getAsCXXRecordDecl(); |
| 4932 | if (UnionFieldRecord && |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4933 | shouldDeleteForClassSubobject(UnionFieldRecord, *UI, |
| 4934 | UnionFieldType.getCVRQualifiers())) |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4935 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | // At least one member in each anonymous union must be non-const |
Douglas Gregor | 221c27f | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 4939 | if (CSM == Sema::CXXDefaultConstructor && AllVariantFieldsAreConst && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4940 | FieldRecord->field_begin() != FieldRecord->field_end()) { |
| 4941 | if (Diagnose) |
| 4942 | S.Diag(FieldRecord->getLocation(), |
| 4943 | diag::note_deleted_default_ctor_all_const) |
| 4944 | << MD->getParent() << /*anonymous union*/1; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4945 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4946 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4947 | |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4948 | // Don't check the implicit member of the anonymous union type. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4949 | // This is technically non-conformant, but sanity demands it. |
| 4950 | return false; |
| 4951 | } |
| 4952 | |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4953 | if (shouldDeleteForClassSubobject(FieldRecord, FD, |
| 4954 | FieldType.getCVRQualifiers())) |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4955 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4956 | } |
| 4957 | |
| 4958 | return false; |
| 4959 | } |
| 4960 | |
| 4961 | /// C++11 [class.ctor] p5: |
| 4962 | /// A defaulted default constructor for a class X is defined as deleted if |
| 4963 | /// X is a union and all of its variant members are of const-qualified type. |
| 4964 | bool SpecialMemberDeletionInfo::shouldDeleteForAllConstMembers() { |
Douglas Gregor | 221c27f | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 4965 | // This is a silly definition, because it gives an empty union a deleted |
| 4966 | // default constructor. Don't do that. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4967 | if (CSM == Sema::CXXDefaultConstructor && inUnion() && AllFieldsAreConst && |
| 4968 | (MD->getParent()->field_begin() != MD->getParent()->field_end())) { |
| 4969 | if (Diagnose) |
| 4970 | S.Diag(MD->getParent()->getLocation(), |
| 4971 | diag::note_deleted_default_ctor_all_const) |
| 4972 | << MD->getParent() << /*not anonymous union*/0; |
| 4973 | return true; |
| 4974 | } |
| 4975 | return false; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
| 4978 | /// Determine whether a defaulted special member function should be defined as |
| 4979 | /// deleted, as specified in C++11 [class.ctor]p5, C++11 [class.copy]p11, |
| 4980 | /// C++11 [class.copy]p23, and C++11 [class.dtor]p5. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4981 | bool Sema::ShouldDeleteSpecialMember(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 4982 | bool Diagnose) { |
Richard Smith | eef0029 | 2012-08-06 02:25:10 +0000 | [diff] [blame] | 4983 | if (MD->isInvalidDecl()) |
| 4984 | return false; |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 4985 | CXXRecordDecl *RD = MD->getParent(); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4986 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4987 | if (!LangOpts.CPlusPlus11 || RD->isInvalidDecl()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4988 | return false; |
| 4989 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4990 | // C++11 [expr.lambda.prim]p19: |
| 4991 | // The closure type associated with a lambda-expression has a |
| 4992 | // deleted (8.4.3) default constructor and a deleted copy |
| 4993 | // assignment operator. |
| 4994 | if (RD->isLambda() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4995 | (CSM == CXXDefaultConstructor || CSM == CXXCopyAssignment)) { |
| 4996 | if (Diagnose) |
| 4997 | Diag(RD->getLocation(), diag::note_lambda_decl); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4998 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4999 | } |
| 5000 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 5001 | // For an anonymous struct or union, the copy and assignment special members |
| 5002 | // will never be used, so skip the check. For an anonymous union declared at |
| 5003 | // namespace scope, the constructor and destructor are used. |
| 5004 | if (CSM != CXXDefaultConstructor && CSM != CXXDestructor && |
| 5005 | RD->isAnonymousStructOrUnion()) |
| 5006 | return false; |
| 5007 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5008 | // C++11 [class.copy]p7, p18: |
| 5009 | // If the class definition declares a move constructor or move assignment |
| 5010 | // operator, an implicitly declared copy constructor or copy assignment |
| 5011 | // operator is defined as deleted. |
| 5012 | if (MD->isImplicit() && |
| 5013 | (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment)) { |
| 5014 | CXXMethodDecl *UserDeclaredMove = 0; |
| 5015 | |
| 5016 | // In Microsoft mode, a user-declared move only causes the deletion of the |
| 5017 | // corresponding copy operation, not both copy operations. |
| 5018 | if (RD->hasUserDeclaredMoveConstructor() && |
| 5019 | (!getLangOpts().MicrosoftMode || CSM == CXXCopyConstructor)) { |
| 5020 | if (!Diagnose) return true; |
Richard Smith | 5579865 | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5021 | |
| 5022 | // Find any user-declared move constructor. |
| 5023 | for (CXXRecordDecl::ctor_iterator I = RD->ctor_begin(), |
| 5024 | E = RD->ctor_end(); I != E; ++I) { |
| 5025 | if (I->isMoveConstructor()) { |
| 5026 | UserDeclaredMove = *I; |
| 5027 | break; |
| 5028 | } |
| 5029 | } |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5030 | assert(UserDeclaredMove); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5031 | } else if (RD->hasUserDeclaredMoveAssignment() && |
| 5032 | (!getLangOpts().MicrosoftMode || CSM == CXXCopyAssignment)) { |
| 5033 | if (!Diagnose) return true; |
Richard Smith | 5579865 | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5034 | |
| 5035 | // Find any user-declared move assignment operator. |
| 5036 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 5037 | E = RD->method_end(); I != E; ++I) { |
| 5038 | if (I->isMoveAssignmentOperator()) { |
| 5039 | UserDeclaredMove = *I; |
| 5040 | break; |
| 5041 | } |
| 5042 | } |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5043 | assert(UserDeclaredMove); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | if (UserDeclaredMove) { |
| 5047 | Diag(UserDeclaredMove->getLocation(), |
| 5048 | diag::note_deleted_copy_user_declared_move) |
Richard Smith | e6af660 | 2012-04-02 21:07:48 +0000 | [diff] [blame] | 5049 | << (CSM == CXXCopyAssignment) << RD |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5050 | << UserDeclaredMove->isMoveAssignmentOperator(); |
| 5051 | return true; |
| 5052 | } |
| 5053 | } |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 5054 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 5055 | // Do access control from the special member function |
| 5056 | ContextRAII MethodContext(*this, MD); |
| 5057 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5058 | // C++11 [class.dtor]p5: |
| 5059 | // -- for a virtual destructor, lookup of the non-array deallocation function |
| 5060 | // results in an ambiguity or in a function that is deleted or inaccessible |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5061 | if (CSM == CXXDestructor && MD->isVirtual()) { |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5062 | FunctionDecl *OperatorDelete = 0; |
| 5063 | DeclarationName Name = |
| 5064 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| 5065 | if (FindDeallocationFunction(MD->getLocation(), MD->getParent(), Name, |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5066 | OperatorDelete, false)) { |
| 5067 | if (Diagnose) |
| 5068 | Diag(RD->getLocation(), diag::note_deleted_dtor_no_operator_delete); |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5069 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5070 | } |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5071 | } |
| 5072 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5073 | SpecialMemberDeletionInfo SMI(*this, MD, CSM, Diagnose); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5074 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5075 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5076 | BE = RD->bases_end(); BI != BE; ++BI) |
| 5077 | if (!BI->isVirtual() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5078 | SMI.shouldDeleteForBase(BI)) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5079 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5080 | |
Richard Smith | e088360 | 2013-07-22 18:06:23 +0000 | [diff] [blame] | 5081 | // Per DR1611, do not consider virtual bases of constructors of abstract |
| 5082 | // classes, since we are not going to construct them. |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 5083 | if (!RD->isAbstract() || !SMI.IsConstructor) { |
| 5084 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 5085 | BE = RD->vbases_end(); |
| 5086 | BI != BE; ++BI) |
| 5087 | if (SMI.shouldDeleteForBase(BI)) |
| 5088 | return true; |
| 5089 | } |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5090 | |
| 5091 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5092 | FE = RD->field_end(); FI != FE; ++FI) |
| 5093 | if (!FI->isInvalidDecl() && !FI->isUnnamedBitfield() && |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5094 | SMI.shouldDeleteForField(*FI)) |
Sean Hunt | e340682 | 2011-05-20 21:43:47 +0000 | [diff] [blame] | 5095 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5096 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5097 | if (SMI.shouldDeleteForAllConstMembers()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5098 | return true; |
| 5099 | |
| 5100 | return false; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5101 | } |
| 5102 | |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5103 | /// Perform lookup for a special member of the specified kind, and determine |
| 5104 | /// whether it is trivial. If the triviality can be determined without the |
| 5105 | /// lookup, skip it. This is intended for use when determining whether a |
| 5106 | /// special member of a containing object is trivial, and thus does not ever |
| 5107 | /// perform overload resolution for default constructors. |
| 5108 | /// |
| 5109 | /// If \p Selected is not \c NULL, \c *Selected will be filled in with the |
| 5110 | /// member that was most likely to be intended to be trivial, if any. |
| 5111 | static bool findTrivialSpecialMember(Sema &S, CXXRecordDecl *RD, |
| 5112 | Sema::CXXSpecialMember CSM, unsigned Quals, |
| 5113 | CXXMethodDecl **Selected) { |
| 5114 | if (Selected) |
| 5115 | *Selected = 0; |
| 5116 | |
| 5117 | switch (CSM) { |
| 5118 | case Sema::CXXInvalid: |
| 5119 | llvm_unreachable("not a special member"); |
| 5120 | |
| 5121 | case Sema::CXXDefaultConstructor: |
| 5122 | // C++11 [class.ctor]p5: |
| 5123 | // A default constructor is trivial if: |
| 5124 | // - all the [direct subobjects] have trivial default constructors |
| 5125 | // |
| 5126 | // Note, no overload resolution is performed in this case. |
| 5127 | if (RD->hasTrivialDefaultConstructor()) |
| 5128 | return true; |
| 5129 | |
| 5130 | if (Selected) { |
| 5131 | // If there's a default constructor which could have been trivial, dig it |
| 5132 | // out. Otherwise, if there's any user-provided default constructor, point |
| 5133 | // to that as an example of why there's not a trivial one. |
| 5134 | CXXConstructorDecl *DefCtor = 0; |
| 5135 | if (RD->needsImplicitDefaultConstructor()) |
| 5136 | S.DeclareImplicitDefaultConstructor(RD); |
| 5137 | for (CXXRecordDecl::ctor_iterator CI = RD->ctor_begin(), |
| 5138 | CE = RD->ctor_end(); CI != CE; ++CI) { |
| 5139 | if (!CI->isDefaultConstructor()) |
| 5140 | continue; |
| 5141 | DefCtor = *CI; |
| 5142 | if (!DefCtor->isUserProvided()) |
| 5143 | break; |
| 5144 | } |
| 5145 | |
| 5146 | *Selected = DefCtor; |
| 5147 | } |
| 5148 | |
| 5149 | return false; |
| 5150 | |
| 5151 | case Sema::CXXDestructor: |
| 5152 | // C++11 [class.dtor]p5: |
| 5153 | // A destructor is trivial if: |
| 5154 | // - all the direct [subobjects] have trivial destructors |
| 5155 | if (RD->hasTrivialDestructor()) |
| 5156 | return true; |
| 5157 | |
| 5158 | if (Selected) { |
| 5159 | if (RD->needsImplicitDestructor()) |
| 5160 | S.DeclareImplicitDestructor(RD); |
| 5161 | *Selected = RD->getDestructor(); |
| 5162 | } |
| 5163 | |
| 5164 | return false; |
| 5165 | |
| 5166 | case Sema::CXXCopyConstructor: |
| 5167 | // C++11 [class.copy]p12: |
| 5168 | // A copy constructor is trivial if: |
| 5169 | // - the constructor selected to copy each direct [subobject] is trivial |
| 5170 | if (RD->hasTrivialCopyConstructor()) { |
| 5171 | if (Quals == Qualifiers::Const) |
| 5172 | // We must either select the trivial copy constructor or reach an |
| 5173 | // ambiguity; no need to actually perform overload resolution. |
| 5174 | return true; |
| 5175 | } else if (!Selected) { |
| 5176 | return false; |
| 5177 | } |
| 5178 | // In C++98, we are not supposed to perform overload resolution here, but we |
| 5179 | // treat that as a language defect, as suggested on cxx-abi-dev, to treat |
| 5180 | // cases like B as having a non-trivial copy constructor: |
| 5181 | // struct A { template<typename T> A(T&); }; |
| 5182 | // struct B { mutable A a; }; |
| 5183 | goto NeedOverloadResolution; |
| 5184 | |
| 5185 | case Sema::CXXCopyAssignment: |
| 5186 | // C++11 [class.copy]p25: |
| 5187 | // A copy assignment operator is trivial if: |
| 5188 | // - the assignment operator selected to copy each direct [subobject] is |
| 5189 | // trivial |
| 5190 | if (RD->hasTrivialCopyAssignment()) { |
| 5191 | if (Quals == Qualifiers::Const) |
| 5192 | return true; |
| 5193 | } else if (!Selected) { |
| 5194 | return false; |
| 5195 | } |
| 5196 | // In C++98, we are not supposed to perform overload resolution here, but we |
| 5197 | // treat that as a language defect. |
| 5198 | goto NeedOverloadResolution; |
| 5199 | |
| 5200 | case Sema::CXXMoveConstructor: |
| 5201 | case Sema::CXXMoveAssignment: |
| 5202 | NeedOverloadResolution: |
| 5203 | Sema::SpecialMemberOverloadResult *SMOR = |
| 5204 | S.LookupSpecialMember(RD, CSM, |
| 5205 | Quals & Qualifiers::Const, |
| 5206 | Quals & Qualifiers::Volatile, |
| 5207 | /*RValueThis*/false, /*ConstThis*/false, |
| 5208 | /*VolatileThis*/false); |
| 5209 | |
| 5210 | // The standard doesn't describe how to behave if the lookup is ambiguous. |
| 5211 | // We treat it as not making the member non-trivial, just like the standard |
| 5212 | // mandates for the default constructor. This should rarely matter, because |
| 5213 | // the member will also be deleted. |
| 5214 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 5215 | return true; |
| 5216 | |
| 5217 | if (!SMOR->getMethod()) { |
| 5218 | assert(SMOR->getKind() == |
| 5219 | Sema::SpecialMemberOverloadResult::NoMemberOrDeleted); |
| 5220 | return false; |
| 5221 | } |
| 5222 | |
| 5223 | // We deliberately don't check if we found a deleted special member. We're |
| 5224 | // not supposed to! |
| 5225 | if (Selected) |
| 5226 | *Selected = SMOR->getMethod(); |
| 5227 | return SMOR->getMethod()->isTrivial(); |
| 5228 | } |
| 5229 | |
| 5230 | llvm_unreachable("unknown special method kind"); |
| 5231 | } |
| 5232 | |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 5233 | static CXXConstructorDecl *findUserDeclaredCtor(CXXRecordDecl *RD) { |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5234 | for (CXXRecordDecl::ctor_iterator CI = RD->ctor_begin(), CE = RD->ctor_end(); |
| 5235 | CI != CE; ++CI) |
| 5236 | if (!CI->isImplicit()) |
| 5237 | return *CI; |
| 5238 | |
| 5239 | // Look for constructor templates. |
| 5240 | typedef CXXRecordDecl::specific_decl_iterator<FunctionTemplateDecl> tmpl_iter; |
| 5241 | for (tmpl_iter TI(RD->decls_begin()), TE(RD->decls_end()); TI != TE; ++TI) { |
| 5242 | if (CXXConstructorDecl *CD = |
| 5243 | dyn_cast<CXXConstructorDecl>(TI->getTemplatedDecl())) |
| 5244 | return CD; |
| 5245 | } |
| 5246 | |
| 5247 | return 0; |
| 5248 | } |
| 5249 | |
| 5250 | /// The kind of subobject we are checking for triviality. The values of this |
| 5251 | /// enumeration are used in diagnostics. |
| 5252 | enum TrivialSubobjectKind { |
| 5253 | /// The subobject is a base class. |
| 5254 | TSK_BaseClass, |
| 5255 | /// The subobject is a non-static data member. |
| 5256 | TSK_Field, |
| 5257 | /// The object is actually the complete object. |
| 5258 | TSK_CompleteObject |
| 5259 | }; |
| 5260 | |
| 5261 | /// Check whether the special member selected for a given type would be trivial. |
| 5262 | static bool checkTrivialSubobjectCall(Sema &S, SourceLocation SubobjLoc, |
| 5263 | QualType SubType, |
| 5264 | Sema::CXXSpecialMember CSM, |
| 5265 | TrivialSubobjectKind Kind, |
| 5266 | bool Diagnose) { |
| 5267 | CXXRecordDecl *SubRD = SubType->getAsCXXRecordDecl(); |
| 5268 | if (!SubRD) |
| 5269 | return true; |
| 5270 | |
| 5271 | CXXMethodDecl *Selected; |
| 5272 | if (findTrivialSpecialMember(S, SubRD, CSM, SubType.getCVRQualifiers(), |
| 5273 | Diagnose ? &Selected : 0)) |
| 5274 | return true; |
| 5275 | |
| 5276 | if (Diagnose) { |
| 5277 | if (!Selected && CSM == Sema::CXXDefaultConstructor) { |
| 5278 | S.Diag(SubobjLoc, diag::note_nontrivial_no_def_ctor) |
| 5279 | << Kind << SubType.getUnqualifiedType(); |
| 5280 | if (CXXConstructorDecl *CD = findUserDeclaredCtor(SubRD)) |
| 5281 | S.Diag(CD->getLocation(), diag::note_user_declared_ctor); |
| 5282 | } else if (!Selected) |
| 5283 | S.Diag(SubobjLoc, diag::note_nontrivial_no_copy) |
| 5284 | << Kind << SubType.getUnqualifiedType() << CSM << SubType; |
| 5285 | else if (Selected->isUserProvided()) { |
| 5286 | if (Kind == TSK_CompleteObject) |
| 5287 | S.Diag(Selected->getLocation(), diag::note_nontrivial_user_provided) |
| 5288 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5289 | else { |
| 5290 | S.Diag(SubobjLoc, diag::note_nontrivial_user_provided) |
| 5291 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5292 | S.Diag(Selected->getLocation(), diag::note_declared_at); |
| 5293 | } |
| 5294 | } else { |
| 5295 | if (Kind != TSK_CompleteObject) |
| 5296 | S.Diag(SubobjLoc, diag::note_nontrivial_subobject) |
| 5297 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5298 | |
| 5299 | // Explain why the defaulted or deleted special member isn't trivial. |
| 5300 | S.SpecialMemberIsTrivial(Selected, CSM, Diagnose); |
| 5301 | } |
| 5302 | } |
| 5303 | |
| 5304 | return false; |
| 5305 | } |
| 5306 | |
| 5307 | /// Check whether the members of a class type allow a special member to be |
| 5308 | /// trivial. |
| 5309 | static bool checkTrivialClassMembers(Sema &S, CXXRecordDecl *RD, |
| 5310 | Sema::CXXSpecialMember CSM, |
| 5311 | bool ConstArg, bool Diagnose) { |
| 5312 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 5313 | FE = RD->field_end(); FI != FE; ++FI) { |
| 5314 | if (FI->isInvalidDecl() || FI->isUnnamedBitfield()) |
| 5315 | continue; |
| 5316 | |
| 5317 | QualType FieldType = S.Context.getBaseElementType(FI->getType()); |
| 5318 | |
| 5319 | // Pretend anonymous struct or union members are members of this class. |
| 5320 | if (FI->isAnonymousStructOrUnion()) { |
| 5321 | if (!checkTrivialClassMembers(S, FieldType->getAsCXXRecordDecl(), |
| 5322 | CSM, ConstArg, Diagnose)) |
| 5323 | return false; |
| 5324 | continue; |
| 5325 | } |
| 5326 | |
| 5327 | // C++11 [class.ctor]p5: |
| 5328 | // A default constructor is trivial if [...] |
| 5329 | // -- no non-static data member of its class has a |
| 5330 | // brace-or-equal-initializer |
| 5331 | if (CSM == Sema::CXXDefaultConstructor && FI->hasInClassInitializer()) { |
| 5332 | if (Diagnose) |
| 5333 | S.Diag(FI->getLocation(), diag::note_nontrivial_in_class_init) << *FI; |
| 5334 | return false; |
| 5335 | } |
| 5336 | |
| 5337 | // Objective C ARC 4.3.5: |
| 5338 | // [...] nontrivally ownership-qualified types are [...] not trivially |
| 5339 | // default constructible, copy constructible, move constructible, copy |
| 5340 | // assignable, move assignable, or destructible [...] |
| 5341 | if (S.getLangOpts().ObjCAutoRefCount && |
| 5342 | FieldType.hasNonTrivialObjCLifetime()) { |
| 5343 | if (Diagnose) |
| 5344 | S.Diag(FI->getLocation(), diag::note_nontrivial_objc_ownership) |
| 5345 | << RD << FieldType.getObjCLifetime(); |
| 5346 | return false; |
| 5347 | } |
| 5348 | |
| 5349 | if (ConstArg && !FI->isMutable()) |
| 5350 | FieldType.addConst(); |
| 5351 | if (!checkTrivialSubobjectCall(S, FI->getLocation(), FieldType, CSM, |
| 5352 | TSK_Field, Diagnose)) |
| 5353 | return false; |
| 5354 | } |
| 5355 | |
| 5356 | return true; |
| 5357 | } |
| 5358 | |
| 5359 | /// Diagnose why the specified class does not have a trivial special member of |
| 5360 | /// the given kind. |
| 5361 | void Sema::DiagnoseNontrivial(const CXXRecordDecl *RD, CXXSpecialMember CSM) { |
| 5362 | QualType Ty = Context.getRecordType(RD); |
| 5363 | if (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment) |
| 5364 | Ty.addConst(); |
| 5365 | |
| 5366 | checkTrivialSubobjectCall(*this, RD->getLocation(), Ty, CSM, |
| 5367 | TSK_CompleteObject, /*Diagnose*/true); |
| 5368 | } |
| 5369 | |
| 5370 | /// Determine whether a defaulted or deleted special member function is trivial, |
| 5371 | /// as specified in C++11 [class.ctor]p5, C++11 [class.copy]p12, |
| 5372 | /// C++11 [class.copy]p25, and C++11 [class.dtor]p5. |
| 5373 | bool Sema::SpecialMemberIsTrivial(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 5374 | bool Diagnose) { |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5375 | assert(!MD->isUserProvided() && CSM != CXXInvalid && "not special enough"); |
| 5376 | |
| 5377 | CXXRecordDecl *RD = MD->getParent(); |
| 5378 | |
| 5379 | bool ConstArg = false; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5380 | |
| 5381 | // C++11 [class.copy]p12, p25: |
| 5382 | // A [special member] is trivial if its declared parameter type is the same |
| 5383 | // as if it had been implicitly declared [...] |
| 5384 | switch (CSM) { |
| 5385 | case CXXDefaultConstructor: |
| 5386 | case CXXDestructor: |
| 5387 | // Trivial default constructors and destructors cannot have parameters. |
| 5388 | break; |
| 5389 | |
| 5390 | case CXXCopyConstructor: |
| 5391 | case CXXCopyAssignment: { |
| 5392 | // Trivial copy operations always have const, non-volatile parameter types. |
| 5393 | ConstArg = true; |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 5394 | const ParmVarDecl *Param0 = MD->getParamDecl(0); |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5395 | const ReferenceType *RT = Param0->getType()->getAs<ReferenceType>(); |
| 5396 | if (!RT || RT->getPointeeType().getCVRQualifiers() != Qualifiers::Const) { |
| 5397 | if (Diagnose) |
| 5398 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 5399 | << Param0->getSourceRange() << Param0->getType() |
| 5400 | << Context.getLValueReferenceType( |
| 5401 | Context.getRecordType(RD).withConst()); |
| 5402 | return false; |
| 5403 | } |
| 5404 | break; |
| 5405 | } |
| 5406 | |
| 5407 | case CXXMoveConstructor: |
| 5408 | case CXXMoveAssignment: { |
| 5409 | // Trivial move operations always have non-cv-qualified parameters. |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 5410 | const ParmVarDecl *Param0 = MD->getParamDecl(0); |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5411 | const RValueReferenceType *RT = |
| 5412 | Param0->getType()->getAs<RValueReferenceType>(); |
| 5413 | if (!RT || RT->getPointeeType().getCVRQualifiers()) { |
| 5414 | if (Diagnose) |
| 5415 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 5416 | << Param0->getSourceRange() << Param0->getType() |
| 5417 | << Context.getRValueReferenceType(Context.getRecordType(RD)); |
| 5418 | return false; |
| 5419 | } |
| 5420 | break; |
| 5421 | } |
| 5422 | |
| 5423 | case CXXInvalid: |
| 5424 | llvm_unreachable("not a special member"); |
| 5425 | } |
| 5426 | |
| 5427 | // FIXME: We require that the parameter-declaration-clause is equivalent to |
| 5428 | // that of an implicit declaration, not just that the declared parameter type |
| 5429 | // matches, in order to prevent absuridities like a function simultaneously |
| 5430 | // being a trivial copy constructor and a non-trivial default constructor. |
| 5431 | // This issue has not yet been assigned a core issue number. |
| 5432 | if (MD->getMinRequiredArguments() < MD->getNumParams()) { |
| 5433 | if (Diagnose) |
| 5434 | Diag(MD->getParamDecl(MD->getMinRequiredArguments())->getLocation(), |
| 5435 | diag::note_nontrivial_default_arg) |
| 5436 | << MD->getParamDecl(MD->getMinRequiredArguments())->getSourceRange(); |
| 5437 | return false; |
| 5438 | } |
| 5439 | if (MD->isVariadic()) { |
| 5440 | if (Diagnose) |
| 5441 | Diag(MD->getLocation(), diag::note_nontrivial_variadic); |
| 5442 | return false; |
| 5443 | } |
| 5444 | |
| 5445 | // C++11 [class.ctor]p5, C++11 [class.dtor]p5: |
| 5446 | // A copy/move [constructor or assignment operator] is trivial if |
| 5447 | // -- the [member] selected to copy/move each direct base class subobject |
| 5448 | // is trivial |
| 5449 | // |
| 5450 | // C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5451 | // A [default constructor or destructor] is trivial if |
| 5452 | // -- all the direct base classes have trivial [default constructors or |
| 5453 | // destructors] |
| 5454 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 5455 | BE = RD->bases_end(); BI != BE; ++BI) |
| 5456 | if (!checkTrivialSubobjectCall(*this, BI->getLocStart(), |
| 5457 | ConstArg ? BI->getType().withConst() |
| 5458 | : BI->getType(), |
| 5459 | CSM, TSK_BaseClass, Diagnose)) |
| 5460 | return false; |
| 5461 | |
| 5462 | // C++11 [class.ctor]p5, C++11 [class.dtor]p5: |
| 5463 | // A copy/move [constructor or assignment operator] for a class X is |
| 5464 | // trivial if |
| 5465 | // -- for each non-static data member of X that is of class type (or array |
| 5466 | // thereof), the constructor selected to copy/move that member is |
| 5467 | // trivial |
| 5468 | // |
| 5469 | // C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5470 | // A [default constructor or destructor] is trivial if |
| 5471 | // -- for all of the non-static data members of its class that are of class |
| 5472 | // type (or array thereof), each such class has a trivial [default |
| 5473 | // constructor or destructor] |
| 5474 | if (!checkTrivialClassMembers(*this, RD, CSM, ConstArg, Diagnose)) |
| 5475 | return false; |
| 5476 | |
| 5477 | // C++11 [class.dtor]p5: |
| 5478 | // A destructor is trivial if [...] |
| 5479 | // -- the destructor is not virtual |
| 5480 | if (CSM == CXXDestructor && MD->isVirtual()) { |
| 5481 | if (Diagnose) |
| 5482 | Diag(MD->getLocation(), diag::note_nontrivial_virtual_dtor) << RD; |
| 5483 | return false; |
| 5484 | } |
| 5485 | |
| 5486 | // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5487 | // A [special member] for class X is trivial if [...] |
| 5488 | // -- class X has no virtual functions and no virtual base classes |
| 5489 | if (CSM != CXXDestructor && MD->getParent()->isDynamicClass()) { |
| 5490 | if (!Diagnose) |
| 5491 | return false; |
| 5492 | |
| 5493 | if (RD->getNumVBases()) { |
| 5494 | // Check for virtual bases. We already know that the corresponding |
| 5495 | // member in all bases is trivial, so vbases must all be direct. |
| 5496 | CXXBaseSpecifier &BS = *RD->vbases_begin(); |
| 5497 | assert(BS.isVirtual()); |
| 5498 | Diag(BS.getLocStart(), diag::note_nontrivial_has_virtual) << RD << 1; |
| 5499 | return false; |
| 5500 | } |
| 5501 | |
| 5502 | // Must have a virtual method. |
| 5503 | for (CXXRecordDecl::method_iterator MI = RD->method_begin(), |
| 5504 | ME = RD->method_end(); MI != ME; ++MI) { |
| 5505 | if (MI->isVirtual()) { |
| 5506 | SourceLocation MLoc = MI->getLocStart(); |
| 5507 | Diag(MLoc, diag::note_nontrivial_has_virtual) << RD << 0; |
| 5508 | return false; |
| 5509 | } |
| 5510 | } |
| 5511 | |
| 5512 | llvm_unreachable("dynamic class with no vbases and no virtual functions"); |
| 5513 | } |
| 5514 | |
| 5515 | // Looks like it's trivial! |
| 5516 | return true; |
| 5517 | } |
| 5518 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5519 | /// \brief Data used with FindHiddenVirtualMethod |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 5520 | namespace { |
| 5521 | struct FindHiddenVirtualMethodData { |
| 5522 | Sema *S; |
| 5523 | CXXMethodDecl *Method; |
| 5524 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5525 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 5526 | }; |
| 5527 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5528 | |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5529 | /// \brief Check whether any most overriden method from MD in Methods |
| 5530 | static bool CheckMostOverridenMethods(const CXXMethodDecl *MD, |
| 5531 | const llvm::SmallPtrSet<const CXXMethodDecl *, 8>& Methods) { |
| 5532 | if (MD->size_overridden_methods() == 0) |
| 5533 | return Methods.count(MD->getCanonicalDecl()); |
| 5534 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 5535 | E = MD->end_overridden_methods(); |
| 5536 | I != E; ++I) |
| 5537 | if (CheckMostOverridenMethods(*I, Methods)) |
| 5538 | return true; |
| 5539 | return false; |
| 5540 | } |
| 5541 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5542 | /// \brief Member lookup function that determines whether a given C++ |
| 5543 | /// method overloads virtual methods in a base class without overriding any, |
| 5544 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 5545 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 5546 | CXXBasePath &Path, |
| 5547 | void *UserData) { |
| 5548 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 5549 | |
| 5550 | FindHiddenVirtualMethodData &Data |
| 5551 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 5552 | |
| 5553 | DeclarationName Name = Data.Method->getDeclName(); |
| 5554 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 5555 | |
| 5556 | bool foundSameNameMethod = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5557 | SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5558 | for (Path.Decls = BaseRecord->lookup(Name); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5559 | !Path.Decls.empty(); |
| 5560 | Path.Decls = Path.Decls.slice(1)) { |
| 5561 | NamedDecl *D = Path.Decls.front(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5562 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 5563 | MD = MD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5564 | foundSameNameMethod = true; |
| 5565 | // Interested only in hidden virtual methods. |
| 5566 | if (!MD->isVirtual()) |
| 5567 | continue; |
| 5568 | // If the method we are checking overrides a method from its base |
| 5569 | // don't warn about the other overloaded methods. |
| 5570 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 5571 | return true; |
| 5572 | // Collect the overload only if its hidden. |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5573 | if (!CheckMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods)) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5574 | overloadedMethods.push_back(MD); |
| 5575 | } |
| 5576 | } |
| 5577 | |
| 5578 | if (foundSameNameMethod) |
| 5579 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 5580 | overloadedMethods.end()); |
| 5581 | return foundSameNameMethod; |
| 5582 | } |
| 5583 | |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5584 | /// \brief Add the most overriden methods from MD to Methods |
| 5585 | static void AddMostOverridenMethods(const CXXMethodDecl *MD, |
| 5586 | llvm::SmallPtrSet<const CXXMethodDecl *, 8>& Methods) { |
| 5587 | if (MD->size_overridden_methods() == 0) |
| 5588 | Methods.insert(MD->getCanonicalDecl()); |
| 5589 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 5590 | E = MD->end_overridden_methods(); |
| 5591 | I != E; ++I) |
| 5592 | AddMostOverridenMethods(*I, Methods); |
| 5593 | } |
| 5594 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5595 | /// \brief See if a method overloads virtual methods in a base class without |
| 5596 | /// overriding any. |
| 5597 | void Sema::DiagnoseHiddenVirtualMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
| 5598 | if (Diags.getDiagnosticLevel(diag::warn_overloaded_virtual, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5599 | MD->getLocation()) == DiagnosticsEngine::Ignored) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5600 | return; |
Benjamin Kramer | c470442 | 2012-05-19 16:03:58 +0000 | [diff] [blame] | 5601 | if (!MD->getDeclName().isIdentifier()) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5602 | return; |
| 5603 | |
| 5604 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 5605 | /*bool RecordPaths=*/false, |
| 5606 | /*bool DetectVirtual=*/false); |
| 5607 | FindHiddenVirtualMethodData Data; |
| 5608 | Data.Method = MD; |
| 5609 | Data.S = this; |
| 5610 | |
| 5611 | // Keep the base methods that were overriden or introduced in the subclass |
| 5612 | // by 'using' in a set. A base method not in this set is hidden. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5613 | DeclContext::lookup_result R = DC->lookup(MD->getDeclName()); |
| 5614 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 5615 | NamedDecl *ND = *I; |
| 5616 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*I)) |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5617 | ND = shad->getTargetDecl(); |
| 5618 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 5619 | AddMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5620 | } |
| 5621 | |
| 5622 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths) && |
| 5623 | !Data.OverloadedMethods.empty()) { |
| 5624 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 5625 | << MD << (Data.OverloadedMethods.size() > 1); |
| 5626 | |
| 5627 | for (unsigned i = 0, e = Data.OverloadedMethods.size(); i != e; ++i) { |
| 5628 | CXXMethodDecl *overloadedMD = Data.OverloadedMethods[i]; |
Richard Trieu | f608aff | 2013-04-05 23:02:24 +0000 | [diff] [blame] | 5629 | PartialDiagnostic PD = PDiag( |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5630 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
Richard Trieu | f608aff | 2013-04-05 23:02:24 +0000 | [diff] [blame] | 5631 | HandleFunctionTypeMismatch(PD, MD->getType(), overloadedMD->getType()); |
| 5632 | Diag(overloadedMD->getLocation(), PD); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5633 | } |
| 5634 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 5635 | } |
| 5636 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5637 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5638 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5639 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 5640 | SourceLocation RBrac, |
| 5641 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5642 | if (!TagDecl) |
| 5643 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5644 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5645 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 5646 | |
Rafael Espindola | f729ce0 | 2012-07-12 04:32:30 +0000 | [diff] [blame] | 5647 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
| 5648 | if (l->getKind() != AttributeList::AT_Visibility) |
| 5649 | continue; |
| 5650 | l->setInvalid(); |
| 5651 | Diag(l->getLoc(), diag::warn_attribute_after_definition_ignored) << |
| 5652 | l->getName(); |
| 5653 | } |
| 5654 | |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 5655 | ActOnFields(S, RLoc, TagDecl, llvm::makeArrayRef( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5656 | // strict aliasing violation! |
| 5657 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 5658 | FieldCollector->getCurNumFields()), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 5659 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5660 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5661 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5662 | } |
| 5663 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5664 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 5665 | /// special functions, such as the default constructor, copy |
| 5666 | /// constructor, or destructor, to the given C++ class (C++ |
| 5667 | /// [special]p1). This routine can only be executed just before the |
| 5668 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5669 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5670 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5671 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5672 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5673 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5674 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5675 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5676 | // If the properties or semantics of the copy constructor couldn't be |
| 5677 | // determined while the class was being declared, force a declaration |
| 5678 | // of it now. |
| 5679 | if (ClassDecl->needsOverloadResolutionForCopyConstructor()) |
| 5680 | DeclareImplicitCopyConstructor(ClassDecl); |
| 5681 | } |
| 5682 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5683 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveConstructor()) { |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 5684 | ++ASTContext::NumImplicitMoveConstructors; |
| 5685 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5686 | if (ClassDecl->needsOverloadResolutionForMoveConstructor()) |
| 5687 | DeclareImplicitMoveConstructor(ClassDecl); |
| 5688 | } |
| 5689 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5690 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 5691 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5692 | |
| 5693 | // If we have a dynamic class, then the copy assignment operator may be |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5694 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5695 | // it shows up in the right place in the vtable and that we diagnose |
| 5696 | // problems with the implicit exception specification. |
| 5697 | if (ClassDecl->isDynamicClass() || |
| 5698 | ClassDecl->needsOverloadResolutionForCopyAssignment()) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5699 | DeclareImplicitCopyAssignment(ClassDecl); |
| 5700 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5701 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5702 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveAssignment()) { |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 5703 | ++ASTContext::NumImplicitMoveAssignmentOperators; |
| 5704 | |
| 5705 | // Likewise for the move assignment operator. |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5706 | if (ClassDecl->isDynamicClass() || |
| 5707 | ClassDecl->needsOverloadResolutionForMoveAssignment()) |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 5708 | DeclareImplicitMoveAssignment(ClassDecl); |
| 5709 | } |
| 5710 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5711 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 5712 | ++ASTContext::NumImplicitDestructors; |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5713 | |
| 5714 | // If we have a dynamic class, then the destructor may be virtual, so we |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5715 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 5716 | // shows up in the right place in the vtable and that we diagnose problems |
| 5717 | // with the implicit exception specification. |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5718 | if (ClassDecl->isDynamicClass() || |
| 5719 | ClassDecl->needsOverloadResolutionForDestructor()) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5720 | DeclareImplicitDestructor(ClassDecl); |
| 5721 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5722 | } |
| 5723 | |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 5724 | void Sema::ActOnReenterDeclaratorTemplateScope(Scope *S, DeclaratorDecl *D) { |
| 5725 | if (!D) |
| 5726 | return; |
| 5727 | |
| 5728 | int NumParamList = D->getNumTemplateParameterLists(); |
| 5729 | for (int i = 0; i < NumParamList; i++) { |
| 5730 | TemplateParameterList* Params = D->getTemplateParameterList(i); |
| 5731 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5732 | ParamEnd = Params->end(); |
| 5733 | Param != ParamEnd; ++Param) { |
| 5734 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 5735 | if (Named->getDeclName()) { |
| 5736 | S->AddDecl(Named); |
| 5737 | IdResolver.AddDecl(Named); |
| 5738 | } |
| 5739 | } |
| 5740 | } |
| 5741 | } |
| 5742 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5743 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 5744 | if (!D) |
| 5745 | return; |
| 5746 | |
| 5747 | TemplateParameterList *Params = 0; |
| 5748 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 5749 | Params = Template->getTemplateParameters(); |
| 5750 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 5751 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 5752 | Params = PartialSpec->getTemplateParameters(); |
| 5753 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 5754 | return; |
| 5755 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 5756 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5757 | ParamEnd = Params->end(); |
| 5758 | Param != ParamEnd; ++Param) { |
| 5759 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 5760 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5761 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 5762 | IdResolver.AddDecl(Named); |
| 5763 | } |
| 5764 | } |
| 5765 | } |
| 5766 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5767 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5768 | if (!RecordD) return; |
| 5769 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5770 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5771 | PushDeclContext(S, Record); |
| 5772 | } |
| 5773 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5774 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5775 | if (!RecordD) return; |
| 5776 | PopDeclContext(); |
| 5777 | } |
| 5778 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5779 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 5780 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 5781 | /// parsing those parts of the given Method declaration that could |
| 5782 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 5783 | /// arguments. This action should enter the scope of the given |
| 5784 | /// Method declaration as if we had just parsed the qualified method |
| 5785 | /// name. However, it should not bring the parameters into scope; |
| 5786 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5787 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5788 | } |
| 5789 | |
| 5790 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 5791 | /// C++ method declaration. We're (re-)introducing the given |
| 5792 | /// function parameter into scope for use in parsing later parts of |
| 5793 | /// the method declaration. For example, we could see an |
| 5794 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5795 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5796 | if (!ParamD) |
| 5797 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5798 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5799 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5800 | |
| 5801 | // If this parameter has an unparsed default argument, clear it out |
| 5802 | // to make way for the parsed default argument. |
| 5803 | if (Param->hasUnparsedDefaultArg()) |
| 5804 | Param->setDefaultArg(0); |
| 5805 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5806 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5807 | if (Param->getDeclName()) |
| 5808 | IdResolver.AddDecl(Param); |
| 5809 | } |
| 5810 | |
| 5811 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 5812 | /// processing the delayed method declaration for Method. The method |
| 5813 | /// declaration is now considered finished. There may be a separate |
| 5814 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 5815 | /// immediately!) for this method, if it was also defined inside the |
| 5816 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5817 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5818 | if (!MethodD) |
| 5819 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5820 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 5821 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5822 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5823 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5824 | |
| 5825 | // Now that we have our default arguments, check the constructor |
| 5826 | // again. It could produce additional diagnostics or affect whether |
| 5827 | // the class has implicitly-declared destructors, among other |
| 5828 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5829 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 5830 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5831 | |
| 5832 | // Check the default arguments, which we may have added. |
| 5833 | if (!Method->isInvalidDecl()) |
| 5834 | CheckCXXDefaultArguments(Method); |
| 5835 | } |
| 5836 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5837 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5838 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5839 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5840 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 5841 | /// will be updated to reflect a well-formed type for the constructor and |
| 5842 | /// returned. |
| 5843 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5844 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5845 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5846 | |
| 5847 | // C++ [class.ctor]p3: |
| 5848 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 5849 | // constructor can be invoked for a const, volatile or const |
| 5850 | // volatile object. A constructor shall not be declared const, |
| 5851 | // volatile, or const volatile (9.3.2). |
| 5852 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5853 | if (!D.isInvalidType()) |
| 5854 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 5855 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 5856 | << SourceRange(D.getIdentifierLoc()); |
| 5857 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5858 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5859 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5860 | if (!D.isInvalidType()) |
| 5861 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 5862 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 5863 | << SourceRange(D.getIdentifierLoc()); |
| 5864 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5865 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5866 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5867 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5868 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5869 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5870 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5871 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 5872 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5873 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5874 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 5875 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5876 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5877 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 5878 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5879 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5880 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5881 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5882 | // C++0x [class.ctor]p4: |
| 5883 | // A constructor shall not be declared with a ref-qualifier. |
| 5884 | if (FTI.hasRefQualifier()) { |
| 5885 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 5886 | << FTI.RefQualifierIsLValueRef |
| 5887 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 5888 | D.setInvalidType(); |
| 5889 | } |
| 5890 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5891 | // Rebuild the function type "R" without any type qualifiers (in |
| 5892 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5893 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5894 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5895 | if (Proto->getResultType() == Context.VoidTy && !D.isInvalidType()) |
| 5896 | return R; |
| 5897 | |
| 5898 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 5899 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5900 | EPI.RefQualifier = RQ_None; |
| 5901 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 5902 | return Context.getFunctionType(Context.VoidTy, Proto->getArgTypes(), EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5903 | } |
| 5904 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5905 | /// CheckConstructor - Checks a fully-formed constructor for |
| 5906 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 5907 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5908 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5909 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 5910 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 5911 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5912 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5913 | |
| 5914 | // C++ [class.copy]p3: |
| 5915 | // A declaration of a constructor for a class X is ill-formed if |
| 5916 | // its first parameter is of type (optionally cv-qualified) X and |
| 5917 | // either there are no other parameters or else all other |
| 5918 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 5919 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5920 | ((Constructor->getNumParams() == 1) || |
| 5921 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5922 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 5923 | Constructor->getTemplateSpecializationKind() |
| 5924 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5925 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 5926 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 5927 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5928 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 5929 | const char *ConstRef |
| 5930 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 5931 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5932 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 5933 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5934 | |
| 5935 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 5936 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5937 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5938 | } |
| 5939 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5940 | } |
| 5941 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 5942 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 5943 | /// well-formedness, issuing any diagnostics required. Returns true |
| 5944 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5945 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5946 | CXXRecordDecl *RD = Destructor->getParent(); |
| 5947 | |
Peter Collingbourne | f51cfb8 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 5948 | if (!Destructor->getOperatorDelete() && Destructor->isVirtual()) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5949 | SourceLocation Loc; |
| 5950 | |
| 5951 | if (!Destructor->isImplicit()) |
| 5952 | Loc = Destructor->getLocation(); |
| 5953 | else |
| 5954 | Loc = RD->getLocation(); |
| 5955 | |
| 5956 | // If we have a virtual destructor, look up the deallocation function |
| 5957 | FunctionDecl *OperatorDelete = 0; |
| 5958 | DeclarationName Name = |
| 5959 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5960 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5961 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 5962 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5963 | MarkFunctionReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5964 | |
| 5965 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5966 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5967 | |
| 5968 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5969 | } |
| 5970 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5971 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5972 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 5973 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 5974 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5975 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5976 | } |
| 5977 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5978 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 5979 | /// the well-formednes of the destructor declarator @p D with type @p |
| 5980 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5981 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 5982 | /// will be updated to reflect a well-formed type for the destructor and |
| 5983 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5984 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5985 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5986 | // C++ [class.dtor]p1: |
| 5987 | // [...] A typedef-name that names a class is a class-name |
| 5988 | // (7.1.3); however, a typedef-name that names a class shall not |
| 5989 | // be used as the identifier in the declarator for a destructor |
| 5990 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5991 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5992 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5993 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5994 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5995 | else if (const TemplateSpecializationType *TST = |
| 5996 | DeclaratorType->getAs<TemplateSpecializationType>()) |
| 5997 | if (TST->isTypeAlias()) |
| 5998 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
| 5999 | << DeclaratorType << 1; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6000 | |
| 6001 | // C++ [class.dtor]p2: |
| 6002 | // A destructor is used to destroy objects of its class type. A |
| 6003 | // destructor takes no parameters, and no return type can be |
| 6004 | // specified for it (not even void). The address of a destructor |
| 6005 | // shall not be taken. A destructor shall not be static. A |
| 6006 | // destructor can be invoked for a const, volatile or const |
| 6007 | // volatile object. A destructor shall not be declared const, |
| 6008 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6009 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6010 | if (!D.isInvalidType()) |
| 6011 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 6012 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6013 | << SourceRange(D.getIdentifierLoc()) |
| 6014 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6015 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6016 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6017 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6018 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6019 | // Destructors don't have return types, but the parser will |
| 6020 | // happily parse something like: |
| 6021 | // |
| 6022 | // class X { |
| 6023 | // float ~X(); |
| 6024 | // }; |
| 6025 | // |
| 6026 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6027 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 6028 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6029 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6030 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6031 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6032 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6033 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6034 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6035 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6036 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6037 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6038 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6039 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6040 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6041 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6042 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6043 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6044 | } |
| 6045 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6046 | // C++0x [class.dtor]p2: |
| 6047 | // A destructor shall not be declared with a ref-qualifier. |
| 6048 | if (FTI.hasRefQualifier()) { |
| 6049 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 6050 | << FTI.RefQualifierIsLValueRef |
| 6051 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 6052 | D.setInvalidType(); |
| 6053 | } |
| 6054 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6055 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 6056 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6057 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 6058 | |
| 6059 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6060 | FTI.freeArgs(); |
| 6061 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6062 | } |
| 6063 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6064 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6065 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6066 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6067 | D.setInvalidType(); |
| 6068 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6069 | |
| 6070 | // Rebuild the function type "R" without any type qualifiers or |
| 6071 | // parameters (in case any of the errors above fired) and with |
| 6072 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6073 | // types. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6074 | if (!D.isInvalidType()) |
| 6075 | return R; |
| 6076 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6077 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6078 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 6079 | EPI.Variadic = false; |
| 6080 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6081 | EPI.RefQualifier = RQ_None; |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 6082 | return Context.getFunctionType(Context.VoidTy, None, EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6083 | } |
| 6084 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6085 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 6086 | /// well-formednes of the conversion function declarator @p D with |
| 6087 | /// type @p R. If there are any errors in the declarator, this routine |
| 6088 | /// will emit diagnostics and return true. Otherwise, it will return |
| 6089 | /// false. Either way, the type @p R will be updated to reflect a |
| 6090 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6091 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6092 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6093 | // C++ [class.conv.fct]p1: |
| 6094 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6095 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6096 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6097 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6098 | if (!D.isInvalidType()) |
| 6099 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
Eli Friedman | 4cde94a | 2013-06-20 20:58:02 +0000 | [diff] [blame] | 6100 | << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 6101 | << D.getName().getSourceRange(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6102 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6103 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6104 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6105 | |
| 6106 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 6107 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6108 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6109 | // Conversion functions don't have return types, but the parser will |
| 6110 | // happily parse something like: |
| 6111 | // |
| 6112 | // class X { |
| 6113 | // float operator bool(); |
| 6114 | // }; |
| 6115 | // |
| 6116 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6117 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 6118 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6119 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6120 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6121 | } |
| 6122 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6123 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 6124 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6125 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6126 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6127 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 6128 | |
| 6129 | // Delete the parameters. |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6130 | D.getFunctionTypeInfo().freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6131 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6132 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6133 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6134 | D.setInvalidType(); |
| 6135 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6136 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6137 | // Diagnose "&operator bool()" and other such nonsense. This |
| 6138 | // is actually a gcc extension which we don't support. |
| 6139 | if (Proto->getResultType() != ConvType) { |
| 6140 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 6141 | << Proto->getResultType(); |
| 6142 | D.setInvalidType(); |
| 6143 | ConvType = Proto->getResultType(); |
| 6144 | } |
| 6145 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6146 | // C++ [class.conv.fct]p4: |
| 6147 | // The conversion-type-id shall not represent a function type nor |
| 6148 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6149 | if (ConvType->isArrayType()) { |
| 6150 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 6151 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6152 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6153 | } else if (ConvType->isFunctionType()) { |
| 6154 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 6155 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6156 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6157 | } |
| 6158 | |
| 6159 | // Rebuild the function type "R" without any parameters (in case any |
| 6160 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6161 | // return type. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6162 | if (D.isInvalidType()) |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 6163 | R = Context.getFunctionType(ConvType, None, Proto->getExtProtoInfo()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6164 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6165 | // C++0x explicit conversion operators. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6166 | if (D.getDeclSpec().isExplicitSpecified()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6167 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6168 | getLangOpts().CPlusPlus11 ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6169 | diag::warn_cxx98_compat_explicit_conversion_functions : |
| 6170 | diag::ext_explicit_conversion_functions) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6171 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6172 | } |
| 6173 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6174 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 6175 | /// the declaration of the given C++ conversion function. This routine |
| 6176 | /// is responsible for recording the conversion function in the C++ |
| 6177 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6178 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6179 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 6180 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 6181 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6182 | |
| 6183 | // Make sure we aren't redeclaring the conversion function. |
| 6184 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6185 | |
| 6186 | // C++ [class.conv.fct]p1: |
| 6187 | // [...] A conversion function is never used to convert a |
| 6188 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 6189 | // same object type (or a reference to it), to a (possibly |
| 6190 | // cv-qualified) base class of that type (or a reference to it), |
| 6191 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6192 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 6193 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6194 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6195 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6196 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6197 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 6198 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 6199 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 6200 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 6201 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6202 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 6203 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 6204 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6205 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6206 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 6207 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6208 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6209 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 6210 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6211 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6212 | } |
| 6213 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 6214 | if (FunctionTemplateDecl *ConversionTemplate |
| 6215 | = Conversion->getDescribedFunctionTemplate()) |
| 6216 | return ConversionTemplate; |
| 6217 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6218 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6219 | } |
| 6220 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6221 | //===----------------------------------------------------------------------===// |
| 6222 | // Namespace Handling |
| 6223 | //===----------------------------------------------------------------------===// |
| 6224 | |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6225 | /// \brief Diagnose a mismatch in 'inline' qualifiers when a namespace is |
| 6226 | /// reopened. |
| 6227 | static void DiagnoseNamespaceInlineMismatch(Sema &S, SourceLocation KeywordLoc, |
| 6228 | SourceLocation Loc, |
| 6229 | IdentifierInfo *II, bool *IsInline, |
| 6230 | NamespaceDecl *PrevNS) { |
| 6231 | assert(*IsInline != PrevNS->isInline()); |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 6232 | |
Richard Smith | c969e6a | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 6233 | // HACK: Work around a bug in libstdc++4.6's <atomic>, where |
| 6234 | // std::__atomic[0,1,2] are defined as non-inline namespaces, then reopened as |
| 6235 | // inline namespaces, with the intention of bringing names into namespace std. |
| 6236 | // |
| 6237 | // We support this just well enough to get that case working; this is not |
| 6238 | // sufficient to support reopening namespaces as inline in general. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6239 | if (*IsInline && II && II->getName().startswith("__atomic") && |
| 6240 | S.getSourceManager().isInSystemHeader(Loc)) { |
Richard Smith | c969e6a | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 6241 | // Mark all prior declarations of the namespace as inline. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6242 | for (NamespaceDecl *NS = PrevNS->getMostRecentDecl(); NS; |
| 6243 | NS = NS->getPreviousDecl()) |
| 6244 | NS->setInline(*IsInline); |
| 6245 | // Patch up the lookup table for the containing namespace. This isn't really |
| 6246 | // correct, but it's good enough for this particular case. |
| 6247 | for (DeclContext::decl_iterator I = PrevNS->decls_begin(), |
| 6248 | E = PrevNS->decls_end(); I != E; ++I) |
| 6249 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*I)) |
| 6250 | PrevNS->getParent()->makeDeclVisibleInContext(ND); |
| 6251 | return; |
| 6252 | } |
| 6253 | |
| 6254 | if (PrevNS->isInline()) |
| 6255 | // The user probably just forgot the 'inline', so suggest that it |
| 6256 | // be added back. |
| 6257 | S.Diag(Loc, diag::warn_inline_namespace_reopened_noninline) |
| 6258 | << FixItHint::CreateInsertion(KeywordLoc, "inline "); |
| 6259 | else |
| 6260 | S.Diag(Loc, diag::err_inline_namespace_mismatch) |
| 6261 | << IsInline; |
| 6262 | |
| 6263 | S.Diag(PrevNS->getLocation(), diag::note_previous_definition); |
| 6264 | *IsInline = PrevNS->isInline(); |
| 6265 | } |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 6266 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6267 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 6268 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6269 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 6270 | SourceLocation InlineLoc, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6271 | SourceLocation NamespaceLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 6272 | SourceLocation IdentLoc, |
| 6273 | IdentifierInfo *II, |
| 6274 | SourceLocation LBrace, |
| 6275 | AttributeList *AttrList) { |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6276 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 6277 | // For anonymous namespace, take the location of the left brace. |
| 6278 | SourceLocation Loc = II ? IdentLoc : LBrace; |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6279 | bool IsInline = InlineLoc.isValid(); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6280 | bool IsInvalid = false; |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6281 | bool IsStd = false; |
| 6282 | bool AddToKnown = false; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6283 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 6284 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6285 | NamespaceDecl *PrevNS = 0; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6286 | if (II) { |
| 6287 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 6288 | // The identifier in an original-namespace-definition shall not |
| 6289 | // have been previously defined in the declarative region in |
| 6290 | // which the original-namespace-definition appears. The |
| 6291 | // identifier in an original-namespace-definition is the name of |
| 6292 | // the namespace. Subsequently in that declarative region, it is |
| 6293 | // treated as an original-namespace-name. |
| 6294 | // |
| 6295 | // Since namespace names are unique in their scope, and we don't |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6296 | // look through using directives, just look for any ordinary names. |
| 6297 | |
| 6298 | const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Member | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6299 | Decl::IDNS_Type | Decl::IDNS_Using | Decl::IDNS_Tag | |
| 6300 | Decl::IDNS_Namespace; |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6301 | NamedDecl *PrevDecl = 0; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 6302 | DeclContext::lookup_result R = CurContext->getRedeclContext()->lookup(II); |
| 6303 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 6304 | ++I) { |
| 6305 | if ((*I)->getIdentifierNamespace() & IDNS) { |
| 6306 | PrevDecl = *I; |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6307 | break; |
| 6308 | } |
| 6309 | } |
| 6310 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6311 | PrevNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl); |
| 6312 | |
| 6313 | if (PrevNS) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6314 | // This is an extended namespace definition. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6315 | if (IsInline != PrevNS->isInline()) |
| 6316 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, Loc, II, |
| 6317 | &IsInline, PrevNS); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6318 | } else if (PrevDecl) { |
| 6319 | // This is an invalid name redefinition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6320 | Diag(Loc, diag::err_redefinition_different_kind) |
| 6321 | << II; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6322 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6323 | IsInvalid = true; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6324 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6325 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6326 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 6327 | // This is the first "real" definition of the namespace "std", so update |
| 6328 | // our cache of the "std" namespace to point at this definition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6329 | PrevNS = getStdNamespace(); |
| 6330 | IsStd = true; |
| 6331 | AddToKnown = !IsInline; |
| 6332 | } else { |
| 6333 | // We've seen this namespace for the first time. |
| 6334 | AddToKnown = !IsInline; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6335 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6336 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6337 | // Anonymous namespaces. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6338 | |
| 6339 | // Determine whether the parent already has an anonymous namespace. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6340 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6341 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6342 | PrevNS = TU->getAnonymousNamespace(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6343 | } else { |
| 6344 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6345 | PrevNS = ND->getAnonymousNamespace(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6346 | } |
| 6347 | |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6348 | if (PrevNS && IsInline != PrevNS->isInline()) |
| 6349 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, NamespaceLoc, II, |
| 6350 | &IsInline, PrevNS); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6351 | } |
| 6352 | |
| 6353 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, IsInline, |
| 6354 | StartLoc, Loc, II, PrevNS); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6355 | if (IsInvalid) |
| 6356 | Namespc->setInvalidDecl(); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6357 | |
| 6358 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 6359 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6360 | // FIXME: Should we be merging attributes? |
| 6361 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 6362 | PushNamespaceVisibilityAttr(Attr, Loc); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6363 | |
| 6364 | if (IsStd) |
| 6365 | StdNamespace = Namespc; |
| 6366 | if (AddToKnown) |
| 6367 | KnownNamespaces[Namespc] = false; |
| 6368 | |
| 6369 | if (II) { |
| 6370 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 6371 | } else { |
| 6372 | // Link the anonymous namespace into its parent. |
| 6373 | DeclContext *Parent = CurContext->getRedeclContext(); |
| 6374 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 6375 | TU->setAnonymousNamespace(Namespc); |
| 6376 | } else { |
| 6377 | cast<NamespaceDecl>(Parent)->setAnonymousNamespace(Namespc); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6378 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6379 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 6380 | CurContext->addDecl(Namespc); |
| 6381 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6382 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 6383 | // behaves as if it were replaced by |
| 6384 | // namespace unique { /* empty body */ } |
| 6385 | // using namespace unique; |
| 6386 | // namespace unique { namespace-body } |
| 6387 | // where all occurrences of 'unique' in a translation unit are |
| 6388 | // replaced by the same identifier and this identifier differs |
| 6389 | // from all other identifiers in the entire program. |
| 6390 | |
| 6391 | // We just create the namespace with an empty name and then add an |
| 6392 | // implicit using declaration, just like the standard suggests. |
| 6393 | // |
| 6394 | // CodeGen enforces the "universally unique" aspect by giving all |
| 6395 | // declarations semantically contained within an anonymous |
| 6396 | // namespace internal linkage. |
| 6397 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6398 | if (!PrevNS) { |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6399 | UsingDirectiveDecl* UD |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6400 | = UsingDirectiveDecl::Create(Context, Parent, |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6401 | /* 'using' */ LBrace, |
| 6402 | /* 'namespace' */ SourceLocation(), |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 6403 | /* qualifier */ NestedNameSpecifierLoc(), |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6404 | /* identifier */ SourceLocation(), |
| 6405 | Namespc, |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6406 | /* Ancestor */ Parent); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6407 | UD->setImplicit(); |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6408 | Parent->addDecl(UD); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6409 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6410 | } |
| 6411 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 6412 | ActOnDocumentableDecl(Namespc); |
| 6413 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6414 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 6415 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6416 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 6417 | // for the namespace has the declarations that showed up in that particular |
| 6418 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6419 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6420 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6421 | } |
| 6422 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6423 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 6424 | /// is a namespace alias, returns the namespace it points to. |
| 6425 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 6426 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 6427 | return AD->getNamespace(); |
| 6428 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 6429 | } |
| 6430 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6431 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 6432 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6433 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6434 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 6435 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6436 | Namespc->setRBraceLoc(RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6437 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 6438 | if (Namespc->hasAttr<VisibilityAttr>()) |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 6439 | PopPragmaVisibility(true, RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6440 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6441 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 6442 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 6443 | return cast_or_null<CXXRecordDecl>( |
| 6444 | StdBadAlloc.get(Context.getExternalSource())); |
| 6445 | } |
| 6446 | |
| 6447 | NamespaceDecl *Sema::getStdNamespace() const { |
| 6448 | return cast_or_null<NamespaceDecl>( |
| 6449 | StdNamespace.get(Context.getExternalSource())); |
| 6450 | } |
| 6451 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6452 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 6453 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 6454 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6455 | if (!StdNamespace) { |
| 6456 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 6457 | StdNamespace = NamespaceDecl::Create(Context, |
| 6458 | Context.getTranslationUnitDecl(), |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6459 | /*Inline=*/false, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6460 | SourceLocation(), SourceLocation(), |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6461 | &PP.getIdentifierTable().get("std"), |
| 6462 | /*PrevDecl=*/0); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 6463 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6464 | } |
| 6465 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 6466 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6467 | } |
| 6468 | |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6469 | bool Sema::isStdInitializerList(QualType Ty, QualType *Element) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6470 | assert(getLangOpts().CPlusPlus && |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6471 | "Looking for std::initializer_list outside of C++."); |
| 6472 | |
| 6473 | // We're looking for implicit instantiations of |
| 6474 | // template <typename E> class std::initializer_list. |
| 6475 | |
| 6476 | if (!StdNamespace) // If we haven't seen namespace std yet, this can't be it. |
| 6477 | return false; |
| 6478 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6479 | ClassTemplateDecl *Template = 0; |
| 6480 | const TemplateArgument *Arguments = 0; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6481 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6482 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6483 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6484 | ClassTemplateSpecializationDecl *Specialization = |
| 6485 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 6486 | if (!Specialization) |
| 6487 | return false; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6488 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6489 | Template = Specialization->getSpecializedTemplate(); |
| 6490 | Arguments = Specialization->getTemplateArgs().data(); |
| 6491 | } else if (const TemplateSpecializationType *TST = |
| 6492 | Ty->getAs<TemplateSpecializationType>()) { |
| 6493 | Template = dyn_cast_or_null<ClassTemplateDecl>( |
| 6494 | TST->getTemplateName().getAsTemplateDecl()); |
| 6495 | Arguments = TST->getArgs(); |
| 6496 | } |
| 6497 | if (!Template) |
| 6498 | return false; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6499 | |
| 6500 | if (!StdInitializerList) { |
| 6501 | // Haven't recognized std::initializer_list yet, maybe this is it. |
| 6502 | CXXRecordDecl *TemplateClass = Template->getTemplatedDecl(); |
| 6503 | if (TemplateClass->getIdentifier() != |
| 6504 | &PP.getIdentifierTable().get("initializer_list") || |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6505 | !getStdNamespace()->InEnclosingNamespaceSetOf( |
| 6506 | TemplateClass->getDeclContext())) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6507 | return false; |
| 6508 | // This is a template called std::initializer_list, but is it the right |
| 6509 | // template? |
| 6510 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6511 | if (Params->getMinRequiredArguments() != 1) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6512 | return false; |
| 6513 | if (!isa<TemplateTypeParmDecl>(Params->getParam(0))) |
| 6514 | return false; |
| 6515 | |
| 6516 | // It's the right template. |
| 6517 | StdInitializerList = Template; |
| 6518 | } |
| 6519 | |
| 6520 | if (Template != StdInitializerList) |
| 6521 | return false; |
| 6522 | |
| 6523 | // This is an instance of std::initializer_list. Find the argument type. |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6524 | if (Element) |
| 6525 | *Element = Arguments[0].getAsType(); |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6526 | return true; |
| 6527 | } |
| 6528 | |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 6529 | static ClassTemplateDecl *LookupStdInitializerList(Sema &S, SourceLocation Loc){ |
| 6530 | NamespaceDecl *Std = S.getStdNamespace(); |
| 6531 | if (!Std) { |
| 6532 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 6533 | return 0; |
| 6534 | } |
| 6535 | |
| 6536 | LookupResult Result(S, &S.PP.getIdentifierTable().get("initializer_list"), |
| 6537 | Loc, Sema::LookupOrdinaryName); |
| 6538 | if (!S.LookupQualifiedName(Result, Std)) { |
| 6539 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 6540 | return 0; |
| 6541 | } |
| 6542 | ClassTemplateDecl *Template = Result.getAsSingle<ClassTemplateDecl>(); |
| 6543 | if (!Template) { |
| 6544 | Result.suppressDiagnostics(); |
| 6545 | // We found something weird. Complain about the first thing we found. |
| 6546 | NamedDecl *Found = *Result.begin(); |
| 6547 | S.Diag(Found->getLocation(), diag::err_malformed_std_initializer_list); |
| 6548 | return 0; |
| 6549 | } |
| 6550 | |
| 6551 | // We found some template called std::initializer_list. Now verify that it's |
| 6552 | // correct. |
| 6553 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6554 | if (Params->getMinRequiredArguments() != 1 || |
| 6555 | !isa<TemplateTypeParmDecl>(Params->getParam(0))) { |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 6556 | S.Diag(Template->getLocation(), diag::err_malformed_std_initializer_list); |
| 6557 | return 0; |
| 6558 | } |
| 6559 | |
| 6560 | return Template; |
| 6561 | } |
| 6562 | |
| 6563 | QualType Sema::BuildStdInitializerList(QualType Element, SourceLocation Loc) { |
| 6564 | if (!StdInitializerList) { |
| 6565 | StdInitializerList = LookupStdInitializerList(*this, Loc); |
| 6566 | if (!StdInitializerList) |
| 6567 | return QualType(); |
| 6568 | } |
| 6569 | |
| 6570 | TemplateArgumentListInfo Args(Loc, Loc); |
| 6571 | Args.addArgument(TemplateArgumentLoc(TemplateArgument(Element), |
| 6572 | Context.getTrivialTypeSourceInfo(Element, |
| 6573 | Loc))); |
| 6574 | return Context.getCanonicalType( |
| 6575 | CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args)); |
| 6576 | } |
| 6577 | |
Sebastian Redl | 98d3606 | 2012-01-17 22:50:14 +0000 | [diff] [blame] | 6578 | bool Sema::isInitListConstructor(const CXXConstructorDecl* Ctor) { |
| 6579 | // C++ [dcl.init.list]p2: |
| 6580 | // A constructor is an initializer-list constructor if its first parameter |
| 6581 | // is of type std::initializer_list<E> or reference to possibly cv-qualified |
| 6582 | // std::initializer_list<E> for some type E, and either there are no other |
| 6583 | // parameters or else all other parameters have default arguments. |
| 6584 | if (Ctor->getNumParams() < 1 || |
| 6585 | (Ctor->getNumParams() > 1 && !Ctor->getParamDecl(1)->hasDefaultArg())) |
| 6586 | return false; |
| 6587 | |
| 6588 | QualType ArgType = Ctor->getParamDecl(0)->getType(); |
| 6589 | if (const ReferenceType *RT = ArgType->getAs<ReferenceType>()) |
| 6590 | ArgType = RT->getPointeeType().getUnqualifiedType(); |
| 6591 | |
| 6592 | return isStdInitializerList(ArgType, 0); |
| 6593 | } |
| 6594 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 6595 | /// \brief Determine whether a using statement is in a context where it will be |
| 6596 | /// apply in all contexts. |
| 6597 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 6598 | switch (CurContext->getDeclKind()) { |
| 6599 | case Decl::TranslationUnit: |
| 6600 | return true; |
| 6601 | case Decl::LinkageSpec: |
| 6602 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 6603 | default: |
| 6604 | return false; |
| 6605 | } |
| 6606 | } |
| 6607 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6608 | namespace { |
| 6609 | |
| 6610 | // Callback to only accept typo corrections that are namespaces. |
| 6611 | class NamespaceValidatorCCC : public CorrectionCandidateCallback { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 6612 | public: |
| 6613 | bool ValidateCandidate(const TypoCorrection &candidate) LLVM_OVERRIDE { |
| 6614 | if (NamedDecl *ND = candidate.getCorrectionDecl()) |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6615 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6616 | return false; |
| 6617 | } |
| 6618 | }; |
| 6619 | |
| 6620 | } |
| 6621 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6622 | static bool TryNamespaceTypoCorrection(Sema &S, LookupResult &R, Scope *Sc, |
| 6623 | CXXScopeSpec &SS, |
| 6624 | SourceLocation IdentLoc, |
| 6625 | IdentifierInfo *Ident) { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6626 | NamespaceValidatorCCC Validator; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6627 | R.clear(); |
| 6628 | if (TypoCorrection Corrected = S.CorrectTypo(R.getLookupNameInfo(), |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6629 | R.getLookupKind(), Sc, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 6630 | Validator)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6631 | std::string CorrectedStr(Corrected.getAsString(S.getLangOpts())); |
| 6632 | std::string CorrectedQuotedStr(Corrected.getQuoted(S.getLangOpts())); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6633 | if (DeclContext *DC = S.computeDeclContext(SS, false)) { |
| 6634 | bool droppedSpecifier = Corrected.WillReplaceSpecifier() && |
| 6635 | Ident->getName().equals(CorrectedStr); |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6636 | S.Diag(IdentLoc, diag::err_using_directive_member_suggest) |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6637 | << Ident << DC << droppedSpecifier << CorrectedQuotedStr |
| 6638 | << SS.getRange() << FixItHint::CreateReplacement( |
| 6639 | Corrected.getCorrectionRange(), CorrectedStr); |
| 6640 | } else { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6641 | S.Diag(IdentLoc, diag::err_using_directive_suggest) |
| 6642 | << Ident << CorrectedQuotedStr |
| 6643 | << FixItHint::CreateReplacement(IdentLoc, CorrectedStr); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6644 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6645 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6646 | S.Diag(Corrected.getCorrectionDecl()->getLocation(), |
| 6647 | diag::note_namespace_defined_here) << CorrectedQuotedStr; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6648 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6649 | R.addDecl(Corrected.getCorrectionDecl()); |
| 6650 | return true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6651 | } |
| 6652 | return false; |
| 6653 | } |
| 6654 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6655 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6656 | SourceLocation UsingLoc, |
| 6657 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6658 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6659 | SourceLocation IdentLoc, |
| 6660 | IdentifierInfo *NamespcName, |
| 6661 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6662 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 6663 | assert(NamespcName && "Invalid NamespcName."); |
| 6664 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 6665 | |
| 6666 | // This can only happen along a recovery path. |
| 6667 | while (S->getFlags() & Scope::TemplateParamScope) |
| 6668 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6669 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6670 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6671 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6672 | NestedNameSpecifier *Qualifier = 0; |
| 6673 | if (SS.isSet()) |
| 6674 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6675 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 6676 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6677 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 6678 | LookupParsedName(R, S, &SS); |
| 6679 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6680 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6681 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6682 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6683 | R.clear(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6684 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 6685 | // "std" hasn't been defined yet, for GCC compatibility. |
| 6686 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 6687 | NamespcName->isStr("std")) { |
| 6688 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 6689 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6690 | R.resolveKind(); |
| 6691 | } |
| 6692 | // Otherwise, attempt typo correction. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6693 | else TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6694 | } |
| 6695 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6696 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6697 | NamedDecl *Named = R.getFoundDecl(); |
| 6698 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 6699 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6700 | // C++ [namespace.udir]p1: |
| 6701 | // A using-directive specifies that the names in the nominated |
| 6702 | // namespace can be used in the scope in which the |
| 6703 | // using-directive appears after the using-directive. During |
| 6704 | // unqualified name lookup (3.4.1), the names appear as if they |
| 6705 | // were declared in the nearest enclosing namespace which |
| 6706 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6707 | // namespace. [Note: in this context, "contains" means "contains |
| 6708 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6709 | |
| 6710 | // Find enclosing context containing both using-directive and |
| 6711 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6712 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6713 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 6714 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 6715 | CommonAncestor = CommonAncestor->getParent(); |
| 6716 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6717 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 6718 | SS.getWithLocInContext(Context), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6719 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 6720 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 6721 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 6722 | !SourceMgr.isFromMainFile(SourceMgr.getExpansionLoc(IdentLoc))) { |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 6723 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 6724 | } |
| 6725 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6726 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6727 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 6728 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6729 | } |
| 6730 | |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 6731 | if (UDir) |
| 6732 | ProcessDeclAttributeList(S, UDir, AttrList); |
| 6733 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6734 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6735 | } |
| 6736 | |
| 6737 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 6738 | // If the scope has an associated entity and the using directive is at |
| 6739 | // namespace or translation unit scope, add the UsingDirectiveDecl into |
| 6740 | // its lookup structure so qualified name lookup can find it. |
| 6741 | DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity()); |
| 6742 | if (Ctx && !Ctx->isFunctionOrMethod()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6743 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6744 | else |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 6745 | // Otherwise, it is at block sope. The using-directives will affect lookup |
| 6746 | // only to the end of the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6747 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6748 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6749 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6750 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6751 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 6752 | AccessSpecifier AS, |
| 6753 | bool HasUsingKeyword, |
| 6754 | SourceLocation UsingLoc, |
| 6755 | CXXScopeSpec &SS, |
| 6756 | UnqualifiedId &Name, |
| 6757 | AttributeList *AttrList, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 6758 | bool HasTypenameKeyword, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 6759 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6760 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6761 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6762 | switch (Name.getKind()) { |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 6763 | case UnqualifiedId::IK_ImplicitSelfParam: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6764 | case UnqualifiedId::IK_Identifier: |
| 6765 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 6766 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6767 | case UnqualifiedId::IK_ConversionFunctionId: |
| 6768 | break; |
| 6769 | |
| 6770 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 6771 | case UnqualifiedId::IK_ConstructorTemplateId: |
Richard Smith | a1366cb | 2012-04-27 19:33:05 +0000 | [diff] [blame] | 6772 | // C++11 inheriting constructors. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6773 | Diag(Name.getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6774 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6775 | diag::warn_cxx98_compat_using_decl_constructor : |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6776 | diag::err_using_decl_constructor) |
| 6777 | << SS.getRange(); |
| 6778 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6779 | if (getLangOpts().CPlusPlus11) break; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6780 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6781 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6782 | |
| 6783 | case UnqualifiedId::IK_DestructorName: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6784 | Diag(Name.getLocStart(), diag::err_using_decl_destructor) |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6785 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6786 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6787 | |
| 6788 | case UnqualifiedId::IK_TemplateId: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6789 | Diag(Name.getLocStart(), diag::err_using_decl_template_id) |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6790 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6791 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6792 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6793 | |
| 6794 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 6795 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6796 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6797 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6798 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6799 | // Warn about access declarations. |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 6800 | if (!HasUsingKeyword) { |
Enea Zaffanella | d4de59d | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 6801 | Diag(Name.getLocStart(), |
Richard Smith | 1b2209f | 2013-06-13 02:12:17 +0000 | [diff] [blame] | 6802 | getLangOpts().CPlusPlus11 ? diag::err_access_decl |
| 6803 | : diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6804 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 6805 | } |
| 6806 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 6807 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 6808 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 6809 | return 0; |
| 6810 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6811 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6812 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6813 | /* IsInstantiation */ false, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 6814 | HasTypenameKeyword, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6815 | if (UD) |
| 6816 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6817 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6818 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6819 | } |
| 6820 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6821 | /// \brief Determine whether a using declaration considers the given |
| 6822 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 6823 | /// the same entity or are both typedefs of the same type. |
| 6824 | static bool |
| 6825 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 6826 | bool &SuppressRedeclaration) { |
| 6827 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 6828 | SuppressRedeclaration = false; |
| 6829 | return true; |
| 6830 | } |
| 6831 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6832 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
| 6833 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6834 | SuppressRedeclaration = true; |
| 6835 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 6836 | TD2->getUnderlyingType()); |
| 6837 | } |
| 6838 | |
| 6839 | return false; |
| 6840 | } |
| 6841 | |
| 6842 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6843 | /// Determines whether to create a using shadow decl for a particular |
| 6844 | /// decl, given the set of decls existing prior to this using lookup. |
| 6845 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 6846 | const LookupResult &Previous) { |
| 6847 | // Diagnose finding a decl which is not from a base class of the |
| 6848 | // current class. We do this now because there are cases where this |
| 6849 | // function will silently decide not to build a shadow decl, which |
| 6850 | // will pre-empt further diagnostics. |
| 6851 | // |
| 6852 | // We don't need to do this in C++0x because we do the check once on |
| 6853 | // the qualifier. |
| 6854 | // |
| 6855 | // FIXME: diagnose the following if we care enough: |
| 6856 | // struct A { int foo; }; |
| 6857 | // struct B : A { using A::foo; }; |
| 6858 | // template <class T> struct C : A {}; |
| 6859 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 6860 | // This is invalid (during instantiation) in C++03 because B::foo |
| 6861 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 6862 | // We can't diagnose it immediately because C<T> is an unknown |
| 6863 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 6864 | // to A::foo, which will look well-formed when we instantiate. |
| 6865 | // The right solution is to not collapse the shadow-decl chain. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6866 | if (!getLangOpts().CPlusPlus11 && CurContext->isRecord()) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6867 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 6868 | |
| 6869 | // Handle enums and anonymous structs. |
| 6870 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 6871 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 6872 | while (OrigRec->isAnonymousStructOrUnion()) |
| 6873 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 6874 | |
| 6875 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 6876 | if (OrigDC == CurContext) { |
| 6877 | Diag(Using->getLocation(), |
| 6878 | diag::err_using_decl_nested_name_specifier_is_current_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6879 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6880 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 6881 | return true; |
| 6882 | } |
| 6883 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6884 | Diag(Using->getQualifierLoc().getBeginLoc(), |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6885 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6886 | << Using->getQualifier() |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6887 | << cast<CXXRecordDecl>(CurContext) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6888 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6889 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 6890 | return true; |
| 6891 | } |
| 6892 | } |
| 6893 | |
| 6894 | if (Previous.empty()) return false; |
| 6895 | |
| 6896 | NamedDecl *Target = Orig; |
| 6897 | if (isa<UsingShadowDecl>(Target)) |
| 6898 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 6899 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 6900 | // If the target happens to be one of the previous declarations, we |
| 6901 | // don't have a conflict. |
| 6902 | // |
| 6903 | // FIXME: but we might be increasing its access, in which case we |
| 6904 | // should redeclare it. |
| 6905 | NamedDecl *NonTag = 0, *Tag = 0; |
| 6906 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6907 | I != E; ++I) { |
| 6908 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6909 | bool Result; |
| 6910 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 6911 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 6912 | |
| 6913 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 6914 | } |
| 6915 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6916 | if (Target->isFunctionOrFunctionTemplate()) { |
| 6917 | FunctionDecl *FD; |
| 6918 | if (isa<FunctionTemplateDecl>(Target)) |
| 6919 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 6920 | else |
| 6921 | FD = cast<FunctionDecl>(Target); |
| 6922 | |
| 6923 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 6924 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6925 | case Ovl_Overload: |
| 6926 | return false; |
| 6927 | |
| 6928 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6929 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6930 | break; |
| 6931 | |
| 6932 | // We found a decl with the exact signature. |
| 6933 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6934 | // If we're in a record, we want to hide the target, so we |
| 6935 | // return true (without a diagnostic) to tell the caller not to |
| 6936 | // build a shadow decl. |
| 6937 | if (CurContext->isRecord()) |
| 6938 | return true; |
| 6939 | |
| 6940 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6941 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6942 | break; |
| 6943 | } |
| 6944 | |
| 6945 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6946 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 6947 | return true; |
| 6948 | } |
| 6949 | |
| 6950 | // Target is not a function. |
| 6951 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6952 | if (isa<TagDecl>(Target)) { |
| 6953 | // No conflict between a tag and a non-tag. |
| 6954 | if (!Tag) return false; |
| 6955 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6956 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6957 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6958 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 6959 | return true; |
| 6960 | } |
| 6961 | |
| 6962 | // No conflict between a tag and a non-tag. |
| 6963 | if (!NonTag) return false; |
| 6964 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6965 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6966 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6967 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 6968 | return true; |
| 6969 | } |
| 6970 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6971 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6972 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6973 | UsingDecl *UD, |
| 6974 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6975 | |
| 6976 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6977 | NamedDecl *Target = Orig; |
| 6978 | if (isa<UsingShadowDecl>(Target)) { |
| 6979 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 6980 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6981 | } |
| 6982 | |
| 6983 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6984 | = UsingShadowDecl::Create(Context, CurContext, |
| 6985 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6986 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 6987 | |
| 6988 | Shadow->setAccess(UD->getAccess()); |
| 6989 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 6990 | Shadow->setInvalidDecl(); |
| 6991 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6992 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6993 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6994 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6995 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6996 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6997 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6998 | return Shadow; |
| 6999 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7000 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7001 | /// Hides a using shadow declaration. This is required by the current |
| 7002 | /// using-decl implementation when a resolvable using declaration in a |
| 7003 | /// class is followed by a declaration which would hide or override |
| 7004 | /// one or more of the using decl's targets; for example: |
| 7005 | /// |
| 7006 | /// struct Base { void foo(int); }; |
| 7007 | /// struct Derived : Base { |
| 7008 | /// using Base::foo; |
| 7009 | /// void foo(int); |
| 7010 | /// }; |
| 7011 | /// |
| 7012 | /// The governing language is C++03 [namespace.udecl]p12: |
| 7013 | /// |
| 7014 | /// When a using-declaration brings names from a base class into a |
| 7015 | /// derived class scope, member functions in the derived class |
| 7016 | /// override and/or hide member functions with the same name and |
| 7017 | /// parameter types in a base class (rather than conflicting). |
| 7018 | /// |
| 7019 | /// There are two ways to implement this: |
| 7020 | /// (1) optimistically create shadow decls when they're not hidden |
| 7021 | /// by existing declarations, or |
| 7022 | /// (2) don't create any shadow decls (or at least don't make them |
| 7023 | /// visible) until we've fully parsed/instantiated the class. |
| 7024 | /// The problem with (1) is that we might have to retroactively remove |
| 7025 | /// a shadow decl, which requires several O(n) operations because the |
| 7026 | /// decl structures are (very reasonably) not designed for removal. |
| 7027 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 7028 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 7029 | if (Shadow->getDeclName().getNameKind() == |
| 7030 | DeclarationName::CXXConversionFunctionName) |
| 7031 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 7032 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7033 | // Remove it from the DeclContext... |
| 7034 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7035 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7036 | // ...and the scope, if applicable... |
| 7037 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7038 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7039 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7040 | } |
| 7041 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7042 | // ...and the using decl. |
| 7043 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 7044 | |
| 7045 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 7046 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7047 | } |
| 7048 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7049 | namespace { |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7050 | class UsingValidatorCCC : public CorrectionCandidateCallback { |
| 7051 | public: |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7052 | UsingValidatorCCC(bool HasTypenameKeyword, bool IsInstantiation) |
| 7053 | : HasTypenameKeyword(HasTypenameKeyword), |
| 7054 | IsInstantiation(IsInstantiation) {} |
| 7055 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7056 | bool ValidateCandidate(const TypoCorrection &Candidate) LLVM_OVERRIDE { |
| 7057 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
| 7058 | |
| 7059 | // Keywords are not valid here. |
| 7060 | if (!ND || isa<NamespaceDecl>(ND)) |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7061 | return false; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7062 | |
| 7063 | // Completely unqualified names are invalid for a 'using' declaration. |
| 7064 | if (Candidate.WillReplaceSpecifier() && !Candidate.getCorrectionSpecifier()) |
| 7065 | return false; |
| 7066 | |
| 7067 | if (isa<TypeDecl>(ND)) |
| 7068 | return HasTypenameKeyword || !IsInstantiation; |
| 7069 | |
| 7070 | return !HasTypenameKeyword; |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7071 | } |
| 7072 | |
| 7073 | private: |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7074 | bool HasTypenameKeyword; |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7075 | bool IsInstantiation; |
| 7076 | }; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7077 | } // end anonymous namespace |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7078 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7079 | /// Builds a using declaration. |
| 7080 | /// |
| 7081 | /// \param IsInstantiation - Whether this call arises from an |
| 7082 | /// instantiation of an unresolved using declaration. We treat |
| 7083 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7084 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 7085 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7086 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7087 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7088 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7089 | bool IsInstantiation, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7090 | bool HasTypenameKeyword, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7091 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7092 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7093 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7094 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 7095 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 7096 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7097 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7098 | if (SS.isEmpty()) { |
| 7099 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7100 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7102 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7103 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7104 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7105 | ForRedeclaration); |
| 7106 | Previous.setHideTags(false); |
| 7107 | if (S) { |
| 7108 | LookupName(Previous, S); |
| 7109 | |
| 7110 | // It is really dumb that we have to do this. |
| 7111 | LookupResult::Filter F = Previous.makeFilter(); |
| 7112 | while (F.hasNext()) { |
| 7113 | NamedDecl *D = F.next(); |
| 7114 | if (!isDeclInScope(D, CurContext, S)) |
| 7115 | F.erase(); |
| 7116 | } |
| 7117 | F.done(); |
| 7118 | } else { |
| 7119 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 7120 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 7121 | LookupQualifiedName(Previous, CurContext); |
| 7122 | } |
| 7123 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7124 | // Check for invalid redeclarations. |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7125 | if (CheckUsingDeclRedeclaration(UsingLoc, HasTypenameKeyword, |
| 7126 | SS, IdentLoc, Previous)) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7127 | return 0; |
| 7128 | |
| 7129 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7130 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 7131 | return 0; |
| 7132 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 7133 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7134 | NamedDecl *D; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7135 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 7136 | if (!LookupContext) { |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7137 | if (HasTypenameKeyword) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7138 | // FIXME: not all declaration name kinds are legal here |
| 7139 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 7140 | UsingLoc, TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7141 | QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7142 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7143 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7144 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 7145 | QualifierLoc, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7146 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7147 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7148 | D = UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7149 | NameInfo, HasTypenameKeyword); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 7150 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7151 | D->setAccess(AS); |
| 7152 | CurContext->addDecl(D); |
| 7153 | |
| 7154 | if (!LookupContext) return D; |
| 7155 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7156 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7157 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7158 | UD->setInvalidDecl(); |
| 7159 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7160 | } |
| 7161 | |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7162 | // The normal rules do not apply to inheriting constructor declarations. |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7163 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7164 | if (CheckInheritingConstructorUsingDecl(UD)) |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 7165 | UD->setInvalidDecl(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7166 | return UD; |
| 7167 | } |
| 7168 | |
| 7169 | // Otherwise, look up the target name. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7170 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7171 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7172 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7173 | // Unlike most lookups, we don't always want to hide tag |
| 7174 | // declarations: tag names are visible through the using declaration |
| 7175 | // even if hidden by ordinary names, *except* in a dependent context |
| 7176 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7177 | if (!IsInstantiation) |
| 7178 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7179 | |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 7180 | // For the purposes of this lookup, we have a base object type |
| 7181 | // equal to that of the current context. |
| 7182 | if (CurContext->isRecord()) { |
| 7183 | R.setBaseObjectType( |
| 7184 | Context.getTypeDeclType(cast<CXXRecordDecl>(CurContext))); |
| 7185 | } |
| 7186 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7187 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7188 | |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7189 | // Try to correct typos if possible. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7190 | if (R.empty()) { |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7191 | UsingValidatorCCC CCC(HasTypenameKeyword, IsInstantiation); |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7192 | if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(), |
| 7193 | R.getLookupKind(), S, &SS, CCC)){ |
| 7194 | // We reject any correction for which ND would be NULL. |
| 7195 | NamedDecl *ND = Corrected.getCorrectionDecl(); |
| 7196 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 7197 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts())); |
| 7198 | R.setLookupName(Corrected.getCorrection()); |
| 7199 | R.addDecl(ND); |
| 7200 | // We reject candidates where droppedSpecifier == true, hence the |
| 7201 | // literal '0' below. |
| 7202 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 7203 | << NameInfo.getName() << LookupContext << 0 |
| 7204 | << CorrectedQuotedStr << SS.getRange() |
| 7205 | << FixItHint::CreateReplacement(Corrected.getCorrectionRange(), |
| 7206 | CorrectedStr); |
| 7207 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 7208 | << CorrectedQuotedStr; |
| 7209 | } else { |
| 7210 | Diag(IdentLoc, diag::err_no_member) |
| 7211 | << NameInfo.getName() << LookupContext << SS.getRange(); |
| 7212 | UD->setInvalidDecl(); |
| 7213 | return UD; |
| 7214 | } |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7215 | } |
| 7216 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7217 | if (R.isAmbiguous()) { |
| 7218 | UD->setInvalidDecl(); |
| 7219 | return UD; |
| 7220 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7221 | |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7222 | if (HasTypenameKeyword) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7223 | // 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] | 7224 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7225 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 7226 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 7227 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 7228 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7229 | UD->setInvalidDecl(); |
| 7230 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7231 | } |
| 7232 | } else { |
| 7233 | // If we asked for a non-typename and we got a type, error out, |
| 7234 | // but only if this is an instantiation of an unresolved using |
| 7235 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7236 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7237 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 7238 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7239 | UD->setInvalidDecl(); |
| 7240 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7241 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7242 | } |
| 7243 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 7244 | // C++0x N2914 [namespace.udecl]p6: |
| 7245 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7246 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 7247 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 7248 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7249 | UD->setInvalidDecl(); |
| 7250 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 7251 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7252 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7253 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 7254 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 7255 | BuildUsingShadowDecl(S, UD, *I); |
| 7256 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7257 | |
| 7258 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7259 | } |
| 7260 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7261 | /// Additional checks for a using declaration referring to a constructor name. |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7262 | bool Sema::CheckInheritingConstructorUsingDecl(UsingDecl *UD) { |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7263 | assert(!UD->hasTypename() && "expecting a constructor name"); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7264 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7265 | const Type *SourceType = UD->getQualifier()->getAsType(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7266 | assert(SourceType && |
| 7267 | "Using decl naming constructor doesn't have type in scope spec."); |
| 7268 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 7269 | |
| 7270 | // Check whether the named type is a direct base class. |
| 7271 | CanQualType CanonicalSourceType = SourceType->getCanonicalTypeUnqualified(); |
| 7272 | CXXRecordDecl::base_class_iterator BaseIt, BaseE; |
| 7273 | for (BaseIt = TargetClass->bases_begin(), BaseE = TargetClass->bases_end(); |
| 7274 | BaseIt != BaseE; ++BaseIt) { |
| 7275 | CanQualType BaseType = BaseIt->getType()->getCanonicalTypeUnqualified(); |
| 7276 | if (CanonicalSourceType == BaseType) |
| 7277 | break; |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7278 | if (BaseIt->getType()->isDependentType()) |
| 7279 | break; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7280 | } |
| 7281 | |
| 7282 | if (BaseIt == BaseE) { |
| 7283 | // Did not find SourceType in the bases. |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7284 | Diag(UD->getUsingLoc(), |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7285 | diag::err_using_decl_constructor_not_in_direct_base) |
| 7286 | << UD->getNameInfo().getSourceRange() |
| 7287 | << QualType(SourceType, 0) << TargetClass; |
| 7288 | return true; |
| 7289 | } |
| 7290 | |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7291 | if (!CurContext->isDependentContext()) |
| 7292 | BaseIt->setInheritConstructors(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7293 | |
| 7294 | return false; |
| 7295 | } |
| 7296 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7297 | /// Checks that the given using declaration is not an invalid |
| 7298 | /// redeclaration. Note that this is checking only for the using decl |
| 7299 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 7300 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7301 | bool HasTypenameKeyword, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7302 | const CXXScopeSpec &SS, |
| 7303 | SourceLocation NameLoc, |
| 7304 | const LookupResult &Prev) { |
| 7305 | // C++03 [namespace.udecl]p8: |
| 7306 | // C++0x [namespace.udecl]p10: |
| 7307 | // A using-declaration is a declaration and can therefore be used |
| 7308 | // repeatedly where (and only where) multiple declarations are |
| 7309 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 7310 | // |
John McCall | 8a72621 | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 7311 | // That's in non-member contexts. |
| 7312 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7313 | return false; |
| 7314 | |
| 7315 | NestedNameSpecifier *Qual |
| 7316 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 7317 | |
| 7318 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 7319 | NamedDecl *D = *I; |
| 7320 | |
| 7321 | bool DTypename; |
| 7322 | NestedNameSpecifier *DQual; |
| 7323 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7324 | DTypename = UD->hasTypename(); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7325 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7326 | } else if (UnresolvedUsingValueDecl *UD |
| 7327 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 7328 | DTypename = false; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7329 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7330 | } else if (UnresolvedUsingTypenameDecl *UD |
| 7331 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 7332 | DTypename = true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7333 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7334 | } else continue; |
| 7335 | |
| 7336 | // using decls differ if one says 'typename' and the other doesn't. |
| 7337 | // FIXME: non-dependent using decls? |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7338 | if (HasTypenameKeyword != DTypename) continue; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7339 | |
| 7340 | // using decls differ if they name different scopes (but note that |
| 7341 | // template instantiation can cause this check to trigger when it |
| 7342 | // didn't before instantiation). |
| 7343 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 7344 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 7345 | continue; |
| 7346 | |
| 7347 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7348 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7349 | return true; |
| 7350 | } |
| 7351 | |
| 7352 | return false; |
| 7353 | } |
| 7354 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7355 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7356 | /// Checks that the given nested-name qualifier used in a using decl |
| 7357 | /// in the current context is appropriately related to the current |
| 7358 | /// scope. If an error is found, diagnoses it and returns true. |
| 7359 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 7360 | const CXXScopeSpec &SS, |
| 7361 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7362 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7363 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7364 | if (!CurContext->isRecord()) { |
| 7365 | // C++03 [namespace.udecl]p3: |
| 7366 | // C++0x [namespace.udecl]p8: |
| 7367 | // A using-declaration for a class member shall be a member-declaration. |
| 7368 | |
| 7369 | // If we weren't able to compute a valid scope, it must be a |
| 7370 | // dependent class scope. |
| 7371 | if (!NamedContext || NamedContext->isRecord()) { |
| 7372 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 7373 | << SS.getRange(); |
| 7374 | return true; |
| 7375 | } |
| 7376 | |
| 7377 | // Otherwise, everything is known to be fine. |
| 7378 | return false; |
| 7379 | } |
| 7380 | |
| 7381 | // The current scope is a record. |
| 7382 | |
| 7383 | // If the named context is dependent, we can't decide much. |
| 7384 | if (!NamedContext) { |
| 7385 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 7386 | // nested-name-specifier does not refer to a base class, which is |
| 7387 | // still possible in some cases. |
| 7388 | |
| 7389 | // Otherwise we have to conservatively report that things might be |
| 7390 | // okay. |
| 7391 | return false; |
| 7392 | } |
| 7393 | |
| 7394 | if (!NamedContext->isRecord()) { |
| 7395 | // Ideally this would point at the last name in the specifier, |
| 7396 | // but we don't have that level of source info. |
| 7397 | Diag(SS.getRange().getBegin(), |
| 7398 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 7399 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 7400 | return true; |
| 7401 | } |
| 7402 | |
Douglas Gregor | 6fb0729 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 7403 | if (!NamedContext->isDependentContext() && |
| 7404 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 7405 | return true; |
| 7406 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7407 | if (getLangOpts().CPlusPlus11) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7408 | // C++0x [namespace.udecl]p3: |
| 7409 | // In a using-declaration used as a member-declaration, the |
| 7410 | // nested-name-specifier shall name a base class of the class |
| 7411 | // being defined. |
| 7412 | |
| 7413 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 7414 | cast<CXXRecordDecl>(NamedContext))) { |
| 7415 | if (CurContext == NamedContext) { |
| 7416 | Diag(NameLoc, |
| 7417 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 7418 | << SS.getRange(); |
| 7419 | return true; |
| 7420 | } |
| 7421 | |
| 7422 | Diag(SS.getRange().getBegin(), |
| 7423 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 7424 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 7425 | << cast<CXXRecordDecl>(CurContext) |
| 7426 | << SS.getRange(); |
| 7427 | return true; |
| 7428 | } |
| 7429 | |
| 7430 | return false; |
| 7431 | } |
| 7432 | |
| 7433 | // C++03 [namespace.udecl]p4: |
| 7434 | // A using-declaration used as a member-declaration shall refer |
| 7435 | // to a member of a base class of the class being defined [etc.]. |
| 7436 | |
| 7437 | // Salient point: SS doesn't have to name a base class as long as |
| 7438 | // lookup only finds members from base classes. Therefore we can |
| 7439 | // diagnose here only if we can prove that that can't happen, |
| 7440 | // i.e. if the class hierarchies provably don't intersect. |
| 7441 | |
| 7442 | // TODO: it would be nice if "definitely valid" results were cached |
| 7443 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 7444 | // need to be repeated. |
| 7445 | |
| 7446 | struct UserData { |
Benjamin Kramer | 8c43dcc | 2012-02-23 16:06:01 +0000 | [diff] [blame] | 7447 | llvm::SmallPtrSet<const CXXRecordDecl*, 4> Bases; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7448 | |
| 7449 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 7450 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 7451 | Data->Bases.insert(Base); |
| 7452 | return true; |
| 7453 | } |
| 7454 | |
| 7455 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 7456 | return !Class->forallBases(collect, this); |
| 7457 | } |
| 7458 | |
| 7459 | /// Returns true if the base is dependent or is one of the |
| 7460 | /// accumulated base classes. |
| 7461 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 7462 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 7463 | return !Data->Bases.count(Base); |
| 7464 | } |
| 7465 | |
| 7466 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 7467 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 7468 | } |
| 7469 | }; |
| 7470 | |
| 7471 | UserData Data; |
| 7472 | |
| 7473 | // Returns false if we find a dependent base. |
| 7474 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 7475 | return false; |
| 7476 | |
| 7477 | // Returns false if the class has a dependent base or if it or one |
| 7478 | // of its bases is present in the base set of the current context. |
| 7479 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 7480 | return false; |
| 7481 | |
| 7482 | Diag(SS.getRange().getBegin(), |
| 7483 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 7484 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 7485 | << cast<CXXRecordDecl>(CurContext) |
| 7486 | << SS.getRange(); |
| 7487 | |
| 7488 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7489 | } |
| 7490 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7491 | Decl *Sema::ActOnAliasDeclaration(Scope *S, |
| 7492 | AccessSpecifier AS, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7493 | MultiTemplateParamsArg TemplateParamLists, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7494 | SourceLocation UsingLoc, |
| 7495 | UnqualifiedId &Name, |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 7496 | AttributeList *AttrList, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7497 | TypeResult Type) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7498 | // Skip up to the relevant declaration scope. |
| 7499 | while (S->getFlags() & Scope::TemplateParamScope) |
| 7500 | S = S->getParent(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7501 | assert((S->getFlags() & Scope::DeclScope) && |
| 7502 | "got alias-declaration outside of declaration scope"); |
| 7503 | |
| 7504 | if (Type.isInvalid()) |
| 7505 | return 0; |
| 7506 | |
| 7507 | bool Invalid = false; |
| 7508 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
| 7509 | TypeSourceInfo *TInfo = 0; |
Nick Lewycky | b79bf1d | 2011-05-02 01:07:19 +0000 | [diff] [blame] | 7510 | GetTypeFromParser(Type.get(), &TInfo); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7511 | |
| 7512 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
| 7513 | return 0; |
| 7514 | |
| 7515 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7516 | UPPC_DeclarationType)) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7517 | Invalid = true; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7518 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 7519 | TInfo->getTypeLoc().getBeginLoc()); |
| 7520 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7521 | |
| 7522 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, ForRedeclaration); |
| 7523 | LookupName(Previous, S); |
| 7524 | |
| 7525 | // Warn about shadowing the name of a template parameter. |
| 7526 | if (Previous.isSingleResult() && |
| 7527 | Previous.getFoundDecl()->isTemplateParameter()) { |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 7528 | DiagnoseTemplateParameterShadow(Name.StartLocation,Previous.getFoundDecl()); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7529 | Previous.clear(); |
| 7530 | } |
| 7531 | |
| 7532 | assert(Name.Kind == UnqualifiedId::IK_Identifier && |
| 7533 | "name in alias declaration must be an identifier"); |
| 7534 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 7535 | Name.StartLocation, |
| 7536 | Name.Identifier, TInfo); |
| 7537 | |
| 7538 | NewTD->setAccess(AS); |
| 7539 | |
| 7540 | if (Invalid) |
| 7541 | NewTD->setInvalidDecl(); |
| 7542 | |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 7543 | ProcessDeclAttributeList(S, NewTD, AttrList); |
| 7544 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7545 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 7546 | Invalid |= NewTD->isInvalidDecl(); |
| 7547 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7548 | bool Redeclaration = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7549 | |
| 7550 | NamedDecl *NewND; |
| 7551 | if (TemplateParamLists.size()) { |
| 7552 | TypeAliasTemplateDecl *OldDecl = 0; |
| 7553 | TemplateParameterList *OldTemplateParams = 0; |
| 7554 | |
| 7555 | if (TemplateParamLists.size() != 1) { |
| 7556 | Diag(UsingLoc, diag::err_alias_template_extra_headers) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7557 | << SourceRange(TemplateParamLists[1]->getTemplateLoc(), |
| 7558 | TemplateParamLists[TemplateParamLists.size()-1]->getRAngleLoc()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7559 | } |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7560 | TemplateParameterList *TemplateParams = TemplateParamLists[0]; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7561 | |
| 7562 | // Only consider previous declarations in the same scope. |
| 7563 | FilterLookupForScope(Previous, CurContext, S, /*ConsiderLinkage*/false, |
| 7564 | /*ExplicitInstantiationOrSpecialization*/false); |
| 7565 | if (!Previous.empty()) { |
| 7566 | Redeclaration = true; |
| 7567 | |
| 7568 | OldDecl = Previous.getAsSingle<TypeAliasTemplateDecl>(); |
| 7569 | if (!OldDecl && !Invalid) { |
| 7570 | Diag(UsingLoc, diag::err_redefinition_different_kind) |
| 7571 | << Name.Identifier; |
| 7572 | |
| 7573 | NamedDecl *OldD = Previous.getRepresentativeDecl(); |
| 7574 | if (OldD->getLocation().isValid()) |
| 7575 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| 7576 | |
| 7577 | Invalid = true; |
| 7578 | } |
| 7579 | |
| 7580 | if (!Invalid && OldDecl && !OldDecl->isInvalidDecl()) { |
| 7581 | if (TemplateParameterListsAreEqual(TemplateParams, |
| 7582 | OldDecl->getTemplateParameters(), |
| 7583 | /*Complain=*/true, |
| 7584 | TPL_TemplateMatch)) |
| 7585 | OldTemplateParams = OldDecl->getTemplateParameters(); |
| 7586 | else |
| 7587 | Invalid = true; |
| 7588 | |
| 7589 | TypeAliasDecl *OldTD = OldDecl->getTemplatedDecl(); |
| 7590 | if (!Invalid && |
| 7591 | !Context.hasSameType(OldTD->getUnderlyingType(), |
| 7592 | NewTD->getUnderlyingType())) { |
| 7593 | // FIXME: The C++0x standard does not clearly say this is ill-formed, |
| 7594 | // but we can't reasonably accept it. |
| 7595 | Diag(NewTD->getLocation(), diag::err_redefinition_different_typedef) |
| 7596 | << 2 << NewTD->getUnderlyingType() << OldTD->getUnderlyingType(); |
| 7597 | if (OldTD->getLocation().isValid()) |
| 7598 | Diag(OldTD->getLocation(), diag::note_previous_definition); |
| 7599 | Invalid = true; |
| 7600 | } |
| 7601 | } |
| 7602 | } |
| 7603 | |
| 7604 | // Merge any previous default template arguments into our parameters, |
| 7605 | // and check the parameter list. |
| 7606 | if (CheckTemplateParameterList(TemplateParams, OldTemplateParams, |
| 7607 | TPC_TypeAliasTemplate)) |
| 7608 | return 0; |
| 7609 | |
| 7610 | TypeAliasTemplateDecl *NewDecl = |
| 7611 | TypeAliasTemplateDecl::Create(Context, CurContext, UsingLoc, |
| 7612 | Name.Identifier, TemplateParams, |
| 7613 | NewTD); |
| 7614 | |
| 7615 | NewDecl->setAccess(AS); |
| 7616 | |
| 7617 | if (Invalid) |
| 7618 | NewDecl->setInvalidDecl(); |
| 7619 | else if (OldDecl) |
| 7620 | NewDecl->setPreviousDeclaration(OldDecl); |
| 7621 | |
| 7622 | NewND = NewDecl; |
| 7623 | } else { |
| 7624 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 7625 | NewND = NewTD; |
| 7626 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7627 | |
| 7628 | if (!Redeclaration) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7629 | PushOnScopeChains(NewND, S); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7630 | |
Dmitri Gribenko | c27bc80 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 7631 | ActOnDocumentableDecl(NewND); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7632 | return NewND; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7633 | } |
| 7634 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7635 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 7636 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 7637 | SourceLocation AliasLoc, |
| 7638 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7639 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 7640 | SourceLocation IdentLoc, |
| 7641 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7642 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7643 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7644 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 7645 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7646 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7647 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 7648 | NamedDecl *PrevDecl |
| 7649 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 7650 | ForRedeclaration); |
| 7651 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 7652 | PrevDecl = 0; |
| 7653 | |
| 7654 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7655 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7656 | // 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] | 7657 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 7658 | // FIXME: At some point, we'll want to create the (redundant) |
| 7659 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7660 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 7661 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7662 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7663 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7664 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7665 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 7666 | diag::err_redefinition_different_kind; |
| 7667 | Diag(AliasLoc, DiagID) << Alias; |
| 7668 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7669 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7670 | } |
| 7671 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7672 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7673 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7674 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7675 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 7676 | if (!TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, Ident)) { |
Richard Smith | bf9658c | 2012-04-05 23:13:23 +0000 | [diff] [blame] | 7677 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7678 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 7679 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 7680 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7681 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 7682 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7683 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 7684 | Alias, SS.getWithLocInContext(Context), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7685 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7686 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 7687 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7688 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 7689 | } |
| 7690 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 7691 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7692 | Sema::ComputeDefaultedDefaultCtorExceptionSpec(SourceLocation Loc, |
| 7693 | CXXMethodDecl *MD) { |
| 7694 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 7695 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7696 | // C++ [except.spec]p14: |
| 7697 | // An implicitly declared special member function (Clause 12) shall have an |
| 7698 | // exception-specification. [...] |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7699 | ImplicitExceptionSpecification ExceptSpec(*this); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 7700 | if (ClassDecl->isInvalidDecl()) |
| 7701 | return ExceptSpec; |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7702 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7703 | // Direct base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7704 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 7705 | BEnd = ClassDecl->bases_end(); |
| 7706 | B != BEnd; ++B) { |
| 7707 | if (B->isVirtual()) // Handled below. |
| 7708 | continue; |
| 7709 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7710 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 7711 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 7712 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 7713 | // If this is a deleted function, add it anyway. This might be conformant |
| 7714 | // with the standard. This might not. I'm not sure. It might not matter. |
| 7715 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7716 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7717 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7718 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7719 | |
| 7720 | // Virtual base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7721 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 7722 | BEnd = ClassDecl->vbases_end(); |
| 7723 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7724 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 7725 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 7726 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 7727 | // If this is a deleted function, add it anyway. This might be conformant |
| 7728 | // with the standard. This might not. I'm not sure. It might not matter. |
| 7729 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7730 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7731 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7732 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7733 | |
| 7734 | // Field constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7735 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 7736 | FEnd = ClassDecl->field_end(); |
| 7737 | F != FEnd; ++F) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7738 | if (F->hasInClassInitializer()) { |
| 7739 | if (Expr *E = F->getInClassInitializer()) |
| 7740 | ExceptSpec.CalledExpr(E); |
| 7741 | else if (!F->isInvalidDecl()) |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7742 | // DR1351: |
| 7743 | // If the brace-or-equal-initializer of a non-static data member |
| 7744 | // invokes a defaulted default constructor of its class or of an |
| 7745 | // enclosing class in a potentially evaluated subexpression, the |
| 7746 | // program is ill-formed. |
| 7747 | // |
| 7748 | // This resolution is unworkable: the exception specification of the |
| 7749 | // default constructor can be needed in an unevaluated context, in |
| 7750 | // particular, in the operand of a noexcept-expression, and we can be |
| 7751 | // unable to compute an exception specification for an enclosed class. |
| 7752 | // |
| 7753 | // We do not allow an in-class initializer to require the evaluation |
| 7754 | // of the exception specification for any in-class initializer whose |
| 7755 | // definition is not lexically complete. |
| 7756 | Diag(Loc, diag::err_in_class_initializer_references_def_ctor) << MD; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7757 | } else if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7758 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 7759 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 7760 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 7761 | // If this is a deleted function, add it anyway. This might be conformant |
| 7762 | // with the standard. This might not. I'm not sure. It might not matter. |
| 7763 | // In particular, the problem is that this function never gets called. It |
| 7764 | // might just be ill-formed because this function attempts to refer to |
| 7765 | // a deleted function here. |
| 7766 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7767 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7768 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7769 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7770 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 7771 | return ExceptSpec; |
| 7772 | } |
| 7773 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 7774 | Sema::ImplicitExceptionSpecification |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 7775 | Sema::ComputeInheritingCtorExceptionSpec(CXXConstructorDecl *CD) { |
| 7776 | CXXRecordDecl *ClassDecl = CD->getParent(); |
| 7777 | |
| 7778 | // C++ [except.spec]p14: |
| 7779 | // An inheriting constructor [...] shall have an exception-specification. [...] |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 7780 | ImplicitExceptionSpecification ExceptSpec(*this); |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 7781 | if (ClassDecl->isInvalidDecl()) |
| 7782 | return ExceptSpec; |
| 7783 | |
| 7784 | // Inherited constructor. |
| 7785 | const CXXConstructorDecl *InheritedCD = CD->getInheritedConstructor(); |
| 7786 | const CXXRecordDecl *InheritedDecl = InheritedCD->getParent(); |
| 7787 | // FIXME: Copying or moving the parameters could add extra exceptions to the |
| 7788 | // set, as could the default arguments for the inherited constructor. This |
| 7789 | // will be addressed when we implement the resolution of core issue 1351. |
| 7790 | ExceptSpec.CalledDecl(CD->getLocStart(), InheritedCD); |
| 7791 | |
| 7792 | // Direct base-class constructors. |
| 7793 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 7794 | BEnd = ClassDecl->bases_end(); |
| 7795 | B != BEnd; ++B) { |
| 7796 | if (B->isVirtual()) // Handled below. |
| 7797 | continue; |
| 7798 | |
| 7799 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 7800 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 7801 | if (BaseClassDecl == InheritedDecl) |
| 7802 | continue; |
| 7803 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 7804 | if (Constructor) |
| 7805 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
| 7806 | } |
| 7807 | } |
| 7808 | |
| 7809 | // Virtual base-class constructors. |
| 7810 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 7811 | BEnd = ClassDecl->vbases_end(); |
| 7812 | B != BEnd; ++B) { |
| 7813 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 7814 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 7815 | if (BaseClassDecl == InheritedDecl) |
| 7816 | continue; |
| 7817 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 7818 | if (Constructor) |
| 7819 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
| 7820 | } |
| 7821 | } |
| 7822 | |
| 7823 | // Field constructors. |
| 7824 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 7825 | FEnd = ClassDecl->field_end(); |
| 7826 | F != FEnd; ++F) { |
| 7827 | if (F->hasInClassInitializer()) { |
| 7828 | if (Expr *E = F->getInClassInitializer()) |
| 7829 | ExceptSpec.CalledExpr(E); |
| 7830 | else if (!F->isInvalidDecl()) |
| 7831 | Diag(CD->getLocation(), |
| 7832 | diag::err_in_class_initializer_references_def_ctor) << CD; |
| 7833 | } else if (const RecordType *RecordTy |
| 7834 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 7835 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 7836 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 7837 | if (Constructor) |
| 7838 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
| 7839 | } |
| 7840 | } |
| 7841 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 7842 | return ExceptSpec; |
| 7843 | } |
| 7844 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 7845 | namespace { |
| 7846 | /// RAII object to register a special member as being currently declared. |
| 7847 | struct DeclaringSpecialMember { |
| 7848 | Sema &S; |
| 7849 | Sema::SpecialMemberDecl D; |
| 7850 | bool WasAlreadyBeingDeclared; |
| 7851 | |
| 7852 | DeclaringSpecialMember(Sema &S, CXXRecordDecl *RD, Sema::CXXSpecialMember CSM) |
| 7853 | : S(S), D(RD, CSM) { |
| 7854 | WasAlreadyBeingDeclared = !S.SpecialMembersBeingDeclared.insert(D); |
| 7855 | if (WasAlreadyBeingDeclared) |
| 7856 | // This almost never happens, but if it does, ensure that our cache |
| 7857 | // doesn't contain a stale result. |
| 7858 | S.SpecialMemberCache.clear(); |
| 7859 | |
| 7860 | // FIXME: Register a note to be produced if we encounter an error while |
| 7861 | // declaring the special member. |
| 7862 | } |
| 7863 | ~DeclaringSpecialMember() { |
| 7864 | if (!WasAlreadyBeingDeclared) |
| 7865 | S.SpecialMembersBeingDeclared.erase(D); |
| 7866 | } |
| 7867 | |
| 7868 | /// \brief Are we already trying to declare this special member? |
| 7869 | bool isAlreadyBeingDeclared() const { |
| 7870 | return WasAlreadyBeingDeclared; |
| 7871 | } |
| 7872 | }; |
| 7873 | } |
| 7874 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 7875 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 7876 | CXXRecordDecl *ClassDecl) { |
| 7877 | // C++ [class.ctor]p5: |
| 7878 | // A default constructor for a class X is a constructor of class X |
| 7879 | // that can be called without an argument. If there is no |
| 7880 | // user-declared constructor for class X, a default constructor is |
| 7881 | // implicitly declared. An implicitly-declared default constructor |
| 7882 | // is an inline public member of its class. |
Richard Smith | d0adeb6 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 7883 | assert(ClassDecl->needsImplicitDefaultConstructor() && |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 7884 | "Should not build implicit default constructor!"); |
| 7885 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 7886 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDefaultConstructor); |
| 7887 | if (DSM.isAlreadyBeingDeclared()) |
| 7888 | return 0; |
| 7889 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 7890 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 7891 | CXXDefaultConstructor, |
| 7892 | false); |
| 7893 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7894 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7895 | CanQualType ClassType |
| 7896 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7897 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7898 | DeclarationName Name |
| 7899 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7900 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 7901 | CXXConstructorDecl *DefaultCon = CXXConstructorDecl::Create( |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7902 | Context, ClassDecl, ClassLoc, NameInfo, /*Type*/QualType(), /*TInfo=*/0, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 7903 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 7904 | Constexpr); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7905 | DefaultCon->setAccess(AS_public); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 7906 | DefaultCon->setDefaulted(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7907 | DefaultCon->setImplicit(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7908 | |
| 7909 | // Build an exception specification pointing back at this constructor. |
| 7910 | FunctionProtoType::ExtProtoInfo EPI; |
| 7911 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 7912 | EPI.ExceptionSpecDecl = DefaultCon; |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 7913 | DefaultCon->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7914 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 7915 | // We don't need to use SpecialMemberIsTrivial here; triviality for default |
| 7916 | // constructors is easy to compute. |
| 7917 | DefaultCon->setTrivial(ClassDecl->hasTrivialDefaultConstructor()); |
| 7918 | |
| 7919 | if (ShouldDeleteSpecialMember(DefaultCon, CXXDefaultConstructor)) |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 7920 | SetDeclDeleted(DefaultCon, ClassLoc); |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 7921 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7922 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7923 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 7924 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 7925 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7926 | PushOnScopeChains(DefaultCon, S, false); |
| 7927 | ClassDecl->addDecl(DefaultCon); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 7928 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7929 | return DefaultCon; |
| 7930 | } |
| 7931 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 7932 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 7933 | CXXConstructorDecl *Constructor) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 7934 | assert((Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 7935 | !Constructor->doesThisDeclarationHaveABody() && |
| 7936 | !Constructor->isDeleted()) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 7937 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7938 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 7939 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 7940 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 7941 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 7942 | SynthesizedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 7943 | DiagnosticErrorTrap Trap(Diags); |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 7944 | if (SetCtorInitializers(Constructor, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7945 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 7946 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 7947 | << CXXDefaultConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 7948 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7949 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 7950 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7951 | |
| 7952 | SourceLocation Loc = Constructor->getLocation(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 7953 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7954 | |
| 7955 | Constructor->setUsed(); |
| 7956 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7957 | |
| 7958 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7959 | L->CompletedImplicitDefinition(Constructor); |
| 7960 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 7961 | } |
| 7962 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7963 | void Sema::ActOnFinishDelayedMemberInitializers(Decl *D) { |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 7964 | // Check that any explicitly-defaulted methods have exception specifications |
| 7965 | // compatible with their implicit exception specifications. |
| 7966 | CheckDelayedExplicitlyDefaultedMemberExceptionSpecs(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7967 | } |
| 7968 | |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 7969 | namespace { |
| 7970 | /// Information on inheriting constructors to declare. |
| 7971 | class InheritingConstructorInfo { |
| 7972 | public: |
| 7973 | InheritingConstructorInfo(Sema &SemaRef, CXXRecordDecl *Derived) |
| 7974 | : SemaRef(SemaRef), Derived(Derived) { |
| 7975 | // Mark the constructors that we already have in the derived class. |
| 7976 | // |
| 7977 | // C++11 [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 7978 | // unless there is a user-declared constructor with the same signature in |
| 7979 | // the class where the using-declaration appears. |
| 7980 | visitAll(Derived, &InheritingConstructorInfo::noteDeclaredInDerived); |
| 7981 | } |
| 7982 | |
| 7983 | void inheritAll(CXXRecordDecl *RD) { |
| 7984 | visitAll(RD, &InheritingConstructorInfo::inherit); |
| 7985 | } |
| 7986 | |
| 7987 | private: |
| 7988 | /// Information about an inheriting constructor. |
| 7989 | struct InheritingConstructor { |
| 7990 | InheritingConstructor() |
| 7991 | : DeclaredInDerived(false), BaseCtor(0), DerivedCtor(0) {} |
| 7992 | |
| 7993 | /// If \c true, a constructor with this signature is already declared |
| 7994 | /// in the derived class. |
| 7995 | bool DeclaredInDerived; |
| 7996 | |
| 7997 | /// The constructor which is inherited. |
| 7998 | const CXXConstructorDecl *BaseCtor; |
| 7999 | |
| 8000 | /// The derived constructor we declared. |
| 8001 | CXXConstructorDecl *DerivedCtor; |
| 8002 | }; |
| 8003 | |
| 8004 | /// Inheriting constructors with a given canonical type. There can be at |
| 8005 | /// most one such non-template constructor, and any number of templated |
| 8006 | /// constructors. |
| 8007 | struct InheritingConstructorsForType { |
| 8008 | InheritingConstructor NonTemplate; |
| 8009 | llvm::SmallVector< |
| 8010 | std::pair<TemplateParameterList*, InheritingConstructor>, 4> Templates; |
| 8011 | |
| 8012 | InheritingConstructor &getEntry(Sema &S, const CXXConstructorDecl *Ctor) { |
| 8013 | if (FunctionTemplateDecl *FTD = Ctor->getDescribedFunctionTemplate()) { |
| 8014 | TemplateParameterList *ParamList = FTD->getTemplateParameters(); |
| 8015 | for (unsigned I = 0, N = Templates.size(); I != N; ++I) |
| 8016 | if (S.TemplateParameterListsAreEqual(ParamList, Templates[I].first, |
| 8017 | false, S.TPL_TemplateMatch)) |
| 8018 | return Templates[I].second; |
| 8019 | Templates.push_back(std::make_pair(ParamList, InheritingConstructor())); |
| 8020 | return Templates.back().second; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8021 | } |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8022 | |
| 8023 | return NonTemplate; |
| 8024 | } |
| 8025 | }; |
| 8026 | |
| 8027 | /// Get or create the inheriting constructor record for a constructor. |
| 8028 | InheritingConstructor &getEntry(const CXXConstructorDecl *Ctor, |
| 8029 | QualType CtorType) { |
| 8030 | return Map[CtorType.getCanonicalType()->castAs<FunctionProtoType>()] |
| 8031 | .getEntry(SemaRef, Ctor); |
| 8032 | } |
| 8033 | |
| 8034 | typedef void (InheritingConstructorInfo::*VisitFn)(const CXXConstructorDecl*); |
| 8035 | |
| 8036 | /// Process all constructors for a class. |
| 8037 | void visitAll(const CXXRecordDecl *RD, VisitFn Callback) { |
| 8038 | for (CXXRecordDecl::ctor_iterator CtorIt = RD->ctor_begin(), |
| 8039 | CtorE = RD->ctor_end(); |
| 8040 | CtorIt != CtorE; ++CtorIt) |
| 8041 | (this->*Callback)(*CtorIt); |
| 8042 | for (CXXRecordDecl::specific_decl_iterator<FunctionTemplateDecl> |
| 8043 | I(RD->decls_begin()), E(RD->decls_end()); |
| 8044 | I != E; ++I) { |
| 8045 | const FunctionDecl *FD = (*I)->getTemplatedDecl(); |
| 8046 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
| 8047 | (this->*Callback)(CD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8048 | } |
| 8049 | } |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8050 | |
| 8051 | /// Note that a constructor (or constructor template) was declared in Derived. |
| 8052 | void noteDeclaredInDerived(const CXXConstructorDecl *Ctor) { |
| 8053 | getEntry(Ctor, Ctor->getType()).DeclaredInDerived = true; |
| 8054 | } |
| 8055 | |
| 8056 | /// Inherit a single constructor. |
| 8057 | void inherit(const CXXConstructorDecl *Ctor) { |
| 8058 | const FunctionProtoType *CtorType = |
| 8059 | Ctor->getType()->castAs<FunctionProtoType>(); |
| 8060 | ArrayRef<QualType> ArgTypes(CtorType->getArgTypes()); |
| 8061 | FunctionProtoType::ExtProtoInfo EPI = CtorType->getExtProtoInfo(); |
| 8062 | |
| 8063 | SourceLocation UsingLoc = getUsingLoc(Ctor->getParent()); |
| 8064 | |
| 8065 | // Core issue (no number yet): the ellipsis is always discarded. |
| 8066 | if (EPI.Variadic) { |
| 8067 | SemaRef.Diag(UsingLoc, diag::warn_using_decl_constructor_ellipsis); |
| 8068 | SemaRef.Diag(Ctor->getLocation(), |
| 8069 | diag::note_using_decl_constructor_ellipsis); |
| 8070 | EPI.Variadic = false; |
| 8071 | } |
| 8072 | |
| 8073 | // Declare a constructor for each number of parameters. |
| 8074 | // |
| 8075 | // C++11 [class.inhctor]p1: |
| 8076 | // The candidate set of inherited constructors from the class X named in |
| 8077 | // the using-declaration consists of [... modulo defects ...] for each |
| 8078 | // constructor or constructor template of X, the set of constructors or |
| 8079 | // constructor templates that results from omitting any ellipsis parameter |
| 8080 | // specification and successively omitting parameters with a default |
| 8081 | // argument from the end of the parameter-type-list |
Richard Smith | 987c030 | 2013-04-17 19:00:52 +0000 | [diff] [blame] | 8082 | unsigned MinParams = minParamsToInherit(Ctor); |
| 8083 | unsigned Params = Ctor->getNumParams(); |
| 8084 | if (Params >= MinParams) { |
| 8085 | do |
| 8086 | declareCtor(UsingLoc, Ctor, |
| 8087 | SemaRef.Context.getFunctionType( |
| 8088 | Ctor->getResultType(), ArgTypes.slice(0, Params), EPI)); |
| 8089 | while (Params > MinParams && |
| 8090 | Ctor->getParamDecl(--Params)->hasDefaultArg()); |
| 8091 | } |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8092 | } |
| 8093 | |
| 8094 | /// Find the using-declaration which specified that we should inherit the |
| 8095 | /// constructors of \p Base. |
| 8096 | SourceLocation getUsingLoc(const CXXRecordDecl *Base) { |
| 8097 | // No fancy lookup required; just look for the base constructor name |
| 8098 | // directly within the derived class. |
| 8099 | ASTContext &Context = SemaRef.Context; |
| 8100 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
| 8101 | Context.getCanonicalType(Context.getRecordType(Base))); |
| 8102 | DeclContext::lookup_const_result Decls = Derived->lookup(Name); |
| 8103 | return Decls.empty() ? Derived->getLocation() : Decls[0]->getLocation(); |
| 8104 | } |
| 8105 | |
| 8106 | unsigned minParamsToInherit(const CXXConstructorDecl *Ctor) { |
| 8107 | // C++11 [class.inhctor]p3: |
| 8108 | // [F]or each constructor template in the candidate set of inherited |
| 8109 | // constructors, a constructor template is implicitly declared |
| 8110 | if (Ctor->getDescribedFunctionTemplate()) |
| 8111 | return 0; |
| 8112 | |
| 8113 | // For each non-template constructor in the candidate set of inherited |
| 8114 | // constructors other than a constructor having no parameters or a |
| 8115 | // copy/move constructor having a single parameter, a constructor is |
| 8116 | // implicitly declared [...] |
| 8117 | if (Ctor->getNumParams() == 0) |
| 8118 | return 1; |
| 8119 | if (Ctor->isCopyOrMoveConstructor()) |
| 8120 | return 2; |
| 8121 | |
| 8122 | // Per discussion on core reflector, never inherit a constructor which |
| 8123 | // would become a default, copy, or move constructor of Derived either. |
| 8124 | const ParmVarDecl *PD = Ctor->getParamDecl(0); |
| 8125 | const ReferenceType *RT = PD->getType()->getAs<ReferenceType>(); |
| 8126 | return (RT && RT->getPointeeCXXRecordDecl() == Derived) ? 2 : 1; |
| 8127 | } |
| 8128 | |
| 8129 | /// Declare a single inheriting constructor, inheriting the specified |
| 8130 | /// constructor, with the given type. |
| 8131 | void declareCtor(SourceLocation UsingLoc, const CXXConstructorDecl *BaseCtor, |
| 8132 | QualType DerivedType) { |
| 8133 | InheritingConstructor &Entry = getEntry(BaseCtor, DerivedType); |
| 8134 | |
| 8135 | // C++11 [class.inhctor]p3: |
| 8136 | // ... a constructor is implicitly declared with the same constructor |
| 8137 | // characteristics unless there is a user-declared constructor with |
| 8138 | // the same signature in the class where the using-declaration appears |
| 8139 | if (Entry.DeclaredInDerived) |
| 8140 | return; |
| 8141 | |
| 8142 | // C++11 [class.inhctor]p7: |
| 8143 | // If two using-declarations declare inheriting constructors with the |
| 8144 | // same signature, the program is ill-formed |
| 8145 | if (Entry.DerivedCtor) { |
| 8146 | if (BaseCtor->getParent() != Entry.BaseCtor->getParent()) { |
| 8147 | // Only diagnose this once per constructor. |
| 8148 | if (Entry.DerivedCtor->isInvalidDecl()) |
| 8149 | return; |
| 8150 | Entry.DerivedCtor->setInvalidDecl(); |
| 8151 | |
| 8152 | SemaRef.Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 8153 | SemaRef.Diag(BaseCtor->getLocation(), |
| 8154 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 8155 | SemaRef.Diag(Entry.BaseCtor->getLocation(), |
| 8156 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 8157 | SemaRef.Diag(Entry.DerivedCtor->getLocation(), |
| 8158 | diag::note_using_decl_constructor_conflict_previous_using); |
| 8159 | } else { |
| 8160 | // Core issue (no number): if the same inheriting constructor is |
| 8161 | // produced by multiple base class constructors from the same base |
| 8162 | // class, the inheriting constructor is defined as deleted. |
| 8163 | SemaRef.SetDeclDeleted(Entry.DerivedCtor, UsingLoc); |
| 8164 | } |
| 8165 | |
| 8166 | return; |
| 8167 | } |
| 8168 | |
| 8169 | ASTContext &Context = SemaRef.Context; |
| 8170 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
| 8171 | Context.getCanonicalType(Context.getRecordType(Derived))); |
| 8172 | DeclarationNameInfo NameInfo(Name, UsingLoc); |
| 8173 | |
| 8174 | TemplateParameterList *TemplateParams = 0; |
| 8175 | if (const FunctionTemplateDecl *FTD = |
| 8176 | BaseCtor->getDescribedFunctionTemplate()) { |
| 8177 | TemplateParams = FTD->getTemplateParameters(); |
| 8178 | // We're reusing template parameters from a different DeclContext. This |
| 8179 | // is questionable at best, but works out because the template depth in |
| 8180 | // both places is guaranteed to be 0. |
| 8181 | // FIXME: Rebuild the template parameters in the new context, and |
| 8182 | // transform the function type to refer to them. |
| 8183 | } |
| 8184 | |
| 8185 | // Build type source info pointing at the using-declaration. This is |
| 8186 | // required by template instantiation. |
| 8187 | TypeSourceInfo *TInfo = |
| 8188 | Context.getTrivialTypeSourceInfo(DerivedType, UsingLoc); |
| 8189 | FunctionProtoTypeLoc ProtoLoc = |
| 8190 | TInfo->getTypeLoc().IgnoreParens().castAs<FunctionProtoTypeLoc>(); |
| 8191 | |
| 8192 | CXXConstructorDecl *DerivedCtor = CXXConstructorDecl::Create( |
| 8193 | Context, Derived, UsingLoc, NameInfo, DerivedType, |
| 8194 | TInfo, BaseCtor->isExplicit(), /*Inline=*/true, |
| 8195 | /*ImplicitlyDeclared=*/true, /*Constexpr=*/BaseCtor->isConstexpr()); |
| 8196 | |
| 8197 | // Build an unevaluated exception specification for this constructor. |
| 8198 | const FunctionProtoType *FPT = DerivedType->castAs<FunctionProtoType>(); |
| 8199 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8200 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8201 | EPI.ExceptionSpecDecl = DerivedCtor; |
| 8202 | DerivedCtor->setType(Context.getFunctionType(FPT->getResultType(), |
| 8203 | FPT->getArgTypes(), EPI)); |
| 8204 | |
| 8205 | // Build the parameter declarations. |
| 8206 | SmallVector<ParmVarDecl *, 16> ParamDecls; |
| 8207 | for (unsigned I = 0, N = FPT->getNumArgs(); I != N; ++I) { |
| 8208 | TypeSourceInfo *TInfo = |
| 8209 | Context.getTrivialTypeSourceInfo(FPT->getArgType(I), UsingLoc); |
| 8210 | ParmVarDecl *PD = ParmVarDecl::Create( |
| 8211 | Context, DerivedCtor, UsingLoc, UsingLoc, /*IdentifierInfo=*/0, |
| 8212 | FPT->getArgType(I), TInfo, SC_None, /*DefaultArg=*/0); |
| 8213 | PD->setScopeInfo(0, I); |
| 8214 | PD->setImplicit(); |
| 8215 | ParamDecls.push_back(PD); |
| 8216 | ProtoLoc.setArg(I, PD); |
| 8217 | } |
| 8218 | |
| 8219 | // Set up the new constructor. |
| 8220 | DerivedCtor->setAccess(BaseCtor->getAccess()); |
| 8221 | DerivedCtor->setParams(ParamDecls); |
| 8222 | DerivedCtor->setInheritedConstructor(BaseCtor); |
| 8223 | if (BaseCtor->isDeleted()) |
| 8224 | SemaRef.SetDeclDeleted(DerivedCtor, UsingLoc); |
| 8225 | |
| 8226 | // If this is a constructor template, build the template declaration. |
| 8227 | if (TemplateParams) { |
| 8228 | FunctionTemplateDecl *DerivedTemplate = |
| 8229 | FunctionTemplateDecl::Create(SemaRef.Context, Derived, UsingLoc, Name, |
| 8230 | TemplateParams, DerivedCtor); |
| 8231 | DerivedTemplate->setAccess(BaseCtor->getAccess()); |
| 8232 | DerivedCtor->setDescribedFunctionTemplate(DerivedTemplate); |
| 8233 | Derived->addDecl(DerivedTemplate); |
| 8234 | } else { |
| 8235 | Derived->addDecl(DerivedCtor); |
| 8236 | } |
| 8237 | |
| 8238 | Entry.BaseCtor = BaseCtor; |
| 8239 | Entry.DerivedCtor = DerivedCtor; |
| 8240 | } |
| 8241 | |
| 8242 | Sema &SemaRef; |
| 8243 | CXXRecordDecl *Derived; |
| 8244 | typedef llvm::DenseMap<const Type *, InheritingConstructorsForType> MapType; |
| 8245 | MapType Map; |
| 8246 | }; |
| 8247 | } |
| 8248 | |
| 8249 | void Sema::DeclareInheritingConstructors(CXXRecordDecl *ClassDecl) { |
| 8250 | // Defer declaring the inheriting constructors until the class is |
| 8251 | // instantiated. |
| 8252 | if (ClassDecl->isDependentContext()) |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8253 | return; |
| 8254 | |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8255 | // Find base classes from which we might inherit constructors. |
| 8256 | SmallVector<CXXRecordDecl*, 4> InheritedBases; |
| 8257 | for (CXXRecordDecl::base_class_iterator BaseIt = ClassDecl->bases_begin(), |
| 8258 | BaseE = ClassDecl->bases_end(); |
| 8259 | BaseIt != BaseE; ++BaseIt) |
| 8260 | if (BaseIt->getInheritConstructors()) |
| 8261 | InheritedBases.push_back(BaseIt->getType()->getAsCXXRecordDecl()); |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8262 | |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8263 | // Go no further if we're not inheriting any constructors. |
| 8264 | if (InheritedBases.empty()) |
| 8265 | return; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8266 | |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8267 | // Declare the inherited constructors. |
| 8268 | InheritingConstructorInfo ICI(*this, ClassDecl); |
| 8269 | for (unsigned I = 0, N = InheritedBases.size(); I != N; ++I) |
| 8270 | ICI.inheritAll(InheritedBases[I]); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8271 | } |
| 8272 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8273 | void Sema::DefineInheritingConstructor(SourceLocation CurrentLocation, |
| 8274 | CXXConstructorDecl *Constructor) { |
| 8275 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 8276 | assert(Constructor->getInheritedConstructor() && |
| 8277 | !Constructor->doesThisDeclarationHaveABody() && |
| 8278 | !Constructor->isDeleted()); |
| 8279 | |
| 8280 | SynthesizedFunctionScope Scope(*this, Constructor); |
| 8281 | DiagnosticErrorTrap Trap(Diags); |
| 8282 | if (SetCtorInitializers(Constructor, /*AnyErrors=*/false) || |
| 8283 | Trap.hasErrorOccurred()) { |
| 8284 | Diag(CurrentLocation, diag::note_inhctor_synthesized_at) |
| 8285 | << Context.getTagDeclType(ClassDecl); |
| 8286 | Constructor->setInvalidDecl(); |
| 8287 | return; |
| 8288 | } |
| 8289 | |
| 8290 | SourceLocation Loc = Constructor->getLocation(); |
| 8291 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
| 8292 | |
| 8293 | Constructor->setUsed(); |
| 8294 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| 8295 | |
| 8296 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8297 | L->CompletedImplicitDefinition(Constructor); |
| 8298 | } |
| 8299 | } |
| 8300 | |
| 8301 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8302 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8303 | Sema::ComputeDefaultedDtorExceptionSpec(CXXMethodDecl *MD) { |
| 8304 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 8305 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8306 | // C++ [except.spec]p14: |
| 8307 | // An implicitly declared special member function (Clause 12) shall have |
| 8308 | // an exception-specification. |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8309 | ImplicitExceptionSpecification ExceptSpec(*this); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 8310 | if (ClassDecl->isInvalidDecl()) |
| 8311 | return ExceptSpec; |
| 8312 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8313 | // Direct base-class destructors. |
| 8314 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 8315 | BEnd = ClassDecl->bases_end(); |
| 8316 | B != BEnd; ++B) { |
| 8317 | if (B->isVirtual()) // Handled below. |
| 8318 | continue; |
| 8319 | |
| 8320 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8321 | ExceptSpec.CalledDecl(B->getLocStart(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8322 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8323 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8324 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8325 | // Virtual base-class destructors. |
| 8326 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 8327 | BEnd = ClassDecl->vbases_end(); |
| 8328 | B != BEnd; ++B) { |
| 8329 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8330 | ExceptSpec.CalledDecl(B->getLocStart(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8331 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8332 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8333 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8334 | // Field destructors. |
| 8335 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 8336 | FEnd = ClassDecl->field_end(); |
| 8337 | F != FEnd; ++F) { |
| 8338 | if (const RecordType *RecordTy |
| 8339 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8340 | ExceptSpec.CalledDecl(F->getLocation(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8341 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8342 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8343 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8344 | return ExceptSpec; |
| 8345 | } |
| 8346 | |
| 8347 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
| 8348 | // C++ [class.dtor]p2: |
| 8349 | // If a class has no user-declared destructor, a destructor is |
| 8350 | // declared implicitly. An implicitly-declared destructor is an |
| 8351 | // inline public member of its class. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 8352 | assert(ClassDecl->needsImplicitDestructor()); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8353 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8354 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDestructor); |
| 8355 | if (DSM.isAlreadyBeingDeclared()) |
| 8356 | return 0; |
| 8357 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8358 | // Create the actual destructor declaration. |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8359 | CanQualType ClassType |
| 8360 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8361 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8362 | DeclarationName Name |
| 8363 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8364 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8365 | CXXDestructorDecl *Destructor |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8366 | = CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
| 8367 | QualType(), 0, /*isInline=*/true, |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8368 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8369 | Destructor->setAccess(AS_public); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8370 | Destructor->setDefaulted(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8371 | Destructor->setImplicit(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8372 | |
| 8373 | // Build an exception specification pointing back at this destructor. |
| 8374 | FunctionProtoType::ExtProtoInfo EPI; |
| 8375 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8376 | EPI.ExceptionSpecDecl = Destructor; |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 8377 | Destructor->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8378 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8379 | AddOverriddenMethods(ClassDecl, Destructor); |
| 8380 | |
| 8381 | // We don't need to use SpecialMemberIsTrivial here; triviality for |
| 8382 | // destructors is easy to compute. |
| 8383 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
| 8384 | |
| 8385 | if (ShouldDeleteSpecialMember(Destructor, CXXDestructor)) |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 8386 | SetDeclDeleted(Destructor, ClassLoc); |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8387 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8388 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8389 | ++ASTContext::NumImplicitDestructorsDeclared; |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8390 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8391 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 8392 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8393 | PushOnScopeChains(Destructor, S, false); |
| 8394 | ClassDecl->addDecl(Destructor); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8395 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8396 | return Destructor; |
| 8397 | } |
| 8398 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8399 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 8400 | CXXDestructorDecl *Destructor) { |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 8401 | assert((Destructor->isDefaulted() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8402 | !Destructor->doesThisDeclarationHaveABody() && |
| 8403 | !Destructor->isDeleted()) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8404 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 8405 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8406 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8407 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8408 | if (Destructor->isInvalidDecl()) |
| 8409 | return; |
| 8410 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8411 | SynthesizedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8412 | |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8413 | DiagnosticErrorTrap Trap(Diags); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 8414 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 8415 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8416 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8417 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 8418 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8419 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 8420 | |
| 8421 | Destructor->setInvalidDecl(); |
| 8422 | return; |
| 8423 | } |
| 8424 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8425 | SourceLocation Loc = Destructor->getLocation(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 8426 | Destructor->setBody(new (Context) CompoundStmt(Loc)); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame] | 8427 | Destructor->setImplicitlyDefined(true); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8428 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8429 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 8430 | |
| 8431 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8432 | L->CompletedImplicitDefinition(Destructor); |
| 8433 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8434 | } |
| 8435 | |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 8436 | /// \brief Perform any semantic analysis which needs to be delayed until all |
| 8437 | /// pending class member declarations have been parsed. |
| 8438 | void Sema::ActOnFinishCXXMemberDecls() { |
Douglas Gregor | 1031884 | 2013-02-01 04:49:10 +0000 | [diff] [blame] | 8439 | // If the context is an invalid C++ class, just suppress these checks. |
| 8440 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8441 | if (Record->isInvalidDecl()) { |
| 8442 | DelayedDestructorExceptionSpecChecks.clear(); |
| 8443 | return; |
| 8444 | } |
| 8445 | } |
| 8446 | |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 8447 | // Perform any deferred checking of exception specifications for virtual |
| 8448 | // destructors. |
| 8449 | for (unsigned i = 0, e = DelayedDestructorExceptionSpecChecks.size(); |
| 8450 | i != e; ++i) { |
| 8451 | const CXXDestructorDecl *Dtor = |
| 8452 | DelayedDestructorExceptionSpecChecks[i].first; |
| 8453 | assert(!Dtor->getParent()->isDependentType() && |
| 8454 | "Should not ever add destructors of templates into the list."); |
| 8455 | CheckOverridingFunctionExceptionSpec(Dtor, |
| 8456 | DelayedDestructorExceptionSpecChecks[i].second); |
| 8457 | } |
| 8458 | DelayedDestructorExceptionSpecChecks.clear(); |
| 8459 | } |
| 8460 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8461 | void Sema::AdjustDestructorExceptionSpec(CXXRecordDecl *ClassDecl, |
| 8462 | CXXDestructorDecl *Destructor) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8463 | assert(getLangOpts().CPlusPlus11 && |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8464 | "adjusting dtor exception specs was introduced in c++11"); |
| 8465 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8466 | // C++11 [class.dtor]p3: |
| 8467 | // A declaration of a destructor that does not have an exception- |
| 8468 | // specification is implicitly considered to have the same exception- |
| 8469 | // specification as an implicit declaration. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8470 | const FunctionProtoType *DtorType = Destructor->getType()-> |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8471 | getAs<FunctionProtoType>(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8472 | if (DtorType->hasExceptionSpec()) |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8473 | return; |
| 8474 | |
Chandler Carruth | 3f224b2 | 2011-09-20 04:55:26 +0000 | [diff] [blame] | 8475 | // Replace the destructor's type, building off the existing one. Fortunately, |
| 8476 | // the only thing of interest in the destructor type is its extended info. |
| 8477 | // The return and arguments are fixed. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8478 | FunctionProtoType::ExtProtoInfo EPI = DtorType->getExtProtoInfo(); |
| 8479 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8480 | EPI.ExceptionSpecDecl = Destructor; |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 8481 | Destructor->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 8482 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8483 | // FIXME: If the destructor has a body that could throw, and the newly created |
| 8484 | // spec doesn't allow exceptions, we should emit a warning, because this |
| 8485 | // change in behavior can break conforming C++03 programs at runtime. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8486 | // However, we don't have a body or an exception specification yet, so it |
| 8487 | // needs to be done somewhere else. |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8488 | } |
| 8489 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8490 | /// When generating a defaulted copy or move assignment operator, if a field |
| 8491 | /// should be copied with __builtin_memcpy rather than via explicit assignments, |
| 8492 | /// do so. This optimization only applies for arrays of scalars, and for arrays |
| 8493 | /// of class type where the selected copy/move-assignment operator is trivial. |
| 8494 | static StmtResult |
| 8495 | buildMemcpyForAssignmentOp(Sema &S, SourceLocation Loc, QualType T, |
| 8496 | Expr *To, Expr *From) { |
| 8497 | // Compute the size of the memory buffer to be copied. |
| 8498 | QualType SizeType = S.Context.getSizeType(); |
| 8499 | llvm::APInt Size(S.Context.getTypeSize(SizeType), |
| 8500 | S.Context.getTypeSizeInChars(T).getQuantity()); |
| 8501 | |
| 8502 | // Take the address of the field references for "from" and "to". We |
| 8503 | // directly construct UnaryOperators here because semantic analysis |
| 8504 | // does not permit us to take the address of an xvalue. |
| 8505 | From = new (S.Context) UnaryOperator(From, UO_AddrOf, |
| 8506 | S.Context.getPointerType(From->getType()), |
| 8507 | VK_RValue, OK_Ordinary, Loc); |
| 8508 | To = new (S.Context) UnaryOperator(To, UO_AddrOf, |
| 8509 | S.Context.getPointerType(To->getType()), |
| 8510 | VK_RValue, OK_Ordinary, Loc); |
| 8511 | |
| 8512 | const Type *E = T->getBaseElementTypeUnsafe(); |
| 8513 | bool NeedsCollectableMemCpy = |
| 8514 | E->isRecordType() && E->getAs<RecordType>()->getDecl()->hasObjectMember(); |
| 8515 | |
| 8516 | // Create a reference to the __builtin_objc_memmove_collectable function |
| 8517 | StringRef MemCpyName = NeedsCollectableMemCpy ? |
| 8518 | "__builtin_objc_memmove_collectable" : |
| 8519 | "__builtin_memcpy"; |
| 8520 | LookupResult R(S, &S.Context.Idents.get(MemCpyName), Loc, |
| 8521 | Sema::LookupOrdinaryName); |
| 8522 | S.LookupName(R, S.TUScope, true); |
| 8523 | |
| 8524 | FunctionDecl *MemCpy = R.getAsSingle<FunctionDecl>(); |
| 8525 | if (!MemCpy) |
| 8526 | // Something went horribly wrong earlier, and we will have complained |
| 8527 | // about it. |
| 8528 | return StmtError(); |
| 8529 | |
| 8530 | ExprResult MemCpyRef = S.BuildDeclRefExpr(MemCpy, S.Context.BuiltinFnTy, |
| 8531 | VK_RValue, Loc, 0); |
| 8532 | assert(MemCpyRef.isUsable() && "Builtin reference cannot fail"); |
| 8533 | |
| 8534 | Expr *CallArgs[] = { |
| 8535 | To, From, IntegerLiteral::Create(S.Context, Size, SizeType, Loc) |
| 8536 | }; |
| 8537 | ExprResult Call = S.ActOnCallExpr(/*Scope=*/0, MemCpyRef.take(), |
| 8538 | Loc, CallArgs, Loc); |
| 8539 | |
| 8540 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 8541 | return S.Owned(Call.takeAs<Stmt>()); |
| 8542 | } |
| 8543 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8544 | /// \brief Builds a statement that copies/moves the given entity from \p From to |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8545 | /// \c To. |
| 8546 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8547 | /// This routine is used to copy/move the members of a class with an |
| 8548 | /// implicitly-declared copy/move assignment operator. When the entities being |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8549 | /// copied are arrays, this routine builds for loops to copy them. |
| 8550 | /// |
| 8551 | /// \param S The Sema object used for type-checking. |
| 8552 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8553 | /// \param Loc The location where the implicit copy/move is being generated. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8554 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8555 | /// \param T The type of the expressions being copied/moved. Both expressions |
| 8556 | /// must have this type. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8557 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8558 | /// \param To The expression we are copying/moving to. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8559 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8560 | /// \param From The expression we are copying/moving from. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8561 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8562 | /// \param CopyingBaseSubobject Whether we're copying/moving a base subobject. |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 8563 | /// Otherwise, it's a non-static member subobject. |
| 8564 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8565 | /// \param Copying Whether we're copying or moving. |
| 8566 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8567 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 8568 | /// |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8569 | /// \returns A statement or a loop that copies the expressions, or StmtResult(0) |
| 8570 | /// if a memcpy should be used instead. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8571 | static StmtResult |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8572 | buildSingleCopyAssignRecursively(Sema &S, SourceLocation Loc, QualType T, |
| 8573 | Expr *To, Expr *From, |
| 8574 | bool CopyingBaseSubobject, bool Copying, |
| 8575 | unsigned Depth = 0) { |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8576 | // C++11 [class.copy]p28: |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8577 | // Each subobject is assigned in the manner appropriate to its type: |
| 8578 | // |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8579 | // - if the subobject is of class type, as if by a call to operator= with |
| 8580 | // the subobject as the object expression and the corresponding |
| 8581 | // subobject of x as a single function argument (as if by explicit |
| 8582 | // qualification; that is, ignoring any possible virtual overriding |
| 8583 | // functions in more derived classes); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8584 | // |
| 8585 | // C++03 [class.copy]p13: |
| 8586 | // - if the subobject is of class type, the copy assignment operator for |
| 8587 | // the class is used (as if by explicit qualification; that is, |
| 8588 | // ignoring any possible virtual overriding functions in more derived |
| 8589 | // classes); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8590 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 8591 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8592 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8593 | // Look for operator=. |
| 8594 | DeclarationName Name |
| 8595 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 8596 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 8597 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8598 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8599 | // Prior to C++11, filter out any result that isn't a copy/move-assignment |
| 8600 | // operator. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8601 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8602 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 8603 | while (F.hasNext()) { |
| 8604 | NamedDecl *D = F.next(); |
| 8605 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 8606 | if (Method->isCopyAssignmentOperator() || |
| 8607 | (!Copying && Method->isMoveAssignmentOperator())) |
| 8608 | continue; |
| 8609 | |
| 8610 | F.erase(); |
| 8611 | } |
| 8612 | F.done(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 8613 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8614 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 8615 | // Suppress the protected check (C++ [class.protected]) for each of the |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8616 | // assignment operators we found. This strange dance is required when |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 8617 | // we're assigning via a base classes's copy-assignment operator. To |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8618 | // ensure that we're getting the right base class subobject (without |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 8619 | // ambiguities), we need to cast "this" to that subobject type; to |
| 8620 | // ensure that we don't go through the virtual call mechanism, we need |
| 8621 | // to qualify the operator= name with the base class (see below). However, |
| 8622 | // this means that if the base class has a protected copy assignment |
| 8623 | // operator, the protected member access check will fail. So, we |
| 8624 | // rewrite "protected" access to "public" access in this case, since we |
| 8625 | // know by construction that we're calling from a derived class. |
| 8626 | if (CopyingBaseSubobject) { |
| 8627 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 8628 | L != LEnd; ++L) { |
| 8629 | if (L.getAccess() == AS_protected) |
| 8630 | L.setAccess(AS_public); |
| 8631 | } |
| 8632 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8633 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8634 | // Create the nested-name-specifier that will be used to qualify the |
| 8635 | // reference to operator=; this is required to suppress the virtual |
| 8636 | // call mechanism. |
| 8637 | CXXScopeSpec SS; |
Manuel Klimek | 5b6a3dd | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 8638 | const Type *CanonicalT = S.Context.getCanonicalType(T.getTypePtr()); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8639 | SS.MakeTrivial(S.Context, |
| 8640 | NestedNameSpecifier::Create(S.Context, 0, false, |
Manuel Klimek | 5b6a3dd | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 8641 | CanonicalT), |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 8642 | Loc); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8643 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8644 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8645 | ExprResult OpEqualRef |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8646 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8647 | /*TemplateKWLoc=*/SourceLocation(), |
| 8648 | /*FirstQualifierInScope=*/0, |
| 8649 | OpLookup, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8650 | /*TemplateArgs=*/0, |
| 8651 | /*SuppressQualifierCheck=*/true); |
| 8652 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8653 | return StmtError(); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8654 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8655 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8656 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8657 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 8658 | OpEqualRef.takeAs<Expr>(), |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 8659 | Loc, From, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8660 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8661 | return StmtError(); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8662 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8663 | // If we built a call to a trivial 'operator=' while copying an array, |
| 8664 | // bail out. We'll replace the whole shebang with a memcpy. |
| 8665 | CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Call.get()); |
| 8666 | if (CE && CE->getMethodDecl()->isTrivial() && Depth) |
| 8667 | return StmtResult((Stmt*)0); |
| 8668 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8669 | // Convert to an expression-statement, and clean up any produced |
| 8670 | // temporaries. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8671 | return S.ActOnExprStmt(Call); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8672 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 8673 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8674 | // - if the subobject is of scalar type, the built-in assignment |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8675 | // operator is used. |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8676 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8677 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8678 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8679 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8680 | return StmtError(); |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8681 | return S.ActOnExprStmt(Assignment); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8682 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8683 | |
| 8684 | // - if the subobject is an array, each element is assigned, in the |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8685 | // manner appropriate to the element type; |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8686 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8687 | // Construct a loop over the array bounds, e.g., |
| 8688 | // |
| 8689 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 8690 | // |
| 8691 | // that will copy each of the array elements. |
| 8692 | QualType SizeType = S.Context.getSizeType(); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8693 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8694 | // Create the iteration variable. |
| 8695 | IdentifierInfo *IterationVarName = 0; |
| 8696 | { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8697 | SmallString<8> Str; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8698 | llvm::raw_svector_ostream OS(Str); |
| 8699 | OS << "__i" << Depth; |
| 8700 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 8701 | } |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8702 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8703 | IterationVarName, SizeType, |
| 8704 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8705 | SC_None); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8706 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8707 | // Initialize the iteration variable to zero. |
| 8708 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 8709 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8710 | |
| 8711 | // Create a reference to the iteration variable; we'll use this several |
| 8712 | // times throughout. |
| 8713 | Expr *IterationVarRef |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 8714 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8715 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 8716 | Expr *IterationVarRefRVal = S.DefaultLvalueConversion(IterationVarRef).take(); |
| 8717 | assert(IterationVarRefRVal && "Conversion of invented variable cannot fail!"); |
| 8718 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8719 | // Create the DeclStmt that holds the iteration variable. |
| 8720 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8721 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8722 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8723 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 8724 | IterationVarRefRVal, |
| 8725 | Loc)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8726 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 8727 | IterationVarRefRVal, |
| 8728 | Loc)); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8729 | if (!Copying) // Cast to rvalue |
| 8730 | From = CastForMoving(S, From); |
| 8731 | |
| 8732 | // Build the copy/move for an individual element of the array. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8733 | StmtResult Copy = |
| 8734 | buildSingleCopyAssignRecursively(S, Loc, ArrayTy->getElementType(), |
| 8735 | To, From, CopyingBaseSubobject, |
| 8736 | Copying, Depth + 1); |
| 8737 | // Bail out if copying fails or if we determined that we should use memcpy. |
| 8738 | if (Copy.isInvalid() || !Copy.get()) |
| 8739 | return Copy; |
| 8740 | |
| 8741 | // Create the comparison against the array bound. |
| 8742 | llvm::APInt Upper |
| 8743 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
| 8744 | Expr *Comparison |
| 8745 | = new (S.Context) BinaryOperator(IterationVarRefRVal, |
| 8746 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 8747 | BO_NE, S.Context.BoolTy, |
| 8748 | VK_RValue, OK_Ordinary, Loc, false); |
| 8749 | |
| 8750 | // Create the pre-increment of the iteration variable. |
| 8751 | Expr *Increment |
| 8752 | = new (S.Context) UnaryOperator(IterationVarRef, UO_PreInc, SizeType, |
| 8753 | VK_LValue, OK_Ordinary, Loc); |
| 8754 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8755 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8756 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8757 | S.MakeFullExpr(Comparison), |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8758 | 0, S.MakeFullDiscardedValueExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8759 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8760 | } |
| 8761 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8762 | static StmtResult |
| 8763 | buildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
| 8764 | Expr *To, Expr *From, |
| 8765 | bool CopyingBaseSubobject, bool Copying) { |
| 8766 | // Maybe we should use a memcpy? |
| 8767 | if (T->isArrayType() && !T.isConstQualified() && !T.isVolatileQualified() && |
| 8768 | T.isTriviallyCopyableType(S.Context)) |
| 8769 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 8770 | |
| 8771 | StmtResult Result(buildSingleCopyAssignRecursively(S, Loc, T, To, From, |
| 8772 | CopyingBaseSubobject, |
| 8773 | Copying, 0)); |
| 8774 | |
| 8775 | // If we ended up picking a trivial assignment operator for an array of a |
| 8776 | // non-trivially-copyable class type, just emit a memcpy. |
| 8777 | if (!Result.isInvalid() && !Result.get()) |
| 8778 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 8779 | |
| 8780 | return Result; |
| 8781 | } |
| 8782 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8783 | Sema::ImplicitExceptionSpecification |
| 8784 | Sema::ComputeDefaultedCopyAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 8785 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 8786 | |
| 8787 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 8788 | if (ClassDecl->isInvalidDecl()) |
| 8789 | return ExceptSpec; |
| 8790 | |
| 8791 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
| 8792 | assert(T->getNumArgs() == 1 && "not a copy assignment op"); |
| 8793 | unsigned ArgQuals = T->getArgType(0).getNonReferenceType().getCVRQualifiers(); |
| 8794 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8795 | // C++ [except.spec]p14: |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8796 | // An implicitly declared special member function (Clause 12) shall have an |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8797 | // exception-specification. [...] |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8798 | |
| 8799 | // It is unspecified whether or not an implicit copy assignment operator |
| 8800 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 8801 | // made. As such, this exception specification is effectively unspecified. |
| 8802 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 8803 | // the side of assuming such calls to be made regardless of whether they |
| 8804 | // actually happen. |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8805 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8806 | BaseEnd = ClassDecl->bases_end(); |
| 8807 | Base != BaseEnd; ++Base) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8808 | if (Base->isVirtual()) |
| 8809 | continue; |
| 8810 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 8811 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8812 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8813 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 8814 | ArgQuals, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8815 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyAssign); |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8816 | } |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8817 | |
| 8818 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8819 | BaseEnd = ClassDecl->vbases_end(); |
| 8820 | Base != BaseEnd; ++Base) { |
| 8821 | CXXRecordDecl *BaseClassDecl |
| 8822 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 8823 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 8824 | ArgQuals, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8825 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyAssign); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8826 | } |
| 8827 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8828 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8829 | FieldEnd = ClassDecl->field_end(); |
| 8830 | Field != FieldEnd; |
| 8831 | ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 8832 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8833 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 8834 | if (CXXMethodDecl *CopyAssign = |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8835 | LookupCopyingAssignment(FieldClassDecl, |
| 8836 | ArgQuals | FieldType.getCVRQualifiers(), |
| 8837 | false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8838 | ExceptSpec.CalledDecl(Field->getLocation(), CopyAssign); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 8839 | } |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8840 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8841 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8842 | return ExceptSpec; |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 8843 | } |
| 8844 | |
| 8845 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
| 8846 | // Note: The following rules are largely analoguous to the copy |
| 8847 | // constructor rules. Note that virtual bases are not taken into account |
| 8848 | // for determining the argument type of the operator. Note also that |
| 8849 | // operators taking an object instead of a reference are allowed. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 8850 | assert(ClassDecl->needsImplicitCopyAssignment()); |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 8851 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8852 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyAssignment); |
| 8853 | if (DSM.isAlreadyBeingDeclared()) |
| 8854 | return 0; |
| 8855 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 8856 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 8857 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 8858 | bool Const = ClassDecl->implicitCopyAssignmentHasConstParam(); |
| 8859 | if (Const) |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 8860 | ArgType = ArgType.withConst(); |
| 8861 | ArgType = Context.getLValueReferenceType(ArgType); |
| 8862 | |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 8863 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 8864 | CXXCopyAssignment, |
| 8865 | Const); |
| 8866 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8867 | // An implicitly-declared copy assignment operator is an inline public |
| 8868 | // member of its class. |
| 8869 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8870 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 8871 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 8872 | CXXMethodDecl *CopyAssignment = |
| 8873 | CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
| 8874 | /*TInfo=*/ 0, /*StorageClass=*/ SC_None, |
| 8875 | /*isInline=*/ true, Constexpr, SourceLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8876 | CopyAssignment->setAccess(AS_public); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 8877 | CopyAssignment->setDefaulted(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8878 | CopyAssignment->setImplicit(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8879 | |
| 8880 | // Build an exception specification pointing back at this member. |
| 8881 | FunctionProtoType::ExtProtoInfo EPI; |
| 8882 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8883 | EPI.ExceptionSpecDecl = CopyAssignment; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 8884 | CopyAssignment->setType(Context.getFunctionType(RetType, ArgType, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8885 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8886 | // Add the parameter to the operator. |
| 8887 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8888 | ClassLoc, ClassLoc, /*Id=*/0, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8889 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8890 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8891 | CopyAssignment->setParams(FromParam); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 8892 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8893 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 8894 | |
| 8895 | CopyAssignment->setTrivial( |
| 8896 | ClassDecl->needsOverloadResolutionForCopyAssignment() |
| 8897 | ? SpecialMemberIsTrivial(CopyAssignment, CXXCopyAssignment) |
| 8898 | : ClassDecl->hasTrivialCopyAssignment()); |
| 8899 | |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 8900 | // C++11 [class.copy]p19: |
Nico Weber | afcc96a | 2012-01-23 03:19:29 +0000 | [diff] [blame] | 8901 | // .... If the class definition does not explicitly declare a copy |
| 8902 | // assignment operator, there is no user-declared move constructor, and |
| 8903 | // there is no user-declared move assignment operator, a copy assignment |
| 8904 | // operator is implicitly declared as defaulted. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8905 | if (ShouldDeleteSpecialMember(CopyAssignment, CXXCopyAssignment)) |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 8906 | SetDeclDeleted(CopyAssignment, ClassLoc); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8907 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8908 | // Note that we have added this copy-assignment operator. |
| 8909 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
| 8910 | |
| 8911 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 8912 | PushOnScopeChains(CopyAssignment, S, false); |
| 8913 | ClassDecl->addDecl(CopyAssignment); |
| 8914 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8915 | return CopyAssignment; |
| 8916 | } |
| 8917 | |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 8918 | /// Diagnose an implicit copy operation for a class which is odr-used, but |
| 8919 | /// which is deprecated because the class has a user-declared copy constructor, |
| 8920 | /// copy assignment operator, or destructor. |
| 8921 | static void diagnoseDeprecatedCopyOperation(Sema &S, CXXMethodDecl *CopyOp, |
| 8922 | SourceLocation UseLoc) { |
| 8923 | assert(CopyOp->isImplicit()); |
| 8924 | |
| 8925 | CXXRecordDecl *RD = CopyOp->getParent(); |
| 8926 | CXXMethodDecl *UserDeclaredOperation = 0; |
| 8927 | |
| 8928 | // In Microsoft mode, assignment operations don't affect constructors and |
| 8929 | // vice versa. |
| 8930 | if (RD->hasUserDeclaredDestructor()) { |
| 8931 | UserDeclaredOperation = RD->getDestructor(); |
| 8932 | } else if (!isa<CXXConstructorDecl>(CopyOp) && |
| 8933 | RD->hasUserDeclaredCopyConstructor() && |
| 8934 | !S.getLangOpts().MicrosoftMode) { |
| 8935 | // Find any user-declared copy constructor. |
| 8936 | for (CXXRecordDecl::ctor_iterator I = RD->ctor_begin(), |
| 8937 | E = RD->ctor_end(); I != E; ++I) { |
| 8938 | if (I->isCopyConstructor()) { |
| 8939 | UserDeclaredOperation = *I; |
| 8940 | break; |
| 8941 | } |
| 8942 | } |
| 8943 | assert(UserDeclaredOperation); |
| 8944 | } else if (isa<CXXConstructorDecl>(CopyOp) && |
| 8945 | RD->hasUserDeclaredCopyAssignment() && |
| 8946 | !S.getLangOpts().MicrosoftMode) { |
| 8947 | // Find any user-declared move assignment operator. |
| 8948 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 8949 | E = RD->method_end(); I != E; ++I) { |
| 8950 | if (I->isCopyAssignmentOperator()) { |
| 8951 | UserDeclaredOperation = *I; |
| 8952 | break; |
| 8953 | } |
| 8954 | } |
| 8955 | assert(UserDeclaredOperation); |
| 8956 | } |
| 8957 | |
| 8958 | if (UserDeclaredOperation) { |
| 8959 | S.Diag(UserDeclaredOperation->getLocation(), |
| 8960 | diag::warn_deprecated_copy_operation) |
| 8961 | << RD << /*copy assignment*/!isa<CXXConstructorDecl>(CopyOp) |
| 8962 | << /*destructor*/isa<CXXDestructorDecl>(UserDeclaredOperation); |
| 8963 | S.Diag(UseLoc, diag::note_member_synthesized_at) |
| 8964 | << (isa<CXXConstructorDecl>(CopyOp) ? Sema::CXXCopyConstructor |
| 8965 | : Sema::CXXCopyAssignment) |
| 8966 | << RD; |
| 8967 | } |
| 8968 | } |
| 8969 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8970 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 8971 | CXXMethodDecl *CopyAssignOperator) { |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 8972 | assert((CopyAssignOperator->isDefaulted() && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8973 | CopyAssignOperator->isOverloadedOperator() && |
| 8974 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8975 | !CopyAssignOperator->doesThisDeclarationHaveABody() && |
| 8976 | !CopyAssignOperator->isDeleted()) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8977 | "DefineImplicitCopyAssignment called for wrong function"); |
| 8978 | |
| 8979 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 8980 | |
| 8981 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 8982 | CopyAssignOperator->setInvalidDecl(); |
| 8983 | return; |
| 8984 | } |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 8985 | |
| 8986 | // C++11 [class.copy]p18: |
| 8987 | // The [definition of an implicitly declared copy assignment operator] is |
| 8988 | // deprecated if the class has a user-declared copy constructor or a |
| 8989 | // user-declared destructor. |
| 8990 | if (getLangOpts().CPlusPlus11 && CopyAssignOperator->isImplicit()) |
| 8991 | diagnoseDeprecatedCopyOperation(*this, CopyAssignOperator, CurrentLocation); |
| 8992 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8993 | CopyAssignOperator->setUsed(); |
| 8994 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8995 | SynthesizedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8996 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8997 | |
| 8998 | // C++0x [class.copy]p30: |
| 8999 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 9000 | // for a non-union class X performs memberwise copy assignment of its |
| 9001 | // subobjects. The direct base classes of X are assigned first, in the |
| 9002 | // order of their declaration in the base-specifier-list, and then the |
| 9003 | // immediate non-static data members of X are assigned, in the order in |
| 9004 | // which they were declared in the class definition. |
| 9005 | |
| 9006 | // The statements that form the synthesized function body. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 9007 | SmallVector<Stmt*, 8> Statements; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9008 | |
| 9009 | // The parameter for the "other" object, which we are copying from. |
| 9010 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 9011 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 9012 | QualType OtherRefType = Other->getType(); |
| 9013 | if (const LValueReferenceType *OtherRef |
| 9014 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 9015 | OtherRefType = OtherRef->getPointeeType(); |
| 9016 | OtherQuals = OtherRefType.getQualifiers(); |
| 9017 | } |
| 9018 | |
| 9019 | // Our location for everything implicitly-generated. |
| 9020 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 9021 | |
| 9022 | // Construct a reference to the "other" object. We'll be using this |
| 9023 | // throughout the generated ASTs. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9024 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9025 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 9026 | |
| 9027 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 9028 | // ASTs. |
| 9029 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 9030 | assert(This && "Reference to this cannot fail!"); |
| 9031 | |
| 9032 | // Assign base classes. |
| 9033 | bool Invalid = false; |
| 9034 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9035 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 9036 | // Form the assignment: |
| 9037 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 9038 | QualType BaseType = Base->getType().getUnqualifiedType(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 9039 | if (!BaseType->isRecordType()) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9040 | Invalid = true; |
| 9041 | continue; |
| 9042 | } |
| 9043 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9044 | CXXCastPath BasePath; |
| 9045 | BasePath.push_back(Base); |
| 9046 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9047 | // Construct the "from" expression, which is an implicit cast to the |
| 9048 | // appropriately-qualified base type. |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9049 | Expr *From = OtherRef; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9050 | From = ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
| 9051 | CK_UncheckedDerivedToBase, |
| 9052 | VK_LValue, &BasePath).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9053 | |
| 9054 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 9055 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9056 | |
| 9057 | // Implicitly cast "this" to the appropriately-qualified base type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9058 | To = ImpCastExprToType(To.take(), |
| 9059 | Context.getCVRQualifiedType(BaseType, |
| 9060 | CopyAssignOperator->getTypeQualifiers()), |
| 9061 | CK_UncheckedDerivedToBase, |
| 9062 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9063 | |
| 9064 | // Build the copy. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9065 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 9066 | To.get(), From, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9067 | /*CopyingBaseSubobject=*/true, |
| 9068 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9069 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9070 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9071 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 9072 | CopyAssignOperator->setInvalidDecl(); |
| 9073 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9074 | } |
| 9075 | |
| 9076 | // Success! Record the copy. |
| 9077 | Statements.push_back(Copy.takeAs<Expr>()); |
| 9078 | } |
| 9079 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9080 | // Assign non-static members. |
| 9081 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9082 | FieldEnd = ClassDecl->field_end(); |
| 9083 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 9084 | if (Field->isUnnamedBitfield()) |
| 9085 | continue; |
Eli Friedman | 8150da3 | 2013-06-07 01:48:56 +0000 | [diff] [blame] | 9086 | |
| 9087 | if (Field->isInvalidDecl()) { |
| 9088 | Invalid = true; |
| 9089 | continue; |
| 9090 | } |
| 9091 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9092 | // Check for members of reference type; we can't copy those. |
| 9093 | if (Field->getType()->isReferenceType()) { |
| 9094 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9095 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 9096 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9097 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9098 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9099 | Invalid = true; |
| 9100 | continue; |
| 9101 | } |
| 9102 | |
| 9103 | // Check for members of const-qualified, non-class type. |
| 9104 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 9105 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 9106 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9107 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 9108 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9109 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9110 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9111 | Invalid = true; |
| 9112 | continue; |
| 9113 | } |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 9114 | |
| 9115 | // Suppress assigning zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9116 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 9117 | continue; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9118 | |
| 9119 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 9120 | if (FieldType->isIncompleteArrayType()) { |
| 9121 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 9122 | "Incomplete array type is not valid"); |
| 9123 | continue; |
| 9124 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9125 | |
| 9126 | // Build references to the field in the object we're copying from and to. |
| 9127 | CXXScopeSpec SS; // Intentionally empty |
| 9128 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 9129 | LookupMemberName); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 9130 | MemberLookup.addDecl(*Field); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9131 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9132 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9133 | Loc, /*IsArrow=*/false, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9134 | SS, SourceLocation(), 0, |
| 9135 | MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9136 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9137 | Loc, /*IsArrow=*/true, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9138 | SS, SourceLocation(), 0, |
| 9139 | MemberLookup, 0); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9140 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 9141 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9142 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9143 | // Build the copy of this field. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9144 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, FieldType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9145 | To.get(), From.get(), |
| 9146 | /*CopyingBaseSubobject=*/false, |
| 9147 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9148 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9149 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9150 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 9151 | CopyAssignOperator->setInvalidDecl(); |
| 9152 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9153 | } |
| 9154 | |
| 9155 | // Success! Record the copy. |
| 9156 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 9157 | } |
| 9158 | |
| 9159 | if (!Invalid) { |
| 9160 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9161 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9162 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9163 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9164 | if (Return.isInvalid()) |
| 9165 | Invalid = true; |
| 9166 | else { |
| 9167 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 9168 | |
| 9169 | if (Trap.hasErrorOccurred()) { |
| 9170 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9171 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 9172 | Invalid = true; |
| 9173 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9174 | } |
| 9175 | } |
| 9176 | |
| 9177 | if (Invalid) { |
| 9178 | CopyAssignOperator->setInvalidDecl(); |
| 9179 | return; |
| 9180 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9181 | |
| 9182 | StmtResult Body; |
| 9183 | { |
| 9184 | CompoundScopeRAII CompoundScope(*this); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9185 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9186 | /*isStmtExpr=*/false); |
| 9187 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 9188 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9189 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 9190 | |
| 9191 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9192 | L->CompletedImplicitDefinition(CopyAssignOperator); |
| 9193 | } |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9194 | } |
| 9195 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9196 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9197 | Sema::ComputeDefaultedMoveAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 9198 | CXXRecordDecl *ClassDecl = MD->getParent(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9199 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9200 | ImplicitExceptionSpecification ExceptSpec(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9201 | if (ClassDecl->isInvalidDecl()) |
| 9202 | return ExceptSpec; |
| 9203 | |
| 9204 | // C++0x [except.spec]p14: |
| 9205 | // An implicitly declared special member function (Clause 12) shall have an |
| 9206 | // exception-specification. [...] |
| 9207 | |
| 9208 | // It is unspecified whether or not an implicit move assignment operator |
| 9209 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 9210 | // made. As such, this exception specification is effectively unspecified. |
| 9211 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 9212 | // the side of assuming such calls to be made regardless of whether they |
| 9213 | // actually happen. |
| 9214 | // Note that a move constructor is not implicitly declared when there are |
| 9215 | // virtual bases, but it can still be user-declared and explicitly defaulted. |
| 9216 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9217 | BaseEnd = ClassDecl->bases_end(); |
| 9218 | Base != BaseEnd; ++Base) { |
| 9219 | if (Base->isVirtual()) |
| 9220 | continue; |
| 9221 | |
| 9222 | CXXRecordDecl *BaseClassDecl |
| 9223 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 9224 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9225 | 0, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9226 | ExceptSpec.CalledDecl(Base->getLocStart(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9227 | } |
| 9228 | |
| 9229 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 9230 | BaseEnd = ClassDecl->vbases_end(); |
| 9231 | Base != BaseEnd; ++Base) { |
| 9232 | CXXRecordDecl *BaseClassDecl |
| 9233 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 9234 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9235 | 0, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9236 | ExceptSpec.CalledDecl(Base->getLocStart(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9237 | } |
| 9238 | |
| 9239 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9240 | FieldEnd = ClassDecl->field_end(); |
| 9241 | Field != FieldEnd; |
| 9242 | ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 9243 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9244 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9245 | if (CXXMethodDecl *MoveAssign = |
| 9246 | LookupMovingAssignment(FieldClassDecl, |
| 9247 | FieldType.getCVRQualifiers(), |
| 9248 | false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9249 | ExceptSpec.CalledDecl(Field->getLocation(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9250 | } |
| 9251 | } |
| 9252 | |
| 9253 | return ExceptSpec; |
| 9254 | } |
| 9255 | |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9256 | /// Determine whether the class type has any direct or indirect virtual base |
| 9257 | /// classes which have a non-trivial move assignment operator. |
| 9258 | static bool |
| 9259 | hasVirtualBaseWithNonTrivialMoveAssignment(Sema &S, CXXRecordDecl *ClassDecl) { |
| 9260 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 9261 | BaseEnd = ClassDecl->vbases_end(); |
| 9262 | Base != BaseEnd; ++Base) { |
| 9263 | CXXRecordDecl *BaseClass = |
| 9264 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 9265 | |
| 9266 | // Try to declare the move assignment. If it would be deleted, then the |
| 9267 | // class does not have a non-trivial move assignment. |
| 9268 | if (BaseClass->needsImplicitMoveAssignment()) |
| 9269 | S.DeclareImplicitMoveAssignment(BaseClass); |
| 9270 | |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 9271 | if (BaseClass->hasNonTrivialMoveAssignment()) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9272 | return true; |
| 9273 | } |
| 9274 | |
| 9275 | return false; |
| 9276 | } |
| 9277 | |
| 9278 | /// Determine whether the given type either has a move constructor or is |
| 9279 | /// trivially copyable. |
| 9280 | static bool |
| 9281 | hasMoveOrIsTriviallyCopyable(Sema &S, QualType Type, bool IsConstructor) { |
| 9282 | Type = S.Context.getBaseElementType(Type); |
| 9283 | |
| 9284 | // FIXME: Technically, non-trivially-copyable non-class types, such as |
| 9285 | // reference types, are supposed to return false here, but that appears |
| 9286 | // to be a standard defect. |
| 9287 | CXXRecordDecl *ClassDecl = Type->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | b5e4ace | 2012-10-10 16:14:06 +0000 | [diff] [blame] | 9288 | if (!ClassDecl || !ClassDecl->getDefinition() || ClassDecl->isInvalidDecl()) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9289 | return true; |
| 9290 | |
| 9291 | if (Type.isTriviallyCopyableType(S.Context)) |
| 9292 | return true; |
| 9293 | |
| 9294 | if (IsConstructor) { |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9295 | // FIXME: Need this because otherwise hasMoveConstructor isn't guaranteed to |
| 9296 | // give the right answer. |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9297 | if (ClassDecl->needsImplicitMoveConstructor()) |
| 9298 | S.DeclareImplicitMoveConstructor(ClassDecl); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9299 | return ClassDecl->hasMoveConstructor(); |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9300 | } |
| 9301 | |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9302 | // FIXME: Need this because otherwise hasMoveAssignment isn't guaranteed to |
| 9303 | // give the right answer. |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9304 | if (ClassDecl->needsImplicitMoveAssignment()) |
| 9305 | S.DeclareImplicitMoveAssignment(ClassDecl); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9306 | return ClassDecl->hasMoveAssignment(); |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9307 | } |
| 9308 | |
| 9309 | /// Determine whether all non-static data members and direct or virtual bases |
| 9310 | /// of class \p ClassDecl have either a move operation, or are trivially |
| 9311 | /// copyable. |
| 9312 | static bool subobjectsHaveMoveOrTrivialCopy(Sema &S, CXXRecordDecl *ClassDecl, |
| 9313 | bool IsConstructor) { |
| 9314 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9315 | BaseEnd = ClassDecl->bases_end(); |
| 9316 | Base != BaseEnd; ++Base) { |
| 9317 | if (Base->isVirtual()) |
| 9318 | continue; |
| 9319 | |
| 9320 | if (!hasMoveOrIsTriviallyCopyable(S, Base->getType(), IsConstructor)) |
| 9321 | return false; |
| 9322 | } |
| 9323 | |
| 9324 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 9325 | BaseEnd = ClassDecl->vbases_end(); |
| 9326 | Base != BaseEnd; ++Base) { |
| 9327 | if (!hasMoveOrIsTriviallyCopyable(S, Base->getType(), IsConstructor)) |
| 9328 | return false; |
| 9329 | } |
| 9330 | |
| 9331 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9332 | FieldEnd = ClassDecl->field_end(); |
| 9333 | Field != FieldEnd; ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 9334 | if (!hasMoveOrIsTriviallyCopyable(S, Field->getType(), IsConstructor)) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9335 | return false; |
| 9336 | } |
| 9337 | |
| 9338 | return true; |
| 9339 | } |
| 9340 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9341 | CXXMethodDecl *Sema::DeclareImplicitMoveAssignment(CXXRecordDecl *ClassDecl) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9342 | // C++11 [class.copy]p20: |
| 9343 | // If the definition of a class X does not explicitly declare a move |
| 9344 | // assignment operator, one will be implicitly declared as defaulted |
| 9345 | // if and only if: |
| 9346 | // |
| 9347 | // - [first 4 bullets] |
| 9348 | assert(ClassDecl->needsImplicitMoveAssignment()); |
| 9349 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9350 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveAssignment); |
| 9351 | if (DSM.isAlreadyBeingDeclared()) |
| 9352 | return 0; |
| 9353 | |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9354 | // [Checked after we build the declaration] |
| 9355 | // - the move assignment operator would not be implicitly defined as |
| 9356 | // deleted, |
| 9357 | |
| 9358 | // [DR1402]: |
| 9359 | // - X has no direct or indirect virtual base class with a non-trivial |
| 9360 | // move assignment operator, and |
| 9361 | // - each of X's non-static data members and direct or virtual base classes |
| 9362 | // has a type that either has a move assignment operator or is trivially |
| 9363 | // copyable. |
| 9364 | if (hasVirtualBaseWithNonTrivialMoveAssignment(*this, ClassDecl) || |
| 9365 | !subobjectsHaveMoveOrTrivialCopy(*this, ClassDecl,/*Constructor*/false)) { |
| 9366 | ClassDecl->setFailedImplicitMoveAssignment(); |
| 9367 | return 0; |
| 9368 | } |
| 9369 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9370 | // Note: The following rules are largely analoguous to the move |
| 9371 | // constructor rules. |
| 9372 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9373 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 9374 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 9375 | ArgType = Context.getRValueReferenceType(ArgType); |
| 9376 | |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9377 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 9378 | CXXMoveAssignment, |
| 9379 | false); |
| 9380 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9381 | // An implicitly-declared move assignment operator is an inline public |
| 9382 | // member of its class. |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9383 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 9384 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 9385 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9386 | CXXMethodDecl *MoveAssignment = |
| 9387 | CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
| 9388 | /*TInfo=*/0, /*StorageClass=*/SC_None, |
| 9389 | /*isInline=*/true, Constexpr, SourceLocation()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9390 | MoveAssignment->setAccess(AS_public); |
| 9391 | MoveAssignment->setDefaulted(); |
| 9392 | MoveAssignment->setImplicit(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9393 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9394 | // Build an exception specification pointing back at this member. |
| 9395 | FunctionProtoType::ExtProtoInfo EPI; |
| 9396 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 9397 | EPI.ExceptionSpecDecl = MoveAssignment; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9398 | MoveAssignment->setType(Context.getFunctionType(RetType, ArgType, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9399 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9400 | // Add the parameter to the operator. |
| 9401 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveAssignment, |
| 9402 | ClassLoc, ClassLoc, /*Id=*/0, |
| 9403 | ArgType, /*TInfo=*/0, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9404 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9405 | MoveAssignment->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9406 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9407 | AddOverriddenMethods(ClassDecl, MoveAssignment); |
| 9408 | |
| 9409 | MoveAssignment->setTrivial( |
| 9410 | ClassDecl->needsOverloadResolutionForMoveAssignment() |
| 9411 | ? SpecialMemberIsTrivial(MoveAssignment, CXXMoveAssignment) |
| 9412 | : ClassDecl->hasTrivialMoveAssignment()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9413 | |
| 9414 | // C++0x [class.copy]p9: |
| 9415 | // If the definition of a class X does not explicitly declare a move |
| 9416 | // assignment operator, one will be implicitly declared as defaulted if and |
| 9417 | // only if: |
| 9418 | // [...] |
| 9419 | // - the move assignment operator would not be implicitly defined as |
| 9420 | // deleted. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 9421 | if (ShouldDeleteSpecialMember(MoveAssignment, CXXMoveAssignment)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9422 | // Cache this result so that we don't try to generate this over and over |
| 9423 | // on every lookup, leaking memory and wasting time. |
| 9424 | ClassDecl->setFailedImplicitMoveAssignment(); |
| 9425 | return 0; |
| 9426 | } |
| 9427 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9428 | // Note that we have added this copy-assignment operator. |
| 9429 | ++ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
| 9430 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9431 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 9432 | PushOnScopeChains(MoveAssignment, S, false); |
| 9433 | ClassDecl->addDecl(MoveAssignment); |
| 9434 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9435 | return MoveAssignment; |
| 9436 | } |
| 9437 | |
| 9438 | void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation, |
| 9439 | CXXMethodDecl *MoveAssignOperator) { |
| 9440 | assert((MoveAssignOperator->isDefaulted() && |
| 9441 | MoveAssignOperator->isOverloadedOperator() && |
| 9442 | MoveAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9443 | !MoveAssignOperator->doesThisDeclarationHaveABody() && |
| 9444 | !MoveAssignOperator->isDeleted()) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9445 | "DefineImplicitMoveAssignment called for wrong function"); |
| 9446 | |
| 9447 | CXXRecordDecl *ClassDecl = MoveAssignOperator->getParent(); |
| 9448 | |
| 9449 | if (ClassDecl->isInvalidDecl() || MoveAssignOperator->isInvalidDecl()) { |
| 9450 | MoveAssignOperator->setInvalidDecl(); |
| 9451 | return; |
| 9452 | } |
| 9453 | |
| 9454 | MoveAssignOperator->setUsed(); |
| 9455 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9456 | SynthesizedFunctionScope Scope(*this, MoveAssignOperator); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9457 | DiagnosticErrorTrap Trap(Diags); |
| 9458 | |
| 9459 | // C++0x [class.copy]p28: |
| 9460 | // The implicitly-defined or move assignment operator for a non-union class |
| 9461 | // X performs memberwise move assignment of its subobjects. The direct base |
| 9462 | // classes of X are assigned first, in the order of their declaration in the |
| 9463 | // base-specifier-list, and then the immediate non-static data members of X |
| 9464 | // are assigned, in the order in which they were declared in the class |
| 9465 | // definition. |
| 9466 | |
| 9467 | // The statements that form the synthesized function body. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 9468 | SmallVector<Stmt*, 8> Statements; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9469 | |
| 9470 | // The parameter for the "other" object, which we are move from. |
| 9471 | ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0); |
| 9472 | QualType OtherRefType = Other->getType()-> |
| 9473 | getAs<RValueReferenceType>()->getPointeeType(); |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 9474 | assert(!OtherRefType.getQualifiers() && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9475 | "Bad argument type of defaulted move assignment"); |
| 9476 | |
| 9477 | // Our location for everything implicitly-generated. |
| 9478 | SourceLocation Loc = MoveAssignOperator->getLocation(); |
| 9479 | |
| 9480 | // Construct a reference to the "other" object. We'll be using this |
| 9481 | // throughout the generated ASTs. |
| 9482 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
| 9483 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 9484 | // Cast to rvalue. |
| 9485 | OtherRef = CastForMoving(*this, OtherRef); |
| 9486 | |
| 9487 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 9488 | // ASTs. |
| 9489 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 9490 | assert(This && "Reference to this cannot fail!"); |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9491 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9492 | // Assign base classes. |
| 9493 | bool Invalid = false; |
| 9494 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9495 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 9496 | // Form the assignment: |
| 9497 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&&>(other)); |
| 9498 | QualType BaseType = Base->getType().getUnqualifiedType(); |
| 9499 | if (!BaseType->isRecordType()) { |
| 9500 | Invalid = true; |
| 9501 | continue; |
| 9502 | } |
| 9503 | |
| 9504 | CXXCastPath BasePath; |
| 9505 | BasePath.push_back(Base); |
| 9506 | |
| 9507 | // Construct the "from" expression, which is an implicit cast to the |
| 9508 | // appropriately-qualified base type. |
| 9509 | Expr *From = OtherRef; |
| 9510 | From = ImpCastExprToType(From, BaseType, CK_UncheckedDerivedToBase, |
Douglas Gregor | b2b5658 | 2011-09-06 16:26:56 +0000 | [diff] [blame] | 9511 | VK_XValue, &BasePath).take(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9512 | |
| 9513 | // Dereference "this". |
| 9514 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
| 9515 | |
| 9516 | // Implicitly cast "this" to the appropriately-qualified base type. |
| 9517 | To = ImpCastExprToType(To.take(), |
| 9518 | Context.getCVRQualifiedType(BaseType, |
| 9519 | MoveAssignOperator->getTypeQualifiers()), |
| 9520 | CK_UncheckedDerivedToBase, |
| 9521 | VK_LValue, &BasePath); |
| 9522 | |
| 9523 | // Build the move. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9524 | StmtResult Move = buildSingleCopyAssign(*this, Loc, BaseType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9525 | To.get(), From, |
| 9526 | /*CopyingBaseSubobject=*/true, |
| 9527 | /*Copying=*/false); |
| 9528 | if (Move.isInvalid()) { |
| 9529 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9530 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9531 | MoveAssignOperator->setInvalidDecl(); |
| 9532 | return; |
| 9533 | } |
| 9534 | |
| 9535 | // Success! Record the move. |
| 9536 | Statements.push_back(Move.takeAs<Expr>()); |
| 9537 | } |
| 9538 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9539 | // Assign non-static members. |
| 9540 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9541 | FieldEnd = ClassDecl->field_end(); |
| 9542 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 9543 | if (Field->isUnnamedBitfield()) |
| 9544 | continue; |
| 9545 | |
Eli Friedman | 8150da3 | 2013-06-07 01:48:56 +0000 | [diff] [blame] | 9546 | if (Field->isInvalidDecl()) { |
| 9547 | Invalid = true; |
| 9548 | continue; |
| 9549 | } |
| 9550 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9551 | // Check for members of reference type; we can't move those. |
| 9552 | if (Field->getType()->isReferenceType()) { |
| 9553 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9554 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 9555 | Diag(Field->getLocation(), diag::note_declared_at); |
| 9556 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9557 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9558 | Invalid = true; |
| 9559 | continue; |
| 9560 | } |
| 9561 | |
| 9562 | // Check for members of const-qualified, non-class type. |
| 9563 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 9564 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 9565 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9566 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 9567 | Diag(Field->getLocation(), diag::note_declared_at); |
| 9568 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9569 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9570 | Invalid = true; |
| 9571 | continue; |
| 9572 | } |
| 9573 | |
| 9574 | // Suppress assigning zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9575 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 9576 | continue; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9577 | |
| 9578 | QualType FieldType = Field->getType().getNonReferenceType(); |
| 9579 | if (FieldType->isIncompleteArrayType()) { |
| 9580 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 9581 | "Incomplete array type is not valid"); |
| 9582 | continue; |
| 9583 | } |
| 9584 | |
| 9585 | // Build references to the field in the object we're copying from and to. |
| 9586 | CXXScopeSpec SS; // Intentionally empty |
| 9587 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 9588 | LookupMemberName); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 9589 | MemberLookup.addDecl(*Field); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9590 | MemberLookup.resolveKind(); |
| 9591 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
| 9592 | Loc, /*IsArrow=*/false, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9593 | SS, SourceLocation(), 0, |
| 9594 | MemberLookup, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9595 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
| 9596 | Loc, /*IsArrow=*/true, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9597 | SS, SourceLocation(), 0, |
| 9598 | MemberLookup, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9599 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 9600 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 9601 | |
| 9602 | assert(!From.get()->isLValue() && // could be xvalue or prvalue |
| 9603 | "Member reference with rvalue base must be rvalue except for reference " |
| 9604 | "members, which aren't allowed for move assignment."); |
| 9605 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9606 | // Build the move of this field. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9607 | StmtResult Move = buildSingleCopyAssign(*this, Loc, FieldType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9608 | To.get(), From.get(), |
| 9609 | /*CopyingBaseSubobject=*/false, |
| 9610 | /*Copying=*/false); |
| 9611 | if (Move.isInvalid()) { |
| 9612 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9613 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9614 | MoveAssignOperator->setInvalidDecl(); |
| 9615 | return; |
| 9616 | } |
Richard Smith | e7ce709 | 2012-11-12 23:33:00 +0000 | [diff] [blame] | 9617 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9618 | // Success! Record the copy. |
| 9619 | Statements.push_back(Move.takeAs<Stmt>()); |
| 9620 | } |
| 9621 | |
| 9622 | if (!Invalid) { |
| 9623 | // Add a "return *this;" |
| 9624 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
| 9625 | |
| 9626 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
| 9627 | if (Return.isInvalid()) |
| 9628 | Invalid = true; |
| 9629 | else { |
| 9630 | Statements.push_back(Return.takeAs<Stmt>()); |
| 9631 | |
| 9632 | if (Trap.hasErrorOccurred()) { |
| 9633 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9634 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9635 | Invalid = true; |
| 9636 | } |
| 9637 | } |
| 9638 | } |
| 9639 | |
| 9640 | if (Invalid) { |
| 9641 | MoveAssignOperator->setInvalidDecl(); |
| 9642 | return; |
| 9643 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9644 | |
| 9645 | StmtResult Body; |
| 9646 | { |
| 9647 | CompoundScopeRAII CompoundScope(*this); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9648 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9649 | /*isStmtExpr=*/false); |
| 9650 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 9651 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9652 | MoveAssignOperator->setBody(Body.takeAs<Stmt>()); |
| 9653 | |
| 9654 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9655 | L->CompletedImplicitDefinition(MoveAssignOperator); |
| 9656 | } |
| 9657 | } |
| 9658 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9659 | Sema::ImplicitExceptionSpecification |
| 9660 | Sema::ComputeDefaultedCopyCtorExceptionSpec(CXXMethodDecl *MD) { |
| 9661 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 9662 | |
| 9663 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 9664 | if (ClassDecl->isInvalidDecl()) |
| 9665 | return ExceptSpec; |
| 9666 | |
| 9667 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
| 9668 | assert(T->getNumArgs() >= 1 && "not a copy ctor"); |
| 9669 | unsigned Quals = T->getArgType(0).getNonReferenceType().getCVRQualifiers(); |
| 9670 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9671 | // C++ [except.spec]p14: |
| 9672 | // An implicitly declared special member function (Clause 12) shall have an |
| 9673 | // exception-specification. [...] |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9674 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9675 | BaseEnd = ClassDecl->bases_end(); |
| 9676 | Base != BaseEnd; |
| 9677 | ++Base) { |
| 9678 | // Virtual bases are handled below. |
| 9679 | if (Base->isVirtual()) |
| 9680 | continue; |
| 9681 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 9682 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9683 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 9684 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9685 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9686 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9687 | } |
| 9688 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 9689 | BaseEnd = ClassDecl->vbases_end(); |
| 9690 | Base != BaseEnd; |
| 9691 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 9692 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9693 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 9694 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9695 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9696 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9697 | } |
| 9698 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9699 | FieldEnd = ClassDecl->field_end(); |
| 9700 | Field != FieldEnd; |
| 9701 | ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 9702 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 9703 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 9704 | if (CXXConstructorDecl *CopyConstructor = |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9705 | LookupCopyingConstructor(FieldClassDecl, |
| 9706 | Quals | FieldType.getCVRQualifiers())) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9707 | ExceptSpec.CalledDecl(Field->getLocation(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9708 | } |
| 9709 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 9710 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9711 | return ExceptSpec; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9712 | } |
| 9713 | |
| 9714 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 9715 | CXXRecordDecl *ClassDecl) { |
| 9716 | // C++ [class.copy]p4: |
| 9717 | // If the class definition does not explicitly declare a copy |
| 9718 | // constructor, one is declared implicitly. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9719 | assert(ClassDecl->needsImplicitCopyConstructor()); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9720 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9721 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyConstructor); |
| 9722 | if (DSM.isAlreadyBeingDeclared()) |
| 9723 | return 0; |
| 9724 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9725 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 9726 | QualType ArgType = ClassType; |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 9727 | bool Const = ClassDecl->implicitCopyConstructorHasConstParam(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9728 | if (Const) |
| 9729 | ArgType = ArgType.withConst(); |
| 9730 | ArgType = Context.getLValueReferenceType(ArgType); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9731 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 9732 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 9733 | CXXCopyConstructor, |
| 9734 | Const); |
| 9735 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9736 | DeclarationName Name |
| 9737 | = Context.DeclarationNames.getCXXConstructorName( |
| 9738 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9739 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 9740 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9741 | |
| 9742 | // An implicitly-declared copy constructor is an inline public |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9743 | // member of its class. |
| 9744 | CXXConstructorDecl *CopyConstructor = CXXConstructorDecl::Create( |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9745 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/0, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9746 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 9747 | Constexpr); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9748 | CopyConstructor->setAccess(AS_public); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9749 | CopyConstructor->setDefaulted(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9750 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9751 | // Build an exception specification pointing back at this member. |
| 9752 | FunctionProtoType::ExtProtoInfo EPI; |
| 9753 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 9754 | EPI.ExceptionSpecDecl = CopyConstructor; |
| 9755 | CopyConstructor->setType( |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9756 | Context.getFunctionType(Context.VoidTy, ArgType, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9757 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9758 | // Add the parameter to the constructor. |
| 9759 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9760 | ClassLoc, ClassLoc, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9761 | /*IdentifierInfo=*/0, |
| 9762 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9763 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9764 | CopyConstructor->setParams(FromParam); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9765 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9766 | CopyConstructor->setTrivial( |
| 9767 | ClassDecl->needsOverloadResolutionForCopyConstructor() |
| 9768 | ? SpecialMemberIsTrivial(CopyConstructor, CXXCopyConstructor) |
| 9769 | : ClassDecl->hasTrivialCopyConstructor()); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 9770 | |
Nico Weber | afcc96a | 2012-01-23 03:19:29 +0000 | [diff] [blame] | 9771 | // C++11 [class.copy]p8: |
| 9772 | // ... If the class definition does not explicitly declare a copy |
| 9773 | // constructor, there is no user-declared move constructor, and there is no |
| 9774 | // user-declared move assignment operator, a copy constructor is implicitly |
| 9775 | // declared as defaulted. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9776 | if (ShouldDeleteSpecialMember(CopyConstructor, CXXCopyConstructor)) |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 9777 | SetDeclDeleted(CopyConstructor, ClassLoc); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9778 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9779 | // Note that we have declared this constructor. |
| 9780 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 9781 | |
| 9782 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 9783 | PushOnScopeChains(CopyConstructor, S, false); |
| 9784 | ClassDecl->addDecl(CopyConstructor); |
| 9785 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9786 | return CopyConstructor; |
| 9787 | } |
| 9788 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9789 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9790 | CXXConstructorDecl *CopyConstructor) { |
| 9791 | assert((CopyConstructor->isDefaulted() && |
| 9792 | CopyConstructor->isCopyConstructor() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9793 | !CopyConstructor->doesThisDeclarationHaveABody() && |
| 9794 | !CopyConstructor->isDeleted()) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9795 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9796 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 9797 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9798 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9799 | |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9800 | // C++11 [class.copy]p7: |
| 9801 | // The [definition of an implicitly declared copy constructro] is |
| 9802 | // deprecated if the class has a user-declared copy assignment operator |
| 9803 | // or a user-declared destructor. |
| 9804 | if (getLangOpts().CPlusPlus11 && CopyConstructor->isImplicit()) |
| 9805 | diagnoseDeprecatedCopyOperation(*this, CopyConstructor, CurrentLocation); |
| 9806 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9807 | SynthesizedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 9808 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9809 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 9810 | if (SetCtorInitializers(CopyConstructor, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 9811 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 9812 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9813 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 9814 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9815 | } else { |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9816 | Sema::CompoundScopeRAII CompoundScope(*this); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9817 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 9818 | CopyConstructor->getLocation(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 9819 | MultiStmtArg(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9820 | /*isStmtExpr=*/false) |
| 9821 | .takeAs<Stmt>()); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame] | 9822 | CopyConstructor->setImplicitlyDefined(true); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 9823 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9824 | |
| 9825 | CopyConstructor->setUsed(); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 9826 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9827 | L->CompletedImplicitDefinition(CopyConstructor); |
| 9828 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9829 | } |
| 9830 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9831 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9832 | Sema::ComputeDefaultedMoveCtorExceptionSpec(CXXMethodDecl *MD) { |
| 9833 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 9834 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9835 | // C++ [except.spec]p14: |
| 9836 | // An implicitly declared special member function (Clause 12) shall have an |
| 9837 | // exception-specification. [...] |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9838 | ImplicitExceptionSpecification ExceptSpec(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9839 | if (ClassDecl->isInvalidDecl()) |
| 9840 | return ExceptSpec; |
| 9841 | |
| 9842 | // Direct base-class constructors. |
| 9843 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 9844 | BEnd = ClassDecl->bases_end(); |
| 9845 | B != BEnd; ++B) { |
| 9846 | if (B->isVirtual()) // Handled below. |
| 9847 | continue; |
| 9848 | |
| 9849 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 9850 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9851 | CXXConstructorDecl *Constructor = |
| 9852 | LookupMovingConstructor(BaseClassDecl, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9853 | // If this is a deleted function, add it anyway. This might be conformant |
| 9854 | // with the standard. This might not. I'm not sure. It might not matter. |
| 9855 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9856 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9857 | } |
| 9858 | } |
| 9859 | |
| 9860 | // Virtual base-class constructors. |
| 9861 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 9862 | BEnd = ClassDecl->vbases_end(); |
| 9863 | B != BEnd; ++B) { |
| 9864 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 9865 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9866 | CXXConstructorDecl *Constructor = |
| 9867 | LookupMovingConstructor(BaseClassDecl, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9868 | // If this is a deleted function, add it anyway. This might be conformant |
| 9869 | // with the standard. This might not. I'm not sure. It might not matter. |
| 9870 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9871 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9872 | } |
| 9873 | } |
| 9874 | |
| 9875 | // Field constructors. |
| 9876 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 9877 | FEnd = ClassDecl->field_end(); |
| 9878 | F != FEnd; ++F) { |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9879 | QualType FieldType = Context.getBaseElementType(F->getType()); |
| 9880 | if (CXXRecordDecl *FieldRecDecl = FieldType->getAsCXXRecordDecl()) { |
| 9881 | CXXConstructorDecl *Constructor = |
| 9882 | LookupMovingConstructor(FieldRecDecl, FieldType.getCVRQualifiers()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9883 | // If this is a deleted function, add it anyway. This might be conformant |
| 9884 | // with the standard. This might not. I'm not sure. It might not matter. |
| 9885 | // In particular, the problem is that this function never gets called. It |
| 9886 | // might just be ill-formed because this function attempts to refer to |
| 9887 | // a deleted function here. |
| 9888 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9889 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9890 | } |
| 9891 | } |
| 9892 | |
| 9893 | return ExceptSpec; |
| 9894 | } |
| 9895 | |
| 9896 | CXXConstructorDecl *Sema::DeclareImplicitMoveConstructor( |
| 9897 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9898 | // C++11 [class.copy]p9: |
| 9899 | // If the definition of a class X does not explicitly declare a move |
| 9900 | // constructor, one will be implicitly declared as defaulted if and only if: |
| 9901 | // |
| 9902 | // - [first 4 bullets] |
| 9903 | assert(ClassDecl->needsImplicitMoveConstructor()); |
| 9904 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9905 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveConstructor); |
| 9906 | if (DSM.isAlreadyBeingDeclared()) |
| 9907 | return 0; |
| 9908 | |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9909 | // [Checked after we build the declaration] |
| 9910 | // - the move assignment operator would not be implicitly defined as |
| 9911 | // deleted, |
| 9912 | |
| 9913 | // [DR1402]: |
| 9914 | // - each of X's non-static data members and direct or virtual base classes |
| 9915 | // has a type that either has a move constructor or is trivially copyable. |
| 9916 | if (!subobjectsHaveMoveOrTrivialCopy(*this, ClassDecl, /*Constructor*/true)) { |
| 9917 | ClassDecl->setFailedImplicitMoveConstructor(); |
| 9918 | return 0; |
| 9919 | } |
| 9920 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9921 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 9922 | QualType ArgType = Context.getRValueReferenceType(ClassType); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9923 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 9924 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 9925 | CXXMoveConstructor, |
| 9926 | false); |
| 9927 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9928 | DeclarationName Name |
| 9929 | = Context.DeclarationNames.getCXXConstructorName( |
| 9930 | Context.getCanonicalType(ClassType)); |
| 9931 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 9932 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 9933 | |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9934 | // C++11 [class.copy]p11: |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9935 | // An implicitly-declared copy/move constructor is an inline public |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9936 | // member of its class. |
| 9937 | CXXConstructorDecl *MoveConstructor = CXXConstructorDecl::Create( |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9938 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/0, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9939 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 9940 | Constexpr); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9941 | MoveConstructor->setAccess(AS_public); |
| 9942 | MoveConstructor->setDefaulted(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9943 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9944 | // Build an exception specification pointing back at this member. |
| 9945 | FunctionProtoType::ExtProtoInfo EPI; |
| 9946 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 9947 | EPI.ExceptionSpecDecl = MoveConstructor; |
| 9948 | MoveConstructor->setType( |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9949 | Context.getFunctionType(Context.VoidTy, ArgType, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9950 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9951 | // Add the parameter to the constructor. |
| 9952 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveConstructor, |
| 9953 | ClassLoc, ClassLoc, |
| 9954 | /*IdentifierInfo=*/0, |
| 9955 | ArgType, /*TInfo=*/0, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9956 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9957 | MoveConstructor->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9958 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9959 | MoveConstructor->setTrivial( |
| 9960 | ClassDecl->needsOverloadResolutionForMoveConstructor() |
| 9961 | ? SpecialMemberIsTrivial(MoveConstructor, CXXMoveConstructor) |
| 9962 | : ClassDecl->hasTrivialMoveConstructor()); |
| 9963 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9964 | // C++0x [class.copy]p9: |
| 9965 | // If the definition of a class X does not explicitly declare a move |
| 9966 | // constructor, one will be implicitly declared as defaulted if and only if: |
| 9967 | // [...] |
| 9968 | // - the move constructor would not be implicitly defined as deleted. |
Sean Hunt | 769bb2d | 2011-10-11 06:43:29 +0000 | [diff] [blame] | 9969 | if (ShouldDeleteSpecialMember(MoveConstructor, CXXMoveConstructor)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9970 | // Cache this result so that we don't try to generate this over and over |
| 9971 | // on every lookup, leaking memory and wasting time. |
| 9972 | ClassDecl->setFailedImplicitMoveConstructor(); |
| 9973 | return 0; |
| 9974 | } |
| 9975 | |
| 9976 | // Note that we have declared this constructor. |
| 9977 | ++ASTContext::NumImplicitMoveConstructorsDeclared; |
| 9978 | |
| 9979 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 9980 | PushOnScopeChains(MoveConstructor, S, false); |
| 9981 | ClassDecl->addDecl(MoveConstructor); |
| 9982 | |
| 9983 | return MoveConstructor; |
| 9984 | } |
| 9985 | |
| 9986 | void Sema::DefineImplicitMoveConstructor(SourceLocation CurrentLocation, |
| 9987 | CXXConstructorDecl *MoveConstructor) { |
| 9988 | assert((MoveConstructor->isDefaulted() && |
| 9989 | MoveConstructor->isMoveConstructor() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9990 | !MoveConstructor->doesThisDeclarationHaveABody() && |
| 9991 | !MoveConstructor->isDeleted()) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9992 | "DefineImplicitMoveConstructor - call it for implicit move ctor"); |
| 9993 | |
| 9994 | CXXRecordDecl *ClassDecl = MoveConstructor->getParent(); |
| 9995 | assert(ClassDecl && "DefineImplicitMoveConstructor - invalid constructor"); |
| 9996 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9997 | SynthesizedFunctionScope Scope(*this, MoveConstructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9998 | DiagnosticErrorTrap Trap(Diags); |
| 9999 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 10000 | if (SetCtorInitializers(MoveConstructor, /*AnyErrors=*/false) || |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10001 | Trap.hasErrorOccurred()) { |
| 10002 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 10003 | << CXXMoveConstructor << Context.getTagDeclType(ClassDecl); |
| 10004 | MoveConstructor->setInvalidDecl(); |
| 10005 | } else { |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 10006 | Sema::CompoundScopeRAII CompoundScope(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10007 | MoveConstructor->setBody(ActOnCompoundStmt(MoveConstructor->getLocation(), |
| 10008 | MoveConstructor->getLocation(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10009 | MultiStmtArg(), |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10010 | /*isStmtExpr=*/false) |
| 10011 | .takeAs<Stmt>()); |
Douglas Gregor | 690b2db | 2011-09-22 20:32:43 +0000 | [diff] [blame] | 10012 | MoveConstructor->setImplicitlyDefined(true); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10013 | } |
| 10014 | |
| 10015 | MoveConstructor->setUsed(); |
| 10016 | |
| 10017 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10018 | L->CompletedImplicitDefinition(MoveConstructor); |
| 10019 | } |
| 10020 | } |
| 10021 | |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10022 | bool Sema::isImplicitlyDeleted(FunctionDecl *FD) { |
Eli Friedman | c4ef948 | 2013-07-18 23:29:14 +0000 | [diff] [blame] | 10023 | return FD->isDeleted() && FD->isDefaulted() && isa<CXXMethodDecl>(FD); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10024 | } |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10025 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10026 | /// \brief Mark the call operator of the given lambda closure type as "used". |
| 10027 | static void markLambdaCallOperatorUsed(Sema &S, CXXRecordDecl *Lambda) { |
| 10028 | CXXMethodDecl *CallOperator |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10029 | = cast<CXXMethodDecl>( |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 10030 | Lambda->lookup( |
| 10031 | S.Context.DeclarationNames.getCXXOperatorName(OO_Call)).front()); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10032 | CallOperator->setReferenced(); |
| 10033 | CallOperator->setUsed(); |
| 10034 | } |
| 10035 | |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10036 | void Sema::DefineImplicitLambdaToFunctionPointerConversion( |
| 10037 | SourceLocation CurrentLocation, |
| 10038 | CXXConversionDecl *Conv) |
| 10039 | { |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10040 | CXXRecordDecl *Lambda = Conv->getParent(); |
| 10041 | |
| 10042 | // Make sure that the lambda call operator is marked used. |
| 10043 | markLambdaCallOperatorUsed(*this, Lambda); |
| 10044 | |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10045 | Conv->setUsed(); |
| 10046 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10047 | SynthesizedFunctionScope Scope(*this, Conv); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10048 | DiagnosticErrorTrap Trap(Diags); |
| 10049 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10050 | // Return the address of the __invoke function. |
| 10051 | DeclarationName InvokeName = &Context.Idents.get("__invoke"); |
| 10052 | CXXMethodDecl *Invoke |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 10053 | = cast<CXXMethodDecl>(Lambda->lookup(InvokeName).front()); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10054 | Expr *FunctionRef = BuildDeclRefExpr(Invoke, Invoke->getType(), |
| 10055 | VK_LValue, Conv->getLocation()).take(); |
| 10056 | assert(FunctionRef && "Can't refer to __invoke function?"); |
| 10057 | Stmt *Return = ActOnReturnStmt(Conv->getLocation(), FunctionRef).take(); |
Nico Weber | d36aa35 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 10058 | Conv->setBody(new (Context) CompoundStmt(Context, Return, |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10059 | Conv->getLocation(), |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10060 | Conv->getLocation())); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10061 | |
| 10062 | // Fill in the __invoke function with a dummy implementation. IR generation |
| 10063 | // will fill in the actual details. |
| 10064 | Invoke->setUsed(); |
| 10065 | Invoke->setReferenced(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 10066 | Invoke->setBody(new (Context) CompoundStmt(Conv->getLocation())); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10067 | |
| 10068 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10069 | L->CompletedImplicitDefinition(Conv); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10070 | L->CompletedImplicitDefinition(Invoke); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10071 | } |
| 10072 | } |
| 10073 | |
| 10074 | void Sema::DefineImplicitLambdaToBlockPointerConversion( |
| 10075 | SourceLocation CurrentLocation, |
| 10076 | CXXConversionDecl *Conv) |
| 10077 | { |
| 10078 | Conv->setUsed(); |
| 10079 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10080 | SynthesizedFunctionScope Scope(*this, Conv); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10081 | DiagnosticErrorTrap Trap(Diags); |
| 10082 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10083 | // Copy-initialize the lambda object as needed to capture it. |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10084 | Expr *This = ActOnCXXThis(CurrentLocation).take(); |
| 10085 | Expr *DerefThis =CreateBuiltinUnaryOp(CurrentLocation, UO_Deref, This).take(); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10086 | |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 10087 | ExprResult BuildBlock = BuildBlockForLambdaConversion(CurrentLocation, |
| 10088 | Conv->getLocation(), |
| 10089 | Conv, DerefThis); |
| 10090 | |
| 10091 | // If we're not under ARC, make sure we still get the _Block_copy/autorelease |
| 10092 | // behavior. Note that only the general conversion function does this |
| 10093 | // (since it's unusable otherwise); in the case where we inline the |
| 10094 | // block literal, it has block literal lifetime semantics. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10095 | if (!BuildBlock.isInvalid() && !getLangOpts().ObjCAutoRefCount) |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 10096 | BuildBlock = ImplicitCastExpr::Create(Context, BuildBlock.get()->getType(), |
| 10097 | CK_CopyAndAutoreleaseBlockObject, |
| 10098 | BuildBlock.get(), 0, VK_RValue); |
| 10099 | |
| 10100 | if (BuildBlock.isInvalid()) { |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10101 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10102 | Conv->setInvalidDecl(); |
| 10103 | return; |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10104 | } |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10105 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10106 | // Create the return statement that returns the block from the conversion |
| 10107 | // function. |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 10108 | StmtResult Return = ActOnReturnStmt(Conv->getLocation(), BuildBlock.get()); |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10109 | if (Return.isInvalid()) { |
| 10110 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
| 10111 | Conv->setInvalidDecl(); |
| 10112 | return; |
| 10113 | } |
| 10114 | |
| 10115 | // Set the body of the conversion function. |
| 10116 | Stmt *ReturnS = Return.take(); |
Nico Weber | d36aa35 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 10117 | Conv->setBody(new (Context) CompoundStmt(Context, ReturnS, |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10118 | Conv->getLocation(), |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10119 | Conv->getLocation())); |
| 10120 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10121 | // We're done; notify the mutation listener, if any. |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10122 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10123 | L->CompletedImplicitDefinition(Conv); |
| 10124 | } |
| 10125 | } |
| 10126 | |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10127 | /// \brief Determine whether the given list arguments contains exactly one |
| 10128 | /// "real" (non-default) argument. |
| 10129 | static bool hasOneRealArgument(MultiExprArg Args) { |
| 10130 | switch (Args.size()) { |
| 10131 | case 0: |
| 10132 | return false; |
| 10133 | |
| 10134 | default: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10135 | if (!Args[1]->isDefaultArgument()) |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10136 | return false; |
| 10137 | |
| 10138 | // fall through |
| 10139 | case 1: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10140 | return !Args[0]->isDefaultArgument(); |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10141 | } |
| 10142 | |
| 10143 | return false; |
| 10144 | } |
| 10145 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10146 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 10147 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10148 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 10149 | MultiExprArg ExprArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10150 | bool HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10151 | bool IsListInitialization, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10152 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 10153 | unsigned ConstructKind, |
| 10154 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 10155 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10156 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 10157 | // C++0x [class.copy]p34: |
| 10158 | // When certain criteria are met, an implementation is allowed to |
| 10159 | // omit the copy/move construction of a class object, even if the |
| 10160 | // copy/move constructor and/or destructor for the object have |
| 10161 | // side effects. [...] |
| 10162 | // - when a temporary class object that has not been bound to a |
| 10163 | // reference (12.2) would be copied/moved to a class object |
| 10164 | // with the same cv-unqualified type, the copy/move operation |
| 10165 | // can be omitted by constructing the temporary object |
| 10166 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 10167 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10168 | Constructor->isCopyOrMoveConstructor() && hasOneRealArgument(ExprArgs)) { |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10169 | Expr *SubExpr = ExprArgs[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 10170 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 10171 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10172 | |
| 10173 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10174 | Elidable, ExprArgs, HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10175 | IsListInitialization, RequiresZeroInit, |
| 10176 | ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 10177 | } |
| 10178 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 10179 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 10180 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10181 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 10182 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 10183 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 10184 | MultiExprArg ExprArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10185 | bool HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10186 | bool IsListInitialization, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10187 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 10188 | unsigned ConstructKind, |
| 10189 | SourceRange ParenRange) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10190 | MarkFunctionReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 10191 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10192 | Constructor, Elidable, ExprArgs, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10193 | HadMultipleCandidates, |
| 10194 | IsListInitialization, RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 10195 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 10196 | ParenRange)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 10197 | } |
| 10198 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 10199 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10200 | if (VD->isInvalidDecl()) return; |
| 10201 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 10202 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10203 | if (ClassDecl->isInvalidDecl()) return; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 10204 | if (ClassDecl->hasIrrelevantDestructor()) return; |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10205 | if (ClassDecl->isDependentContext()) return; |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 10206 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10207 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10208 | MarkFunctionReferenced(VD->getLocation(), Destructor); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10209 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 10210 | PDiag(diag::err_access_dtor_var) |
| 10211 | << VD->getDeclName() |
| 10212 | << VD->getType()); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 10213 | DiagnoseUseOfDecl(Destructor, VD->getLocation()); |
Anders Carlsson | 2b32dad | 2011-03-24 01:01:41 +0000 | [diff] [blame] | 10214 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10215 | if (!VD->hasGlobalStorage()) return; |
| 10216 | |
| 10217 | // Emit warning for non-trivial dtor in global scope (a real global, |
| 10218 | // class-static, function-static). |
| 10219 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 10220 | |
| 10221 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 10222 | if (!VD->isStaticLocal()) |
| 10223 | Diag(VD->getLocation(), diag::warn_global_destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10224 | } |
| 10225 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10226 | /// \brief Given a constructor and the set of arguments provided for the |
| 10227 | /// constructor, convert the arguments and add any required default arguments |
| 10228 | /// to form a proper call to this constructor. |
| 10229 | /// |
| 10230 | /// \returns true if an error occurred, false otherwise. |
| 10231 | bool |
| 10232 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 10233 | MultiExprArg ArgsPtr, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 10234 | SourceLocation Loc, |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 10235 | SmallVectorImpl<Expr*> &ConvertedArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 10236 | bool AllowExplicit, |
| 10237 | bool IsListInitialization) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10238 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 10239 | unsigned NumArgs = ArgsPtr.size(); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10240 | Expr **Args = ArgsPtr.data(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10241 | |
| 10242 | const FunctionProtoType *Proto |
| 10243 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 10244 | assert(Proto && "Constructor without a prototype?"); |
| 10245 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10246 | |
| 10247 | // 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] | 10248 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10249 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10250 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10251 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10252 | |
| 10253 | VariadicCallType CallType = |
| 10254 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10255 | SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10256 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10257 | Proto, 0, |
| 10258 | llvm::makeArrayRef(Args, NumArgs), |
| 10259 | AllArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 10260 | CallType, AllowExplicit, |
| 10261 | IsListInitialization); |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 10262 | ConvertedArgs.append(AllArgs.begin(), AllArgs.end()); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10263 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10264 | DiagnoseSentinelCalls(Constructor, Loc, AllArgs); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10265 | |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 10266 | CheckConstructorCall(Constructor, |
| 10267 | llvm::makeArrayRef<const Expr *>(AllArgs.data(), |
| 10268 | AllArgs.size()), |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 10269 | Proto, Loc); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10270 | |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10271 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 10272 | } |
| 10273 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10274 | static inline bool |
| 10275 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 10276 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10277 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10278 | if (isa<NamespaceDecl>(DC)) { |
| 10279 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10280 | diag::err_operator_new_delete_declared_in_namespace) |
| 10281 | << FnDecl->getDeclName(); |
| 10282 | } |
| 10283 | |
| 10284 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 10285 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10286 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10287 | diag::err_operator_new_delete_declared_static) |
| 10288 | << FnDecl->getDeclName(); |
| 10289 | } |
| 10290 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 10291 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10292 | } |
| 10293 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10294 | static inline bool |
| 10295 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 10296 | CanQualType ExpectedResultType, |
| 10297 | CanQualType ExpectedFirstParamType, |
| 10298 | unsigned DependentParamTypeDiag, |
| 10299 | unsigned InvalidParamTypeDiag) { |
| 10300 | QualType ResultType = |
| 10301 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 10302 | |
| 10303 | // Check that the result type is not dependent. |
| 10304 | if (ResultType->isDependentType()) |
| 10305 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10306 | diag::err_operator_new_delete_dependent_result_type) |
| 10307 | << FnDecl->getDeclName() << ExpectedResultType; |
| 10308 | |
| 10309 | // Check that the result type is what we expect. |
| 10310 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 10311 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10312 | diag::err_operator_new_delete_invalid_result_type) |
| 10313 | << FnDecl->getDeclName() << ExpectedResultType; |
| 10314 | |
| 10315 | // A function template must have at least 2 parameters. |
| 10316 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 10317 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10318 | diag::err_operator_new_delete_template_too_few_parameters) |
| 10319 | << FnDecl->getDeclName(); |
| 10320 | |
| 10321 | // The function decl must have at least 1 parameter. |
| 10322 | if (FnDecl->getNumParams() == 0) |
| 10323 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10324 | diag::err_operator_new_delete_too_few_parameters) |
| 10325 | << FnDecl->getDeclName(); |
| 10326 | |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 10327 | // Check the first parameter type is not dependent. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10328 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 10329 | if (FirstParamType->isDependentType()) |
| 10330 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 10331 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 10332 | |
| 10333 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 10334 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10335 | ExpectedFirstParamType) |
| 10336 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 10337 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 10338 | |
| 10339 | return false; |
| 10340 | } |
| 10341 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10342 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10343 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10344 | // C++ [basic.stc.dynamic.allocation]p1: |
| 10345 | // A program is ill-formed if an allocation function is declared in a |
| 10346 | // namespace scope other than global scope or declared static in global |
| 10347 | // scope. |
| 10348 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 10349 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10350 | |
| 10351 | CanQualType SizeTy = |
| 10352 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 10353 | |
| 10354 | // C++ [basic.stc.dynamic.allocation]p1: |
| 10355 | // The return type shall be void*. The first parameter shall have type |
| 10356 | // std::size_t. |
| 10357 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 10358 | SizeTy, |
| 10359 | diag::err_operator_new_dependent_param_type, |
| 10360 | diag::err_operator_new_param_type)) |
| 10361 | return true; |
| 10362 | |
| 10363 | // C++ [basic.stc.dynamic.allocation]p1: |
| 10364 | // The first parameter shall not have an associated default argument. |
| 10365 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 10366 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10367 | diag::err_operator_new_default_arg) |
| 10368 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 10369 | |
| 10370 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 10371 | } |
| 10372 | |
| 10373 | static bool |
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 10374 | CheckOperatorDeleteDeclaration(Sema &SemaRef, FunctionDecl *FnDecl) { |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10375 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 10376 | // A program is ill-formed if deallocation functions are declared in a |
| 10377 | // namespace scope other than global scope or declared static in global |
| 10378 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10379 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 10380 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10381 | |
| 10382 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 10383 | // Each deallocation function shall return void and its first parameter |
| 10384 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10385 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 10386 | SemaRef.Context.VoidPtrTy, |
| 10387 | diag::err_operator_delete_dependent_param_type, |
| 10388 | diag::err_operator_delete_param_type)) |
| 10389 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10390 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10391 | return false; |
| 10392 | } |
| 10393 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10394 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 10395 | /// of this overloaded operator is well-formed. If so, returns false; |
| 10396 | /// otherwise, emits appropriate diagnostics and returns true. |
| 10397 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10398 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10399 | "Expected an overloaded operator declaration"); |
| 10400 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10401 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 10402 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10403 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10404 | // The allocation and deallocation functions, operator new, |
| 10405 | // operator new[], operator delete and operator delete[], are |
| 10406 | // described completely in 3.7.3. The attributes and restrictions |
| 10407 | // found in the rest of this subclause do not apply to them unless |
| 10408 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 10409 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10410 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 10411 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 10412 | if (Op == OO_New || Op == OO_Array_New) |
| 10413 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10414 | |
| 10415 | // C++ [over.oper]p6: |
| 10416 | // An operator function shall either be a non-static member |
| 10417 | // function or be a non-member function and have at least one |
| 10418 | // parameter whose type is a class, a reference to a class, an |
| 10419 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10420 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 10421 | if (MethodDecl->isStatic()) |
| 10422 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10423 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10424 | } else { |
| 10425 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10426 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 10427 | ParamEnd = FnDecl->param_end(); |
| 10428 | Param != ParamEnd; ++Param) { |
| 10429 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 10430 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 10431 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10432 | ClassOrEnumParam = true; |
| 10433 | break; |
| 10434 | } |
| 10435 | } |
| 10436 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10437 | if (!ClassOrEnumParam) |
| 10438 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 10439 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10440 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10441 | } |
| 10442 | |
| 10443 | // C++ [over.oper]p8: |
| 10444 | // An operator function cannot have default arguments (8.3.6), |
| 10445 | // except where explicitly stated below. |
| 10446 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10447 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10448 | // (C++ [over.call]p1). |
| 10449 | if (Op != OO_Call) { |
| 10450 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 10451 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10452 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10453 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 10454 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10455 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10456 | } |
| 10457 | } |
| 10458 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10459 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 10460 | { false, false, false } |
| 10461 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 10462 | , { Unary, Binary, MemberOnly } |
| 10463 | #include "clang/Basic/OperatorKinds.def" |
| 10464 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10465 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10466 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 10467 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 10468 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10469 | |
| 10470 | // C++ [over.oper]p8: |
| 10471 | // [...] Operator functions cannot have more or fewer parameters |
| 10472 | // than the number required for the corresponding operator, as |
| 10473 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10474 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10475 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10476 | if (Op != OO_Call && |
| 10477 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 10478 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 10479 | (NumParams < 1) || (NumParams > 2))) { |
| 10480 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10481 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10482 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10483 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10484 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10485 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10486 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 10487 | assert(CanBeBinaryOperator && |
| 10488 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10489 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10490 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10491 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10492 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10493 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10494 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 10495 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10496 | // Overloaded operators other than operator() cannot be variadic. |
| 10497 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 10498 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 10499 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10500 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10501 | } |
| 10502 | |
| 10503 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10504 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 10505 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 10506 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10507 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10508 | } |
| 10509 | |
| 10510 | // C++ [over.inc]p1: |
| 10511 | // The user-defined function called operator++ implements the |
| 10512 | // prefix and postfix ++ operator. If this function is a member |
| 10513 | // function with no parameters, or a non-member function with one |
| 10514 | // parameter of class or enumeration type, it defines the prefix |
| 10515 | // increment operator ++ for objects of that type. If the function |
| 10516 | // is a member function with one parameter (which shall be of type |
| 10517 | // int) or a non-member function with two parameters (the second |
| 10518 | // of which shall be of type int), it defines the postfix |
| 10519 | // increment operator ++ for objects of that type. |
| 10520 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 10521 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 10522 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 10523 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10524 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 10525 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 10526 | if (!ParamIsInt) |
| 10527 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10528 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 10529 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10530 | } |
| 10531 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10532 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10533 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 10534 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10535 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 10536 | /// of this literal operator function is well-formed. If so, returns |
| 10537 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 10538 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
Richard Smith | e5658f0 | 2012-03-10 22:18:57 +0000 | [diff] [blame] | 10539 | if (isa<CXXMethodDecl>(FnDecl)) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10540 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 10541 | << FnDecl->getDeclName(); |
| 10542 | return true; |
| 10543 | } |
| 10544 | |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10545 | if (FnDecl->isExternC()) { |
| 10546 | Diag(FnDecl->getLocation(), diag::err_literal_operator_extern_c); |
| 10547 | return true; |
| 10548 | } |
| 10549 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10550 | bool Valid = false; |
| 10551 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 10552 | // This might be the definition of a literal operator template. |
| 10553 | FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate(); |
| 10554 | // This might be a specialization of a literal operator template. |
| 10555 | if (!TpDecl) |
| 10556 | TpDecl = FnDecl->getPrimaryTemplate(); |
| 10557 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10558 | // template <char...> type operator "" name() is the only valid template |
| 10559 | // signature, and the only valid signature with no parameters. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 10560 | if (TpDecl) { |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10561 | if (FnDecl->param_size() == 0) { |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10562 | // Must have only one template parameter |
| 10563 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 10564 | if (Params->size() == 1) { |
| 10565 | NonTypeTemplateParmDecl *PmDecl = |
Richard Smith | 5295b97 | 2012-08-03 21:14:57 +0000 | [diff] [blame] | 10566 | dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10567 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10568 | // The template parameter must be a char parameter pack. |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10569 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 10570 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 10571 | Valid = true; |
| 10572 | } |
| 10573 | } |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10574 | } else if (FnDecl->param_size()) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10575 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10576 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 10577 | |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10578 | QualType T = (*Param)->getType().getUnqualifiedType(); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10579 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 10580 | // unsigned long long int, long double, and any character type are allowed |
| 10581 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10582 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 10583 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 10584 | Context.hasSameType(T, Context.CharTy) || |
Hans Wennborg | 15f92ba | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 10585 | Context.hasSameType(T, Context.WideCharTy) || |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10586 | Context.hasSameType(T, Context.Char16Ty) || |
| 10587 | Context.hasSameType(T, Context.Char32Ty)) { |
| 10588 | if (++Param == FnDecl->param_end()) |
| 10589 | Valid = true; |
| 10590 | goto FinishedParams; |
| 10591 | } |
| 10592 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 10593 | // 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] | 10594 | const PointerType *PT = T->getAs<PointerType>(); |
| 10595 | if (!PT) |
| 10596 | goto FinishedParams; |
| 10597 | T = PT->getPointeeType(); |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10598 | if (!T.isConstQualified() || T.isVolatileQualified()) |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10599 | goto FinishedParams; |
| 10600 | T = T.getUnqualifiedType(); |
| 10601 | |
| 10602 | // Move on to the second parameter; |
| 10603 | ++Param; |
| 10604 | |
| 10605 | // If there is no second parameter, the first must be a const char * |
| 10606 | if (Param == FnDecl->param_end()) { |
| 10607 | if (Context.hasSameType(T, Context.CharTy)) |
| 10608 | Valid = true; |
| 10609 | goto FinishedParams; |
| 10610 | } |
| 10611 | |
| 10612 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 10613 | // are allowed as the first parameter to a two-parameter function |
| 10614 | if (!(Context.hasSameType(T, Context.CharTy) || |
Hans Wennborg | 15f92ba | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 10615 | Context.hasSameType(T, Context.WideCharTy) || |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10616 | Context.hasSameType(T, Context.Char16Ty) || |
| 10617 | Context.hasSameType(T, Context.Char32Ty))) |
| 10618 | goto FinishedParams; |
| 10619 | |
| 10620 | // The second and final parameter must be an std::size_t |
| 10621 | T = (*Param)->getType().getUnqualifiedType(); |
| 10622 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 10623 | ++Param == FnDecl->param_end()) |
| 10624 | Valid = true; |
| 10625 | } |
| 10626 | |
| 10627 | // FIXME: This diagnostic is absolutely terrible. |
| 10628 | FinishedParams: |
| 10629 | if (!Valid) { |
| 10630 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 10631 | << FnDecl->getDeclName(); |
| 10632 | return true; |
| 10633 | } |
| 10634 | |
Richard Smith | a9e88b2 | 2012-03-09 08:16:22 +0000 | [diff] [blame] | 10635 | // A parameter-declaration-clause containing a default argument is not |
| 10636 | // equivalent to any of the permitted forms. |
| 10637 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 10638 | ParamEnd = FnDecl->param_end(); |
| 10639 | Param != ParamEnd; ++Param) { |
| 10640 | if ((*Param)->hasDefaultArg()) { |
| 10641 | Diag((*Param)->getDefaultArgRange().getBegin(), |
| 10642 | diag::err_literal_operator_default_argument) |
| 10643 | << (*Param)->getDefaultArgRange(); |
| 10644 | break; |
| 10645 | } |
| 10646 | } |
| 10647 | |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 10648 | StringRef LiteralName |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 10649 | = FnDecl->getDeclName().getCXXLiteralIdentifier()->getName(); |
| 10650 | if (LiteralName[0] != '_') { |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 10651 | // C++11 [usrlit.suffix]p1: |
| 10652 | // Literal suffix identifiers that do not start with an underscore |
| 10653 | // are reserved for future standardization. |
Richard Smith | 4ac537b | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 10654 | Diag(FnDecl->getLocation(), diag::warn_user_literal_reserved) |
| 10655 | << NumericLiteralParser::isValidUDSuffix(getLangOpts(), LiteralName); |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 10656 | } |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 10657 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10658 | return false; |
| 10659 | } |
| 10660 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10661 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 10662 | /// linkage specification, including the language and (if present) |
| 10663 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 10664 | /// the location of the language string literal, which is provided |
| 10665 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 10666 | /// the '{' brace. Otherwise, this linkage specification does not |
| 10667 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 10668 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 10669 | SourceLocation LangLoc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10670 | StringRef Lang, |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 10671 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10672 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 10673 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10674 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 10675 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10676 | Language = LinkageSpecDecl::lang_cxx; |
| 10677 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10678 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10679 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10680 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10681 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10682 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10683 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10684 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Rafael Espindola | e5e575d | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 10685 | ExternLoc, LangLoc, Language, |
| 10686 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 10687 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10688 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10689 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10690 | } |
| 10691 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 10692 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10693 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 10694 | /// valid, it's the position of the closing '}' brace in a linkage |
| 10695 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10696 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 10697 | Decl *LinkageSpec, |
| 10698 | SourceLocation RBraceLoc) { |
| 10699 | if (LinkageSpec) { |
| 10700 | if (RBraceLoc.isValid()) { |
| 10701 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 10702 | LSDecl->setRBraceLoc(RBraceLoc); |
| 10703 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10704 | PopDeclContext(); |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 10705 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10706 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 10707 | } |
| 10708 | |
Michael Han | 684aa73 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 10709 | Decl *Sema::ActOnEmptyDeclaration(Scope *S, |
| 10710 | AttributeList *AttrList, |
| 10711 | SourceLocation SemiLoc) { |
| 10712 | Decl *ED = EmptyDecl::Create(Context, CurContext, SemiLoc); |
| 10713 | // Attribute declarations appertain to empty declaration so we handle |
| 10714 | // them here. |
| 10715 | if (AttrList) |
| 10716 | ProcessDeclAttributeList(S, ED, AttrList); |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 10717 | |
Michael Han | 684aa73 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 10718 | CurContext->addDecl(ED); |
| 10719 | return ED; |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 10720 | } |
| 10721 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10722 | /// \brief Perform semantic analysis for the variable declaration that |
| 10723 | /// occurs within a C++ catch clause, returning the newly-created |
| 10724 | /// variable. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10725 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 10726 | TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10727 | SourceLocation StartLoc, |
| 10728 | SourceLocation Loc, |
| 10729 | IdentifierInfo *Name) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10730 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 10731 | QualType ExDeclType = TInfo->getType(); |
| 10732 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10733 | // Arrays and functions decay. |
| 10734 | if (ExDeclType->isArrayType()) |
| 10735 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 10736 | else if (ExDeclType->isFunctionType()) |
| 10737 | ExDeclType = Context.getPointerType(ExDeclType); |
| 10738 | |
| 10739 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 10740 | // The exception-declaration shall not denote a pointer or reference to an |
| 10741 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 10742 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10743 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 10744 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 10745 | Invalid = true; |
| 10746 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10747 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10748 | QualType BaseType = ExDeclType; |
| 10749 | 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] | 10750 | unsigned DK = diag::err_catch_incomplete; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 10751 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10752 | BaseType = Ptr->getPointeeType(); |
| 10753 | Mode = 1; |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 10754 | DK = diag::err_catch_incomplete_ptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10755 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 10756 | // 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] | 10757 | BaseType = Ref->getPointeeType(); |
| 10758 | Mode = 2; |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 10759 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10760 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 10761 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 10762 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10763 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10764 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10765 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10766 | RequireNonAbstractType(Loc, ExDeclType, |
| 10767 | diag::err_abstract_type_in_decl, |
| 10768 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 10769 | Invalid = true; |
| 10770 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 10771 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 10772 | // of ObjC types, and no runtime supports catching ObjC types by value. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10773 | if (!Invalid && getLangOpts().ObjC1) { |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 10774 | QualType T = ExDeclType; |
| 10775 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 10776 | T = RT->getPointeeType(); |
| 10777 | |
| 10778 | if (T->isObjCObjectType()) { |
| 10779 | Diag(Loc, diag::err_objc_object_catch); |
| 10780 | Invalid = true; |
| 10781 | } else if (T->isObjCObjectPointerType()) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 10782 | // FIXME: should this be a test for macosx-fragile specifically? |
| 10783 | if (getLangOpts().ObjCRuntime.isFragile()) |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 10784 | Diag(Loc, diag::warn_objc_pointer_cxx_catch_fragile); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 10785 | } |
| 10786 | } |
| 10787 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10788 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 10789 | ExDeclType, TInfo, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 10790 | ExDecl->setExceptionVariable(true); |
| 10791 | |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 10792 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10793 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(ExDecl)) |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 10794 | Invalid = true; |
| 10795 | |
Douglas Gregor | c41b878 | 2011-07-06 18:14:43 +0000 | [diff] [blame] | 10796 | if (!Invalid && !ExDeclType->isDependentType()) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 10797 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
John McCall | b760f11 | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 10798 | // Insulate this from anything else we might currently be parsing. |
| 10799 | EnterExpressionEvaluationContext scope(*this, PotentiallyEvaluated); |
| 10800 | |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 10801 | // C++ [except.handle]p16: |
| 10802 | // The object declared in an exception-declaration or, if the |
| 10803 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 10804 | // copy-initialized (8.5) from the exception object. [...] |
| 10805 | // The object is destroyed when the handler exits, after the destruction |
| 10806 | // of any automatic objects initialized within the handler. |
| 10807 | // |
| 10808 | // We just pretend to initialize the object with itself, then make sure |
| 10809 | // it can be destroyed later. |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 10810 | QualType initType = ExDeclType; |
| 10811 | |
| 10812 | InitializedEntity entity = |
| 10813 | InitializedEntity::InitializeVariable(ExDecl); |
| 10814 | InitializationKind initKind = |
| 10815 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 10816 | |
| 10817 | Expr *opaqueValue = |
| 10818 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 10819 | InitializationSequence sequence(*this, entity, initKind, opaqueValue); |
| 10820 | ExprResult result = sequence.Perform(*this, entity, initKind, opaqueValue); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 10821 | if (result.isInvalid()) |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 10822 | Invalid = true; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 10823 | else { |
| 10824 | // If the constructor used was non-trivial, set this as the |
| 10825 | // "initializer". |
| 10826 | CXXConstructExpr *construct = cast<CXXConstructExpr>(result.take()); |
| 10827 | if (!construct->getConstructor()->isTrivial()) { |
| 10828 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 10829 | ExDecl->setInit(init); |
| 10830 | } |
| 10831 | |
| 10832 | // And make sure it's destructable. |
| 10833 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 10834 | } |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 10835 | } |
| 10836 | } |
| 10837 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10838 | if (Invalid) |
| 10839 | ExDecl->setInvalidDecl(); |
| 10840 | |
| 10841 | return ExDecl; |
| 10842 | } |
| 10843 | |
| 10844 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 10845 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10846 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 10847 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 10848 | bool Invalid = D.isInvalidType(); |
| 10849 | |
| 10850 | // Check for unexpanded parameter packs. |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 10851 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 10852 | UPPC_ExceptionType)) { |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 10853 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 10854 | D.getIdentifierLoc()); |
| 10855 | Invalid = true; |
| 10856 | } |
| 10857 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10858 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 10859 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 10860 | LookupOrdinaryName, |
| 10861 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10862 | // The scope should be freshly made just for us. There is just no way |
| 10863 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10864 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10865 | if (PrevDecl->isTemplateParameter()) { |
| 10866 | // Maybe we will complain about the shadowed template parameter. |
| 10867 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 10868 | PrevDecl = 0; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10869 | } |
| 10870 | } |
| 10871 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 10872 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10873 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 10874 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 10875 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10876 | } |
| 10877 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 10878 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10879 | D.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10880 | D.getIdentifierLoc(), |
| 10881 | D.getIdentifier()); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 10882 | if (Invalid) |
| 10883 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10884 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10885 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10886 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10887 | PushOnScopeChains(ExDecl, S); |
| 10888 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 10889 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10890 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 10891 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10892 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10893 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 10894 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10895 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10896 | Expr *AssertExpr, |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10897 | Expr *AssertMessageExpr, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10898 | SourceLocation RParenLoc) { |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10899 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 10900 | |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10901 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 10902 | return 0; |
| 10903 | |
| 10904 | return BuildStaticAssertDeclaration(StaticAssertLoc, AssertExpr, |
| 10905 | AssertMessage, RParenLoc, false); |
| 10906 | } |
| 10907 | |
| 10908 | Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
| 10909 | Expr *AssertExpr, |
| 10910 | StringLiteral *AssertMessage, |
| 10911 | SourceLocation RParenLoc, |
| 10912 | bool Failed) { |
| 10913 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent() && |
| 10914 | !Failed) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10915 | // In a static_assert-declaration, the constant-expression shall be a |
| 10916 | // constant expression that can be contextually converted to bool. |
| 10917 | ExprResult Converted = PerformContextuallyConvertToBool(AssertExpr); |
| 10918 | if (Converted.isInvalid()) |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10919 | Failed = true; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10920 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10921 | llvm::APSInt Cond; |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10922 | if (!Failed && VerifyIntegerConstantExpression(Converted.get(), &Cond, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10923 | diag::err_static_assert_expression_is_not_constant, |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10924 | /*AllowFold=*/false).isInvalid()) |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10925 | Failed = true; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 10926 | |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10927 | if (!Failed && !Cond) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10928 | SmallString<256> MsgBuffer; |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 10929 | llvm::raw_svector_ostream Msg(MsgBuffer); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 10930 | AssertMessage->printPretty(Msg, 0, getPrintingPolicy()); |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10931 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 10932 | << Msg.str() << AssertExpr->getSourceRange(); |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10933 | Failed = true; |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 10934 | } |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 10935 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10936 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10937 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10938 | AssertExpr, AssertMessage, RParenLoc, |
| 10939 | Failed); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10940 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 10941 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10942 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 10943 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 10944 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10945 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 10946 | /// |
| 10947 | /// \returns A friend declaration that. |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10948 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation LocStart, |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 10949 | SourceLocation FriendLoc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10950 | TypeSourceInfo *TSInfo) { |
| 10951 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 10952 | |
| 10953 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 10954 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10955 | |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10956 | // C++03 [class.friend]p2: |
| 10957 | // An elaborated-type-specifier shall be used in a friend declaration |
| 10958 | // for a class.* |
| 10959 | // |
| 10960 | // * The class-key of the elaborated-type-specifier is required. |
| 10961 | if (!ActiveTemplateInstantiations.empty()) { |
| 10962 | // Do not complain about the form of friend template types during |
| 10963 | // template instantiation; we will already have complained when the |
| 10964 | // template was declared. |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 10965 | } else { |
| 10966 | if (!T->isElaboratedTypeSpecifier()) { |
| 10967 | // If we evaluated the type to a record type, suggest putting |
| 10968 | // a tag in front. |
| 10969 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 10970 | RecordDecl *RD = RT->getDecl(); |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10971 | |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 10972 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10973 | |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 10974 | Diag(TypeRange.getBegin(), |
| 10975 | getLangOpts().CPlusPlus11 ? |
| 10976 | diag::warn_cxx98_compat_unelaborated_friend_type : |
| 10977 | diag::ext_unelaborated_friend_type) |
| 10978 | << (unsigned) RD->getTagKind() |
| 10979 | << T |
| 10980 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 10981 | InsertionText); |
| 10982 | } else { |
| 10983 | Diag(FriendLoc, |
| 10984 | getLangOpts().CPlusPlus11 ? |
| 10985 | diag::warn_cxx98_compat_nonclass_type_friend : |
| 10986 | diag::ext_nonclass_type_friend) |
| 10987 | << T |
| 10988 | << TypeRange; |
| 10989 | } |
| 10990 | } else if (T->getAs<EnumType>()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10991 | Diag(FriendLoc, |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10992 | getLangOpts().CPlusPlus11 ? |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 10993 | diag::warn_cxx98_compat_enum_friend : |
| 10994 | diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10995 | << T |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10996 | << TypeRange; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10997 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10998 | |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 10999 | // C++11 [class.friend]p3: |
| 11000 | // A friend declaration that does not declare a function shall have one |
| 11001 | // of the following forms: |
| 11002 | // friend elaborated-type-specifier ; |
| 11003 | // friend simple-type-specifier ; |
| 11004 | // friend typename-specifier ; |
| 11005 | if (getLangOpts().CPlusPlus11 && LocStart != FriendLoc) |
| 11006 | Diag(FriendLoc, diag::err_friend_not_first_in_declaration) << T; |
| 11007 | } |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11008 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 11009 | // If the type specifier in a friend declaration designates a (possibly |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11010 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 11011 | // the friend declaration is ignored. |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 11012 | return FriendDecl::Create(Context, CurContext, LocStart, TSInfo, FriendLoc); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11013 | } |
| 11014 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11015 | /// Handle a friend tag declaration where the scope specifier was |
| 11016 | /// templated. |
| 11017 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 11018 | unsigned TagSpec, SourceLocation TagLoc, |
| 11019 | CXXScopeSpec &SS, |
Enea Zaffanella | 8c84028 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 11020 | IdentifierInfo *Name, |
| 11021 | SourceLocation NameLoc, |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11022 | AttributeList *Attr, |
| 11023 | MultiTemplateParamsArg TempParamLists) { |
| 11024 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 11025 | |
| 11026 | bool isExplicitSpecialization = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11027 | bool Invalid = false; |
| 11028 | |
Robert Wilhelm | 1169e2f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 11029 | if (TemplateParameterList *TemplateParams = |
| 11030 | MatchTemplateParametersToScopeSpecifier( |
| 11031 | TagLoc, NameLoc, SS, TempParamLists, /*friend*/ true, |
| 11032 | isExplicitSpecialization, Invalid)) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11033 | if (TemplateParams->size() > 0) { |
| 11034 | // This is a declaration of a class template. |
| 11035 | if (Invalid) |
| 11036 | return 0; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 11037 | |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 11038 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 11039 | SS, Name, NameLoc, Attr, |
| 11040 | TemplateParams, AS_public, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 11041 | /*ModulePrivateLoc=*/SourceLocation(), |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 11042 | TempParamLists.size() - 1, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11043 | TempParamLists.data()).take(); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11044 | } else { |
| 11045 | // The "template<>" header is extraneous. |
| 11046 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 11047 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 11048 | isExplicitSpecialization = true; |
| 11049 | } |
| 11050 | } |
| 11051 | |
| 11052 | if (Invalid) return 0; |
| 11053 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11054 | bool isAllExplicitSpecializations = true; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 11055 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11056 | if (TempParamLists[I]->size()) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11057 | isAllExplicitSpecializations = false; |
| 11058 | break; |
| 11059 | } |
| 11060 | } |
| 11061 | |
| 11062 | // FIXME: don't ignore attributes. |
| 11063 | |
| 11064 | // If it's explicit specializations all the way down, just forget |
| 11065 | // about the template header and build an appropriate non-templated |
| 11066 | // friend. TODO: for source fidelity, remember the headers. |
| 11067 | if (isAllExplicitSpecializations) { |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11068 | if (SS.isEmpty()) { |
| 11069 | bool Owned = false; |
| 11070 | bool IsDependent = false; |
| 11071 | return ActOnTag(S, TagSpec, TUK_Friend, TagLoc, SS, Name, NameLoc, |
| 11072 | Attr, AS_public, |
| 11073 | /*ModulePrivateLoc=*/SourceLocation(), |
| 11074 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 11075 | /*ScopedEnumKWLoc=*/SourceLocation(), |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11076 | /*ScopedEnumUsesClassTag=*/false, |
| 11077 | /*UnderlyingType=*/TypeResult()); |
| 11078 | } |
| 11079 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11080 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11081 | ElaboratedTypeKeyword Keyword |
| 11082 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11083 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 11084 | *Name, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11085 | if (T.isNull()) |
| 11086 | return 0; |
| 11087 | |
| 11088 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 11089 | if (isa<DependentNameType>(T)) { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11090 | DependentNameTypeLoc TL = |
| 11091 | TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 11092 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11093 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11094 | TL.setNameLoc(NameLoc); |
| 11095 | } else { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11096 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 11097 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 11098 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11099 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11100 | } |
| 11101 | |
| 11102 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
Enea Zaffanella | 8c84028 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 11103 | TSI, FriendLoc, TempParamLists); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11104 | Friend->setAccess(AS_public); |
| 11105 | CurContext->addDecl(Friend); |
| 11106 | return Friend; |
| 11107 | } |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11108 | |
| 11109 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 11110 | |
| 11111 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11112 | |
| 11113 | // Handle the case of a templated-scope friend class. e.g. |
| 11114 | // template <class T> class A<T>::B; |
| 11115 | // FIXME: we don't support these right now. |
| 11116 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 11117 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 11118 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11119 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 11120 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11121 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11122 | TL.setNameLoc(NameLoc); |
| 11123 | |
| 11124 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
Enea Zaffanella | 8c84028 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 11125 | TSI, FriendLoc, TempParamLists); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11126 | Friend->setAccess(AS_public); |
| 11127 | Friend->setUnsupportedFriend(true); |
| 11128 | CurContext->addDecl(Friend); |
| 11129 | return Friend; |
| 11130 | } |
| 11131 | |
| 11132 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11133 | /// Handle a friend type declaration. This works in tandem with |
| 11134 | /// ActOnTag. |
| 11135 | /// |
| 11136 | /// Notes on friend class templates: |
| 11137 | /// |
| 11138 | /// We generally treat friend class declarations as if they were |
| 11139 | /// declaring a class. So, for example, the elaborated type specifier |
| 11140 | /// in a friend declaration is required to obey the restrictions of a |
| 11141 | /// class-head (i.e. no typedefs in the scope chain), template |
| 11142 | /// parameters are required to match up with simple template-ids, &c. |
| 11143 | /// However, unlike when declaring a template specialization, it's |
| 11144 | /// okay to refer to a template specialization without an empty |
| 11145 | /// template parameter declaration, e.g. |
| 11146 | /// friend class A<T>::B<unsigned>; |
| 11147 | /// We permit this as a special case; if there are any template |
| 11148 | /// parameters present at all, require proper matching, i.e. |
James Dennett | ef2b5b3 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 11149 | /// template <> template \<class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11150 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 11151 | MultiTemplateParamsArg TempParams) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11152 | SourceLocation Loc = DS.getLocStart(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11153 | |
| 11154 | assert(DS.isFriendSpecified()); |
| 11155 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 11156 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11157 | // Try to convert the decl specifier to a type. This works for |
| 11158 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 11159 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 11160 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11161 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 11162 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 11163 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11164 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11165 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 11166 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 11167 | return 0; |
| 11168 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11169 | // This is definitely an error in C++98. It's probably meant to |
| 11170 | // be forbidden in C++0x, too, but the specification is just |
| 11171 | // poorly written. |
| 11172 | // |
| 11173 | // The problem is with declarations like the following: |
| 11174 | // template <T> friend A<T>::foo; |
| 11175 | // where deciding whether a class C is a friend or not now hinges |
| 11176 | // on whether there exists an instantiation of A that causes |
| 11177 | // 'foo' to equal C. There are restrictions on class-heads |
| 11178 | // (which we declare (by fiat) elaborated friend declarations to |
| 11179 | // be) that makes this tractable. |
| 11180 | // |
| 11181 | // FIXME: handle "template <> friend class A<T>;", which |
| 11182 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 11183 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11184 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 11185 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11186 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11187 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11188 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11189 | // C++98 [class.friend]p1: A friend of a class is a function |
| 11190 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 11191 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 11192 | // deadline. It's also a very silly restriction that seriously |
| 11193 | // affects inner classes and which nobody else seems to implement; |
| 11194 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 11195 | // |
| 11196 | // But note that we could warn about it: it's always useless to |
| 11197 | // friend one of your own members (it's not, however, worthless to |
| 11198 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11199 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11200 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11201 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11202 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11203 | NumTempParamLists, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11204 | TempParams.data(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 11205 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11206 | DS.getFriendSpecLoc()); |
| 11207 | else |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 11208 | D = CheckFriendTypeDecl(Loc, DS.getFriendSpecLoc(), TSI); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11209 | |
| 11210 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11211 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11212 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11213 | D->setAccess(AS_public); |
| 11214 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11215 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11216 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11217 | } |
| 11218 | |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 11219 | NamedDecl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, |
| 11220 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11221 | const DeclSpec &DS = D.getDeclSpec(); |
| 11222 | |
| 11223 | assert(DS.isFriendSpecified()); |
| 11224 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 11225 | |
| 11226 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11227 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11228 | |
| 11229 | // C++ [class.friend]p1 |
| 11230 | // A friend of a class is a function or class.... |
| 11231 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11232 | // It *doesn't* see through dependent types, which is correct |
| 11233 | // according to [temp.arg.type]p3: |
| 11234 | // If a declaration acquires a function type through a |
| 11235 | // type dependent on a template-parameter and this causes |
| 11236 | // a declaration that does not use the syntactic form of a |
| 11237 | // function declarator to have a function type, the program |
| 11238 | // is ill-formed. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11239 | if (!TInfo->getType()->isFunctionType()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11240 | Diag(Loc, diag::err_unexpected_friend); |
| 11241 | |
| 11242 | // It might be worthwhile to try to recover by creating an |
| 11243 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11244 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11245 | } |
| 11246 | |
| 11247 | // C++ [namespace.memdef]p3 |
| 11248 | // - If a friend declaration in a non-local class first declares a |
| 11249 | // class or function, the friend class or function is a member |
| 11250 | // of the innermost enclosing namespace. |
| 11251 | // - The name of the friend is not found by simple name lookup |
| 11252 | // until a matching declaration is provided in that namespace |
| 11253 | // scope (either before or after the class declaration granting |
| 11254 | // friendship). |
| 11255 | // - If a friend function is called, its name may be found by the |
| 11256 | // name lookup that considers functions from namespaces and |
| 11257 | // classes associated with the types of the function arguments. |
| 11258 | // - When looking for a prior declaration of a class or a function |
| 11259 | // declared as a friend, scopes outside the innermost enclosing |
| 11260 | // namespace scope are not considered. |
| 11261 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11262 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 11263 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 11264 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11265 | assert(Name); |
| 11266 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 11267 | // Check for unexpanded parameter packs. |
| 11268 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 11269 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 11270 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 11271 | return 0; |
| 11272 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11273 | // The context we found the declaration in, or in which we should |
| 11274 | // create the declaration. |
| 11275 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 11276 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 11277 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11278 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11279 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11280 | // FIXME: there are different rules in local classes |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11281 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11282 | // There are four cases here. |
| 11283 | // - 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] | 11284 | // appropriate scope and look for a function or function template |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11285 | // there as appropriate. |
| 11286 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 11287 | if (SS.isInvalid() || !SS.isSet()) { |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11288 | // C++0x [namespace.memdef]p3: |
| 11289 | // If the name in a friend declaration is neither qualified nor |
| 11290 | // a template-id and the declaration is a function or an |
| 11291 | // elaborated-type-specifier, the lookup to determine whether |
| 11292 | // the entity has been previously declared shall not consider |
| 11293 | // any scopes outside the innermost enclosing namespace. |
| 11294 | // C++0x [class.friend]p11: |
| 11295 | // If a friend declaration appears in a local class and the name |
| 11296 | // specified is an unqualified name, a prior declaration is |
| 11297 | // looked up without considering scopes that are outside the |
| 11298 | // innermost enclosing non-class scope. For a friend function |
| 11299 | // declaration, if there is no prior declaration, the program is |
| 11300 | // ill-formed. |
| 11301 | bool isLocal = cast<CXXRecordDecl>(CurContext)->isLocalClass(); |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 11302 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11303 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11304 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11305 | DC = CurContext; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11306 | |
Rafael Espindola | 11dc634 | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11307 | // Skip class contexts. If someone can cite chapter and verse |
| 11308 | // for this behavior, that would be nice --- it's what GCC and |
| 11309 | // EDG do, and it seems like a reasonable intent, but the spec |
| 11310 | // really only says that checks for unqualified existing |
| 11311 | // declarations should stop at the nearest enclosing namespace, |
| 11312 | // not that they should only consider the nearest enclosing |
| 11313 | // namespace. |
| 11314 | while (DC->isRecord()) |
| 11315 | DC = DC->getParent(); |
| 11316 | |
| 11317 | DeclContext *LookupDC = DC; |
| 11318 | while (LookupDC->isTransparentContext()) |
| 11319 | LookupDC = LookupDC->getParent(); |
| 11320 | |
| 11321 | while (true) { |
| 11322 | LookupQualifiedName(Previous, LookupDC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11323 | |
| 11324 | // TODO: decide what we think about using declarations. |
Rafael Espindola | 11dc634 | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11325 | if (isLocal) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11326 | break; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11327 | |
Rafael Espindola | 11dc634 | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11328 | if (!Previous.empty()) { |
| 11329 | DC = LookupDC; |
| 11330 | break; |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 11331 | } |
Rafael Espindola | 11dc634 | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11332 | |
| 11333 | if (isTemplateId) { |
| 11334 | if (isa<TranslationUnitDecl>(LookupDC)) break; |
| 11335 | } else { |
| 11336 | if (LookupDC->isFileContext()) break; |
| 11337 | } |
| 11338 | LookupDC = LookupDC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11339 | } |
| 11340 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 11341 | DCScope = getScopeForDeclContext(S, DC); |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 11342 | |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11343 | // C++ [class.friend]p6: |
| 11344 | // A function can be defined in a friend declaration of a class if and |
| 11345 | // only if the class is a non-local class (9.8), the function name is |
| 11346 | // unqualified, and the function has namespace scope. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11347 | if (isLocal && D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11348 | Diag(NameInfo.getBeginLoc(), diag::err_friend_def_in_local_class); |
| 11349 | } |
| 11350 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11351 | // - There's a non-dependent scope specifier, in which case we |
| 11352 | // compute it and do a previous lookup there for a function |
| 11353 | // or function template. |
| 11354 | } else if (!SS.getScopeRep()->isDependent()) { |
| 11355 | DC = computeDeclContext(SS); |
| 11356 | if (!DC) return 0; |
| 11357 | |
| 11358 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 11359 | |
| 11360 | LookupQualifiedName(Previous, DC); |
| 11361 | |
| 11362 | // Ignore things found implicitly in the wrong scope. |
| 11363 | // TODO: better diagnostics for this case. Suggesting the right |
| 11364 | // qualified scope would be nice... |
| 11365 | LookupResult::Filter F = Previous.makeFilter(); |
| 11366 | while (F.hasNext()) { |
| 11367 | NamedDecl *D = F.next(); |
| 11368 | if (!DC->InEnclosingNamespaceSetOf( |
| 11369 | D->getDeclContext()->getRedeclContext())) |
| 11370 | F.erase(); |
| 11371 | } |
| 11372 | F.done(); |
| 11373 | |
| 11374 | if (Previous.empty()) { |
| 11375 | D.setInvalidType(); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11376 | Diag(Loc, diag::err_qualified_friend_not_found) |
| 11377 | << Name << TInfo->getType(); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11378 | return 0; |
| 11379 | } |
| 11380 | |
| 11381 | // C++ [class.friend]p1: A friend of a class is a function or |
| 11382 | // class that is not a member of the class . . . |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 11383 | if (DC->Equals(CurContext)) |
| 11384 | Diag(DS.getFriendSpecLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11385 | getLangOpts().CPlusPlus11 ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 11386 | diag::warn_cxx98_compat_friend_is_member : |
| 11387 | diag::err_friend_is_member); |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11388 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11389 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11390 | // C++ [class.friend]p6: |
| 11391 | // A function can be defined in a friend declaration of a class if and |
| 11392 | // only if the class is a non-local class (9.8), the function name is |
| 11393 | // unqualified, and the function has namespace scope. |
| 11394 | SemaDiagnosticBuilder DB |
| 11395 | = Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def); |
| 11396 | |
| 11397 | DB << SS.getScopeRep(); |
| 11398 | if (DC->isFileContext()) |
| 11399 | DB << FixItHint::CreateRemoval(SS.getRange()); |
| 11400 | SS.clear(); |
| 11401 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11402 | |
| 11403 | // - There's a scope specifier that does not match any template |
| 11404 | // parameter lists, in which case we use some arbitrary context, |
| 11405 | // create a method or method template, and wait for instantiation. |
| 11406 | // - There's a scope specifier that does match some template |
| 11407 | // parameter lists, which we don't handle right now. |
| 11408 | } else { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11409 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11410 | // C++ [class.friend]p6: |
| 11411 | // A function can be defined in a friend declaration of a class if and |
| 11412 | // only if the class is a non-local class (9.8), the function name is |
| 11413 | // unqualified, and the function has namespace scope. |
| 11414 | Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def) |
| 11415 | << SS.getScopeRep(); |
| 11416 | } |
| 11417 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11418 | DC = CurContext; |
| 11419 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11420 | } |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11421 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11422 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11423 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 11424 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 11425 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 11426 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11427 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 11428 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 11429 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11430 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11431 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11432 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11433 | |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 11434 | // FIXME: This is an egregious hack to cope with cases where the scope stack |
| 11435 | // does not contain the declaration context, i.e., in an out-of-line |
| 11436 | // definition of a class. |
| 11437 | Scope FakeDCScope(S, Scope::DeclScope, Diags); |
| 11438 | if (!DCScope) { |
| 11439 | FakeDCScope.setEntity(DC); |
| 11440 | DCScope = &FakeDCScope; |
| 11441 | } |
| 11442 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 11443 | bool AddToScope = true; |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11444 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, TInfo, Previous, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11445 | TemplateParams, AddToScope); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11446 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11447 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 11448 | assert(ND->getDeclContext() == DC); |
| 11449 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 11450 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11451 | // Add the function declaration to the appropriate lookup tables, |
| 11452 | // adjusting the redeclarations list as necessary. We don't |
| 11453 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11454 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11455 | // Also update the scope-based lookup if the target context's |
| 11456 | // lookup context is in lexical scope. |
| 11457 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 11458 | DC = DC->getRedeclContext(); |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 11459 | DC->makeDeclVisibleInContext(ND); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11460 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 11461 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11462 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11463 | |
| 11464 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 11465 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11466 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 11467 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11468 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11469 | |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 11470 | if (ND->isInvalidDecl()) { |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11471 | FrD->setInvalidDecl(); |
John McCall | 1f2e1a9 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 11472 | } else { |
| 11473 | if (DC->isRecord()) CheckFriendAccess(ND); |
| 11474 | |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 11475 | FunctionDecl *FD; |
| 11476 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 11477 | FD = FTD->getTemplatedDecl(); |
| 11478 | else |
| 11479 | FD = cast<FunctionDecl>(ND); |
| 11480 | |
David Majnemer | f6a144f | 2013-06-25 23:09:30 +0000 | [diff] [blame] | 11481 | // C++11 [dcl.fct.default]p4: If a friend declaration specifies a |
| 11482 | // default argument expression, that declaration shall be a definition |
| 11483 | // and shall be the only declaration of the function or function |
| 11484 | // template in the translation unit. |
| 11485 | if (functionDeclHasDefaultArgument(FD)) { |
| 11486 | if (FunctionDecl *OldFD = FD->getPreviousDecl()) { |
| 11487 | Diag(FD->getLocation(), diag::err_friend_decl_with_def_arg_redeclared); |
| 11488 | Diag(OldFD->getLocation(), diag::note_previous_declaration); |
| 11489 | } else if (!D.isFunctionDefinition()) |
| 11490 | Diag(FD->getLocation(), diag::err_friend_decl_with_def_arg_must_be_def); |
| 11491 | } |
| 11492 | |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 11493 | // Mark templated-scope function declarations as unsupported. |
| 11494 | if (FD->getNumTemplateParameterLists()) |
| 11495 | FrD->setUnsupportedFriend(true); |
| 11496 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11497 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11498 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 11499 | } |
| 11500 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11501 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 11502 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11503 | |
Aaron Ballman | afb7ce3 | 2013-01-16 23:39:10 +0000 | [diff] [blame] | 11504 | FunctionDecl *Fn = dyn_cast_or_null<FunctionDecl>(Dcl); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11505 | if (!Fn) { |
| 11506 | Diag(DelLoc, diag::err_deleted_non_function); |
| 11507 | return; |
| 11508 | } |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 11509 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 11510 | if (const FunctionDecl *Prev = Fn->getPreviousDecl()) { |
David Blaikie | d9cf826 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 11511 | // Don't consider the implicit declaration we generate for explicit |
| 11512 | // specializations. FIXME: Do not generate these implicit declarations. |
David Blaikie | 619ee6a | 2012-06-29 18:00:25 +0000 | [diff] [blame] | 11513 | if ((Prev->getTemplateSpecializationKind() != TSK_ExplicitSpecialization |
| 11514 | || Prev->getPreviousDecl()) && !Prev->isDefined()) { |
David Blaikie | d9cf826 | 2012-06-25 21:55:30 +0000 | [diff] [blame] | 11515 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 11516 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 11517 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11518 | // If the declaration wasn't the first, we delete the function anyway for |
| 11519 | // recovery. |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 11520 | Fn = Fn->getCanonicalDecl(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11521 | } |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 11522 | |
| 11523 | if (Fn->isDeleted()) |
| 11524 | return; |
| 11525 | |
| 11526 | // See if we're deleting a function which is already known to override a |
| 11527 | // non-deleted virtual function. |
| 11528 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn)) { |
| 11529 | bool IssuedDiagnostic = false; |
| 11530 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 11531 | E = MD->end_overridden_methods(); |
| 11532 | I != E; ++I) { |
| 11533 | if (!(*MD->begin_overridden_methods())->isDeleted()) { |
| 11534 | if (!IssuedDiagnostic) { |
| 11535 | Diag(DelLoc, diag::err_deleted_override) << MD->getDeclName(); |
| 11536 | IssuedDiagnostic = true; |
| 11537 | } |
| 11538 | Diag((*I)->getLocation(), diag::note_overridden_virtual_function); |
| 11539 | } |
| 11540 | } |
| 11541 | } |
| 11542 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 11543 | Fn->setDeletedAsWritten(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 11544 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 11545 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11546 | void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) { |
Aaron Ballman | afb7ce3 | 2013-01-16 23:39:10 +0000 | [diff] [blame] | 11547 | CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Dcl); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11548 | |
| 11549 | if (MD) { |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 11550 | if (MD->getParent()->isDependentType()) { |
| 11551 | MD->setDefaulted(); |
| 11552 | MD->setExplicitlyDefaulted(); |
| 11553 | return; |
| 11554 | } |
| 11555 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11556 | CXXSpecialMember Member = getSpecialMember(MD); |
| 11557 | if (Member == CXXInvalid) { |
Eli Friedman | fcb5a25 | 2013-07-11 23:55:07 +0000 | [diff] [blame] | 11558 | if (!MD->isInvalidDecl()) |
| 11559 | Diag(DefaultLoc, diag::err_default_special_members); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11560 | return; |
| 11561 | } |
| 11562 | |
| 11563 | MD->setDefaulted(); |
| 11564 | MD->setExplicitlyDefaulted(); |
| 11565 | |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 11566 | // If this definition appears within the record, do the checking when |
| 11567 | // the record is complete. |
| 11568 | const FunctionDecl *Primary = MD; |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 11569 | if (const FunctionDecl *Pattern = MD->getTemplateInstantiationPattern()) |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 11570 | // Find the uninstantiated declaration that actually had the '= default' |
| 11571 | // on it. |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 11572 | Pattern->isDefined(Primary); |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 11573 | |
Richard Smith | 12fef49 | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 11574 | // If the method was defaulted on its first declaration, we will have |
| 11575 | // already performed the checking in CheckCompletedCXXClass. Such a |
| 11576 | // declaration doesn't trigger an implicit definition. |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 11577 | if (Primary == Primary->getCanonicalDecl()) |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11578 | return; |
| 11579 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11580 | CheckExplicitlyDefaultedSpecialMember(MD); |
| 11581 | |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 11582 | // The exception specification is needed because we are defining the |
| 11583 | // function. |
| 11584 | ResolveExceptionSpec(DefaultLoc, |
| 11585 | MD->getType()->castAs<FunctionProtoType>()); |
| 11586 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11587 | if (MD->isInvalidDecl()) |
| 11588 | return; |
| 11589 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11590 | switch (Member) { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11591 | case CXXDefaultConstructor: |
| 11592 | DefineImplicitDefaultConstructor(DefaultLoc, |
| 11593 | cast<CXXConstructorDecl>(MD)); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 11594 | break; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11595 | case CXXCopyConstructor: |
| 11596 | DefineImplicitCopyConstructor(DefaultLoc, cast<CXXConstructorDecl>(MD)); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11597 | break; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11598 | case CXXCopyAssignment: |
| 11599 | DefineImplicitCopyAssignment(DefaultLoc, MD); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 11600 | break; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11601 | case CXXDestructor: |
| 11602 | DefineImplicitDestructor(DefaultLoc, cast<CXXDestructorDecl>(MD)); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 11603 | break; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11604 | case CXXMoveConstructor: |
| 11605 | DefineImplicitMoveConstructor(DefaultLoc, cast<CXXConstructorDecl>(MD)); |
Sean Hunt | 8271317 | 2011-05-25 23:16:36 +0000 | [diff] [blame] | 11606 | break; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11607 | case CXXMoveAssignment: |
| 11608 | DefineImplicitMoveAssignment(DefaultLoc, MD); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11609 | break; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 11610 | case CXXInvalid: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 11611 | llvm_unreachable("Invalid special member."); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 11612 | } |
| 11613 | } else { |
| 11614 | Diag(DefaultLoc, diag::err_default_special_members); |
| 11615 | } |
| 11616 | } |
| 11617 | |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 11618 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 11619 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 11620 | Stmt *SubStmt = *CI; |
| 11621 | if (!SubStmt) |
| 11622 | continue; |
| 11623 | if (isa<ReturnStmt>(SubStmt)) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11624 | Self.Diag(SubStmt->getLocStart(), |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 11625 | diag::err_return_in_constructor_handler); |
| 11626 | if (!isa<Expr>(SubStmt)) |
| 11627 | SearchForReturnInStmt(Self, SubStmt); |
| 11628 | } |
| 11629 | } |
| 11630 | |
| 11631 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 11632 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 11633 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 11634 | SearchForReturnInStmt(*this, Handler); |
| 11635 | } |
| 11636 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 11637 | |
David Blaikie | 299adab | 2013-01-18 23:03:15 +0000 | [diff] [blame] | 11638 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 11639 | const CXXMethodDecl *Old) { |
| 11640 | const FunctionType *NewFT = New->getType()->getAs<FunctionType>(); |
| 11641 | const FunctionType *OldFT = Old->getType()->getAs<FunctionType>(); |
| 11642 | |
| 11643 | CallingConv NewCC = NewFT->getCallConv(), OldCC = OldFT->getCallConv(); |
| 11644 | |
| 11645 | // If the calling conventions match, everything is fine |
| 11646 | if (NewCC == OldCC) |
| 11647 | return false; |
| 11648 | |
| 11649 | // If either of the calling conventions are set to "default", we need to pick |
| 11650 | // something more sensible based on the target. This supports code where the |
| 11651 | // one method explicitly sets thiscall, and another has no explicit calling |
| 11652 | // convention. |
| 11653 | CallingConv Default = |
| 11654 | Context.getTargetInfo().getDefaultCallingConv(TargetInfo::CCMT_Member); |
| 11655 | if (NewCC == CC_Default) |
| 11656 | NewCC = Default; |
| 11657 | if (OldCC == CC_Default) |
| 11658 | OldCC = Default; |
| 11659 | |
| 11660 | // If the calling conventions still don't match, then report the error |
| 11661 | if (NewCC != OldCC) { |
David Blaikie | 299adab | 2013-01-18 23:03:15 +0000 | [diff] [blame] | 11662 | Diag(New->getLocation(), |
| 11663 | diag::err_conflicting_overriding_cc_attributes) |
| 11664 | << New->getDeclName() << New->getType() << Old->getType(); |
| 11665 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 11666 | return true; |
Aaron Ballman | fff3248 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 11667 | } |
| 11668 | |
| 11669 | return false; |
| 11670 | } |
| 11671 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11672 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 11673 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 11674 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 11675 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 11676 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 11677 | if (Context.hasSameType(NewTy, OldTy) || |
| 11678 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 11679 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11680 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11681 | // Check if the return types are covariant |
| 11682 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11683 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11684 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 11685 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 11686 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11687 | NewClassTy = NewPT->getPointeeType(); |
| 11688 | OldClassTy = OldPT->getPointeeType(); |
| 11689 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 11690 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 11691 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 11692 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 11693 | NewClassTy = NewRT->getPointeeType(); |
| 11694 | OldClassTy = OldRT->getPointeeType(); |
| 11695 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11696 | } |
| 11697 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11698 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11699 | // The return types aren't either both pointers or references to a class type. |
| 11700 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11701 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11702 | diag::err_different_return_type_for_overriding_virtual_function) |
| 11703 | << New->getDeclName() << NewTy << OldTy; |
| 11704 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11705 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11706 | return true; |
| 11707 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 11708 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 11709 | // C++ [class.virtual]p6: |
| 11710 | // If the return type of D::f differs from the return type of B::f, the |
| 11711 | // class type in the return type of D::f shall be complete at the point of |
| 11712 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 11713 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 11714 | if (!RT->isBeingDefined() && |
| 11715 | RequireCompleteType(New->getLocation(), NewClassTy, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11716 | diag::err_covariant_return_incomplete, |
| 11717 | New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 11718 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 11719 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 11720 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 11721 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11722 | // Check if the new class derives from the old class. |
| 11723 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 11724 | Diag(New->getLocation(), |
| 11725 | diag::err_covariant_return_not_derived) |
| 11726 | << New->getDeclName() << NewTy << OldTy; |
| 11727 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 11728 | return true; |
| 11729 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11730 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11731 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 11732 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 11733 | diag::err_covariant_return_inaccessible_base, |
| 11734 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 11735 | // FIXME: Should this point to the return type? |
| 11736 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 11737 | // FIXME: this note won't trigger for delayed access control |
| 11738 | // diagnostics, and it's impossible to get an undelayed error |
| 11739 | // here from access control during the original parse because |
| 11740 | // the ParsingDeclSpec/ParsingDeclarator are still in scope. |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11741 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 11742 | return true; |
| 11743 | } |
| 11744 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11745 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11746 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 11747 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11748 | Diag(New->getLocation(), |
| 11749 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 11750 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11751 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 11752 | return true; |
| 11753 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11754 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11755 | |
| 11756 | // The new class type must have the same or less qualifiers as the old type. |
| 11757 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 11758 | Diag(New->getLocation(), |
| 11759 | diag::err_covariant_return_type_class_type_more_qualified) |
| 11760 | << New->getDeclName() << NewTy << OldTy; |
| 11761 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 11762 | return true; |
| 11763 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11764 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 11765 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 11766 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11767 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 11768 | /// \brief Mark the given method pure. |
| 11769 | /// |
| 11770 | /// \param Method the method to be marked pure. |
| 11771 | /// |
| 11772 | /// \param InitRange the source range that covers the "0" initializer. |
| 11773 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 11774 | SourceLocation EndLoc = InitRange.getEnd(); |
| 11775 | if (EndLoc.isValid()) |
| 11776 | Method->setRangeEnd(EndLoc); |
| 11777 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 11778 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 11779 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 11780 | return false; |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 11781 | } |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 11782 | |
| 11783 | if (!Method->isInvalidDecl()) |
| 11784 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 11785 | << Method->getDeclName() << InitRange; |
| 11786 | return true; |
| 11787 | } |
| 11788 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 11789 | /// \brief Determine whether the given declaration is a static data member. |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11790 | static bool isStaticDataMember(const Decl *D) { |
| 11791 | if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(D)) |
| 11792 | return Var->isStaticDataMember(); |
| 11793 | |
| 11794 | return false; |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 11795 | } |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 11796 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 11797 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 11798 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 11799 | /// is a fresh scope pushed for just this purpose. |
| 11800 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11801 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 11802 | /// static data member of class X, names should be looked up in the scope of |
| 11803 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11804 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11805 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 11806 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11807 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 11808 | // We should only get called for declarations with scope specifiers, like: |
| 11809 | // int foo::bar; |
| 11810 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 11811 | EnterDeclaratorContext(S, D->getDeclContext()); |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 11812 | |
| 11813 | // If we are parsing the initializer for a static data member, push a |
| 11814 | // new expression evaluation context that is associated with this static |
| 11815 | // data member. |
| 11816 | if (isStaticDataMember(D)) |
| 11817 | PushExpressionEvaluationContext(PotentiallyEvaluated, D); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11818 | } |
| 11819 | |
| 11820 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11821 | /// initializer for the out-of-line declaration 'D'. |
| 11822 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11823 | // If there is no declaration, there was an error parsing it. |
Argyrios Kyrtzidis | b65abda | 2011-04-22 18:52:25 +0000 | [diff] [blame] | 11824 | if (D == 0 || D->isInvalidDecl()) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11825 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 11826 | if (isStaticDataMember(D)) |
| 11827 | PopExpressionEvaluationContext(); |
| 11828 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 11829 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 11830 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 11831 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 11832 | |
| 11833 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 11834 | /// C++ if/switch/while/for statement. |
| 11835 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11836 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 11837 | // C++ 6.4p2: |
| 11838 | // The declarator shall not specify a function or an array. |
| 11839 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 11840 | // new class or enumeration. |
| 11841 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 11842 | "Parser allowed 'typedef' as storage class of condition decl."); |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 11843 | |
| 11844 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 11845 | if (!Dcl) |
| 11846 | return true; |
| 11847 | |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 11848 | if (isa<FunctionDecl>(Dcl)) { // The declarator shall not specify a function. |
| 11849 | Diag(Dcl->getLocation(), diag::err_invalid_use_of_function_type) |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 11850 | << D.getSourceRange(); |
Douglas Gregor | 9a30c99 | 2011-07-05 16:13:20 +0000 | [diff] [blame] | 11851 | return true; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 11852 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 11853 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 11854 | return Dcl; |
| 11855 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 11856 | |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 11857 | void Sema::LoadExternalVTableUses() { |
| 11858 | if (!ExternalSource) |
| 11859 | return; |
| 11860 | |
| 11861 | SmallVector<ExternalVTableUse, 4> VTables; |
| 11862 | ExternalSource->ReadUsedVTables(VTables); |
| 11863 | SmallVector<VTableUse, 4> NewUses; |
| 11864 | for (unsigned I = 0, N = VTables.size(); I != N; ++I) { |
| 11865 | llvm::DenseMap<CXXRecordDecl *, bool>::iterator Pos |
| 11866 | = VTablesUsed.find(VTables[I].Record); |
| 11867 | // Even if a definition wasn't required before, it may be required now. |
| 11868 | if (Pos != VTablesUsed.end()) { |
| 11869 | if (!Pos->second && VTables[I].DefinitionRequired) |
| 11870 | Pos->second = true; |
| 11871 | continue; |
| 11872 | } |
| 11873 | |
| 11874 | VTablesUsed[VTables[I].Record] = VTables[I].DefinitionRequired; |
| 11875 | NewUses.push_back(VTableUse(VTables[I].Record, VTables[I].Location)); |
| 11876 | } |
| 11877 | |
| 11878 | VTableUses.insert(VTableUses.begin(), NewUses.begin(), NewUses.end()); |
| 11879 | } |
| 11880 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11881 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 11882 | bool DefinitionRequired) { |
| 11883 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 11884 | // not have a vtable. |
| 11885 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
John McCall | aeeacf7 | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 11886 | CurContext->isDependentContext() || isUnevaluatedContext()) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 11887 | return; |
| 11888 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11889 | // Try to insert this class into the map. |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 11890 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11891 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 11892 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 11893 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 11894 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 11895 | // If we already had an entry, check to see if we are promoting this vtable |
| 11896 | // to required a definition. If so, we need to reappend to the VTableUses |
| 11897 | // list, since we may have already processed the first entry. |
| 11898 | if (DefinitionRequired && !Pos.first->second) { |
| 11899 | Pos.first->second = true; |
| 11900 | } else { |
| 11901 | // Otherwise, we can early exit. |
| 11902 | return; |
| 11903 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11904 | } |
| 11905 | |
| 11906 | // Local classes need to have their virtual members marked |
| 11907 | // immediately. For all other classes, we mark their virtual members |
| 11908 | // at the end of the translation unit. |
| 11909 | if (Class->isLocalClass()) |
| 11910 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 11911 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11912 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 11913 | } |
| 11914 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11915 | bool Sema::DefineUsedVTables() { |
Douglas Gregor | dfe6543 | 2011-07-28 19:11:31 +0000 | [diff] [blame] | 11916 | LoadExternalVTableUses(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11917 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 11918 | return false; |
Chandler Carruth | aee543a | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 11919 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11920 | // Note: The VTableUses vector could grow as a result of marking |
| 11921 | // the members of a class as "used", so we check the size each |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11922 | // time through the loop and prefer indices (which are stable) to |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11923 | // iterators (which are not). |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 11924 | bool DefinedAnything = false; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11925 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 11926 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11927 | if (!Class) |
| 11928 | continue; |
| 11929 | |
| 11930 | SourceLocation Loc = VTableUses[I].second; |
| 11931 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11932 | bool DefineVTable = true; |
| 11933 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11934 | // If this class has a key function, but that key function is |
| 11935 | // defined in another translation unit, we don't need to emit the |
| 11936 | // vtable even though we're using it. |
John McCall | d5617ee | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 11937 | const CXXMethodDecl *KeyFunction = Context.getCurrentKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 11938 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11939 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 11940 | case TSK_Undeclared: |
| 11941 | case TSK_ExplicitSpecialization: |
| 11942 | case TSK_ExplicitInstantiationDeclaration: |
| 11943 | // The key function is in another translation unit. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11944 | DefineVTable = false; |
| 11945 | break; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11946 | |
| 11947 | case TSK_ExplicitInstantiationDefinition: |
| 11948 | case TSK_ImplicitInstantiation: |
| 11949 | // We will be instantiating the key function. |
| 11950 | break; |
| 11951 | } |
| 11952 | } else if (!KeyFunction) { |
| 11953 | // If we have a class with no key function that is the subject |
| 11954 | // of an explicit instantiation declaration, suppress the |
| 11955 | // vtable; it will live with the explicit instantiation |
| 11956 | // definition. |
| 11957 | bool IsExplicitInstantiationDeclaration |
| 11958 | = Class->getTemplateSpecializationKind() |
| 11959 | == TSK_ExplicitInstantiationDeclaration; |
| 11960 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 11961 | REnd = Class->redecls_end(); |
| 11962 | R != REnd; ++R) { |
| 11963 | TemplateSpecializationKind TSK |
| 11964 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 11965 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 11966 | IsExplicitInstantiationDeclaration = true; |
| 11967 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 11968 | IsExplicitInstantiationDeclaration = false; |
| 11969 | break; |
| 11970 | } |
| 11971 | } |
| 11972 | |
| 11973 | if (IsExplicitInstantiationDeclaration) |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 11974 | DefineVTable = false; |
| 11975 | } |
| 11976 | |
| 11977 | // The exception specifications for all virtual members may be needed even |
| 11978 | // if we are not providing an authoritative form of the vtable in this TU. |
| 11979 | // We may choose to emit it available_externally anyway. |
| 11980 | if (!DefineVTable) { |
| 11981 | MarkVirtualMemberExceptionSpecsNeeded(Loc, Class); |
| 11982 | continue; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11983 | } |
| 11984 | |
| 11985 | // Mark all of the virtual members of this class as referenced, so |
| 11986 | // that we can build a vtable. Then, tell the AST consumer that a |
| 11987 | // vtable for this class is required. |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 11988 | DefinedAnything = true; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11989 | MarkVirtualMembersReferenced(Loc, Class); |
| 11990 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 11991 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 11992 | |
| 11993 | // Optionally warn if we're emitting a weak vtable. |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 11994 | if (Class->isExternallyVisible() && |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 11995 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Douglas Gregor | a120d01 | 2011-09-23 19:04:03 +0000 | [diff] [blame] | 11996 | const FunctionDecl *KeyFunctionDef = 0; |
| 11997 | if (!KeyFunction || |
| 11998 | (KeyFunction->hasBody(KeyFunctionDef) && |
| 11999 | KeyFunctionDef->isInlined())) |
David Blaikie | 44d95b5 | 2011-12-09 18:32:50 +0000 | [diff] [blame] | 12000 | Diag(Class->getLocation(), Class->getTemplateSpecializationKind() == |
| 12001 | TSK_ExplicitInstantiationDefinition |
| 12002 | ? diag::warn_weak_template_vtable : diag::warn_weak_vtable) |
| 12003 | << Class; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12004 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 12005 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 12006 | VTableUses.clear(); |
| 12007 | |
Douglas Gregor | 7884403 | 2011-04-22 22:25:37 +0000 | [diff] [blame] | 12008 | return DefinedAnything; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 12009 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12010 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12011 | void Sema::MarkVirtualMemberExceptionSpecsNeeded(SourceLocation Loc, |
| 12012 | const CXXRecordDecl *RD) { |
| 12013 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 12014 | E = RD->method_end(); I != E; ++I) |
| 12015 | if ((*I)->isVirtual() && !(*I)->isPure()) |
| 12016 | ResolveExceptionSpec(Loc, (*I)->getType()->castAs<FunctionProtoType>()); |
| 12017 | } |
| 12018 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 12019 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 12020 | const CXXRecordDecl *RD) { |
Richard Smith | ff817f7 | 2012-07-07 06:59:51 +0000 | [diff] [blame] | 12021 | // Mark all functions which will appear in RD's vtable as used. |
| 12022 | CXXFinalOverriderMap FinalOverriders; |
| 12023 | RD->getFinalOverriders(FinalOverriders); |
| 12024 | for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(), |
| 12025 | E = FinalOverriders.end(); |
| 12026 | I != E; ++I) { |
| 12027 | for (OverridingMethods::const_iterator OI = I->second.begin(), |
| 12028 | OE = I->second.end(); |
| 12029 | OI != OE; ++OI) { |
| 12030 | assert(OI->second.size() > 0 && "no final overrider"); |
| 12031 | CXXMethodDecl *Overrider = OI->second.front().Method; |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12032 | |
Richard Smith | ff817f7 | 2012-07-07 06:59:51 +0000 | [diff] [blame] | 12033 | // C++ [basic.def.odr]p2: |
| 12034 | // [...] A virtual member function is used if it is not pure. [...] |
| 12035 | if (!Overrider->isPure()) |
| 12036 | MarkFunctionReferenced(Loc, Overrider); |
| 12037 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12038 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 12039 | |
| 12040 | // Only classes that have virtual bases need a VTT. |
| 12041 | if (RD->getNumVBases() == 0) |
| 12042 | return; |
| 12043 | |
| 12044 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 12045 | e = RD->bases_end(); i != e; ++i) { |
| 12046 | const CXXRecordDecl *Base = |
| 12047 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 12048 | if (Base->getNumVBases() == 0) |
| 12049 | continue; |
| 12050 | MarkVirtualMembersReferenced(Loc, Base); |
| 12051 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 12052 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12053 | |
| 12054 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 12055 | /// Objective-C implementation whose ivars need be initialized. |
| 12056 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 12057 | if (!getLangOpts().CPlusPlus) |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12058 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 12059 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 12060 | SmallVector<ObjCIvarDecl*, 8> ivars; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12061 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 12062 | if (ivars.empty()) |
| 12063 | return; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 12064 | SmallVector<CXXCtorInitializer*, 32> AllToInit; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12065 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 12066 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 12067 | if (Field->isInvalidDecl()) |
| 12068 | continue; |
| 12069 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 12070 | CXXCtorInitializer *Member; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12071 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 12072 | InitializationKind InitKind = |
| 12073 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 12074 | |
| 12075 | InitializationSequence InitSeq(*this, InitEntity, InitKind, None); |
| 12076 | ExprResult MemberInit = |
| 12077 | InitSeq.Perform(*this, InitEntity, InitKind, None); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 12078 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12079 | // Note, MemberInit could actually come back empty if no initialization |
| 12080 | // is required (e.g., because it would call a trivial default constructor) |
| 12081 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 12082 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 12083 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12084 | Member = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 12085 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 12086 | SourceLocation(), |
| 12087 | MemberInit.takeAs<Expr>(), |
| 12088 | SourceLocation()); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12089 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 12090 | |
| 12091 | // Be sure that the destructor is accessible and is marked as referenced. |
| 12092 | if (const RecordType *RecordTy |
| 12093 | = Context.getBaseElementType(Field->getType()) |
| 12094 | ->getAs<RecordType>()) { |
| 12095 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 12096 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 12097 | MarkFunctionReferenced(Field->getLocation(), Destructor); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 12098 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 12099 | PDiag(diag::err_access_dtor_ivar) |
| 12100 | << Context.getBaseElementType(Field->getType())); |
| 12101 | } |
| 12102 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 12103 | } |
| 12104 | ObjCImplementation->setIvarInitializers(Context, |
| 12105 | AllToInit.data(), AllToInit.size()); |
| 12106 | } |
| 12107 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 12108 | |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12109 | static |
| 12110 | void DelegatingCycleHelper(CXXConstructorDecl* Ctor, |
| 12111 | llvm::SmallSet<CXXConstructorDecl*, 4> &Valid, |
| 12112 | llvm::SmallSet<CXXConstructorDecl*, 4> &Invalid, |
| 12113 | llvm::SmallSet<CXXConstructorDecl*, 4> &Current, |
| 12114 | Sema &S) { |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12115 | if (Ctor->isInvalidDecl()) |
| 12116 | return; |
| 12117 | |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12118 | CXXConstructorDecl *Target = Ctor->getTargetConstructor(); |
| 12119 | |
| 12120 | // Target may not be determinable yet, for instance if this is a dependent |
| 12121 | // call in an uninstantiated template. |
| 12122 | if (Target) { |
| 12123 | const FunctionDecl *FNTarget = 0; |
| 12124 | (void)Target->hasBody(FNTarget); |
| 12125 | Target = const_cast<CXXConstructorDecl*>( |
| 12126 | cast_or_null<CXXConstructorDecl>(FNTarget)); |
| 12127 | } |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12128 | |
| 12129 | CXXConstructorDecl *Canonical = Ctor->getCanonicalDecl(), |
| 12130 | // Avoid dereferencing a null pointer here. |
| 12131 | *TCanonical = Target ? Target->getCanonicalDecl() : 0; |
| 12132 | |
| 12133 | if (!Current.insert(Canonical)) |
| 12134 | return; |
| 12135 | |
| 12136 | // We know that beyond here, we aren't chaining into a cycle. |
| 12137 | if (!Target || !Target->isDelegatingConstructor() || |
| 12138 | Target->isInvalidDecl() || Valid.count(TCanonical)) { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12139 | Valid.insert(Current.begin(), Current.end()); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12140 | Current.clear(); |
| 12141 | // We've hit a cycle. |
| 12142 | } else if (TCanonical == Canonical || Invalid.count(TCanonical) || |
| 12143 | Current.count(TCanonical)) { |
| 12144 | // If we haven't diagnosed this cycle yet, do so now. |
| 12145 | if (!Invalid.count(TCanonical)) { |
| 12146 | S.Diag((*Ctor->init_begin())->getSourceLocation(), |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 12147 | diag::warn_delegating_ctor_cycle) |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12148 | << Ctor; |
| 12149 | |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12150 | // Don't add a note for a function delegating directly to itself. |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12151 | if (TCanonical != Canonical) |
| 12152 | S.Diag(Target->getLocation(), diag::note_it_delegates_to); |
| 12153 | |
| 12154 | CXXConstructorDecl *C = Target; |
| 12155 | while (C->getCanonicalDecl() != Canonical) { |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12156 | const FunctionDecl *FNTarget = 0; |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12157 | (void)C->getTargetConstructor()->hasBody(FNTarget); |
| 12158 | assert(FNTarget && "Ctor cycle through bodiless function"); |
| 12159 | |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12160 | C = const_cast<CXXConstructorDecl*>( |
| 12161 | cast<CXXConstructorDecl>(FNTarget)); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12162 | S.Diag(C->getLocation(), diag::note_which_delegates_to); |
| 12163 | } |
| 12164 | } |
| 12165 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12166 | Invalid.insert(Current.begin(), Current.end()); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12167 | Current.clear(); |
| 12168 | } else { |
| 12169 | DelegatingCycleHelper(Target, Valid, Invalid, Current, S); |
| 12170 | } |
| 12171 | } |
| 12172 | |
| 12173 | |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 12174 | void Sema::CheckDelegatingCtorCycles() { |
| 12175 | llvm::SmallSet<CXXConstructorDecl*, 4> Valid, Invalid, Current; |
| 12176 | |
Douglas Gregor | 0129b56 | 2011-07-27 21:57:17 +0000 | [diff] [blame] | 12177 | for (DelegatingCtorDeclsType::iterator |
| 12178 | I = DelegatingCtorDecls.begin(ExternalSource), |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12179 | E = DelegatingCtorDecls.end(); |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 12180 | I != E; ++I) |
| 12181 | DelegatingCycleHelper(*I, Valid, Invalid, Current, *this); |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12182 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12183 | for (llvm::SmallSet<CXXConstructorDecl *, 4>::iterator CI = Invalid.begin(), |
| 12184 | CE = Invalid.end(); |
| 12185 | CI != CE; ++CI) |
Sean Hunt | ebcbe1d | 2011-05-04 23:29:54 +0000 | [diff] [blame] | 12186 | (*CI)->setInvalidDecl(); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 12187 | } |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 12188 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12189 | namespace { |
| 12190 | /// \brief AST visitor that finds references to the 'this' expression. |
| 12191 | class FindCXXThisExpr : public RecursiveASTVisitor<FindCXXThisExpr> { |
| 12192 | Sema &S; |
| 12193 | |
| 12194 | public: |
| 12195 | explicit FindCXXThisExpr(Sema &S) : S(S) { } |
| 12196 | |
| 12197 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 12198 | S.Diag(E->getLocation(), diag::err_this_static_member_func) |
| 12199 | << E->isImplicit(); |
| 12200 | return false; |
| 12201 | } |
| 12202 | }; |
| 12203 | } |
| 12204 | |
| 12205 | bool Sema::checkThisInStaticMemberFunctionType(CXXMethodDecl *Method) { |
| 12206 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 12207 | if (!TSInfo) |
| 12208 | return false; |
| 12209 | |
| 12210 | TypeLoc TL = TSInfo->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12211 | FunctionProtoTypeLoc ProtoTL = TL.getAs<FunctionProtoTypeLoc>(); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12212 | if (!ProtoTL) |
| 12213 | return false; |
| 12214 | |
| 12215 | // C++11 [expr.prim.general]p3: |
| 12216 | // [The expression this] shall not appear before the optional |
| 12217 | // cv-qualifier-seq and it shall not appear within the declaration of a |
| 12218 | // static member function (although its type and value category are defined |
| 12219 | // within a static member function as they are within a non-static member |
| 12220 | // function). [ Note: this is because declaration matching does not occur |
NAKAMURA Takumi | c86d1fd | 2012-04-21 09:40:04 +0000 | [diff] [blame] | 12221 | // until the complete declarator is known. - end note ] |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12222 | const FunctionProtoType *Proto = ProtoTL.getTypePtr(); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12223 | FindCXXThisExpr Finder(*this); |
| 12224 | |
| 12225 | // If the return type came after the cv-qualifier-seq, check it now. |
| 12226 | if (Proto->hasTrailingReturn() && |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12227 | !Finder.TraverseTypeLoc(ProtoTL.getResultLoc())) |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12228 | return true; |
| 12229 | |
| 12230 | // Check the exception specification. |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12231 | if (checkThisInStaticMemberFunctionExceptionSpec(Method)) |
| 12232 | return true; |
| 12233 | |
| 12234 | return checkThisInStaticMemberFunctionAttributes(Method); |
| 12235 | } |
| 12236 | |
| 12237 | bool Sema::checkThisInStaticMemberFunctionExceptionSpec(CXXMethodDecl *Method) { |
| 12238 | TypeSourceInfo *TSInfo = Method->getTypeSourceInfo(); |
| 12239 | if (!TSInfo) |
| 12240 | return false; |
| 12241 | |
| 12242 | TypeLoc TL = TSInfo->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12243 | FunctionProtoTypeLoc ProtoTL = TL.getAs<FunctionProtoTypeLoc>(); |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12244 | if (!ProtoTL) |
| 12245 | return false; |
| 12246 | |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 12247 | const FunctionProtoType *Proto = ProtoTL.getTypePtr(); |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12248 | FindCXXThisExpr Finder(*this); |
| 12249 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12250 | switch (Proto->getExceptionSpecType()) { |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 12251 | case EST_Uninstantiated: |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 12252 | case EST_Unevaluated: |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12253 | case EST_BasicNoexcept: |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12254 | case EST_DynamicNone: |
| 12255 | case EST_MSAny: |
| 12256 | case EST_None: |
| 12257 | break; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12258 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12259 | case EST_ComputedNoexcept: |
| 12260 | if (!Finder.TraverseStmt(Proto->getNoexceptExpr())) |
| 12261 | return true; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12262 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12263 | case EST_Dynamic: |
| 12264 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12265 | EEnd = Proto->exception_end(); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12266 | E != EEnd; ++E) { |
| 12267 | if (!Finder.TraverseType(*E)) |
| 12268 | return true; |
| 12269 | } |
| 12270 | break; |
| 12271 | } |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12272 | |
| 12273 | return false; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 12274 | } |
| 12275 | |
| 12276 | bool Sema::checkThisInStaticMemberFunctionAttributes(CXXMethodDecl *Method) { |
| 12277 | FindCXXThisExpr Finder(*this); |
| 12278 | |
| 12279 | // Check attributes. |
| 12280 | for (Decl::attr_iterator A = Method->attr_begin(), AEnd = Method->attr_end(); |
| 12281 | A != AEnd; ++A) { |
| 12282 | // FIXME: This should be emitted by tblgen. |
| 12283 | Expr *Arg = 0; |
| 12284 | ArrayRef<Expr *> Args; |
| 12285 | if (GuardedByAttr *G = dyn_cast<GuardedByAttr>(*A)) |
| 12286 | Arg = G->getArg(); |
| 12287 | else if (PtGuardedByAttr *G = dyn_cast<PtGuardedByAttr>(*A)) |
| 12288 | Arg = G->getArg(); |
| 12289 | else if (AcquiredAfterAttr *AA = dyn_cast<AcquiredAfterAttr>(*A)) |
| 12290 | Args = ArrayRef<Expr *>(AA->args_begin(), AA->args_size()); |
| 12291 | else if (AcquiredBeforeAttr *AB = dyn_cast<AcquiredBeforeAttr>(*A)) |
| 12292 | Args = ArrayRef<Expr *>(AB->args_begin(), AB->args_size()); |
| 12293 | else if (ExclusiveLockFunctionAttr *ELF |
| 12294 | = dyn_cast<ExclusiveLockFunctionAttr>(*A)) |
| 12295 | Args = ArrayRef<Expr *>(ELF->args_begin(), ELF->args_size()); |
| 12296 | else if (SharedLockFunctionAttr *SLF |
| 12297 | = dyn_cast<SharedLockFunctionAttr>(*A)) |
| 12298 | Args = ArrayRef<Expr *>(SLF->args_begin(), SLF->args_size()); |
| 12299 | else if (ExclusiveTrylockFunctionAttr *ETLF |
| 12300 | = dyn_cast<ExclusiveTrylockFunctionAttr>(*A)) { |
| 12301 | Arg = ETLF->getSuccessValue(); |
| 12302 | Args = ArrayRef<Expr *>(ETLF->args_begin(), ETLF->args_size()); |
| 12303 | } else if (SharedTrylockFunctionAttr *STLF |
| 12304 | = dyn_cast<SharedTrylockFunctionAttr>(*A)) { |
| 12305 | Arg = STLF->getSuccessValue(); |
| 12306 | Args = ArrayRef<Expr *>(STLF->args_begin(), STLF->args_size()); |
| 12307 | } else if (UnlockFunctionAttr *UF = dyn_cast<UnlockFunctionAttr>(*A)) |
| 12308 | Args = ArrayRef<Expr *>(UF->args_begin(), UF->args_size()); |
| 12309 | else if (LockReturnedAttr *LR = dyn_cast<LockReturnedAttr>(*A)) |
| 12310 | Arg = LR->getArg(); |
| 12311 | else if (LocksExcludedAttr *LE = dyn_cast<LocksExcludedAttr>(*A)) |
| 12312 | Args = ArrayRef<Expr *>(LE->args_begin(), LE->args_size()); |
| 12313 | else if (ExclusiveLocksRequiredAttr *ELR |
| 12314 | = dyn_cast<ExclusiveLocksRequiredAttr>(*A)) |
| 12315 | Args = ArrayRef<Expr *>(ELR->args_begin(), ELR->args_size()); |
| 12316 | else if (SharedLocksRequiredAttr *SLR |
| 12317 | = dyn_cast<SharedLocksRequiredAttr>(*A)) |
| 12318 | Args = ArrayRef<Expr *>(SLR->args_begin(), SLR->args_size()); |
| 12319 | |
| 12320 | if (Arg && !Finder.TraverseStmt(Arg)) |
| 12321 | return true; |
| 12322 | |
| 12323 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 12324 | if (!Finder.TraverseStmt(Args[I])) |
| 12325 | return true; |
| 12326 | } |
| 12327 | } |
| 12328 | |
| 12329 | return false; |
| 12330 | } |
| 12331 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12332 | void |
| 12333 | Sema::checkExceptionSpecification(ExceptionSpecificationType EST, |
| 12334 | ArrayRef<ParsedType> DynamicExceptions, |
| 12335 | ArrayRef<SourceRange> DynamicExceptionRanges, |
| 12336 | Expr *NoexceptExpr, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 12337 | SmallVectorImpl<QualType> &Exceptions, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12338 | FunctionProtoType::ExtProtoInfo &EPI) { |
| 12339 | Exceptions.clear(); |
| 12340 | EPI.ExceptionSpecType = EST; |
| 12341 | if (EST == EST_Dynamic) { |
| 12342 | Exceptions.reserve(DynamicExceptions.size()); |
| 12343 | for (unsigned ei = 0, ee = DynamicExceptions.size(); ei != ee; ++ei) { |
| 12344 | // FIXME: Preserve type source info. |
| 12345 | QualType ET = GetTypeFromParser(DynamicExceptions[ei]); |
| 12346 | |
| 12347 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 12348 | collectUnexpandedParameterPacks(ET, Unexpanded); |
| 12349 | if (!Unexpanded.empty()) { |
| 12350 | DiagnoseUnexpandedParameterPacks(DynamicExceptionRanges[ei].getBegin(), |
| 12351 | UPPC_ExceptionType, |
| 12352 | Unexpanded); |
| 12353 | continue; |
| 12354 | } |
| 12355 | |
| 12356 | // Check that the type is valid for an exception spec, and |
| 12357 | // drop it if not. |
| 12358 | if (!CheckSpecifiedExceptionType(ET, DynamicExceptionRanges[ei])) |
| 12359 | Exceptions.push_back(ET); |
| 12360 | } |
| 12361 | EPI.NumExceptions = Exceptions.size(); |
| 12362 | EPI.Exceptions = Exceptions.data(); |
| 12363 | return; |
| 12364 | } |
| 12365 | |
| 12366 | if (EST == EST_ComputedNoexcept) { |
| 12367 | // If an error occurred, there's no expression here. |
| 12368 | if (NoexceptExpr) { |
| 12369 | assert((NoexceptExpr->isTypeDependent() || |
| 12370 | NoexceptExpr->getType()->getCanonicalTypeUnqualified() == |
| 12371 | Context.BoolTy) && |
| 12372 | "Parser should have made sure that the expression is boolean"); |
| 12373 | if (NoexceptExpr && DiagnoseUnexpandedParameterPack(NoexceptExpr)) { |
| 12374 | EPI.ExceptionSpecType = EST_BasicNoexcept; |
| 12375 | return; |
| 12376 | } |
| 12377 | |
| 12378 | if (!NoexceptExpr->isValueDependent()) |
| 12379 | NoexceptExpr = VerifyIntegerConstantExpression(NoexceptExpr, 0, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 12380 | diag::err_noexcept_needs_constant_expression, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 12381 | /*AllowFold*/ false).take(); |
| 12382 | EPI.NoexceptExpr = NoexceptExpr; |
| 12383 | } |
| 12384 | return; |
| 12385 | } |
| 12386 | } |
| 12387 | |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 12388 | /// IdentifyCUDATarget - Determine the CUDA compilation target for this function |
| 12389 | Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const FunctionDecl *D) { |
| 12390 | // Implicitly declared functions (e.g. copy constructors) are |
| 12391 | // __host__ __device__ |
| 12392 | if (D->isImplicit()) |
| 12393 | return CFT_HostDevice; |
| 12394 | |
| 12395 | if (D->hasAttr<CUDAGlobalAttr>()) |
| 12396 | return CFT_Global; |
| 12397 | |
| 12398 | if (D->hasAttr<CUDADeviceAttr>()) { |
| 12399 | if (D->hasAttr<CUDAHostAttr>()) |
| 12400 | return CFT_HostDevice; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 12401 | return CFT_Device; |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 12402 | } |
| 12403 | |
| 12404 | return CFT_Host; |
| 12405 | } |
| 12406 | |
| 12407 | bool Sema::CheckCUDATarget(CUDAFunctionTarget CallerTarget, |
| 12408 | CUDAFunctionTarget CalleeTarget) { |
| 12409 | // CUDA B.1.1 "The __device__ qualifier declares a function that is... |
| 12410 | // Callable from the device only." |
| 12411 | if (CallerTarget == CFT_Host && CalleeTarget == CFT_Device) |
| 12412 | return true; |
| 12413 | |
| 12414 | // CUDA B.1.2 "The __global__ qualifier declares a function that is... |
| 12415 | // Callable from the host only." |
| 12416 | // CUDA B.1.3 "The __host__ qualifier declares a function that is... |
| 12417 | // Callable from the host only." |
| 12418 | if ((CallerTarget == CFT_Device || CallerTarget == CFT_Global) && |
| 12419 | (CalleeTarget == CFT_Host || CalleeTarget == CFT_Global)) |
| 12420 | return true; |
| 12421 | |
| 12422 | if (CallerTarget == CFT_HostDevice && CalleeTarget != CFT_HostDevice) |
| 12423 | return true; |
| 12424 | |
| 12425 | return false; |
| 12426 | } |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 12427 | |
| 12428 | /// HandleMSProperty - Analyze a __delcspec(property) field of a C++ class. |
| 12429 | /// |
| 12430 | MSPropertyDecl *Sema::HandleMSProperty(Scope *S, RecordDecl *Record, |
| 12431 | SourceLocation DeclStart, |
| 12432 | Declarator &D, Expr *BitWidth, |
| 12433 | InClassInitStyle InitStyle, |
| 12434 | AccessSpecifier AS, |
| 12435 | AttributeList *MSPropertyAttr) { |
| 12436 | IdentifierInfo *II = D.getIdentifier(); |
| 12437 | if (!II) { |
| 12438 | Diag(DeclStart, diag::err_anonymous_property); |
| 12439 | return NULL; |
| 12440 | } |
| 12441 | SourceLocation Loc = D.getIdentifierLoc(); |
| 12442 | |
| 12443 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 12444 | QualType T = TInfo->getType(); |
| 12445 | if (getLangOpts().CPlusPlus) { |
| 12446 | CheckExtraCXXDefaultArguments(D); |
| 12447 | |
| 12448 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 12449 | UPPC_DataMemberType)) { |
| 12450 | D.setInvalidType(); |
| 12451 | T = Context.IntTy; |
| 12452 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc); |
| 12453 | } |
| 12454 | } |
| 12455 | |
| 12456 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
| 12457 | |
| 12458 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 12459 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 12460 | diag::err_invalid_thread) |
| 12461 | << DeclSpec::getSpecifierName(TSCS); |
| 12462 | |
| 12463 | // Check to see if this name was declared as a member previously |
| 12464 | NamedDecl *PrevDecl = 0; |
| 12465 | LookupResult Previous(*this, II, Loc, LookupMemberName, ForRedeclaration); |
| 12466 | LookupName(Previous, S); |
| 12467 | switch (Previous.getResultKind()) { |
| 12468 | case LookupResult::Found: |
| 12469 | case LookupResult::FoundUnresolvedValue: |
| 12470 | PrevDecl = Previous.getAsSingle<NamedDecl>(); |
| 12471 | break; |
| 12472 | |
| 12473 | case LookupResult::FoundOverloaded: |
| 12474 | PrevDecl = Previous.getRepresentativeDecl(); |
| 12475 | break; |
| 12476 | |
| 12477 | case LookupResult::NotFound: |
| 12478 | case LookupResult::NotFoundInCurrentInstantiation: |
| 12479 | case LookupResult::Ambiguous: |
| 12480 | break; |
| 12481 | } |
| 12482 | |
| 12483 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 12484 | // Maybe we will complain about the shadowed template parameter. |
| 12485 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 12486 | // Just pretend that we didn't see the previous declaration. |
| 12487 | PrevDecl = 0; |
| 12488 | } |
| 12489 | |
| 12490 | if (PrevDecl && !isDeclInScope(PrevDecl, Record, S)) |
| 12491 | PrevDecl = 0; |
| 12492 | |
| 12493 | SourceLocation TSSL = D.getLocStart(); |
| 12494 | MSPropertyDecl *NewPD; |
| 12495 | const AttributeList::PropertyData &Data = MSPropertyAttr->getPropertyData(); |
| 12496 | NewPD = new (Context) MSPropertyDecl(Record, Loc, |
| 12497 | II, T, TInfo, TSSL, |
| 12498 | Data.GetterId, Data.SetterId); |
| 12499 | ProcessDeclAttributes(TUScope, NewPD, D); |
| 12500 | NewPD->setAccess(AS); |
| 12501 | |
| 12502 | if (NewPD->isInvalidDecl()) |
| 12503 | Record->setInvalidDecl(); |
| 12504 | |
| 12505 | if (D.getDeclSpec().isModulePrivateSpecified()) |
| 12506 | NewPD->setModulePrivate(); |
| 12507 | |
| 12508 | if (NewPD->isInvalidDecl() && PrevDecl) { |
| 12509 | // Don't introduce NewFD into scope; there's already something |
| 12510 | // with the same name in the same scope. |
| 12511 | } else if (II) { |
| 12512 | PushOnScopeChains(NewPD, S); |
| 12513 | } else |
| 12514 | Record->addDecl(NewPD); |
| 12515 | |
| 12516 | return NewPD; |
| 12517 | } |