Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 1 | //===------ SemaDeclCXX.cpp - Semantic Analysis for C++ Declarations ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 15 | #include "clang/Sema/CXXFieldCollector.h" |
| 16 | #include "clang/Sema/Scope.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Initialization.h" |
| 18 | #include "clang/Sema/Lookup.h" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 21 | #include "clang/AST/CharUnits.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclVisitor.h" |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 25 | #include "clang/AST/RecordLayout.h" |
| 26 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeOrdering.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 29 | #include "clang/Sema/DeclSpec.h" |
| 30 | #include "clang/Sema/ParsedTemplate.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 31 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 32 | #include "clang/Lex/Preprocessor.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 35 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 36 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace clang; |
| 39 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | // CheckDefaultArgumentVisitor |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 44 | namespace { |
| 45 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 46 | /// the default argument of a parameter to determine whether it |
| 47 | /// contains any ill-formed subexpressions. For example, this will |
| 48 | /// diagnose the use of local variables or parameters within the |
| 49 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 50 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 51 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 52 | Expr *DefaultArg; |
| 53 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 55 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 57 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 59 | bool VisitExpr(Expr *Node); |
| 60 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 61 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 62 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 64 | /// VisitExpr - Visit all of the children of this expression. |
| 65 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 66 | bool IsInvalid = false; |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 67 | for (Stmt::child_range I = Node->children(); I; ++I) |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 68 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 69 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 72 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 73 | /// determine whether this declaration can be used in the default |
| 74 | /// argument expression. |
| 75 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 76 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 77 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 78 | // C++ [dcl.fct.default]p9 |
| 79 | // Default arguments are evaluated each time the function is |
| 80 | // called. The order of evaluation of function arguments is |
| 81 | // unspecified. Consequently, parameters of a function shall not |
| 82 | // be used in default argument expressions, even if they are not |
| 83 | // evaluated. Parameters of a function declared before a default |
| 84 | // argument expression are in scope and can hide namespace and |
| 85 | // class member names. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 87 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 88 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 89 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 90 | // C++ [dcl.fct.default]p7 |
| 91 | // Local variables shall not be used in default argument |
| 92 | // expressions. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 93 | if (VDecl->isLocalVarDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 95 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 96 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 97 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 99 | return false; |
| 100 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 102 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 103 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 104 | // C++ [dcl.fct.default]p8: |
| 105 | // The keyword this shall not be used in a default argument of a |
| 106 | // member function. |
| 107 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 108 | diag::err_param_default_argument_references_this) |
| 109 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 110 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 113 | bool |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 114 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | SourceLocation EqualLoc) { |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 116 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 117 | diag::err_typecheck_decl_incomplete_type)) { |
| 118 | Param->setInvalidDecl(); |
| 119 | return true; |
| 120 | } |
| 121 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 122 | // C++ [dcl.fct.default]p5 |
| 123 | // A default argument expression is implicitly converted (clause |
| 124 | // 4) to the parameter type. The default argument expression has |
| 125 | // the same semantic constraints as the initializer expression in |
| 126 | // a declaration of a variable of the parameter type, using the |
| 127 | // copy-initialization semantics (8.5). |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 128 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 129 | Param); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 130 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 131 | EqualLoc); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 132 | InitializationSequence InitSeq(*this, Entity, Kind, &Arg, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 133 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
Nico Weber | 6bb4dcb | 2010-11-28 22:53:37 +0000 | [diff] [blame] | 134 | MultiExprArg(*this, &Arg, 1)); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 135 | if (Result.isInvalid()) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 136 | return true; |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 137 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 138 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 139 | CheckImplicitConversions(Arg, EqualLoc); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 140 | Arg = MaybeCreateExprWithCleanups(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 142 | // Okay: add the default argument to the parameter |
| 143 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Douglas Gregor | 8cfb7a3 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 145 | // We have already instantiated this parameter; provide each of the |
| 146 | // instantiations with the uninstantiated default argument. |
| 147 | UnparsedDefaultArgInstantiationsMap::iterator InstPos |
| 148 | = UnparsedDefaultArgInstantiations.find(Param); |
| 149 | if (InstPos != UnparsedDefaultArgInstantiations.end()) { |
| 150 | for (unsigned I = 0, N = InstPos->second.size(); I != N; ++I) |
| 151 | InstPos->second[I]->setUninstantiatedDefaultArg(Arg); |
| 152 | |
| 153 | // We're done tracking this parameter's instantiations. |
| 154 | UnparsedDefaultArgInstantiations.erase(InstPos); |
| 155 | } |
| 156 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 157 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 160 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 161 | /// provided for a function parameter is well-formed. If so, attach it |
| 162 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 163 | void |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 164 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 165 | Expr *DefaultArg) { |
| 166 | if (!param || !DefaultArg) |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 167 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 169 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 170 | UnparsedDefaultArgLocs.erase(Param); |
| 171 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 172 | // Default arguments are only permitted in C++ |
| 173 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 174 | Diag(EqualLoc, diag::err_param_default_argument) |
| 175 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 176 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 180 | // Check for unexpanded parameter packs. |
| 181 | if (DiagnoseUnexpandedParameterPack(DefaultArg, UPPC_DefaultArgument)) { |
| 182 | Param->setInvalidDecl(); |
| 183 | return; |
| 184 | } |
| 185 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 186 | // Check that the default argument is well-formed |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 187 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 188 | if (DefaultArgChecker.Visit(DefaultArg)) { |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 189 | Param->setInvalidDecl(); |
| 190 | return; |
| 191 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 193 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 196 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 197 | /// argument for a function parameter, but we can't parse it yet |
| 198 | /// because we're inside a class definition. Note that this default |
| 199 | /// argument will be parsed later. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 200 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 201 | SourceLocation EqualLoc, |
| 202 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 203 | if (!param) |
| 204 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 206 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 207 | if (Param) |
| 208 | Param->setUnparsedDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 210 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 213 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 214 | /// the default argument for the parameter param failed. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 215 | void Sema::ActOnParamDefaultArgumentError(Decl *param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 216 | if (!param) |
| 217 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 219 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 221 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 223 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 226 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 227 | /// arguments in the declarator, which is not a function declaration |
| 228 | /// or definition and therefore is not permitted to have default |
| 229 | /// arguments. This routine should be invoked for every declarator |
| 230 | /// that is not a function declaration or definition. |
| 231 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 232 | // C++ [dcl.fct.default]p3 |
| 233 | // A default argument expression shall be specified only in the |
| 234 | // parameter-declaration-clause of a function declaration or in a |
| 235 | // template-parameter (14.1). It shall not be specified for a |
| 236 | // parameter pack. If it is specified in a |
| 237 | // parameter-declaration-clause, it shall not occur within a |
| 238 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 239 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 240 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 241 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 242 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 243 | ParmVarDecl *Param = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 244 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 245 | if (Param->hasUnparsedDefaultArg()) { |
| 246 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 247 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 248 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 249 | delete Toks; |
| 250 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 251 | } else if (Param->getDefaultArg()) { |
| 252 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 253 | << Param->getDefaultArg()->getSourceRange(); |
| 254 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 261 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 262 | // function, once we already know that they have the same |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 263 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 264 | // error, false otherwise. |
| 265 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 266 | bool Invalid = false; |
| 267 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 268 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 269 | // For non-template functions, default arguments can be added in |
| 270 | // later declarations of a function in the same |
| 271 | // scope. Declarations in different scopes have completely |
| 272 | // distinct sets of default arguments. That is, declarations in |
| 273 | // inner scopes do not acquire default arguments from |
| 274 | // declarations in outer scopes, and vice versa. In a given |
| 275 | // function declaration, all parameters subsequent to a |
| 276 | // parameter with a default argument shall have default |
| 277 | // arguments supplied in this or previous declarations. A |
| 278 | // default argument shall not be redefined by a later |
| 279 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 280 | // |
| 281 | // C++ [dcl.fct.default]p6: |
| 282 | // Except for member functions of class templates, the default arguments |
| 283 | // in a member function definition that appears outside of the class |
| 284 | // definition are added to the set of default arguments provided by the |
| 285 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 286 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 287 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 288 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 289 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 290 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 291 | |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 292 | unsigned DiagDefaultParamID = |
| 293 | diag::err_param_default_argument_redefinition; |
| 294 | |
| 295 | // MSVC accepts that default parameters be redefined for member functions |
| 296 | // of template class. The new default parameter's value is ignored. |
| 297 | Invalid = true; |
| 298 | if (getLangOptions().Microsoft) { |
| 299 | CXXMethodDecl* MD = dyn_cast<CXXMethodDecl>(New); |
| 300 | if (MD && MD->getParent()->getDescribedClassTemplate()) { |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 301 | // Merge the old default argument into the new parameter. |
| 302 | NewParam->setHasInheritedDefaultArg(); |
| 303 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 304 | NewParam->setUninstantiatedDefaultArg( |
| 305 | OldParam->getUninstantiatedDefaultArg()); |
| 306 | else |
| 307 | NewParam->setDefaultArg(OldParam->getInit()); |
Francois Pichet | cf320c6 | 2011-04-22 08:25:24 +0000 | [diff] [blame^] | 308 | DiagDefaultParamID = diag::warn_param_default_argument_redefinition; |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 309 | Invalid = false; |
| 310 | } |
| 311 | } |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 312 | |
Francois Pichet | 8cf9049 | 2011-04-10 04:58:30 +0000 | [diff] [blame] | 313 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 314 | // hint here. Alternatively, we could walk the type-source information |
| 315 | // for NewParam to find the last source location in the type... but it |
| 316 | // isn't worth the effort right now. This is the kind of test case that |
| 317 | // is hard to get right: |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 318 | // int f(int); |
| 319 | // void g(int (*fp)(int) = f); |
| 320 | // void g(int (*fp)(int) = &f); |
Francois Pichet | 8d051e0 | 2011-04-10 03:03:52 +0000 | [diff] [blame] | 321 | Diag(NewParam->getLocation(), DiagDefaultParamID) |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 322 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 323 | |
| 324 | // Look for the function declaration where the default argument was |
| 325 | // actually written, which may be a declaration prior to Old. |
| 326 | for (FunctionDecl *Older = Old->getPreviousDeclaration(); |
| 327 | Older; Older = Older->getPreviousDeclaration()) { |
| 328 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 329 | break; |
| 330 | |
| 331 | OldParam = Older->getParamDecl(p); |
| 332 | } |
| 333 | |
| 334 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 335 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 336 | } else if (OldParam->hasDefaultArg()) { |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 337 | // Merge the old default argument into the new parameter. |
| 338 | // It's important to use getInit() here; getDefaultArg() |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 339 | // strips off any top-level ExprWithCleanups. |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 340 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 341 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 342 | NewParam->setUninstantiatedDefaultArg( |
| 343 | OldParam->getUninstantiatedDefaultArg()); |
| 344 | else |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 345 | NewParam->setDefaultArg(OldParam->getInit()); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 346 | } else if (NewParam->hasDefaultArg()) { |
| 347 | if (New->getDescribedFunctionTemplate()) { |
| 348 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 349 | Diag(NewParam->getLocation(), |
| 350 | diag::err_param_default_argument_template_redecl) |
| 351 | << NewParam->getDefaultArgRange(); |
| 352 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 353 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 354 | } else if (New->getTemplateSpecializationKind() |
| 355 | != TSK_ImplicitInstantiation && |
| 356 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 357 | // C++ [temp.expr.spec]p21: |
| 358 | // Default function arguments shall not be specified in a declaration |
| 359 | // or a definition for one of the following explicit specializations: |
| 360 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 361 | // - the explicit specialization of a member function template; |
| 362 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 363 | // template where the class template specialization to which the |
| 364 | // member function specialization belongs is implicitly |
| 365 | // instantiated. |
| 366 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 367 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 368 | << New->getDeclName() |
| 369 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 370 | } else if (New->getDeclContext()->isDependentContext()) { |
| 371 | // C++ [dcl.fct.default]p6 (DR217): |
| 372 | // Default arguments for a member function of a class template shall |
| 373 | // be specified on the initial declaration of the member function |
| 374 | // within the class template. |
| 375 | // |
| 376 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 377 | // leads me to the conclusion that one cannot add default function |
| 378 | // arguments for an out-of-line definition of a member function of a |
| 379 | // dependent type. |
| 380 | int WhichKind = 2; |
| 381 | if (CXXRecordDecl *Record |
| 382 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 383 | if (Record->getDescribedClassTemplate()) |
| 384 | WhichKind = 0; |
| 385 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 386 | WhichKind = 1; |
| 387 | else |
| 388 | WhichKind = 2; |
| 389 | } |
| 390 | |
| 391 | Diag(NewParam->getLocation(), |
| 392 | diag::err_param_default_argument_member_template_redecl) |
| 393 | << WhichKind |
| 394 | << NewParam->getDefaultArgRange(); |
| 395 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 399 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 400 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 401 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 402 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 405 | /// \brief Merge the exception specifications of two variable declarations. |
| 406 | /// |
| 407 | /// This is called when there's a redeclaration of a VarDecl. The function |
| 408 | /// checks if the redeclaration might have an exception specification and |
| 409 | /// validates compatibility and merges the specs if necessary. |
| 410 | void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) { |
| 411 | // Shortcut if exceptions are disabled. |
| 412 | if (!getLangOptions().CXXExceptions) |
| 413 | return; |
| 414 | |
| 415 | assert(Context.hasSameType(New->getType(), Old->getType()) && |
| 416 | "Should only be called if types are otherwise the same."); |
| 417 | |
| 418 | QualType NewType = New->getType(); |
| 419 | QualType OldType = Old->getType(); |
| 420 | |
| 421 | // We're only interested in pointers and references to functions, as well |
| 422 | // as pointers to member functions. |
| 423 | if (const ReferenceType *R = NewType->getAs<ReferenceType>()) { |
| 424 | NewType = R->getPointeeType(); |
| 425 | OldType = OldType->getAs<ReferenceType>()->getPointeeType(); |
| 426 | } else if (const PointerType *P = NewType->getAs<PointerType>()) { |
| 427 | NewType = P->getPointeeType(); |
| 428 | OldType = OldType->getAs<PointerType>()->getPointeeType(); |
| 429 | } else if (const MemberPointerType *M = NewType->getAs<MemberPointerType>()) { |
| 430 | NewType = M->getPointeeType(); |
| 431 | OldType = OldType->getAs<MemberPointerType>()->getPointeeType(); |
| 432 | } |
| 433 | |
| 434 | if (!NewType->isFunctionProtoType()) |
| 435 | return; |
| 436 | |
| 437 | // There's lots of special cases for functions. For function pointers, system |
| 438 | // libraries are hopefully not as broken so that we don't need these |
| 439 | // workarounds. |
| 440 | if (CheckEquivalentExceptionSpec( |
| 441 | OldType->getAs<FunctionProtoType>(), Old->getLocation(), |
| 442 | NewType->getAs<FunctionProtoType>(), New->getLocation())) { |
| 443 | New->setInvalidDecl(); |
| 444 | } |
| 445 | } |
| 446 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 447 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 448 | /// function declaration are well-formed according to C++ |
| 449 | /// [dcl.fct.default]. |
| 450 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 451 | unsigned NumParams = FD->getNumParams(); |
| 452 | unsigned p; |
| 453 | |
| 454 | // Find first parameter with a default argument |
| 455 | for (p = 0; p < NumParams; ++p) { |
| 456 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 457 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 458 | break; |
| 459 | } |
| 460 | |
| 461 | // C++ [dcl.fct.default]p4: |
| 462 | // In a given function declaration, all parameters |
| 463 | // subsequent to a parameter with a default argument shall |
| 464 | // have default arguments supplied in this or previous |
| 465 | // declarations. A default argument shall not be redefined |
| 466 | // by a later declaration (not even to the same value). |
| 467 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 469 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 470 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 471 | if (Param->isInvalidDecl()) |
| 472 | /* We already complained about this parameter. */; |
| 473 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 475 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 476 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 477 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 479 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 481 | LastMissingDefaultArg = p; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | if (LastMissingDefaultArg > 0) { |
| 486 | // Some default arguments were missing. Clear out all of the |
| 487 | // default arguments up to (and including) the last missing |
| 488 | // default argument, so that we leave the function parameters |
| 489 | // in a semantically valid state. |
| 490 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 491 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 492 | if (Param->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 493 | Param->setDefaultArg(0); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 498 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 499 | /// isCurrentClassName - Determine whether the identifier II is the |
| 500 | /// name of the class type currently being defined. In the case of |
| 501 | /// nested classes, this will only return true if II is the name of |
| 502 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 503 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 504 | const CXXScopeSpec *SS) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 505 | assert(getLangOptions().CPlusPlus && "No class names in C!"); |
| 506 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 507 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 508 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 509 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 510 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 511 | } else |
| 512 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 513 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 514 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 515 | return &II == CurDecl->getIdentifier(); |
| 516 | else |
| 517 | return false; |
| 518 | } |
| 519 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 521 | /// |
| 522 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 523 | /// and returns NULL otherwise. |
| 524 | CXXBaseSpecifier * |
| 525 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 526 | SourceRange SpecifierRange, |
| 527 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 528 | TypeSourceInfo *TInfo, |
| 529 | SourceLocation EllipsisLoc) { |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 530 | QualType BaseType = TInfo->getType(); |
| 531 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 532 | // C++ [class.union]p1: |
| 533 | // A union shall not have base classes. |
| 534 | if (Class->isUnion()) { |
| 535 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 536 | << SpecifierRange; |
| 537 | return 0; |
| 538 | } |
| 539 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 540 | if (EllipsisLoc.isValid() && |
| 541 | !TInfo->getType()->containsUnexpandedParameterPack()) { |
| 542 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 543 | << TInfo->getTypeLoc().getSourceRange(); |
| 544 | EllipsisLoc = SourceLocation(); |
| 545 | } |
| 546 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 547 | if (BaseType->isDependentType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 549 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 550 | Access, TInfo, EllipsisLoc); |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 551 | |
| 552 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 553 | |
| 554 | // Base specifiers must be record types. |
| 555 | if (!BaseType->isRecordType()) { |
| 556 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | // C++ [class.union]p1: |
| 561 | // A union shall not be used as a base class. |
| 562 | if (BaseType->isUnionType()) { |
| 563 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | // C++ [class.derived]p2: |
| 568 | // The class-name in a base-specifier shall not be an incompletely |
| 569 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 571 | PDiag(diag::err_incomplete_base_class) |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 572 | << SpecifierRange)) { |
| 573 | Class->setInvalidDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 574 | return 0; |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 575 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 576 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 577 | // 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] | 578 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 579 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 580 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 581 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 582 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 583 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 584 | |
Anders Carlsson | 1d20927 | 2011-03-25 14:55:14 +0000 | [diff] [blame] | 585 | // C++ [class]p3: |
| 586 | // If a class is marked final and it appears as a base-type-specifier in |
| 587 | // base-clause, the program is ill-formed. |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 588 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
Anders Carlsson | dfc2f10 | 2011-01-22 17:51:53 +0000 | [diff] [blame] | 589 | Diag(BaseLoc, diag::err_class_marked_final_used_as_base) |
| 590 | << CXXBaseDecl->getDeclName(); |
| 591 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 592 | << CXXBaseDecl->getDeclName(); |
| 593 | return 0; |
| 594 | } |
| 595 | |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 596 | if (BaseDecl->isInvalidDecl()) |
| 597 | Class->setInvalidDecl(); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 598 | |
| 599 | // Create the base specifier. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 600 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 601 | Class->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 602 | Access, TInfo, EllipsisLoc); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 605 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 606 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | /// example: |
| 608 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 609 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 610 | BaseResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 611 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 612 | bool Virtual, AccessSpecifier Access, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 613 | ParsedType basetype, SourceLocation BaseLoc, |
| 614 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 615 | if (!classdecl) |
| 616 | return true; |
| 617 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 618 | AdjustDeclIfTemplate(classdecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 619 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 620 | if (!Class) |
| 621 | return true; |
| 622 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 623 | TypeSourceInfo *TInfo = 0; |
| 624 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 625 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 626 | if (EllipsisLoc.isInvalid() && |
| 627 | DiagnoseUnexpandedParameterPack(SpecifierRange.getBegin(), TInfo, |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 628 | UPPC_BaseType)) |
| 629 | return true; |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 630 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 631 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 632 | Virtual, Access, TInfo, |
| 633 | EllipsisLoc)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 634 | return BaseSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 636 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 637 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 638 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 639 | /// \brief Performs the actual work of attaching the given base class |
| 640 | /// specifiers to a C++ class. |
| 641 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 642 | unsigned NumBases) { |
| 643 | if (NumBases == 0) |
| 644 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 645 | |
| 646 | // Used to keep track of which base types we have already seen, so |
| 647 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 648 | // that the key is always the unqualified canonical type of the base |
| 649 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 650 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 651 | |
| 652 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 653 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 654 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 655 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 657 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 658 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Fariborz Jahanian | 0ed5c5d | 2010-05-20 23:34:56 +0000 | [diff] [blame] | 659 | if (!Class->hasObjectMember()) { |
| 660 | if (const RecordType *FDTTy = |
| 661 | NewBaseType.getTypePtr()->getAs<RecordType>()) |
| 662 | if (FDTTy->getDecl()->hasObjectMember()) |
| 663 | Class->setHasObjectMember(true); |
| 664 | } |
| 665 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 666 | if (KnownBaseTypes[NewBaseType]) { |
| 667 | // C++ [class.mi]p3: |
| 668 | // A class shall not be specified as a direct base class of a |
| 669 | // derived class more than once. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 670 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 671 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 672 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 673 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 674 | |
| 675 | // Delete the duplicate base class specifier; we're going to |
| 676 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 677 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 678 | |
| 679 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 680 | } else { |
| 681 | // Okay, add this new base class. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 682 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 683 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
| 687 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 688 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 689 | |
| 690 | // Delete the remaining (good) base class specifiers, since their |
| 691 | // data has been copied into the CXXRecordDecl. |
| 692 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 693 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 694 | |
| 695 | return Invalid; |
| 696 | } |
| 697 | |
| 698 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 699 | /// class, after checking whether there are any duplicate base |
| 700 | /// classes. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 701 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, BaseTy **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 702 | unsigned NumBases) { |
| 703 | if (!ClassDecl || !Bases || !NumBases) |
| 704 | return; |
| 705 | |
| 706 | AdjustDeclIfTemplate(ClassDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 707 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 708 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 709 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 710 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 711 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 712 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 713 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 714 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 715 | return ICT->getDecl(); |
| 716 | else |
| 717 | return 0; |
| 718 | } |
| 719 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 720 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 721 | /// derived from the type \p Base. |
| 722 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 723 | if (!getLangOptions().CPlusPlus) |
| 724 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 725 | |
| 726 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 727 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 728 | return false; |
| 729 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 730 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 731 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 732 | return false; |
| 733 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 734 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 735 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 739 | /// derived from the type \p Base. |
| 740 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 741 | if (!getLangOptions().CPlusPlus) |
| 742 | return false; |
| 743 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 744 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 745 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 746 | return false; |
| 747 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 748 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 749 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 750 | return false; |
| 751 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 752 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 753 | } |
| 754 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 755 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 756 | CXXCastPath &BasePathArray) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 757 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 758 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 759 | |
| 760 | const CXXBasePath &Path = Paths.front(); |
| 761 | |
| 762 | // We first go backward and check if we have a virtual base. |
| 763 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 764 | // the nearest virtual base. |
| 765 | unsigned Start = 0; |
| 766 | for (unsigned I = Path.size(); I != 0; --I) { |
| 767 | if (Path[I - 1].Base->isVirtual()) { |
| 768 | Start = I - 1; |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | // Now add all bases. |
| 774 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 775 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 778 | /// \brief Determine whether the given base path includes a virtual |
| 779 | /// base class. |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 780 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 781 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 782 | BEnd = BasePath.end(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 783 | B != BEnd; ++B) |
| 784 | if ((*B)->isVirtual()) |
| 785 | return true; |
| 786 | |
| 787 | return false; |
| 788 | } |
| 789 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 790 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 791 | /// conversion (where Derived and Base are class types) is |
| 792 | /// well-formed, meaning that the conversion is unambiguous (and |
| 793 | /// that all of the base classes are accessible). Returns true |
| 794 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 795 | /// otherwise. Loc is the location where this routine should point to |
| 796 | /// if there is an error, and Range is the source range to highlight |
| 797 | /// if there is an error. |
| 798 | bool |
| 799 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 800 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 801 | unsigned AmbigiousBaseConvID, |
| 802 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 803 | DeclarationName Name, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 804 | CXXCastPath *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 805 | // First, determine whether the path from Derived to Base is |
| 806 | // ambiguous. This is slightly more expensive than checking whether |
| 807 | // the Derived to Base conversion exists, because here we need to |
| 808 | // explore multiple paths to determine if there is an ambiguity. |
| 809 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 810 | /*DetectVirtual=*/false); |
| 811 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 812 | assert(DerivationOkay && |
| 813 | "Can only be used with a derived-to-base conversion"); |
| 814 | (void)DerivationOkay; |
| 815 | |
| 816 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 817 | if (InaccessibleBaseID) { |
| 818 | // Check that the base class can be accessed. |
| 819 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 820 | InaccessibleBaseID)) { |
| 821 | case AR_inaccessible: |
| 822 | return true; |
| 823 | case AR_accessible: |
| 824 | case AR_dependent: |
| 825 | case AR_delayed: |
| 826 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 827 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 828 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 829 | |
| 830 | // Build a base path if necessary. |
| 831 | if (BasePath) |
| 832 | BuildBasePathArray(Paths, *BasePath); |
| 833 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | // We know that the derived-to-base conversion is ambiguous, and |
| 837 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 838 | // search just one more time to compute all of the possible paths so |
| 839 | // that we can print them out. This is more expensive than any of |
| 840 | // the previous derived-to-base checks we've done, but at this point |
| 841 | // performance isn't as much of an issue. |
| 842 | Paths.clear(); |
| 843 | Paths.setRecordingPaths(true); |
| 844 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 845 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 846 | (void)StillOkay; |
| 847 | |
| 848 | // Build up a textual representation of the ambiguous paths, e.g., |
| 849 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 850 | // conversions in the diagnostic. We only print one of the paths |
| 851 | // to each base class subobject. |
| 852 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 853 | |
| 854 | Diag(Loc, AmbigiousBaseConvID) |
| 855 | << Derived << Base << PathDisplayStr << Range << Name; |
| 856 | return true; |
| 857 | } |
| 858 | |
| 859 | bool |
| 860 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 861 | SourceLocation Loc, SourceRange Range, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 862 | CXXCastPath *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 863 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 864 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 865 | IgnoreAccess ? 0 |
| 866 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 867 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 868 | Loc, Range, DeclarationName(), |
| 869 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | |
| 873 | /// @brief Builds a string representing ambiguous paths from a |
| 874 | /// specific derived class to different subobjects of the same base |
| 875 | /// class. |
| 876 | /// |
| 877 | /// This function builds a string that can be used in error messages |
| 878 | /// to show the different paths that one can take through the |
| 879 | /// inheritance hierarchy to go from the derived class to different |
| 880 | /// subobjects of a base class. The result looks something like this: |
| 881 | /// @code |
| 882 | /// struct D -> struct B -> struct A |
| 883 | /// struct D -> struct C -> struct A |
| 884 | /// @endcode |
| 885 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 886 | std::string PathDisplayStr; |
| 887 | std::set<unsigned> DisplayedPaths; |
| 888 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 889 | Path != Paths.end(); ++Path) { |
| 890 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 891 | // We haven't displayed a path to this particular base |
| 892 | // class subobject yet. |
| 893 | PathDisplayStr += "\n "; |
| 894 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 895 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 896 | Element != Path->end(); ++Element) |
| 897 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | return PathDisplayStr; |
| 902 | } |
| 903 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 904 | //===----------------------------------------------------------------------===// |
| 905 | // C++ class member Handling |
| 906 | //===----------------------------------------------------------------------===// |
| 907 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 908 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 909 | Decl *Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 910 | SourceLocation ASLoc, |
| 911 | SourceLocation ColonLoc) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 912 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 913 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 914 | ASLoc, ColonLoc); |
| 915 | CurContext->addHiddenDecl(ASDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 916 | return ASDecl; |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 919 | /// CheckOverrideControl - Check C++0x override control semantics. |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 920 | void Sema::CheckOverrideControl(const Decl *D) { |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 921 | const CXXMethodDecl *MD = llvm::dyn_cast<CXXMethodDecl>(D); |
| 922 | if (!MD || !MD->isVirtual()) |
| 923 | return; |
| 924 | |
Anders Carlsson | 3ffe183 | 2011-01-20 06:33:26 +0000 | [diff] [blame] | 925 | if (MD->isDependentContext()) |
| 926 | return; |
| 927 | |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 928 | // C++0x [class.virtual]p3: |
| 929 | // If a virtual function is marked with the virt-specifier override and does |
| 930 | // not override a member function of a base class, |
| 931 | // the program is ill-formed. |
| 932 | bool HasOverriddenMethods = |
| 933 | MD->begin_overridden_methods() != MD->end_overridden_methods(); |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 934 | if (MD->hasAttr<OverrideAttr>() && !HasOverriddenMethods) { |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 935 | Diag(MD->getLocation(), |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 936 | diag::err_function_marked_override_not_overriding) |
| 937 | << MD->getDeclName(); |
| 938 | return; |
| 939 | } |
| 940 | } |
| 941 | |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 942 | /// CheckIfOverriddenFunctionIsMarkedFinal - Checks whether a virtual member |
| 943 | /// function overrides a virtual member function marked 'final', according to |
| 944 | /// C++0x [class.virtual]p3. |
| 945 | bool Sema::CheckIfOverriddenFunctionIsMarkedFinal(const CXXMethodDecl *New, |
| 946 | const CXXMethodDecl *Old) { |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 947 | if (!Old->hasAttr<FinalAttr>()) |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 948 | return false; |
| 949 | |
| 950 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 951 | << New->getDeclName(); |
| 952 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 953 | return true; |
Anders Carlsson | 2e1c730 | 2011-01-20 16:25:36 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 956 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 957 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 958 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
Chris Lattner | b6688e0 | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 959 | /// any. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 960 | Decl * |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 961 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 962 | MultiTemplateParamsArg TemplateParameterLists, |
Anders Carlsson | 69a8735 | 2011-01-20 03:57:25 +0000 | [diff] [blame] | 963 | ExprTy *BW, const VirtSpecifiers &VS, |
| 964 | ExprTy *InitExpr, bool IsDefinition, |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 965 | bool Deleted) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 966 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 967 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 968 | DeclarationName Name = NameInfo.getName(); |
| 969 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 970 | |
| 971 | // For anonymous bitfields, the location should point to the type. |
| 972 | if (Loc.isInvalid()) |
| 973 | Loc = D.getSourceRange().getBegin(); |
| 974 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 975 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 976 | Expr *Init = static_cast<Expr*>(InitExpr); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 977 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 978 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 979 | assert(!DS.isFriendSpecified()); |
| 980 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 981 | bool isFunc = false; |
| 982 | if (D.isFunctionDeclarator()) |
| 983 | isFunc = true; |
| 984 | else if (D.getNumTypeObjects() == 0 && |
| 985 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 986 | QualType TDType = GetTypeFromParser(DS.getRepAsType()); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 987 | isFunc = TDType->isFunctionType(); |
| 988 | } |
| 989 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 990 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 991 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 992 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 993 | // data members and cannot be applied to names declared const or static, |
| 994 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 995 | switch (DS.getStorageClassSpec()) { |
| 996 | case DeclSpec::SCS_unspecified: |
| 997 | case DeclSpec::SCS_typedef: |
| 998 | case DeclSpec::SCS_static: |
| 999 | // FALL THROUGH. |
| 1000 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1001 | case DeclSpec::SCS_mutable: |
| 1002 | if (isFunc) { |
| 1003 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1004 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1005 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1006 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 1008 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 1009 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1010 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1011 | } |
| 1012 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1013 | default: |
| 1014 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1015 | Diag(DS.getStorageClassSpecLoc(), |
| 1016 | diag::err_storageclass_invalid_for_member); |
| 1017 | else |
| 1018 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 1019 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 1020 | } |
| 1021 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1022 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 1023 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 1024 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1025 | |
| 1026 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1027 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 1028 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 1029 | |
| 1030 | |
| 1031 | if (SS.isSet() && !SS.isInvalid()) { |
| 1032 | // The user provided a superfluous scope specifier inside a class |
| 1033 | // definition: |
| 1034 | // |
| 1035 | // class X { |
| 1036 | // int X::member; |
| 1037 | // }; |
| 1038 | DeclContext *DC = 0; |
| 1039 | if ((DC = computeDeclContext(SS, false)) && DC->Equals(CurContext)) |
| 1040 | Diag(D.getIdentifierLoc(), diag::warn_member_extra_qualification) |
| 1041 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
| 1042 | else |
| 1043 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 1044 | << Name << SS.getRange(); |
| 1045 | |
| 1046 | SS.clear(); |
| 1047 | } |
| 1048 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1049 | // FIXME: Check for template parameters! |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 1050 | // FIXME: Check that the name is an identifier! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1051 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 1052 | AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1053 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1054 | } else { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1055 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1056 | if (!Member) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1057 | return 0; |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 1058 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1059 | |
| 1060 | // Non-instance-fields can't have a bitfield. |
| 1061 | if (BitWidth) { |
| 1062 | if (Member->isInvalidDecl()) { |
| 1063 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 1064 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1065 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 1066 | // "static member 'A' cannot be a bit-field" |
| 1067 | Diag(Loc, diag::err_static_not_bitfield) |
| 1068 | << Name << BitWidth->getSourceRange(); |
| 1069 | } else if (isa<TypedefDecl>(Member)) { |
| 1070 | // "typedef member 'x' cannot be a bit-field" |
| 1071 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 1072 | << Name << BitWidth->getSourceRange(); |
| 1073 | } else { |
| 1074 | // A function typedef ("typedef int f(); f a;"). |
| 1075 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 1076 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 1078 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1079 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 1081 | BitWidth = 0; |
| 1082 | Member->setInvalidDecl(); |
| 1083 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 1084 | |
| 1085 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 1087 | // If we have declared a member function template, set the access of the |
| 1088 | // templated declaration as well. |
| 1089 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 1090 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 1091 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1092 | |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1093 | if (VS.isOverrideSpecified()) { |
| 1094 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 1095 | if (!MD || !MD->isVirtual()) { |
| 1096 | Diag(Member->getLocStart(), |
| 1097 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1098 | << "override" << FixItHint::CreateRemoval(VS.getOverrideLoc()); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1099 | } else |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1100 | MD->addAttr(new (Context) OverrideAttr(VS.getOverrideLoc(), Context)); |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1101 | } |
| 1102 | if (VS.isFinalSpecified()) { |
| 1103 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member); |
| 1104 | if (!MD || !MD->isVirtual()) { |
| 1105 | Diag(Member->getLocStart(), |
| 1106 | diag::override_keyword_only_allowed_on_virtual_member_functions) |
| 1107 | << "final" << FixItHint::CreateRemoval(VS.getFinalLoc()); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1108 | } else |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 1109 | MD->addAttr(new (Context) FinalAttr(VS.getFinalLoc(), Context)); |
Anders Carlsson | aae5af2 | 2011-01-20 04:34:22 +0000 | [diff] [blame] | 1110 | } |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1111 | |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1112 | if (VS.getLastLocation().isValid()) { |
| 1113 | // Update the end location of a method that has a virt-specifiers. |
| 1114 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Member)) |
| 1115 | MD->setRangeEnd(VS.getLastLocation()); |
| 1116 | } |
| 1117 | |
Anders Carlsson | 4ebf160 | 2011-01-20 06:29:02 +0000 | [diff] [blame] | 1118 | CheckOverrideControl(Member); |
Anders Carlsson | 9e682d9 | 2011-01-20 05:57:14 +0000 | [diff] [blame] | 1119 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 1120 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 1122 | if (Init) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1123 | AddInitializerToDecl(Member, Init, false, |
| 1124 | DS.getTypeSpecType() == DeclSpec::TST_auto); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1125 | if (Deleted) // FIXME: Source location is not very good. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1126 | SetDeclDeleted(Member, D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1127 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1128 | FinalizeDeclaration(Member); |
| 1129 | |
John McCall | b25b295 | 2011-02-15 07:12:36 +0000 | [diff] [blame] | 1130 | if (isInstField) |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1131 | FieldCollector->Add(cast<FieldDecl>(Member)); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1132 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1135 | /// \brief Find the direct and/or virtual base specifiers that |
| 1136 | /// correspond to the given base type, for use in base initialization |
| 1137 | /// within a constructor. |
| 1138 | static bool FindBaseInitializer(Sema &SemaRef, |
| 1139 | CXXRecordDecl *ClassDecl, |
| 1140 | QualType BaseType, |
| 1141 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 1142 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 1143 | // First, check for a direct base class. |
| 1144 | DirectBaseSpec = 0; |
| 1145 | for (CXXRecordDecl::base_class_const_iterator Base |
| 1146 | = ClassDecl->bases_begin(); |
| 1147 | Base != ClassDecl->bases_end(); ++Base) { |
| 1148 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 1149 | // We found a direct base of this type. That's what we're |
| 1150 | // initializing. |
| 1151 | DirectBaseSpec = &*Base; |
| 1152 | break; |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | // Check for a virtual base class. |
| 1157 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1158 | // there are no virtual bases. |
| 1159 | VirtualBaseSpec = 0; |
| 1160 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1161 | // We haven't found a base yet; search the class hierarchy for a |
| 1162 | // virtual base class. |
| 1163 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1164 | /*DetectVirtual=*/false); |
| 1165 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1166 | BaseType, Paths)) { |
| 1167 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1168 | Path != Paths.end(); ++Path) { |
| 1169 | if (Path->back().Base->isVirtual()) { |
| 1170 | VirtualBaseSpec = Path->back().Base; |
| 1171 | break; |
| 1172 | } |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | return DirectBaseSpec || VirtualBaseSpec; |
| 1178 | } |
| 1179 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1180 | /// ActOnMemInitializer - Handle a C++ member initializer. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1181 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1182 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1183 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1184 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1185 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1186 | ParsedType TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1187 | SourceLocation IdLoc, |
| 1188 | SourceLocation LParenLoc, |
| 1189 | ExprTy **Args, unsigned NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1190 | SourceLocation RParenLoc, |
| 1191 | SourceLocation EllipsisLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1192 | if (!ConstructorD) |
| 1193 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1195 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | |
| 1197 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1198 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1199 | if (!Constructor) { |
| 1200 | // The user wrote a constructor initializer on a function that is |
| 1201 | // not a C++ constructor. Ignore the error for now, because we may |
| 1202 | // have more member initializers coming; we'll diagnose it just |
| 1203 | // once in ActOnMemInitializers. |
| 1204 | return true; |
| 1205 | } |
| 1206 | |
| 1207 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1208 | |
| 1209 | // C++ [class.base.init]p2: |
| 1210 | // 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] | 1211 | // constructor's class and, if not found in that scope, are looked |
| 1212 | // up in the scope containing the constructor's definition. |
| 1213 | // [Note: if the constructor's class contains a member with the |
| 1214 | // same name as a direct or virtual base class of the class, a |
| 1215 | // mem-initializer-id naming the member or base class and composed |
| 1216 | // of a single identifier refers to the class member. A |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1217 | // mem-initializer-id for the hidden base class may be specified |
| 1218 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1219 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1220 | // Look for a member, first. |
| 1221 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1223 | = ClassDecl->lookup(MemberOrBase); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1224 | if (Result.first != Result.second) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1225 | Member = dyn_cast<FieldDecl>(*Result.first); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1226 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1227 | if (Member) { |
| 1228 | if (EllipsisLoc.isValid()) |
| 1229 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
| 1230 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1231 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1232 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1233 | LParenLoc, RParenLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1236 | // Handle anonymous union case. |
| 1237 | if (IndirectFieldDecl* IndirectField |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1238 | = dyn_cast<IndirectFieldDecl>(*Result.first)) { |
| 1239 | if (EllipsisLoc.isValid()) |
| 1240 | Diag(EllipsisLoc, diag::err_pack_expansion_member_init) |
| 1241 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1242 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1243 | return BuildMemberInitializer(IndirectField, (Expr**)Args, |
| 1244 | NumArgs, IdLoc, |
| 1245 | LParenLoc, RParenLoc); |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1246 | } |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1247 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1248 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1249 | // 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] | 1250 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1251 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1252 | |
| 1253 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1254 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1255 | } else { |
| 1256 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1257 | LookupParsedName(R, S, &SS); |
| 1258 | |
| 1259 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1260 | if (!TyD) { |
| 1261 | if (R.isAmbiguous()) return true; |
| 1262 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1263 | // We don't want access-control diagnostics here. |
| 1264 | R.suppressDiagnostics(); |
| 1265 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1266 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1267 | bool NotUnknownSpecialization = false; |
| 1268 | DeclContext *DC = computeDeclContext(SS, false); |
| 1269 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1270 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1271 | |
| 1272 | if (!NotUnknownSpecialization) { |
| 1273 | // When the scope specifier can refer to a member of an unknown |
| 1274 | // specialization, we take it as a type name. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 1275 | BaseType = CheckTypenameType(ETK_None, SourceLocation(), |
| 1276 | SS.getWithLocInContext(Context), |
| 1277 | *MemberOrBase, IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1278 | if (BaseType.isNull()) |
| 1279 | return true; |
| 1280 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1281 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 1282 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1283 | } |
| 1284 | } |
| 1285 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1286 | // If no results were found, try to correct typos. |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1287 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1288 | CorrectTypo(R, S, &SS, ClassDecl, 0, CTC_NoKeywords) && |
| 1289 | R.isSingleResult()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1290 | if (FieldDecl *Member = R.getAsSingle<FieldDecl>()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1291 | if (Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl)) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1292 | // We have found a non-static data member with a similar |
| 1293 | // name to what was typed; complain and initialize that |
| 1294 | // member. |
| 1295 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1296 | << MemberOrBase << true << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1297 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1298 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1299 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 1300 | << Member->getDeclName(); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1301 | |
| 1302 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 1303 | LParenLoc, RParenLoc); |
| 1304 | } |
| 1305 | } else if (TypeDecl *Type = R.getAsSingle<TypeDecl>()) { |
| 1306 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1307 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1308 | if (FindBaseInitializer(*this, ClassDecl, |
| 1309 | Context.getTypeDeclType(Type), |
| 1310 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1311 | // We have found a direct or virtual base class with a |
| 1312 | // similar name to what was typed; complain and initialize |
| 1313 | // that base class. |
| 1314 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1315 | << MemberOrBase << false << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1316 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1317 | R.getLookupName().getAsString()); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1318 | |
| 1319 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1320 | : VirtualBaseSpec; |
| 1321 | Diag(BaseSpec->getSourceRange().getBegin(), |
| 1322 | diag::note_base_class_specified_here) |
| 1323 | << BaseSpec->getType() |
| 1324 | << BaseSpec->getSourceRange(); |
| 1325 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1326 | TyD = Type; |
| 1327 | } |
| 1328 | } |
| 1329 | } |
| 1330 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1331 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1332 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1333 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1334 | return true; |
| 1335 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1338 | if (BaseType.isNull()) { |
| 1339 | BaseType = Context.getTypeDeclType(TyD); |
| 1340 | if (SS.isSet()) { |
| 1341 | NestedNameSpecifier *Qualifier = |
| 1342 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1343 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1344 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1345 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1346 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1347 | } |
| 1348 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1349 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1350 | if (!TInfo) |
| 1351 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1352 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1353 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1354 | LParenLoc, RParenLoc, ClassDecl, EllipsisLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1357 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1358 | /// containing the field that is being initialized. Returns true if there is an |
| 1359 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1360 | /// otherwise. |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1361 | static bool InitExprContainsUninitializedFields(const Stmt *S, |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1362 | const ValueDecl *LhsField, |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1363 | SourceLocation *L) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1364 | assert(isa<FieldDecl>(LhsField) || isa<IndirectFieldDecl>(LhsField)); |
| 1365 | |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1366 | if (isa<CallExpr>(S)) { |
| 1367 | // Do not descend into function calls or constructors, as the use |
| 1368 | // of an uninitialized field may be valid. One would have to inspect |
| 1369 | // the contents of the function/ctor to determine if it is safe or not. |
| 1370 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1371 | // may be safe, depending on what the function/ctor does. |
| 1372 | return false; |
| 1373 | } |
| 1374 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 1375 | const NamedDecl *RhsField = ME->getMemberDecl(); |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1376 | |
| 1377 | if (const VarDecl *VD = dyn_cast<VarDecl>(RhsField)) { |
| 1378 | // The member expression points to a static data member. |
| 1379 | assert(VD->isStaticDataMember() && |
| 1380 | "Member points to non-static data member!"); |
Nick Lewycky | edd5911 | 2010-10-06 18:37:39 +0000 | [diff] [blame] | 1381 | (void)VD; |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1382 | return false; |
| 1383 | } |
| 1384 | |
| 1385 | if (isa<EnumConstantDecl>(RhsField)) { |
| 1386 | // The member expression points to an enum. |
| 1387 | return false; |
| 1388 | } |
| 1389 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1390 | if (RhsField == LhsField) { |
| 1391 | // Initializing a field with itself. Throw a warning. |
| 1392 | // But wait; there are exceptions! |
| 1393 | // Exception #1: The field may not belong to this record. |
| 1394 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1395 | const Expr *base = ME->getBase(); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1396 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1397 | // Even though the field matches, it does not belong to this record. |
| 1398 | return false; |
| 1399 | } |
| 1400 | // None of the exceptions triggered; return true to indicate an |
| 1401 | // uninitialized field was used. |
| 1402 | *L = ME->getMemberLoc(); |
| 1403 | return true; |
| 1404 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1405 | } else if (isa<UnaryExprOrTypeTraitExpr>(S)) { |
Argyrios Kyrtzidis | ff8819b | 2010-09-21 10:47:20 +0000 | [diff] [blame] | 1406 | // sizeof/alignof doesn't reference contents, do not warn. |
| 1407 | return false; |
| 1408 | } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(S)) { |
| 1409 | // address-of doesn't reference contents (the pointer may be dereferenced |
| 1410 | // in the same expression but it would be rare; and weird). |
| 1411 | if (UOE->getOpcode() == UO_AddrOf) |
| 1412 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1413 | } |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1414 | for (Stmt::const_child_range it = S->children(); it; ++it) { |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1415 | if (!*it) { |
| 1416 | // An expression such as 'member(arg ?: "")' may trigger this. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1417 | continue; |
| 1418 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1419 | if (InitExprContainsUninitializedFields(*it, LhsField, L)) |
| 1420 | return true; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1421 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1422 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1425 | MemInitResult |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1426 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr **Args, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1427 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1428 | SourceLocation LParenLoc, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1429 | SourceLocation RParenLoc) { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1430 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 1431 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 1432 | assert((DirectMember || IndirectMember) && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1433 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 1434 | |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 1435 | if (Member->isInvalidDecl()) |
| 1436 | return true; |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1437 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1438 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1439 | // foo(foo) |
| 1440 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1441 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1442 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1443 | SourceLocation L; |
| 1444 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1445 | // FIXME: Return true in the case when other fields are used before being |
| 1446 | // uninitialized. For example, let this field be the i'th field. When |
| 1447 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1448 | // fields are used, as they are not yet initialized. |
| 1449 | // Right now we are only handling the case where the i'th field uses |
| 1450 | // itself in its initializer. |
| 1451 | Diag(L, diag::warn_field_is_uninit); |
| 1452 | } |
| 1453 | } |
| 1454 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1455 | bool HasDependentArg = false; |
| 1456 | for (unsigned i = 0; i < NumArgs; i++) |
| 1457 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1458 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1459 | Expr *Init; |
Eli Friedman | 0f2b97d | 2010-07-24 21:19:15 +0000 | [diff] [blame] | 1460 | if (Member->getType()->isDependentType() || HasDependentArg) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1461 | // Can't check initialization for a member of dependent type or when |
| 1462 | // any of the arguments are type-dependent expressions. |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1463 | Init = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1464 | RParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1465 | |
| 1466 | // Erase any temporaries within this evaluation context; we're not |
| 1467 | // going to track them in the AST, since we'll be rebuilding the |
| 1468 | // ASTs during template instantiation. |
| 1469 | ExprTemporaries.erase( |
| 1470 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1471 | ExprTemporaries.end()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1472 | } else { |
| 1473 | // Initialize the member. |
| 1474 | InitializedEntity MemberEntity = |
| 1475 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 1476 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 1477 | InitializationKind Kind = |
| 1478 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1479 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1480 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 1481 | |
| 1482 | ExprResult MemberInit = |
| 1483 | InitSeq.Perform(*this, MemberEntity, Kind, |
| 1484 | MultiExprArg(*this, Args, NumArgs), 0); |
| 1485 | if (MemberInit.isInvalid()) |
| 1486 | return true; |
| 1487 | |
| 1488 | CheckImplicitConversions(MemberInit.get(), LParenLoc); |
| 1489 | |
| 1490 | // C++0x [class.base.init]p7: |
| 1491 | // The initialization of each base and member constitutes a |
| 1492 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1493 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1494 | if (MemberInit.isInvalid()) |
| 1495 | return true; |
| 1496 | |
| 1497 | // If we are in a dependent context, template instantiation will |
| 1498 | // perform this type-checking again. Just save the arguments that we |
| 1499 | // received in a ParenListExpr. |
| 1500 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1501 | // of the information that we have about the member |
| 1502 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1503 | // and this approach is far more likely to get the corner cases right. |
| 1504 | if (CurContext->isDependentContext()) |
| 1505 | Init = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1506 | RParenLoc); |
| 1507 | else |
| 1508 | Init = MemberInit.get(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1511 | if (DirectMember) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1512 | return new (Context) CXXCtorInitializer(Context, DirectMember, |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1513 | IdLoc, LParenLoc, Init, |
| 1514 | RParenLoc); |
| 1515 | } else { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1516 | return new (Context) CXXCtorInitializer(Context, IndirectMember, |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1517 | IdLoc, LParenLoc, Init, |
| 1518 | RParenLoc); |
| 1519 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1522 | MemInitResult |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1523 | Sema::BuildDelegatingInitializer(TypeSourceInfo *TInfo, |
| 1524 | Expr **Args, unsigned NumArgs, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1525 | SourceLocation NameLoc, |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1526 | SourceLocation LParenLoc, |
| 1527 | SourceLocation RParenLoc, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1528 | CXXRecordDecl *ClassDecl) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1529 | SourceLocation Loc = TInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1530 | if (!LangOpts.CPlusPlus0x) |
| 1531 | return Diag(Loc, diag::err_delegation_0x_only) |
| 1532 | << TInfo->getTypeLoc().getLocalSourceRange(); |
Sebastian Redl | f9c32eb | 2011-03-12 13:53:51 +0000 | [diff] [blame] | 1533 | |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1534 | // Initialize the object. |
| 1535 | InitializedEntity DelegationEntity = InitializedEntity::InitializeDelegation( |
| 1536 | QualType(ClassDecl->getTypeForDecl(), 0)); |
| 1537 | InitializationKind Kind = |
| 1538 | InitializationKind::CreateDirect(NameLoc, LParenLoc, RParenLoc); |
| 1539 | |
| 1540 | InitializationSequence InitSeq(*this, DelegationEntity, Kind, Args, NumArgs); |
| 1541 | |
| 1542 | ExprResult DelegationInit = |
| 1543 | InitSeq.Perform(*this, DelegationEntity, Kind, |
| 1544 | MultiExprArg(*this, Args, NumArgs), 0); |
| 1545 | if (DelegationInit.isInvalid()) |
| 1546 | return true; |
| 1547 | |
| 1548 | CXXConstructExpr *ConExpr = cast<CXXConstructExpr>(DelegationInit.get()); |
| 1549 | CXXConstructorDecl *Constructor = ConExpr->getConstructor(); |
| 1550 | assert(Constructor && "Delegating constructor with no target?"); |
| 1551 | |
| 1552 | CheckImplicitConversions(DelegationInit.get(), LParenLoc); |
| 1553 | |
| 1554 | // C++0x [class.base.init]p7: |
| 1555 | // The initialization of each base and member constitutes a |
| 1556 | // full-expression. |
| 1557 | DelegationInit = MaybeCreateExprWithCleanups(DelegationInit); |
| 1558 | if (DelegationInit.isInvalid()) |
| 1559 | return true; |
| 1560 | |
| 1561 | // If we are in a dependent context, template instantiation will |
| 1562 | // perform this type-checking again. Just save the arguments that we |
| 1563 | // received in a ParenListExpr. |
| 1564 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1565 | // of the information that we have about the base |
| 1566 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1567 | // and this approach is far more likely to get the corner cases right. |
| 1568 | if (CurContext->isDependentContext()) { |
| 1569 | ExprResult Init |
| 1570 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, |
| 1571 | NumArgs, RParenLoc)); |
| 1572 | return new (Context) CXXCtorInitializer(Context, Loc, LParenLoc, |
| 1573 | Constructor, Init.takeAs<Expr>(), |
| 1574 | RParenLoc); |
| 1575 | } |
| 1576 | |
| 1577 | return new (Context) CXXCtorInitializer(Context, Loc, LParenLoc, Constructor, |
| 1578 | DelegationInit.takeAs<Expr>(), |
| 1579 | RParenLoc); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1583 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1584 | Expr **Args, unsigned NumArgs, |
| 1585 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1586 | CXXRecordDecl *ClassDecl, |
| 1587 | SourceLocation EllipsisLoc) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1588 | bool HasDependentArg = false; |
| 1589 | for (unsigned i = 0; i < NumArgs; i++) |
| 1590 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1591 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1592 | SourceLocation BaseLoc |
| 1593 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1594 | |
| 1595 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 1596 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 1597 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1598 | |
| 1599 | // C++ [class.base.init]p2: |
| 1600 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1601 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1602 | // of that class, the mem-initializer is ill-formed. A |
| 1603 | // mem-initializer-list can initialize a base class using any |
| 1604 | // name that denotes that base class type. |
| 1605 | bool Dependent = BaseType->isDependentType() || HasDependentArg; |
| 1606 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1607 | if (EllipsisLoc.isValid()) { |
| 1608 | // This is a pack expansion. |
| 1609 | if (!BaseType->containsUnexpandedParameterPack()) { |
| 1610 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 1611 | << SourceRange(BaseLoc, RParenLoc); |
| 1612 | |
| 1613 | EllipsisLoc = SourceLocation(); |
| 1614 | } |
| 1615 | } else { |
| 1616 | // Check for any unexpanded parameter packs. |
| 1617 | if (DiagnoseUnexpandedParameterPack(BaseLoc, BaseTInfo, UPPC_Initializer)) |
| 1618 | return true; |
| 1619 | |
| 1620 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1621 | if (DiagnoseUnexpandedParameterPack(Args[I])) |
| 1622 | return true; |
| 1623 | } |
| 1624 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1625 | // Check for direct and virtual base classes. |
| 1626 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1627 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1628 | if (!Dependent) { |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1629 | if (Context.hasSameUnqualifiedType(QualType(ClassDecl->getTypeForDecl(),0), |
| 1630 | BaseType)) |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1631 | return BuildDelegatingInitializer(BaseTInfo, Args, NumArgs, BaseLoc, |
| 1632 | LParenLoc, RParenLoc, ClassDecl); |
Sean Hunt | 97fcc49 | 2011-01-08 19:20:43 +0000 | [diff] [blame] | 1633 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1634 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 1635 | VirtualBaseSpec); |
| 1636 | |
| 1637 | // C++ [base.class.init]p2: |
| 1638 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 1639 | // constructor's class or a direct or virtual base of that class, the |
| 1640 | // mem-initializer is ill-formed. |
| 1641 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 1642 | // If the class has any dependent bases, then it's possible that |
| 1643 | // one of those types will resolve to the same type as |
| 1644 | // BaseType. Therefore, just treat this as a dependent base |
| 1645 | // class initialization. FIXME: Should we try to check the |
| 1646 | // initialization anyway? It seems odd. |
| 1647 | if (ClassDecl->hasAnyDependentBases()) |
| 1648 | Dependent = true; |
| 1649 | else |
| 1650 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 1651 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 1652 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | if (Dependent) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1657 | // Can't check initialization for a base of dependent type or when |
| 1658 | // any of the arguments are type-dependent expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1659 | ExprResult BaseInit |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1660 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1661 | RParenLoc)); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1662 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1663 | // Erase any temporaries within this evaluation context; we're not |
| 1664 | // going to track them in the AST, since we'll be rebuilding the |
| 1665 | // ASTs during template instantiation. |
| 1666 | ExprTemporaries.erase( |
| 1667 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1668 | ExprTemporaries.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1669 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1670 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1671 | /*IsVirtual=*/false, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1672 | LParenLoc, |
| 1673 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1674 | RParenLoc, |
| 1675 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1676 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1677 | |
| 1678 | // C++ [base.class.init]p2: |
| 1679 | // If a mem-initializer-id is ambiguous because it designates both |
| 1680 | // a direct non-virtual base class and an inherited virtual base |
| 1681 | // class, the mem-initializer is ill-formed. |
| 1682 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1683 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1684 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1685 | |
| 1686 | CXXBaseSpecifier *BaseSpec |
| 1687 | = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
| 1688 | if (!BaseSpec) |
| 1689 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 1690 | |
| 1691 | // Initialize the base. |
| 1692 | InitializedEntity BaseEntity = |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1693 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1694 | InitializationKind Kind = |
| 1695 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc); |
| 1696 | |
| 1697 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 1698 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1699 | ExprResult BaseInit = |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1700 | InitSeq.Perform(*this, BaseEntity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1701 | MultiExprArg(*this, Args, NumArgs), 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1702 | if (BaseInit.isInvalid()) |
| 1703 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1704 | |
| 1705 | CheckImplicitConversions(BaseInit.get(), LParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1706 | |
| 1707 | // C++0x [class.base.init]p7: |
| 1708 | // The initialization of each base and member constitutes a |
| 1709 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1710 | BaseInit = MaybeCreateExprWithCleanups(BaseInit); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1711 | if (BaseInit.isInvalid()) |
| 1712 | return true; |
| 1713 | |
| 1714 | // If we are in a dependent context, template instantiation will |
| 1715 | // perform this type-checking again. Just save the arguments that we |
| 1716 | // received in a ParenListExpr. |
| 1717 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1718 | // of the information that we have about the base |
| 1719 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1720 | // and this approach is far more likely to get the corner cases right. |
| 1721 | if (CurContext->isDependentContext()) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1722 | ExprResult Init |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1723 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1724 | RParenLoc)); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1725 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1726 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1727 | LParenLoc, |
| 1728 | Init.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1729 | RParenLoc, |
| 1730 | EllipsisLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1733 | return new (Context) CXXCtorInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1734 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1735 | LParenLoc, |
| 1736 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1737 | RParenLoc, |
| 1738 | EllipsisLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1741 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 1742 | /// initialize its base or member. |
| 1743 | enum ImplicitInitializerKind { |
| 1744 | IIK_Default, |
| 1745 | IIK_Copy, |
| 1746 | IIK_Move |
| 1747 | }; |
| 1748 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1749 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1750 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1751 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1752 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1753 | bool IsInheritedVirtualBase, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1754 | CXXCtorInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1755 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1756 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 1757 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1758 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1759 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1760 | |
| 1761 | switch (ImplicitInitKind) { |
| 1762 | case IIK_Default: { |
| 1763 | InitializationKind InitKind |
| 1764 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 1765 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1766 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1767 | MultiExprArg(SemaRef, 0, 0)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1768 | break; |
| 1769 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1770 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1771 | case IIK_Copy: { |
| 1772 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1773 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1774 | |
| 1775 | Expr *CopyCtorArg = |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1776 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), Param, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1777 | Constructor->getLocation(), ParamType, |
| 1778 | VK_LValue, 0); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1779 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1780 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 1781 | QualType ArgTy = |
| 1782 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 1783 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1784 | |
| 1785 | CXXCastPath BasePath; |
| 1786 | BasePath.push_back(BaseSpec); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1787 | CopyCtorArg = SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
| 1788 | CK_UncheckedDerivedToBase, |
| 1789 | VK_LValue, &BasePath).take(); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1790 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1791 | InitializationKind InitKind |
| 1792 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1793 | SourceLocation(), SourceLocation()); |
| 1794 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1795 | &CopyCtorArg, 1); |
| 1796 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1797 | MultiExprArg(&CopyCtorArg, 1)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1798 | break; |
| 1799 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1800 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1801 | case IIK_Move: |
| 1802 | assert(false && "Unhandled initializer kind!"); |
| 1803 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1804 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1805 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1806 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1807 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1808 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1809 | CXXBaseInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1810 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1811 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 1812 | SourceLocation()), |
| 1813 | BaseSpec->isVirtual(), |
| 1814 | SourceLocation(), |
| 1815 | BaseInit.takeAs<Expr>(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 1816 | SourceLocation(), |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1817 | SourceLocation()); |
| 1818 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1819 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1822 | static bool |
| 1823 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1824 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1825 | FieldDecl *Field, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1826 | CXXCtorInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 1827 | if (Field->isInvalidDecl()) |
| 1828 | return true; |
| 1829 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1830 | SourceLocation Loc = Constructor->getLocation(); |
| 1831 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1832 | if (ImplicitInitKind == IIK_Copy) { |
| 1833 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1834 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1835 | |
| 1836 | Expr *MemberExprBase = |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1837 | DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(), Param, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1838 | Loc, ParamType, VK_LValue, 0); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1839 | |
| 1840 | // Build a reference to this field within the parameter. |
| 1841 | CXXScopeSpec SS; |
| 1842 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 1843 | Sema::LookupMemberName); |
| 1844 | MemberLookup.addDecl(Field, AS_public); |
| 1845 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1846 | ExprResult CopyCtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1847 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1848 | ParamType, Loc, |
| 1849 | /*IsArrow=*/false, |
| 1850 | SS, |
| 1851 | /*FirstQualifierInScope=*/0, |
| 1852 | MemberLookup, |
| 1853 | /*TemplateArgs=*/0); |
| 1854 | if (CopyCtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1855 | return true; |
| 1856 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1857 | // When the field we are copying is an array, create index variables for |
| 1858 | // each dimension of the array. We use these index variables to subscript |
| 1859 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 1860 | // necessary iteration with these index variables. |
| 1861 | llvm::SmallVector<VarDecl *, 4> IndexVariables; |
| 1862 | QualType BaseType = Field->getType(); |
| 1863 | QualType SizeType = SemaRef.Context.getSizeType(); |
| 1864 | while (const ConstantArrayType *Array |
| 1865 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
| 1866 | // Create the iteration variable for this array index. |
| 1867 | IdentifierInfo *IterationVarName = 0; |
| 1868 | { |
| 1869 | llvm::SmallString<8> Str; |
| 1870 | llvm::raw_svector_ostream OS(Str); |
| 1871 | OS << "__i" << IndexVariables.size(); |
| 1872 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 1873 | } |
| 1874 | VarDecl *IterationVar |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1875 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1876 | IterationVarName, SizeType, |
| 1877 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1878 | SC_None, SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1879 | IndexVariables.push_back(IterationVar); |
| 1880 | |
| 1881 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1882 | ExprResult IterationVarRef |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1883 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_RValue, Loc); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1884 | assert(!IterationVarRef.isInvalid() && |
| 1885 | "Reference to invented variable cannot fail!"); |
| 1886 | |
| 1887 | // Subscript the array with this iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1888 | CopyCtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CopyCtorArg.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1889 | Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1890 | IterationVarRef.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1891 | Loc); |
| 1892 | if (CopyCtorArg.isInvalid()) |
| 1893 | return true; |
| 1894 | |
| 1895 | BaseType = Array->getElementType(); |
| 1896 | } |
| 1897 | |
| 1898 | // Construct the entity that we will be initializing. For an array, this |
| 1899 | // will be first element in the array, which may require several levels |
| 1900 | // of array-subscript entities. |
| 1901 | llvm::SmallVector<InitializedEntity, 4> Entities; |
| 1902 | Entities.reserve(1 + IndexVariables.size()); |
| 1903 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
| 1904 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 1905 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 1906 | 0, |
| 1907 | Entities.back())); |
| 1908 | |
| 1909 | // Direct-initialize to use the copy constructor. |
| 1910 | InitializationKind InitKind = |
| 1911 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 1912 | |
| 1913 | Expr *CopyCtorArgE = CopyCtorArg.takeAs<Expr>(); |
| 1914 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
| 1915 | &CopyCtorArgE, 1); |
| 1916 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1917 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1918 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1919 | MultiExprArg(&CopyCtorArgE, 1)); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1920 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1921 | if (MemberInit.isInvalid()) |
| 1922 | return true; |
| 1923 | |
| 1924 | CXXMemberInit |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1925 | = CXXCtorInitializer::Create(SemaRef.Context, Field, Loc, Loc, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1926 | MemberInit.takeAs<Expr>(), Loc, |
| 1927 | IndexVariables.data(), |
| 1928 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1929 | return false; |
| 1930 | } |
| 1931 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1932 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 1933 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1934 | QualType FieldBaseElementType = |
| 1935 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 1936 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1937 | if (FieldBaseElementType->isRecordType()) { |
| 1938 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1939 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1940 | InitializationKind::CreateDefault(Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1941 | |
| 1942 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1943 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1944 | InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1945 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1946 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1947 | if (MemberInit.isInvalid()) |
| 1948 | return true; |
| 1949 | |
| 1950 | CXXMemberInit = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1951 | new (SemaRef.Context) CXXCtorInitializer(SemaRef.Context, |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1952 | Field, Loc, Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1953 | MemberInit.get(), |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1954 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1955 | return false; |
| 1956 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 1957 | |
| 1958 | if (FieldBaseElementType->isReferenceType()) { |
| 1959 | SemaRef.Diag(Constructor->getLocation(), |
| 1960 | diag::err_uninitialized_member_in_ctor) |
| 1961 | << (int)Constructor->isImplicit() |
| 1962 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1963 | << 0 << Field->getDeclName(); |
| 1964 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1965 | return true; |
| 1966 | } |
| 1967 | |
| 1968 | if (FieldBaseElementType.isConstQualified()) { |
| 1969 | SemaRef.Diag(Constructor->getLocation(), |
| 1970 | diag::err_uninitialized_member_in_ctor) |
| 1971 | << (int)Constructor->isImplicit() |
| 1972 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1973 | << 1 << Field->getDeclName(); |
| 1974 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1975 | return true; |
| 1976 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1977 | |
| 1978 | // Nothing to initialize. |
| 1979 | CXXMemberInit = 0; |
| 1980 | return false; |
| 1981 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1982 | |
| 1983 | namespace { |
| 1984 | struct BaseAndFieldInfo { |
| 1985 | Sema &S; |
| 1986 | CXXConstructorDecl *Ctor; |
| 1987 | bool AnyErrorsInInits; |
| 1988 | ImplicitInitializerKind IIK; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1989 | llvm::DenseMap<const void *, CXXCtorInitializer*> AllBaseFields; |
| 1990 | llvm::SmallVector<CXXCtorInitializer*, 8> AllToInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1991 | |
| 1992 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 1993 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
| 1994 | // FIXME: Handle implicit move constructors. |
| 1995 | if (Ctor->isImplicit() && Ctor->isCopyConstructor()) |
| 1996 | IIK = IIK_Copy; |
| 1997 | else |
| 1998 | IIK = IIK_Default; |
| 1999 | } |
| 2000 | }; |
| 2001 | } |
| 2002 | |
| 2003 | static bool CollectFieldInitializer(BaseAndFieldInfo &Info, |
| 2004 | FieldDecl *Top, FieldDecl *Field) { |
| 2005 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2006 | // Overwhelmingly common case: we have a direct initializer for this field. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2007 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(Field)) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2008 | Info.AllToInit.push_back(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2009 | return false; |
| 2010 | } |
| 2011 | |
| 2012 | if (Info.IIK == IIK_Default && Field->isAnonymousStructOrUnion()) { |
| 2013 | const RecordType *FieldClassType = Field->getType()->getAs<RecordType>(); |
| 2014 | assert(FieldClassType && "anonymous struct/union without record type"); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2015 | CXXRecordDecl *FieldClassDecl |
| 2016 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2017 | |
| 2018 | // Even though union members never have non-trivial default |
| 2019 | // constructions in C++03, we still build member initializers for aggregate |
| 2020 | // record types which can be union members, and C++0x allows non-trivial |
| 2021 | // default constructors for union members, so we ensure that only one |
| 2022 | // member is initialized for these. |
| 2023 | if (FieldClassDecl->isUnion()) { |
| 2024 | // First check for an explicit initializer for one field. |
| 2025 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 2026 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2027 | if (CXXCtorInitializer *Init = Info.AllBaseFields.lookup(*FA)) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2028 | Info.AllToInit.push_back(Init); |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2029 | |
| 2030 | // Once we've initialized a field of an anonymous union, the union |
| 2031 | // field in the class is also initialized, so exit immediately. |
| 2032 | return false; |
Argyrios Kyrtzidis | 881b36c | 2010-08-16 17:27:13 +0000 | [diff] [blame] | 2033 | } else if ((*FA)->isAnonymousStructOrUnion()) { |
| 2034 | if (CollectFieldInitializer(Info, Top, *FA)) |
| 2035 | return true; |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | // Fallthrough and construct a default initializer for the union as |
| 2040 | // a whole, which can call its default constructor if such a thing exists |
| 2041 | // (C++0x perhaps). FIXME: It's not clear that this is the correct |
| 2042 | // behavior going forward with C++0x, when anonymous unions there are |
| 2043 | // finalized, we should revisit this. |
| 2044 | } else { |
| 2045 | // For structs, we simply descend through to initialize all members where |
| 2046 | // necessary. |
| 2047 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 2048 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 2049 | if (CollectFieldInitializer(Info, Top, *FA)) |
| 2050 | return true; |
| 2051 | } |
| 2052 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | // Don't try to build an implicit initializer if there were semantic |
| 2056 | // errors in any of the initializers (and therefore we might be |
| 2057 | // missing some that the user actually wrote). |
| 2058 | if (Info.AnyErrorsInInits) |
| 2059 | return false; |
| 2060 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2061 | CXXCtorInitializer *Init = 0; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2062 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, Init)) |
| 2063 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2064 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2065 | if (Init) |
| 2066 | Info.AllToInit.push_back(Init); |
| 2067 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2068 | return false; |
| 2069 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2070 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2071 | bool |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2072 | Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, |
| 2073 | CXXCtorInitializer **Initializers, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2074 | unsigned NumInitializers, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2075 | bool AnyErrors) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2076 | if (Constructor->getDeclContext()->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2077 | // Just store the initializers as written, they will be checked during |
| 2078 | // instantiation. |
| 2079 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2080 | Constructor->setNumCtorInitializers(NumInitializers); |
| 2081 | CXXCtorInitializer **baseOrMemberInitializers = |
| 2082 | new (Context) CXXCtorInitializer*[NumInitializers]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2083 | memcpy(baseOrMemberInitializers, Initializers, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2084 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 2085 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | return false; |
| 2089 | } |
| 2090 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2091 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2092 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2093 | // We need to build the initializer AST according to order of construction |
| 2094 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2095 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 2096 | if (!ClassDecl) |
| 2097 | return true; |
| 2098 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2099 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2100 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2101 | for (unsigned i = 0; i < NumInitializers; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2102 | CXXCtorInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2103 | |
| 2104 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2105 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2106 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2107 | Info.AllBaseFields[Member->getAnyMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2110 | // Keep track of the direct virtual bases. |
| 2111 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 2112 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 2113 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 2114 | if (I->isVirtual()) |
| 2115 | DirectVBases.insert(I); |
| 2116 | } |
| 2117 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2118 | // Push virtual bases before others. |
| 2119 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2120 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 2121 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2122 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2123 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 2124 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2125 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2126 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2127 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2128 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2129 | VBase, IsInheritedVirtualBase, |
| 2130 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2131 | HadError = true; |
| 2132 | continue; |
| 2133 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 2134 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2135 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2136 | } |
| 2137 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2138 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2139 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2140 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2141 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 2142 | // Virtuals are in the virtual base list and already constructed. |
| 2143 | if (Base->isVirtual()) |
| 2144 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2146 | if (CXXCtorInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2147 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 2148 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2149 | } else if (!AnyErrors) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2150 | CXXCtorInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2151 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 2152 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 2153 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2154 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2155 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 2156 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 2157 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2158 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2159 | } |
| 2160 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2161 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2162 | // Fields. |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2163 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 2164 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 2165 | if ((*Field)->getType()->isIncompleteArrayType()) { |
| 2166 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 2167 | "Incomplete array type is not valid"); |
| 2168 | continue; |
| 2169 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2170 | if (CollectFieldInitializer(Info, *Field, *Field)) |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 2171 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 2172 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2173 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2174 | NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2175 | if (NumInitializers > 0) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2176 | Constructor->setNumCtorInitializers(NumInitializers); |
| 2177 | CXXCtorInitializer **baseOrMemberInitializers = |
| 2178 | new (Context) CXXCtorInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 2179 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2180 | NumInitializers * sizeof(CXXCtorInitializer*)); |
| 2181 | Constructor->setCtorInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 2182 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2183 | // Constructors implicitly reference the base and member |
| 2184 | // destructors. |
| 2185 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 2186 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2187 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 2188 | |
| 2189 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2192 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 2193 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2194 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2195 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 2196 | return static_cast<void *>(RT->getDecl()); |
| 2197 | } |
| 2198 | return static_cast<void *>(Field); |
| 2199 | } |
| 2200 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2201 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2202 | return const_cast<Type*>(Context.getCanonicalType(BaseType).getTypePtr()); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2205 | static void *GetKeyForMember(ASTContext &Context, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2206 | CXXCtorInitializer *Member) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2207 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2208 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 2209 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2210 | // For fields injected into the class via declaration of an anonymous union, |
| 2211 | // use its anonymous union class declaration as the unique key. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2212 | FieldDecl *Field = Member->getAnyMember(); |
| 2213 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2214 | // If the field is a member of an anonymous struct or union, our key |
| 2215 | // is the anonymous record decl that's a direct child of the class. |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 2216 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2217 | if (RD->isAnonymousStructOrUnion()) { |
| 2218 | while (true) { |
| 2219 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 2220 | if (Parent->isAnonymousStructOrUnion()) |
| 2221 | RD = Parent; |
| 2222 | else |
| 2223 | break; |
| 2224 | } |
| 2225 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 2226 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2227 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2228 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 2229 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2232 | static void |
| 2233 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2234 | const CXXConstructorDecl *Constructor, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2235 | CXXCtorInitializer **Inits, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2236 | unsigned NumInits) { |
| 2237 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 2238 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2239 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 2240 | // Don't check initializers order unless the warning is enabled at the |
| 2241 | // location of at least one initializer. |
| 2242 | bool ShouldCheckOrder = false; |
| 2243 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2244 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 2245 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order, |
| 2246 | Init->getSourceLocation()) |
| 2247 | != Diagnostic::Ignored) { |
| 2248 | ShouldCheckOrder = true; |
| 2249 | break; |
| 2250 | } |
| 2251 | } |
| 2252 | if (!ShouldCheckOrder) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2253 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2254 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2255 | // Build the list of bases and members in the order that they'll |
| 2256 | // actually be initialized. The explicit initializers should be in |
| 2257 | // this same order but may be missing things. |
| 2258 | llvm::SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2259 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2260 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2261 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2262 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2263 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2264 | ClassDecl->vbases_begin(), |
| 2265 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2266 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2268 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2269 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2270 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2271 | if (Base->isVirtual()) |
| 2272 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2273 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2274 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2275 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2276 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2277 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2278 | E = ClassDecl->field_end(); Field != E; ++Field) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2279 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2280 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2281 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 2282 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2283 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2284 | CXXCtorInitializer *PrevInit = 0; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2285 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2286 | CXXCtorInitializer *Init = Inits[InitIndex]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2287 | void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2288 | |
| 2289 | // Scan forward to try to find this initializer in the idealized |
| 2290 | // initializers list. |
| 2291 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2292 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2293 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2294 | |
| 2295 | // If we didn't find this initializer, it must be because we |
| 2296 | // scanned past it on a previous iteration. That can only |
| 2297 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 2298 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2299 | Sema::SemaDiagnosticBuilder D = |
| 2300 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 2301 | diag::warn_initializer_out_of_order); |
| 2302 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2303 | if (PrevInit->isAnyMemberInitializer()) |
| 2304 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2305 | else |
| 2306 | D << 1 << PrevInit->getBaseClassInfo()->getType(); |
| 2307 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2308 | if (Init->isAnyMemberInitializer()) |
| 2309 | D << 0 << Init->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2310 | else |
| 2311 | D << 1 << Init->getBaseClassInfo()->getType(); |
| 2312 | |
| 2313 | // Move back to the initializer's location in the ideal list. |
| 2314 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2315 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2316 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2317 | |
| 2318 | assert(IdealIndex != NumIdealInits && |
| 2319 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2320 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2321 | |
| 2322 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2323 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2326 | namespace { |
| 2327 | bool CheckRedundantInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2328 | CXXCtorInitializer *Init, |
| 2329 | CXXCtorInitializer *&PrevInit) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2330 | if (!PrevInit) { |
| 2331 | PrevInit = Init; |
| 2332 | return false; |
| 2333 | } |
| 2334 | |
| 2335 | if (FieldDecl *Field = Init->getMember()) |
| 2336 | S.Diag(Init->getSourceLocation(), |
| 2337 | diag::err_multiple_mem_initialization) |
| 2338 | << Field->getDeclName() |
| 2339 | << Init->getSourceRange(); |
| 2340 | else { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2341 | const Type *BaseClass = Init->getBaseClass(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2342 | assert(BaseClass && "neither field nor base"); |
| 2343 | S.Diag(Init->getSourceLocation(), |
| 2344 | diag::err_multiple_base_initialization) |
| 2345 | << QualType(BaseClass, 0) |
| 2346 | << Init->getSourceRange(); |
| 2347 | } |
| 2348 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 2349 | << 0 << PrevInit->getSourceRange(); |
| 2350 | |
| 2351 | return true; |
| 2352 | } |
| 2353 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2354 | typedef std::pair<NamedDecl *, CXXCtorInitializer *> UnionEntry; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2355 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 2356 | |
| 2357 | bool CheckRedundantUnionInit(Sema &S, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2358 | CXXCtorInitializer *Init, |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2359 | RedundantUnionMap &Unions) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2360 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2361 | RecordDecl *Parent = Field->getParent(); |
| 2362 | if (!Parent->isAnonymousStructOrUnion()) |
| 2363 | return false; |
| 2364 | |
| 2365 | NamedDecl *Child = Field; |
| 2366 | do { |
| 2367 | if (Parent->isUnion()) { |
| 2368 | UnionEntry &En = Unions[Parent]; |
| 2369 | if (En.first && En.first != Child) { |
| 2370 | S.Diag(Init->getSourceLocation(), |
| 2371 | diag::err_multiple_mem_union_initialization) |
| 2372 | << Field->getDeclName() |
| 2373 | << Init->getSourceRange(); |
| 2374 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 2375 | << 0 << En.second->getSourceRange(); |
| 2376 | return true; |
| 2377 | } else if (!En.first) { |
| 2378 | En.first = Child; |
| 2379 | En.second = Init; |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | Child = Parent; |
| 2384 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 2385 | } while (Parent->isAnonymousStructOrUnion()); |
| 2386 | |
| 2387 | return false; |
| 2388 | } |
| 2389 | } |
| 2390 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2391 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2392 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2393 | SourceLocation ColonLoc, |
| 2394 | MemInitTy **meminits, unsigned NumMemInits, |
| 2395 | bool AnyErrors) { |
| 2396 | if (!ConstructorDecl) |
| 2397 | return; |
| 2398 | |
| 2399 | AdjustDeclIfTemplate(ConstructorDecl); |
| 2400 | |
| 2401 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2402 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2403 | |
| 2404 | if (!Constructor) { |
| 2405 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 2406 | return; |
| 2407 | } |
| 2408 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2409 | CXXCtorInitializer **MemInits = |
| 2410 | reinterpret_cast<CXXCtorInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2411 | |
| 2412 | // Mapping for the duplicate initializers check. |
| 2413 | // For member initializers, this is keyed with a FieldDecl*. |
| 2414 | // For base initializers, this is keyed with a Type*. |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2415 | llvm::DenseMap<void*, CXXCtorInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2416 | |
| 2417 | // Mapping for the inconsistent anonymous-union initializers check. |
| 2418 | RedundantUnionMap MemberUnions; |
| 2419 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2420 | bool HadError = false; |
| 2421 | for (unsigned i = 0; i < NumMemInits; i++) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2422 | CXXCtorInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2423 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 2424 | // Set the source order index. |
| 2425 | Init->setSourceOrder(i); |
| 2426 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2427 | if (Init->isAnyMemberInitializer()) { |
| 2428 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2429 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 2430 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 2431 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2432 | } else if (Init->isBaseInitializer()) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2433 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 2434 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 2435 | HadError = true; |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2436 | } else { |
| 2437 | assert(Init->isDelegatingInitializer()); |
| 2438 | // This must be the only initializer |
| 2439 | if (i != 0 || NumMemInits > 1) { |
| 2440 | Diag(MemInits[0]->getSourceLocation(), |
| 2441 | diag::err_delegating_initializer_alone) |
| 2442 | << MemInits[0]->getSourceRange(); |
| 2443 | HadError = true; |
| 2444 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2445 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2448 | if (HadError) |
| 2449 | return; |
| 2450 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2451 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2452 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2453 | SetCtorInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2456 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2457 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 2458 | CXXRecordDecl *ClassDecl) { |
| 2459 | // Ignore dependent contexts. |
| 2460 | if (ClassDecl->isDependentContext()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2461 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2462 | |
| 2463 | // FIXME: all the access-control diagnostics are positioned on the |
| 2464 | // field/base declaration. That's probably good; that said, the |
| 2465 | // user might reasonably want to know why the destructor is being |
| 2466 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2467 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2468 | // Non-static data members. |
| 2469 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 2470 | E = ClassDecl->field_end(); I != E; ++I) { |
| 2471 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 2472 | if (Field->isInvalidDecl()) |
| 2473 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2474 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 2475 | |
| 2476 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 2477 | if (!RT) |
| 2478 | continue; |
| 2479 | |
| 2480 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2481 | if (FieldClassDecl->isInvalidDecl()) |
| 2482 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2483 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2484 | continue; |
| 2485 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2486 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2487 | assert(Dtor && "No dtor found for FieldClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2488 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2489 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2490 | << Field->getDeclName() |
| 2491 | << FieldType); |
| 2492 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2493 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2496 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 2497 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2498 | // Bases. |
| 2499 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2500 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2501 | // Bases are always records in a well-formed non-dependent class. |
| 2502 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 2503 | |
| 2504 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2505 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2506 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2507 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2508 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2509 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 2510 | if (BaseClassDecl->isInvalidDecl()) |
| 2511 | continue; |
| 2512 | // Ignore trivial destructors. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2513 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2514 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2515 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2516 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2517 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2518 | |
| 2519 | // FIXME: caret should be on the start of the class name |
| 2520 | CheckDestructorAccess(Base->getSourceRange().getBegin(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2521 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2522 | << Base->getType() |
| 2523 | << Base->getSourceRange()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2524 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2525 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2529 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2530 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2531 | |
| 2532 | // Bases are always records in a well-formed non-dependent class. |
| 2533 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 2534 | |
| 2535 | // Ignore direct virtual bases. |
| 2536 | if (DirectVirtualBases.count(RT)) |
| 2537 | continue; |
| 2538 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2539 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2540 | // If our base class is invalid, we probably can't get its dtor anyway. |
| 2541 | if (BaseClassDecl->isInvalidDecl()) |
| 2542 | continue; |
| 2543 | // Ignore trivial destructors. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2544 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2545 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2546 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2547 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
Matt Beaumont-Gay | 3334b0b | 2011-03-28 01:39:13 +0000 | [diff] [blame] | 2548 | assert(Dtor && "No dtor found for BaseClassDecl!"); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2549 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2550 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2551 | << VBase->getType()); |
| 2552 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2553 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2554 | } |
| 2555 | } |
| 2556 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2557 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2558 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2559 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2560 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2562 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2563 | SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2567 | unsigned DiagID, AbstractDiagSelID SelID) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2568 | if (SelID == -1) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2569 | return RequireNonAbstractType(Loc, T, PDiag(DiagID)); |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2570 | else |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2571 | return RequireNonAbstractType(Loc, T, PDiag(DiagID) << SelID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2574 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2575 | const PartialDiagnostic &PD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2576 | if (!getLangOptions().CPlusPlus) |
| 2577 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2578 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 2579 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2580 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2581 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2582 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2583 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2584 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2585 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2586 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2587 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2588 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2589 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2591 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2592 | if (!RT) |
| 2593 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2594 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2595 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2596 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2597 | // We can't answer whether something is abstract until it has a |
| 2598 | // definition. If it's currently being defined, we'll walk back |
| 2599 | // over all the declarations when we have a full definition. |
| 2600 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 2601 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2602 | return false; |
| 2603 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2604 | if (!RD->isAbstract()) |
| 2605 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2607 | Diag(Loc, PD) << RD->getDeclName(); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2608 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2609 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2610 | return true; |
| 2611 | } |
| 2612 | |
| 2613 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 2614 | // Check if we've already emitted the list of pure virtual functions |
| 2615 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2616 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2617 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2618 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2619 | CXXFinalOverriderMap FinalOverriders; |
| 2620 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2621 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2622 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 2623 | // more than once. |
| 2624 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 2625 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2626 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2627 | MEnd = FinalOverriders.end(); |
| 2628 | M != MEnd; |
| 2629 | ++M) { |
| 2630 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2631 | SOEnd = M->second.end(); |
| 2632 | SO != SOEnd; ++SO) { |
| 2633 | // C++ [class.abstract]p4: |
| 2634 | // A class is abstract if it contains or inherits at least one |
| 2635 | // pure virtual function for which the final overrider is pure |
| 2636 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2638 | // |
| 2639 | if (SO->second.size() != 1) |
| 2640 | continue; |
| 2641 | |
| 2642 | if (!SO->second.front().Method->isPure()) |
| 2643 | continue; |
| 2644 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2645 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 2646 | continue; |
| 2647 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2648 | Diag(SO->second.front().Method->getLocation(), |
| 2649 | diag::note_pure_virtual_function) |
Chandler Carruth | 45f11b7 | 2011-02-18 23:59:51 +0000 | [diff] [blame] | 2650 | << SO->second.front().Method->getDeclName() << RD->getDeclName(); |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2651 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | if (!PureVirtualClassDiagSet) |
| 2655 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 2656 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2659 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2660 | struct AbstractUsageInfo { |
| 2661 | Sema &S; |
| 2662 | CXXRecordDecl *Record; |
| 2663 | CanQualType AbstractType; |
| 2664 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2665 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2666 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 2667 | : S(S), Record(Record), |
| 2668 | AbstractType(S.Context.getCanonicalType( |
| 2669 | S.Context.getTypeDeclType(Record))), |
| 2670 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2671 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2672 | void DiagnoseAbstractType() { |
| 2673 | if (Invalid) return; |
| 2674 | S.DiagnoseAbstractType(Record); |
| 2675 | Invalid = true; |
| 2676 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2677 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2678 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 2679 | }; |
| 2680 | |
| 2681 | struct CheckAbstractUsage { |
| 2682 | AbstractUsageInfo &Info; |
| 2683 | const NamedDecl *Ctx; |
| 2684 | |
| 2685 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 2686 | : Info(Info), Ctx(Ctx) {} |
| 2687 | |
| 2688 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2689 | switch (TL.getTypeLocClass()) { |
| 2690 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 2691 | #define TYPELOC(CLASS, PARENT) \ |
| 2692 | case TypeLoc::CLASS: Check(cast<CLASS##TypeLoc>(TL), Sel); break; |
| 2693 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2694 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2695 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2696 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2697 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2698 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 2699 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
Douglas Gregor | 7019186 | 2011-02-22 23:21:06 +0000 | [diff] [blame] | 2700 | if (!TL.getArg(I)) |
| 2701 | continue; |
| 2702 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2703 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 2704 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2705 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2706 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2707 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2708 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2709 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 2710 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2711 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2712 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2713 | // Visit the type parameters from a permissive context. |
| 2714 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2715 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 2716 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 2717 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 2718 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 2719 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2720 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2721 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2723 | // Visit pointee types from a permissive context. |
| 2724 | #define CheckPolymorphic(Type) \ |
| 2725 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 2726 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 2727 | } |
| 2728 | CheckPolymorphic(PointerTypeLoc) |
| 2729 | CheckPolymorphic(ReferenceTypeLoc) |
| 2730 | CheckPolymorphic(MemberPointerTypeLoc) |
| 2731 | CheckPolymorphic(BlockPointerTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2732 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2733 | /// Handle all the types we haven't given a more specific |
| 2734 | /// implementation for above. |
| 2735 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2736 | // Every other kind of type that we haven't called out already |
| 2737 | // that has an inner type is either (1) sugar or (2) contains that |
| 2738 | // inner type in some way as a subobject. |
| 2739 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 2740 | return Visit(Next, Sel); |
| 2741 | |
| 2742 | // If there's no inner type and we're in a permissive context, |
| 2743 | // don't diagnose. |
| 2744 | if (Sel == Sema::AbstractNone) return; |
| 2745 | |
| 2746 | // Check whether the type matches the abstract type. |
| 2747 | QualType T = TL.getType(); |
| 2748 | if (T->isArrayType()) { |
| 2749 | Sel = Sema::AbstractArrayType; |
| 2750 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2751 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2752 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 2753 | if (CT != Info.AbstractType) return; |
| 2754 | |
| 2755 | // It matched; do some magic. |
| 2756 | if (Sel == Sema::AbstractArrayType) { |
| 2757 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 2758 | << T << TL.getSourceRange(); |
| 2759 | } else { |
| 2760 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 2761 | << Sel << T << TL.getSourceRange(); |
| 2762 | } |
| 2763 | Info.DiagnoseAbstractType(); |
| 2764 | } |
| 2765 | }; |
| 2766 | |
| 2767 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 2768 | Sema::AbstractDiagSelID Sel) { |
| 2769 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 2770 | } |
| 2771 | |
| 2772 | } |
| 2773 | |
| 2774 | /// Check for invalid uses of an abstract type in a method declaration. |
| 2775 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2776 | CXXMethodDecl *MD) { |
| 2777 | // No need to do the check on definitions, which require that |
| 2778 | // the return/param types be complete. |
| 2779 | if (MD->isThisDeclarationADefinition()) |
| 2780 | return; |
| 2781 | |
| 2782 | // For safety's sake, just ignore it if we don't have type source |
| 2783 | // information. This should never happen for non-implicit methods, |
| 2784 | // but... |
| 2785 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 2786 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 2787 | } |
| 2788 | |
| 2789 | /// Check for invalid uses of an abstract type within a class definition. |
| 2790 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2791 | CXXRecordDecl *RD) { |
| 2792 | for (CXXRecordDecl::decl_iterator |
| 2793 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 2794 | Decl *D = *I; |
| 2795 | if (D->isImplicit()) continue; |
| 2796 | |
| 2797 | // Methods and method templates. |
| 2798 | if (isa<CXXMethodDecl>(D)) { |
| 2799 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 2800 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 2801 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 2802 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 2803 | |
| 2804 | // Fields and static variables. |
| 2805 | } else if (isa<FieldDecl>(D)) { |
| 2806 | FieldDecl *FD = cast<FieldDecl>(D); |
| 2807 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 2808 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 2809 | } else if (isa<VarDecl>(D)) { |
| 2810 | VarDecl *VD = cast<VarDecl>(D); |
| 2811 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 2812 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 2813 | |
| 2814 | // Nested classes and class templates. |
| 2815 | } else if (isa<CXXRecordDecl>(D)) { |
| 2816 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 2817 | } else if (isa<ClassTemplateDecl>(D)) { |
| 2818 | CheckAbstractClassUsage(Info, |
| 2819 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 2820 | } |
| 2821 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2824 | /// \brief Perform semantic checks on a class definition that has been |
| 2825 | /// completing, introducing implicitly-declared members, checking for |
| 2826 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2827 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 2828 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2829 | return; |
| 2830 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2831 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 2832 | AbstractUsageInfo Info(*this, Record); |
| 2833 | CheckAbstractClassUsage(Info, Record); |
| 2834 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2835 | |
| 2836 | // If this is not an aggregate type and has no user-declared constructor, |
| 2837 | // complain about any non-static data members of reference or const scalar |
| 2838 | // type, since they will never get initializers. |
| 2839 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 2840 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor()) { |
| 2841 | bool Complained = false; |
| 2842 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 2843 | FEnd = Record->field_end(); |
| 2844 | F != FEnd; ++F) { |
| 2845 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 2846 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2847 | if (!Complained) { |
| 2848 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 2849 | << Record->getTagKind() << Record; |
| 2850 | Complained = true; |
| 2851 | } |
| 2852 | |
| 2853 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 2854 | << F->getType()->isReferenceType() |
| 2855 | << F->getDeclName(); |
| 2856 | } |
| 2857 | } |
| 2858 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2859 | |
Anders Carlsson | a5c6c2a | 2011-01-25 18:08:22 +0000 | [diff] [blame] | 2860 | if (Record->isDynamicClass() && !Record->isDependentType()) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2861 | DynamicClasses.push_back(Record); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 2862 | |
| 2863 | if (Record->getIdentifier()) { |
| 2864 | // C++ [class.mem]p13: |
| 2865 | // If T is the name of a class, then each of the following shall have a |
| 2866 | // name different from T: |
| 2867 | // - every member of every anonymous union that is a member of class T. |
| 2868 | // |
| 2869 | // C++ [class.mem]p14: |
| 2870 | // In addition, if class T has a user-declared constructor (12.1), every |
| 2871 | // non-static data member of class T shall have a name different from T. |
| 2872 | for (DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2873 | R.first != R.second; ++R.first) { |
| 2874 | NamedDecl *D = *R.first; |
| 2875 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 2876 | isa<IndirectFieldDecl>(D)) { |
| 2877 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 2878 | << D->getDeclName(); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 2879 | break; |
| 2880 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2881 | } |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 2882 | } |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 2883 | |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 2884 | // Warn if the class has virtual methods but non-virtual public destructor. |
Douglas Gregor | f4b793c | 2011-02-19 19:14:36 +0000 | [diff] [blame] | 2885 | if (Record->isPolymorphic() && !Record->isDependentType()) { |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 2886 | CXXDestructorDecl *dtor = Record->getDestructor(); |
Argyrios Kyrtzidis | 9641fc8 | 2011-01-31 17:10:25 +0000 | [diff] [blame] | 2887 | if (!dtor || (!dtor->isVirtual() && dtor->getAccess() == AS_public)) |
Argyrios Kyrtzidis | def4e2a | 2011-01-31 07:05:00 +0000 | [diff] [blame] | 2888 | Diag(dtor ? dtor->getLocation() : Record->getLocation(), |
| 2889 | diag::warn_non_virtual_dtor) << Context.getRecordType(Record); |
| 2890 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 2891 | |
| 2892 | // See if a method overloads virtual methods in a base |
| 2893 | /// class without overriding any. |
| 2894 | if (!Record->isDependentType()) { |
| 2895 | for (CXXRecordDecl::method_iterator M = Record->method_begin(), |
| 2896 | MEnd = Record->method_end(); |
| 2897 | M != MEnd; ++M) { |
Argyrios Kyrtzidis | 0266aa3 | 2011-03-03 22:58:57 +0000 | [diff] [blame] | 2898 | if (!(*M)->isStatic()) |
| 2899 | DiagnoseHiddenVirtualMethods(Record, *M); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 2900 | } |
| 2901 | } |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 2902 | |
| 2903 | // Declare inherited constructors. We do this eagerly here because: |
| 2904 | // - The standard requires an eager diagnostic for conflicting inherited |
| 2905 | // constructors from different classes. |
| 2906 | // - The lazy declaration of the other implicit constructors is so as to not |
| 2907 | // waste space and performance on classes that are not meant to be |
| 2908 | // instantiated (e.g. meta-functions). This doesn't apply to classes that |
| 2909 | // have inherited constructors. |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 2910 | DeclareInheritedConstructors(Record); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | /// \brief Data used with FindHiddenVirtualMethod |
Benjamin Kramer | c54061a | 2011-03-04 13:12:48 +0000 | [diff] [blame] | 2914 | namespace { |
| 2915 | struct FindHiddenVirtualMethodData { |
| 2916 | Sema *S; |
| 2917 | CXXMethodDecl *Method; |
| 2918 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverridenAndUsingBaseMethods; |
| 2919 | llvm::SmallVector<CXXMethodDecl *, 8> OverloadedMethods; |
| 2920 | }; |
| 2921 | } |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 2922 | |
| 2923 | /// \brief Member lookup function that determines whether a given C++ |
| 2924 | /// method overloads virtual methods in a base class without overriding any, |
| 2925 | /// to be used with CXXRecordDecl::lookupInBases(). |
| 2926 | static bool FindHiddenVirtualMethod(const CXXBaseSpecifier *Specifier, |
| 2927 | CXXBasePath &Path, |
| 2928 | void *UserData) { |
| 2929 | RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl(); |
| 2930 | |
| 2931 | FindHiddenVirtualMethodData &Data |
| 2932 | = *static_cast<FindHiddenVirtualMethodData*>(UserData); |
| 2933 | |
| 2934 | DeclarationName Name = Data.Method->getDeclName(); |
| 2935 | assert(Name.getNameKind() == DeclarationName::Identifier); |
| 2936 | |
| 2937 | bool foundSameNameMethod = false; |
| 2938 | llvm::SmallVector<CXXMethodDecl *, 8> overloadedMethods; |
| 2939 | for (Path.Decls = BaseRecord->lookup(Name); |
| 2940 | Path.Decls.first != Path.Decls.second; |
| 2941 | ++Path.Decls.first) { |
| 2942 | NamedDecl *D = *Path.Decls.first; |
| 2943 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 2944 | MD = MD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 2945 | foundSameNameMethod = true; |
| 2946 | // Interested only in hidden virtual methods. |
| 2947 | if (!MD->isVirtual()) |
| 2948 | continue; |
| 2949 | // If the method we are checking overrides a method from its base |
| 2950 | // don't warn about the other overloaded methods. |
| 2951 | if (!Data.S->IsOverload(Data.Method, MD, false)) |
| 2952 | return true; |
| 2953 | // Collect the overload only if its hidden. |
| 2954 | if (!Data.OverridenAndUsingBaseMethods.count(MD)) |
| 2955 | overloadedMethods.push_back(MD); |
| 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | if (foundSameNameMethod) |
| 2960 | Data.OverloadedMethods.append(overloadedMethods.begin(), |
| 2961 | overloadedMethods.end()); |
| 2962 | return foundSameNameMethod; |
| 2963 | } |
| 2964 | |
| 2965 | /// \brief See if a method overloads virtual methods in a base class without |
| 2966 | /// overriding any. |
| 2967 | void Sema::DiagnoseHiddenVirtualMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) { |
| 2968 | if (Diags.getDiagnosticLevel(diag::warn_overloaded_virtual, |
| 2969 | MD->getLocation()) == Diagnostic::Ignored) |
| 2970 | return; |
| 2971 | if (MD->getDeclName().getNameKind() != DeclarationName::Identifier) |
| 2972 | return; |
| 2973 | |
| 2974 | CXXBasePaths Paths(/*FindAmbiguities=*/true, // true to look in all bases. |
| 2975 | /*bool RecordPaths=*/false, |
| 2976 | /*bool DetectVirtual=*/false); |
| 2977 | FindHiddenVirtualMethodData Data; |
| 2978 | Data.Method = MD; |
| 2979 | Data.S = this; |
| 2980 | |
| 2981 | // Keep the base methods that were overriden or introduced in the subclass |
| 2982 | // by 'using' in a set. A base method not in this set is hidden. |
| 2983 | for (DeclContext::lookup_result res = DC->lookup(MD->getDeclName()); |
| 2984 | res.first != res.second; ++res.first) { |
| 2985 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*res.first)) |
| 2986 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 2987 | E = MD->end_overridden_methods(); |
| 2988 | I != E; ++I) |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 2989 | Data.OverridenAndUsingBaseMethods.insert((*I)->getCanonicalDecl()); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 2990 | if (UsingShadowDecl *shad = dyn_cast<UsingShadowDecl>(*res.first)) |
| 2991 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(shad->getTargetDecl())) |
Argyrios Kyrtzidis | 74b47f9 | 2011-02-10 18:13:41 +0000 | [diff] [blame] | 2992 | Data.OverridenAndUsingBaseMethods.insert(MD->getCanonicalDecl()); |
Argyrios Kyrtzidis | 799ef66 | 2011-02-03 18:01:15 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | if (DC->lookupInBases(&FindHiddenVirtualMethod, &Data, Paths) && |
| 2996 | !Data.OverloadedMethods.empty()) { |
| 2997 | Diag(MD->getLocation(), diag::warn_overloaded_virtual) |
| 2998 | << MD << (Data.OverloadedMethods.size() > 1); |
| 2999 | |
| 3000 | for (unsigned i = 0, e = Data.OverloadedMethods.size(); i != e; ++i) { |
| 3001 | CXXMethodDecl *overloadedMD = Data.OverloadedMethods[i]; |
| 3002 | Diag(overloadedMD->getLocation(), |
| 3003 | diag::note_hidden_overloaded_virtual_declared_here) << overloadedMD; |
| 3004 | } |
| 3005 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 3008 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3009 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 3010 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 3011 | SourceLocation RBrac, |
| 3012 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 3013 | if (!TagDecl) |
| 3014 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3015 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 3016 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 3017 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 3018 | ActOnFields(S, RLoc, TagDecl, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3019 | // strict aliasing violation! |
| 3020 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 3021 | FieldCollector->getCurNumFields(), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3022 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3023 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3024 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3027 | namespace { |
| 3028 | /// \brief Helper class that collects exception specifications for |
| 3029 | /// implicitly-declared special member functions. |
| 3030 | class ImplicitExceptionSpecification { |
| 3031 | ASTContext &Context; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3032 | // We order exception specifications thus: |
| 3033 | // noexcept is the most restrictive, but is only used in C++0x. |
| 3034 | // throw() comes next. |
| 3035 | // Then a throw(collected exceptions) |
| 3036 | // Finally no specification. |
| 3037 | // throw(...) is used instead if any called function uses it. |
| 3038 | ExceptionSpecificationType ComputedEST; |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3039 | llvm::SmallPtrSet<CanQualType, 4> ExceptionsSeen; |
| 3040 | llvm::SmallVector<QualType, 4> Exceptions; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3041 | |
| 3042 | void ClearExceptions() { |
| 3043 | ExceptionsSeen.clear(); |
| 3044 | Exceptions.clear(); |
| 3045 | } |
| 3046 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3047 | public: |
| 3048 | explicit ImplicitExceptionSpecification(ASTContext &Context) |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3049 | : Context(Context), ComputedEST(EST_BasicNoexcept) { |
| 3050 | if (!Context.getLangOptions().CPlusPlus0x) |
| 3051 | ComputedEST = EST_DynamicNone; |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3052 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3053 | |
| 3054 | /// \brief Get the computed exception specification type. |
| 3055 | ExceptionSpecificationType getExceptionSpecType() const { |
| 3056 | assert(ComputedEST != EST_ComputedNoexcept && |
| 3057 | "noexcept(expr) should not be a possible result"); |
| 3058 | return ComputedEST; |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3059 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3060 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3061 | /// \brief The number of exceptions in the exception specification. |
| 3062 | unsigned size() const { return Exceptions.size(); } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3063 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3064 | /// \brief The set of exceptions in the exception specification. |
| 3065 | const QualType *data() const { return Exceptions.data(); } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3066 | |
| 3067 | /// \brief Integrate another called method into the collected data. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3068 | void CalledDecl(CXXMethodDecl *Method) { |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3069 | // If we have an MSAny spec already, don't bother. |
| 3070 | if (!Method || ComputedEST == EST_MSAny) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3071 | return; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3072 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3073 | const FunctionProtoType *Proto |
| 3074 | = Method->getType()->getAs<FunctionProtoType>(); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3075 | |
| 3076 | ExceptionSpecificationType EST = Proto->getExceptionSpecType(); |
| 3077 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3078 | // If this function can throw any exceptions, make a note of that. |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3079 | if (EST == EST_MSAny || EST == EST_None) { |
| 3080 | ClearExceptions(); |
| 3081 | ComputedEST = EST; |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3082 | return; |
| 3083 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3084 | |
| 3085 | // If this function has a basic noexcept, it doesn't affect the outcome. |
| 3086 | if (EST == EST_BasicNoexcept) |
| 3087 | return; |
| 3088 | |
| 3089 | // If we have a throw-all spec at this point, ignore the function. |
| 3090 | if (ComputedEST == EST_None) |
| 3091 | return; |
| 3092 | |
| 3093 | // If we're still at noexcept(true) and there's a nothrow() callee, |
| 3094 | // change to that specification. |
| 3095 | if (EST == EST_DynamicNone) { |
| 3096 | if (ComputedEST == EST_BasicNoexcept) |
| 3097 | ComputedEST = EST_DynamicNone; |
| 3098 | return; |
| 3099 | } |
| 3100 | |
| 3101 | // Check out noexcept specs. |
| 3102 | if (EST == EST_ComputedNoexcept) { |
Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 3103 | FunctionProtoType::NoexceptResult NR = Proto->getNoexceptSpec(Context); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3104 | assert(NR != FunctionProtoType::NR_NoNoexcept && |
| 3105 | "Must have noexcept result for EST_ComputedNoexcept."); |
| 3106 | assert(NR != FunctionProtoType::NR_Dependent && |
| 3107 | "Should not generate implicit declarations for dependent cases, " |
| 3108 | "and don't know how to handle them anyway."); |
| 3109 | |
| 3110 | // noexcept(false) -> no spec on the new function |
| 3111 | if (NR == FunctionProtoType::NR_Throw) { |
| 3112 | ClearExceptions(); |
| 3113 | ComputedEST = EST_None; |
| 3114 | } |
| 3115 | // noexcept(true) won't change anything either. |
| 3116 | return; |
| 3117 | } |
| 3118 | |
| 3119 | assert(EST == EST_Dynamic && "EST case not considered earlier."); |
| 3120 | assert(ComputedEST != EST_None && |
| 3121 | "Shouldn't collect exceptions when throw-all is guaranteed."); |
| 3122 | ComputedEST = EST_Dynamic; |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3123 | // Record the exceptions in this function's exception specification. |
| 3124 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 3125 | EEnd = Proto->exception_end(); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3126 | E != EEnd; ++E) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3127 | if (ExceptionsSeen.insert(Context.getCanonicalType(*E))) |
| 3128 | Exceptions.push_back(*E); |
| 3129 | } |
| 3130 | }; |
| 3131 | } |
| 3132 | |
| 3133 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 3134 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 3135 | /// special functions, such as the default constructor, copy |
| 3136 | /// constructor, or destructor, to the given C++ class (C++ |
| 3137 | /// [special]p1). This routine can only be executed just before the |
| 3138 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3139 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 3140 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 3141 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 3142 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 3143 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 3144 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 3145 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 3146 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 3147 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
| 3148 | |
| 3149 | // If we have a dynamic class, then the copy assignment operator may be |
| 3150 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
| 3151 | // it shows up in the right place in the vtable and that we diagnose |
| 3152 | // problems with the implicit exception specification. |
| 3153 | if (ClassDecl->isDynamicClass()) |
| 3154 | DeclareImplicitCopyAssignment(ClassDecl); |
| 3155 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3156 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 3157 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 3158 | ++ASTContext::NumImplicitDestructors; |
| 3159 | |
| 3160 | // If we have a dynamic class, then the destructor may be virtual, so we |
| 3161 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 3162 | // shows up in the right place in the vtable and that we diagnose problems |
| 3163 | // with the implicit exception specification. |
| 3164 | if (ClassDecl->isDynamicClass()) |
| 3165 | DeclareImplicitDestructor(ClassDecl); |
| 3166 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3169 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 3170 | if (!D) |
| 3171 | return; |
| 3172 | |
| 3173 | TemplateParameterList *Params = 0; |
| 3174 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 3175 | Params = Template->getTemplateParameters(); |
| 3176 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 3177 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 3178 | Params = PartialSpec->getTemplateParameters(); |
| 3179 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3180 | return; |
| 3181 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3182 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 3183 | ParamEnd = Params->end(); |
| 3184 | Param != ParamEnd; ++Param) { |
| 3185 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 3186 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3187 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 3188 | IdResolver.AddDecl(Named); |
| 3189 | } |
| 3190 | } |
| 3191 | } |
| 3192 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3193 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 3194 | if (!RecordD) return; |
| 3195 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3196 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 3197 | PushDeclContext(S, Record); |
| 3198 | } |
| 3199 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3200 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 3201 | if (!RecordD) return; |
| 3202 | PopDeclContext(); |
| 3203 | } |
| 3204 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3205 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 3206 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 3207 | /// parsing those parts of the given Method declaration that could |
| 3208 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 3209 | /// arguments. This action should enter the scope of the given |
| 3210 | /// Method declaration as if we had just parsed the qualified method |
| 3211 | /// name. However, it should not bring the parameters into scope; |
| 3212 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3213 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
| 3216 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 3217 | /// C++ method declaration. We're (re-)introducing the given |
| 3218 | /// function parameter into scope for use in parsing later parts of |
| 3219 | /// the method declaration. For example, we could see an |
| 3220 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3221 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 3222 | if (!ParamD) |
| 3223 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3224 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3225 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 3226 | |
| 3227 | // If this parameter has an unparsed default argument, clear it out |
| 3228 | // to make way for the parsed default argument. |
| 3229 | if (Param->hasUnparsedDefaultArg()) |
| 3230 | Param->setDefaultArg(0); |
| 3231 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3232 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3233 | if (Param->getDeclName()) |
| 3234 | IdResolver.AddDecl(Param); |
| 3235 | } |
| 3236 | |
| 3237 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 3238 | /// processing the delayed method declaration for Method. The method |
| 3239 | /// declaration is now considered finished. There may be a separate |
| 3240 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 3241 | /// immediately!) for this method, if it was also defined inside the |
| 3242 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3243 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 3244 | if (!MethodD) |
| 3245 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3246 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 3247 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3249 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3250 | |
| 3251 | // Now that we have our default arguments, check the constructor |
| 3252 | // again. It could produce additional diagnostics or affect whether |
| 3253 | // the class has implicitly-declared destructors, among other |
| 3254 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3255 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 3256 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3257 | |
| 3258 | // Check the default arguments, which we may have added. |
| 3259 | if (!Method->isInvalidDecl()) |
| 3260 | CheckCXXDefaultArguments(Method); |
| 3261 | } |
| 3262 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3263 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3264 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3265 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3266 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 3267 | /// will be updated to reflect a well-formed type for the constructor and |
| 3268 | /// returned. |
| 3269 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3270 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3271 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3272 | |
| 3273 | // C++ [class.ctor]p3: |
| 3274 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 3275 | // constructor can be invoked for a const, volatile or const |
| 3276 | // volatile object. A constructor shall not be declared const, |
| 3277 | // volatile, or const volatile (9.3.2). |
| 3278 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3279 | if (!D.isInvalidType()) |
| 3280 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 3281 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 3282 | << SourceRange(D.getIdentifierLoc()); |
| 3283 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3284 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3285 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3286 | if (!D.isInvalidType()) |
| 3287 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 3288 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 3289 | << SourceRange(D.getIdentifierLoc()); |
| 3290 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3291 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3292 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3293 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3294 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3295 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3296 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3297 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 3298 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3299 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3300 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 3301 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3302 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3303 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 3304 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3305 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3306 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3307 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 3308 | // C++0x [class.ctor]p4: |
| 3309 | // A constructor shall not be declared with a ref-qualifier. |
| 3310 | if (FTI.hasRefQualifier()) { |
| 3311 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_constructor) |
| 3312 | << FTI.RefQualifierIsLValueRef |
| 3313 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 3314 | D.setInvalidType(); |
| 3315 | } |
| 3316 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3317 | // Rebuild the function type "R" without any type qualifiers (in |
| 3318 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3319 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3320 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3321 | if (Proto->getResultType() == Context.VoidTy && !D.isInvalidType()) |
| 3322 | return R; |
| 3323 | |
| 3324 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 3325 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 3326 | EPI.RefQualifier = RQ_None; |
| 3327 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3328 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3329 | Proto->getNumArgs(), EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3330 | } |
| 3331 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3332 | /// CheckConstructor - Checks a fully-formed constructor for |
| 3333 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 3334 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3335 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3336 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 3337 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 3338 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3339 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3340 | |
| 3341 | // C++ [class.copy]p3: |
| 3342 | // A declaration of a constructor for a class X is ill-formed if |
| 3343 | // its first parameter is of type (optionally cv-qualified) X and |
| 3344 | // either there are no other parameters or else all other |
| 3345 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 3346 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3347 | ((Constructor->getNumParams() == 1) || |
| 3348 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3349 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 3350 | Constructor->getTemplateSpecializationKind() |
| 3351 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3352 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 3353 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 3354 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3355 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 3356 | const char *ConstRef |
| 3357 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 3358 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3359 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 3360 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 3361 | |
| 3362 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 3363 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3364 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3365 | } |
| 3366 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 3369 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 3370 | /// well-formedness, issuing any diagnostics required. Returns true |
| 3371 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 3372 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 3373 | CXXRecordDecl *RD = Destructor->getParent(); |
| 3374 | |
| 3375 | if (Destructor->isVirtual()) { |
| 3376 | SourceLocation Loc; |
| 3377 | |
| 3378 | if (!Destructor->isImplicit()) |
| 3379 | Loc = Destructor->getLocation(); |
| 3380 | else |
| 3381 | Loc = RD->getLocation(); |
| 3382 | |
| 3383 | // If we have a virtual destructor, look up the deallocation function |
| 3384 | FunctionDecl *OperatorDelete = 0; |
| 3385 | DeclarationName Name = |
| 3386 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 3387 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 3388 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 3389 | |
| 3390 | MarkDeclarationReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 3391 | |
| 3392 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 3393 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 3394 | |
| 3395 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3398 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 3399 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 3400 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 3401 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3402 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3405 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 3406 | /// the well-formednes of the destructor declarator @p D with type @p |
| 3407 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3408 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 3409 | /// will be updated to reflect a well-formed type for the destructor and |
| 3410 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3411 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3412 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3413 | // C++ [class.dtor]p1: |
| 3414 | // [...] A typedef-name that names a class is a class-name |
| 3415 | // (7.1.3); however, a typedef-name that names a class shall not |
| 3416 | // be used as the identifier in the declarator for a destructor |
| 3417 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3418 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3419 | if (const TypedefType *TT = DeclaratorType->getAs<TypedefType>()) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3420 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3421 | << DeclaratorType << isa<TypeAliasDecl>(TT->getDecl()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3422 | |
| 3423 | // C++ [class.dtor]p2: |
| 3424 | // A destructor is used to destroy objects of its class type. A |
| 3425 | // destructor takes no parameters, and no return type can be |
| 3426 | // specified for it (not even void). The address of a destructor |
| 3427 | // shall not be taken. A destructor shall not be static. A |
| 3428 | // destructor can be invoked for a const, volatile or const |
| 3429 | // volatile object. A destructor shall not be declared const, |
| 3430 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3431 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3432 | if (!D.isInvalidType()) |
| 3433 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 3434 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3435 | << SourceRange(D.getIdentifierLoc()) |
| 3436 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 3437 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3438 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3439 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3440 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3441 | // Destructors don't have return types, but the parser will |
| 3442 | // happily parse something like: |
| 3443 | // |
| 3444 | // class X { |
| 3445 | // float ~X(); |
| 3446 | // }; |
| 3447 | // |
| 3448 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3449 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 3450 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 3451 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3453 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3454 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3455 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3456 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3457 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 3458 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3459 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3460 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 3461 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3462 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3463 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 3464 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3465 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3466 | } |
| 3467 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 3468 | // C++0x [class.dtor]p2: |
| 3469 | // A destructor shall not be declared with a ref-qualifier. |
| 3470 | if (FTI.hasRefQualifier()) { |
| 3471 | Diag(FTI.getRefQualifierLoc(), diag::err_ref_qualifier_destructor) |
| 3472 | << FTI.RefQualifierIsLValueRef |
| 3473 | << FixItHint::CreateRemoval(FTI.getRefQualifierLoc()); |
| 3474 | D.setInvalidType(); |
| 3475 | } |
| 3476 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3477 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 3478 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3479 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 3480 | |
| 3481 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3482 | FTI.freeArgs(); |
| 3483 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3486 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3487 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3488 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3489 | D.setInvalidType(); |
| 3490 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3491 | |
| 3492 | // Rebuild the function type "R" without any type qualifiers or |
| 3493 | // parameters (in case any of the errors above fired) and with |
| 3494 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3495 | // types. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3496 | if (!D.isInvalidType()) |
| 3497 | return R; |
| 3498 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3499 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3500 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 3501 | EPI.Variadic = false; |
| 3502 | EPI.TypeQuals = 0; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 3503 | EPI.RefQualifier = RQ_None; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3504 | return Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3507 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 3508 | /// well-formednes of the conversion function declarator @p D with |
| 3509 | /// type @p R. If there are any errors in the declarator, this routine |
| 3510 | /// will emit diagnostics and return true. Otherwise, it will return |
| 3511 | /// false. Either way, the type @p R will be updated to reflect a |
| 3512 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3513 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3514 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3515 | // C++ [class.conv.fct]p1: |
| 3516 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3517 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3518 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3519 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3520 | if (!D.isInvalidType()) |
| 3521 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 3522 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 3523 | << SourceRange(D.getIdentifierLoc()); |
| 3524 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3525 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3526 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3527 | |
| 3528 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 3529 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3530 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3531 | // Conversion functions don't have return types, but the parser will |
| 3532 | // happily parse something like: |
| 3533 | // |
| 3534 | // class X { |
| 3535 | // float operator bool(); |
| 3536 | // }; |
| 3537 | // |
| 3538 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3539 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 3540 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 3541 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3542 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3545 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 3546 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3547 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3548 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3549 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 3550 | |
| 3551 | // Delete the parameters. |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3552 | D.getFunctionTypeInfo().freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3553 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3554 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3555 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3556 | D.setInvalidType(); |
| 3557 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3558 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3559 | // Diagnose "&operator bool()" and other such nonsense. This |
| 3560 | // is actually a gcc extension which we don't support. |
| 3561 | if (Proto->getResultType() != ConvType) { |
| 3562 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 3563 | << Proto->getResultType(); |
| 3564 | D.setInvalidType(); |
| 3565 | ConvType = Proto->getResultType(); |
| 3566 | } |
| 3567 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3568 | // C++ [class.conv.fct]p4: |
| 3569 | // The conversion-type-id shall not represent a function type nor |
| 3570 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3571 | if (ConvType->isArrayType()) { |
| 3572 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 3573 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3574 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3575 | } else if (ConvType->isFunctionType()) { |
| 3576 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 3577 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3578 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | // Rebuild the function type "R" without any parameters (in case any |
| 3582 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3583 | // return type. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3584 | if (D.isInvalidType()) |
| 3585 | R = Context.getFunctionType(ConvType, 0, 0, Proto->getExtProtoInfo()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3586 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3587 | // C++0x explicit conversion operators. |
| 3588 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3589 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3590 | diag::warn_explicit_conversion_functions) |
| 3591 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3594 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 3595 | /// the declaration of the given C++ conversion function. This routine |
| 3596 | /// is responsible for recording the conversion function in the C++ |
| 3597 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3598 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3599 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 3600 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 3601 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3602 | |
| 3603 | // Make sure we aren't redeclaring the conversion function. |
| 3604 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3605 | |
| 3606 | // C++ [class.conv.fct]p1: |
| 3607 | // [...] A conversion function is never used to convert a |
| 3608 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 3609 | // same object type (or a reference to it), to a (possibly |
| 3610 | // cv-qualified) base class of that type (or a reference to it), |
| 3611 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3612 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 3613 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3614 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3615 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3616 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3617 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 3618 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 3619 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 3620 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 3621 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3622 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 3623 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3624 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3625 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3626 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3627 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3628 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3629 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3630 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3631 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 3634 | if (FunctionTemplateDecl *ConversionTemplate |
| 3635 | = Conversion->getDescribedFunctionTemplate()) |
| 3636 | return ConversionTemplate; |
| 3637 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3638 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3641 | //===----------------------------------------------------------------------===// |
| 3642 | // Namespace Handling |
| 3643 | //===----------------------------------------------------------------------===// |
| 3644 | |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3645 | |
| 3646 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3647 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 3648 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3649 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 3650 | SourceLocation InlineLoc, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 3651 | SourceLocation NamespaceLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3652 | SourceLocation IdentLoc, |
| 3653 | IdentifierInfo *II, |
| 3654 | SourceLocation LBrace, |
| 3655 | AttributeList *AttrList) { |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 3656 | SourceLocation StartLoc = InlineLoc.isValid() ? InlineLoc : NamespaceLoc; |
| 3657 | // For anonymous namespace, take the location of the left brace. |
| 3658 | SourceLocation Loc = II ? IdentLoc : LBrace; |
Douglas Gregor | 21e09b6 | 2010-08-19 20:55:47 +0000 | [diff] [blame] | 3659 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 3660 | StartLoc, Loc, II); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3661 | Namespc->setInline(InlineLoc.isValid()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3662 | |
| 3663 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 3664 | |
Anders Carlsson | 2a3503d | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 3665 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 3666 | |
John McCall | 90f1450 | 2010-12-10 02:59:44 +0000 | [diff] [blame] | 3667 | if (const VisibilityAttr *Attr = Namespc->getAttr<VisibilityAttr>()) |
| 3668 | PushNamespaceVisibilityAttr(Attr); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3669 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3670 | if (II) { |
| 3671 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 3672 | // The identifier in an original-namespace-definition shall not |
| 3673 | // have been previously defined in the declarative region in |
| 3674 | // which the original-namespace-definition appears. The |
| 3675 | // identifier in an original-namespace-definition is the name of |
| 3676 | // the namespace. Subsequently in that declarative region, it is |
| 3677 | // treated as an original-namespace-name. |
| 3678 | // |
| 3679 | // Since namespace names are unique in their scope, and we don't |
| 3680 | // look through using directives, just |
| 3681 | DeclContext::lookup_result R = CurContext->getRedeclContext()->lookup(II); |
| 3682 | NamedDecl *PrevDecl = R.first == R.second? 0 : *R.first; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3683 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3684 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 3685 | // This is an extended namespace definition. |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3686 | if (Namespc->isInline() != OrigNS->isInline()) { |
| 3687 | // inline-ness must match |
| 3688 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 3689 | << Namespc->isInline(); |
| 3690 | Diag(OrigNS->getLocation(), diag::note_previous_definition); |
| 3691 | Namespc->setInvalidDecl(); |
| 3692 | // Recover by ignoring the new namespace's inline status. |
| 3693 | Namespc->setInline(OrigNS->isInline()); |
| 3694 | } |
| 3695 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3696 | // Attach this namespace decl to the chain of extended namespace |
| 3697 | // definitions. |
| 3698 | OrigNS->setNextNamespace(Namespc); |
| 3699 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3700 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | // Remove the previous declaration from the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3702 | if (DeclRegionScope->isDeclScope(OrigNS)) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 3703 | IdResolver.RemoveDecl(OrigNS); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3704 | DeclRegionScope->RemoveDecl(OrigNS); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3705 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3706 | } else if (PrevDecl) { |
| 3707 | // This is an invalid name redefinition. |
| 3708 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 3709 | << Namespc->getDeclName(); |
| 3710 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3711 | Namespc->setInvalidDecl(); |
| 3712 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3713 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3714 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3715 | // This is the first "real" definition of the namespace "std", so update |
| 3716 | // our cache of the "std" namespace to point at this definition. |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3717 | if (NamespaceDecl *StdNS = getStdNamespace()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3718 | // We had already defined a dummy namespace "std". Link this new |
| 3719 | // namespace definition to the dummy namespace "std". |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3720 | StdNS->setNextNamespace(Namespc); |
| 3721 | StdNS->setLocation(IdentLoc); |
| 3722 | Namespc->setOriginalNamespace(StdNS->getOriginalNamespace()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | // Make our StdNamespace cache point at the first real definition of the |
| 3726 | // "std" namespace. |
| 3727 | StdNamespace = Namespc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3728 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3729 | |
| 3730 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 3731 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3732 | // Anonymous namespaces. |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3733 | assert(Namespc->isAnonymousNamespace()); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3734 | |
| 3735 | // Link the anonymous namespace into its parent. |
| 3736 | NamespaceDecl *PrevDecl; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3737 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3738 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 3739 | PrevDecl = TU->getAnonymousNamespace(); |
| 3740 | TU->setAnonymousNamespace(Namespc); |
| 3741 | } else { |
| 3742 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 3743 | PrevDecl = ND->getAnonymousNamespace(); |
| 3744 | ND->setAnonymousNamespace(Namespc); |
| 3745 | } |
| 3746 | |
| 3747 | // Link the anonymous namespace with its previous declaration. |
| 3748 | if (PrevDecl) { |
| 3749 | assert(PrevDecl->isAnonymousNamespace()); |
| 3750 | assert(!PrevDecl->getNextNamespace()); |
| 3751 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 3752 | PrevDecl->setNextNamespace(Namespc); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3753 | |
| 3754 | if (Namespc->isInline() != PrevDecl->isInline()) { |
| 3755 | // inline-ness must match |
| 3756 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 3757 | << Namespc->isInline(); |
| 3758 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3759 | Namespc->setInvalidDecl(); |
| 3760 | // Recover by ignoring the new namespace's inline status. |
| 3761 | Namespc->setInline(PrevDecl->isInline()); |
| 3762 | } |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3763 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3764 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 3765 | CurContext->addDecl(Namespc); |
| 3766 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3767 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 3768 | // behaves as if it were replaced by |
| 3769 | // namespace unique { /* empty body */ } |
| 3770 | // using namespace unique; |
| 3771 | // namespace unique { namespace-body } |
| 3772 | // where all occurrences of 'unique' in a translation unit are |
| 3773 | // replaced by the same identifier and this identifier differs |
| 3774 | // from all other identifiers in the entire program. |
| 3775 | |
| 3776 | // We just create the namespace with an empty name and then add an |
| 3777 | // implicit using declaration, just like the standard suggests. |
| 3778 | // |
| 3779 | // CodeGen enforces the "universally unique" aspect by giving all |
| 3780 | // declarations semantically contained within an anonymous |
| 3781 | // namespace internal linkage. |
| 3782 | |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3783 | if (!PrevDecl) { |
| 3784 | UsingDirectiveDecl* UD |
| 3785 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 3786 | /* 'using' */ LBrace, |
| 3787 | /* 'namespace' */ SourceLocation(), |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 3788 | /* qualifier */ NestedNameSpecifierLoc(), |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3789 | /* identifier */ SourceLocation(), |
| 3790 | Namespc, |
| 3791 | /* Ancestor */ CurContext); |
| 3792 | UD->setImplicit(); |
| 3793 | CurContext->addDecl(UD); |
| 3794 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 3798 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3799 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 3800 | // for the namespace has the declarations that showed up in that particular |
| 3801 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3802 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3803 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3806 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 3807 | /// is a namespace alias, returns the namespace it points to. |
| 3808 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 3809 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 3810 | return AD->getNamespace(); |
| 3811 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 3812 | } |
| 3813 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3814 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 3815 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3816 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3817 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 3818 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 3819 | Namespc->setRBraceLoc(RBrace); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3820 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3821 | if (Namespc->hasAttr<VisibilityAttr>()) |
| 3822 | PopPragmaVisibility(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3823 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3824 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 3825 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 3826 | return cast_or_null<CXXRecordDecl>( |
| 3827 | StdBadAlloc.get(Context.getExternalSource())); |
| 3828 | } |
| 3829 | |
| 3830 | NamespaceDecl *Sema::getStdNamespace() const { |
| 3831 | return cast_or_null<NamespaceDecl>( |
| 3832 | StdNamespace.get(Context.getExternalSource())); |
| 3833 | } |
| 3834 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3835 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 3836 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 3837 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3838 | if (!StdNamespace) { |
| 3839 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 3840 | StdNamespace = NamespaceDecl::Create(Context, |
| 3841 | Context.getTranslationUnitDecl(), |
Abramo Bagnara | acba90f | 2011-03-08 12:38:20 +0000 | [diff] [blame] | 3842 | SourceLocation(), SourceLocation(), |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3843 | &PP.getIdentifierTable().get("std")); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3844 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3847 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 3850 | /// \brief Determine whether a using statement is in a context where it will be |
| 3851 | /// apply in all contexts. |
| 3852 | static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) { |
| 3853 | switch (CurContext->getDeclKind()) { |
| 3854 | case Decl::TranslationUnit: |
| 3855 | return true; |
| 3856 | case Decl::LinkageSpec: |
| 3857 | return IsUsingDirectiveInToplevelContext(CurContext->getParent()); |
| 3858 | default: |
| 3859 | return false; |
| 3860 | } |
| 3861 | } |
| 3862 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3863 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3864 | SourceLocation UsingLoc, |
| 3865 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3866 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3867 | SourceLocation IdentLoc, |
| 3868 | IdentifierInfo *NamespcName, |
| 3869 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3870 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3871 | assert(NamespcName && "Invalid NamespcName."); |
| 3872 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 3873 | |
| 3874 | // This can only happen along a recovery path. |
| 3875 | while (S->getFlags() & Scope::TemplateParamScope) |
| 3876 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3877 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3878 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3879 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3880 | NestedNameSpecifier *Qualifier = 0; |
| 3881 | if (SS.isSet()) |
| 3882 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3883 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 3884 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3885 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 3886 | LookupParsedName(R, S, &SS); |
| 3887 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3888 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3889 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3890 | if (R.empty()) { |
| 3891 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 3892 | // "std" hasn't been defined yet, for GCC compatibility. |
| 3893 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 3894 | NamespcName->isStr("std")) { |
| 3895 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 3896 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3897 | R.resolveKind(); |
| 3898 | } |
| 3899 | // Otherwise, attempt typo correction. |
| 3900 | else if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 3901 | CTC_NoKeywords, 0)) { |
| 3902 | if (R.getAsSingle<NamespaceDecl>() || |
| 3903 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 3904 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 3905 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 3906 | << NamespcName << DC << Corrected << SS.getRange() |
| 3907 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 3908 | else |
| 3909 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 3910 | << NamespcName << Corrected |
| 3911 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 3912 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 3913 | << Corrected; |
| 3914 | |
| 3915 | NamespcName = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3916 | } else { |
| 3917 | R.clear(); |
| 3918 | R.setLookupName(NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3919 | } |
| 3920 | } |
| 3921 | } |
| 3922 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3923 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3924 | NamedDecl *Named = R.getFoundDecl(); |
| 3925 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 3926 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3927 | // C++ [namespace.udir]p1: |
| 3928 | // A using-directive specifies that the names in the nominated |
| 3929 | // namespace can be used in the scope in which the |
| 3930 | // using-directive appears after the using-directive. During |
| 3931 | // unqualified name lookup (3.4.1), the names appear as if they |
| 3932 | // were declared in the nearest enclosing namespace which |
| 3933 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3934 | // namespace. [Note: in this context, "contains" means "contains |
| 3935 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3936 | |
| 3937 | // Find enclosing context containing both using-directive and |
| 3938 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3939 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3940 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 3941 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 3942 | CommonAncestor = CommonAncestor->getParent(); |
| 3943 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3944 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 3945 | SS.getWithLocInContext(Context), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3946 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 3947 | |
Douglas Gregor | 9172aa6 | 2011-03-26 22:25:30 +0000 | [diff] [blame] | 3948 | if (IsUsingDirectiveInToplevelContext(CurContext) && |
Nico Weber | 2166948 | 2011-04-02 19:45:15 +0000 | [diff] [blame] | 3949 | !SourceMgr.isFromMainFile(SourceMgr.getInstantiationLoc(IdentLoc))) { |
Douglas Gregor | d6a49bb | 2011-03-18 16:10:52 +0000 | [diff] [blame] | 3950 | Diag(IdentLoc, diag::warn_using_directive_in_header); |
| 3951 | } |
| 3952 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3953 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3954 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 3955 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3958 | // FIXME: We ignore attributes for now. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3959 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 3963 | // If scope has associated entity, then using directive is at namespace |
| 3964 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 3965 | // it's lookup structure. |
| 3966 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3967 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3968 | else |
| 3969 | // Otherwise it is block-sope. using-directives will affect lookup |
| 3970 | // only to the end of scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3971 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3972 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3973 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3974 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3975 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 3976 | AccessSpecifier AS, |
| 3977 | bool HasUsingKeyword, |
| 3978 | SourceLocation UsingLoc, |
| 3979 | CXXScopeSpec &SS, |
| 3980 | UnqualifiedId &Name, |
| 3981 | AttributeList *AttrList, |
| 3982 | bool IsTypeName, |
| 3983 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3984 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3985 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3986 | switch (Name.getKind()) { |
| 3987 | case UnqualifiedId::IK_Identifier: |
| 3988 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3989 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3990 | case UnqualifiedId::IK_ConversionFunctionId: |
| 3991 | break; |
| 3992 | |
| 3993 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3994 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3995 | // C++0x inherited constructors. |
| 3996 | if (getLangOptions().CPlusPlus0x) break; |
| 3997 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3998 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 3999 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4000 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 4001 | |
| 4002 | case UnqualifiedId::IK_DestructorName: |
| 4003 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 4004 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4005 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 4006 | |
| 4007 | case UnqualifiedId::IK_TemplateId: |
| 4008 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 4009 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4010 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 4011 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4012 | |
| 4013 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 4014 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4015 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4016 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4017 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 4018 | // Warn about using declarations. |
| 4019 | // TODO: store that the declaration was written without 'using' and |
| 4020 | // talk about access decls instead of using decls in the |
| 4021 | // diagnostics. |
| 4022 | if (!HasUsingKeyword) { |
| 4023 | UsingLoc = Name.getSourceRange().getBegin(); |
| 4024 | |
| 4025 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4026 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 4029 | if (DiagnoseUnexpandedParameterPack(SS, UPPC_UsingDeclaration) || |
| 4030 | DiagnoseUnexpandedParameterPack(TargetNameInfo, UPPC_UsingDeclaration)) |
| 4031 | return 0; |
| 4032 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4033 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4034 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4035 | /* IsInstantiation */ false, |
| 4036 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4037 | if (UD) |
| 4038 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4039 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4040 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 4043 | /// \brief Determine whether a using declaration considers the given |
| 4044 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 4045 | /// the same entity or are both typedefs of the same type. |
| 4046 | static bool |
| 4047 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 4048 | bool &SuppressRedeclaration) { |
| 4049 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 4050 | SuppressRedeclaration = false; |
| 4051 | return true; |
| 4052 | } |
| 4053 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4054 | if (TypedefNameDecl *TD1 = dyn_cast<TypedefNameDecl>(D1)) |
| 4055 | if (TypedefNameDecl *TD2 = dyn_cast<TypedefNameDecl>(D2)) { |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 4056 | SuppressRedeclaration = true; |
| 4057 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 4058 | TD2->getUnderlyingType()); |
| 4059 | } |
| 4060 | |
| 4061 | return false; |
| 4062 | } |
| 4063 | |
| 4064 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4065 | /// Determines whether to create a using shadow decl for a particular |
| 4066 | /// decl, given the set of decls existing prior to this using lookup. |
| 4067 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 4068 | const LookupResult &Previous) { |
| 4069 | // Diagnose finding a decl which is not from a base class of the |
| 4070 | // current class. We do this now because there are cases where this |
| 4071 | // function will silently decide not to build a shadow decl, which |
| 4072 | // will pre-empt further diagnostics. |
| 4073 | // |
| 4074 | // We don't need to do this in C++0x because we do the check once on |
| 4075 | // the qualifier. |
| 4076 | // |
| 4077 | // FIXME: diagnose the following if we care enough: |
| 4078 | // struct A { int foo; }; |
| 4079 | // struct B : A { using A::foo; }; |
| 4080 | // template <class T> struct C : A {}; |
| 4081 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 4082 | // This is invalid (during instantiation) in C++03 because B::foo |
| 4083 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 4084 | // We can't diagnose it immediately because C<T> is an unknown |
| 4085 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 4086 | // to A::foo, which will look well-formed when we instantiate. |
| 4087 | // The right solution is to not collapse the shadow-decl chain. |
| 4088 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 4089 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 4090 | |
| 4091 | // Handle enums and anonymous structs. |
| 4092 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 4093 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 4094 | while (OrigRec->isAnonymousStructOrUnion()) |
| 4095 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 4096 | |
| 4097 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 4098 | if (OrigDC == CurContext) { |
| 4099 | Diag(Using->getLocation(), |
| 4100 | diag::err_using_decl_nested_name_specifier_is_current_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4101 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4102 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 4103 | return true; |
| 4104 | } |
| 4105 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4106 | Diag(Using->getQualifierLoc().getBeginLoc(), |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4107 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4108 | << Using->getQualifier() |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4109 | << cast<CXXRecordDecl>(CurContext) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4110 | << Using->getQualifierLoc().getSourceRange(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4111 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 4112 | return true; |
| 4113 | } |
| 4114 | } |
| 4115 | |
| 4116 | if (Previous.empty()) return false; |
| 4117 | |
| 4118 | NamedDecl *Target = Orig; |
| 4119 | if (isa<UsingShadowDecl>(Target)) |
| 4120 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 4121 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 4122 | // If the target happens to be one of the previous declarations, we |
| 4123 | // don't have a conflict. |
| 4124 | // |
| 4125 | // FIXME: but we might be increasing its access, in which case we |
| 4126 | // should redeclare it. |
| 4127 | NamedDecl *NonTag = 0, *Tag = 0; |
| 4128 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4129 | I != E; ++I) { |
| 4130 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 4131 | bool Result; |
| 4132 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 4133 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 4134 | |
| 4135 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 4136 | } |
| 4137 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4138 | if (Target->isFunctionOrFunctionTemplate()) { |
| 4139 | FunctionDecl *FD; |
| 4140 | if (isa<FunctionTemplateDecl>(Target)) |
| 4141 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 4142 | else |
| 4143 | FD = cast<FunctionDecl>(Target); |
| 4144 | |
| 4145 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4146 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4147 | case Ovl_Overload: |
| 4148 | return false; |
| 4149 | |
| 4150 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 4151 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4152 | break; |
| 4153 | |
| 4154 | // We found a decl with the exact signature. |
| 4155 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4156 | // If we're in a record, we want to hide the target, so we |
| 4157 | // return true (without a diagnostic) to tell the caller not to |
| 4158 | // build a shadow decl. |
| 4159 | if (CurContext->isRecord()) |
| 4160 | return true; |
| 4161 | |
| 4162 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 4163 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4164 | break; |
| 4165 | } |
| 4166 | |
| 4167 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 4168 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 4169 | return true; |
| 4170 | } |
| 4171 | |
| 4172 | // Target is not a function. |
| 4173 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4174 | if (isa<TagDecl>(Target)) { |
| 4175 | // No conflict between a tag and a non-tag. |
| 4176 | if (!Tag) return false; |
| 4177 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 4178 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4179 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 4180 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 4181 | return true; |
| 4182 | } |
| 4183 | |
| 4184 | // No conflict between a tag and a non-tag. |
| 4185 | if (!NonTag) return false; |
| 4186 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 4187 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4188 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 4189 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 4190 | return true; |
| 4191 | } |
| 4192 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4193 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4194 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4195 | UsingDecl *UD, |
| 4196 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4197 | |
| 4198 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4199 | NamedDecl *Target = Orig; |
| 4200 | if (isa<UsingShadowDecl>(Target)) { |
| 4201 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 4202 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
| 4205 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4206 | = UsingShadowDecl::Create(Context, CurContext, |
| 4207 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4208 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 4209 | |
| 4210 | Shadow->setAccess(UD->getAccess()); |
| 4211 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 4212 | Shadow->setInvalidDecl(); |
| 4213 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4214 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4215 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4216 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4217 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4218 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4219 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4220 | return Shadow; |
| 4221 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4222 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4223 | /// Hides a using shadow declaration. This is required by the current |
| 4224 | /// using-decl implementation when a resolvable using declaration in a |
| 4225 | /// class is followed by a declaration which would hide or override |
| 4226 | /// one or more of the using decl's targets; for example: |
| 4227 | /// |
| 4228 | /// struct Base { void foo(int); }; |
| 4229 | /// struct Derived : Base { |
| 4230 | /// using Base::foo; |
| 4231 | /// void foo(int); |
| 4232 | /// }; |
| 4233 | /// |
| 4234 | /// The governing language is C++03 [namespace.udecl]p12: |
| 4235 | /// |
| 4236 | /// When a using-declaration brings names from a base class into a |
| 4237 | /// derived class scope, member functions in the derived class |
| 4238 | /// override and/or hide member functions with the same name and |
| 4239 | /// parameter types in a base class (rather than conflicting). |
| 4240 | /// |
| 4241 | /// There are two ways to implement this: |
| 4242 | /// (1) optimistically create shadow decls when they're not hidden |
| 4243 | /// by existing declarations, or |
| 4244 | /// (2) don't create any shadow decls (or at least don't make them |
| 4245 | /// visible) until we've fully parsed/instantiated the class. |
| 4246 | /// The problem with (1) is that we might have to retroactively remove |
| 4247 | /// a shadow decl, which requires several O(n) operations because the |
| 4248 | /// decl structures are (very reasonably) not designed for removal. |
| 4249 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 4250 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4251 | if (Shadow->getDeclName().getNameKind() == |
| 4252 | DeclarationName::CXXConversionFunctionName) |
| 4253 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 4254 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4255 | // Remove it from the DeclContext... |
| 4256 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4257 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4258 | // ...and the scope, if applicable... |
| 4259 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4260 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4261 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4264 | // ...and the using decl. |
| 4265 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 4266 | |
| 4267 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4268 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4269 | } |
| 4270 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4271 | /// Builds a using declaration. |
| 4272 | /// |
| 4273 | /// \param IsInstantiation - Whether this call arises from an |
| 4274 | /// instantiation of an unresolved using declaration. We treat |
| 4275 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4276 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 4277 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4278 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4279 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 4280 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4281 | bool IsInstantiation, |
| 4282 | bool IsTypeName, |
| 4283 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 4284 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4285 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 4286 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 4287 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 4288 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4289 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 4290 | if (SS.isEmpty()) { |
| 4291 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 4292 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 4293 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4295 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4296 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4297 | ForRedeclaration); |
| 4298 | Previous.setHideTags(false); |
| 4299 | if (S) { |
| 4300 | LookupName(Previous, S); |
| 4301 | |
| 4302 | // It is really dumb that we have to do this. |
| 4303 | LookupResult::Filter F = Previous.makeFilter(); |
| 4304 | while (F.hasNext()) { |
| 4305 | NamedDecl *D = F.next(); |
| 4306 | if (!isDeclInScope(D, CurContext, S)) |
| 4307 | F.erase(); |
| 4308 | } |
| 4309 | F.done(); |
| 4310 | } else { |
| 4311 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 4312 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 4313 | LookupQualifiedName(Previous, CurContext); |
| 4314 | } |
| 4315 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4316 | // Check for invalid redeclarations. |
| 4317 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 4318 | return 0; |
| 4319 | |
| 4320 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4321 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 4322 | return 0; |
| 4323 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 4324 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4325 | NamedDecl *D; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4326 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 4327 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4328 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4329 | // FIXME: not all declaration name kinds are legal here |
| 4330 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 4331 | UsingLoc, TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4332 | QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4333 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4334 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4335 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, UsingLoc, |
| 4336 | QualifierLoc, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4337 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4338 | } else { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4339 | D = UsingDecl::Create(Context, CurContext, UsingLoc, QualifierLoc, |
| 4340 | NameInfo, IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 4341 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4342 | D->setAccess(AS); |
| 4343 | CurContext->addDecl(D); |
| 4344 | |
| 4345 | if (!LookupContext) return D; |
| 4346 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4347 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 4348 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4349 | UD->setInvalidDecl(); |
| 4350 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4353 | // Constructor inheriting using decls get special treatment. |
| 4354 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
Sebastian Redl | caa35e4 | 2011-03-12 13:44:32 +0000 | [diff] [blame] | 4355 | if (CheckInheritedConstructorUsingDecl(UD)) |
| 4356 | UD->setInvalidDecl(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4357 | return UD; |
| 4358 | } |
| 4359 | |
| 4360 | // Otherwise, look up the target name. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4361 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4362 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4363 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4364 | // Unlike most lookups, we don't always want to hide tag |
| 4365 | // declarations: tag names are visible through the using declaration |
| 4366 | // even if hidden by ordinary names, *except* in a dependent context |
| 4367 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4368 | if (!IsInstantiation) |
| 4369 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4370 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4371 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4372 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4373 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 4374 | Diag(IdentLoc, diag::err_no_member) |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 4375 | << NameInfo.getName() << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4376 | UD->setInvalidDecl(); |
| 4377 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4380 | if (R.isAmbiguous()) { |
| 4381 | UD->setInvalidDecl(); |
| 4382 | return UD; |
| 4383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4384 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4385 | if (IsTypeName) { |
| 4386 | // 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] | 4387 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4388 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 4389 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 4390 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 4391 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4392 | UD->setInvalidDecl(); |
| 4393 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4394 | } |
| 4395 | } else { |
| 4396 | // If we asked for a non-typename and we got a type, error out, |
| 4397 | // but only if this is an instantiation of an unresolved using |
| 4398 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4399 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4400 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 4401 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4402 | UD->setInvalidDecl(); |
| 4403 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4404 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 4407 | // C++0x N2914 [namespace.udecl]p6: |
| 4408 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4409 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 4410 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 4411 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4412 | UD->setInvalidDecl(); |
| 4413 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 4414 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4415 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4416 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 4417 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 4418 | BuildUsingShadowDecl(S, UD, *I); |
| 4419 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 4420 | |
| 4421 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4424 | /// Additional checks for a using declaration referring to a constructor name. |
| 4425 | bool Sema::CheckInheritedConstructorUsingDecl(UsingDecl *UD) { |
| 4426 | if (UD->isTypeName()) { |
| 4427 | // FIXME: Cannot specify typename when specifying constructor |
| 4428 | return true; |
| 4429 | } |
| 4430 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4431 | const Type *SourceType = UD->getQualifier()->getAsType(); |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4432 | assert(SourceType && |
| 4433 | "Using decl naming constructor doesn't have type in scope spec."); |
| 4434 | CXXRecordDecl *TargetClass = cast<CXXRecordDecl>(CurContext); |
| 4435 | |
| 4436 | // Check whether the named type is a direct base class. |
| 4437 | CanQualType CanonicalSourceType = SourceType->getCanonicalTypeUnqualified(); |
| 4438 | CXXRecordDecl::base_class_iterator BaseIt, BaseE; |
| 4439 | for (BaseIt = TargetClass->bases_begin(), BaseE = TargetClass->bases_end(); |
| 4440 | BaseIt != BaseE; ++BaseIt) { |
| 4441 | CanQualType BaseType = BaseIt->getType()->getCanonicalTypeUnqualified(); |
| 4442 | if (CanonicalSourceType == BaseType) |
| 4443 | break; |
| 4444 | } |
| 4445 | |
| 4446 | if (BaseIt == BaseE) { |
| 4447 | // Did not find SourceType in the bases. |
| 4448 | Diag(UD->getUsingLocation(), |
| 4449 | diag::err_using_decl_constructor_not_in_direct_base) |
| 4450 | << UD->getNameInfo().getSourceRange() |
| 4451 | << QualType(SourceType, 0) << TargetClass; |
| 4452 | return true; |
| 4453 | } |
| 4454 | |
| 4455 | BaseIt->setInheritConstructors(); |
| 4456 | |
| 4457 | return false; |
| 4458 | } |
| 4459 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4460 | /// Checks that the given using declaration is not an invalid |
| 4461 | /// redeclaration. Note that this is checking only for the using decl |
| 4462 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 4463 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 4464 | bool isTypeName, |
| 4465 | const CXXScopeSpec &SS, |
| 4466 | SourceLocation NameLoc, |
| 4467 | const LookupResult &Prev) { |
| 4468 | // C++03 [namespace.udecl]p8: |
| 4469 | // C++0x [namespace.udecl]p10: |
| 4470 | // A using-declaration is a declaration and can therefore be used |
| 4471 | // repeatedly where (and only where) multiple declarations are |
| 4472 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 4473 | // |
John McCall | 8a72621 | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 4474 | // That's in non-member contexts. |
| 4475 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4476 | return false; |
| 4477 | |
| 4478 | NestedNameSpecifier *Qual |
| 4479 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 4480 | |
| 4481 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 4482 | NamedDecl *D = *I; |
| 4483 | |
| 4484 | bool DTypename; |
| 4485 | NestedNameSpecifier *DQual; |
| 4486 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 4487 | DTypename = UD->isTypeName(); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4488 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4489 | } else if (UnresolvedUsingValueDecl *UD |
| 4490 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 4491 | DTypename = false; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4492 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4493 | } else if (UnresolvedUsingTypenameDecl *UD |
| 4494 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 4495 | DTypename = true; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 4496 | DQual = UD->getQualifier(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4497 | } else continue; |
| 4498 | |
| 4499 | // using decls differ if one says 'typename' and the other doesn't. |
| 4500 | // FIXME: non-dependent using decls? |
| 4501 | if (isTypeName != DTypename) continue; |
| 4502 | |
| 4503 | // using decls differ if they name different scopes (but note that |
| 4504 | // template instantiation can cause this check to trigger when it |
| 4505 | // didn't before instantiation). |
| 4506 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 4507 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 4508 | continue; |
| 4509 | |
| 4510 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 4511 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 4512 | return true; |
| 4513 | } |
| 4514 | |
| 4515 | return false; |
| 4516 | } |
| 4517 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4518 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4519 | /// Checks that the given nested-name qualifier used in a using decl |
| 4520 | /// in the current context is appropriately related to the current |
| 4521 | /// scope. If an error is found, diagnoses it and returns true. |
| 4522 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 4523 | const CXXScopeSpec &SS, |
| 4524 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4525 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4526 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4527 | if (!CurContext->isRecord()) { |
| 4528 | // C++03 [namespace.udecl]p3: |
| 4529 | // C++0x [namespace.udecl]p8: |
| 4530 | // A using-declaration for a class member shall be a member-declaration. |
| 4531 | |
| 4532 | // If we weren't able to compute a valid scope, it must be a |
| 4533 | // dependent class scope. |
| 4534 | if (!NamedContext || NamedContext->isRecord()) { |
| 4535 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 4536 | << SS.getRange(); |
| 4537 | return true; |
| 4538 | } |
| 4539 | |
| 4540 | // Otherwise, everything is known to be fine. |
| 4541 | return false; |
| 4542 | } |
| 4543 | |
| 4544 | // The current scope is a record. |
| 4545 | |
| 4546 | // If the named context is dependent, we can't decide much. |
| 4547 | if (!NamedContext) { |
| 4548 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 4549 | // nested-name-specifier does not refer to a base class, which is |
| 4550 | // still possible in some cases. |
| 4551 | |
| 4552 | // Otherwise we have to conservatively report that things might be |
| 4553 | // okay. |
| 4554 | return false; |
| 4555 | } |
| 4556 | |
| 4557 | if (!NamedContext->isRecord()) { |
| 4558 | // Ideally this would point at the last name in the specifier, |
| 4559 | // but we don't have that level of source info. |
| 4560 | Diag(SS.getRange().getBegin(), |
| 4561 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 4562 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 4563 | return true; |
| 4564 | } |
| 4565 | |
Douglas Gregor | 6fb0729 | 2010-12-21 07:41:49 +0000 | [diff] [blame] | 4566 | if (!NamedContext->isDependentContext() && |
| 4567 | RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), NamedContext)) |
| 4568 | return true; |
| 4569 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4570 | if (getLangOptions().CPlusPlus0x) { |
| 4571 | // C++0x [namespace.udecl]p3: |
| 4572 | // In a using-declaration used as a member-declaration, the |
| 4573 | // nested-name-specifier shall name a base class of the class |
| 4574 | // being defined. |
| 4575 | |
| 4576 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 4577 | cast<CXXRecordDecl>(NamedContext))) { |
| 4578 | if (CurContext == NamedContext) { |
| 4579 | Diag(NameLoc, |
| 4580 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 4581 | << SS.getRange(); |
| 4582 | return true; |
| 4583 | } |
| 4584 | |
| 4585 | Diag(SS.getRange().getBegin(), |
| 4586 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 4587 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 4588 | << cast<CXXRecordDecl>(CurContext) |
| 4589 | << SS.getRange(); |
| 4590 | return true; |
| 4591 | } |
| 4592 | |
| 4593 | return false; |
| 4594 | } |
| 4595 | |
| 4596 | // C++03 [namespace.udecl]p4: |
| 4597 | // A using-declaration used as a member-declaration shall refer |
| 4598 | // to a member of a base class of the class being defined [etc.]. |
| 4599 | |
| 4600 | // Salient point: SS doesn't have to name a base class as long as |
| 4601 | // lookup only finds members from base classes. Therefore we can |
| 4602 | // diagnose here only if we can prove that that can't happen, |
| 4603 | // i.e. if the class hierarchies provably don't intersect. |
| 4604 | |
| 4605 | // TODO: it would be nice if "definitely valid" results were cached |
| 4606 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 4607 | // need to be repeated. |
| 4608 | |
| 4609 | struct UserData { |
| 4610 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 4611 | |
| 4612 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 4613 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 4614 | Data->Bases.insert(Base); |
| 4615 | return true; |
| 4616 | } |
| 4617 | |
| 4618 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 4619 | return !Class->forallBases(collect, this); |
| 4620 | } |
| 4621 | |
| 4622 | /// Returns true if the base is dependent or is one of the |
| 4623 | /// accumulated base classes. |
| 4624 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 4625 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 4626 | return !Data->Bases.count(Base); |
| 4627 | } |
| 4628 | |
| 4629 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 4630 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 4631 | } |
| 4632 | }; |
| 4633 | |
| 4634 | UserData Data; |
| 4635 | |
| 4636 | // Returns false if we find a dependent base. |
| 4637 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 4638 | return false; |
| 4639 | |
| 4640 | // Returns false if the class has a dependent base or if it or one |
| 4641 | // of its bases is present in the base set of the current context. |
| 4642 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 4643 | return false; |
| 4644 | |
| 4645 | Diag(SS.getRange().getBegin(), |
| 4646 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 4647 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 4648 | << cast<CXXRecordDecl>(CurContext) |
| 4649 | << SS.getRange(); |
| 4650 | |
| 4651 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4654 | Decl *Sema::ActOnAliasDeclaration(Scope *S, |
| 4655 | AccessSpecifier AS, |
| 4656 | SourceLocation UsingLoc, |
| 4657 | UnqualifiedId &Name, |
| 4658 | TypeResult Type) { |
| 4659 | assert((S->getFlags() & Scope::DeclScope) && |
| 4660 | "got alias-declaration outside of declaration scope"); |
| 4661 | |
| 4662 | if (Type.isInvalid()) |
| 4663 | return 0; |
| 4664 | |
| 4665 | bool Invalid = false; |
| 4666 | DeclarationNameInfo NameInfo = GetNameFromUnqualifiedId(Name); |
| 4667 | TypeSourceInfo *TInfo = 0; |
| 4668 | QualType T = GetTypeFromParser(Type.get(), &TInfo); |
| 4669 | |
| 4670 | if (DiagnoseClassNameShadow(CurContext, NameInfo)) |
| 4671 | return 0; |
| 4672 | |
| 4673 | if (DiagnoseUnexpandedParameterPack(Name.StartLocation, TInfo, |
| 4674 | UPPC_DeclarationType)) |
| 4675 | Invalid = true; |
| 4676 | |
| 4677 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, ForRedeclaration); |
| 4678 | LookupName(Previous, S); |
| 4679 | |
| 4680 | // Warn about shadowing the name of a template parameter. |
| 4681 | if (Previous.isSingleResult() && |
| 4682 | Previous.getFoundDecl()->isTemplateParameter()) { |
| 4683 | if (DiagnoseTemplateParameterShadow(Name.StartLocation, |
| 4684 | Previous.getFoundDecl())) |
| 4685 | Invalid = true; |
| 4686 | Previous.clear(); |
| 4687 | } |
| 4688 | |
| 4689 | assert(Name.Kind == UnqualifiedId::IK_Identifier && |
| 4690 | "name in alias declaration must be an identifier"); |
| 4691 | TypeAliasDecl *NewTD = TypeAliasDecl::Create(Context, CurContext, UsingLoc, |
| 4692 | Name.StartLocation, |
| 4693 | Name.Identifier, TInfo); |
| 4694 | |
| 4695 | NewTD->setAccess(AS); |
| 4696 | |
| 4697 | if (Invalid) |
| 4698 | NewTD->setInvalidDecl(); |
| 4699 | |
| 4700 | bool Redeclaration = false; |
| 4701 | ActOnTypedefNameDecl(S, CurContext, NewTD, Previous, Redeclaration); |
| 4702 | |
| 4703 | if (!Redeclaration) |
| 4704 | PushOnScopeChains(NewTD, S); |
| 4705 | |
| 4706 | return NewTD; |
| 4707 | } |
| 4708 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4709 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 4710 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4711 | SourceLocation AliasLoc, |
| 4712 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4713 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 4714 | SourceLocation IdentLoc, |
| 4715 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4716 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4717 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4718 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 4719 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4720 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4721 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 4722 | NamedDecl *PrevDecl |
| 4723 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 4724 | ForRedeclaration); |
| 4725 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 4726 | PrevDecl = 0; |
| 4727 | |
| 4728 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4729 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4730 | // 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] | 4731 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4732 | // FIXME: At some point, we'll want to create the (redundant) |
| 4733 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4734 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4735 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4736 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4737 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4738 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4739 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 4740 | diag::err_redefinition_different_kind; |
| 4741 | Diag(AliasLoc, DiagID) << Alias; |
| 4742 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4743 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4744 | } |
| 4745 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4746 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4747 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4748 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4749 | if (R.empty()) { |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4750 | if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 4751 | CTC_NoKeywords, 0)) { |
| 4752 | if (R.getAsSingle<NamespaceDecl>() || |
| 4753 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 4754 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 4755 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 4756 | << Ident << DC << Corrected << SS.getRange() |
| 4757 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4758 | else |
| 4759 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 4760 | << Ident << Corrected |
| 4761 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4762 | |
| 4763 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 4764 | << Corrected; |
| 4765 | |
| 4766 | Ident = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 4767 | } else { |
| 4768 | R.clear(); |
| 4769 | R.setLookupName(Ident); |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4770 | } |
| 4771 | } |
| 4772 | |
| 4773 | if (R.empty()) { |
| 4774 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4775 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4776 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 4777 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4778 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4779 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4780 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 4781 | Alias, SS.getWithLocInContext(Context), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4782 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4783 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 4784 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4785 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4788 | namespace { |
| 4789 | /// \brief Scoped object used to handle the state changes required in Sema |
| 4790 | /// to implicitly define the body of a C++ member function; |
| 4791 | class ImplicitlyDefinedFunctionScope { |
| 4792 | Sema &S; |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4793 | Sema::ContextRAII SavedContext; |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4794 | |
| 4795 | public: |
| 4796 | ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method) |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4797 | : S(S), SavedContext(S, Method) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4798 | { |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4799 | S.PushFunctionScope(); |
| 4800 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 4801 | } |
| 4802 | |
| 4803 | ~ImplicitlyDefinedFunctionScope() { |
| 4804 | S.PopExpressionEvaluationContext(); |
| 4805 | S.PopFunctionOrBlockScope(); |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4806 | } |
| 4807 | }; |
| 4808 | } |
| 4809 | |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4810 | static CXXConstructorDecl *getDefaultConstructorUnsafe(Sema &Self, |
| 4811 | CXXRecordDecl *D) { |
| 4812 | ASTContext &Context = Self.Context; |
| 4813 | QualType ClassType = Context.getTypeDeclType(D); |
| 4814 | DeclarationName ConstructorName |
| 4815 | = Context.DeclarationNames.getCXXConstructorName( |
| 4816 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
| 4817 | |
| 4818 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 4819 | for (llvm::tie(Con, ConEnd) = D->lookup(ConstructorName); |
| 4820 | Con != ConEnd; ++Con) { |
| 4821 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 4822 | if (isa<FunctionTemplateDecl>(*Con)) |
| 4823 | continue; |
| 4824 | |
| 4825 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 4826 | if (Constructor->isDefaultConstructor()) |
| 4827 | return Constructor; |
| 4828 | } |
| 4829 | return 0; |
| 4830 | } |
| 4831 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4832 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 4833 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4834 | // C++ [class.ctor]p5: |
| 4835 | // A default constructor for a class X is a constructor of class X |
| 4836 | // that can be called without an argument. If there is no |
| 4837 | // user-declared constructor for class X, a default constructor is |
| 4838 | // implicitly declared. An implicitly-declared default constructor |
| 4839 | // is an inline public member of its class. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4840 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
| 4841 | "Should not build implicit default constructor!"); |
| 4842 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4843 | // C++ [except.spec]p14: |
| 4844 | // An implicitly declared special member function (Clause 12) shall have an |
| 4845 | // exception-specification. [...] |
| 4846 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4847 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 4848 | // Direct base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4849 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4850 | BEnd = ClassDecl->bases_end(); |
| 4851 | B != BEnd; ++B) { |
| 4852 | if (B->isVirtual()) // Handled below. |
| 4853 | continue; |
| 4854 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4855 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 4856 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4857 | if (!BaseClassDecl->hasDeclaredDefaultConstructor()) |
| 4858 | ExceptSpec.CalledDecl(DeclareImplicitDefaultConstructor(BaseClassDecl)); |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4859 | else if (CXXConstructorDecl *Constructor |
| 4860 | = getDefaultConstructorUnsafe(*this, BaseClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4861 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4862 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4863 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 4864 | |
| 4865 | // Virtual base-class constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4866 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 4867 | BEnd = ClassDecl->vbases_end(); |
| 4868 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4869 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 4870 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4871 | if (!BaseClassDecl->hasDeclaredDefaultConstructor()) |
| 4872 | ExceptSpec.CalledDecl(DeclareImplicitDefaultConstructor(BaseClassDecl)); |
| 4873 | else if (CXXConstructorDecl *Constructor |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4874 | = getDefaultConstructorUnsafe(*this, BaseClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4875 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4876 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4877 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 4878 | |
| 4879 | // Field constructors. |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4880 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4881 | FEnd = ClassDecl->field_end(); |
| 4882 | F != FEnd; ++F) { |
| 4883 | if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4884 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 4885 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4886 | if (!FieldClassDecl->hasDeclaredDefaultConstructor()) |
| 4887 | ExceptSpec.CalledDecl( |
| 4888 | DeclareImplicitDefaultConstructor(FieldClassDecl)); |
| 4889 | else if (CXXConstructorDecl *Constructor |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4890 | = getDefaultConstructorUnsafe(*this, FieldClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4891 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4892 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4893 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4894 | |
| 4895 | FunctionProtoType::ExtProtoInfo EPI; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 4896 | EPI.ExceptionSpecType = ExceptSpec.getExceptionSpecType(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4897 | EPI.NumExceptions = ExceptSpec.size(); |
| 4898 | EPI.Exceptions = ExceptSpec.data(); |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 4899 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4900 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4901 | CanQualType ClassType |
| 4902 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4903 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4904 | DeclarationName Name |
| 4905 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4906 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4907 | CXXConstructorDecl *DefaultCon |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4908 | = CXXConstructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4909 | Context.getFunctionType(Context.VoidTy, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 4910 | 0, 0, EPI), |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4911 | /*TInfo=*/0, |
| 4912 | /*isExplicit=*/false, |
| 4913 | /*isInline=*/true, |
| 4914 | /*isImplicitlyDeclared=*/true); |
| 4915 | DefaultCon->setAccess(AS_public); |
| 4916 | DefaultCon->setImplicit(); |
| 4917 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4918 | |
| 4919 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4920 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
| 4921 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4922 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4923 | PushOnScopeChains(DefaultCon, S, false); |
| 4924 | ClassDecl->addDecl(DefaultCon); |
| 4925 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4926 | return DefaultCon; |
| 4927 | } |
| 4928 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4929 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 4930 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4931 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4932 | !Constructor->isUsed(false)) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4933 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4934 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 4935 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4936 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 4937 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4938 | ImplicitlyDefinedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 4939 | DiagnosticErrorTrap Trap(Diags); |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 4940 | if (SetCtorInitializers(Constructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4941 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4942 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 4943 | << CXXConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4944 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4945 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4946 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4947 | |
| 4948 | SourceLocation Loc = Constructor->getLocation(); |
| 4949 | Constructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 4950 | |
| 4951 | Constructor->setUsed(); |
| 4952 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 4955 | void Sema::DeclareInheritedConstructors(CXXRecordDecl *ClassDecl) { |
| 4956 | // We start with an initial pass over the base classes to collect those that |
| 4957 | // inherit constructors from. If there are none, we can forgo all further |
| 4958 | // processing. |
| 4959 | typedef llvm::SmallVector<const RecordType *, 4> BasesVector; |
| 4960 | BasesVector BasesToInheritFrom; |
| 4961 | for (CXXRecordDecl::base_class_iterator BaseIt = ClassDecl->bases_begin(), |
| 4962 | BaseE = ClassDecl->bases_end(); |
| 4963 | BaseIt != BaseE; ++BaseIt) { |
| 4964 | if (BaseIt->getInheritConstructors()) { |
| 4965 | QualType Base = BaseIt->getType(); |
| 4966 | if (Base->isDependentType()) { |
| 4967 | // If we inherit constructors from anything that is dependent, just |
| 4968 | // abort processing altogether. We'll get another chance for the |
| 4969 | // instantiations. |
| 4970 | return; |
| 4971 | } |
| 4972 | BasesToInheritFrom.push_back(Base->castAs<RecordType>()); |
| 4973 | } |
| 4974 | } |
| 4975 | if (BasesToInheritFrom.empty()) |
| 4976 | return; |
| 4977 | |
| 4978 | // Now collect the constructors that we already have in the current class. |
| 4979 | // Those take precedence over inherited constructors. |
| 4980 | // C++0x [class.inhctor]p3: [...] a constructor is implicitly declared [...] |
| 4981 | // unless there is a user-declared constructor with the same signature in |
| 4982 | // the class where the using-declaration appears. |
| 4983 | llvm::SmallSet<const Type *, 8> ExistingConstructors; |
| 4984 | for (CXXRecordDecl::ctor_iterator CtorIt = ClassDecl->ctor_begin(), |
| 4985 | CtorE = ClassDecl->ctor_end(); |
| 4986 | CtorIt != CtorE; ++CtorIt) { |
| 4987 | ExistingConstructors.insert( |
| 4988 | Context.getCanonicalType(CtorIt->getType()).getTypePtr()); |
| 4989 | } |
| 4990 | |
| 4991 | Scope *S = getScopeForContext(ClassDecl); |
| 4992 | DeclarationName CreatedCtorName = |
| 4993 | Context.DeclarationNames.getCXXConstructorName( |
| 4994 | ClassDecl->getTypeForDecl()->getCanonicalTypeUnqualified()); |
| 4995 | |
| 4996 | // Now comes the true work. |
| 4997 | // First, we keep a map from constructor types to the base that introduced |
| 4998 | // them. Needed for finding conflicting constructors. We also keep the |
| 4999 | // actually inserted declarations in there, for pretty diagnostics. |
| 5000 | typedef std::pair<CanQualType, CXXConstructorDecl *> ConstructorInfo; |
| 5001 | typedef llvm::DenseMap<const Type *, ConstructorInfo> ConstructorToSourceMap; |
| 5002 | ConstructorToSourceMap InheritedConstructors; |
| 5003 | for (BasesVector::iterator BaseIt = BasesToInheritFrom.begin(), |
| 5004 | BaseE = BasesToInheritFrom.end(); |
| 5005 | BaseIt != BaseE; ++BaseIt) { |
| 5006 | const RecordType *Base = *BaseIt; |
| 5007 | CanQualType CanonicalBase = Base->getCanonicalTypeUnqualified(); |
| 5008 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(Base->getDecl()); |
| 5009 | for (CXXRecordDecl::ctor_iterator CtorIt = BaseDecl->ctor_begin(), |
| 5010 | CtorE = BaseDecl->ctor_end(); |
| 5011 | CtorIt != CtorE; ++CtorIt) { |
| 5012 | // Find the using declaration for inheriting this base's constructors. |
| 5013 | DeclarationName Name = |
| 5014 | Context.DeclarationNames.getCXXConstructorName(CanonicalBase); |
| 5015 | UsingDecl *UD = dyn_cast_or_null<UsingDecl>( |
| 5016 | LookupSingleName(S, Name,SourceLocation(), LookupUsingDeclName)); |
| 5017 | SourceLocation UsingLoc = UD ? UD->getLocation() : |
| 5018 | ClassDecl->getLocation(); |
| 5019 | |
| 5020 | // C++0x [class.inhctor]p1: The candidate set of inherited constructors |
| 5021 | // from the class X named in the using-declaration consists of actual |
| 5022 | // constructors and notional constructors that result from the |
| 5023 | // transformation of defaulted parameters as follows: |
| 5024 | // - all non-template default constructors of X, and |
| 5025 | // - for each non-template constructor of X that has at least one |
| 5026 | // parameter with a default argument, the set of constructors that |
| 5027 | // results from omitting any ellipsis parameter specification and |
| 5028 | // successively omitting parameters with a default argument from the |
| 5029 | // end of the parameter-type-list. |
| 5030 | CXXConstructorDecl *BaseCtor = *CtorIt; |
| 5031 | bool CanBeCopyOrMove = BaseCtor->isCopyOrMoveConstructor(); |
| 5032 | const FunctionProtoType *BaseCtorType = |
| 5033 | BaseCtor->getType()->getAs<FunctionProtoType>(); |
| 5034 | |
| 5035 | for (unsigned params = BaseCtor->getMinRequiredArguments(), |
| 5036 | maxParams = BaseCtor->getNumParams(); |
| 5037 | params <= maxParams; ++params) { |
| 5038 | // Skip default constructors. They're never inherited. |
| 5039 | if (params == 0) |
| 5040 | continue; |
| 5041 | // Skip copy and move constructors for the same reason. |
| 5042 | if (CanBeCopyOrMove && params == 1) |
| 5043 | continue; |
| 5044 | |
| 5045 | // Build up a function type for this particular constructor. |
| 5046 | // FIXME: The working paper does not consider that the exception spec |
| 5047 | // for the inheriting constructor might be larger than that of the |
| 5048 | // source. This code doesn't yet, either. |
| 5049 | const Type *NewCtorType; |
| 5050 | if (params == maxParams) |
| 5051 | NewCtorType = BaseCtorType; |
| 5052 | else { |
| 5053 | llvm::SmallVector<QualType, 16> Args; |
| 5054 | for (unsigned i = 0; i < params; ++i) { |
| 5055 | Args.push_back(BaseCtorType->getArgType(i)); |
| 5056 | } |
| 5057 | FunctionProtoType::ExtProtoInfo ExtInfo = |
| 5058 | BaseCtorType->getExtProtoInfo(); |
| 5059 | ExtInfo.Variadic = false; |
| 5060 | NewCtorType = Context.getFunctionType(BaseCtorType->getResultType(), |
| 5061 | Args.data(), params, ExtInfo) |
| 5062 | .getTypePtr(); |
| 5063 | } |
| 5064 | const Type *CanonicalNewCtorType = |
| 5065 | Context.getCanonicalType(NewCtorType); |
| 5066 | |
| 5067 | // Now that we have the type, first check if the class already has a |
| 5068 | // constructor with this signature. |
| 5069 | if (ExistingConstructors.count(CanonicalNewCtorType)) |
| 5070 | continue; |
| 5071 | |
| 5072 | // Then we check if we have already declared an inherited constructor |
| 5073 | // with this signature. |
| 5074 | std::pair<ConstructorToSourceMap::iterator, bool> result = |
| 5075 | InheritedConstructors.insert(std::make_pair( |
| 5076 | CanonicalNewCtorType, |
| 5077 | std::make_pair(CanonicalBase, (CXXConstructorDecl*)0))); |
| 5078 | if (!result.second) { |
| 5079 | // Already in the map. If it came from a different class, that's an |
| 5080 | // error. Not if it's from the same. |
| 5081 | CanQualType PreviousBase = result.first->second.first; |
| 5082 | if (CanonicalBase != PreviousBase) { |
| 5083 | const CXXConstructorDecl *PrevCtor = result.first->second.second; |
| 5084 | const CXXConstructorDecl *PrevBaseCtor = |
| 5085 | PrevCtor->getInheritedConstructor(); |
| 5086 | assert(PrevBaseCtor && "Conflicting constructor was not inherited"); |
| 5087 | |
| 5088 | Diag(UsingLoc, diag::err_using_decl_constructor_conflict); |
| 5089 | Diag(BaseCtor->getLocation(), |
| 5090 | diag::note_using_decl_constructor_conflict_current_ctor); |
| 5091 | Diag(PrevBaseCtor->getLocation(), |
| 5092 | diag::note_using_decl_constructor_conflict_previous_ctor); |
| 5093 | Diag(PrevCtor->getLocation(), |
| 5094 | diag::note_using_decl_constructor_conflict_previous_using); |
| 5095 | } |
| 5096 | continue; |
| 5097 | } |
| 5098 | |
| 5099 | // OK, we're there, now add the constructor. |
| 5100 | // C++0x [class.inhctor]p8: [...] that would be performed by a |
| 5101 | // user-writtern inline constructor [...] |
| 5102 | DeclarationNameInfo DNI(CreatedCtorName, UsingLoc); |
| 5103 | CXXConstructorDecl *NewCtor = CXXConstructorDecl::Create( |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5104 | Context, ClassDecl, UsingLoc, DNI, QualType(NewCtorType, 0), |
| 5105 | /*TInfo=*/0, BaseCtor->isExplicit(), /*Inline=*/true, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5106 | /*ImplicitlyDeclared=*/true); |
| 5107 | NewCtor->setAccess(BaseCtor->getAccess()); |
| 5108 | |
| 5109 | // Build up the parameter decls and add them. |
| 5110 | llvm::SmallVector<ParmVarDecl *, 16> ParamDecls; |
| 5111 | for (unsigned i = 0; i < params; ++i) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5112 | ParamDecls.push_back(ParmVarDecl::Create(Context, NewCtor, |
| 5113 | UsingLoc, UsingLoc, |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 5114 | /*IdentifierInfo=*/0, |
| 5115 | BaseCtorType->getArgType(i), |
| 5116 | /*TInfo=*/0, SC_None, |
| 5117 | SC_None, /*DefaultArg=*/0)); |
| 5118 | } |
| 5119 | NewCtor->setParams(ParamDecls.data(), ParamDecls.size()); |
| 5120 | NewCtor->setInheritedConstructor(BaseCtor); |
| 5121 | |
| 5122 | PushOnScopeChains(NewCtor, S, false); |
| 5123 | ClassDecl->addDecl(NewCtor); |
| 5124 | result.first->second.second = NewCtor; |
| 5125 | } |
| 5126 | } |
| 5127 | } |
| 5128 | } |
| 5129 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5130 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 5131 | // C++ [class.dtor]p2: |
| 5132 | // If a class has no user-declared destructor, a destructor is |
| 5133 | // declared implicitly. An implicitly-declared destructor is an |
| 5134 | // inline public member of its class. |
| 5135 | |
| 5136 | // C++ [except.spec]p14: |
| 5137 | // An implicitly declared special member function (Clause 12) shall have |
| 5138 | // an exception-specification. |
| 5139 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 5140 | |
| 5141 | // Direct base-class destructors. |
| 5142 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 5143 | BEnd = ClassDecl->bases_end(); |
| 5144 | B != BEnd; ++B) { |
| 5145 | if (B->isVirtual()) // Handled below. |
| 5146 | continue; |
| 5147 | |
| 5148 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 5149 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5150 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | // Virtual base-class destructors. |
| 5154 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 5155 | BEnd = ClassDecl->vbases_end(); |
| 5156 | B != BEnd; ++B) { |
| 5157 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 5158 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5159 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 5160 | } |
| 5161 | |
| 5162 | // Field destructors. |
| 5163 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 5164 | FEnd = ClassDecl->field_end(); |
| 5165 | F != FEnd; ++F) { |
| 5166 | if (const RecordType *RecordTy |
| 5167 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
| 5168 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5169 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 5170 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 5171 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5172 | // Create the actual destructor declaration. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5173 | FunctionProtoType::ExtProtoInfo EPI; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 5174 | EPI.ExceptionSpecType = ExceptSpec.getExceptionSpecType(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5175 | EPI.NumExceptions = ExceptSpec.size(); |
| 5176 | EPI.Exceptions = ExceptSpec.data(); |
| 5177 | QualType Ty = Context.getFunctionType(Context.VoidTy, 0, 0, EPI); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 5178 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 5179 | CanQualType ClassType |
| 5180 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5181 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 5182 | DeclarationName Name |
| 5183 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5184 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 5185 | CXXDestructorDecl *Destructor |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 5186 | = CXXDestructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, Ty, 0, |
| 5187 | /*isInline=*/true, |
| 5188 | /*isImplicitlyDeclared=*/true); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 5189 | Destructor->setAccess(AS_public); |
| 5190 | Destructor->setImplicit(); |
| 5191 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5192 | |
| 5193 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5194 | ++ASTContext::NumImplicitDestructorsDeclared; |
| 5195 | |
| 5196 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5197 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5198 | PushOnScopeChains(Destructor, S, false); |
| 5199 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 5200 | |
| 5201 | // This could be uniqued if it ever proves significant. |
| 5202 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
| 5203 | |
| 5204 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 5205 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 5206 | return Destructor; |
| 5207 | } |
| 5208 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5209 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 5210 | CXXDestructorDecl *Destructor) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 5211 | assert((Destructor->isImplicit() && !Destructor->isUsed(false)) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5212 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 5213 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5214 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5215 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5216 | if (Destructor->isInvalidDecl()) |
| 5217 | return; |
| 5218 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5219 | ImplicitlyDefinedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5220 | |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 5221 | DiagnosticErrorTrap Trap(Diags); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 5222 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 5223 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5224 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5225 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 5226 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5227 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 5228 | |
| 5229 | Destructor->setInvalidDecl(); |
| 5230 | return; |
| 5231 | } |
| 5232 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 5233 | SourceLocation Loc = Destructor->getLocation(); |
| 5234 | Destructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 5235 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5236 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5237 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5238 | } |
| 5239 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5240 | /// \brief Builds a statement that copies the given entity from \p From to |
| 5241 | /// \c To. |
| 5242 | /// |
| 5243 | /// This routine is used to copy the members of a class with an |
| 5244 | /// implicitly-declared copy assignment operator. When the entities being |
| 5245 | /// copied are arrays, this routine builds for loops to copy them. |
| 5246 | /// |
| 5247 | /// \param S The Sema object used for type-checking. |
| 5248 | /// |
| 5249 | /// \param Loc The location where the implicit copy is being generated. |
| 5250 | /// |
| 5251 | /// \param T The type of the expressions being copied. Both expressions must |
| 5252 | /// have this type. |
| 5253 | /// |
| 5254 | /// \param To The expression we are copying to. |
| 5255 | /// |
| 5256 | /// \param From The expression we are copying from. |
| 5257 | /// |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 5258 | /// \param CopyingBaseSubobject Whether we're copying a base subobject. |
| 5259 | /// Otherwise, it's a non-static member subobject. |
| 5260 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5261 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 5262 | /// |
| 5263 | /// \returns A statement or a loop that copies the expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5264 | static StmtResult |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5265 | BuildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5266 | Expr *To, Expr *From, |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 5267 | bool CopyingBaseSubobject, unsigned Depth = 0) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5268 | // C++0x [class.copy]p30: |
| 5269 | // Each subobject is assigned in the manner appropriate to its type: |
| 5270 | // |
| 5271 | // - if the subobject is of class type, the copy assignment operator |
| 5272 | // for the class is used (as if by explicit qualification; that is, |
| 5273 | // ignoring any possible virtual overriding functions in more derived |
| 5274 | // classes); |
| 5275 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 5276 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 5277 | |
| 5278 | // Look for operator=. |
| 5279 | DeclarationName Name |
| 5280 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 5281 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 5282 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| 5283 | |
| 5284 | // Filter out any result that isn't a copy-assignment operator. |
| 5285 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 5286 | while (F.hasNext()) { |
| 5287 | NamedDecl *D = F.next(); |
| 5288 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 5289 | if (Method->isCopyAssignmentOperator()) |
| 5290 | continue; |
| 5291 | |
| 5292 | F.erase(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 5293 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5294 | F.done(); |
| 5295 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 5296 | // Suppress the protected check (C++ [class.protected]) for each of the |
| 5297 | // assignment operators we found. This strange dance is required when |
| 5298 | // we're assigning via a base classes's copy-assignment operator. To |
| 5299 | // ensure that we're getting the right base class subobject (without |
| 5300 | // ambiguities), we need to cast "this" to that subobject type; to |
| 5301 | // ensure that we don't go through the virtual call mechanism, we need |
| 5302 | // to qualify the operator= name with the base class (see below). However, |
| 5303 | // this means that if the base class has a protected copy assignment |
| 5304 | // operator, the protected member access check will fail. So, we |
| 5305 | // rewrite "protected" access to "public" access in this case, since we |
| 5306 | // know by construction that we're calling from a derived class. |
| 5307 | if (CopyingBaseSubobject) { |
| 5308 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 5309 | L != LEnd; ++L) { |
| 5310 | if (L.getAccess() == AS_protected) |
| 5311 | L.setAccess(AS_public); |
| 5312 | } |
| 5313 | } |
| 5314 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5315 | // Create the nested-name-specifier that will be used to qualify the |
| 5316 | // reference to operator=; this is required to suppress the virtual |
| 5317 | // call mechanism. |
| 5318 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 5319 | SS.MakeTrivial(S.Context, |
| 5320 | NestedNameSpecifier::Create(S.Context, 0, false, |
| 5321 | T.getTypePtr()), |
| 5322 | Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5323 | |
| 5324 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5325 | ExprResult OpEqualRef |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5326 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5327 | /*FirstQualifierInScope=*/0, OpLookup, |
| 5328 | /*TemplateArgs=*/0, |
| 5329 | /*SuppressQualifierCheck=*/true); |
| 5330 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5331 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5332 | |
| 5333 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5334 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5335 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5336 | OpEqualRef.takeAs<Expr>(), |
| 5337 | Loc, &From, 1, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5338 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5339 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5340 | |
| 5341 | return S.Owned(Call.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5342 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 5343 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5344 | // - if the subobject is of scalar type, the built-in assignment |
| 5345 | // operator is used. |
| 5346 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| 5347 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5348 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5349 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5350 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5351 | |
| 5352 | return S.Owned(Assignment.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5353 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5354 | |
| 5355 | // - if the subobject is an array, each element is assigned, in the |
| 5356 | // manner appropriate to the element type; |
| 5357 | |
| 5358 | // Construct a loop over the array bounds, e.g., |
| 5359 | // |
| 5360 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 5361 | // |
| 5362 | // that will copy each of the array elements. |
| 5363 | QualType SizeType = S.Context.getSizeType(); |
| 5364 | |
| 5365 | // Create the iteration variable. |
| 5366 | IdentifierInfo *IterationVarName = 0; |
| 5367 | { |
| 5368 | llvm::SmallString<8> Str; |
| 5369 | llvm::raw_svector_ostream OS(Str); |
| 5370 | OS << "__i" << Depth; |
| 5371 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 5372 | } |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5373 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5374 | IterationVarName, SizeType, |
| 5375 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5376 | SC_None, SC_None); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5377 | |
| 5378 | // Initialize the iteration variable to zero. |
| 5379 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5380 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5381 | |
| 5382 | // Create a reference to the iteration variable; we'll use this several |
| 5383 | // times throughout. |
| 5384 | Expr *IterationVarRef |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5385 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_RValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5386 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
| 5387 | |
| 5388 | // Create the DeclStmt that holds the iteration variable. |
| 5389 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| 5390 | |
| 5391 | // Create the comparison against the array bound. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5392 | llvm::APInt Upper |
| 5393 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5394 | Expr *Comparison |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5395 | = new (S.Context) BinaryOperator(IterationVarRef, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5396 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 5397 | BO_NE, S.Context.BoolTy, |
| 5398 | VK_RValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5399 | |
| 5400 | // Create the pre-increment of the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5401 | Expr *Increment |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5402 | = new (S.Context) UnaryOperator(IterationVarRef, UO_PreInc, SizeType, |
| 5403 | VK_LValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5404 | |
| 5405 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5406 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
| 5407 | IterationVarRef, Loc)); |
| 5408 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
| 5409 | IterationVarRef, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5410 | |
| 5411 | // Build the copy for an individual element of the array. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5412 | StmtResult Copy = BuildSingleCopyAssign(S, Loc, ArrayTy->getElementType(), |
| 5413 | To, From, CopyingBaseSubobject, |
| 5414 | Depth + 1); |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 5415 | if (Copy.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5416 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5417 | |
| 5418 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5419 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5420 | S.MakeFullExpr(Comparison), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5421 | 0, S.MakeFullExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5422 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5423 | } |
| 5424 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5425 | /// \brief Determine whether the given class has a copy assignment operator |
| 5426 | /// that accepts a const-qualified argument. |
| 5427 | static bool hasConstCopyAssignment(Sema &S, const CXXRecordDecl *CClass) { |
| 5428 | CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(CClass); |
| 5429 | |
| 5430 | if (!Class->hasDeclaredCopyAssignment()) |
| 5431 | S.DeclareImplicitCopyAssignment(Class); |
| 5432 | |
| 5433 | QualType ClassType = S.Context.getCanonicalType(S.Context.getTypeDeclType(Class)); |
| 5434 | DeclarationName OpName |
| 5435 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 5436 | |
| 5437 | DeclContext::lookup_const_iterator Op, OpEnd; |
| 5438 | for (llvm::tie(Op, OpEnd) = Class->lookup(OpName); Op != OpEnd; ++Op) { |
| 5439 | // C++ [class.copy]p9: |
| 5440 | // A user-declared copy assignment operator is a non-static non-template |
| 5441 | // member function of class X with exactly one parameter of type X, X&, |
| 5442 | // const X&, volatile X& or const volatile X&. |
| 5443 | const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); |
| 5444 | if (!Method) |
| 5445 | continue; |
| 5446 | |
| 5447 | if (Method->isStatic()) |
| 5448 | continue; |
| 5449 | if (Method->getPrimaryTemplate()) |
| 5450 | continue; |
| 5451 | const FunctionProtoType *FnType = |
| 5452 | Method->getType()->getAs<FunctionProtoType>(); |
| 5453 | assert(FnType && "Overloaded operator has no prototype."); |
| 5454 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 5455 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 5456 | continue; |
| 5457 | bool AcceptsConst = true; |
| 5458 | QualType ArgType = FnType->getArgType(0); |
| 5459 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()){ |
| 5460 | ArgType = Ref->getPointeeType(); |
| 5461 | // Is it a non-const lvalue reference? |
| 5462 | if (!ArgType.isConstQualified()) |
| 5463 | AcceptsConst = false; |
| 5464 | } |
| 5465 | if (!S.Context.hasSameUnqualifiedType(ArgType, ClassType)) |
| 5466 | continue; |
| 5467 | |
| 5468 | // We have a single argument of type cv X or cv X&, i.e. we've found the |
| 5469 | // copy assignment operator. Return whether it accepts const arguments. |
| 5470 | return AcceptsConst; |
| 5471 | } |
| 5472 | assert(Class->isInvalidDecl() && |
| 5473 | "No copy assignment operator declared in valid code."); |
| 5474 | return false; |
| 5475 | } |
| 5476 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5477 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5478 | // Note: The following rules are largely analoguous to the copy |
| 5479 | // constructor rules. Note that virtual bases are not taken into account |
| 5480 | // for determining the argument type of the operator. Note also that |
| 5481 | // operators taking an object instead of a reference are allowed. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 5482 | |
| 5483 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5484 | // C++ [class.copy]p10: |
| 5485 | // If the class definition does not explicitly declare a copy |
| 5486 | // assignment operator, one is declared implicitly. |
| 5487 | // The implicitly-defined copy assignment operator for a class X |
| 5488 | // will have the form |
| 5489 | // |
| 5490 | // X& X::operator=(const X&) |
| 5491 | // |
| 5492 | // if |
| 5493 | bool HasConstCopyAssignment = true; |
| 5494 | |
| 5495 | // -- each direct base class B of X has a copy assignment operator |
| 5496 | // whose parameter is of type const B&, const volatile B& or B, |
| 5497 | // and |
| 5498 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5499 | BaseEnd = ClassDecl->bases_end(); |
| 5500 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
| 5501 | assert(!Base->getType()->isDependentType() && |
| 5502 | "Cannot generate implicit members for class with dependent bases."); |
| 5503 | const CXXRecordDecl *BaseClassDecl |
| 5504 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5505 | HasConstCopyAssignment = hasConstCopyAssignment(*this, BaseClassDecl); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | // -- for all the nonstatic data members of X that are of a class |
| 5509 | // type M (or array thereof), each such class type has a copy |
| 5510 | // assignment operator whose parameter is of type const M&, |
| 5511 | // const volatile M& or M. |
| 5512 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5513 | FieldEnd = ClassDecl->field_end(); |
| 5514 | HasConstCopyAssignment && Field != FieldEnd; |
| 5515 | ++Field) { |
| 5516 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 5517 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 5518 | const CXXRecordDecl *FieldClassDecl |
| 5519 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5520 | HasConstCopyAssignment = hasConstCopyAssignment(*this, FieldClassDecl); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5521 | } |
| 5522 | } |
| 5523 | |
| 5524 | // Otherwise, the implicitly declared copy assignment operator will |
| 5525 | // have the form |
| 5526 | // |
| 5527 | // X& X::operator=(X&) |
| 5528 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 5529 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 5530 | if (HasConstCopyAssignment) |
| 5531 | ArgType = ArgType.withConst(); |
| 5532 | ArgType = Context.getLValueReferenceType(ArgType); |
| 5533 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 5534 | // C++ [except.spec]p14: |
| 5535 | // An implicitly declared special member function (Clause 12) shall have an |
| 5536 | // exception-specification. [...] |
| 5537 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 5538 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5539 | BaseEnd = ClassDecl->bases_end(); |
| 5540 | Base != BaseEnd; ++Base) { |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5541 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 5542 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5543 | |
| 5544 | if (!BaseClassDecl->hasDeclaredCopyAssignment()) |
| 5545 | DeclareImplicitCopyAssignment(BaseClassDecl); |
| 5546 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 5547 | if (CXXMethodDecl *CopyAssign |
| 5548 | = BaseClassDecl->getCopyAssignmentOperator(HasConstCopyAssignment)) |
| 5549 | ExceptSpec.CalledDecl(CopyAssign); |
| 5550 | } |
| 5551 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5552 | FieldEnd = ClassDecl->field_end(); |
| 5553 | Field != FieldEnd; |
| 5554 | ++Field) { |
| 5555 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 5556 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5557 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 5558 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5559 | |
| 5560 | if (!FieldClassDecl->hasDeclaredCopyAssignment()) |
| 5561 | DeclareImplicitCopyAssignment(FieldClassDecl); |
| 5562 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 5563 | if (CXXMethodDecl *CopyAssign |
| 5564 | = FieldClassDecl->getCopyAssignmentOperator(HasConstCopyAssignment)) |
| 5565 | ExceptSpec.CalledDecl(CopyAssign); |
| 5566 | } |
| 5567 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 5568 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5569 | // An implicitly-declared copy assignment operator is an inline public |
| 5570 | // member of its class. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5571 | FunctionProtoType::ExtProtoInfo EPI; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 5572 | EPI.ExceptionSpecType = ExceptSpec.getExceptionSpecType(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5573 | EPI.NumExceptions = ExceptSpec.size(); |
| 5574 | EPI.Exceptions = ExceptSpec.data(); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5575 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5576 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 5577 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5578 | CXXMethodDecl *CopyAssignment |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5579 | = CXXMethodDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 5580 | Context.getFunctionType(RetType, &ArgType, 1, EPI), |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5581 | /*TInfo=*/0, /*isStatic=*/false, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5582 | /*StorageClassAsWritten=*/SC_None, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 5583 | /*isInline=*/true, |
| 5584 | SourceLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5585 | CopyAssignment->setAccess(AS_public); |
| 5586 | CopyAssignment->setImplicit(); |
| 5587 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5588 | |
| 5589 | // Add the parameter to the operator. |
| 5590 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5591 | ClassLoc, ClassLoc, /*Id=*/0, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5592 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5593 | SC_None, |
| 5594 | SC_None, 0); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5595 | CopyAssignment->setParams(&FromParam, 1); |
| 5596 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5597 | // Note that we have added this copy-assignment operator. |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5598 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
| 5599 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5600 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 5601 | PushOnScopeChains(CopyAssignment, S, false); |
| 5602 | ClassDecl->addDecl(CopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 5603 | |
| 5604 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 5605 | return CopyAssignment; |
| 5606 | } |
| 5607 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5608 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 5609 | CXXMethodDecl *CopyAssignOperator) { |
| 5610 | assert((CopyAssignOperator->isImplicit() && |
| 5611 | CopyAssignOperator->isOverloadedOperator() && |
| 5612 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 5613 | !CopyAssignOperator->isUsed(false)) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5614 | "DefineImplicitCopyAssignment called for wrong function"); |
| 5615 | |
| 5616 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 5617 | |
| 5618 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 5619 | CopyAssignOperator->setInvalidDecl(); |
| 5620 | return; |
| 5621 | } |
| 5622 | |
| 5623 | CopyAssignOperator->setUsed(); |
| 5624 | |
| 5625 | ImplicitlyDefinedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 5626 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5627 | |
| 5628 | // C++0x [class.copy]p30: |
| 5629 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 5630 | // for a non-union class X performs memberwise copy assignment of its |
| 5631 | // subobjects. The direct base classes of X are assigned first, in the |
| 5632 | // order of their declaration in the base-specifier-list, and then the |
| 5633 | // immediate non-static data members of X are assigned, in the order in |
| 5634 | // which they were declared in the class definition. |
| 5635 | |
| 5636 | // The statements that form the synthesized function body. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5637 | ASTOwningVector<Stmt*> Statements(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5638 | |
| 5639 | // The parameter for the "other" object, which we are copying from. |
| 5640 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 5641 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 5642 | QualType OtherRefType = Other->getType(); |
| 5643 | if (const LValueReferenceType *OtherRef |
| 5644 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 5645 | OtherRefType = OtherRef->getPointeeType(); |
| 5646 | OtherQuals = OtherRefType.getQualifiers(); |
| 5647 | } |
| 5648 | |
| 5649 | // Our location for everything implicitly-generated. |
| 5650 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 5651 | |
| 5652 | // Construct a reference to the "other" object. We'll be using this |
| 5653 | // throughout the generated ASTs. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5654 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5655 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 5656 | |
| 5657 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 5658 | // ASTs. |
| 5659 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 5660 | assert(This && "Reference to this cannot fail!"); |
| 5661 | |
| 5662 | // Assign base classes. |
| 5663 | bool Invalid = false; |
| 5664 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5665 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 5666 | // Form the assignment: |
| 5667 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 5668 | QualType BaseType = Base->getType().getUnqualifiedType(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 5669 | if (!BaseType->isRecordType()) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5670 | Invalid = true; |
| 5671 | continue; |
| 5672 | } |
| 5673 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5674 | CXXCastPath BasePath; |
| 5675 | BasePath.push_back(Base); |
| 5676 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5677 | // Construct the "from" expression, which is an implicit cast to the |
| 5678 | // appropriately-qualified base type. |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5679 | Expr *From = OtherRef; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5680 | From = ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
| 5681 | CK_UncheckedDerivedToBase, |
| 5682 | VK_LValue, &BasePath).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5683 | |
| 5684 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5685 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5686 | |
| 5687 | // Implicitly cast "this" to the appropriately-qualified base type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5688 | To = ImpCastExprToType(To.take(), |
| 5689 | Context.getCVRQualifiedType(BaseType, |
| 5690 | CopyAssignOperator->getTypeQualifiers()), |
| 5691 | CK_UncheckedDerivedToBase, |
| 5692 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5693 | |
| 5694 | // Build the copy. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5695 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5696 | To.get(), From, |
| 5697 | /*CopyingBaseSubobject=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5698 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 5699 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5700 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5701 | CopyAssignOperator->setInvalidDecl(); |
| 5702 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5703 | } |
| 5704 | |
| 5705 | // Success! Record the copy. |
| 5706 | Statements.push_back(Copy.takeAs<Expr>()); |
| 5707 | } |
| 5708 | |
| 5709 | // \brief Reference to the __builtin_memcpy function. |
| 5710 | Expr *BuiltinMemCpyRef = 0; |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 5711 | // \brief Reference to the __builtin_objc_memmove_collectable function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5712 | Expr *CollectableMemCpyRef = 0; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5713 | |
| 5714 | // Assign non-static members. |
| 5715 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5716 | FieldEnd = ClassDecl->field_end(); |
| 5717 | Field != FieldEnd; ++Field) { |
| 5718 | // Check for members of reference type; we can't copy those. |
| 5719 | if (Field->getType()->isReferenceType()) { |
| 5720 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 5721 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 5722 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 5723 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5724 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5725 | Invalid = true; |
| 5726 | continue; |
| 5727 | } |
| 5728 | |
| 5729 | // Check for members of const-qualified, non-class type. |
| 5730 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 5731 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 5732 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 5733 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 5734 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 5735 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5736 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5737 | Invalid = true; |
| 5738 | continue; |
| 5739 | } |
| 5740 | |
| 5741 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 5742 | if (FieldType->isIncompleteArrayType()) { |
| 5743 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 5744 | "Incomplete array type is not valid"); |
| 5745 | continue; |
| 5746 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5747 | |
| 5748 | // Build references to the field in the object we're copying from and to. |
| 5749 | CXXScopeSpec SS; // Intentionally empty |
| 5750 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 5751 | LookupMemberName); |
| 5752 | MemberLookup.addDecl(*Field); |
| 5753 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5754 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5755 | Loc, /*IsArrow=*/false, |
| 5756 | SS, 0, MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5757 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5758 | Loc, /*IsArrow=*/true, |
| 5759 | SS, 0, MemberLookup, 0); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5760 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 5761 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 5762 | |
| 5763 | // If the field should be copied with __builtin_memcpy rather than via |
| 5764 | // explicit assignments, do so. This optimization only applies for arrays |
| 5765 | // of scalars and arrays of class type with trivial copy-assignment |
| 5766 | // operators. |
| 5767 | if (FieldType->isArrayType() && |
| 5768 | (!BaseType->isRecordType() || |
| 5769 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()) |
| 5770 | ->hasTrivialCopyAssignment())) { |
| 5771 | // Compute the size of the memory buffer to be copied. |
| 5772 | QualType SizeType = Context.getSizeType(); |
| 5773 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 5774 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 5775 | for (const ConstantArrayType *Array |
| 5776 | = Context.getAsConstantArrayType(FieldType); |
| 5777 | Array; |
| 5778 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5779 | llvm::APInt ArraySize |
| 5780 | = Array->getSize().zextOrTrunc(Size.getBitWidth()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5781 | Size *= ArraySize; |
| 5782 | } |
| 5783 | |
| 5784 | // Take the address of the field references for "from" and "to". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5785 | From = CreateBuiltinUnaryOp(Loc, UO_AddrOf, From.get()); |
| 5786 | To = CreateBuiltinUnaryOp(Loc, UO_AddrOf, To.get()); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5787 | |
| 5788 | bool NeedsCollectableMemCpy = |
| 5789 | (BaseType->isRecordType() && |
| 5790 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()); |
| 5791 | |
| 5792 | if (NeedsCollectableMemCpy) { |
| 5793 | if (!CollectableMemCpyRef) { |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 5794 | // Create a reference to the __builtin_objc_memmove_collectable function. |
| 5795 | LookupResult R(*this, |
| 5796 | &Context.Idents.get("__builtin_objc_memmove_collectable"), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5797 | Loc, LookupOrdinaryName); |
| 5798 | LookupName(R, TUScope, true); |
| 5799 | |
| 5800 | FunctionDecl *CollectableMemCpy = R.getAsSingle<FunctionDecl>(); |
| 5801 | if (!CollectableMemCpy) { |
| 5802 | // Something went horribly wrong earlier, and we will have |
| 5803 | // complained about it. |
| 5804 | Invalid = true; |
| 5805 | continue; |
| 5806 | } |
| 5807 | |
| 5808 | CollectableMemCpyRef = BuildDeclRefExpr(CollectableMemCpy, |
| 5809 | CollectableMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5810 | VK_LValue, Loc, 0).take(); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5811 | assert(CollectableMemCpyRef && "Builtin reference cannot fail"); |
| 5812 | } |
| 5813 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5814 | // Create a reference to the __builtin_memcpy builtin function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5815 | else if (!BuiltinMemCpyRef) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5816 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 5817 | LookupOrdinaryName); |
| 5818 | LookupName(R, TUScope, true); |
| 5819 | |
| 5820 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 5821 | if (!BuiltinMemCpy) { |
| 5822 | // Something went horribly wrong earlier, and we will have complained |
| 5823 | // about it. |
| 5824 | Invalid = true; |
| 5825 | continue; |
| 5826 | } |
| 5827 | |
| 5828 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 5829 | BuiltinMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5830 | VK_LValue, Loc, 0).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5831 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 5832 | } |
| 5833 | |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5834 | ASTOwningVector<Expr*> CallArgs(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5835 | CallArgs.push_back(To.takeAs<Expr>()); |
| 5836 | CallArgs.push_back(From.takeAs<Expr>()); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5837 | CallArgs.push_back(IntegerLiteral::Create(Context, Size, SizeType, Loc)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5838 | ExprResult Call = ExprError(); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5839 | if (NeedsCollectableMemCpy) |
| 5840 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5841 | CollectableMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5842 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5843 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5844 | else |
| 5845 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5846 | BuiltinMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5847 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5848 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5849 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5850 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 5851 | Statements.push_back(Call.takeAs<Expr>()); |
| 5852 | continue; |
| 5853 | } |
| 5854 | |
| 5855 | // Build the copy of this field. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5856 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, FieldType, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5857 | To.get(), From.get(), |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 5858 | /*CopyingBaseSubobject=*/false); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5859 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 5860 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5861 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5862 | CopyAssignOperator->setInvalidDecl(); |
| 5863 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5864 | } |
| 5865 | |
| 5866 | // Success! Record the copy. |
| 5867 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 5868 | } |
| 5869 | |
| 5870 | if (!Invalid) { |
| 5871 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5872 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5873 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5874 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5875 | if (Return.isInvalid()) |
| 5876 | Invalid = true; |
| 5877 | else { |
| 5878 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5879 | |
| 5880 | if (Trap.hasErrorOccurred()) { |
| 5881 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5882 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5883 | Invalid = true; |
| 5884 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5885 | } |
| 5886 | } |
| 5887 | |
| 5888 | if (Invalid) { |
| 5889 | CopyAssignOperator->setInvalidDecl(); |
| 5890 | return; |
| 5891 | } |
| 5892 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5893 | StmtResult Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5894 | /*isStmtExpr=*/false); |
| 5895 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 5896 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5897 | } |
| 5898 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5899 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 5900 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5901 | // C++ [class.copy]p4: |
| 5902 | // If the class definition does not explicitly declare a copy |
| 5903 | // constructor, one is declared implicitly. |
| 5904 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5905 | // C++ [class.copy]p5: |
| 5906 | // The implicitly-declared copy constructor for a class X will |
| 5907 | // have the form |
| 5908 | // |
| 5909 | // X::X(const X&) |
| 5910 | // |
| 5911 | // if |
| 5912 | bool HasConstCopyConstructor = true; |
| 5913 | |
| 5914 | // -- each direct or virtual base class B of X has a copy |
| 5915 | // constructor whose first parameter is of type const B& or |
| 5916 | // const volatile B&, and |
| 5917 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5918 | BaseEnd = ClassDecl->bases_end(); |
| 5919 | HasConstCopyConstructor && Base != BaseEnd; |
| 5920 | ++Base) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5921 | // Virtual bases are handled below. |
| 5922 | if (Base->isVirtual()) |
| 5923 | continue; |
| 5924 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5925 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5926 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5927 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5928 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5929 | |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5930 | HasConstCopyConstructor |
| 5931 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 5932 | } |
| 5933 | |
| 5934 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 5935 | BaseEnd = ClassDecl->vbases_end(); |
| 5936 | HasConstCopyConstructor && Base != BaseEnd; |
| 5937 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5938 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5939 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5940 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5941 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5942 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5943 | HasConstCopyConstructor |
| 5944 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 5945 | } |
| 5946 | |
| 5947 | // -- for all the nonstatic data members of X that are of a |
| 5948 | // class type M (or array thereof), each such class type |
| 5949 | // has a copy constructor whose first parameter is of type |
| 5950 | // const M& or const volatile M&. |
| 5951 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5952 | FieldEnd = ClassDecl->field_end(); |
| 5953 | HasConstCopyConstructor && Field != FieldEnd; |
| 5954 | ++Field) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5955 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5956 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5957 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5958 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5959 | if (!FieldClassDecl->hasDeclaredCopyConstructor()) |
| 5960 | DeclareImplicitCopyConstructor(FieldClassDecl); |
| 5961 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5962 | HasConstCopyConstructor |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5963 | = FieldClassDecl->hasConstCopyConstructor(Context); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5964 | } |
| 5965 | } |
| 5966 | |
| 5967 | // Otherwise, the implicitly declared copy constructor will have |
| 5968 | // the form |
| 5969 | // |
| 5970 | // X::X(X&) |
| 5971 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 5972 | QualType ArgType = ClassType; |
| 5973 | if (HasConstCopyConstructor) |
| 5974 | ArgType = ArgType.withConst(); |
| 5975 | ArgType = Context.getLValueReferenceType(ArgType); |
| 5976 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5977 | // C++ [except.spec]p14: |
| 5978 | // An implicitly declared special member function (Clause 12) shall have an |
| 5979 | // exception-specification. [...] |
| 5980 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 5981 | unsigned Quals = HasConstCopyConstructor? Qualifiers::Const : 0; |
| 5982 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5983 | BaseEnd = ClassDecl->bases_end(); |
| 5984 | Base != BaseEnd; |
| 5985 | ++Base) { |
| 5986 | // Virtual bases are handled below. |
| 5987 | if (Base->isVirtual()) |
| 5988 | continue; |
| 5989 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5990 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5991 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5992 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5993 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5994 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5995 | if (CXXConstructorDecl *CopyConstructor |
| 5996 | = BaseClassDecl->getCopyConstructor(Context, Quals)) |
| 5997 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5998 | } |
| 5999 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 6000 | BaseEnd = ClassDecl->vbases_end(); |
| 6001 | Base != BaseEnd; |
| 6002 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 6003 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 6004 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 6005 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 6006 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 6007 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 6008 | if (CXXConstructorDecl *CopyConstructor |
| 6009 | = BaseClassDecl->getCopyConstructor(Context, Quals)) |
| 6010 | ExceptSpec.CalledDecl(CopyConstructor); |
| 6011 | } |
| 6012 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 6013 | FieldEnd = ClassDecl->field_end(); |
| 6014 | Field != FieldEnd; |
| 6015 | ++Field) { |
| 6016 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 6017 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 6018 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 6019 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 6020 | if (!FieldClassDecl->hasDeclaredCopyConstructor()) |
| 6021 | DeclareImplicitCopyConstructor(FieldClassDecl); |
| 6022 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 6023 | if (CXXConstructorDecl *CopyConstructor |
| 6024 | = FieldClassDecl->getCopyConstructor(Context, Quals)) |
| 6025 | ExceptSpec.CalledDecl(CopyConstructor); |
| 6026 | } |
| 6027 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6028 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 6029 | // An implicitly-declared copy constructor is an inline public |
| 6030 | // member of its class. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6031 | FunctionProtoType::ExtProtoInfo EPI; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 6032 | EPI.ExceptionSpecType = ExceptSpec.getExceptionSpecType(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6033 | EPI.NumExceptions = ExceptSpec.size(); |
| 6034 | EPI.Exceptions = ExceptSpec.data(); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 6035 | DeclarationName Name |
| 6036 | = Context.DeclarationNames.getCXXConstructorName( |
| 6037 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6038 | SourceLocation ClassLoc = ClassDecl->getLocation(); |
| 6039 | DeclarationNameInfo NameInfo(Name, ClassLoc); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 6040 | CXXConstructorDecl *CopyConstructor |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6041 | = CXXConstructorDecl::Create(Context, ClassDecl, ClassLoc, NameInfo, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 6042 | Context.getFunctionType(Context.VoidTy, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6043 | &ArgType, 1, EPI), |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 6044 | /*TInfo=*/0, |
| 6045 | /*isExplicit=*/false, |
| 6046 | /*isInline=*/true, |
| 6047 | /*isImplicitlyDeclared=*/true); |
| 6048 | CopyConstructor->setAccess(AS_public); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 6049 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
| 6050 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 6051 | // Note that we have declared this constructor. |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 6052 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 6053 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 6054 | // Add the parameter to the constructor. |
| 6055 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6056 | ClassLoc, ClassLoc, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 6057 | /*IdentifierInfo=*/0, |
| 6058 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6059 | SC_None, |
| 6060 | SC_None, 0); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 6061 | CopyConstructor->setParams(&FromParam, 1); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 6062 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 6063 | PushOnScopeChains(CopyConstructor, S, false); |
| 6064 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 6065 | |
| 6066 | return CopyConstructor; |
| 6067 | } |
| 6068 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6069 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 6070 | CXXConstructorDecl *CopyConstructor, |
| 6071 | unsigned TypeQuals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6072 | assert((CopyConstructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 6073 | CopyConstructor->isCopyConstructor(TypeQuals) && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 6074 | !CopyConstructor->isUsed(false)) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6075 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6076 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 6077 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6078 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6079 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 6080 | ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 6081 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6082 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 6083 | if (SetCtorInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) || |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 6084 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 6085 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 6086 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 6087 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 6088 | } else { |
| 6089 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 6090 | CopyConstructor->getLocation(), |
| 6091 | MultiStmtArg(*this, 0, 0), |
| 6092 | /*isStmtExpr=*/false) |
| 6093 | .takeAs<Stmt>()); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 6094 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 6095 | |
| 6096 | CopyConstructor->setUsed(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6097 | } |
| 6098 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6099 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 6100 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6101 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6102 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6103 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6104 | unsigned ConstructKind, |
| 6105 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 6106 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6107 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 6108 | // C++0x [class.copy]p34: |
| 6109 | // When certain criteria are met, an implementation is allowed to |
| 6110 | // omit the copy/move construction of a class object, even if the |
| 6111 | // copy/move constructor and/or destructor for the object have |
| 6112 | // side effects. [...] |
| 6113 | // - when a temporary class object that has not been bound to a |
| 6114 | // reference (12.2) would be copied/moved to a class object |
| 6115 | // with the same cv-unqualified type, the copy/move operation |
| 6116 | // can be omitted by constructing the temporary object |
| 6117 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 6118 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
Douglas Gregor | 70a21de | 2011-01-27 23:24:55 +0000 | [diff] [blame] | 6119 | Constructor->isCopyOrMoveConstructor() && ExprArgs.size() >= 1) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 6120 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 6121 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 6122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6123 | |
| 6124 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6125 | Elidable, move(ExprArgs), RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6126 | ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 6127 | } |
| 6128 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 6129 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 6130 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6131 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 6132 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 6133 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6134 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6135 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6136 | unsigned ConstructKind, |
| 6137 | SourceRange ParenRange) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 6138 | unsigned NumExprs = ExprArgs.size(); |
| 6139 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6140 | |
Nick Lewycky | 909a70d | 2011-03-25 01:44:32 +0000 | [diff] [blame] | 6141 | for (specific_attr_iterator<NonNullAttr> |
| 6142 | i = Constructor->specific_attr_begin<NonNullAttr>(), |
| 6143 | e = Constructor->specific_attr_end<NonNullAttr>(); i != e; ++i) { |
| 6144 | const NonNullAttr *NonNull = *i; |
| 6145 | CheckNonNullArguments(NonNull, ExprArgs.get(), ConstructLoc); |
| 6146 | } |
| 6147 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 6148 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6149 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6150 | Constructor, Elidable, Exprs, NumExprs, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 6151 | RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6152 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 6153 | ParenRange)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 6154 | } |
| 6155 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6156 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 6157 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 6158 | MultiExprArg Exprs) { |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6159 | // FIXME: Provide the correct paren SourceRange when available. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6160 | ExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 6161 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6162 | move(Exprs), false, CXXConstructExpr::CK_Complete, |
| 6163 | SourceRange()); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 6164 | if (TempResult.isInvalid()) |
| 6165 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6166 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 6167 | Expr *Temp = TempResult.takeAs<Expr>(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6168 | CheckImplicitConversions(Temp, VD->getLocation()); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 6169 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6170 | Temp = MaybeCreateExprWithCleanups(Temp); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 6171 | VD->setInit(Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6172 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 6173 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 6176 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 6177 | if (VD->isInvalidDecl()) return; |
| 6178 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 6179 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 6180 | if (ClassDecl->isInvalidDecl()) return; |
| 6181 | if (ClassDecl->hasTrivialDestructor()) return; |
| 6182 | if (ClassDecl->isDependentContext()) return; |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 6183 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 6184 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
| 6185 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
| 6186 | CheckDestructorAccess(VD->getLocation(), Destructor, |
| 6187 | PDiag(diag::err_access_dtor_var) |
| 6188 | << VD->getDeclName() |
| 6189 | << VD->getType()); |
Anders Carlsson | 2b32dad | 2011-03-24 01:01:41 +0000 | [diff] [blame] | 6190 | |
Chandler Carruth | 1d71cbf | 2011-03-27 21:26:48 +0000 | [diff] [blame] | 6191 | if (!VD->hasGlobalStorage()) return; |
| 6192 | |
| 6193 | // Emit warning for non-trivial dtor in global scope (a real global, |
| 6194 | // class-static, function-static). |
| 6195 | Diag(VD->getLocation(), diag::warn_exit_time_destructor); |
| 6196 | |
| 6197 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 6198 | if (!VD->isStaticLocal()) |
| 6199 | Diag(VD->getLocation(), diag::warn_global_destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6200 | } |
| 6201 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6202 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6203 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 6204 | /// e.g: "int x(1);" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6205 | void Sema::AddCXXDirectInitializerToDecl(Decl *RealDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6206 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6207 | MultiExprArg Exprs, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 6208 | SourceLocation RParenLoc, |
| 6209 | bool TypeMayContainAuto) { |
Daniel Dunbar | 5184626 | 2009-12-24 19:19:26 +0000 | [diff] [blame] | 6210 | assert(Exprs.size() != 0 && Exprs.get() && "missing expressions"); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6211 | |
| 6212 | // If there is no declaration, there was an error parsing it. Just ignore |
| 6213 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6214 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6215 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6216 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6217 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 6218 | if (!VDecl) { |
| 6219 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 6220 | RealDecl->setInvalidDecl(); |
| 6221 | return; |
| 6222 | } |
| 6223 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 6224 | // C++0x [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. |
| 6225 | if (TypeMayContainAuto && VDecl->getType()->getContainedAutoType()) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 6226 | // FIXME: n3225 doesn't actually seem to indicate this is ill-formed |
| 6227 | if (Exprs.size() > 1) { |
| 6228 | Diag(Exprs.get()[1]->getSourceRange().getBegin(), |
| 6229 | diag::err_auto_var_init_multiple_expressions) |
| 6230 | << VDecl->getDeclName() << VDecl->getType() |
| 6231 | << VDecl->getSourceRange(); |
| 6232 | RealDecl->setInvalidDecl(); |
| 6233 | return; |
| 6234 | } |
| 6235 | |
| 6236 | Expr *Init = Exprs.get()[0]; |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 6237 | TypeSourceInfo *DeducedType = 0; |
| 6238 | if (!DeduceAutoType(VDecl->getTypeSourceInfo(), Init, DeducedType)) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 6239 | Diag(VDecl->getLocation(), diag::err_auto_var_deduction_failure) |
| 6240 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 6241 | << Init->getSourceRange(); |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 6242 | if (!DeducedType) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 6243 | RealDecl->setInvalidDecl(); |
| 6244 | return; |
| 6245 | } |
Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 6246 | VDecl->setTypeSourceInfo(DeducedType); |
| 6247 | VDecl->setType(DeducedType->getType()); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 6248 | |
| 6249 | // If this is a redeclaration, check that the type we just deduced matches |
| 6250 | // the previously declared type. |
| 6251 | if (VarDecl *Old = VDecl->getPreviousDeclaration()) |
| 6252 | MergeVarDeclTypes(VDecl, Old); |
| 6253 | } |
| 6254 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 6255 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 6256 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6257 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 6258 | // |
| 6259 | // Clients that want to distinguish between the two forms, can check for |
| 6260 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 6261 | // A major benefit is that clients that don't particularly care about which |
| 6262 | // exactly form was it (like the CodeGen) can handle both cases without |
| 6263 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 6264 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6265 | // C++ 8.5p11: |
| 6266 | // The form of initialization (using parentheses or '=') is generally |
| 6267 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 6268 | // class type. |
| 6269 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 6270 | if (!VDecl->getType()->isDependentType() && |
| 6271 | RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 6272 | diag::err_typecheck_decl_incomplete_type)) { |
| 6273 | VDecl->setInvalidDecl(); |
| 6274 | return; |
| 6275 | } |
| 6276 | |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6277 | // The variable can not have an abstract class type. |
| 6278 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 6279 | diag::err_abstract_type_in_decl, |
| 6280 | AbstractVariableType)) |
| 6281 | VDecl->setInvalidDecl(); |
| 6282 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 6283 | const VarDecl *Def; |
| 6284 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6285 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| 6286 | << VDecl->getDeclName(); |
| 6287 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 6288 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 6289 | return; |
| 6290 | } |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 6291 | |
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 6292 | // C++ [class.static.data]p4 |
| 6293 | // If a static data member is of const integral or const |
| 6294 | // enumeration type, its declaration in the class definition can |
| 6295 | // specify a constant-initializer which shall be an integral |
| 6296 | // constant expression (5.19). In that case, the member can appear |
| 6297 | // in integral constant expressions. The member shall still be |
| 6298 | // defined in a namespace scope if it is used in the program and the |
| 6299 | // namespace scope definition shall not contain an initializer. |
| 6300 | // |
| 6301 | // We already performed a redefinition check above, but for static |
| 6302 | // data members we also need to check whether there was an in-class |
| 6303 | // declaration with an initializer. |
| 6304 | const VarDecl* PrevInit = 0; |
| 6305 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { |
| 6306 | Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName(); |
| 6307 | Diag(PrevInit->getLocation(), diag::note_previous_definition); |
| 6308 | return; |
| 6309 | } |
| 6310 | |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 6311 | bool IsDependent = false; |
| 6312 | for (unsigned I = 0, N = Exprs.size(); I != N; ++I) { |
| 6313 | if (DiagnoseUnexpandedParameterPack(Exprs.get()[I], UPPC_Expression)) { |
| 6314 | VDecl->setInvalidDecl(); |
| 6315 | return; |
| 6316 | } |
| 6317 | |
| 6318 | if (Exprs.get()[I]->isTypeDependent()) |
| 6319 | IsDependent = true; |
| 6320 | } |
| 6321 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 6322 | // If either the declaration has a dependent type or if any of the |
| 6323 | // expressions is type-dependent, we represent the initialization |
| 6324 | // via a ParenListExpr for later use during template instantiation. |
Douglas Gregor | a31040f | 2010-12-16 01:31:22 +0000 | [diff] [blame] | 6325 | if (VDecl->getType()->isDependentType() || IsDependent) { |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 6326 | // Let clients know that initialization was done with a direct initializer. |
| 6327 | VDecl->setCXXDirectInitializer(true); |
| 6328 | |
| 6329 | // Store the initialization expressions as a ParenListExpr. |
| 6330 | unsigned NumExprs = Exprs.size(); |
| 6331 | VDecl->setInit(new (Context) ParenListExpr(Context, LParenLoc, |
| 6332 | (Expr **)Exprs.release(), |
| 6333 | NumExprs, RParenLoc)); |
| 6334 | return; |
| 6335 | } |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6336 | |
| 6337 | // Capture the variable that is being initialized and the style of |
| 6338 | // initialization. |
| 6339 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 6340 | |
| 6341 | // FIXME: Poor source location information. |
| 6342 | InitializationKind Kind |
| 6343 | = InitializationKind::CreateDirect(VDecl->getLocation(), |
| 6344 | LParenLoc, RParenLoc); |
| 6345 | |
| 6346 | InitializationSequence InitSeq(*this, Entity, Kind, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6347 | Exprs.get(), Exprs.size()); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6348 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(Exprs)); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6349 | if (Result.isInvalid()) { |
| 6350 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6351 | return; |
| 6352 | } |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6353 | |
| 6354 | CheckImplicitConversions(Result.get(), LParenLoc); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 6355 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 6356 | Result = MaybeCreateExprWithCleanups(Result); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 6357 | VDecl->setInit(Result.takeAs<Expr>()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6358 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 6359 | |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 6360 | CheckCompleteVariableDeclaration(VDecl); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 6361 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 6362 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 6363 | /// \brief Given a constructor and the set of arguments provided for the |
| 6364 | /// constructor, convert the arguments and add any required default arguments |
| 6365 | /// to form a proper call to this constructor. |
| 6366 | /// |
| 6367 | /// \returns true if an error occurred, false otherwise. |
| 6368 | bool |
| 6369 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 6370 | MultiExprArg ArgsPtr, |
| 6371 | SourceLocation Loc, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 6372 | ASTOwningVector<Expr*> &ConvertedArgs) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 6373 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 6374 | unsigned NumArgs = ArgsPtr.size(); |
| 6375 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 6376 | |
| 6377 | const FunctionProtoType *Proto |
| 6378 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 6379 | assert(Proto && "Constructor without a prototype?"); |
| 6380 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 6381 | |
| 6382 | // 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] | 6383 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 6384 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 6385 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 6386 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 6387 | |
| 6388 | VariadicCallType CallType = |
| 6389 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 6390 | llvm::SmallVector<Expr *, 8> AllArgs; |
| 6391 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 6392 | Proto, 0, Args, NumArgs, AllArgs, |
| 6393 | CallType); |
| 6394 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 6395 | ConvertedArgs.push_back(AllArgs[i]); |
| 6396 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 6397 | } |
| 6398 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 6399 | static inline bool |
| 6400 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 6401 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6402 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 6403 | if (isa<NamespaceDecl>(DC)) { |
| 6404 | return SemaRef.Diag(FnDecl->getLocation(), |
| 6405 | diag::err_operator_new_delete_declared_in_namespace) |
| 6406 | << FnDecl->getDeclName(); |
| 6407 | } |
| 6408 | |
| 6409 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6410 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 6411 | return SemaRef.Diag(FnDecl->getLocation(), |
| 6412 | diag::err_operator_new_delete_declared_static) |
| 6413 | << FnDecl->getDeclName(); |
| 6414 | } |
| 6415 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 6416 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 6417 | } |
| 6418 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6419 | static inline bool |
| 6420 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 6421 | CanQualType ExpectedResultType, |
| 6422 | CanQualType ExpectedFirstParamType, |
| 6423 | unsigned DependentParamTypeDiag, |
| 6424 | unsigned InvalidParamTypeDiag) { |
| 6425 | QualType ResultType = |
| 6426 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 6427 | |
| 6428 | // Check that the result type is not dependent. |
| 6429 | if (ResultType->isDependentType()) |
| 6430 | return SemaRef.Diag(FnDecl->getLocation(), |
| 6431 | diag::err_operator_new_delete_dependent_result_type) |
| 6432 | << FnDecl->getDeclName() << ExpectedResultType; |
| 6433 | |
| 6434 | // Check that the result type is what we expect. |
| 6435 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 6436 | return SemaRef.Diag(FnDecl->getLocation(), |
| 6437 | diag::err_operator_new_delete_invalid_result_type) |
| 6438 | << FnDecl->getDeclName() << ExpectedResultType; |
| 6439 | |
| 6440 | // A function template must have at least 2 parameters. |
| 6441 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 6442 | return SemaRef.Diag(FnDecl->getLocation(), |
| 6443 | diag::err_operator_new_delete_template_too_few_parameters) |
| 6444 | << FnDecl->getDeclName(); |
| 6445 | |
| 6446 | // The function decl must have at least 1 parameter. |
| 6447 | if (FnDecl->getNumParams() == 0) |
| 6448 | return SemaRef.Diag(FnDecl->getLocation(), |
| 6449 | diag::err_operator_new_delete_too_few_parameters) |
| 6450 | << FnDecl->getDeclName(); |
| 6451 | |
| 6452 | // Check the the first parameter type is not dependent. |
| 6453 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 6454 | if (FirstParamType->isDependentType()) |
| 6455 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 6456 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 6457 | |
| 6458 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 6459 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6460 | ExpectedFirstParamType) |
| 6461 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 6462 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 6463 | |
| 6464 | return false; |
| 6465 | } |
| 6466 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 6467 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6468 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 6469 | // C++ [basic.stc.dynamic.allocation]p1: |
| 6470 | // A program is ill-formed if an allocation function is declared in a |
| 6471 | // namespace scope other than global scope or declared static in global |
| 6472 | // scope. |
| 6473 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 6474 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6475 | |
| 6476 | CanQualType SizeTy = |
| 6477 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 6478 | |
| 6479 | // C++ [basic.stc.dynamic.allocation]p1: |
| 6480 | // The return type shall be void*. The first parameter shall have type |
| 6481 | // std::size_t. |
| 6482 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 6483 | SizeTy, |
| 6484 | diag::err_operator_new_dependent_param_type, |
| 6485 | diag::err_operator_new_param_type)) |
| 6486 | return true; |
| 6487 | |
| 6488 | // C++ [basic.stc.dynamic.allocation]p1: |
| 6489 | // The first parameter shall not have an associated default argument. |
| 6490 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 6491 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6492 | diag::err_operator_new_default_arg) |
| 6493 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 6494 | |
| 6495 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 6496 | } |
| 6497 | |
| 6498 | static bool |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 6499 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 6500 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 6501 | // A program is ill-formed if deallocation functions are declared in a |
| 6502 | // namespace scope other than global scope or declared static in global |
| 6503 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 6504 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 6505 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 6506 | |
| 6507 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 6508 | // Each deallocation function shall return void and its first parameter |
| 6509 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6510 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 6511 | SemaRef.Context.VoidPtrTy, |
| 6512 | diag::err_operator_delete_dependent_param_type, |
| 6513 | diag::err_operator_delete_param_type)) |
| 6514 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 6515 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 6516 | return false; |
| 6517 | } |
| 6518 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6519 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 6520 | /// of this overloaded operator is well-formed. If so, returns false; |
| 6521 | /// otherwise, emits appropriate diagnostics and returns true. |
| 6522 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6523 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6524 | "Expected an overloaded operator declaration"); |
| 6525 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6526 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 6527 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6528 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6529 | // The allocation and deallocation functions, operator new, |
| 6530 | // operator new[], operator delete and operator delete[], are |
| 6531 | // described completely in 3.7.3. The attributes and restrictions |
| 6532 | // found in the rest of this subclause do not apply to them unless |
| 6533 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 6534 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 6535 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 6536 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 6537 | if (Op == OO_New || Op == OO_Array_New) |
| 6538 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6539 | |
| 6540 | // C++ [over.oper]p6: |
| 6541 | // An operator function shall either be a non-static member |
| 6542 | // function or be a non-member function and have at least one |
| 6543 | // parameter whose type is a class, a reference to a class, an |
| 6544 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6545 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 6546 | if (MethodDecl->isStatic()) |
| 6547 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6548 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6549 | } else { |
| 6550 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6551 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 6552 | ParamEnd = FnDecl->param_end(); |
| 6553 | Param != ParamEnd; ++Param) { |
| 6554 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 6555 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 6556 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6557 | ClassOrEnumParam = true; |
| 6558 | break; |
| 6559 | } |
| 6560 | } |
| 6561 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6562 | if (!ClassOrEnumParam) |
| 6563 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 6564 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6565 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6566 | } |
| 6567 | |
| 6568 | // C++ [over.oper]p8: |
| 6569 | // An operator function cannot have default arguments (8.3.6), |
| 6570 | // except where explicitly stated below. |
| 6571 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6572 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6573 | // (C++ [over.call]p1). |
| 6574 | if (Op != OO_Call) { |
| 6575 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 6576 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6577 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6578 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 6579 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 6580 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6581 | } |
| 6582 | } |
| 6583 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 6584 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 6585 | { false, false, false } |
| 6586 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 6587 | , { Unary, Binary, MemberOnly } |
| 6588 | #include "clang/Basic/OperatorKinds.def" |
| 6589 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6590 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 6591 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 6592 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 6593 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6594 | |
| 6595 | // C++ [over.oper]p8: |
| 6596 | // [...] Operator functions cannot have more or fewer parameters |
| 6597 | // than the number required for the corresponding operator, as |
| 6598 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6599 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6600 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6601 | if (Op != OO_Call && |
| 6602 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 6603 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 6604 | (NumParams < 1) || (NumParams > 2))) { |
| 6605 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 6606 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 6607 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 6608 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 6609 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 6610 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 6611 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 6612 | assert(CanBeBinaryOperator && |
| 6613 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 6614 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 6615 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6616 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 6617 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6618 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6619 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 6620 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6621 | // Overloaded operators other than operator() cannot be variadic. |
| 6622 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6623 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 6624 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6625 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6626 | } |
| 6627 | |
| 6628 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6629 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 6630 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 6631 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6632 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6633 | } |
| 6634 | |
| 6635 | // C++ [over.inc]p1: |
| 6636 | // The user-defined function called operator++ implements the |
| 6637 | // prefix and postfix ++ operator. If this function is a member |
| 6638 | // function with no parameters, or a non-member function with one |
| 6639 | // parameter of class or enumeration type, it defines the prefix |
| 6640 | // increment operator ++ for objects of that type. If the function |
| 6641 | // is a member function with one parameter (which shall be of type |
| 6642 | // int) or a non-member function with two parameters (the second |
| 6643 | // of which shall be of type int), it defines the postfix |
| 6644 | // increment operator ++ for objects of that type. |
| 6645 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 6646 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 6647 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6648 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6649 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 6650 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 6651 | if (!ParamIsInt) |
| 6652 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6653 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6654 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6655 | } |
| 6656 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 6657 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 6658 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 6659 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 6660 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 6661 | /// of this literal operator function is well-formed. If so, returns |
| 6662 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 6663 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 6664 | DeclContext *DC = FnDecl->getDeclContext(); |
| 6665 | Decl::Kind Kind = DC->getDeclKind(); |
| 6666 | if (Kind != Decl::TranslationUnit && Kind != Decl::Namespace && |
| 6667 | Kind != Decl::LinkageSpec) { |
| 6668 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 6669 | << FnDecl->getDeclName(); |
| 6670 | return true; |
| 6671 | } |
| 6672 | |
| 6673 | bool Valid = false; |
| 6674 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 6675 | // template <char...> type operator "" name() is the only valid template |
| 6676 | // signature, and the only valid signature with no parameters. |
| 6677 | if (FnDecl->param_size() == 0) { |
| 6678 | if (FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate()) { |
| 6679 | // Must have only one template parameter |
| 6680 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 6681 | if (Params->size() == 1) { |
| 6682 | NonTypeTemplateParmDecl *PmDecl = |
| 6683 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 6684 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 6685 | // The template parameter must be a char parameter pack. |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 6686 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 6687 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 6688 | Valid = true; |
| 6689 | } |
| 6690 | } |
| 6691 | } else { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 6692 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 6693 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 6694 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 6695 | QualType T = (*Param)->getType(); |
| 6696 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 6697 | // unsigned long long int, long double, and any character type are allowed |
| 6698 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 6699 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 6700 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 6701 | Context.hasSameType(T, Context.CharTy) || |
| 6702 | Context.hasSameType(T, Context.WCharTy) || |
| 6703 | Context.hasSameType(T, Context.Char16Ty) || |
| 6704 | Context.hasSameType(T, Context.Char32Ty)) { |
| 6705 | if (++Param == FnDecl->param_end()) |
| 6706 | Valid = true; |
| 6707 | goto FinishedParams; |
| 6708 | } |
| 6709 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 6710 | // 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] | 6711 | const PointerType *PT = T->getAs<PointerType>(); |
| 6712 | if (!PT) |
| 6713 | goto FinishedParams; |
| 6714 | T = PT->getPointeeType(); |
| 6715 | if (!T.isConstQualified()) |
| 6716 | goto FinishedParams; |
| 6717 | T = T.getUnqualifiedType(); |
| 6718 | |
| 6719 | // Move on to the second parameter; |
| 6720 | ++Param; |
| 6721 | |
| 6722 | // If there is no second parameter, the first must be a const char * |
| 6723 | if (Param == FnDecl->param_end()) { |
| 6724 | if (Context.hasSameType(T, Context.CharTy)) |
| 6725 | Valid = true; |
| 6726 | goto FinishedParams; |
| 6727 | } |
| 6728 | |
| 6729 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 6730 | // are allowed as the first parameter to a two-parameter function |
| 6731 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 6732 | Context.hasSameType(T, Context.WCharTy) || |
| 6733 | Context.hasSameType(T, Context.Char16Ty) || |
| 6734 | Context.hasSameType(T, Context.Char32Ty))) |
| 6735 | goto FinishedParams; |
| 6736 | |
| 6737 | // The second and final parameter must be an std::size_t |
| 6738 | T = (*Param)->getType().getUnqualifiedType(); |
| 6739 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 6740 | ++Param == FnDecl->param_end()) |
| 6741 | Valid = true; |
| 6742 | } |
| 6743 | |
| 6744 | // FIXME: This diagnostic is absolutely terrible. |
| 6745 | FinishedParams: |
| 6746 | if (!Valid) { |
| 6747 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 6748 | << FnDecl->getDeclName(); |
| 6749 | return true; |
| 6750 | } |
| 6751 | |
| 6752 | return false; |
| 6753 | } |
| 6754 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6755 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 6756 | /// linkage specification, including the language and (if present) |
| 6757 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 6758 | /// the location of the language string literal, which is provided |
| 6759 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 6760 | /// the '{' brace. Otherwise, this linkage specification does not |
| 6761 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 6762 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 6763 | SourceLocation LangLoc, |
| 6764 | llvm::StringRef Lang, |
| 6765 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6766 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 6767 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6768 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 6769 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6770 | Language = LinkageSpecDecl::lang_cxx; |
| 6771 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6772 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6773 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6774 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6775 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6776 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6777 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6778 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 6779 | ExternLoc, LangLoc, Language); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6780 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6781 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6782 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6783 | } |
| 6784 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 6785 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6786 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 6787 | /// valid, it's the position of the closing '}' brace in a linkage |
| 6788 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6789 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 6790 | Decl *LinkageSpec, |
| 6791 | SourceLocation RBraceLoc) { |
| 6792 | if (LinkageSpec) { |
| 6793 | if (RBraceLoc.isValid()) { |
| 6794 | LinkageSpecDecl* LSDecl = cast<LinkageSpecDecl>(LinkageSpec); |
| 6795 | LSDecl->setRBraceLoc(RBraceLoc); |
| 6796 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6797 | PopDeclContext(); |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 6798 | } |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6799 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 6800 | } |
| 6801 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6802 | /// \brief Perform semantic analysis for the variable declaration that |
| 6803 | /// occurs within a C++ catch clause, returning the newly-created |
| 6804 | /// variable. |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6805 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 6806 | TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6807 | SourceLocation StartLoc, |
| 6808 | SourceLocation Loc, |
| 6809 | IdentifierInfo *Name) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6810 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6811 | QualType ExDeclType = TInfo->getType(); |
| 6812 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6813 | // Arrays and functions decay. |
| 6814 | if (ExDeclType->isArrayType()) |
| 6815 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 6816 | else if (ExDeclType->isFunctionType()) |
| 6817 | ExDeclType = Context.getPointerType(ExDeclType); |
| 6818 | |
| 6819 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 6820 | // The exception-declaration shall not denote a pointer or reference to an |
| 6821 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6822 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6823 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6824 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6825 | Invalid = true; |
| 6826 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6827 | |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6828 | // GCC allows catching pointers and references to incomplete types |
| 6829 | // as an extension; so do we, but we warn by default. |
| 6830 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6831 | QualType BaseType = ExDeclType; |
| 6832 | 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] | 6833 | unsigned DK = diag::err_catch_incomplete; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6834 | bool IncompleteCatchIsInvalid = true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6835 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6836 | BaseType = Ptr->getPointeeType(); |
| 6837 | Mode = 1; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6838 | DK = diag::ext_catch_incomplete_ptr; |
| 6839 | IncompleteCatchIsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6840 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6841 | // 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] | 6842 | BaseType = Ref->getPointeeType(); |
| 6843 | Mode = 2; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6844 | DK = diag::ext_catch_incomplete_ref; |
| 6845 | IncompleteCatchIsInvalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6846 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6847 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6848 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK) && |
| 6849 | IncompleteCatchIsInvalid) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6850 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6851 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6852 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6853 | RequireNonAbstractType(Loc, ExDeclType, |
| 6854 | diag::err_abstract_type_in_decl, |
| 6855 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 6856 | Invalid = true; |
| 6857 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6858 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 6859 | // of ObjC types, and no runtime supports catching ObjC types by value. |
| 6860 | if (!Invalid && getLangOptions().ObjC1) { |
| 6861 | QualType T = ExDeclType; |
| 6862 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 6863 | T = RT->getPointeeType(); |
| 6864 | |
| 6865 | if (T->isObjCObjectType()) { |
| 6866 | Diag(Loc, diag::err_objc_object_catch); |
| 6867 | Invalid = true; |
| 6868 | } else if (T->isObjCObjectPointerType()) { |
David Chisnall | 80558d2 | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 6869 | if (!getLangOptions().ObjCNonFragileABI) { |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6870 | Diag(Loc, diag::err_objc_pointer_cxx_catch_fragile); |
| 6871 | Invalid = true; |
| 6872 | } |
| 6873 | } |
| 6874 | } |
| 6875 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6876 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, StartLoc, Loc, Name, |
| 6877 | ExDeclType, TInfo, SC_None, SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 6878 | ExDecl->setExceptionVariable(true); |
| 6879 | |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6880 | if (!Invalid) { |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 6881 | if (const RecordType *recordType = ExDeclType->getAs<RecordType>()) { |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6882 | // C++ [except.handle]p16: |
| 6883 | // The object declared in an exception-declaration or, if the |
| 6884 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 6885 | // copy-initialized (8.5) from the exception object. [...] |
| 6886 | // The object is destroyed when the handler exits, after the destruction |
| 6887 | // of any automatic objects initialized within the handler. |
| 6888 | // |
| 6889 | // We just pretend to initialize the object with itself, then make sure |
| 6890 | // it can be destroyed later. |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 6891 | QualType initType = ExDeclType; |
| 6892 | |
| 6893 | InitializedEntity entity = |
| 6894 | InitializedEntity::InitializeVariable(ExDecl); |
| 6895 | InitializationKind initKind = |
| 6896 | InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 6897 | |
| 6898 | Expr *opaqueValue = |
| 6899 | new (Context) OpaqueValueExpr(Loc, initType, VK_LValue, OK_Ordinary); |
| 6900 | InitializationSequence sequence(*this, entity, initKind, &opaqueValue, 1); |
| 6901 | ExprResult result = sequence.Perform(*this, entity, initKind, |
| 6902 | MultiExprArg(&opaqueValue, 1)); |
| 6903 | if (result.isInvalid()) |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6904 | Invalid = true; |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 6905 | else { |
| 6906 | // If the constructor used was non-trivial, set this as the |
| 6907 | // "initializer". |
| 6908 | CXXConstructExpr *construct = cast<CXXConstructExpr>(result.take()); |
| 6909 | if (!construct->getConstructor()->isTrivial()) { |
| 6910 | Expr *init = MaybeCreateExprWithCleanups(construct); |
| 6911 | ExDecl->setInit(init); |
| 6912 | } |
| 6913 | |
| 6914 | // And make sure it's destructable. |
| 6915 | FinalizeVarWithDestructor(ExDecl, recordType); |
| 6916 | } |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6917 | } |
| 6918 | } |
| 6919 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6920 | if (Invalid) |
| 6921 | ExDecl->setInvalidDecl(); |
| 6922 | |
| 6923 | return ExDecl; |
| 6924 | } |
| 6925 | |
| 6926 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 6927 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6928 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6929 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
Douglas Gregor | a669c53 | 2010-12-16 17:48:04 +0000 | [diff] [blame] | 6930 | bool Invalid = D.isInvalidType(); |
| 6931 | |
| 6932 | // Check for unexpanded parameter packs. |
| 6933 | if (TInfo && DiagnoseUnexpandedParameterPack(D.getIdentifierLoc(), TInfo, |
| 6934 | UPPC_ExceptionType)) { |
| 6935 | TInfo = Context.getTrivialTypeSourceInfo(Context.IntTy, |
| 6936 | D.getIdentifierLoc()); |
| 6937 | Invalid = true; |
| 6938 | } |
| 6939 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6940 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6941 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 6942 | LookupOrdinaryName, |
| 6943 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6944 | // The scope should be freshly made just for us. There is just no way |
| 6945 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6946 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6947 | if (PrevDecl->isTemplateParameter()) { |
| 6948 | // Maybe we will complain about the shadowed template parameter. |
| 6949 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6950 | } |
| 6951 | } |
| 6952 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6953 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6954 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 6955 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6956 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6957 | } |
| 6958 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6959 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6960 | D.getSourceRange().getBegin(), |
| 6961 | D.getIdentifierLoc(), |
| 6962 | D.getIdentifier()); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6963 | if (Invalid) |
| 6964 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6965 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6966 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6967 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6968 | PushOnScopeChains(ExDecl, S); |
| 6969 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6970 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6971 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6972 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6973 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6974 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6975 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 6976 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation StaticAssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6977 | Expr *AssertExpr, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 6978 | Expr *AssertMessageExpr_, |
| 6979 | SourceLocation RParenLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6980 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr_); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6981 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6982 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 6983 | llvm::APSInt Value(32); |
| 6984 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 6985 | Diag(StaticAssertLoc, |
| 6986 | diag::err_static_assert_expression_is_not_constant) << |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6987 | AssertExpr->getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6988 | return 0; |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6989 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6990 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6991 | if (Value == 0) { |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 6992 | Diag(StaticAssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 6993 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6994 | } |
| 6995 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6996 | |
Douglas Gregor | 399ad97 | 2010-12-15 23:55:21 +0000 | [diff] [blame] | 6997 | if (DiagnoseUnexpandedParameterPack(AssertExpr, UPPC_StaticAssertExpression)) |
| 6998 | return 0; |
| 6999 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 7000 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, StaticAssertLoc, |
| 7001 | AssertExpr, AssertMessage, RParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7002 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 7003 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7004 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 7005 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 7006 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 7007 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 7008 | /// |
| 7009 | /// \returns A friend declaration that. |
| 7010 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation FriendLoc, |
| 7011 | TypeSourceInfo *TSInfo) { |
| 7012 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 7013 | |
| 7014 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 7015 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 7016 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 7017 | if (!getLangOptions().CPlusPlus0x) { |
| 7018 | // C++03 [class.friend]p2: |
| 7019 | // An elaborated-type-specifier shall be used in a friend declaration |
| 7020 | // for a class.* |
| 7021 | // |
| 7022 | // * The class-key of the elaborated-type-specifier is required. |
| 7023 | if (!ActiveTemplateInstantiations.empty()) { |
| 7024 | // Do not complain about the form of friend template types during |
| 7025 | // template instantiation; we will already have complained when the |
| 7026 | // template was declared. |
| 7027 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 7028 | // If we evaluated the type to a record type, suggest putting |
| 7029 | // a tag in front. |
| 7030 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 7031 | RecordDecl *RD = RT->getDecl(); |
| 7032 | |
| 7033 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 7034 | |
| 7035 | Diag(TypeRange.getBegin(), diag::ext_unelaborated_friend_type) |
| 7036 | << (unsigned) RD->getTagKind() |
| 7037 | << T |
| 7038 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 7039 | InsertionText); |
| 7040 | } else { |
| 7041 | Diag(FriendLoc, diag::ext_nonclass_type_friend) |
| 7042 | << T |
| 7043 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
| 7044 | } |
| 7045 | } else if (T->getAs<EnumType>()) { |
| 7046 | Diag(FriendLoc, diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 7047 | << T |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 7048 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 7049 | } |
| 7050 | } |
| 7051 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 7052 | // C++0x [class.friend]p3: |
| 7053 | // If the type specifier in a friend declaration designates a (possibly |
| 7054 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 7055 | // the friend declaration is ignored. |
| 7056 | |
| 7057 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 7058 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 7059 | |
| 7060 | return FriendDecl::Create(Context, CurContext, FriendLoc, TSInfo, FriendLoc); |
| 7061 | } |
| 7062 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 7063 | /// Handle a friend tag declaration where the scope specifier was |
| 7064 | /// templated. |
| 7065 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 7066 | unsigned TagSpec, SourceLocation TagLoc, |
| 7067 | CXXScopeSpec &SS, |
| 7068 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 7069 | AttributeList *Attr, |
| 7070 | MultiTemplateParamsArg TempParamLists) { |
| 7071 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7072 | |
| 7073 | bool isExplicitSpecialization = false; |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 7074 | bool Invalid = false; |
| 7075 | |
| 7076 | if (TemplateParameterList *TemplateParams |
| 7077 | = MatchTemplateParametersToScopeSpecifier(TagLoc, SS, |
| 7078 | TempParamLists.get(), |
| 7079 | TempParamLists.size(), |
| 7080 | /*friend*/ true, |
| 7081 | isExplicitSpecialization, |
| 7082 | Invalid)) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 7083 | if (TemplateParams->size() > 0) { |
| 7084 | // This is a declaration of a class template. |
| 7085 | if (Invalid) |
| 7086 | return 0; |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 7087 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 7088 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 7089 | SS, Name, NameLoc, Attr, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 7090 | TemplateParams, AS_public, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7091 | TempParamLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 7092 | (TemplateParameterList**) TempParamLists.release()).take(); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 7093 | } else { |
| 7094 | // The "template<>" header is extraneous. |
| 7095 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 7096 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 7097 | isExplicitSpecialization = true; |
| 7098 | } |
| 7099 | } |
| 7100 | |
| 7101 | if (Invalid) return 0; |
| 7102 | |
| 7103 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 7104 | |
| 7105 | bool isAllExplicitSpecializations = true; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 7106 | for (unsigned I = TempParamLists.size(); I-- > 0; ) { |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 7107 | if (TempParamLists.get()[I]->size()) { |
| 7108 | isAllExplicitSpecializations = false; |
| 7109 | break; |
| 7110 | } |
| 7111 | } |
| 7112 | |
| 7113 | // FIXME: don't ignore attributes. |
| 7114 | |
| 7115 | // If it's explicit specializations all the way down, just forget |
| 7116 | // about the template header and build an appropriate non-templated |
| 7117 | // friend. TODO: for source fidelity, remember the headers. |
| 7118 | if (isAllExplicitSpecializations) { |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7119 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 7120 | ElaboratedTypeKeyword Keyword |
| 7121 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7122 | QualType T = CheckTypenameType(Keyword, TagLoc, QualifierLoc, |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 7123 | *Name, NameLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 7124 | if (T.isNull()) |
| 7125 | return 0; |
| 7126 | |
| 7127 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 7128 | if (isa<DependentNameType>(T)) { |
| 7129 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 7130 | TL.setKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7131 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 7132 | TL.setNameLoc(NameLoc); |
| 7133 | } else { |
| 7134 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
| 7135 | TL.setKeywordLoc(TagLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 7136 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 7137 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(NameLoc); |
| 7138 | } |
| 7139 | |
| 7140 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 7141 | TSI, FriendLoc); |
| 7142 | Friend->setAccess(AS_public); |
| 7143 | CurContext->addDecl(Friend); |
| 7144 | return Friend; |
| 7145 | } |
| 7146 | |
| 7147 | // Handle the case of a templated-scope friend class. e.g. |
| 7148 | // template <class T> class A<T>::B; |
| 7149 | // FIXME: we don't support these right now. |
| 7150 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 7151 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 7152 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 7153 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 7154 | TL.setKeywordLoc(TagLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 7155 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 7156 | TL.setNameLoc(NameLoc); |
| 7157 | |
| 7158 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 7159 | TSI, FriendLoc); |
| 7160 | Friend->setAccess(AS_public); |
| 7161 | Friend->setUnsupportedFriend(true); |
| 7162 | CurContext->addDecl(Friend); |
| 7163 | return Friend; |
| 7164 | } |
| 7165 | |
| 7166 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 7167 | /// Handle a friend type declaration. This works in tandem with |
| 7168 | /// ActOnTag. |
| 7169 | /// |
| 7170 | /// Notes on friend class templates: |
| 7171 | /// |
| 7172 | /// We generally treat friend class declarations as if they were |
| 7173 | /// declaring a class. So, for example, the elaborated type specifier |
| 7174 | /// in a friend declaration is required to obey the restrictions of a |
| 7175 | /// class-head (i.e. no typedefs in the scope chain), template |
| 7176 | /// parameters are required to match up with simple template-ids, &c. |
| 7177 | /// However, unlike when declaring a template specialization, it's |
| 7178 | /// okay to refer to a template specialization without an empty |
| 7179 | /// template parameter declaration, e.g. |
| 7180 | /// friend class A<T>::B<unsigned>; |
| 7181 | /// We permit this as a special case; if there are any template |
| 7182 | /// parameters present at all, require proper matching, i.e. |
| 7183 | /// template <> template <class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7184 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 7185 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7186 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7187 | |
| 7188 | assert(DS.isFriendSpecified()); |
| 7189 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 7190 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 7191 | // Try to convert the decl specifier to a type. This works for |
| 7192 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 7193 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 7194 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7195 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 7196 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 7197 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7198 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7199 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 7200 | if (DiagnoseUnexpandedParameterPack(Loc, TSI, UPPC_FriendDeclaration)) |
| 7201 | return 0; |
| 7202 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 7203 | // This is definitely an error in C++98. It's probably meant to |
| 7204 | // be forbidden in C++0x, too, but the specification is just |
| 7205 | // poorly written. |
| 7206 | // |
| 7207 | // The problem is with declarations like the following: |
| 7208 | // template <T> friend A<T>::foo; |
| 7209 | // where deciding whether a class C is a friend or not now hinges |
| 7210 | // on whether there exists an instantiation of A that causes |
| 7211 | // 'foo' to equal C. There are restrictions on class-heads |
| 7212 | // (which we declare (by fiat) elaborated friend declarations to |
| 7213 | // be) that makes this tractable. |
| 7214 | // |
| 7215 | // FIXME: handle "template <> friend class A<T>;", which |
| 7216 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 7217 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 7218 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 7219 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7220 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 7221 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 7222 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7223 | // C++98 [class.friend]p1: A friend of a class is a function |
| 7224 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 7225 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 7226 | // deadline. It's also a very silly restriction that seriously |
| 7227 | // affects inner classes and which nobody else seems to implement; |
| 7228 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 7229 | // |
| 7230 | // But note that we could warn about it: it's always useless to |
| 7231 | // friend one of your own members (it's not, however, worthless to |
| 7232 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7233 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 7234 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 7235 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 7236 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 7237 | NumTempParamLists, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 7238 | TempParams.release(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 7239 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 7240 | DS.getFriendSpecLoc()); |
| 7241 | else |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 7242 | D = CheckFriendTypeDecl(DS.getFriendSpecLoc(), TSI); |
| 7243 | |
| 7244 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7245 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 7246 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 7247 | D->setAccess(AS_public); |
| 7248 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7249 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7250 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7251 | } |
| 7252 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7253 | Decl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, bool IsDefinition, |
| 7254 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7255 | const DeclSpec &DS = D.getDeclSpec(); |
| 7256 | |
| 7257 | assert(DS.isFriendSpecified()); |
| 7258 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 7259 | |
| 7260 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7261 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 7262 | QualType T = TInfo->getType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7263 | |
| 7264 | // C++ [class.friend]p1 |
| 7265 | // A friend of a class is a function or class.... |
| 7266 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7267 | // It *doesn't* see through dependent types, which is correct |
| 7268 | // according to [temp.arg.type]p3: |
| 7269 | // If a declaration acquires a function type through a |
| 7270 | // type dependent on a template-parameter and this causes |
| 7271 | // a declaration that does not use the syntactic form of a |
| 7272 | // function declarator to have a function type, the program |
| 7273 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7274 | if (!T->isFunctionType()) { |
| 7275 | Diag(Loc, diag::err_unexpected_friend); |
| 7276 | |
| 7277 | // It might be worthwhile to try to recover by creating an |
| 7278 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7279 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7280 | } |
| 7281 | |
| 7282 | // C++ [namespace.memdef]p3 |
| 7283 | // - If a friend declaration in a non-local class first declares a |
| 7284 | // class or function, the friend class or function is a member |
| 7285 | // of the innermost enclosing namespace. |
| 7286 | // - The name of the friend is not found by simple name lookup |
| 7287 | // until a matching declaration is provided in that namespace |
| 7288 | // scope (either before or after the class declaration granting |
| 7289 | // friendship). |
| 7290 | // - If a friend function is called, its name may be found by the |
| 7291 | // name lookup that considers functions from namespaces and |
| 7292 | // classes associated with the types of the function arguments. |
| 7293 | // - When looking for a prior declaration of a class or a function |
| 7294 | // declared as a friend, scopes outside the innermost enclosing |
| 7295 | // namespace scope are not considered. |
| 7296 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7297 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7298 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 7299 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7300 | assert(Name); |
| 7301 | |
Douglas Gregor | 6ccab97 | 2010-12-16 01:14:37 +0000 | [diff] [blame] | 7302 | // Check for unexpanded parameter packs. |
| 7303 | if (DiagnoseUnexpandedParameterPack(Loc, TInfo, UPPC_FriendDeclaration) || |
| 7304 | DiagnoseUnexpandedParameterPack(NameInfo, UPPC_FriendDeclaration) || |
| 7305 | DiagnoseUnexpandedParameterPack(SS, UPPC_FriendDeclaration)) |
| 7306 | return 0; |
| 7307 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7308 | // The context we found the declaration in, or in which we should |
| 7309 | // create the declaration. |
| 7310 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 7311 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 7312 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7313 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7314 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7315 | // FIXME: there are different rules in local classes |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7316 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7317 | // There are four cases here. |
| 7318 | // - There's no scope specifier, in which case we just go to the |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7319 | // appropriate scope and look for a function or function template |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7320 | // there as appropriate. |
| 7321 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 7322 | if (SS.isInvalid() || !SS.isSet()) { |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7323 | // C++0x [namespace.memdef]p3: |
| 7324 | // If the name in a friend declaration is neither qualified nor |
| 7325 | // a template-id and the declaration is a function or an |
| 7326 | // elaborated-type-specifier, the lookup to determine whether |
| 7327 | // the entity has been previously declared shall not consider |
| 7328 | // any scopes outside the innermost enclosing namespace. |
| 7329 | // C++0x [class.friend]p11: |
| 7330 | // If a friend declaration appears in a local class and the name |
| 7331 | // specified is an unqualified name, a prior declaration is |
| 7332 | // looked up without considering scopes that are outside the |
| 7333 | // innermost enclosing non-class scope. For a friend function |
| 7334 | // declaration, if there is no prior declaration, the program is |
| 7335 | // ill-formed. |
| 7336 | bool isLocal = cast<CXXRecordDecl>(CurContext)->isLocalClass(); |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 7337 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7338 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7339 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7340 | DC = CurContext; |
| 7341 | while (true) { |
| 7342 | // Skip class contexts. If someone can cite chapter and verse |
| 7343 | // for this behavior, that would be nice --- it's what GCC and |
| 7344 | // EDG do, and it seems like a reasonable intent, but the spec |
| 7345 | // really only says that checks for unqualified existing |
| 7346 | // declarations should stop at the nearest enclosing namespace, |
| 7347 | // not that they should only consider the nearest enclosing |
| 7348 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 7349 | while (DC->isRecord()) |
| 7350 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7351 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7352 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7353 | |
| 7354 | // TODO: decide what we think about using declarations. |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7355 | if (isLocal || !Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7356 | break; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7357 | |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 7358 | if (isTemplateId) { |
| 7359 | if (isa<TranslationUnitDecl>(DC)) break; |
| 7360 | } else { |
| 7361 | if (DC->isFileContext()) break; |
| 7362 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7363 | DC = DC->getParent(); |
| 7364 | } |
| 7365 | |
| 7366 | // C++ [class.friend]p1: A friend of a class is a function or |
| 7367 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 7368 | // C++0x changes this for both friend types and functions. |
| 7369 | // Most C++ 98 compilers do seem to give an error here, so |
| 7370 | // we do, too. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 7371 | if (!Previous.empty() && DC->Equals(CurContext) |
| 7372 | && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7373 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7374 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 7375 | DCScope = getScopeForDeclContext(S, DC); |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7376 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7377 | // - There's a non-dependent scope specifier, in which case we |
| 7378 | // compute it and do a previous lookup there for a function |
| 7379 | // or function template. |
| 7380 | } else if (!SS.getScopeRep()->isDependent()) { |
| 7381 | DC = computeDeclContext(SS); |
| 7382 | if (!DC) return 0; |
| 7383 | |
| 7384 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 7385 | |
| 7386 | LookupQualifiedName(Previous, DC); |
| 7387 | |
| 7388 | // Ignore things found implicitly in the wrong scope. |
| 7389 | // TODO: better diagnostics for this case. Suggesting the right |
| 7390 | // qualified scope would be nice... |
| 7391 | LookupResult::Filter F = Previous.makeFilter(); |
| 7392 | while (F.hasNext()) { |
| 7393 | NamedDecl *D = F.next(); |
| 7394 | if (!DC->InEnclosingNamespaceSetOf( |
| 7395 | D->getDeclContext()->getRedeclContext())) |
| 7396 | F.erase(); |
| 7397 | } |
| 7398 | F.done(); |
| 7399 | |
| 7400 | if (Previous.empty()) { |
| 7401 | D.setInvalidType(); |
| 7402 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 7403 | return 0; |
| 7404 | } |
| 7405 | |
| 7406 | // C++ [class.friend]p1: A friend of a class is a function or |
| 7407 | // class that is not a member of the class . . . |
| 7408 | if (DC->Equals(CurContext)) |
| 7409 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 7410 | |
| 7411 | // - There's a scope specifier that does not match any template |
| 7412 | // parameter lists, in which case we use some arbitrary context, |
| 7413 | // create a method or method template, and wait for instantiation. |
| 7414 | // - There's a scope specifier that does match some template |
| 7415 | // parameter lists, which we don't handle right now. |
| 7416 | } else { |
| 7417 | DC = CurContext; |
| 7418 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7419 | } |
| 7420 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 7421 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7422 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7423 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 7424 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 7425 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7426 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 7427 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 7428 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7429 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7430 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7431 | } |
| 7432 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 7433 | bool Redeclaration = false; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 7434 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, T, TInfo, Previous, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 7435 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 7436 | IsDefinition, |
| 7437 | Redeclaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7438 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 7439 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 7440 | assert(ND->getDeclContext() == DC); |
| 7441 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 7442 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 7443 | // Add the function declaration to the appropriate lookup tables, |
| 7444 | // adjusting the redeclarations list as necessary. We don't |
| 7445 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7446 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 7447 | // Also update the scope-based lookup if the target context's |
| 7448 | // lookup context is in lexical scope. |
| 7449 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 7450 | DC = DC->getRedeclContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 7451 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 7452 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 7453 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 7454 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7455 | |
| 7456 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 7457 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7458 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 7459 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 7460 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 7461 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7462 | if (ND->isInvalidDecl()) |
| 7463 | FrD->setInvalidDecl(); |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 7464 | else { |
| 7465 | FunctionDecl *FD; |
| 7466 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 7467 | FD = FTD->getTemplatedDecl(); |
| 7468 | else |
| 7469 | FD = cast<FunctionDecl>(ND); |
| 7470 | |
| 7471 | // Mark templated-scope function declarations as unsupported. |
| 7472 | if (FD->getNumTemplateParameterLists()) |
| 7473 | FrD->setUnsupportedFriend(true); |
| 7474 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 7475 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7476 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 7477 | } |
| 7478 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7479 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 7480 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7481 | |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 7482 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 7483 | if (!Fn) { |
| 7484 | Diag(DelLoc, diag::err_deleted_non_function); |
| 7485 | return; |
| 7486 | } |
| 7487 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 7488 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 7489 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 7490 | // If the declaration wasn't the first, we delete the function anyway for |
| 7491 | // recovery. |
| 7492 | } |
| 7493 | Fn->setDeleted(); |
| 7494 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 7495 | |
| 7496 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 7497 | for (Stmt::child_range CI = S->children(); CI; ++CI) { |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 7498 | Stmt *SubStmt = *CI; |
| 7499 | if (!SubStmt) |
| 7500 | continue; |
| 7501 | if (isa<ReturnStmt>(SubStmt)) |
| 7502 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 7503 | diag::err_return_in_constructor_handler); |
| 7504 | if (!isa<Expr>(SubStmt)) |
| 7505 | SearchForReturnInStmt(Self, SubStmt); |
| 7506 | } |
| 7507 | } |
| 7508 | |
| 7509 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 7510 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 7511 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 7512 | SearchForReturnInStmt(*this, Handler); |
| 7513 | } |
| 7514 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7515 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7516 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7517 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7518 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 7519 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7520 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 7521 | if (Context.hasSameType(NewTy, OldTy) || |
| 7522 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7523 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7524 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7525 | // Check if the return types are covariant |
| 7526 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7527 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7528 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 7529 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 7530 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7531 | NewClassTy = NewPT->getPointeeType(); |
| 7532 | OldClassTy = OldPT->getPointeeType(); |
| 7533 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 7534 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 7535 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 7536 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 7537 | NewClassTy = NewRT->getPointeeType(); |
| 7538 | OldClassTy = OldRT->getPointeeType(); |
| 7539 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7540 | } |
| 7541 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7542 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7543 | // The return types aren't either both pointers or references to a class type. |
| 7544 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7545 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7546 | diag::err_different_return_type_for_overriding_virtual_function) |
| 7547 | << New->getDeclName() << NewTy << OldTy; |
| 7548 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7549 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7550 | return true; |
| 7551 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7552 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 7553 | // C++ [class.virtual]p6: |
| 7554 | // If the return type of D::f differs from the return type of B::f, the |
| 7555 | // class type in the return type of D::f shall be complete at the point of |
| 7556 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 7557 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 7558 | if (!RT->isBeingDefined() && |
| 7559 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 7560 | PDiag(diag::err_covariant_return_incomplete) |
| 7561 | << New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 7562 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 7563 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 7564 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 7565 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7566 | // Check if the new class derives from the old class. |
| 7567 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 7568 | Diag(New->getLocation(), |
| 7569 | diag::err_covariant_return_not_derived) |
| 7570 | << New->getDeclName() << NewTy << OldTy; |
| 7571 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 7572 | return true; |
| 7573 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7574 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7575 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 7576 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 7577 | diag::err_covariant_return_inaccessible_base, |
| 7578 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 7579 | // FIXME: Should this point to the return type? |
| 7580 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7581 | // FIXME: this note won't trigger for delayed access control |
| 7582 | // diagnostics, and it's impossible to get an undelayed error |
| 7583 | // here from access control during the original parse because |
| 7584 | // the ParsingDeclSpec/ParsingDeclarator are still in scope. |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7585 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 7586 | return true; |
| 7587 | } |
| 7588 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7589 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7590 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 7591 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7592 | Diag(New->getLocation(), |
| 7593 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7594 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7595 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 7596 | return true; |
| 7597 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7598 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7599 | |
| 7600 | // The new class type must have the same or less qualifiers as the old type. |
| 7601 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 7602 | Diag(New->getLocation(), |
| 7603 | diag::err_covariant_return_type_class_type_more_qualified) |
| 7604 | << New->getDeclName() << NewTy << OldTy; |
| 7605 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 7606 | return true; |
| 7607 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7608 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 7609 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 7610 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7611 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7612 | /// \brief Mark the given method pure. |
| 7613 | /// |
| 7614 | /// \param Method the method to be marked pure. |
| 7615 | /// |
| 7616 | /// \param InitRange the source range that covers the "0" initializer. |
| 7617 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 7618 | SourceLocation EndLoc = InitRange.getEnd(); |
| 7619 | if (EndLoc.isValid()) |
| 7620 | Method->setRangeEnd(EndLoc); |
| 7621 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7622 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 7623 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7624 | return false; |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 7625 | } |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 7626 | |
| 7627 | if (!Method->isInvalidDecl()) |
| 7628 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 7629 | << Method->getDeclName() << InitRange; |
| 7630 | return true; |
| 7631 | } |
| 7632 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 7633 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 7634 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 7635 | /// is a fresh scope pushed for just this purpose. |
| 7636 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7637 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 7638 | /// static data member of class X, names should be looked up in the scope of |
| 7639 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7640 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7641 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 7642 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7643 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 7644 | // We should only get called for declarations with scope specifiers, like: |
| 7645 | // int foo::bar; |
| 7646 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 7647 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7648 | } |
| 7649 | |
| 7650 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7651 | /// initializer for the out-of-line declaration 'D'. |
| 7652 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7653 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 7654 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7655 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 7656 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 7657 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 7658 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 7659 | |
| 7660 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 7661 | /// C++ if/switch/while/for statement. |
| 7662 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7663 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 7664 | // C++ 6.4p2: |
| 7665 | // The declarator shall not specify a function or an array. |
| 7666 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 7667 | // new class or enumeration. |
| 7668 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 7669 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 7670 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 7671 | TagDecl *OwnedTag = 0; |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7672 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag); |
| 7673 | QualType Ty = TInfo->getType(); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 7674 | |
| 7675 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 7676 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 7677 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 7678 | Diag(D.getIdentifierLoc(), diag::err_invalid_use_of_function_type) |
| 7679 | << D.getSourceRange(); |
| 7680 | return DeclResult(); |
| 7681 | } else if (OwnedTag && OwnedTag->isDefinition()) { |
| 7682 | // The type-specifier-seq shall not declare a new class or enumeration. |
| 7683 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition); |
| 7684 | } |
| 7685 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 7686 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 7687 | if (!Dcl) |
| 7688 | return DeclResult(); |
| 7689 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 7690 | return Dcl; |
| 7691 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 7692 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7693 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 7694 | bool DefinitionRequired) { |
| 7695 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 7696 | // not have a vtable. |
| 7697 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 7698 | CurContext->isDependentContext() || |
| 7699 | ExprEvalContexts.back().Context == Unevaluated) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 7700 | return; |
| 7701 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7702 | // Try to insert this class into the map. |
| 7703 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 7704 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 7705 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 7706 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 7707 | // If we already had an entry, check to see if we are promoting this vtable |
| 7708 | // to required a definition. If so, we need to reappend to the VTableUses |
| 7709 | // list, since we may have already processed the first entry. |
| 7710 | if (DefinitionRequired && !Pos.first->second) { |
| 7711 | Pos.first->second = true; |
| 7712 | } else { |
| 7713 | // Otherwise, we can early exit. |
| 7714 | return; |
| 7715 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7716 | } |
| 7717 | |
| 7718 | // Local classes need to have their virtual members marked |
| 7719 | // immediately. For all other classes, we mark their virtual members |
| 7720 | // at the end of the translation unit. |
| 7721 | if (Class->isLocalClass()) |
| 7722 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 7723 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7724 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 7725 | } |
| 7726 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7727 | bool Sema::DefineUsedVTables() { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7728 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7729 | return false; |
Chandler Carruth | aee543a | 2010-12-12 21:36:11 +0000 | [diff] [blame] | 7730 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7731 | // Note: The VTableUses vector could grow as a result of marking |
| 7732 | // the members of a class as "used", so we check the size each |
| 7733 | // time through the loop and prefer indices (with are stable) to |
| 7734 | // iterators (which are not). |
| 7735 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 7736 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7737 | if (!Class) |
| 7738 | continue; |
| 7739 | |
| 7740 | SourceLocation Loc = VTableUses[I].second; |
| 7741 | |
| 7742 | // If this class has a key function, but that key function is |
| 7743 | // defined in another translation unit, we don't need to emit the |
| 7744 | // vtable even though we're using it. |
| 7745 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 7746 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7747 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 7748 | case TSK_Undeclared: |
| 7749 | case TSK_ExplicitSpecialization: |
| 7750 | case TSK_ExplicitInstantiationDeclaration: |
| 7751 | // The key function is in another translation unit. |
| 7752 | continue; |
| 7753 | |
| 7754 | case TSK_ExplicitInstantiationDefinition: |
| 7755 | case TSK_ImplicitInstantiation: |
| 7756 | // We will be instantiating the key function. |
| 7757 | break; |
| 7758 | } |
| 7759 | } else if (!KeyFunction) { |
| 7760 | // If we have a class with no key function that is the subject |
| 7761 | // of an explicit instantiation declaration, suppress the |
| 7762 | // vtable; it will live with the explicit instantiation |
| 7763 | // definition. |
| 7764 | bool IsExplicitInstantiationDeclaration |
| 7765 | = Class->getTemplateSpecializationKind() |
| 7766 | == TSK_ExplicitInstantiationDeclaration; |
| 7767 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 7768 | REnd = Class->redecls_end(); |
| 7769 | R != REnd; ++R) { |
| 7770 | TemplateSpecializationKind TSK |
| 7771 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 7772 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 7773 | IsExplicitInstantiationDeclaration = true; |
| 7774 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 7775 | IsExplicitInstantiationDeclaration = false; |
| 7776 | break; |
| 7777 | } |
| 7778 | } |
| 7779 | |
| 7780 | if (IsExplicitInstantiationDeclaration) |
| 7781 | continue; |
| 7782 | } |
| 7783 | |
| 7784 | // Mark all of the virtual members of this class as referenced, so |
| 7785 | // that we can build a vtable. Then, tell the AST consumer that a |
| 7786 | // vtable for this class is required. |
| 7787 | MarkVirtualMembersReferenced(Loc, Class); |
| 7788 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 7789 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 7790 | |
| 7791 | // Optionally warn if we're emitting a weak vtable. |
| 7792 | if (Class->getLinkage() == ExternalLinkage && |
| 7793 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 7794 | if (!KeyFunction || (KeyFunction->hasBody() && KeyFunction->isInlined())) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7795 | Diag(Class->getLocation(), diag::warn_weak_vtable) << Class; |
| 7796 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 7797 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7798 | VTableUses.clear(); |
| 7799 | |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7800 | return true; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 7801 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7802 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 7803 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 7804 | const CXXRecordDecl *RD) { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7805 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 7806 | e = RD->method_end(); i != e; ++i) { |
| 7807 | CXXMethodDecl *MD = *i; |
| 7808 | |
| 7809 | // C++ [basic.def.odr]p2: |
| 7810 | // [...] A virtual member function is used if it is not pure. [...] |
| 7811 | if (MD->isVirtual() && !MD->isPure()) |
| 7812 | MarkDeclarationReferenced(Loc, MD); |
| 7813 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 7814 | |
| 7815 | // Only classes that have virtual bases need a VTT. |
| 7816 | if (RD->getNumVBases() == 0) |
| 7817 | return; |
| 7818 | |
| 7819 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 7820 | e = RD->bases_end(); i != e; ++i) { |
| 7821 | const CXXRecordDecl *Base = |
| 7822 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 7823 | if (Base->getNumVBases() == 0) |
| 7824 | continue; |
| 7825 | MarkVirtualMembersReferenced(Loc, Base); |
| 7826 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7827 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7828 | |
| 7829 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 7830 | /// Objective-C implementation whose ivars need be initialized. |
| 7831 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 7832 | if (!getLangOptions().CPlusPlus) |
| 7833 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 7834 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7835 | llvm::SmallVector<ObjCIvarDecl*, 8> ivars; |
| 7836 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 7837 | if (ivars.empty()) |
| 7838 | return; |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 7839 | llvm::SmallVector<CXXCtorInitializer*, 32> AllToInit; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7840 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 7841 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 7842 | if (Field->isInvalidDecl()) |
| 7843 | continue; |
| 7844 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 7845 | CXXCtorInitializer *Member; |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7846 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 7847 | InitializationKind InitKind = |
| 7848 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 7849 | |
| 7850 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7851 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7852 | InitSeq.Perform(*this, InitEntity, InitKind, MultiExprArg()); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 7853 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7854 | // Note, MemberInit could actually come back empty if no initialization |
| 7855 | // is required (e.g., because it would call a trivial default constructor) |
| 7856 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 7857 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7858 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7859 | Member = |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 7860 | new (Context) CXXCtorInitializer(Context, Field, SourceLocation(), |
| 7861 | SourceLocation(), |
| 7862 | MemberInit.takeAs<Expr>(), |
| 7863 | SourceLocation()); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7864 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 7865 | |
| 7866 | // Be sure that the destructor is accessible and is marked as referenced. |
| 7867 | if (const RecordType *RecordTy |
| 7868 | = Context.getBaseElementType(Field->getType()) |
| 7869 | ->getAs<RecordType>()) { |
| 7870 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 7871 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 7872 | MarkDeclarationReferenced(Field->getLocation(), Destructor); |
| 7873 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 7874 | PDiag(diag::err_access_dtor_ivar) |
| 7875 | << Context.getBaseElementType(Field->getType())); |
| 7876 | } |
| 7877 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7878 | } |
| 7879 | ObjCImplementation->setIvarInitializers(Context, |
| 7880 | AllToInit.data(), AllToInit.size()); |
| 7881 | } |
| 7882 | } |