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, |
Robert Wilhelm | e7205c0 | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 954 | SmallVectorImpl<SourceLocation> &ReturnStmts, |
Richard Smith | a10b978 | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 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 | |
Robert Wilhelm | 344472e | 2013-08-23 16:11:15 +0000 | [diff] [blame^] | 1252 | Current = Queue.pop_back_val(); |
Douglas Gregor | 229d47a | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | return false; |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1259 | /// |
| 1260 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 1261 | /// and returns NULL otherwise. |
| 1262 | CXXBaseSpecifier * |
| 1263 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 1264 | SourceRange SpecifierRange, |
| 1265 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1266 | TypeSourceInfo *TInfo, |
| 1267 | SourceLocation EllipsisLoc) { |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1268 | QualType BaseType = TInfo->getType(); |
| 1269 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1270 | // C++ [class.union]p1: |
| 1271 | // A union shall not have base classes. |
| 1272 | if (Class->isUnion()) { |
| 1273 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 1274 | << SpecifierRange; |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1278 | if (EllipsisLoc.isValid() && |
| 1279 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 1280 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1281 | << TInfo->getTypeLoc().getSourceRange(); |
| 1282 | EllipsisLoc = SourceLocation(); |
| 1283 | } |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1284 | |
| 1285 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
| 1286 | |
| 1287 | if (BaseType->isDependentType()) { |
| 1288 | // Make sure that we don't have circular inheritance among our dependent |
| 1289 | // bases. For non-dependent bases, the check for completeness below handles |
| 1290 | // this. |
| 1291 | if (CXXRecordDecl *BaseDecl = BaseType->getAsCXXRecordDecl()) { |
| 1292 | if (BaseDecl->getCanonicalDecl() == Class->getCanonicalDecl() || |
| 1293 | ((BaseDecl = BaseDecl->getDefinition()) && |
Douglas Gregor | 229d47a | 2012-11-10 07:24:09 +0000 | [diff] [blame] | 1294 | findCircularInheritance(Class, BaseDecl))) { |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1295 | Diag(BaseLoc, diag::err_circular_inheritance) |
| 1296 | << BaseType << Context.getTypeDeclType(Class); |
| 1297 | |
| 1298 | if (BaseDecl->getCanonicalDecl() != Class->getCanonicalDecl()) |
| 1299 | Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 1300 | << BaseType; |
| 1301 | |
| 1302 | return 0; |
| 1303 | } |
| 1304 | } |
| 1305 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1307 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1308 | Access, TInfo, EllipsisLoc); |
Douglas Gregor | d777e28 | 2012-11-10 01:18:17 +0000 | [diff] [blame] | 1309 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Base specifiers must be record types. |
| 1312 | if (!BaseType->isRecordType()) { |
| 1313 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
| 1317 | // C++ [class.union]p1: |
| 1318 | // A union shall not be used as a base class. |
| 1319 | if (BaseType->isUnionType()) { |
| 1320 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
| 1324 | // C++ [class.derived]p2: |
| 1325 | // The class-name in a base-specifier shall not be an incompletely |
| 1326 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 | if (RequireCompleteType(BaseLoc, BaseType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1328 | diag::err_incomplete_base_class, SpecifierRange)) { |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1329 | Class->setInvalidDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1330 | return 0; |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1331 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1332 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1333 | // 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] | 1334 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1335 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1336 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1337 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
David Majnemer | 2f68669 | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 1338 | CXXRecordDecl *CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1339 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 1340 | |
Anders Carlsson | 1d20927 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 1341 | // C++ [class]p3: |
| 1342 | // If a class is marked final and it appears as a base-type-specifier in |
| 1343 | // base-clause, the program is ill-formed. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1344 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 1345 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
| 1346 | << CXXBaseDecl->getDeclName(); |
| 1347 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 1348 | << CXXBaseDecl->getDeclName(); |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 1352 | if (BaseDecl->isInvalidDecl()) |
| 1353 | Class->setInvalidDecl(); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1354 | |
| 1355 | // Create the base specifier. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1356 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1357 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1358 | Access, TInfo, EllipsisLoc); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1361 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 1362 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | /// example: |
| 1364 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1365 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1366 | BaseResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1367 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1368 | ParsedAttributes &Attributes, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1369 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1370 | ParsedType basetype, SourceLocation BaseLoc, |
| 1371 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1372 | if (!classdecl) |
| 1373 | return true; |
| 1374 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1375 | AdjustDeclIfTemplate(classdecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1376 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 1377 | if (!Class) |
| 1378 | return true; |
| 1379 | |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1380 | // We do not support any C++11 attributes on base-specifiers yet. |
| 1381 | // Diagnose any attributes we see. |
| 1382 | if (!Attributes.empty()) { |
| 1383 | for (AttributeList *Attr = Attributes.getList(); Attr; |
| 1384 | Attr = Attr->getNext()) { |
| 1385 | if (Attr->isInvalid() || |
| 1386 | Attr->getKind() == AttributeList::IgnoredAttribute) |
| 1387 | continue; |
| 1388 | Diag(Attr->getLoc(), |
| 1389 | Attr->getKind() == AttributeList::UnknownAttribute |
| 1390 | ? diag::warn_unknown_attribute_ignored |
| 1391 | : diag::err_base_specifier_attribute) |
| 1392 | << Attr->getName(); |
| 1393 | } |
| 1394 | } |
| 1395 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 1396 | TypeSourceInfo *TInfo = 0; |
| 1397 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1398 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1399 | if (EllipsisLoc.isInvalid() && |
| 1400 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 1401 | UPPC_BaseType)) |
| 1402 | return true; |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1403 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1404 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 1405 | Virtual, Access, TInfo, |
| 1406 | EllipsisLoc)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1407 | return BaseSpec; |
Douglas Gregor | 8a50fe0 | 2012-07-02 21:00:41 +0000 | [diff] [blame] | 1408 | else |
| 1409 | Class->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1411 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1412 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1413 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1414 | /// \brief Performs the actual work of attaching the given base class |
| 1415 | /// specifiers to a C++ class. |
| 1416 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 1417 | unsigned NumBases) { |
| 1418 | if (NumBases == 0) |
| 1419 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1420 | |
| 1421 | // Used to keep track of which base types we have already seen, so |
| 1422 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1423 | // that the key is always the unqualified canonical type of the base |
| 1424 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1425 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 1426 | |
| 1427 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1428 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1429 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1430 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1431 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1432 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1433 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1434 | |
| 1435 | CXXBaseSpecifier *&KnownBase = KnownBaseTypes[NewBaseType]; |
| 1436 | if (KnownBase) { |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1437 | // C++ [class.mi]p3: |
| 1438 | // A class shall not be specified as a direct base class of a |
| 1439 | // derived class more than once. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1440 | Diag(Bases[idx]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1441 | diag::err_duplicate_base_class) |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1442 | << KnownBase->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1443 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1444 | |
| 1445 | // Delete the duplicate base class specifier; we're going to |
| 1446 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1447 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1448 | |
| 1449 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1450 | } else { |
| 1451 | // Okay, add this new base class. |
Benjamin Kramer | 52c1668 | 2012-03-05 17:20:04 +0000 | [diff] [blame] | 1452 | KnownBase = Bases[idx]; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1453 | Bases[NumGoodBases++] = Bases[idx]; |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1454 | if (const RecordType *Record = NewBaseType->getAs<RecordType>()) { |
| 1455 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
| 1456 | if (Class->isInterface() && |
| 1457 | (!RD->isInterface() || |
| 1458 | KnownBase->getAccessSpecifier() != AS_public)) { |
| 1459 | // The Microsoft extension __interface does not permit bases that |
| 1460 | // are not themselves public interfaces. |
| 1461 | Diag(KnownBase->getLocStart(), diag::err_invalid_base_in_interface) |
| 1462 | << getRecordDiagFromTagKind(RD->getTagKind()) << RD->getName() |
| 1463 | << RD->getSourceRange(); |
| 1464 | Invalid = true; |
| 1465 | } |
| 1466 | if (RD->hasAttr<WeakAttr>()) |
| 1467 | Class->addAttr(::new (Context) WeakAttr(SourceRange(), Context)); |
| 1468 | } |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 1473 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 1474 | |
| 1475 | // Delete the remaining (good) base class specifiers, since their |
| 1476 | // data has been copied into the CXXRecordDecl. |
| 1477 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1478 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1479 | |
| 1480 | return Invalid; |
| 1481 | } |
| 1482 | |
| 1483 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 1484 | /// class, after checking whether there are any duplicate base |
| 1485 | /// classes. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 1486 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, CXXBaseSpecifier **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1487 | unsigned NumBases) { |
| 1488 | if (!ClassDecl || !Bases || !NumBases) |
| 1489 | return; |
| 1490 | |
| 1491 | AdjustDeclIfTemplate(ClassDecl); |
Robert Wilhelm | 0d317a0 | 2013-07-22 05:04:01 +0000 | [diff] [blame] | 1492 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), Bases, NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1493 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1494 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1495 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1496 | /// derived from the type \p Base. |
| 1497 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1498 | if (!getLangOpts().CPlusPlus) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1499 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1500 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1501 | CXXRecordDecl *DerivedRD = Derived->getAsCXXRecordDecl(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1502 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1503 | return false; |
| 1504 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1505 | CXXRecordDecl *BaseRD = Base->getAsCXXRecordDecl(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1506 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1507 | return false; |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1508 | |
| 1509 | // If either the base or the derived type is invalid, don't try to |
| 1510 | // check whether one is derived from the other. |
| 1511 | if (BaseRD->isInvalidDecl() || DerivedRD->isInvalidDecl()) |
| 1512 | return false; |
| 1513 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1514 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 1515 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 1519 | /// derived from the type \p Base. |
| 1520 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1521 | if (!getLangOpts().CPlusPlus) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1522 | return false; |
| 1523 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1524 | CXXRecordDecl *DerivedRD = Derived->getAsCXXRecordDecl(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1525 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1526 | return false; |
| 1527 | |
Douglas Gregor | 0162c1c | 2013-03-26 23:36:30 +0000 | [diff] [blame] | 1528 | CXXRecordDecl *BaseRD = Base->getAsCXXRecordDecl(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1529 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1530 | return false; |
| 1531 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1532 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 1533 | } |
| 1534 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1535 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1536 | CXXCastPath &BasePathArray) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1537 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 1538 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 1539 | |
| 1540 | const CXXBasePath &Path = Paths.front(); |
| 1541 | |
| 1542 | // We first go backward and check if we have a virtual base. |
| 1543 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 1544 | // the nearest virtual base. |
| 1545 | unsigned Start = 0; |
| 1546 | for (unsigned I = Path.size(); I != 0; --I) { |
| 1547 | if (Path[I - 1].Base->isVirtual()) { |
| 1548 | Start = I - 1; |
| 1549 | break; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | // Now add all bases. |
| 1554 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1555 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1558 | /// \brief Determine whether the given base path includes a virtual |
| 1559 | /// base class. |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1560 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 1561 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 1562 | BEnd = BasePath.end(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 1563 | B != BEnd; ++B) |
| 1564 | if ((*B)->isVirtual()) |
| 1565 | return true; |
| 1566 | |
| 1567 | return false; |
| 1568 | } |
| 1569 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1570 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 1571 | /// conversion (where Derived and Base are class types) is |
| 1572 | /// well-formed, meaning that the conversion is unambiguous (and |
| 1573 | /// that all of the base classes are accessible). Returns true |
| 1574 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 1575 | /// otherwise. Loc is the location where this routine should point to |
| 1576 | /// if there is an error, and Range is the source range to highlight |
| 1577 | /// if there is an error. |
| 1578 | bool |
| 1579 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1580 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1581 | unsigned AmbigiousBaseConvID, |
| 1582 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1583 | DeclarationName Name, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1584 | CXXCastPath *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1585 | // First, determine whether the path from Derived to Base is |
| 1586 | // ambiguous. This is slightly more expensive than checking whether |
| 1587 | // the Derived to Base conversion exists, because here we need to |
| 1588 | // explore multiple paths to determine if there is an ambiguity. |
| 1589 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1590 | /*DetectVirtual=*/false); |
| 1591 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1592 | assert(DerivationOkay && |
| 1593 | "Can only be used with a derived-to-base conversion"); |
| 1594 | (void)DerivationOkay; |
| 1595 | |
| 1596 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1597 | if (InaccessibleBaseID) { |
| 1598 | // Check that the base class can be accessed. |
| 1599 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 1600 | InaccessibleBaseID)) { |
| 1601 | case AR_inaccessible: |
| 1602 | return true; |
| 1603 | case AR_accessible: |
| 1604 | case AR_dependent: |
| 1605 | case AR_delayed: |
| 1606 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1607 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 1608 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1609 | |
| 1610 | // Build a base path if necessary. |
| 1611 | if (BasePath) |
| 1612 | BuildBasePathArray(Paths, *BasePath); |
| 1613 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
David Majnemer | 2f68669 | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 1616 | if (AmbigiousBaseConvID) { |
| 1617 | // We know that the derived-to-base conversion is ambiguous, and |
| 1618 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 1619 | // search just one more time to compute all of the possible paths so |
| 1620 | // that we can print them out. This is more expensive than any of |
| 1621 | // the previous derived-to-base checks we've done, but at this point |
| 1622 | // performance isn't as much of an issue. |
| 1623 | Paths.clear(); |
| 1624 | Paths.setRecordingPaths(true); |
| 1625 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 1626 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 1627 | (void)StillOkay; |
| 1628 | |
| 1629 | // Build up a textual representation of the ambiguous paths, e.g., |
| 1630 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 1631 | // conversions in the diagnostic. We only print one of the paths |
| 1632 | // to each base class subobject. |
| 1633 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1634 | |
| 1635 | Diag(Loc, AmbigiousBaseConvID) |
| 1636 | << Derived << Base << PathDisplayStr << Range << Name; |
| 1637 | } |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1638 | return true; |
| 1639 | } |
| 1640 | |
| 1641 | bool |
| 1642 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1643 | SourceLocation Loc, SourceRange Range, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1644 | CXXCastPath *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1645 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1646 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 1647 | IgnoreAccess ? 0 |
| 1648 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1649 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 1650 | Loc, Range, DeclarationName(), |
| 1651 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | |
| 1655 | /// @brief Builds a string representing ambiguous paths from a |
| 1656 | /// specific derived class to different subobjects of the same base |
| 1657 | /// class. |
| 1658 | /// |
| 1659 | /// This function builds a string that can be used in error messages |
| 1660 | /// to show the different paths that one can take through the |
| 1661 | /// inheritance hierarchy to go from the derived class to different |
| 1662 | /// subobjects of a base class. The result looks something like this: |
| 1663 | /// @code |
| 1664 | /// struct D -> struct B -> struct A |
| 1665 | /// struct D -> struct C -> struct A |
| 1666 | /// @endcode |
| 1667 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 1668 | std::string PathDisplayStr; |
| 1669 | std::set<unsigned> DisplayedPaths; |
| 1670 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1671 | Path != Paths.end(); ++Path) { |
| 1672 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 1673 | // We haven't displayed a path to this particular base |
| 1674 | // class subobject yet. |
| 1675 | PathDisplayStr += "\n "; |
| 1676 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 1677 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 1678 | Element != Path->end(); ++Element) |
| 1679 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | return PathDisplayStr; |
| 1684 | } |
| 1685 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1686 | //===----------------------------------------------------------------------===// |
| 1687 | // C++ class member Handling |
| 1688 | //===----------------------------------------------------------------------===// |
| 1689 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1690 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1691 | bool Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 1692 | SourceLocation ASLoc, |
| 1693 | SourceLocation ColonLoc, |
| 1694 | AttributeList *Attrs) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1695 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1696 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1697 | ASLoc, ColonLoc); |
| 1698 | CurContext->addHiddenDecl(ASDecl); |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 1699 | return ProcessAccessDeclAttributeList(ASDecl, Attrs); |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1702 | /// CheckOverrideControl - Check C++11 override control semantics. |
| 1703 | void Sema::CheckOverrideControl(Decl *D) { |
Richard Smith | cddbc1d | 2012-09-06 18:32:18 +0000 | [diff] [blame] | 1704 | if (D->isInvalidDecl()) |
| 1705 | return; |
| 1706 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1707 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1708 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1709 | // Do we know which functions this declaration might be overriding? |
| 1710 | bool OverridesAreKnown = !MD || |
| 1711 | (!MD->getParent()->hasAnyDependentBases() && |
| 1712 | !MD->getType()->isDependentType()); |
Anders Carlsson | 3ffe183 | 2011-01-20 06:33:26 +0000 | [diff] [blame] | 1713 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1714 | if (!MD || !MD->isVirtual()) { |
| 1715 | if (OverridesAreKnown) { |
| 1716 | if (OverrideAttr *OA = D->getAttr<OverrideAttr>()) { |
| 1717 | Diag(OA->getLocation(), |
| 1718 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1719 | << "override" << FixItHint::CreateRemoval(OA->getLocation()); |
| 1720 | D->dropAttr<OverrideAttr>(); |
| 1721 | } |
| 1722 | if (FinalAttr *FA = D->getAttr<FinalAttr>()) { |
| 1723 | Diag(FA->getLocation(), |
| 1724 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1725 | << "final" << FixItHint::CreateRemoval(FA->getLocation()); |
| 1726 | D->dropAttr<FinalAttr>(); |
| 1727 | } |
| 1728 | } |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1729 | return; |
| 1730 | } |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1731 | |
| 1732 | if (!OverridesAreKnown) |
| 1733 | return; |
| 1734 | |
| 1735 | // C++11 [class.virtual]p5: |
| 1736 | // If a virtual function is marked with the virt-specifier override and |
| 1737 | // does not override a member function of a base class, the program is |
| 1738 | // ill-formed. |
| 1739 | bool HasOverriddenMethods = |
| 1740 | MD->begin_overridden_methods() != MD->end_overridden_methods(); |
| 1741 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) |
| 1742 | Diag(MD->getLocation(), diag::err_function_marked_override_not_overriding) |
| 1743 | << MD->getDeclName(); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1746 | /// CheckIfOverriddenFunctionIsMarkedFinal - Checks whether a virtual member |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1747 | /// function overrides a virtual member function marked 'final', according to |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 1748 | /// C++11 [class.virtual]p4. |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1749 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 1750 | const CXXMethodDecl *Old) { |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1751 | if (!Old->hasAttr<FinalAttr>()) |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 1752 | return false; |
| 1753 | |
| 1754 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 1755 | << New->getDeclName(); |
| 1756 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 1757 | return true; |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1760 | static bool InitializationHasSideEffects(const FieldDecl &FD) { |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 1761 | const Type *T = FD.getType()->getBaseElementTypeUnsafe(); |
| 1762 | // FIXME: Destruction of ObjC lifetime types has side-effects. |
| 1763 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 1764 | return !RD->isCompleteDefinition() || |
| 1765 | !RD->hasTrivialDefaultConstructor() || |
| 1766 | !RD->hasTrivialDestructor(); |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 1767 | return false; |
| 1768 | } |
| 1769 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1770 | static AttributeList *getMSPropertyAttr(AttributeList *list) { |
| 1771 | for (AttributeList* it = list; it != 0; it = it->getNext()) |
| 1772 | if (it->isDeclspecPropertyAttribute()) |
| 1773 | return it; |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1777 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 1778 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1779 | /// bitfield width if there is one, 'InitExpr' specifies the initializer if |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1780 | /// one has been parsed, and 'InitStyle' is set if an in-class initializer is |
| 1781 | /// present (but parsing it has been deferred). |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 1782 | NamedDecl * |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1783 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1784 | MultiTemplateParamsArg TemplateParameterLists, |
Richard Trieu | f81e5a9 | 2011-09-09 02:00:50 +0000 | [diff] [blame] | 1785 | Expr *BW, const VirtSpecifiers &VS, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 1786 | InClassInitStyle InitStyle) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1787 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1788 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 1789 | DeclarationName Name = NameInfo.getName(); |
| 1790 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1791 | |
| 1792 | // For anonymous bitfields, the location should point to the type. |
| 1793 | if (Loc.isInvalid()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1794 | Loc = D.getLocStart(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 1795 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1796 | Expr *BitWidth = static_cast<Expr*>(BW); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1797 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1798 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1799 | assert(!DS.isFriendSpecified()); |
| 1800 | |
Richard Smith | 1ab0d90 | 2011-06-25 02:28:38 +0000 | [diff] [blame] | 1801 | bool isFunc = D.isDeclarationOfFunction(); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 1802 | |
John McCall | e402e72 | 2012-09-25 07:32:39 +0000 | [diff] [blame] | 1803 | if (cast<CXXRecordDecl>(CurContext)->isInterface()) { |
| 1804 | // The Microsoft extension __interface only permits public member functions |
| 1805 | // and prohibits constructors, destructors, operators, non-public member |
| 1806 | // functions, static methods and data members. |
| 1807 | unsigned InvalidDecl; |
| 1808 | bool ShowDeclName = true; |
| 1809 | if (!isFunc) |
| 1810 | InvalidDecl = (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) ? 0 : 1; |
| 1811 | else if (AS != AS_public) |
| 1812 | InvalidDecl = 2; |
| 1813 | else if (DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 1814 | InvalidDecl = 3; |
| 1815 | else switch (Name.getNameKind()) { |
| 1816 | case DeclarationName::CXXConstructorName: |
| 1817 | InvalidDecl = 4; |
| 1818 | ShowDeclName = false; |
| 1819 | break; |
| 1820 | |
| 1821 | case DeclarationName::CXXDestructorName: |
| 1822 | InvalidDecl = 5; |
| 1823 | ShowDeclName = false; |
| 1824 | break; |
| 1825 | |
| 1826 | case DeclarationName::CXXOperatorName: |
| 1827 | case DeclarationName::CXXConversionFunctionName: |
| 1828 | InvalidDecl = 6; |
| 1829 | break; |
| 1830 | |
| 1831 | default: |
| 1832 | InvalidDecl = 0; |
| 1833 | break; |
| 1834 | } |
| 1835 | |
| 1836 | if (InvalidDecl) { |
| 1837 | if (ShowDeclName) |
| 1838 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 1839 | << (InvalidDecl-1) << Name; |
| 1840 | else |
| 1841 | Diag(Loc, diag::err_invalid_member_in_interface) |
| 1842 | << (InvalidDecl-1) << ""; |
| 1843 | return 0; |
| 1844 | } |
| 1845 | } |
| 1846 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1847 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 1848 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1849 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 1850 | // data members and cannot be applied to names declared const or static, |
| 1851 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1852 | switch (DS.getStorageClassSpec()) { |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1853 | case DeclSpec::SCS_unspecified: |
| 1854 | case DeclSpec::SCS_typedef: |
| 1855 | case DeclSpec::SCS_static: |
| 1856 | break; |
| 1857 | case DeclSpec::SCS_mutable: |
| 1858 | if (isFunc) { |
| 1859 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1861 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 1862 | // declarator. Otherwise we could get follow-up errors. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1863 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1864 | } |
| 1865 | break; |
| 1866 | default: |
| 1867 | Diag(DS.getStorageClassSpecLoc(), |
| 1868 | diag::err_storageclass_invalid_for_member); |
| 1869 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 1870 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1873 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 1874 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 1875 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1876 | |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1877 | if (DS.isConstexprSpecified() && isInstField) { |
| 1878 | SemaDiagnosticBuilder B = |
| 1879 | Diag(DS.getConstexprSpecLoc(), diag::err_invalid_constexpr_member); |
| 1880 | SourceLocation ConstexprLoc = DS.getConstexprSpecLoc(); |
| 1881 | if (InitStyle == ICIS_NoInit) { |
| 1882 | B << 0 << 0 << FixItHint::CreateReplacement(ConstexprLoc, "const"); |
| 1883 | D.getMutableDeclSpec().ClearConstexprSpec(); |
| 1884 | const char *PrevSpec; |
| 1885 | unsigned DiagID; |
| 1886 | bool Failed = D.getMutableDeclSpec().SetTypeQual(DeclSpec::TQ_const, ConstexprLoc, |
| 1887 | PrevSpec, DiagID, getLangOpts()); |
Matt Beaumont-Gay | 3e55e3e | 2013-01-31 00:08:03 +0000 | [diff] [blame] | 1888 | (void)Failed; |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1889 | assert(!Failed && "Making a constexpr member const shouldn't fail"); |
| 1890 | } else { |
| 1891 | B << 1; |
| 1892 | const char *PrevSpec; |
| 1893 | unsigned DiagID; |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1894 | if (D.getMutableDeclSpec().SetStorageClassSpec( |
| 1895 | *this, DeclSpec::SCS_static, ConstexprLoc, PrevSpec, DiagID)) { |
Matt Beaumont-Gay | 3e55e3e | 2013-01-31 00:08:03 +0000 | [diff] [blame] | 1896 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_mutable && |
David Blaikie | 1d87fba | 2013-01-30 01:22:18 +0000 | [diff] [blame] | 1897 | "This is the only DeclSpec that should fail to be applied"); |
| 1898 | B << 1; |
| 1899 | } else { |
| 1900 | B << 0 << FixItHint::CreateInsertion(ConstexprLoc, "static "); |
| 1901 | isInstField = false; |
| 1902 | } |
| 1903 | } |
| 1904 | } |
| 1905 | |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 1906 | NamedDecl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1907 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1908 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 1909 | |
| 1910 | // Data members must have identifiers for names. |
Benjamin Kramer | c1aa40c | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1911 | if (!Name.isIdentifier()) { |
Douglas Gregor | b5a0187 | 2011-10-09 18:55:59 +0000 | [diff] [blame] | 1912 | Diag(Loc, diag::err_bad_variable_name) |
| 1913 | << Name; |
| 1914 | return 0; |
| 1915 | } |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1916 | |
Benjamin Kramer | c1aa40c | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1917 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 1918 | |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1919 | // Member field could not be with "template" keyword. |
| 1920 | // So TemplateParameterLists should be empty in this case. |
| 1921 | if (TemplateParameterLists.size()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1922 | TemplateParameterList* TemplateParams = TemplateParameterLists[0]; |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1923 | if (TemplateParams->size()) { |
| 1924 | // There is no such thing as a member field template. |
| 1925 | Diag(D.getIdentifierLoc(), diag::err_template_member) |
| 1926 | << II |
| 1927 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 1928 | TemplateParams->getRAngleLoc()); |
| 1929 | } else { |
| 1930 | // There is an extraneous 'template<>' for this member. |
| 1931 | Diag(TemplateParams->getTemplateLoc(), |
| 1932 | diag::err_template_member_noparams) |
| 1933 | << II |
| 1934 | << SourceRange(TemplateParams->getTemplateLoc(), |
| 1935 | TemplateParams->getRAngleLoc()); |
| 1936 | } |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1940 | if (SS.isSet() && !SS.isInvalid()) { |
| 1941 | // The user provided a superfluous scope specifier inside a class |
| 1942 | // definition: |
| 1943 | // |
| 1944 | // class X { |
| 1945 | // int X::member; |
| 1946 | // }; |
Douglas Gregor | 6960587 | 2012-03-28 16:01:27 +0000 | [diff] [blame] | 1947 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 1948 | diagnoseQualifiedDeclaration(SS, DC, Name, D.getIdentifierLoc()); |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1949 | else |
| 1950 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 1951 | << Name << SS.getRange(); |
Douglas Gregor | 5d8419c | 2011-11-01 22:13:30 +0000 | [diff] [blame] | 1952 | |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1953 | SS.clear(); |
| 1954 | } |
Douglas Gregor | f250365 | 2011-09-21 14:40:46 +0000 | [diff] [blame] | 1955 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1956 | AttributeList *MSPropertyAttr = |
| 1957 | getMSPropertyAttr(D.getDeclSpec().getAttributes().getList()); |
Eli Friedman | b26f012 | 2013-06-28 20:48:34 +0000 | [diff] [blame] | 1958 | if (MSPropertyAttr) { |
| 1959 | Member = HandleMSProperty(S, cast<CXXRecordDecl>(CurContext), Loc, D, |
| 1960 | BitWidth, InitStyle, AS, MSPropertyAttr); |
| 1961 | if (!Member) |
| 1962 | return 0; |
| 1963 | isInstField = false; |
| 1964 | } else { |
| 1965 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, |
| 1966 | BitWidth, InitStyle, AS); |
| 1967 | assert(Member && "HandleField never returns null"); |
| 1968 | } |
| 1969 | } else { |
| 1970 | assert(InitStyle == ICIS_NoInit || D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static); |
| 1971 | |
| 1972 | Member = HandleDeclarator(S, D, TemplateParameterLists); |
| 1973 | if (!Member) |
| 1974 | return 0; |
| 1975 | |
| 1976 | // Non-instance-fields can't have a bitfield. |
| 1977 | if (BitWidth) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1978 | if (Member->isInvalidDecl()) { |
| 1979 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 1980 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1981 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 1982 | // "static member 'A' cannot be a bit-field" |
| 1983 | Diag(Loc, diag::err_static_not_bitfield) |
| 1984 | << Name << BitWidth->getSourceRange(); |
| 1985 | } else if (isa<TypedefDecl>(Member)) { |
| 1986 | // "typedef member 'x' cannot be a bit-field" |
| 1987 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 1988 | << Name << BitWidth->getSourceRange(); |
| 1989 | } else { |
| 1990 | // A function typedef ("typedef int f(); f a;"). |
| 1991 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 1992 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1994 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1995 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1996 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1997 | BitWidth = 0; |
| 1998 | Member->setInvalidDecl(); |
| 1999 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 2000 | |
| 2001 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2003 | // If we have declared a member function template or static data member |
| 2004 | // template, set the access of the templated declaration as well. |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 2005 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 2006 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2007 | else if (VarTemplateDecl *VarTmpl = dyn_cast<VarTemplateDecl>(Member)) |
| 2008 | VarTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 2009 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2010 | |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2011 | if (VS.isOverrideSpecified()) |
| 2012 | Member->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context)); |
| 2013 | if (VS.isFinalSpecified()) |
| 2014 | Member->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context)); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 2015 | |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2016 | if (VS.getLastLocation().isValid()) { |
| 2017 | // Update the end location of a method that has a virt-specifiers. |
| 2018 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 2019 | MD->setRangeEnd(VS.getLastLocation()); |
| 2020 | } |
Richard Smith | a4b3965 | 2012-08-06 03:25:17 +0000 | [diff] [blame] | 2021 | |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 2022 | CheckOverrideControl(Member); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 2023 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 2024 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2025 | |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2026 | if (isInstField) { |
| 2027 | FieldDecl *FD = cast<FieldDecl>(Member); |
| 2028 | FieldCollector->Add(FD); |
| 2029 | |
| 2030 | if (Diags.getDiagnosticLevel(diag::warn_unused_private_field, |
| 2031 | FD->getLocation()) |
| 2032 | != DiagnosticsEngine::Ignored) { |
| 2033 | // Remember all explicit private FieldDecls that have a name, no side |
| 2034 | // effects and are not part of a dependent type declaration. |
| 2035 | if (!FD->isImplicit() && FD->getDeclName() && |
| 2036 | FD->getAccess() == AS_private && |
Daniel Jasper | 568eae4 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 2037 | !FD->hasAttr<UnusedAttr>() && |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 2038 | !FD->getParent()->isDependentContext() && |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2039 | !InitializationHasSideEffects(*FD)) |
| 2040 | UnusedPrivateFields.insert(FD); |
| 2041 | } |
| 2042 | } |
| 2043 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2044 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2047 | namespace { |
| 2048 | class UninitializedFieldVisitor |
| 2049 | : public EvaluatedExprVisitor<UninitializedFieldVisitor> { |
| 2050 | Sema &S; |
| 2051 | ValueDecl *VD; |
| 2052 | public: |
| 2053 | typedef EvaluatedExprVisitor<UninitializedFieldVisitor> Inherited; |
| 2054 | UninitializedFieldVisitor(Sema &S, ValueDecl *VD) : Inherited(S.Context), |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2055 | S(S) { |
| 2056 | if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(VD)) |
| 2057 | this->VD = IFD->getAnonField(); |
| 2058 | else |
| 2059 | this->VD = VD; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | void HandleExpr(Expr *E) { |
| 2063 | if (!E) return; |
| 2064 | |
| 2065 | // Expressions like x(x) sometimes lack the surrounding expressions |
| 2066 | // but need to be checked anyways. |
| 2067 | HandleValue(E); |
| 2068 | Visit(E); |
| 2069 | } |
| 2070 | |
| 2071 | void HandleValue(Expr *E) { |
| 2072 | E = E->IgnoreParens(); |
| 2073 | |
| 2074 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 2075 | if (isa<EnumConstantDecl>(ME->getMemberDecl())) |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2076 | return; |
| 2077 | |
| 2078 | // FieldME is the inner-most MemberExpr that is not an anonymous struct |
| 2079 | // or union. |
| 2080 | MemberExpr *FieldME = ME; |
| 2081 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2082 | Expr *Base = E; |
| 2083 | while (isa<MemberExpr>(Base)) { |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2084 | ME = cast<MemberExpr>(Base); |
| 2085 | |
| 2086 | if (isa<VarDecl>(ME->getMemberDecl())) |
| 2087 | return; |
| 2088 | |
| 2089 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2090 | if (!FD->isAnonymousStructOrUnion()) |
| 2091 | FieldME = ME; |
| 2092 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2093 | Base = ME->getBase(); |
| 2094 | } |
| 2095 | |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2096 | if (VD == FieldME->getMemberDecl() && isa<CXXThisExpr>(Base)) { |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2097 | unsigned diag = VD->getType()->isReferenceType() |
| 2098 | ? diag::warn_reference_field_is_uninit |
| 2099 | : diag::warn_field_is_uninit; |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2100 | S.Diag(FieldME->getExprLoc(), diag) << VD; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2101 | } |
Nick Lewycky | 621ba4f | 2012-11-15 08:19:20 +0000 | [diff] [blame] | 2102 | return; |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 2106 | HandleValue(CO->getTrueExpr()); |
| 2107 | HandleValue(CO->getFalseExpr()); |
| 2108 | return; |
| 2109 | } |
| 2110 | |
| 2111 | if (BinaryConditionalOperator *BCO = |
| 2112 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 2113 | HandleValue(BCO->getCommon()); |
| 2114 | HandleValue(BCO->getFalseExpr()); |
| 2115 | return; |
| 2116 | } |
| 2117 | |
| 2118 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 2119 | switch (BO->getOpcode()) { |
| 2120 | default: |
| 2121 | return; |
| 2122 | case(BO_PtrMemD): |
| 2123 | case(BO_PtrMemI): |
| 2124 | HandleValue(BO->getLHS()); |
| 2125 | return; |
| 2126 | case(BO_Comma): |
| 2127 | HandleValue(BO->getRHS()); |
| 2128 | return; |
| 2129 | } |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 2134 | if (E->getCastKind() == CK_LValueToRValue) |
| 2135 | HandleValue(E->getSubExpr()); |
| 2136 | |
| 2137 | Inherited::VisitImplicitCastExpr(E); |
| 2138 | } |
| 2139 | |
| 2140 | void VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { |
| 2141 | Expr *Callee = E->getCallee(); |
| 2142 | if (isa<MemberExpr>(Callee)) |
| 2143 | HandleValue(Callee); |
| 2144 | |
| 2145 | Inherited::VisitCXXMemberCallExpr(E); |
| 2146 | } |
| 2147 | }; |
| 2148 | static void CheckInitExprContainsUninitializedFields(Sema &S, Expr *E, |
| 2149 | ValueDecl *VD) { |
| 2150 | UninitializedFieldVisitor(S, VD).HandleExpr(E); |
| 2151 | } |
| 2152 | } // namespace |
| 2153 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2154 | /// ActOnCXXInClassMemberInitializer - This is invoked after parsing an |
Richard Smith | 0ff6f8f | 2011-07-20 00:12:52 +0000 | [diff] [blame] | 2155 | /// in-class initializer for a non-static C++ class member, and after |
| 2156 | /// instantiating an in-class initializer in a class template. Such actions |
| 2157 | /// are deferred until the class is complete. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2158 | void |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2159 | Sema::ActOnCXXInClassMemberInitializer(Decl *D, SourceLocation InitLoc, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2160 | Expr *InitExpr) { |
| 2161 | FieldDecl *FD = cast<FieldDecl>(D); |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2162 | assert(FD->getInClassInitStyle() != ICIS_NoInit && |
| 2163 | "must set init style when field is created"); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2164 | |
| 2165 | if (!InitExpr) { |
| 2166 | FD->setInvalidDecl(); |
| 2167 | FD->removeInClassInitializer(); |
| 2168 | return; |
| 2169 | } |
| 2170 | |
Peter Collingbourne | fef2189 | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 2171 | if (DiagnoseUnexpandedParameterPack(InitExpr, UPPC_Initializer)) { |
| 2172 | FD->setInvalidDecl(); |
| 2173 | FD->removeInClassInitializer(); |
| 2174 | return; |
| 2175 | } |
| 2176 | |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2177 | if (getDiagnostics().getDiagnosticLevel(diag::warn_field_is_uninit, InitLoc) |
| 2178 | != DiagnosticsEngine::Ignored) { |
| 2179 | CheckInitExprContainsUninitializedFields(*this, InitExpr, FD); |
| 2180 | } |
| 2181 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2182 | ExprResult Init = InitExpr; |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2183 | if (!FD->getType()->isDependentType() && !InitExpr->isTypeDependent()) { |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2184 | InitializedEntity Entity = InitializedEntity::InitializeMember(FD); |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2185 | InitializationKind Kind = FD->getInClassInitStyle() == ICIS_ListInit |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 2186 | ? InitializationKind::CreateDirectList(InitExpr->getLocStart()) |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2187 | : InitializationKind::CreateCopy(InitExpr->getLocStart(), InitLoc); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2188 | InitializationSequence Seq(*this, Entity, Kind, InitExpr); |
| 2189 | Init = Seq.Perform(*this, Entity, Kind, InitExpr); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2190 | if (Init.isInvalid()) { |
| 2191 | FD->setInvalidDecl(); |
| 2192 | return; |
| 2193 | } |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2196 | // C++11 [class.base.init]p7: |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2197 | // The initialization of each base and member constitutes a |
| 2198 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2199 | Init = ActOnFinishFullExpr(Init.take(), InitLoc); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2200 | if (Init.isInvalid()) { |
| 2201 | FD->setInvalidDecl(); |
| 2202 | return; |
| 2203 | } |
| 2204 | |
| 2205 | InitExpr = Init.release(); |
| 2206 | |
| 2207 | FD->setInClassInitializer(InitExpr); |
| 2208 | } |
| 2209 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2210 | /// \brief Find the direct and/or virtual base specifiers that |
| 2211 | /// correspond to the given base type, for use in base initialization |
| 2212 | /// within a constructor. |
| 2213 | static bool FindBaseInitializer(Sema &SemaRef, |
| 2214 | CXXRecordDecl *ClassDecl, |
| 2215 | QualType BaseType, |
| 2216 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 2217 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 2218 | // First, check for a direct base class. |
| 2219 | DirectBaseSpec = 0; |
| 2220 | for (CXXRecordDecl::base_class_const_iterator Base |
| 2221 | = ClassDecl->bases_begin(); |
| 2222 | Base != ClassDecl->bases_end(); ++Base) { |
| 2223 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 2224 | // We found a direct base of this type. That's what we're |
| 2225 | // initializing. |
| 2226 | DirectBaseSpec = &*Base; |
| 2227 | break; |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | // Check for a virtual base class. |
| 2232 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 2233 | // there are no virtual bases. |
| 2234 | VirtualBaseSpec = 0; |
| 2235 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 2236 | // We haven't found a base yet; search the class hierarchy for a |
| 2237 | // virtual base class. |
| 2238 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 2239 | /*DetectVirtual=*/false); |
| 2240 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 2241 | BaseType, Paths)) { |
| 2242 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 2243 | Path != Paths.end(); ++Path) { |
| 2244 | if (Path->back().Base->isVirtual()) { |
| 2245 | VirtualBaseSpec = Path->back().Base; |
| 2246 | break; |
| 2247 | } |
| 2248 | } |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | return DirectBaseSpec || VirtualBaseSpec; |
| 2253 | } |
| 2254 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2255 | /// \brief Handle a C++ member initializer using braced-init-list syntax. |
| 2256 | MemInitResult |
| 2257 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
| 2258 | Scope *S, |
| 2259 | CXXScopeSpec &SS, |
| 2260 | IdentifierInfo *MemberOrBase, |
| 2261 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2262 | const DeclSpec &DS, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2263 | SourceLocation IdLoc, |
| 2264 | Expr *InitList, |
| 2265 | SourceLocation EllipsisLoc) { |
| 2266 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2267 | DS, IdLoc, InitList, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2268 | EllipsisLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | /// \brief Handle a C++ member initializer using parentheses syntax. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2272 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2273 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2274 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2275 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2276 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2277 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2278 | const DeclSpec &DS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2279 | SourceLocation IdLoc, |
| 2280 | SourceLocation LParenLoc, |
Dmitri Gribenko | a36bbac | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 2281 | ArrayRef<Expr *> Args, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2282 | SourceLocation RParenLoc, |
| 2283 | SourceLocation EllipsisLoc) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2284 | Expr *List = new (Context) ParenListExpr(Context, LParenLoc, |
Dmitri Gribenko | a36bbac | 2013-05-09 23:51:52 +0000 | [diff] [blame] | 2285 | Args, RParenLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2286 | return BuildMemInitializer(ConstructorD, S, SS, MemberOrBase, TemplateTypeTy, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2287 | DS, IdLoc, List, EllipsisLoc); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2290 | namespace { |
| 2291 | |
Kaelyn Uhrain | dc98cd0 | 2012-01-11 21:17:51 +0000 | [diff] [blame] | 2292 | // Callback to only accept typo corrections that can be a valid C++ member |
| 2293 | // intializer: either a non-static field member or a base class. |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2294 | class MemInitializerValidatorCCC : public CorrectionCandidateCallback { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2295 | public: |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2296 | explicit MemInitializerValidatorCCC(CXXRecordDecl *ClassDecl) |
| 2297 | : ClassDecl(ClassDecl) {} |
| 2298 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2299 | bool ValidateCandidate(const TypoCorrection &candidate) LLVM_OVERRIDE { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2300 | if (NamedDecl *ND = candidate.getCorrectionDecl()) { |
| 2301 | if (FieldDecl *Member = dyn_cast<FieldDecl>(ND)) |
| 2302 | return Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl); |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2303 | return isa<TypeDecl>(ND); |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2304 | } |
| 2305 | return false; |
| 2306 | } |
| 2307 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2308 | private: |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2309 | CXXRecordDecl *ClassDecl; |
| 2310 | }; |
| 2311 | |
| 2312 | } |
| 2313 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2314 | /// \brief Handle a C++ member initializer. |
| 2315 | MemInitResult |
| 2316 | Sema::BuildMemInitializer(Decl *ConstructorD, |
| 2317 | Scope *S, |
| 2318 | CXXScopeSpec &SS, |
| 2319 | IdentifierInfo *MemberOrBase, |
| 2320 | ParsedType TemplateTypeTy, |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2321 | const DeclSpec &DS, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2322 | SourceLocation IdLoc, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2323 | Expr *Init, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2324 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2325 | if (!ConstructorD) |
| 2326 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2327 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2328 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | |
| 2330 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2331 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2332 | if (!Constructor) { |
| 2333 | // The user wrote a constructor initializer on a function that is |
| 2334 | // not a C++ constructor. Ignore the error for now, because we may |
| 2335 | // have more member initializers coming; we'll diagnose it just |
| 2336 | // once in ActOnMemInitializers. |
| 2337 | return true; |
| 2338 | } |
| 2339 | |
| 2340 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2341 | |
| 2342 | // C++ [class.base.init]p2: |
| 2343 | // 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] | 2344 | // constructor's class and, if not found in that scope, are looked |
| 2345 | // up in the scope containing the constructor's definition. |
| 2346 | // [Note: if the constructor's class contains a member with the |
| 2347 | // same name as a direct or virtual base class of the class, a |
| 2348 | // mem-initializer-id naming the member or base class and composed |
| 2349 | // of a single identifier refers to the class member. A |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2350 | // mem-initializer-id for the hidden base class may be specified |
| 2351 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 2352 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2353 | // Look for a member, first. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2354 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 2355 | = ClassDecl->lookup(MemberOrBase); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2356 | if (!Result.empty()) { |
Peter Collingbourne | dc69be2 | 2011-10-23 18:59:37 +0000 | [diff] [blame] | 2357 | ValueDecl *Member; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2358 | if ((Member = dyn_cast<FieldDecl>(Result.front())) || |
| 2359 | (Member = dyn_cast<IndirectFieldDecl>(Result.front()))) { |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2360 | if (EllipsisLoc.isValid()) |
| 2361 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2362 | << MemberOrBase |
| 2363 | << SourceRange(IdLoc, Init->getSourceRange().getEnd()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2364 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2365 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2366 | } |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2367 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2368 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2369 | // 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] | 2370 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2371 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2372 | |
| 2373 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2374 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
David Blaikie | f211662 | 2012-01-24 06:03:59 +0000 | [diff] [blame] | 2375 | } else if (DS.getTypeSpecType() == TST_decltype) { |
| 2376 | BaseType = BuildDecltypeType(DS.getRepAsExpr(), DS.getTypeSpecTypeLoc()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2377 | } else { |
| 2378 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 2379 | LookupParsedName(R, S, &SS); |
| 2380 | |
| 2381 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 2382 | if (!TyD) { |
| 2383 | if (R.isAmbiguous()) return true; |
| 2384 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 2385 | // We don't want access-control diagnostics here. |
| 2386 | R.suppressDiagnostics(); |
| 2387 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2388 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 2389 | bool NotUnknownSpecialization = false; |
| 2390 | DeclContext *DC = computeDeclContext(SS, false); |
| 2391 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 2392 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 2393 | |
| 2394 | if (!NotUnknownSpecialization) { |
| 2395 | // When the scope specifier can refer to a member of an unknown |
| 2396 | // specialization, we take it as a type name. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 2397 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 2398 | SS.getWithLocInContext(Context), |
| 2399 | *MemberOrBase, IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 2400 | if (BaseType.isNull()) |
| 2401 | return true; |
| 2402 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2403 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 2404 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2405 | } |
| 2406 | } |
| 2407 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2408 | // If no results were found, try to correct typos. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2409 | TypoCorrection Corr; |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2410 | MemInitializerValidatorCCC Validator(ClassDecl); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2411 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2412 | (Corr = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 2413 | Validator, ClassDecl))) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2414 | if (FieldDecl *Member = Corr.getCorrectionDeclAs<FieldDecl>()) { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 2415 | // We have found a non-static data member with a similar |
| 2416 | // name to what was typed; complain and initialize that |
| 2417 | // member. |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2418 | diagnoseTypo(Corr, |
| 2419 | PDiag(diag::err_mem_init_not_member_or_class_suggest) |
| 2420 | << MemberOrBase << true); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2421 | return BuildMemberInitializer(Member, Init, IdLoc); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 2422 | } else if (TypeDecl *Type = Corr.getCorrectionDeclAs<TypeDecl>()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2423 | const CXXBaseSpecifier *DirectBaseSpec; |
| 2424 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 2425 | if (FindBaseInitializer(*this, ClassDecl, |
| 2426 | Context.getTypeDeclType(Type), |
| 2427 | DirectBaseSpec, VirtualBaseSpec)) { |
| 2428 | // We have found a direct or virtual base class with a |
| 2429 | // similar name to what was typed; complain and initialize |
| 2430 | // that base class. |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2431 | diagnoseTypo(Corr, |
| 2432 | PDiag(diag::err_mem_init_not_member_or_class_suggest) |
| 2433 | << MemberOrBase << false, |
| 2434 | PDiag() /*Suppress note, we provide our own.*/); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2435 | |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2436 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec ? DirectBaseSpec |
| 2437 | : VirtualBaseSpec; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2438 | Diag(BaseSpec->getLocStart(), |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 2439 | diag::note_base_class_specified_here) |
| 2440 | << BaseSpec->getType() |
| 2441 | << BaseSpec->getSourceRange(); |
| 2442 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2443 | TyD = Type; |
| 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2448 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2449 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2450 | << MemberOrBase << SourceRange(IdLoc,Init->getSourceRange().getEnd()); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 2451 | return true; |
| 2452 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2455 | if (BaseType.isNull()) { |
| 2456 | BaseType = Context.getTypeDeclType(TyD); |
| 2457 | if (SS.isSet()) { |
| 2458 | NestedNameSpecifier *Qualifier = |
| 2459 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2460 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2461 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2462 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 2463 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 2464 | } |
| 2465 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2466 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2467 | if (!TInfo) |
| 2468 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2469 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2470 | return BuildBaseInitializer(BaseType, TInfo, Init, ClassDecl, EllipsisLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2473 | /// Checks a member initializer expression for cases where reference (or |
| 2474 | /// pointer) members are bound to by-value parameters (or their addresses). |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2475 | static void CheckForDanglingReferenceOrPointer(Sema &S, ValueDecl *Member, |
| 2476 | Expr *Init, |
| 2477 | SourceLocation IdLoc) { |
| 2478 | QualType MemberTy = Member->getType(); |
| 2479 | |
| 2480 | // We only handle pointers and references currently. |
| 2481 | // FIXME: Would this be relevant for ObjC object pointers? Or block pointers? |
| 2482 | if (!MemberTy->isReferenceType() && !MemberTy->isPointerType()) |
| 2483 | return; |
| 2484 | |
| 2485 | const bool IsPointer = MemberTy->isPointerType(); |
| 2486 | if (IsPointer) { |
| 2487 | if (const UnaryOperator *Op |
| 2488 | = dyn_cast<UnaryOperator>(Init->IgnoreParenImpCasts())) { |
| 2489 | // The only case we're worried about with pointers requires taking the |
| 2490 | // address. |
| 2491 | if (Op->getOpcode() != UO_AddrOf) |
| 2492 | return; |
| 2493 | |
| 2494 | Init = Op->getSubExpr(); |
| 2495 | } else { |
| 2496 | // We only handle address-of expression initializers for pointers. |
| 2497 | return; |
| 2498 | } |
| 2499 | } |
| 2500 | |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2501 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Init->IgnoreParens())) { |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2502 | // We only warn when referring to a non-reference parameter declaration. |
| 2503 | const ParmVarDecl *Parameter = dyn_cast<ParmVarDecl>(DRE->getDecl()); |
| 2504 | if (!Parameter || Parameter->getType()->isReferenceType()) |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2505 | return; |
| 2506 | |
| 2507 | S.Diag(Init->getExprLoc(), |
| 2508 | IsPointer ? diag::warn_init_ptr_member_to_parameter_addr |
| 2509 | : diag::warn_bind_ref_member_to_parameter) |
| 2510 | << Member << Parameter << Init->getSourceRange(); |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2511 | } else { |
| 2512 | // Other initializers are fine. |
| 2513 | return; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2514 | } |
Chandler Carruth | bf3380a | 2011-09-03 02:21:57 +0000 | [diff] [blame] | 2515 | |
| 2516 | S.Diag(Member->getLocation(), diag::note_ref_or_ptr_member_declared_here) |
| 2517 | << (unsigned)IsPointer; |
Chandler Carruth | 81c6477 | 2011-09-03 01:14:15 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2520 | MemInitResult |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2521 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr *Init, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2522 | SourceLocation IdLoc) { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2523 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 2524 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 2525 | assert((DirectMember || IndirectMember) && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2526 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 2527 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2528 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Peter Collingbourne | fef2189 | 2011-10-23 18:59:44 +0000 | [diff] [blame] | 2529 | return true; |
| 2530 | |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 2531 | if (Member->isInvalidDecl()) |
| 2532 | return true; |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2533 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2534 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 2535 | // foo(foo) |
| 2536 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 2537 | // TODO: implement -Wuninitialized and fold this into that framework. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2538 | MultiExprArg Args; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2539 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2540 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2541 | } else if (InitListExpr *InitList = dyn_cast<InitListExpr>(Init)) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2542 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2543 | } else { |
| 2544 | // Template instantiation doesn't reconstruct ParenListExprs for us. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2545 | Args = Init; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2546 | } |
Daniel Jasper | f8cc02e | 2012-06-06 08:32:04 +0000 | [diff] [blame] | 2547 | |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 2548 | if (getDiagnostics().getDiagnosticLevel(diag::warn_field_is_uninit, IdLoc) |
| 2549 | != DiagnosticsEngine::Ignored) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2550 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 2551 | // FIXME: Warn about the case when other fields are used before being |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2552 | // initialized. For example, let this field be the i'th field. When |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2553 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 2554 | // fields are used, as they are not yet initialized. |
| 2555 | // Right now we are only handling the case where the i'th field uses |
| 2556 | // itself in its initializer. |
Hans Wennborg | 471f985 | 2012-09-18 15:58:06 +0000 | [diff] [blame] | 2557 | // Also need to take into account that some fields may be initialized by |
| 2558 | // in-class initializers, see C++11 [class.base.init]p9. |
Richard Trieu | de5e75c | 2012-06-14 23:11:34 +0000 | [diff] [blame] | 2559 | CheckInitExprContainsUninitializedFields(*this, Args[i], Member); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 2560 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2561 | SourceRange InitRange = Init->getSourceRange(); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2562 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2563 | if (Member->getType()->isDependentType() || Init->isTypeDependent()) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2564 | // Can't check initialization for a member of dependent type or when |
| 2565 | // any of the arguments are type-dependent expressions. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2566 | DiscardCleanupsInEvaluationContext(); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2567 | } else { |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2568 | bool InitList = false; |
| 2569 | if (isa<InitListExpr>(Init)) { |
| 2570 | InitList = true; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2571 | Args = Init; |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2574 | // Initialize the member. |
| 2575 | InitializedEntity MemberEntity = |
| 2576 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 2577 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 2578 | InitializationKind Kind = |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2579 | InitList ? InitializationKind::CreateDirectList(IdLoc) |
| 2580 | : InitializationKind::CreateDirect(IdLoc, InitRange.getBegin(), |
| 2581 | InitRange.getEnd()); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2582 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2583 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args); |
| 2584 | ExprResult MemberInit = InitSeq.Perform(*this, MemberEntity, Kind, Args, 0); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2585 | if (MemberInit.isInvalid()) |
| 2586 | return true; |
| 2587 | |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 2588 | CheckForDanglingReferenceOrPointer(*this, Member, MemberInit.get(), IdLoc); |
| 2589 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2590 | // C++11 [class.base.init]p7: |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2591 | // The initialization of each base and member constitutes a |
| 2592 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2593 | MemberInit = ActOnFinishFullExpr(MemberInit.get(), InitRange.getBegin()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2594 | if (MemberInit.isInvalid()) |
| 2595 | return true; |
| 2596 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 2597 | Init = MemberInit.get(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2600 | if (DirectMember) { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2601 | return new (Context) CXXCtorInitializer(Context, DirectMember, IdLoc, |
| 2602 | InitRange.getBegin(), Init, |
| 2603 | InitRange.getEnd()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2604 | } else { |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2605 | return new (Context) CXXCtorInitializer(Context, IndirectMember, IdLoc, |
| 2606 | InitRange.getBegin(), Init, |
| 2607 | InitRange.getEnd()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 2608 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2611 | MemInitResult |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2612 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, Expr *Init, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2613 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2614 | SourceLocation NameLoc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2615 | if (!LangOpts.CPlusPlus11) |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2616 | return Diag(NameLoc, diag::err_delegating_ctor) |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2617 | << TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2618 | Diag(NameLoc, diag::warn_cxx98_compat_delegating_ctor); |
Sebastian Redl | f9c32eb | 2011-03-12 13:53:51 +0000 | [diff] [blame] | 2619 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2620 | bool InitList = true; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2621 | MultiExprArg Args = Init; |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2622 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 2623 | InitList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2624 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2627 | SourceRange InitRange = Init->getSourceRange(); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2628 | // Initialize the object. |
| 2629 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 2630 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 2631 | InitializationKind Kind = |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2632 | InitList ? InitializationKind::CreateDirectList(NameLoc) |
| 2633 | : InitializationKind::CreateDirect(NameLoc, InitRange.getBegin(), |
| 2634 | InitRange.getEnd()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2635 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2636 | ExprResult DelegationInit = InitSeq.Perform(*this, DelegationEntity, Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2637 | Args, 0); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2638 | if (DelegationInit.isInvalid()) |
| 2639 | return true; |
| 2640 | |
Matt Beaumont-Gay | 2eb0ce3 | 2011-11-01 18:10:22 +0000 | [diff] [blame] | 2641 | assert(cast<CXXConstructExpr>(DelegationInit.get())->getConstructor() && |
| 2642 | "Delegating constructor with no target?"); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2643 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2644 | // C++11 [class.base.init]p7: |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2645 | // The initialization of each base and member constitutes a |
| 2646 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2647 | DelegationInit = ActOnFinishFullExpr(DelegationInit.get(), |
| 2648 | InitRange.getBegin()); |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2649 | if (DelegationInit.isInvalid()) |
| 2650 | return true; |
| 2651 | |
Eli Friedman | d21016f | 2012-05-19 23:35:23 +0000 | [diff] [blame] | 2652 | // If we are in a dependent context, template instantiation will |
| 2653 | // perform this type-checking again. Just save the arguments that we |
| 2654 | // received in a ParenListExpr. |
| 2655 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2656 | // of the information that we have about the base |
| 2657 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2658 | // and this approach is far more likely to get the corner cases right. |
| 2659 | if (CurContext->isDependentContext()) |
| 2660 | DelegationInit = Owned(Init); |
| 2661 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2662 | return new (Context) CXXCtorInitializer(Context, TInfo, InitRange.getBegin(), |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2663 | DelegationInit.takeAs<Expr>(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2664 | InitRange.getEnd()); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2668 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2669 | Expr *Init, CXXRecordDecl *ClassDecl, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2670 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2671 | SourceLocation BaseLoc |
| 2672 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2673 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2674 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 2675 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 2676 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2677 | |
| 2678 | // C++ [class.base.init]p2: |
| 2679 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2680 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2681 | // of that class, the mem-initializer is ill-formed. A |
| 2682 | // mem-initializer-list can initialize a base class using any |
| 2683 | // name that denotes that base class type. |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2684 | bool Dependent = BaseType->isDependentType() || Init->isTypeDependent(); |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2685 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2686 | SourceRange InitRange = Init->getSourceRange(); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2687 | if (EllipsisLoc.isValid()) { |
| 2688 | // This is a pack expansion. |
| 2689 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 2690 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2691 | << SourceRange(BaseLoc, InitRange.getEnd()); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2692 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2693 | EllipsisLoc = SourceLocation(); |
| 2694 | } |
| 2695 | } else { |
| 2696 | // Check for any unexpanded parameter packs. |
| 2697 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 2698 | return true; |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2699 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2700 | if (DiagnoseUnexpandedParameterPack(Init, UPPC_Initializer)) |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2701 | return true; |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2702 | } |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2703 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2704 | // Check for direct and virtual base classes. |
| 2705 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 2706 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 2707 | if (!Dependent) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2708 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 2709 | BaseType)) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2710 | return BuildDelegatingInitializer(BaseTInfo, Init, ClassDecl); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 2711 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 2712 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 2713 | VirtualBaseSpec); |
| 2714 | |
| 2715 | // C++ [base.class.init]p2: |
| 2716 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 2717 | // constructor's class or a direct or virtual base of that class, the |
| 2718 | // mem-initializer is ill-formed. |
| 2719 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 2720 | // If the class has any dependent bases, then it's possible that |
| 2721 | // one of those types will resolve to the same type as |
| 2722 | // BaseType. Therefore, just treat this as a dependent base |
| 2723 | // class initialization. FIXME: Should we try to check the |
| 2724 | // initialization anyway? It seems odd. |
| 2725 | if (ClassDecl->hasAnyDependentBases()) |
| 2726 | Dependent = true; |
| 2727 | else |
| 2728 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 2729 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 2730 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | if (Dependent) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2735 | DiscardCleanupsInEvaluationContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2736 | |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2737 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
| 2738 | /*IsVirtual=*/false, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2739 | InitRange.getBegin(), Init, |
| 2740 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2741 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2742 | |
| 2743 | // C++ [base.class.init]p2: |
| 2744 | // If a mem-initializer-id is ambiguous because it designates both |
| 2745 | // a direct non-virtual base class and an inherited virtual base |
| 2746 | // class, the mem-initializer is ill-formed. |
| 2747 | if (DirectBaseSpec && VirtualBaseSpec) |
| 2748 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 2749 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2750 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2751 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2752 | if (!BaseSpec) |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2753 | BaseSpec = VirtualBaseSpec; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2754 | |
| 2755 | // Initialize the base. |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2756 | bool InitList = true; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2757 | MultiExprArg Args = Init; |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2758 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2759 | InitList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2760 | Args = MultiExprArg(ParenList->getExprs(), ParenList->getNumExprs()); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2761 | } |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 2762 | |
| 2763 | InitializedEntity BaseEntity = |
| 2764 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
| 2765 | InitializationKind Kind = |
| 2766 | InitList ? InitializationKind::CreateDirectList(BaseLoc) |
| 2767 | : InitializationKind::CreateDirect(BaseLoc, InitRange.getBegin(), |
| 2768 | InitRange.getEnd()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2769 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args); |
| 2770 | ExprResult BaseInit = InitSeq.Perform(*this, BaseEntity, Kind, Args, 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2771 | if (BaseInit.isInvalid()) |
| 2772 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 2773 | |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2774 | // C++11 [class.base.init]p7: |
| 2775 | // The initialization of each base and member constitutes a |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2776 | // full-expression. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 2777 | BaseInit = ActOnFinishFullExpr(BaseInit.get(), InitRange.getBegin()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2778 | if (BaseInit.isInvalid()) |
| 2779 | return true; |
| 2780 | |
| 2781 | // If we are in a dependent context, template instantiation will |
| 2782 | // perform this type-checking again. Just save the arguments that we |
| 2783 | // received in a ParenListExpr. |
| 2784 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 2785 | // of the information that we have about the base |
| 2786 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 2787 | // 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] | 2788 | if (CurContext->isDependentContext()) |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2789 | BaseInit = Owned(Init); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2790 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2791 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2792 | BaseSpec->isVirtual(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2793 | InitRange.getBegin(), |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 2794 | BaseInit.takeAs<Expr>(), |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2795 | InitRange.getEnd(), EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2798 | // Create a static_cast\<T&&>(expr). |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 2799 | static Expr *CastForMoving(Sema &SemaRef, Expr *E, QualType T = QualType()) { |
| 2800 | if (T.isNull()) T = E->getType(); |
| 2801 | QualType TargetType = SemaRef.BuildReferenceType( |
| 2802 | T, /*SpelledAsLValue*/false, SourceLocation(), DeclarationName()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2803 | SourceLocation ExprLoc = E->getLocStart(); |
| 2804 | TypeSourceInfo *TargetLoc = SemaRef.Context.getTrivialTypeSourceInfo( |
| 2805 | TargetType, ExprLoc); |
| 2806 | |
| 2807 | return SemaRef.BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
| 2808 | SourceRange(ExprLoc, ExprLoc), |
| 2809 | E->getSourceRange()).take(); |
| 2810 | } |
| 2811 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2812 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 2813 | /// initialize its base or member. |
| 2814 | enum ImplicitInitializerKind { |
| 2815 | IIK_Default, |
| 2816 | IIK_Copy, |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 2817 | IIK_Move, |
| 2818 | IIK_Inherit |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2819 | }; |
| 2820 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2821 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2822 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2823 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2824 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2825 | bool IsInheritedVirtualBase, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2826 | CXXCtorInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2827 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2828 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 2829 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2830 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2831 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2832 | |
| 2833 | switch (ImplicitInitKind) { |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 2834 | case IIK_Inherit: { |
| 2835 | const CXXRecordDecl *Inherited = |
| 2836 | Constructor->getInheritedConstructor()->getParent(); |
| 2837 | const CXXRecordDecl *Base = BaseSpec->getType()->getAsCXXRecordDecl(); |
| 2838 | if (Base && Inherited->getCanonicalDecl() == Base->getCanonicalDecl()) { |
| 2839 | // C++11 [class.inhctor]p8: |
| 2840 | // Each expression in the expression-list is of the form |
| 2841 | // static_cast<T&&>(p), where p is the name of the corresponding |
| 2842 | // constructor parameter and T is the declared type of p. |
| 2843 | SmallVector<Expr*, 16> Args; |
| 2844 | for (unsigned I = 0, E = Constructor->getNumParams(); I != E; ++I) { |
| 2845 | ParmVarDecl *PD = Constructor->getParamDecl(I); |
| 2846 | ExprResult ArgExpr = |
| 2847 | SemaRef.BuildDeclRefExpr(PD, PD->getType().getNonReferenceType(), |
| 2848 | VK_LValue, SourceLocation()); |
| 2849 | if (ArgExpr.isInvalid()) |
| 2850 | return true; |
| 2851 | Args.push_back(CastForMoving(SemaRef, ArgExpr.take(), PD->getType())); |
| 2852 | } |
| 2853 | |
| 2854 | InitializationKind InitKind = InitializationKind::CreateDirect( |
| 2855 | Constructor->getLocation(), SourceLocation(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2856 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, Args); |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 2857 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, Args); |
| 2858 | break; |
| 2859 | } |
| 2860 | } |
| 2861 | // Fall through. |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2862 | case IIK_Default: { |
| 2863 | InitializationKind InitKind |
| 2864 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2865 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, None); |
| 2866 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, None); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2867 | break; |
| 2868 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2869 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2870 | case IIK_Move: |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2871 | case IIK_Copy: { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2872 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2873 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 2874 | QualType ParamType = Param->getType().getNonReferenceType(); |
Eli Friedman | cf7c14c | 2012-01-16 21:00:51 +0000 | [diff] [blame] | 2875 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2876 | Expr *CopyCtorArg = |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2877 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2878 | SourceLocation(), Param, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2879 | Constructor->getLocation(), ParamType, |
| 2880 | VK_LValue, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2881 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2882 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(CopyCtorArg)); |
| 2883 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 2884 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 2885 | QualType ArgTy = |
| 2886 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 2887 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2888 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2889 | if (Moving) { |
| 2890 | CopyCtorArg = CastForMoving(SemaRef, CopyCtorArg); |
| 2891 | } |
| 2892 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2893 | CXXCastPath BasePath; |
| 2894 | BasePath.push_back(BaseSpec); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2895 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 2896 | CK_UncheckedDerivedToBase, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2897 | Moving ? VK_XValue : VK_LValue, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2898 | &BasePath).take(); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 2899 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2900 | InitializationKind InitKind |
| 2901 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 2902 | SourceLocation(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2903 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, CopyCtorArg); |
| 2904 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, CopyCtorArg); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2905 | break; |
| 2906 | } |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2907 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2908 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2909 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2910 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2911 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2912 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2913 | CXXBaseInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2914 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2915 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 2916 | SourceLocation()), |
| 2917 | BaseSpec->isVirtual(), |
| 2918 | SourceLocation(), |
| 2919 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2920 | SourceLocation(), |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2921 | SourceLocation()); |
| 2922 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2923 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2926 | static bool RefersToRValueRef(Expr *MemRef) { |
| 2927 | ValueDecl *Referenced = cast<MemberExpr>(MemRef)->getMemberDecl(); |
| 2928 | return Referenced->getType()->isRValueReferenceType(); |
| 2929 | } |
| 2930 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2931 | static bool |
| 2932 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2933 | ImplicitInitializerKind ImplicitInitKind, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 2934 | FieldDecl *Field, IndirectFieldDecl *Indirect, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2935 | CXXCtorInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2936 | if (Field->isInvalidDecl()) |
| 2937 | return true; |
| 2938 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 2939 | SourceLocation Loc = Constructor->getLocation(); |
| 2940 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2941 | if (ImplicitInitKind == IIK_Copy || ImplicitInitKind == IIK_Move) { |
| 2942 | bool Moving = ImplicitInitKind == IIK_Move; |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2943 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 2944 | QualType ParamType = Param->getType().getNonReferenceType(); |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 2945 | |
| 2946 | // Suppress copying zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2947 | if (Field->isBitField() && Field->getBitWidthValue(SemaRef.Context) == 0) |
| 2948 | return false; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 2949 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2950 | Expr *MemberExprBase = |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2951 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2952 | SourceLocation(), Param, false, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2953 | Loc, ParamType, VK_LValue, 0); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2954 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2955 | SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(MemberExprBase)); |
| 2956 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2957 | if (Moving) { |
| 2958 | MemberExprBase = CastForMoving(SemaRef, MemberExprBase); |
| 2959 | } |
| 2960 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2961 | // Build a reference to this field within the parameter. |
| 2962 | CXXScopeSpec SS; |
| 2963 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 2964 | Sema::LookupMemberName); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2965 | MemberLookup.addDecl(Indirect ? cast<ValueDecl>(Indirect) |
| 2966 | : cast<ValueDecl>(Field), AS_public); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2967 | MemberLookup.resolveKind(); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2968 | ExprResult CtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2969 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2970 | ParamType, Loc, |
| 2971 | /*IsArrow=*/false, |
| 2972 | SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2973 | /*TemplateKWLoc=*/SourceLocation(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2974 | /*FirstQualifierInScope=*/0, |
| 2975 | MemberLookup, |
| 2976 | /*TemplateArgs=*/0); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2977 | if (CtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 2978 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2979 | |
| 2980 | // C++11 [class.copy]p15: |
| 2981 | // - if a member m has rvalue reference type T&&, it is direct-initialized |
| 2982 | // with static_cast<T&&>(x.m); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 2983 | if (RefersToRValueRef(CtorArg.get())) { |
| 2984 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2987 | // When the field we are copying is an array, create index variables for |
| 2988 | // each dimension of the array. We use these index variables to subscript |
| 2989 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 2990 | // necessary iteration with these index variables. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2991 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2992 | QualType BaseType = Field->getType(); |
| 2993 | QualType SizeType = SemaRef.Context.getSizeType(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2994 | bool InitializingArray = false; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2995 | while (const ConstantArrayType *Array |
| 2996 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 2997 | InitializingArray = true; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 2998 | // Create the iteration variable for this array index. |
| 2999 | IdentifierInfo *IterationVarName = 0; |
| 3000 | { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3001 | SmallString<8> Str; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3002 | llvm::raw_svector_ostream OS(Str); |
| 3003 | OS << "__i" << IndexVariables.size(); |
| 3004 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 3005 | } |
| 3006 | VarDecl *IterationVar |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3007 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3008 | IterationVarName, SizeType, |
| 3009 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3010 | SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3011 | IndexVariables.push_back(IterationVar); |
| 3012 | |
| 3013 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3014 | ExprResult IterationVarRef |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 3015 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3016 | assert(!IterationVarRef.isInvalid() && |
| 3017 | "Reference to invented variable cannot fail!"); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 3018 | IterationVarRef = SemaRef.DefaultLvalueConversion(IterationVarRef.take()); |
| 3019 | assert(!IterationVarRef.isInvalid() && |
| 3020 | "Conversion of invented variable cannot fail!"); |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3021 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3022 | // Subscript the array with this iteration variable. |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3023 | CtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CtorArg.take(), Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3024 | IterationVarRef.take(), |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3025 | Loc); |
| 3026 | if (CtorArg.isInvalid()) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3027 | return true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3028 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3029 | BaseType = Array->getElementType(); |
| 3030 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3031 | |
| 3032 | // The array subscript expression is an lvalue, which is wrong for moving. |
| 3033 | if (Moving && InitializingArray) |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3034 | CtorArg = CastForMoving(SemaRef, CtorArg.take()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3035 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3036 | // Construct the entity that we will be initializing. For an array, this |
| 3037 | // will be first element in the array, which may require several levels |
| 3038 | // of array-subscript entities. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3039 | SmallVector<InitializedEntity, 4> Entities; |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3040 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3041 | if (Indirect) |
| 3042 | Entities.push_back(InitializedEntity::InitializeMember(Indirect)); |
| 3043 | else |
| 3044 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3045 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 3046 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 3047 | 0, |
| 3048 | Entities.back())); |
| 3049 | |
| 3050 | // Direct-initialize to use the copy constructor. |
| 3051 | InitializationKind InitKind = |
| 3052 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 3053 | |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3054 | Expr *CtorArgE = CtorArg.takeAs<Expr>(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3055 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, CtorArgE); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3056 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3057 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3058 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
Sebastian Redl | 74e611a | 2011-09-04 18:14:28 +0000 | [diff] [blame] | 3059 | MultiExprArg(&CtorArgE, 1)); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3060 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 3061 | if (MemberInit.isInvalid()) |
| 3062 | return true; |
| 3063 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3064 | if (Indirect) { |
| 3065 | assert(IndexVariables.size() == 0 && |
| 3066 | "Indirect field improperly initialized"); |
| 3067 | CXXMemberInit |
| 3068 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 3069 | Loc, Loc, |
| 3070 | MemberInit.takeAs<Expr>(), |
| 3071 | Loc); |
| 3072 | } else |
| 3073 | CXXMemberInit = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, |
| 3074 | Loc, MemberInit.takeAs<Expr>(), |
| 3075 | Loc, |
| 3076 | IndexVariables.data(), |
| 3077 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3078 | return false; |
| 3079 | } |
| 3080 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3081 | assert((ImplicitInitKind == IIK_Default || ImplicitInitKind == IIK_Inherit) && |
| 3082 | "Unhandled implicit init kind!"); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3083 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3084 | QualType FieldBaseElementType = |
| 3085 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 3086 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3087 | if (FieldBaseElementType->isRecordType()) { |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3088 | InitializedEntity InitEntity |
| 3089 | = Indirect? InitializedEntity::InitializeMember(Indirect) |
| 3090 | : InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 3091 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 3092 | InitializationKind::CreateDefault(Loc); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3093 | |
| 3094 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, None); |
| 3095 | ExprResult MemberInit = |
| 3096 | InitSeq.Perform(SemaRef, InitEntity, InitKind, None); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3097 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 3098 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3099 | if (MemberInit.isInvalid()) |
| 3100 | return true; |
| 3101 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3102 | if (Indirect) |
| 3103 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 3104 | Indirect, Loc, |
| 3105 | Loc, |
| 3106 | MemberInit.get(), |
| 3107 | Loc); |
| 3108 | else |
| 3109 | CXXMemberInit = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
| 3110 | Field, Loc, Loc, |
| 3111 | MemberInit.get(), |
| 3112 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3113 | return false; |
| 3114 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3115 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 3116 | if (!Field->getParent()->isUnion()) { |
| 3117 | if (FieldBaseElementType->isReferenceType()) { |
| 3118 | SemaRef.Diag(Constructor->getLocation(), |
| 3119 | diag::err_uninitialized_member_in_ctor) |
| 3120 | << (int)Constructor->isImplicit() |
| 3121 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 3122 | << 0 << Field->getDeclName(); |
| 3123 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 3124 | return true; |
| 3125 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3126 | |
Sean Hunt | 1f2f384 | 2011-05-17 00:19:05 +0000 | [diff] [blame] | 3127 | if (FieldBaseElementType.isConstQualified()) { |
| 3128 | SemaRef.Diag(Constructor->getLocation(), |
| 3129 | diag::err_uninitialized_member_in_ctor) |
| 3130 | << (int)Constructor->isImplicit() |
| 3131 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 3132 | << 1 << Field->getDeclName(); |
| 3133 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 3134 | return true; |
| 3135 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 3136 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3137 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3138 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3139 | FieldBaseElementType->isObjCRetainableType() && |
| 3140 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_None && |
| 3141 | FieldBaseElementType.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) { |
Douglas Gregor | 3fe52ff | 2012-07-23 04:23:39 +0000 | [diff] [blame] | 3142 | // ARC: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3143 | // Default-initialize Objective-C pointers to NULL. |
| 3144 | CXXMemberInit |
| 3145 | = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 3146 | Loc, Loc, |
| 3147 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType()), |
| 3148 | Loc); |
| 3149 | return false; |
| 3150 | } |
| 3151 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 3152 | // Nothing to initialize. |
| 3153 | CXXMemberInit = 0; |
| 3154 | return false; |
| 3155 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3156 | |
| 3157 | namespace { |
| 3158 | struct BaseAndFieldInfo { |
| 3159 | Sema &S; |
| 3160 | CXXConstructorDecl *Ctor; |
| 3161 | bool AnyErrorsInInits; |
| 3162 | ImplicitInitializerKind IIK; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3163 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3164 | SmallVector<CXXCtorInitializer*, 8> AllToInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3165 | |
| 3166 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 3167 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3168 | bool Generated = Ctor->isImplicit() || Ctor->isDefaulted(); |
| 3169 | if (Generated && Ctor->isCopyConstructor()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3170 | IIK = IIK_Copy; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3171 | else if (Generated && Ctor->isMoveConstructor()) |
| 3172 | IIK = IIK_Move; |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3173 | else if (Ctor->getInheritedConstructor()) |
| 3174 | IIK = IIK_Inherit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3175 | else |
| 3176 | IIK = IIK_Default; |
| 3177 | } |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3178 | |
| 3179 | bool isImplicitCopyOrMove() const { |
| 3180 | switch (IIK) { |
| 3181 | case IIK_Copy: |
| 3182 | case IIK_Move: |
| 3183 | return true; |
| 3184 | |
| 3185 | case IIK_Default: |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3186 | case IIK_Inherit: |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3187 | return false; |
| 3188 | } |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 3189 | |
| 3190 | llvm_unreachable("Invalid ImplicitInitializerKind!"); |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3191 | } |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3192 | |
| 3193 | bool addFieldInitializer(CXXCtorInitializer *Init) { |
| 3194 | AllToInit.push_back(Init); |
| 3195 | |
| 3196 | // Check whether this initializer makes the field "used". |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3197 | if (Init->getInit()->HasSideEffects(S.Context)) |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3198 | S.UnusedPrivateFields.remove(Init->getAnyMember()); |
| 3199 | |
| 3200 | return false; |
| 3201 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3202 | }; |
| 3203 | } |
| 3204 | |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 3205 | /// \brief Determine whether the given indirect field declaration is somewhere |
| 3206 | /// within an anonymous union. |
| 3207 | static bool isWithinAnonymousUnion(IndirectFieldDecl *F) { |
| 3208 | for (IndirectFieldDecl::chain_iterator C = F->chain_begin(), |
| 3209 | CEnd = F->chain_end(); |
| 3210 | C != CEnd; ++C) |
| 3211 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>((*C)->getDeclContext())) |
| 3212 | if (Record->isUnion()) |
| 3213 | return true; |
| 3214 | |
| 3215 | return false; |
| 3216 | } |
| 3217 | |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3218 | /// \brief Determine whether the given type is an incomplete or zero-lenfgth |
| 3219 | /// array type. |
| 3220 | static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) { |
| 3221 | if (T->isIncompleteArrayType()) |
| 3222 | return true; |
| 3223 | |
| 3224 | while (const ConstantArrayType *ArrayT = Context.getAsConstantArrayType(T)) { |
| 3225 | if (!ArrayT->getSize()) |
| 3226 | return true; |
| 3227 | |
| 3228 | T = ArrayT->getElementType(); |
| 3229 | } |
| 3230 | |
| 3231 | return false; |
| 3232 | } |
| 3233 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3234 | static bool CollectFieldInitializer(Sema &SemaRef, BaseAndFieldInfo &Info, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3235 | FieldDecl *Field, |
| 3236 | IndirectFieldDecl *Indirect = 0) { |
Eli Friedman | 5fb478b | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 3237 | if (Field->isInvalidDecl()) |
| 3238 | return false; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3239 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 3240 | // Overwhelmingly common case: we have a direct initializer for this field. |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3241 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(Field)) |
| 3242 | return Info.addFieldInitializer(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3243 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3244 | // 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] | 3245 | // has a brace-or-equal-initializer, the entity is initialized as specified |
| 3246 | // in [dcl.init]. |
Douglas Gregor | f485388 | 2011-11-28 20:03:15 +0000 | [diff] [blame] | 3247 | if (Field->hasInClassInitializer() && !Info.isImplicitCopyOrMove()) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3248 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, |
| 3249 | Info.Ctor->getLocation(), Field); |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3250 | CXXCtorInitializer *Init; |
| 3251 | if (Indirect) |
| 3252 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Indirect, |
| 3253 | SourceLocation(), |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3254 | SourceLocation(), DIE, |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3255 | SourceLocation()); |
| 3256 | else |
| 3257 | Init = new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, Field, |
| 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()); |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3261 | return Info.addFieldInitializer(Init); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
Richard Smith | c115f63 | 2011-09-18 11:14:50 +0000 | [diff] [blame] | 3264 | // Don't build an implicit initializer for union members if none was |
| 3265 | // explicitly specified. |
Richard Smith | a495066 | 2011-09-19 13:34:43 +0000 | [diff] [blame] | 3266 | if (Field->getParent()->isUnion() || |
| 3267 | (Indirect && isWithinAnonymousUnion(Indirect))) |
Richard Smith | c115f63 | 2011-09-18 11:14:50 +0000 | [diff] [blame] | 3268 | return false; |
| 3269 | |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3270 | // Don't initialize incomplete or zero-length arrays. |
| 3271 | if (isIncompleteOrZeroLengthArrayType(SemaRef.Context, Field->getType())) |
| 3272 | return false; |
| 3273 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3274 | // Don't try to build an implicit initializer if there were semantic |
| 3275 | // errors in any of the initializers (and therefore we might be |
| 3276 | // missing some that the user actually wrote). |
Eli Friedman | 5fb478b | 2013-06-28 21:07:41 +0000 | [diff] [blame] | 3277 | if (Info.AnyErrorsInInits) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3278 | return false; |
| 3279 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3280 | CXXCtorInitializer *Init = 0; |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3281 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, |
| 3282 | Indirect, Init)) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3283 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3284 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3285 | if (!Init) |
| 3286 | return false; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3287 | |
Richard Smith | 0b8220a | 2012-08-07 21:30:42 +0000 | [diff] [blame] | 3288 | return Info.addFieldInitializer(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3289 | } |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3290 | |
| 3291 | bool |
| 3292 | Sema::SetDelegatingInitializer(CXXConstructorDecl *Constructor, |
| 3293 | CXXCtorInitializer *Initializer) { |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3294 | assert(Initializer->isDelegatingInitializer()); |
Sean Hunt | 01aacc0 | 2011-05-03 20:43:02 +0000 | [diff] [blame] | 3295 | Constructor->setNumCtorInitializers(1); |
| 3296 | CXXCtorInitializer **initializer = |
| 3297 | new (Context) CXXCtorInitializer*[1]; |
| 3298 | memcpy(initializer, &Initializer, sizeof (CXXCtorInitializer*)); |
| 3299 | Constructor->setCtorInitializers(initializer); |
| 3300 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3301 | if (CXXDestructorDecl *Dtor = LookupDestructor(Constructor->getParent())) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3302 | MarkFunctionReferenced(Initializer->getSourceLocation(), Dtor); |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 3303 | DiagnoseUseOfDecl(Dtor, Initializer->getSourceLocation()); |
| 3304 | } |
| 3305 | |
Sean Hunt | c159870 | 2011-05-05 00:05:47 +0000 | [diff] [blame] | 3306 | DelegatingCtorDecls.push_back(Constructor); |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3307 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3308 | return false; |
| 3309 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3310 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3311 | bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, bool AnyErrors, |
| 3312 | ArrayRef<CXXCtorInitializer *> Initializers) { |
Douglas Gregor | d836c0d | 2011-09-22 23:04:35 +0000 | [diff] [blame] | 3313 | if (Constructor->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3314 | // Just store the initializers as written, they will be checked during |
| 3315 | // instantiation. |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3316 | if (!Initializers.empty()) { |
| 3317 | Constructor->setNumCtorInitializers(Initializers.size()); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3318 | CXXCtorInitializer **baseOrMemberInitializers = |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3319 | new (Context) CXXCtorInitializer*[Initializers.size()]; |
| 3320 | memcpy(baseOrMemberInitializers, Initializers.data(), |
| 3321 | Initializers.size() * sizeof(CXXCtorInitializer*)); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3322 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3323 | } |
Richard Smith | 54b3ba8 | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 3324 | |
| 3325 | // Let template instantiation know whether we had errors. |
| 3326 | if (AnyErrors) |
| 3327 | Constructor->setInvalidDecl(); |
| 3328 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3329 | return false; |
| 3330 | } |
| 3331 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3332 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3333 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3334 | // We need to build the initializer AST according to order of construction |
| 3335 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3336 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 3337 | if (!ClassDecl) |
| 3338 | return true; |
| 3339 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3340 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3341 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3342 | for (unsigned i = 0; i < Initializers.size(); i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3343 | CXXCtorInitializer *Member = Initializers[i]; |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3344 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3345 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3346 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3347 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3348 | Info.AllBaseFields[Member->getAnyMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3351 | // Keep track of the direct virtual bases. |
| 3352 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 3353 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 3354 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 3355 | if (I->isVirtual()) |
| 3356 | DirectVBases.insert(I); |
| 3357 | } |
| 3358 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3359 | // Push virtual bases before others. |
| 3360 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 3361 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 3362 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3363 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3364 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3365 | // [class.base.init]p7, per DR257: |
| 3366 | // A mem-initializer where the mem-initializer-id names a virtual base |
| 3367 | // class is ignored during execution of a constructor of any class that |
| 3368 | // is not the most derived class. |
| 3369 | if (ClassDecl->isAbstract()) { |
| 3370 | // FIXME: Provide a fixit to remove the base specifier. This requires |
| 3371 | // tracking the location of the associated comma for a base specifier. |
| 3372 | Diag(Value->getSourceLocation(), diag::warn_abstract_vbase_init_ignored) |
| 3373 | << VBase->getType() << ClassDecl; |
| 3374 | DiagnoseAbstractType(ClassDecl); |
| 3375 | } |
| 3376 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3377 | Info.AllToInit.push_back(Value); |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3378 | } else if (!AnyErrors && !ClassDecl->isAbstract()) { |
| 3379 | // [class.base.init]p8, per DR257: |
| 3380 | // If a given [...] base class is not named by a mem-initializer-id |
| 3381 | // [...] and the entity is not a virtual base class of an abstract |
| 3382 | // class, then [...] the entity is default-initialized. |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 3383 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3384 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3385 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3386 | VBase, IsInheritedVirtualBase, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3387 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3388 | HadError = true; |
| 3389 | continue; |
| 3390 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 3391 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3392 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3393 | } |
| 3394 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3395 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3396 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3397 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3398 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 3399 | // Virtuals are in the virtual base list and already constructed. |
| 3400 | if (Base->isVirtual()) |
| 3401 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3402 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3403 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3404 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 3405 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3406 | } else if (!AnyErrors) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3407 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3408 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 3409 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 3410 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3411 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3412 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 3413 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 3414 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3415 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3416 | } |
| 3417 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3418 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3419 | // Fields. |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3420 | for (DeclContext::decl_iterator Mem = ClassDecl->decls_begin(), |
| 3421 | MemEnd = ClassDecl->decls_end(); |
| 3422 | Mem != MemEnd; ++Mem) { |
| 3423 | if (FieldDecl *F = dyn_cast<FieldDecl>(*Mem)) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3424 | // C++ [class.bit]p2: |
| 3425 | // A declaration for a bit-field that omits the identifier declares an |
| 3426 | // unnamed bit-field. Unnamed bit-fields are not members and cannot be |
| 3427 | // initialized. |
| 3428 | if (F->isUnnamedBitfield()) |
| 3429 | continue; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3430 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 3431 | // 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] | 3432 | // handle anonymous struct/union fields based on their individual |
| 3433 | // indirect fields. |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3434 | if (F->isAnonymousStructOrUnion() && !Info.isImplicitCopyOrMove()) |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3435 | continue; |
| 3436 | |
| 3437 | if (CollectFieldInitializer(*this, Info, F)) |
| 3438 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 3439 | continue; |
| 3440 | } |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3441 | |
| 3442 | // Beyond this point, we only consider default initialization. |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 3443 | if (Info.isImplicitCopyOrMove()) |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3444 | continue; |
| 3445 | |
| 3446 | if (IndirectFieldDecl *F = dyn_cast<IndirectFieldDecl>(*Mem)) { |
| 3447 | if (F->getType()->isIncompleteArrayType()) { |
| 3448 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 3449 | "Incomplete array type is not valid"); |
| 3450 | continue; |
| 3451 | } |
| 3452 | |
Douglas Gregor | 4dc41c9 | 2011-08-10 15:22:55 +0000 | [diff] [blame] | 3453 | // Initialize each field of an anonymous struct individually. |
| 3454 | if (CollectFieldInitializer(*this, Info, F->getAnonField(), F)) |
| 3455 | HadError = true; |
| 3456 | |
| 3457 | continue; |
| 3458 | } |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 3459 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3460 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3461 | unsigned NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3462 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3463 | Constructor->setNumCtorInitializers(NumInitializers); |
| 3464 | CXXCtorInitializer **baseOrMemberInitializers = |
| 3465 | new (Context) CXXCtorInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 3466 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3467 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 3468 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 3469 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3470 | // Constructors implicitly reference the base and member |
| 3471 | // destructors. |
| 3472 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 3473 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3474 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3475 | |
| 3476 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3479 | static void PopulateKeysForFields(FieldDecl *Field, SmallVectorImpl<const void*> &IdealInits) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3480 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3481 | const RecordDecl *RD = RT->getDecl(); |
| 3482 | if (RD->isAnonymousStructOrUnion()) { |
| 3483 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3484 | E = RD->field_end(); Field != E; ++Field) |
| 3485 | PopulateKeysForFields(*Field, IdealInits); |
| 3486 | return; |
| 3487 | } |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3488 | } |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3489 | IdealInits.push_back(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3492 | static const void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 3493 | return Context.getCanonicalType(BaseType).getTypePtr(); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3496 | static const void *GetKeyForMember(ASTContext &Context, |
| 3497 | CXXCtorInitializer *Member) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3498 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3499 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 3500 | |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3501 | return Member->getAnyMember(); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3504 | static void DiagnoseBaseOrMemInitializerOrder( |
| 3505 | Sema &SemaRef, const CXXConstructorDecl *Constructor, |
| 3506 | ArrayRef<CXXCtorInitializer *> Inits) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3507 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 3508 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3509 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3510 | // Don't check initializers order unless the warning is enabled at the |
| 3511 | // location of at least one initializer. |
| 3512 | bool ShouldCheckOrder = false; |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3513 | for (unsigned InitIndex = 0; InitIndex != Inits.size(); ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3514 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3515 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order, |
| 3516 | Init->getSourceLocation()) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 3517 | != DiagnosticsEngine::Ignored) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 3518 | ShouldCheckOrder = true; |
| 3519 | break; |
| 3520 | } |
| 3521 | } |
| 3522 | if (!ShouldCheckOrder) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3523 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3524 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3525 | // Build the list of bases and members in the order that they'll |
| 3526 | // actually be initialized. The explicit initializers should be in |
| 3527 | // this same order but may be missing things. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3528 | SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3530 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 3531 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3532 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3533 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3534 | ClassDecl->vbases_begin(), |
| 3535 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3536 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3538 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 3539 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3540 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3541 | if (Base->isVirtual()) |
| 3542 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3543 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3544 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3545 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3546 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3547 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3548 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 3549 | if (Field->isUnnamedBitfield()) |
| 3550 | continue; |
| 3551 | |
David Blaikie | ee000bb | 2013-01-17 08:49:22 +0000 | [diff] [blame] | 3552 | PopulateKeysForFields(*Field, IdealInitKeys); |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3555 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 3556 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 3557 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3558 | CXXCtorInitializer *PrevInit = 0; |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3559 | for (unsigned InitIndex = 0; InitIndex != Inits.size(); ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3560 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3561 | const void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3562 | |
| 3563 | // Scan forward to try to find this initializer in the idealized |
| 3564 | // initializers list. |
| 3565 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3566 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3567 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3568 | |
| 3569 | // If we didn't find this initializer, it must be because we |
| 3570 | // scanned past it on a previous iteration. That can only |
| 3571 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 3572 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3573 | Sema::SemaDiagnosticBuilder D = |
| 3574 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 3575 | diag::warn_initializer_out_of_order); |
| 3576 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3577 | if (PrevInit->isAnyMemberInitializer()) |
| 3578 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3579 | else |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 3580 | D << 1 << PrevInit->getTypeSourceInfo()->getType(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3581 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3582 | if (Init->isAnyMemberInitializer()) |
| 3583 | D << 0 << Init->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 << Init->getTypeSourceInfo()->getType(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3586 | |
| 3587 | // Move back to the initializer's location in the ideal list. |
| 3588 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 3589 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 3590 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3591 | |
| 3592 | assert(IdealIndex != NumIdealInits && |
| 3593 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3594 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 3595 | |
| 3596 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 3597 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3600 | namespace { |
| 3601 | bool CheckRedundantInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3602 | CXXCtorInitializer *Init, |
| 3603 | CXXCtorInitializer *&PrevInit) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3604 | if (!PrevInit) { |
| 3605 | PrevInit = Init; |
| 3606 | return false; |
| 3607 | } |
| 3608 | |
Douglas Gregor | dc392c1 | 2013-03-25 23:28:23 +0000 | [diff] [blame] | 3609 | if (FieldDecl *Field = Init->getAnyMember()) |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3610 | S.Diag(Init->getSourceLocation(), |
| 3611 | diag::err_multiple_mem_initialization) |
| 3612 | << Field->getDeclName() |
| 3613 | << Init->getSourceRange(); |
| 3614 | else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3615 | const Type *BaseClass = Init->getBaseClass(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3616 | assert(BaseClass && "neither field nor base"); |
| 3617 | S.Diag(Init->getSourceLocation(), |
| 3618 | diag::err_multiple_base_initialization) |
| 3619 | << QualType(BaseClass, 0) |
| 3620 | << Init->getSourceRange(); |
| 3621 | } |
| 3622 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 3623 | << 0 << PrevInit->getSourceRange(); |
| 3624 | |
| 3625 | return true; |
| 3626 | } |
| 3627 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3628 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3629 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 3630 | |
| 3631 | bool CheckRedundantUnionInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3632 | CXXCtorInitializer *Init, |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3633 | RedundantUnionMap &Unions) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3634 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3635 | RecordDecl *Parent = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3636 | NamedDecl *Child = Field; |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3637 | |
| 3638 | while (Parent->isAnonymousStructOrUnion() || Parent->isUnion()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3639 | if (Parent->isUnion()) { |
| 3640 | UnionEntry &En = Unions[Parent]; |
| 3641 | if (En.first && En.first != Child) { |
| 3642 | S.Diag(Init->getSourceLocation(), |
| 3643 | diag::err_multiple_mem_union_initialization) |
| 3644 | << Field->getDeclName() |
| 3645 | << Init->getSourceRange(); |
| 3646 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 3647 | << 0 << En.second->getSourceRange(); |
| 3648 | return true; |
David Blaikie | 5bbe816 | 2011-11-12 20:54:14 +0000 | [diff] [blame] | 3649 | } |
| 3650 | if (!En.first) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3651 | En.first = Child; |
| 3652 | En.second = Init; |
| 3653 | } |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3654 | if (!Parent->isAnonymousStructOrUnion()) |
| 3655 | return false; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | Child = Parent; |
| 3659 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
David Blaikie | 6fe2965 | 2011-11-17 06:01:57 +0000 | [diff] [blame] | 3660 | } |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3661 | |
| 3662 | return false; |
| 3663 | } |
| 3664 | } |
| 3665 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3666 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3667 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3668 | SourceLocation ColonLoc, |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3669 | ArrayRef<CXXCtorInitializer*> MemInits, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3670 | bool AnyErrors) { |
| 3671 | if (!ConstructorDecl) |
| 3672 | return; |
| 3673 | |
| 3674 | AdjustDeclIfTemplate(ConstructorDecl); |
| 3675 | |
| 3676 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3677 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3678 | |
| 3679 | if (!Constructor) { |
| 3680 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 3681 | return; |
| 3682 | } |
| 3683 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3684 | // Mapping for the duplicate initializers check. |
| 3685 | // For member initializers, this is keyed with a FieldDecl*. |
| 3686 | // For base initializers, this is keyed with a Type*. |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3687 | llvm::DenseMap<const void *, CXXCtorInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3688 | |
| 3689 | // Mapping for the inconsistent anonymous-union initializers check. |
| 3690 | RedundantUnionMap MemberUnions; |
| 3691 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3692 | bool HadError = false; |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3693 | for (unsigned i = 0; i < MemInits.size(); i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3694 | CXXCtorInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3695 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 3696 | // Set the source order index. |
| 3697 | Init->setSourceOrder(i); |
| 3698 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3699 | if (Init->isAnyMemberInitializer()) { |
| 3700 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3701 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 3702 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 3703 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3704 | } else if (Init->isBaseInitializer()) { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3705 | const void *Key = |
| 3706 | GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 3707 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 3708 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3709 | } else { |
| 3710 | assert(Init->isDelegatingInitializer()); |
| 3711 | // This must be the only initializer |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3712 | if (MemInits.size() != 1) { |
Richard Smith | a6ddea6 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 3713 | Diag(Init->getSourceLocation(), |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3714 | diag::err_delegating_initializer_alone) |
Richard Smith | a6ddea6 | 2012-09-14 18:21:10 +0000 | [diff] [blame] | 3715 | << Init->getSourceRange() << MemInits[i ? 0 : 1]->getSourceRange(); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3716 | // We will treat this as being the only initializer. |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 3717 | } |
Sean Hunt | fe57eef | 2011-05-04 05:57:24 +0000 | [diff] [blame] | 3718 | SetDelegatingInitializer(Constructor, MemInits[i]); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3719 | // Return immediately as the initializer is set. |
| 3720 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3721 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 3724 | if (HadError) |
| 3725 | return; |
| 3726 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3727 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 3728 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3729 | SetCtorInitializers(Constructor, AnyErrors, MemInits); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3732 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 3733 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 3734 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 416f63e | 2011-09-18 12:11:43 +0000 | [diff] [blame] | 3735 | // Ignore dependent contexts. Also ignore unions, since their members never |
| 3736 | // have destructors implicitly called. |
| 3737 | if (ClassDecl->isDependentContext() || ClassDecl->isUnion()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3738 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3739 | |
| 3740 | // FIXME: all the access-control diagnostics are positioned on the |
| 3741 | // field/base declaration. That's probably good; that said, the |
| 3742 | // user might reasonably want to know why the destructor is being |
| 3743 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3744 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3745 | // Non-static data members. |
| 3746 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 3747 | E = ClassDecl->field_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3748 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 3749 | if (Field->isInvalidDecl()) |
| 3750 | continue; |
Douglas Gregor | ddb2147 | 2011-11-02 23:04:16 +0000 | [diff] [blame] | 3751 | |
| 3752 | // Don't destroy incomplete or zero-length arrays. |
| 3753 | if (isIncompleteOrZeroLengthArrayType(Context, Field->getType())) |
| 3754 | continue; |
| 3755 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3756 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 3757 | |
| 3758 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 3759 | if (!RT) |
| 3760 | continue; |
| 3761 | |
| 3762 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3763 | if (FieldClassDecl->isInvalidDecl()) |
| 3764 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3765 | if (FieldClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3766 | continue; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 3767 | // The destructor for an implicit anonymous union member is never invoked. |
| 3768 | if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion()) |
| 3769 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3770 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3771 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3772 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3773 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3774 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3775 | << Field->getDeclName() |
| 3776 | << FieldType); |
| 3777 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3778 | MarkFunctionReferenced(Location, Dtor); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3779 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3782 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 3783 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3784 | // Bases. |
| 3785 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3786 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3787 | // Bases are always records in a well-formed non-dependent class. |
| 3788 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 3789 | |
| 3790 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3791 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3792 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3793 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3794 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3795 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 3796 | if (BaseClassDecl->isInvalidDecl()) |
| 3797 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3798 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3799 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3801 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3802 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3803 | |
| 3804 | // FIXME: caret should be on the start of the class name |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3805 | CheckDestructorAccess(Base->getLocStart(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3806 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3807 | << Base->getType() |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 3808 | << Base->getSourceRange(), |
| 3809 | Context.getTypeDeclType(ClassDecl)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3810 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3811 | MarkFunctionReferenced(Location, Dtor); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3812 | DiagnoseUseOfDecl(Dtor, Location); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
| 3815 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3816 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 3817 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3818 | |
| 3819 | // Bases are always records in a well-formed non-dependent class. |
John McCall | 63f5578 | 2012-04-09 21:51:56 +0000 | [diff] [blame] | 3820 | const RecordType *RT = VBase->getType()->castAs<RecordType>(); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3821 | |
| 3822 | // Ignore direct virtual bases. |
| 3823 | if (DirectVirtualBases.count(RT)) |
| 3824 | continue; |
| 3825 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3826 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3827 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 3828 | if (BaseClassDecl->isInvalidDecl()) |
| 3829 | continue; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3830 | if (BaseClassDecl->hasIrrelevantDestructor()) |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3831 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3832 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3833 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 3834 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
David Majnemer | 2f68669 | 2013-06-22 06:43:58 +0000 | [diff] [blame] | 3835 | if (CheckDestructorAccess( |
| 3836 | ClassDecl->getLocation(), Dtor, |
| 3837 | PDiag(diag::err_access_dtor_vbase) |
| 3838 | << Context.getTypeDeclType(ClassDecl) << VBase->getType(), |
| 3839 | Context.getTypeDeclType(ClassDecl)) == |
| 3840 | AR_accessible) { |
| 3841 | CheckDerivedToBaseConversion( |
| 3842 | Context.getTypeDeclType(ClassDecl), VBase->getType(), |
| 3843 | diag::err_access_dtor_vbase, 0, ClassDecl->getLocation(), |
| 3844 | SourceRange(), DeclarationName(), 0); |
| 3845 | } |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3846 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3847 | MarkFunctionReferenced(Location, Dtor); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 3848 | DiagnoseUseOfDecl(Dtor, Location); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 3849 | } |
| 3850 | } |
| 3851 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3852 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 3853 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 3854 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3856 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3857 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 3858 | SetCtorInitializers(Constructor, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 3859 | } |
| 3860 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3861 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3862 | unsigned DiagID, AbstractDiagSelID SelID) { |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3863 | class NonAbstractTypeDiagnoser : public TypeDiagnoser { |
| 3864 | unsigned DiagID; |
| 3865 | AbstractDiagSelID SelID; |
| 3866 | |
| 3867 | public: |
| 3868 | NonAbstractTypeDiagnoser(unsigned DiagID, AbstractDiagSelID SelID) |
| 3869 | : TypeDiagnoser(DiagID == 0), DiagID(DiagID), SelID(SelID) { } |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 3870 | |
| 3871 | void diagnose(Sema &S, SourceLocation Loc, QualType T) LLVM_OVERRIDE { |
Eli Friedman | 2217f85 | 2012-08-14 02:06:07 +0000 | [diff] [blame] | 3872 | if (Suppressed) return; |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3873 | if (SelID == -1) |
| 3874 | S.Diag(Loc, DiagID) << T; |
| 3875 | else |
| 3876 | S.Diag(Loc, DiagID) << SelID << T; |
| 3877 | } |
| 3878 | } Diagnoser(DiagID, SelID); |
| 3879 | |
| 3880 | return RequireNonAbstractType(Loc, T, Diagnoser); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 3883 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3884 | TypeDiagnoser &Diagnoser) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3885 | if (!getLangOpts().CPlusPlus) |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3886 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3887 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 3888 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3889 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3890 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3891 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3892 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3893 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3894 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3895 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3896 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3897 | return RequireNonAbstractType(Loc, AT->getElementType(), Diagnoser); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 3898 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3899 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3900 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3901 | if (!RT) |
| 3902 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3903 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3904 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3905 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3906 | // We can't answer whether something is abstract until it has a |
| 3907 | // definition. If it's currently being defined, we'll walk back |
| 3908 | // over all the declarations when we have a full definition. |
| 3909 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 3910 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3911 | return false; |
| 3912 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3913 | if (!RD->isAbstract()) |
| 3914 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3915 | |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 3916 | Diagnoser.diagnose(*this, Loc, T); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3917 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3918 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3919 | return true; |
| 3920 | } |
| 3921 | |
| 3922 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 3923 | // Check if we've already emitted the list of pure virtual functions |
| 3924 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3925 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3926 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3927 | |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 3928 | // If the diagnostic is suppressed, don't emit the notes. We're only |
| 3929 | // going to emit them once, so try to attach them to a diagnostic we're |
| 3930 | // actually going to show. |
| 3931 | if (Diags.isLastDiagnosticIgnored()) |
| 3932 | return; |
| 3933 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3934 | CXXFinalOverriderMap FinalOverriders; |
| 3935 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3936 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 3937 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 3938 | // more than once. |
| 3939 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 3940 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3941 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 3942 | MEnd = FinalOverriders.end(); |
| 3943 | M != MEnd; |
| 3944 | ++M) { |
| 3945 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 3946 | SOEnd = M->second.end(); |
| 3947 | SO != SOEnd; ++SO) { |
| 3948 | // C++ [class.abstract]p4: |
| 3949 | // A class is abstract if it contains or inherits at least one |
| 3950 | // pure virtual function for which the final overrider is pure |
| 3951 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3952 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3953 | // |
| 3954 | if (SO->second.size() != 1) |
| 3955 | continue; |
| 3956 | |
| 3957 | if (!SO->second.front().Method->isPure()) |
| 3958 | continue; |
| 3959 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 3960 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 3961 | continue; |
| 3962 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3963 | Diag(SO->second.front().Method->getLocation(), |
| 3964 | diag::note_pure_virtual_function) |
Chandler Carruth | 45f11b7 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 3965 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 3966 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
| 3969 | if (!PureVirtualClassDiagSet) |
| 3970 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 3971 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3974 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3975 | struct AbstractUsageInfo { |
| 3976 | Sema &S; |
| 3977 | CXXRecordDecl *Record; |
| 3978 | CanQualType AbstractType; |
| 3979 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3980 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3981 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 3982 | : S(S), Record(Record), |
| 3983 | AbstractType(S.Context.getCanonicalType( |
| 3984 | S.Context.getTypeDeclType(Record))), |
| 3985 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 3986 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3987 | void DiagnoseAbstractType() { |
| 3988 | if (Invalid) return; |
| 3989 | S.DiagnoseAbstractType(Record); |
| 3990 | Invalid = true; |
| 3991 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 3992 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 3993 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 3994 | }; |
| 3995 | |
| 3996 | struct CheckAbstractUsage { |
| 3997 | AbstractUsageInfo &Info; |
| 3998 | const NamedDecl *Ctx; |
| 3999 | |
| 4000 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 4001 | : Info(Info), Ctx(Ctx) {} |
| 4002 | |
| 4003 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4004 | switch (TL.getTypeLocClass()) { |
| 4005 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 4006 | #define TYPELOC(CLASS, PARENT) \ |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4007 | case TypeLoc::CLASS: Check(TL.castAs<CLASS##TypeLoc>(), Sel); break; |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4008 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4009 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4010 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4012 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4013 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 4014 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
Douglas Gregor | 7019186 | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 4015 | if (!TL.getArg(I)) |
| 4016 | continue; |
| 4017 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4018 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 4019 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4020 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4021 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4022 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4023 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4024 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 4025 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4026 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4027 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4028 | // Visit the type parameters from a permissive context. |
| 4029 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 4030 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 4031 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 4032 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 4033 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 4034 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4035 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4036 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4037 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4038 | // Visit pointee types from a permissive context. |
| 4039 | #define CheckPolymorphic(Type) \ |
| 4040 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 4041 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 4042 | } |
| 4043 | CheckPolymorphic(PointerTypeLoc) |
| 4044 | CheckPolymorphic(ReferenceTypeLoc) |
| 4045 | CheckPolymorphic(MemberPointerTypeLoc) |
| 4046 | CheckPolymorphic(BlockPointerTypeLoc) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4047 | CheckPolymorphic(AtomicTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4048 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4049 | /// Handle all the types we haven't given a more specific |
| 4050 | /// implementation for above. |
| 4051 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 4052 | // Every other kind of type that we haven't called out already |
| 4053 | // that has an inner type is either (1) sugar or (2) contains that |
| 4054 | // inner type in some way as a subobject. |
| 4055 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 4056 | return Visit(Next, Sel); |
| 4057 | |
| 4058 | // If there's no inner type and we're in a permissive context, |
| 4059 | // don't diagnose. |
| 4060 | if (Sel == Sema::AbstractNone) return; |
| 4061 | |
| 4062 | // Check whether the type matches the abstract type. |
| 4063 | QualType T = TL.getType(); |
| 4064 | if (T->isArrayType()) { |
| 4065 | Sel = Sema::AbstractArrayType; |
| 4066 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 4067 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4068 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 4069 | if (CT != Info.AbstractType) return; |
| 4070 | |
| 4071 | // It matched; do some magic. |
| 4072 | if (Sel == Sema::AbstractArrayType) { |
| 4073 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 4074 | << T << TL.getSourceRange(); |
| 4075 | } else { |
| 4076 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 4077 | << Sel << T << TL.getSourceRange(); |
| 4078 | } |
| 4079 | Info.DiagnoseAbstractType(); |
| 4080 | } |
| 4081 | }; |
| 4082 | |
| 4083 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 4084 | Sema::AbstractDiagSelID Sel) { |
| 4085 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 4086 | } |
| 4087 | |
| 4088 | } |
| 4089 | |
| 4090 | /// Check for invalid uses of an abstract type in a method declaration. |
| 4091 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 4092 | CXXMethodDecl *MD) { |
| 4093 | // No need to do the check on definitions, which require that |
| 4094 | // the return/param types be complete. |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 4095 | if (MD->doesThisDeclarationHaveABody()) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4096 | return; |
| 4097 | |
| 4098 | // For safety's sake, just ignore it if we don't have type source |
| 4099 | // information. This should never happen for non-implicit methods, |
| 4100 | // but... |
| 4101 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 4102 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 4103 | } |
| 4104 | |
| 4105 | /// Check for invalid uses of an abstract type within a class definition. |
| 4106 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 4107 | CXXRecordDecl *RD) { |
| 4108 | for (CXXRecordDecl::decl_iterator |
| 4109 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 4110 | Decl *D = *I; |
| 4111 | if (D->isImplicit()) continue; |
| 4112 | |
| 4113 | // Methods and method templates. |
| 4114 | if (isa<CXXMethodDecl>(D)) { |
| 4115 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 4116 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 4117 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 4118 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 4119 | |
| 4120 | // Fields and static variables. |
| 4121 | } else if (isa<FieldDecl>(D)) { |
| 4122 | FieldDecl *FD = cast<FieldDecl>(D); |
| 4123 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 4124 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 4125 | } else if (isa<VarDecl>(D)) { |
| 4126 | VarDecl *VD = cast<VarDecl>(D); |
| 4127 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 4128 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 4129 | |
| 4130 | // Nested classes and class templates. |
| 4131 | } else if (isa<CXXRecordDecl>(D)) { |
| 4132 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 4133 | } else if (isa<ClassTemplateDecl>(D)) { |
| 4134 | CheckAbstractClassUsage(Info, |
| 4135 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 4136 | } |
| 4137 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4140 | /// \brief Perform semantic checks on a class definition that has been |
| 4141 | /// completing, introducing implicitly-declared members, checking for |
| 4142 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4143 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 4144 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 4145 | return; |
| 4146 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 4147 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 4148 | AbstractUsageInfo Info(*this, Record); |
| 4149 | CheckAbstractClassUsage(Info, Record); |
| 4150 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4151 | |
| 4152 | // If this is not an aggregate type and has no user-declared constructor, |
| 4153 | // complain about any non-static data members of reference or const scalar |
| 4154 | // type, since they will never get initializers. |
| 4155 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
Douglas Gregor | 5e058eb | 2012-02-09 02:20:38 +0000 | [diff] [blame] | 4156 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor() && |
| 4157 | !Record->isLambda()) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4158 | bool Complained = false; |
| 4159 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 4160 | FEnd = Record->field_end(); |
| 4161 | F != FEnd; ++F) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 4162 | if (F->hasInClassInitializer() || F->isUnnamedBitfield()) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4163 | continue; |
| 4164 | |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4165 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 4166 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 4167 | if (!Complained) { |
| 4168 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 4169 | << Record->getTagKind() << Record; |
| 4170 | Complained = true; |
| 4171 | } |
| 4172 | |
| 4173 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 4174 | << F->getType()->isReferenceType() |
| 4175 | << F->getDeclName(); |
| 4176 | } |
| 4177 | } |
| 4178 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4179 | |
Anders Carlsson | a5c6c2a | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 4180 | if (Record->isDynamicClass() && !Record->isDependentType()) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4181 | DynamicClasses.push_back(Record); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4182 | |
| 4183 | if (Record->getIdentifier()) { |
| 4184 | // C++ [class.mem]p13: |
| 4185 | // If T is the name of a class, then each of the following shall have a |
| 4186 | // name different from T: |
| 4187 | // - every member of every anonymous union that is a member of class T. |
| 4188 | // |
| 4189 | // C++ [class.mem]p14: |
| 4190 | // In addition, if class T has a user-declared constructor (12.1), every |
| 4191 | // 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] | 4192 | DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
| 4193 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 4194 | ++I) { |
| 4195 | NamedDecl *D = *I; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4196 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 4197 | isa<IndirectFieldDecl>(D)) { |
| 4198 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 4199 | << D->getDeclName(); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4200 | break; |
| 4201 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4202 | } |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 4203 | } |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4204 | |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 4205 | // Warn if the class has virtual methods but non-virtual public destructor. |
Douglas Gregor | f4b793c | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 4206 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4207 | CXXDestructorDecl *dtor = Record->getDestructor(); |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 4208 | if (!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 4209 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 4210 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 4211 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4212 | |
David Blaikie | b6b5b97 | 2012-09-21 03:21:07 +0000 | [diff] [blame] | 4213 | if (Record->isAbstract() && Record->hasAttr<FinalAttr>()) { |
| 4214 | Diag(Record->getLocation(), diag::warn_abstract_final_class); |
| 4215 | DiagnoseAbstractType(Record); |
| 4216 | } |
| 4217 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4218 | if (!Record->isDependentType()) { |
| 4219 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 4220 | MEnd = Record->method_end(); |
| 4221 | M != MEnd; ++M) { |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4222 | // See if a method overloads virtual methods in a base |
| 4223 | // class without overriding any. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4224 | if (!M->isStatic()) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4225 | DiagnoseHiddenVirtualMethods(Record, *M); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4226 | |
| 4227 | // Check whether the explicitly-defaulted special members are valid. |
| 4228 | if (!M->isInvalidDecl() && M->isExplicitlyDefaulted()) |
| 4229 | CheckExplicitlyDefaultedSpecialMember(*M); |
| 4230 | |
| 4231 | // For an explicitly defaulted or deleted special member, we defer |
| 4232 | // determining triviality until the class is complete. That time is now! |
| 4233 | if (!M->isImplicit() && !M->isUserProvided()) { |
| 4234 | CXXSpecialMember CSM = getSpecialMember(*M); |
| 4235 | if (CSM != CXXInvalid) { |
| 4236 | M->setTrivial(SpecialMemberIsTrivial(*M, CSM)); |
| 4237 | |
| 4238 | // Inform the class that we've finished declaring this member. |
| 4239 | Record->finishedDefaultedOrDeletedMember(*M); |
| 4240 | } |
| 4241 | } |
| 4242 | } |
| 4243 | } |
| 4244 | |
| 4245 | // C++11 [dcl.constexpr]p8: A constexpr specifier for a non-static member |
| 4246 | // function that is not a constructor declares that member function to be |
| 4247 | // const. [...] The class of which that function is a member shall be |
| 4248 | // a literal type. |
| 4249 | // |
| 4250 | // If the class has virtual bases, any constexpr members will already have |
| 4251 | // been diagnosed by the checks performed on the member declaration, so |
| 4252 | // suppress this (less useful) diagnostic. |
| 4253 | // |
| 4254 | // We delay this until we know whether an explicitly-defaulted (or deleted) |
| 4255 | // destructor for the class is trivial. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4256 | if (LangOpts.CPlusPlus11 && !Record->isDependentType() && |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4257 | !Record->isLiteral() && !Record->getNumVBases()) { |
| 4258 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 4259 | MEnd = Record->method_end(); |
| 4260 | M != MEnd; ++M) { |
| 4261 | if (M->isConstexpr() && M->isInstance() && !isa<CXXConstructorDecl>(*M)) { |
| 4262 | switch (Record->getTemplateSpecializationKind()) { |
| 4263 | case TSK_ImplicitInstantiation: |
| 4264 | case TSK_ExplicitInstantiationDeclaration: |
| 4265 | case TSK_ExplicitInstantiationDefinition: |
| 4266 | // If a template instantiates to a non-literal type, but its members |
| 4267 | // instantiate to constexpr functions, the template is technically |
| 4268 | // ill-formed, but we allow it for sanity. |
| 4269 | continue; |
| 4270 | |
| 4271 | case TSK_Undeclared: |
| 4272 | case TSK_ExplicitSpecialization: |
| 4273 | RequireLiteralType(M->getLocation(), Context.getRecordType(Record), |
| 4274 | diag::err_constexpr_method_non_literal); |
| 4275 | break; |
| 4276 | } |
| 4277 | |
| 4278 | // Only produce one error per class. |
| 4279 | break; |
| 4280 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 4281 | } |
| 4282 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4283 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4284 | // Declare inheriting constructors. We do this eagerly here because: |
| 4285 | // - The standard requires an eager diagnostic for conflicting inheriting |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4286 | // constructors from different classes. |
| 4287 | // - The lazy declaration of the other implicit constructors is so as to not |
| 4288 | // waste space and performance on classes that are not meant to be |
| 4289 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4290 | // have inheriting constructors. |
| 4291 | DeclareInheritingConstructors(Record); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4294 | /// Is the special member function which would be selected to perform the |
| 4295 | /// specified operation on the specified class type a constexpr constructor? |
| 4296 | static bool specialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 4297 | Sema::CXXSpecialMember CSM, |
| 4298 | bool ConstArg) { |
| 4299 | Sema::SpecialMemberOverloadResult *SMOR = |
| 4300 | S.LookupSpecialMember(ClassDecl, CSM, ConstArg, |
| 4301 | false, false, false, false); |
| 4302 | if (!SMOR || !SMOR->getMethod()) |
| 4303 | // A constructor we wouldn't select can't be "involved in initializing" |
| 4304 | // anything. |
| 4305 | return true; |
| 4306 | return SMOR->getMethod()->isConstexpr(); |
| 4307 | } |
| 4308 | |
| 4309 | /// Determine whether the specified special member function would be constexpr |
| 4310 | /// if it were implicitly defined. |
| 4311 | static bool defaultedSpecialMemberIsConstexpr(Sema &S, CXXRecordDecl *ClassDecl, |
| 4312 | Sema::CXXSpecialMember CSM, |
| 4313 | bool ConstArg) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4314 | if (!S.getLangOpts().CPlusPlus11) |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4315 | return false; |
| 4316 | |
| 4317 | // C++11 [dcl.constexpr]p4: |
| 4318 | // In the definition of a constexpr constructor [...] |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4319 | bool Ctor = true; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4320 | switch (CSM) { |
| 4321 | case Sema::CXXDefaultConstructor: |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4322 | // Since default constructor lookup is essentially trivial (and cannot |
| 4323 | // involve, for instance, template instantiation), we compute whether a |
| 4324 | // defaulted default constructor is constexpr directly within CXXRecordDecl. |
| 4325 | // |
| 4326 | // This is important for performance; we need to know whether the default |
| 4327 | // constructor is constexpr to determine whether the type is a literal type. |
| 4328 | return ClassDecl->defaultedDefaultConstructorIsConstexpr(); |
| 4329 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4330 | case Sema::CXXCopyConstructor: |
| 4331 | case Sema::CXXMoveConstructor: |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4332 | // For copy or move constructors, we need to perform overload resolution. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4333 | break; |
| 4334 | |
| 4335 | case Sema::CXXCopyAssignment: |
| 4336 | case Sema::CXXMoveAssignment: |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4337 | if (!S.getLangOpts().CPlusPlus1y) |
| 4338 | return false; |
| 4339 | // In C++1y, we need to perform overload resolution. |
| 4340 | Ctor = false; |
| 4341 | break; |
| 4342 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4343 | case Sema::CXXDestructor: |
| 4344 | case Sema::CXXInvalid: |
| 4345 | return false; |
| 4346 | } |
| 4347 | |
| 4348 | // -- if the class is a non-empty union, or for each non-empty anonymous |
| 4349 | // union member of a non-union class, exactly one non-static data member |
| 4350 | // shall be initialized; [DR1359] |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4351 | // |
| 4352 | // If we squint, this is guaranteed, since exactly one non-static data member |
| 4353 | // will be initialized (if the constructor isn't deleted), we just don't know |
| 4354 | // which one. |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4355 | if (Ctor && ClassDecl->isUnion()) |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4356 | return true; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4357 | |
| 4358 | // -- the class shall not have any virtual base classes; |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4359 | if (Ctor && ClassDecl->getNumVBases()) |
| 4360 | return false; |
| 4361 | |
| 4362 | // C++1y [class.copy]p26: |
| 4363 | // -- [the class] is a literal type, and |
| 4364 | if (!Ctor && !ClassDecl->isLiteral()) |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4365 | return false; |
| 4366 | |
| 4367 | // -- every constructor involved in initializing [...] base class |
| 4368 | // sub-objects shall be a constexpr constructor; |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4369 | // -- the assignment operator selected to copy/move each direct base |
| 4370 | // class is a constexpr function, and |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4371 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4372 | BEnd = ClassDecl->bases_end(); |
| 4373 | B != BEnd; ++B) { |
| 4374 | const RecordType *BaseType = B->getType()->getAs<RecordType>(); |
| 4375 | if (!BaseType) continue; |
| 4376 | |
| 4377 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4378 | if (!specialMemberIsConstexpr(S, BaseClassDecl, CSM, ConstArg)) |
| 4379 | return false; |
| 4380 | } |
| 4381 | |
| 4382 | // -- every constructor involved in initializing non-static data members |
| 4383 | // [...] shall be a constexpr constructor; |
| 4384 | // -- every non-static data member and base class sub-object shall be |
| 4385 | // initialized |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4386 | // -- for each non-stastic data member of X that is of class type (or array |
| 4387 | // thereof), the assignment operator selected to copy/move that member is |
| 4388 | // a constexpr function |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4389 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4390 | FEnd = ClassDecl->field_end(); |
| 4391 | F != FEnd; ++F) { |
| 4392 | if (F->isInvalidDecl()) |
| 4393 | continue; |
Richard Smith | d3861ce | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 4394 | if (const RecordType *RecordTy = |
| 4395 | S.Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4396 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4397 | if (!specialMemberIsConstexpr(S, FieldRecDecl, CSM, ConstArg)) |
| 4398 | return false; |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4399 | } |
| 4400 | } |
| 4401 | |
| 4402 | // All OK, it's constexpr! |
| 4403 | return true; |
| 4404 | } |
| 4405 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4406 | static Sema::ImplicitExceptionSpecification |
| 4407 | computeImplicitExceptionSpec(Sema &S, SourceLocation Loc, CXXMethodDecl *MD) { |
| 4408 | switch (S.getSpecialMember(MD)) { |
| 4409 | case Sema::CXXDefaultConstructor: |
| 4410 | return S.ComputeDefaultedDefaultCtorExceptionSpec(Loc, MD); |
| 4411 | case Sema::CXXCopyConstructor: |
| 4412 | return S.ComputeDefaultedCopyCtorExceptionSpec(MD); |
| 4413 | case Sema::CXXCopyAssignment: |
| 4414 | return S.ComputeDefaultedCopyAssignmentExceptionSpec(MD); |
| 4415 | case Sema::CXXMoveConstructor: |
| 4416 | return S.ComputeDefaultedMoveCtorExceptionSpec(MD); |
| 4417 | case Sema::CXXMoveAssignment: |
| 4418 | return S.ComputeDefaultedMoveAssignmentExceptionSpec(MD); |
| 4419 | case Sema::CXXDestructor: |
| 4420 | return S.ComputeDefaultedDtorExceptionSpec(MD); |
| 4421 | case Sema::CXXInvalid: |
| 4422 | break; |
| 4423 | } |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 4424 | assert(cast<CXXConstructorDecl>(MD)->getInheritedConstructor() && |
| 4425 | "only special members have implicit exception specs"); |
| 4426 | return S.ComputeInheritingCtorExceptionSpec(cast<CXXConstructorDecl>(MD)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4429 | static void |
| 4430 | updateExceptionSpec(Sema &S, FunctionDecl *FD, const FunctionProtoType *FPT, |
| 4431 | const Sema::ImplicitExceptionSpecification &ExceptSpec) { |
| 4432 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 4433 | ExceptSpec.getEPI(EPI); |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 4434 | FD->setType(S.Context.getFunctionType(FPT->getResultType(), |
| 4435 | FPT->getArgTypes(), EPI)); |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4438 | void Sema::EvaluateImplicitExceptionSpec(SourceLocation Loc, CXXMethodDecl *MD) { |
| 4439 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
| 4440 | if (FPT->getExceptionSpecType() != EST_Unevaluated) |
| 4441 | return; |
| 4442 | |
Richard Smith | dd25e80 | 2012-07-30 23:48:14 +0000 | [diff] [blame] | 4443 | // Evaluate the exception specification. |
| 4444 | ImplicitExceptionSpecification ExceptSpec = |
| 4445 | computeImplicitExceptionSpec(*this, Loc, MD); |
| 4446 | |
| 4447 | // Update the type of the special member to use it. |
| 4448 | updateExceptionSpec(*this, MD, FPT, ExceptSpec); |
| 4449 | |
| 4450 | // A user-provided destructor can be defined outside the class. When that |
| 4451 | // happens, be sure to update the exception specification on both |
| 4452 | // declarations. |
| 4453 | const FunctionProtoType *CanonicalFPT = |
| 4454 | MD->getCanonicalDecl()->getType()->castAs<FunctionProtoType>(); |
| 4455 | if (CanonicalFPT->getExceptionSpecType() == EST_Unevaluated) |
| 4456 | updateExceptionSpec(*this, MD->getCanonicalDecl(), |
| 4457 | CanonicalFPT, ExceptSpec); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4460 | void Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD) { |
| 4461 | CXXRecordDecl *RD = MD->getParent(); |
| 4462 | CXXSpecialMember CSM = getSpecialMember(MD); |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 4463 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4464 | assert(MD->isExplicitlyDefaulted() && CSM != CXXInvalid && |
| 4465 | "not an explicitly-defaulted special member"); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 4466 | |
| 4467 | // Whether this was the first-declared instance of the constructor. |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4468 | // This affects whether we implicitly add an exception spec and constexpr. |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4469 | bool First = MD == MD->getCanonicalDecl(); |
| 4470 | |
| 4471 | bool HadError = false; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4472 | |
| 4473 | // C++11 [dcl.fct.def.default]p1: |
| 4474 | // A function that is explicitly defaulted shall |
| 4475 | // -- be a special member function (checked elsewhere), |
| 4476 | // -- have the same type (except for ref-qualifiers, and except that a |
| 4477 | // copy operation can take a non-const reference) as an implicit |
| 4478 | // declaration, and |
| 4479 | // -- not have default arguments. |
| 4480 | unsigned ExpectedParams = 1; |
| 4481 | if (CSM == CXXDefaultConstructor || CSM == CXXDestructor) |
| 4482 | ExpectedParams = 0; |
| 4483 | if (MD->getNumParams() != ExpectedParams) { |
| 4484 | // This also checks for default arguments: a copy or move constructor with a |
| 4485 | // default argument is classified as a default constructor, and assignment |
| 4486 | // operations and destructors can't have default arguments. |
| 4487 | Diag(MD->getLocation(), diag::err_defaulted_special_member_params) |
| 4488 | << CSM << MD->getSourceRange(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4489 | HadError = true; |
Richard Smith | 5046439 | 2012-12-07 02:10:28 +0000 | [diff] [blame] | 4490 | } else if (MD->isVariadic()) { |
| 4491 | Diag(MD->getLocation(), diag::err_defaulted_special_member_variadic) |
| 4492 | << CSM << MD->getSourceRange(); |
| 4493 | HadError = true; |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4494 | } |
| 4495 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4496 | const FunctionProtoType *Type = MD->getType()->getAs<FunctionProtoType>(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4497 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4498 | bool CanHaveConstParam = false; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4499 | if (CSM == CXXCopyConstructor) |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 4500 | CanHaveConstParam = RD->implicitCopyConstructorHasConstParam(); |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4501 | else if (CSM == CXXCopyAssignment) |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 4502 | CanHaveConstParam = RD->implicitCopyAssignmentHasConstParam(); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4503 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4504 | QualType ReturnType = Context.VoidTy; |
| 4505 | if (CSM == CXXCopyAssignment || CSM == CXXMoveAssignment) { |
| 4506 | // Check for return type matching. |
| 4507 | ReturnType = Type->getResultType(); |
| 4508 | QualType ExpectedReturnType = |
| 4509 | Context.getLValueReferenceType(Context.getTypeDeclType(RD)); |
| 4510 | if (!Context.hasSameType(ReturnType, ExpectedReturnType)) { |
| 4511 | Diag(MD->getLocation(), diag::err_defaulted_special_member_return_type) |
| 4512 | << (CSM == CXXMoveAssignment) << ExpectedReturnType; |
| 4513 | HadError = true; |
| 4514 | } |
| 4515 | |
| 4516 | // A defaulted special member cannot have cv-qualifiers. |
| 4517 | if (Type->getTypeQuals()) { |
| 4518 | Diag(MD->getLocation(), diag::err_defaulted_special_member_quals) |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4519 | << (CSM == CXXMoveAssignment) << getLangOpts().CPlusPlus1y; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4520 | HadError = true; |
| 4521 | } |
| 4522 | } |
| 4523 | |
| 4524 | // Check for parameter type matching. |
| 4525 | QualType ArgType = ExpectedParams ? Type->getArgType(0) : QualType(); |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4526 | bool HasConstParam = false; |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4527 | if (ExpectedParams && ArgType->isReferenceType()) { |
| 4528 | // Argument must be reference to possibly-const T. |
| 4529 | QualType ReferentType = ArgType->getPointeeType(); |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4530 | HasConstParam = ReferentType.isConstQualified(); |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4531 | |
| 4532 | if (ReferentType.isVolatileQualified()) { |
| 4533 | Diag(MD->getLocation(), |
| 4534 | diag::err_defaulted_special_member_volatile_param) << CSM; |
| 4535 | HadError = true; |
| 4536 | } |
| 4537 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4538 | if (HasConstParam && !CanHaveConstParam) { |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4539 | if (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment) { |
| 4540 | Diag(MD->getLocation(), |
| 4541 | diag::err_defaulted_special_member_copy_const_param) |
| 4542 | << (CSM == CXXCopyAssignment); |
| 4543 | // FIXME: Explain why this special member can't be const. |
| 4544 | } else { |
| 4545 | Diag(MD->getLocation(), |
| 4546 | diag::err_defaulted_special_member_move_const_param) |
| 4547 | << (CSM == CXXMoveAssignment); |
| 4548 | } |
| 4549 | HadError = true; |
| 4550 | } |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4551 | } else if (ExpectedParams) { |
| 4552 | // A copy assignment operator can take its argument by value, but a |
| 4553 | // defaulted one cannot. |
| 4554 | assert(CSM == CXXCopyAssignment && "unexpected non-ref argument"); |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4555 | Diag(MD->getLocation(), diag::err_defaulted_copy_assign_not_ref); |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 4556 | HadError = true; |
| 4557 | } |
Sean Hunt | be63122 | 2011-05-17 20:44:43 +0000 | [diff] [blame] | 4558 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4559 | // C++11 [dcl.fct.def.default]p2: |
| 4560 | // An explicitly-defaulted function may be declared constexpr only if it |
| 4561 | // would have been implicitly declared as constexpr, |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4562 | // Do not apply this rule to members of class templates, since core issue 1358 |
| 4563 | // makes such functions always instantiate to constexpr functions. For |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4564 | // functions which cannot be constexpr (for non-constructors in C++11 and for |
| 4565 | // destructors in C++1y), this is checked elsewhere. |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 4566 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, RD, CSM, |
| 4567 | HasConstParam); |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4568 | if ((getLangOpts().CPlusPlus1y ? !isa<CXXDestructorDecl>(MD) |
| 4569 | : isa<CXXConstructorDecl>(MD)) && |
| 4570 | MD->isConstexpr() && !Constexpr && |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4571 | MD->getTemplatedKind() == FunctionDecl::TK_NonTemplate) { |
| 4572 | Diag(MD->getLocStart(), diag::err_incorrect_defaulted_constexpr) << CSM; |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4573 | // FIXME: Explain why the special member can't be constexpr. |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4574 | HadError = true; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4575 | } |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4576 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4577 | // and may have an explicit exception-specification only if it is compatible |
| 4578 | // with the exception-specification on the implicit declaration. |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4579 | if (Type->hasExceptionSpec()) { |
| 4580 | // Delay the check if this is the first declaration of the special member, |
| 4581 | // since we may not have parsed some necessary in-class initializers yet. |
Richard Smith | 12fef49 | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 4582 | if (First) { |
| 4583 | // If the exception specification needs to be instantiated, do so now, |
| 4584 | // before we clobber it with an EST_Unevaluated specification below. |
| 4585 | if (Type->getExceptionSpecType() == EST_Uninstantiated) { |
| 4586 | InstantiateExceptionSpec(MD->getLocStart(), MD); |
| 4587 | Type = MD->getType()->getAs<FunctionProtoType>(); |
| 4588 | } |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4589 | DelayedDefaultedMemberExceptionSpecs.push_back(std::make_pair(MD, Type)); |
Richard Smith | 12fef49 | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 4590 | } else |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4591 | CheckExplicitlyDefaultedMemberExceptionSpec(MD, Type); |
| 4592 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4593 | |
| 4594 | // If a function is explicitly defaulted on its first declaration, |
| 4595 | if (First) { |
| 4596 | // -- it is implicitly considered to be constexpr if the implicit |
| 4597 | // definition would be, |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4598 | MD->setConstexpr(Constexpr); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4599 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4600 | // -- it is implicitly considered to have the same exception-specification |
| 4601 | // as if it had been implicitly declared, |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4602 | FunctionProtoType::ExtProtoInfo EPI = Type->getExtProtoInfo(); |
| 4603 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 4604 | EPI.ExceptionSpecDecl = MD; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 4605 | MD->setType(Context.getFunctionType(ReturnType, |
| 4606 | ArrayRef<QualType>(&ArgType, |
| 4607 | ExpectedParams), |
| 4608 | EPI)); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4611 | if (ShouldDeleteSpecialMember(MD, CSM)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4612 | if (First) { |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 4613 | SetDeclDeleted(MD, MD->getLocation()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4614 | } else { |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4615 | // C++11 [dcl.fct.def.default]p4: |
| 4616 | // [For a] user-provided explicitly-defaulted function [...] if such a |
| 4617 | // function is implicitly defined as deleted, the program is ill-formed. |
| 4618 | Diag(MD->getLocation(), diag::err_out_of_line_default_deletes) << CSM; |
| 4619 | HadError = true; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4620 | } |
| 4621 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 4622 | |
Richard Smith | 3003e1d | 2012-05-15 04:39:51 +0000 | [diff] [blame] | 4623 | if (HadError) |
| 4624 | MD->setInvalidDecl(); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 4625 | } |
| 4626 | |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4627 | /// Check whether the exception specification provided for an |
| 4628 | /// explicitly-defaulted special member matches the exception specification |
| 4629 | /// that would have been generated for an implicit special member, per |
| 4630 | /// C++11 [dcl.fct.def.default]p2. |
| 4631 | void Sema::CheckExplicitlyDefaultedMemberExceptionSpec( |
| 4632 | CXXMethodDecl *MD, const FunctionProtoType *SpecifiedType) { |
| 4633 | // Compute the implicit exception specification. |
| 4634 | FunctionProtoType::ExtProtoInfo EPI; |
| 4635 | computeImplicitExceptionSpec(*this, MD->getLocation(), MD).getEPI(EPI); |
| 4636 | const FunctionProtoType *ImplicitType = cast<FunctionProtoType>( |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 4637 | Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4638 | |
| 4639 | // Ensure that it matches. |
| 4640 | CheckEquivalentExceptionSpec( |
| 4641 | PDiag(diag::err_incorrect_defaulted_exception_spec) |
| 4642 | << getSpecialMember(MD), PDiag(), |
| 4643 | ImplicitType, SourceLocation(), |
| 4644 | SpecifiedType, MD->getLocation()); |
| 4645 | } |
| 4646 | |
| 4647 | void Sema::CheckDelayedExplicitlyDefaultedMemberExceptionSpecs() { |
| 4648 | for (unsigned I = 0, N = DelayedDefaultedMemberExceptionSpecs.size(); |
| 4649 | I != N; ++I) |
| 4650 | CheckExplicitlyDefaultedMemberExceptionSpec( |
| 4651 | DelayedDefaultedMemberExceptionSpecs[I].first, |
| 4652 | DelayedDefaultedMemberExceptionSpecs[I].second); |
| 4653 | |
| 4654 | DelayedDefaultedMemberExceptionSpecs.clear(); |
| 4655 | } |
| 4656 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4657 | namespace { |
| 4658 | struct SpecialMemberDeletionInfo { |
| 4659 | Sema &S; |
| 4660 | CXXMethodDecl *MD; |
| 4661 | Sema::CXXSpecialMember CSM; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4662 | bool Diagnose; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4663 | |
| 4664 | // Properties of the special member, computed for convenience. |
| 4665 | bool IsConstructor, IsAssignment, IsMove, ConstArg, VolatileArg; |
| 4666 | SourceLocation Loc; |
| 4667 | |
| 4668 | bool AllFieldsAreConst; |
| 4669 | |
| 4670 | SpecialMemberDeletionInfo(Sema &S, CXXMethodDecl *MD, |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4671 | Sema::CXXSpecialMember CSM, bool Diagnose) |
| 4672 | : S(S), MD(MD), CSM(CSM), Diagnose(Diagnose), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4673 | IsConstructor(false), IsAssignment(false), IsMove(false), |
| 4674 | ConstArg(false), VolatileArg(false), Loc(MD->getLocation()), |
| 4675 | AllFieldsAreConst(true) { |
| 4676 | switch (CSM) { |
| 4677 | case Sema::CXXDefaultConstructor: |
| 4678 | case Sema::CXXCopyConstructor: |
| 4679 | IsConstructor = true; |
| 4680 | break; |
| 4681 | case Sema::CXXMoveConstructor: |
| 4682 | IsConstructor = true; |
| 4683 | IsMove = true; |
| 4684 | break; |
| 4685 | case Sema::CXXCopyAssignment: |
| 4686 | IsAssignment = true; |
| 4687 | break; |
| 4688 | case Sema::CXXMoveAssignment: |
| 4689 | IsAssignment = true; |
| 4690 | IsMove = true; |
| 4691 | break; |
| 4692 | case Sema::CXXDestructor: |
| 4693 | break; |
| 4694 | case Sema::CXXInvalid: |
| 4695 | llvm_unreachable("invalid special member kind"); |
| 4696 | } |
| 4697 | |
| 4698 | if (MD->getNumParams()) { |
| 4699 | ConstArg = MD->getParamDecl(0)->getType().isConstQualified(); |
| 4700 | VolatileArg = MD->getParamDecl(0)->getType().isVolatileQualified(); |
| 4701 | } |
| 4702 | } |
| 4703 | |
| 4704 | bool inUnion() const { return MD->getParent()->isUnion(); } |
| 4705 | |
| 4706 | /// Look up the corresponding special member in the given class. |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4707 | Sema::SpecialMemberOverloadResult *lookupIn(CXXRecordDecl *Class, |
| 4708 | unsigned Quals) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4709 | unsigned TQ = MD->getTypeQualifiers(); |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4710 | // cv-qualifiers on class members don't affect default ctor / dtor calls. |
| 4711 | if (CSM == Sema::CXXDefaultConstructor || CSM == Sema::CXXDestructor) |
| 4712 | Quals = 0; |
| 4713 | return S.LookupSpecialMember(Class, CSM, |
| 4714 | ConstArg || (Quals & Qualifiers::Const), |
| 4715 | VolatileArg || (Quals & Qualifiers::Volatile), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4716 | MD->getRefQualifier() == RQ_RValue, |
| 4717 | TQ & Qualifiers::Const, |
| 4718 | TQ & Qualifiers::Volatile); |
| 4719 | } |
| 4720 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4721 | typedef llvm::PointerUnion<CXXBaseSpecifier*, FieldDecl*> Subobject; |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4722 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4723 | bool shouldDeleteForBase(CXXBaseSpecifier *Base); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4724 | bool shouldDeleteForField(FieldDecl *FD); |
| 4725 | bool shouldDeleteForAllConstMembers(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4726 | |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4727 | bool shouldDeleteForClassSubobject(CXXRecordDecl *Class, Subobject Subobj, |
| 4728 | unsigned Quals); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4729 | bool shouldDeleteForSubobjectCall(Subobject Subobj, |
| 4730 | Sema::SpecialMemberOverloadResult *SMOR, |
| 4731 | bool IsDtorCallInCtor); |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4732 | |
| 4733 | bool isAccessible(Subobject Subobj, CXXMethodDecl *D); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4734 | }; |
| 4735 | } |
| 4736 | |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4737 | /// Is the given special member inaccessible when used on the given |
| 4738 | /// sub-object. |
| 4739 | bool SpecialMemberDeletionInfo::isAccessible(Subobject Subobj, |
| 4740 | CXXMethodDecl *target) { |
| 4741 | /// If we're operating on a base class, the object type is the |
| 4742 | /// type of this special member. |
| 4743 | QualType objectTy; |
Dmitri Gribenko | 1ad23d6 | 2012-09-10 21:20:09 +0000 | [diff] [blame] | 4744 | AccessSpecifier access = target->getAccess(); |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4745 | if (CXXBaseSpecifier *base = Subobj.dyn_cast<CXXBaseSpecifier*>()) { |
| 4746 | objectTy = S.Context.getTypeDeclType(MD->getParent()); |
| 4747 | access = CXXRecordDecl::MergeAccess(base->getAccessSpecifier(), access); |
| 4748 | |
| 4749 | // If we're operating on a field, the object type is the type of the field. |
| 4750 | } else { |
| 4751 | objectTy = S.Context.getTypeDeclType(target->getParent()); |
| 4752 | } |
| 4753 | |
| 4754 | return S.isSpecialMemberAccessibleForDeletion(target, access, objectTy); |
| 4755 | } |
| 4756 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4757 | /// Check whether we should delete a special member due to the implicit |
| 4758 | /// definition containing a call to a special member of a subobject. |
| 4759 | bool SpecialMemberDeletionInfo::shouldDeleteForSubobjectCall( |
| 4760 | Subobject Subobj, Sema::SpecialMemberOverloadResult *SMOR, |
| 4761 | bool IsDtorCallInCtor) { |
| 4762 | CXXMethodDecl *Decl = SMOR->getMethod(); |
| 4763 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
| 4764 | |
| 4765 | int DiagKind = -1; |
| 4766 | |
| 4767 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::NoMemberOrDeleted) |
| 4768 | DiagKind = !Decl ? 0 : 1; |
| 4769 | else if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 4770 | DiagKind = 2; |
John McCall | 12d8d80 | 2012-04-09 20:53:23 +0000 | [diff] [blame] | 4771 | else if (!isAccessible(Subobj, Decl)) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4772 | DiagKind = 3; |
| 4773 | else if (!IsDtorCallInCtor && Field && Field->getParent()->isUnion() && |
| 4774 | !Decl->isTrivial()) { |
| 4775 | // A member of a union must have a trivial corresponding special member. |
| 4776 | // As a weird special case, a destructor call from a union's constructor |
| 4777 | // must be accessible and non-deleted, but need not be trivial. Such a |
| 4778 | // destructor is never actually called, but is semantically checked as |
| 4779 | // if it were. |
| 4780 | DiagKind = 4; |
| 4781 | } |
| 4782 | |
| 4783 | if (DiagKind == -1) |
| 4784 | return false; |
| 4785 | |
| 4786 | if (Diagnose) { |
| 4787 | if (Field) { |
| 4788 | S.Diag(Field->getLocation(), |
| 4789 | diag::note_deleted_special_member_class_subobject) |
| 4790 | << CSM << MD->getParent() << /*IsField*/true |
| 4791 | << Field << DiagKind << IsDtorCallInCtor; |
| 4792 | } else { |
| 4793 | CXXBaseSpecifier *Base = Subobj.get<CXXBaseSpecifier*>(); |
| 4794 | S.Diag(Base->getLocStart(), |
| 4795 | diag::note_deleted_special_member_class_subobject) |
| 4796 | << CSM << MD->getParent() << /*IsField*/false |
| 4797 | << Base->getType() << DiagKind << IsDtorCallInCtor; |
| 4798 | } |
| 4799 | |
| 4800 | if (DiagKind == 1) |
| 4801 | S.NoteDeletedFunction(Decl); |
| 4802 | // FIXME: Explain inaccessibility if DiagKind == 3. |
| 4803 | } |
| 4804 | |
| 4805 | return true; |
| 4806 | } |
| 4807 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4808 | /// 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] | 4809 | /// 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] | 4810 | bool SpecialMemberDeletionInfo::shouldDeleteForClassSubobject( |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4811 | CXXRecordDecl *Class, Subobject Subobj, unsigned Quals) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4812 | FieldDecl *Field = Subobj.dyn_cast<FieldDecl*>(); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4813 | |
| 4814 | // C++11 [class.ctor]p5: |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4815 | // -- any direct or virtual base class, or non-static data member with no |
| 4816 | // brace-or-equal-initializer, has class type M (or array thereof) and |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4817 | // either M has no default constructor or overload resolution as applied |
| 4818 | // to M's default constructor results in an ambiguity or in a function |
| 4819 | // that is deleted or inaccessible |
| 4820 | // C++11 [class.copy]p11, C++11 [class.copy]p23: |
| 4821 | // -- a direct or virtual base class B that cannot be copied/moved because |
| 4822 | // overload resolution, as applied to B's corresponding special member, |
| 4823 | // results in an ambiguity or a function that is deleted or inaccessible |
| 4824 | // from the defaulted special member |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4825 | // C++11 [class.dtor]p5: |
| 4826 | // -- any direct or virtual base class [...] has a type with a destructor |
| 4827 | // that is deleted or inaccessible |
| 4828 | if (!(CSM == Sema::CXXDefaultConstructor && |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4829 | Field && Field->hasInClassInitializer()) && |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4830 | shouldDeleteForSubobjectCall(Subobj, lookupIn(Class, Quals), false)) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4831 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4832 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4833 | // C++11 [class.ctor]p5, C++11 [class.copy]p11: |
| 4834 | // -- any direct or virtual base class or non-static data member has a |
| 4835 | // type with a destructor that is deleted or inaccessible |
| 4836 | if (IsConstructor) { |
| 4837 | Sema::SpecialMemberOverloadResult *SMOR = |
| 4838 | S.LookupSpecialMember(Class, Sema::CXXDestructor, |
| 4839 | false, false, false, false, false); |
| 4840 | if (shouldDeleteForSubobjectCall(Subobj, SMOR, true)) |
| 4841 | return true; |
| 4842 | } |
| 4843 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4844 | return false; |
| 4845 | } |
| 4846 | |
| 4847 | /// Check whether we should delete a special member function due to the class |
| 4848 | /// having a particular direct or virtual base class. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4849 | bool SpecialMemberDeletionInfo::shouldDeleteForBase(CXXBaseSpecifier *Base) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 4850 | CXXRecordDecl *BaseClass = Base->getType()->getAsCXXRecordDecl(); |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4851 | return shouldDeleteForClassSubobject(BaseClass, Base, 0); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4852 | } |
| 4853 | |
| 4854 | /// Check whether we should delete a special member function due to the class |
| 4855 | /// having a particular non-static data member. |
| 4856 | bool SpecialMemberDeletionInfo::shouldDeleteForField(FieldDecl *FD) { |
| 4857 | QualType FieldType = S.Context.getBaseElementType(FD->getType()); |
| 4858 | CXXRecordDecl *FieldRecord = FieldType->getAsCXXRecordDecl(); |
| 4859 | |
| 4860 | if (CSM == Sema::CXXDefaultConstructor) { |
| 4861 | // For a default constructor, all references must be initialized in-class |
| 4862 | // and, if a union, it must have a non-const member. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4863 | if (FieldType->isReferenceType() && !FD->hasInClassInitializer()) { |
| 4864 | if (Diagnose) |
| 4865 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
| 4866 | << MD->getParent() << FD << FieldType << /*Reference*/0; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4867 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4868 | } |
Richard Smith | 79363f5 | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 4869 | // C++11 [class.ctor]p5: any non-variant non-static data member of |
| 4870 | // const-qualified type (or array thereof) with no |
| 4871 | // brace-or-equal-initializer does not have a user-provided default |
| 4872 | // constructor. |
| 4873 | if (!inUnion() && FieldType.isConstQualified() && |
| 4874 | !FD->hasInClassInitializer() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4875 | (!FieldRecord || !FieldRecord->hasUserProvidedDefaultConstructor())) { |
| 4876 | if (Diagnose) |
| 4877 | S.Diag(FD->getLocation(), diag::note_deleted_default_ctor_uninit_field) |
Richard Smith | a2e76f5 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 4878 | << MD->getParent() << FD << FD->getType() << /*Const*/1; |
Richard Smith | 79363f5 | 2012-02-27 06:07:25 +0000 | [diff] [blame] | 4879 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4880 | } |
| 4881 | |
| 4882 | if (inUnion() && !FieldType.isConstQualified()) |
| 4883 | AllFieldsAreConst = false; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4884 | } else if (CSM == Sema::CXXCopyConstructor) { |
| 4885 | // For a copy constructor, data members must not be of rvalue reference |
| 4886 | // type. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4887 | if (FieldType->isRValueReferenceType()) { |
| 4888 | if (Diagnose) |
| 4889 | S.Diag(FD->getLocation(), diag::note_deleted_copy_ctor_rvalue_reference) |
| 4890 | << MD->getParent() << FD << FieldType; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4891 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4892 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4893 | } else if (IsAssignment) { |
| 4894 | // For an assignment operator, data members must not be of reference type. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4895 | if (FieldType->isReferenceType()) { |
| 4896 | if (Diagnose) |
| 4897 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
| 4898 | << IsMove << MD->getParent() << FD << FieldType << /*Reference*/0; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4899 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4900 | } |
| 4901 | if (!FieldRecord && FieldType.isConstQualified()) { |
| 4902 | // C++11 [class.copy]p23: |
| 4903 | // -- a non-static data member of const non-class type (or array thereof) |
| 4904 | if (Diagnose) |
| 4905 | S.Diag(FD->getLocation(), diag::note_deleted_assign_field) |
Richard Smith | a2e76f5 | 2012-04-29 06:32:34 +0000 | [diff] [blame] | 4906 | << IsMove << MD->getParent() << FD << FD->getType() << /*Const*/1; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4907 | return true; |
| 4908 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | if (FieldRecord) { |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4912 | // Some additional restrictions exist on the variant members. |
| 4913 | if (!inUnion() && FieldRecord->isUnion() && |
| 4914 | FieldRecord->isAnonymousStructOrUnion()) { |
| 4915 | bool AllVariantFieldsAreConst = true; |
| 4916 | |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4917 | // FIXME: Handle anonymous unions declared within anonymous unions. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4918 | for (CXXRecordDecl::field_iterator UI = FieldRecord->field_begin(), |
| 4919 | UE = FieldRecord->field_end(); |
| 4920 | UI != UE; ++UI) { |
| 4921 | QualType UnionFieldType = S.Context.getBaseElementType(UI->getType()); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4922 | |
| 4923 | if (!UnionFieldType.isConstQualified()) |
| 4924 | AllVariantFieldsAreConst = false; |
| 4925 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4926 | CXXRecordDecl *UnionFieldRecord = UnionFieldType->getAsCXXRecordDecl(); |
| 4927 | if (UnionFieldRecord && |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4928 | shouldDeleteForClassSubobject(UnionFieldRecord, *UI, |
| 4929 | UnionFieldType.getCVRQualifiers())) |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 4930 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4931 | } |
| 4932 | |
| 4933 | // At least one member in each anonymous union must be non-const |
Douglas Gregor | 221c27f | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 4934 | if (CSM == Sema::CXXDefaultConstructor && AllVariantFieldsAreConst && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4935 | FieldRecord->field_begin() != FieldRecord->field_end()) { |
| 4936 | if (Diagnose) |
| 4937 | S.Diag(FieldRecord->getLocation(), |
| 4938 | diag::note_deleted_default_ctor_all_const) |
| 4939 | << MD->getParent() << /*anonymous union*/1; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4940 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4941 | } |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4942 | |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4943 | // Don't check the implicit member of the anonymous union type. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4944 | // This is technically non-conformant, but sanity demands it. |
| 4945 | return false; |
| 4946 | } |
| 4947 | |
Richard Smith | 517bb84 | 2012-07-18 03:51:16 +0000 | [diff] [blame] | 4948 | if (shouldDeleteForClassSubobject(FieldRecord, FD, |
| 4949 | FieldType.getCVRQualifiers())) |
Richard Smith | df8dc86 | 2012-03-29 19:00:10 +0000 | [diff] [blame] | 4950 | return true; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
| 4953 | return false; |
| 4954 | } |
| 4955 | |
| 4956 | /// C++11 [class.ctor] p5: |
| 4957 | /// A defaulted default constructor for a class X is defined as deleted if |
| 4958 | /// X is a union and all of its variant members are of const-qualified type. |
| 4959 | bool SpecialMemberDeletionInfo::shouldDeleteForAllConstMembers() { |
Douglas Gregor | 221c27f | 2012-02-24 21:25:53 +0000 | [diff] [blame] | 4960 | // This is a silly definition, because it gives an empty union a deleted |
| 4961 | // default constructor. Don't do that. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4962 | if (CSM == Sema::CXXDefaultConstructor && inUnion() && AllFieldsAreConst && |
| 4963 | (MD->getParent()->field_begin() != MD->getParent()->field_end())) { |
| 4964 | if (Diagnose) |
| 4965 | S.Diag(MD->getParent()->getLocation(), |
| 4966 | diag::note_deleted_default_ctor_all_const) |
| 4967 | << MD->getParent() << /*not anonymous union*/0; |
| 4968 | return true; |
| 4969 | } |
| 4970 | return false; |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4971 | } |
| 4972 | |
| 4973 | /// Determine whether a defaulted special member function should be defined as |
| 4974 | /// deleted, as specified in C++11 [class.ctor]p5, C++11 [class.copy]p11, |
| 4975 | /// C++11 [class.copy]p23, and C++11 [class.dtor]p5. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4976 | bool Sema::ShouldDeleteSpecialMember(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 4977 | bool Diagnose) { |
Richard Smith | eef0029 | 2012-08-06 02:25:10 +0000 | [diff] [blame] | 4978 | if (MD->isInvalidDecl()) |
| 4979 | return false; |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 4980 | CXXRecordDecl *RD = MD->getParent(); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4981 | assert(!RD->isDependentType() && "do deletion after instantiation"); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4982 | if (!LangOpts.CPlusPlus11 || RD->isInvalidDecl()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 4983 | return false; |
| 4984 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4985 | // C++11 [expr.lambda.prim]p19: |
| 4986 | // The closure type associated with a lambda-expression has a |
| 4987 | // deleted (8.4.3) default constructor and a deleted copy |
| 4988 | // assignment operator. |
| 4989 | if (RD->isLambda() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4990 | (CSM == CXXDefaultConstructor || CSM == CXXCopyAssignment)) { |
| 4991 | if (Diagnose) |
| 4992 | Diag(RD->getLocation(), diag::note_lambda_decl); |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 4993 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 4996 | // For an anonymous struct or union, the copy and assignment special members |
| 4997 | // will never be used, so skip the check. For an anonymous union declared at |
| 4998 | // namespace scope, the constructor and destructor are used. |
| 4999 | if (CSM != CXXDefaultConstructor && CSM != CXXDestructor && |
| 5000 | RD->isAnonymousStructOrUnion()) |
| 5001 | return false; |
| 5002 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5003 | // C++11 [class.copy]p7, p18: |
| 5004 | // If the class definition declares a move constructor or move assignment |
| 5005 | // operator, an implicitly declared copy constructor or copy assignment |
| 5006 | // operator is defined as deleted. |
| 5007 | if (MD->isImplicit() && |
| 5008 | (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment)) { |
| 5009 | CXXMethodDecl *UserDeclaredMove = 0; |
| 5010 | |
| 5011 | // In Microsoft mode, a user-declared move only causes the deletion of the |
| 5012 | // corresponding copy operation, not both copy operations. |
| 5013 | if (RD->hasUserDeclaredMoveConstructor() && |
| 5014 | (!getLangOpts().MicrosoftMode || CSM == CXXCopyConstructor)) { |
| 5015 | if (!Diagnose) return true; |
Richard Smith | 5579865 | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5016 | |
| 5017 | // Find any user-declared move constructor. |
| 5018 | for (CXXRecordDecl::ctor_iterator I = RD->ctor_begin(), |
| 5019 | E = RD->ctor_end(); I != E; ++I) { |
| 5020 | if (I->isMoveConstructor()) { |
| 5021 | UserDeclaredMove = *I; |
| 5022 | break; |
| 5023 | } |
| 5024 | } |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5025 | assert(UserDeclaredMove); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5026 | } else if (RD->hasUserDeclaredMoveAssignment() && |
| 5027 | (!getLangOpts().MicrosoftMode || CSM == CXXCopyAssignment)) { |
| 5028 | if (!Diagnose) return true; |
Richard Smith | 5579865 | 2012-12-08 04:10:18 +0000 | [diff] [blame] | 5029 | |
| 5030 | // Find any user-declared move assignment operator. |
| 5031 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 5032 | E = RD->method_end(); I != E; ++I) { |
| 5033 | if (I->isMoveAssignmentOperator()) { |
| 5034 | UserDeclaredMove = *I; |
| 5035 | break; |
| 5036 | } |
| 5037 | } |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 5038 | assert(UserDeclaredMove); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5039 | } |
| 5040 | |
| 5041 | if (UserDeclaredMove) { |
| 5042 | Diag(UserDeclaredMove->getLocation(), |
| 5043 | diag::note_deleted_copy_user_declared_move) |
Richard Smith | e6af660 | 2012-04-02 21:07:48 +0000 | [diff] [blame] | 5044 | << (CSM == CXXCopyAssignment) << RD |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5045 | << UserDeclaredMove->isMoveAssignmentOperator(); |
| 5046 | return true; |
| 5047 | } |
| 5048 | } |
Sean Hunt | e16da07 | 2011-10-10 06:18:57 +0000 | [diff] [blame] | 5049 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 5050 | // Do access control from the special member function |
| 5051 | ContextRAII MethodContext(*this, MD); |
| 5052 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5053 | // C++11 [class.dtor]p5: |
| 5054 | // -- for a virtual destructor, lookup of the non-array deallocation function |
| 5055 | // 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] | 5056 | if (CSM == CXXDestructor && MD->isVirtual()) { |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5057 | FunctionDecl *OperatorDelete = 0; |
| 5058 | DeclarationName Name = |
| 5059 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| 5060 | if (FindDeallocationFunction(MD->getLocation(), MD->getParent(), Name, |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5061 | OperatorDelete, false)) { |
| 5062 | if (Diagnose) |
| 5063 | Diag(RD->getLocation(), diag::note_deleted_dtor_no_operator_delete); |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5064 | return true; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5065 | } |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 5066 | } |
| 5067 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5068 | SpecialMemberDeletionInfo SMI(*this, MD, CSM, Diagnose); |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5069 | |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5070 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5071 | BE = RD->bases_end(); BI != BE; ++BI) |
| 5072 | if (!BI->isVirtual() && |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5073 | SMI.shouldDeleteForBase(BI)) |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5074 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5075 | |
Richard Smith | e088360 | 2013-07-22 18:06:23 +0000 | [diff] [blame] | 5076 | // Per DR1611, do not consider virtual bases of constructors of abstract |
| 5077 | // classes, since we are not going to construct them. |
Richard Smith | cbc820a | 2013-07-22 02:56:56 +0000 | [diff] [blame] | 5078 | if (!RD->isAbstract() || !SMI.IsConstructor) { |
| 5079 | for (CXXRecordDecl::base_class_iterator BI = RD->vbases_begin(), |
| 5080 | BE = RD->vbases_end(); |
| 5081 | BI != BE; ++BI) |
| 5082 | if (SMI.shouldDeleteForBase(BI)) |
| 5083 | return true; |
| 5084 | } |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5085 | |
| 5086 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5087 | FE = RD->field_end(); FI != FE; ++FI) |
| 5088 | if (!FI->isInvalidDecl() && !FI->isUnnamedBitfield() && |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5089 | SMI.shouldDeleteForField(*FI)) |
Sean Hunt | e340682 | 2011-05-20 21:43:47 +0000 | [diff] [blame] | 5090 | return true; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5091 | |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 5092 | if (SMI.shouldDeleteForAllConstMembers()) |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 5093 | return true; |
| 5094 | |
| 5095 | return false; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5098 | /// Perform lookup for a special member of the specified kind, and determine |
| 5099 | /// whether it is trivial. If the triviality can be determined without the |
| 5100 | /// lookup, skip it. This is intended for use when determining whether a |
| 5101 | /// special member of a containing object is trivial, and thus does not ever |
| 5102 | /// perform overload resolution for default constructors. |
| 5103 | /// |
| 5104 | /// If \p Selected is not \c NULL, \c *Selected will be filled in with the |
| 5105 | /// member that was most likely to be intended to be trivial, if any. |
| 5106 | static bool findTrivialSpecialMember(Sema &S, CXXRecordDecl *RD, |
| 5107 | Sema::CXXSpecialMember CSM, unsigned Quals, |
| 5108 | CXXMethodDecl **Selected) { |
| 5109 | if (Selected) |
| 5110 | *Selected = 0; |
| 5111 | |
| 5112 | switch (CSM) { |
| 5113 | case Sema::CXXInvalid: |
| 5114 | llvm_unreachable("not a special member"); |
| 5115 | |
| 5116 | case Sema::CXXDefaultConstructor: |
| 5117 | // C++11 [class.ctor]p5: |
| 5118 | // A default constructor is trivial if: |
| 5119 | // - all the [direct subobjects] have trivial default constructors |
| 5120 | // |
| 5121 | // Note, no overload resolution is performed in this case. |
| 5122 | if (RD->hasTrivialDefaultConstructor()) |
| 5123 | return true; |
| 5124 | |
| 5125 | if (Selected) { |
| 5126 | // If there's a default constructor which could have been trivial, dig it |
| 5127 | // out. Otherwise, if there's any user-provided default constructor, point |
| 5128 | // to that as an example of why there's not a trivial one. |
| 5129 | CXXConstructorDecl *DefCtor = 0; |
| 5130 | if (RD->needsImplicitDefaultConstructor()) |
| 5131 | S.DeclareImplicitDefaultConstructor(RD); |
| 5132 | for (CXXRecordDecl::ctor_iterator CI = RD->ctor_begin(), |
| 5133 | CE = RD->ctor_end(); CI != CE; ++CI) { |
| 5134 | if (!CI->isDefaultConstructor()) |
| 5135 | continue; |
| 5136 | DefCtor = *CI; |
| 5137 | if (!DefCtor->isUserProvided()) |
| 5138 | break; |
| 5139 | } |
| 5140 | |
| 5141 | *Selected = DefCtor; |
| 5142 | } |
| 5143 | |
| 5144 | return false; |
| 5145 | |
| 5146 | case Sema::CXXDestructor: |
| 5147 | // C++11 [class.dtor]p5: |
| 5148 | // A destructor is trivial if: |
| 5149 | // - all the direct [subobjects] have trivial destructors |
| 5150 | if (RD->hasTrivialDestructor()) |
| 5151 | return true; |
| 5152 | |
| 5153 | if (Selected) { |
| 5154 | if (RD->needsImplicitDestructor()) |
| 5155 | S.DeclareImplicitDestructor(RD); |
| 5156 | *Selected = RD->getDestructor(); |
| 5157 | } |
| 5158 | |
| 5159 | return false; |
| 5160 | |
| 5161 | case Sema::CXXCopyConstructor: |
| 5162 | // C++11 [class.copy]p12: |
| 5163 | // A copy constructor is trivial if: |
| 5164 | // - the constructor selected to copy each direct [subobject] is trivial |
| 5165 | if (RD->hasTrivialCopyConstructor()) { |
| 5166 | if (Quals == Qualifiers::Const) |
| 5167 | // We must either select the trivial copy constructor or reach an |
| 5168 | // ambiguity; no need to actually perform overload resolution. |
| 5169 | return true; |
| 5170 | } else if (!Selected) { |
| 5171 | return false; |
| 5172 | } |
| 5173 | // In C++98, we are not supposed to perform overload resolution here, but we |
| 5174 | // treat that as a language defect, as suggested on cxx-abi-dev, to treat |
| 5175 | // cases like B as having a non-trivial copy constructor: |
| 5176 | // struct A { template<typename T> A(T&); }; |
| 5177 | // struct B { mutable A a; }; |
| 5178 | goto NeedOverloadResolution; |
| 5179 | |
| 5180 | case Sema::CXXCopyAssignment: |
| 5181 | // C++11 [class.copy]p25: |
| 5182 | // A copy assignment operator is trivial if: |
| 5183 | // - the assignment operator selected to copy each direct [subobject] is |
| 5184 | // trivial |
| 5185 | if (RD->hasTrivialCopyAssignment()) { |
| 5186 | if (Quals == Qualifiers::Const) |
| 5187 | return true; |
| 5188 | } else if (!Selected) { |
| 5189 | return false; |
| 5190 | } |
| 5191 | // In C++98, we are not supposed to perform overload resolution here, but we |
| 5192 | // treat that as a language defect. |
| 5193 | goto NeedOverloadResolution; |
| 5194 | |
| 5195 | case Sema::CXXMoveConstructor: |
| 5196 | case Sema::CXXMoveAssignment: |
| 5197 | NeedOverloadResolution: |
| 5198 | Sema::SpecialMemberOverloadResult *SMOR = |
| 5199 | S.LookupSpecialMember(RD, CSM, |
| 5200 | Quals & Qualifiers::Const, |
| 5201 | Quals & Qualifiers::Volatile, |
| 5202 | /*RValueThis*/false, /*ConstThis*/false, |
| 5203 | /*VolatileThis*/false); |
| 5204 | |
| 5205 | // The standard doesn't describe how to behave if the lookup is ambiguous. |
| 5206 | // We treat it as not making the member non-trivial, just like the standard |
| 5207 | // mandates for the default constructor. This should rarely matter, because |
| 5208 | // the member will also be deleted. |
| 5209 | if (SMOR->getKind() == Sema::SpecialMemberOverloadResult::Ambiguous) |
| 5210 | return true; |
| 5211 | |
| 5212 | if (!SMOR->getMethod()) { |
| 5213 | assert(SMOR->getKind() == |
| 5214 | Sema::SpecialMemberOverloadResult::NoMemberOrDeleted); |
| 5215 | return false; |
| 5216 | } |
| 5217 | |
| 5218 | // We deliberately don't check if we found a deleted special member. We're |
| 5219 | // not supposed to! |
| 5220 | if (Selected) |
| 5221 | *Selected = SMOR->getMethod(); |
| 5222 | return SMOR->getMethod()->isTrivial(); |
| 5223 | } |
| 5224 | |
| 5225 | llvm_unreachable("unknown special method kind"); |
| 5226 | } |
| 5227 | |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 5228 | static CXXConstructorDecl *findUserDeclaredCtor(CXXRecordDecl *RD) { |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5229 | for (CXXRecordDecl::ctor_iterator CI = RD->ctor_begin(), CE = RD->ctor_end(); |
| 5230 | CI != CE; ++CI) |
| 5231 | if (!CI->isImplicit()) |
| 5232 | return *CI; |
| 5233 | |
| 5234 | // Look for constructor templates. |
| 5235 | typedef CXXRecordDecl::specific_decl_iterator<FunctionTemplateDecl> tmpl_iter; |
| 5236 | for (tmpl_iter TI(RD->decls_begin()), TE(RD->decls_end()); TI != TE; ++TI) { |
| 5237 | if (CXXConstructorDecl *CD = |
| 5238 | dyn_cast<CXXConstructorDecl>(TI->getTemplatedDecl())) |
| 5239 | return CD; |
| 5240 | } |
| 5241 | |
| 5242 | return 0; |
| 5243 | } |
| 5244 | |
| 5245 | /// The kind of subobject we are checking for triviality. The values of this |
| 5246 | /// enumeration are used in diagnostics. |
| 5247 | enum TrivialSubobjectKind { |
| 5248 | /// The subobject is a base class. |
| 5249 | TSK_BaseClass, |
| 5250 | /// The subobject is a non-static data member. |
| 5251 | TSK_Field, |
| 5252 | /// The object is actually the complete object. |
| 5253 | TSK_CompleteObject |
| 5254 | }; |
| 5255 | |
| 5256 | /// Check whether the special member selected for a given type would be trivial. |
| 5257 | static bool checkTrivialSubobjectCall(Sema &S, SourceLocation SubobjLoc, |
| 5258 | QualType SubType, |
| 5259 | Sema::CXXSpecialMember CSM, |
| 5260 | TrivialSubobjectKind Kind, |
| 5261 | bool Diagnose) { |
| 5262 | CXXRecordDecl *SubRD = SubType->getAsCXXRecordDecl(); |
| 5263 | if (!SubRD) |
| 5264 | return true; |
| 5265 | |
| 5266 | CXXMethodDecl *Selected; |
| 5267 | if (findTrivialSpecialMember(S, SubRD, CSM, SubType.getCVRQualifiers(), |
| 5268 | Diagnose ? &Selected : 0)) |
| 5269 | return true; |
| 5270 | |
| 5271 | if (Diagnose) { |
| 5272 | if (!Selected && CSM == Sema::CXXDefaultConstructor) { |
| 5273 | S.Diag(SubobjLoc, diag::note_nontrivial_no_def_ctor) |
| 5274 | << Kind << SubType.getUnqualifiedType(); |
| 5275 | if (CXXConstructorDecl *CD = findUserDeclaredCtor(SubRD)) |
| 5276 | S.Diag(CD->getLocation(), diag::note_user_declared_ctor); |
| 5277 | } else if (!Selected) |
| 5278 | S.Diag(SubobjLoc, diag::note_nontrivial_no_copy) |
| 5279 | << Kind << SubType.getUnqualifiedType() << CSM << SubType; |
| 5280 | else if (Selected->isUserProvided()) { |
| 5281 | if (Kind == TSK_CompleteObject) |
| 5282 | S.Diag(Selected->getLocation(), diag::note_nontrivial_user_provided) |
| 5283 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5284 | else { |
| 5285 | S.Diag(SubobjLoc, diag::note_nontrivial_user_provided) |
| 5286 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5287 | S.Diag(Selected->getLocation(), diag::note_declared_at); |
| 5288 | } |
| 5289 | } else { |
| 5290 | if (Kind != TSK_CompleteObject) |
| 5291 | S.Diag(SubobjLoc, diag::note_nontrivial_subobject) |
| 5292 | << Kind << SubType.getUnqualifiedType() << CSM; |
| 5293 | |
| 5294 | // Explain why the defaulted or deleted special member isn't trivial. |
| 5295 | S.SpecialMemberIsTrivial(Selected, CSM, Diagnose); |
| 5296 | } |
| 5297 | } |
| 5298 | |
| 5299 | return false; |
| 5300 | } |
| 5301 | |
| 5302 | /// Check whether the members of a class type allow a special member to be |
| 5303 | /// trivial. |
| 5304 | static bool checkTrivialClassMembers(Sema &S, CXXRecordDecl *RD, |
| 5305 | Sema::CXXSpecialMember CSM, |
| 5306 | bool ConstArg, bool Diagnose) { |
| 5307 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 5308 | FE = RD->field_end(); FI != FE; ++FI) { |
| 5309 | if (FI->isInvalidDecl() || FI->isUnnamedBitfield()) |
| 5310 | continue; |
| 5311 | |
| 5312 | QualType FieldType = S.Context.getBaseElementType(FI->getType()); |
| 5313 | |
| 5314 | // Pretend anonymous struct or union members are members of this class. |
| 5315 | if (FI->isAnonymousStructOrUnion()) { |
| 5316 | if (!checkTrivialClassMembers(S, FieldType->getAsCXXRecordDecl(), |
| 5317 | CSM, ConstArg, Diagnose)) |
| 5318 | return false; |
| 5319 | continue; |
| 5320 | } |
| 5321 | |
| 5322 | // C++11 [class.ctor]p5: |
| 5323 | // A default constructor is trivial if [...] |
| 5324 | // -- no non-static data member of its class has a |
| 5325 | // brace-or-equal-initializer |
| 5326 | if (CSM == Sema::CXXDefaultConstructor && FI->hasInClassInitializer()) { |
| 5327 | if (Diagnose) |
| 5328 | S.Diag(FI->getLocation(), diag::note_nontrivial_in_class_init) << *FI; |
| 5329 | return false; |
| 5330 | } |
| 5331 | |
| 5332 | // Objective C ARC 4.3.5: |
| 5333 | // [...] nontrivally ownership-qualified types are [...] not trivially |
| 5334 | // default constructible, copy constructible, move constructible, copy |
| 5335 | // assignable, move assignable, or destructible [...] |
| 5336 | if (S.getLangOpts().ObjCAutoRefCount && |
| 5337 | FieldType.hasNonTrivialObjCLifetime()) { |
| 5338 | if (Diagnose) |
| 5339 | S.Diag(FI->getLocation(), diag::note_nontrivial_objc_ownership) |
| 5340 | << RD << FieldType.getObjCLifetime(); |
| 5341 | return false; |
| 5342 | } |
| 5343 | |
| 5344 | if (ConstArg && !FI->isMutable()) |
| 5345 | FieldType.addConst(); |
| 5346 | if (!checkTrivialSubobjectCall(S, FI->getLocation(), FieldType, CSM, |
| 5347 | TSK_Field, Diagnose)) |
| 5348 | return false; |
| 5349 | } |
| 5350 | |
| 5351 | return true; |
| 5352 | } |
| 5353 | |
| 5354 | /// Diagnose why the specified class does not have a trivial special member of |
| 5355 | /// the given kind. |
| 5356 | void Sema::DiagnoseNontrivial(const CXXRecordDecl *RD, CXXSpecialMember CSM) { |
| 5357 | QualType Ty = Context.getRecordType(RD); |
| 5358 | if (CSM == CXXCopyConstructor || CSM == CXXCopyAssignment) |
| 5359 | Ty.addConst(); |
| 5360 | |
| 5361 | checkTrivialSubobjectCall(*this, RD->getLocation(), Ty, CSM, |
| 5362 | TSK_CompleteObject, /*Diagnose*/true); |
| 5363 | } |
| 5364 | |
| 5365 | /// Determine whether a defaulted or deleted special member function is trivial, |
| 5366 | /// as specified in C++11 [class.ctor]p5, C++11 [class.copy]p12, |
| 5367 | /// C++11 [class.copy]p25, and C++11 [class.dtor]p5. |
| 5368 | bool Sema::SpecialMemberIsTrivial(CXXMethodDecl *MD, CXXSpecialMember CSM, |
| 5369 | bool Diagnose) { |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5370 | assert(!MD->isUserProvided() && CSM != CXXInvalid && "not special enough"); |
| 5371 | |
| 5372 | CXXRecordDecl *RD = MD->getParent(); |
| 5373 | |
| 5374 | bool ConstArg = false; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5375 | |
| 5376 | // C++11 [class.copy]p12, p25: |
| 5377 | // A [special member] is trivial if its declared parameter type is the same |
| 5378 | // as if it had been implicitly declared [...] |
| 5379 | switch (CSM) { |
| 5380 | case CXXDefaultConstructor: |
| 5381 | case CXXDestructor: |
| 5382 | // Trivial default constructors and destructors cannot have parameters. |
| 5383 | break; |
| 5384 | |
| 5385 | case CXXCopyConstructor: |
| 5386 | case CXXCopyAssignment: { |
| 5387 | // Trivial copy operations always have const, non-volatile parameter types. |
| 5388 | ConstArg = true; |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 5389 | const ParmVarDecl *Param0 = MD->getParamDecl(0); |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5390 | const ReferenceType *RT = Param0->getType()->getAs<ReferenceType>(); |
| 5391 | if (!RT || RT->getPointeeType().getCVRQualifiers() != Qualifiers::Const) { |
| 5392 | if (Diagnose) |
| 5393 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 5394 | << Param0->getSourceRange() << Param0->getType() |
| 5395 | << Context.getLValueReferenceType( |
| 5396 | Context.getRecordType(RD).withConst()); |
| 5397 | return false; |
| 5398 | } |
| 5399 | break; |
| 5400 | } |
| 5401 | |
| 5402 | case CXXMoveConstructor: |
| 5403 | case CXXMoveAssignment: { |
| 5404 | // Trivial move operations always have non-cv-qualified parameters. |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 5405 | const ParmVarDecl *Param0 = MD->getParamDecl(0); |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 5406 | const RValueReferenceType *RT = |
| 5407 | Param0->getType()->getAs<RValueReferenceType>(); |
| 5408 | if (!RT || RT->getPointeeType().getCVRQualifiers()) { |
| 5409 | if (Diagnose) |
| 5410 | Diag(Param0->getLocation(), diag::note_nontrivial_param_type) |
| 5411 | << Param0->getSourceRange() << Param0->getType() |
| 5412 | << Context.getRValueReferenceType(Context.getRecordType(RD)); |
| 5413 | return false; |
| 5414 | } |
| 5415 | break; |
| 5416 | } |
| 5417 | |
| 5418 | case CXXInvalid: |
| 5419 | llvm_unreachable("not a special member"); |
| 5420 | } |
| 5421 | |
| 5422 | // FIXME: We require that the parameter-declaration-clause is equivalent to |
| 5423 | // that of an implicit declaration, not just that the declared parameter type |
| 5424 | // matches, in order to prevent absuridities like a function simultaneously |
| 5425 | // being a trivial copy constructor and a non-trivial default constructor. |
| 5426 | // This issue has not yet been assigned a core issue number. |
| 5427 | if (MD->getMinRequiredArguments() < MD->getNumParams()) { |
| 5428 | if (Diagnose) |
| 5429 | Diag(MD->getParamDecl(MD->getMinRequiredArguments())->getLocation(), |
| 5430 | diag::note_nontrivial_default_arg) |
| 5431 | << MD->getParamDecl(MD->getMinRequiredArguments())->getSourceRange(); |
| 5432 | return false; |
| 5433 | } |
| 5434 | if (MD->isVariadic()) { |
| 5435 | if (Diagnose) |
| 5436 | Diag(MD->getLocation(), diag::note_nontrivial_variadic); |
| 5437 | return false; |
| 5438 | } |
| 5439 | |
| 5440 | // C++11 [class.ctor]p5, C++11 [class.dtor]p5: |
| 5441 | // A copy/move [constructor or assignment operator] is trivial if |
| 5442 | // -- the [member] selected to copy/move each direct base class subobject |
| 5443 | // is trivial |
| 5444 | // |
| 5445 | // C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5446 | // A [default constructor or destructor] is trivial if |
| 5447 | // -- all the direct base classes have trivial [default constructors or |
| 5448 | // destructors] |
| 5449 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 5450 | BE = RD->bases_end(); BI != BE; ++BI) |
| 5451 | if (!checkTrivialSubobjectCall(*this, BI->getLocStart(), |
| 5452 | ConstArg ? BI->getType().withConst() |
| 5453 | : BI->getType(), |
| 5454 | CSM, TSK_BaseClass, Diagnose)) |
| 5455 | return false; |
| 5456 | |
| 5457 | // C++11 [class.ctor]p5, C++11 [class.dtor]p5: |
| 5458 | // A copy/move [constructor or assignment operator] for a class X is |
| 5459 | // trivial if |
| 5460 | // -- for each non-static data member of X that is of class type (or array |
| 5461 | // thereof), the constructor selected to copy/move that member is |
| 5462 | // trivial |
| 5463 | // |
| 5464 | // C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5465 | // A [default constructor or destructor] is trivial if |
| 5466 | // -- for all of the non-static data members of its class that are of class |
| 5467 | // type (or array thereof), each such class has a trivial [default |
| 5468 | // constructor or destructor] |
| 5469 | if (!checkTrivialClassMembers(*this, RD, CSM, ConstArg, Diagnose)) |
| 5470 | return false; |
| 5471 | |
| 5472 | // C++11 [class.dtor]p5: |
| 5473 | // A destructor is trivial if [...] |
| 5474 | // -- the destructor is not virtual |
| 5475 | if (CSM == CXXDestructor && MD->isVirtual()) { |
| 5476 | if (Diagnose) |
| 5477 | Diag(MD->getLocation(), diag::note_nontrivial_virtual_dtor) << RD; |
| 5478 | return false; |
| 5479 | } |
| 5480 | |
| 5481 | // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 5482 | // A [special member] for class X is trivial if [...] |
| 5483 | // -- class X has no virtual functions and no virtual base classes |
| 5484 | if (CSM != CXXDestructor && MD->getParent()->isDynamicClass()) { |
| 5485 | if (!Diagnose) |
| 5486 | return false; |
| 5487 | |
| 5488 | if (RD->getNumVBases()) { |
| 5489 | // Check for virtual bases. We already know that the corresponding |
| 5490 | // member in all bases is trivial, so vbases must all be direct. |
| 5491 | CXXBaseSpecifier &BS = *RD->vbases_begin(); |
| 5492 | assert(BS.isVirtual()); |
| 5493 | Diag(BS.getLocStart(), diag::note_nontrivial_has_virtual) << RD << 1; |
| 5494 | return false; |
| 5495 | } |
| 5496 | |
| 5497 | // Must have a virtual method. |
| 5498 | for (CXXRecordDecl::method_iterator MI = RD->method_begin(), |
| 5499 | ME = RD->method_end(); MI != ME; ++MI) { |
| 5500 | if (MI->isVirtual()) { |
| 5501 | SourceLocation MLoc = MI->getLocStart(); |
| 5502 | Diag(MLoc, diag::note_nontrivial_has_virtual) << RD << 0; |
| 5503 | return false; |
| 5504 | } |
| 5505 | } |
| 5506 | |
| 5507 | llvm_unreachable("dynamic class with no vbases and no virtual functions"); |
| 5508 | } |
| 5509 | |
| 5510 | // Looks like it's trivial! |
| 5511 | return true; |
| 5512 | } |
| 5513 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5514 | /// \brief Data used with FindHiddenVirtualMethod |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 5515 | namespace { |
| 5516 | struct FindHiddenVirtualMethodData { |
| 5517 | Sema *S; |
| 5518 | CXXMethodDecl *Method; |
| 5519 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5520 | SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 5521 | }; |
| 5522 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5523 | |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5524 | /// \brief Check whether any most overriden method from MD in Methods |
| 5525 | static bool CheckMostOverridenMethods(const CXXMethodDecl *MD, |
| 5526 | const llvm::SmallPtrSet<const CXXMethodDecl *, 8>& Methods) { |
| 5527 | if (MD->size_overridden_methods() == 0) |
| 5528 | return Methods.count(MD->getCanonicalDecl()); |
| 5529 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 5530 | E = MD->end_overridden_methods(); |
| 5531 | I != E; ++I) |
| 5532 | if (CheckMostOverridenMethods(*I, Methods)) |
| 5533 | return true; |
| 5534 | return false; |
| 5535 | } |
| 5536 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5537 | /// \brief Member lookup function that determines whether a given C++ |
| 5538 | /// method overloads virtual methods in a base class without overriding any, |
| 5539 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 5540 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 5541 | CXXBasePath &Path, |
| 5542 | void *UserData) { |
| 5543 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 5544 | |
| 5545 | FindHiddenVirtualMethodData &Data |
| 5546 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 5547 | |
| 5548 | DeclarationName Name = Data.Method->getDeclName(); |
| 5549 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 5550 | |
| 5551 | bool foundSameNameMethod = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5552 | SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5553 | for (Path.Decls = BaseRecord->lookup(Name); |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5554 | !Path.Decls.empty(); |
| 5555 | Path.Decls = Path.Decls.slice(1)) { |
| 5556 | NamedDecl *D = Path.Decls.front(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5557 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 5558 | MD = MD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5559 | foundSameNameMethod = true; |
| 5560 | // Interested only in hidden virtual methods. |
| 5561 | if (!MD->isVirtual()) |
| 5562 | continue; |
| 5563 | // If the method we are checking overrides a method from its base |
| 5564 | // don't warn about the other overloaded methods. |
| 5565 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 5566 | return true; |
| 5567 | // Collect the overload only if its hidden. |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5568 | if (!CheckMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods)) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5569 | overloadedMethods.push_back(MD); |
| 5570 | } |
| 5571 | } |
| 5572 | |
| 5573 | if (foundSameNameMethod) |
| 5574 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 5575 | overloadedMethods.end()); |
| 5576 | return foundSameNameMethod; |
| 5577 | } |
| 5578 | |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5579 | /// \brief Add the most overriden methods from MD to Methods |
| 5580 | static void AddMostOverridenMethods(const CXXMethodDecl *MD, |
| 5581 | llvm::SmallPtrSet<const CXXMethodDecl *, 8>& Methods) { |
| 5582 | if (MD->size_overridden_methods() == 0) |
| 5583 | Methods.insert(MD->getCanonicalDecl()); |
| 5584 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 5585 | E = MD->end_overridden_methods(); |
| 5586 | I != E; ++I) |
| 5587 | AddMostOverridenMethods(*I, Methods); |
| 5588 | } |
| 5589 | |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5590 | /// \brief See if a method overloads virtual methods in a base class without |
| 5591 | /// overriding any. |
| 5592 | void Sema::DiagnoseHiddenVirtualMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
| 5593 | if (Diags.getDiagnosticLevel(diag::warn_overloaded_virtual, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 5594 | MD->getLocation()) == DiagnosticsEngine::Ignored) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5595 | return; |
Benjamin Kramer | c470442 | 2012-05-19 16:03:58 +0000 | [diff] [blame] | 5596 | if (!MD->getDeclName().isIdentifier()) |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5597 | return; |
| 5598 | |
| 5599 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 5600 | /*bool RecordPaths=*/false, |
| 5601 | /*bool DetectVirtual=*/false); |
| 5602 | FindHiddenVirtualMethodData Data; |
| 5603 | Data.Method = MD; |
| 5604 | Data.S = this; |
| 5605 | |
| 5606 | // Keep the base methods that were overriden or introduced in the subclass |
| 5607 | // 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] | 5608 | DeclContext::lookup_result R = DC->lookup(MD->getDeclName()); |
| 5609 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 5610 | NamedDecl *ND = *I; |
| 5611 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*I)) |
David Blaikie | 5f75068 | 2012-10-19 00:53:08 +0000 | [diff] [blame] | 5612 | ND = shad->getTargetDecl(); |
| 5613 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND)) |
| 5614 | AddMostOverridenMethods(MD, Data.OverridenAndUsingBaseMethods); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
| 5617 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths) && |
| 5618 | !Data.OverloadedMethods.empty()) { |
| 5619 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 5620 | << MD << (Data.OverloadedMethods.size() > 1); |
| 5621 | |
| 5622 | for (unsigned i = 0, e = Data.OverloadedMethods.size(); i != e; ++i) { |
| 5623 | CXXMethodDecl *overloadedMD = Data.OverloadedMethods[i]; |
Richard Trieu | f608aff | 2013-04-05 23:02:24 +0000 | [diff] [blame] | 5624 | PartialDiagnostic PD = PDiag( |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5625 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
Richard Trieu | f608aff | 2013-04-05 23:02:24 +0000 | [diff] [blame] | 5626 | HandleFunctionTypeMismatch(PD, MD->getType(), overloadedMD->getType()); |
| 5627 | Diag(overloadedMD->getLocation(), PD); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 5628 | } |
| 5629 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 5630 | } |
| 5631 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5632 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5633 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5634 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 5635 | SourceLocation RBrac, |
| 5636 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5637 | if (!TagDecl) |
| 5638 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5639 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5640 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 5641 | |
Rafael Espindola | f729ce0 | 2012-07-12 04:32:30 +0000 | [diff] [blame] | 5642 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
| 5643 | if (l->getKind() != AttributeList::AT_Visibility) |
| 5644 | continue; |
| 5645 | l->setInvalid(); |
| 5646 | Diag(l->getLoc(), diag::warn_attribute_after_definition_ignored) << |
| 5647 | l->getName(); |
| 5648 | } |
| 5649 | |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 5650 | ActOnFields(S, RLoc, TagDecl, llvm::makeArrayRef( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5651 | // strict aliasing violation! |
| 5652 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 5653 | FieldCollector->getCurNumFields()), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 5654 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5655 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5656 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5659 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 5660 | /// special functions, such as the default constructor, copy |
| 5661 | /// constructor, or destructor, to the given C++ class (C++ |
| 5662 | /// [special]p1). This routine can only be executed just before the |
| 5663 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5664 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 5665 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5666 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5667 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5668 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5669 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5670 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5671 | // If the properties or semantics of the copy constructor couldn't be |
| 5672 | // determined while the class was being declared, force a declaration |
| 5673 | // of it now. |
| 5674 | if (ClassDecl->needsOverloadResolutionForCopyConstructor()) |
| 5675 | DeclareImplicitCopyConstructor(ClassDecl); |
| 5676 | } |
| 5677 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5678 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveConstructor()) { |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 5679 | ++ASTContext::NumImplicitMoveConstructors; |
| 5680 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5681 | if (ClassDecl->needsOverloadResolutionForMoveConstructor()) |
| 5682 | DeclareImplicitMoveConstructor(ClassDecl); |
| 5683 | } |
| 5684 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5685 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 5686 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5687 | |
| 5688 | // 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] | 5689 | // 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] | 5690 | // it shows up in the right place in the vtable and that we diagnose |
| 5691 | // problems with the implicit exception specification. |
| 5692 | if (ClassDecl->isDynamicClass() || |
| 5693 | ClassDecl->needsOverloadResolutionForCopyAssignment()) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5694 | DeclareImplicitCopyAssignment(ClassDecl); |
| 5695 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5696 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5697 | if (getLangOpts().CPlusPlus11 && ClassDecl->needsImplicitMoveAssignment()) { |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 5698 | ++ASTContext::NumImplicitMoveAssignmentOperators; |
| 5699 | |
| 5700 | // Likewise for the move assignment operator. |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5701 | if (ClassDecl->isDynamicClass() || |
| 5702 | ClassDecl->needsOverloadResolutionForMoveAssignment()) |
Richard Smith | b701d3d | 2011-12-24 21:56:24 +0000 | [diff] [blame] | 5703 | DeclareImplicitMoveAssignment(ClassDecl); |
| 5704 | } |
| 5705 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5706 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 5707 | ++ASTContext::NumImplicitDestructors; |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5708 | |
| 5709 | // 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] | 5710 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 5711 | // shows up in the right place in the vtable and that we diagnose problems |
| 5712 | // with the implicit exception specification. |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 5713 | if (ClassDecl->isDynamicClass() || |
| 5714 | ClassDecl->needsOverloadResolutionForDestructor()) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5715 | DeclareImplicitDestructor(ClassDecl); |
| 5716 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 5717 | } |
| 5718 | |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 5719 | void Sema::ActOnReenterDeclaratorTemplateScope(Scope *S, DeclaratorDecl *D) { |
| 5720 | if (!D) |
| 5721 | return; |
| 5722 | |
| 5723 | int NumParamList = D->getNumTemplateParameterLists(); |
| 5724 | for (int i = 0; i < NumParamList; i++) { |
| 5725 | TemplateParameterList* Params = D->getTemplateParameterList(i); |
| 5726 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5727 | ParamEnd = Params->end(); |
| 5728 | Param != ParamEnd; ++Param) { |
| 5729 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 5730 | if (Named->getDeclName()) { |
| 5731 | S->AddDecl(Named); |
| 5732 | IdResolver.AddDecl(Named); |
| 5733 | } |
| 5734 | } |
| 5735 | } |
| 5736 | } |
| 5737 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5738 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 5739 | if (!D) |
| 5740 | return; |
| 5741 | |
| 5742 | TemplateParameterList *Params = 0; |
| 5743 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 5744 | Params = Template->getTemplateParameters(); |
| 5745 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 5746 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 5747 | Params = PartialSpec->getTemplateParameters(); |
| 5748 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 5749 | return; |
| 5750 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 5751 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5752 | ParamEnd = Params->end(); |
| 5753 | Param != ParamEnd; ++Param) { |
| 5754 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 5755 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5756 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 5757 | IdResolver.AddDecl(Named); |
| 5758 | } |
| 5759 | } |
| 5760 | } |
| 5761 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5762 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5763 | if (!RecordD) return; |
| 5764 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5765 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5766 | PushDeclContext(S, Record); |
| 5767 | } |
| 5768 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5769 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5770 | if (!RecordD) return; |
| 5771 | PopDeclContext(); |
| 5772 | } |
| 5773 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5774 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 5775 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 5776 | /// parsing those parts of the given Method declaration that could |
| 5777 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 5778 | /// arguments. This action should enter the scope of the given |
| 5779 | /// Method declaration as if we had just parsed the qualified method |
| 5780 | /// name. However, it should not bring the parameters into scope; |
| 5781 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5782 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
| 5785 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 5786 | /// C++ method declaration. We're (re-)introducing the given |
| 5787 | /// function parameter into scope for use in parsing later parts of |
| 5788 | /// the method declaration. For example, we could see an |
| 5789 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5790 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5791 | if (!ParamD) |
| 5792 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5793 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5794 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5795 | |
| 5796 | // If this parameter has an unparsed default argument, clear it out |
| 5797 | // to make way for the parsed default argument. |
| 5798 | if (Param->hasUnparsedDefaultArg()) |
| 5799 | Param->setDefaultArg(0); |
| 5800 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5801 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5802 | if (Param->getDeclName()) |
| 5803 | IdResolver.AddDecl(Param); |
| 5804 | } |
| 5805 | |
| 5806 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 5807 | /// processing the delayed method declaration for Method. The method |
| 5808 | /// declaration is now considered finished. There may be a separate |
| 5809 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 5810 | /// immediately!) for this method, if it was also defined inside the |
| 5811 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5812 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 5813 | if (!MethodD) |
| 5814 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5815 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 5816 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5817 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5818 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5819 | |
| 5820 | // Now that we have our default arguments, check the constructor |
| 5821 | // again. It could produce additional diagnostics or affect whether |
| 5822 | // the class has implicitly-declared destructors, among other |
| 5823 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5824 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 5825 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5826 | |
| 5827 | // Check the default arguments, which we may have added. |
| 5828 | if (!Method->isInvalidDecl()) |
| 5829 | CheckCXXDefaultArguments(Method); |
| 5830 | } |
| 5831 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5832 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5833 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5834 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5835 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 5836 | /// will be updated to reflect a well-formed type for the constructor and |
| 5837 | /// returned. |
| 5838 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5839 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5840 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5841 | |
| 5842 | // C++ [class.ctor]p3: |
| 5843 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 5844 | // constructor can be invoked for a const, volatile or const |
| 5845 | // volatile object. A constructor shall not be declared const, |
| 5846 | // volatile, or const volatile (9.3.2). |
| 5847 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5848 | if (!D.isInvalidType()) |
| 5849 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 5850 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 5851 | << SourceRange(D.getIdentifierLoc()); |
| 5852 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5853 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5854 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5855 | if (!D.isInvalidType()) |
| 5856 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 5857 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 5858 | << SourceRange(D.getIdentifierLoc()); |
| 5859 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5860 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5861 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5862 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5863 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5864 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5865 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5866 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 5867 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5868 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5869 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 5870 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5871 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5872 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 5873 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5874 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5875 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5876 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5877 | // C++0x [class.ctor]p4: |
| 5878 | // A constructor shall not be declared with a ref-qualifier. |
| 5879 | if (FTI.hasRefQualifier()) { |
| 5880 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 5881 | << FTI.RefQualifierIsLValueRef |
| 5882 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 5883 | D.setInvalidType(); |
| 5884 | } |
| 5885 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5886 | // Rebuild the function type "R" without any type qualifiers (in |
| 5887 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5888 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5889 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5890 | if (Proto->getResultType() == Context.VoidTy && !D.isInvalidType()) |
| 5891 | return R; |
| 5892 | |
| 5893 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 5894 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 5895 | EPI.RefQualifier = RQ_None; |
| 5896 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 5897 | return Context.getFunctionType(Context.VoidTy, Proto->getArgTypes(), EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5900 | /// CheckConstructor - Checks a fully-formed constructor for |
| 5901 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 5902 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5903 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5904 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 5905 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 5906 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5907 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5908 | |
| 5909 | // C++ [class.copy]p3: |
| 5910 | // A declaration of a constructor for a class X is ill-formed if |
| 5911 | // its first parameter is of type (optionally cv-qualified) X and |
| 5912 | // either there are no other parameters or else all other |
| 5913 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 5914 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5915 | ((Constructor->getNumParams() == 1) || |
| 5916 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5917 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 5918 | Constructor->getTemplateSpecializationKind() |
| 5919 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5920 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 5921 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 5922 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5923 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 5924 | const char *ConstRef |
| 5925 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 5926 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5927 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 5928 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 5929 | |
| 5930 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 5931 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 5932 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5933 | } |
| 5934 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 5937 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 5938 | /// well-formedness, issuing any diagnostics required. Returns true |
| 5939 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5940 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5941 | CXXRecordDecl *RD = Destructor->getParent(); |
| 5942 | |
Peter Collingbourne | f51cfb8 | 2013-05-20 14:12:25 +0000 | [diff] [blame] | 5943 | if (!Destructor->getOperatorDelete() && Destructor->isVirtual()) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5944 | SourceLocation Loc; |
| 5945 | |
| 5946 | if (!Destructor->isImplicit()) |
| 5947 | Loc = Destructor->getLocation(); |
| 5948 | else |
| 5949 | Loc = RD->getLocation(); |
| 5950 | |
| 5951 | // If we have a virtual destructor, look up the deallocation function |
| 5952 | FunctionDecl *OperatorDelete = 0; |
| 5953 | DeclarationName Name = |
| 5954 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5955 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5956 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 5957 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5958 | MarkFunctionReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5959 | |
| 5960 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5961 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5962 | |
| 5963 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5964 | } |
| 5965 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5966 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5967 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 5968 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 5969 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5970 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 5971 | } |
| 5972 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5973 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 5974 | /// the well-formednes of the destructor declarator @p D with type @p |
| 5975 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5976 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 5977 | /// will be updated to reflect a well-formed type for the destructor and |
| 5978 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 5979 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5980 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5981 | // C++ [class.dtor]p1: |
| 5982 | // [...] A typedef-name that names a class is a class-name |
| 5983 | // (7.1.3); however, a typedef-name that names a class shall not |
| 5984 | // be used as the identifier in the declarator for a destructor |
| 5985 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5986 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5987 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 5988 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 5989 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 5990 | else if (const TemplateSpecializationType *TST = |
| 5991 | DeclaratorType->getAs<TemplateSpecializationType>()) |
| 5992 | if (TST->isTypeAlias()) |
| 5993 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
| 5994 | << DeclaratorType << 1; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 5995 | |
| 5996 | // C++ [class.dtor]p2: |
| 5997 | // A destructor is used to destroy objects of its class type. A |
| 5998 | // destructor takes no parameters, and no return type can be |
| 5999 | // specified for it (not even void). The address of a destructor |
| 6000 | // shall not be taken. A destructor shall not be static. A |
| 6001 | // destructor can be invoked for a const, volatile or const |
| 6002 | // volatile object. A destructor shall not be declared const, |
| 6003 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6004 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6005 | if (!D.isInvalidType()) |
| 6006 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 6007 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6008 | << SourceRange(D.getIdentifierLoc()) |
| 6009 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6010 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6011 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6012 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6013 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6014 | // Destructors don't have return types, but the parser will |
| 6015 | // happily parse something like: |
| 6016 | // |
| 6017 | // class X { |
| 6018 | // float ~X(); |
| 6019 | // }; |
| 6020 | // |
| 6021 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6022 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 6023 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6024 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6025 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6026 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6027 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6028 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6029 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6030 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6031 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6032 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6033 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6034 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6035 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6036 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 6037 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6038 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6039 | } |
| 6040 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6041 | // C++0x [class.dtor]p2: |
| 6042 | // A destructor shall not be declared with a ref-qualifier. |
| 6043 | if (FTI.hasRefQualifier()) { |
| 6044 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 6045 | << FTI.RefQualifierIsLValueRef |
| 6046 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 6047 | D.setInvalidType(); |
| 6048 | } |
| 6049 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6050 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 6051 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6052 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 6053 | |
| 6054 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6055 | FTI.freeArgs(); |
| 6056 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6057 | } |
| 6058 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6059 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6060 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6061 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 6062 | D.setInvalidType(); |
| 6063 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6064 | |
| 6065 | // Rebuild the function type "R" without any type qualifiers or |
| 6066 | // parameters (in case any of the errors above fired) and with |
| 6067 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6068 | // types. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6069 | if (!D.isInvalidType()) |
| 6070 | return R; |
| 6071 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 6072 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6073 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 6074 | EPI.Variadic = false; |
| 6075 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 6076 | EPI.RefQualifier = RQ_None; |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 6077 | return Context.getFunctionType(Context.VoidTy, None, EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 6078 | } |
| 6079 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6080 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 6081 | /// well-formednes of the conversion function declarator @p D with |
| 6082 | /// type @p R. If there are any errors in the declarator, this routine |
| 6083 | /// will emit diagnostics and return true. Otherwise, it will return |
| 6084 | /// false. Either way, the type @p R will be updated to reflect a |
| 6085 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6086 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6087 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6088 | // C++ [class.conv.fct]p1: |
| 6089 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6090 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6091 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6092 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6093 | if (!D.isInvalidType()) |
| 6094 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
Eli Friedman | 4cde94a | 2013-06-20 20:58:02 +0000 | [diff] [blame] | 6095 | << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 6096 | << D.getName().getSourceRange(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6097 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6098 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6099 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6100 | |
| 6101 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 6102 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6103 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6104 | // Conversion functions don't have return types, but the parser will |
| 6105 | // happily parse something like: |
| 6106 | // |
| 6107 | // class X { |
| 6108 | // float operator bool(); |
| 6109 | // }; |
| 6110 | // |
| 6111 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6112 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 6113 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 6114 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6115 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6116 | } |
| 6117 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6118 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 6119 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6120 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6121 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6122 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 6123 | |
| 6124 | // Delete the parameters. |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 6125 | D.getFunctionTypeInfo().freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6126 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6127 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6128 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6129 | D.setInvalidType(); |
| 6130 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6131 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 6132 | // Diagnose "&operator bool()" and other such nonsense. This |
| 6133 | // is actually a gcc extension which we don't support. |
| 6134 | if (Proto->getResultType() != ConvType) { |
| 6135 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 6136 | << Proto->getResultType(); |
| 6137 | D.setInvalidType(); |
| 6138 | ConvType = Proto->getResultType(); |
| 6139 | } |
| 6140 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6141 | // C++ [class.conv.fct]p4: |
| 6142 | // The conversion-type-id shall not represent a function type nor |
| 6143 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6144 | if (ConvType->isArrayType()) { |
| 6145 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 6146 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6147 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6148 | } else if (ConvType->isFunctionType()) { |
| 6149 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 6150 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 6151 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | // Rebuild the function type "R" without any parameters (in case any |
| 6155 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6156 | // return type. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6157 | if (D.isInvalidType()) |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 6158 | R = Context.getFunctionType(ConvType, None, Proto->getExtProtoInfo()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6159 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6160 | // C++0x explicit conversion operators. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6161 | if (D.getDeclSpec().isExplicitSpecified()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6162 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6163 | getLangOpts().CPlusPlus11 ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6164 | diag::warn_cxx98_compat_explicit_conversion_functions : |
| 6165 | diag::ext_explicit_conversion_functions) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 6166 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6167 | } |
| 6168 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6169 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 6170 | /// the declaration of the given C++ conversion function. This routine |
| 6171 | /// is responsible for recording the conversion function in the C++ |
| 6172 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6173 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6174 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 6175 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 6176 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6177 | |
| 6178 | // Make sure we aren't redeclaring the conversion function. |
| 6179 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6180 | |
| 6181 | // C++ [class.conv.fct]p1: |
| 6182 | // [...] A conversion function is never used to convert a |
| 6183 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 6184 | // same object type (or a reference to it), to a (possibly |
| 6185 | // cv-qualified) base class of that type (or a reference to it), |
| 6186 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6187 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 6188 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6189 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6190 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6191 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6192 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 6193 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 6194 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 6195 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 6196 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6197 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 6198 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 6199 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6200 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6201 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 6202 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6203 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6204 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 6205 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6206 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6207 | } |
| 6208 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 6209 | if (FunctionTemplateDecl *ConversionTemplate |
| 6210 | = Conversion->getDescribedFunctionTemplate()) |
| 6211 | return ConversionTemplate; |
| 6212 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6213 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 6214 | } |
| 6215 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6216 | //===----------------------------------------------------------------------===// |
| 6217 | // Namespace Handling |
| 6218 | //===----------------------------------------------------------------------===// |
| 6219 | |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6220 | /// \brief Diagnose a mismatch in 'inline' qualifiers when a namespace is |
| 6221 | /// reopened. |
| 6222 | static void DiagnoseNamespaceInlineMismatch(Sema &S, SourceLocation KeywordLoc, |
| 6223 | SourceLocation Loc, |
| 6224 | IdentifierInfo *II, bool *IsInline, |
| 6225 | NamespaceDecl *PrevNS) { |
| 6226 | assert(*IsInline != PrevNS->isInline()); |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 6227 | |
Richard Smith | c969e6a | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 6228 | // HACK: Work around a bug in libstdc++4.6's <atomic>, where |
| 6229 | // std::__atomic[0,1,2] are defined as non-inline namespaces, then reopened as |
| 6230 | // inline namespaces, with the intention of bringing names into namespace std. |
| 6231 | // |
| 6232 | // We support this just well enough to get that case working; this is not |
| 6233 | // sufficient to support reopening namespaces as inline in general. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6234 | if (*IsInline && II && II->getName().startswith("__atomic") && |
| 6235 | S.getSourceManager().isInSystemHeader(Loc)) { |
Richard Smith | c969e6a | 2012-10-05 01:46:25 +0000 | [diff] [blame] | 6236 | // Mark all prior declarations of the namespace as inline. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6237 | for (NamespaceDecl *NS = PrevNS->getMostRecentDecl(); NS; |
| 6238 | NS = NS->getPreviousDecl()) |
| 6239 | NS->setInline(*IsInline); |
| 6240 | // Patch up the lookup table for the containing namespace. This isn't really |
| 6241 | // correct, but it's good enough for this particular case. |
| 6242 | for (DeclContext::decl_iterator I = PrevNS->decls_begin(), |
| 6243 | E = PrevNS->decls_end(); I != E; ++I) |
| 6244 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*I)) |
| 6245 | PrevNS->getParent()->makeDeclVisibleInContext(ND); |
| 6246 | return; |
| 6247 | } |
| 6248 | |
| 6249 | if (PrevNS->isInline()) |
| 6250 | // The user probably just forgot the 'inline', so suggest that it |
| 6251 | // be added back. |
| 6252 | S.Diag(Loc, diag::warn_inline_namespace_reopened_noninline) |
| 6253 | << FixItHint::CreateInsertion(KeywordLoc, "inline "); |
| 6254 | else |
| 6255 | S.Diag(Loc, diag::err_inline_namespace_mismatch) |
| 6256 | << IsInline; |
| 6257 | |
| 6258 | S.Diag(PrevNS->getLocation(), diag::note_previous_definition); |
| 6259 | *IsInline = PrevNS->isInline(); |
| 6260 | } |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 6261 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6262 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 6263 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6264 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 6265 | SourceLocation InlineLoc, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6266 | SourceLocation NamespaceLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 6267 | SourceLocation IdentLoc, |
| 6268 | IdentifierInfo *II, |
| 6269 | SourceLocation LBrace, |
| 6270 | AttributeList *AttrList) { |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6271 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 6272 | // For anonymous namespace, take the location of the left brace. |
| 6273 | SourceLocation Loc = II ? IdentLoc : LBrace; |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6274 | bool IsInline = InlineLoc.isValid(); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6275 | bool IsInvalid = false; |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6276 | bool IsStd = false; |
| 6277 | bool AddToKnown = false; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6278 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 6279 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6280 | NamespaceDecl *PrevNS = 0; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6281 | if (II) { |
| 6282 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 6283 | // The identifier in an original-namespace-definition shall not |
| 6284 | // have been previously defined in the declarative region in |
| 6285 | // which the original-namespace-definition appears. The |
| 6286 | // identifier in an original-namespace-definition is the name of |
| 6287 | // the namespace. Subsequently in that declarative region, it is |
| 6288 | // treated as an original-namespace-name. |
| 6289 | // |
| 6290 | // Since namespace names are unique in their scope, and we don't |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6291 | // look through using directives, just look for any ordinary names. |
| 6292 | |
| 6293 | const unsigned IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Member | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6294 | Decl::IDNS_Type | Decl::IDNS_Using | Decl::IDNS_Tag | |
| 6295 | Decl::IDNS_Namespace; |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6296 | NamedDecl *PrevDecl = 0; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 6297 | DeclContext::lookup_result R = CurContext->getRedeclContext()->lookup(II); |
| 6298 | for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E; |
| 6299 | ++I) { |
| 6300 | if ((*I)->getIdentifierNamespace() & IDNS) { |
| 6301 | PrevDecl = *I; |
Douglas Gregor | 010157f | 2011-05-06 23:28:47 +0000 | [diff] [blame] | 6302 | break; |
| 6303 | } |
| 6304 | } |
| 6305 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6306 | PrevNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl); |
| 6307 | |
| 6308 | if (PrevNS) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6309 | // This is an extended namespace definition. |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6310 | if (IsInline != PrevNS->isInline()) |
| 6311 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, Loc, II, |
| 6312 | &IsInline, PrevNS); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6313 | } else if (PrevDecl) { |
| 6314 | // This is an invalid name redefinition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6315 | Diag(Loc, diag::err_redefinition_different_kind) |
| 6316 | << II; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6317 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6318 | IsInvalid = true; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6319 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6320 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6321 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 6322 | // This is the first "real" definition of the namespace "std", so update |
| 6323 | // our cache of the "std" namespace to point at this definition. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6324 | PrevNS = getStdNamespace(); |
| 6325 | IsStd = true; |
| 6326 | AddToKnown = !IsInline; |
| 6327 | } else { |
| 6328 | // We've seen this namespace for the first time. |
| 6329 | AddToKnown = !IsInline; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6330 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6331 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6332 | // Anonymous namespaces. |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6333 | |
| 6334 | // Determine whether the parent already has an anonymous namespace. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6335 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6336 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6337 | PrevNS = TU->getAnonymousNamespace(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6338 | } else { |
| 6339 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6340 | PrevNS = ND->getAnonymousNamespace(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6341 | } |
| 6342 | |
Richard Smith | d1a55a6 | 2012-10-04 22:13:39 +0000 | [diff] [blame] | 6343 | if (PrevNS && IsInline != PrevNS->isInline()) |
| 6344 | DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, NamespaceLoc, II, |
| 6345 | &IsInline, PrevNS); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6346 | } |
| 6347 | |
| 6348 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, IsInline, |
| 6349 | StartLoc, Loc, II, PrevNS); |
Douglas Gregor | 6731074 | 2012-01-10 22:14:10 +0000 | [diff] [blame] | 6350 | if (IsInvalid) |
| 6351 | Namespc->setInvalidDecl(); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6352 | |
| 6353 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 6354 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6355 | // FIXME: Should we be merging attributes? |
| 6356 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 6357 | PushNamespaceVisibilityAttr(Attr, Loc); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6358 | |
| 6359 | if (IsStd) |
| 6360 | StdNamespace = Namespc; |
| 6361 | if (AddToKnown) |
| 6362 | KnownNamespaces[Namespc] = false; |
| 6363 | |
| 6364 | if (II) { |
| 6365 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 6366 | } else { |
| 6367 | // Link the anonymous namespace into its parent. |
| 6368 | DeclContext *Parent = CurContext->getRedeclContext(); |
| 6369 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 6370 | TU->setAnonymousNamespace(Namespc); |
| 6371 | } else { |
| 6372 | cast<NamespaceDecl>(Parent)->setAnonymousNamespace(Namespc); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6373 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6374 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 6375 | CurContext->addDecl(Namespc); |
| 6376 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 6377 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 6378 | // behaves as if it were replaced by |
| 6379 | // namespace unique { /* empty body */ } |
| 6380 | // using namespace unique; |
| 6381 | // namespace unique { namespace-body } |
| 6382 | // where all occurrences of 'unique' in a translation unit are |
| 6383 | // replaced by the same identifier and this identifier differs |
| 6384 | // from all other identifiers in the entire program. |
| 6385 | |
| 6386 | // We just create the namespace with an empty name and then add an |
| 6387 | // implicit using declaration, just like the standard suggests. |
| 6388 | // |
| 6389 | // CodeGen enforces the "universally unique" aspect by giving all |
| 6390 | // declarations semantically contained within an anonymous |
| 6391 | // namespace internal linkage. |
| 6392 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6393 | if (!PrevNS) { |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6394 | UsingDirectiveDecl* UD |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6395 | = UsingDirectiveDecl::Create(Context, Parent, |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6396 | /* 'using' */ LBrace, |
| 6397 | /* 'namespace' */ SourceLocation(), |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 6398 | /* qualifier */ NestedNameSpecifierLoc(), |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6399 | /* identifier */ SourceLocation(), |
| 6400 | Namespc, |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6401 | /* Ancestor */ Parent); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6402 | UD->setImplicit(); |
Nick Lewycky | 4b7631b | 2012-11-04 20:21:54 +0000 | [diff] [blame] | 6403 | Parent->addDecl(UD); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 6404 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6405 | } |
| 6406 | |
Dmitri Gribenko | a5ef44f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 6407 | ActOnDocumentableDecl(Namespc); |
| 6408 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6409 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 6410 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6411 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 6412 | // for the namespace has the declarations that showed up in that particular |
| 6413 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6414 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6415 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6418 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 6419 | /// is a namespace alias, returns the namespace it points to. |
| 6420 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 6421 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 6422 | return AD->getNamespace(); |
| 6423 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 6424 | } |
| 6425 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6426 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 6427 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6428 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6429 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 6430 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6431 | Namespc->setRBraceLoc(RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6432 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 6433 | if (Namespc->hasAttr<VisibilityAttr>()) |
Rafael Espindola | 20039ae | 2012-02-01 23:24:59 +0000 | [diff] [blame] | 6434 | PopPragmaVisibility(true, RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 6435 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6436 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 6437 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 6438 | return cast_or_null<CXXRecordDecl>( |
| 6439 | StdBadAlloc.get(Context.getExternalSource())); |
| 6440 | } |
| 6441 | |
| 6442 | NamespaceDecl *Sema::getStdNamespace() const { |
| 6443 | return cast_or_null<NamespaceDecl>( |
| 6444 | StdNamespace.get(Context.getExternalSource())); |
| 6445 | } |
| 6446 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6447 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 6448 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 6449 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6450 | if (!StdNamespace) { |
| 6451 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 6452 | StdNamespace = NamespaceDecl::Create(Context, |
| 6453 | Context.getTranslationUnitDecl(), |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6454 | /*Inline=*/false, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 6455 | SourceLocation(), SourceLocation(), |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 6456 | &PP.getIdentifierTable().get("std"), |
| 6457 | /*PrevDecl=*/0); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 6458 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6459 | } |
| 6460 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 6461 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6462 | } |
| 6463 | |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6464 | bool Sema::isStdInitializerList(QualType Ty, QualType *Element) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6465 | assert(getLangOpts().CPlusPlus && |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6466 | "Looking for std::initializer_list outside of C++."); |
| 6467 | |
| 6468 | // We're looking for implicit instantiations of |
| 6469 | // template <typename E> class std::initializer_list. |
| 6470 | |
| 6471 | if (!StdNamespace) // If we haven't seen namespace std yet, this can't be it. |
| 6472 | return false; |
| 6473 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6474 | ClassTemplateDecl *Template = 0; |
| 6475 | const TemplateArgument *Arguments = 0; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6476 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6477 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6478 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6479 | ClassTemplateSpecializationDecl *Specialization = |
| 6480 | dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()); |
| 6481 | if (!Specialization) |
| 6482 | return false; |
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 | Template = Specialization->getSpecializedTemplate(); |
| 6485 | Arguments = Specialization->getTemplateArgs().data(); |
| 6486 | } else if (const TemplateSpecializationType *TST = |
| 6487 | Ty->getAs<TemplateSpecializationType>()) { |
| 6488 | Template = dyn_cast_or_null<ClassTemplateDecl>( |
| 6489 | TST->getTemplateName().getAsTemplateDecl()); |
| 6490 | Arguments = TST->getArgs(); |
| 6491 | } |
| 6492 | if (!Template) |
| 6493 | return false; |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6494 | |
| 6495 | if (!StdInitializerList) { |
| 6496 | // Haven't recognized std::initializer_list yet, maybe this is it. |
| 6497 | CXXRecordDecl *TemplateClass = Template->getTemplatedDecl(); |
| 6498 | if (TemplateClass->getIdentifier() != |
| 6499 | &PP.getIdentifierTable().get("initializer_list") || |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6500 | !getStdNamespace()->InEnclosingNamespaceSetOf( |
| 6501 | TemplateClass->getDeclContext())) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6502 | return false; |
| 6503 | // This is a template called std::initializer_list, but is it the right |
| 6504 | // template? |
| 6505 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6506 | if (Params->getMinRequiredArguments() != 1) |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6507 | return false; |
| 6508 | if (!isa<TemplateTypeParmDecl>(Params->getParam(0))) |
| 6509 | return false; |
| 6510 | |
| 6511 | // It's the right template. |
| 6512 | StdInitializerList = Template; |
| 6513 | } |
| 6514 | |
| 6515 | if (Template != StdInitializerList) |
| 6516 | return false; |
| 6517 | |
| 6518 | // This is an instance of std::initializer_list. Find the argument type. |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 6519 | if (Element) |
| 6520 | *Element = Arguments[0].getAsType(); |
Sebastian Redl | 395e04d | 2012-01-17 22:49:33 +0000 | [diff] [blame] | 6521 | return true; |
| 6522 | } |
| 6523 | |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 6524 | static ClassTemplateDecl *LookupStdInitializerList(Sema &S, SourceLocation Loc){ |
| 6525 | NamespaceDecl *Std = S.getStdNamespace(); |
| 6526 | if (!Std) { |
| 6527 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 6528 | return 0; |
| 6529 | } |
| 6530 | |
| 6531 | LookupResult Result(S, &S.PP.getIdentifierTable().get("initializer_list"), |
| 6532 | Loc, Sema::LookupOrdinaryName); |
| 6533 | if (!S.LookupQualifiedName(Result, Std)) { |
| 6534 | S.Diag(Loc, diag::err_implied_std_initializer_list_not_found); |
| 6535 | return 0; |
| 6536 | } |
| 6537 | ClassTemplateDecl *Template = Result.getAsSingle<ClassTemplateDecl>(); |
| 6538 | if (!Template) { |
| 6539 | Result.suppressDiagnostics(); |
| 6540 | // We found something weird. Complain about the first thing we found. |
| 6541 | NamedDecl *Found = *Result.begin(); |
| 6542 | S.Diag(Found->getLocation(), diag::err_malformed_std_initializer_list); |
| 6543 | return 0; |
| 6544 | } |
| 6545 | |
| 6546 | // We found some template called std::initializer_list. Now verify that it's |
| 6547 | // correct. |
| 6548 | TemplateParameterList *Params = Template->getTemplateParameters(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 6549 | if (Params->getMinRequiredArguments() != 1 || |
| 6550 | !isa<TemplateTypeParmDecl>(Params->getParam(0))) { |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 6551 | S.Diag(Template->getLocation(), diag::err_malformed_std_initializer_list); |
| 6552 | return 0; |
| 6553 | } |
| 6554 | |
| 6555 | return Template; |
| 6556 | } |
| 6557 | |
| 6558 | QualType Sema::BuildStdInitializerList(QualType Element, SourceLocation Loc) { |
| 6559 | if (!StdInitializerList) { |
| 6560 | StdInitializerList = LookupStdInitializerList(*this, Loc); |
| 6561 | if (!StdInitializerList) |
| 6562 | return QualType(); |
| 6563 | } |
| 6564 | |
| 6565 | TemplateArgumentListInfo Args(Loc, Loc); |
| 6566 | Args.addArgument(TemplateArgumentLoc(TemplateArgument(Element), |
| 6567 | Context.getTrivialTypeSourceInfo(Element, |
| 6568 | Loc))); |
| 6569 | return Context.getCanonicalType( |
| 6570 | CheckTemplateIdType(TemplateName(StdInitializerList), Loc, Args)); |
| 6571 | } |
| 6572 | |
Sebastian Redl | 98d3606 | 2012-01-17 22:50:14 +0000 | [diff] [blame] | 6573 | bool Sema::isInitListConstructor(const CXXConstructorDecl* Ctor) { |
| 6574 | // C++ [dcl.init.list]p2: |
| 6575 | // A constructor is an initializer-list constructor if its first parameter |
| 6576 | // is of type std::initializer_list<E> or reference to possibly cv-qualified |
| 6577 | // std::initializer_list<E> for some type E, and either there are no other |
| 6578 | // parameters or else all other parameters have default arguments. |
| 6579 | if (Ctor->getNumParams() < 1 || |
| 6580 | (Ctor->getNumParams() > 1 && !Ctor->getParamDecl(1)->hasDefaultArg())) |
| 6581 | return false; |
| 6582 | |
| 6583 | QualType ArgType = Ctor->getParamDecl(0)->getType(); |
| 6584 | if (const ReferenceType *RT = ArgType->getAs<ReferenceType>()) |
| 6585 | ArgType = RT->getPointeeType().getUnqualifiedType(); |
| 6586 | |
| 6587 | return isStdInitializerList(ArgType, 0); |
| 6588 | } |
| 6589 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 6590 | /// \brief Determine whether a using statement is in a context where it will be |
| 6591 | /// apply in all contexts. |
| 6592 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 6593 | switch (CurContext->getDeclKind()) { |
| 6594 | case Decl::TranslationUnit: |
| 6595 | return true; |
| 6596 | case Decl::LinkageSpec: |
| 6597 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 6598 | default: |
| 6599 | return false; |
| 6600 | } |
| 6601 | } |
| 6602 | |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6603 | namespace { |
| 6604 | |
| 6605 | // Callback to only accept typo corrections that are namespaces. |
| 6606 | class NamespaceValidatorCCC : public CorrectionCandidateCallback { |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 6607 | public: |
| 6608 | bool ValidateCandidate(const TypoCorrection &candidate) LLVM_OVERRIDE { |
| 6609 | if (NamedDecl *ND = candidate.getCorrectionDecl()) |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6610 | return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND); |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6611 | return false; |
| 6612 | } |
| 6613 | }; |
| 6614 | |
| 6615 | } |
| 6616 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6617 | static bool TryNamespaceTypoCorrection(Sema &S, LookupResult &R, Scope *Sc, |
| 6618 | CXXScopeSpec &SS, |
| 6619 | SourceLocation IdentLoc, |
| 6620 | IdentifierInfo *Ident) { |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6621 | NamespaceValidatorCCC Validator; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6622 | R.clear(); |
| 6623 | if (TypoCorrection Corrected = S.CorrectTypo(R.getLookupNameInfo(), |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6624 | R.getLookupKind(), Sc, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 6625 | Validator)) { |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6626 | if (DeclContext *DC = S.computeDeclContext(SS, false)) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6627 | std::string CorrectedStr(Corrected.getAsString(S.getLangOpts())); |
| 6628 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6629 | Ident->getName().equals(CorrectedStr); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6630 | S.diagnoseTypo(Corrected, |
| 6631 | S.PDiag(diag::err_using_directive_member_suggest) |
| 6632 | << Ident << DC << DroppedSpecifier << SS.getRange(), |
| 6633 | S.PDiag(diag::note_namespace_defined_here)); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6634 | } else { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 6635 | S.diagnoseTypo(Corrected, |
| 6636 | S.PDiag(diag::err_using_directive_suggest) << Ident, |
| 6637 | S.PDiag(diag::note_namespace_defined_here)); |
Kaelyn Uhrain | b2567dd | 2013-07-02 23:47:44 +0000 | [diff] [blame] | 6638 | } |
Kaelyn Uhrain | 7d5e694 | 2012-01-11 19:37:46 +0000 | [diff] [blame] | 6639 | R.addDecl(Corrected.getCorrectionDecl()); |
| 6640 | return true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6641 | } |
| 6642 | return false; |
| 6643 | } |
| 6644 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6645 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6646 | SourceLocation UsingLoc, |
| 6647 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6648 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6649 | SourceLocation IdentLoc, |
| 6650 | IdentifierInfo *NamespcName, |
| 6651 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6652 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 6653 | assert(NamespcName && "Invalid NamespcName."); |
| 6654 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 6655 | |
| 6656 | // This can only happen along a recovery path. |
| 6657 | while (S->getFlags() & Scope::TemplateParamScope) |
| 6658 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6659 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6660 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6661 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6662 | NestedNameSpecifier *Qualifier = 0; |
| 6663 | if (SS.isSet()) |
| 6664 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6665 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 6666 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6667 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 6668 | LookupParsedName(R, S, &SS); |
| 6669 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6670 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6671 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6672 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6673 | R.clear(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6674 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 6675 | // "std" hasn't been defined yet, for GCC compatibility. |
| 6676 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 6677 | NamespcName->isStr("std")) { |
| 6678 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 6679 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6680 | R.resolveKind(); |
| 6681 | } |
| 6682 | // Otherwise, attempt typo correction. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 6683 | else TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 6684 | } |
| 6685 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6686 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6687 | NamedDecl *Named = R.getFoundDecl(); |
| 6688 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 6689 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6690 | // C++ [namespace.udir]p1: |
| 6691 | // A using-directive specifies that the names in the nominated |
| 6692 | // namespace can be used in the scope in which the |
| 6693 | // using-directive appears after the using-directive. During |
| 6694 | // unqualified name lookup (3.4.1), the names appear as if they |
| 6695 | // were declared in the nearest enclosing namespace which |
| 6696 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 6697 | // namespace. [Note: in this context, "contains" means "contains |
| 6698 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6699 | |
| 6700 | // Find enclosing context containing both using-directive and |
| 6701 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6702 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6703 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 6704 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 6705 | CommonAncestor = CommonAncestor->getParent(); |
| 6706 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6707 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 6708 | SS.getWithLocInContext(Context), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 6709 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 6710 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 6711 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
Eli Friedman | 2414697 | 2013-08-22 00:27:10 +0000 | [diff] [blame] | 6712 | !SourceMgr.isInMainFile(SourceMgr.getExpansionLoc(IdentLoc))) { |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 6713 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 6714 | } |
| 6715 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6716 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6717 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 6718 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6719 | } |
| 6720 | |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 6721 | if (UDir) |
| 6722 | ProcessDeclAttributeList(S, UDir, AttrList); |
| 6723 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6724 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6725 | } |
| 6726 | |
| 6727 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 6728 | // If the scope has an associated entity and the using directive is at |
| 6729 | // namespace or translation unit scope, add the UsingDirectiveDecl into |
| 6730 | // its lookup structure so qualified name lookup can find it. |
| 6731 | DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity()); |
| 6732 | if (Ctx && !Ctx->isFunctionOrMethod()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6733 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 6734 | else |
Richard Smith | 1b7f9cb | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 6735 | // Otherwise, it is at block sope. The using-directives will affect lookup |
| 6736 | // only to the end of the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6737 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 6738 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6739 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6740 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6741 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 6742 | AccessSpecifier AS, |
| 6743 | bool HasUsingKeyword, |
| 6744 | SourceLocation UsingLoc, |
| 6745 | CXXScopeSpec &SS, |
| 6746 | UnqualifiedId &Name, |
| 6747 | AttributeList *AttrList, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 6748 | bool HasTypenameKeyword, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 6749 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 6750 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6751 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6752 | switch (Name.getKind()) { |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 6753 | case UnqualifiedId::IK_ImplicitSelfParam: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6754 | case UnqualifiedId::IK_Identifier: |
| 6755 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 6756 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6757 | case UnqualifiedId::IK_ConversionFunctionId: |
| 6758 | break; |
| 6759 | |
| 6760 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 6761 | case UnqualifiedId::IK_ConstructorTemplateId: |
Richard Smith | a1366cb | 2012-04-27 19:33:05 +0000 | [diff] [blame] | 6762 | // C++11 inheriting constructors. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6763 | Diag(Name.getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6764 | getLangOpts().CPlusPlus11 ? |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6765 | diag::warn_cxx98_compat_using_decl_constructor : |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6766 | diag::err_using_decl_constructor) |
| 6767 | << SS.getRange(); |
| 6768 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6769 | if (getLangOpts().CPlusPlus11) break; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6770 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6771 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6772 | |
| 6773 | case UnqualifiedId::IK_DestructorName: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6774 | Diag(Name.getLocStart(), diag::err_using_decl_destructor) |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6775 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6776 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6777 | |
| 6778 | case UnqualifiedId::IK_TemplateId: |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 6779 | Diag(Name.getLocStart(), diag::err_using_decl_template_id) |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 6780 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
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 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6783 | |
| 6784 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 6785 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6786 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6787 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6788 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6789 | // Warn about access declarations. |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 6790 | if (!HasUsingKeyword) { |
Enea Zaffanella | d4de59d | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 6791 | Diag(Name.getLocStart(), |
Richard Smith | 1b2209f | 2013-06-13 02:12:17 +0000 | [diff] [blame] | 6792 | getLangOpts().CPlusPlus11 ? diag::err_access_decl |
| 6793 | : diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6794 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 6795 | } |
| 6796 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 6797 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 6798 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 6799 | return 0; |
| 6800 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6801 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 6802 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6803 | /* IsInstantiation */ false, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 6804 | HasTypenameKeyword, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 6805 | if (UD) |
| 6806 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6807 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6808 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 6809 | } |
| 6810 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6811 | /// \brief Determine whether a using declaration considers the given |
| 6812 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 6813 | /// the same entity or are both typedefs of the same type. |
| 6814 | static bool |
| 6815 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 6816 | bool &SuppressRedeclaration) { |
| 6817 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 6818 | SuppressRedeclaration = false; |
| 6819 | return true; |
| 6820 | } |
| 6821 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 6822 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
| 6823 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6824 | SuppressRedeclaration = true; |
| 6825 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 6826 | TD2->getUnderlyingType()); |
| 6827 | } |
| 6828 | |
| 6829 | return false; |
| 6830 | } |
| 6831 | |
| 6832 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6833 | /// Determines whether to create a using shadow decl for a particular |
| 6834 | /// decl, given the set of decls existing prior to this using lookup. |
| 6835 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 6836 | const LookupResult &Previous) { |
| 6837 | // Diagnose finding a decl which is not from a base class of the |
| 6838 | // current class. We do this now because there are cases where this |
| 6839 | // function will silently decide not to build a shadow decl, which |
| 6840 | // will pre-empt further diagnostics. |
| 6841 | // |
| 6842 | // We don't need to do this in C++0x because we do the check once on |
| 6843 | // the qualifier. |
| 6844 | // |
| 6845 | // FIXME: diagnose the following if we care enough: |
| 6846 | // struct A { int foo; }; |
| 6847 | // struct B : A { using A::foo; }; |
| 6848 | // template <class T> struct C : A {}; |
| 6849 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 6850 | // This is invalid (during instantiation) in C++03 because B::foo |
| 6851 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 6852 | // We can't diagnose it immediately because C<T> is an unknown |
| 6853 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 6854 | // to A::foo, which will look well-formed when we instantiate. |
| 6855 | // The right solution is to not collapse the shadow-decl chain. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6856 | if (!getLangOpts().CPlusPlus11 && CurContext->isRecord()) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6857 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 6858 | |
| 6859 | // Handle enums and anonymous structs. |
| 6860 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 6861 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 6862 | while (OrigRec->isAnonymousStructOrUnion()) |
| 6863 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 6864 | |
| 6865 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 6866 | if (OrigDC == CurContext) { |
| 6867 | Diag(Using->getLocation(), |
| 6868 | diag::err_using_decl_nested_name_specifier_is_current_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6869 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6870 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 6871 | return true; |
| 6872 | } |
| 6873 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6874 | Diag(Using->getQualifierLoc().getBeginLoc(), |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6875 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6876 | << Using->getQualifier() |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6877 | << cast<CXXRecordDecl>(CurContext) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6878 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6879 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 6880 | return true; |
| 6881 | } |
| 6882 | } |
| 6883 | |
| 6884 | if (Previous.empty()) return false; |
| 6885 | |
| 6886 | NamedDecl *Target = Orig; |
| 6887 | if (isa<UsingShadowDecl>(Target)) |
| 6888 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 6889 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 6890 | // If the target happens to be one of the previous declarations, we |
| 6891 | // don't have a conflict. |
| 6892 | // |
| 6893 | // FIXME: but we might be increasing its access, in which case we |
| 6894 | // should redeclare it. |
| 6895 | NamedDecl *NonTag = 0, *Tag = 0; |
| 6896 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 6897 | I != E; ++I) { |
| 6898 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 6899 | bool Result; |
| 6900 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 6901 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 6902 | |
| 6903 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 6904 | } |
| 6905 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6906 | if (Target->isFunctionOrFunctionTemplate()) { |
| 6907 | FunctionDecl *FD; |
| 6908 | if (isa<FunctionTemplateDecl>(Target)) |
| 6909 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 6910 | else |
| 6911 | FD = cast<FunctionDecl>(Target); |
| 6912 | |
| 6913 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 6914 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6915 | case Ovl_Overload: |
| 6916 | return false; |
| 6917 | |
| 6918 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6919 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6920 | break; |
| 6921 | |
| 6922 | // We found a decl with the exact signature. |
| 6923 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6924 | // If we're in a record, we want to hide the target, so we |
| 6925 | // return true (without a diagnostic) to tell the caller not to |
| 6926 | // build a shadow decl. |
| 6927 | if (CurContext->isRecord()) |
| 6928 | return true; |
| 6929 | |
| 6930 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6931 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6932 | break; |
| 6933 | } |
| 6934 | |
| 6935 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6936 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 6937 | return true; |
| 6938 | } |
| 6939 | |
| 6940 | // Target is not a function. |
| 6941 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6942 | if (isa<TagDecl>(Target)) { |
| 6943 | // No conflict between a tag and a non-tag. |
| 6944 | if (!Tag) return false; |
| 6945 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6946 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6947 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6948 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 6949 | return true; |
| 6950 | } |
| 6951 | |
| 6952 | // No conflict between a tag and a non-tag. |
| 6953 | if (!NonTag) return false; |
| 6954 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 6955 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6956 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 6957 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 6958 | return true; |
| 6959 | } |
| 6960 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6961 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6962 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6963 | UsingDecl *UD, |
| 6964 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6965 | |
| 6966 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6967 | NamedDecl *Target = Orig; |
| 6968 | if (isa<UsingShadowDecl>(Target)) { |
| 6969 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 6970 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6971 | } |
| 6972 | |
| 6973 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6974 | = UsingShadowDecl::Create(Context, CurContext, |
| 6975 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6976 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 6977 | |
| 6978 | Shadow->setAccess(UD->getAccess()); |
| 6979 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 6980 | Shadow->setInvalidDecl(); |
| 6981 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6982 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6983 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6984 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6985 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 6986 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6987 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6988 | return Shadow; |
| 6989 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 6990 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 6991 | /// Hides a using shadow declaration. This is required by the current |
| 6992 | /// using-decl implementation when a resolvable using declaration in a |
| 6993 | /// class is followed by a declaration which would hide or override |
| 6994 | /// one or more of the using decl's targets; for example: |
| 6995 | /// |
| 6996 | /// struct Base { void foo(int); }; |
| 6997 | /// struct Derived : Base { |
| 6998 | /// using Base::foo; |
| 6999 | /// void foo(int); |
| 7000 | /// }; |
| 7001 | /// |
| 7002 | /// The governing language is C++03 [namespace.udecl]p12: |
| 7003 | /// |
| 7004 | /// When a using-declaration brings names from a base class into a |
| 7005 | /// derived class scope, member functions in the derived class |
| 7006 | /// override and/or hide member functions with the same name and |
| 7007 | /// parameter types in a base class (rather than conflicting). |
| 7008 | /// |
| 7009 | /// There are two ways to implement this: |
| 7010 | /// (1) optimistically create shadow decls when they're not hidden |
| 7011 | /// by existing declarations, or |
| 7012 | /// (2) don't create any shadow decls (or at least don't make them |
| 7013 | /// visible) until we've fully parsed/instantiated the class. |
| 7014 | /// The problem with (1) is that we might have to retroactively remove |
| 7015 | /// a shadow decl, which requires several O(n) operations because the |
| 7016 | /// decl structures are (very reasonably) not designed for removal. |
| 7017 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 7018 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 7019 | if (Shadow->getDeclName().getNameKind() == |
| 7020 | DeclarationName::CXXConversionFunctionName) |
| 7021 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 7022 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7023 | // Remove it from the DeclContext... |
| 7024 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7025 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7026 | // ...and the scope, if applicable... |
| 7027 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7028 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7029 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7030 | } |
| 7031 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7032 | // ...and the using decl. |
| 7033 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 7034 | |
| 7035 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 7036 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7037 | } |
| 7038 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7039 | namespace { |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7040 | class UsingValidatorCCC : public CorrectionCandidateCallback { |
| 7041 | public: |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7042 | UsingValidatorCCC(bool HasTypenameKeyword, bool IsInstantiation) |
| 7043 | : HasTypenameKeyword(HasTypenameKeyword), |
| 7044 | IsInstantiation(IsInstantiation) {} |
| 7045 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7046 | bool ValidateCandidate(const TypoCorrection &Candidate) LLVM_OVERRIDE { |
| 7047 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
| 7048 | |
| 7049 | // Keywords are not valid here. |
| 7050 | if (!ND || isa<NamespaceDecl>(ND)) |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7051 | return false; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7052 | |
| 7053 | // Completely unqualified names are invalid for a 'using' declaration. |
| 7054 | if (Candidate.WillReplaceSpecifier() && !Candidate.getCorrectionSpecifier()) |
| 7055 | return false; |
| 7056 | |
| 7057 | if (isa<TypeDecl>(ND)) |
| 7058 | return HasTypenameKeyword || !IsInstantiation; |
| 7059 | |
| 7060 | return !HasTypenameKeyword; |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7061 | } |
| 7062 | |
| 7063 | private: |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7064 | bool HasTypenameKeyword; |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7065 | bool IsInstantiation; |
| 7066 | }; |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 7067 | } // end anonymous namespace |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7068 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7069 | /// Builds a using declaration. |
| 7070 | /// |
| 7071 | /// \param IsInstantiation - Whether this call arises from an |
| 7072 | /// instantiation of an unresolved using declaration. We treat |
| 7073 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7074 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 7075 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7076 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7077 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7078 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7079 | bool IsInstantiation, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7080 | bool HasTypenameKeyword, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7081 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7082 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7083 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7084 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 7085 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 7086 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7087 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7088 | if (SS.isEmpty()) { |
| 7089 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 7090 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7091 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7092 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7093 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7094 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7095 | ForRedeclaration); |
| 7096 | Previous.setHideTags(false); |
| 7097 | if (S) { |
| 7098 | LookupName(Previous, S); |
| 7099 | |
| 7100 | // It is really dumb that we have to do this. |
| 7101 | LookupResult::Filter F = Previous.makeFilter(); |
| 7102 | while (F.hasNext()) { |
| 7103 | NamedDecl *D = F.next(); |
| 7104 | if (!isDeclInScope(D, CurContext, S)) |
| 7105 | F.erase(); |
| 7106 | } |
| 7107 | F.done(); |
| 7108 | } else { |
| 7109 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 7110 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 7111 | LookupQualifiedName(Previous, CurContext); |
| 7112 | } |
| 7113 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7114 | // Check for invalid redeclarations. |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7115 | if (CheckUsingDeclRedeclaration(UsingLoc, HasTypenameKeyword, |
| 7116 | SS, IdentLoc, Previous)) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7117 | return 0; |
| 7118 | |
| 7119 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7120 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 7121 | return 0; |
| 7122 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 7123 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7124 | NamedDecl *D; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7125 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 7126 | if (!LookupContext) { |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7127 | if (HasTypenameKeyword) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7128 | // FIXME: not all declaration name kinds are legal here |
| 7129 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 7130 | UsingLoc, TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7131 | QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7132 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7133 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7134 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 7135 | QualifierLoc, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7136 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7137 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7138 | D = UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7139 | NameInfo, HasTypenameKeyword); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 7140 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7141 | D->setAccess(AS); |
| 7142 | CurContext->addDecl(D); |
| 7143 | |
| 7144 | if (!LookupContext) return D; |
| 7145 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7146 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 7147 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7148 | UD->setInvalidDecl(); |
| 7149 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7150 | } |
| 7151 | |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7152 | // The normal rules do not apply to inheriting constructor declarations. |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7153 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7154 | if (CheckInheritingConstructorUsingDecl(UD)) |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 7155 | UD->setInvalidDecl(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7156 | return UD; |
| 7157 | } |
| 7158 | |
| 7159 | // Otherwise, look up the target name. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7160 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 7161 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7162 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7163 | // Unlike most lookups, we don't always want to hide tag |
| 7164 | // declarations: tag names are visible through the using declaration |
| 7165 | // even if hidden by ordinary names, *except* in a dependent context |
| 7166 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7167 | if (!IsInstantiation) |
| 7168 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7169 | |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 7170 | // For the purposes of this lookup, we have a base object type |
| 7171 | // equal to that of the current context. |
| 7172 | if (CurContext->isRecord()) { |
| 7173 | R.setBaseObjectType( |
| 7174 | Context.getTypeDeclType(cast<CXXRecordDecl>(CurContext))); |
| 7175 | } |
| 7176 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7177 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7178 | |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7179 | // Try to correct typos if possible. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7180 | if (R.empty()) { |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7181 | UsingValidatorCCC CCC(HasTypenameKeyword, IsInstantiation); |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7182 | if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(), |
| 7183 | R.getLookupKind(), S, &SS, CCC)){ |
| 7184 | // We reject any correction for which ND would be NULL. |
| 7185 | NamedDecl *ND = Corrected.getCorrectionDecl(); |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7186 | R.setLookupName(Corrected.getCorrection()); |
| 7187 | R.addDecl(ND); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 7188 | // We reject candidates where DroppedSpecifier == true, hence the |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7189 | // literal '0' below. |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 7190 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest) |
| 7191 | << NameInfo.getName() << LookupContext << 0 |
| 7192 | << SS.getRange()); |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7193 | } else { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 7194 | Diag(IdentLoc, diag::err_no_member) |
Kaelyn Uhrain | 0daf1f4 | 2013-07-10 17:34:22 +0000 | [diff] [blame] | 7195 | << NameInfo.getName() << LookupContext << SS.getRange(); |
| 7196 | UD->setInvalidDecl(); |
| 7197 | return UD; |
| 7198 | } |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7199 | } |
| 7200 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7201 | if (R.isAmbiguous()) { |
| 7202 | UD->setInvalidDecl(); |
| 7203 | return UD; |
| 7204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7205 | |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7206 | if (HasTypenameKeyword) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7207 | // 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] | 7208 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7209 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 7210 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 7211 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 7212 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7213 | UD->setInvalidDecl(); |
| 7214 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7215 | } |
| 7216 | } else { |
| 7217 | // If we asked for a non-typename and we got a type, error out, |
| 7218 | // but only if this is an instantiation of an unresolved using |
| 7219 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7220 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7221 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 7222 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7223 | UD->setInvalidDecl(); |
| 7224 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 7225 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 7226 | } |
| 7227 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 7228 | // C++0x N2914 [namespace.udecl]p6: |
| 7229 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7230 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 7231 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 7232 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7233 | UD->setInvalidDecl(); |
| 7234 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 7235 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7236 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7237 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 7238 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 7239 | BuildUsingShadowDecl(S, UD, *I); |
| 7240 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 7241 | |
| 7242 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 7243 | } |
| 7244 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7245 | /// Additional checks for a using declaration referring to a constructor name. |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7246 | bool Sema::CheckInheritingConstructorUsingDecl(UsingDecl *UD) { |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7247 | assert(!UD->hasTypename() && "expecting a constructor name"); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7248 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7249 | const Type *SourceType = UD->getQualifier()->getAsType(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7250 | assert(SourceType && |
| 7251 | "Using decl naming constructor doesn't have type in scope spec."); |
| 7252 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 7253 | |
| 7254 | // Check whether the named type is a direct base class. |
| 7255 | CanQualType CanonicalSourceType = SourceType->getCanonicalTypeUnqualified(); |
| 7256 | CXXRecordDecl::base_class_iterator BaseIt, BaseE; |
| 7257 | for (BaseIt = TargetClass->bases_begin(), BaseE = TargetClass->bases_end(); |
| 7258 | BaseIt != BaseE; ++BaseIt) { |
| 7259 | CanQualType BaseType = BaseIt->getType()->getCanonicalTypeUnqualified(); |
| 7260 | if (CanonicalSourceType == BaseType) |
| 7261 | break; |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7262 | if (BaseIt->getType()->isDependentType()) |
| 7263 | break; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7264 | } |
| 7265 | |
| 7266 | if (BaseIt == BaseE) { |
| 7267 | // Did not find SourceType in the bases. |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7268 | Diag(UD->getUsingLoc(), |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7269 | diag::err_using_decl_constructor_not_in_direct_base) |
| 7270 | << UD->getNameInfo().getSourceRange() |
| 7271 | << QualType(SourceType, 0) << TargetClass; |
| 7272 | return true; |
| 7273 | } |
| 7274 | |
Richard Smith | c5a89a1 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 7275 | if (!CurContext->isDependentContext()) |
| 7276 | BaseIt->setInheritConstructors(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 7277 | |
| 7278 | return false; |
| 7279 | } |
| 7280 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7281 | /// Checks that the given using declaration is not an invalid |
| 7282 | /// redeclaration. Note that this is checking only for the using decl |
| 7283 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 7284 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7285 | bool HasTypenameKeyword, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7286 | const CXXScopeSpec &SS, |
| 7287 | SourceLocation NameLoc, |
| 7288 | const LookupResult &Prev) { |
| 7289 | // C++03 [namespace.udecl]p8: |
| 7290 | // C++0x [namespace.udecl]p10: |
| 7291 | // A using-declaration is a declaration and can therefore be used |
| 7292 | // repeatedly where (and only where) multiple declarations are |
| 7293 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 7294 | // |
John McCall | 8a72621 | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 7295 | // That's in non-member contexts. |
| 7296 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7297 | return false; |
| 7298 | |
| 7299 | NestedNameSpecifier *Qual |
| 7300 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 7301 | |
| 7302 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 7303 | NamedDecl *D = *I; |
| 7304 | |
| 7305 | bool DTypename; |
| 7306 | NestedNameSpecifier *DQual; |
| 7307 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7308 | DTypename = UD->hasTypename(); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7309 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7310 | } else if (UnresolvedUsingValueDecl *UD |
| 7311 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 7312 | DTypename = false; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7313 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7314 | } else if (UnresolvedUsingTypenameDecl *UD |
| 7315 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 7316 | DTypename = true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 7317 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7318 | } else continue; |
| 7319 | |
| 7320 | // using decls differ if one says 'typename' and the other doesn't. |
| 7321 | // FIXME: non-dependent using decls? |
Enea Zaffanella | 8d030c7 | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 7322 | if (HasTypenameKeyword != DTypename) continue; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7323 | |
| 7324 | // using decls differ if they name different scopes (but note that |
| 7325 | // template instantiation can cause this check to trigger when it |
| 7326 | // didn't before instantiation). |
| 7327 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 7328 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 7329 | continue; |
| 7330 | |
| 7331 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 7332 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 7333 | return true; |
| 7334 | } |
| 7335 | |
| 7336 | return false; |
| 7337 | } |
| 7338 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7339 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7340 | /// Checks that the given nested-name qualifier used in a using decl |
| 7341 | /// in the current context is appropriately related to the current |
| 7342 | /// scope. If an error is found, diagnoses it and returns true. |
| 7343 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 7344 | const CXXScopeSpec &SS, |
| 7345 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7346 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7347 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7348 | if (!CurContext->isRecord()) { |
| 7349 | // C++03 [namespace.udecl]p3: |
| 7350 | // C++0x [namespace.udecl]p8: |
| 7351 | // A using-declaration for a class member shall be a member-declaration. |
| 7352 | |
| 7353 | // If we weren't able to compute a valid scope, it must be a |
| 7354 | // dependent class scope. |
| 7355 | if (!NamedContext || NamedContext->isRecord()) { |
| 7356 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 7357 | << SS.getRange(); |
| 7358 | return true; |
| 7359 | } |
| 7360 | |
| 7361 | // Otherwise, everything is known to be fine. |
| 7362 | return false; |
| 7363 | } |
| 7364 | |
| 7365 | // The current scope is a record. |
| 7366 | |
| 7367 | // If the named context is dependent, we can't decide much. |
| 7368 | if (!NamedContext) { |
| 7369 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 7370 | // nested-name-specifier does not refer to a base class, which is |
| 7371 | // still possible in some cases. |
| 7372 | |
| 7373 | // Otherwise we have to conservatively report that things might be |
| 7374 | // okay. |
| 7375 | return false; |
| 7376 | } |
| 7377 | |
| 7378 | if (!NamedContext->isRecord()) { |
| 7379 | // Ideally this would point at the last name in the specifier, |
| 7380 | // but we don't have that level of source info. |
| 7381 | Diag(SS.getRange().getBegin(), |
| 7382 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 7383 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 7384 | return true; |
| 7385 | } |
| 7386 | |
Douglas Gregor | 6fb0729 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 7387 | if (!NamedContext->isDependentContext() && |
| 7388 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 7389 | return true; |
| 7390 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7391 | if (getLangOpts().CPlusPlus11) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7392 | // C++0x [namespace.udecl]p3: |
| 7393 | // In a using-declaration used as a member-declaration, the |
| 7394 | // nested-name-specifier shall name a base class of the class |
| 7395 | // being defined. |
| 7396 | |
| 7397 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 7398 | cast<CXXRecordDecl>(NamedContext))) { |
| 7399 | if (CurContext == NamedContext) { |
| 7400 | Diag(NameLoc, |
| 7401 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 7402 | << SS.getRange(); |
| 7403 | return true; |
| 7404 | } |
| 7405 | |
| 7406 | Diag(SS.getRange().getBegin(), |
| 7407 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 7408 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 7409 | << cast<CXXRecordDecl>(CurContext) |
| 7410 | << SS.getRange(); |
| 7411 | return true; |
| 7412 | } |
| 7413 | |
| 7414 | return false; |
| 7415 | } |
| 7416 | |
| 7417 | // C++03 [namespace.udecl]p4: |
| 7418 | // A using-declaration used as a member-declaration shall refer |
| 7419 | // to a member of a base class of the class being defined [etc.]. |
| 7420 | |
| 7421 | // Salient point: SS doesn't have to name a base class as long as |
| 7422 | // lookup only finds members from base classes. Therefore we can |
| 7423 | // diagnose here only if we can prove that that can't happen, |
| 7424 | // i.e. if the class hierarchies provably don't intersect. |
| 7425 | |
| 7426 | // TODO: it would be nice if "definitely valid" results were cached |
| 7427 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 7428 | // need to be repeated. |
| 7429 | |
| 7430 | struct UserData { |
Benjamin Kramer | 8c43dcc | 2012-02-23 16:06:01 +0000 | [diff] [blame] | 7431 | llvm::SmallPtrSet<const CXXRecordDecl*, 4> Bases; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 7432 | |
| 7433 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 7434 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 7435 | Data->Bases.insert(Base); |
| 7436 | return true; |
| 7437 | } |
| 7438 | |
| 7439 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 7440 | return !Class->forallBases(collect, this); |
| 7441 | } |
| 7442 | |
| 7443 | /// Returns true if the base is dependent or is one of the |
| 7444 | /// accumulated base classes. |
| 7445 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 7446 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 7447 | return !Data->Bases.count(Base); |
| 7448 | } |
| 7449 | |
| 7450 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 7451 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 7452 | } |
| 7453 | }; |
| 7454 | |
| 7455 | UserData Data; |
| 7456 | |
| 7457 | // Returns false if we find a dependent base. |
| 7458 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 7459 | return false; |
| 7460 | |
| 7461 | // Returns false if the class has a dependent base or if it or one |
| 7462 | // of its bases is present in the base set of the current context. |
| 7463 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 7464 | return false; |
| 7465 | |
| 7466 | Diag(SS.getRange().getBegin(), |
| 7467 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 7468 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 7469 | << cast<CXXRecordDecl>(CurContext) |
| 7470 | << SS.getRange(); |
| 7471 | |
| 7472 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 7473 | } |
| 7474 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7475 | Decl *Sema::ActOnAliasDeclaration(Scope *S, |
| 7476 | AccessSpecifier AS, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7477 | MultiTemplateParamsArg TemplateParamLists, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7478 | SourceLocation UsingLoc, |
| 7479 | UnqualifiedId &Name, |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 7480 | AttributeList *AttrList, |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7481 | TypeResult Type) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7482 | // Skip up to the relevant declaration scope. |
| 7483 | while (S->getFlags() & Scope::TemplateParamScope) |
| 7484 | S = S->getParent(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7485 | assert((S->getFlags() & Scope::DeclScope) && |
| 7486 | "got alias-declaration outside of declaration scope"); |
| 7487 | |
| 7488 | if (Type.isInvalid()) |
| 7489 | return 0; |
| 7490 | |
| 7491 | bool Invalid = false; |
| 7492 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
| 7493 | TypeSourceInfo *TInfo = 0; |
Nick Lewycky | b79bf1d | 2011-05-02 01:07:19 +0000 | [diff] [blame] | 7494 | GetTypeFromParser(Type.get(), &TInfo); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7495 | |
| 7496 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
| 7497 | return 0; |
| 7498 | |
| 7499 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7500 | UPPC_DeclarationType)) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7501 | Invalid = true; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7502 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 7503 | TInfo->getTypeLoc().getBeginLoc()); |
| 7504 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7505 | |
| 7506 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, ForRedeclaration); |
| 7507 | LookupName(Previous, S); |
| 7508 | |
| 7509 | // Warn about shadowing the name of a template parameter. |
| 7510 | if (Previous.isSingleResult() && |
| 7511 | Previous.getFoundDecl()->isTemplateParameter()) { |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 7512 | DiagnoseTemplateParameterShadow(Name.StartLocation,Previous.getFoundDecl()); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7513 | Previous.clear(); |
| 7514 | } |
| 7515 | |
| 7516 | assert(Name.Kind == UnqualifiedId::IK_Identifier && |
| 7517 | "name in alias declaration must be an identifier"); |
| 7518 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 7519 | Name.StartLocation, |
| 7520 | Name.Identifier, TInfo); |
| 7521 | |
| 7522 | NewTD->setAccess(AS); |
| 7523 | |
| 7524 | if (Invalid) |
| 7525 | NewTD->setInvalidDecl(); |
| 7526 | |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 7527 | ProcessDeclAttributeList(S, NewTD, AttrList); |
| 7528 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7529 | CheckTypedefForVariablyModifiedType(S, NewTD); |
| 7530 | Invalid |= NewTD->isInvalidDecl(); |
| 7531 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7532 | bool Redeclaration = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7533 | |
| 7534 | NamedDecl *NewND; |
| 7535 | if (TemplateParamLists.size()) { |
| 7536 | TypeAliasTemplateDecl *OldDecl = 0; |
| 7537 | TemplateParameterList *OldTemplateParams = 0; |
| 7538 | |
| 7539 | if (TemplateParamLists.size() != 1) { |
| 7540 | Diag(UsingLoc, diag::err_alias_template_extra_headers) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7541 | << SourceRange(TemplateParamLists[1]->getTemplateLoc(), |
| 7542 | TemplateParamLists[TemplateParamLists.size()-1]->getRAngleLoc()); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7543 | } |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7544 | TemplateParameterList *TemplateParams = TemplateParamLists[0]; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7545 | |
| 7546 | // Only consider previous declarations in the same scope. |
| 7547 | FilterLookupForScope(Previous, CurContext, S, /*ConsiderLinkage*/false, |
| 7548 | /*ExplicitInstantiationOrSpecialization*/false); |
| 7549 | if (!Previous.empty()) { |
| 7550 | Redeclaration = true; |
| 7551 | |
| 7552 | OldDecl = Previous.getAsSingle<TypeAliasTemplateDecl>(); |
| 7553 | if (!OldDecl && !Invalid) { |
| 7554 | Diag(UsingLoc, diag::err_redefinition_different_kind) |
| 7555 | << Name.Identifier; |
| 7556 | |
| 7557 | NamedDecl *OldD = Previous.getRepresentativeDecl(); |
| 7558 | if (OldD->getLocation().isValid()) |
| 7559 | Diag(OldD->getLocation(), diag::note_previous_definition); |
| 7560 | |
| 7561 | Invalid = true; |
| 7562 | } |
| 7563 | |
| 7564 | if (!Invalid && OldDecl && !OldDecl->isInvalidDecl()) { |
| 7565 | if (TemplateParameterListsAreEqual(TemplateParams, |
| 7566 | OldDecl->getTemplateParameters(), |
| 7567 | /*Complain=*/true, |
| 7568 | TPL_TemplateMatch)) |
| 7569 | OldTemplateParams = OldDecl->getTemplateParameters(); |
| 7570 | else |
| 7571 | Invalid = true; |
| 7572 | |
| 7573 | TypeAliasDecl *OldTD = OldDecl->getTemplatedDecl(); |
| 7574 | if (!Invalid && |
| 7575 | !Context.hasSameType(OldTD->getUnderlyingType(), |
| 7576 | NewTD->getUnderlyingType())) { |
| 7577 | // FIXME: The C++0x standard does not clearly say this is ill-formed, |
| 7578 | // but we can't reasonably accept it. |
| 7579 | Diag(NewTD->getLocation(), diag::err_redefinition_different_typedef) |
| 7580 | << 2 << NewTD->getUnderlyingType() << OldTD->getUnderlyingType(); |
| 7581 | if (OldTD->getLocation().isValid()) |
| 7582 | Diag(OldTD->getLocation(), diag::note_previous_definition); |
| 7583 | Invalid = true; |
| 7584 | } |
| 7585 | } |
| 7586 | } |
| 7587 | |
| 7588 | // Merge any previous default template arguments into our parameters, |
| 7589 | // and check the parameter list. |
| 7590 | if (CheckTemplateParameterList(TemplateParams, OldTemplateParams, |
| 7591 | TPC_TypeAliasTemplate)) |
| 7592 | return 0; |
| 7593 | |
| 7594 | TypeAliasTemplateDecl *NewDecl = |
| 7595 | TypeAliasTemplateDecl::Create(Context, CurContext, UsingLoc, |
| 7596 | Name.Identifier, TemplateParams, |
| 7597 | NewTD); |
| 7598 | |
| 7599 | NewDecl->setAccess(AS); |
| 7600 | |
| 7601 | if (Invalid) |
| 7602 | NewDecl->setInvalidDecl(); |
| 7603 | else if (OldDecl) |
| 7604 | NewDecl->setPreviousDeclaration(OldDecl); |
| 7605 | |
| 7606 | NewND = NewDecl; |
| 7607 | } else { |
| 7608 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 7609 | NewND = NewTD; |
| 7610 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7611 | |
| 7612 | if (!Redeclaration) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7613 | PushOnScopeChains(NewND, S); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7614 | |
Dmitri Gribenko | c27bc80 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 7615 | ActOnDocumentableDecl(NewND); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 7616 | return NewND; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 7617 | } |
| 7618 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7619 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 7620 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 7621 | SourceLocation AliasLoc, |
| 7622 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 7623 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 7624 | SourceLocation IdentLoc, |
| 7625 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7626 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7627 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7628 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 7629 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7630 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7631 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 7632 | NamedDecl *PrevDecl |
| 7633 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 7634 | ForRedeclaration); |
| 7635 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 7636 | PrevDecl = 0; |
| 7637 | |
| 7638 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7639 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7640 | // 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] | 7641 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 7642 | // FIXME: At some point, we'll want to create the (redundant) |
| 7643 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7644 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 7645 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7646 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 7647 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7648 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7649 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 7650 | diag::err_redefinition_different_kind; |
| 7651 | Diag(AliasLoc, DiagID) << Alias; |
| 7652 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7653 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 7654 | } |
| 7655 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 7656 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7657 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7658 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7659 | if (R.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 7660 | if (!TryNamespaceTypoCorrection(*this, R, S, SS, IdentLoc, Ident)) { |
Richard Smith | bf9658c | 2012-04-05 23:13:23 +0000 | [diff] [blame] | 7661 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7662 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 7663 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 7664 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7665 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 7666 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7667 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 7668 | Alias, SS.getWithLocInContext(Context), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 7669 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7670 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 7671 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7672 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 7673 | } |
| 7674 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 7675 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7676 | Sema::ComputeDefaultedDefaultCtorExceptionSpec(SourceLocation Loc, |
| 7677 | CXXMethodDecl *MD) { |
| 7678 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 7679 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7680 | // C++ [except.spec]p14: |
| 7681 | // An implicitly declared special member function (Clause 12) shall have an |
| 7682 | // exception-specification. [...] |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7683 | ImplicitExceptionSpecification ExceptSpec(*this); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 7684 | if (ClassDecl->isInvalidDecl()) |
| 7685 | return ExceptSpec; |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7686 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7687 | // Direct base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7688 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 7689 | BEnd = ClassDecl->bases_end(); |
| 7690 | B != BEnd; ++B) { |
| 7691 | if (B->isVirtual()) // Handled below. |
| 7692 | continue; |
| 7693 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7694 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 7695 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 7696 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 7697 | // If this is a deleted function, add it anyway. This might be conformant |
| 7698 | // with the standard. This might not. I'm not sure. It might not matter. |
| 7699 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7700 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7701 | } |
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 | |
| 7704 | // Virtual base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7705 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 7706 | BEnd = ClassDecl->vbases_end(); |
| 7707 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7708 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 7709 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 7710 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 7711 | // If this is a deleted function, add it anyway. This might be conformant |
| 7712 | // with the standard. This might not. I'm not sure. It might not matter. |
| 7713 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7714 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7715 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7716 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 7717 | |
| 7718 | // Field constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7719 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 7720 | FEnd = ClassDecl->field_end(); |
| 7721 | F != FEnd; ++F) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7722 | if (F->hasInClassInitializer()) { |
| 7723 | if (Expr *E = F->getInClassInitializer()) |
| 7724 | ExceptSpec.CalledExpr(E); |
| 7725 | else if (!F->isInvalidDecl()) |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7726 | // DR1351: |
| 7727 | // If the brace-or-equal-initializer of a non-static data member |
| 7728 | // invokes a defaulted default constructor of its class or of an |
| 7729 | // enclosing class in a potentially evaluated subexpression, the |
| 7730 | // program is ill-formed. |
| 7731 | // |
| 7732 | // This resolution is unworkable: the exception specification of the |
| 7733 | // default constructor can be needed in an unevaluated context, in |
| 7734 | // particular, in the operand of a noexcept-expression, and we can be |
| 7735 | // unable to compute an exception specification for an enclosed class. |
| 7736 | // |
| 7737 | // We do not allow an in-class initializer to require the evaluation |
| 7738 | // of the exception specification for any in-class initializer whose |
| 7739 | // definition is not lexically complete. |
| 7740 | Diag(Loc, diag::err_in_class_initializer_references_def_ctor) << MD; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7741 | } else if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7742 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
Sean Hunt | b320e0c | 2011-06-10 03:50:41 +0000 | [diff] [blame] | 7743 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 7744 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 7745 | // If this is a deleted function, add it anyway. This might be conformant |
| 7746 | // with the standard. This might not. I'm not sure. It might not matter. |
| 7747 | // In particular, the problem is that this function never gets called. It |
| 7748 | // might just be ill-formed because this function attempts to refer to |
| 7749 | // a deleted function here. |
| 7750 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 7751 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7752 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7753 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7754 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 7755 | return ExceptSpec; |
| 7756 | } |
| 7757 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 7758 | Sema::ImplicitExceptionSpecification |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 7759 | Sema::ComputeInheritingCtorExceptionSpec(CXXConstructorDecl *CD) { |
| 7760 | CXXRecordDecl *ClassDecl = CD->getParent(); |
| 7761 | |
| 7762 | // C++ [except.spec]p14: |
| 7763 | // An inheriting constructor [...] shall have an exception-specification. [...] |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 7764 | ImplicitExceptionSpecification ExceptSpec(*this); |
Richard Smith | 0b0ca47 | 2013-04-10 06:11:48 +0000 | [diff] [blame] | 7765 | if (ClassDecl->isInvalidDecl()) |
| 7766 | return ExceptSpec; |
| 7767 | |
| 7768 | // Inherited constructor. |
| 7769 | const CXXConstructorDecl *InheritedCD = CD->getInheritedConstructor(); |
| 7770 | const CXXRecordDecl *InheritedDecl = InheritedCD->getParent(); |
| 7771 | // FIXME: Copying or moving the parameters could add extra exceptions to the |
| 7772 | // set, as could the default arguments for the inherited constructor. This |
| 7773 | // will be addressed when we implement the resolution of core issue 1351. |
| 7774 | ExceptSpec.CalledDecl(CD->getLocStart(), InheritedCD); |
| 7775 | |
| 7776 | // Direct base-class constructors. |
| 7777 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 7778 | BEnd = ClassDecl->bases_end(); |
| 7779 | B != BEnd; ++B) { |
| 7780 | if (B->isVirtual()) // Handled below. |
| 7781 | continue; |
| 7782 | |
| 7783 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 7784 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 7785 | if (BaseClassDecl == InheritedDecl) |
| 7786 | continue; |
| 7787 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 7788 | if (Constructor) |
| 7789 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
| 7790 | } |
| 7791 | } |
| 7792 | |
| 7793 | // Virtual base-class constructors. |
| 7794 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 7795 | BEnd = ClassDecl->vbases_end(); |
| 7796 | B != BEnd; ++B) { |
| 7797 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 7798 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 7799 | if (BaseClassDecl == InheritedDecl) |
| 7800 | continue; |
| 7801 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(BaseClassDecl); |
| 7802 | if (Constructor) |
| 7803 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
| 7804 | } |
| 7805 | } |
| 7806 | |
| 7807 | // Field constructors. |
| 7808 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 7809 | FEnd = ClassDecl->field_end(); |
| 7810 | F != FEnd; ++F) { |
| 7811 | if (F->hasInClassInitializer()) { |
| 7812 | if (Expr *E = F->getInClassInitializer()) |
| 7813 | ExceptSpec.CalledExpr(E); |
| 7814 | else if (!F->isInvalidDecl()) |
| 7815 | Diag(CD->getLocation(), |
| 7816 | diag::err_in_class_initializer_references_def_ctor) << CD; |
| 7817 | } else if (const RecordType *RecordTy |
| 7818 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 7819 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 7820 | CXXConstructorDecl *Constructor = LookupDefaultConstructor(FieldRecDecl); |
| 7821 | if (Constructor) |
| 7822 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
| 7823 | } |
| 7824 | } |
| 7825 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 7826 | return ExceptSpec; |
| 7827 | } |
| 7828 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 7829 | namespace { |
| 7830 | /// RAII object to register a special member as being currently declared. |
| 7831 | struct DeclaringSpecialMember { |
| 7832 | Sema &S; |
| 7833 | Sema::SpecialMemberDecl D; |
| 7834 | bool WasAlreadyBeingDeclared; |
| 7835 | |
| 7836 | DeclaringSpecialMember(Sema &S, CXXRecordDecl *RD, Sema::CXXSpecialMember CSM) |
| 7837 | : S(S), D(RD, CSM) { |
| 7838 | WasAlreadyBeingDeclared = !S.SpecialMembersBeingDeclared.insert(D); |
| 7839 | if (WasAlreadyBeingDeclared) |
| 7840 | // This almost never happens, but if it does, ensure that our cache |
| 7841 | // doesn't contain a stale result. |
| 7842 | S.SpecialMemberCache.clear(); |
| 7843 | |
| 7844 | // FIXME: Register a note to be produced if we encounter an error while |
| 7845 | // declaring the special member. |
| 7846 | } |
| 7847 | ~DeclaringSpecialMember() { |
| 7848 | if (!WasAlreadyBeingDeclared) |
| 7849 | S.SpecialMembersBeingDeclared.erase(D); |
| 7850 | } |
| 7851 | |
| 7852 | /// \brief Are we already trying to declare this special member? |
| 7853 | bool isAlreadyBeingDeclared() const { |
| 7854 | return WasAlreadyBeingDeclared; |
| 7855 | } |
| 7856 | }; |
| 7857 | } |
| 7858 | |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 7859 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 7860 | CXXRecordDecl *ClassDecl) { |
| 7861 | // C++ [class.ctor]p5: |
| 7862 | // A default constructor for a class X is a constructor of class X |
| 7863 | // that can be called without an argument. If there is no |
| 7864 | // user-declared constructor for class X, a default constructor is |
| 7865 | // implicitly declared. An implicitly-declared default constructor |
| 7866 | // is an inline public member of its class. |
Richard Smith | d0adeb6 | 2012-11-27 21:20:31 +0000 | [diff] [blame] | 7867 | assert(ClassDecl->needsImplicitDefaultConstructor() && |
Sean Hunt | 001cad9 | 2011-05-10 00:49:42 +0000 | [diff] [blame] | 7868 | "Should not build implicit default constructor!"); |
| 7869 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 7870 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDefaultConstructor); |
| 7871 | if (DSM.isAlreadyBeingDeclared()) |
| 7872 | return 0; |
| 7873 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 7874 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 7875 | CXXDefaultConstructor, |
| 7876 | false); |
| 7877 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 7878 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7879 | CanQualType ClassType |
| 7880 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7881 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7882 | DeclarationName Name |
| 7883 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 7884 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 7885 | CXXConstructorDecl *DefaultCon = CXXConstructorDecl::Create( |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7886 | Context, ClassDecl, ClassLoc, NameInfo, /*Type*/QualType(), /*TInfo=*/0, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 7887 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 7888 | Constexpr); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7889 | DefaultCon->setAccess(AS_public); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 7890 | DefaultCon->setDefaulted(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7891 | DefaultCon->setImplicit(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7892 | |
| 7893 | // Build an exception specification pointing back at this constructor. |
| 7894 | FunctionProtoType::ExtProtoInfo EPI; |
| 7895 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 7896 | EPI.ExceptionSpecDecl = DefaultCon; |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 7897 | DefaultCon->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 7898 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 7899 | // We don't need to use SpecialMemberIsTrivial here; triviality for default |
| 7900 | // constructors is easy to compute. |
| 7901 | DefaultCon->setTrivial(ClassDecl->hasTrivialDefaultConstructor()); |
| 7902 | |
| 7903 | if (ShouldDeleteSpecialMember(DefaultCon, CXXDefaultConstructor)) |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 7904 | SetDeclDeleted(DefaultCon, ClassLoc); |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 7905 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7906 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7907 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 7908 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 7909 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 7910 | PushOnScopeChains(DefaultCon, S, false); |
| 7911 | ClassDecl->addDecl(DefaultCon); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 7912 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 7913 | return DefaultCon; |
| 7914 | } |
| 7915 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 7916 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 7917 | CXXConstructorDecl *Constructor) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 7918 | assert((Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 7919 | !Constructor->doesThisDeclarationHaveABody() && |
| 7920 | !Constructor->isDeleted()) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 7921 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7922 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 7923 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 7924 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 7925 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 7926 | SynthesizedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 7927 | DiagnosticErrorTrap Trap(Diags); |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 7928 | if (SetCtorInitializers(Constructor, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 7929 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 7930 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Sean Hunt | f961ea5 | 2011-05-10 19:08:14 +0000 | [diff] [blame] | 7931 | << CXXDefaultConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 7932 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7933 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 7934 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7935 | |
| 7936 | SourceLocation Loc = Constructor->getLocation(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 7937 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 7938 | |
| 7939 | Constructor->setUsed(); |
| 7940 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 7941 | |
| 7942 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 7943 | L->CompletedImplicitDefinition(Constructor); |
| 7944 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 7945 | } |
| 7946 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7947 | void Sema::ActOnFinishDelayedMemberInitializers(Decl *D) { |
Richard Smith | 1d28caf | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 7948 | // Check that any explicitly-defaulted methods have exception specifications |
| 7949 | // compatible with their implicit exception specifications. |
| 7950 | CheckDelayedExplicitlyDefaultedMemberExceptionSpecs(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 7951 | } |
| 7952 | |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 7953 | namespace { |
| 7954 | /// Information on inheriting constructors to declare. |
| 7955 | class InheritingConstructorInfo { |
| 7956 | public: |
| 7957 | InheritingConstructorInfo(Sema &SemaRef, CXXRecordDecl *Derived) |
| 7958 | : SemaRef(SemaRef), Derived(Derived) { |
| 7959 | // Mark the constructors that we already have in the derived class. |
| 7960 | // |
| 7961 | // C++11 [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 7962 | // unless there is a user-declared constructor with the same signature in |
| 7963 | // the class where the using-declaration appears. |
| 7964 | visitAll(Derived, &InheritingConstructorInfo::noteDeclaredInDerived); |
| 7965 | } |
| 7966 | |
| 7967 | void inheritAll(CXXRecordDecl *RD) { |
| 7968 | visitAll(RD, &InheritingConstructorInfo::inherit); |
| 7969 | } |
| 7970 | |
| 7971 | private: |
| 7972 | /// Information about an inheriting constructor. |
| 7973 | struct InheritingConstructor { |
| 7974 | InheritingConstructor() |
| 7975 | : DeclaredInDerived(false), BaseCtor(0), DerivedCtor(0) {} |
| 7976 | |
| 7977 | /// If \c true, a constructor with this signature is already declared |
| 7978 | /// in the derived class. |
| 7979 | bool DeclaredInDerived; |
| 7980 | |
| 7981 | /// The constructor which is inherited. |
| 7982 | const CXXConstructorDecl *BaseCtor; |
| 7983 | |
| 7984 | /// The derived constructor we declared. |
| 7985 | CXXConstructorDecl *DerivedCtor; |
| 7986 | }; |
| 7987 | |
| 7988 | /// Inheriting constructors with a given canonical type. There can be at |
| 7989 | /// most one such non-template constructor, and any number of templated |
| 7990 | /// constructors. |
| 7991 | struct InheritingConstructorsForType { |
| 7992 | InheritingConstructor NonTemplate; |
Robert Wilhelm | e7205c0 | 2013-08-10 12:33:24 +0000 | [diff] [blame] | 7993 | SmallVector<std::pair<TemplateParameterList *, InheritingConstructor>, 4> |
| 7994 | Templates; |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 7995 | |
| 7996 | InheritingConstructor &getEntry(Sema &S, const CXXConstructorDecl *Ctor) { |
| 7997 | if (FunctionTemplateDecl *FTD = Ctor->getDescribedFunctionTemplate()) { |
| 7998 | TemplateParameterList *ParamList = FTD->getTemplateParameters(); |
| 7999 | for (unsigned I = 0, N = Templates.size(); I != N; ++I) |
| 8000 | if (S.TemplateParameterListsAreEqual(ParamList, Templates[I].first, |
| 8001 | false, S.TPL_TemplateMatch)) |
| 8002 | return Templates[I].second; |
| 8003 | Templates.push_back(std::make_pair(ParamList, InheritingConstructor())); |
| 8004 | return Templates.back().second; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8005 | } |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8006 | |
| 8007 | return NonTemplate; |
| 8008 | } |
| 8009 | }; |
| 8010 | |
| 8011 | /// Get or create the inheriting constructor record for a constructor. |
| 8012 | InheritingConstructor &getEntry(const CXXConstructorDecl *Ctor, |
| 8013 | QualType CtorType) { |
| 8014 | return Map[CtorType.getCanonicalType()->castAs<FunctionProtoType>()] |
| 8015 | .getEntry(SemaRef, Ctor); |
| 8016 | } |
| 8017 | |
| 8018 | typedef void (InheritingConstructorInfo::*VisitFn)(const CXXConstructorDecl*); |
| 8019 | |
| 8020 | /// Process all constructors for a class. |
| 8021 | void visitAll(const CXXRecordDecl *RD, VisitFn Callback) { |
| 8022 | for (CXXRecordDecl::ctor_iterator CtorIt = RD->ctor_begin(), |
| 8023 | CtorE = RD->ctor_end(); |
| 8024 | CtorIt != CtorE; ++CtorIt) |
| 8025 | (this->*Callback)(*CtorIt); |
| 8026 | for (CXXRecordDecl::specific_decl_iterator<FunctionTemplateDecl> |
| 8027 | I(RD->decls_begin()), E(RD->decls_end()); |
| 8028 | I != E; ++I) { |
| 8029 | const FunctionDecl *FD = (*I)->getTemplatedDecl(); |
| 8030 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) |
| 8031 | (this->*Callback)(CD); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8032 | } |
| 8033 | } |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8034 | |
| 8035 | /// Note that a constructor (or constructor template) was declared in Derived. |
| 8036 | void noteDeclaredInDerived(const CXXConstructorDecl *Ctor) { |
| 8037 | getEntry(Ctor, Ctor->getType()).DeclaredInDerived = true; |
| 8038 | } |
| 8039 | |
| 8040 | /// Inherit a single constructor. |
| 8041 | void inherit(const CXXConstructorDecl *Ctor) { |
| 8042 | const FunctionProtoType *CtorType = |
| 8043 | Ctor->getType()->castAs<FunctionProtoType>(); |
| 8044 | ArrayRef<QualType> ArgTypes(CtorType->getArgTypes()); |
| 8045 | FunctionProtoType::ExtProtoInfo EPI = CtorType->getExtProtoInfo(); |
| 8046 | |
| 8047 | SourceLocation UsingLoc = getUsingLoc(Ctor->getParent()); |
| 8048 | |
| 8049 | // Core issue (no number yet): the ellipsis is always discarded. |
| 8050 | if (EPI.Variadic) { |
| 8051 | SemaRef.Diag(UsingLoc, diag::warn_using_decl_constructor_ellipsis); |
| 8052 | SemaRef.Diag(Ctor->getLocation(), |
| 8053 | diag::note_using_decl_constructor_ellipsis); |
| 8054 | EPI.Variadic = false; |
| 8055 | } |
| 8056 | |
| 8057 | // Declare a constructor for each number of parameters. |
| 8058 | // |
| 8059 | // C++11 [class.inhctor]p1: |
| 8060 | // The candidate set of inherited constructors from the class X named in |
| 8061 | // the using-declaration consists of [... modulo defects ...] for each |
| 8062 | // constructor or constructor template of X, the set of constructors or |
| 8063 | // constructor templates that results from omitting any ellipsis parameter |
| 8064 | // specification and successively omitting parameters with a default |
| 8065 | // argument from the end of the parameter-type-list |
Richard Smith | 987c030 | 2013-04-17 19:00:52 +0000 | [diff] [blame] | 8066 | unsigned MinParams = minParamsToInherit(Ctor); |
| 8067 | unsigned Params = Ctor->getNumParams(); |
| 8068 | if (Params >= MinParams) { |
| 8069 | do |
| 8070 | declareCtor(UsingLoc, Ctor, |
| 8071 | SemaRef.Context.getFunctionType( |
| 8072 | Ctor->getResultType(), ArgTypes.slice(0, Params), EPI)); |
| 8073 | while (Params > MinParams && |
| 8074 | Ctor->getParamDecl(--Params)->hasDefaultArg()); |
| 8075 | } |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8076 | } |
| 8077 | |
| 8078 | /// Find the using-declaration which specified that we should inherit the |
| 8079 | /// constructors of \p Base. |
| 8080 | SourceLocation getUsingLoc(const CXXRecordDecl *Base) { |
| 8081 | // No fancy lookup required; just look for the base constructor name |
| 8082 | // directly within the derived class. |
| 8083 | ASTContext &Context = SemaRef.Context; |
| 8084 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
| 8085 | Context.getCanonicalType(Context.getRecordType(Base))); |
| 8086 | DeclContext::lookup_const_result Decls = Derived->lookup(Name); |
| 8087 | return Decls.empty() ? Derived->getLocation() : Decls[0]->getLocation(); |
| 8088 | } |
| 8089 | |
| 8090 | unsigned minParamsToInherit(const CXXConstructorDecl *Ctor) { |
| 8091 | // C++11 [class.inhctor]p3: |
| 8092 | // [F]or each constructor template in the candidate set of inherited |
| 8093 | // constructors, a constructor template is implicitly declared |
| 8094 | if (Ctor->getDescribedFunctionTemplate()) |
| 8095 | return 0; |
| 8096 | |
| 8097 | // For each non-template constructor in the candidate set of inherited |
| 8098 | // constructors other than a constructor having no parameters or a |
| 8099 | // copy/move constructor having a single parameter, a constructor is |
| 8100 | // implicitly declared [...] |
| 8101 | if (Ctor->getNumParams() == 0) |
| 8102 | return 1; |
| 8103 | if (Ctor->isCopyOrMoveConstructor()) |
| 8104 | return 2; |
| 8105 | |
| 8106 | // Per discussion on core reflector, never inherit a constructor which |
| 8107 | // would become a default, copy, or move constructor of Derived either. |
| 8108 | const ParmVarDecl *PD = Ctor->getParamDecl(0); |
| 8109 | const ReferenceType *RT = PD->getType()->getAs<ReferenceType>(); |
| 8110 | return (RT && RT->getPointeeCXXRecordDecl() == Derived) ? 2 : 1; |
| 8111 | } |
| 8112 | |
| 8113 | /// Declare a single inheriting constructor, inheriting the specified |
| 8114 | /// constructor, with the given type. |
| 8115 | void declareCtor(SourceLocation UsingLoc, const CXXConstructorDecl *BaseCtor, |
| 8116 | QualType DerivedType) { |
| 8117 | InheritingConstructor &Entry = getEntry(BaseCtor, DerivedType); |
| 8118 | |
| 8119 | // C++11 [class.inhctor]p3: |
| 8120 | // ... a constructor is implicitly declared with the same constructor |
| 8121 | // characteristics unless there is a user-declared constructor with |
| 8122 | // the same signature in the class where the using-declaration appears |
| 8123 | if (Entry.DeclaredInDerived) |
| 8124 | return; |
| 8125 | |
| 8126 | // C++11 [class.inhctor]p7: |
| 8127 | // If two using-declarations declare inheriting constructors with the |
| 8128 | // same signature, the program is ill-formed |
| 8129 | if (Entry.DerivedCtor) { |
| 8130 | if (BaseCtor->getParent() != Entry.BaseCtor->getParent()) { |
| 8131 | // Only diagnose this once per constructor. |
| 8132 | if (Entry.DerivedCtor->isInvalidDecl()) |
| 8133 | return; |
| 8134 | Entry.DerivedCtor->setInvalidDecl(); |
| 8135 | |
| 8136 | SemaRef.Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 8137 | SemaRef.Diag(BaseCtor->getLocation(), |
| 8138 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 8139 | SemaRef.Diag(Entry.BaseCtor->getLocation(), |
| 8140 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 8141 | SemaRef.Diag(Entry.DerivedCtor->getLocation(), |
| 8142 | diag::note_using_decl_constructor_conflict_previous_using); |
| 8143 | } else { |
| 8144 | // Core issue (no number): if the same inheriting constructor is |
| 8145 | // produced by multiple base class constructors from the same base |
| 8146 | // class, the inheriting constructor is defined as deleted. |
| 8147 | SemaRef.SetDeclDeleted(Entry.DerivedCtor, UsingLoc); |
| 8148 | } |
| 8149 | |
| 8150 | return; |
| 8151 | } |
| 8152 | |
| 8153 | ASTContext &Context = SemaRef.Context; |
| 8154 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
| 8155 | Context.getCanonicalType(Context.getRecordType(Derived))); |
| 8156 | DeclarationNameInfo NameInfo(Name, UsingLoc); |
| 8157 | |
| 8158 | TemplateParameterList *TemplateParams = 0; |
| 8159 | if (const FunctionTemplateDecl *FTD = |
| 8160 | BaseCtor->getDescribedFunctionTemplate()) { |
| 8161 | TemplateParams = FTD->getTemplateParameters(); |
| 8162 | // We're reusing template parameters from a different DeclContext. This |
| 8163 | // is questionable at best, but works out because the template depth in |
| 8164 | // both places is guaranteed to be 0. |
| 8165 | // FIXME: Rebuild the template parameters in the new context, and |
| 8166 | // transform the function type to refer to them. |
| 8167 | } |
| 8168 | |
| 8169 | // Build type source info pointing at the using-declaration. This is |
| 8170 | // required by template instantiation. |
| 8171 | TypeSourceInfo *TInfo = |
| 8172 | Context.getTrivialTypeSourceInfo(DerivedType, UsingLoc); |
| 8173 | FunctionProtoTypeLoc ProtoLoc = |
| 8174 | TInfo->getTypeLoc().IgnoreParens().castAs<FunctionProtoTypeLoc>(); |
| 8175 | |
| 8176 | CXXConstructorDecl *DerivedCtor = CXXConstructorDecl::Create( |
| 8177 | Context, Derived, UsingLoc, NameInfo, DerivedType, |
| 8178 | TInfo, BaseCtor->isExplicit(), /*Inline=*/true, |
| 8179 | /*ImplicitlyDeclared=*/true, /*Constexpr=*/BaseCtor->isConstexpr()); |
| 8180 | |
| 8181 | // Build an unevaluated exception specification for this constructor. |
| 8182 | const FunctionProtoType *FPT = DerivedType->castAs<FunctionProtoType>(); |
| 8183 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8184 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8185 | EPI.ExceptionSpecDecl = DerivedCtor; |
| 8186 | DerivedCtor->setType(Context.getFunctionType(FPT->getResultType(), |
| 8187 | FPT->getArgTypes(), EPI)); |
| 8188 | |
| 8189 | // Build the parameter declarations. |
| 8190 | SmallVector<ParmVarDecl *, 16> ParamDecls; |
| 8191 | for (unsigned I = 0, N = FPT->getNumArgs(); I != N; ++I) { |
| 8192 | TypeSourceInfo *TInfo = |
| 8193 | Context.getTrivialTypeSourceInfo(FPT->getArgType(I), UsingLoc); |
| 8194 | ParmVarDecl *PD = ParmVarDecl::Create( |
| 8195 | Context, DerivedCtor, UsingLoc, UsingLoc, /*IdentifierInfo=*/0, |
| 8196 | FPT->getArgType(I), TInfo, SC_None, /*DefaultArg=*/0); |
| 8197 | PD->setScopeInfo(0, I); |
| 8198 | PD->setImplicit(); |
| 8199 | ParamDecls.push_back(PD); |
| 8200 | ProtoLoc.setArg(I, PD); |
| 8201 | } |
| 8202 | |
| 8203 | // Set up the new constructor. |
| 8204 | DerivedCtor->setAccess(BaseCtor->getAccess()); |
| 8205 | DerivedCtor->setParams(ParamDecls); |
| 8206 | DerivedCtor->setInheritedConstructor(BaseCtor); |
| 8207 | if (BaseCtor->isDeleted()) |
| 8208 | SemaRef.SetDeclDeleted(DerivedCtor, UsingLoc); |
| 8209 | |
| 8210 | // If this is a constructor template, build the template declaration. |
| 8211 | if (TemplateParams) { |
| 8212 | FunctionTemplateDecl *DerivedTemplate = |
| 8213 | FunctionTemplateDecl::Create(SemaRef.Context, Derived, UsingLoc, Name, |
| 8214 | TemplateParams, DerivedCtor); |
| 8215 | DerivedTemplate->setAccess(BaseCtor->getAccess()); |
| 8216 | DerivedCtor->setDescribedFunctionTemplate(DerivedTemplate); |
| 8217 | Derived->addDecl(DerivedTemplate); |
| 8218 | } else { |
| 8219 | Derived->addDecl(DerivedCtor); |
| 8220 | } |
| 8221 | |
| 8222 | Entry.BaseCtor = BaseCtor; |
| 8223 | Entry.DerivedCtor = DerivedCtor; |
| 8224 | } |
| 8225 | |
| 8226 | Sema &SemaRef; |
| 8227 | CXXRecordDecl *Derived; |
| 8228 | typedef llvm::DenseMap<const Type *, InheritingConstructorsForType> MapType; |
| 8229 | MapType Map; |
| 8230 | }; |
| 8231 | } |
| 8232 | |
| 8233 | void Sema::DeclareInheritingConstructors(CXXRecordDecl *ClassDecl) { |
| 8234 | // Defer declaring the inheriting constructors until the class is |
| 8235 | // instantiated. |
| 8236 | if (ClassDecl->isDependentContext()) |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8237 | return; |
| 8238 | |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8239 | // Find base classes from which we might inherit constructors. |
| 8240 | SmallVector<CXXRecordDecl*, 4> InheritedBases; |
| 8241 | for (CXXRecordDecl::base_class_iterator BaseIt = ClassDecl->bases_begin(), |
| 8242 | BaseE = ClassDecl->bases_end(); |
| 8243 | BaseIt != BaseE; ++BaseIt) |
| 8244 | if (BaseIt->getInheritConstructors()) |
| 8245 | InheritedBases.push_back(BaseIt->getType()->getAsCXXRecordDecl()); |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8246 | |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8247 | // Go no further if we're not inheriting any constructors. |
| 8248 | if (InheritedBases.empty()) |
| 8249 | return; |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8250 | |
Richard Smith | 4841ca5 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 8251 | // Declare the inherited constructors. |
| 8252 | InheritingConstructorInfo ICI(*this, ClassDecl); |
| 8253 | for (unsigned I = 0, N = InheritedBases.size(); I != N; ++I) |
| 8254 | ICI.inheritAll(InheritedBases[I]); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 8255 | } |
| 8256 | |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 8257 | void Sema::DefineInheritingConstructor(SourceLocation CurrentLocation, |
| 8258 | CXXConstructorDecl *Constructor) { |
| 8259 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 8260 | assert(Constructor->getInheritedConstructor() && |
| 8261 | !Constructor->doesThisDeclarationHaveABody() && |
| 8262 | !Constructor->isDeleted()); |
| 8263 | |
| 8264 | SynthesizedFunctionScope Scope(*this, Constructor); |
| 8265 | DiagnosticErrorTrap Trap(Diags); |
| 8266 | if (SetCtorInitializers(Constructor, /*AnyErrors=*/false) || |
| 8267 | Trap.hasErrorOccurred()) { |
| 8268 | Diag(CurrentLocation, diag::note_inhctor_synthesized_at) |
| 8269 | << Context.getTagDeclType(ClassDecl); |
| 8270 | Constructor->setInvalidDecl(); |
| 8271 | return; |
| 8272 | } |
| 8273 | |
| 8274 | SourceLocation Loc = Constructor->getLocation(); |
| 8275 | Constructor->setBody(new (Context) CompoundStmt(Loc)); |
| 8276 | |
| 8277 | Constructor->setUsed(); |
| 8278 | MarkVTableUsed(CurrentLocation, ClassDecl); |
| 8279 | |
| 8280 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8281 | L->CompletedImplicitDefinition(Constructor); |
| 8282 | } |
| 8283 | } |
| 8284 | |
| 8285 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8286 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8287 | Sema::ComputeDefaultedDtorExceptionSpec(CXXMethodDecl *MD) { |
| 8288 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 8289 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8290 | // C++ [except.spec]p14: |
| 8291 | // An implicitly declared special member function (Clause 12) shall have |
| 8292 | // an exception-specification. |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8293 | ImplicitExceptionSpecification ExceptSpec(*this); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 8294 | if (ClassDecl->isInvalidDecl()) |
| 8295 | return ExceptSpec; |
| 8296 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8297 | // Direct base-class destructors. |
| 8298 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 8299 | BEnd = ClassDecl->bases_end(); |
| 8300 | B != BEnd; ++B) { |
| 8301 | if (B->isVirtual()) // Handled below. |
| 8302 | continue; |
| 8303 | |
| 8304 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8305 | ExceptSpec.CalledDecl(B->getLocStart(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8306 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8307 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8308 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8309 | // Virtual base-class destructors. |
| 8310 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 8311 | BEnd = ClassDecl->vbases_end(); |
| 8312 | B != BEnd; ++B) { |
| 8313 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8314 | ExceptSpec.CalledDecl(B->getLocStart(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8315 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8316 | } |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8317 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8318 | // Field destructors. |
| 8319 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 8320 | FEnd = ClassDecl->field_end(); |
| 8321 | F != FEnd; ++F) { |
| 8322 | if (const RecordType *RecordTy |
| 8323 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8324 | ExceptSpec.CalledDecl(F->getLocation(), |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8325 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8326 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8327 | |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8328 | return ExceptSpec; |
| 8329 | } |
| 8330 | |
| 8331 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
| 8332 | // C++ [class.dtor]p2: |
| 8333 | // If a class has no user-declared destructor, a destructor is |
| 8334 | // declared implicitly. An implicitly-declared destructor is an |
| 8335 | // inline public member of its class. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 8336 | assert(ClassDecl->needsImplicitDestructor()); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8337 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8338 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXDestructor); |
| 8339 | if (DSM.isAlreadyBeingDeclared()) |
| 8340 | return 0; |
| 8341 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8342 | // Create the actual destructor declaration. |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8343 | CanQualType ClassType |
| 8344 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8345 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8346 | DeclarationName Name |
| 8347 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8348 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8349 | CXXDestructorDecl *Destructor |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8350 | = CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
| 8351 | QualType(), 0, /*isInline=*/true, |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8352 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8353 | Destructor->setAccess(AS_public); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8354 | Destructor->setDefaulted(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8355 | Destructor->setImplicit(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8356 | |
| 8357 | // Build an exception specification pointing back at this destructor. |
| 8358 | FunctionProtoType::ExtProtoInfo EPI; |
| 8359 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8360 | EPI.ExceptionSpecDecl = Destructor; |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 8361 | Destructor->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8362 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8363 | AddOverriddenMethods(ClassDecl, Destructor); |
| 8364 | |
| 8365 | // We don't need to use SpecialMemberIsTrivial here; triviality for |
| 8366 | // destructors is easy to compute. |
| 8367 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
| 8368 | |
| 8369 | if (ShouldDeleteSpecialMember(Destructor, CXXDestructor)) |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 8370 | SetDeclDeleted(Destructor, ClassLoc); |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8371 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8372 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8373 | ++ASTContext::NumImplicitDestructorsDeclared; |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8374 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8375 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 8376 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 8377 | PushOnScopeChains(Destructor, S, false); |
| 8378 | ClassDecl->addDecl(Destructor); |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 8379 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 8380 | return Destructor; |
| 8381 | } |
| 8382 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8383 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 8384 | CXXDestructorDecl *Destructor) { |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 8385 | assert((Destructor->isDefaulted() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8386 | !Destructor->doesThisDeclarationHaveABody() && |
| 8387 | !Destructor->isDeleted()) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8388 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 8389 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8390 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8391 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8392 | if (Destructor->isInvalidDecl()) |
| 8393 | return; |
| 8394 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8395 | SynthesizedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 8396 | |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8397 | DiagnosticErrorTrap Trap(Diags); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 8398 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 8399 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8400 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 8401 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 8402 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 8403 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 8404 | |
| 8405 | Destructor->setInvalidDecl(); |
| 8406 | return; |
| 8407 | } |
| 8408 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 8409 | SourceLocation Loc = Destructor->getLocation(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 8410 | Destructor->setBody(new (Context) CompoundStmt(Loc)); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8411 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8412 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 8413 | |
| 8414 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 8415 | L->CompletedImplicitDefinition(Destructor); |
| 8416 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8417 | } |
| 8418 | |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 8419 | /// \brief Perform any semantic analysis which needs to be delayed until all |
| 8420 | /// pending class member declarations have been parsed. |
| 8421 | void Sema::ActOnFinishCXXMemberDecls() { |
Douglas Gregor | 1031884 | 2013-02-01 04:49:10 +0000 | [diff] [blame] | 8422 | // If the context is an invalid C++ class, just suppress these checks. |
| 8423 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 8424 | if (Record->isInvalidDecl()) { |
| 8425 | DelayedDestructorExceptionSpecChecks.clear(); |
| 8426 | return; |
| 8427 | } |
| 8428 | } |
| 8429 | |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 8430 | // Perform any deferred checking of exception specifications for virtual |
| 8431 | // destructors. |
| 8432 | for (unsigned i = 0, e = DelayedDestructorExceptionSpecChecks.size(); |
| 8433 | i != e; ++i) { |
| 8434 | const CXXDestructorDecl *Dtor = |
| 8435 | DelayedDestructorExceptionSpecChecks[i].first; |
| 8436 | assert(!Dtor->getParent()->isDependentType() && |
| 8437 | "Should not ever add destructors of templates into the list."); |
| 8438 | CheckOverridingFunctionExceptionSpec(Dtor, |
| 8439 | DelayedDestructorExceptionSpecChecks[i].second); |
| 8440 | } |
| 8441 | DelayedDestructorExceptionSpecChecks.clear(); |
| 8442 | } |
| 8443 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8444 | void Sema::AdjustDestructorExceptionSpec(CXXRecordDecl *ClassDecl, |
| 8445 | CXXDestructorDecl *Destructor) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8446 | assert(getLangOpts().CPlusPlus11 && |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8447 | "adjusting dtor exception specs was introduced in c++11"); |
| 8448 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8449 | // C++11 [class.dtor]p3: |
| 8450 | // A declaration of a destructor that does not have an exception- |
| 8451 | // specification is implicitly considered to have the same exception- |
| 8452 | // specification as an implicit declaration. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8453 | const FunctionProtoType *DtorType = Destructor->getType()-> |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8454 | getAs<FunctionProtoType>(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8455 | if (DtorType->hasExceptionSpec()) |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8456 | return; |
| 8457 | |
Chandler Carruth | 3f224b2 | 2011-09-20 04:55:26 +0000 | [diff] [blame] | 8458 | // Replace the destructor's type, building off the existing one. Fortunately, |
| 8459 | // the only thing of interest in the destructor type is its extended info. |
| 8460 | // The return and arguments are fixed. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8461 | FunctionProtoType::ExtProtoInfo EPI = DtorType->getExtProtoInfo(); |
| 8462 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8463 | EPI.ExceptionSpecDecl = Destructor; |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 8464 | Destructor->setType(Context.getFunctionType(Context.VoidTy, None, EPI)); |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 8465 | |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8466 | // FIXME: If the destructor has a body that could throw, and the newly created |
| 8467 | // spec doesn't allow exceptions, we should emit a warning, because this |
| 8468 | // change in behavior can break conforming C++03 programs at runtime. |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8469 | // However, we don't have a body or an exception specification yet, so it |
| 8470 | // needs to be done somewhere else. |
Sebastian Redl | 0ee3391 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 8471 | } |
| 8472 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8473 | /// When generating a defaulted copy or move assignment operator, if a field |
| 8474 | /// should be copied with __builtin_memcpy rather than via explicit assignments, |
| 8475 | /// do so. This optimization only applies for arrays of scalars, and for arrays |
| 8476 | /// of class type where the selected copy/move-assignment operator is trivial. |
| 8477 | static StmtResult |
| 8478 | buildMemcpyForAssignmentOp(Sema &S, SourceLocation Loc, QualType T, |
| 8479 | Expr *To, Expr *From) { |
| 8480 | // Compute the size of the memory buffer to be copied. |
| 8481 | QualType SizeType = S.Context.getSizeType(); |
| 8482 | llvm::APInt Size(S.Context.getTypeSize(SizeType), |
| 8483 | S.Context.getTypeSizeInChars(T).getQuantity()); |
| 8484 | |
| 8485 | // Take the address of the field references for "from" and "to". We |
| 8486 | // directly construct UnaryOperators here because semantic analysis |
| 8487 | // does not permit us to take the address of an xvalue. |
| 8488 | From = new (S.Context) UnaryOperator(From, UO_AddrOf, |
| 8489 | S.Context.getPointerType(From->getType()), |
| 8490 | VK_RValue, OK_Ordinary, Loc); |
| 8491 | To = new (S.Context) UnaryOperator(To, UO_AddrOf, |
| 8492 | S.Context.getPointerType(To->getType()), |
| 8493 | VK_RValue, OK_Ordinary, Loc); |
| 8494 | |
| 8495 | const Type *E = T->getBaseElementTypeUnsafe(); |
| 8496 | bool NeedsCollectableMemCpy = |
| 8497 | E->isRecordType() && E->getAs<RecordType>()->getDecl()->hasObjectMember(); |
| 8498 | |
| 8499 | // Create a reference to the __builtin_objc_memmove_collectable function |
| 8500 | StringRef MemCpyName = NeedsCollectableMemCpy ? |
| 8501 | "__builtin_objc_memmove_collectable" : |
| 8502 | "__builtin_memcpy"; |
| 8503 | LookupResult R(S, &S.Context.Idents.get(MemCpyName), Loc, |
| 8504 | Sema::LookupOrdinaryName); |
| 8505 | S.LookupName(R, S.TUScope, true); |
| 8506 | |
| 8507 | FunctionDecl *MemCpy = R.getAsSingle<FunctionDecl>(); |
| 8508 | if (!MemCpy) |
| 8509 | // Something went horribly wrong earlier, and we will have complained |
| 8510 | // about it. |
| 8511 | return StmtError(); |
| 8512 | |
| 8513 | ExprResult MemCpyRef = S.BuildDeclRefExpr(MemCpy, S.Context.BuiltinFnTy, |
| 8514 | VK_RValue, Loc, 0); |
| 8515 | assert(MemCpyRef.isUsable() && "Builtin reference cannot fail"); |
| 8516 | |
| 8517 | Expr *CallArgs[] = { |
| 8518 | To, From, IntegerLiteral::Create(S.Context, Size, SizeType, Loc) |
| 8519 | }; |
| 8520 | ExprResult Call = S.ActOnCallExpr(/*Scope=*/0, MemCpyRef.take(), |
| 8521 | Loc, CallArgs, Loc); |
| 8522 | |
| 8523 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 8524 | return S.Owned(Call.takeAs<Stmt>()); |
| 8525 | } |
| 8526 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8527 | /// \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] | 8528 | /// \c To. |
| 8529 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8530 | /// This routine is used to copy/move the members of a class with an |
| 8531 | /// implicitly-declared copy/move assignment operator. When the entities being |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8532 | /// copied are arrays, this routine builds for loops to copy them. |
| 8533 | /// |
| 8534 | /// \param S The Sema object used for type-checking. |
| 8535 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8536 | /// \param Loc The location where the implicit copy/move is being generated. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8537 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8538 | /// \param T The type of the expressions being copied/moved. Both expressions |
| 8539 | /// must have this type. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8540 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8541 | /// \param To The expression we are copying/moving to. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8542 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8543 | /// \param From The expression we are copying/moving from. |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8544 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8545 | /// \param CopyingBaseSubobject Whether we're copying/moving a base subobject. |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 8546 | /// Otherwise, it's a non-static member subobject. |
| 8547 | /// |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8548 | /// \param Copying Whether we're copying or moving. |
| 8549 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8550 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 8551 | /// |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8552 | /// \returns A statement or a loop that copies the expressions, or StmtResult(0) |
| 8553 | /// if a memcpy should be used instead. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8554 | static StmtResult |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8555 | buildSingleCopyAssignRecursively(Sema &S, SourceLocation Loc, QualType T, |
| 8556 | Expr *To, Expr *From, |
| 8557 | bool CopyingBaseSubobject, bool Copying, |
| 8558 | unsigned Depth = 0) { |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8559 | // C++11 [class.copy]p28: |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8560 | // Each subobject is assigned in the manner appropriate to its type: |
| 8561 | // |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8562 | // - if the subobject is of class type, as if by a call to operator= with |
| 8563 | // the subobject as the object expression and the corresponding |
| 8564 | // subobject of x as a single function argument (as if by explicit |
| 8565 | // qualification; that is, ignoring any possible virtual overriding |
| 8566 | // functions in more derived classes); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8567 | // |
| 8568 | // C++03 [class.copy]p13: |
| 8569 | // - if the subobject is of class type, the copy assignment operator for |
| 8570 | // the class is used (as if by explicit qualification; that is, |
| 8571 | // ignoring any possible virtual overriding functions in more derived |
| 8572 | // classes); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8573 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 8574 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8575 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8576 | // Look for operator=. |
| 8577 | DeclarationName Name |
| 8578 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 8579 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 8580 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8581 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8582 | // Prior to C++11, filter out any result that isn't a copy/move-assignment |
| 8583 | // operator. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8584 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8585 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 8586 | while (F.hasNext()) { |
| 8587 | NamedDecl *D = F.next(); |
| 8588 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 8589 | if (Method->isCopyAssignmentOperator() || |
| 8590 | (!Copying && Method->isMoveAssignmentOperator())) |
| 8591 | continue; |
| 8592 | |
| 8593 | F.erase(); |
| 8594 | } |
| 8595 | F.done(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 8596 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8597 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 8598 | // Suppress the protected check (C++ [class.protected]) for each of the |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8599 | // assignment operators we found. This strange dance is required when |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 8600 | // we're assigning via a base classes's copy-assignment operator. To |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8601 | // ensure that we're getting the right base class subobject (without |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 8602 | // ambiguities), we need to cast "this" to that subobject type; to |
| 8603 | // ensure that we don't go through the virtual call mechanism, we need |
| 8604 | // to qualify the operator= name with the base class (see below). However, |
| 8605 | // this means that if the base class has a protected copy assignment |
| 8606 | // operator, the protected member access check will fail. So, we |
| 8607 | // rewrite "protected" access to "public" access in this case, since we |
| 8608 | // know by construction that we're calling from a derived class. |
| 8609 | if (CopyingBaseSubobject) { |
| 8610 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 8611 | L != LEnd; ++L) { |
| 8612 | if (L.getAccess() == AS_protected) |
| 8613 | L.setAccess(AS_public); |
| 8614 | } |
| 8615 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8616 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8617 | // Create the nested-name-specifier that will be used to qualify the |
| 8618 | // reference to operator=; this is required to suppress the virtual |
| 8619 | // call mechanism. |
| 8620 | CXXScopeSpec SS; |
Manuel Klimek | 5b6a3dd | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 8621 | const Type *CanonicalT = S.Context.getCanonicalType(T.getTypePtr()); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8622 | SS.MakeTrivial(S.Context, |
| 8623 | NestedNameSpecifier::Create(S.Context, 0, false, |
Manuel Klimek | 5b6a3dd | 2012-02-06 21:51:39 +0000 | [diff] [blame] | 8624 | CanonicalT), |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 8625 | Loc); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8626 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8627 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8628 | ExprResult OpEqualRef |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8629 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 8630 | /*TemplateKWLoc=*/SourceLocation(), |
| 8631 | /*FirstQualifierInScope=*/0, |
| 8632 | OpLookup, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8633 | /*TemplateArgs=*/0, |
| 8634 | /*SuppressQualifierCheck=*/true); |
| 8635 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8636 | return StmtError(); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8637 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8638 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8639 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8640 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 8641 | OpEqualRef.takeAs<Expr>(), |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 8642 | Loc, From, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8643 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8644 | return StmtError(); |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8645 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8646 | // If we built a call to a trivial 'operator=' while copying an array, |
| 8647 | // bail out. We'll replace the whole shebang with a memcpy. |
| 8648 | CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(Call.get()); |
| 8649 | if (CE && CE->getMethodDecl()->isTrivial() && Depth) |
| 8650 | return StmtResult((Stmt*)0); |
| 8651 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8652 | // Convert to an expression-statement, and clean up any produced |
| 8653 | // temporaries. |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8654 | return S.ActOnExprStmt(Call); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8655 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 8656 | |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8657 | // - if the subobject is of scalar type, the built-in assignment |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8658 | // operator is used. |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8659 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8660 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8661 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8662 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 8663 | return StmtError(); |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8664 | return S.ActOnExprStmt(Assignment); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8665 | } |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8666 | |
| 8667 | // - if the subobject is an array, each element is assigned, in the |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8668 | // manner appropriate to the element type; |
Richard Smith | 044c8aa | 2012-11-13 00:54:12 +0000 | [diff] [blame] | 8669 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8670 | // Construct a loop over the array bounds, e.g., |
| 8671 | // |
| 8672 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 8673 | // |
| 8674 | // that will copy each of the array elements. |
| 8675 | QualType SizeType = S.Context.getSizeType(); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8676 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8677 | // Create the iteration variable. |
| 8678 | IdentifierInfo *IterationVarName = 0; |
| 8679 | { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 8680 | SmallString<8> Str; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8681 | llvm::raw_svector_ostream OS(Str); |
| 8682 | OS << "__i" << Depth; |
| 8683 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 8684 | } |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8685 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8686 | IterationVarName, SizeType, |
| 8687 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 8688 | SC_None); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8689 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8690 | // Initialize the iteration variable to zero. |
| 8691 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 8692 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8693 | |
| 8694 | // Create a reference to the iteration variable; we'll use this several |
| 8695 | // times throughout. |
| 8696 | Expr *IterationVarRef |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 8697 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8698 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 8699 | Expr *IterationVarRefRVal = S.DefaultLvalueConversion(IterationVarRef).take(); |
| 8700 | assert(IterationVarRefRVal && "Conversion of invented variable cannot fail!"); |
| 8701 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8702 | // Create the DeclStmt that holds the iteration variable. |
| 8703 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8704 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8705 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8706 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 8707 | IterationVarRefRVal, |
| 8708 | Loc)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8709 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
Eli Friedman | 8c38206 | 2012-01-23 02:35:22 +0000 | [diff] [blame] | 8710 | IterationVarRefRVal, |
| 8711 | Loc)); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 8712 | if (!Copying) // Cast to rvalue |
| 8713 | From = CastForMoving(S, From); |
| 8714 | |
| 8715 | // Build the copy/move for an individual element of the array. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8716 | StmtResult Copy = |
| 8717 | buildSingleCopyAssignRecursively(S, Loc, ArrayTy->getElementType(), |
| 8718 | To, From, CopyingBaseSubobject, |
| 8719 | Copying, Depth + 1); |
| 8720 | // Bail out if copying fails or if we determined that we should use memcpy. |
| 8721 | if (Copy.isInvalid() || !Copy.get()) |
| 8722 | return Copy; |
| 8723 | |
| 8724 | // Create the comparison against the array bound. |
| 8725 | llvm::APInt Upper |
| 8726 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
| 8727 | Expr *Comparison |
| 8728 | = new (S.Context) BinaryOperator(IterationVarRefRVal, |
| 8729 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 8730 | BO_NE, S.Context.BoolTy, |
| 8731 | VK_RValue, OK_Ordinary, Loc, false); |
| 8732 | |
| 8733 | // Create the pre-increment of the iteration variable. |
| 8734 | Expr *Increment |
| 8735 | = new (S.Context) UnaryOperator(IterationVarRef, UO_PreInc, SizeType, |
| 8736 | VK_LValue, OK_Ordinary, Loc); |
| 8737 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8738 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8739 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8740 | S.MakeFullExpr(Comparison), |
Richard Smith | 4195637 | 2013-01-14 22:39:08 +0000 | [diff] [blame] | 8741 | 0, S.MakeFullDiscardedValueExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8742 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8743 | } |
| 8744 | |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 8745 | static StmtResult |
| 8746 | buildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
| 8747 | Expr *To, Expr *From, |
| 8748 | bool CopyingBaseSubobject, bool Copying) { |
| 8749 | // Maybe we should use a memcpy? |
| 8750 | if (T->isArrayType() && !T.isConstQualified() && !T.isVolatileQualified() && |
| 8751 | T.isTriviallyCopyableType(S.Context)) |
| 8752 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 8753 | |
| 8754 | StmtResult Result(buildSingleCopyAssignRecursively(S, Loc, T, To, From, |
| 8755 | CopyingBaseSubobject, |
| 8756 | Copying, 0)); |
| 8757 | |
| 8758 | // If we ended up picking a trivial assignment operator for an array of a |
| 8759 | // non-trivially-copyable class type, just emit a memcpy. |
| 8760 | if (!Result.isInvalid() && !Result.get()) |
| 8761 | return buildMemcpyForAssignmentOp(S, Loc, T, To, From); |
| 8762 | |
| 8763 | return Result; |
| 8764 | } |
| 8765 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8766 | Sema::ImplicitExceptionSpecification |
| 8767 | Sema::ComputeDefaultedCopyAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 8768 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 8769 | |
| 8770 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 8771 | if (ClassDecl->isInvalidDecl()) |
| 8772 | return ExceptSpec; |
| 8773 | |
| 8774 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
| 8775 | assert(T->getNumArgs() == 1 && "not a copy assignment op"); |
| 8776 | unsigned ArgQuals = T->getArgType(0).getNonReferenceType().getCVRQualifiers(); |
| 8777 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8778 | // C++ [except.spec]p14: |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8779 | // An implicitly declared special member function (Clause 12) shall have an |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8780 | // exception-specification. [...] |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8781 | |
| 8782 | // It is unspecified whether or not an implicit copy assignment operator |
| 8783 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 8784 | // made. As such, this exception specification is effectively unspecified. |
| 8785 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 8786 | // the side of assuming such calls to be made regardless of whether they |
| 8787 | // actually happen. |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8788 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 8789 | BaseEnd = ClassDecl->bases_end(); |
| 8790 | Base != BaseEnd; ++Base) { |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8791 | if (Base->isVirtual()) |
| 8792 | continue; |
| 8793 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 8794 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8795 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8796 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 8797 | ArgQuals, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8798 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyAssign); |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8799 | } |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8800 | |
| 8801 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 8802 | BaseEnd = ClassDecl->vbases_end(); |
| 8803 | Base != BaseEnd; ++Base) { |
| 8804 | CXXRecordDecl *BaseClassDecl |
| 8805 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 8806 | if (CXXMethodDecl *CopyAssign = LookupCopyingAssignment(BaseClassDecl, |
| 8807 | ArgQuals, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8808 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyAssign); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8809 | } |
| 8810 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8811 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 8812 | FieldEnd = ClassDecl->field_end(); |
| 8813 | Field != FieldEnd; |
| 8814 | ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 8815 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 8816 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 8817 | if (CXXMethodDecl *CopyAssign = |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 8818 | LookupCopyingAssignment(FieldClassDecl, |
| 8819 | ArgQuals | FieldType.getCVRQualifiers(), |
| 8820 | false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 8821 | ExceptSpec.CalledDecl(Field->getLocation(), CopyAssign); |
Abramo Bagnara | cdb8076 | 2011-07-11 08:52:40 +0000 | [diff] [blame] | 8822 | } |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 8823 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 8824 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8825 | return ExceptSpec; |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 8826 | } |
| 8827 | |
| 8828 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
| 8829 | // Note: The following rules are largely analoguous to the copy |
| 8830 | // constructor rules. Note that virtual bases are not taken into account |
| 8831 | // for determining the argument type of the operator. Note also that |
| 8832 | // operators taking an object instead of a reference are allowed. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 8833 | assert(ClassDecl->needsImplicitCopyAssignment()); |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 8834 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 8835 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyAssignment); |
| 8836 | if (DSM.isAlreadyBeingDeclared()) |
| 8837 | return 0; |
| 8838 | |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 8839 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 8840 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 8841 | bool Const = ClassDecl->implicitCopyAssignmentHasConstParam(); |
| 8842 | if (Const) |
Sean Hunt | 30de05c | 2011-05-14 05:23:20 +0000 | [diff] [blame] | 8843 | ArgType = ArgType.withConst(); |
| 8844 | ArgType = Context.getLValueReferenceType(ArgType); |
| 8845 | |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 8846 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 8847 | CXXCopyAssignment, |
| 8848 | Const); |
| 8849 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8850 | // An implicitly-declared copy assignment operator is an inline public |
| 8851 | // member of its class. |
| 8852 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8853 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 8854 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 8855 | CXXMethodDecl *CopyAssignment = |
| 8856 | CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
| 8857 | /*TInfo=*/ 0, /*StorageClass=*/ SC_None, |
| 8858 | /*isInline=*/ true, Constexpr, SourceLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8859 | CopyAssignment->setAccess(AS_public); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 8860 | CopyAssignment->setDefaulted(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8861 | CopyAssignment->setImplicit(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8862 | |
| 8863 | // Build an exception specification pointing back at this member. |
| 8864 | FunctionProtoType::ExtProtoInfo EPI; |
| 8865 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 8866 | EPI.ExceptionSpecDecl = CopyAssignment; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 8867 | CopyAssignment->setType(Context.getFunctionType(RetType, ArgType, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 8868 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8869 | // Add the parameter to the operator. |
| 8870 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 8871 | ClassLoc, ClassLoc, /*Id=*/0, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8872 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8873 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 8874 | CopyAssignment->setParams(FromParam); |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 8875 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8876 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 8877 | |
| 8878 | CopyAssignment->setTrivial( |
| 8879 | ClassDecl->needsOverloadResolutionForCopyAssignment() |
| 8880 | ? SpecialMemberIsTrivial(CopyAssignment, CXXCopyAssignment) |
| 8881 | : ClassDecl->hasTrivialCopyAssignment()); |
| 8882 | |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 8883 | // C++11 [class.copy]p19: |
Nico Weber | afcc96a | 2012-01-23 03:19:29 +0000 | [diff] [blame] | 8884 | // .... If the class definition does not explicitly declare a copy |
| 8885 | // assignment operator, there is no user-declared move constructor, and |
| 8886 | // there is no user-declared move assignment operator, a copy assignment |
| 8887 | // operator is implicitly declared as defaulted. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8888 | if (ShouldDeleteSpecialMember(CopyAssignment, CXXCopyAssignment)) |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 8889 | SetDeclDeleted(CopyAssignment, ClassLoc); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 8890 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 8891 | // Note that we have added this copy-assignment operator. |
| 8892 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
| 8893 | |
| 8894 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 8895 | PushOnScopeChains(CopyAssignment, S, false); |
| 8896 | ClassDecl->addDecl(CopyAssignment); |
| 8897 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 8898 | return CopyAssignment; |
| 8899 | } |
| 8900 | |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 8901 | /// Diagnose an implicit copy operation for a class which is odr-used, but |
| 8902 | /// which is deprecated because the class has a user-declared copy constructor, |
| 8903 | /// copy assignment operator, or destructor. |
| 8904 | static void diagnoseDeprecatedCopyOperation(Sema &S, CXXMethodDecl *CopyOp, |
| 8905 | SourceLocation UseLoc) { |
| 8906 | assert(CopyOp->isImplicit()); |
| 8907 | |
| 8908 | CXXRecordDecl *RD = CopyOp->getParent(); |
| 8909 | CXXMethodDecl *UserDeclaredOperation = 0; |
| 8910 | |
| 8911 | // In Microsoft mode, assignment operations don't affect constructors and |
| 8912 | // vice versa. |
| 8913 | if (RD->hasUserDeclaredDestructor()) { |
| 8914 | UserDeclaredOperation = RD->getDestructor(); |
| 8915 | } else if (!isa<CXXConstructorDecl>(CopyOp) && |
| 8916 | RD->hasUserDeclaredCopyConstructor() && |
| 8917 | !S.getLangOpts().MicrosoftMode) { |
| 8918 | // Find any user-declared copy constructor. |
| 8919 | for (CXXRecordDecl::ctor_iterator I = RD->ctor_begin(), |
| 8920 | E = RD->ctor_end(); I != E; ++I) { |
| 8921 | if (I->isCopyConstructor()) { |
| 8922 | UserDeclaredOperation = *I; |
| 8923 | break; |
| 8924 | } |
| 8925 | } |
| 8926 | assert(UserDeclaredOperation); |
| 8927 | } else if (isa<CXXConstructorDecl>(CopyOp) && |
| 8928 | RD->hasUserDeclaredCopyAssignment() && |
| 8929 | !S.getLangOpts().MicrosoftMode) { |
| 8930 | // Find any user-declared move assignment operator. |
| 8931 | for (CXXRecordDecl::method_iterator I = RD->method_begin(), |
| 8932 | E = RD->method_end(); I != E; ++I) { |
| 8933 | if (I->isCopyAssignmentOperator()) { |
| 8934 | UserDeclaredOperation = *I; |
| 8935 | break; |
| 8936 | } |
| 8937 | } |
| 8938 | assert(UserDeclaredOperation); |
| 8939 | } |
| 8940 | |
| 8941 | if (UserDeclaredOperation) { |
| 8942 | S.Diag(UserDeclaredOperation->getLocation(), |
| 8943 | diag::warn_deprecated_copy_operation) |
| 8944 | << RD << /*copy assignment*/!isa<CXXConstructorDecl>(CopyOp) |
| 8945 | << /*destructor*/isa<CXXDestructorDecl>(UserDeclaredOperation); |
| 8946 | S.Diag(UseLoc, diag::note_member_synthesized_at) |
| 8947 | << (isa<CXXConstructorDecl>(CopyOp) ? Sema::CXXCopyConstructor |
| 8948 | : Sema::CXXCopyAssignment) |
| 8949 | << RD; |
| 8950 | } |
| 8951 | } |
| 8952 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8953 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 8954 | CXXMethodDecl *CopyAssignOperator) { |
Sean Hunt | 7f41019 | 2011-05-14 05:23:24 +0000 | [diff] [blame] | 8955 | assert((CopyAssignOperator->isDefaulted() && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8956 | CopyAssignOperator->isOverloadedOperator() && |
| 8957 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 8958 | !CopyAssignOperator->doesThisDeclarationHaveABody() && |
| 8959 | !CopyAssignOperator->isDeleted()) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8960 | "DefineImplicitCopyAssignment called for wrong function"); |
| 8961 | |
| 8962 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 8963 | |
| 8964 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 8965 | CopyAssignOperator->setInvalidDecl(); |
| 8966 | return; |
| 8967 | } |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 8968 | |
| 8969 | // C++11 [class.copy]p18: |
| 8970 | // The [definition of an implicitly declared copy assignment operator] is |
| 8971 | // deprecated if the class has a user-declared copy constructor or a |
| 8972 | // user-declared destructor. |
| 8973 | if (getLangOpts().CPlusPlus11 && CopyAssignOperator->isImplicit()) |
| 8974 | diagnoseDeprecatedCopyOperation(*this, CopyAssignOperator, CurrentLocation); |
| 8975 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8976 | CopyAssignOperator->setUsed(); |
| 8977 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 8978 | SynthesizedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 8979 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8980 | |
| 8981 | // C++0x [class.copy]p30: |
| 8982 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 8983 | // for a non-union class X performs memberwise copy assignment of its |
| 8984 | // subobjects. The direct base classes of X are assigned first, in the |
| 8985 | // order of their declaration in the base-specifier-list, and then the |
| 8986 | // immediate non-static data members of X are assigned, in the order in |
| 8987 | // which they were declared in the class definition. |
| 8988 | |
| 8989 | // The statements that form the synthesized function body. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 8990 | SmallVector<Stmt*, 8> Statements; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 8991 | |
| 8992 | // The parameter for the "other" object, which we are copying from. |
| 8993 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 8994 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 8995 | QualType OtherRefType = Other->getType(); |
| 8996 | if (const LValueReferenceType *OtherRef |
| 8997 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 8998 | OtherRefType = OtherRef->getPointeeType(); |
| 8999 | OtherQuals = OtherRefType.getQualifiers(); |
| 9000 | } |
| 9001 | |
| 9002 | // Our location for everything implicitly-generated. |
| 9003 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 9004 | |
| 9005 | // Construct a reference to the "other" object. We'll be using this |
| 9006 | // throughout the generated ASTs. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9007 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9008 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 9009 | |
| 9010 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 9011 | // ASTs. |
| 9012 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 9013 | assert(This && "Reference to this cannot fail!"); |
| 9014 | |
| 9015 | // Assign base classes. |
| 9016 | bool Invalid = false; |
| 9017 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9018 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 9019 | // Form the assignment: |
| 9020 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 9021 | QualType BaseType = Base->getType().getUnqualifiedType(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 9022 | if (!BaseType->isRecordType()) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9023 | Invalid = true; |
| 9024 | continue; |
| 9025 | } |
| 9026 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 9027 | CXXCastPath BasePath; |
| 9028 | BasePath.push_back(Base); |
| 9029 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9030 | // Construct the "from" expression, which is an implicit cast to the |
| 9031 | // appropriately-qualified base type. |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 9032 | Expr *From = OtherRef; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9033 | From = ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
| 9034 | CK_UncheckedDerivedToBase, |
| 9035 | VK_LValue, &BasePath).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9036 | |
| 9037 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 9038 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9039 | |
| 9040 | // Implicitly cast "this" to the appropriately-qualified base type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9041 | To = ImpCastExprToType(To.take(), |
| 9042 | Context.getCVRQualifiedType(BaseType, |
| 9043 | CopyAssignOperator->getTypeQualifiers()), |
| 9044 | CK_UncheckedDerivedToBase, |
| 9045 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9046 | |
| 9047 | // Build the copy. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9048 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 9049 | To.get(), From, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9050 | /*CopyingBaseSubobject=*/true, |
| 9051 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9052 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9053 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9054 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 9055 | CopyAssignOperator->setInvalidDecl(); |
| 9056 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9057 | } |
| 9058 | |
| 9059 | // Success! Record the copy. |
| 9060 | Statements.push_back(Copy.takeAs<Expr>()); |
| 9061 | } |
| 9062 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9063 | // Assign non-static members. |
| 9064 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9065 | FieldEnd = ClassDecl->field_end(); |
| 9066 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 9067 | if (Field->isUnnamedBitfield()) |
| 9068 | continue; |
Eli Friedman | 8150da3 | 2013-06-07 01:48:56 +0000 | [diff] [blame] | 9069 | |
| 9070 | if (Field->isInvalidDecl()) { |
| 9071 | Invalid = true; |
| 9072 | continue; |
| 9073 | } |
| 9074 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9075 | // Check for members of reference type; we can't copy those. |
| 9076 | if (Field->getType()->isReferenceType()) { |
| 9077 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9078 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 9079 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9080 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9081 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9082 | Invalid = true; |
| 9083 | continue; |
| 9084 | } |
| 9085 | |
| 9086 | // Check for members of const-qualified, non-class type. |
| 9087 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 9088 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 9089 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9090 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 9091 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9092 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9093 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9094 | Invalid = true; |
| 9095 | continue; |
| 9096 | } |
John McCall | b77115d | 2011-06-17 00:18:42 +0000 | [diff] [blame] | 9097 | |
| 9098 | // Suppress assigning zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9099 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 9100 | continue; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9101 | |
| 9102 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 9103 | if (FieldType->isIncompleteArrayType()) { |
| 9104 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 9105 | "Incomplete array type is not valid"); |
| 9106 | continue; |
| 9107 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9108 | |
| 9109 | // Build references to the field in the object we're copying from and to. |
| 9110 | CXXScopeSpec SS; // Intentionally empty |
| 9111 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 9112 | LookupMemberName); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 9113 | MemberLookup.addDecl(*Field); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9114 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9115 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9116 | Loc, /*IsArrow=*/false, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9117 | SS, SourceLocation(), 0, |
| 9118 | MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9119 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9120 | Loc, /*IsArrow=*/true, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9121 | SS, SourceLocation(), 0, |
| 9122 | MemberLookup, 0); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9123 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 9124 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9125 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9126 | // Build the copy of this field. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9127 | StmtResult Copy = buildSingleCopyAssign(*this, Loc, FieldType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9128 | To.get(), From.get(), |
| 9129 | /*CopyingBaseSubobject=*/false, |
| 9130 | /*Copying=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9131 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 9132 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9133 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 9134 | CopyAssignOperator->setInvalidDecl(); |
| 9135 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9136 | } |
| 9137 | |
| 9138 | // Success! Record the copy. |
| 9139 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 9140 | } |
| 9141 | |
| 9142 | if (!Invalid) { |
| 9143 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9144 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9145 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9146 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9147 | if (Return.isInvalid()) |
| 9148 | Invalid = true; |
| 9149 | else { |
| 9150 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 9151 | |
| 9152 | if (Trap.hasErrorOccurred()) { |
| 9153 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9154 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 9155 | Invalid = true; |
| 9156 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9157 | } |
| 9158 | } |
| 9159 | |
| 9160 | if (Invalid) { |
| 9161 | CopyAssignOperator->setInvalidDecl(); |
| 9162 | return; |
| 9163 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9164 | |
| 9165 | StmtResult Body; |
| 9166 | { |
| 9167 | CompoundScopeRAII CompoundScope(*this); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9168 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9169 | /*isStmtExpr=*/false); |
| 9170 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 9171 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 9172 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 9173 | |
| 9174 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9175 | L->CompletedImplicitDefinition(CopyAssignOperator); |
| 9176 | } |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9177 | } |
| 9178 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9179 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9180 | Sema::ComputeDefaultedMoveAssignmentExceptionSpec(CXXMethodDecl *MD) { |
| 9181 | CXXRecordDecl *ClassDecl = MD->getParent(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9182 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9183 | ImplicitExceptionSpecification ExceptSpec(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9184 | if (ClassDecl->isInvalidDecl()) |
| 9185 | return ExceptSpec; |
| 9186 | |
| 9187 | // C++0x [except.spec]p14: |
| 9188 | // An implicitly declared special member function (Clause 12) shall have an |
| 9189 | // exception-specification. [...] |
| 9190 | |
| 9191 | // It is unspecified whether or not an implicit move assignment operator |
| 9192 | // attempts to deduplicate calls to assignment operators of virtual bases are |
| 9193 | // made. As such, this exception specification is effectively unspecified. |
| 9194 | // Based on a similar decision made for constness in C++0x, we're erring on |
| 9195 | // the side of assuming such calls to be made regardless of whether they |
| 9196 | // actually happen. |
| 9197 | // Note that a move constructor is not implicitly declared when there are |
| 9198 | // virtual bases, but it can still be user-declared and explicitly defaulted. |
| 9199 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9200 | BaseEnd = ClassDecl->bases_end(); |
| 9201 | Base != BaseEnd; ++Base) { |
| 9202 | if (Base->isVirtual()) |
| 9203 | continue; |
| 9204 | |
| 9205 | CXXRecordDecl *BaseClassDecl |
| 9206 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 9207 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9208 | 0, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9209 | ExceptSpec.CalledDecl(Base->getLocStart(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9210 | } |
| 9211 | |
| 9212 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 9213 | BaseEnd = ClassDecl->vbases_end(); |
| 9214 | Base != BaseEnd; ++Base) { |
| 9215 | CXXRecordDecl *BaseClassDecl |
| 9216 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 9217 | if (CXXMethodDecl *MoveAssign = LookupMovingAssignment(BaseClassDecl, |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9218 | 0, false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9219 | ExceptSpec.CalledDecl(Base->getLocStart(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9220 | } |
| 9221 | |
| 9222 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9223 | FieldEnd = ClassDecl->field_end(); |
| 9224 | Field != FieldEnd; |
| 9225 | ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 9226 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9227 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9228 | if (CXXMethodDecl *MoveAssign = |
| 9229 | LookupMovingAssignment(FieldClassDecl, |
| 9230 | FieldType.getCVRQualifiers(), |
| 9231 | false, 0)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9232 | ExceptSpec.CalledDecl(Field->getLocation(), MoveAssign); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9233 | } |
| 9234 | } |
| 9235 | |
| 9236 | return ExceptSpec; |
| 9237 | } |
| 9238 | |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9239 | /// Determine whether the class type has any direct or indirect virtual base |
| 9240 | /// classes which have a non-trivial move assignment operator. |
| 9241 | static bool |
| 9242 | hasVirtualBaseWithNonTrivialMoveAssignment(Sema &S, CXXRecordDecl *ClassDecl) { |
| 9243 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 9244 | BaseEnd = ClassDecl->vbases_end(); |
| 9245 | Base != BaseEnd; ++Base) { |
| 9246 | CXXRecordDecl *BaseClass = |
| 9247 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 9248 | |
| 9249 | // Try to declare the move assignment. If it would be deleted, then the |
| 9250 | // class does not have a non-trivial move assignment. |
| 9251 | if (BaseClass->needsImplicitMoveAssignment()) |
| 9252 | S.DeclareImplicitMoveAssignment(BaseClass); |
| 9253 | |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 9254 | if (BaseClass->hasNonTrivialMoveAssignment()) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9255 | return true; |
| 9256 | } |
| 9257 | |
| 9258 | return false; |
| 9259 | } |
| 9260 | |
| 9261 | /// Determine whether the given type either has a move constructor or is |
| 9262 | /// trivially copyable. |
| 9263 | static bool |
| 9264 | hasMoveOrIsTriviallyCopyable(Sema &S, QualType Type, bool IsConstructor) { |
| 9265 | Type = S.Context.getBaseElementType(Type); |
| 9266 | |
| 9267 | // FIXME: Technically, non-trivially-copyable non-class types, such as |
| 9268 | // reference types, are supposed to return false here, but that appears |
| 9269 | // to be a standard defect. |
| 9270 | CXXRecordDecl *ClassDecl = Type->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | b5e4ace | 2012-10-10 16:14:06 +0000 | [diff] [blame] | 9271 | if (!ClassDecl || !ClassDecl->getDefinition() || ClassDecl->isInvalidDecl()) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9272 | return true; |
| 9273 | |
| 9274 | if (Type.isTriviallyCopyableType(S.Context)) |
| 9275 | return true; |
| 9276 | |
| 9277 | if (IsConstructor) { |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9278 | // FIXME: Need this because otherwise hasMoveConstructor isn't guaranteed to |
| 9279 | // give the right answer. |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9280 | if (ClassDecl->needsImplicitMoveConstructor()) |
| 9281 | S.DeclareImplicitMoveConstructor(ClassDecl); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9282 | return ClassDecl->hasMoveConstructor(); |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9283 | } |
| 9284 | |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9285 | // FIXME: Need this because otherwise hasMoveAssignment isn't guaranteed to |
| 9286 | // give the right answer. |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9287 | if (ClassDecl->needsImplicitMoveAssignment()) |
| 9288 | S.DeclareImplicitMoveAssignment(ClassDecl); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9289 | return ClassDecl->hasMoveAssignment(); |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9290 | } |
| 9291 | |
| 9292 | /// Determine whether all non-static data members and direct or virtual bases |
| 9293 | /// of class \p ClassDecl have either a move operation, or are trivially |
| 9294 | /// copyable. |
| 9295 | static bool subobjectsHaveMoveOrTrivialCopy(Sema &S, CXXRecordDecl *ClassDecl, |
| 9296 | bool IsConstructor) { |
| 9297 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9298 | BaseEnd = ClassDecl->bases_end(); |
| 9299 | Base != BaseEnd; ++Base) { |
| 9300 | if (Base->isVirtual()) |
| 9301 | continue; |
| 9302 | |
| 9303 | if (!hasMoveOrIsTriviallyCopyable(S, Base->getType(), IsConstructor)) |
| 9304 | return false; |
| 9305 | } |
| 9306 | |
| 9307 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 9308 | BaseEnd = ClassDecl->vbases_end(); |
| 9309 | Base != BaseEnd; ++Base) { |
| 9310 | if (!hasMoveOrIsTriviallyCopyable(S, Base->getType(), IsConstructor)) |
| 9311 | return false; |
| 9312 | } |
| 9313 | |
| 9314 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9315 | FieldEnd = ClassDecl->field_end(); |
| 9316 | Field != FieldEnd; ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 9317 | if (!hasMoveOrIsTriviallyCopyable(S, Field->getType(), IsConstructor)) |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9318 | return false; |
| 9319 | } |
| 9320 | |
| 9321 | return true; |
| 9322 | } |
| 9323 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9324 | CXXMethodDecl *Sema::DeclareImplicitMoveAssignment(CXXRecordDecl *ClassDecl) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9325 | // C++11 [class.copy]p20: |
| 9326 | // If the definition of a class X does not explicitly declare a move |
| 9327 | // assignment operator, one will be implicitly declared as defaulted |
| 9328 | // if and only if: |
| 9329 | // |
| 9330 | // - [first 4 bullets] |
| 9331 | assert(ClassDecl->needsImplicitMoveAssignment()); |
| 9332 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9333 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveAssignment); |
| 9334 | if (DSM.isAlreadyBeingDeclared()) |
| 9335 | return 0; |
| 9336 | |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9337 | // [Checked after we build the declaration] |
| 9338 | // - the move assignment operator would not be implicitly defined as |
| 9339 | // deleted, |
| 9340 | |
| 9341 | // [DR1402]: |
| 9342 | // - X has no direct or indirect virtual base class with a non-trivial |
| 9343 | // move assignment operator, and |
| 9344 | // - each of X's non-static data members and direct or virtual base classes |
| 9345 | // has a type that either has a move assignment operator or is trivially |
| 9346 | // copyable. |
| 9347 | if (hasVirtualBaseWithNonTrivialMoveAssignment(*this, ClassDecl) || |
| 9348 | !subobjectsHaveMoveOrTrivialCopy(*this, ClassDecl,/*Constructor*/false)) { |
| 9349 | ClassDecl->setFailedImplicitMoveAssignment(); |
| 9350 | return 0; |
| 9351 | } |
| 9352 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9353 | // Note: The following rules are largely analoguous to the move |
| 9354 | // constructor rules. |
| 9355 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9356 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 9357 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 9358 | ArgType = Context.getRValueReferenceType(ArgType); |
| 9359 | |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9360 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 9361 | CXXMoveAssignment, |
| 9362 | false); |
| 9363 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9364 | // An implicitly-declared move assignment operator is an inline public |
| 9365 | // member of its class. |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9366 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 9367 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 9368 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9369 | CXXMethodDecl *MoveAssignment = |
| 9370 | CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, QualType(), |
| 9371 | /*TInfo=*/0, /*StorageClass=*/SC_None, |
| 9372 | /*isInline=*/true, Constexpr, SourceLocation()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9373 | MoveAssignment->setAccess(AS_public); |
| 9374 | MoveAssignment->setDefaulted(); |
| 9375 | MoveAssignment->setImplicit(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9376 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9377 | // Build an exception specification pointing back at this member. |
| 9378 | FunctionProtoType::ExtProtoInfo EPI; |
| 9379 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 9380 | EPI.ExceptionSpecDecl = MoveAssignment; |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9381 | MoveAssignment->setType(Context.getFunctionType(RetType, ArgType, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9382 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9383 | // Add the parameter to the operator. |
| 9384 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveAssignment, |
| 9385 | ClassLoc, ClassLoc, /*Id=*/0, |
| 9386 | ArgType, /*TInfo=*/0, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9387 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9388 | MoveAssignment->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9389 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9390 | AddOverriddenMethods(ClassDecl, MoveAssignment); |
| 9391 | |
| 9392 | MoveAssignment->setTrivial( |
| 9393 | ClassDecl->needsOverloadResolutionForMoveAssignment() |
| 9394 | ? SpecialMemberIsTrivial(MoveAssignment, CXXMoveAssignment) |
| 9395 | : ClassDecl->hasTrivialMoveAssignment()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9396 | |
| 9397 | // C++0x [class.copy]p9: |
| 9398 | // If the definition of a class X does not explicitly declare a move |
| 9399 | // assignment operator, one will be implicitly declared as defaulted if and |
| 9400 | // only if: |
| 9401 | // [...] |
| 9402 | // - the move assignment operator would not be implicitly defined as |
| 9403 | // deleted. |
Richard Smith | 7d5088a | 2012-02-18 02:02:13 +0000 | [diff] [blame] | 9404 | if (ShouldDeleteSpecialMember(MoveAssignment, CXXMoveAssignment)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9405 | // Cache this result so that we don't try to generate this over and over |
| 9406 | // on every lookup, leaking memory and wasting time. |
| 9407 | ClassDecl->setFailedImplicitMoveAssignment(); |
| 9408 | return 0; |
| 9409 | } |
| 9410 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9411 | // Note that we have added this copy-assignment operator. |
| 9412 | ++ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
| 9413 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9414 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 9415 | PushOnScopeChains(MoveAssignment, S, false); |
| 9416 | ClassDecl->addDecl(MoveAssignment); |
| 9417 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9418 | return MoveAssignment; |
| 9419 | } |
| 9420 | |
| 9421 | void Sema::DefineImplicitMoveAssignment(SourceLocation CurrentLocation, |
| 9422 | CXXMethodDecl *MoveAssignOperator) { |
| 9423 | assert((MoveAssignOperator->isDefaulted() && |
| 9424 | MoveAssignOperator->isOverloadedOperator() && |
| 9425 | MoveAssignOperator->getOverloadedOperator() == OO_Equal && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9426 | !MoveAssignOperator->doesThisDeclarationHaveABody() && |
| 9427 | !MoveAssignOperator->isDeleted()) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9428 | "DefineImplicitMoveAssignment called for wrong function"); |
| 9429 | |
| 9430 | CXXRecordDecl *ClassDecl = MoveAssignOperator->getParent(); |
| 9431 | |
| 9432 | if (ClassDecl->isInvalidDecl() || MoveAssignOperator->isInvalidDecl()) { |
| 9433 | MoveAssignOperator->setInvalidDecl(); |
| 9434 | return; |
| 9435 | } |
| 9436 | |
| 9437 | MoveAssignOperator->setUsed(); |
| 9438 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9439 | SynthesizedFunctionScope Scope(*this, MoveAssignOperator); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9440 | DiagnosticErrorTrap Trap(Diags); |
| 9441 | |
| 9442 | // C++0x [class.copy]p28: |
| 9443 | // The implicitly-defined or move assignment operator for a non-union class |
| 9444 | // X performs memberwise move assignment of its subobjects. The direct base |
| 9445 | // classes of X are assigned first, in the order of their declaration in the |
| 9446 | // base-specifier-list, and then the immediate non-static data members of X |
| 9447 | // are assigned, in the order in which they were declared in the class |
| 9448 | // definition. |
| 9449 | |
| 9450 | // The statements that form the synthesized function body. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 9451 | SmallVector<Stmt*, 8> Statements; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9452 | |
| 9453 | // The parameter for the "other" object, which we are move from. |
| 9454 | ParmVarDecl *Other = MoveAssignOperator->getParamDecl(0); |
| 9455 | QualType OtherRefType = Other->getType()-> |
| 9456 | getAs<RValueReferenceType>()->getPointeeType(); |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 9457 | assert(!OtherRefType.getQualifiers() && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9458 | "Bad argument type of defaulted move assignment"); |
| 9459 | |
| 9460 | // Our location for everything implicitly-generated. |
| 9461 | SourceLocation Loc = MoveAssignOperator->getLocation(); |
| 9462 | |
| 9463 | // Construct a reference to the "other" object. We'll be using this |
| 9464 | // throughout the generated ASTs. |
| 9465 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
| 9466 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 9467 | // Cast to rvalue. |
| 9468 | OtherRef = CastForMoving(*this, OtherRef); |
| 9469 | |
| 9470 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 9471 | // ASTs. |
| 9472 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 9473 | assert(This && "Reference to this cannot fail!"); |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9474 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9475 | // Assign base classes. |
| 9476 | bool Invalid = false; |
| 9477 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9478 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 9479 | // Form the assignment: |
| 9480 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&&>(other)); |
| 9481 | QualType BaseType = Base->getType().getUnqualifiedType(); |
| 9482 | if (!BaseType->isRecordType()) { |
| 9483 | Invalid = true; |
| 9484 | continue; |
| 9485 | } |
| 9486 | |
| 9487 | CXXCastPath BasePath; |
| 9488 | BasePath.push_back(Base); |
| 9489 | |
| 9490 | // Construct the "from" expression, which is an implicit cast to the |
| 9491 | // appropriately-qualified base type. |
| 9492 | Expr *From = OtherRef; |
| 9493 | From = ImpCastExprToType(From, BaseType, CK_UncheckedDerivedToBase, |
Douglas Gregor | b2b5658 | 2011-09-06 16:26:56 +0000 | [diff] [blame] | 9494 | VK_XValue, &BasePath).take(); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9495 | |
| 9496 | // Dereference "this". |
| 9497 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
| 9498 | |
| 9499 | // Implicitly cast "this" to the appropriately-qualified base type. |
| 9500 | To = ImpCastExprToType(To.take(), |
| 9501 | Context.getCVRQualifiedType(BaseType, |
| 9502 | MoveAssignOperator->getTypeQualifiers()), |
| 9503 | CK_UncheckedDerivedToBase, |
| 9504 | VK_LValue, &BasePath); |
| 9505 | |
| 9506 | // Build the move. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9507 | StmtResult Move = buildSingleCopyAssign(*this, Loc, BaseType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9508 | To.get(), From, |
| 9509 | /*CopyingBaseSubobject=*/true, |
| 9510 | /*Copying=*/false); |
| 9511 | if (Move.isInvalid()) { |
| 9512 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9513 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9514 | MoveAssignOperator->setInvalidDecl(); |
| 9515 | return; |
| 9516 | } |
| 9517 | |
| 9518 | // Success! Record the move. |
| 9519 | Statements.push_back(Move.takeAs<Expr>()); |
| 9520 | } |
| 9521 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9522 | // Assign non-static members. |
| 9523 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9524 | FieldEnd = ClassDecl->field_end(); |
| 9525 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 9526 | if (Field->isUnnamedBitfield()) |
| 9527 | continue; |
| 9528 | |
Eli Friedman | 8150da3 | 2013-06-07 01:48:56 +0000 | [diff] [blame] | 9529 | if (Field->isInvalidDecl()) { |
| 9530 | Invalid = true; |
| 9531 | continue; |
| 9532 | } |
| 9533 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9534 | // Check for members of reference type; we can't move those. |
| 9535 | if (Field->getType()->isReferenceType()) { |
| 9536 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9537 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 9538 | Diag(Field->getLocation(), diag::note_declared_at); |
| 9539 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9540 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9541 | Invalid = true; |
| 9542 | continue; |
| 9543 | } |
| 9544 | |
| 9545 | // Check for members of const-qualified, non-class type. |
| 9546 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 9547 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 9548 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 9549 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 9550 | Diag(Field->getLocation(), diag::note_declared_at); |
| 9551 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9552 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9553 | Invalid = true; |
| 9554 | continue; |
| 9555 | } |
| 9556 | |
| 9557 | // Suppress assigning zero-width bitfields. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9558 | if (Field->isBitField() && Field->getBitWidthValue(Context) == 0) |
| 9559 | continue; |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9560 | |
| 9561 | QualType FieldType = Field->getType().getNonReferenceType(); |
| 9562 | if (FieldType->isIncompleteArrayType()) { |
| 9563 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 9564 | "Incomplete array type is not valid"); |
| 9565 | continue; |
| 9566 | } |
| 9567 | |
| 9568 | // Build references to the field in the object we're copying from and to. |
| 9569 | CXXScopeSpec SS; // Intentionally empty |
| 9570 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 9571 | LookupMemberName); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 9572 | MemberLookup.addDecl(*Field); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9573 | MemberLookup.resolveKind(); |
| 9574 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
| 9575 | Loc, /*IsArrow=*/false, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9576 | SS, SourceLocation(), 0, |
| 9577 | MemberLookup, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9578 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
| 9579 | Loc, /*IsArrow=*/true, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 9580 | SS, SourceLocation(), 0, |
| 9581 | MemberLookup, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9582 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 9583 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 9584 | |
| 9585 | assert(!From.get()->isLValue() && // could be xvalue or prvalue |
| 9586 | "Member reference with rvalue base must be rvalue except for reference " |
| 9587 | "members, which aren't allowed for move assignment."); |
| 9588 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9589 | // Build the move of this field. |
Richard Smith | 8c88953 | 2012-11-14 00:50:40 +0000 | [diff] [blame] | 9590 | StmtResult Move = buildSingleCopyAssign(*this, Loc, FieldType, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9591 | To.get(), From.get(), |
| 9592 | /*CopyingBaseSubobject=*/false, |
| 9593 | /*Copying=*/false); |
| 9594 | if (Move.isInvalid()) { |
| 9595 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9596 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9597 | MoveAssignOperator->setInvalidDecl(); |
| 9598 | return; |
| 9599 | } |
Richard Smith | e7ce709 | 2012-11-12 23:33:00 +0000 | [diff] [blame] | 9600 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9601 | // Success! Record the copy. |
| 9602 | Statements.push_back(Move.takeAs<Stmt>()); |
| 9603 | } |
| 9604 | |
| 9605 | if (!Invalid) { |
| 9606 | // Add a "return *this;" |
| 9607 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
| 9608 | |
| 9609 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
| 9610 | if (Return.isInvalid()) |
| 9611 | Invalid = true; |
| 9612 | else { |
| 9613 | Statements.push_back(Return.takeAs<Stmt>()); |
| 9614 | |
| 9615 | if (Trap.hasErrorOccurred()) { |
| 9616 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9617 | << CXXMoveAssignment << Context.getTagDeclType(ClassDecl); |
| 9618 | Invalid = true; |
| 9619 | } |
| 9620 | } |
| 9621 | } |
| 9622 | |
| 9623 | if (Invalid) { |
| 9624 | MoveAssignOperator->setInvalidDecl(); |
| 9625 | return; |
| 9626 | } |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9627 | |
| 9628 | StmtResult Body; |
| 9629 | { |
| 9630 | CompoundScopeRAII CompoundScope(*this); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 9631 | Body = ActOnCompoundStmt(Loc, Loc, Statements, |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9632 | /*isStmtExpr=*/false); |
| 9633 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 9634 | } |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9635 | MoveAssignOperator->setBody(Body.takeAs<Stmt>()); |
| 9636 | |
| 9637 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9638 | L->CompletedImplicitDefinition(MoveAssignOperator); |
| 9639 | } |
| 9640 | } |
| 9641 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9642 | Sema::ImplicitExceptionSpecification |
| 9643 | Sema::ComputeDefaultedCopyCtorExceptionSpec(CXXMethodDecl *MD) { |
| 9644 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 9645 | |
| 9646 | ImplicitExceptionSpecification ExceptSpec(*this); |
| 9647 | if (ClassDecl->isInvalidDecl()) |
| 9648 | return ExceptSpec; |
| 9649 | |
| 9650 | const FunctionProtoType *T = MD->getType()->castAs<FunctionProtoType>(); |
| 9651 | assert(T->getNumArgs() >= 1 && "not a copy ctor"); |
| 9652 | unsigned Quals = T->getArgType(0).getNonReferenceType().getCVRQualifiers(); |
| 9653 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9654 | // C++ [except.spec]p14: |
| 9655 | // An implicitly declared special member function (Clause 12) shall have an |
| 9656 | // exception-specification. [...] |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9657 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 9658 | BaseEnd = ClassDecl->bases_end(); |
| 9659 | Base != BaseEnd; |
| 9660 | ++Base) { |
| 9661 | // Virtual bases are handled below. |
| 9662 | if (Base->isVirtual()) |
| 9663 | continue; |
| 9664 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 9665 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9666 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 9667 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9668 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9669 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9670 | } |
| 9671 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 9672 | BaseEnd = ClassDecl->vbases_end(); |
| 9673 | Base != BaseEnd; |
| 9674 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 9675 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9676 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 9677 | if (CXXConstructorDecl *CopyConstructor = |
Sean Hunt | 661c67a | 2011-06-21 23:42:56 +0000 | [diff] [blame] | 9678 | LookupCopyingConstructor(BaseClassDecl, Quals)) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9679 | ExceptSpec.CalledDecl(Base->getLocStart(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9680 | } |
| 9681 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 9682 | FieldEnd = ClassDecl->field_end(); |
| 9683 | Field != FieldEnd; |
| 9684 | ++Field) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 9685 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
Sean Hunt | c530d17 | 2011-06-10 04:44:37 +0000 | [diff] [blame] | 9686 | if (CXXRecordDecl *FieldClassDecl = FieldType->getAsCXXRecordDecl()) { |
| 9687 | if (CXXConstructorDecl *CopyConstructor = |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9688 | LookupCopyingConstructor(FieldClassDecl, |
| 9689 | Quals | FieldType.getCVRQualifiers())) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9690 | ExceptSpec.CalledDecl(Field->getLocation(), CopyConstructor); |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 9691 | } |
| 9692 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 9693 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9694 | return ExceptSpec; |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9695 | } |
| 9696 | |
| 9697 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 9698 | CXXRecordDecl *ClassDecl) { |
| 9699 | // C++ [class.copy]p4: |
| 9700 | // If the class definition does not explicitly declare a copy |
| 9701 | // constructor, one is declared implicitly. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 9702 | assert(ClassDecl->needsImplicitCopyConstructor()); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9703 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9704 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXCopyConstructor); |
| 9705 | if (DSM.isAlreadyBeingDeclared()) |
| 9706 | return 0; |
| 9707 | |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9708 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 9709 | QualType ArgType = ClassType; |
Richard Smith | acf796b | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 9710 | bool Const = ClassDecl->implicitCopyConstructorHasConstParam(); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9711 | if (Const) |
| 9712 | ArgType = ArgType.withConst(); |
| 9713 | ArgType = Context.getLValueReferenceType(ArgType); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9714 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 9715 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 9716 | CXXCopyConstructor, |
| 9717 | Const); |
| 9718 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9719 | DeclarationName Name |
| 9720 | = Context.DeclarationNames.getCXXConstructorName( |
| 9721 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9722 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 9723 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9724 | |
| 9725 | // An implicitly-declared copy constructor is an inline public |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9726 | // member of its class. |
| 9727 | CXXConstructorDecl *CopyConstructor = CXXConstructorDecl::Create( |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9728 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/0, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9729 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 9730 | Constexpr); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9731 | CopyConstructor->setAccess(AS_public); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9732 | CopyConstructor->setDefaulted(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9733 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9734 | // Build an exception specification pointing back at this member. |
| 9735 | FunctionProtoType::ExtProtoInfo EPI; |
| 9736 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 9737 | EPI.ExceptionSpecDecl = CopyConstructor; |
| 9738 | CopyConstructor->setType( |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9739 | Context.getFunctionType(Context.VoidTy, ArgType, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9740 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9741 | // Add the parameter to the constructor. |
| 9742 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 9743 | ClassLoc, ClassLoc, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9744 | /*IdentifierInfo=*/0, |
| 9745 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 9746 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9747 | CopyConstructor->setParams(FromParam); |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9748 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9749 | CopyConstructor->setTrivial( |
| 9750 | ClassDecl->needsOverloadResolutionForCopyConstructor() |
| 9751 | ? SpecialMemberIsTrivial(CopyConstructor, CXXCopyConstructor) |
| 9752 | : ClassDecl->hasTrivialCopyConstructor()); |
Sean Hunt | 71a682f | 2011-05-18 03:41:58 +0000 | [diff] [blame] | 9753 | |
Nico Weber | afcc96a | 2012-01-23 03:19:29 +0000 | [diff] [blame] | 9754 | // C++11 [class.copy]p8: |
| 9755 | // ... If the class definition does not explicitly declare a copy |
| 9756 | // constructor, there is no user-declared move constructor, and there is no |
| 9757 | // user-declared move assignment operator, a copy constructor is implicitly |
| 9758 | // declared as defaulted. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9759 | if (ShouldDeleteSpecialMember(CopyConstructor, CXXCopyConstructor)) |
Richard Smith | 0ab5b4c | 2013-04-02 19:38:47 +0000 | [diff] [blame] | 9760 | SetDeclDeleted(CopyConstructor, ClassLoc); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 9761 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9762 | // Note that we have declared this constructor. |
| 9763 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 9764 | |
| 9765 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 9766 | PushOnScopeChains(CopyConstructor, S, false); |
| 9767 | ClassDecl->addDecl(CopyConstructor); |
| 9768 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 9769 | return CopyConstructor; |
| 9770 | } |
| 9771 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9772 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9773 | CXXConstructorDecl *CopyConstructor) { |
| 9774 | assert((CopyConstructor->isDefaulted() && |
| 9775 | CopyConstructor->isCopyConstructor() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9776 | !CopyConstructor->doesThisDeclarationHaveABody() && |
| 9777 | !CopyConstructor->isDeleted()) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9778 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9779 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 9780 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9781 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9782 | |
Richard Smith | 36155c1 | 2013-06-13 03:23:42 +0000 | [diff] [blame] | 9783 | // C++11 [class.copy]p7: |
| 9784 | // The [definition of an implicitly declared copy constructro] is |
| 9785 | // deprecated if the class has a user-declared copy assignment operator |
| 9786 | // or a user-declared destructor. |
| 9787 | if (getLangOpts().CPlusPlus11 && CopyConstructor->isImplicit()) |
| 9788 | diagnoseDeprecatedCopyOperation(*this, CopyConstructor, CurrentLocation); |
| 9789 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9790 | SynthesizedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 9791 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 9792 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 9793 | if (SetCtorInitializers(CopyConstructor, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 9794 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 9795 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9796 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 9797 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9798 | } else { |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9799 | Sema::CompoundScopeRAII CompoundScope(*this); |
Robert Wilhelm | c895f4d | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 9800 | CopyConstructor->setBody(ActOnCompoundStmt( |
| 9801 | CopyConstructor->getLocation(), CopyConstructor->getLocation(), None, |
| 9802 | /*isStmtExpr=*/ false).takeAs<Stmt>()); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 9803 | } |
Robert Wilhelm | c895f4d | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 9804 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 9805 | CopyConstructor->setUsed(); |
Sebastian Redl | 58a2cd8 | 2011-04-24 16:28:06 +0000 | [diff] [blame] | 9806 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9807 | L->CompletedImplicitDefinition(CopyConstructor); |
| 9808 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9809 | } |
| 9810 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9811 | Sema::ImplicitExceptionSpecification |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9812 | Sema::ComputeDefaultedMoveCtorExceptionSpec(CXXMethodDecl *MD) { |
| 9813 | CXXRecordDecl *ClassDecl = MD->getParent(); |
| 9814 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9815 | // C++ [except.spec]p14: |
| 9816 | // An implicitly declared special member function (Clause 12) shall have an |
| 9817 | // exception-specification. [...] |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9818 | ImplicitExceptionSpecification ExceptSpec(*this); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9819 | if (ClassDecl->isInvalidDecl()) |
| 9820 | return ExceptSpec; |
| 9821 | |
| 9822 | // Direct base-class constructors. |
| 9823 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 9824 | BEnd = ClassDecl->bases_end(); |
| 9825 | B != BEnd; ++B) { |
| 9826 | if (B->isVirtual()) // Handled below. |
| 9827 | continue; |
| 9828 | |
| 9829 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 9830 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9831 | CXXConstructorDecl *Constructor = |
| 9832 | LookupMovingConstructor(BaseClassDecl, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9833 | // If this is a deleted function, add it anyway. This might be conformant |
| 9834 | // with the standard. This might not. I'm not sure. It might not matter. |
| 9835 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9836 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9837 | } |
| 9838 | } |
| 9839 | |
| 9840 | // Virtual base-class constructors. |
| 9841 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 9842 | BEnd = ClassDecl->vbases_end(); |
| 9843 | B != BEnd; ++B) { |
| 9844 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 9845 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9846 | CXXConstructorDecl *Constructor = |
| 9847 | LookupMovingConstructor(BaseClassDecl, 0); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9848 | // If this is a deleted function, add it anyway. This might be conformant |
| 9849 | // with the standard. This might not. I'm not sure. It might not matter. |
| 9850 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9851 | ExceptSpec.CalledDecl(B->getLocStart(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9852 | } |
| 9853 | } |
| 9854 | |
| 9855 | // Field constructors. |
| 9856 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 9857 | FEnd = ClassDecl->field_end(); |
| 9858 | F != FEnd; ++F) { |
Richard Smith | 6a06e5f | 2012-07-18 03:36:00 +0000 | [diff] [blame] | 9859 | QualType FieldType = Context.getBaseElementType(F->getType()); |
| 9860 | if (CXXRecordDecl *FieldRecDecl = FieldType->getAsCXXRecordDecl()) { |
| 9861 | CXXConstructorDecl *Constructor = |
| 9862 | LookupMovingConstructor(FieldRecDecl, FieldType.getCVRQualifiers()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9863 | // If this is a deleted function, add it anyway. This might be conformant |
| 9864 | // with the standard. This might not. I'm not sure. It might not matter. |
| 9865 | // In particular, the problem is that this function never gets called. It |
| 9866 | // might just be ill-formed because this function attempts to refer to |
| 9867 | // a deleted function here. |
| 9868 | if (Constructor) |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 9869 | ExceptSpec.CalledDecl(F->getLocation(), Constructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9870 | } |
| 9871 | } |
| 9872 | |
| 9873 | return ExceptSpec; |
| 9874 | } |
| 9875 | |
| 9876 | CXXConstructorDecl *Sema::DeclareImplicitMoveConstructor( |
| 9877 | CXXRecordDecl *ClassDecl) { |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9878 | // C++11 [class.copy]p9: |
| 9879 | // If the definition of a class X does not explicitly declare a move |
| 9880 | // constructor, one will be implicitly declared as defaulted if and only if: |
| 9881 | // |
| 9882 | // - [first 4 bullets] |
| 9883 | assert(ClassDecl->needsImplicitMoveConstructor()); |
| 9884 | |
Richard Smith | afb4918 | 2012-11-29 01:34:07 +0000 | [diff] [blame] | 9885 | DeclaringSpecialMember DSM(*this, ClassDecl, CXXMoveConstructor); |
| 9886 | if (DSM.isAlreadyBeingDeclared()) |
| 9887 | return 0; |
| 9888 | |
Richard Smith | 1c931be | 2012-04-02 18:40:40 +0000 | [diff] [blame] | 9889 | // [Checked after we build the declaration] |
| 9890 | // - the move assignment operator would not be implicitly defined as |
| 9891 | // deleted, |
| 9892 | |
| 9893 | // [DR1402]: |
| 9894 | // - each of X's non-static data members and direct or virtual base classes |
| 9895 | // has a type that either has a move constructor or is trivially copyable. |
| 9896 | if (!subobjectsHaveMoveOrTrivialCopy(*this, ClassDecl, /*Constructor*/true)) { |
| 9897 | ClassDecl->setFailedImplicitMoveConstructor(); |
| 9898 | return 0; |
| 9899 | } |
| 9900 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9901 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 9902 | QualType ArgType = Context.getRValueReferenceType(ClassType); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9903 | |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 9904 | bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, ClassDecl, |
| 9905 | CXXMoveConstructor, |
| 9906 | false); |
| 9907 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9908 | DeclarationName Name |
| 9909 | = Context.DeclarationNames.getCXXConstructorName( |
| 9910 | Context.getCanonicalType(ClassType)); |
| 9911 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 9912 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
| 9913 | |
Richard Smith | a8942d7 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 9914 | // C++11 [class.copy]p11: |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9915 | // An implicitly-declared copy/move constructor is an inline public |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9916 | // member of its class. |
| 9917 | CXXConstructorDecl *MoveConstructor = CXXConstructorDecl::Create( |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9918 | Context, ClassDecl, ClassLoc, NameInfo, QualType(), /*TInfo=*/0, |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9919 | /*isExplicit=*/false, /*isInline=*/true, /*isImplicitlyDeclared=*/true, |
Richard Smith | 7756afa | 2012-06-10 05:43:50 +0000 | [diff] [blame] | 9920 | Constexpr); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9921 | MoveConstructor->setAccess(AS_public); |
| 9922 | MoveConstructor->setDefaulted(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9923 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9924 | // Build an exception specification pointing back at this member. |
| 9925 | FunctionProtoType::ExtProtoInfo EPI; |
| 9926 | EPI.ExceptionSpecType = EST_Unevaluated; |
| 9927 | EPI.ExceptionSpecDecl = MoveConstructor; |
| 9928 | MoveConstructor->setType( |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9929 | Context.getFunctionType(Context.VoidTy, ArgType, EPI)); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 9930 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9931 | // Add the parameter to the constructor. |
| 9932 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, MoveConstructor, |
| 9933 | ClassLoc, ClassLoc, |
| 9934 | /*IdentifierInfo=*/0, |
| 9935 | ArgType, /*TInfo=*/0, |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9936 | SC_None, 0); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9937 | MoveConstructor->setParams(FromParam); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9938 | |
Richard Smith | bc2a35d | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 9939 | MoveConstructor->setTrivial( |
| 9940 | ClassDecl->needsOverloadResolutionForMoveConstructor() |
| 9941 | ? SpecialMemberIsTrivial(MoveConstructor, CXXMoveConstructor) |
| 9942 | : ClassDecl->hasTrivialMoveConstructor()); |
| 9943 | |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9944 | // C++0x [class.copy]p9: |
| 9945 | // If the definition of a class X does not explicitly declare a move |
| 9946 | // constructor, one will be implicitly declared as defaulted if and only if: |
| 9947 | // [...] |
| 9948 | // - the move constructor would not be implicitly defined as deleted. |
Sean Hunt | 769bb2d | 2011-10-11 06:43:29 +0000 | [diff] [blame] | 9949 | if (ShouldDeleteSpecialMember(MoveConstructor, CXXMoveConstructor)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9950 | // Cache this result so that we don't try to generate this over and over |
| 9951 | // on every lookup, leaking memory and wasting time. |
| 9952 | ClassDecl->setFailedImplicitMoveConstructor(); |
| 9953 | return 0; |
| 9954 | } |
| 9955 | |
| 9956 | // Note that we have declared this constructor. |
| 9957 | ++ASTContext::NumImplicitMoveConstructorsDeclared; |
| 9958 | |
| 9959 | if (Scope *S = getScopeForContext(ClassDecl)) |
| 9960 | PushOnScopeChains(MoveConstructor, S, false); |
| 9961 | ClassDecl->addDecl(MoveConstructor); |
| 9962 | |
| 9963 | return MoveConstructor; |
| 9964 | } |
| 9965 | |
| 9966 | void Sema::DefineImplicitMoveConstructor(SourceLocation CurrentLocation, |
| 9967 | CXXConstructorDecl *MoveConstructor) { |
| 9968 | assert((MoveConstructor->isDefaulted() && |
| 9969 | MoveConstructor->isMoveConstructor() && |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 9970 | !MoveConstructor->doesThisDeclarationHaveABody() && |
| 9971 | !MoveConstructor->isDeleted()) && |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9972 | "DefineImplicitMoveConstructor - call it for implicit move ctor"); |
| 9973 | |
| 9974 | CXXRecordDecl *ClassDecl = MoveConstructor->getParent(); |
| 9975 | assert(ClassDecl && "DefineImplicitMoveConstructor - invalid constructor"); |
| 9976 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 9977 | SynthesizedFunctionScope Scope(*this, MoveConstructor); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9978 | DiagnosticErrorTrap Trap(Diags); |
| 9979 | |
David Blaikie | 93c8617 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 9980 | if (SetCtorInitializers(MoveConstructor, /*AnyErrors=*/false) || |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9981 | Trap.hasErrorOccurred()) { |
| 9982 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 9983 | << CXXMoveConstructor << Context.getTagDeclType(ClassDecl); |
| 9984 | MoveConstructor->setInvalidDecl(); |
| 9985 | } else { |
Dmitri Gribenko | 625bb56 | 2012-02-14 22:14:32 +0000 | [diff] [blame] | 9986 | Sema::CompoundScopeRAII CompoundScope(*this); |
Robert Wilhelm | c895f4d | 2013-08-19 20:51:20 +0000 | [diff] [blame] | 9987 | MoveConstructor->setBody(ActOnCompoundStmt( |
| 9988 | MoveConstructor->getLocation(), MoveConstructor->getLocation(), None, |
| 9989 | /*isStmtExpr=*/ false).takeAs<Stmt>()); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9990 | } |
| 9991 | |
| 9992 | MoveConstructor->setUsed(); |
| 9993 | |
| 9994 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 9995 | L->CompletedImplicitDefinition(MoveConstructor); |
| 9996 | } |
| 9997 | } |
| 9998 | |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 9999 | bool Sema::isImplicitlyDeleted(FunctionDecl *FD) { |
Eli Friedman | c4ef948 | 2013-07-18 23:29:14 +0000 | [diff] [blame] | 10000 | return FD->isDeleted() && FD->isDefaulted() && isa<CXXMethodDecl>(FD); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 10001 | } |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10002 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10003 | /// \brief Mark the call operator of the given lambda closure type as "used". |
| 10004 | static void markLambdaCallOperatorUsed(Sema &S, CXXRecordDecl *Lambda) { |
| 10005 | CXXMethodDecl *CallOperator |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10006 | = cast<CXXMethodDecl>( |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 10007 | Lambda->lookup( |
| 10008 | S.Context.DeclarationNames.getCXXOperatorName(OO_Call)).front()); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10009 | CallOperator->setReferenced(); |
| 10010 | CallOperator->setUsed(); |
| 10011 | } |
| 10012 | |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10013 | void Sema::DefineImplicitLambdaToFunctionPointerConversion( |
| 10014 | SourceLocation CurrentLocation, |
| 10015 | CXXConversionDecl *Conv) |
| 10016 | { |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 10017 | CXXRecordDecl *Lambda = Conv->getParent(); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10018 | |
| 10019 | // Make sure that the lambda call operator is marked used. |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 10020 | markLambdaCallOperatorUsed(*this, Lambda); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10021 | |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10022 | Conv->setUsed(); |
| 10023 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10024 | SynthesizedFunctionScope Scope(*this, Conv); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10025 | DiagnosticErrorTrap Trap(Diags); |
| 10026 | |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 10027 | // Return the address of the __invoke function. |
| 10028 | DeclarationName InvokeName = &Context.Idents.get("__invoke"); |
| 10029 | CXXMethodDecl *Invoke |
| 10030 | = cast<CXXMethodDecl>(Lambda->lookup(InvokeName).front()); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10031 | Expr *FunctionRef = BuildDeclRefExpr(Invoke, Invoke->getType(), |
| 10032 | VK_LValue, Conv->getLocation()).take(); |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 10033 | assert(FunctionRef && "Can't refer to __invoke function?"); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10034 | Stmt *Return = ActOnReturnStmt(Conv->getLocation(), FunctionRef).take(); |
Nico Weber | d36aa35 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 10035 | Conv->setBody(new (Context) CompoundStmt(Context, Return, |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10036 | Conv->getLocation(), |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10037 | Conv->getLocation())); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10038 | |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 10039 | // Fill in the __invoke function with a dummy implementation. IR generation |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10040 | // will fill in the actual details. |
| 10041 | Invoke->setUsed(); |
| 10042 | Invoke->setReferenced(); |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 10043 | Invoke->setBody(new (Context) CompoundStmt(Conv->getLocation())); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10044 | |
| 10045 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10046 | L->CompletedImplicitDefinition(Conv); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 10047 | L->CompletedImplicitDefinition(Invoke); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10048 | } |
| 10049 | } |
| 10050 | |
| 10051 | void Sema::DefineImplicitLambdaToBlockPointerConversion( |
| 10052 | SourceLocation CurrentLocation, |
| 10053 | CXXConversionDecl *Conv) |
| 10054 | { |
| 10055 | Conv->setUsed(); |
| 10056 | |
Eli Friedman | 9a14db3 | 2012-10-18 20:14:08 +0000 | [diff] [blame] | 10057 | SynthesizedFunctionScope Scope(*this, Conv); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10058 | DiagnosticErrorTrap Trap(Diags); |
| 10059 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10060 | // Copy-initialize the lambda object as needed to capture it. |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10061 | Expr *This = ActOnCXXThis(CurrentLocation).take(); |
| 10062 | Expr *DerefThis =CreateBuiltinUnaryOp(CurrentLocation, UO_Deref, This).take(); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10063 | |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 10064 | ExprResult BuildBlock = BuildBlockForLambdaConversion(CurrentLocation, |
| 10065 | Conv->getLocation(), |
| 10066 | Conv, DerefThis); |
| 10067 | |
| 10068 | // If we're not under ARC, make sure we still get the _Block_copy/autorelease |
| 10069 | // behavior. Note that only the general conversion function does this |
| 10070 | // (since it's unusable otherwise); in the case where we inline the |
| 10071 | // block literal, it has block literal lifetime semantics. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10072 | if (!BuildBlock.isInvalid() && !getLangOpts().ObjCAutoRefCount) |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 10073 | BuildBlock = ImplicitCastExpr::Create(Context, BuildBlock.get()->getType(), |
| 10074 | CK_CopyAndAutoreleaseBlockObject, |
| 10075 | BuildBlock.get(), 0, VK_RValue); |
| 10076 | |
| 10077 | if (BuildBlock.isInvalid()) { |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10078 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10079 | Conv->setInvalidDecl(); |
| 10080 | return; |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10081 | } |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10082 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10083 | // Create the return statement that returns the block from the conversion |
| 10084 | // function. |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 10085 | StmtResult Return = ActOnReturnStmt(Conv->getLocation(), BuildBlock.get()); |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10086 | if (Return.isInvalid()) { |
| 10087 | Diag(CurrentLocation, diag::note_lambda_to_block_conv); |
| 10088 | Conv->setInvalidDecl(); |
| 10089 | return; |
| 10090 | } |
| 10091 | |
| 10092 | // Set the body of the conversion function. |
| 10093 | Stmt *ReturnS = Return.take(); |
Nico Weber | d36aa35 | 2012-12-29 20:03:39 +0000 | [diff] [blame] | 10094 | Conv->setBody(new (Context) CompoundStmt(Context, ReturnS, |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10095 | Conv->getLocation(), |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10096 | Conv->getLocation())); |
| 10097 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10098 | // We're done; notify the mutation listener, if any. |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10099 | if (ASTMutationListener *L = getASTMutationListener()) { |
| 10100 | L->CompletedImplicitDefinition(Conv); |
| 10101 | } |
| 10102 | } |
| 10103 | |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10104 | /// \brief Determine whether the given list arguments contains exactly one |
| 10105 | /// "real" (non-default) argument. |
| 10106 | static bool hasOneRealArgument(MultiExprArg Args) { |
| 10107 | switch (Args.size()) { |
| 10108 | case 0: |
| 10109 | return false; |
| 10110 | |
| 10111 | default: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10112 | if (!Args[1]->isDefaultArgument()) |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10113 | return false; |
| 10114 | |
| 10115 | // fall through |
| 10116 | case 1: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10117 | return !Args[0]->isDefaultArgument(); |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10118 | } |
| 10119 | |
| 10120 | return false; |
| 10121 | } |
| 10122 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10123 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 10124 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10125 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 10126 | MultiExprArg ExprArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10127 | bool HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10128 | bool IsListInitialization, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10129 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 10130 | unsigned ConstructKind, |
| 10131 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 10132 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10133 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 10134 | // C++0x [class.copy]p34: |
| 10135 | // When certain criteria are met, an implementation is allowed to |
| 10136 | // omit the copy/move construction of a class object, even if the |
| 10137 | // copy/move constructor and/or destructor for the object have |
| 10138 | // side effects. [...] |
| 10139 | // - when a temporary class object that has not been bound to a |
| 10140 | // reference (12.2) would be copied/moved to a class object |
| 10141 | // with the same cv-unqualified type, the copy/move operation |
| 10142 | // can be omitted by constructing the temporary object |
| 10143 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 10144 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
Douglas Gregor | f52757d | 2012-03-10 06:53:13 +0000 | [diff] [blame] | 10145 | Constructor->isCopyOrMoveConstructor() && hasOneRealArgument(ExprArgs)) { |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10146 | Expr *SubExpr = ExprArgs[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 10147 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 10148 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10149 | |
| 10150 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 10151 | Elidable, ExprArgs, HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10152 | IsListInitialization, RequiresZeroInit, |
| 10153 | ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 10154 | } |
| 10155 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 10156 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 10157 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10158 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 10159 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 10160 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 10161 | MultiExprArg ExprArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 10162 | bool HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10163 | bool IsListInitialization, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 10164 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 10165 | unsigned ConstructKind, |
| 10166 | SourceRange ParenRange) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10167 | MarkFunctionReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 10168 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 10169 | Constructor, Elidable, ExprArgs, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 10170 | HadMultipleCandidates, |
| 10171 | IsListInitialization, RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 10172 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 10173 | ParenRange)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 10174 | } |
| 10175 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 10176 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10177 | if (VD->isInvalidDecl()) return; |
| 10178 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 10179 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10180 | if (ClassDecl->isInvalidDecl()) return; |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 10181 | if (ClassDecl->hasIrrelevantDestructor()) return; |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10182 | if (ClassDecl->isDependentContext()) return; |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 10183 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10184 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10185 | MarkFunctionReferenced(VD->getLocation(), Destructor); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10186 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 10187 | PDiag(diag::err_access_dtor_var) |
| 10188 | << VD->getDeclName() |
| 10189 | << VD->getType()); |
Richard Smith | 213d70b | 2012-02-18 04:13:32 +0000 | [diff] [blame] | 10190 | DiagnoseUseOfDecl(Destructor, VD->getLocation()); |
Anders Carlsson | 2b32dad | 2011-03-24 01:01:41 +0000 | [diff] [blame] | 10191 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 10192 | if (!VD->hasGlobalStorage()) return; |
| 10193 | |
| 10194 | // Emit warning for non-trivial dtor in global scope (a real global, |
| 10195 | // class-static, function-static). |
| 10196 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 10197 | |
| 10198 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 10199 | if (!VD->isStaticLocal()) |
| 10200 | Diag(VD->getLocation(), diag::warn_global_destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10201 | } |
| 10202 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10203 | /// \brief Given a constructor and the set of arguments provided for the |
| 10204 | /// constructor, convert the arguments and add any required default arguments |
| 10205 | /// to form a proper call to this constructor. |
| 10206 | /// |
| 10207 | /// \returns true if an error occurred, false otherwise. |
| 10208 | bool |
| 10209 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 10210 | MultiExprArg ArgsPtr, |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 10211 | SourceLocation Loc, |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 10212 | SmallVectorImpl<Expr*> &ConvertedArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 10213 | bool AllowExplicit, |
| 10214 | bool IsListInitialization) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10215 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 10216 | unsigned NumArgs = ArgsPtr.size(); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10217 | Expr **Args = ArgsPtr.data(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10218 | |
| 10219 | const FunctionProtoType *Proto |
| 10220 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 10221 | assert(Proto && "Constructor without a prototype?"); |
| 10222 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10223 | |
| 10224 | // 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] | 10225 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10226 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10227 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 10228 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10229 | |
| 10230 | VariadicCallType CallType = |
| 10231 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10232 | SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10233 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10234 | Proto, 0, |
| 10235 | llvm::makeArrayRef(Args, NumArgs), |
| 10236 | AllArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 10237 | CallType, AllowExplicit, |
| 10238 | IsListInitialization); |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 10239 | ConvertedArgs.append(AllArgs.begin(), AllArgs.end()); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10240 | |
Dmitri Gribenko | 9e00f12 | 2013-05-09 21:02:07 +0000 | [diff] [blame] | 10241 | DiagnoseSentinelCalls(Constructor, Loc, AllArgs); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10242 | |
Dmitri Gribenko | 1c030e9 | 2013-01-13 20:46:02 +0000 | [diff] [blame] | 10243 | CheckConstructorCall(Constructor, |
| 10244 | llvm::makeArrayRef<const Expr *>(AllArgs.data(), |
| 10245 | AllArgs.size()), |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 10246 | Proto, Loc); |
Eli Friedman | e61eb04 | 2012-02-18 04:48:30 +0000 | [diff] [blame] | 10247 | |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 10248 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 10249 | } |
| 10250 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10251 | static inline bool |
| 10252 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 10253 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 10254 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10255 | if (isa<NamespaceDecl>(DC)) { |
| 10256 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10257 | diag::err_operator_new_delete_declared_in_namespace) |
| 10258 | << FnDecl->getDeclName(); |
| 10259 | } |
| 10260 | |
| 10261 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 10262 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10263 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10264 | diag::err_operator_new_delete_declared_static) |
| 10265 | << FnDecl->getDeclName(); |
| 10266 | } |
| 10267 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 10268 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10269 | } |
| 10270 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10271 | static inline bool |
| 10272 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 10273 | CanQualType ExpectedResultType, |
| 10274 | CanQualType ExpectedFirstParamType, |
| 10275 | unsigned DependentParamTypeDiag, |
| 10276 | unsigned InvalidParamTypeDiag) { |
| 10277 | QualType ResultType = |
| 10278 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 10279 | |
| 10280 | // Check that the result type is not dependent. |
| 10281 | if (ResultType->isDependentType()) |
| 10282 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10283 | diag::err_operator_new_delete_dependent_result_type) |
| 10284 | << FnDecl->getDeclName() << ExpectedResultType; |
| 10285 | |
| 10286 | // Check that the result type is what we expect. |
| 10287 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 10288 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10289 | diag::err_operator_new_delete_invalid_result_type) |
| 10290 | << FnDecl->getDeclName() << ExpectedResultType; |
| 10291 | |
| 10292 | // A function template must have at least 2 parameters. |
| 10293 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 10294 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10295 | diag::err_operator_new_delete_template_too_few_parameters) |
| 10296 | << FnDecl->getDeclName(); |
| 10297 | |
| 10298 | // The function decl must have at least 1 parameter. |
| 10299 | if (FnDecl->getNumParams() == 0) |
| 10300 | return SemaRef.Diag(FnDecl->getLocation(), |
| 10301 | diag::err_operator_new_delete_too_few_parameters) |
| 10302 | << FnDecl->getDeclName(); |
| 10303 | |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 10304 | // Check the first parameter type is not dependent. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10305 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 10306 | if (FirstParamType->isDependentType()) |
| 10307 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 10308 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 10309 | |
| 10310 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 10311 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10312 | ExpectedFirstParamType) |
| 10313 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 10314 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 10315 | |
| 10316 | return false; |
| 10317 | } |
| 10318 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10319 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10320 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10321 | // C++ [basic.stc.dynamic.allocation]p1: |
| 10322 | // A program is ill-formed if an allocation function is declared in a |
| 10323 | // namespace scope other than global scope or declared static in global |
| 10324 | // scope. |
| 10325 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 10326 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10327 | |
| 10328 | CanQualType SizeTy = |
| 10329 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 10330 | |
| 10331 | // C++ [basic.stc.dynamic.allocation]p1: |
| 10332 | // The return type shall be void*. The first parameter shall have type |
| 10333 | // std::size_t. |
| 10334 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 10335 | SizeTy, |
| 10336 | diag::err_operator_new_dependent_param_type, |
| 10337 | diag::err_operator_new_param_type)) |
| 10338 | return true; |
| 10339 | |
| 10340 | // C++ [basic.stc.dynamic.allocation]p1: |
| 10341 | // The first parameter shall not have an associated default argument. |
| 10342 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 10343 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10344 | diag::err_operator_new_default_arg) |
| 10345 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 10346 | |
| 10347 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 10348 | } |
| 10349 | |
| 10350 | static bool |
Richard Smith | 444d384 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 10351 | CheckOperatorDeleteDeclaration(Sema &SemaRef, FunctionDecl *FnDecl) { |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10352 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 10353 | // A program is ill-formed if deallocation functions are declared in a |
| 10354 | // namespace scope other than global scope or declared static in global |
| 10355 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 10356 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 10357 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10358 | |
| 10359 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 10360 | // Each deallocation function shall return void and its first parameter |
| 10361 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10362 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 10363 | SemaRef.Context.VoidPtrTy, |
| 10364 | diag::err_operator_delete_dependent_param_type, |
| 10365 | diag::err_operator_delete_param_type)) |
| 10366 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10367 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10368 | return false; |
| 10369 | } |
| 10370 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10371 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 10372 | /// of this overloaded operator is well-formed. If so, returns false; |
| 10373 | /// otherwise, emits appropriate diagnostics and returns true. |
| 10374 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10375 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10376 | "Expected an overloaded operator declaration"); |
| 10377 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10378 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 10379 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10380 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10381 | // The allocation and deallocation functions, operator new, |
| 10382 | // operator new[], operator delete and operator delete[], are |
| 10383 | // described completely in 3.7.3. The attributes and restrictions |
| 10384 | // found in the rest of this subclause do not apply to them unless |
| 10385 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 10386 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 10387 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 10388 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 10389 | if (Op == OO_New || Op == OO_Array_New) |
| 10390 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10391 | |
| 10392 | // C++ [over.oper]p6: |
| 10393 | // An operator function shall either be a non-static member |
| 10394 | // function or be a non-member function and have at least one |
| 10395 | // parameter whose type is a class, a reference to a class, an |
| 10396 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10397 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 10398 | if (MethodDecl->isStatic()) |
| 10399 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10400 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10401 | } else { |
| 10402 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10403 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 10404 | ParamEnd = FnDecl->param_end(); |
| 10405 | Param != ParamEnd; ++Param) { |
| 10406 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 10407 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 10408 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10409 | ClassOrEnumParam = true; |
| 10410 | break; |
| 10411 | } |
| 10412 | } |
| 10413 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10414 | if (!ClassOrEnumParam) |
| 10415 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 10416 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10417 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10418 | } |
| 10419 | |
| 10420 | // C++ [over.oper]p8: |
| 10421 | // An operator function cannot have default arguments (8.3.6), |
| 10422 | // except where explicitly stated below. |
| 10423 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10424 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10425 | // (C++ [over.call]p1). |
| 10426 | if (Op != OO_Call) { |
| 10427 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 10428 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10429 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10430 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 10431 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 10432 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10433 | } |
| 10434 | } |
| 10435 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10436 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 10437 | { false, false, false } |
| 10438 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 10439 | , { Unary, Binary, MemberOnly } |
| 10440 | #include "clang/Basic/OperatorKinds.def" |
| 10441 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10442 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10443 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 10444 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 10445 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10446 | |
| 10447 | // C++ [over.oper]p8: |
| 10448 | // [...] Operator functions cannot have more or fewer parameters |
| 10449 | // than the number required for the corresponding operator, as |
| 10450 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10451 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10452 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10453 | if (Op != OO_Call && |
| 10454 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 10455 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 10456 | (NumParams < 1) || (NumParams > 2))) { |
| 10457 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10458 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10459 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10460 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10461 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10462 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10463 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 10464 | assert(CanBeBinaryOperator && |
| 10465 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10466 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 10467 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10468 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 10469 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10470 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10471 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 10472 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10473 | // Overloaded operators other than operator() cannot be variadic. |
| 10474 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 10475 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 10476 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10477 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10478 | } |
| 10479 | |
| 10480 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10481 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 10482 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 10483 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 10484 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10485 | } |
| 10486 | |
| 10487 | // C++ [over.inc]p1: |
| 10488 | // The user-defined function called operator++ implements the |
| 10489 | // prefix and postfix ++ operator. If this function is a member |
| 10490 | // function with no parameters, or a non-member function with one |
| 10491 | // parameter of class or enumeration type, it defines the prefix |
| 10492 | // increment operator ++ for objects of that type. If the function |
| 10493 | // is a member function with one parameter (which shall be of type |
| 10494 | // int) or a non-member function with two parameters (the second |
| 10495 | // of which shall be of type int), it defines the postfix |
| 10496 | // increment operator ++ for objects of that type. |
| 10497 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 10498 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 10499 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 10500 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10501 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 10502 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 10503 | if (!ParamIsInt) |
| 10504 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10505 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 10506 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10507 | } |
| 10508 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 10509 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 10510 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 10511 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10512 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 10513 | /// of this literal operator function is well-formed. If so, returns |
| 10514 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 10515 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
Richard Smith | e5658f0 | 2012-03-10 22:18:57 +0000 | [diff] [blame] | 10516 | if (isa<CXXMethodDecl>(FnDecl)) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10517 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 10518 | << FnDecl->getDeclName(); |
| 10519 | return true; |
| 10520 | } |
| 10521 | |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10522 | if (FnDecl->isExternC()) { |
| 10523 | Diag(FnDecl->getLocation(), diag::err_literal_operator_extern_c); |
| 10524 | return true; |
| 10525 | } |
| 10526 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10527 | bool Valid = false; |
| 10528 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 10529 | // This might be the definition of a literal operator template. |
| 10530 | FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate(); |
| 10531 | // This might be a specialization of a literal operator template. |
| 10532 | if (!TpDecl) |
| 10533 | TpDecl = FnDecl->getPrimaryTemplate(); |
| 10534 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10535 | // template <char...> type operator "" name() is the only valid template |
| 10536 | // signature, and the only valid signature with no parameters. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 10537 | if (TpDecl) { |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10538 | if (FnDecl->param_size() == 0) { |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10539 | // Must have only one template parameter |
| 10540 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 10541 | if (Params->size() == 1) { |
| 10542 | NonTypeTemplateParmDecl *PmDecl = |
Richard Smith | 5295b97 | 2012-08-03 21:14:57 +0000 | [diff] [blame] | 10543 | dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10544 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10545 | // The template parameter must be a char parameter pack. |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10546 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 10547 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 10548 | Valid = true; |
| 10549 | } |
| 10550 | } |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10551 | } else if (FnDecl->param_size()) { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10552 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 10553 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 10554 | |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10555 | QualType T = (*Param)->getType().getUnqualifiedType(); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10556 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 10557 | // unsigned long long int, long double, and any character type are allowed |
| 10558 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10559 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 10560 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 10561 | Context.hasSameType(T, Context.CharTy) || |
Hans Wennborg | 15f92ba | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 10562 | Context.hasSameType(T, Context.WideCharTy) || |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10563 | Context.hasSameType(T, Context.Char16Ty) || |
| 10564 | Context.hasSameType(T, Context.Char32Ty)) { |
| 10565 | if (++Param == FnDecl->param_end()) |
| 10566 | Valid = true; |
| 10567 | goto FinishedParams; |
| 10568 | } |
| 10569 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 10570 | // 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] | 10571 | const PointerType *PT = T->getAs<PointerType>(); |
| 10572 | if (!PT) |
| 10573 | goto FinishedParams; |
| 10574 | T = PT->getPointeeType(); |
Richard Smith | b4a7b1e | 2012-03-04 09:41:16 +0000 | [diff] [blame] | 10575 | if (!T.isConstQualified() || T.isVolatileQualified()) |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10576 | goto FinishedParams; |
| 10577 | T = T.getUnqualifiedType(); |
| 10578 | |
| 10579 | // Move on to the second parameter; |
| 10580 | ++Param; |
| 10581 | |
| 10582 | // If there is no second parameter, the first must be a const char * |
| 10583 | if (Param == FnDecl->param_end()) { |
| 10584 | if (Context.hasSameType(T, Context.CharTy)) |
| 10585 | Valid = true; |
| 10586 | goto FinishedParams; |
| 10587 | } |
| 10588 | |
| 10589 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 10590 | // are allowed as the first parameter to a two-parameter function |
| 10591 | if (!(Context.hasSameType(T, Context.CharTy) || |
Hans Wennborg | 15f92ba | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 10592 | Context.hasSameType(T, Context.WideCharTy) || |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10593 | Context.hasSameType(T, Context.Char16Ty) || |
| 10594 | Context.hasSameType(T, Context.Char32Ty))) |
| 10595 | goto FinishedParams; |
| 10596 | |
| 10597 | // The second and final parameter must be an std::size_t |
| 10598 | T = (*Param)->getType().getUnqualifiedType(); |
| 10599 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 10600 | ++Param == FnDecl->param_end()) |
| 10601 | Valid = true; |
| 10602 | } |
| 10603 | |
| 10604 | // FIXME: This diagnostic is absolutely terrible. |
| 10605 | FinishedParams: |
| 10606 | if (!Valid) { |
| 10607 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 10608 | << FnDecl->getDeclName(); |
| 10609 | return true; |
| 10610 | } |
| 10611 | |
Richard Smith | a9e88b2 | 2012-03-09 08:16:22 +0000 | [diff] [blame] | 10612 | // A parameter-declaration-clause containing a default argument is not |
| 10613 | // equivalent to any of the permitted forms. |
| 10614 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 10615 | ParamEnd = FnDecl->param_end(); |
| 10616 | Param != ParamEnd; ++Param) { |
| 10617 | if ((*Param)->hasDefaultArg()) { |
| 10618 | Diag((*Param)->getDefaultArgRange().getBegin(), |
| 10619 | diag::err_literal_operator_default_argument) |
| 10620 | << (*Param)->getDefaultArgRange(); |
| 10621 | break; |
| 10622 | } |
| 10623 | } |
| 10624 | |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 10625 | StringRef LiteralName |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 10626 | = FnDecl->getDeclName().getCXXLiteralIdentifier()->getName(); |
| 10627 | if (LiteralName[0] != '_') { |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 10628 | // C++11 [usrlit.suffix]p1: |
| 10629 | // Literal suffix identifiers that do not start with an underscore |
| 10630 | // are reserved for future standardization. |
Richard Smith | 4ac537b | 2013-07-23 08:14:48 +0000 | [diff] [blame] | 10631 | Diag(FnDecl->getLocation(), diag::warn_user_literal_reserved) |
| 10632 | << NumericLiteralParser::isValidUDSuffix(getLangOpts(), LiteralName); |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 10633 | } |
Richard Smith | 2fb4ae3 | 2012-03-08 02:39:21 +0000 | [diff] [blame] | 10634 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 10635 | return false; |
| 10636 | } |
| 10637 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10638 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 10639 | /// linkage specification, including the language and (if present) |
| 10640 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 10641 | /// the location of the language string literal, which is provided |
| 10642 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 10643 | /// the '{' brace. Otherwise, this linkage specification does not |
| 10644 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 10645 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 10646 | SourceLocation LangLoc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 10647 | StringRef Lang, |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 10648 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10649 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 10650 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10651 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 10652 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10653 | Language = LinkageSpecDecl::lang_cxx; |
| 10654 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10655 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10656 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10657 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10658 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10659 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10660 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10661 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Rafael Espindola | e5e575d | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 10662 | ExternLoc, LangLoc, Language, |
| 10663 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 10664 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10665 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10666 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 10667 | } |
| 10668 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 10669 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10670 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 10671 | /// valid, it's the position of the closing '}' brace in a linkage |
| 10672 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10673 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 10674 | Decl *LinkageSpec, |
| 10675 | SourceLocation RBraceLoc) { |
| 10676 | if (LinkageSpec) { |
| 10677 | if (RBraceLoc.isValid()) { |
| 10678 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 10679 | LSDecl->setRBraceLoc(RBraceLoc); |
| 10680 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10681 | PopDeclContext(); |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 10682 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 10683 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 10684 | } |
| 10685 | |
Michael Han | 684aa73 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 10686 | Decl *Sema::ActOnEmptyDeclaration(Scope *S, |
| 10687 | AttributeList *AttrList, |
| 10688 | SourceLocation SemiLoc) { |
| 10689 | Decl *ED = EmptyDecl::Create(Context, CurContext, SemiLoc); |
| 10690 | // Attribute declarations appertain to empty declaration so we handle |
| 10691 | // them here. |
| 10692 | if (AttrList) |
| 10693 | ProcessDeclAttributeList(S, ED, AttrList); |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 10694 | |
Michael Han | 684aa73 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 10695 | CurContext->addDecl(ED); |
| 10696 | return ED; |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 10697 | } |
| 10698 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10699 | /// \brief Perform semantic analysis for the variable declaration that |
| 10700 | /// occurs within a C++ catch clause, returning the newly-created |
| 10701 | /// variable. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10702 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 10703 | TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10704 | SourceLocation StartLoc, |
| 10705 | SourceLocation Loc, |
| 10706 | IdentifierInfo *Name) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10707 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 10708 | QualType ExDeclType = TInfo->getType(); |
| 10709 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10710 | // Arrays and functions decay. |
| 10711 | if (ExDeclType->isArrayType()) |
| 10712 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 10713 | else if (ExDeclType->isFunctionType()) |
| 10714 | ExDeclType = Context.getPointerType(ExDeclType); |
| 10715 | |
| 10716 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 10717 | // The exception-declaration shall not denote a pointer or reference to an |
| 10718 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 10719 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10720 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 10721 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 10722 | Invalid = true; |
| 10723 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10724 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10725 | QualType BaseType = ExDeclType; |
| 10726 | 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] | 10727 | unsigned DK = diag::err_catch_incomplete; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 10728 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10729 | BaseType = Ptr->getPointeeType(); |
| 10730 | Mode = 1; |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 10731 | DK = diag::err_catch_incomplete_ptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10732 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 10733 | // 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] | 10734 | BaseType = Ref->getPointeeType(); |
| 10735 | Mode = 2; |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 10736 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10737 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 10738 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | ecd7b04 | 2012-01-24 19:01:26 +0000 | [diff] [blame] | 10739 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10740 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10741 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10742 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10743 | RequireNonAbstractType(Loc, ExDeclType, |
| 10744 | diag::err_abstract_type_in_decl, |
| 10745 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 10746 | Invalid = true; |
| 10747 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 10748 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 10749 | // of ObjC types, and no runtime supports catching ObjC types by value. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10750 | if (!Invalid && getLangOpts().ObjC1) { |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 10751 | QualType T = ExDeclType; |
| 10752 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 10753 | T = RT->getPointeeType(); |
| 10754 | |
| 10755 | if (T->isObjCObjectType()) { |
| 10756 | Diag(Loc, diag::err_objc_object_catch); |
| 10757 | Invalid = true; |
| 10758 | } else if (T->isObjCObjectPointerType()) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 10759 | // FIXME: should this be a test for macosx-fragile specifically? |
| 10760 | if (getLangOpts().ObjCRuntime.isFragile()) |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 10761 | Diag(Loc, diag::warn_objc_pointer_cxx_catch_fragile); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 10762 | } |
| 10763 | } |
| 10764 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10765 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 10766 | ExDeclType, TInfo, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 10767 | ExDecl->setExceptionVariable(true); |
| 10768 | |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 10769 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10770 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(ExDecl)) |
Douglas Gregor | 9aab9c4 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 10771 | Invalid = true; |
| 10772 | |
Douglas Gregor | c41b878 | 2011-07-06 18:14:43 +0000 | [diff] [blame] | 10773 | if (!Invalid && !ExDeclType->isDependentType()) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 10774 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
John McCall | b760f11 | 2013-03-22 02:10:40 +0000 | [diff] [blame] | 10775 | // Insulate this from anything else we might currently be parsing. |
| 10776 | EnterExpressionEvaluationContext scope(*this, PotentiallyEvaluated); |
| 10777 | |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 10778 | // C++ [except.handle]p16: |
| 10779 | // The object declared in an exception-declaration or, if the |
| 10780 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 10781 | // copy-initialized (8.5) from the exception object. [...] |
| 10782 | // The object is destroyed when the handler exits, after the destruction |
| 10783 | // of any automatic objects initialized within the handler. |
| 10784 | // |
| 10785 | // We just pretend to initialize the object with itself, then make sure |
| 10786 | // it can be destroyed later. |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 10787 | QualType initType = ExDeclType; |
| 10788 | |
| 10789 | InitializedEntity entity = |
| 10790 | InitializedEntity::InitializeVariable(ExDecl); |
| 10791 | InitializationKind initKind = |
| 10792 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 10793 | |
| 10794 | Expr *opaqueValue = |
| 10795 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 10796 | InitializationSequence sequence(*this, entity, initKind, opaqueValue); |
| 10797 | ExprResult result = sequence.Perform(*this, entity, initKind, opaqueValue); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 10798 | if (result.isInvalid()) |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 10799 | Invalid = true; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 10800 | else { |
| 10801 | // If the constructor used was non-trivial, set this as the |
| 10802 | // "initializer". |
| 10803 | CXXConstructExpr *construct = cast<CXXConstructExpr>(result.take()); |
| 10804 | if (!construct->getConstructor()->isTrivial()) { |
| 10805 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 10806 | ExDecl->setInit(init); |
| 10807 | } |
| 10808 | |
| 10809 | // And make sure it's destructable. |
| 10810 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 10811 | } |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 10812 | } |
| 10813 | } |
| 10814 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10815 | if (Invalid) |
| 10816 | ExDecl->setInvalidDecl(); |
| 10817 | |
| 10818 | return ExDecl; |
| 10819 | } |
| 10820 | |
| 10821 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 10822 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10823 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 10824 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 10825 | bool Invalid = D.isInvalidType(); |
| 10826 | |
| 10827 | // Check for unexpanded parameter packs. |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame] | 10828 | if (DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 10829 | UPPC_ExceptionType)) { |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 10830 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 10831 | D.getIdentifierLoc()); |
| 10832 | Invalid = true; |
| 10833 | } |
| 10834 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10835 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 10836 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 10837 | LookupOrdinaryName, |
| 10838 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10839 | // The scope should be freshly made just for us. There is just no way |
| 10840 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10841 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10842 | if (PrevDecl->isTemplateParameter()) { |
| 10843 | // Maybe we will complain about the shadowed template parameter. |
| 10844 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 10845 | PrevDecl = 0; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10846 | } |
| 10847 | } |
| 10848 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 10849 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10850 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 10851 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 10852 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10853 | } |
| 10854 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 10855 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 10856 | D.getLocStart(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 10857 | D.getIdentifierLoc(), |
| 10858 | D.getIdentifier()); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 10859 | if (Invalid) |
| 10860 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10861 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10862 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10863 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 10864 | PushOnScopeChains(ExDecl, S); |
| 10865 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 10866 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10867 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 10868 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10869 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 10870 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 10871 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10872 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 10873 | Expr *AssertExpr, |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10874 | Expr *AssertMessageExpr, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10875 | SourceLocation RParenLoc) { |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10876 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 10877 | |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10878 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 10879 | return 0; |
| 10880 | |
| 10881 | return BuildStaticAssertDeclaration(StaticAssertLoc, AssertExpr, |
| 10882 | AssertMessage, RParenLoc, false); |
| 10883 | } |
| 10884 | |
| 10885 | Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
| 10886 | Expr *AssertExpr, |
| 10887 | StringLiteral *AssertMessage, |
| 10888 | SourceLocation RParenLoc, |
| 10889 | bool Failed) { |
| 10890 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent() && |
| 10891 | !Failed) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10892 | // In a static_assert-declaration, the constant-expression shall be a |
| 10893 | // constant expression that can be contextually converted to bool. |
| 10894 | ExprResult Converted = PerformContextuallyConvertToBool(AssertExpr); |
| 10895 | if (Converted.isInvalid()) |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10896 | Failed = true; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10897 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10898 | llvm::APSInt Cond; |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10899 | if (!Failed && VerifyIntegerConstantExpression(Converted.get(), &Cond, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10900 | diag::err_static_assert_expression_is_not_constant, |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10901 | /*AllowFold=*/false).isInvalid()) |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10902 | Failed = true; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 10903 | |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10904 | if (!Failed && !Cond) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10905 | SmallString<256> MsgBuffer; |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 10906 | llvm::raw_svector_ostream Msg(MsgBuffer); |
Richard Smith | d1420c6 | 2012-08-16 03:56:14 +0000 | [diff] [blame] | 10907 | AssertMessage->printPretty(Msg, 0, getPrintingPolicy()); |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10908 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 10909 | << Msg.str() << AssertExpr->getSourceRange(); |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10910 | Failed = true; |
Richard Smith | 0cc323c | 2012-03-05 23:20:05 +0000 | [diff] [blame] | 10911 | } |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 10912 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10913 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 10914 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
Richard Smith | e3f470a | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 10915 | AssertExpr, AssertMessage, RParenLoc, |
| 10916 | Failed); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10917 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 10918 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 10919 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 10920 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 10921 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10922 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 10923 | /// |
| 10924 | /// \returns A friend declaration that. |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10925 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation LocStart, |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 10926 | SourceLocation FriendLoc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10927 | TypeSourceInfo *TSInfo) { |
| 10928 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 10929 | |
| 10930 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 10931 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10932 | |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10933 | // C++03 [class.friend]p2: |
| 10934 | // An elaborated-type-specifier shall be used in a friend declaration |
| 10935 | // for a class.* |
| 10936 | // |
| 10937 | // * The class-key of the elaborated-type-specifier is required. |
| 10938 | if (!ActiveTemplateInstantiations.empty()) { |
| 10939 | // Do not complain about the form of friend template types during |
| 10940 | // template instantiation; we will already have complained when the |
| 10941 | // template was declared. |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 10942 | } else { |
| 10943 | if (!T->isElaboratedTypeSpecifier()) { |
| 10944 | // If we evaluated the type to a record type, suggest putting |
| 10945 | // a tag in front. |
| 10946 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 10947 | RecordDecl *RD = RT->getDecl(); |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10948 | |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 10949 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10950 | |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 10951 | Diag(TypeRange.getBegin(), |
| 10952 | getLangOpts().CPlusPlus11 ? |
| 10953 | diag::warn_cxx98_compat_unelaborated_friend_type : |
| 10954 | diag::ext_unelaborated_friend_type) |
| 10955 | << (unsigned) RD->getTagKind() |
| 10956 | << T |
| 10957 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 10958 | InsertionText); |
| 10959 | } else { |
| 10960 | Diag(FriendLoc, |
| 10961 | getLangOpts().CPlusPlus11 ? |
| 10962 | diag::warn_cxx98_compat_nonclass_type_friend : |
| 10963 | diag::ext_nonclass_type_friend) |
| 10964 | << T |
| 10965 | << TypeRange; |
| 10966 | } |
| 10967 | } else if (T->getAs<EnumType>()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 10968 | Diag(FriendLoc, |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10969 | getLangOpts().CPlusPlus11 ? |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 10970 | diag::warn_cxx98_compat_enum_friend : |
| 10971 | diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10972 | << T |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10973 | << TypeRange; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10974 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10975 | |
Nick Lewycky | ce6a10e | 2013-02-06 05:59:33 +0000 | [diff] [blame] | 10976 | // C++11 [class.friend]p3: |
| 10977 | // A friend declaration that does not declare a function shall have one |
| 10978 | // of the following forms: |
| 10979 | // friend elaborated-type-specifier ; |
| 10980 | // friend simple-type-specifier ; |
| 10981 | // friend typename-specifier ; |
| 10982 | if (getLangOpts().CPlusPlus11 && LocStart != FriendLoc) |
| 10983 | Diag(FriendLoc, diag::err_friend_not_first_in_declaration) << T; |
| 10984 | } |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10985 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 10986 | // If the type specifier in a friend declaration designates a (possibly |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10987 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 10988 | // the friend declaration is ignored. |
Richard Smith | d6f80da | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 10989 | return FriendDecl::Create(Context, CurContext, LocStart, TSInfo, FriendLoc); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 10990 | } |
| 10991 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10992 | /// Handle a friend tag declaration where the scope specifier was |
| 10993 | /// templated. |
| 10994 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 10995 | unsigned TagSpec, SourceLocation TagLoc, |
| 10996 | CXXScopeSpec &SS, |
Enea Zaffanella | 8c84028 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 10997 | IdentifierInfo *Name, |
| 10998 | SourceLocation NameLoc, |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 10999 | AttributeList *Attr, |
| 11000 | MultiTemplateParamsArg TempParamLists) { |
| 11001 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 11002 | |
| 11003 | bool isExplicitSpecialization = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11004 | bool Invalid = false; |
| 11005 | |
Robert Wilhelm | 1169e2f | 2013-07-21 15:20:44 +0000 | [diff] [blame] | 11006 | if (TemplateParameterList *TemplateParams = |
| 11007 | MatchTemplateParametersToScopeSpecifier( |
| 11008 | TagLoc, NameLoc, SS, TempParamLists, /*friend*/ true, |
| 11009 | isExplicitSpecialization, Invalid)) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11010 | if (TemplateParams->size() > 0) { |
| 11011 | // This is a declaration of a class template. |
| 11012 | if (Invalid) |
| 11013 | return 0; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 11014 | |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 11015 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 11016 | SS, Name, NameLoc, Attr, |
| 11017 | TemplateParams, AS_public, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 11018 | /*ModulePrivateLoc=*/SourceLocation(), |
Eric Christopher | 4110e13 | 2011-07-21 05:34:24 +0000 | [diff] [blame] | 11019 | TempParamLists.size() - 1, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11020 | TempParamLists.data()).take(); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11021 | } else { |
| 11022 | // The "template<>" header is extraneous. |
| 11023 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 11024 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 11025 | isExplicitSpecialization = true; |
| 11026 | } |
| 11027 | } |
| 11028 | |
| 11029 | if (Invalid) return 0; |
| 11030 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11031 | bool isAllExplicitSpecializations = true; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 11032 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11033 | if (TempParamLists[I]->size()) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11034 | isAllExplicitSpecializations = false; |
| 11035 | break; |
| 11036 | } |
| 11037 | } |
| 11038 | |
| 11039 | // FIXME: don't ignore attributes. |
| 11040 | |
| 11041 | // If it's explicit specializations all the way down, just forget |
| 11042 | // about the template header and build an appropriate non-templated |
| 11043 | // friend. TODO: for source fidelity, remember the headers. |
| 11044 | if (isAllExplicitSpecializations) { |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11045 | if (SS.isEmpty()) { |
| 11046 | bool Owned = false; |
| 11047 | bool IsDependent = false; |
| 11048 | return ActOnTag(S, TagSpec, TUK_Friend, TagLoc, SS, Name, NameLoc, |
| 11049 | Attr, AS_public, |
| 11050 | /*ModulePrivateLoc=*/SourceLocation(), |
| 11051 | MultiTemplateParamsArg(), Owned, IsDependent, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 11052 | /*ScopedEnumKWLoc=*/SourceLocation(), |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11053 | /*ScopedEnumUsesClassTag=*/false, |
| 11054 | /*UnderlyingType=*/TypeResult()); |
| 11055 | } |
| 11056 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11057 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11058 | ElaboratedTypeKeyword Keyword |
| 11059 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11060 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 11061 | *Name, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11062 | if (T.isNull()) |
| 11063 | return 0; |
| 11064 | |
| 11065 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 11066 | if (isa<DependentNameType>(T)) { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11067 | DependentNameTypeLoc TL = |
| 11068 | TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 11069 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11070 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11071 | TL.setNameLoc(NameLoc); |
| 11072 | } else { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11073 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 11074 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 11075 | TL.setQualifierLoc(QualifierLoc); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11076 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11077 | } |
| 11078 | |
| 11079 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
Enea Zaffanella | 8c84028 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 11080 | TSI, FriendLoc, TempParamLists); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11081 | Friend->setAccess(AS_public); |
| 11082 | CurContext->addDecl(Friend); |
| 11083 | return Friend; |
| 11084 | } |
Douglas Gregor | ba4ee9a | 2011-10-20 15:58:54 +0000 | [diff] [blame] | 11085 | |
| 11086 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 11087 | |
| 11088 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11089 | |
| 11090 | // Handle the case of a templated-scope friend class. e.g. |
| 11091 | // template <class T> class A<T>::B; |
| 11092 | // FIXME: we don't support these right now. |
| 11093 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 11094 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 11095 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 11096 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 11097 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 11098 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11099 | TL.setNameLoc(NameLoc); |
| 11100 | |
| 11101 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
Enea Zaffanella | 8c84028 | 2013-01-31 09:54:08 +0000 | [diff] [blame] | 11102 | TSI, FriendLoc, TempParamLists); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 11103 | Friend->setAccess(AS_public); |
| 11104 | Friend->setUnsupportedFriend(true); |
| 11105 | CurContext->addDecl(Friend); |
| 11106 | return Friend; |
| 11107 | } |
| 11108 | |
| 11109 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11110 | /// Handle a friend type declaration. This works in tandem with |
| 11111 | /// ActOnTag. |
| 11112 | /// |
| 11113 | /// Notes on friend class templates: |
| 11114 | /// |
| 11115 | /// We generally treat friend class declarations as if they were |
| 11116 | /// declaring a class. So, for example, the elaborated type specifier |
| 11117 | /// in a friend declaration is required to obey the restrictions of a |
| 11118 | /// class-head (i.e. no typedefs in the scope chain), template |
| 11119 | /// parameters are required to match up with simple template-ids, &c. |
| 11120 | /// However, unlike when declaring a template specialization, it's |
| 11121 | /// okay to refer to a template specialization without an empty |
| 11122 | /// template parameter declaration, e.g. |
| 11123 | /// friend class A<T>::B<unsigned>; |
| 11124 | /// We permit this as a special case; if there are any template |
| 11125 | /// parameters present at all, require proper matching, i.e. |
James Dennett | ef2b5b3 | 2012-06-15 22:23:43 +0000 | [diff] [blame] | 11126 | /// template <> template \<class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11127 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 11128 | MultiTemplateParamsArg TempParams) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11129 | SourceLocation Loc = DS.getLocStart(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11130 | |
| 11131 | assert(DS.isFriendSpecified()); |
| 11132 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 11133 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11134 | // Try to convert the decl specifier to a type. This works for |
| 11135 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 11136 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 11137 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11138 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 11139 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 11140 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11141 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11142 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 11143 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 11144 | return 0; |
| 11145 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11146 | // This is definitely an error in C++98. It's probably meant to |
| 11147 | // be forbidden in C++0x, too, but the specification is just |
| 11148 | // poorly written. |
| 11149 | // |
| 11150 | // The problem is with declarations like the following: |
| 11151 | // template <T> friend A<T>::foo; |
| 11152 | // where deciding whether a class C is a friend or not now hinges |
| 11153 | // on whether there exists an instantiation of A that causes |
| 11154 | // 'foo' to equal C. There are restrictions on class-heads |
| 11155 | // (which we declare (by fiat) elaborated friend declarations to |
| 11156 | // be) that makes this tractable. |
| 11157 | // |
| 11158 | // FIXME: handle "template <> friend class A<T>;", which |
| 11159 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 11160 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11161 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 11162 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11163 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11164 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11165 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11166 | // C++98 [class.friend]p1: A friend of a class is a function |
| 11167 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 11168 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 11169 | // deadline. It's also a very silly restriction that seriously |
| 11170 | // affects inner classes and which nobody else seems to implement; |
| 11171 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 11172 | // |
| 11173 | // But note that we could warn about it: it's always useless to |
| 11174 | // friend one of your own members (it's not, however, worthless to |
| 11175 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11176 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11177 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11178 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11179 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11180 | NumTempParamLists, |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 11181 | TempParams.data(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 11182 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11183 | DS.getFriendSpecLoc()); |
| 11184 | else |
Abramo Bagnara | 0216df8 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 11185 | D = CheckFriendTypeDecl(Loc, DS.getFriendSpecLoc(), TSI); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11186 | |
| 11187 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11188 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 11189 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 11190 | D->setAccess(AS_public); |
| 11191 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11192 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11193 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11194 | } |
| 11195 | |
Rafael Espindola | fc35cbc | 2013-01-08 20:44:06 +0000 | [diff] [blame] | 11196 | NamedDecl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, |
| 11197 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11198 | const DeclSpec &DS = D.getDeclSpec(); |
| 11199 | |
| 11200 | assert(DS.isFriendSpecified()); |
| 11201 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 11202 | |
| 11203 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 11204 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11205 | |
| 11206 | // C++ [class.friend]p1 |
| 11207 | // A friend of a class is a function or class.... |
| 11208 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 11209 | // It *doesn't* see through dependent types, which is correct |
| 11210 | // according to [temp.arg.type]p3: |
| 11211 | // If a declaration acquires a function type through a |
| 11212 | // type dependent on a template-parameter and this causes |
| 11213 | // a declaration that does not use the syntactic form of a |
| 11214 | // function declarator to have a function type, the program |
| 11215 | // is ill-formed. |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11216 | if (!TInfo->getType()->isFunctionType()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11217 | Diag(Loc, diag::err_unexpected_friend); |
| 11218 | |
| 11219 | // It might be worthwhile to try to recover by creating an |
| 11220 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11221 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11222 | } |
| 11223 | |
| 11224 | // C++ [namespace.memdef]p3 |
| 11225 | // - If a friend declaration in a non-local class first declares a |
| 11226 | // class or function, the friend class or function is a member |
| 11227 | // of the innermost enclosing namespace. |
| 11228 | // - The name of the friend is not found by simple name lookup |
| 11229 | // until a matching declaration is provided in that namespace |
| 11230 | // scope (either before or after the class declaration granting |
| 11231 | // friendship). |
| 11232 | // - If a friend function is called, its name may be found by the |
| 11233 | // name lookup that considers functions from namespaces and |
| 11234 | // classes associated with the types of the function arguments. |
| 11235 | // - When looking for a prior declaration of a class or a function |
| 11236 | // declared as a friend, scopes outside the innermost enclosing |
| 11237 | // namespace scope are not considered. |
| 11238 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11239 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 11240 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 11241 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11242 | assert(Name); |
| 11243 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 11244 | // Check for unexpanded parameter packs. |
| 11245 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 11246 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 11247 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 11248 | return 0; |
| 11249 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11250 | // The context we found the declaration in, or in which we should |
| 11251 | // create the declaration. |
| 11252 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 11253 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 11254 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 11255 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11256 | |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11257 | // There are five cases here. |
| 11258 | // - There's no scope specifier and we're in a local class. Only look |
| 11259 | // for functions declared in the immediately-enclosing block scope. |
| 11260 | // We recover from invalid scope qualifiers as if they just weren't there. |
| 11261 | FunctionDecl *FunctionContainingLocalClass = 0; |
| 11262 | if ((SS.isInvalid() || !SS.isSet()) && |
| 11263 | (FunctionContainingLocalClass = |
| 11264 | cast<CXXRecordDecl>(CurContext)->isLocalClass())) { |
| 11265 | // C++11 [class.friend]p11: |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11266 | // If a friend declaration appears in a local class and the name |
| 11267 | // specified is an unqualified name, a prior declaration is |
| 11268 | // looked up without considering scopes that are outside the |
| 11269 | // innermost enclosing non-class scope. For a friend function |
| 11270 | // declaration, if there is no prior declaration, the program is |
| 11271 | // ill-formed. |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11272 | |
| 11273 | // Find the innermost enclosing non-class scope. This is the block |
| 11274 | // scope containing the local class definition (or for a nested class, |
| 11275 | // the outer local class). |
| 11276 | DCScope = S->getFnParent(); |
| 11277 | |
| 11278 | // Look up the function name in the scope. |
| 11279 | Previous.clear(LookupLocalFriendName); |
| 11280 | LookupName(Previous, S, /*AllowBuiltinCreation*/false); |
| 11281 | |
| 11282 | if (!Previous.empty()) { |
| 11283 | // All possible previous declarations must have the same context: |
| 11284 | // either they were declared at block scope or they are members of |
| 11285 | // one of the enclosing local classes. |
| 11286 | DC = Previous.getRepresentativeDecl()->getDeclContext(); |
| 11287 | } else { |
| 11288 | // This is ill-formed, but provide the context that we would have |
| 11289 | // declared the function in, if we were permitted to, for error recovery. |
| 11290 | DC = FunctionContainingLocalClass; |
| 11291 | } |
| 11292 | |
| 11293 | // C++ [class.friend]p6: |
| 11294 | // A function can be defined in a friend declaration of a class if and |
| 11295 | // only if the class is a non-local class (9.8), the function name is |
| 11296 | // unqualified, and the function has namespace scope. |
| 11297 | if (D.isFunctionDefinition()) { |
| 11298 | Diag(NameInfo.getBeginLoc(), diag::err_friend_def_in_local_class); |
| 11299 | } |
| 11300 | |
| 11301 | // - There's no scope specifier, in which case we just go to the |
| 11302 | // appropriate scope and look for a function or function template |
| 11303 | // there as appropriate. |
| 11304 | } else if (SS.isInvalid() || !SS.isSet()) { |
| 11305 | // C++11 [namespace.memdef]p3: |
| 11306 | // If the name in a friend declaration is neither qualified nor |
| 11307 | // a template-id and the declaration is a function or an |
| 11308 | // elaborated-type-specifier, the lookup to determine whether |
| 11309 | // the entity has been previously declared shall not consider |
| 11310 | // any scopes outside the innermost enclosing namespace. |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 11311 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11312 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11313 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11314 | DC = CurContext; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11315 | |
Rafael Espindola | 11dc634 | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11316 | // Skip class contexts. If someone can cite chapter and verse |
| 11317 | // for this behavior, that would be nice --- it's what GCC and |
| 11318 | // EDG do, and it seems like a reasonable intent, but the spec |
| 11319 | // really only says that checks for unqualified existing |
| 11320 | // declarations should stop at the nearest enclosing namespace, |
| 11321 | // not that they should only consider the nearest enclosing |
| 11322 | // namespace. |
| 11323 | while (DC->isRecord()) |
| 11324 | DC = DC->getParent(); |
| 11325 | |
| 11326 | DeclContext *LookupDC = DC; |
| 11327 | while (LookupDC->isTransparentContext()) |
| 11328 | LookupDC = LookupDC->getParent(); |
| 11329 | |
| 11330 | while (true) { |
| 11331 | LookupQualifiedName(Previous, LookupDC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11332 | |
Rafael Espindola | 11dc634 | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11333 | if (!Previous.empty()) { |
| 11334 | DC = LookupDC; |
| 11335 | break; |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 11336 | } |
Rafael Espindola | 11dc634 | 2013-04-25 20:12:36 +0000 | [diff] [blame] | 11337 | |
| 11338 | if (isTemplateId) { |
| 11339 | if (isa<TranslationUnitDecl>(LookupDC)) break; |
| 11340 | } else { |
| 11341 | if (LookupDC->isFileContext()) break; |
| 11342 | } |
| 11343 | LookupDC = LookupDC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11344 | } |
| 11345 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 11346 | DCScope = getScopeForDeclContext(S, DC); |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11347 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11348 | // - There's a non-dependent scope specifier, in which case we |
| 11349 | // compute it and do a previous lookup there for a function |
| 11350 | // or function template. |
| 11351 | } else if (!SS.getScopeRep()->isDependent()) { |
| 11352 | DC = computeDeclContext(SS); |
| 11353 | if (!DC) return 0; |
| 11354 | |
| 11355 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 11356 | |
| 11357 | LookupQualifiedName(Previous, DC); |
| 11358 | |
| 11359 | // Ignore things found implicitly in the wrong scope. |
| 11360 | // TODO: better diagnostics for this case. Suggesting the right |
| 11361 | // qualified scope would be nice... |
| 11362 | LookupResult::Filter F = Previous.makeFilter(); |
| 11363 | while (F.hasNext()) { |
| 11364 | NamedDecl *D = F.next(); |
| 11365 | if (!DC->InEnclosingNamespaceSetOf( |
| 11366 | D->getDeclContext()->getRedeclContext())) |
| 11367 | F.erase(); |
| 11368 | } |
| 11369 | F.done(); |
| 11370 | |
| 11371 | if (Previous.empty()) { |
| 11372 | D.setInvalidType(); |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11373 | Diag(Loc, diag::err_qualified_friend_not_found) |
| 11374 | << Name << TInfo->getType(); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11375 | return 0; |
| 11376 | } |
| 11377 | |
| 11378 | // C++ [class.friend]p1: A friend of a class is a function or |
| 11379 | // class that is not a member of the class . . . |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 11380 | if (DC->Equals(CurContext)) |
| 11381 | Diag(DS.getFriendSpecLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11382 | getLangOpts().CPlusPlus11 ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 11383 | diag::warn_cxx98_compat_friend_is_member : |
| 11384 | diag::err_friend_is_member); |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11385 | |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11386 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11387 | // C++ [class.friend]p6: |
| 11388 | // A function can be defined in a friend declaration of a class if and |
| 11389 | // only if the class is a non-local class (9.8), the function name is |
| 11390 | // unqualified, and the function has namespace scope. |
| 11391 | SemaDiagnosticBuilder DB |
| 11392 | = Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def); |
| 11393 | |
| 11394 | DB << SS.getScopeRep(); |
| 11395 | if (DC->isFileContext()) |
| 11396 | DB << FixItHint::CreateRemoval(SS.getRange()); |
| 11397 | SS.clear(); |
| 11398 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11399 | |
| 11400 | // - There's a scope specifier that does not match any template |
| 11401 | // parameter lists, in which case we use some arbitrary context, |
| 11402 | // create a method or method template, and wait for instantiation. |
| 11403 | // - There's a scope specifier that does match some template |
| 11404 | // parameter lists, which we don't handle right now. |
| 11405 | } else { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11406 | if (D.isFunctionDefinition()) { |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11407 | // C++ [class.friend]p6: |
| 11408 | // A function can be defined in a friend declaration of a class if and |
| 11409 | // only if the class is a non-local class (9.8), the function name is |
| 11410 | // unqualified, and the function has namespace scope. |
| 11411 | Diag(SS.getRange().getBegin(), diag::err_qualified_friend_def) |
| 11412 | << SS.getScopeRep(); |
| 11413 | } |
| 11414 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 11415 | DC = CurContext; |
| 11416 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11417 | } |
Douglas Gregor | 883af83 | 2011-10-10 01:11:59 +0000 | [diff] [blame] | 11418 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 11419 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11420 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 11421 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 11422 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 11423 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11424 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 11425 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 11426 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11427 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11428 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 11429 | } |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11430 | |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 11431 | // FIXME: This is an egregious hack to cope with cases where the scope stack |
| 11432 | // does not contain the declaration context, i.e., in an out-of-line |
| 11433 | // definition of a class. |
| 11434 | Scope FakeDCScope(S, Scope::DeclScope, Diags); |
| 11435 | if (!DCScope) { |
| 11436 | FakeDCScope.setEntity(DC); |
| 11437 | DCScope = &FakeDCScope; |
| 11438 | } |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11439 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 11440 | bool AddToScope = true; |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 11441 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, TInfo, Previous, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 11442 | TemplateParams, AddToScope); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 11443 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 11444 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 11445 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 11446 | |
Richard Smith | 4e9686b | 2013-08-09 04:35:01 +0000 | [diff] [blame] | 11447 | // If we performed typo correction, we might have added a scope specifier |
| 11448 | // and changed the decl context. |
| 11449 | DC = ND->getDeclContext(); |
| 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 | } |