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" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
| 25 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DeclSpec.h" |
| 29 | #include "clang/Sema/ParsedTemplate.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 30 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 34 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 35 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace clang; |
| 38 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // CheckDefaultArgumentVisitor |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 45 | /// the default argument of a parameter to determine whether it |
| 46 | /// contains any ill-formed subexpressions. For example, this will |
| 47 | /// diagnose the use of local variables or parameters within the |
| 48 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 49 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 50 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 51 | Expr *DefaultArg; |
| 52 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 54 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 56 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 58 | bool VisitExpr(Expr *Node); |
| 59 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 60 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 61 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 63 | /// VisitExpr - Visit all of the children of this expression. |
| 64 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 65 | bool IsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | for (Stmt::child_iterator I = Node->child_begin(), |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 67 | E = Node->child_end(); I != E; ++I) |
| 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, |
John McCall | ca0408f | 2010-08-23 06:44:23 +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); |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 140 | Arg = MaybeCreateCXXExprWithTemporaries(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 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 180 | // Check that the default argument is well-formed |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 181 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 182 | if (DefaultArgChecker.Visit(DefaultArg)) { |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 183 | Param->setInvalidDecl(); |
| 184 | return; |
| 185 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 187 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 190 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 191 | /// argument for a function parameter, but we can't parse it yet |
| 192 | /// because we're inside a class definition. Note that this default |
| 193 | /// argument will be parsed later. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 194 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 195 | SourceLocation EqualLoc, |
| 196 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 197 | if (!param) |
| 198 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 200 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 201 | if (Param) |
| 202 | Param->setUnparsedDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 204 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 207 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 208 | /// the default argument for the parameter param failed. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 209 | void Sema::ActOnParamDefaultArgumentError(Decl *param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 210 | if (!param) |
| 211 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 213 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 215 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 217 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 220 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 221 | /// arguments in the declarator, which is not a function declaration |
| 222 | /// or definition and therefore is not permitted to have default |
| 223 | /// arguments. This routine should be invoked for every declarator |
| 224 | /// that is not a function declaration or definition. |
| 225 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 226 | // C++ [dcl.fct.default]p3 |
| 227 | // A default argument expression shall be specified only in the |
| 228 | // parameter-declaration-clause of a function declaration or in a |
| 229 | // template-parameter (14.1). It shall not be specified for a |
| 230 | // parameter pack. If it is specified in a |
| 231 | // parameter-declaration-clause, it shall not occur within a |
| 232 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 233 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 234 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 235 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 236 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 237 | ParmVarDecl *Param = |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 238 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 239 | if (Param->hasUnparsedDefaultArg()) { |
| 240 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 241 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 242 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 243 | delete Toks; |
| 244 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 245 | } else if (Param->getDefaultArg()) { |
| 246 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 247 | << Param->getDefaultArg()->getSourceRange(); |
| 248 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 255 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 256 | // function, once we already know that they have the same |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 257 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 258 | // error, false otherwise. |
| 259 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 260 | bool Invalid = false; |
| 261 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 262 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 263 | // For non-template functions, default arguments can be added in |
| 264 | // later declarations of a function in the same |
| 265 | // scope. Declarations in different scopes have completely |
| 266 | // distinct sets of default arguments. That is, declarations in |
| 267 | // inner scopes do not acquire default arguments from |
| 268 | // declarations in outer scopes, and vice versa. In a given |
| 269 | // function declaration, all parameters subsequent to a |
| 270 | // parameter with a default argument shall have default |
| 271 | // arguments supplied in this or previous declarations. A |
| 272 | // default argument shall not be redefined by a later |
| 273 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 274 | // |
| 275 | // C++ [dcl.fct.default]p6: |
| 276 | // Except for member functions of class templates, the default arguments |
| 277 | // in a member function definition that appears outside of the class |
| 278 | // definition are added to the set of default arguments provided by the |
| 279 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 280 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 281 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 282 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 283 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 284 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 285 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 286 | // hint here. Alternatively, we could walk the type-source information |
| 287 | // for NewParam to find the last source location in the type... but it |
| 288 | // isn't worth the effort right now. This is the kind of test case that |
| 289 | // is hard to get right: |
| 290 | |
| 291 | // int f(int); |
| 292 | // void g(int (*fp)(int) = f); |
| 293 | // void g(int (*fp)(int) = &f); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | Diag(NewParam->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 295 | diag::err_param_default_argument_redefinition) |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 296 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 297 | |
| 298 | // Look for the function declaration where the default argument was |
| 299 | // actually written, which may be a declaration prior to Old. |
| 300 | for (FunctionDecl *Older = Old->getPreviousDeclaration(); |
| 301 | Older; Older = Older->getPreviousDeclaration()) { |
| 302 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 303 | break; |
| 304 | |
| 305 | OldParam = Older->getParamDecl(p); |
| 306 | } |
| 307 | |
| 308 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 309 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 310 | Invalid = true; |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 311 | } else if (OldParam->hasDefaultArg()) { |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 312 | // Merge the old default argument into the new parameter. |
| 313 | // It's important to use getInit() here; getDefaultArg() |
| 314 | // strips off any top-level CXXExprWithTemporaries. |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 315 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 316 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 317 | NewParam->setUninstantiatedDefaultArg( |
| 318 | OldParam->getUninstantiatedDefaultArg()); |
| 319 | else |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 320 | NewParam->setDefaultArg(OldParam->getInit()); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 321 | } else if (NewParam->hasDefaultArg()) { |
| 322 | if (New->getDescribedFunctionTemplate()) { |
| 323 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 324 | Diag(NewParam->getLocation(), |
| 325 | diag::err_param_default_argument_template_redecl) |
| 326 | << NewParam->getDefaultArgRange(); |
| 327 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 328 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 329 | } else if (New->getTemplateSpecializationKind() |
| 330 | != TSK_ImplicitInstantiation && |
| 331 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 332 | // C++ [temp.expr.spec]p21: |
| 333 | // Default function arguments shall not be specified in a declaration |
| 334 | // or a definition for one of the following explicit specializations: |
| 335 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 336 | // - the explicit specialization of a member function template; |
| 337 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 338 | // template where the class template specialization to which the |
| 339 | // member function specialization belongs is implicitly |
| 340 | // instantiated. |
| 341 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 342 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 343 | << New->getDeclName() |
| 344 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 345 | } else if (New->getDeclContext()->isDependentContext()) { |
| 346 | // C++ [dcl.fct.default]p6 (DR217): |
| 347 | // Default arguments for a member function of a class template shall |
| 348 | // be specified on the initial declaration of the member function |
| 349 | // within the class template. |
| 350 | // |
| 351 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 352 | // leads me to the conclusion that one cannot add default function |
| 353 | // arguments for an out-of-line definition of a member function of a |
| 354 | // dependent type. |
| 355 | int WhichKind = 2; |
| 356 | if (CXXRecordDecl *Record |
| 357 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 358 | if (Record->getDescribedClassTemplate()) |
| 359 | WhichKind = 0; |
| 360 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 361 | WhichKind = 1; |
| 362 | else |
| 363 | WhichKind = 2; |
| 364 | } |
| 365 | |
| 366 | Diag(NewParam->getLocation(), |
| 367 | diag::err_param_default_argument_member_template_redecl) |
| 368 | << WhichKind |
| 369 | << NewParam->getDefaultArgRange(); |
| 370 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 374 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 375 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 376 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 377 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 381 | /// function declaration are well-formed according to C++ |
| 382 | /// [dcl.fct.default]. |
| 383 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 384 | unsigned NumParams = FD->getNumParams(); |
| 385 | unsigned p; |
| 386 | |
| 387 | // Find first parameter with a default argument |
| 388 | for (p = 0; p < NumParams; ++p) { |
| 389 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 390 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | |
| 394 | // C++ [dcl.fct.default]p4: |
| 395 | // In a given function declaration, all parameters |
| 396 | // subsequent to a parameter with a default argument shall |
| 397 | // have default arguments supplied in this or previous |
| 398 | // declarations. A default argument shall not be redefined |
| 399 | // by a later declaration (not even to the same value). |
| 400 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 402 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 403 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 404 | if (Param->isInvalidDecl()) |
| 405 | /* We already complained about this parameter. */; |
| 406 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 408 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 409 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 410 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 412 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 414 | LastMissingDefaultArg = p; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (LastMissingDefaultArg > 0) { |
| 419 | // Some default arguments were missing. Clear out all of the |
| 420 | // default arguments up to (and including) the last missing |
| 421 | // default argument, so that we leave the function parameters |
| 422 | // in a semantically valid state. |
| 423 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 424 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 425 | if (Param->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 426 | Param->setDefaultArg(0); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 431 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 432 | /// isCurrentClassName - Determine whether the identifier II is the |
| 433 | /// name of the class type currently being defined. In the case of |
| 434 | /// nested classes, this will only return true if II is the name of |
| 435 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 436 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 437 | const CXXScopeSpec *SS) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 438 | assert(getLangOptions().CPlusPlus && "No class names in C!"); |
| 439 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 440 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 441 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 442 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 443 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 444 | } else |
| 445 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 446 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 447 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 448 | return &II == CurDecl->getIdentifier(); |
| 449 | else |
| 450 | return false; |
| 451 | } |
| 452 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 454 | /// |
| 455 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 456 | /// and returns NULL otherwise. |
| 457 | CXXBaseSpecifier * |
| 458 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 459 | SourceRange SpecifierRange, |
| 460 | bool Virtual, AccessSpecifier Access, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 461 | TypeSourceInfo *TInfo) { |
| 462 | QualType BaseType = TInfo->getType(); |
| 463 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 464 | // C++ [class.union]p1: |
| 465 | // A union shall not have base classes. |
| 466 | if (Class->isUnion()) { |
| 467 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 468 | << SpecifierRange; |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | if (BaseType->isDependentType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 474 | Class->getTagKind() == TTK_Class, |
| 475 | Access, TInfo); |
| 476 | |
| 477 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 478 | |
| 479 | // Base specifiers must be record types. |
| 480 | if (!BaseType->isRecordType()) { |
| 481 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | // C++ [class.union]p1: |
| 486 | // A union shall not be used as a base class. |
| 487 | if (BaseType->isUnionType()) { |
| 488 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | // C++ [class.derived]p2: |
| 493 | // The class-name in a base-specifier shall not be an incompletely |
| 494 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 496 | PDiag(diag::err_incomplete_base_class) |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 497 | << SpecifierRange)) { |
| 498 | Class->setInvalidDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 499 | return 0; |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 500 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 501 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 502 | // 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] | 503 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 504 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 505 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 506 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 507 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 508 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 509 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 510 | // C++0x CWG Issue #817 indicates that [[final]] classes shouldn't be bases. |
| 511 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
| 512 | Diag(BaseLoc, diag::err_final_base) << BaseType.getAsString(); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 513 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 514 | << BaseType; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 515 | return 0; |
| 516 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 517 | |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 518 | if (BaseDecl->isInvalidDecl()) |
| 519 | Class->setInvalidDecl(); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 520 | |
| 521 | // Create the base specifier. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 522 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 523 | Class->getTagKind() == TTK_Class, |
| 524 | Access, TInfo); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 527 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 528 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | /// example: |
| 530 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 531 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 532 | BaseResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 533 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 534 | bool Virtual, AccessSpecifier Access, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 535 | ParsedType basetype, SourceLocation BaseLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 536 | if (!classdecl) |
| 537 | return true; |
| 538 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 539 | AdjustDeclIfTemplate(classdecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 540 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 541 | if (!Class) |
| 542 | return true; |
| 543 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 544 | TypeSourceInfo *TInfo = 0; |
| 545 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 546 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 547 | Virtual, Access, TInfo)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 548 | return BaseSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 550 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 551 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 553 | /// \brief Performs the actual work of attaching the given base class |
| 554 | /// specifiers to a C++ class. |
| 555 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 556 | unsigned NumBases) { |
| 557 | if (NumBases == 0) |
| 558 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 559 | |
| 560 | // Used to keep track of which base types we have already seen, so |
| 561 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 562 | // that the key is always the unqualified canonical type of the base |
| 563 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 564 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 565 | |
| 566 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 567 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 568 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 569 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 571 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 572 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Fariborz Jahanian | 0ed5c5d | 2010-05-20 23:34:56 +0000 | [diff] [blame] | 573 | if (!Class->hasObjectMember()) { |
| 574 | if (const RecordType *FDTTy = |
| 575 | NewBaseType.getTypePtr()->getAs<RecordType>()) |
| 576 | if (FDTTy->getDecl()->hasObjectMember()) |
| 577 | Class->setHasObjectMember(true); |
| 578 | } |
| 579 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 580 | if (KnownBaseTypes[NewBaseType]) { |
| 581 | // C++ [class.mi]p3: |
| 582 | // A class shall not be specified as a direct base class of a |
| 583 | // derived class more than once. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 584 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 585 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 586 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 587 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 588 | |
| 589 | // Delete the duplicate base class specifier; we're going to |
| 590 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 591 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 592 | |
| 593 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 594 | } else { |
| 595 | // Okay, add this new base class. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 596 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 597 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
| 601 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 602 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 603 | |
| 604 | // Delete the remaining (good) base class specifiers, since their |
| 605 | // data has been copied into the CXXRecordDecl. |
| 606 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 607 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 608 | |
| 609 | return Invalid; |
| 610 | } |
| 611 | |
| 612 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 613 | /// class, after checking whether there are any duplicate base |
| 614 | /// classes. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 615 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, BaseTy **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 616 | unsigned NumBases) { |
| 617 | if (!ClassDecl || !Bases || !NumBases) |
| 618 | return; |
| 619 | |
| 620 | AdjustDeclIfTemplate(ClassDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 621 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 622 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 623 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 624 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 625 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 626 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 627 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 628 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 629 | return ICT->getDecl(); |
| 630 | else |
| 631 | return 0; |
| 632 | } |
| 633 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 634 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 635 | /// derived from the type \p Base. |
| 636 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 637 | if (!getLangOptions().CPlusPlus) |
| 638 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 639 | |
| 640 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 641 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 642 | return false; |
| 643 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 644 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 645 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 646 | return false; |
| 647 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 648 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 649 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 653 | /// derived from the type \p Base. |
| 654 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 655 | if (!getLangOptions().CPlusPlus) |
| 656 | return false; |
| 657 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 658 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 659 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 660 | return false; |
| 661 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 662 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 663 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 664 | return false; |
| 665 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 666 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 667 | } |
| 668 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 669 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 670 | CXXCastPath &BasePathArray) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 671 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 672 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 673 | |
| 674 | const CXXBasePath &Path = Paths.front(); |
| 675 | |
| 676 | // We first go backward and check if we have a virtual base. |
| 677 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 678 | // the nearest virtual base. |
| 679 | unsigned Start = 0; |
| 680 | for (unsigned I = Path.size(); I != 0; --I) { |
| 681 | if (Path[I - 1].Base->isVirtual()) { |
| 682 | Start = I - 1; |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | // Now add all bases. |
| 688 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 689 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 692 | /// \brief Determine whether the given base path includes a virtual |
| 693 | /// base class. |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 694 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 695 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 696 | BEnd = BasePath.end(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 697 | B != BEnd; ++B) |
| 698 | if ((*B)->isVirtual()) |
| 699 | return true; |
| 700 | |
| 701 | return false; |
| 702 | } |
| 703 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 704 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 705 | /// conversion (where Derived and Base are class types) is |
| 706 | /// well-formed, meaning that the conversion is unambiguous (and |
| 707 | /// that all of the base classes are accessible). Returns true |
| 708 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 709 | /// otherwise. Loc is the location where this routine should point to |
| 710 | /// if there is an error, and Range is the source range to highlight |
| 711 | /// if there is an error. |
| 712 | bool |
| 713 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 714 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 715 | unsigned AmbigiousBaseConvID, |
| 716 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 717 | DeclarationName Name, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 718 | CXXCastPath *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 719 | // First, determine whether the path from Derived to Base is |
| 720 | // ambiguous. This is slightly more expensive than checking whether |
| 721 | // the Derived to Base conversion exists, because here we need to |
| 722 | // explore multiple paths to determine if there is an ambiguity. |
| 723 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 724 | /*DetectVirtual=*/false); |
| 725 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 726 | assert(DerivationOkay && |
| 727 | "Can only be used with a derived-to-base conversion"); |
| 728 | (void)DerivationOkay; |
| 729 | |
| 730 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 731 | if (InaccessibleBaseID) { |
| 732 | // Check that the base class can be accessed. |
| 733 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 734 | InaccessibleBaseID)) { |
| 735 | case AR_inaccessible: |
| 736 | return true; |
| 737 | case AR_accessible: |
| 738 | case AR_dependent: |
| 739 | case AR_delayed: |
| 740 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 741 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 742 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 743 | |
| 744 | // Build a base path if necessary. |
| 745 | if (BasePath) |
| 746 | BuildBasePathArray(Paths, *BasePath); |
| 747 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | // We know that the derived-to-base conversion is ambiguous, and |
| 751 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 752 | // search just one more time to compute all of the possible paths so |
| 753 | // that we can print them out. This is more expensive than any of |
| 754 | // the previous derived-to-base checks we've done, but at this point |
| 755 | // performance isn't as much of an issue. |
| 756 | Paths.clear(); |
| 757 | Paths.setRecordingPaths(true); |
| 758 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 759 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 760 | (void)StillOkay; |
| 761 | |
| 762 | // Build up a textual representation of the ambiguous paths, e.g., |
| 763 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 764 | // conversions in the diagnostic. We only print one of the paths |
| 765 | // to each base class subobject. |
| 766 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 767 | |
| 768 | Diag(Loc, AmbigiousBaseConvID) |
| 769 | << Derived << Base << PathDisplayStr << Range << Name; |
| 770 | return true; |
| 771 | } |
| 772 | |
| 773 | bool |
| 774 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 775 | SourceLocation Loc, SourceRange Range, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 776 | CXXCastPath *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 777 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 778 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 779 | IgnoreAccess ? 0 |
| 780 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 781 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 782 | Loc, Range, DeclarationName(), |
| 783 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | |
| 787 | /// @brief Builds a string representing ambiguous paths from a |
| 788 | /// specific derived class to different subobjects of the same base |
| 789 | /// class. |
| 790 | /// |
| 791 | /// This function builds a string that can be used in error messages |
| 792 | /// to show the different paths that one can take through the |
| 793 | /// inheritance hierarchy to go from the derived class to different |
| 794 | /// subobjects of a base class. The result looks something like this: |
| 795 | /// @code |
| 796 | /// struct D -> struct B -> struct A |
| 797 | /// struct D -> struct C -> struct A |
| 798 | /// @endcode |
| 799 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 800 | std::string PathDisplayStr; |
| 801 | std::set<unsigned> DisplayedPaths; |
| 802 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 803 | Path != Paths.end(); ++Path) { |
| 804 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 805 | // We haven't displayed a path to this particular base |
| 806 | // class subobject yet. |
| 807 | PathDisplayStr += "\n "; |
| 808 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 809 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 810 | Element != Path->end(); ++Element) |
| 811 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | return PathDisplayStr; |
| 816 | } |
| 817 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 818 | //===----------------------------------------------------------------------===// |
| 819 | // C++ class member Handling |
| 820 | //===----------------------------------------------------------------------===// |
| 821 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 822 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 823 | Decl *Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 824 | SourceLocation ASLoc, |
| 825 | SourceLocation ColonLoc) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 826 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 827 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 828 | ASLoc, ColonLoc); |
| 829 | CurContext->addHiddenDecl(ASDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 830 | return ASDecl; |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 833 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 834 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 835 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
Chris Lattner | b6688e0 | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 836 | /// any. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 837 | Decl * |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 838 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 839 | MultiTemplateParamsArg TemplateParameterLists, |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 840 | ExprTy *BW, ExprTy *InitExpr, bool IsDefinition, |
| 841 | bool Deleted) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 842 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 843 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 844 | DeclarationName Name = NameInfo.getName(); |
| 845 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 846 | |
| 847 | // For anonymous bitfields, the location should point to the type. |
| 848 | if (Loc.isInvalid()) |
| 849 | Loc = D.getSourceRange().getBegin(); |
| 850 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 851 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 852 | Expr *Init = static_cast<Expr*>(InitExpr); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 853 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 854 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 855 | assert(!DS.isFriendSpecified()); |
| 856 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 857 | bool isFunc = false; |
| 858 | if (D.isFunctionDeclarator()) |
| 859 | isFunc = true; |
| 860 | else if (D.getNumTypeObjects() == 0 && |
| 861 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 862 | QualType TDType = GetTypeFromParser(DS.getRepAsType()); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 863 | isFunc = TDType->isFunctionType(); |
| 864 | } |
| 865 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 866 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 867 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 868 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 869 | // data members and cannot be applied to names declared const or static, |
| 870 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 871 | switch (DS.getStorageClassSpec()) { |
| 872 | case DeclSpec::SCS_unspecified: |
| 873 | case DeclSpec::SCS_typedef: |
| 874 | case DeclSpec::SCS_static: |
| 875 | // FALL THROUGH. |
| 876 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 877 | case DeclSpec::SCS_mutable: |
| 878 | if (isFunc) { |
| 879 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 880 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 881 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 882 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 884 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 885 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 886 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 887 | } |
| 888 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 889 | default: |
| 890 | if (DS.getStorageClassSpecLoc().isValid()) |
| 891 | Diag(DS.getStorageClassSpecLoc(), |
| 892 | diag::err_storageclass_invalid_for_member); |
| 893 | else |
| 894 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 895 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 896 | } |
| 897 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 898 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 899 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 900 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 901 | |
| 902 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 903 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 904 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 905 | |
| 906 | |
| 907 | if (SS.isSet() && !SS.isInvalid()) { |
| 908 | // The user provided a superfluous scope specifier inside a class |
| 909 | // definition: |
| 910 | // |
| 911 | // class X { |
| 912 | // int X::member; |
| 913 | // }; |
| 914 | DeclContext *DC = 0; |
| 915 | if ((DC = computeDeclContext(SS, false)) && DC->Equals(CurContext)) |
| 916 | Diag(D.getIdentifierLoc(), diag::warn_member_extra_qualification) |
| 917 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
| 918 | else |
| 919 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 920 | << Name << SS.getRange(); |
| 921 | |
| 922 | SS.clear(); |
| 923 | } |
| 924 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 925 | // FIXME: Check for template parameters! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 926 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 927 | AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 928 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 929 | } else { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 930 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 931 | if (!Member) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 932 | return 0; |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 933 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 934 | |
| 935 | // Non-instance-fields can't have a bitfield. |
| 936 | if (BitWidth) { |
| 937 | if (Member->isInvalidDecl()) { |
| 938 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 939 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 940 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 941 | // "static member 'A' cannot be a bit-field" |
| 942 | Diag(Loc, diag::err_static_not_bitfield) |
| 943 | << Name << BitWidth->getSourceRange(); |
| 944 | } else if (isa<TypedefDecl>(Member)) { |
| 945 | // "typedef member 'x' cannot be a bit-field" |
| 946 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 947 | << Name << BitWidth->getSourceRange(); |
| 948 | } else { |
| 949 | // A function typedef ("typedef int f(); f a;"). |
| 950 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 951 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 953 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 954 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 956 | BitWidth = 0; |
| 957 | Member->setInvalidDecl(); |
| 958 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 959 | |
| 960 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 962 | // If we have declared a member function template, set the access of the |
| 963 | // templated declaration as well. |
| 964 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 965 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 966 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 967 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 968 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 969 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 970 | if (Init) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 971 | AddInitializerToDecl(Member, Init, false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 972 | if (Deleted) // FIXME: Source location is not very good. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 973 | SetDeclDeleted(Member, D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 974 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 975 | if (isInstField) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 976 | FieldCollector->Add(cast<FieldDecl>(Member)); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 977 | return 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 978 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 979 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 982 | /// \brief Find the direct and/or virtual base specifiers that |
| 983 | /// correspond to the given base type, for use in base initialization |
| 984 | /// within a constructor. |
| 985 | static bool FindBaseInitializer(Sema &SemaRef, |
| 986 | CXXRecordDecl *ClassDecl, |
| 987 | QualType BaseType, |
| 988 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 989 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 990 | // First, check for a direct base class. |
| 991 | DirectBaseSpec = 0; |
| 992 | for (CXXRecordDecl::base_class_const_iterator Base |
| 993 | = ClassDecl->bases_begin(); |
| 994 | Base != ClassDecl->bases_end(); ++Base) { |
| 995 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 996 | // We found a direct base of this type. That's what we're |
| 997 | // initializing. |
| 998 | DirectBaseSpec = &*Base; |
| 999 | break; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | // Check for a virtual base class. |
| 1004 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1005 | // there are no virtual bases. |
| 1006 | VirtualBaseSpec = 0; |
| 1007 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1008 | // We haven't found a base yet; search the class hierarchy for a |
| 1009 | // virtual base class. |
| 1010 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1011 | /*DetectVirtual=*/false); |
| 1012 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1013 | BaseType, Paths)) { |
| 1014 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1015 | Path != Paths.end(); ++Path) { |
| 1016 | if (Path->back().Base->isVirtual()) { |
| 1017 | VirtualBaseSpec = Path->back().Base; |
| 1018 | break; |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | return DirectBaseSpec || VirtualBaseSpec; |
| 1025 | } |
| 1026 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1027 | /// ActOnMemInitializer - Handle a C++ member initializer. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1028 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1029 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1030 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1031 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1032 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1033 | ParsedType TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1034 | SourceLocation IdLoc, |
| 1035 | SourceLocation LParenLoc, |
| 1036 | ExprTy **Args, unsigned NumArgs, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1037 | SourceLocation RParenLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1038 | if (!ConstructorD) |
| 1039 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1041 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | |
| 1043 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1044 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1045 | if (!Constructor) { |
| 1046 | // The user wrote a constructor initializer on a function that is |
| 1047 | // not a C++ constructor. Ignore the error for now, because we may |
| 1048 | // have more member initializers coming; we'll diagnose it just |
| 1049 | // once in ActOnMemInitializers. |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
| 1053 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1054 | |
| 1055 | // C++ [class.base.init]p2: |
| 1056 | // Names in a mem-initializer-id are looked up in the scope of the |
| 1057 | // constructor’s class and, if not found in that scope, are looked |
| 1058 | // up in the scope containing the constructor’s |
| 1059 | // definition. [Note: if the constructor’s class contains a member |
| 1060 | // with the same name as a direct or virtual base class of the |
| 1061 | // class, a mem-initializer-id naming the member or base class and |
| 1062 | // composed of a single identifier refers to the class member. A |
| 1063 | // mem-initializer-id for the hidden base class may be specified |
| 1064 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1065 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1066 | // Look for a member, first. |
| 1067 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1068 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1069 | = ClassDecl->lookup(MemberOrBase); |
| 1070 | if (Result.first != Result.second) |
| 1071 | Member = dyn_cast<FieldDecl>(*Result.first); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1072 | |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1073 | // FIXME: Handle members of an anonymous union. |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1074 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1075 | if (Member) |
| 1076 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1077 | LParenLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1078 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1079 | // 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] | 1080 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1081 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1082 | |
| 1083 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1084 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1085 | } else { |
| 1086 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1087 | LookupParsedName(R, S, &SS); |
| 1088 | |
| 1089 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1090 | if (!TyD) { |
| 1091 | if (R.isAmbiguous()) return true; |
| 1092 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1093 | // We don't want access-control diagnostics here. |
| 1094 | R.suppressDiagnostics(); |
| 1095 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1096 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1097 | bool NotUnknownSpecialization = false; |
| 1098 | DeclContext *DC = computeDeclContext(SS, false); |
| 1099 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1100 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1101 | |
| 1102 | if (!NotUnknownSpecialization) { |
| 1103 | // When the scope specifier can refer to a member of an unknown |
| 1104 | // specialization, we take it as a type name. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 1105 | BaseType = CheckTypenameType(ETK_None, |
| 1106 | (NestedNameSpecifier *)SS.getScopeRep(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 1107 | *MemberOrBase, SourceLocation(), |
| 1108 | SS.getRange(), IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1109 | if (BaseType.isNull()) |
| 1110 | return true; |
| 1111 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1112 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 1113 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1117 | // If no results were found, try to correct typos. |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1118 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1119 | CorrectTypo(R, S, &SS, ClassDecl, 0, CTC_NoKeywords) && |
| 1120 | R.isSingleResult()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1121 | if (FieldDecl *Member = R.getAsSingle<FieldDecl>()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1122 | if (Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl)) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1123 | // We have found a non-static data member with a similar |
| 1124 | // name to what was typed; complain and initialize that |
| 1125 | // member. |
| 1126 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1127 | << MemberOrBase << true << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1128 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1129 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1130 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 1131 | << Member->getDeclName(); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1132 | |
| 1133 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 1134 | LParenLoc, RParenLoc); |
| 1135 | } |
| 1136 | } else if (TypeDecl *Type = R.getAsSingle<TypeDecl>()) { |
| 1137 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1138 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1139 | if (FindBaseInitializer(*this, ClassDecl, |
| 1140 | Context.getTypeDeclType(Type), |
| 1141 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1142 | // We have found a direct or virtual base class with a |
| 1143 | // similar name to what was typed; complain and initialize |
| 1144 | // that base class. |
| 1145 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1146 | << MemberOrBase << false << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1147 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1148 | R.getLookupName().getAsString()); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1149 | |
| 1150 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1151 | : VirtualBaseSpec; |
| 1152 | Diag(BaseSpec->getSourceRange().getBegin(), |
| 1153 | diag::note_base_class_specified_here) |
| 1154 | << BaseSpec->getType() |
| 1155 | << BaseSpec->getSourceRange(); |
| 1156 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1157 | TyD = Type; |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1162 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1163 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1164 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1165 | return true; |
| 1166 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1169 | if (BaseType.isNull()) { |
| 1170 | BaseType = Context.getTypeDeclType(TyD); |
| 1171 | if (SS.isSet()) { |
| 1172 | NestedNameSpecifier *Qualifier = |
| 1173 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1174 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1175 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1176 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1177 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1178 | } |
| 1179 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1181 | if (!TInfo) |
| 1182 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1183 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1184 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1185 | LParenLoc, RParenLoc, ClassDecl); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1188 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1189 | /// containing the field that is being initialized. Returns true if there is an |
| 1190 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1191 | /// otherwise. |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1192 | static bool InitExprContainsUninitializedFields(const Stmt *S, |
| 1193 | const FieldDecl *LhsField, |
| 1194 | SourceLocation *L) { |
| 1195 | if (isa<CallExpr>(S)) { |
| 1196 | // Do not descend into function calls or constructors, as the use |
| 1197 | // of an uninitialized field may be valid. One would have to inspect |
| 1198 | // the contents of the function/ctor to determine if it is safe or not. |
| 1199 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1200 | // may be safe, depending on what the function/ctor does. |
| 1201 | return false; |
| 1202 | } |
| 1203 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 1204 | const NamedDecl *RhsField = ME->getMemberDecl(); |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1205 | |
| 1206 | if (const VarDecl *VD = dyn_cast<VarDecl>(RhsField)) { |
| 1207 | // The member expression points to a static data member. |
| 1208 | assert(VD->isStaticDataMember() && |
| 1209 | "Member points to non-static data member!"); |
Nick Lewycky | edd5911 | 2010-10-06 18:37:39 +0000 | [diff] [blame] | 1210 | (void)VD; |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1211 | return false; |
| 1212 | } |
| 1213 | |
| 1214 | if (isa<EnumConstantDecl>(RhsField)) { |
| 1215 | // The member expression points to an enum. |
| 1216 | return false; |
| 1217 | } |
| 1218 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1219 | if (RhsField == LhsField) { |
| 1220 | // Initializing a field with itself. Throw a warning. |
| 1221 | // But wait; there are exceptions! |
| 1222 | // Exception #1: The field may not belong to this record. |
| 1223 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1224 | const Expr *base = ME->getBase(); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1225 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1226 | // Even though the field matches, it does not belong to this record. |
| 1227 | return false; |
| 1228 | } |
| 1229 | // None of the exceptions triggered; return true to indicate an |
| 1230 | // uninitialized field was used. |
| 1231 | *L = ME->getMemberLoc(); |
| 1232 | return true; |
| 1233 | } |
Argyrios Kyrtzidis | ff8819b | 2010-09-21 10:47:20 +0000 | [diff] [blame] | 1234 | } else if (isa<SizeOfAlignOfExpr>(S)) { |
| 1235 | // sizeof/alignof doesn't reference contents, do not warn. |
| 1236 | return false; |
| 1237 | } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(S)) { |
| 1238 | // address-of doesn't reference contents (the pointer may be dereferenced |
| 1239 | // in the same expression but it would be rare; and weird). |
| 1240 | if (UOE->getOpcode() == UO_AddrOf) |
| 1241 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1242 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1243 | for (Stmt::const_child_iterator it = S->child_begin(), e = S->child_end(); |
| 1244 | it != e; ++it) { |
| 1245 | if (!*it) { |
| 1246 | // An expression such as 'member(arg ?: "")' may trigger this. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1247 | continue; |
| 1248 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1249 | if (InitExprContainsUninitializedFields(*it, LhsField, L)) |
| 1250 | return true; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1251 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1252 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1255 | MemInitResult |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1256 | Sema::BuildMemberInitializer(FieldDecl *Member, Expr **Args, |
| 1257 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1258 | SourceLocation LParenLoc, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1259 | SourceLocation RParenLoc) { |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 1260 | if (Member->isInvalidDecl()) |
| 1261 | return true; |
| 1262 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1263 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1264 | // foo(foo) |
| 1265 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1266 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1267 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1268 | SourceLocation L; |
| 1269 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1270 | // FIXME: Return true in the case when other fields are used before being |
| 1271 | // uninitialized. For example, let this field be the i'th field. When |
| 1272 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1273 | // fields are used, as they are not yet initialized. |
| 1274 | // Right now we are only handling the case where the i'th field uses |
| 1275 | // itself in its initializer. |
| 1276 | Diag(L, diag::warn_field_is_uninit); |
| 1277 | } |
| 1278 | } |
| 1279 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1280 | bool HasDependentArg = false; |
| 1281 | for (unsigned i = 0; i < NumArgs; i++) |
| 1282 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1283 | |
Eli Friedman | 0f2b97d | 2010-07-24 21:19:15 +0000 | [diff] [blame] | 1284 | if (Member->getType()->isDependentType() || HasDependentArg) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1285 | // Can't check initialization for a member of dependent type or when |
| 1286 | // any of the arguments are type-dependent expressions. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1287 | Expr *Init |
| 1288 | = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1289 | RParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1290 | |
| 1291 | // Erase any temporaries within this evaluation context; we're not |
| 1292 | // going to track them in the AST, since we'll be rebuilding the |
| 1293 | // ASTs during template instantiation. |
| 1294 | ExprTemporaries.erase( |
| 1295 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1296 | ExprTemporaries.end()); |
| 1297 | |
| 1298 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1299 | LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1300 | Init, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1301 | RParenLoc); |
| 1302 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1303 | } |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1304 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1305 | // Initialize the member. |
| 1306 | InitializedEntity MemberEntity = |
| 1307 | InitializedEntity::InitializeMember(Member, 0); |
| 1308 | InitializationKind Kind = |
| 1309 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc); |
| 1310 | |
| 1311 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 1312 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1313 | ExprResult MemberInit = |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1314 | InitSeq.Perform(*this, MemberEntity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1315 | MultiExprArg(*this, Args, NumArgs), 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1316 | if (MemberInit.isInvalid()) |
| 1317 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1318 | |
| 1319 | CheckImplicitConversions(MemberInit.get(), LParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1320 | |
| 1321 | // C++0x [class.base.init]p7: |
| 1322 | // The initialization of each base and member constitutes a |
| 1323 | // full-expression. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1324 | MemberInit = MaybeCreateCXXExprWithTemporaries(MemberInit.get()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1325 | if (MemberInit.isInvalid()) |
| 1326 | return true; |
| 1327 | |
| 1328 | // If we are in a dependent context, template instantiation will |
| 1329 | // perform this type-checking again. Just save the arguments that we |
| 1330 | // received in a ParenListExpr. |
| 1331 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1332 | // of the information that we have about the member |
| 1333 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1334 | // and this approach is far more likely to get the corner cases right. |
| 1335 | if (CurContext->isDependentContext()) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1336 | Expr *Init = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1337 | RParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1338 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1339 | LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1340 | Init, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1341 | RParenLoc); |
| 1342 | } |
| 1343 | |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1344 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1345 | LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1346 | MemberInit.get(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1347 | RParenLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1350 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1351 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1352 | Expr **Args, unsigned NumArgs, |
| 1353 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
| 1354 | CXXRecordDecl *ClassDecl) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1355 | bool HasDependentArg = false; |
| 1356 | for (unsigned i = 0; i < NumArgs; i++) |
| 1357 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1358 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1359 | SourceLocation BaseLoc |
| 1360 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1361 | |
| 1362 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 1363 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 1364 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1365 | |
| 1366 | // C++ [class.base.init]p2: |
| 1367 | // [...] Unless the mem-initializer-id names a nonstatic data |
| 1368 | // member of the constructor’s class or a direct or virtual base |
| 1369 | // of that class, the mem-initializer is ill-formed. A |
| 1370 | // mem-initializer-list can initialize a base class using any |
| 1371 | // name that denotes that base class type. |
| 1372 | bool Dependent = BaseType->isDependentType() || HasDependentArg; |
| 1373 | |
| 1374 | // Check for direct and virtual base classes. |
| 1375 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1376 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1377 | if (!Dependent) { |
| 1378 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 1379 | VirtualBaseSpec); |
| 1380 | |
| 1381 | // C++ [base.class.init]p2: |
| 1382 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 1383 | // constructor's class or a direct or virtual base of that class, the |
| 1384 | // mem-initializer is ill-formed. |
| 1385 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 1386 | // If the class has any dependent bases, then it's possible that |
| 1387 | // one of those types will resolve to the same type as |
| 1388 | // BaseType. Therefore, just treat this as a dependent base |
| 1389 | // class initialization. FIXME: Should we try to check the |
| 1390 | // initialization anyway? It seems odd. |
| 1391 | if (ClassDecl->hasAnyDependentBases()) |
| 1392 | Dependent = true; |
| 1393 | else |
| 1394 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 1395 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 1396 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | if (Dependent) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1401 | // Can't check initialization for a base of dependent type or when |
| 1402 | // any of the arguments are type-dependent expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1403 | ExprResult BaseInit |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1404 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1405 | RParenLoc)); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1406 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1407 | // Erase any temporaries within this evaluation context; we're not |
| 1408 | // going to track them in the AST, since we'll be rebuilding the |
| 1409 | // ASTs during template instantiation. |
| 1410 | ExprTemporaries.erase( |
| 1411 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1412 | ExprTemporaries.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1414 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1415 | /*IsVirtual=*/false, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1416 | LParenLoc, |
| 1417 | BaseInit.takeAs<Expr>(), |
| 1418 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1419 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1420 | |
| 1421 | // C++ [base.class.init]p2: |
| 1422 | // If a mem-initializer-id is ambiguous because it designates both |
| 1423 | // a direct non-virtual base class and an inherited virtual base |
| 1424 | // class, the mem-initializer is ill-formed. |
| 1425 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1426 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1427 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1428 | |
| 1429 | CXXBaseSpecifier *BaseSpec |
| 1430 | = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
| 1431 | if (!BaseSpec) |
| 1432 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 1433 | |
| 1434 | // Initialize the base. |
| 1435 | InitializedEntity BaseEntity = |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1436 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1437 | InitializationKind Kind = |
| 1438 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc); |
| 1439 | |
| 1440 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 1441 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1442 | ExprResult BaseInit = |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1443 | InitSeq.Perform(*this, BaseEntity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1444 | MultiExprArg(*this, Args, NumArgs), 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1445 | if (BaseInit.isInvalid()) |
| 1446 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1447 | |
| 1448 | CheckImplicitConversions(BaseInit.get(), LParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1449 | |
| 1450 | // C++0x [class.base.init]p7: |
| 1451 | // The initialization of each base and member constitutes a |
| 1452 | // full-expression. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1453 | BaseInit = MaybeCreateCXXExprWithTemporaries(BaseInit.get()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1454 | if (BaseInit.isInvalid()) |
| 1455 | return true; |
| 1456 | |
| 1457 | // If we are in a dependent context, template instantiation will |
| 1458 | // perform this type-checking again. Just save the arguments that we |
| 1459 | // received in a ParenListExpr. |
| 1460 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1461 | // of the information that we have about the base |
| 1462 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1463 | // and this approach is far more likely to get the corner cases right. |
| 1464 | if (CurContext->isDependentContext()) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1465 | ExprResult Init |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1466 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1467 | RParenLoc)); |
| 1468 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1469 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1470 | LParenLoc, |
| 1471 | Init.takeAs<Expr>(), |
| 1472 | RParenLoc); |
| 1473 | } |
| 1474 | |
| 1475 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1476 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1477 | LParenLoc, |
| 1478 | BaseInit.takeAs<Expr>(), |
| 1479 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1482 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 1483 | /// initialize its base or member. |
| 1484 | enum ImplicitInitializerKind { |
| 1485 | IIK_Default, |
| 1486 | IIK_Copy, |
| 1487 | IIK_Move |
| 1488 | }; |
| 1489 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1490 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1491 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1492 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1493 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1494 | bool IsInheritedVirtualBase, |
| 1495 | CXXBaseOrMemberInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1496 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1497 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 1498 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1499 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1500 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1501 | |
| 1502 | switch (ImplicitInitKind) { |
| 1503 | case IIK_Default: { |
| 1504 | InitializationKind InitKind |
| 1505 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 1506 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1507 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1508 | MultiExprArg(SemaRef, 0, 0)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1509 | break; |
| 1510 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1511 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1512 | case IIK_Copy: { |
| 1513 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1514 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1515 | |
| 1516 | Expr *CopyCtorArg = |
| 1517 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
Douglas Gregor | 62b71f4 | 2010-05-03 15:43:53 +0000 | [diff] [blame] | 1518 | Constructor->getLocation(), ParamType, 0); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1519 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1520 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 1521 | QualType ArgTy = |
| 1522 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 1523 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1524 | |
| 1525 | CXXCastPath BasePath; |
| 1526 | BasePath.push_back(BaseSpec); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1527 | SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1528 | CK_UncheckedDerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1529 | VK_LValue, &BasePath); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1530 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1531 | InitializationKind InitKind |
| 1532 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1533 | SourceLocation(), SourceLocation()); |
| 1534 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1535 | &CopyCtorArg, 1); |
| 1536 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1537 | MultiExprArg(&CopyCtorArg, 1)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1538 | break; |
| 1539 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1540 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1541 | case IIK_Move: |
| 1542 | assert(false && "Unhandled initializer kind!"); |
| 1543 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1544 | |
| 1545 | if (BaseInit.isInvalid()) |
| 1546 | return true; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1547 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1548 | BaseInit = SemaRef.MaybeCreateCXXExprWithTemporaries(BaseInit.get()); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1549 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1550 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1551 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1552 | CXXBaseInit = |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1553 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
| 1554 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 1555 | SourceLocation()), |
| 1556 | BaseSpec->isVirtual(), |
| 1557 | SourceLocation(), |
| 1558 | BaseInit.takeAs<Expr>(), |
| 1559 | SourceLocation()); |
| 1560 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1561 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1564 | static bool |
| 1565 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1566 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1567 | FieldDecl *Field, |
| 1568 | CXXBaseOrMemberInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 1569 | if (Field->isInvalidDecl()) |
| 1570 | return true; |
| 1571 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1572 | SourceLocation Loc = Constructor->getLocation(); |
| 1573 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1574 | if (ImplicitInitKind == IIK_Copy) { |
| 1575 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1576 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1577 | |
| 1578 | Expr *MemberExprBase = |
| 1579 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1580 | Loc, ParamType, 0); |
| 1581 | |
| 1582 | // Build a reference to this field within the parameter. |
| 1583 | CXXScopeSpec SS; |
| 1584 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 1585 | Sema::LookupMemberName); |
| 1586 | MemberLookup.addDecl(Field, AS_public); |
| 1587 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1588 | ExprResult CopyCtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1589 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1590 | ParamType, Loc, |
| 1591 | /*IsArrow=*/false, |
| 1592 | SS, |
| 1593 | /*FirstQualifierInScope=*/0, |
| 1594 | MemberLookup, |
| 1595 | /*TemplateArgs=*/0); |
| 1596 | if (CopyCtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1597 | return true; |
| 1598 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1599 | // When the field we are copying is an array, create index variables for |
| 1600 | // each dimension of the array. We use these index variables to subscript |
| 1601 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 1602 | // necessary iteration with these index variables. |
| 1603 | llvm::SmallVector<VarDecl *, 4> IndexVariables; |
| 1604 | QualType BaseType = Field->getType(); |
| 1605 | QualType SizeType = SemaRef.Context.getSizeType(); |
| 1606 | while (const ConstantArrayType *Array |
| 1607 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
| 1608 | // Create the iteration variable for this array index. |
| 1609 | IdentifierInfo *IterationVarName = 0; |
| 1610 | { |
| 1611 | llvm::SmallString<8> Str; |
| 1612 | llvm::raw_svector_ostream OS(Str); |
| 1613 | OS << "__i" << IndexVariables.size(); |
| 1614 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 1615 | } |
| 1616 | VarDecl *IterationVar |
| 1617 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, |
| 1618 | IterationVarName, SizeType, |
| 1619 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1620 | SC_None, SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1621 | IndexVariables.push_back(IterationVar); |
| 1622 | |
| 1623 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1624 | ExprResult IterationVarRef |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1625 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, Loc); |
| 1626 | assert(!IterationVarRef.isInvalid() && |
| 1627 | "Reference to invented variable cannot fail!"); |
| 1628 | |
| 1629 | // Subscript the array with this iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1630 | CopyCtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CopyCtorArg.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1631 | Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1632 | IterationVarRef.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1633 | Loc); |
| 1634 | if (CopyCtorArg.isInvalid()) |
| 1635 | return true; |
| 1636 | |
| 1637 | BaseType = Array->getElementType(); |
| 1638 | } |
| 1639 | |
| 1640 | // Construct the entity that we will be initializing. For an array, this |
| 1641 | // will be first element in the array, which may require several levels |
| 1642 | // of array-subscript entities. |
| 1643 | llvm::SmallVector<InitializedEntity, 4> Entities; |
| 1644 | Entities.reserve(1 + IndexVariables.size()); |
| 1645 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
| 1646 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 1647 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 1648 | 0, |
| 1649 | Entities.back())); |
| 1650 | |
| 1651 | // Direct-initialize to use the copy constructor. |
| 1652 | InitializationKind InitKind = |
| 1653 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 1654 | |
| 1655 | Expr *CopyCtorArgE = CopyCtorArg.takeAs<Expr>(); |
| 1656 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
| 1657 | &CopyCtorArgE, 1); |
| 1658 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1659 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1660 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1661 | MultiExprArg(&CopyCtorArgE, 1)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1662 | MemberInit = SemaRef.MaybeCreateCXXExprWithTemporaries(MemberInit.get()); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1663 | if (MemberInit.isInvalid()) |
| 1664 | return true; |
| 1665 | |
| 1666 | CXXMemberInit |
| 1667 | = CXXBaseOrMemberInitializer::Create(SemaRef.Context, Field, Loc, Loc, |
| 1668 | MemberInit.takeAs<Expr>(), Loc, |
| 1669 | IndexVariables.data(), |
| 1670 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1671 | return false; |
| 1672 | } |
| 1673 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1674 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 1675 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1676 | QualType FieldBaseElementType = |
| 1677 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 1678 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1679 | if (FieldBaseElementType->isRecordType()) { |
| 1680 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1681 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1682 | InitializationKind::CreateDefault(Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1683 | |
| 1684 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1685 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1686 | InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1687 | if (MemberInit.isInvalid()) |
| 1688 | return true; |
| 1689 | |
| 1690 | MemberInit = SemaRef.MaybeCreateCXXExprWithTemporaries(MemberInit.get()); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1691 | if (MemberInit.isInvalid()) |
| 1692 | return true; |
| 1693 | |
| 1694 | CXXMemberInit = |
| 1695 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1696 | Field, Loc, Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1697 | MemberInit.get(), |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1698 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1699 | return false; |
| 1700 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 1701 | |
| 1702 | if (FieldBaseElementType->isReferenceType()) { |
| 1703 | SemaRef.Diag(Constructor->getLocation(), |
| 1704 | diag::err_uninitialized_member_in_ctor) |
| 1705 | << (int)Constructor->isImplicit() |
| 1706 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1707 | << 0 << Field->getDeclName(); |
| 1708 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1709 | return true; |
| 1710 | } |
| 1711 | |
| 1712 | if (FieldBaseElementType.isConstQualified()) { |
| 1713 | SemaRef.Diag(Constructor->getLocation(), |
| 1714 | diag::err_uninitialized_member_in_ctor) |
| 1715 | << (int)Constructor->isImplicit() |
| 1716 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1717 | << 1 << Field->getDeclName(); |
| 1718 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1719 | return true; |
| 1720 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1721 | |
| 1722 | // Nothing to initialize. |
| 1723 | CXXMemberInit = 0; |
| 1724 | return false; |
| 1725 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1726 | |
| 1727 | namespace { |
| 1728 | struct BaseAndFieldInfo { |
| 1729 | Sema &S; |
| 1730 | CXXConstructorDecl *Ctor; |
| 1731 | bool AnyErrorsInInits; |
| 1732 | ImplicitInitializerKind IIK; |
| 1733 | llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; |
| 1734 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 8> AllToInit; |
| 1735 | |
| 1736 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 1737 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
| 1738 | // FIXME: Handle implicit move constructors. |
| 1739 | if (Ctor->isImplicit() && Ctor->isCopyConstructor()) |
| 1740 | IIK = IIK_Copy; |
| 1741 | else |
| 1742 | IIK = IIK_Default; |
| 1743 | } |
| 1744 | }; |
| 1745 | } |
| 1746 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1747 | static void RecordFieldInitializer(BaseAndFieldInfo &Info, |
| 1748 | FieldDecl *Top, FieldDecl *Field, |
| 1749 | CXXBaseOrMemberInitializer *Init) { |
| 1750 | // If the member doesn't need to be initialized, Init will still be null. |
| 1751 | if (!Init) |
| 1752 | return; |
| 1753 | |
| 1754 | Info.AllToInit.push_back(Init); |
| 1755 | if (Field != Top) { |
| 1756 | Init->setMember(Top); |
| 1757 | Init->setAnonUnionMember(Field); |
| 1758 | } |
| 1759 | } |
| 1760 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1761 | static bool CollectFieldInitializer(BaseAndFieldInfo &Info, |
| 1762 | FieldDecl *Top, FieldDecl *Field) { |
| 1763 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1764 | // Overwhelmingly common case: we have a direct initializer for this field. |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1765 | if (CXXBaseOrMemberInitializer *Init = Info.AllBaseFields.lookup(Field)) { |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1766 | RecordFieldInitializer(Info, Top, Field, Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1767 | return false; |
| 1768 | } |
| 1769 | |
| 1770 | if (Info.IIK == IIK_Default && Field->isAnonymousStructOrUnion()) { |
| 1771 | const RecordType *FieldClassType = Field->getType()->getAs<RecordType>(); |
| 1772 | assert(FieldClassType && "anonymous struct/union without record type"); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1773 | CXXRecordDecl *FieldClassDecl |
| 1774 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1775 | |
| 1776 | // Even though union members never have non-trivial default |
| 1777 | // constructions in C++03, we still build member initializers for aggregate |
| 1778 | // record types which can be union members, and C++0x allows non-trivial |
| 1779 | // default constructors for union members, so we ensure that only one |
| 1780 | // member is initialized for these. |
| 1781 | if (FieldClassDecl->isUnion()) { |
| 1782 | // First check for an explicit initializer for one field. |
| 1783 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 1784 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1785 | if (CXXBaseOrMemberInitializer *Init = Info.AllBaseFields.lookup(*FA)) { |
| 1786 | RecordFieldInitializer(Info, Top, *FA, Init); |
| 1787 | |
| 1788 | // Once we've initialized a field of an anonymous union, the union |
| 1789 | // field in the class is also initialized, so exit immediately. |
| 1790 | return false; |
Argyrios Kyrtzidis | 881b36c | 2010-08-16 17:27:13 +0000 | [diff] [blame] | 1791 | } else if ((*FA)->isAnonymousStructOrUnion()) { |
| 1792 | if (CollectFieldInitializer(Info, Top, *FA)) |
| 1793 | return true; |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | // Fallthrough and construct a default initializer for the union as |
| 1798 | // a whole, which can call its default constructor if such a thing exists |
| 1799 | // (C++0x perhaps). FIXME: It's not clear that this is the correct |
| 1800 | // behavior going forward with C++0x, when anonymous unions there are |
| 1801 | // finalized, we should revisit this. |
| 1802 | } else { |
| 1803 | // For structs, we simply descend through to initialize all members where |
| 1804 | // necessary. |
| 1805 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 1806 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1807 | if (CollectFieldInitializer(Info, Top, *FA)) |
| 1808 | return true; |
| 1809 | } |
| 1810 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | // Don't try to build an implicit initializer if there were semantic |
| 1814 | // errors in any of the initializers (and therefore we might be |
| 1815 | // missing some that the user actually wrote). |
| 1816 | if (Info.AnyErrorsInInits) |
| 1817 | return false; |
| 1818 | |
| 1819 | CXXBaseOrMemberInitializer *Init = 0; |
| 1820 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, Init)) |
| 1821 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1822 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1823 | RecordFieldInitializer(Info, Top, Field, Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1824 | return false; |
| 1825 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1826 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1827 | bool |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1828 | Sema::SetBaseOrMemberInitializers(CXXConstructorDecl *Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1829 | CXXBaseOrMemberInitializer **Initializers, |
| 1830 | unsigned NumInitializers, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1831 | bool AnyErrors) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1832 | if (Constructor->getDeclContext()->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1833 | // Just store the initializers as written, they will be checked during |
| 1834 | // instantiation. |
| 1835 | if (NumInitializers > 0) { |
| 1836 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1837 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1838 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
| 1839 | memcpy(baseOrMemberInitializers, Initializers, |
| 1840 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
| 1841 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
| 1842 | } |
| 1843 | |
| 1844 | return false; |
| 1845 | } |
| 1846 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1847 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1848 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1849 | // We need to build the initializer AST according to order of construction |
| 1850 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1851 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 1852 | if (!ClassDecl) |
| 1853 | return true; |
| 1854 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1855 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1857 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1858 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1859 | |
| 1860 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1861 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1862 | else |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1863 | Info.AllBaseFields[Member->getMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1866 | // Keep track of the direct virtual bases. |
| 1867 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 1868 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 1869 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 1870 | if (I->isVirtual()) |
| 1871 | DirectVBases.insert(I); |
| 1872 | } |
| 1873 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1874 | // Push virtual bases before others. |
| 1875 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 1876 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 1877 | |
| 1878 | if (CXXBaseOrMemberInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1879 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 1880 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1881 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1882 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1883 | CXXBaseOrMemberInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1884 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1885 | VBase, IsInheritedVirtualBase, |
| 1886 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1887 | HadError = true; |
| 1888 | continue; |
| 1889 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1890 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1891 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1892 | } |
| 1893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1895 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1896 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1897 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1898 | // Virtuals are in the virtual base list and already constructed. |
| 1899 | if (Base->isVirtual()) |
| 1900 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1902 | if (CXXBaseOrMemberInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1903 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 1904 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1905 | } else if (!AnyErrors) { |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1906 | CXXBaseOrMemberInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1907 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1908 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1909 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1910 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1911 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1912 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1913 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1914 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1915 | } |
| 1916 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1918 | // Fields. |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1919 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 1920 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 1921 | if ((*Field)->getType()->isIncompleteArrayType()) { |
| 1922 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 1923 | "Incomplete array type is not valid"); |
| 1924 | continue; |
| 1925 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1926 | if (CollectFieldInitializer(Info, *Field, *Field)) |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1927 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 1928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1930 | NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1931 | if (NumInitializers > 0) { |
| 1932 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1933 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1934 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1935 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1936 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1937 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 1938 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1939 | // Constructors implicitly reference the base and member |
| 1940 | // destructors. |
| 1941 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 1942 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1943 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1944 | |
| 1945 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1948 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 1949 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1950 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1951 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 1952 | return static_cast<void *>(RT->getDecl()); |
| 1953 | } |
| 1954 | return static_cast<void *>(Field); |
| 1955 | } |
| 1956 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1957 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 1958 | return Context.getCanonicalType(BaseType).getTypePtr(); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1961 | static void *GetKeyForMember(ASTContext &Context, |
| 1962 | CXXBaseOrMemberInitializer *Member, |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1963 | bool MemberMaybeAnon = false) { |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1964 | if (!Member->isMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1965 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1966 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1967 | // For fields injected into the class via declaration of an anonymous union, |
| 1968 | // use its anonymous union class declaration as the unique key. |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1969 | FieldDecl *Field = Member->getMember(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1971 | // After SetBaseOrMemberInitializers call, Field is the anonymous union |
| 1972 | // data member of the class. Data member used in the initializer list is |
| 1973 | // in AnonUnionMember field. |
| 1974 | if (MemberMaybeAnon && Field->isAnonymousStructOrUnion()) |
| 1975 | Field = Member->getAnonUnionMember(); |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1976 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1977 | // If the field is a member of an anonymous struct or union, our key |
| 1978 | // 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] | 1979 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1980 | if (RD->isAnonymousStructOrUnion()) { |
| 1981 | while (true) { |
| 1982 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 1983 | if (Parent->isAnonymousStructOrUnion()) |
| 1984 | RD = Parent; |
| 1985 | else |
| 1986 | break; |
| 1987 | } |
| 1988 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1989 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1990 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1992 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1995 | static void |
| 1996 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1997 | const CXXConstructorDecl *Constructor, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1998 | CXXBaseOrMemberInitializer **Inits, |
| 1999 | unsigned NumInits) { |
| 2000 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 2001 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2003 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order) |
| 2004 | == Diagnostic::Ignored) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2005 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2006 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2007 | // Build the list of bases and members in the order that they'll |
| 2008 | // actually be initialized. The explicit initializers should be in |
| 2009 | // this same order but may be missing things. |
| 2010 | llvm::SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2012 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2013 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2014 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2015 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2016 | ClassDecl->vbases_begin(), |
| 2017 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2018 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2020 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2021 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2022 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2023 | if (Base->isVirtual()) |
| 2024 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2025 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2026 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2028 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2029 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2030 | E = ClassDecl->field_end(); Field != E; ++Field) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2031 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2033 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 2034 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2035 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2036 | CXXBaseOrMemberInitializer *PrevInit = 0; |
| 2037 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
| 2038 | CXXBaseOrMemberInitializer *Init = Inits[InitIndex]; |
| 2039 | void *InitKey = GetKeyForMember(SemaRef.Context, Init, true); |
| 2040 | |
| 2041 | // Scan forward to try to find this initializer in the idealized |
| 2042 | // initializers list. |
| 2043 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2044 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2045 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2046 | |
| 2047 | // If we didn't find this initializer, it must be because we |
| 2048 | // scanned past it on a previous iteration. That can only |
| 2049 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 2050 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2051 | Sema::SemaDiagnosticBuilder D = |
| 2052 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 2053 | diag::warn_initializer_out_of_order); |
| 2054 | |
| 2055 | if (PrevInit->isMemberInitializer()) |
| 2056 | D << 0 << PrevInit->getMember()->getDeclName(); |
| 2057 | else |
| 2058 | D << 1 << PrevInit->getBaseClassInfo()->getType(); |
| 2059 | |
| 2060 | if (Init->isMemberInitializer()) |
| 2061 | D << 0 << Init->getMember()->getDeclName(); |
| 2062 | else |
| 2063 | D << 1 << Init->getBaseClassInfo()->getType(); |
| 2064 | |
| 2065 | // Move back to the initializer's location in the ideal list. |
| 2066 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2067 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2068 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2069 | |
| 2070 | assert(IdealIndex != NumIdealInits && |
| 2071 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2072 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2073 | |
| 2074 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2075 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2078 | namespace { |
| 2079 | bool CheckRedundantInit(Sema &S, |
| 2080 | CXXBaseOrMemberInitializer *Init, |
| 2081 | CXXBaseOrMemberInitializer *&PrevInit) { |
| 2082 | if (!PrevInit) { |
| 2083 | PrevInit = Init; |
| 2084 | return false; |
| 2085 | } |
| 2086 | |
| 2087 | if (FieldDecl *Field = Init->getMember()) |
| 2088 | S.Diag(Init->getSourceLocation(), |
| 2089 | diag::err_multiple_mem_initialization) |
| 2090 | << Field->getDeclName() |
| 2091 | << Init->getSourceRange(); |
| 2092 | else { |
| 2093 | Type *BaseClass = Init->getBaseClass(); |
| 2094 | assert(BaseClass && "neither field nor base"); |
| 2095 | S.Diag(Init->getSourceLocation(), |
| 2096 | diag::err_multiple_base_initialization) |
| 2097 | << QualType(BaseClass, 0) |
| 2098 | << Init->getSourceRange(); |
| 2099 | } |
| 2100 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 2101 | << 0 << PrevInit->getSourceRange(); |
| 2102 | |
| 2103 | return true; |
| 2104 | } |
| 2105 | |
| 2106 | typedef std::pair<NamedDecl *, CXXBaseOrMemberInitializer *> UnionEntry; |
| 2107 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 2108 | |
| 2109 | bool CheckRedundantUnionInit(Sema &S, |
| 2110 | CXXBaseOrMemberInitializer *Init, |
| 2111 | RedundantUnionMap &Unions) { |
| 2112 | FieldDecl *Field = Init->getMember(); |
| 2113 | RecordDecl *Parent = Field->getParent(); |
| 2114 | if (!Parent->isAnonymousStructOrUnion()) |
| 2115 | return false; |
| 2116 | |
| 2117 | NamedDecl *Child = Field; |
| 2118 | do { |
| 2119 | if (Parent->isUnion()) { |
| 2120 | UnionEntry &En = Unions[Parent]; |
| 2121 | if (En.first && En.first != Child) { |
| 2122 | S.Diag(Init->getSourceLocation(), |
| 2123 | diag::err_multiple_mem_union_initialization) |
| 2124 | << Field->getDeclName() |
| 2125 | << Init->getSourceRange(); |
| 2126 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 2127 | << 0 << En.second->getSourceRange(); |
| 2128 | return true; |
| 2129 | } else if (!En.first) { |
| 2130 | En.first = Child; |
| 2131 | En.second = Init; |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | Child = Parent; |
| 2136 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 2137 | } while (Parent->isAnonymousStructOrUnion()); |
| 2138 | |
| 2139 | return false; |
| 2140 | } |
| 2141 | } |
| 2142 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2143 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2144 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2145 | SourceLocation ColonLoc, |
| 2146 | MemInitTy **meminits, unsigned NumMemInits, |
| 2147 | bool AnyErrors) { |
| 2148 | if (!ConstructorDecl) |
| 2149 | return; |
| 2150 | |
| 2151 | AdjustDeclIfTemplate(ConstructorDecl); |
| 2152 | |
| 2153 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2154 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2155 | |
| 2156 | if (!Constructor) { |
| 2157 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 2158 | return; |
| 2159 | } |
| 2160 | |
| 2161 | CXXBaseOrMemberInitializer **MemInits = |
| 2162 | reinterpret_cast<CXXBaseOrMemberInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2163 | |
| 2164 | // Mapping for the duplicate initializers check. |
| 2165 | // For member initializers, this is keyed with a FieldDecl*. |
| 2166 | // For base initializers, this is keyed with a Type*. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2167 | llvm::DenseMap<void*, CXXBaseOrMemberInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2168 | |
| 2169 | // Mapping for the inconsistent anonymous-union initializers check. |
| 2170 | RedundantUnionMap MemberUnions; |
| 2171 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2172 | bool HadError = false; |
| 2173 | for (unsigned i = 0; i < NumMemInits; i++) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2174 | CXXBaseOrMemberInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2175 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 2176 | // Set the source order index. |
| 2177 | Init->setSourceOrder(i); |
| 2178 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2179 | if (Init->isMemberInitializer()) { |
| 2180 | FieldDecl *Field = Init->getMember(); |
| 2181 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 2182 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 2183 | HadError = true; |
| 2184 | } else { |
| 2185 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 2186 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 2187 | HadError = true; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2188 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2191 | if (HadError) |
| 2192 | return; |
| 2193 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2194 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2195 | |
| 2196 | SetBaseOrMemberInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2199 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2200 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 2201 | CXXRecordDecl *ClassDecl) { |
| 2202 | // Ignore dependent contexts. |
| 2203 | if (ClassDecl->isDependentContext()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2204 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2205 | |
| 2206 | // FIXME: all the access-control diagnostics are positioned on the |
| 2207 | // field/base declaration. That's probably good; that said, the |
| 2208 | // user might reasonably want to know why the destructor is being |
| 2209 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2210 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2211 | // Non-static data members. |
| 2212 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 2213 | E = ClassDecl->field_end(); I != E; ++I) { |
| 2214 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 2215 | if (Field->isInvalidDecl()) |
| 2216 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2217 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 2218 | |
| 2219 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 2220 | if (!RT) |
| 2221 | continue; |
| 2222 | |
| 2223 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 2224 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2225 | continue; |
| 2226 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2227 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2228 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2229 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2230 | << Field->getDeclName() |
| 2231 | << FieldType); |
| 2232 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2233 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2236 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 2237 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2238 | // Bases. |
| 2239 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2240 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2241 | // Bases are always records in a well-formed non-dependent class. |
| 2242 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 2243 | |
| 2244 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2245 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2246 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2247 | |
| 2248 | // Ignore trivial destructors. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2249 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2250 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2251 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2252 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2253 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2254 | |
| 2255 | // FIXME: caret should be on the start of the class name |
| 2256 | CheckDestructorAccess(Base->getSourceRange().getBegin(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2257 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2258 | << Base->getType() |
| 2259 | << Base->getSourceRange()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2260 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2261 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2265 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2266 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2267 | |
| 2268 | // Bases are always records in a well-formed non-dependent class. |
| 2269 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 2270 | |
| 2271 | // Ignore direct virtual bases. |
| 2272 | if (DirectVirtualBases.count(RT)) |
| 2273 | continue; |
| 2274 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2275 | // Ignore trivial destructors. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2276 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2277 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2278 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2279 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2280 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2281 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2282 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2283 | << VBase->getType()); |
| 2284 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2285 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2286 | } |
| 2287 | } |
| 2288 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2289 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2290 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2291 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2294 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2295 | SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2298 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2299 | unsigned DiagID, AbstractDiagSelID SelID) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2300 | if (SelID == -1) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2301 | return RequireNonAbstractType(Loc, T, PDiag(DiagID)); |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2302 | else |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2303 | return RequireNonAbstractType(Loc, T, PDiag(DiagID) << SelID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2306 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2307 | const PartialDiagnostic &PD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2308 | if (!getLangOptions().CPlusPlus) |
| 2309 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2310 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 2311 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2312 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2313 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2314 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2315 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2316 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2317 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2318 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2319 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2320 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2321 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2322 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2323 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2324 | if (!RT) |
| 2325 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2326 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2327 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2328 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2329 | // We can't answer whether something is abstract until it has a |
| 2330 | // definition. If it's currently being defined, we'll walk back |
| 2331 | // over all the declarations when we have a full definition. |
| 2332 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 2333 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2334 | return false; |
| 2335 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2336 | if (!RD->isAbstract()) |
| 2337 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2338 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2339 | Diag(Loc, PD) << RD->getDeclName(); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2340 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2341 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2342 | return true; |
| 2343 | } |
| 2344 | |
| 2345 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 2346 | // Check if we've already emitted the list of pure virtual functions |
| 2347 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2348 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2349 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2350 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2351 | CXXFinalOverriderMap FinalOverriders; |
| 2352 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2354 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 2355 | // more than once. |
| 2356 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 2357 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2358 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2359 | MEnd = FinalOverriders.end(); |
| 2360 | M != MEnd; |
| 2361 | ++M) { |
| 2362 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2363 | SOEnd = M->second.end(); |
| 2364 | SO != SOEnd; ++SO) { |
| 2365 | // C++ [class.abstract]p4: |
| 2366 | // A class is abstract if it contains or inherits at least one |
| 2367 | // pure virtual function for which the final overrider is pure |
| 2368 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2370 | // |
| 2371 | if (SO->second.size() != 1) |
| 2372 | continue; |
| 2373 | |
| 2374 | if (!SO->second.front().Method->isPure()) |
| 2375 | continue; |
| 2376 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2377 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 2378 | continue; |
| 2379 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2380 | Diag(SO->second.front().Method->getLocation(), |
| 2381 | diag::note_pure_virtual_function) |
| 2382 | << SO->second.front().Method->getDeclName(); |
| 2383 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | if (!PureVirtualClassDiagSet) |
| 2387 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 2388 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2391 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2392 | struct AbstractUsageInfo { |
| 2393 | Sema &S; |
| 2394 | CXXRecordDecl *Record; |
| 2395 | CanQualType AbstractType; |
| 2396 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2397 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2398 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 2399 | : S(S), Record(Record), |
| 2400 | AbstractType(S.Context.getCanonicalType( |
| 2401 | S.Context.getTypeDeclType(Record))), |
| 2402 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2403 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2404 | void DiagnoseAbstractType() { |
| 2405 | if (Invalid) return; |
| 2406 | S.DiagnoseAbstractType(Record); |
| 2407 | Invalid = true; |
| 2408 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2409 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2410 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 2411 | }; |
| 2412 | |
| 2413 | struct CheckAbstractUsage { |
| 2414 | AbstractUsageInfo &Info; |
| 2415 | const NamedDecl *Ctx; |
| 2416 | |
| 2417 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 2418 | : Info(Info), Ctx(Ctx) {} |
| 2419 | |
| 2420 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2421 | switch (TL.getTypeLocClass()) { |
| 2422 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 2423 | #define TYPELOC(CLASS, PARENT) \ |
| 2424 | case TypeLoc::CLASS: Check(cast<CLASS##TypeLoc>(TL), Sel); break; |
| 2425 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2426 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2427 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2429 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2430 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 2431 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2432 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 2433 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2434 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2435 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2436 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2437 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2438 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 2439 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2440 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2441 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2442 | // Visit the type parameters from a permissive context. |
| 2443 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2444 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 2445 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 2446 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 2447 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 2448 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2449 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2450 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2451 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2452 | // Visit pointee types from a permissive context. |
| 2453 | #define CheckPolymorphic(Type) \ |
| 2454 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 2455 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 2456 | } |
| 2457 | CheckPolymorphic(PointerTypeLoc) |
| 2458 | CheckPolymorphic(ReferenceTypeLoc) |
| 2459 | CheckPolymorphic(MemberPointerTypeLoc) |
| 2460 | CheckPolymorphic(BlockPointerTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2461 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2462 | /// Handle all the types we haven't given a more specific |
| 2463 | /// implementation for above. |
| 2464 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2465 | // Every other kind of type that we haven't called out already |
| 2466 | // that has an inner type is either (1) sugar or (2) contains that |
| 2467 | // inner type in some way as a subobject. |
| 2468 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 2469 | return Visit(Next, Sel); |
| 2470 | |
| 2471 | // If there's no inner type and we're in a permissive context, |
| 2472 | // don't diagnose. |
| 2473 | if (Sel == Sema::AbstractNone) return; |
| 2474 | |
| 2475 | // Check whether the type matches the abstract type. |
| 2476 | QualType T = TL.getType(); |
| 2477 | if (T->isArrayType()) { |
| 2478 | Sel = Sema::AbstractArrayType; |
| 2479 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2480 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2481 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 2482 | if (CT != Info.AbstractType) return; |
| 2483 | |
| 2484 | // It matched; do some magic. |
| 2485 | if (Sel == Sema::AbstractArrayType) { |
| 2486 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 2487 | << T << TL.getSourceRange(); |
| 2488 | } else { |
| 2489 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 2490 | << Sel << T << TL.getSourceRange(); |
| 2491 | } |
| 2492 | Info.DiagnoseAbstractType(); |
| 2493 | } |
| 2494 | }; |
| 2495 | |
| 2496 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 2497 | Sema::AbstractDiagSelID Sel) { |
| 2498 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 2499 | } |
| 2500 | |
| 2501 | } |
| 2502 | |
| 2503 | /// Check for invalid uses of an abstract type in a method declaration. |
| 2504 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2505 | CXXMethodDecl *MD) { |
| 2506 | // No need to do the check on definitions, which require that |
| 2507 | // the return/param types be complete. |
| 2508 | if (MD->isThisDeclarationADefinition()) |
| 2509 | return; |
| 2510 | |
| 2511 | // For safety's sake, just ignore it if we don't have type source |
| 2512 | // information. This should never happen for non-implicit methods, |
| 2513 | // but... |
| 2514 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 2515 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 2516 | } |
| 2517 | |
| 2518 | /// Check for invalid uses of an abstract type within a class definition. |
| 2519 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2520 | CXXRecordDecl *RD) { |
| 2521 | for (CXXRecordDecl::decl_iterator |
| 2522 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 2523 | Decl *D = *I; |
| 2524 | if (D->isImplicit()) continue; |
| 2525 | |
| 2526 | // Methods and method templates. |
| 2527 | if (isa<CXXMethodDecl>(D)) { |
| 2528 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 2529 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 2530 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 2531 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 2532 | |
| 2533 | // Fields and static variables. |
| 2534 | } else if (isa<FieldDecl>(D)) { |
| 2535 | FieldDecl *FD = cast<FieldDecl>(D); |
| 2536 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 2537 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 2538 | } else if (isa<VarDecl>(D)) { |
| 2539 | VarDecl *VD = cast<VarDecl>(D); |
| 2540 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 2541 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 2542 | |
| 2543 | // Nested classes and class templates. |
| 2544 | } else if (isa<CXXRecordDecl>(D)) { |
| 2545 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 2546 | } else if (isa<ClassTemplateDecl>(D)) { |
| 2547 | CheckAbstractClassUsage(Info, |
| 2548 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 2549 | } |
| 2550 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2553 | /// \brief Perform semantic checks on a class definition that has been |
| 2554 | /// completing, introducing implicitly-declared members, checking for |
| 2555 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2556 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 2557 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2558 | return; |
| 2559 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2560 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 2561 | AbstractUsageInfo Info(*this, Record); |
| 2562 | CheckAbstractClassUsage(Info, Record); |
| 2563 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2564 | |
| 2565 | // If this is not an aggregate type and has no user-declared constructor, |
| 2566 | // complain about any non-static data members of reference or const scalar |
| 2567 | // type, since they will never get initializers. |
| 2568 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 2569 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor()) { |
| 2570 | bool Complained = false; |
| 2571 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 2572 | FEnd = Record->field_end(); |
| 2573 | F != FEnd; ++F) { |
| 2574 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 2575 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2576 | if (!Complained) { |
| 2577 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 2578 | << Record->getTagKind() << Record; |
| 2579 | Complained = true; |
| 2580 | } |
| 2581 | |
| 2582 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 2583 | << F->getType()->isReferenceType() |
| 2584 | << F->getDeclName(); |
| 2585 | } |
| 2586 | } |
| 2587 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2588 | |
| 2589 | if (Record->isDynamicClass()) |
| 2590 | DynamicClasses.push_back(Record); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 2591 | |
| 2592 | if (Record->getIdentifier()) { |
| 2593 | // C++ [class.mem]p13: |
| 2594 | // If T is the name of a class, then each of the following shall have a |
| 2595 | // name different from T: |
| 2596 | // - every member of every anonymous union that is a member of class T. |
| 2597 | // |
| 2598 | // C++ [class.mem]p14: |
| 2599 | // In addition, if class T has a user-declared constructor (12.1), every |
| 2600 | // non-static data member of class T shall have a name different from T. |
| 2601 | for (DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
| 2602 | R.first != R.second; ++R.first) |
| 2603 | if (FieldDecl *Field = dyn_cast<FieldDecl>(*R.first)) { |
| 2604 | if (Record->hasUserDeclaredConstructor() || |
| 2605 | !Field->getDeclContext()->Equals(Record)) { |
| 2606 | Diag(Field->getLocation(), diag::err_member_name_of_class) |
| 2607 | << Field->getDeclName(); |
| 2608 | break; |
| 2609 | } |
| 2610 | } |
| 2611 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2614 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2615 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2616 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2617 | SourceLocation RBrac, |
| 2618 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2619 | if (!TagDecl) |
| 2620 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2621 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2622 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2623 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2624 | ActOnFields(S, RLoc, TagDecl, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2625 | // strict aliasing violation! |
| 2626 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2627 | FieldCollector->getCurNumFields(), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2628 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2629 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2630 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2633 | namespace { |
| 2634 | /// \brief Helper class that collects exception specifications for |
| 2635 | /// implicitly-declared special member functions. |
| 2636 | class ImplicitExceptionSpecification { |
| 2637 | ASTContext &Context; |
| 2638 | bool AllowsAllExceptions; |
| 2639 | llvm::SmallPtrSet<CanQualType, 4> ExceptionsSeen; |
| 2640 | llvm::SmallVector<QualType, 4> Exceptions; |
| 2641 | |
| 2642 | public: |
| 2643 | explicit ImplicitExceptionSpecification(ASTContext &Context) |
| 2644 | : Context(Context), AllowsAllExceptions(false) { } |
| 2645 | |
| 2646 | /// \brief Whether the special member function should have any |
| 2647 | /// exception specification at all. |
| 2648 | bool hasExceptionSpecification() const { |
| 2649 | return !AllowsAllExceptions; |
| 2650 | } |
| 2651 | |
| 2652 | /// \brief Whether the special member function should have a |
| 2653 | /// throw(...) exception specification (a Microsoft extension). |
| 2654 | bool hasAnyExceptionSpecification() const { |
| 2655 | return false; |
| 2656 | } |
| 2657 | |
| 2658 | /// \brief The number of exceptions in the exception specification. |
| 2659 | unsigned size() const { return Exceptions.size(); } |
| 2660 | |
| 2661 | /// \brief The set of exceptions in the exception specification. |
| 2662 | const QualType *data() const { return Exceptions.data(); } |
| 2663 | |
| 2664 | /// \brief Note that |
| 2665 | void CalledDecl(CXXMethodDecl *Method) { |
| 2666 | // If we already know that we allow all exceptions, do nothing. |
Douglas Gregor | 4681ca8 | 2010-07-01 15:29:53 +0000 | [diff] [blame] | 2667 | if (AllowsAllExceptions || !Method) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2668 | return; |
| 2669 | |
| 2670 | const FunctionProtoType *Proto |
| 2671 | = Method->getType()->getAs<FunctionProtoType>(); |
| 2672 | |
| 2673 | // If this function can throw any exceptions, make a note of that. |
| 2674 | if (!Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec()) { |
| 2675 | AllowsAllExceptions = true; |
| 2676 | ExceptionsSeen.clear(); |
| 2677 | Exceptions.clear(); |
| 2678 | return; |
| 2679 | } |
| 2680 | |
| 2681 | // Record the exceptions in this function's exception specification. |
| 2682 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 2683 | EEnd = Proto->exception_end(); |
| 2684 | E != EEnd; ++E) |
| 2685 | if (ExceptionsSeen.insert(Context.getCanonicalType(*E))) |
| 2686 | Exceptions.push_back(*E); |
| 2687 | } |
| 2688 | }; |
| 2689 | } |
| 2690 | |
| 2691 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2692 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 2693 | /// special functions, such as the default constructor, copy |
| 2694 | /// constructor, or destructor, to the given C++ class (C++ |
| 2695 | /// [special]p1). This routine can only be executed just before the |
| 2696 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2697 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 2698 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 2699 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2700 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 2701 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 2702 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2703 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 2704 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 2705 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
| 2706 | |
| 2707 | // If we have a dynamic class, then the copy assignment operator may be |
| 2708 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
| 2709 | // it shows up in the right place in the vtable and that we diagnose |
| 2710 | // problems with the implicit exception specification. |
| 2711 | if (ClassDecl->isDynamicClass()) |
| 2712 | DeclareImplicitCopyAssignment(ClassDecl); |
| 2713 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2714 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 2715 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 2716 | ++ASTContext::NumImplicitDestructors; |
| 2717 | |
| 2718 | // If we have a dynamic class, then the destructor may be virtual, so we |
| 2719 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 2720 | // shows up in the right place in the vtable and that we diagnose problems |
| 2721 | // with the implicit exception specification. |
| 2722 | if (ClassDecl->isDynamicClass()) |
| 2723 | DeclareImplicitDestructor(ClassDecl); |
| 2724 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2727 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 2728 | if (!D) |
| 2729 | return; |
| 2730 | |
| 2731 | TemplateParameterList *Params = 0; |
| 2732 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 2733 | Params = Template->getTemplateParameters(); |
| 2734 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2735 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 2736 | Params = PartialSpec->getTemplateParameters(); |
| 2737 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2738 | return; |
| 2739 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2740 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2741 | ParamEnd = Params->end(); |
| 2742 | Param != ParamEnd; ++Param) { |
| 2743 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 2744 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2745 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2746 | IdResolver.AddDecl(Named); |
| 2747 | } |
| 2748 | } |
| 2749 | } |
| 2750 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2751 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2752 | if (!RecordD) return; |
| 2753 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2754 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2755 | PushDeclContext(S, Record); |
| 2756 | } |
| 2757 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2758 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2759 | if (!RecordD) return; |
| 2760 | PopDeclContext(); |
| 2761 | } |
| 2762 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2763 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 2764 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 2765 | /// parsing those parts of the given Method declaration that could |
| 2766 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 2767 | /// arguments. This action should enter the scope of the given |
| 2768 | /// Method declaration as if we had just parsed the qualified method |
| 2769 | /// name. However, it should not bring the parameters into scope; |
| 2770 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2771 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 2775 | /// C++ method declaration. We're (re-)introducing the given |
| 2776 | /// function parameter into scope for use in parsing later parts of |
| 2777 | /// the method declaration. For example, we could see an |
| 2778 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2779 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2780 | if (!ParamD) |
| 2781 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2783 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2784 | |
| 2785 | // If this parameter has an unparsed default argument, clear it out |
| 2786 | // to make way for the parsed default argument. |
| 2787 | if (Param->hasUnparsedDefaultArg()) |
| 2788 | Param->setDefaultArg(0); |
| 2789 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2790 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2791 | if (Param->getDeclName()) |
| 2792 | IdResolver.AddDecl(Param); |
| 2793 | } |
| 2794 | |
| 2795 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 2796 | /// processing the delayed method declaration for Method. The method |
| 2797 | /// declaration is now considered finished. There may be a separate |
| 2798 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 2799 | /// immediately!) for this method, if it was also defined inside the |
| 2800 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2801 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2802 | if (!MethodD) |
| 2803 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2805 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2806 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2807 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2808 | |
| 2809 | // Now that we have our default arguments, check the constructor |
| 2810 | // again. It could produce additional diagnostics or affect whether |
| 2811 | // the class has implicitly-declared destructors, among other |
| 2812 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2813 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 2814 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2815 | |
| 2816 | // Check the default arguments, which we may have added. |
| 2817 | if (!Method->isInvalidDecl()) |
| 2818 | CheckCXXDefaultArguments(Method); |
| 2819 | } |
| 2820 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2821 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2822 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2823 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2824 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 2825 | /// will be updated to reflect a well-formed type for the constructor and |
| 2826 | /// returned. |
| 2827 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2828 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2829 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2830 | |
| 2831 | // C++ [class.ctor]p3: |
| 2832 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 2833 | // constructor can be invoked for a const, volatile or const |
| 2834 | // volatile object. A constructor shall not be declared const, |
| 2835 | // volatile, or const volatile (9.3.2). |
| 2836 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2837 | if (!D.isInvalidType()) |
| 2838 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2839 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 2840 | << SourceRange(D.getIdentifierLoc()); |
| 2841 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2842 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2843 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2844 | if (!D.isInvalidType()) |
| 2845 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2846 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2847 | << SourceRange(D.getIdentifierLoc()); |
| 2848 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2849 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2850 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2852 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2853 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2854 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2855 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2856 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2857 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2858 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2859 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2860 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2861 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2862 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2863 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2864 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2865 | // Rebuild the function type "R" without any type qualifiers (in |
| 2866 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2867 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2868 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2869 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 2870 | Proto->getNumArgs(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2871 | Proto->isVariadic(), 0, |
| 2872 | Proto->hasExceptionSpec(), |
| 2873 | Proto->hasAnyExceptionSpec(), |
| 2874 | Proto->getNumExceptions(), |
| 2875 | Proto->exception_begin(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2876 | Proto->getExtInfo()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2879 | /// CheckConstructor - Checks a fully-formed constructor for |
| 2880 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 2881 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2882 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2883 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2884 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 2885 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2886 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2887 | |
| 2888 | // C++ [class.copy]p3: |
| 2889 | // A declaration of a constructor for a class X is ill-formed if |
| 2890 | // its first parameter is of type (optionally cv-qualified) X and |
| 2891 | // either there are no other parameters or else all other |
| 2892 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2893 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2894 | ((Constructor->getNumParams() == 1) || |
| 2895 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2896 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 2897 | Constructor->getTemplateSpecializationKind() |
| 2898 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2899 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 2900 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 2901 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2902 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 2903 | const char *ConstRef |
| 2904 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 2905 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2906 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 2907 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2908 | |
| 2909 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 2910 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2911 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2912 | } |
| 2913 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 2916 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 2917 | /// well-formedness, issuing any diagnostics required. Returns true |
| 2918 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2919 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2920 | CXXRecordDecl *RD = Destructor->getParent(); |
| 2921 | |
| 2922 | if (Destructor->isVirtual()) { |
| 2923 | SourceLocation Loc; |
| 2924 | |
| 2925 | if (!Destructor->isImplicit()) |
| 2926 | Loc = Destructor->getLocation(); |
| 2927 | else |
| 2928 | Loc = RD->getLocation(); |
| 2929 | |
| 2930 | // If we have a virtual destructor, look up the deallocation function |
| 2931 | FunctionDecl *OperatorDelete = 0; |
| 2932 | DeclarationName Name = |
| 2933 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2934 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2935 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 2936 | |
| 2937 | MarkDeclarationReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2938 | |
| 2939 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2940 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2941 | |
| 2942 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2945 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2946 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 2947 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 2948 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2949 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2952 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 2953 | /// the well-formednes of the destructor declarator @p D with type @p |
| 2954 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2955 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 2956 | /// will be updated to reflect a well-formed type for the destructor and |
| 2957 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2958 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2959 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2960 | // C++ [class.dtor]p1: |
| 2961 | // [...] A typedef-name that names a class is a class-name |
| 2962 | // (7.1.3); however, a typedef-name that names a class shall not |
| 2963 | // be used as the identifier in the declarator for a destructor |
| 2964 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2965 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2966 | if (isa<TypedefType>(DeclaratorType)) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2967 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2968 | << DeclaratorType; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2969 | |
| 2970 | // C++ [class.dtor]p2: |
| 2971 | // A destructor is used to destroy objects of its class type. A |
| 2972 | // destructor takes no parameters, and no return type can be |
| 2973 | // specified for it (not even void). The address of a destructor |
| 2974 | // shall not be taken. A destructor shall not be static. A |
| 2975 | // destructor can be invoked for a const, volatile or const |
| 2976 | // volatile object. A destructor shall not be declared const, |
| 2977 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2978 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2979 | if (!D.isInvalidType()) |
| 2980 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 2981 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2982 | << SourceRange(D.getIdentifierLoc()) |
| 2983 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 2984 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2985 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2986 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2987 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2988 | // Destructors don't have return types, but the parser will |
| 2989 | // happily parse something like: |
| 2990 | // |
| 2991 | // class X { |
| 2992 | // float ~X(); |
| 2993 | // }; |
| 2994 | // |
| 2995 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2996 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 2997 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2998 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2999 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3000 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3001 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 3002 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3003 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3004 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 3005 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3006 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3007 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 3008 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3009 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3010 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 3011 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3012 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 3016 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3017 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 3018 | |
| 3019 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3020 | FTI.freeArgs(); |
| 3021 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3024 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3025 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3026 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3027 | D.setInvalidType(); |
| 3028 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3029 | |
| 3030 | // Rebuild the function type "R" without any type qualifiers or |
| 3031 | // parameters (in case any of the errors above fired) and with |
| 3032 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3033 | // types. |
| 3034 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 3035 | if (!Proto) |
| 3036 | return QualType(); |
| 3037 | |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 3038 | return Context.getFunctionType(Context.VoidTy, 0, 0, false, 0, |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3039 | Proto->hasExceptionSpec(), |
| 3040 | Proto->hasAnyExceptionSpec(), |
| 3041 | Proto->getNumExceptions(), |
| 3042 | Proto->exception_begin(), |
| 3043 | Proto->getExtInfo()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3046 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 3047 | /// well-formednes of the conversion function declarator @p D with |
| 3048 | /// type @p R. If there are any errors in the declarator, this routine |
| 3049 | /// will emit diagnostics and return true. Otherwise, it will return |
| 3050 | /// false. Either way, the type @p R will be updated to reflect a |
| 3051 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3052 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3053 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3054 | // C++ [class.conv.fct]p1: |
| 3055 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3056 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3057 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3058 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3059 | if (!D.isInvalidType()) |
| 3060 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 3061 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 3062 | << SourceRange(D.getIdentifierLoc()); |
| 3063 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3064 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3065 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3066 | |
| 3067 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 3068 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3069 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3070 | // Conversion functions don't have return types, but the parser will |
| 3071 | // happily parse something like: |
| 3072 | // |
| 3073 | // class X { |
| 3074 | // float operator bool(); |
| 3075 | // }; |
| 3076 | // |
| 3077 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3078 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 3079 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 3080 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3081 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3084 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 3085 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3086 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3087 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3088 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 3089 | |
| 3090 | // Delete the parameters. |
Chris Lattner | 1833a83 | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 3091 | D.getTypeObject(0).Fun.freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3092 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3093 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3094 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3095 | D.setInvalidType(); |
| 3096 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3097 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3098 | // Diagnose "&operator bool()" and other such nonsense. This |
| 3099 | // is actually a gcc extension which we don't support. |
| 3100 | if (Proto->getResultType() != ConvType) { |
| 3101 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 3102 | << Proto->getResultType(); |
| 3103 | D.setInvalidType(); |
| 3104 | ConvType = Proto->getResultType(); |
| 3105 | } |
| 3106 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3107 | // C++ [class.conv.fct]p4: |
| 3108 | // The conversion-type-id shall not represent a function type nor |
| 3109 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3110 | if (ConvType->isArrayType()) { |
| 3111 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 3112 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3113 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3114 | } else if (ConvType->isFunctionType()) { |
| 3115 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 3116 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3117 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | // Rebuild the function type "R" without any parameters (in case any |
| 3121 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3122 | // return type. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3123 | if (D.isInvalidType()) { |
| 3124 | R = Context.getFunctionType(ConvType, 0, 0, false, |
| 3125 | Proto->getTypeQuals(), |
| 3126 | Proto->hasExceptionSpec(), |
| 3127 | Proto->hasAnyExceptionSpec(), |
| 3128 | Proto->getNumExceptions(), |
| 3129 | Proto->exception_begin(), |
| 3130 | Proto->getExtInfo()); |
| 3131 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3132 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3133 | // C++0x explicit conversion operators. |
| 3134 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3135 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3136 | diag::warn_explicit_conversion_functions) |
| 3137 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3140 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 3141 | /// the declaration of the given C++ conversion function. This routine |
| 3142 | /// is responsible for recording the conversion function in the C++ |
| 3143 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3144 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3145 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 3146 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 3147 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3148 | |
| 3149 | // Make sure we aren't redeclaring the conversion function. |
| 3150 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3151 | |
| 3152 | // C++ [class.conv.fct]p1: |
| 3153 | // [...] A conversion function is never used to convert a |
| 3154 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 3155 | // same object type (or a reference to it), to a (possibly |
| 3156 | // cv-qualified) base class of that type (or a reference to it), |
| 3157 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3158 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 3159 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3160 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3161 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3162 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3163 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 3164 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 3165 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 3166 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 3167 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3168 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 3169 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3170 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3171 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3172 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3173 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3174 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3175 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3176 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3177 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 3180 | if (FunctionTemplateDecl *ConversionTemplate |
| 3181 | = Conversion->getDescribedFunctionTemplate()) |
| 3182 | return ConversionTemplate; |
| 3183 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3184 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3187 | //===----------------------------------------------------------------------===// |
| 3188 | // Namespace Handling |
| 3189 | //===----------------------------------------------------------------------===// |
| 3190 | |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3191 | |
| 3192 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3193 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 3194 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3195 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 3196 | SourceLocation InlineLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3197 | SourceLocation IdentLoc, |
| 3198 | IdentifierInfo *II, |
| 3199 | SourceLocation LBrace, |
| 3200 | AttributeList *AttrList) { |
Douglas Gregor | 21e09b6 | 2010-08-19 20:55:47 +0000 | [diff] [blame] | 3201 | // anonymous namespace starts at its left brace |
| 3202 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, |
| 3203 | (II ? IdentLoc : LBrace) , II); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3204 | Namespc->setLBracLoc(LBrace); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3205 | Namespc->setInline(InlineLoc.isValid()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3206 | |
| 3207 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 3208 | |
Anders Carlsson | 2a3503d | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 3209 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 3210 | |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3211 | if (const VisibilityAttr *attr = Namespc->getAttr<VisibilityAttr>()) |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3212 | PushVisibilityAttr(attr); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3213 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3214 | if (II) { |
| 3215 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 3216 | // The identifier in an original-namespace-definition shall not |
| 3217 | // have been previously defined in the declarative region in |
| 3218 | // which the original-namespace-definition appears. The |
| 3219 | // identifier in an original-namespace-definition is the name of |
| 3220 | // the namespace. Subsequently in that declarative region, it is |
| 3221 | // treated as an original-namespace-name. |
| 3222 | // |
| 3223 | // Since namespace names are unique in their scope, and we don't |
| 3224 | // look through using directives, just |
| 3225 | DeclContext::lookup_result R = CurContext->getRedeclContext()->lookup(II); |
| 3226 | NamedDecl *PrevDecl = R.first == R.second? 0 : *R.first; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3228 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 3229 | // This is an extended namespace definition. |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3230 | if (Namespc->isInline() != OrigNS->isInline()) { |
| 3231 | // inline-ness must match |
| 3232 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 3233 | << Namespc->isInline(); |
| 3234 | Diag(OrigNS->getLocation(), diag::note_previous_definition); |
| 3235 | Namespc->setInvalidDecl(); |
| 3236 | // Recover by ignoring the new namespace's inline status. |
| 3237 | Namespc->setInline(OrigNS->isInline()); |
| 3238 | } |
| 3239 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3240 | // Attach this namespace decl to the chain of extended namespace |
| 3241 | // definitions. |
| 3242 | OrigNS->setNextNamespace(Namespc); |
| 3243 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3244 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | // Remove the previous declaration from the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3246 | if (DeclRegionScope->isDeclScope(OrigNS)) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 3247 | IdResolver.RemoveDecl(OrigNS); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3248 | DeclRegionScope->RemoveDecl(OrigNS); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3249 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3250 | } else if (PrevDecl) { |
| 3251 | // This is an invalid name redefinition. |
| 3252 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 3253 | << Namespc->getDeclName(); |
| 3254 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3255 | Namespc->setInvalidDecl(); |
| 3256 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3257 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3258 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3259 | // This is the first "real" definition of the namespace "std", so update |
| 3260 | // our cache of the "std" namespace to point at this definition. |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3261 | if (NamespaceDecl *StdNS = getStdNamespace()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3262 | // We had already defined a dummy namespace "std". Link this new |
| 3263 | // namespace definition to the dummy namespace "std". |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3264 | StdNS->setNextNamespace(Namespc); |
| 3265 | StdNS->setLocation(IdentLoc); |
| 3266 | Namespc->setOriginalNamespace(StdNS->getOriginalNamespace()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3267 | } |
| 3268 | |
| 3269 | // Make our StdNamespace cache point at the first real definition of the |
| 3270 | // "std" namespace. |
| 3271 | StdNamespace = Namespc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3272 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3273 | |
| 3274 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 3275 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3276 | // Anonymous namespaces. |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3277 | assert(Namespc->isAnonymousNamespace()); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3278 | |
| 3279 | // Link the anonymous namespace into its parent. |
| 3280 | NamespaceDecl *PrevDecl; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3281 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3282 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 3283 | PrevDecl = TU->getAnonymousNamespace(); |
| 3284 | TU->setAnonymousNamespace(Namespc); |
| 3285 | } else { |
| 3286 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 3287 | PrevDecl = ND->getAnonymousNamespace(); |
| 3288 | ND->setAnonymousNamespace(Namespc); |
| 3289 | } |
| 3290 | |
| 3291 | // Link the anonymous namespace with its previous declaration. |
| 3292 | if (PrevDecl) { |
| 3293 | assert(PrevDecl->isAnonymousNamespace()); |
| 3294 | assert(!PrevDecl->getNextNamespace()); |
| 3295 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 3296 | PrevDecl->setNextNamespace(Namespc); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3297 | |
| 3298 | if (Namespc->isInline() != PrevDecl->isInline()) { |
| 3299 | // inline-ness must match |
| 3300 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 3301 | << Namespc->isInline(); |
| 3302 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3303 | Namespc->setInvalidDecl(); |
| 3304 | // Recover by ignoring the new namespace's inline status. |
| 3305 | Namespc->setInline(PrevDecl->isInline()); |
| 3306 | } |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3307 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3308 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 3309 | CurContext->addDecl(Namespc); |
| 3310 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3311 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 3312 | // behaves as if it were replaced by |
| 3313 | // namespace unique { /* empty body */ } |
| 3314 | // using namespace unique; |
| 3315 | // namespace unique { namespace-body } |
| 3316 | // where all occurrences of 'unique' in a translation unit are |
| 3317 | // replaced by the same identifier and this identifier differs |
| 3318 | // from all other identifiers in the entire program. |
| 3319 | |
| 3320 | // We just create the namespace with an empty name and then add an |
| 3321 | // implicit using declaration, just like the standard suggests. |
| 3322 | // |
| 3323 | // CodeGen enforces the "universally unique" aspect by giving all |
| 3324 | // declarations semantically contained within an anonymous |
| 3325 | // namespace internal linkage. |
| 3326 | |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3327 | if (!PrevDecl) { |
| 3328 | UsingDirectiveDecl* UD |
| 3329 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 3330 | /* 'using' */ LBrace, |
| 3331 | /* 'namespace' */ SourceLocation(), |
| 3332 | /* qualifier */ SourceRange(), |
| 3333 | /* NNS */ NULL, |
| 3334 | /* identifier */ SourceLocation(), |
| 3335 | Namespc, |
| 3336 | /* Ancestor */ CurContext); |
| 3337 | UD->setImplicit(); |
| 3338 | CurContext->addDecl(UD); |
| 3339 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 3343 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3344 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 3345 | // for the namespace has the declarations that showed up in that particular |
| 3346 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3347 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3348 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3351 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 3352 | /// is a namespace alias, returns the namespace it points to. |
| 3353 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 3354 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 3355 | return AD->getNamespace(); |
| 3356 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 3357 | } |
| 3358 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3359 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 3360 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3361 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3362 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 3363 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 3364 | Namespc->setRBracLoc(RBrace); |
| 3365 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3366 | if (Namespc->hasAttr<VisibilityAttr>()) |
| 3367 | PopPragmaVisibility(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3368 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3369 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 3370 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 3371 | return cast_or_null<CXXRecordDecl>( |
| 3372 | StdBadAlloc.get(Context.getExternalSource())); |
| 3373 | } |
| 3374 | |
| 3375 | NamespaceDecl *Sema::getStdNamespace() const { |
| 3376 | return cast_or_null<NamespaceDecl>( |
| 3377 | StdNamespace.get(Context.getExternalSource())); |
| 3378 | } |
| 3379 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3380 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 3381 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 3382 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3383 | if (!StdNamespace) { |
| 3384 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 3385 | StdNamespace = NamespaceDecl::Create(Context, |
| 3386 | Context.getTranslationUnitDecl(), |
| 3387 | SourceLocation(), |
| 3388 | &PP.getIdentifierTable().get("std")); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3389 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3392 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3395 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3396 | SourceLocation UsingLoc, |
| 3397 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3398 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3399 | SourceLocation IdentLoc, |
| 3400 | IdentifierInfo *NamespcName, |
| 3401 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3402 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3403 | assert(NamespcName && "Invalid NamespcName."); |
| 3404 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 3405 | |
| 3406 | // This can only happen along a recovery path. |
| 3407 | while (S->getFlags() & Scope::TemplateParamScope) |
| 3408 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3409 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3410 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3411 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3412 | NestedNameSpecifier *Qualifier = 0; |
| 3413 | if (SS.isSet()) |
| 3414 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3415 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 3416 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3417 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 3418 | LookupParsedName(R, S, &SS); |
| 3419 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3420 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3421 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3422 | if (R.empty()) { |
| 3423 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 3424 | // "std" hasn't been defined yet, for GCC compatibility. |
| 3425 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 3426 | NamespcName->isStr("std")) { |
| 3427 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 3428 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3429 | R.resolveKind(); |
| 3430 | } |
| 3431 | // Otherwise, attempt typo correction. |
| 3432 | else if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 3433 | CTC_NoKeywords, 0)) { |
| 3434 | if (R.getAsSingle<NamespaceDecl>() || |
| 3435 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 3436 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 3437 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 3438 | << NamespcName << DC << Corrected << SS.getRange() |
| 3439 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 3440 | else |
| 3441 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 3442 | << NamespcName << Corrected |
| 3443 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 3444 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 3445 | << Corrected; |
| 3446 | |
| 3447 | NamespcName = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3448 | } else { |
| 3449 | R.clear(); |
| 3450 | R.setLookupName(NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3451 | } |
| 3452 | } |
| 3453 | } |
| 3454 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3455 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3456 | NamedDecl *Named = R.getFoundDecl(); |
| 3457 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 3458 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3459 | // C++ [namespace.udir]p1: |
| 3460 | // A using-directive specifies that the names in the nominated |
| 3461 | // namespace can be used in the scope in which the |
| 3462 | // using-directive appears after the using-directive. During |
| 3463 | // unqualified name lookup (3.4.1), the names appear as if they |
| 3464 | // were declared in the nearest enclosing namespace which |
| 3465 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3466 | // namespace. [Note: in this context, "contains" means "contains |
| 3467 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3468 | |
| 3469 | // Find enclosing context containing both using-directive and |
| 3470 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3471 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3472 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 3473 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 3474 | CommonAncestor = CommonAncestor->getParent(); |
| 3475 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3476 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 3477 | SS.getRange(), |
| 3478 | (NestedNameSpecifier *)SS.getScopeRep(), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3479 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3480 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3481 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 3482 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3485 | // FIXME: We ignore attributes for now. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3486 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3487 | } |
| 3488 | |
| 3489 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 3490 | // If scope has associated entity, then using directive is at namespace |
| 3491 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 3492 | // it's lookup structure. |
| 3493 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3494 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3495 | else |
| 3496 | // Otherwise it is block-sope. using-directives will affect lookup |
| 3497 | // only to the end of scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3498 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3499 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3500 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3501 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3502 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 3503 | AccessSpecifier AS, |
| 3504 | bool HasUsingKeyword, |
| 3505 | SourceLocation UsingLoc, |
| 3506 | CXXScopeSpec &SS, |
| 3507 | UnqualifiedId &Name, |
| 3508 | AttributeList *AttrList, |
| 3509 | bool IsTypeName, |
| 3510 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3511 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3512 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3513 | switch (Name.getKind()) { |
| 3514 | case UnqualifiedId::IK_Identifier: |
| 3515 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3516 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3517 | case UnqualifiedId::IK_ConversionFunctionId: |
| 3518 | break; |
| 3519 | |
| 3520 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3521 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3522 | // C++0x inherited constructors. |
| 3523 | if (getLangOptions().CPlusPlus0x) break; |
| 3524 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3525 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 3526 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3527 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3528 | |
| 3529 | case UnqualifiedId::IK_DestructorName: |
| 3530 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 3531 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3532 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3533 | |
| 3534 | case UnqualifiedId::IK_TemplateId: |
| 3535 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 3536 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3537 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3538 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3539 | |
| 3540 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 3541 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3542 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3543 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3544 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3545 | // Warn about using declarations. |
| 3546 | // TODO: store that the declaration was written without 'using' and |
| 3547 | // talk about access decls instead of using decls in the |
| 3548 | // diagnostics. |
| 3549 | if (!HasUsingKeyword) { |
| 3550 | UsingLoc = Name.getSourceRange().getBegin(); |
| 3551 | |
| 3552 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3553 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3556 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3557 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3558 | /* IsInstantiation */ false, |
| 3559 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3560 | if (UD) |
| 3561 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3562 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3563 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 3566 | /// \brief Determine whether a using declaration considers the given |
| 3567 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 3568 | /// the same entity or are both typedefs of the same type. |
| 3569 | static bool |
| 3570 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 3571 | bool &SuppressRedeclaration) { |
| 3572 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 3573 | SuppressRedeclaration = false; |
| 3574 | return true; |
| 3575 | } |
| 3576 | |
| 3577 | if (TypedefDecl *TD1 = dyn_cast<TypedefDecl>(D1)) |
| 3578 | if (TypedefDecl *TD2 = dyn_cast<TypedefDecl>(D2)) { |
| 3579 | SuppressRedeclaration = true; |
| 3580 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 3581 | TD2->getUnderlyingType()); |
| 3582 | } |
| 3583 | |
| 3584 | return false; |
| 3585 | } |
| 3586 | |
| 3587 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3588 | /// Determines whether to create a using shadow decl for a particular |
| 3589 | /// decl, given the set of decls existing prior to this using lookup. |
| 3590 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 3591 | const LookupResult &Previous) { |
| 3592 | // Diagnose finding a decl which is not from a base class of the |
| 3593 | // current class. We do this now because there are cases where this |
| 3594 | // function will silently decide not to build a shadow decl, which |
| 3595 | // will pre-empt further diagnostics. |
| 3596 | // |
| 3597 | // We don't need to do this in C++0x because we do the check once on |
| 3598 | // the qualifier. |
| 3599 | // |
| 3600 | // FIXME: diagnose the following if we care enough: |
| 3601 | // struct A { int foo; }; |
| 3602 | // struct B : A { using A::foo; }; |
| 3603 | // template <class T> struct C : A {}; |
| 3604 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 3605 | // This is invalid (during instantiation) in C++03 because B::foo |
| 3606 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 3607 | // We can't diagnose it immediately because C<T> is an unknown |
| 3608 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 3609 | // to A::foo, which will look well-formed when we instantiate. |
| 3610 | // The right solution is to not collapse the shadow-decl chain. |
| 3611 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 3612 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 3613 | |
| 3614 | // Handle enums and anonymous structs. |
| 3615 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 3616 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 3617 | while (OrigRec->isAnonymousStructOrUnion()) |
| 3618 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 3619 | |
| 3620 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 3621 | if (OrigDC == CurContext) { |
| 3622 | Diag(Using->getLocation(), |
| 3623 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3624 | << Using->getNestedNameRange(); |
| 3625 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3626 | return true; |
| 3627 | } |
| 3628 | |
| 3629 | Diag(Using->getNestedNameRange().getBegin(), |
| 3630 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3631 | << Using->getTargetNestedNameDecl() |
| 3632 | << cast<CXXRecordDecl>(CurContext) |
| 3633 | << Using->getNestedNameRange(); |
| 3634 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3635 | return true; |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | if (Previous.empty()) return false; |
| 3640 | |
| 3641 | NamedDecl *Target = Orig; |
| 3642 | if (isa<UsingShadowDecl>(Target)) |
| 3643 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3644 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3645 | // If the target happens to be one of the previous declarations, we |
| 3646 | // don't have a conflict. |
| 3647 | // |
| 3648 | // FIXME: but we might be increasing its access, in which case we |
| 3649 | // should redeclare it. |
| 3650 | NamedDecl *NonTag = 0, *Tag = 0; |
| 3651 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 3652 | I != E; ++I) { |
| 3653 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 3654 | bool Result; |
| 3655 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 3656 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3657 | |
| 3658 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 3659 | } |
| 3660 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3661 | if (Target->isFunctionOrFunctionTemplate()) { |
| 3662 | FunctionDecl *FD; |
| 3663 | if (isa<FunctionTemplateDecl>(Target)) |
| 3664 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 3665 | else |
| 3666 | FD = cast<FunctionDecl>(Target); |
| 3667 | |
| 3668 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3669 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3670 | case Ovl_Overload: |
| 3671 | return false; |
| 3672 | |
| 3673 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3674 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3675 | break; |
| 3676 | |
| 3677 | // We found a decl with the exact signature. |
| 3678 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3679 | // If we're in a record, we want to hide the target, so we |
| 3680 | // return true (without a diagnostic) to tell the caller not to |
| 3681 | // build a shadow decl. |
| 3682 | if (CurContext->isRecord()) |
| 3683 | return true; |
| 3684 | |
| 3685 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3686 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3687 | break; |
| 3688 | } |
| 3689 | |
| 3690 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3691 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 3692 | return true; |
| 3693 | } |
| 3694 | |
| 3695 | // Target is not a function. |
| 3696 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3697 | if (isa<TagDecl>(Target)) { |
| 3698 | // No conflict between a tag and a non-tag. |
| 3699 | if (!Tag) return false; |
| 3700 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3701 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3702 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3703 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 3704 | return true; |
| 3705 | } |
| 3706 | |
| 3707 | // No conflict between a tag and a non-tag. |
| 3708 | if (!NonTag) return false; |
| 3709 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3710 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3711 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3712 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 3713 | return true; |
| 3714 | } |
| 3715 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3716 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3717 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3718 | UsingDecl *UD, |
| 3719 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3720 | |
| 3721 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3722 | NamedDecl *Target = Orig; |
| 3723 | if (isa<UsingShadowDecl>(Target)) { |
| 3724 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3725 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3729 | = UsingShadowDecl::Create(Context, CurContext, |
| 3730 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3731 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 3732 | |
| 3733 | Shadow->setAccess(UD->getAccess()); |
| 3734 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 3735 | Shadow->setInvalidDecl(); |
| 3736 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3737 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3738 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3739 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3740 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3741 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3742 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3743 | return Shadow; |
| 3744 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3745 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3746 | /// Hides a using shadow declaration. This is required by the current |
| 3747 | /// using-decl implementation when a resolvable using declaration in a |
| 3748 | /// class is followed by a declaration which would hide or override |
| 3749 | /// one or more of the using decl's targets; for example: |
| 3750 | /// |
| 3751 | /// struct Base { void foo(int); }; |
| 3752 | /// struct Derived : Base { |
| 3753 | /// using Base::foo; |
| 3754 | /// void foo(int); |
| 3755 | /// }; |
| 3756 | /// |
| 3757 | /// The governing language is C++03 [namespace.udecl]p12: |
| 3758 | /// |
| 3759 | /// When a using-declaration brings names from a base class into a |
| 3760 | /// derived class scope, member functions in the derived class |
| 3761 | /// override and/or hide member functions with the same name and |
| 3762 | /// parameter types in a base class (rather than conflicting). |
| 3763 | /// |
| 3764 | /// There are two ways to implement this: |
| 3765 | /// (1) optimistically create shadow decls when they're not hidden |
| 3766 | /// by existing declarations, or |
| 3767 | /// (2) don't create any shadow decls (or at least don't make them |
| 3768 | /// visible) until we've fully parsed/instantiated the class. |
| 3769 | /// The problem with (1) is that we might have to retroactively remove |
| 3770 | /// a shadow decl, which requires several O(n) operations because the |
| 3771 | /// decl structures are (very reasonably) not designed for removal. |
| 3772 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 3773 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3774 | if (Shadow->getDeclName().getNameKind() == |
| 3775 | DeclarationName::CXXConversionFunctionName) |
| 3776 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 3777 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3778 | // Remove it from the DeclContext... |
| 3779 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3780 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3781 | // ...and the scope, if applicable... |
| 3782 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3783 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3784 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3787 | // ...and the using decl. |
| 3788 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 3789 | |
| 3790 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3791 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3794 | /// Builds a using declaration. |
| 3795 | /// |
| 3796 | /// \param IsInstantiation - Whether this call arises from an |
| 3797 | /// instantiation of an unresolved using declaration. We treat |
| 3798 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3799 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 3800 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3801 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3802 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3803 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3804 | bool IsInstantiation, |
| 3805 | bool IsTypeName, |
| 3806 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3807 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3808 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3809 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 3810 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3811 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3812 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3813 | if (SS.isEmpty()) { |
| 3814 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3815 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3816 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3818 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3819 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3820 | ForRedeclaration); |
| 3821 | Previous.setHideTags(false); |
| 3822 | if (S) { |
| 3823 | LookupName(Previous, S); |
| 3824 | |
| 3825 | // It is really dumb that we have to do this. |
| 3826 | LookupResult::Filter F = Previous.makeFilter(); |
| 3827 | while (F.hasNext()) { |
| 3828 | NamedDecl *D = F.next(); |
| 3829 | if (!isDeclInScope(D, CurContext, S)) |
| 3830 | F.erase(); |
| 3831 | } |
| 3832 | F.done(); |
| 3833 | } else { |
| 3834 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 3835 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 3836 | LookupQualifiedName(Previous, CurContext); |
| 3837 | } |
| 3838 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3839 | NestedNameSpecifier *NNS = |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3840 | static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3841 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3842 | // Check for invalid redeclarations. |
| 3843 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 3844 | return 0; |
| 3845 | |
| 3846 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3847 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 3848 | return 0; |
| 3849 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3850 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3851 | NamedDecl *D; |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3852 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3853 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3854 | // FIXME: not all declaration name kinds are legal here |
| 3855 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 3856 | UsingLoc, TypenameLoc, |
| 3857 | SS.getRange(), NNS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3858 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3859 | } else { |
| 3860 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3861 | UsingLoc, SS.getRange(), |
| 3862 | NNS, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3863 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3864 | } else { |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3865 | D = UsingDecl::Create(Context, CurContext, |
| 3866 | SS.getRange(), UsingLoc, NNS, NameInfo, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3867 | IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3868 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3869 | D->setAccess(AS); |
| 3870 | CurContext->addDecl(D); |
| 3871 | |
| 3872 | if (!LookupContext) return D; |
| 3873 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3874 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3875 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3876 | UD->setInvalidDecl(); |
| 3877 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3880 | // Look up the target name. |
| 3881 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3882 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3883 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3884 | // Unlike most lookups, we don't always want to hide tag |
| 3885 | // declarations: tag names are visible through the using declaration |
| 3886 | // even if hidden by ordinary names, *except* in a dependent context |
| 3887 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3888 | if (!IsInstantiation) |
| 3889 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3890 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3891 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3893 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 3894 | Diag(IdentLoc, diag::err_no_member) |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3895 | << NameInfo.getName() << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3896 | UD->setInvalidDecl(); |
| 3897 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3900 | if (R.isAmbiguous()) { |
| 3901 | UD->setInvalidDecl(); |
| 3902 | return UD; |
| 3903 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3904 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3905 | if (IsTypeName) { |
| 3906 | // 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] | 3907 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3908 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 3909 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 3910 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 3911 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3912 | UD->setInvalidDecl(); |
| 3913 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3914 | } |
| 3915 | } else { |
| 3916 | // If we asked for a non-typename and we got a type, error out, |
| 3917 | // but only if this is an instantiation of an unresolved using |
| 3918 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3919 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3920 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 3921 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3922 | UD->setInvalidDecl(); |
| 3923 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3924 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3927 | // C++0x N2914 [namespace.udecl]p6: |
| 3928 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3929 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3930 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 3931 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3932 | UD->setInvalidDecl(); |
| 3933 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3934 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3935 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3936 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 3937 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 3938 | BuildUsingShadowDecl(S, UD, *I); |
| 3939 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3940 | |
| 3941 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3944 | /// Checks that the given using declaration is not an invalid |
| 3945 | /// redeclaration. Note that this is checking only for the using decl |
| 3946 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 3947 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 3948 | bool isTypeName, |
| 3949 | const CXXScopeSpec &SS, |
| 3950 | SourceLocation NameLoc, |
| 3951 | const LookupResult &Prev) { |
| 3952 | // C++03 [namespace.udecl]p8: |
| 3953 | // C++0x [namespace.udecl]p10: |
| 3954 | // A using-declaration is a declaration and can therefore be used |
| 3955 | // repeatedly where (and only where) multiple declarations are |
| 3956 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 3957 | // |
| 3958 | // That's in non-member contexts. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3959 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3960 | return false; |
| 3961 | |
| 3962 | NestedNameSpecifier *Qual |
| 3963 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 3964 | |
| 3965 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 3966 | NamedDecl *D = *I; |
| 3967 | |
| 3968 | bool DTypename; |
| 3969 | NestedNameSpecifier *DQual; |
| 3970 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 3971 | DTypename = UD->isTypeName(); |
| 3972 | DQual = UD->getTargetNestedNameDecl(); |
| 3973 | } else if (UnresolvedUsingValueDecl *UD |
| 3974 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 3975 | DTypename = false; |
| 3976 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3977 | } else if (UnresolvedUsingTypenameDecl *UD |
| 3978 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 3979 | DTypename = true; |
| 3980 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3981 | } else continue; |
| 3982 | |
| 3983 | // using decls differ if one says 'typename' and the other doesn't. |
| 3984 | // FIXME: non-dependent using decls? |
| 3985 | if (isTypeName != DTypename) continue; |
| 3986 | |
| 3987 | // using decls differ if they name different scopes (but note that |
| 3988 | // template instantiation can cause this check to trigger when it |
| 3989 | // didn't before instantiation). |
| 3990 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 3991 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 3992 | continue; |
| 3993 | |
| 3994 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3995 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3996 | return true; |
| 3997 | } |
| 3998 | |
| 3999 | return false; |
| 4000 | } |
| 4001 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4002 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4003 | /// Checks that the given nested-name qualifier used in a using decl |
| 4004 | /// in the current context is appropriately related to the current |
| 4005 | /// scope. If an error is found, diagnoses it and returns true. |
| 4006 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 4007 | const CXXScopeSpec &SS, |
| 4008 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4009 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4010 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 4011 | if (!CurContext->isRecord()) { |
| 4012 | // C++03 [namespace.udecl]p3: |
| 4013 | // C++0x [namespace.udecl]p8: |
| 4014 | // A using-declaration for a class member shall be a member-declaration. |
| 4015 | |
| 4016 | // If we weren't able to compute a valid scope, it must be a |
| 4017 | // dependent class scope. |
| 4018 | if (!NamedContext || NamedContext->isRecord()) { |
| 4019 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 4020 | << SS.getRange(); |
| 4021 | return true; |
| 4022 | } |
| 4023 | |
| 4024 | // Otherwise, everything is known to be fine. |
| 4025 | return false; |
| 4026 | } |
| 4027 | |
| 4028 | // The current scope is a record. |
| 4029 | |
| 4030 | // If the named context is dependent, we can't decide much. |
| 4031 | if (!NamedContext) { |
| 4032 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 4033 | // nested-name-specifier does not refer to a base class, which is |
| 4034 | // still possible in some cases. |
| 4035 | |
| 4036 | // Otherwise we have to conservatively report that things might be |
| 4037 | // okay. |
| 4038 | return false; |
| 4039 | } |
| 4040 | |
| 4041 | if (!NamedContext->isRecord()) { |
| 4042 | // Ideally this would point at the last name in the specifier, |
| 4043 | // but we don't have that level of source info. |
| 4044 | Diag(SS.getRange().getBegin(), |
| 4045 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 4046 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 4047 | return true; |
| 4048 | } |
| 4049 | |
| 4050 | if (getLangOptions().CPlusPlus0x) { |
| 4051 | // C++0x [namespace.udecl]p3: |
| 4052 | // In a using-declaration used as a member-declaration, the |
| 4053 | // nested-name-specifier shall name a base class of the class |
| 4054 | // being defined. |
| 4055 | |
| 4056 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 4057 | cast<CXXRecordDecl>(NamedContext))) { |
| 4058 | if (CurContext == NamedContext) { |
| 4059 | Diag(NameLoc, |
| 4060 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 4061 | << SS.getRange(); |
| 4062 | return true; |
| 4063 | } |
| 4064 | |
| 4065 | Diag(SS.getRange().getBegin(), |
| 4066 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 4067 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 4068 | << cast<CXXRecordDecl>(CurContext) |
| 4069 | << SS.getRange(); |
| 4070 | return true; |
| 4071 | } |
| 4072 | |
| 4073 | return false; |
| 4074 | } |
| 4075 | |
| 4076 | // C++03 [namespace.udecl]p4: |
| 4077 | // A using-declaration used as a member-declaration shall refer |
| 4078 | // to a member of a base class of the class being defined [etc.]. |
| 4079 | |
| 4080 | // Salient point: SS doesn't have to name a base class as long as |
| 4081 | // lookup only finds members from base classes. Therefore we can |
| 4082 | // diagnose here only if we can prove that that can't happen, |
| 4083 | // i.e. if the class hierarchies provably don't intersect. |
| 4084 | |
| 4085 | // TODO: it would be nice if "definitely valid" results were cached |
| 4086 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 4087 | // need to be repeated. |
| 4088 | |
| 4089 | struct UserData { |
| 4090 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 4091 | |
| 4092 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 4093 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 4094 | Data->Bases.insert(Base); |
| 4095 | return true; |
| 4096 | } |
| 4097 | |
| 4098 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 4099 | return !Class->forallBases(collect, this); |
| 4100 | } |
| 4101 | |
| 4102 | /// Returns true if the base is dependent or is one of the |
| 4103 | /// accumulated base classes. |
| 4104 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 4105 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 4106 | return !Data->Bases.count(Base); |
| 4107 | } |
| 4108 | |
| 4109 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 4110 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 4111 | } |
| 4112 | }; |
| 4113 | |
| 4114 | UserData Data; |
| 4115 | |
| 4116 | // Returns false if we find a dependent base. |
| 4117 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 4118 | return false; |
| 4119 | |
| 4120 | // Returns false if the class has a dependent base or if it or one |
| 4121 | // of its bases is present in the base set of the current context. |
| 4122 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 4123 | return false; |
| 4124 | |
| 4125 | Diag(SS.getRange().getBegin(), |
| 4126 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 4127 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 4128 | << cast<CXXRecordDecl>(CurContext) |
| 4129 | << SS.getRange(); |
| 4130 | |
| 4131 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4134 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 4135 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4136 | SourceLocation AliasLoc, |
| 4137 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4138 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 4139 | SourceLocation IdentLoc, |
| 4140 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4141 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4142 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4143 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 4144 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4145 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4146 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 4147 | NamedDecl *PrevDecl |
| 4148 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 4149 | ForRedeclaration); |
| 4150 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 4151 | PrevDecl = 0; |
| 4152 | |
| 4153 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4154 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4155 | // 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] | 4156 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4157 | // FIXME: At some point, we'll want to create the (redundant) |
| 4158 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4159 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4160 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4161 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4162 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4163 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4164 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 4165 | diag::err_redefinition_different_kind; |
| 4166 | Diag(AliasLoc, DiagID) << Alias; |
| 4167 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4168 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4169 | } |
| 4170 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4171 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4172 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4173 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4174 | if (R.empty()) { |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4175 | if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 4176 | CTC_NoKeywords, 0)) { |
| 4177 | if (R.getAsSingle<NamespaceDecl>() || |
| 4178 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 4179 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 4180 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 4181 | << Ident << DC << Corrected << SS.getRange() |
| 4182 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4183 | else |
| 4184 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 4185 | << Ident << Corrected |
| 4186 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4187 | |
| 4188 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 4189 | << Corrected; |
| 4190 | |
| 4191 | Ident = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 4192 | } else { |
| 4193 | R.clear(); |
| 4194 | R.setLookupName(Ident); |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4195 | } |
| 4196 | } |
| 4197 | |
| 4198 | if (R.empty()) { |
| 4199 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4200 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4201 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 4202 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4203 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4204 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4205 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 4206 | Alias, SS.getRange(), |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 4207 | (NestedNameSpecifier *)SS.getScopeRep(), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4208 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4209 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 4210 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4211 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4214 | namespace { |
| 4215 | /// \brief Scoped object used to handle the state changes required in Sema |
| 4216 | /// to implicitly define the body of a C++ member function; |
| 4217 | class ImplicitlyDefinedFunctionScope { |
| 4218 | Sema &S; |
| 4219 | DeclContext *PreviousContext; |
| 4220 | |
| 4221 | public: |
| 4222 | ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method) |
| 4223 | : S(S), PreviousContext(S.CurContext) |
| 4224 | { |
| 4225 | S.CurContext = Method; |
| 4226 | S.PushFunctionScope(); |
| 4227 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 4228 | } |
| 4229 | |
| 4230 | ~ImplicitlyDefinedFunctionScope() { |
| 4231 | S.PopExpressionEvaluationContext(); |
| 4232 | S.PopFunctionOrBlockScope(); |
| 4233 | S.CurContext = PreviousContext; |
| 4234 | } |
| 4235 | }; |
| 4236 | } |
| 4237 | |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4238 | static CXXConstructorDecl *getDefaultConstructorUnsafe(Sema &Self, |
| 4239 | CXXRecordDecl *D) { |
| 4240 | ASTContext &Context = Self.Context; |
| 4241 | QualType ClassType = Context.getTypeDeclType(D); |
| 4242 | DeclarationName ConstructorName |
| 4243 | = Context.DeclarationNames.getCXXConstructorName( |
| 4244 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
| 4245 | |
| 4246 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 4247 | for (llvm::tie(Con, ConEnd) = D->lookup(ConstructorName); |
| 4248 | Con != ConEnd; ++Con) { |
| 4249 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 4250 | if (isa<FunctionTemplateDecl>(*Con)) |
| 4251 | continue; |
| 4252 | |
| 4253 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 4254 | if (Constructor->isDefaultConstructor()) |
| 4255 | return Constructor; |
| 4256 | } |
| 4257 | return 0; |
| 4258 | } |
| 4259 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4260 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 4261 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4262 | // C++ [class.ctor]p5: |
| 4263 | // A default constructor for a class X is a constructor of class X |
| 4264 | // that can be called without an argument. If there is no |
| 4265 | // user-declared constructor for class X, a default constructor is |
| 4266 | // implicitly declared. An implicitly-declared default constructor |
| 4267 | // is an inline public member of its class. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4268 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
| 4269 | "Should not build implicit default constructor!"); |
| 4270 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4271 | // C++ [except.spec]p14: |
| 4272 | // An implicitly declared special member function (Clause 12) shall have an |
| 4273 | // exception-specification. [...] |
| 4274 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4275 | |
| 4276 | // Direct base-class destructors. |
| 4277 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4278 | BEnd = ClassDecl->bases_end(); |
| 4279 | B != BEnd; ++B) { |
| 4280 | if (B->isVirtual()) // Handled below. |
| 4281 | continue; |
| 4282 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4283 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 4284 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4285 | if (!BaseClassDecl->hasDeclaredDefaultConstructor()) |
| 4286 | ExceptSpec.CalledDecl(DeclareImplicitDefaultConstructor(BaseClassDecl)); |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4287 | else if (CXXConstructorDecl *Constructor |
| 4288 | = getDefaultConstructorUnsafe(*this, BaseClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4289 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4290 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
| 4293 | // Virtual base-class destructors. |
| 4294 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 4295 | BEnd = ClassDecl->vbases_end(); |
| 4296 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4297 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 4298 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4299 | if (!BaseClassDecl->hasDeclaredDefaultConstructor()) |
| 4300 | ExceptSpec.CalledDecl(DeclareImplicitDefaultConstructor(BaseClassDecl)); |
| 4301 | else if (CXXConstructorDecl *Constructor |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4302 | = getDefaultConstructorUnsafe(*this, BaseClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4303 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4304 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | // Field destructors. |
| 4308 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4309 | FEnd = ClassDecl->field_end(); |
| 4310 | F != FEnd; ++F) { |
| 4311 | if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4312 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 4313 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4314 | if (!FieldClassDecl->hasDeclaredDefaultConstructor()) |
| 4315 | ExceptSpec.CalledDecl( |
| 4316 | DeclareImplicitDefaultConstructor(FieldClassDecl)); |
| 4317 | else if (CXXConstructorDecl *Constructor |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4318 | = getDefaultConstructorUnsafe(*this, FieldClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4319 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4320 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | |
| 4324 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4325 | CanQualType ClassType |
| 4326 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 4327 | DeclarationName Name |
| 4328 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4329 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4330 | CXXConstructorDecl *DefaultCon |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4331 | = CXXConstructorDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4332 | Context.getFunctionType(Context.VoidTy, |
| 4333 | 0, 0, false, 0, |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4334 | ExceptSpec.hasExceptionSpecification(), |
| 4335 | ExceptSpec.hasAnyExceptionSpecification(), |
| 4336 | ExceptSpec.size(), |
| 4337 | ExceptSpec.data(), |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4338 | FunctionType::ExtInfo()), |
| 4339 | /*TInfo=*/0, |
| 4340 | /*isExplicit=*/false, |
| 4341 | /*isInline=*/true, |
| 4342 | /*isImplicitlyDeclared=*/true); |
| 4343 | DefaultCon->setAccess(AS_public); |
| 4344 | DefaultCon->setImplicit(); |
| 4345 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4346 | |
| 4347 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4348 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
| 4349 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4350 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4351 | PushOnScopeChains(DefaultCon, S, false); |
| 4352 | ClassDecl->addDecl(DefaultCon); |
| 4353 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4354 | return DefaultCon; |
| 4355 | } |
| 4356 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4357 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 4358 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4359 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4360 | !Constructor->isUsed(false)) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4361 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 4363 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4364 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 4365 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4366 | ImplicitlyDefinedFunctionScope Scope(*this, Constructor); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4367 | ErrorTrap Trap(*this); |
| 4368 | if (SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false) || |
| 4369 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4370 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 4371 | << CXXConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4372 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4373 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4374 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4375 | |
| 4376 | SourceLocation Loc = Constructor->getLocation(); |
| 4377 | Constructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 4378 | |
| 4379 | Constructor->setUsed(); |
| 4380 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4383 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4384 | // C++ [class.dtor]p2: |
| 4385 | // If a class has no user-declared destructor, a destructor is |
| 4386 | // declared implicitly. An implicitly-declared destructor is an |
| 4387 | // inline public member of its class. |
| 4388 | |
| 4389 | // C++ [except.spec]p14: |
| 4390 | // An implicitly declared special member function (Clause 12) shall have |
| 4391 | // an exception-specification. |
| 4392 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4393 | |
| 4394 | // Direct base-class destructors. |
| 4395 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4396 | BEnd = ClassDecl->bases_end(); |
| 4397 | B != BEnd; ++B) { |
| 4398 | if (B->isVirtual()) // Handled below. |
| 4399 | continue; |
| 4400 | |
| 4401 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 4402 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4403 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | // Virtual base-class destructors. |
| 4407 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 4408 | BEnd = ClassDecl->vbases_end(); |
| 4409 | B != BEnd; ++B) { |
| 4410 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 4411 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4412 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | // Field destructors. |
| 4416 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4417 | FEnd = ClassDecl->field_end(); |
| 4418 | F != FEnd; ++F) { |
| 4419 | if (const RecordType *RecordTy |
| 4420 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
| 4421 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4422 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4425 | // Create the actual destructor declaration. |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4426 | QualType Ty = Context.getFunctionType(Context.VoidTy, |
| 4427 | 0, 0, false, 0, |
| 4428 | ExceptSpec.hasExceptionSpecification(), |
| 4429 | ExceptSpec.hasAnyExceptionSpecification(), |
| 4430 | ExceptSpec.size(), |
| 4431 | ExceptSpec.data(), |
| 4432 | FunctionType::ExtInfo()); |
| 4433 | |
| 4434 | CanQualType ClassType |
| 4435 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 4436 | DeclarationName Name |
| 4437 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4438 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4439 | CXXDestructorDecl *Destructor |
Craig Silverstein | b41d899 | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 4440 | = CXXDestructorDecl::Create(Context, ClassDecl, NameInfo, Ty, 0, |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4441 | /*isInline=*/true, |
| 4442 | /*isImplicitlyDeclared=*/true); |
| 4443 | Destructor->setAccess(AS_public); |
| 4444 | Destructor->setImplicit(); |
| 4445 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4446 | |
| 4447 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4448 | ++ASTContext::NumImplicitDestructorsDeclared; |
| 4449 | |
| 4450 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4451 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4452 | PushOnScopeChains(Destructor, S, false); |
| 4453 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4454 | |
| 4455 | // This could be uniqued if it ever proves significant. |
| 4456 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
| 4457 | |
| 4458 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4459 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4460 | return Destructor; |
| 4461 | } |
| 4462 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4463 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 4464 | CXXDestructorDecl *Destructor) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4465 | assert((Destructor->isImplicit() && !Destructor->isUsed(false)) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4466 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4467 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4468 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4469 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4470 | if (Destructor->isInvalidDecl()) |
| 4471 | return; |
| 4472 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4473 | ImplicitlyDefinedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4474 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4475 | ErrorTrap Trap(*this); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 4476 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 4477 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4478 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4479 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4480 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4481 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 4482 | |
| 4483 | Destructor->setInvalidDecl(); |
| 4484 | return; |
| 4485 | } |
| 4486 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4487 | SourceLocation Loc = Destructor->getLocation(); |
| 4488 | Destructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 4489 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4490 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4491 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4494 | /// \brief Builds a statement that copies the given entity from \p From to |
| 4495 | /// \c To. |
| 4496 | /// |
| 4497 | /// This routine is used to copy the members of a class with an |
| 4498 | /// implicitly-declared copy assignment operator. When the entities being |
| 4499 | /// copied are arrays, this routine builds for loops to copy them. |
| 4500 | /// |
| 4501 | /// \param S The Sema object used for type-checking. |
| 4502 | /// |
| 4503 | /// \param Loc The location where the implicit copy is being generated. |
| 4504 | /// |
| 4505 | /// \param T The type of the expressions being copied. Both expressions must |
| 4506 | /// have this type. |
| 4507 | /// |
| 4508 | /// \param To The expression we are copying to. |
| 4509 | /// |
| 4510 | /// \param From The expression we are copying from. |
| 4511 | /// |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4512 | /// \param CopyingBaseSubobject Whether we're copying a base subobject. |
| 4513 | /// Otherwise, it's a non-static member subobject. |
| 4514 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4515 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 4516 | /// |
| 4517 | /// \returns A statement or a loop that copies the expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4518 | static StmtResult |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4519 | BuildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4520 | Expr *To, Expr *From, |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4521 | bool CopyingBaseSubobject, unsigned Depth = 0) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4522 | // C++0x [class.copy]p30: |
| 4523 | // Each subobject is assigned in the manner appropriate to its type: |
| 4524 | // |
| 4525 | // - if the subobject is of class type, the copy assignment operator |
| 4526 | // for the class is used (as if by explicit qualification; that is, |
| 4527 | // ignoring any possible virtual overriding functions in more derived |
| 4528 | // classes); |
| 4529 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 4530 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4531 | |
| 4532 | // Look for operator=. |
| 4533 | DeclarationName Name |
| 4534 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 4535 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 4536 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| 4537 | |
| 4538 | // Filter out any result that isn't a copy-assignment operator. |
| 4539 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 4540 | while (F.hasNext()) { |
| 4541 | NamedDecl *D = F.next(); |
| 4542 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 4543 | if (Method->isCopyAssignmentOperator()) |
| 4544 | continue; |
| 4545 | |
| 4546 | F.erase(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4547 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4548 | F.done(); |
| 4549 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4550 | // Suppress the protected check (C++ [class.protected]) for each of the |
| 4551 | // assignment operators we found. This strange dance is required when |
| 4552 | // we're assigning via a base classes's copy-assignment operator. To |
| 4553 | // ensure that we're getting the right base class subobject (without |
| 4554 | // ambiguities), we need to cast "this" to that subobject type; to |
| 4555 | // ensure that we don't go through the virtual call mechanism, we need |
| 4556 | // to qualify the operator= name with the base class (see below). However, |
| 4557 | // this means that if the base class has a protected copy assignment |
| 4558 | // operator, the protected member access check will fail. So, we |
| 4559 | // rewrite "protected" access to "public" access in this case, since we |
| 4560 | // know by construction that we're calling from a derived class. |
| 4561 | if (CopyingBaseSubobject) { |
| 4562 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 4563 | L != LEnd; ++L) { |
| 4564 | if (L.getAccess() == AS_protected) |
| 4565 | L.setAccess(AS_public); |
| 4566 | } |
| 4567 | } |
| 4568 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4569 | // Create the nested-name-specifier that will be used to qualify the |
| 4570 | // reference to operator=; this is required to suppress the virtual |
| 4571 | // call mechanism. |
| 4572 | CXXScopeSpec SS; |
| 4573 | SS.setRange(Loc); |
| 4574 | SS.setScopeRep(NestedNameSpecifier::Create(S.Context, 0, false, |
| 4575 | T.getTypePtr())); |
| 4576 | |
| 4577 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4578 | ExprResult OpEqualRef |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4579 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4580 | /*FirstQualifierInScope=*/0, OpLookup, |
| 4581 | /*TemplateArgs=*/0, |
| 4582 | /*SuppressQualifierCheck=*/true); |
| 4583 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4584 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4585 | |
| 4586 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4587 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4588 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4589 | OpEqualRef.takeAs<Expr>(), |
| 4590 | Loc, &From, 1, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4591 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4592 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4593 | |
| 4594 | return S.Owned(Call.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4595 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4596 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4597 | // - if the subobject is of scalar type, the built-in assignment |
| 4598 | // operator is used. |
| 4599 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| 4600 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4601 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4602 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4603 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4604 | |
| 4605 | return S.Owned(Assignment.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4606 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4607 | |
| 4608 | // - if the subobject is an array, each element is assigned, in the |
| 4609 | // manner appropriate to the element type; |
| 4610 | |
| 4611 | // Construct a loop over the array bounds, e.g., |
| 4612 | // |
| 4613 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 4614 | // |
| 4615 | // that will copy each of the array elements. |
| 4616 | QualType SizeType = S.Context.getSizeType(); |
| 4617 | |
| 4618 | // Create the iteration variable. |
| 4619 | IdentifierInfo *IterationVarName = 0; |
| 4620 | { |
| 4621 | llvm::SmallString<8> Str; |
| 4622 | llvm::raw_svector_ostream OS(Str); |
| 4623 | OS << "__i" << Depth; |
| 4624 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 4625 | } |
| 4626 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, |
| 4627 | IterationVarName, SizeType, |
| 4628 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4629 | SC_None, SC_None); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4630 | |
| 4631 | // Initialize the iteration variable to zero. |
| 4632 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4633 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4634 | |
| 4635 | // Create a reference to the iteration variable; we'll use this several |
| 4636 | // times throughout. |
| 4637 | Expr *IterationVarRef |
| 4638 | = S.BuildDeclRefExpr(IterationVar, SizeType, Loc).takeAs<Expr>(); |
| 4639 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
| 4640 | |
| 4641 | // Create the DeclStmt that holds the iteration variable. |
| 4642 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| 4643 | |
| 4644 | // Create the comparison against the array bound. |
| 4645 | llvm::APInt Upper = ArrayTy->getSize(); |
| 4646 | Upper.zextOrTrunc(S.Context.getTypeSize(SizeType)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4647 | Expr *Comparison |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4648 | = new (S.Context) BinaryOperator(IterationVarRef, |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4649 | IntegerLiteral::Create(S.Context, |
| 4650 | Upper, SizeType, Loc), |
| 4651 | BO_NE, S.Context.BoolTy, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4652 | |
| 4653 | // Create the pre-increment of the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4654 | Expr *Increment |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4655 | = new (S.Context) UnaryOperator(IterationVarRef, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4656 | UO_PreInc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4657 | SizeType, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4658 | |
| 4659 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4660 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
| 4661 | IterationVarRef, Loc)); |
| 4662 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
| 4663 | IterationVarRef, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4664 | |
| 4665 | // Build the copy for an individual element of the array. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4666 | StmtResult Copy = BuildSingleCopyAssign(S, Loc, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4667 | ArrayTy->getElementType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4668 | To, From, |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4669 | CopyingBaseSubobject, Depth+1); |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 4670 | if (Copy.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4671 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4672 | |
| 4673 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4674 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4675 | S.MakeFullExpr(Comparison), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4676 | 0, S.MakeFullExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4677 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4680 | /// \brief Determine whether the given class has a copy assignment operator |
| 4681 | /// that accepts a const-qualified argument. |
| 4682 | static bool hasConstCopyAssignment(Sema &S, const CXXRecordDecl *CClass) { |
| 4683 | CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(CClass); |
| 4684 | |
| 4685 | if (!Class->hasDeclaredCopyAssignment()) |
| 4686 | S.DeclareImplicitCopyAssignment(Class); |
| 4687 | |
| 4688 | QualType ClassType = S.Context.getCanonicalType(S.Context.getTypeDeclType(Class)); |
| 4689 | DeclarationName OpName |
| 4690 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 4691 | |
| 4692 | DeclContext::lookup_const_iterator Op, OpEnd; |
| 4693 | for (llvm::tie(Op, OpEnd) = Class->lookup(OpName); Op != OpEnd; ++Op) { |
| 4694 | // C++ [class.copy]p9: |
| 4695 | // A user-declared copy assignment operator is a non-static non-template |
| 4696 | // member function of class X with exactly one parameter of type X, X&, |
| 4697 | // const X&, volatile X& or const volatile X&. |
| 4698 | const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); |
| 4699 | if (!Method) |
| 4700 | continue; |
| 4701 | |
| 4702 | if (Method->isStatic()) |
| 4703 | continue; |
| 4704 | if (Method->getPrimaryTemplate()) |
| 4705 | continue; |
| 4706 | const FunctionProtoType *FnType = |
| 4707 | Method->getType()->getAs<FunctionProtoType>(); |
| 4708 | assert(FnType && "Overloaded operator has no prototype."); |
| 4709 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 4710 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 4711 | continue; |
| 4712 | bool AcceptsConst = true; |
| 4713 | QualType ArgType = FnType->getArgType(0); |
| 4714 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()){ |
| 4715 | ArgType = Ref->getPointeeType(); |
| 4716 | // Is it a non-const lvalue reference? |
| 4717 | if (!ArgType.isConstQualified()) |
| 4718 | AcceptsConst = false; |
| 4719 | } |
| 4720 | if (!S.Context.hasSameUnqualifiedType(ArgType, ClassType)) |
| 4721 | continue; |
| 4722 | |
| 4723 | // We have a single argument of type cv X or cv X&, i.e. we've found the |
| 4724 | // copy assignment operator. Return whether it accepts const arguments. |
| 4725 | return AcceptsConst; |
| 4726 | } |
| 4727 | assert(Class->isInvalidDecl() && |
| 4728 | "No copy assignment operator declared in valid code."); |
| 4729 | return false; |
| 4730 | } |
| 4731 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4732 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4733 | // Note: The following rules are largely analoguous to the copy |
| 4734 | // constructor rules. Note that virtual bases are not taken into account |
| 4735 | // for determining the argument type of the operator. Note also that |
| 4736 | // operators taking an object instead of a reference are allowed. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4737 | |
| 4738 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4739 | // C++ [class.copy]p10: |
| 4740 | // If the class definition does not explicitly declare a copy |
| 4741 | // assignment operator, one is declared implicitly. |
| 4742 | // The implicitly-defined copy assignment operator for a class X |
| 4743 | // will have the form |
| 4744 | // |
| 4745 | // X& X::operator=(const X&) |
| 4746 | // |
| 4747 | // if |
| 4748 | bool HasConstCopyAssignment = true; |
| 4749 | |
| 4750 | // -- each direct base class B of X has a copy assignment operator |
| 4751 | // whose parameter is of type const B&, const volatile B& or B, |
| 4752 | // and |
| 4753 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4754 | BaseEnd = ClassDecl->bases_end(); |
| 4755 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
| 4756 | assert(!Base->getType()->isDependentType() && |
| 4757 | "Cannot generate implicit members for class with dependent bases."); |
| 4758 | const CXXRecordDecl *BaseClassDecl |
| 4759 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4760 | HasConstCopyAssignment = hasConstCopyAssignment(*this, BaseClassDecl); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | // -- for all the nonstatic data members of X that are of a class |
| 4764 | // type M (or array thereof), each such class type has a copy |
| 4765 | // assignment operator whose parameter is of type const M&, |
| 4766 | // const volatile M& or M. |
| 4767 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4768 | FieldEnd = ClassDecl->field_end(); |
| 4769 | HasConstCopyAssignment && Field != FieldEnd; |
| 4770 | ++Field) { |
| 4771 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 4772 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 4773 | const CXXRecordDecl *FieldClassDecl |
| 4774 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4775 | HasConstCopyAssignment = hasConstCopyAssignment(*this, FieldClassDecl); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4776 | } |
| 4777 | } |
| 4778 | |
| 4779 | // Otherwise, the implicitly declared copy assignment operator will |
| 4780 | // have the form |
| 4781 | // |
| 4782 | // X& X::operator=(X&) |
| 4783 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 4784 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 4785 | if (HasConstCopyAssignment) |
| 4786 | ArgType = ArgType.withConst(); |
| 4787 | ArgType = Context.getLValueReferenceType(ArgType); |
| 4788 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4789 | // C++ [except.spec]p14: |
| 4790 | // An implicitly declared special member function (Clause 12) shall have an |
| 4791 | // exception-specification. [...] |
| 4792 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4793 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4794 | BaseEnd = ClassDecl->bases_end(); |
| 4795 | Base != BaseEnd; ++Base) { |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4796 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4797 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4798 | |
| 4799 | if (!BaseClassDecl->hasDeclaredCopyAssignment()) |
| 4800 | DeclareImplicitCopyAssignment(BaseClassDecl); |
| 4801 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4802 | if (CXXMethodDecl *CopyAssign |
| 4803 | = BaseClassDecl->getCopyAssignmentOperator(HasConstCopyAssignment)) |
| 4804 | ExceptSpec.CalledDecl(CopyAssign); |
| 4805 | } |
| 4806 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4807 | FieldEnd = ClassDecl->field_end(); |
| 4808 | Field != FieldEnd; |
| 4809 | ++Field) { |
| 4810 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 4811 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4812 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4813 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4814 | |
| 4815 | if (!FieldClassDecl->hasDeclaredCopyAssignment()) |
| 4816 | DeclareImplicitCopyAssignment(FieldClassDecl); |
| 4817 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4818 | if (CXXMethodDecl *CopyAssign |
| 4819 | = FieldClassDecl->getCopyAssignmentOperator(HasConstCopyAssignment)) |
| 4820 | ExceptSpec.CalledDecl(CopyAssign); |
| 4821 | } |
| 4822 | } |
| 4823 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4824 | // An implicitly-declared copy assignment operator is an inline public |
| 4825 | // member of its class. |
| 4826 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4827 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4828 | CXXMethodDecl *CopyAssignment |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4829 | = CXXMethodDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4830 | Context.getFunctionType(RetType, &ArgType, 1, |
| 4831 | false, 0, |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4832 | ExceptSpec.hasExceptionSpecification(), |
| 4833 | ExceptSpec.hasAnyExceptionSpecification(), |
| 4834 | ExceptSpec.size(), |
| 4835 | ExceptSpec.data(), |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4836 | FunctionType::ExtInfo()), |
| 4837 | /*TInfo=*/0, /*isStatic=*/false, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4838 | /*StorageClassAsWritten=*/SC_None, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4839 | /*isInline=*/true); |
| 4840 | CopyAssignment->setAccess(AS_public); |
| 4841 | CopyAssignment->setImplicit(); |
| 4842 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4843 | |
| 4844 | // Add the parameter to the operator. |
| 4845 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 4846 | ClassDecl->getLocation(), |
| 4847 | /*Id=*/0, |
| 4848 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4849 | SC_None, |
| 4850 | SC_None, 0); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4851 | CopyAssignment->setParams(&FromParam, 1); |
| 4852 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4853 | // Note that we have added this copy-assignment operator. |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4854 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
| 4855 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4856 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4857 | PushOnScopeChains(CopyAssignment, S, false); |
| 4858 | ClassDecl->addDecl(CopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4859 | |
| 4860 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 4861 | return CopyAssignment; |
| 4862 | } |
| 4863 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4864 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 4865 | CXXMethodDecl *CopyAssignOperator) { |
| 4866 | assert((CopyAssignOperator->isImplicit() && |
| 4867 | CopyAssignOperator->isOverloadedOperator() && |
| 4868 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4869 | !CopyAssignOperator->isUsed(false)) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4870 | "DefineImplicitCopyAssignment called for wrong function"); |
| 4871 | |
| 4872 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 4873 | |
| 4874 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 4875 | CopyAssignOperator->setInvalidDecl(); |
| 4876 | return; |
| 4877 | } |
| 4878 | |
| 4879 | CopyAssignOperator->setUsed(); |
| 4880 | |
| 4881 | ImplicitlyDefinedFunctionScope Scope(*this, CopyAssignOperator); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4882 | ErrorTrap Trap(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4883 | |
| 4884 | // C++0x [class.copy]p30: |
| 4885 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 4886 | // for a non-union class X performs memberwise copy assignment of its |
| 4887 | // subobjects. The direct base classes of X are assigned first, in the |
| 4888 | // order of their declaration in the base-specifier-list, and then the |
| 4889 | // immediate non-static data members of X are assigned, in the order in |
| 4890 | // which they were declared in the class definition. |
| 4891 | |
| 4892 | // The statements that form the synthesized function body. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4893 | ASTOwningVector<Stmt*> Statements(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4894 | |
| 4895 | // The parameter for the "other" object, which we are copying from. |
| 4896 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 4897 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 4898 | QualType OtherRefType = Other->getType(); |
| 4899 | if (const LValueReferenceType *OtherRef |
| 4900 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 4901 | OtherRefType = OtherRef->getPointeeType(); |
| 4902 | OtherQuals = OtherRefType.getQualifiers(); |
| 4903 | } |
| 4904 | |
| 4905 | // Our location for everything implicitly-generated. |
| 4906 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 4907 | |
| 4908 | // Construct a reference to the "other" object. We'll be using this |
| 4909 | // throughout the generated ASTs. |
| 4910 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, Loc).takeAs<Expr>(); |
| 4911 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 4912 | |
| 4913 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 4914 | // ASTs. |
| 4915 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 4916 | assert(This && "Reference to this cannot fail!"); |
| 4917 | |
| 4918 | // Assign base classes. |
| 4919 | bool Invalid = false; |
| 4920 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4921 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 4922 | // Form the assignment: |
| 4923 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 4924 | QualType BaseType = Base->getType().getUnqualifiedType(); |
| 4925 | CXXRecordDecl *BaseClassDecl = 0; |
| 4926 | if (const RecordType *BaseRecordT = BaseType->getAs<RecordType>()) |
| 4927 | BaseClassDecl = cast<CXXRecordDecl>(BaseRecordT->getDecl()); |
| 4928 | else { |
| 4929 | Invalid = true; |
| 4930 | continue; |
| 4931 | } |
| 4932 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4933 | CXXCastPath BasePath; |
| 4934 | BasePath.push_back(Base); |
| 4935 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4936 | // Construct the "from" expression, which is an implicit cast to the |
| 4937 | // appropriately-qualified base type. |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4938 | Expr *From = OtherRef; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4939 | ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4940 | CK_UncheckedDerivedToBase, |
| 4941 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4942 | |
| 4943 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4944 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4945 | |
| 4946 | // Implicitly cast "this" to the appropriately-qualified base type. |
| 4947 | Expr *ToE = To.takeAs<Expr>(); |
| 4948 | ImpCastExprToType(ToE, |
| 4949 | Context.getCVRQualifiedType(BaseType, |
| 4950 | CopyAssignOperator->getTypeQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4951 | CK_UncheckedDerivedToBase, |
| 4952 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4953 | To = Owned(ToE); |
| 4954 | |
| 4955 | // Build the copy. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4956 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4957 | To.get(), From, |
| 4958 | /*CopyingBaseSubobject=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4959 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 4960 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4961 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 4962 | CopyAssignOperator->setInvalidDecl(); |
| 4963 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4964 | } |
| 4965 | |
| 4966 | // Success! Record the copy. |
| 4967 | Statements.push_back(Copy.takeAs<Expr>()); |
| 4968 | } |
| 4969 | |
| 4970 | // \brief Reference to the __builtin_memcpy function. |
| 4971 | Expr *BuiltinMemCpyRef = 0; |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 4972 | // \brief Reference to the __builtin_objc_memmove_collectable function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4973 | Expr *CollectableMemCpyRef = 0; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4974 | |
| 4975 | // Assign non-static members. |
| 4976 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4977 | FieldEnd = ClassDecl->field_end(); |
| 4978 | Field != FieldEnd; ++Field) { |
| 4979 | // Check for members of reference type; we can't copy those. |
| 4980 | if (Field->getType()->isReferenceType()) { |
| 4981 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 4982 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 4983 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 4984 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4985 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4986 | Invalid = true; |
| 4987 | continue; |
| 4988 | } |
| 4989 | |
| 4990 | // Check for members of const-qualified, non-class type. |
| 4991 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 4992 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 4993 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 4994 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 4995 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 4996 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4997 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4998 | Invalid = true; |
| 4999 | continue; |
| 5000 | } |
| 5001 | |
| 5002 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 5003 | if (FieldType->isIncompleteArrayType()) { |
| 5004 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 5005 | "Incomplete array type is not valid"); |
| 5006 | continue; |
| 5007 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5008 | |
| 5009 | // Build references to the field in the object we're copying from and to. |
| 5010 | CXXScopeSpec SS; // Intentionally empty |
| 5011 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 5012 | LookupMemberName); |
| 5013 | MemberLookup.addDecl(*Field); |
| 5014 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5015 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5016 | Loc, /*IsArrow=*/false, |
| 5017 | SS, 0, MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5018 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5019 | Loc, /*IsArrow=*/true, |
| 5020 | SS, 0, MemberLookup, 0); |
| 5021 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 5022 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 5023 | |
| 5024 | // If the field should be copied with __builtin_memcpy rather than via |
| 5025 | // explicit assignments, do so. This optimization only applies for arrays |
| 5026 | // of scalars and arrays of class type with trivial copy-assignment |
| 5027 | // operators. |
| 5028 | if (FieldType->isArrayType() && |
| 5029 | (!BaseType->isRecordType() || |
| 5030 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()) |
| 5031 | ->hasTrivialCopyAssignment())) { |
| 5032 | // Compute the size of the memory buffer to be copied. |
| 5033 | QualType SizeType = Context.getSizeType(); |
| 5034 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 5035 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 5036 | for (const ConstantArrayType *Array |
| 5037 | = Context.getAsConstantArrayType(FieldType); |
| 5038 | Array; |
| 5039 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
| 5040 | llvm::APInt ArraySize = Array->getSize(); |
| 5041 | ArraySize.zextOrTrunc(Size.getBitWidth()); |
| 5042 | Size *= ArraySize; |
| 5043 | } |
| 5044 | |
| 5045 | // Take the address of the field references for "from" and "to". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5046 | From = CreateBuiltinUnaryOp(Loc, UO_AddrOf, From.get()); |
| 5047 | To = CreateBuiltinUnaryOp(Loc, UO_AddrOf, To.get()); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5048 | |
| 5049 | bool NeedsCollectableMemCpy = |
| 5050 | (BaseType->isRecordType() && |
| 5051 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()); |
| 5052 | |
| 5053 | if (NeedsCollectableMemCpy) { |
| 5054 | if (!CollectableMemCpyRef) { |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 5055 | // Create a reference to the __builtin_objc_memmove_collectable function. |
| 5056 | LookupResult R(*this, |
| 5057 | &Context.Idents.get("__builtin_objc_memmove_collectable"), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5058 | Loc, LookupOrdinaryName); |
| 5059 | LookupName(R, TUScope, true); |
| 5060 | |
| 5061 | FunctionDecl *CollectableMemCpy = R.getAsSingle<FunctionDecl>(); |
| 5062 | if (!CollectableMemCpy) { |
| 5063 | // Something went horribly wrong earlier, and we will have |
| 5064 | // complained about it. |
| 5065 | Invalid = true; |
| 5066 | continue; |
| 5067 | } |
| 5068 | |
| 5069 | CollectableMemCpyRef = BuildDeclRefExpr(CollectableMemCpy, |
| 5070 | CollectableMemCpy->getType(), |
| 5071 | Loc, 0).takeAs<Expr>(); |
| 5072 | assert(CollectableMemCpyRef && "Builtin reference cannot fail"); |
| 5073 | } |
| 5074 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5075 | // Create a reference to the __builtin_memcpy builtin function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5076 | else if (!BuiltinMemCpyRef) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5077 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 5078 | LookupOrdinaryName); |
| 5079 | LookupName(R, TUScope, true); |
| 5080 | |
| 5081 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 5082 | if (!BuiltinMemCpy) { |
| 5083 | // Something went horribly wrong earlier, and we will have complained |
| 5084 | // about it. |
| 5085 | Invalid = true; |
| 5086 | continue; |
| 5087 | } |
| 5088 | |
| 5089 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 5090 | BuiltinMemCpy->getType(), |
| 5091 | Loc, 0).takeAs<Expr>(); |
| 5092 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 5093 | } |
| 5094 | |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5095 | ASTOwningVector<Expr*> CallArgs(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5096 | CallArgs.push_back(To.takeAs<Expr>()); |
| 5097 | CallArgs.push_back(From.takeAs<Expr>()); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5098 | CallArgs.push_back(IntegerLiteral::Create(Context, Size, SizeType, Loc)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5099 | ExprResult Call = ExprError(); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5100 | if (NeedsCollectableMemCpy) |
| 5101 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5102 | CollectableMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5103 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5104 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5105 | else |
| 5106 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5107 | BuiltinMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5108 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5109 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5110 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5111 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 5112 | Statements.push_back(Call.takeAs<Expr>()); |
| 5113 | continue; |
| 5114 | } |
| 5115 | |
| 5116 | // Build the copy of this field. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5117 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, FieldType, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5118 | To.get(), From.get(), |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 5119 | /*CopyingBaseSubobject=*/false); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5120 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 5121 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5122 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5123 | CopyAssignOperator->setInvalidDecl(); |
| 5124 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5125 | } |
| 5126 | |
| 5127 | // Success! Record the copy. |
| 5128 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 5129 | } |
| 5130 | |
| 5131 | if (!Invalid) { |
| 5132 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5133 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5134 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5135 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5136 | if (Return.isInvalid()) |
| 5137 | Invalid = true; |
| 5138 | else { |
| 5139 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5140 | |
| 5141 | if (Trap.hasErrorOccurred()) { |
| 5142 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5143 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5144 | Invalid = true; |
| 5145 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5146 | } |
| 5147 | } |
| 5148 | |
| 5149 | if (Invalid) { |
| 5150 | CopyAssignOperator->setInvalidDecl(); |
| 5151 | return; |
| 5152 | } |
| 5153 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5154 | StmtResult Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5155 | /*isStmtExpr=*/false); |
| 5156 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 5157 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5158 | } |
| 5159 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5160 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 5161 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5162 | // C++ [class.copy]p4: |
| 5163 | // If the class definition does not explicitly declare a copy |
| 5164 | // constructor, one is declared implicitly. |
| 5165 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5166 | // C++ [class.copy]p5: |
| 5167 | // The implicitly-declared copy constructor for a class X will |
| 5168 | // have the form |
| 5169 | // |
| 5170 | // X::X(const X&) |
| 5171 | // |
| 5172 | // if |
| 5173 | bool HasConstCopyConstructor = true; |
| 5174 | |
| 5175 | // -- each direct or virtual base class B of X has a copy |
| 5176 | // constructor whose first parameter is of type const B& or |
| 5177 | // const volatile B&, and |
| 5178 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5179 | BaseEnd = ClassDecl->bases_end(); |
| 5180 | HasConstCopyConstructor && Base != BaseEnd; |
| 5181 | ++Base) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5182 | // Virtual bases are handled below. |
| 5183 | if (Base->isVirtual()) |
| 5184 | continue; |
| 5185 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5186 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5187 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5188 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5189 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5190 | |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5191 | HasConstCopyConstructor |
| 5192 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 5193 | } |
| 5194 | |
| 5195 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 5196 | BaseEnd = ClassDecl->vbases_end(); |
| 5197 | HasConstCopyConstructor && Base != BaseEnd; |
| 5198 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5199 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5200 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5201 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5202 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5203 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5204 | HasConstCopyConstructor |
| 5205 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 5206 | } |
| 5207 | |
| 5208 | // -- for all the nonstatic data members of X that are of a |
| 5209 | // class type M (or array thereof), each such class type |
| 5210 | // has a copy constructor whose first parameter is of type |
| 5211 | // const M& or const volatile M&. |
| 5212 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5213 | FieldEnd = ClassDecl->field_end(); |
| 5214 | HasConstCopyConstructor && Field != FieldEnd; |
| 5215 | ++Field) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5216 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5217 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5218 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5219 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5220 | if (!FieldClassDecl->hasDeclaredCopyConstructor()) |
| 5221 | DeclareImplicitCopyConstructor(FieldClassDecl); |
| 5222 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5223 | HasConstCopyConstructor |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5224 | = FieldClassDecl->hasConstCopyConstructor(Context); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5225 | } |
| 5226 | } |
| 5227 | |
| 5228 | // Otherwise, the implicitly declared copy constructor will have |
| 5229 | // the form |
| 5230 | // |
| 5231 | // X::X(X&) |
| 5232 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 5233 | QualType ArgType = ClassType; |
| 5234 | if (HasConstCopyConstructor) |
| 5235 | ArgType = ArgType.withConst(); |
| 5236 | ArgType = Context.getLValueReferenceType(ArgType); |
| 5237 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5238 | // C++ [except.spec]p14: |
| 5239 | // An implicitly declared special member function (Clause 12) shall have an |
| 5240 | // exception-specification. [...] |
| 5241 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 5242 | unsigned Quals = HasConstCopyConstructor? Qualifiers::Const : 0; |
| 5243 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5244 | BaseEnd = ClassDecl->bases_end(); |
| 5245 | Base != BaseEnd; |
| 5246 | ++Base) { |
| 5247 | // Virtual bases are handled below. |
| 5248 | if (Base->isVirtual()) |
| 5249 | continue; |
| 5250 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5251 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5252 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5253 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5254 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5255 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5256 | if (CXXConstructorDecl *CopyConstructor |
| 5257 | = BaseClassDecl->getCopyConstructor(Context, Quals)) |
| 5258 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5259 | } |
| 5260 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 5261 | BaseEnd = ClassDecl->vbases_end(); |
| 5262 | Base != BaseEnd; |
| 5263 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5264 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5265 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5266 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5267 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5268 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5269 | if (CXXConstructorDecl *CopyConstructor |
| 5270 | = BaseClassDecl->getCopyConstructor(Context, Quals)) |
| 5271 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5272 | } |
| 5273 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5274 | FieldEnd = ClassDecl->field_end(); |
| 5275 | Field != FieldEnd; |
| 5276 | ++Field) { |
| 5277 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 5278 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5279 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5280 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5281 | if (!FieldClassDecl->hasDeclaredCopyConstructor()) |
| 5282 | DeclareImplicitCopyConstructor(FieldClassDecl); |
| 5283 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5284 | if (CXXConstructorDecl *CopyConstructor |
| 5285 | = FieldClassDecl->getCopyConstructor(Context, Quals)) |
| 5286 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5287 | } |
| 5288 | } |
| 5289 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5290 | // An implicitly-declared copy constructor is an inline public |
| 5291 | // member of its class. |
| 5292 | DeclarationName Name |
| 5293 | = Context.DeclarationNames.getCXXConstructorName( |
| 5294 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5295 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5296 | CXXConstructorDecl *CopyConstructor |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5297 | = CXXConstructorDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5298 | Context.getFunctionType(Context.VoidTy, |
| 5299 | &ArgType, 1, |
| 5300 | false, 0, |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5301 | ExceptSpec.hasExceptionSpecification(), |
| 5302 | ExceptSpec.hasAnyExceptionSpecification(), |
| 5303 | ExceptSpec.size(), |
| 5304 | ExceptSpec.data(), |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5305 | FunctionType::ExtInfo()), |
| 5306 | /*TInfo=*/0, |
| 5307 | /*isExplicit=*/false, |
| 5308 | /*isInline=*/true, |
| 5309 | /*isImplicitlyDeclared=*/true); |
| 5310 | CopyConstructor->setAccess(AS_public); |
| 5311 | CopyConstructor->setImplicit(); |
| 5312 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
| 5313 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5314 | // Note that we have declared this constructor. |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5315 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 5316 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5317 | // Add the parameter to the constructor. |
| 5318 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 5319 | ClassDecl->getLocation(), |
| 5320 | /*IdentifierInfo=*/0, |
| 5321 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5322 | SC_None, |
| 5323 | SC_None, 0); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5324 | CopyConstructor->setParams(&FromParam, 1); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5325 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5326 | PushOnScopeChains(CopyConstructor, S, false); |
| 5327 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5328 | |
| 5329 | return CopyConstructor; |
| 5330 | } |
| 5331 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5332 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 5333 | CXXConstructorDecl *CopyConstructor, |
| 5334 | unsigned TypeQuals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5335 | assert((CopyConstructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 5336 | CopyConstructor->isCopyConstructor(TypeQuals) && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 5337 | !CopyConstructor->isUsed(false)) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5338 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5339 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 5340 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5341 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5342 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5343 | ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5344 | ErrorTrap Trap(*this); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5345 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5346 | if (SetBaseOrMemberInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) || |
| 5347 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 5348 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5349 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 5350 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5351 | } else { |
| 5352 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 5353 | CopyConstructor->getLocation(), |
| 5354 | MultiStmtArg(*this, 0, 0), |
| 5355 | /*isStmtExpr=*/false) |
| 5356 | .takeAs<Stmt>()); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 5357 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5358 | |
| 5359 | CopyConstructor->setUsed(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5360 | } |
| 5361 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5362 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 5363 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5364 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5365 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5366 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5367 | unsigned ConstructKind, |
| 5368 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5369 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5370 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5371 | // C++0x [class.copy]p34: |
| 5372 | // When certain criteria are met, an implementation is allowed to |
| 5373 | // omit the copy/move construction of a class object, even if the |
| 5374 | // copy/move constructor and/or destructor for the object have |
| 5375 | // side effects. [...] |
| 5376 | // - when a temporary class object that has not been bound to a |
| 5377 | // reference (12.2) would be copied/moved to a class object |
| 5378 | // with the same cv-unqualified type, the copy/move operation |
| 5379 | // can be omitted by constructing the temporary object |
| 5380 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5381 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
| 5382 | Constructor->isCopyConstructor() && ExprArgs.size() >= 1) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5383 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5384 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5385 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5386 | |
| 5387 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5388 | Elidable, move(ExprArgs), RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5389 | ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5390 | } |
| 5391 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5392 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 5393 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5394 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 5395 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 5396 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5397 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5398 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5399 | unsigned ConstructKind, |
| 5400 | SourceRange ParenRange) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 5401 | unsigned NumExprs = ExprArgs.size(); |
| 5402 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5403 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5404 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5405 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5406 | Constructor, Elidable, Exprs, NumExprs, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5407 | RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5408 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 5409 | ParenRange)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5412 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5413 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 5414 | MultiExprArg Exprs) { |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5415 | // FIXME: Provide the correct paren SourceRange when available. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5416 | ExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 5417 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5418 | move(Exprs), false, CXXConstructExpr::CK_Complete, |
| 5419 | SourceRange()); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 5420 | if (TempResult.isInvalid()) |
| 5421 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5422 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 5423 | Expr *Temp = TempResult.takeAs<Expr>(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5424 | CheckImplicitConversions(Temp, VD->getLocation()); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5425 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 5426 | Temp = MaybeCreateCXXExprWithTemporaries(Temp); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5427 | VD->setInit(Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5428 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 5429 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 5430 | } |
| 5431 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 5432 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
| 5433 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Douglas Gregor | 333de06 | 2010-02-25 18:11:54 +0000 | [diff] [blame] | 5434 | if (!ClassDecl->isInvalidDecl() && !VD->isInvalidDecl() && |
Douglas Gregor | fb2db46 | 2010-05-22 17:12:29 +0000 | [diff] [blame] | 5435 | !ClassDecl->hasTrivialDestructor() && !ClassDecl->isDependentContext()) { |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5436 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 5437 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5438 | CheckDestructorAccess(VD->getLocation(), Destructor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5439 | PDiag(diag::err_access_dtor_var) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5440 | << VD->getDeclName() |
| 5441 | << VD->getType()); |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 5442 | |
John McCall | ae79222 | 2010-09-18 05:25:11 +0000 | [diff] [blame] | 5443 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 5444 | if (!VD->isInvalidDecl() && VD->hasGlobalStorage() && !VD->isStaticLocal()) |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 5445 | Diag(VD->getLocation(), diag::warn_global_destructor); |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 5446 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5447 | } |
| 5448 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5449 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5450 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 5451 | /// e.g: "int x(1);" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5452 | void Sema::AddCXXDirectInitializerToDecl(Decl *RealDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5453 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5454 | MultiExprArg Exprs, |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5455 | SourceLocation RParenLoc) { |
Daniel Dunbar | 5184626 | 2009-12-24 19:19:26 +0000 | [diff] [blame] | 5456 | assert(Exprs.size() != 0 && Exprs.get() && "missing expressions"); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5457 | |
| 5458 | // If there is no declaration, there was an error parsing it. Just ignore |
| 5459 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5460 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5461 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5462 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5463 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 5464 | if (!VDecl) { |
| 5465 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 5466 | RealDecl->setInvalidDecl(); |
| 5467 | return; |
| 5468 | } |
| 5469 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 5470 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 5471 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5472 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 5473 | // |
| 5474 | // Clients that want to distinguish between the two forms, can check for |
| 5475 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 5476 | // A major benefit is that clients that don't particularly care about which |
| 5477 | // exactly form was it (like the CodeGen) can handle both cases without |
| 5478 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 5479 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5480 | // C++ 8.5p11: |
| 5481 | // The form of initialization (using parentheses or '=') is generally |
| 5482 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 5483 | // class type. |
| 5484 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 5485 | if (!VDecl->getType()->isDependentType() && |
| 5486 | RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 5487 | diag::err_typecheck_decl_incomplete_type)) { |
| 5488 | VDecl->setInvalidDecl(); |
| 5489 | return; |
| 5490 | } |
| 5491 | |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5492 | // The variable can not have an abstract class type. |
| 5493 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 5494 | diag::err_abstract_type_in_decl, |
| 5495 | AbstractVariableType)) |
| 5496 | VDecl->setInvalidDecl(); |
| 5497 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 5498 | const VarDecl *Def; |
| 5499 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5500 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| 5501 | << VDecl->getDeclName(); |
| 5502 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5503 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 5504 | return; |
| 5505 | } |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 5506 | |
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 5507 | // C++ [class.static.data]p4 |
| 5508 | // If a static data member is of const integral or const |
| 5509 | // enumeration type, its declaration in the class definition can |
| 5510 | // specify a constant-initializer which shall be an integral |
| 5511 | // constant expression (5.19). In that case, the member can appear |
| 5512 | // in integral constant expressions. The member shall still be |
| 5513 | // defined in a namespace scope if it is used in the program and the |
| 5514 | // namespace scope definition shall not contain an initializer. |
| 5515 | // |
| 5516 | // We already performed a redefinition check above, but for static |
| 5517 | // data members we also need to check whether there was an in-class |
| 5518 | // declaration with an initializer. |
| 5519 | const VarDecl* PrevInit = 0; |
| 5520 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { |
| 5521 | Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName(); |
| 5522 | Diag(PrevInit->getLocation(), diag::note_previous_definition); |
| 5523 | return; |
| 5524 | } |
| 5525 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 5526 | // If either the declaration has a dependent type or if any of the |
| 5527 | // expressions is type-dependent, we represent the initialization |
| 5528 | // via a ParenListExpr for later use during template instantiation. |
| 5529 | if (VDecl->getType()->isDependentType() || |
| 5530 | Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { |
| 5531 | // Let clients know that initialization was done with a direct initializer. |
| 5532 | VDecl->setCXXDirectInitializer(true); |
| 5533 | |
| 5534 | // Store the initialization expressions as a ParenListExpr. |
| 5535 | unsigned NumExprs = Exprs.size(); |
| 5536 | VDecl->setInit(new (Context) ParenListExpr(Context, LParenLoc, |
| 5537 | (Expr **)Exprs.release(), |
| 5538 | NumExprs, RParenLoc)); |
| 5539 | return; |
| 5540 | } |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5541 | |
| 5542 | // Capture the variable that is being initialized and the style of |
| 5543 | // initialization. |
| 5544 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 5545 | |
| 5546 | // FIXME: Poor source location information. |
| 5547 | InitializationKind Kind |
| 5548 | = InitializationKind::CreateDirect(VDecl->getLocation(), |
| 5549 | LParenLoc, RParenLoc); |
| 5550 | |
| 5551 | InitializationSequence InitSeq(*this, Entity, Kind, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5552 | Exprs.get(), Exprs.size()); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5553 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(Exprs)); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5554 | if (Result.isInvalid()) { |
| 5555 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5556 | return; |
| 5557 | } |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5558 | |
| 5559 | CheckImplicitConversions(Result.get(), LParenLoc); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5560 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5561 | Result = MaybeCreateCXXExprWithTemporaries(Result.get()); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5562 | VDecl->setInit(Result.takeAs<Expr>()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5563 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 5564 | |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 5565 | if (!VDecl->isInvalidDecl() && |
| 5566 | !VDecl->getDeclContext()->isDependentContext() && |
Sebastian Redl | 36281c6 | 2010-09-08 04:46:19 +0000 | [diff] [blame] | 5567 | VDecl->hasGlobalStorage() && !VDecl->isStaticLocal() && |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 5568 | !VDecl->getInit()->isConstantInitializer(Context, |
| 5569 | VDecl->getType()->isReferenceType())) |
| 5570 | Diag(VDecl->getLocation(), diag::warn_global_constructor) |
| 5571 | << VDecl->getInit()->getSourceRange(); |
| 5572 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 5573 | if (const RecordType *Record = VDecl->getType()->getAs<RecordType>()) |
| 5574 | FinalizeVarWithDestructor(VDecl, Record); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5575 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 5576 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5577 | /// \brief Given a constructor and the set of arguments provided for the |
| 5578 | /// constructor, convert the arguments and add any required default arguments |
| 5579 | /// to form a proper call to this constructor. |
| 5580 | /// |
| 5581 | /// \returns true if an error occurred, false otherwise. |
| 5582 | bool |
| 5583 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 5584 | MultiExprArg ArgsPtr, |
| 5585 | SourceLocation Loc, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5586 | ASTOwningVector<Expr*> &ConvertedArgs) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5587 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 5588 | unsigned NumArgs = ArgsPtr.size(); |
| 5589 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 5590 | |
| 5591 | const FunctionProtoType *Proto |
| 5592 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 5593 | assert(Proto && "Constructor without a prototype?"); |
| 5594 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5595 | |
| 5596 | // 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] | 5597 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5598 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 5599 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5600 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 5601 | |
| 5602 | VariadicCallType CallType = |
| 5603 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 5604 | llvm::SmallVector<Expr *, 8> AllArgs; |
| 5605 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 5606 | Proto, 0, Args, NumArgs, AllArgs, |
| 5607 | CallType); |
| 5608 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 5609 | ConvertedArgs.push_back(AllArgs[i]); |
| 5610 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 5611 | } |
| 5612 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5613 | static inline bool |
| 5614 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 5615 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5616 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5617 | if (isa<NamespaceDecl>(DC)) { |
| 5618 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5619 | diag::err_operator_new_delete_declared_in_namespace) |
| 5620 | << FnDecl->getDeclName(); |
| 5621 | } |
| 5622 | |
| 5623 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5624 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5625 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5626 | diag::err_operator_new_delete_declared_static) |
| 5627 | << FnDecl->getDeclName(); |
| 5628 | } |
| 5629 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 5630 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5631 | } |
| 5632 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5633 | static inline bool |
| 5634 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 5635 | CanQualType ExpectedResultType, |
| 5636 | CanQualType ExpectedFirstParamType, |
| 5637 | unsigned DependentParamTypeDiag, |
| 5638 | unsigned InvalidParamTypeDiag) { |
| 5639 | QualType ResultType = |
| 5640 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5641 | |
| 5642 | // Check that the result type is not dependent. |
| 5643 | if (ResultType->isDependentType()) |
| 5644 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5645 | diag::err_operator_new_delete_dependent_result_type) |
| 5646 | << FnDecl->getDeclName() << ExpectedResultType; |
| 5647 | |
| 5648 | // Check that the result type is what we expect. |
| 5649 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 5650 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5651 | diag::err_operator_new_delete_invalid_result_type) |
| 5652 | << FnDecl->getDeclName() << ExpectedResultType; |
| 5653 | |
| 5654 | // A function template must have at least 2 parameters. |
| 5655 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 5656 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5657 | diag::err_operator_new_delete_template_too_few_parameters) |
| 5658 | << FnDecl->getDeclName(); |
| 5659 | |
| 5660 | // The function decl must have at least 1 parameter. |
| 5661 | if (FnDecl->getNumParams() == 0) |
| 5662 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5663 | diag::err_operator_new_delete_too_few_parameters) |
| 5664 | << FnDecl->getDeclName(); |
| 5665 | |
| 5666 | // Check the the first parameter type is not dependent. |
| 5667 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 5668 | if (FirstParamType->isDependentType()) |
| 5669 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 5670 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 5671 | |
| 5672 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 5673 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5674 | ExpectedFirstParamType) |
| 5675 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 5676 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 5677 | |
| 5678 | return false; |
| 5679 | } |
| 5680 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5681 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5682 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5683 | // C++ [basic.stc.dynamic.allocation]p1: |
| 5684 | // A program is ill-formed if an allocation function is declared in a |
| 5685 | // namespace scope other than global scope or declared static in global |
| 5686 | // scope. |
| 5687 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 5688 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5689 | |
| 5690 | CanQualType SizeTy = |
| 5691 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 5692 | |
| 5693 | // C++ [basic.stc.dynamic.allocation]p1: |
| 5694 | // The return type shall be void*. The first parameter shall have type |
| 5695 | // std::size_t. |
| 5696 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 5697 | SizeTy, |
| 5698 | diag::err_operator_new_dependent_param_type, |
| 5699 | diag::err_operator_new_param_type)) |
| 5700 | return true; |
| 5701 | |
| 5702 | // C++ [basic.stc.dynamic.allocation]p1: |
| 5703 | // The first parameter shall not have an associated default argument. |
| 5704 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5705 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5706 | diag::err_operator_new_default_arg) |
| 5707 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 5708 | |
| 5709 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5710 | } |
| 5711 | |
| 5712 | static bool |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5713 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 5714 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 5715 | // A program is ill-formed if deallocation functions are declared in a |
| 5716 | // namespace scope other than global scope or declared static in global |
| 5717 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5718 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 5719 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5720 | |
| 5721 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 5722 | // Each deallocation function shall return void and its first parameter |
| 5723 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5724 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 5725 | SemaRef.Context.VoidPtrTy, |
| 5726 | diag::err_operator_delete_dependent_param_type, |
| 5727 | diag::err_operator_delete_param_type)) |
| 5728 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5729 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5730 | return false; |
| 5731 | } |
| 5732 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5733 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 5734 | /// of this overloaded operator is well-formed. If so, returns false; |
| 5735 | /// otherwise, emits appropriate diagnostics and returns true. |
| 5736 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5737 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5738 | "Expected an overloaded operator declaration"); |
| 5739 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5740 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 5741 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5742 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5743 | // The allocation and deallocation functions, operator new, |
| 5744 | // operator new[], operator delete and operator delete[], are |
| 5745 | // described completely in 3.7.3. The attributes and restrictions |
| 5746 | // found in the rest of this subclause do not apply to them unless |
| 5747 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 5748 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5749 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 5750 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5751 | if (Op == OO_New || Op == OO_Array_New) |
| 5752 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5753 | |
| 5754 | // C++ [over.oper]p6: |
| 5755 | // An operator function shall either be a non-static member |
| 5756 | // function or be a non-member function and have at least one |
| 5757 | // parameter whose type is a class, a reference to a class, an |
| 5758 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5759 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 5760 | if (MethodDecl->isStatic()) |
| 5761 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5762 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5763 | } else { |
| 5764 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5765 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 5766 | ParamEnd = FnDecl->param_end(); |
| 5767 | Param != ParamEnd; ++Param) { |
| 5768 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 5769 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 5770 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5771 | ClassOrEnumParam = true; |
| 5772 | break; |
| 5773 | } |
| 5774 | } |
| 5775 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5776 | if (!ClassOrEnumParam) |
| 5777 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5778 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5779 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5780 | } |
| 5781 | |
| 5782 | // C++ [over.oper]p8: |
| 5783 | // An operator function cannot have default arguments (8.3.6), |
| 5784 | // except where explicitly stated below. |
| 5785 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5786 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5787 | // (C++ [over.call]p1). |
| 5788 | if (Op != OO_Call) { |
| 5789 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 5790 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5791 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5792 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5793 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5794 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5795 | } |
| 5796 | } |
| 5797 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5798 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 5799 | { false, false, false } |
| 5800 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 5801 | , { Unary, Binary, MemberOnly } |
| 5802 | #include "clang/Basic/OperatorKinds.def" |
| 5803 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5804 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5805 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 5806 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 5807 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5808 | |
| 5809 | // C++ [over.oper]p8: |
| 5810 | // [...] Operator functions cannot have more or fewer parameters |
| 5811 | // than the number required for the corresponding operator, as |
| 5812 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5813 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5814 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5815 | if (Op != OO_Call && |
| 5816 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 5817 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 5818 | (NumParams < 1) || (NumParams > 2))) { |
| 5819 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5820 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5821 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5822 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5823 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5824 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5825 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 5826 | assert(CanBeBinaryOperator && |
| 5827 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5828 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5829 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5830 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5831 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5832 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5833 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5834 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5835 | // Overloaded operators other than operator() cannot be variadic. |
| 5836 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5837 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5838 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5839 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5840 | } |
| 5841 | |
| 5842 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5843 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 5844 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5845 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5846 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5847 | } |
| 5848 | |
| 5849 | // C++ [over.inc]p1: |
| 5850 | // The user-defined function called operator++ implements the |
| 5851 | // prefix and postfix ++ operator. If this function is a member |
| 5852 | // function with no parameters, or a non-member function with one |
| 5853 | // parameter of class or enumeration type, it defines the prefix |
| 5854 | // increment operator ++ for objects of that type. If the function |
| 5855 | // is a member function with one parameter (which shall be of type |
| 5856 | // int) or a non-member function with two parameters (the second |
| 5857 | // of which shall be of type int), it defines the postfix |
| 5858 | // increment operator ++ for objects of that type. |
| 5859 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 5860 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 5861 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5862 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5863 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 5864 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 5865 | if (!ParamIsInt) |
| 5866 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5867 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5868 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5869 | } |
| 5870 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5871 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5872 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 5873 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5874 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 5875 | /// of this literal operator function is well-formed. If so, returns |
| 5876 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 5877 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 5878 | DeclContext *DC = FnDecl->getDeclContext(); |
| 5879 | Decl::Kind Kind = DC->getDeclKind(); |
| 5880 | if (Kind != Decl::TranslationUnit && Kind != Decl::Namespace && |
| 5881 | Kind != Decl::LinkageSpec) { |
| 5882 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 5883 | << FnDecl->getDeclName(); |
| 5884 | return true; |
| 5885 | } |
| 5886 | |
| 5887 | bool Valid = false; |
| 5888 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5889 | // template <char...> type operator "" name() is the only valid template |
| 5890 | // signature, and the only valid signature with no parameters. |
| 5891 | if (FnDecl->param_size() == 0) { |
| 5892 | if (FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate()) { |
| 5893 | // Must have only one template parameter |
| 5894 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 5895 | if (Params->size() == 1) { |
| 5896 | NonTypeTemplateParmDecl *PmDecl = |
| 5897 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5898 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5899 | // The template parameter must be a char parameter pack. |
| 5900 | // FIXME: This test will always fail because non-type parameter packs |
| 5901 | // have not been implemented. |
| 5902 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 5903 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 5904 | Valid = true; |
| 5905 | } |
| 5906 | } |
| 5907 | } else { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5908 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5909 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 5910 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5911 | QualType T = (*Param)->getType(); |
| 5912 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 5913 | // unsigned long long int, long double, and any character type are allowed |
| 5914 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5915 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 5916 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 5917 | Context.hasSameType(T, Context.CharTy) || |
| 5918 | Context.hasSameType(T, Context.WCharTy) || |
| 5919 | Context.hasSameType(T, Context.Char16Ty) || |
| 5920 | Context.hasSameType(T, Context.Char32Ty)) { |
| 5921 | if (++Param == FnDecl->param_end()) |
| 5922 | Valid = true; |
| 5923 | goto FinishedParams; |
| 5924 | } |
| 5925 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 5926 | // 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] | 5927 | const PointerType *PT = T->getAs<PointerType>(); |
| 5928 | if (!PT) |
| 5929 | goto FinishedParams; |
| 5930 | T = PT->getPointeeType(); |
| 5931 | if (!T.isConstQualified()) |
| 5932 | goto FinishedParams; |
| 5933 | T = T.getUnqualifiedType(); |
| 5934 | |
| 5935 | // Move on to the second parameter; |
| 5936 | ++Param; |
| 5937 | |
| 5938 | // If there is no second parameter, the first must be a const char * |
| 5939 | if (Param == FnDecl->param_end()) { |
| 5940 | if (Context.hasSameType(T, Context.CharTy)) |
| 5941 | Valid = true; |
| 5942 | goto FinishedParams; |
| 5943 | } |
| 5944 | |
| 5945 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 5946 | // are allowed as the first parameter to a two-parameter function |
| 5947 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 5948 | Context.hasSameType(T, Context.WCharTy) || |
| 5949 | Context.hasSameType(T, Context.Char16Ty) || |
| 5950 | Context.hasSameType(T, Context.Char32Ty))) |
| 5951 | goto FinishedParams; |
| 5952 | |
| 5953 | // The second and final parameter must be an std::size_t |
| 5954 | T = (*Param)->getType().getUnqualifiedType(); |
| 5955 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 5956 | ++Param == FnDecl->param_end()) |
| 5957 | Valid = true; |
| 5958 | } |
| 5959 | |
| 5960 | // FIXME: This diagnostic is absolutely terrible. |
| 5961 | FinishedParams: |
| 5962 | if (!Valid) { |
| 5963 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 5964 | << FnDecl->getDeclName(); |
| 5965 | return true; |
| 5966 | } |
| 5967 | |
| 5968 | return false; |
| 5969 | } |
| 5970 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5971 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 5972 | /// linkage specification, including the language and (if present) |
| 5973 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 5974 | /// the location of the language string literal, which is provided |
| 5975 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 5976 | /// the '{' brace. Otherwise, this linkage specification does not |
| 5977 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 5978 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 5979 | SourceLocation LangLoc, |
| 5980 | llvm::StringRef Lang, |
| 5981 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5982 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 5983 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5984 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 5985 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5986 | Language = LinkageSpecDecl::lang_cxx; |
| 5987 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5988 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5989 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5990 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5991 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5992 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5993 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5994 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5995 | LangLoc, Language, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5996 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5997 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5998 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5999 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 6000 | } |
| 6001 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 6002 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6003 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 6004 | /// valid, it's the position of the closing '}' brace in a linkage |
| 6005 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6006 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 6007 | Decl *LinkageSpec, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6008 | SourceLocation RBraceLoc) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 6009 | if (LinkageSpec) |
| 6010 | PopDeclContext(); |
| 6011 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 6012 | } |
| 6013 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6014 | /// \brief Perform semantic analysis for the variable declaration that |
| 6015 | /// occurs within a C++ catch clause, returning the newly-created |
| 6016 | /// variable. |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6017 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 6018 | TypeSourceInfo *TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6019 | IdentifierInfo *Name, |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6020 | SourceLocation Loc) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6021 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6022 | QualType ExDeclType = TInfo->getType(); |
| 6023 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6024 | // Arrays and functions decay. |
| 6025 | if (ExDeclType->isArrayType()) |
| 6026 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 6027 | else if (ExDeclType->isFunctionType()) |
| 6028 | ExDeclType = Context.getPointerType(ExDeclType); |
| 6029 | |
| 6030 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 6031 | // The exception-declaration shall not denote a pointer or reference to an |
| 6032 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6033 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6034 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6035 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6036 | Invalid = true; |
| 6037 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6038 | |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6039 | // GCC allows catching pointers and references to incomplete types |
| 6040 | // as an extension; so do we, but we warn by default. |
| 6041 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6042 | QualType BaseType = ExDeclType; |
| 6043 | 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] | 6044 | unsigned DK = diag::err_catch_incomplete; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6045 | bool IncompleteCatchIsInvalid = true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6046 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6047 | BaseType = Ptr->getPointeeType(); |
| 6048 | Mode = 1; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6049 | DK = diag::ext_catch_incomplete_ptr; |
| 6050 | IncompleteCatchIsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6051 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6052 | // 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] | 6053 | BaseType = Ref->getPointeeType(); |
| 6054 | Mode = 2; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6055 | DK = diag::ext_catch_incomplete_ref; |
| 6056 | IncompleteCatchIsInvalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6057 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6058 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6059 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK) && |
| 6060 | IncompleteCatchIsInvalid) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6061 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6062 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6063 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6064 | RequireNonAbstractType(Loc, ExDeclType, |
| 6065 | diag::err_abstract_type_in_decl, |
| 6066 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 6067 | Invalid = true; |
| 6068 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6069 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 6070 | // of ObjC types, and no runtime supports catching ObjC types by value. |
| 6071 | if (!Invalid && getLangOptions().ObjC1) { |
| 6072 | QualType T = ExDeclType; |
| 6073 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 6074 | T = RT->getPointeeType(); |
| 6075 | |
| 6076 | if (T->isObjCObjectType()) { |
| 6077 | Diag(Loc, diag::err_objc_object_catch); |
| 6078 | Invalid = true; |
| 6079 | } else if (T->isObjCObjectPointerType()) { |
| 6080 | if (!getLangOptions().NeXTRuntime) { |
| 6081 | Diag(Loc, diag::err_objc_pointer_cxx_catch_gnu); |
| 6082 | Invalid = true; |
| 6083 | } else if (!getLangOptions().ObjCNonFragileABI) { |
| 6084 | Diag(Loc, diag::err_objc_pointer_cxx_catch_fragile); |
| 6085 | Invalid = true; |
| 6086 | } |
| 6087 | } |
| 6088 | } |
| 6089 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6090 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6091 | Name, ExDeclType, TInfo, SC_None, |
| 6092 | SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 6093 | ExDecl->setExceptionVariable(true); |
| 6094 | |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6095 | if (!Invalid) { |
| 6096 | if (const RecordType *RecordTy = ExDeclType->getAs<RecordType>()) { |
| 6097 | // C++ [except.handle]p16: |
| 6098 | // The object declared in an exception-declaration or, if the |
| 6099 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 6100 | // copy-initialized (8.5) from the exception object. [...] |
| 6101 | // The object is destroyed when the handler exits, after the destruction |
| 6102 | // of any automatic objects initialized within the handler. |
| 6103 | // |
| 6104 | // We just pretend to initialize the object with itself, then make sure |
| 6105 | // it can be destroyed later. |
| 6106 | InitializedEntity Entity = InitializedEntity::InitializeVariable(ExDecl); |
| 6107 | Expr *ExDeclRef = DeclRefExpr::Create(Context, 0, SourceRange(), ExDecl, |
| 6108 | Loc, ExDeclType, 0); |
| 6109 | InitializationKind Kind = InitializationKind::CreateCopy(Loc, |
| 6110 | SourceLocation()); |
| 6111 | InitializationSequence InitSeq(*this, Entity, Kind, &ExDeclRef, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6112 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 6113 | MultiExprArg(*this, &ExDeclRef, 1)); |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6114 | if (Result.isInvalid()) |
| 6115 | Invalid = true; |
| 6116 | else |
| 6117 | FinalizeVarWithDestructor(ExDecl, RecordTy); |
| 6118 | } |
| 6119 | } |
| 6120 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6121 | if (Invalid) |
| 6122 | ExDecl->setInvalidDecl(); |
| 6123 | |
| 6124 | return ExDecl; |
| 6125 | } |
| 6126 | |
| 6127 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 6128 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6129 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6130 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 6131 | QualType ExDeclType = TInfo->getType(); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6132 | |
| 6133 | bool Invalid = D.isInvalidType(); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6134 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6135 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 6136 | LookupOrdinaryName, |
| 6137 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6138 | // The scope should be freshly made just for us. There is just no way |
| 6139 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6140 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6141 | if (PrevDecl->isTemplateParameter()) { |
| 6142 | // Maybe we will complain about the shadowed template parameter. |
| 6143 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6144 | } |
| 6145 | } |
| 6146 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6147 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6148 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 6149 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6150 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6151 | } |
| 6152 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6153 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6154 | D.getIdentifier(), |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6155 | D.getIdentifierLoc()); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6156 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6157 | if (Invalid) |
| 6158 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6159 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6160 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6161 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6162 | PushOnScopeChains(ExDecl, S); |
| 6163 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6164 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6165 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6166 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6167 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6168 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6169 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6170 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6171 | Expr *AssertExpr, |
| 6172 | Expr *AssertMessageExpr_) { |
| 6173 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr_); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6174 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6175 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 6176 | llvm::APSInt Value(32); |
| 6177 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 6178 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 6179 | AssertExpr->getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6180 | return 0; |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6181 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6182 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6183 | if (Value == 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6184 | Diag(AssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 6185 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6186 | } |
| 6187 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6188 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6189 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6190 | AssertExpr, AssertMessage); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6191 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6192 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6193 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6194 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6195 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6196 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 6197 | /// |
| 6198 | /// \returns A friend declaration that. |
| 6199 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation FriendLoc, |
| 6200 | TypeSourceInfo *TSInfo) { |
| 6201 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 6202 | |
| 6203 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6204 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6205 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 6206 | if (!getLangOptions().CPlusPlus0x) { |
| 6207 | // C++03 [class.friend]p2: |
| 6208 | // An elaborated-type-specifier shall be used in a friend declaration |
| 6209 | // for a class.* |
| 6210 | // |
| 6211 | // * The class-key of the elaborated-type-specifier is required. |
| 6212 | if (!ActiveTemplateInstantiations.empty()) { |
| 6213 | // Do not complain about the form of friend template types during |
| 6214 | // template instantiation; we will already have complained when the |
| 6215 | // template was declared. |
| 6216 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 6217 | // If we evaluated the type to a record type, suggest putting |
| 6218 | // a tag in front. |
| 6219 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 6220 | RecordDecl *RD = RT->getDecl(); |
| 6221 | |
| 6222 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 6223 | |
| 6224 | Diag(TypeRange.getBegin(), diag::ext_unelaborated_friend_type) |
| 6225 | << (unsigned) RD->getTagKind() |
| 6226 | << T |
| 6227 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 6228 | InsertionText); |
| 6229 | } else { |
| 6230 | Diag(FriendLoc, diag::ext_nonclass_type_friend) |
| 6231 | << T |
| 6232 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
| 6233 | } |
| 6234 | } else if (T->getAs<EnumType>()) { |
| 6235 | Diag(FriendLoc, diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6236 | << T |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6237 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6238 | } |
| 6239 | } |
| 6240 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 6241 | // C++0x [class.friend]p3: |
| 6242 | // If the type specifier in a friend declaration designates a (possibly |
| 6243 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 6244 | // the friend declaration is ignored. |
| 6245 | |
| 6246 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 6247 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6248 | |
| 6249 | return FriendDecl::Create(Context, CurContext, FriendLoc, TSInfo, FriendLoc); |
| 6250 | } |
| 6251 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 6252 | /// Handle a friend tag declaration where the scope specifier was |
| 6253 | /// templated. |
| 6254 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 6255 | unsigned TagSpec, SourceLocation TagLoc, |
| 6256 | CXXScopeSpec &SS, |
| 6257 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 6258 | AttributeList *Attr, |
| 6259 | MultiTemplateParamsArg TempParamLists) { |
| 6260 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6261 | |
| 6262 | bool isExplicitSpecialization = false; |
| 6263 | unsigned NumMatchedTemplateParamLists = TempParamLists.size(); |
| 6264 | bool Invalid = false; |
| 6265 | |
| 6266 | if (TemplateParameterList *TemplateParams |
| 6267 | = MatchTemplateParametersToScopeSpecifier(TagLoc, SS, |
| 6268 | TempParamLists.get(), |
| 6269 | TempParamLists.size(), |
| 6270 | /*friend*/ true, |
| 6271 | isExplicitSpecialization, |
| 6272 | Invalid)) { |
| 6273 | --NumMatchedTemplateParamLists; |
| 6274 | |
| 6275 | if (TemplateParams->size() > 0) { |
| 6276 | // This is a declaration of a class template. |
| 6277 | if (Invalid) |
| 6278 | return 0; |
| 6279 | |
| 6280 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 6281 | SS, Name, NameLoc, Attr, |
| 6282 | TemplateParams, AS_public).take(); |
| 6283 | } else { |
| 6284 | // The "template<>" header is extraneous. |
| 6285 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 6286 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 6287 | isExplicitSpecialization = true; |
| 6288 | } |
| 6289 | } |
| 6290 | |
| 6291 | if (Invalid) return 0; |
| 6292 | |
| 6293 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 6294 | |
| 6295 | bool isAllExplicitSpecializations = true; |
| 6296 | for (unsigned I = 0; I != NumMatchedTemplateParamLists; ++I) { |
| 6297 | if (TempParamLists.get()[I]->size()) { |
| 6298 | isAllExplicitSpecializations = false; |
| 6299 | break; |
| 6300 | } |
| 6301 | } |
| 6302 | |
| 6303 | // FIXME: don't ignore attributes. |
| 6304 | |
| 6305 | // If it's explicit specializations all the way down, just forget |
| 6306 | // about the template header and build an appropriate non-templated |
| 6307 | // friend. TODO: for source fidelity, remember the headers. |
| 6308 | if (isAllExplicitSpecializations) { |
| 6309 | ElaboratedTypeKeyword Keyword |
| 6310 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 6311 | QualType T = CheckTypenameType(Keyword, SS.getScopeRep(), *Name, |
| 6312 | TagLoc, SS.getRange(), NameLoc); |
| 6313 | if (T.isNull()) |
| 6314 | return 0; |
| 6315 | |
| 6316 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6317 | if (isa<DependentNameType>(T)) { |
| 6318 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 6319 | TL.setKeywordLoc(TagLoc); |
| 6320 | TL.setQualifierRange(SS.getRange()); |
| 6321 | TL.setNameLoc(NameLoc); |
| 6322 | } else { |
| 6323 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
| 6324 | TL.setKeywordLoc(TagLoc); |
| 6325 | TL.setQualifierRange(SS.getRange()); |
| 6326 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(NameLoc); |
| 6327 | } |
| 6328 | |
| 6329 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 6330 | TSI, FriendLoc); |
| 6331 | Friend->setAccess(AS_public); |
| 6332 | CurContext->addDecl(Friend); |
| 6333 | return Friend; |
| 6334 | } |
| 6335 | |
| 6336 | // Handle the case of a templated-scope friend class. e.g. |
| 6337 | // template <class T> class A<T>::B; |
| 6338 | // FIXME: we don't support these right now. |
| 6339 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 6340 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 6341 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6342 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 6343 | TL.setKeywordLoc(TagLoc); |
| 6344 | TL.setQualifierRange(SS.getRange()); |
| 6345 | TL.setNameLoc(NameLoc); |
| 6346 | |
| 6347 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 6348 | TSI, FriendLoc); |
| 6349 | Friend->setAccess(AS_public); |
| 6350 | Friend->setUnsupportedFriend(true); |
| 6351 | CurContext->addDecl(Friend); |
| 6352 | return Friend; |
| 6353 | } |
| 6354 | |
| 6355 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6356 | /// Handle a friend type declaration. This works in tandem with |
| 6357 | /// ActOnTag. |
| 6358 | /// |
| 6359 | /// Notes on friend class templates: |
| 6360 | /// |
| 6361 | /// We generally treat friend class declarations as if they were |
| 6362 | /// declaring a class. So, for example, the elaborated type specifier |
| 6363 | /// in a friend declaration is required to obey the restrictions of a |
| 6364 | /// class-head (i.e. no typedefs in the scope chain), template |
| 6365 | /// parameters are required to match up with simple template-ids, &c. |
| 6366 | /// However, unlike when declaring a template specialization, it's |
| 6367 | /// okay to refer to a template specialization without an empty |
| 6368 | /// template parameter declaration, e.g. |
| 6369 | /// friend class A<T>::B<unsigned>; |
| 6370 | /// We permit this as a special case; if there are any template |
| 6371 | /// parameters present at all, require proper matching, i.e. |
| 6372 | /// template <> template <class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6373 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 6374 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6375 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6376 | |
| 6377 | assert(DS.isFriendSpecified()); |
| 6378 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 6379 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6380 | // Try to convert the decl specifier to a type. This works for |
| 6381 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 6382 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 6383 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6384 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 6385 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 6386 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6387 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6388 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6389 | // This is definitely an error in C++98. It's probably meant to |
| 6390 | // be forbidden in C++0x, too, but the specification is just |
| 6391 | // poorly written. |
| 6392 | // |
| 6393 | // The problem is with declarations like the following: |
| 6394 | // template <T> friend A<T>::foo; |
| 6395 | // where deciding whether a class C is a friend or not now hinges |
| 6396 | // on whether there exists an instantiation of A that causes |
| 6397 | // 'foo' to equal C. There are restrictions on class-heads |
| 6398 | // (which we declare (by fiat) elaborated friend declarations to |
| 6399 | // be) that makes this tractable. |
| 6400 | // |
| 6401 | // FIXME: handle "template <> friend class A<T>;", which |
| 6402 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 6403 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6404 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 6405 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6406 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6407 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6408 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6409 | // C++98 [class.friend]p1: A friend of a class is a function |
| 6410 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 6411 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 6412 | // deadline. It's also a very silly restriction that seriously |
| 6413 | // affects inner classes and which nobody else seems to implement; |
| 6414 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6415 | // |
| 6416 | // But note that we could warn about it: it's always useless to |
| 6417 | // friend one of your own members (it's not, however, worthless to |
| 6418 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6419 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6420 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6421 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6422 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6423 | NumTempParamLists, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 6424 | TempParams.release(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6425 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6426 | DS.getFriendSpecLoc()); |
| 6427 | else |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6428 | D = CheckFriendTypeDecl(DS.getFriendSpecLoc(), TSI); |
| 6429 | |
| 6430 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6431 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6432 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6433 | D->setAccess(AS_public); |
| 6434 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6435 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6436 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6437 | } |
| 6438 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6439 | Decl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, bool IsDefinition, |
| 6440 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6441 | const DeclSpec &DS = D.getDeclSpec(); |
| 6442 | |
| 6443 | assert(DS.isFriendSpecified()); |
| 6444 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 6445 | |
| 6446 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6447 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 6448 | QualType T = TInfo->getType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6449 | |
| 6450 | // C++ [class.friend]p1 |
| 6451 | // A friend of a class is a function or class.... |
| 6452 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6453 | // It *doesn't* see through dependent types, which is correct |
| 6454 | // according to [temp.arg.type]p3: |
| 6455 | // If a declaration acquires a function type through a |
| 6456 | // type dependent on a template-parameter and this causes |
| 6457 | // a declaration that does not use the syntactic form of a |
| 6458 | // function declarator to have a function type, the program |
| 6459 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6460 | if (!T->isFunctionType()) { |
| 6461 | Diag(Loc, diag::err_unexpected_friend); |
| 6462 | |
| 6463 | // It might be worthwhile to try to recover by creating an |
| 6464 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6465 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6466 | } |
| 6467 | |
| 6468 | // C++ [namespace.memdef]p3 |
| 6469 | // - If a friend declaration in a non-local class first declares a |
| 6470 | // class or function, the friend class or function is a member |
| 6471 | // of the innermost enclosing namespace. |
| 6472 | // - The name of the friend is not found by simple name lookup |
| 6473 | // until a matching declaration is provided in that namespace |
| 6474 | // scope (either before or after the class declaration granting |
| 6475 | // friendship). |
| 6476 | // - If a friend function is called, its name may be found by the |
| 6477 | // name lookup that considers functions from namespaces and |
| 6478 | // classes associated with the types of the function arguments. |
| 6479 | // - When looking for a prior declaration of a class or a function |
| 6480 | // declared as a friend, scopes outside the innermost enclosing |
| 6481 | // namespace scope are not considered. |
| 6482 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6483 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6484 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6485 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6486 | assert(Name); |
| 6487 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6488 | // The context we found the declaration in, or in which we should |
| 6489 | // create the declaration. |
| 6490 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 6491 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6492 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6493 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6494 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6495 | // FIXME: there are different rules in local classes |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6496 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6497 | // There are four cases here. |
| 6498 | // - 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] | 6499 | // appropriate scope and look for a function or function template |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6500 | // there as appropriate. |
| 6501 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 6502 | if (SS.isInvalid() || !SS.isSet()) { |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6503 | // C++0x [namespace.memdef]p3: |
| 6504 | // If the name in a friend declaration is neither qualified nor |
| 6505 | // a template-id and the declaration is a function or an |
| 6506 | // elaborated-type-specifier, the lookup to determine whether |
| 6507 | // the entity has been previously declared shall not consider |
| 6508 | // any scopes outside the innermost enclosing namespace. |
| 6509 | // C++0x [class.friend]p11: |
| 6510 | // If a friend declaration appears in a local class and the name |
| 6511 | // specified is an unqualified name, a prior declaration is |
| 6512 | // looked up without considering scopes that are outside the |
| 6513 | // innermost enclosing non-class scope. For a friend function |
| 6514 | // declaration, if there is no prior declaration, the program is |
| 6515 | // ill-formed. |
| 6516 | bool isLocal = cast<CXXRecordDecl>(CurContext)->isLocalClass(); |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 6517 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6518 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6519 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6520 | DC = CurContext; |
| 6521 | while (true) { |
| 6522 | // Skip class contexts. If someone can cite chapter and verse |
| 6523 | // for this behavior, that would be nice --- it's what GCC and |
| 6524 | // EDG do, and it seems like a reasonable intent, but the spec |
| 6525 | // really only says that checks for unqualified existing |
| 6526 | // declarations should stop at the nearest enclosing namespace, |
| 6527 | // not that they should only consider the nearest enclosing |
| 6528 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6529 | while (DC->isRecord()) |
| 6530 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6531 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6532 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6533 | |
| 6534 | // TODO: decide what we think about using declarations. |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6535 | if (isLocal || !Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6536 | break; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6537 | |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 6538 | if (isTemplateId) { |
| 6539 | if (isa<TranslationUnitDecl>(DC)) break; |
| 6540 | } else { |
| 6541 | if (DC->isFileContext()) break; |
| 6542 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6543 | DC = DC->getParent(); |
| 6544 | } |
| 6545 | |
| 6546 | // C++ [class.friend]p1: A friend of a class is a function or |
| 6547 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 6548 | // C++0x changes this for both friend types and functions. |
| 6549 | // Most C++ 98 compilers do seem to give an error here, so |
| 6550 | // we do, too. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6551 | if (!Previous.empty() && DC->Equals(CurContext) |
| 6552 | && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6553 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6554 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 6555 | DCScope = getScopeForDeclContext(S, DC); |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6556 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6557 | // - There's a non-dependent scope specifier, in which case we |
| 6558 | // compute it and do a previous lookup there for a function |
| 6559 | // or function template. |
| 6560 | } else if (!SS.getScopeRep()->isDependent()) { |
| 6561 | DC = computeDeclContext(SS); |
| 6562 | if (!DC) return 0; |
| 6563 | |
| 6564 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 6565 | |
| 6566 | LookupQualifiedName(Previous, DC); |
| 6567 | |
| 6568 | // Ignore things found implicitly in the wrong scope. |
| 6569 | // TODO: better diagnostics for this case. Suggesting the right |
| 6570 | // qualified scope would be nice... |
| 6571 | LookupResult::Filter F = Previous.makeFilter(); |
| 6572 | while (F.hasNext()) { |
| 6573 | NamedDecl *D = F.next(); |
| 6574 | if (!DC->InEnclosingNamespaceSetOf( |
| 6575 | D->getDeclContext()->getRedeclContext())) |
| 6576 | F.erase(); |
| 6577 | } |
| 6578 | F.done(); |
| 6579 | |
| 6580 | if (Previous.empty()) { |
| 6581 | D.setInvalidType(); |
| 6582 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 6583 | return 0; |
| 6584 | } |
| 6585 | |
| 6586 | // C++ [class.friend]p1: A friend of a class is a function or |
| 6587 | // class that is not a member of the class . . . |
| 6588 | if (DC->Equals(CurContext)) |
| 6589 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 6590 | |
| 6591 | // - There's a scope specifier that does not match any template |
| 6592 | // parameter lists, in which case we use some arbitrary context, |
| 6593 | // create a method or method template, and wait for instantiation. |
| 6594 | // - There's a scope specifier that does match some template |
| 6595 | // parameter lists, which we don't handle right now. |
| 6596 | } else { |
| 6597 | DC = CurContext; |
| 6598 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6599 | } |
| 6600 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6601 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6602 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6603 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 6604 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 6605 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6606 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6607 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 6608 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6609 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6610 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6611 | } |
| 6612 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6613 | bool Redeclaration = false; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 6614 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, T, TInfo, Previous, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6615 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 6616 | IsDefinition, |
| 6617 | Redeclaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6618 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6619 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6620 | assert(ND->getDeclContext() == DC); |
| 6621 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 6622 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6623 | // Add the function declaration to the appropriate lookup tables, |
| 6624 | // adjusting the redeclarations list as necessary. We don't |
| 6625 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6626 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6627 | // Also update the scope-based lookup if the target context's |
| 6628 | // lookup context is in lexical scope. |
| 6629 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6630 | DC = DC->getRedeclContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6631 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6632 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6633 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6634 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6635 | |
| 6636 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6637 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6638 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 6639 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6640 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6641 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6642 | if (ND->isInvalidDecl()) |
| 6643 | FrD->setInvalidDecl(); |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 6644 | else { |
| 6645 | FunctionDecl *FD; |
| 6646 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 6647 | FD = FTD->getTemplatedDecl(); |
| 6648 | else |
| 6649 | FD = cast<FunctionDecl>(ND); |
| 6650 | |
| 6651 | // Mark templated-scope function declarations as unsupported. |
| 6652 | if (FD->getNumTemplateParameterLists()) |
| 6653 | FrD->setUnsupportedFriend(true); |
| 6654 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6655 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6656 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 6657 | } |
| 6658 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6659 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 6660 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6661 | |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6662 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 6663 | if (!Fn) { |
| 6664 | Diag(DelLoc, diag::err_deleted_non_function); |
| 6665 | return; |
| 6666 | } |
| 6667 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 6668 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 6669 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 6670 | // If the declaration wasn't the first, we delete the function anyway for |
| 6671 | // recovery. |
| 6672 | } |
| 6673 | Fn->setDeleted(); |
| 6674 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 6675 | |
| 6676 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 6677 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 6678 | ++CI) { |
| 6679 | Stmt *SubStmt = *CI; |
| 6680 | if (!SubStmt) |
| 6681 | continue; |
| 6682 | if (isa<ReturnStmt>(SubStmt)) |
| 6683 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 6684 | diag::err_return_in_constructor_handler); |
| 6685 | if (!isa<Expr>(SubStmt)) |
| 6686 | SearchForReturnInStmt(Self, SubStmt); |
| 6687 | } |
| 6688 | } |
| 6689 | |
| 6690 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 6691 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 6692 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 6693 | SearchForReturnInStmt(*this, Handler); |
| 6694 | } |
| 6695 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6696 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6697 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6698 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6699 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 6700 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6701 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 6702 | if (Context.hasSameType(NewTy, OldTy) || |
| 6703 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6704 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6705 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6706 | // Check if the return types are covariant |
| 6707 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6708 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6709 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 6710 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 6711 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6712 | NewClassTy = NewPT->getPointeeType(); |
| 6713 | OldClassTy = OldPT->getPointeeType(); |
| 6714 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 6715 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 6716 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 6717 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 6718 | NewClassTy = NewRT->getPointeeType(); |
| 6719 | OldClassTy = OldRT->getPointeeType(); |
| 6720 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6721 | } |
| 6722 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6723 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6724 | // The return types aren't either both pointers or references to a class type. |
| 6725 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6726 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6727 | diag::err_different_return_type_for_overriding_virtual_function) |
| 6728 | << New->getDeclName() << NewTy << OldTy; |
| 6729 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6730 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6731 | return true; |
| 6732 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6733 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 6734 | // C++ [class.virtual]p6: |
| 6735 | // If the return type of D::f differs from the return type of B::f, the |
| 6736 | // class type in the return type of D::f shall be complete at the point of |
| 6737 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 6738 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 6739 | if (!RT->isBeingDefined() && |
| 6740 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 6741 | PDiag(diag::err_covariant_return_incomplete) |
| 6742 | << New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 6743 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 6744 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 6745 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6746 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6747 | // Check if the new class derives from the old class. |
| 6748 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 6749 | Diag(New->getLocation(), |
| 6750 | diag::err_covariant_return_not_derived) |
| 6751 | << New->getDeclName() << NewTy << OldTy; |
| 6752 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6753 | return true; |
| 6754 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6755 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6756 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 6757 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 6758 | diag::err_covariant_return_inaccessible_base, |
| 6759 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 6760 | // FIXME: Should this point to the return type? |
| 6761 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6762 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6763 | return true; |
| 6764 | } |
| 6765 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6766 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6767 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 6768 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6769 | Diag(New->getLocation(), |
| 6770 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6771 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6772 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6773 | return true; |
| 6774 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6775 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6776 | |
| 6777 | // The new class type must have the same or less qualifiers as the old type. |
| 6778 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 6779 | Diag(New->getLocation(), |
| 6780 | diag::err_covariant_return_type_class_type_more_qualified) |
| 6781 | << New->getDeclName() << NewTy << OldTy; |
| 6782 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6783 | return true; |
| 6784 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6785 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6786 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6787 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6788 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 6789 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
| 6790 | const CXXMethodDecl *Old) |
| 6791 | { |
| 6792 | if (Old->hasAttr<FinalAttr>()) { |
| 6793 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 6794 | << New->getDeclName(); |
| 6795 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6796 | return true; |
| 6797 | } |
| 6798 | |
| 6799 | return false; |
| 6800 | } |
| 6801 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 6802 | /// \brief Mark the given method pure. |
| 6803 | /// |
| 6804 | /// \param Method the method to be marked pure. |
| 6805 | /// |
| 6806 | /// \param InitRange the source range that covers the "0" initializer. |
| 6807 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
| 6808 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 6809 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 6810 | return false; |
| 6811 | } |
| 6812 | |
| 6813 | if (!Method->isInvalidDecl()) |
| 6814 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 6815 | << Method->getDeclName() << InitRange; |
| 6816 | return true; |
| 6817 | } |
| 6818 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6819 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 6820 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 6821 | /// is a fresh scope pushed for just this purpose. |
| 6822 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6823 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 6824 | /// static data member of class X, names should be looked up in the scope of |
| 6825 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6826 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6827 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6828 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6829 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6830 | // We should only get called for declarations with scope specifiers, like: |
| 6831 | // int foo::bar; |
| 6832 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6833 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6834 | } |
| 6835 | |
| 6836 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6837 | /// initializer for the out-of-line declaration 'D'. |
| 6838 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6839 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6840 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6841 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6842 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6843 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6844 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6845 | |
| 6846 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 6847 | /// C++ if/switch/while/for statement. |
| 6848 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6849 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6850 | // C++ 6.4p2: |
| 6851 | // The declarator shall not specify a function or an array. |
| 6852 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 6853 | // new class or enumeration. |
| 6854 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 6855 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 6856 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6857 | TagDecl *OwnedTag = 0; |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6858 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag); |
| 6859 | QualType Ty = TInfo->getType(); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6860 | |
| 6861 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 6862 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 6863 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 6864 | Diag(D.getIdentifierLoc(), diag::err_invalid_use_of_function_type) |
| 6865 | << D.getSourceRange(); |
| 6866 | return DeclResult(); |
| 6867 | } else if (OwnedTag && OwnedTag->isDefinition()) { |
| 6868 | // The type-specifier-seq shall not declare a new class or enumeration. |
| 6869 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition); |
| 6870 | } |
| 6871 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6872 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6873 | if (!Dcl) |
| 6874 | return DeclResult(); |
| 6875 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6876 | return Dcl; |
| 6877 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6878 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6879 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 6880 | bool DefinitionRequired) { |
| 6881 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 6882 | // not have a vtable. |
| 6883 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 6884 | CurContext->isDependentContext() || |
| 6885 | ExprEvalContexts.back().Context == Unevaluated) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 6886 | return; |
| 6887 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6888 | // Try to insert this class into the map. |
| 6889 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 6890 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 6891 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 6892 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 6893 | // If we already had an entry, check to see if we are promoting this vtable |
| 6894 | // to required a definition. If so, we need to reappend to the VTableUses |
| 6895 | // list, since we may have already processed the first entry. |
| 6896 | if (DefinitionRequired && !Pos.first->second) { |
| 6897 | Pos.first->second = true; |
| 6898 | } else { |
| 6899 | // Otherwise, we can early exit. |
| 6900 | return; |
| 6901 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6902 | } |
| 6903 | |
| 6904 | // Local classes need to have their virtual members marked |
| 6905 | // immediately. For all other classes, we mark their virtual members |
| 6906 | // at the end of the translation unit. |
| 6907 | if (Class->isLocalClass()) |
| 6908 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 6909 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6910 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 6911 | } |
| 6912 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6913 | bool Sema::DefineUsedVTables() { |
| 6914 | // If any dynamic classes have their key function defined within |
| 6915 | // this translation unit, then those vtables are considered "used" and must |
| 6916 | // be emitted. |
| 6917 | for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) { |
| 6918 | if (const CXXMethodDecl *KeyFunction |
| 6919 | = Context.getKeyFunction(DynamicClasses[I])) { |
| 6920 | const FunctionDecl *Definition = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 6921 | if (KeyFunction->hasBody(Definition)) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6922 | MarkVTableUsed(Definition->getLocation(), DynamicClasses[I], true); |
| 6923 | } |
| 6924 | } |
| 6925 | |
| 6926 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6927 | return false; |
| 6928 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6929 | // Note: The VTableUses vector could grow as a result of marking |
| 6930 | // the members of a class as "used", so we check the size each |
| 6931 | // time through the loop and prefer indices (with are stable) to |
| 6932 | // iterators (which are not). |
| 6933 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 6934 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6935 | if (!Class) |
| 6936 | continue; |
| 6937 | |
| 6938 | SourceLocation Loc = VTableUses[I].second; |
| 6939 | |
| 6940 | // If this class has a key function, but that key function is |
| 6941 | // defined in another translation unit, we don't need to emit the |
| 6942 | // vtable even though we're using it. |
| 6943 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 6944 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6945 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 6946 | case TSK_Undeclared: |
| 6947 | case TSK_ExplicitSpecialization: |
| 6948 | case TSK_ExplicitInstantiationDeclaration: |
| 6949 | // The key function is in another translation unit. |
| 6950 | continue; |
| 6951 | |
| 6952 | case TSK_ExplicitInstantiationDefinition: |
| 6953 | case TSK_ImplicitInstantiation: |
| 6954 | // We will be instantiating the key function. |
| 6955 | break; |
| 6956 | } |
| 6957 | } else if (!KeyFunction) { |
| 6958 | // If we have a class with no key function that is the subject |
| 6959 | // of an explicit instantiation declaration, suppress the |
| 6960 | // vtable; it will live with the explicit instantiation |
| 6961 | // definition. |
| 6962 | bool IsExplicitInstantiationDeclaration |
| 6963 | = Class->getTemplateSpecializationKind() |
| 6964 | == TSK_ExplicitInstantiationDeclaration; |
| 6965 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 6966 | REnd = Class->redecls_end(); |
| 6967 | R != REnd; ++R) { |
| 6968 | TemplateSpecializationKind TSK |
| 6969 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 6970 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 6971 | IsExplicitInstantiationDeclaration = true; |
| 6972 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 6973 | IsExplicitInstantiationDeclaration = false; |
| 6974 | break; |
| 6975 | } |
| 6976 | } |
| 6977 | |
| 6978 | if (IsExplicitInstantiationDeclaration) |
| 6979 | continue; |
| 6980 | } |
| 6981 | |
| 6982 | // Mark all of the virtual members of this class as referenced, so |
| 6983 | // that we can build a vtable. Then, tell the AST consumer that a |
| 6984 | // vtable for this class is required. |
| 6985 | MarkVirtualMembersReferenced(Loc, Class); |
| 6986 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 6987 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 6988 | |
| 6989 | // Optionally warn if we're emitting a weak vtable. |
| 6990 | if (Class->getLinkage() == ExternalLinkage && |
| 6991 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 6992 | if (!KeyFunction || (KeyFunction->hasBody() && KeyFunction->isInlined())) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6993 | Diag(Class->getLocation(), diag::warn_weak_vtable) << Class; |
| 6994 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6995 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6996 | VTableUses.clear(); |
| 6997 | |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6998 | return true; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6999 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7000 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 7001 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 7002 | const CXXRecordDecl *RD) { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7003 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 7004 | e = RD->method_end(); i != e; ++i) { |
| 7005 | CXXMethodDecl *MD = *i; |
| 7006 | |
| 7007 | // C++ [basic.def.odr]p2: |
| 7008 | // [...] A virtual member function is used if it is not pure. [...] |
| 7009 | if (MD->isVirtual() && !MD->isPure()) |
| 7010 | MarkDeclarationReferenced(Loc, MD); |
| 7011 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 7012 | |
| 7013 | // Only classes that have virtual bases need a VTT. |
| 7014 | if (RD->getNumVBases() == 0) |
| 7015 | return; |
| 7016 | |
| 7017 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 7018 | e = RD->bases_end(); i != e; ++i) { |
| 7019 | const CXXRecordDecl *Base = |
| 7020 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 7021 | if (Base->getNumVBases() == 0) |
| 7022 | continue; |
| 7023 | MarkVirtualMembersReferenced(Loc, Base); |
| 7024 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7025 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7026 | |
| 7027 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 7028 | /// Objective-C implementation whose ivars need be initialized. |
| 7029 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 7030 | if (!getLangOptions().CPlusPlus) |
| 7031 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 7032 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7033 | llvm::SmallVector<ObjCIvarDecl*, 8> ivars; |
| 7034 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 7035 | if (ivars.empty()) |
| 7036 | return; |
| 7037 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
| 7038 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 7039 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 7040 | if (Field->isInvalidDecl()) |
| 7041 | continue; |
| 7042 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7043 | CXXBaseOrMemberInitializer *Member; |
| 7044 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 7045 | InitializationKind InitKind = |
| 7046 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 7047 | |
| 7048 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7049 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7050 | InitSeq.Perform(*this, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7051 | MemberInit = MaybeCreateCXXExprWithTemporaries(MemberInit.get()); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7052 | // Note, MemberInit could actually come back empty if no initialization |
| 7053 | // is required (e.g., because it would call a trivial default constructor) |
| 7054 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 7055 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7056 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7057 | Member = |
| 7058 | new (Context) CXXBaseOrMemberInitializer(Context, |
| 7059 | Field, SourceLocation(), |
| 7060 | SourceLocation(), |
| 7061 | MemberInit.takeAs<Expr>(), |
| 7062 | SourceLocation()); |
| 7063 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 7064 | |
| 7065 | // Be sure that the destructor is accessible and is marked as referenced. |
| 7066 | if (const RecordType *RecordTy |
| 7067 | = Context.getBaseElementType(Field->getType()) |
| 7068 | ->getAs<RecordType>()) { |
| 7069 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 7070 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 7071 | MarkDeclarationReferenced(Field->getLocation(), Destructor); |
| 7072 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 7073 | PDiag(diag::err_access_dtor_ivar) |
| 7074 | << Context.getBaseElementType(Field->getType())); |
| 7075 | } |
| 7076 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7077 | } |
| 7078 | ObjCImplementation->setIvarInitializers(Context, |
| 7079 | AllToInit.data(), AllToInit.size()); |
| 7080 | } |
| 7081 | } |