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. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 93 | if (VDecl->isBlockVarDecl()) |
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(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 846 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 847 | Expr *Init = static_cast<Expr*>(InitExpr); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 848 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 849 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 850 | assert(!DS.isFriendSpecified()); |
| 851 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 852 | bool isFunc = false; |
| 853 | if (D.isFunctionDeclarator()) |
| 854 | isFunc = true; |
| 855 | else if (D.getNumTypeObjects() == 0 && |
| 856 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 857 | QualType TDType = GetTypeFromParser(DS.getRepAsType()); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 858 | isFunc = TDType->isFunctionType(); |
| 859 | } |
| 860 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 861 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 862 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 863 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 864 | // data members and cannot be applied to names declared const or static, |
| 865 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 866 | switch (DS.getStorageClassSpec()) { |
| 867 | case DeclSpec::SCS_unspecified: |
| 868 | case DeclSpec::SCS_typedef: |
| 869 | case DeclSpec::SCS_static: |
| 870 | // FALL THROUGH. |
| 871 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 872 | case DeclSpec::SCS_mutable: |
| 873 | if (isFunc) { |
| 874 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 875 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 876 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 877 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 879 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 880 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 881 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 882 | } |
| 883 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 884 | default: |
| 885 | if (DS.getStorageClassSpecLoc().isValid()) |
| 886 | Diag(DS.getStorageClassSpecLoc(), |
| 887 | diag::err_storageclass_invalid_for_member); |
| 888 | else |
| 889 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 890 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 891 | } |
| 892 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 893 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 894 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 895 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 896 | |
| 897 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 898 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 899 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 900 | |
| 901 | |
| 902 | if (SS.isSet() && !SS.isInvalid()) { |
| 903 | // The user provided a superfluous scope specifier inside a class |
| 904 | // definition: |
| 905 | // |
| 906 | // class X { |
| 907 | // int X::member; |
| 908 | // }; |
| 909 | DeclContext *DC = 0; |
| 910 | if ((DC = computeDeclContext(SS, false)) && DC->Equals(CurContext)) |
| 911 | Diag(D.getIdentifierLoc(), diag::warn_member_extra_qualification) |
| 912 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
| 913 | else |
| 914 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 915 | << Name << SS.getRange(); |
| 916 | |
| 917 | SS.clear(); |
| 918 | } |
| 919 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 920 | // FIXME: Check for template parameters! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 921 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 922 | AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 923 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 924 | } else { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 925 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 926 | if (!Member) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 927 | return 0; |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 928 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 929 | |
| 930 | // Non-instance-fields can't have a bitfield. |
| 931 | if (BitWidth) { |
| 932 | if (Member->isInvalidDecl()) { |
| 933 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 934 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 935 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 936 | // "static member 'A' cannot be a bit-field" |
| 937 | Diag(Loc, diag::err_static_not_bitfield) |
| 938 | << Name << BitWidth->getSourceRange(); |
| 939 | } else if (isa<TypedefDecl>(Member)) { |
| 940 | // "typedef member 'x' cannot be a bit-field" |
| 941 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 942 | << Name << BitWidth->getSourceRange(); |
| 943 | } else { |
| 944 | // A function typedef ("typedef int f(); f a;"). |
| 945 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 946 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 948 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 949 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 951 | BitWidth = 0; |
| 952 | Member->setInvalidDecl(); |
| 953 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 954 | |
| 955 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 957 | // If we have declared a member function template, set the access of the |
| 958 | // templated declaration as well. |
| 959 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 960 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 961 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 962 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 963 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 964 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 965 | if (Init) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 966 | AddInitializerToDecl(Member, Init, false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 967 | if (Deleted) // FIXME: Source location is not very good. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 968 | SetDeclDeleted(Member, D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 969 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 970 | if (isInstField) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 971 | FieldCollector->Add(cast<FieldDecl>(Member)); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 972 | return 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 973 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 974 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 977 | /// \brief Find the direct and/or virtual base specifiers that |
| 978 | /// correspond to the given base type, for use in base initialization |
| 979 | /// within a constructor. |
| 980 | static bool FindBaseInitializer(Sema &SemaRef, |
| 981 | CXXRecordDecl *ClassDecl, |
| 982 | QualType BaseType, |
| 983 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 984 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 985 | // First, check for a direct base class. |
| 986 | DirectBaseSpec = 0; |
| 987 | for (CXXRecordDecl::base_class_const_iterator Base |
| 988 | = ClassDecl->bases_begin(); |
| 989 | Base != ClassDecl->bases_end(); ++Base) { |
| 990 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 991 | // We found a direct base of this type. That's what we're |
| 992 | // initializing. |
| 993 | DirectBaseSpec = &*Base; |
| 994 | break; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | // Check for a virtual base class. |
| 999 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1000 | // there are no virtual bases. |
| 1001 | VirtualBaseSpec = 0; |
| 1002 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1003 | // We haven't found a base yet; search the class hierarchy for a |
| 1004 | // virtual base class. |
| 1005 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1006 | /*DetectVirtual=*/false); |
| 1007 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1008 | BaseType, Paths)) { |
| 1009 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1010 | Path != Paths.end(); ++Path) { |
| 1011 | if (Path->back().Base->isVirtual()) { |
| 1012 | VirtualBaseSpec = Path->back().Base; |
| 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | return DirectBaseSpec || VirtualBaseSpec; |
| 1020 | } |
| 1021 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1022 | /// ActOnMemInitializer - Handle a C++ member initializer. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1023 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1024 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1025 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1026 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1027 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1028 | ParsedType TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1029 | SourceLocation IdLoc, |
| 1030 | SourceLocation LParenLoc, |
| 1031 | ExprTy **Args, unsigned NumArgs, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1032 | SourceLocation RParenLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1033 | if (!ConstructorD) |
| 1034 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1036 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | |
| 1038 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1039 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1040 | if (!Constructor) { |
| 1041 | // The user wrote a constructor initializer on a function that is |
| 1042 | // not a C++ constructor. Ignore the error for now, because we may |
| 1043 | // have more member initializers coming; we'll diagnose it just |
| 1044 | // once in ActOnMemInitializers. |
| 1045 | return true; |
| 1046 | } |
| 1047 | |
| 1048 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1049 | |
| 1050 | // C++ [class.base.init]p2: |
| 1051 | // Names in a mem-initializer-id are looked up in the scope of the |
| 1052 | // constructor’s class and, if not found in that scope, are looked |
| 1053 | // up in the scope containing the constructor’s |
| 1054 | // definition. [Note: if the constructor’s class contains a member |
| 1055 | // with the same name as a direct or virtual base class of the |
| 1056 | // class, a mem-initializer-id naming the member or base class and |
| 1057 | // composed of a single identifier refers to the class member. A |
| 1058 | // mem-initializer-id for the hidden base class may be specified |
| 1059 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1060 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1061 | // Look for a member, first. |
| 1062 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1064 | = ClassDecl->lookup(MemberOrBase); |
| 1065 | if (Result.first != Result.second) |
| 1066 | Member = dyn_cast<FieldDecl>(*Result.first); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1067 | |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1068 | // FIXME: Handle members of an anonymous union. |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1069 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1070 | if (Member) |
| 1071 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1072 | LParenLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1073 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1074 | // 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] | 1075 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1076 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1077 | |
| 1078 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1079 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1080 | } else { |
| 1081 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1082 | LookupParsedName(R, S, &SS); |
| 1083 | |
| 1084 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1085 | if (!TyD) { |
| 1086 | if (R.isAmbiguous()) return true; |
| 1087 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1088 | // We don't want access-control diagnostics here. |
| 1089 | R.suppressDiagnostics(); |
| 1090 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1091 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1092 | bool NotUnknownSpecialization = false; |
| 1093 | DeclContext *DC = computeDeclContext(SS, false); |
| 1094 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1095 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1096 | |
| 1097 | if (!NotUnknownSpecialization) { |
| 1098 | // When the scope specifier can refer to a member of an unknown |
| 1099 | // specialization, we take it as a type name. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 1100 | BaseType = CheckTypenameType(ETK_None, |
| 1101 | (NestedNameSpecifier *)SS.getScopeRep(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 1102 | *MemberOrBase, SourceLocation(), |
| 1103 | SS.getRange(), IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1104 | if (BaseType.isNull()) |
| 1105 | return true; |
| 1106 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1107 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 1108 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1112 | // If no results were found, try to correct typos. |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1113 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1114 | CorrectTypo(R, S, &SS, ClassDecl, 0, CTC_NoKeywords) && |
| 1115 | R.isSingleResult()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1116 | if (FieldDecl *Member = R.getAsSingle<FieldDecl>()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1117 | if (Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl)) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1118 | // We have found a non-static data member with a similar |
| 1119 | // name to what was typed; complain and initialize that |
| 1120 | // member. |
| 1121 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1122 | << MemberOrBase << true << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1123 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1124 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1125 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 1126 | << Member->getDeclName(); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1127 | |
| 1128 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 1129 | LParenLoc, RParenLoc); |
| 1130 | } |
| 1131 | } else if (TypeDecl *Type = R.getAsSingle<TypeDecl>()) { |
| 1132 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1133 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1134 | if (FindBaseInitializer(*this, ClassDecl, |
| 1135 | Context.getTypeDeclType(Type), |
| 1136 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1137 | // We have found a direct or virtual base class with a |
| 1138 | // similar name to what was typed; complain and initialize |
| 1139 | // that base class. |
| 1140 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1141 | << MemberOrBase << false << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1142 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1143 | R.getLookupName().getAsString()); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1144 | |
| 1145 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1146 | : VirtualBaseSpec; |
| 1147 | Diag(BaseSpec->getSourceRange().getBegin(), |
| 1148 | diag::note_base_class_specified_here) |
| 1149 | << BaseSpec->getType() |
| 1150 | << BaseSpec->getSourceRange(); |
| 1151 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1152 | TyD = Type; |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1157 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1158 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1159 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1160 | return true; |
| 1161 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1164 | if (BaseType.isNull()) { |
| 1165 | BaseType = Context.getTypeDeclType(TyD); |
| 1166 | if (SS.isSet()) { |
| 1167 | NestedNameSpecifier *Qualifier = |
| 1168 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1169 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1170 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1171 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1172 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1173 | } |
| 1174 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1176 | if (!TInfo) |
| 1177 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1178 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1179 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1180 | LParenLoc, RParenLoc, ClassDecl); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1183 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1184 | /// containing the field that is being initialized. Returns true if there is an |
| 1185 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1186 | /// otherwise. |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1187 | static bool InitExprContainsUninitializedFields(const Stmt *S, |
| 1188 | const FieldDecl *LhsField, |
| 1189 | SourceLocation *L) { |
| 1190 | if (isa<CallExpr>(S)) { |
| 1191 | // Do not descend into function calls or constructors, as the use |
| 1192 | // of an uninitialized field may be valid. One would have to inspect |
| 1193 | // the contents of the function/ctor to determine if it is safe or not. |
| 1194 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1195 | // may be safe, depending on what the function/ctor does. |
| 1196 | return false; |
| 1197 | } |
| 1198 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 1199 | const NamedDecl *RhsField = ME->getMemberDecl(); |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1200 | |
| 1201 | if (const VarDecl *VD = dyn_cast<VarDecl>(RhsField)) { |
| 1202 | // The member expression points to a static data member. |
| 1203 | assert(VD->isStaticDataMember() && |
| 1204 | "Member points to non-static data member!"); |
Nick Lewycky | edd5911 | 2010-10-06 18:37:39 +0000 | [diff] [blame] | 1205 | (void)VD; |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1206 | return false; |
| 1207 | } |
| 1208 | |
| 1209 | if (isa<EnumConstantDecl>(RhsField)) { |
| 1210 | // The member expression points to an enum. |
| 1211 | return false; |
| 1212 | } |
| 1213 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1214 | if (RhsField == LhsField) { |
| 1215 | // Initializing a field with itself. Throw a warning. |
| 1216 | // But wait; there are exceptions! |
| 1217 | // Exception #1: The field may not belong to this record. |
| 1218 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1219 | const Expr *base = ME->getBase(); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1220 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1221 | // Even though the field matches, it does not belong to this record. |
| 1222 | return false; |
| 1223 | } |
| 1224 | // None of the exceptions triggered; return true to indicate an |
| 1225 | // uninitialized field was used. |
| 1226 | *L = ME->getMemberLoc(); |
| 1227 | return true; |
| 1228 | } |
Argyrios Kyrtzidis | ff8819b | 2010-09-21 10:47:20 +0000 | [diff] [blame] | 1229 | } else if (isa<SizeOfAlignOfExpr>(S)) { |
| 1230 | // sizeof/alignof doesn't reference contents, do not warn. |
| 1231 | return false; |
| 1232 | } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(S)) { |
| 1233 | // address-of doesn't reference contents (the pointer may be dereferenced |
| 1234 | // in the same expression but it would be rare; and weird). |
| 1235 | if (UOE->getOpcode() == UO_AddrOf) |
| 1236 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1237 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1238 | for (Stmt::const_child_iterator it = S->child_begin(), e = S->child_end(); |
| 1239 | it != e; ++it) { |
| 1240 | if (!*it) { |
| 1241 | // An expression such as 'member(arg ?: "")' may trigger this. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1242 | continue; |
| 1243 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1244 | if (InitExprContainsUninitializedFields(*it, LhsField, L)) |
| 1245 | return true; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1246 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1247 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1250 | MemInitResult |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1251 | Sema::BuildMemberInitializer(FieldDecl *Member, Expr **Args, |
| 1252 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1253 | SourceLocation LParenLoc, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1254 | SourceLocation RParenLoc) { |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1255 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1256 | // foo(foo) |
| 1257 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1258 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1259 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1260 | SourceLocation L; |
| 1261 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1262 | // FIXME: Return true in the case when other fields are used before being |
| 1263 | // uninitialized. For example, let this field be the i'th field. When |
| 1264 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1265 | // fields are used, as they are not yet initialized. |
| 1266 | // Right now we are only handling the case where the i'th field uses |
| 1267 | // itself in its initializer. |
| 1268 | Diag(L, diag::warn_field_is_uninit); |
| 1269 | } |
| 1270 | } |
| 1271 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1272 | bool HasDependentArg = false; |
| 1273 | for (unsigned i = 0; i < NumArgs; i++) |
| 1274 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1275 | |
Eli Friedman | 0f2b97d | 2010-07-24 21:19:15 +0000 | [diff] [blame] | 1276 | if (Member->getType()->isDependentType() || HasDependentArg) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1277 | // Can't check initialization for a member of dependent type or when |
| 1278 | // any of the arguments are type-dependent expressions. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1279 | Expr *Init |
| 1280 | = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1281 | RParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1282 | |
| 1283 | // Erase any temporaries within this evaluation context; we're not |
| 1284 | // going to track them in the AST, since we'll be rebuilding the |
| 1285 | // ASTs during template instantiation. |
| 1286 | ExprTemporaries.erase( |
| 1287 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1288 | ExprTemporaries.end()); |
| 1289 | |
| 1290 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1291 | LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1292 | Init, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1293 | RParenLoc); |
| 1294 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1295 | } |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1296 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1297 | if (Member->isInvalidDecl()) |
| 1298 | return true; |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1299 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1300 | // Initialize the member. |
| 1301 | InitializedEntity MemberEntity = |
| 1302 | InitializedEntity::InitializeMember(Member, 0); |
| 1303 | InitializationKind Kind = |
| 1304 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc); |
| 1305 | |
| 1306 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 1307 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1308 | ExprResult MemberInit = |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1309 | InitSeq.Perform(*this, MemberEntity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1310 | MultiExprArg(*this, Args, NumArgs), 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1311 | if (MemberInit.isInvalid()) |
| 1312 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1313 | |
| 1314 | CheckImplicitConversions(MemberInit.get(), LParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1315 | |
| 1316 | // C++0x [class.base.init]p7: |
| 1317 | // The initialization of each base and member constitutes a |
| 1318 | // full-expression. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1319 | MemberInit = MaybeCreateCXXExprWithTemporaries(MemberInit.get()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1320 | if (MemberInit.isInvalid()) |
| 1321 | return true; |
| 1322 | |
| 1323 | // If we are in a dependent context, template instantiation will |
| 1324 | // perform this type-checking again. Just save the arguments that we |
| 1325 | // received in a ParenListExpr. |
| 1326 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1327 | // of the information that we have about the member |
| 1328 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1329 | // and this approach is far more likely to get the corner cases right. |
| 1330 | if (CurContext->isDependentContext()) { |
| 1331 | // Bump the reference count of all of the arguments. |
| 1332 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1333 | Args[I]->Retain(); |
| 1334 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1335 | Expr *Init = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1336 | RParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1337 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1338 | LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1339 | Init, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1340 | RParenLoc); |
| 1341 | } |
| 1342 | |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1343 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1344 | LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1345 | MemberInit.get(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1346 | RParenLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1349 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1350 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1351 | Expr **Args, unsigned NumArgs, |
| 1352 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
| 1353 | CXXRecordDecl *ClassDecl) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1354 | bool HasDependentArg = false; |
| 1355 | for (unsigned i = 0; i < NumArgs; i++) |
| 1356 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1357 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1358 | SourceLocation BaseLoc |
| 1359 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1360 | |
| 1361 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 1362 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 1363 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1364 | |
| 1365 | // C++ [class.base.init]p2: |
| 1366 | // [...] Unless the mem-initializer-id names a nonstatic data |
| 1367 | // member of the constructor’s class or a direct or virtual base |
| 1368 | // of that class, the mem-initializer is ill-formed. A |
| 1369 | // mem-initializer-list can initialize a base class using any |
| 1370 | // name that denotes that base class type. |
| 1371 | bool Dependent = BaseType->isDependentType() || HasDependentArg; |
| 1372 | |
| 1373 | // Check for direct and virtual base classes. |
| 1374 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1375 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1376 | if (!Dependent) { |
| 1377 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 1378 | VirtualBaseSpec); |
| 1379 | |
| 1380 | // C++ [base.class.init]p2: |
| 1381 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 1382 | // constructor's class or a direct or virtual base of that class, the |
| 1383 | // mem-initializer is ill-formed. |
| 1384 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 1385 | // If the class has any dependent bases, then it's possible that |
| 1386 | // one of those types will resolve to the same type as |
| 1387 | // BaseType. Therefore, just treat this as a dependent base |
| 1388 | // class initialization. FIXME: Should we try to check the |
| 1389 | // initialization anyway? It seems odd. |
| 1390 | if (ClassDecl->hasAnyDependentBases()) |
| 1391 | Dependent = true; |
| 1392 | else |
| 1393 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 1394 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 1395 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | if (Dependent) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1400 | // Can't check initialization for a base of dependent type or when |
| 1401 | // any of the arguments are type-dependent expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1402 | ExprResult BaseInit |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1403 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1404 | RParenLoc)); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1405 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1406 | // Erase any temporaries within this evaluation context; we're not |
| 1407 | // going to track them in the AST, since we'll be rebuilding the |
| 1408 | // ASTs during template instantiation. |
| 1409 | ExprTemporaries.erase( |
| 1410 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1411 | ExprTemporaries.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1413 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1414 | /*IsVirtual=*/false, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1415 | LParenLoc, |
| 1416 | BaseInit.takeAs<Expr>(), |
| 1417 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1418 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1419 | |
| 1420 | // C++ [base.class.init]p2: |
| 1421 | // If a mem-initializer-id is ambiguous because it designates both |
| 1422 | // a direct non-virtual base class and an inherited virtual base |
| 1423 | // class, the mem-initializer is ill-formed. |
| 1424 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1425 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1426 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1427 | |
| 1428 | CXXBaseSpecifier *BaseSpec |
| 1429 | = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
| 1430 | if (!BaseSpec) |
| 1431 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 1432 | |
| 1433 | // Initialize the base. |
| 1434 | InitializedEntity BaseEntity = |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1435 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1436 | InitializationKind Kind = |
| 1437 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc); |
| 1438 | |
| 1439 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 1440 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1441 | ExprResult BaseInit = |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1442 | InitSeq.Perform(*this, BaseEntity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1443 | MultiExprArg(*this, Args, NumArgs), 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1444 | if (BaseInit.isInvalid()) |
| 1445 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1446 | |
| 1447 | CheckImplicitConversions(BaseInit.get(), LParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1448 | |
| 1449 | // C++0x [class.base.init]p7: |
| 1450 | // The initialization of each base and member constitutes a |
| 1451 | // full-expression. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1452 | BaseInit = MaybeCreateCXXExprWithTemporaries(BaseInit.get()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1453 | if (BaseInit.isInvalid()) |
| 1454 | return true; |
| 1455 | |
| 1456 | // If we are in a dependent context, template instantiation will |
| 1457 | // perform this type-checking again. Just save the arguments that we |
| 1458 | // received in a ParenListExpr. |
| 1459 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1460 | // of the information that we have about the base |
| 1461 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1462 | // and this approach is far more likely to get the corner cases right. |
| 1463 | if (CurContext->isDependentContext()) { |
| 1464 | // Bump the reference count of all of the arguments. |
| 1465 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1466 | Args[I]->Retain(); |
| 1467 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1468 | ExprResult Init |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1469 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1470 | RParenLoc)); |
| 1471 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1472 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1473 | LParenLoc, |
| 1474 | Init.takeAs<Expr>(), |
| 1475 | RParenLoc); |
| 1476 | } |
| 1477 | |
| 1478 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1479 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1480 | LParenLoc, |
| 1481 | BaseInit.takeAs<Expr>(), |
| 1482 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1485 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 1486 | /// initialize its base or member. |
| 1487 | enum ImplicitInitializerKind { |
| 1488 | IIK_Default, |
| 1489 | IIK_Copy, |
| 1490 | IIK_Move |
| 1491 | }; |
| 1492 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1493 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1494 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1495 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1496 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1497 | bool IsInheritedVirtualBase, |
| 1498 | CXXBaseOrMemberInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1499 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1500 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 1501 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1502 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1503 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1504 | |
| 1505 | switch (ImplicitInitKind) { |
| 1506 | case IIK_Default: { |
| 1507 | InitializationKind InitKind |
| 1508 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 1509 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1510 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1511 | MultiExprArg(SemaRef, 0, 0)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1512 | break; |
| 1513 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1514 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1515 | case IIK_Copy: { |
| 1516 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1517 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1518 | |
| 1519 | Expr *CopyCtorArg = |
| 1520 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
Douglas Gregor | 62b71f4 | 2010-05-03 15:43:53 +0000 | [diff] [blame] | 1521 | Constructor->getLocation(), ParamType, 0); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1522 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1523 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 1524 | QualType ArgTy = |
| 1525 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 1526 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1527 | |
| 1528 | CXXCastPath BasePath; |
| 1529 | BasePath.push_back(BaseSpec); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1530 | SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1531 | CK_UncheckedDerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1532 | VK_LValue, &BasePath); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1533 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1534 | InitializationKind InitKind |
| 1535 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1536 | SourceLocation(), SourceLocation()); |
| 1537 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1538 | &CopyCtorArg, 1); |
| 1539 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1540 | MultiExprArg(&CopyCtorArg, 1)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1541 | break; |
| 1542 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1543 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1544 | case IIK_Move: |
| 1545 | assert(false && "Unhandled initializer kind!"); |
| 1546 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1547 | |
| 1548 | if (BaseInit.isInvalid()) |
| 1549 | return true; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1550 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1551 | BaseInit = SemaRef.MaybeCreateCXXExprWithTemporaries(BaseInit.get()); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1552 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1553 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1554 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1555 | CXXBaseInit = |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1556 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
| 1557 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 1558 | SourceLocation()), |
| 1559 | BaseSpec->isVirtual(), |
| 1560 | SourceLocation(), |
| 1561 | BaseInit.takeAs<Expr>(), |
| 1562 | SourceLocation()); |
| 1563 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1564 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1567 | static bool |
| 1568 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1569 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1570 | FieldDecl *Field, |
| 1571 | CXXBaseOrMemberInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 1572 | if (Field->isInvalidDecl()) |
| 1573 | return true; |
| 1574 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1575 | SourceLocation Loc = Constructor->getLocation(); |
| 1576 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1577 | if (ImplicitInitKind == IIK_Copy) { |
| 1578 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1579 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1580 | |
| 1581 | Expr *MemberExprBase = |
| 1582 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1583 | Loc, ParamType, 0); |
| 1584 | |
| 1585 | // Build a reference to this field within the parameter. |
| 1586 | CXXScopeSpec SS; |
| 1587 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 1588 | Sema::LookupMemberName); |
| 1589 | MemberLookup.addDecl(Field, AS_public); |
| 1590 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1591 | ExprResult CopyCtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1592 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1593 | ParamType, Loc, |
| 1594 | /*IsArrow=*/false, |
| 1595 | SS, |
| 1596 | /*FirstQualifierInScope=*/0, |
| 1597 | MemberLookup, |
| 1598 | /*TemplateArgs=*/0); |
| 1599 | if (CopyCtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1600 | return true; |
| 1601 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1602 | // When the field we are copying is an array, create index variables for |
| 1603 | // each dimension of the array. We use these index variables to subscript |
| 1604 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 1605 | // necessary iteration with these index variables. |
| 1606 | llvm::SmallVector<VarDecl *, 4> IndexVariables; |
| 1607 | QualType BaseType = Field->getType(); |
| 1608 | QualType SizeType = SemaRef.Context.getSizeType(); |
| 1609 | while (const ConstantArrayType *Array |
| 1610 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
| 1611 | // Create the iteration variable for this array index. |
| 1612 | IdentifierInfo *IterationVarName = 0; |
| 1613 | { |
| 1614 | llvm::SmallString<8> Str; |
| 1615 | llvm::raw_svector_ostream OS(Str); |
| 1616 | OS << "__i" << IndexVariables.size(); |
| 1617 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 1618 | } |
| 1619 | VarDecl *IterationVar |
| 1620 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, |
| 1621 | IterationVarName, SizeType, |
| 1622 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1623 | SC_None, SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1624 | IndexVariables.push_back(IterationVar); |
| 1625 | |
| 1626 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1627 | ExprResult IterationVarRef |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1628 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, Loc); |
| 1629 | assert(!IterationVarRef.isInvalid() && |
| 1630 | "Reference to invented variable cannot fail!"); |
| 1631 | |
| 1632 | // Subscript the array with this iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1633 | CopyCtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CopyCtorArg.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1634 | Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1635 | IterationVarRef.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1636 | Loc); |
| 1637 | if (CopyCtorArg.isInvalid()) |
| 1638 | return true; |
| 1639 | |
| 1640 | BaseType = Array->getElementType(); |
| 1641 | } |
| 1642 | |
| 1643 | // Construct the entity that we will be initializing. For an array, this |
| 1644 | // will be first element in the array, which may require several levels |
| 1645 | // of array-subscript entities. |
| 1646 | llvm::SmallVector<InitializedEntity, 4> Entities; |
| 1647 | Entities.reserve(1 + IndexVariables.size()); |
| 1648 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
| 1649 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 1650 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 1651 | 0, |
| 1652 | Entities.back())); |
| 1653 | |
| 1654 | // Direct-initialize to use the copy constructor. |
| 1655 | InitializationKind InitKind = |
| 1656 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 1657 | |
| 1658 | Expr *CopyCtorArgE = CopyCtorArg.takeAs<Expr>(); |
| 1659 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
| 1660 | &CopyCtorArgE, 1); |
| 1661 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1662 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1663 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1664 | MultiExprArg(&CopyCtorArgE, 1)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1665 | MemberInit = SemaRef.MaybeCreateCXXExprWithTemporaries(MemberInit.get()); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1666 | if (MemberInit.isInvalid()) |
| 1667 | return true; |
| 1668 | |
| 1669 | CXXMemberInit |
| 1670 | = CXXBaseOrMemberInitializer::Create(SemaRef.Context, Field, Loc, Loc, |
| 1671 | MemberInit.takeAs<Expr>(), Loc, |
| 1672 | IndexVariables.data(), |
| 1673 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1674 | return false; |
| 1675 | } |
| 1676 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1677 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 1678 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1679 | QualType FieldBaseElementType = |
| 1680 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 1681 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1682 | if (FieldBaseElementType->isRecordType()) { |
| 1683 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1684 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1685 | InitializationKind::CreateDefault(Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1686 | |
| 1687 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1688 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1689 | InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1690 | if (MemberInit.isInvalid()) |
| 1691 | return true; |
| 1692 | |
| 1693 | MemberInit = SemaRef.MaybeCreateCXXExprWithTemporaries(MemberInit.get()); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1694 | if (MemberInit.isInvalid()) |
| 1695 | return true; |
| 1696 | |
| 1697 | CXXMemberInit = |
| 1698 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1699 | Field, Loc, Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1700 | MemberInit.get(), |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1701 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1702 | return false; |
| 1703 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 1704 | |
| 1705 | if (FieldBaseElementType->isReferenceType()) { |
| 1706 | SemaRef.Diag(Constructor->getLocation(), |
| 1707 | diag::err_uninitialized_member_in_ctor) |
| 1708 | << (int)Constructor->isImplicit() |
| 1709 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1710 | << 0 << Field->getDeclName(); |
| 1711 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1712 | return true; |
| 1713 | } |
| 1714 | |
| 1715 | if (FieldBaseElementType.isConstQualified()) { |
| 1716 | SemaRef.Diag(Constructor->getLocation(), |
| 1717 | diag::err_uninitialized_member_in_ctor) |
| 1718 | << (int)Constructor->isImplicit() |
| 1719 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1720 | << 1 << Field->getDeclName(); |
| 1721 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1722 | return true; |
| 1723 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1724 | |
| 1725 | // Nothing to initialize. |
| 1726 | CXXMemberInit = 0; |
| 1727 | return false; |
| 1728 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1729 | |
| 1730 | namespace { |
| 1731 | struct BaseAndFieldInfo { |
| 1732 | Sema &S; |
| 1733 | CXXConstructorDecl *Ctor; |
| 1734 | bool AnyErrorsInInits; |
| 1735 | ImplicitInitializerKind IIK; |
| 1736 | llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; |
| 1737 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 8> AllToInit; |
| 1738 | |
| 1739 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 1740 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
| 1741 | // FIXME: Handle implicit move constructors. |
| 1742 | if (Ctor->isImplicit() && Ctor->isCopyConstructor()) |
| 1743 | IIK = IIK_Copy; |
| 1744 | else |
| 1745 | IIK = IIK_Default; |
| 1746 | } |
| 1747 | }; |
| 1748 | } |
| 1749 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1750 | static void RecordFieldInitializer(BaseAndFieldInfo &Info, |
| 1751 | FieldDecl *Top, FieldDecl *Field, |
| 1752 | CXXBaseOrMemberInitializer *Init) { |
| 1753 | // If the member doesn't need to be initialized, Init will still be null. |
| 1754 | if (!Init) |
| 1755 | return; |
| 1756 | |
| 1757 | Info.AllToInit.push_back(Init); |
| 1758 | if (Field != Top) { |
| 1759 | Init->setMember(Top); |
| 1760 | Init->setAnonUnionMember(Field); |
| 1761 | } |
| 1762 | } |
| 1763 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1764 | static bool CollectFieldInitializer(BaseAndFieldInfo &Info, |
| 1765 | FieldDecl *Top, FieldDecl *Field) { |
| 1766 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1767 | // Overwhelmingly common case: we have a direct initializer for this field. |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1768 | if (CXXBaseOrMemberInitializer *Init = Info.AllBaseFields.lookup(Field)) { |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1769 | RecordFieldInitializer(Info, Top, Field, Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1770 | return false; |
| 1771 | } |
| 1772 | |
| 1773 | if (Info.IIK == IIK_Default && Field->isAnonymousStructOrUnion()) { |
| 1774 | const RecordType *FieldClassType = Field->getType()->getAs<RecordType>(); |
| 1775 | assert(FieldClassType && "anonymous struct/union without record type"); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1776 | CXXRecordDecl *FieldClassDecl |
| 1777 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1778 | |
| 1779 | // Even though union members never have non-trivial default |
| 1780 | // constructions in C++03, we still build member initializers for aggregate |
| 1781 | // record types which can be union members, and C++0x allows non-trivial |
| 1782 | // default constructors for union members, so we ensure that only one |
| 1783 | // member is initialized for these. |
| 1784 | if (FieldClassDecl->isUnion()) { |
| 1785 | // First check for an explicit initializer for one field. |
| 1786 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 1787 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1788 | if (CXXBaseOrMemberInitializer *Init = Info.AllBaseFields.lookup(*FA)) { |
| 1789 | RecordFieldInitializer(Info, Top, *FA, Init); |
| 1790 | |
| 1791 | // Once we've initialized a field of an anonymous union, the union |
| 1792 | // field in the class is also initialized, so exit immediately. |
| 1793 | return false; |
Argyrios Kyrtzidis | 881b36c | 2010-08-16 17:27:13 +0000 | [diff] [blame] | 1794 | } else if ((*FA)->isAnonymousStructOrUnion()) { |
| 1795 | if (CollectFieldInitializer(Info, Top, *FA)) |
| 1796 | return true; |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | // Fallthrough and construct a default initializer for the union as |
| 1801 | // a whole, which can call its default constructor if such a thing exists |
| 1802 | // (C++0x perhaps). FIXME: It's not clear that this is the correct |
| 1803 | // behavior going forward with C++0x, when anonymous unions there are |
| 1804 | // finalized, we should revisit this. |
| 1805 | } else { |
| 1806 | // For structs, we simply descend through to initialize all members where |
| 1807 | // necessary. |
| 1808 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 1809 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1810 | if (CollectFieldInitializer(Info, Top, *FA)) |
| 1811 | return true; |
| 1812 | } |
| 1813 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | // Don't try to build an implicit initializer if there were semantic |
| 1817 | // errors in any of the initializers (and therefore we might be |
| 1818 | // missing some that the user actually wrote). |
| 1819 | if (Info.AnyErrorsInInits) |
| 1820 | return false; |
| 1821 | |
| 1822 | CXXBaseOrMemberInitializer *Init = 0; |
| 1823 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, Init)) |
| 1824 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1825 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1826 | RecordFieldInitializer(Info, Top, Field, Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1827 | return false; |
| 1828 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1829 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1830 | bool |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1831 | Sema::SetBaseOrMemberInitializers(CXXConstructorDecl *Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1832 | CXXBaseOrMemberInitializer **Initializers, |
| 1833 | unsigned NumInitializers, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1834 | bool AnyErrors) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1835 | if (Constructor->getDeclContext()->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1836 | // Just store the initializers as written, they will be checked during |
| 1837 | // instantiation. |
| 1838 | if (NumInitializers > 0) { |
| 1839 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1840 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1841 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
| 1842 | memcpy(baseOrMemberInitializers, Initializers, |
| 1843 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
| 1844 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
| 1845 | } |
| 1846 | |
| 1847 | return false; |
| 1848 | } |
| 1849 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1850 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1851 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1852 | // We need to build the initializer AST according to order of construction |
| 1853 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1854 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 1855 | if (!ClassDecl) |
| 1856 | return true; |
| 1857 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1858 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1860 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1861 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1862 | |
| 1863 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1864 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1865 | else |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1866 | Info.AllBaseFields[Member->getMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1869 | // Keep track of the direct virtual bases. |
| 1870 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 1871 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 1872 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 1873 | if (I->isVirtual()) |
| 1874 | DirectVBases.insert(I); |
| 1875 | } |
| 1876 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1877 | // Push virtual bases before others. |
| 1878 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 1879 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 1880 | |
| 1881 | if (CXXBaseOrMemberInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1882 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 1883 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1884 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1885 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1886 | CXXBaseOrMemberInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1887 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1888 | VBase, IsInheritedVirtualBase, |
| 1889 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1890 | HadError = true; |
| 1891 | continue; |
| 1892 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1893 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1894 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1895 | } |
| 1896 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1897 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1898 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1899 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1900 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1901 | // Virtuals are in the virtual base list and already constructed. |
| 1902 | if (Base->isVirtual()) |
| 1903 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1904 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1905 | if (CXXBaseOrMemberInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1906 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 1907 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1908 | } else if (!AnyErrors) { |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1909 | CXXBaseOrMemberInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1910 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1911 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1912 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1913 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1914 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1915 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1916 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1917 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1918 | } |
| 1919 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1920 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1921 | // Fields. |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1922 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 1923 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 1924 | if ((*Field)->getType()->isIncompleteArrayType()) { |
| 1925 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 1926 | "Incomplete array type is not valid"); |
| 1927 | continue; |
| 1928 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1929 | if (CollectFieldInitializer(Info, *Field, *Field)) |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1930 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 1931 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1933 | NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1934 | if (NumInitializers > 0) { |
| 1935 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1936 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1937 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1938 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1939 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1940 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 1941 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1942 | // Constructors implicitly reference the base and member |
| 1943 | // destructors. |
| 1944 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 1945 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1946 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1947 | |
| 1948 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1951 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 1952 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1953 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1954 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 1955 | return static_cast<void *>(RT->getDecl()); |
| 1956 | } |
| 1957 | return static_cast<void *>(Field); |
| 1958 | } |
| 1959 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1960 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 1961 | return Context.getCanonicalType(BaseType).getTypePtr(); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1964 | static void *GetKeyForMember(ASTContext &Context, |
| 1965 | CXXBaseOrMemberInitializer *Member, |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1966 | bool MemberMaybeAnon = false) { |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1967 | if (!Member->isMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1968 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1969 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1970 | // For fields injected into the class via declaration of an anonymous union, |
| 1971 | // use its anonymous union class declaration as the unique key. |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1972 | FieldDecl *Field = Member->getMember(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1973 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1974 | // After SetBaseOrMemberInitializers call, Field is the anonymous union |
| 1975 | // data member of the class. Data member used in the initializer list is |
| 1976 | // in AnonUnionMember field. |
| 1977 | if (MemberMaybeAnon && Field->isAnonymousStructOrUnion()) |
| 1978 | Field = Member->getAnonUnionMember(); |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1979 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1980 | // If the field is a member of an anonymous struct or union, our key |
| 1981 | // 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] | 1982 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1983 | if (RD->isAnonymousStructOrUnion()) { |
| 1984 | while (true) { |
| 1985 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 1986 | if (Parent->isAnonymousStructOrUnion()) |
| 1987 | RD = Parent; |
| 1988 | else |
| 1989 | break; |
| 1990 | } |
| 1991 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1992 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1993 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1995 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1998 | static void |
| 1999 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2000 | const CXXConstructorDecl *Constructor, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2001 | CXXBaseOrMemberInitializer **Inits, |
| 2002 | unsigned NumInits) { |
| 2003 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 2004 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2006 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order) |
| 2007 | == Diagnostic::Ignored) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2008 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2009 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2010 | // Build the list of bases and members in the order that they'll |
| 2011 | // actually be initialized. The explicit initializers should be in |
| 2012 | // this same order but may be missing things. |
| 2013 | llvm::SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2015 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 2016 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2017 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2018 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2019 | ClassDecl->vbases_begin(), |
| 2020 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2021 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2023 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2024 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2025 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2026 | if (Base->isVirtual()) |
| 2027 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2028 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2029 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2031 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2032 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2033 | E = ClassDecl->field_end(); Field != E; ++Field) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2034 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2036 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 2037 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2038 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2039 | CXXBaseOrMemberInitializer *PrevInit = 0; |
| 2040 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
| 2041 | CXXBaseOrMemberInitializer *Init = Inits[InitIndex]; |
| 2042 | void *InitKey = GetKeyForMember(SemaRef.Context, Init, true); |
| 2043 | |
| 2044 | // Scan forward to try to find this initializer in the idealized |
| 2045 | // initializers list. |
| 2046 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2047 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2048 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2049 | |
| 2050 | // If we didn't find this initializer, it must be because we |
| 2051 | // scanned past it on a previous iteration. That can only |
| 2052 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 2053 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2054 | Sema::SemaDiagnosticBuilder D = |
| 2055 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 2056 | diag::warn_initializer_out_of_order); |
| 2057 | |
| 2058 | if (PrevInit->isMemberInitializer()) |
| 2059 | D << 0 << PrevInit->getMember()->getDeclName(); |
| 2060 | else |
| 2061 | D << 1 << PrevInit->getBaseClassInfo()->getType(); |
| 2062 | |
| 2063 | if (Init->isMemberInitializer()) |
| 2064 | D << 0 << Init->getMember()->getDeclName(); |
| 2065 | else |
| 2066 | D << 1 << Init->getBaseClassInfo()->getType(); |
| 2067 | |
| 2068 | // Move back to the initializer's location in the ideal list. |
| 2069 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2070 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2071 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2072 | |
| 2073 | assert(IdealIndex != NumIdealInits && |
| 2074 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2075 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2076 | |
| 2077 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2078 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2081 | namespace { |
| 2082 | bool CheckRedundantInit(Sema &S, |
| 2083 | CXXBaseOrMemberInitializer *Init, |
| 2084 | CXXBaseOrMemberInitializer *&PrevInit) { |
| 2085 | if (!PrevInit) { |
| 2086 | PrevInit = Init; |
| 2087 | return false; |
| 2088 | } |
| 2089 | |
| 2090 | if (FieldDecl *Field = Init->getMember()) |
| 2091 | S.Diag(Init->getSourceLocation(), |
| 2092 | diag::err_multiple_mem_initialization) |
| 2093 | << Field->getDeclName() |
| 2094 | << Init->getSourceRange(); |
| 2095 | else { |
| 2096 | Type *BaseClass = Init->getBaseClass(); |
| 2097 | assert(BaseClass && "neither field nor base"); |
| 2098 | S.Diag(Init->getSourceLocation(), |
| 2099 | diag::err_multiple_base_initialization) |
| 2100 | << QualType(BaseClass, 0) |
| 2101 | << Init->getSourceRange(); |
| 2102 | } |
| 2103 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 2104 | << 0 << PrevInit->getSourceRange(); |
| 2105 | |
| 2106 | return true; |
| 2107 | } |
| 2108 | |
| 2109 | typedef std::pair<NamedDecl *, CXXBaseOrMemberInitializer *> UnionEntry; |
| 2110 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 2111 | |
| 2112 | bool CheckRedundantUnionInit(Sema &S, |
| 2113 | CXXBaseOrMemberInitializer *Init, |
| 2114 | RedundantUnionMap &Unions) { |
| 2115 | FieldDecl *Field = Init->getMember(); |
| 2116 | RecordDecl *Parent = Field->getParent(); |
| 2117 | if (!Parent->isAnonymousStructOrUnion()) |
| 2118 | return false; |
| 2119 | |
| 2120 | NamedDecl *Child = Field; |
| 2121 | do { |
| 2122 | if (Parent->isUnion()) { |
| 2123 | UnionEntry &En = Unions[Parent]; |
| 2124 | if (En.first && En.first != Child) { |
| 2125 | S.Diag(Init->getSourceLocation(), |
| 2126 | diag::err_multiple_mem_union_initialization) |
| 2127 | << Field->getDeclName() |
| 2128 | << Init->getSourceRange(); |
| 2129 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 2130 | << 0 << En.second->getSourceRange(); |
| 2131 | return true; |
| 2132 | } else if (!En.first) { |
| 2133 | En.first = Child; |
| 2134 | En.second = Init; |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | Child = Parent; |
| 2139 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 2140 | } while (Parent->isAnonymousStructOrUnion()); |
| 2141 | |
| 2142 | return false; |
| 2143 | } |
| 2144 | } |
| 2145 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2146 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2147 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2148 | SourceLocation ColonLoc, |
| 2149 | MemInitTy **meminits, unsigned NumMemInits, |
| 2150 | bool AnyErrors) { |
| 2151 | if (!ConstructorDecl) |
| 2152 | return; |
| 2153 | |
| 2154 | AdjustDeclIfTemplate(ConstructorDecl); |
| 2155 | |
| 2156 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2157 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2158 | |
| 2159 | if (!Constructor) { |
| 2160 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 2161 | return; |
| 2162 | } |
| 2163 | |
| 2164 | CXXBaseOrMemberInitializer **MemInits = |
| 2165 | reinterpret_cast<CXXBaseOrMemberInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2166 | |
| 2167 | // Mapping for the duplicate initializers check. |
| 2168 | // For member initializers, this is keyed with a FieldDecl*. |
| 2169 | // For base initializers, this is keyed with a Type*. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2170 | llvm::DenseMap<void*, CXXBaseOrMemberInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2171 | |
| 2172 | // Mapping for the inconsistent anonymous-union initializers check. |
| 2173 | RedundantUnionMap MemberUnions; |
| 2174 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2175 | bool HadError = false; |
| 2176 | for (unsigned i = 0; i < NumMemInits; i++) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2177 | CXXBaseOrMemberInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2178 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 2179 | // Set the source order index. |
| 2180 | Init->setSourceOrder(i); |
| 2181 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2182 | if (Init->isMemberInitializer()) { |
| 2183 | FieldDecl *Field = Init->getMember(); |
| 2184 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 2185 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 2186 | HadError = true; |
| 2187 | } else { |
| 2188 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 2189 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 2190 | HadError = true; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2191 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2194 | if (HadError) |
| 2195 | return; |
| 2196 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2197 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2198 | |
| 2199 | SetBaseOrMemberInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2202 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2203 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 2204 | CXXRecordDecl *ClassDecl) { |
| 2205 | // Ignore dependent contexts. |
| 2206 | if (ClassDecl->isDependentContext()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2207 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2208 | |
| 2209 | // FIXME: all the access-control diagnostics are positioned on the |
| 2210 | // field/base declaration. That's probably good; that said, the |
| 2211 | // user might reasonably want to know why the destructor is being |
| 2212 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2213 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2214 | // Non-static data members. |
| 2215 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 2216 | E = ClassDecl->field_end(); I != E; ++I) { |
| 2217 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 2218 | if (Field->isInvalidDecl()) |
| 2219 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2220 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 2221 | |
| 2222 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 2223 | if (!RT) |
| 2224 | continue; |
| 2225 | |
| 2226 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 2227 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2228 | continue; |
| 2229 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2230 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2231 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2232 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2233 | << Field->getDeclName() |
| 2234 | << FieldType); |
| 2235 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2236 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2239 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 2240 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2241 | // Bases. |
| 2242 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2243 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2244 | // Bases are always records in a well-formed non-dependent class. |
| 2245 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 2246 | |
| 2247 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2248 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2249 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2250 | |
| 2251 | // Ignore trivial destructors. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2252 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2253 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2254 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2255 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2256 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2257 | |
| 2258 | // FIXME: caret should be on the start of the class name |
| 2259 | CheckDestructorAccess(Base->getSourceRange().getBegin(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2260 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2261 | << Base->getType() |
| 2262 | << Base->getSourceRange()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2263 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2264 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2268 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2269 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2270 | |
| 2271 | // Bases are always records in a well-formed non-dependent class. |
| 2272 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 2273 | |
| 2274 | // Ignore direct virtual bases. |
| 2275 | if (DirectVirtualBases.count(RT)) |
| 2276 | continue; |
| 2277 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2278 | // Ignore trivial destructors. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2279 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2280 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2281 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2282 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2283 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2284 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2285 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2286 | << VBase->getType()); |
| 2287 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2288 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2289 | } |
| 2290 | } |
| 2291 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2292 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2293 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2294 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2295 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2297 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2298 | SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2302 | unsigned DiagID, AbstractDiagSelID SelID) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2303 | if (SelID == -1) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2304 | return RequireNonAbstractType(Loc, T, PDiag(DiagID)); |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2305 | else |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2306 | return RequireNonAbstractType(Loc, T, PDiag(DiagID) << SelID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2309 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2310 | const PartialDiagnostic &PD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2311 | if (!getLangOptions().CPlusPlus) |
| 2312 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2313 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 2314 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2315 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2317 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2318 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2319 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2320 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2322 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2323 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2324 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2325 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2326 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2327 | if (!RT) |
| 2328 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2330 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2331 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2332 | // We can't answer whether something is abstract until it has a |
| 2333 | // definition. If it's currently being defined, we'll walk back |
| 2334 | // over all the declarations when we have a full definition. |
| 2335 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 2336 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2337 | return false; |
| 2338 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2339 | if (!RD->isAbstract()) |
| 2340 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2341 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2342 | Diag(Loc, PD) << RD->getDeclName(); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2343 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2344 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2345 | return true; |
| 2346 | } |
| 2347 | |
| 2348 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 2349 | // Check if we've already emitted the list of pure virtual functions |
| 2350 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2351 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2352 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2354 | CXXFinalOverriderMap FinalOverriders; |
| 2355 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2356 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2357 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 2358 | // more than once. |
| 2359 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 2360 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2361 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2362 | MEnd = FinalOverriders.end(); |
| 2363 | M != MEnd; |
| 2364 | ++M) { |
| 2365 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2366 | SOEnd = M->second.end(); |
| 2367 | SO != SOEnd; ++SO) { |
| 2368 | // C++ [class.abstract]p4: |
| 2369 | // A class is abstract if it contains or inherits at least one |
| 2370 | // pure virtual function for which the final overrider is pure |
| 2371 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2372 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2373 | // |
| 2374 | if (SO->second.size() != 1) |
| 2375 | continue; |
| 2376 | |
| 2377 | if (!SO->second.front().Method->isPure()) |
| 2378 | continue; |
| 2379 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2380 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 2381 | continue; |
| 2382 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2383 | Diag(SO->second.front().Method->getLocation(), |
| 2384 | diag::note_pure_virtual_function) |
| 2385 | << SO->second.front().Method->getDeclName(); |
| 2386 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | if (!PureVirtualClassDiagSet) |
| 2390 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 2391 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2394 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2395 | struct AbstractUsageInfo { |
| 2396 | Sema &S; |
| 2397 | CXXRecordDecl *Record; |
| 2398 | CanQualType AbstractType; |
| 2399 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2400 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2401 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 2402 | : S(S), Record(Record), |
| 2403 | AbstractType(S.Context.getCanonicalType( |
| 2404 | S.Context.getTypeDeclType(Record))), |
| 2405 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2406 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2407 | void DiagnoseAbstractType() { |
| 2408 | if (Invalid) return; |
| 2409 | S.DiagnoseAbstractType(Record); |
| 2410 | Invalid = true; |
| 2411 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2412 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2413 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 2414 | }; |
| 2415 | |
| 2416 | struct CheckAbstractUsage { |
| 2417 | AbstractUsageInfo &Info; |
| 2418 | const NamedDecl *Ctx; |
| 2419 | |
| 2420 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 2421 | : Info(Info), Ctx(Ctx) {} |
| 2422 | |
| 2423 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2424 | switch (TL.getTypeLocClass()) { |
| 2425 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 2426 | #define TYPELOC(CLASS, PARENT) \ |
| 2427 | case TypeLoc::CLASS: Check(cast<CLASS##TypeLoc>(TL), Sel); break; |
| 2428 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2429 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2430 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2431 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2432 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2433 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 2434 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2435 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 2436 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2437 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2438 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2439 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2440 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2441 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 2442 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2444 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2445 | // Visit the type parameters from a permissive context. |
| 2446 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2447 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 2448 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 2449 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 2450 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 2451 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2452 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2453 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2454 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2455 | // Visit pointee types from a permissive context. |
| 2456 | #define CheckPolymorphic(Type) \ |
| 2457 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 2458 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 2459 | } |
| 2460 | CheckPolymorphic(PointerTypeLoc) |
| 2461 | CheckPolymorphic(ReferenceTypeLoc) |
| 2462 | CheckPolymorphic(MemberPointerTypeLoc) |
| 2463 | CheckPolymorphic(BlockPointerTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2464 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2465 | /// Handle all the types we haven't given a more specific |
| 2466 | /// implementation for above. |
| 2467 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2468 | // Every other kind of type that we haven't called out already |
| 2469 | // that has an inner type is either (1) sugar or (2) contains that |
| 2470 | // inner type in some way as a subobject. |
| 2471 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 2472 | return Visit(Next, Sel); |
| 2473 | |
| 2474 | // If there's no inner type and we're in a permissive context, |
| 2475 | // don't diagnose. |
| 2476 | if (Sel == Sema::AbstractNone) return; |
| 2477 | |
| 2478 | // Check whether the type matches the abstract type. |
| 2479 | QualType T = TL.getType(); |
| 2480 | if (T->isArrayType()) { |
| 2481 | Sel = Sema::AbstractArrayType; |
| 2482 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2483 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2484 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 2485 | if (CT != Info.AbstractType) return; |
| 2486 | |
| 2487 | // It matched; do some magic. |
| 2488 | if (Sel == Sema::AbstractArrayType) { |
| 2489 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 2490 | << T << TL.getSourceRange(); |
| 2491 | } else { |
| 2492 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 2493 | << Sel << T << TL.getSourceRange(); |
| 2494 | } |
| 2495 | Info.DiagnoseAbstractType(); |
| 2496 | } |
| 2497 | }; |
| 2498 | |
| 2499 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 2500 | Sema::AbstractDiagSelID Sel) { |
| 2501 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 2502 | } |
| 2503 | |
| 2504 | } |
| 2505 | |
| 2506 | /// Check for invalid uses of an abstract type in a method declaration. |
| 2507 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2508 | CXXMethodDecl *MD) { |
| 2509 | // No need to do the check on definitions, which require that |
| 2510 | // the return/param types be complete. |
| 2511 | if (MD->isThisDeclarationADefinition()) |
| 2512 | return; |
| 2513 | |
| 2514 | // For safety's sake, just ignore it if we don't have type source |
| 2515 | // information. This should never happen for non-implicit methods, |
| 2516 | // but... |
| 2517 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 2518 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 2519 | } |
| 2520 | |
| 2521 | /// Check for invalid uses of an abstract type within a class definition. |
| 2522 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2523 | CXXRecordDecl *RD) { |
| 2524 | for (CXXRecordDecl::decl_iterator |
| 2525 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 2526 | Decl *D = *I; |
| 2527 | if (D->isImplicit()) continue; |
| 2528 | |
| 2529 | // Methods and method templates. |
| 2530 | if (isa<CXXMethodDecl>(D)) { |
| 2531 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 2532 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 2533 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 2534 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 2535 | |
| 2536 | // Fields and static variables. |
| 2537 | } else if (isa<FieldDecl>(D)) { |
| 2538 | FieldDecl *FD = cast<FieldDecl>(D); |
| 2539 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 2540 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 2541 | } else if (isa<VarDecl>(D)) { |
| 2542 | VarDecl *VD = cast<VarDecl>(D); |
| 2543 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 2544 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 2545 | |
| 2546 | // Nested classes and class templates. |
| 2547 | } else if (isa<CXXRecordDecl>(D)) { |
| 2548 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 2549 | } else if (isa<ClassTemplateDecl>(D)) { |
| 2550 | CheckAbstractClassUsage(Info, |
| 2551 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 2552 | } |
| 2553 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2556 | /// \brief Perform semantic checks on a class definition that has been |
| 2557 | /// completing, introducing implicitly-declared members, checking for |
| 2558 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2559 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 2560 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2561 | return; |
| 2562 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2563 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 2564 | AbstractUsageInfo Info(*this, Record); |
| 2565 | CheckAbstractClassUsage(Info, Record); |
| 2566 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2567 | |
| 2568 | // If this is not an aggregate type and has no user-declared constructor, |
| 2569 | // complain about any non-static data members of reference or const scalar |
| 2570 | // type, since they will never get initializers. |
| 2571 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 2572 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor()) { |
| 2573 | bool Complained = false; |
| 2574 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 2575 | FEnd = Record->field_end(); |
| 2576 | F != FEnd; ++F) { |
| 2577 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 2578 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2579 | if (!Complained) { |
| 2580 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 2581 | << Record->getTagKind() << Record; |
| 2582 | Complained = true; |
| 2583 | } |
| 2584 | |
| 2585 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 2586 | << F->getType()->isReferenceType() |
| 2587 | << F->getDeclName(); |
| 2588 | } |
| 2589 | } |
| 2590 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2591 | |
| 2592 | if (Record->isDynamicClass()) |
| 2593 | DynamicClasses.push_back(Record); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2596 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2597 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2598 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2599 | SourceLocation RBrac, |
| 2600 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2601 | if (!TagDecl) |
| 2602 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2603 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2604 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2605 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2606 | ActOnFields(S, RLoc, TagDecl, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2607 | // strict aliasing violation! |
| 2608 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2609 | FieldCollector->getCurNumFields(), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2610 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2611 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2612 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2615 | namespace { |
| 2616 | /// \brief Helper class that collects exception specifications for |
| 2617 | /// implicitly-declared special member functions. |
| 2618 | class ImplicitExceptionSpecification { |
| 2619 | ASTContext &Context; |
| 2620 | bool AllowsAllExceptions; |
| 2621 | llvm::SmallPtrSet<CanQualType, 4> ExceptionsSeen; |
| 2622 | llvm::SmallVector<QualType, 4> Exceptions; |
| 2623 | |
| 2624 | public: |
| 2625 | explicit ImplicitExceptionSpecification(ASTContext &Context) |
| 2626 | : Context(Context), AllowsAllExceptions(false) { } |
| 2627 | |
| 2628 | /// \brief Whether the special member function should have any |
| 2629 | /// exception specification at all. |
| 2630 | bool hasExceptionSpecification() const { |
| 2631 | return !AllowsAllExceptions; |
| 2632 | } |
| 2633 | |
| 2634 | /// \brief Whether the special member function should have a |
| 2635 | /// throw(...) exception specification (a Microsoft extension). |
| 2636 | bool hasAnyExceptionSpecification() const { |
| 2637 | return false; |
| 2638 | } |
| 2639 | |
| 2640 | /// \brief The number of exceptions in the exception specification. |
| 2641 | unsigned size() const { return Exceptions.size(); } |
| 2642 | |
| 2643 | /// \brief The set of exceptions in the exception specification. |
| 2644 | const QualType *data() const { return Exceptions.data(); } |
| 2645 | |
| 2646 | /// \brief Note that |
| 2647 | void CalledDecl(CXXMethodDecl *Method) { |
| 2648 | // If we already know that we allow all exceptions, do nothing. |
Douglas Gregor | 4681ca8 | 2010-07-01 15:29:53 +0000 | [diff] [blame] | 2649 | if (AllowsAllExceptions || !Method) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2650 | return; |
| 2651 | |
| 2652 | const FunctionProtoType *Proto |
| 2653 | = Method->getType()->getAs<FunctionProtoType>(); |
| 2654 | |
| 2655 | // If this function can throw any exceptions, make a note of that. |
| 2656 | if (!Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec()) { |
| 2657 | AllowsAllExceptions = true; |
| 2658 | ExceptionsSeen.clear(); |
| 2659 | Exceptions.clear(); |
| 2660 | return; |
| 2661 | } |
| 2662 | |
| 2663 | // Record the exceptions in this function's exception specification. |
| 2664 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 2665 | EEnd = Proto->exception_end(); |
| 2666 | E != EEnd; ++E) |
| 2667 | if (ExceptionsSeen.insert(Context.getCanonicalType(*E))) |
| 2668 | Exceptions.push_back(*E); |
| 2669 | } |
| 2670 | }; |
| 2671 | } |
| 2672 | |
| 2673 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2674 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 2675 | /// special functions, such as the default constructor, copy |
| 2676 | /// constructor, or destructor, to the given C++ class (C++ |
| 2677 | /// [special]p1). This routine can only be executed just before the |
| 2678 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2679 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 2680 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 2681 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2682 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 2683 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 2684 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2685 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 2686 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 2687 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
| 2688 | |
| 2689 | // If we have a dynamic class, then the copy assignment operator may be |
| 2690 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
| 2691 | // it shows up in the right place in the vtable and that we diagnose |
| 2692 | // problems with the implicit exception specification. |
| 2693 | if (ClassDecl->isDynamicClass()) |
| 2694 | DeclareImplicitCopyAssignment(ClassDecl); |
| 2695 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2696 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 2697 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 2698 | ++ASTContext::NumImplicitDestructors; |
| 2699 | |
| 2700 | // If we have a dynamic class, then the destructor may be virtual, so we |
| 2701 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 2702 | // shows up in the right place in the vtable and that we diagnose problems |
| 2703 | // with the implicit exception specification. |
| 2704 | if (ClassDecl->isDynamicClass()) |
| 2705 | DeclareImplicitDestructor(ClassDecl); |
| 2706 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2709 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 2710 | if (!D) |
| 2711 | return; |
| 2712 | |
| 2713 | TemplateParameterList *Params = 0; |
| 2714 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 2715 | Params = Template->getTemplateParameters(); |
| 2716 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2717 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 2718 | Params = PartialSpec->getTemplateParameters(); |
| 2719 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2720 | return; |
| 2721 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2722 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2723 | ParamEnd = Params->end(); |
| 2724 | Param != ParamEnd; ++Param) { |
| 2725 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 2726 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2727 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2728 | IdResolver.AddDecl(Named); |
| 2729 | } |
| 2730 | } |
| 2731 | } |
| 2732 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2733 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2734 | if (!RecordD) return; |
| 2735 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2736 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2737 | PushDeclContext(S, Record); |
| 2738 | } |
| 2739 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2740 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2741 | if (!RecordD) return; |
| 2742 | PopDeclContext(); |
| 2743 | } |
| 2744 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2745 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 2746 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 2747 | /// parsing those parts of the given Method declaration that could |
| 2748 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 2749 | /// arguments. This action should enter the scope of the given |
| 2750 | /// Method declaration as if we had just parsed the qualified method |
| 2751 | /// name. However, it should not bring the parameters into scope; |
| 2752 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2753 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
| 2756 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 2757 | /// C++ method declaration. We're (re-)introducing the given |
| 2758 | /// function parameter into scope for use in parsing later parts of |
| 2759 | /// the method declaration. For example, we could see an |
| 2760 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2761 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2762 | if (!ParamD) |
| 2763 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2765 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2766 | |
| 2767 | // If this parameter has an unparsed default argument, clear it out |
| 2768 | // to make way for the parsed default argument. |
| 2769 | if (Param->hasUnparsedDefaultArg()) |
| 2770 | Param->setDefaultArg(0); |
| 2771 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2772 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2773 | if (Param->getDeclName()) |
| 2774 | IdResolver.AddDecl(Param); |
| 2775 | } |
| 2776 | |
| 2777 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 2778 | /// processing the delayed method declaration for Method. The method |
| 2779 | /// declaration is now considered finished. There may be a separate |
| 2780 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 2781 | /// immediately!) for this method, if it was also defined inside the |
| 2782 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2783 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2784 | if (!MethodD) |
| 2785 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2786 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2787 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2788 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2789 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2790 | |
| 2791 | // Now that we have our default arguments, check the constructor |
| 2792 | // again. It could produce additional diagnostics or affect whether |
| 2793 | // the class has implicitly-declared destructors, among other |
| 2794 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2795 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 2796 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2797 | |
| 2798 | // Check the default arguments, which we may have added. |
| 2799 | if (!Method->isInvalidDecl()) |
| 2800 | CheckCXXDefaultArguments(Method); |
| 2801 | } |
| 2802 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2803 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2804 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2805 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2806 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 2807 | /// will be updated to reflect a well-formed type for the constructor and |
| 2808 | /// returned. |
| 2809 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2810 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2811 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2812 | |
| 2813 | // C++ [class.ctor]p3: |
| 2814 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 2815 | // constructor can be invoked for a const, volatile or const |
| 2816 | // volatile object. A constructor shall not be declared const, |
| 2817 | // volatile, or const volatile (9.3.2). |
| 2818 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2819 | if (!D.isInvalidType()) |
| 2820 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2821 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 2822 | << SourceRange(D.getIdentifierLoc()); |
| 2823 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2824 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2825 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2826 | if (!D.isInvalidType()) |
| 2827 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2828 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2829 | << SourceRange(D.getIdentifierLoc()); |
| 2830 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2831 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2832 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2833 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2834 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2835 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2836 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2837 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2838 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2839 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2840 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2841 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2842 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2843 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2844 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2845 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2847 | // Rebuild the function type "R" without any type qualifiers (in |
| 2848 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2849 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2850 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2851 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 2852 | Proto->getNumArgs(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2853 | Proto->isVariadic(), 0, |
| 2854 | Proto->hasExceptionSpec(), |
| 2855 | Proto->hasAnyExceptionSpec(), |
| 2856 | Proto->getNumExceptions(), |
| 2857 | Proto->exception_begin(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2858 | Proto->getExtInfo()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2861 | /// CheckConstructor - Checks a fully-formed constructor for |
| 2862 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 2863 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2864 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2865 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2866 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 2867 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2868 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2869 | |
| 2870 | // C++ [class.copy]p3: |
| 2871 | // A declaration of a constructor for a class X is ill-formed if |
| 2872 | // its first parameter is of type (optionally cv-qualified) X and |
| 2873 | // either there are no other parameters or else all other |
| 2874 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2875 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2876 | ((Constructor->getNumParams() == 1) || |
| 2877 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2878 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 2879 | Constructor->getTemplateSpecializationKind() |
| 2880 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2881 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 2882 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 2883 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2884 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 2885 | const char *ConstRef |
| 2886 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 2887 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2888 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 2889 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2890 | |
| 2891 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 2892 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2893 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2894 | } |
| 2895 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 2898 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 2899 | /// well-formedness, issuing any diagnostics required. Returns true |
| 2900 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2901 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2902 | CXXRecordDecl *RD = Destructor->getParent(); |
| 2903 | |
| 2904 | if (Destructor->isVirtual()) { |
| 2905 | SourceLocation Loc; |
| 2906 | |
| 2907 | if (!Destructor->isImplicit()) |
| 2908 | Loc = Destructor->getLocation(); |
| 2909 | else |
| 2910 | Loc = RD->getLocation(); |
| 2911 | |
| 2912 | // If we have a virtual destructor, look up the deallocation function |
| 2913 | FunctionDecl *OperatorDelete = 0; |
| 2914 | DeclarationName Name = |
| 2915 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2916 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2917 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 2918 | |
| 2919 | MarkDeclarationReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2920 | |
| 2921 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2922 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2923 | |
| 2924 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2928 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 2929 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 2930 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2931 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2934 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 2935 | /// the well-formednes of the destructor declarator @p D with type @p |
| 2936 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2937 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 2938 | /// will be updated to reflect a well-formed type for the destructor and |
| 2939 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2940 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2941 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2942 | // C++ [class.dtor]p1: |
| 2943 | // [...] A typedef-name that names a class is a class-name |
| 2944 | // (7.1.3); however, a typedef-name that names a class shall not |
| 2945 | // be used as the identifier in the declarator for a destructor |
| 2946 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2947 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2948 | if (isa<TypedefType>(DeclaratorType)) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2949 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2950 | << DeclaratorType; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2951 | |
| 2952 | // C++ [class.dtor]p2: |
| 2953 | // A destructor is used to destroy objects of its class type. A |
| 2954 | // destructor takes no parameters, and no return type can be |
| 2955 | // specified for it (not even void). The address of a destructor |
| 2956 | // shall not be taken. A destructor shall not be static. A |
| 2957 | // destructor can be invoked for a const, volatile or const |
| 2958 | // volatile object. A destructor shall not be declared const, |
| 2959 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2960 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2961 | if (!D.isInvalidType()) |
| 2962 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 2963 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2964 | << SourceRange(D.getIdentifierLoc()) |
| 2965 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 2966 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2967 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2968 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2969 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2970 | // Destructors don't have return types, but the parser will |
| 2971 | // happily parse something like: |
| 2972 | // |
| 2973 | // class X { |
| 2974 | // float ~X(); |
| 2975 | // }; |
| 2976 | // |
| 2977 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2978 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 2979 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2980 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2981 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2982 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2983 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2984 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2985 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2986 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2987 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2988 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2989 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2990 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2991 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2992 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2993 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2994 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2998 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2999 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 3000 | |
| 3001 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3002 | FTI.freeArgs(); |
| 3003 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3006 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3007 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3008 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3009 | D.setInvalidType(); |
| 3010 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3011 | |
| 3012 | // Rebuild the function type "R" without any type qualifiers or |
| 3013 | // parameters (in case any of the errors above fired) and with |
| 3014 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3015 | // types. |
| 3016 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 3017 | if (!Proto) |
| 3018 | return QualType(); |
| 3019 | |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 3020 | return Context.getFunctionType(Context.VoidTy, 0, 0, false, 0, |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3021 | Proto->hasExceptionSpec(), |
| 3022 | Proto->hasAnyExceptionSpec(), |
| 3023 | Proto->getNumExceptions(), |
| 3024 | Proto->exception_begin(), |
| 3025 | Proto->getExtInfo()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3028 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 3029 | /// well-formednes of the conversion function declarator @p D with |
| 3030 | /// type @p R. If there are any errors in the declarator, this routine |
| 3031 | /// will emit diagnostics and return true. Otherwise, it will return |
| 3032 | /// false. Either way, the type @p R will be updated to reflect a |
| 3033 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3034 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3035 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3036 | // C++ [class.conv.fct]p1: |
| 3037 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3038 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3039 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3040 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3041 | if (!D.isInvalidType()) |
| 3042 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 3043 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 3044 | << SourceRange(D.getIdentifierLoc()); |
| 3045 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3046 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3047 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3048 | |
| 3049 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 3050 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3051 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3052 | // Conversion functions don't have return types, but the parser will |
| 3053 | // happily parse something like: |
| 3054 | // |
| 3055 | // class X { |
| 3056 | // float operator bool(); |
| 3057 | // }; |
| 3058 | // |
| 3059 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3060 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 3061 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 3062 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3063 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3066 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 3067 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3068 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3069 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3070 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 3071 | |
| 3072 | // Delete the parameters. |
Chris Lattner | 1833a83 | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 3073 | D.getTypeObject(0).Fun.freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3074 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3075 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3076 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3077 | D.setInvalidType(); |
| 3078 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3079 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3080 | // Diagnose "&operator bool()" and other such nonsense. This |
| 3081 | // is actually a gcc extension which we don't support. |
| 3082 | if (Proto->getResultType() != ConvType) { |
| 3083 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 3084 | << Proto->getResultType(); |
| 3085 | D.setInvalidType(); |
| 3086 | ConvType = Proto->getResultType(); |
| 3087 | } |
| 3088 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3089 | // C++ [class.conv.fct]p4: |
| 3090 | // The conversion-type-id shall not represent a function type nor |
| 3091 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3092 | if (ConvType->isArrayType()) { |
| 3093 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 3094 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3095 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3096 | } else if (ConvType->isFunctionType()) { |
| 3097 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 3098 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3099 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | // Rebuild the function type "R" without any parameters (in case any |
| 3103 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3104 | // return type. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3105 | if (D.isInvalidType()) { |
| 3106 | R = Context.getFunctionType(ConvType, 0, 0, false, |
| 3107 | Proto->getTypeQuals(), |
| 3108 | Proto->hasExceptionSpec(), |
| 3109 | Proto->hasAnyExceptionSpec(), |
| 3110 | Proto->getNumExceptions(), |
| 3111 | Proto->exception_begin(), |
| 3112 | Proto->getExtInfo()); |
| 3113 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3114 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3115 | // C++0x explicit conversion operators. |
| 3116 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3118 | diag::warn_explicit_conversion_functions) |
| 3119 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3122 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 3123 | /// the declaration of the given C++ conversion function. This routine |
| 3124 | /// is responsible for recording the conversion function in the C++ |
| 3125 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3126 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3127 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 3128 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 3129 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3130 | |
| 3131 | // Make sure we aren't redeclaring the conversion function. |
| 3132 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3133 | |
| 3134 | // C++ [class.conv.fct]p1: |
| 3135 | // [...] A conversion function is never used to convert a |
| 3136 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 3137 | // same object type (or a reference to it), to a (possibly |
| 3138 | // cv-qualified) base class of that type (or a reference to it), |
| 3139 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3140 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 3141 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3142 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3143 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3144 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3145 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 3146 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 3147 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 3148 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 3149 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3150 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 3151 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3152 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3153 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3154 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3155 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3156 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3157 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3158 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3159 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 3162 | if (FunctionTemplateDecl *ConversionTemplate |
| 3163 | = Conversion->getDescribedFunctionTemplate()) |
| 3164 | return ConversionTemplate; |
| 3165 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3166 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3169 | //===----------------------------------------------------------------------===// |
| 3170 | // Namespace Handling |
| 3171 | //===----------------------------------------------------------------------===// |
| 3172 | |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3173 | |
| 3174 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3175 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 3176 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3177 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 3178 | SourceLocation InlineLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3179 | SourceLocation IdentLoc, |
| 3180 | IdentifierInfo *II, |
| 3181 | SourceLocation LBrace, |
| 3182 | AttributeList *AttrList) { |
Douglas Gregor | 21e09b6 | 2010-08-19 20:55:47 +0000 | [diff] [blame] | 3183 | // anonymous namespace starts at its left brace |
| 3184 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, |
| 3185 | (II ? IdentLoc : LBrace) , II); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3186 | Namespc->setLBracLoc(LBrace); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3187 | Namespc->setInline(InlineLoc.isValid()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3188 | |
| 3189 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 3190 | |
Anders Carlsson | 2a3503d | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 3191 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 3192 | |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3193 | if (const VisibilityAttr *attr = Namespc->getAttr<VisibilityAttr>()) |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3194 | PushVisibilityAttr(attr); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3195 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3196 | if (II) { |
| 3197 | // C++ [namespace.def]p2: |
| 3198 | // The identifier in an original-namespace-definition shall not have been |
| 3199 | // previously defined in the declarative region in which the |
| 3200 | // original-namespace-definition appears. The identifier in an |
| 3201 | // original-namespace-definition is the name of the namespace. Subsequently |
| 3202 | // in that declarative region, it is treated as an original-namespace-name. |
| 3203 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3204 | NamedDecl *PrevDecl |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 3205 | = LookupSingleName(DeclRegionScope, II, IdentLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 3206 | ForRedeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3207 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3208 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 3209 | // This is an extended namespace definition. |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3210 | if (Namespc->isInline() != OrigNS->isInline()) { |
| 3211 | // inline-ness must match |
| 3212 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 3213 | << Namespc->isInline(); |
| 3214 | Diag(OrigNS->getLocation(), diag::note_previous_definition); |
| 3215 | Namespc->setInvalidDecl(); |
| 3216 | // Recover by ignoring the new namespace's inline status. |
| 3217 | Namespc->setInline(OrigNS->isInline()); |
| 3218 | } |
| 3219 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3220 | // Attach this namespace decl to the chain of extended namespace |
| 3221 | // definitions. |
| 3222 | OrigNS->setNextNamespace(Namespc); |
| 3223 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3224 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3225 | // Remove the previous declaration from the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3226 | if (DeclRegionScope->isDeclScope(OrigNS)) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 3227 | IdResolver.RemoveDecl(OrigNS); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3228 | DeclRegionScope->RemoveDecl(OrigNS); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3229 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3230 | } else if (PrevDecl) { |
| 3231 | // This is an invalid name redefinition. |
| 3232 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 3233 | << Namespc->getDeclName(); |
| 3234 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3235 | Namespc->setInvalidDecl(); |
| 3236 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3237 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3238 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3239 | // This is the first "real" definition of the namespace "std", so update |
| 3240 | // our cache of the "std" namespace to point at this definition. |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3241 | if (NamespaceDecl *StdNS = getStdNamespace()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3242 | // We had already defined a dummy namespace "std". Link this new |
| 3243 | // namespace definition to the dummy namespace "std". |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3244 | StdNS->setNextNamespace(Namespc); |
| 3245 | StdNS->setLocation(IdentLoc); |
| 3246 | Namespc->setOriginalNamespace(StdNS->getOriginalNamespace()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | // Make our StdNamespace cache point at the first real definition of the |
| 3250 | // "std" namespace. |
| 3251 | StdNamespace = Namespc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3252 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3253 | |
| 3254 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 3255 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3256 | // Anonymous namespaces. |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3257 | assert(Namespc->isAnonymousNamespace()); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3258 | |
| 3259 | // Link the anonymous namespace into its parent. |
| 3260 | NamespaceDecl *PrevDecl; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3261 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3262 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 3263 | PrevDecl = TU->getAnonymousNamespace(); |
| 3264 | TU->setAnonymousNamespace(Namespc); |
| 3265 | } else { |
| 3266 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 3267 | PrevDecl = ND->getAnonymousNamespace(); |
| 3268 | ND->setAnonymousNamespace(Namespc); |
| 3269 | } |
| 3270 | |
| 3271 | // Link the anonymous namespace with its previous declaration. |
| 3272 | if (PrevDecl) { |
| 3273 | assert(PrevDecl->isAnonymousNamespace()); |
| 3274 | assert(!PrevDecl->getNextNamespace()); |
| 3275 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 3276 | PrevDecl->setNextNamespace(Namespc); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3277 | |
| 3278 | if (Namespc->isInline() != PrevDecl->isInline()) { |
| 3279 | // inline-ness must match |
| 3280 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 3281 | << Namespc->isInline(); |
| 3282 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3283 | Namespc->setInvalidDecl(); |
| 3284 | // Recover by ignoring the new namespace's inline status. |
| 3285 | Namespc->setInline(PrevDecl->isInline()); |
| 3286 | } |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3287 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3288 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 3289 | CurContext->addDecl(Namespc); |
| 3290 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3291 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 3292 | // behaves as if it were replaced by |
| 3293 | // namespace unique { /* empty body */ } |
| 3294 | // using namespace unique; |
| 3295 | // namespace unique { namespace-body } |
| 3296 | // where all occurrences of 'unique' in a translation unit are |
| 3297 | // replaced by the same identifier and this identifier differs |
| 3298 | // from all other identifiers in the entire program. |
| 3299 | |
| 3300 | // We just create the namespace with an empty name and then add an |
| 3301 | // implicit using declaration, just like the standard suggests. |
| 3302 | // |
| 3303 | // CodeGen enforces the "universally unique" aspect by giving all |
| 3304 | // declarations semantically contained within an anonymous |
| 3305 | // namespace internal linkage. |
| 3306 | |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3307 | if (!PrevDecl) { |
| 3308 | UsingDirectiveDecl* UD |
| 3309 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 3310 | /* 'using' */ LBrace, |
| 3311 | /* 'namespace' */ SourceLocation(), |
| 3312 | /* qualifier */ SourceRange(), |
| 3313 | /* NNS */ NULL, |
| 3314 | /* identifier */ SourceLocation(), |
| 3315 | Namespc, |
| 3316 | /* Ancestor */ CurContext); |
| 3317 | UD->setImplicit(); |
| 3318 | CurContext->addDecl(UD); |
| 3319 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 3323 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3324 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 3325 | // for the namespace has the declarations that showed up in that particular |
| 3326 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3327 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3328 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3331 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 3332 | /// is a namespace alias, returns the namespace it points to. |
| 3333 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 3334 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 3335 | return AD->getNamespace(); |
| 3336 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 3337 | } |
| 3338 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3339 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 3340 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3341 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3342 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 3343 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 3344 | Namespc->setRBracLoc(RBrace); |
| 3345 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3346 | if (Namespc->hasAttr<VisibilityAttr>()) |
| 3347 | PopPragmaVisibility(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3348 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3349 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 3350 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 3351 | return cast_or_null<CXXRecordDecl>( |
| 3352 | StdBadAlloc.get(Context.getExternalSource())); |
| 3353 | } |
| 3354 | |
| 3355 | NamespaceDecl *Sema::getStdNamespace() const { |
| 3356 | return cast_or_null<NamespaceDecl>( |
| 3357 | StdNamespace.get(Context.getExternalSource())); |
| 3358 | } |
| 3359 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3360 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 3361 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 3362 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3363 | if (!StdNamespace) { |
| 3364 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 3365 | StdNamespace = NamespaceDecl::Create(Context, |
| 3366 | Context.getTranslationUnitDecl(), |
| 3367 | SourceLocation(), |
| 3368 | &PP.getIdentifierTable().get("std")); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3369 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3372 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3375 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3376 | SourceLocation UsingLoc, |
| 3377 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3378 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3379 | SourceLocation IdentLoc, |
| 3380 | IdentifierInfo *NamespcName, |
| 3381 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3382 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3383 | assert(NamespcName && "Invalid NamespcName."); |
| 3384 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3385 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3386 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3387 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3388 | NestedNameSpecifier *Qualifier = 0; |
| 3389 | if (SS.isSet()) |
| 3390 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3391 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 3392 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3393 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 3394 | LookupParsedName(R, S, &SS); |
| 3395 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3396 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3398 | if (R.empty()) { |
| 3399 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 3400 | // "std" hasn't been defined yet, for GCC compatibility. |
| 3401 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 3402 | NamespcName->isStr("std")) { |
| 3403 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 3404 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3405 | R.resolveKind(); |
| 3406 | } |
| 3407 | // Otherwise, attempt typo correction. |
| 3408 | else if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 3409 | CTC_NoKeywords, 0)) { |
| 3410 | if (R.getAsSingle<NamespaceDecl>() || |
| 3411 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 3412 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 3413 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 3414 | << NamespcName << DC << Corrected << SS.getRange() |
| 3415 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 3416 | else |
| 3417 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 3418 | << NamespcName << Corrected |
| 3419 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 3420 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 3421 | << Corrected; |
| 3422 | |
| 3423 | NamespcName = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3424 | } else { |
| 3425 | R.clear(); |
| 3426 | R.setLookupName(NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3427 | } |
| 3428 | } |
| 3429 | } |
| 3430 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3431 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3432 | NamedDecl *Named = R.getFoundDecl(); |
| 3433 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 3434 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3435 | // C++ [namespace.udir]p1: |
| 3436 | // A using-directive specifies that the names in the nominated |
| 3437 | // namespace can be used in the scope in which the |
| 3438 | // using-directive appears after the using-directive. During |
| 3439 | // unqualified name lookup (3.4.1), the names appear as if they |
| 3440 | // were declared in the nearest enclosing namespace which |
| 3441 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3442 | // namespace. [Note: in this context, "contains" means "contains |
| 3443 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3444 | |
| 3445 | // Find enclosing context containing both using-directive and |
| 3446 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3447 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3448 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 3449 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 3450 | CommonAncestor = CommonAncestor->getParent(); |
| 3451 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3452 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 3453 | SS.getRange(), |
| 3454 | (NestedNameSpecifier *)SS.getScopeRep(), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3455 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3456 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3457 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 3458 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3461 | // FIXME: We ignore attributes for now. |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3462 | delete AttrList; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3463 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 3467 | // If scope has associated entity, then using directive is at namespace |
| 3468 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 3469 | // it's lookup structure. |
| 3470 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3471 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3472 | else |
| 3473 | // Otherwise it is block-sope. using-directives will affect lookup |
| 3474 | // only to the end of scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3475 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3476 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3477 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3478 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3479 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
Anders Carlsson | 595adc1 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 3480 | AccessSpecifier AS, |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3481 | bool HasUsingKeyword, |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3482 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3483 | CXXScopeSpec &SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3484 | UnqualifiedId &Name, |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3485 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3486 | bool IsTypeName, |
| 3487 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3488 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3489 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3490 | switch (Name.getKind()) { |
| 3491 | case UnqualifiedId::IK_Identifier: |
| 3492 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3493 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3494 | case UnqualifiedId::IK_ConversionFunctionId: |
| 3495 | break; |
| 3496 | |
| 3497 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3498 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3499 | // C++0x inherited constructors. |
| 3500 | if (getLangOptions().CPlusPlus0x) break; |
| 3501 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3502 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 3503 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3504 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3505 | |
| 3506 | case UnqualifiedId::IK_DestructorName: |
| 3507 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 3508 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3509 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3510 | |
| 3511 | case UnqualifiedId::IK_TemplateId: |
| 3512 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 3513 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3514 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3515 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3516 | |
| 3517 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 3518 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3519 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3520 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3521 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3522 | // Warn about using declarations. |
| 3523 | // TODO: store that the declaration was written without 'using' and |
| 3524 | // talk about access decls instead of using decls in the |
| 3525 | // diagnostics. |
| 3526 | if (!HasUsingKeyword) { |
| 3527 | UsingLoc = Name.getSourceRange().getBegin(); |
| 3528 | |
| 3529 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3530 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3533 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3534 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3535 | /* IsInstantiation */ false, |
| 3536 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3537 | if (UD) |
| 3538 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3539 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3540 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 3543 | /// \brief Determine whether a using declaration considers the given |
| 3544 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 3545 | /// the same entity or are both typedefs of the same type. |
| 3546 | static bool |
| 3547 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 3548 | bool &SuppressRedeclaration) { |
| 3549 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 3550 | SuppressRedeclaration = false; |
| 3551 | return true; |
| 3552 | } |
| 3553 | |
| 3554 | if (TypedefDecl *TD1 = dyn_cast<TypedefDecl>(D1)) |
| 3555 | if (TypedefDecl *TD2 = dyn_cast<TypedefDecl>(D2)) { |
| 3556 | SuppressRedeclaration = true; |
| 3557 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 3558 | TD2->getUnderlyingType()); |
| 3559 | } |
| 3560 | |
| 3561 | return false; |
| 3562 | } |
| 3563 | |
| 3564 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3565 | /// Determines whether to create a using shadow decl for a particular |
| 3566 | /// decl, given the set of decls existing prior to this using lookup. |
| 3567 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 3568 | const LookupResult &Previous) { |
| 3569 | // Diagnose finding a decl which is not from a base class of the |
| 3570 | // current class. We do this now because there are cases where this |
| 3571 | // function will silently decide not to build a shadow decl, which |
| 3572 | // will pre-empt further diagnostics. |
| 3573 | // |
| 3574 | // We don't need to do this in C++0x because we do the check once on |
| 3575 | // the qualifier. |
| 3576 | // |
| 3577 | // FIXME: diagnose the following if we care enough: |
| 3578 | // struct A { int foo; }; |
| 3579 | // struct B : A { using A::foo; }; |
| 3580 | // template <class T> struct C : A {}; |
| 3581 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 3582 | // This is invalid (during instantiation) in C++03 because B::foo |
| 3583 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 3584 | // We can't diagnose it immediately because C<T> is an unknown |
| 3585 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 3586 | // to A::foo, which will look well-formed when we instantiate. |
| 3587 | // The right solution is to not collapse the shadow-decl chain. |
| 3588 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 3589 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 3590 | |
| 3591 | // Handle enums and anonymous structs. |
| 3592 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 3593 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 3594 | while (OrigRec->isAnonymousStructOrUnion()) |
| 3595 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 3596 | |
| 3597 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 3598 | if (OrigDC == CurContext) { |
| 3599 | Diag(Using->getLocation(), |
| 3600 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3601 | << Using->getNestedNameRange(); |
| 3602 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3603 | return true; |
| 3604 | } |
| 3605 | |
| 3606 | Diag(Using->getNestedNameRange().getBegin(), |
| 3607 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3608 | << Using->getTargetNestedNameDecl() |
| 3609 | << cast<CXXRecordDecl>(CurContext) |
| 3610 | << Using->getNestedNameRange(); |
| 3611 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3612 | return true; |
| 3613 | } |
| 3614 | } |
| 3615 | |
| 3616 | if (Previous.empty()) return false; |
| 3617 | |
| 3618 | NamedDecl *Target = Orig; |
| 3619 | if (isa<UsingShadowDecl>(Target)) |
| 3620 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3621 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3622 | // If the target happens to be one of the previous declarations, we |
| 3623 | // don't have a conflict. |
| 3624 | // |
| 3625 | // FIXME: but we might be increasing its access, in which case we |
| 3626 | // should redeclare it. |
| 3627 | NamedDecl *NonTag = 0, *Tag = 0; |
| 3628 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 3629 | I != E; ++I) { |
| 3630 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 3631 | bool Result; |
| 3632 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 3633 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3634 | |
| 3635 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 3636 | } |
| 3637 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3638 | if (Target->isFunctionOrFunctionTemplate()) { |
| 3639 | FunctionDecl *FD; |
| 3640 | if (isa<FunctionTemplateDecl>(Target)) |
| 3641 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 3642 | else |
| 3643 | FD = cast<FunctionDecl>(Target); |
| 3644 | |
| 3645 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3646 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3647 | case Ovl_Overload: |
| 3648 | return false; |
| 3649 | |
| 3650 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3651 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3652 | break; |
| 3653 | |
| 3654 | // We found a decl with the exact signature. |
| 3655 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3656 | // If we're in a record, we want to hide the target, so we |
| 3657 | // return true (without a diagnostic) to tell the caller not to |
| 3658 | // build a shadow decl. |
| 3659 | if (CurContext->isRecord()) |
| 3660 | return true; |
| 3661 | |
| 3662 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3663 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3664 | break; |
| 3665 | } |
| 3666 | |
| 3667 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3668 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 3669 | return true; |
| 3670 | } |
| 3671 | |
| 3672 | // Target is not a function. |
| 3673 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3674 | if (isa<TagDecl>(Target)) { |
| 3675 | // No conflict between a tag and a non-tag. |
| 3676 | if (!Tag) return false; |
| 3677 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3678 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3679 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3680 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 3681 | return true; |
| 3682 | } |
| 3683 | |
| 3684 | // No conflict between a tag and a non-tag. |
| 3685 | if (!NonTag) return false; |
| 3686 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3687 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3688 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3689 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 3690 | return true; |
| 3691 | } |
| 3692 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3693 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3694 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3695 | UsingDecl *UD, |
| 3696 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3697 | |
| 3698 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3699 | NamedDecl *Target = Orig; |
| 3700 | if (isa<UsingShadowDecl>(Target)) { |
| 3701 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3702 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
| 3705 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3706 | = UsingShadowDecl::Create(Context, CurContext, |
| 3707 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3708 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 3709 | |
| 3710 | Shadow->setAccess(UD->getAccess()); |
| 3711 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 3712 | Shadow->setInvalidDecl(); |
| 3713 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3714 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3715 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3716 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3717 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3718 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3719 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3720 | return Shadow; |
| 3721 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3722 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3723 | /// Hides a using shadow declaration. This is required by the current |
| 3724 | /// using-decl implementation when a resolvable using declaration in a |
| 3725 | /// class is followed by a declaration which would hide or override |
| 3726 | /// one or more of the using decl's targets; for example: |
| 3727 | /// |
| 3728 | /// struct Base { void foo(int); }; |
| 3729 | /// struct Derived : Base { |
| 3730 | /// using Base::foo; |
| 3731 | /// void foo(int); |
| 3732 | /// }; |
| 3733 | /// |
| 3734 | /// The governing language is C++03 [namespace.udecl]p12: |
| 3735 | /// |
| 3736 | /// When a using-declaration brings names from a base class into a |
| 3737 | /// derived class scope, member functions in the derived class |
| 3738 | /// override and/or hide member functions with the same name and |
| 3739 | /// parameter types in a base class (rather than conflicting). |
| 3740 | /// |
| 3741 | /// There are two ways to implement this: |
| 3742 | /// (1) optimistically create shadow decls when they're not hidden |
| 3743 | /// by existing declarations, or |
| 3744 | /// (2) don't create any shadow decls (or at least don't make them |
| 3745 | /// visible) until we've fully parsed/instantiated the class. |
| 3746 | /// The problem with (1) is that we might have to retroactively remove |
| 3747 | /// a shadow decl, which requires several O(n) operations because the |
| 3748 | /// decl structures are (very reasonably) not designed for removal. |
| 3749 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 3750 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3751 | if (Shadow->getDeclName().getNameKind() == |
| 3752 | DeclarationName::CXXConversionFunctionName) |
| 3753 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 3754 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3755 | // Remove it from the DeclContext... |
| 3756 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3757 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3758 | // ...and the scope, if applicable... |
| 3759 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3760 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3761 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3764 | // ...and the using decl. |
| 3765 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 3766 | |
| 3767 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3768 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3771 | /// Builds a using declaration. |
| 3772 | /// |
| 3773 | /// \param IsInstantiation - Whether this call arises from an |
| 3774 | /// instantiation of an unresolved using declaration. We treat |
| 3775 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3776 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 3777 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3778 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3779 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3780 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3781 | bool IsInstantiation, |
| 3782 | bool IsTypeName, |
| 3783 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3784 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3785 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3786 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 3787 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3788 | // FIXME: We ignore attributes for now. |
| 3789 | delete AttrList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3790 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3791 | if (SS.isEmpty()) { |
| 3792 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3793 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3794 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3795 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3796 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3797 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3798 | ForRedeclaration); |
| 3799 | Previous.setHideTags(false); |
| 3800 | if (S) { |
| 3801 | LookupName(Previous, S); |
| 3802 | |
| 3803 | // It is really dumb that we have to do this. |
| 3804 | LookupResult::Filter F = Previous.makeFilter(); |
| 3805 | while (F.hasNext()) { |
| 3806 | NamedDecl *D = F.next(); |
| 3807 | if (!isDeclInScope(D, CurContext, S)) |
| 3808 | F.erase(); |
| 3809 | } |
| 3810 | F.done(); |
| 3811 | } else { |
| 3812 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 3813 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 3814 | LookupQualifiedName(Previous, CurContext); |
| 3815 | } |
| 3816 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | NestedNameSpecifier *NNS = |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3818 | static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3819 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3820 | // Check for invalid redeclarations. |
| 3821 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 3822 | return 0; |
| 3823 | |
| 3824 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3825 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 3826 | return 0; |
| 3827 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3828 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3829 | NamedDecl *D; |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3830 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3831 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3832 | // FIXME: not all declaration name kinds are legal here |
| 3833 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 3834 | UsingLoc, TypenameLoc, |
| 3835 | SS.getRange(), NNS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3836 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3837 | } else { |
| 3838 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3839 | UsingLoc, SS.getRange(), |
| 3840 | NNS, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3841 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3842 | } else { |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3843 | D = UsingDecl::Create(Context, CurContext, |
| 3844 | SS.getRange(), UsingLoc, NNS, NameInfo, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3845 | IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3846 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3847 | D->setAccess(AS); |
| 3848 | CurContext->addDecl(D); |
| 3849 | |
| 3850 | if (!LookupContext) return D; |
| 3851 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3853 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3854 | UD->setInvalidDecl(); |
| 3855 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3858 | // Look up the target name. |
| 3859 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3860 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3861 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3862 | // Unlike most lookups, we don't always want to hide tag |
| 3863 | // declarations: tag names are visible through the using declaration |
| 3864 | // even if hidden by ordinary names, *except* in a dependent context |
| 3865 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3866 | if (!IsInstantiation) |
| 3867 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3868 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3869 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3870 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3871 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 3872 | Diag(IdentLoc, diag::err_no_member) |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3873 | << NameInfo.getName() << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3874 | UD->setInvalidDecl(); |
| 3875 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3878 | if (R.isAmbiguous()) { |
| 3879 | UD->setInvalidDecl(); |
| 3880 | return UD; |
| 3881 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3882 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3883 | if (IsTypeName) { |
| 3884 | // 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] | 3885 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3886 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 3887 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 3888 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 3889 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3890 | UD->setInvalidDecl(); |
| 3891 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3892 | } |
| 3893 | } else { |
| 3894 | // If we asked for a non-typename and we got a type, error out, |
| 3895 | // but only if this is an instantiation of an unresolved using |
| 3896 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3897 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3898 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 3899 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3900 | UD->setInvalidDecl(); |
| 3901 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3902 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3905 | // C++0x N2914 [namespace.udecl]p6: |
| 3906 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3907 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3908 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 3909 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3910 | UD->setInvalidDecl(); |
| 3911 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3912 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3913 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3914 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 3915 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 3916 | BuildUsingShadowDecl(S, UD, *I); |
| 3917 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3918 | |
| 3919 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3920 | } |
| 3921 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3922 | /// Checks that the given using declaration is not an invalid |
| 3923 | /// redeclaration. Note that this is checking only for the using decl |
| 3924 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 3925 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 3926 | bool isTypeName, |
| 3927 | const CXXScopeSpec &SS, |
| 3928 | SourceLocation NameLoc, |
| 3929 | const LookupResult &Prev) { |
| 3930 | // C++03 [namespace.udecl]p8: |
| 3931 | // C++0x [namespace.udecl]p10: |
| 3932 | // A using-declaration is a declaration and can therefore be used |
| 3933 | // repeatedly where (and only where) multiple declarations are |
| 3934 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 3935 | // |
| 3936 | // That's in non-member contexts. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3937 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3938 | return false; |
| 3939 | |
| 3940 | NestedNameSpecifier *Qual |
| 3941 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 3942 | |
| 3943 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 3944 | NamedDecl *D = *I; |
| 3945 | |
| 3946 | bool DTypename; |
| 3947 | NestedNameSpecifier *DQual; |
| 3948 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 3949 | DTypename = UD->isTypeName(); |
| 3950 | DQual = UD->getTargetNestedNameDecl(); |
| 3951 | } else if (UnresolvedUsingValueDecl *UD |
| 3952 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 3953 | DTypename = false; |
| 3954 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3955 | } else if (UnresolvedUsingTypenameDecl *UD |
| 3956 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 3957 | DTypename = true; |
| 3958 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3959 | } else continue; |
| 3960 | |
| 3961 | // using decls differ if one says 'typename' and the other doesn't. |
| 3962 | // FIXME: non-dependent using decls? |
| 3963 | if (isTypeName != DTypename) continue; |
| 3964 | |
| 3965 | // using decls differ if they name different scopes (but note that |
| 3966 | // template instantiation can cause this check to trigger when it |
| 3967 | // didn't before instantiation). |
| 3968 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 3969 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 3970 | continue; |
| 3971 | |
| 3972 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3973 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3974 | return true; |
| 3975 | } |
| 3976 | |
| 3977 | return false; |
| 3978 | } |
| 3979 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3980 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3981 | /// Checks that the given nested-name qualifier used in a using decl |
| 3982 | /// in the current context is appropriately related to the current |
| 3983 | /// scope. If an error is found, diagnoses it and returns true. |
| 3984 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 3985 | const CXXScopeSpec &SS, |
| 3986 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3987 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3988 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3989 | if (!CurContext->isRecord()) { |
| 3990 | // C++03 [namespace.udecl]p3: |
| 3991 | // C++0x [namespace.udecl]p8: |
| 3992 | // A using-declaration for a class member shall be a member-declaration. |
| 3993 | |
| 3994 | // If we weren't able to compute a valid scope, it must be a |
| 3995 | // dependent class scope. |
| 3996 | if (!NamedContext || NamedContext->isRecord()) { |
| 3997 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 3998 | << SS.getRange(); |
| 3999 | return true; |
| 4000 | } |
| 4001 | |
| 4002 | // Otherwise, everything is known to be fine. |
| 4003 | return false; |
| 4004 | } |
| 4005 | |
| 4006 | // The current scope is a record. |
| 4007 | |
| 4008 | // If the named context is dependent, we can't decide much. |
| 4009 | if (!NamedContext) { |
| 4010 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 4011 | // nested-name-specifier does not refer to a base class, which is |
| 4012 | // still possible in some cases. |
| 4013 | |
| 4014 | // Otherwise we have to conservatively report that things might be |
| 4015 | // okay. |
| 4016 | return false; |
| 4017 | } |
| 4018 | |
| 4019 | if (!NamedContext->isRecord()) { |
| 4020 | // Ideally this would point at the last name in the specifier, |
| 4021 | // but we don't have that level of source info. |
| 4022 | Diag(SS.getRange().getBegin(), |
| 4023 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 4024 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 4025 | return true; |
| 4026 | } |
| 4027 | |
| 4028 | if (getLangOptions().CPlusPlus0x) { |
| 4029 | // C++0x [namespace.udecl]p3: |
| 4030 | // In a using-declaration used as a member-declaration, the |
| 4031 | // nested-name-specifier shall name a base class of the class |
| 4032 | // being defined. |
| 4033 | |
| 4034 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 4035 | cast<CXXRecordDecl>(NamedContext))) { |
| 4036 | if (CurContext == NamedContext) { |
| 4037 | Diag(NameLoc, |
| 4038 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 4039 | << SS.getRange(); |
| 4040 | return true; |
| 4041 | } |
| 4042 | |
| 4043 | Diag(SS.getRange().getBegin(), |
| 4044 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 4045 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 4046 | << cast<CXXRecordDecl>(CurContext) |
| 4047 | << SS.getRange(); |
| 4048 | return true; |
| 4049 | } |
| 4050 | |
| 4051 | return false; |
| 4052 | } |
| 4053 | |
| 4054 | // C++03 [namespace.udecl]p4: |
| 4055 | // A using-declaration used as a member-declaration shall refer |
| 4056 | // to a member of a base class of the class being defined [etc.]. |
| 4057 | |
| 4058 | // Salient point: SS doesn't have to name a base class as long as |
| 4059 | // lookup only finds members from base classes. Therefore we can |
| 4060 | // diagnose here only if we can prove that that can't happen, |
| 4061 | // i.e. if the class hierarchies provably don't intersect. |
| 4062 | |
| 4063 | // TODO: it would be nice if "definitely valid" results were cached |
| 4064 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 4065 | // need to be repeated. |
| 4066 | |
| 4067 | struct UserData { |
| 4068 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 4069 | |
| 4070 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 4071 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 4072 | Data->Bases.insert(Base); |
| 4073 | return true; |
| 4074 | } |
| 4075 | |
| 4076 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 4077 | return !Class->forallBases(collect, this); |
| 4078 | } |
| 4079 | |
| 4080 | /// Returns true if the base is dependent or is one of the |
| 4081 | /// accumulated base classes. |
| 4082 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 4083 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 4084 | return !Data->Bases.count(Base); |
| 4085 | } |
| 4086 | |
| 4087 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 4088 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 4089 | } |
| 4090 | }; |
| 4091 | |
| 4092 | UserData Data; |
| 4093 | |
| 4094 | // Returns false if we find a dependent base. |
| 4095 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 4096 | return false; |
| 4097 | |
| 4098 | // Returns false if the class has a dependent base or if it or one |
| 4099 | // of its bases is present in the base set of the current context. |
| 4100 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 4101 | return false; |
| 4102 | |
| 4103 | Diag(SS.getRange().getBegin(), |
| 4104 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 4105 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 4106 | << cast<CXXRecordDecl>(CurContext) |
| 4107 | << SS.getRange(); |
| 4108 | |
| 4109 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4112 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 4113 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4114 | SourceLocation AliasLoc, |
| 4115 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4116 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 4117 | SourceLocation IdentLoc, |
| 4118 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4119 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4120 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4121 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 4122 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4123 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4124 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 4125 | NamedDecl *PrevDecl |
| 4126 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 4127 | ForRedeclaration); |
| 4128 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 4129 | PrevDecl = 0; |
| 4130 | |
| 4131 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4132 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4133 | // 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] | 4134 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4135 | // FIXME: At some point, we'll want to create the (redundant) |
| 4136 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4137 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4138 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4139 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4140 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4141 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4142 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 4143 | diag::err_redefinition_different_kind; |
| 4144 | Diag(AliasLoc, DiagID) << Alias; |
| 4145 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4146 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4149 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4150 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4151 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4152 | if (R.empty()) { |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4153 | if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 4154 | CTC_NoKeywords, 0)) { |
| 4155 | if (R.getAsSingle<NamespaceDecl>() || |
| 4156 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 4157 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 4158 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 4159 | << Ident << DC << Corrected << SS.getRange() |
| 4160 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4161 | else |
| 4162 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 4163 | << Ident << Corrected |
| 4164 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4165 | |
| 4166 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 4167 | << Corrected; |
| 4168 | |
| 4169 | Ident = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 4170 | } else { |
| 4171 | R.clear(); |
| 4172 | R.setLookupName(Ident); |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4173 | } |
| 4174 | } |
| 4175 | |
| 4176 | if (R.empty()) { |
| 4177 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4178 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4179 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 4180 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4181 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4182 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4183 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 4184 | Alias, SS.getRange(), |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 4185 | (NestedNameSpecifier *)SS.getScopeRep(), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4186 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4187 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 4188 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4189 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4192 | namespace { |
| 4193 | /// \brief Scoped object used to handle the state changes required in Sema |
| 4194 | /// to implicitly define the body of a C++ member function; |
| 4195 | class ImplicitlyDefinedFunctionScope { |
| 4196 | Sema &S; |
| 4197 | DeclContext *PreviousContext; |
| 4198 | |
| 4199 | public: |
| 4200 | ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method) |
| 4201 | : S(S), PreviousContext(S.CurContext) |
| 4202 | { |
| 4203 | S.CurContext = Method; |
| 4204 | S.PushFunctionScope(); |
| 4205 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 4206 | } |
| 4207 | |
| 4208 | ~ImplicitlyDefinedFunctionScope() { |
| 4209 | S.PopExpressionEvaluationContext(); |
| 4210 | S.PopFunctionOrBlockScope(); |
| 4211 | S.CurContext = PreviousContext; |
| 4212 | } |
| 4213 | }; |
| 4214 | } |
| 4215 | |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4216 | static CXXConstructorDecl *getDefaultConstructorUnsafe(Sema &Self, |
| 4217 | CXXRecordDecl *D) { |
| 4218 | ASTContext &Context = Self.Context; |
| 4219 | QualType ClassType = Context.getTypeDeclType(D); |
| 4220 | DeclarationName ConstructorName |
| 4221 | = Context.DeclarationNames.getCXXConstructorName( |
| 4222 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
| 4223 | |
| 4224 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 4225 | for (llvm::tie(Con, ConEnd) = D->lookup(ConstructorName); |
| 4226 | Con != ConEnd; ++Con) { |
| 4227 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 4228 | if (isa<FunctionTemplateDecl>(*Con)) |
| 4229 | continue; |
| 4230 | |
| 4231 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 4232 | if (Constructor->isDefaultConstructor()) |
| 4233 | return Constructor; |
| 4234 | } |
| 4235 | return 0; |
| 4236 | } |
| 4237 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4238 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 4239 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4240 | // C++ [class.ctor]p5: |
| 4241 | // A default constructor for a class X is a constructor of class X |
| 4242 | // that can be called without an argument. If there is no |
| 4243 | // user-declared constructor for class X, a default constructor is |
| 4244 | // implicitly declared. An implicitly-declared default constructor |
| 4245 | // is an inline public member of its class. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4246 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
| 4247 | "Should not build implicit default constructor!"); |
| 4248 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4249 | // C++ [except.spec]p14: |
| 4250 | // An implicitly declared special member function (Clause 12) shall have an |
| 4251 | // exception-specification. [...] |
| 4252 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4253 | |
| 4254 | // Direct base-class destructors. |
| 4255 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4256 | BEnd = ClassDecl->bases_end(); |
| 4257 | B != BEnd; ++B) { |
| 4258 | if (B->isVirtual()) // Handled below. |
| 4259 | continue; |
| 4260 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4261 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 4262 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4263 | if (!BaseClassDecl->hasDeclaredDefaultConstructor()) |
| 4264 | ExceptSpec.CalledDecl(DeclareImplicitDefaultConstructor(BaseClassDecl)); |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4265 | else if (CXXConstructorDecl *Constructor |
| 4266 | = getDefaultConstructorUnsafe(*this, BaseClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4267 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4268 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4269 | } |
| 4270 | |
| 4271 | // Virtual base-class destructors. |
| 4272 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 4273 | BEnd = ClassDecl->vbases_end(); |
| 4274 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4275 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 4276 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4277 | if (!BaseClassDecl->hasDeclaredDefaultConstructor()) |
| 4278 | ExceptSpec.CalledDecl(DeclareImplicitDefaultConstructor(BaseClassDecl)); |
| 4279 | else if (CXXConstructorDecl *Constructor |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4280 | = getDefaultConstructorUnsafe(*this, BaseClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4281 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4282 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | // Field destructors. |
| 4286 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4287 | FEnd = ClassDecl->field_end(); |
| 4288 | F != FEnd; ++F) { |
| 4289 | if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4290 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 4291 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4292 | if (!FieldClassDecl->hasDeclaredDefaultConstructor()) |
| 4293 | ExceptSpec.CalledDecl( |
| 4294 | DeclareImplicitDefaultConstructor(FieldClassDecl)); |
| 4295 | else if (CXXConstructorDecl *Constructor |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4296 | = getDefaultConstructorUnsafe(*this, FieldClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4297 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4298 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
| 4301 | |
| 4302 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4303 | CanQualType ClassType |
| 4304 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 4305 | DeclarationName Name |
| 4306 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4307 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4308 | CXXConstructorDecl *DefaultCon |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4309 | = CXXConstructorDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4310 | Context.getFunctionType(Context.VoidTy, |
| 4311 | 0, 0, false, 0, |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4312 | ExceptSpec.hasExceptionSpecification(), |
| 4313 | ExceptSpec.hasAnyExceptionSpecification(), |
| 4314 | ExceptSpec.size(), |
| 4315 | ExceptSpec.data(), |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4316 | FunctionType::ExtInfo()), |
| 4317 | /*TInfo=*/0, |
| 4318 | /*isExplicit=*/false, |
| 4319 | /*isInline=*/true, |
| 4320 | /*isImplicitlyDeclared=*/true); |
| 4321 | DefaultCon->setAccess(AS_public); |
| 4322 | DefaultCon->setImplicit(); |
| 4323 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4324 | |
| 4325 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4326 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
| 4327 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4328 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4329 | PushOnScopeChains(DefaultCon, S, false); |
| 4330 | ClassDecl->addDecl(DefaultCon); |
| 4331 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4332 | return DefaultCon; |
| 4333 | } |
| 4334 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4335 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 4336 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4337 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4338 | !Constructor->isUsed(false)) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4339 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4340 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 4341 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4342 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 4343 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4344 | ImplicitlyDefinedFunctionScope Scope(*this, Constructor); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4345 | ErrorTrap Trap(*this); |
| 4346 | if (SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false) || |
| 4347 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4348 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 4349 | << CXXConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4350 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4351 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4352 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4353 | |
| 4354 | SourceLocation Loc = Constructor->getLocation(); |
| 4355 | Constructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 4356 | |
| 4357 | Constructor->setUsed(); |
| 4358 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4361 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4362 | // C++ [class.dtor]p2: |
| 4363 | // If a class has no user-declared destructor, a destructor is |
| 4364 | // declared implicitly. An implicitly-declared destructor is an |
| 4365 | // inline public member of its class. |
| 4366 | |
| 4367 | // C++ [except.spec]p14: |
| 4368 | // An implicitly declared special member function (Clause 12) shall have |
| 4369 | // an exception-specification. |
| 4370 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4371 | |
| 4372 | // Direct base-class destructors. |
| 4373 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4374 | BEnd = ClassDecl->bases_end(); |
| 4375 | B != BEnd; ++B) { |
| 4376 | if (B->isVirtual()) // Handled below. |
| 4377 | continue; |
| 4378 | |
| 4379 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 4380 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4381 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4382 | } |
| 4383 | |
| 4384 | // Virtual base-class destructors. |
| 4385 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 4386 | BEnd = ClassDecl->vbases_end(); |
| 4387 | B != BEnd; ++B) { |
| 4388 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 4389 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4390 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | // Field destructors. |
| 4394 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4395 | FEnd = ClassDecl->field_end(); |
| 4396 | F != FEnd; ++F) { |
| 4397 | if (const RecordType *RecordTy |
| 4398 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
| 4399 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4400 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4401 | } |
| 4402 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4403 | // Create the actual destructor declaration. |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4404 | QualType Ty = Context.getFunctionType(Context.VoidTy, |
| 4405 | 0, 0, false, 0, |
| 4406 | ExceptSpec.hasExceptionSpecification(), |
| 4407 | ExceptSpec.hasAnyExceptionSpecification(), |
| 4408 | ExceptSpec.size(), |
| 4409 | ExceptSpec.data(), |
| 4410 | FunctionType::ExtInfo()); |
| 4411 | |
| 4412 | CanQualType ClassType |
| 4413 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 4414 | DeclarationName Name |
| 4415 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4416 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4417 | CXXDestructorDecl *Destructor |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4418 | = CXXDestructorDecl::Create(Context, ClassDecl, NameInfo, Ty, |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4419 | /*isInline=*/true, |
| 4420 | /*isImplicitlyDeclared=*/true); |
| 4421 | Destructor->setAccess(AS_public); |
| 4422 | Destructor->setImplicit(); |
| 4423 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4424 | |
| 4425 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4426 | ++ASTContext::NumImplicitDestructorsDeclared; |
| 4427 | |
| 4428 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4429 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4430 | PushOnScopeChains(Destructor, S, false); |
| 4431 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4432 | |
| 4433 | // This could be uniqued if it ever proves significant. |
| 4434 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
| 4435 | |
| 4436 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4437 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4438 | return Destructor; |
| 4439 | } |
| 4440 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4441 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 4442 | CXXDestructorDecl *Destructor) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4443 | assert((Destructor->isImplicit() && !Destructor->isUsed(false)) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4444 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4445 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4446 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4447 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4448 | if (Destructor->isInvalidDecl()) |
| 4449 | return; |
| 4450 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4451 | ImplicitlyDefinedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4453 | ErrorTrap Trap(*this); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 4454 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 4455 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4456 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4457 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4458 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4459 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 4460 | |
| 4461 | Destructor->setInvalidDecl(); |
| 4462 | return; |
| 4463 | } |
| 4464 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4465 | SourceLocation Loc = Destructor->getLocation(); |
| 4466 | Destructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 4467 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4468 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4469 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4472 | /// \brief Builds a statement that copies the given entity from \p From to |
| 4473 | /// \c To. |
| 4474 | /// |
| 4475 | /// This routine is used to copy the members of a class with an |
| 4476 | /// implicitly-declared copy assignment operator. When the entities being |
| 4477 | /// copied are arrays, this routine builds for loops to copy them. |
| 4478 | /// |
| 4479 | /// \param S The Sema object used for type-checking. |
| 4480 | /// |
| 4481 | /// \param Loc The location where the implicit copy is being generated. |
| 4482 | /// |
| 4483 | /// \param T The type of the expressions being copied. Both expressions must |
| 4484 | /// have this type. |
| 4485 | /// |
| 4486 | /// \param To The expression we are copying to. |
| 4487 | /// |
| 4488 | /// \param From The expression we are copying from. |
| 4489 | /// |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4490 | /// \param CopyingBaseSubobject Whether we're copying a base subobject. |
| 4491 | /// Otherwise, it's a non-static member subobject. |
| 4492 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4493 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 4494 | /// |
| 4495 | /// \returns A statement or a loop that copies the expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4496 | static StmtResult |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4497 | BuildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4498 | Expr *To, Expr *From, |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4499 | bool CopyingBaseSubobject, unsigned Depth = 0) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4500 | // C++0x [class.copy]p30: |
| 4501 | // Each subobject is assigned in the manner appropriate to its type: |
| 4502 | // |
| 4503 | // - if the subobject is of class type, the copy assignment operator |
| 4504 | // for the class is used (as if by explicit qualification; that is, |
| 4505 | // ignoring any possible virtual overriding functions in more derived |
| 4506 | // classes); |
| 4507 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 4508 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4509 | |
| 4510 | // Look for operator=. |
| 4511 | DeclarationName Name |
| 4512 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 4513 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 4514 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| 4515 | |
| 4516 | // Filter out any result that isn't a copy-assignment operator. |
| 4517 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 4518 | while (F.hasNext()) { |
| 4519 | NamedDecl *D = F.next(); |
| 4520 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 4521 | if (Method->isCopyAssignmentOperator()) |
| 4522 | continue; |
| 4523 | |
| 4524 | F.erase(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4525 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4526 | F.done(); |
| 4527 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4528 | // Suppress the protected check (C++ [class.protected]) for each of the |
| 4529 | // assignment operators we found. This strange dance is required when |
| 4530 | // we're assigning via a base classes's copy-assignment operator. To |
| 4531 | // ensure that we're getting the right base class subobject (without |
| 4532 | // ambiguities), we need to cast "this" to that subobject type; to |
| 4533 | // ensure that we don't go through the virtual call mechanism, we need |
| 4534 | // to qualify the operator= name with the base class (see below). However, |
| 4535 | // this means that if the base class has a protected copy assignment |
| 4536 | // operator, the protected member access check will fail. So, we |
| 4537 | // rewrite "protected" access to "public" access in this case, since we |
| 4538 | // know by construction that we're calling from a derived class. |
| 4539 | if (CopyingBaseSubobject) { |
| 4540 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 4541 | L != LEnd; ++L) { |
| 4542 | if (L.getAccess() == AS_protected) |
| 4543 | L.setAccess(AS_public); |
| 4544 | } |
| 4545 | } |
| 4546 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4547 | // Create the nested-name-specifier that will be used to qualify the |
| 4548 | // reference to operator=; this is required to suppress the virtual |
| 4549 | // call mechanism. |
| 4550 | CXXScopeSpec SS; |
| 4551 | SS.setRange(Loc); |
| 4552 | SS.setScopeRep(NestedNameSpecifier::Create(S.Context, 0, false, |
| 4553 | T.getTypePtr())); |
| 4554 | |
| 4555 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4556 | ExprResult OpEqualRef |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4557 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4558 | /*FirstQualifierInScope=*/0, OpLookup, |
| 4559 | /*TemplateArgs=*/0, |
| 4560 | /*SuppressQualifierCheck=*/true); |
| 4561 | if (OpEqualRef.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4562 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4563 | |
| 4564 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4565 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4566 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4567 | OpEqualRef.takeAs<Expr>(), |
| 4568 | Loc, &From, 1, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4569 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4570 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4571 | |
| 4572 | return S.Owned(Call.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4573 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4574 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4575 | // - if the subobject is of scalar type, the built-in assignment |
| 4576 | // operator is used. |
| 4577 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| 4578 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4579 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4580 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4581 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4582 | |
| 4583 | return S.Owned(Assignment.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4584 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4585 | |
| 4586 | // - if the subobject is an array, each element is assigned, in the |
| 4587 | // manner appropriate to the element type; |
| 4588 | |
| 4589 | // Construct a loop over the array bounds, e.g., |
| 4590 | // |
| 4591 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 4592 | // |
| 4593 | // that will copy each of the array elements. |
| 4594 | QualType SizeType = S.Context.getSizeType(); |
| 4595 | |
| 4596 | // Create the iteration variable. |
| 4597 | IdentifierInfo *IterationVarName = 0; |
| 4598 | { |
| 4599 | llvm::SmallString<8> Str; |
| 4600 | llvm::raw_svector_ostream OS(Str); |
| 4601 | OS << "__i" << Depth; |
| 4602 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 4603 | } |
| 4604 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, |
| 4605 | IterationVarName, SizeType, |
| 4606 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4607 | SC_None, SC_None); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4608 | |
| 4609 | // Initialize the iteration variable to zero. |
| 4610 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4611 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4612 | |
| 4613 | // Create a reference to the iteration variable; we'll use this several |
| 4614 | // times throughout. |
| 4615 | Expr *IterationVarRef |
| 4616 | = S.BuildDeclRefExpr(IterationVar, SizeType, Loc).takeAs<Expr>(); |
| 4617 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
| 4618 | |
| 4619 | // Create the DeclStmt that holds the iteration variable. |
| 4620 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| 4621 | |
| 4622 | // Create the comparison against the array bound. |
| 4623 | llvm::APInt Upper = ArrayTy->getSize(); |
| 4624 | Upper.zextOrTrunc(S.Context.getTypeSize(SizeType)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4625 | Expr *Comparison |
| 4626 | = new (S.Context) BinaryOperator(IterationVarRef->Retain(), |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4627 | IntegerLiteral::Create(S.Context, |
| 4628 | Upper, SizeType, Loc), |
| 4629 | BO_NE, S.Context.BoolTy, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4630 | |
| 4631 | // Create the pre-increment of the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4632 | Expr *Increment |
| 4633 | = new (S.Context) UnaryOperator(IterationVarRef->Retain(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4634 | UO_PreInc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4635 | SizeType, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4636 | |
| 4637 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4638 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
| 4639 | IterationVarRef, Loc)); |
| 4640 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
| 4641 | IterationVarRef, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4642 | |
| 4643 | // Build the copy for an individual element of the array. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4644 | StmtResult Copy = BuildSingleCopyAssign(S, Loc, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4645 | ArrayTy->getElementType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4646 | To, From, |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4647 | CopyingBaseSubobject, Depth+1); |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 4648 | if (Copy.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4649 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4650 | |
| 4651 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4652 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4653 | S.MakeFullExpr(Comparison), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4654 | 0, S.MakeFullExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4655 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4656 | } |
| 4657 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4658 | /// \brief Determine whether the given class has a copy assignment operator |
| 4659 | /// that accepts a const-qualified argument. |
| 4660 | static bool hasConstCopyAssignment(Sema &S, const CXXRecordDecl *CClass) { |
| 4661 | CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(CClass); |
| 4662 | |
| 4663 | if (!Class->hasDeclaredCopyAssignment()) |
| 4664 | S.DeclareImplicitCopyAssignment(Class); |
| 4665 | |
| 4666 | QualType ClassType = S.Context.getCanonicalType(S.Context.getTypeDeclType(Class)); |
| 4667 | DeclarationName OpName |
| 4668 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 4669 | |
| 4670 | DeclContext::lookup_const_iterator Op, OpEnd; |
| 4671 | for (llvm::tie(Op, OpEnd) = Class->lookup(OpName); Op != OpEnd; ++Op) { |
| 4672 | // C++ [class.copy]p9: |
| 4673 | // A user-declared copy assignment operator is a non-static non-template |
| 4674 | // member function of class X with exactly one parameter of type X, X&, |
| 4675 | // const X&, volatile X& or const volatile X&. |
| 4676 | const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); |
| 4677 | if (!Method) |
| 4678 | continue; |
| 4679 | |
| 4680 | if (Method->isStatic()) |
| 4681 | continue; |
| 4682 | if (Method->getPrimaryTemplate()) |
| 4683 | continue; |
| 4684 | const FunctionProtoType *FnType = |
| 4685 | Method->getType()->getAs<FunctionProtoType>(); |
| 4686 | assert(FnType && "Overloaded operator has no prototype."); |
| 4687 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 4688 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 4689 | continue; |
| 4690 | bool AcceptsConst = true; |
| 4691 | QualType ArgType = FnType->getArgType(0); |
| 4692 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()){ |
| 4693 | ArgType = Ref->getPointeeType(); |
| 4694 | // Is it a non-const lvalue reference? |
| 4695 | if (!ArgType.isConstQualified()) |
| 4696 | AcceptsConst = false; |
| 4697 | } |
| 4698 | if (!S.Context.hasSameUnqualifiedType(ArgType, ClassType)) |
| 4699 | continue; |
| 4700 | |
| 4701 | // We have a single argument of type cv X or cv X&, i.e. we've found the |
| 4702 | // copy assignment operator. Return whether it accepts const arguments. |
| 4703 | return AcceptsConst; |
| 4704 | } |
| 4705 | assert(Class->isInvalidDecl() && |
| 4706 | "No copy assignment operator declared in valid code."); |
| 4707 | return false; |
| 4708 | } |
| 4709 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4710 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4711 | // Note: The following rules are largely analoguous to the copy |
| 4712 | // constructor rules. Note that virtual bases are not taken into account |
| 4713 | // for determining the argument type of the operator. Note also that |
| 4714 | // operators taking an object instead of a reference are allowed. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4715 | |
| 4716 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4717 | // C++ [class.copy]p10: |
| 4718 | // If the class definition does not explicitly declare a copy |
| 4719 | // assignment operator, one is declared implicitly. |
| 4720 | // The implicitly-defined copy assignment operator for a class X |
| 4721 | // will have the form |
| 4722 | // |
| 4723 | // X& X::operator=(const X&) |
| 4724 | // |
| 4725 | // if |
| 4726 | bool HasConstCopyAssignment = true; |
| 4727 | |
| 4728 | // -- each direct base class B of X has a copy assignment operator |
| 4729 | // whose parameter is of type const B&, const volatile B& or B, |
| 4730 | // and |
| 4731 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4732 | BaseEnd = ClassDecl->bases_end(); |
| 4733 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
| 4734 | assert(!Base->getType()->isDependentType() && |
| 4735 | "Cannot generate implicit members for class with dependent bases."); |
| 4736 | const CXXRecordDecl *BaseClassDecl |
| 4737 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4738 | HasConstCopyAssignment = hasConstCopyAssignment(*this, BaseClassDecl); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | // -- for all the nonstatic data members of X that are of a class |
| 4742 | // type M (or array thereof), each such class type has a copy |
| 4743 | // assignment operator whose parameter is of type const M&, |
| 4744 | // const volatile M& or M. |
| 4745 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4746 | FieldEnd = ClassDecl->field_end(); |
| 4747 | HasConstCopyAssignment && Field != FieldEnd; |
| 4748 | ++Field) { |
| 4749 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 4750 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 4751 | const CXXRecordDecl *FieldClassDecl |
| 4752 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4753 | HasConstCopyAssignment = hasConstCopyAssignment(*this, FieldClassDecl); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4754 | } |
| 4755 | } |
| 4756 | |
| 4757 | // Otherwise, the implicitly declared copy assignment operator will |
| 4758 | // have the form |
| 4759 | // |
| 4760 | // X& X::operator=(X&) |
| 4761 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 4762 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 4763 | if (HasConstCopyAssignment) |
| 4764 | ArgType = ArgType.withConst(); |
| 4765 | ArgType = Context.getLValueReferenceType(ArgType); |
| 4766 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4767 | // C++ [except.spec]p14: |
| 4768 | // An implicitly declared special member function (Clause 12) shall have an |
| 4769 | // exception-specification. [...] |
| 4770 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4771 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4772 | BaseEnd = ClassDecl->bases_end(); |
| 4773 | Base != BaseEnd; ++Base) { |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4774 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4775 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4776 | |
| 4777 | if (!BaseClassDecl->hasDeclaredCopyAssignment()) |
| 4778 | DeclareImplicitCopyAssignment(BaseClassDecl); |
| 4779 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4780 | if (CXXMethodDecl *CopyAssign |
| 4781 | = BaseClassDecl->getCopyAssignmentOperator(HasConstCopyAssignment)) |
| 4782 | ExceptSpec.CalledDecl(CopyAssign); |
| 4783 | } |
| 4784 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4785 | FieldEnd = ClassDecl->field_end(); |
| 4786 | Field != FieldEnd; |
| 4787 | ++Field) { |
| 4788 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 4789 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4790 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4791 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4792 | |
| 4793 | if (!FieldClassDecl->hasDeclaredCopyAssignment()) |
| 4794 | DeclareImplicitCopyAssignment(FieldClassDecl); |
| 4795 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4796 | if (CXXMethodDecl *CopyAssign |
| 4797 | = FieldClassDecl->getCopyAssignmentOperator(HasConstCopyAssignment)) |
| 4798 | ExceptSpec.CalledDecl(CopyAssign); |
| 4799 | } |
| 4800 | } |
| 4801 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4802 | // An implicitly-declared copy assignment operator is an inline public |
| 4803 | // member of its class. |
| 4804 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4805 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4806 | CXXMethodDecl *CopyAssignment |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4807 | = CXXMethodDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4808 | Context.getFunctionType(RetType, &ArgType, 1, |
| 4809 | false, 0, |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4810 | ExceptSpec.hasExceptionSpecification(), |
| 4811 | ExceptSpec.hasAnyExceptionSpecification(), |
| 4812 | ExceptSpec.size(), |
| 4813 | ExceptSpec.data(), |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4814 | FunctionType::ExtInfo()), |
| 4815 | /*TInfo=*/0, /*isStatic=*/false, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4816 | /*StorageClassAsWritten=*/SC_None, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4817 | /*isInline=*/true); |
| 4818 | CopyAssignment->setAccess(AS_public); |
| 4819 | CopyAssignment->setImplicit(); |
| 4820 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4821 | |
| 4822 | // Add the parameter to the operator. |
| 4823 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 4824 | ClassDecl->getLocation(), |
| 4825 | /*Id=*/0, |
| 4826 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4827 | SC_None, |
| 4828 | SC_None, 0); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4829 | CopyAssignment->setParams(&FromParam, 1); |
| 4830 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4831 | // Note that we have added this copy-assignment operator. |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4832 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
| 4833 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4834 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4835 | PushOnScopeChains(CopyAssignment, S, false); |
| 4836 | ClassDecl->addDecl(CopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4837 | |
| 4838 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 4839 | return CopyAssignment; |
| 4840 | } |
| 4841 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4842 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 4843 | CXXMethodDecl *CopyAssignOperator) { |
| 4844 | assert((CopyAssignOperator->isImplicit() && |
| 4845 | CopyAssignOperator->isOverloadedOperator() && |
| 4846 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4847 | !CopyAssignOperator->isUsed(false)) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4848 | "DefineImplicitCopyAssignment called for wrong function"); |
| 4849 | |
| 4850 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 4851 | |
| 4852 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 4853 | CopyAssignOperator->setInvalidDecl(); |
| 4854 | return; |
| 4855 | } |
| 4856 | |
| 4857 | CopyAssignOperator->setUsed(); |
| 4858 | |
| 4859 | ImplicitlyDefinedFunctionScope Scope(*this, CopyAssignOperator); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4860 | ErrorTrap Trap(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4861 | |
| 4862 | // C++0x [class.copy]p30: |
| 4863 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 4864 | // for a non-union class X performs memberwise copy assignment of its |
| 4865 | // subobjects. The direct base classes of X are assigned first, in the |
| 4866 | // order of their declaration in the base-specifier-list, and then the |
| 4867 | // immediate non-static data members of X are assigned, in the order in |
| 4868 | // which they were declared in the class definition. |
| 4869 | |
| 4870 | // The statements that form the synthesized function body. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4871 | ASTOwningVector<Stmt*> Statements(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4872 | |
| 4873 | // The parameter for the "other" object, which we are copying from. |
| 4874 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 4875 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 4876 | QualType OtherRefType = Other->getType(); |
| 4877 | if (const LValueReferenceType *OtherRef |
| 4878 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 4879 | OtherRefType = OtherRef->getPointeeType(); |
| 4880 | OtherQuals = OtherRefType.getQualifiers(); |
| 4881 | } |
| 4882 | |
| 4883 | // Our location for everything implicitly-generated. |
| 4884 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 4885 | |
| 4886 | // Construct a reference to the "other" object. We'll be using this |
| 4887 | // throughout the generated ASTs. |
| 4888 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, Loc).takeAs<Expr>(); |
| 4889 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 4890 | |
| 4891 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 4892 | // ASTs. |
| 4893 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 4894 | assert(This && "Reference to this cannot fail!"); |
| 4895 | |
| 4896 | // Assign base classes. |
| 4897 | bool Invalid = false; |
| 4898 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4899 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 4900 | // Form the assignment: |
| 4901 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 4902 | QualType BaseType = Base->getType().getUnqualifiedType(); |
| 4903 | CXXRecordDecl *BaseClassDecl = 0; |
| 4904 | if (const RecordType *BaseRecordT = BaseType->getAs<RecordType>()) |
| 4905 | BaseClassDecl = cast<CXXRecordDecl>(BaseRecordT->getDecl()); |
| 4906 | else { |
| 4907 | Invalid = true; |
| 4908 | continue; |
| 4909 | } |
| 4910 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4911 | CXXCastPath BasePath; |
| 4912 | BasePath.push_back(Base); |
| 4913 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4914 | // Construct the "from" expression, which is an implicit cast to the |
| 4915 | // appropriately-qualified base type. |
| 4916 | Expr *From = OtherRef->Retain(); |
| 4917 | ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4918 | CK_UncheckedDerivedToBase, |
| 4919 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4920 | |
| 4921 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4922 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4923 | |
| 4924 | // Implicitly cast "this" to the appropriately-qualified base type. |
| 4925 | Expr *ToE = To.takeAs<Expr>(); |
| 4926 | ImpCastExprToType(ToE, |
| 4927 | Context.getCVRQualifiedType(BaseType, |
| 4928 | CopyAssignOperator->getTypeQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4929 | CK_UncheckedDerivedToBase, |
| 4930 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4931 | To = Owned(ToE); |
| 4932 | |
| 4933 | // Build the copy. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4934 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4935 | To.get(), From, |
| 4936 | /*CopyingBaseSubobject=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4937 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 4938 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4939 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 4940 | CopyAssignOperator->setInvalidDecl(); |
| 4941 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
| 4944 | // Success! Record the copy. |
| 4945 | Statements.push_back(Copy.takeAs<Expr>()); |
| 4946 | } |
| 4947 | |
| 4948 | // \brief Reference to the __builtin_memcpy function. |
| 4949 | Expr *BuiltinMemCpyRef = 0; |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 4950 | // \brief Reference to the __builtin_objc_memmove_collectable function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4951 | Expr *CollectableMemCpyRef = 0; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4952 | |
| 4953 | // Assign non-static members. |
| 4954 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4955 | FieldEnd = ClassDecl->field_end(); |
| 4956 | Field != FieldEnd; ++Field) { |
| 4957 | // Check for members of reference type; we can't copy those. |
| 4958 | if (Field->getType()->isReferenceType()) { |
| 4959 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 4960 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 4961 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 4962 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4963 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4964 | Invalid = true; |
| 4965 | continue; |
| 4966 | } |
| 4967 | |
| 4968 | // Check for members of const-qualified, non-class type. |
| 4969 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 4970 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 4971 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 4972 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 4973 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 4974 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4975 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4976 | Invalid = true; |
| 4977 | continue; |
| 4978 | } |
| 4979 | |
| 4980 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 4981 | if (FieldType->isIncompleteArrayType()) { |
| 4982 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 4983 | "Incomplete array type is not valid"); |
| 4984 | continue; |
| 4985 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4986 | |
| 4987 | // Build references to the field in the object we're copying from and to. |
| 4988 | CXXScopeSpec SS; // Intentionally empty |
| 4989 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 4990 | LookupMemberName); |
| 4991 | MemberLookup.addDecl(*Field); |
| 4992 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4993 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4994 | Loc, /*IsArrow=*/false, |
| 4995 | SS, 0, MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4996 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4997 | Loc, /*IsArrow=*/true, |
| 4998 | SS, 0, MemberLookup, 0); |
| 4999 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 5000 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 5001 | |
| 5002 | // If the field should be copied with __builtin_memcpy rather than via |
| 5003 | // explicit assignments, do so. This optimization only applies for arrays |
| 5004 | // of scalars and arrays of class type with trivial copy-assignment |
| 5005 | // operators. |
| 5006 | if (FieldType->isArrayType() && |
| 5007 | (!BaseType->isRecordType() || |
| 5008 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()) |
| 5009 | ->hasTrivialCopyAssignment())) { |
| 5010 | // Compute the size of the memory buffer to be copied. |
| 5011 | QualType SizeType = Context.getSizeType(); |
| 5012 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 5013 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 5014 | for (const ConstantArrayType *Array |
| 5015 | = Context.getAsConstantArrayType(FieldType); |
| 5016 | Array; |
| 5017 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
| 5018 | llvm::APInt ArraySize = Array->getSize(); |
| 5019 | ArraySize.zextOrTrunc(Size.getBitWidth()); |
| 5020 | Size *= ArraySize; |
| 5021 | } |
| 5022 | |
| 5023 | // Take the address of the field references for "from" and "to". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5024 | From = CreateBuiltinUnaryOp(Loc, UO_AddrOf, From.get()); |
| 5025 | To = CreateBuiltinUnaryOp(Loc, UO_AddrOf, To.get()); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5026 | |
| 5027 | bool NeedsCollectableMemCpy = |
| 5028 | (BaseType->isRecordType() && |
| 5029 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()); |
| 5030 | |
| 5031 | if (NeedsCollectableMemCpy) { |
| 5032 | if (!CollectableMemCpyRef) { |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 5033 | // Create a reference to the __builtin_objc_memmove_collectable function. |
| 5034 | LookupResult R(*this, |
| 5035 | &Context.Idents.get("__builtin_objc_memmove_collectable"), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5036 | Loc, LookupOrdinaryName); |
| 5037 | LookupName(R, TUScope, true); |
| 5038 | |
| 5039 | FunctionDecl *CollectableMemCpy = R.getAsSingle<FunctionDecl>(); |
| 5040 | if (!CollectableMemCpy) { |
| 5041 | // Something went horribly wrong earlier, and we will have |
| 5042 | // complained about it. |
| 5043 | Invalid = true; |
| 5044 | continue; |
| 5045 | } |
| 5046 | |
| 5047 | CollectableMemCpyRef = BuildDeclRefExpr(CollectableMemCpy, |
| 5048 | CollectableMemCpy->getType(), |
| 5049 | Loc, 0).takeAs<Expr>(); |
| 5050 | assert(CollectableMemCpyRef && "Builtin reference cannot fail"); |
| 5051 | } |
| 5052 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5053 | // Create a reference to the __builtin_memcpy builtin function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5054 | else if (!BuiltinMemCpyRef) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5055 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 5056 | LookupOrdinaryName); |
| 5057 | LookupName(R, TUScope, true); |
| 5058 | |
| 5059 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 5060 | if (!BuiltinMemCpy) { |
| 5061 | // Something went horribly wrong earlier, and we will have complained |
| 5062 | // about it. |
| 5063 | Invalid = true; |
| 5064 | continue; |
| 5065 | } |
| 5066 | |
| 5067 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 5068 | BuiltinMemCpy->getType(), |
| 5069 | Loc, 0).takeAs<Expr>(); |
| 5070 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 5071 | } |
| 5072 | |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5073 | ASTOwningVector<Expr*> CallArgs(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5074 | CallArgs.push_back(To.takeAs<Expr>()); |
| 5075 | CallArgs.push_back(From.takeAs<Expr>()); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5076 | CallArgs.push_back(IntegerLiteral::Create(Context, Size, SizeType, Loc)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5077 | ExprResult Call = ExprError(); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5078 | if (NeedsCollectableMemCpy) |
| 5079 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5080 | CollectableMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5081 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5082 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5083 | else |
| 5084 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5085 | BuiltinMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5086 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5087 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5088 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5089 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 5090 | Statements.push_back(Call.takeAs<Expr>()); |
| 5091 | continue; |
| 5092 | } |
| 5093 | |
| 5094 | // Build the copy of this field. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5095 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, FieldType, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5096 | To.get(), From.get(), |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 5097 | /*CopyingBaseSubobject=*/false); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5098 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 5099 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5100 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5101 | CopyAssignOperator->setInvalidDecl(); |
| 5102 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5103 | } |
| 5104 | |
| 5105 | // Success! Record the copy. |
| 5106 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 5107 | } |
| 5108 | |
| 5109 | if (!Invalid) { |
| 5110 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5111 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5112 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5113 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5114 | if (Return.isInvalid()) |
| 5115 | Invalid = true; |
| 5116 | else { |
| 5117 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5118 | |
| 5119 | if (Trap.hasErrorOccurred()) { |
| 5120 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5121 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5122 | Invalid = true; |
| 5123 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5124 | } |
| 5125 | } |
| 5126 | |
| 5127 | if (Invalid) { |
| 5128 | CopyAssignOperator->setInvalidDecl(); |
| 5129 | return; |
| 5130 | } |
| 5131 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5132 | StmtResult Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5133 | /*isStmtExpr=*/false); |
| 5134 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 5135 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5136 | } |
| 5137 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5138 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 5139 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5140 | // C++ [class.copy]p4: |
| 5141 | // If the class definition does not explicitly declare a copy |
| 5142 | // constructor, one is declared implicitly. |
| 5143 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5144 | // C++ [class.copy]p5: |
| 5145 | // The implicitly-declared copy constructor for a class X will |
| 5146 | // have the form |
| 5147 | // |
| 5148 | // X::X(const X&) |
| 5149 | // |
| 5150 | // if |
| 5151 | bool HasConstCopyConstructor = true; |
| 5152 | |
| 5153 | // -- each direct or virtual base class B of X has a copy |
| 5154 | // constructor whose first parameter is of type const B& or |
| 5155 | // const volatile B&, and |
| 5156 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5157 | BaseEnd = ClassDecl->bases_end(); |
| 5158 | HasConstCopyConstructor && Base != BaseEnd; |
| 5159 | ++Base) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5160 | // Virtual bases are handled below. |
| 5161 | if (Base->isVirtual()) |
| 5162 | continue; |
| 5163 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5164 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5165 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5166 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5167 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5168 | |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5169 | HasConstCopyConstructor |
| 5170 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 5171 | } |
| 5172 | |
| 5173 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 5174 | BaseEnd = ClassDecl->vbases_end(); |
| 5175 | HasConstCopyConstructor && Base != BaseEnd; |
| 5176 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5177 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5178 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5179 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5180 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5181 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5182 | HasConstCopyConstructor |
| 5183 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 5184 | } |
| 5185 | |
| 5186 | // -- for all the nonstatic data members of X that are of a |
| 5187 | // class type M (or array thereof), each such class type |
| 5188 | // has a copy constructor whose first parameter is of type |
| 5189 | // const M& or const volatile M&. |
| 5190 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5191 | FieldEnd = ClassDecl->field_end(); |
| 5192 | HasConstCopyConstructor && Field != FieldEnd; |
| 5193 | ++Field) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5194 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5195 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5196 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5197 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5198 | if (!FieldClassDecl->hasDeclaredCopyConstructor()) |
| 5199 | DeclareImplicitCopyConstructor(FieldClassDecl); |
| 5200 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5201 | HasConstCopyConstructor |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5202 | = FieldClassDecl->hasConstCopyConstructor(Context); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5203 | } |
| 5204 | } |
| 5205 | |
| 5206 | // Otherwise, the implicitly declared copy constructor will have |
| 5207 | // the form |
| 5208 | // |
| 5209 | // X::X(X&) |
| 5210 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 5211 | QualType ArgType = ClassType; |
| 5212 | if (HasConstCopyConstructor) |
| 5213 | ArgType = ArgType.withConst(); |
| 5214 | ArgType = Context.getLValueReferenceType(ArgType); |
| 5215 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5216 | // C++ [except.spec]p14: |
| 5217 | // An implicitly declared special member function (Clause 12) shall have an |
| 5218 | // exception-specification. [...] |
| 5219 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 5220 | unsigned Quals = HasConstCopyConstructor? Qualifiers::Const : 0; |
| 5221 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5222 | BaseEnd = ClassDecl->bases_end(); |
| 5223 | Base != BaseEnd; |
| 5224 | ++Base) { |
| 5225 | // Virtual bases are handled below. |
| 5226 | if (Base->isVirtual()) |
| 5227 | continue; |
| 5228 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5229 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5230 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5231 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5232 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5233 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5234 | if (CXXConstructorDecl *CopyConstructor |
| 5235 | = BaseClassDecl->getCopyConstructor(Context, Quals)) |
| 5236 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5237 | } |
| 5238 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 5239 | BaseEnd = ClassDecl->vbases_end(); |
| 5240 | Base != BaseEnd; |
| 5241 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5242 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5243 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5244 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5245 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5246 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5247 | if (CXXConstructorDecl *CopyConstructor |
| 5248 | = BaseClassDecl->getCopyConstructor(Context, Quals)) |
| 5249 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5250 | } |
| 5251 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5252 | FieldEnd = ClassDecl->field_end(); |
| 5253 | Field != FieldEnd; |
| 5254 | ++Field) { |
| 5255 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 5256 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5257 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5258 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5259 | if (!FieldClassDecl->hasDeclaredCopyConstructor()) |
| 5260 | DeclareImplicitCopyConstructor(FieldClassDecl); |
| 5261 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5262 | if (CXXConstructorDecl *CopyConstructor |
| 5263 | = FieldClassDecl->getCopyConstructor(Context, Quals)) |
| 5264 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5265 | } |
| 5266 | } |
| 5267 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5268 | // An implicitly-declared copy constructor is an inline public |
| 5269 | // member of its class. |
| 5270 | DeclarationName Name |
| 5271 | = Context.DeclarationNames.getCXXConstructorName( |
| 5272 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5273 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5274 | CXXConstructorDecl *CopyConstructor |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5275 | = CXXConstructorDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5276 | Context.getFunctionType(Context.VoidTy, |
| 5277 | &ArgType, 1, |
| 5278 | false, 0, |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5279 | ExceptSpec.hasExceptionSpecification(), |
| 5280 | ExceptSpec.hasAnyExceptionSpecification(), |
| 5281 | ExceptSpec.size(), |
| 5282 | ExceptSpec.data(), |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5283 | FunctionType::ExtInfo()), |
| 5284 | /*TInfo=*/0, |
| 5285 | /*isExplicit=*/false, |
| 5286 | /*isInline=*/true, |
| 5287 | /*isImplicitlyDeclared=*/true); |
| 5288 | CopyConstructor->setAccess(AS_public); |
| 5289 | CopyConstructor->setImplicit(); |
| 5290 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
| 5291 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5292 | // Note that we have declared this constructor. |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5293 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 5294 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5295 | // Add the parameter to the constructor. |
| 5296 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 5297 | ClassDecl->getLocation(), |
| 5298 | /*IdentifierInfo=*/0, |
| 5299 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5300 | SC_None, |
| 5301 | SC_None, 0); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5302 | CopyConstructor->setParams(&FromParam, 1); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5303 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5304 | PushOnScopeChains(CopyConstructor, S, false); |
| 5305 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5306 | |
| 5307 | return CopyConstructor; |
| 5308 | } |
| 5309 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5310 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 5311 | CXXConstructorDecl *CopyConstructor, |
| 5312 | unsigned TypeQuals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5313 | assert((CopyConstructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 5314 | CopyConstructor->isCopyConstructor(TypeQuals) && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 5315 | !CopyConstructor->isUsed(false)) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5316 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5317 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 5318 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5319 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5320 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5321 | ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5322 | ErrorTrap Trap(*this); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5323 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5324 | if (SetBaseOrMemberInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) || |
| 5325 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 5326 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5327 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 5328 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5329 | } else { |
| 5330 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 5331 | CopyConstructor->getLocation(), |
| 5332 | MultiStmtArg(*this, 0, 0), |
| 5333 | /*isStmtExpr=*/false) |
| 5334 | .takeAs<Stmt>()); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 5335 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5336 | |
| 5337 | CopyConstructor->setUsed(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5338 | } |
| 5339 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5340 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 5341 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5342 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5343 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5344 | bool RequiresZeroInit, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5345 | unsigned ConstructKind) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5346 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5347 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5348 | // C++0x [class.copy]p34: |
| 5349 | // When certain criteria are met, an implementation is allowed to |
| 5350 | // omit the copy/move construction of a class object, even if the |
| 5351 | // copy/move constructor and/or destructor for the object have |
| 5352 | // side effects. [...] |
| 5353 | // - when a temporary class object that has not been bound to a |
| 5354 | // reference (12.2) would be copied/moved to a class object |
| 5355 | // with the same cv-unqualified type, the copy/move operation |
| 5356 | // can be omitted by constructing the temporary object |
| 5357 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5358 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
| 5359 | Constructor->isCopyConstructor() && ExprArgs.size() >= 1) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5360 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5361 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5363 | |
| 5364 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5365 | Elidable, move(ExprArgs), RequiresZeroInit, |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 5366 | ConstructKind); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5367 | } |
| 5368 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5369 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 5370 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5371 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 5372 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 5373 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5374 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5375 | bool RequiresZeroInit, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5376 | unsigned ConstructKind) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 5377 | unsigned NumExprs = ExprArgs.size(); |
| 5378 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5379 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5380 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5381 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5382 | Constructor, Elidable, Exprs, NumExprs, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5383 | RequiresZeroInit, |
| 5384 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind))); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5385 | } |
| 5386 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5387 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5388 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 5389 | MultiExprArg Exprs) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5390 | ExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 5391 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5392 | move(Exprs), false, CXXConstructExpr::CK_Complete); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 5393 | if (TempResult.isInvalid()) |
| 5394 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5395 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 5396 | Expr *Temp = TempResult.takeAs<Expr>(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5397 | CheckImplicitConversions(Temp, VD->getLocation()); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5398 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 5399 | Temp = MaybeCreateCXXExprWithTemporaries(Temp); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5400 | VD->setInit(Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5401 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 5402 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 5403 | } |
| 5404 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 5405 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
| 5406 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Douglas Gregor | 333de06 | 2010-02-25 18:11:54 +0000 | [diff] [blame] | 5407 | if (!ClassDecl->isInvalidDecl() && !VD->isInvalidDecl() && |
Douglas Gregor | fb2db46 | 2010-05-22 17:12:29 +0000 | [diff] [blame] | 5408 | !ClassDecl->hasTrivialDestructor() && !ClassDecl->isDependentContext()) { |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5409 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 5410 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5411 | CheckDestructorAccess(VD->getLocation(), Destructor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5412 | PDiag(diag::err_access_dtor_var) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5413 | << VD->getDeclName() |
| 5414 | << VD->getType()); |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 5415 | |
John McCall | ae79222 | 2010-09-18 05:25:11 +0000 | [diff] [blame] | 5416 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 5417 | if (!VD->isInvalidDecl() && VD->hasGlobalStorage() && !VD->isStaticLocal()) |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 5418 | Diag(VD->getLocation(), diag::warn_global_destructor); |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 5419 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5422 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5423 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 5424 | /// e.g: "int x(1);" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5425 | void Sema::AddCXXDirectInitializerToDecl(Decl *RealDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5426 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5427 | MultiExprArg Exprs, |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5428 | SourceLocation RParenLoc) { |
Daniel Dunbar | 5184626 | 2009-12-24 19:19:26 +0000 | [diff] [blame] | 5429 | assert(Exprs.size() != 0 && Exprs.get() && "missing expressions"); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5430 | |
| 5431 | // If there is no declaration, there was an error parsing it. Just ignore |
| 5432 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5433 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5434 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5435 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5436 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 5437 | if (!VDecl) { |
| 5438 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 5439 | RealDecl->setInvalidDecl(); |
| 5440 | return; |
| 5441 | } |
| 5442 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 5443 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 5444 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5445 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 5446 | // |
| 5447 | // Clients that want to distinguish between the two forms, can check for |
| 5448 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 5449 | // A major benefit is that clients that don't particularly care about which |
| 5450 | // exactly form was it (like the CodeGen) can handle both cases without |
| 5451 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 5452 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5453 | // C++ 8.5p11: |
| 5454 | // The form of initialization (using parentheses or '=') is generally |
| 5455 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 5456 | // class type. |
| 5457 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 5458 | if (!VDecl->getType()->isDependentType() && |
| 5459 | RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 5460 | diag::err_typecheck_decl_incomplete_type)) { |
| 5461 | VDecl->setInvalidDecl(); |
| 5462 | return; |
| 5463 | } |
| 5464 | |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5465 | // The variable can not have an abstract class type. |
| 5466 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 5467 | diag::err_abstract_type_in_decl, |
| 5468 | AbstractVariableType)) |
| 5469 | VDecl->setInvalidDecl(); |
| 5470 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 5471 | const VarDecl *Def; |
| 5472 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5473 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| 5474 | << VDecl->getDeclName(); |
| 5475 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5476 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 5477 | return; |
| 5478 | } |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 5479 | |
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 5480 | // C++ [class.static.data]p4 |
| 5481 | // If a static data member is of const integral or const |
| 5482 | // enumeration type, its declaration in the class definition can |
| 5483 | // specify a constant-initializer which shall be an integral |
| 5484 | // constant expression (5.19). In that case, the member can appear |
| 5485 | // in integral constant expressions. The member shall still be |
| 5486 | // defined in a namespace scope if it is used in the program and the |
| 5487 | // namespace scope definition shall not contain an initializer. |
| 5488 | // |
| 5489 | // We already performed a redefinition check above, but for static |
| 5490 | // data members we also need to check whether there was an in-class |
| 5491 | // declaration with an initializer. |
| 5492 | const VarDecl* PrevInit = 0; |
| 5493 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { |
| 5494 | Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName(); |
| 5495 | Diag(PrevInit->getLocation(), diag::note_previous_definition); |
| 5496 | return; |
| 5497 | } |
| 5498 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 5499 | // If either the declaration has a dependent type or if any of the |
| 5500 | // expressions is type-dependent, we represent the initialization |
| 5501 | // via a ParenListExpr for later use during template instantiation. |
| 5502 | if (VDecl->getType()->isDependentType() || |
| 5503 | Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { |
| 5504 | // Let clients know that initialization was done with a direct initializer. |
| 5505 | VDecl->setCXXDirectInitializer(true); |
| 5506 | |
| 5507 | // Store the initialization expressions as a ParenListExpr. |
| 5508 | unsigned NumExprs = Exprs.size(); |
| 5509 | VDecl->setInit(new (Context) ParenListExpr(Context, LParenLoc, |
| 5510 | (Expr **)Exprs.release(), |
| 5511 | NumExprs, RParenLoc)); |
| 5512 | return; |
| 5513 | } |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5514 | |
| 5515 | // Capture the variable that is being initialized and the style of |
| 5516 | // initialization. |
| 5517 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 5518 | |
| 5519 | // FIXME: Poor source location information. |
| 5520 | InitializationKind Kind |
| 5521 | = InitializationKind::CreateDirect(VDecl->getLocation(), |
| 5522 | LParenLoc, RParenLoc); |
| 5523 | |
| 5524 | InitializationSequence InitSeq(*this, Entity, Kind, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5525 | Exprs.get(), Exprs.size()); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5526 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(Exprs)); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5527 | if (Result.isInvalid()) { |
| 5528 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5529 | return; |
| 5530 | } |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5531 | |
| 5532 | CheckImplicitConversions(Result.get(), LParenLoc); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5533 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5534 | Result = MaybeCreateCXXExprWithTemporaries(Result.get()); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5535 | VDecl->setInit(Result.takeAs<Expr>()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5536 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 5537 | |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 5538 | if (!VDecl->isInvalidDecl() && |
| 5539 | !VDecl->getDeclContext()->isDependentContext() && |
Sebastian Redl | 36281c6 | 2010-09-08 04:46:19 +0000 | [diff] [blame] | 5540 | VDecl->hasGlobalStorage() && !VDecl->isStaticLocal() && |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 5541 | !VDecl->getInit()->isConstantInitializer(Context, |
| 5542 | VDecl->getType()->isReferenceType())) |
| 5543 | Diag(VDecl->getLocation(), diag::warn_global_constructor) |
| 5544 | << VDecl->getInit()->getSourceRange(); |
| 5545 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 5546 | if (const RecordType *Record = VDecl->getType()->getAs<RecordType>()) |
| 5547 | FinalizeVarWithDestructor(VDecl, Record); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5548 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 5549 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5550 | /// \brief Given a constructor and the set of arguments provided for the |
| 5551 | /// constructor, convert the arguments and add any required default arguments |
| 5552 | /// to form a proper call to this constructor. |
| 5553 | /// |
| 5554 | /// \returns true if an error occurred, false otherwise. |
| 5555 | bool |
| 5556 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 5557 | MultiExprArg ArgsPtr, |
| 5558 | SourceLocation Loc, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5559 | ASTOwningVector<Expr*> &ConvertedArgs) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5560 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 5561 | unsigned NumArgs = ArgsPtr.size(); |
| 5562 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 5563 | |
| 5564 | const FunctionProtoType *Proto |
| 5565 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 5566 | assert(Proto && "Constructor without a prototype?"); |
| 5567 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5568 | |
| 5569 | // 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] | 5570 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5571 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 5572 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5573 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 5574 | |
| 5575 | VariadicCallType CallType = |
| 5576 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 5577 | llvm::SmallVector<Expr *, 8> AllArgs; |
| 5578 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 5579 | Proto, 0, Args, NumArgs, AllArgs, |
| 5580 | CallType); |
| 5581 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 5582 | ConvertedArgs.push_back(AllArgs[i]); |
| 5583 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5586 | static inline bool |
| 5587 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 5588 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5589 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5590 | if (isa<NamespaceDecl>(DC)) { |
| 5591 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5592 | diag::err_operator_new_delete_declared_in_namespace) |
| 5593 | << FnDecl->getDeclName(); |
| 5594 | } |
| 5595 | |
| 5596 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5597 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5598 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5599 | diag::err_operator_new_delete_declared_static) |
| 5600 | << FnDecl->getDeclName(); |
| 5601 | } |
| 5602 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 5603 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5604 | } |
| 5605 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5606 | static inline bool |
| 5607 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 5608 | CanQualType ExpectedResultType, |
| 5609 | CanQualType ExpectedFirstParamType, |
| 5610 | unsigned DependentParamTypeDiag, |
| 5611 | unsigned InvalidParamTypeDiag) { |
| 5612 | QualType ResultType = |
| 5613 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5614 | |
| 5615 | // Check that the result type is not dependent. |
| 5616 | if (ResultType->isDependentType()) |
| 5617 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5618 | diag::err_operator_new_delete_dependent_result_type) |
| 5619 | << FnDecl->getDeclName() << ExpectedResultType; |
| 5620 | |
| 5621 | // Check that the result type is what we expect. |
| 5622 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 5623 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5624 | diag::err_operator_new_delete_invalid_result_type) |
| 5625 | << FnDecl->getDeclName() << ExpectedResultType; |
| 5626 | |
| 5627 | // A function template must have at least 2 parameters. |
| 5628 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 5629 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5630 | diag::err_operator_new_delete_template_too_few_parameters) |
| 5631 | << FnDecl->getDeclName(); |
| 5632 | |
| 5633 | // The function decl must have at least 1 parameter. |
| 5634 | if (FnDecl->getNumParams() == 0) |
| 5635 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5636 | diag::err_operator_new_delete_too_few_parameters) |
| 5637 | << FnDecl->getDeclName(); |
| 5638 | |
| 5639 | // Check the the first parameter type is not dependent. |
| 5640 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 5641 | if (FirstParamType->isDependentType()) |
| 5642 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 5643 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 5644 | |
| 5645 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 5646 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5647 | ExpectedFirstParamType) |
| 5648 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 5649 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 5650 | |
| 5651 | return false; |
| 5652 | } |
| 5653 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5654 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5655 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5656 | // C++ [basic.stc.dynamic.allocation]p1: |
| 5657 | // A program is ill-formed if an allocation function is declared in a |
| 5658 | // namespace scope other than global scope or declared static in global |
| 5659 | // scope. |
| 5660 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 5661 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5662 | |
| 5663 | CanQualType SizeTy = |
| 5664 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 5665 | |
| 5666 | // C++ [basic.stc.dynamic.allocation]p1: |
| 5667 | // The return type shall be void*. The first parameter shall have type |
| 5668 | // std::size_t. |
| 5669 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 5670 | SizeTy, |
| 5671 | diag::err_operator_new_dependent_param_type, |
| 5672 | diag::err_operator_new_param_type)) |
| 5673 | return true; |
| 5674 | |
| 5675 | // C++ [basic.stc.dynamic.allocation]p1: |
| 5676 | // The first parameter shall not have an associated default argument. |
| 5677 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5678 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5679 | diag::err_operator_new_default_arg) |
| 5680 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 5681 | |
| 5682 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5683 | } |
| 5684 | |
| 5685 | static bool |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5686 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 5687 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 5688 | // A program is ill-formed if deallocation functions are declared in a |
| 5689 | // namespace scope other than global scope or declared static in global |
| 5690 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5691 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 5692 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5693 | |
| 5694 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 5695 | // Each deallocation function shall return void and its first parameter |
| 5696 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5697 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 5698 | SemaRef.Context.VoidPtrTy, |
| 5699 | diag::err_operator_delete_dependent_param_type, |
| 5700 | diag::err_operator_delete_param_type)) |
| 5701 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5702 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5703 | return false; |
| 5704 | } |
| 5705 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5706 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 5707 | /// of this overloaded operator is well-formed. If so, returns false; |
| 5708 | /// otherwise, emits appropriate diagnostics and returns true. |
| 5709 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5710 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5711 | "Expected an overloaded operator declaration"); |
| 5712 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5713 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 5714 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5715 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5716 | // The allocation and deallocation functions, operator new, |
| 5717 | // operator new[], operator delete and operator delete[], are |
| 5718 | // described completely in 3.7.3. The attributes and restrictions |
| 5719 | // found in the rest of this subclause do not apply to them unless |
| 5720 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 5721 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5722 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 5723 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5724 | if (Op == OO_New || Op == OO_Array_New) |
| 5725 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5726 | |
| 5727 | // C++ [over.oper]p6: |
| 5728 | // An operator function shall either be a non-static member |
| 5729 | // function or be a non-member function and have at least one |
| 5730 | // parameter whose type is a class, a reference to a class, an |
| 5731 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5732 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 5733 | if (MethodDecl->isStatic()) |
| 5734 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5735 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5736 | } else { |
| 5737 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5738 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 5739 | ParamEnd = FnDecl->param_end(); |
| 5740 | Param != ParamEnd; ++Param) { |
| 5741 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 5742 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 5743 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5744 | ClassOrEnumParam = true; |
| 5745 | break; |
| 5746 | } |
| 5747 | } |
| 5748 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5749 | if (!ClassOrEnumParam) |
| 5750 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5751 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5752 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5753 | } |
| 5754 | |
| 5755 | // C++ [over.oper]p8: |
| 5756 | // An operator function cannot have default arguments (8.3.6), |
| 5757 | // except where explicitly stated below. |
| 5758 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5759 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5760 | // (C++ [over.call]p1). |
| 5761 | if (Op != OO_Call) { |
| 5762 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 5763 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5764 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5765 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5766 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5767 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5768 | } |
| 5769 | } |
| 5770 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5771 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 5772 | { false, false, false } |
| 5773 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 5774 | , { Unary, Binary, MemberOnly } |
| 5775 | #include "clang/Basic/OperatorKinds.def" |
| 5776 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5777 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5778 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 5779 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 5780 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5781 | |
| 5782 | // C++ [over.oper]p8: |
| 5783 | // [...] Operator functions cannot have more or fewer parameters |
| 5784 | // than the number required for the corresponding operator, as |
| 5785 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5786 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5787 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5788 | if (Op != OO_Call && |
| 5789 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 5790 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 5791 | (NumParams < 1) || (NumParams > 2))) { |
| 5792 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5793 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5794 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5795 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5796 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5797 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5798 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 5799 | assert(CanBeBinaryOperator && |
| 5800 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5801 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5802 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5803 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5804 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5805 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5806 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5807 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5808 | // Overloaded operators other than operator() cannot be variadic. |
| 5809 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5810 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5811 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5812 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5813 | } |
| 5814 | |
| 5815 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5816 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 5817 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5818 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5819 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5820 | } |
| 5821 | |
| 5822 | // C++ [over.inc]p1: |
| 5823 | // The user-defined function called operator++ implements the |
| 5824 | // prefix and postfix ++ operator. If this function is a member |
| 5825 | // function with no parameters, or a non-member function with one |
| 5826 | // parameter of class or enumeration type, it defines the prefix |
| 5827 | // increment operator ++ for objects of that type. If the function |
| 5828 | // is a member function with one parameter (which shall be of type |
| 5829 | // int) or a non-member function with two parameters (the second |
| 5830 | // of which shall be of type int), it defines the postfix |
| 5831 | // increment operator ++ for objects of that type. |
| 5832 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 5833 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 5834 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5835 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5836 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 5837 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 5838 | if (!ParamIsInt) |
| 5839 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5840 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5841 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5842 | } |
| 5843 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5844 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5845 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 5846 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5847 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 5848 | /// of this literal operator function is well-formed. If so, returns |
| 5849 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 5850 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 5851 | DeclContext *DC = FnDecl->getDeclContext(); |
| 5852 | Decl::Kind Kind = DC->getDeclKind(); |
| 5853 | if (Kind != Decl::TranslationUnit && Kind != Decl::Namespace && |
| 5854 | Kind != Decl::LinkageSpec) { |
| 5855 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 5856 | << FnDecl->getDeclName(); |
| 5857 | return true; |
| 5858 | } |
| 5859 | |
| 5860 | bool Valid = false; |
| 5861 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5862 | // template <char...> type operator "" name() is the only valid template |
| 5863 | // signature, and the only valid signature with no parameters. |
| 5864 | if (FnDecl->param_size() == 0) { |
| 5865 | if (FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate()) { |
| 5866 | // Must have only one template parameter |
| 5867 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 5868 | if (Params->size() == 1) { |
| 5869 | NonTypeTemplateParmDecl *PmDecl = |
| 5870 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5871 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5872 | // The template parameter must be a char parameter pack. |
| 5873 | // FIXME: This test will always fail because non-type parameter packs |
| 5874 | // have not been implemented. |
| 5875 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 5876 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 5877 | Valid = true; |
| 5878 | } |
| 5879 | } |
| 5880 | } else { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5881 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5882 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 5883 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5884 | QualType T = (*Param)->getType(); |
| 5885 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 5886 | // unsigned long long int, long double, and any character type are allowed |
| 5887 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5888 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 5889 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 5890 | Context.hasSameType(T, Context.CharTy) || |
| 5891 | Context.hasSameType(T, Context.WCharTy) || |
| 5892 | Context.hasSameType(T, Context.Char16Ty) || |
| 5893 | Context.hasSameType(T, Context.Char32Ty)) { |
| 5894 | if (++Param == FnDecl->param_end()) |
| 5895 | Valid = true; |
| 5896 | goto FinishedParams; |
| 5897 | } |
| 5898 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 5899 | // 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] | 5900 | const PointerType *PT = T->getAs<PointerType>(); |
| 5901 | if (!PT) |
| 5902 | goto FinishedParams; |
| 5903 | T = PT->getPointeeType(); |
| 5904 | if (!T.isConstQualified()) |
| 5905 | goto FinishedParams; |
| 5906 | T = T.getUnqualifiedType(); |
| 5907 | |
| 5908 | // Move on to the second parameter; |
| 5909 | ++Param; |
| 5910 | |
| 5911 | // If there is no second parameter, the first must be a const char * |
| 5912 | if (Param == FnDecl->param_end()) { |
| 5913 | if (Context.hasSameType(T, Context.CharTy)) |
| 5914 | Valid = true; |
| 5915 | goto FinishedParams; |
| 5916 | } |
| 5917 | |
| 5918 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 5919 | // are allowed as the first parameter to a two-parameter function |
| 5920 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 5921 | Context.hasSameType(T, Context.WCharTy) || |
| 5922 | Context.hasSameType(T, Context.Char16Ty) || |
| 5923 | Context.hasSameType(T, Context.Char32Ty))) |
| 5924 | goto FinishedParams; |
| 5925 | |
| 5926 | // The second and final parameter must be an std::size_t |
| 5927 | T = (*Param)->getType().getUnqualifiedType(); |
| 5928 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 5929 | ++Param == FnDecl->param_end()) |
| 5930 | Valid = true; |
| 5931 | } |
| 5932 | |
| 5933 | // FIXME: This diagnostic is absolutely terrible. |
| 5934 | FinishedParams: |
| 5935 | if (!Valid) { |
| 5936 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 5937 | << FnDecl->getDeclName(); |
| 5938 | return true; |
| 5939 | } |
| 5940 | |
| 5941 | return false; |
| 5942 | } |
| 5943 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5944 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 5945 | /// linkage specification, including the language and (if present) |
| 5946 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 5947 | /// the location of the language string literal, which is provided |
| 5948 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 5949 | /// the '{' brace. Otherwise, this linkage specification does not |
| 5950 | /// have any braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5951 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5952 | SourceLocation ExternLoc, |
| 5953 | SourceLocation LangLoc, |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 5954 | llvm::StringRef Lang, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5955 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5956 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 5957 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5958 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 5959 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5960 | Language = LinkageSpecDecl::lang_cxx; |
| 5961 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5962 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5963 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5964 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5965 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5966 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5967 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5968 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5969 | LangLoc, Language, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5970 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5971 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5972 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5973 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5974 | } |
| 5975 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 5976 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5977 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 5978 | /// valid, it's the position of the closing '}' brace in a linkage |
| 5979 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5980 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 5981 | Decl *LinkageSpec, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5982 | SourceLocation RBraceLoc) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5983 | if (LinkageSpec) |
| 5984 | PopDeclContext(); |
| 5985 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 5986 | } |
| 5987 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5988 | /// \brief Perform semantic analysis for the variable declaration that |
| 5989 | /// occurs within a C++ catch clause, returning the newly-created |
| 5990 | /// variable. |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 5991 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5992 | TypeSourceInfo *TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5993 | IdentifierInfo *Name, |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 5994 | SourceLocation Loc) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5995 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 5996 | QualType ExDeclType = TInfo->getType(); |
| 5997 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5998 | // Arrays and functions decay. |
| 5999 | if (ExDeclType->isArrayType()) |
| 6000 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 6001 | else if (ExDeclType->isFunctionType()) |
| 6002 | ExDeclType = Context.getPointerType(ExDeclType); |
| 6003 | |
| 6004 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 6005 | // The exception-declaration shall not denote a pointer or reference to an |
| 6006 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6007 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6008 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6009 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6010 | Invalid = true; |
| 6011 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6012 | |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6013 | // GCC allows catching pointers and references to incomplete types |
| 6014 | // as an extension; so do we, but we warn by default. |
| 6015 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6016 | QualType BaseType = ExDeclType; |
| 6017 | 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] | 6018 | unsigned DK = diag::err_catch_incomplete; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6019 | bool IncompleteCatchIsInvalid = true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6020 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6021 | BaseType = Ptr->getPointeeType(); |
| 6022 | Mode = 1; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6023 | DK = diag::ext_catch_incomplete_ptr; |
| 6024 | IncompleteCatchIsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6025 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6026 | // 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] | 6027 | BaseType = Ref->getPointeeType(); |
| 6028 | Mode = 2; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6029 | DK = diag::ext_catch_incomplete_ref; |
| 6030 | IncompleteCatchIsInvalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6031 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6032 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6033 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK) && |
| 6034 | IncompleteCatchIsInvalid) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6035 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6036 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6037 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6038 | RequireNonAbstractType(Loc, ExDeclType, |
| 6039 | diag::err_abstract_type_in_decl, |
| 6040 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 6041 | Invalid = true; |
| 6042 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6043 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 6044 | // of ObjC types, and no runtime supports catching ObjC types by value. |
| 6045 | if (!Invalid && getLangOptions().ObjC1) { |
| 6046 | QualType T = ExDeclType; |
| 6047 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 6048 | T = RT->getPointeeType(); |
| 6049 | |
| 6050 | if (T->isObjCObjectType()) { |
| 6051 | Diag(Loc, diag::err_objc_object_catch); |
| 6052 | Invalid = true; |
| 6053 | } else if (T->isObjCObjectPointerType()) { |
| 6054 | if (!getLangOptions().NeXTRuntime) { |
| 6055 | Diag(Loc, diag::err_objc_pointer_cxx_catch_gnu); |
| 6056 | Invalid = true; |
| 6057 | } else if (!getLangOptions().ObjCNonFragileABI) { |
| 6058 | Diag(Loc, diag::err_objc_pointer_cxx_catch_fragile); |
| 6059 | Invalid = true; |
| 6060 | } |
| 6061 | } |
| 6062 | } |
| 6063 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6064 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6065 | Name, ExDeclType, TInfo, SC_None, |
| 6066 | SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 6067 | ExDecl->setExceptionVariable(true); |
| 6068 | |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6069 | if (!Invalid) { |
| 6070 | if (const RecordType *RecordTy = ExDeclType->getAs<RecordType>()) { |
| 6071 | // C++ [except.handle]p16: |
| 6072 | // The object declared in an exception-declaration or, if the |
| 6073 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 6074 | // copy-initialized (8.5) from the exception object. [...] |
| 6075 | // The object is destroyed when the handler exits, after the destruction |
| 6076 | // of any automatic objects initialized within the handler. |
| 6077 | // |
| 6078 | // We just pretend to initialize the object with itself, then make sure |
| 6079 | // it can be destroyed later. |
| 6080 | InitializedEntity Entity = InitializedEntity::InitializeVariable(ExDecl); |
| 6081 | Expr *ExDeclRef = DeclRefExpr::Create(Context, 0, SourceRange(), ExDecl, |
| 6082 | Loc, ExDeclType, 0); |
| 6083 | InitializationKind Kind = InitializationKind::CreateCopy(Loc, |
| 6084 | SourceLocation()); |
| 6085 | InitializationSequence InitSeq(*this, Entity, Kind, &ExDeclRef, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6086 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 6087 | MultiExprArg(*this, &ExDeclRef, 1)); |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6088 | if (Result.isInvalid()) |
| 6089 | Invalid = true; |
| 6090 | else |
| 6091 | FinalizeVarWithDestructor(ExDecl, RecordTy); |
| 6092 | } |
| 6093 | } |
| 6094 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6095 | if (Invalid) |
| 6096 | ExDecl->setInvalidDecl(); |
| 6097 | |
| 6098 | return ExDecl; |
| 6099 | } |
| 6100 | |
| 6101 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 6102 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6103 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6104 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 6105 | QualType ExDeclType = TInfo->getType(); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6106 | |
| 6107 | bool Invalid = D.isInvalidType(); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6108 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6109 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 6110 | LookupOrdinaryName, |
| 6111 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6112 | // The scope should be freshly made just for us. There is just no way |
| 6113 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6114 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6115 | if (PrevDecl->isTemplateParameter()) { |
| 6116 | // Maybe we will complain about the shadowed template parameter. |
| 6117 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6118 | } |
| 6119 | } |
| 6120 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6121 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6122 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 6123 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6124 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6125 | } |
| 6126 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6127 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6128 | D.getIdentifier(), |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6129 | D.getIdentifierLoc()); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6130 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6131 | if (Invalid) |
| 6132 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6133 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6134 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6135 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6136 | PushOnScopeChains(ExDecl, S); |
| 6137 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6138 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6139 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6140 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6141 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6142 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6143 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6144 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6145 | Expr *AssertExpr, |
| 6146 | Expr *AssertMessageExpr_) { |
| 6147 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr_); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6148 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6149 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 6150 | llvm::APSInt Value(32); |
| 6151 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 6152 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 6153 | AssertExpr->getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6154 | return 0; |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6155 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6156 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6157 | if (Value == 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6158 | Diag(AssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 6159 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6160 | } |
| 6161 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6162 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6163 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6164 | AssertExpr, AssertMessage); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6165 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6166 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6167 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6168 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6169 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6170 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 6171 | /// |
| 6172 | /// \returns A friend declaration that. |
| 6173 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation FriendLoc, |
| 6174 | TypeSourceInfo *TSInfo) { |
| 6175 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 6176 | |
| 6177 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6178 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6179 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 6180 | if (!getLangOptions().CPlusPlus0x) { |
| 6181 | // C++03 [class.friend]p2: |
| 6182 | // An elaborated-type-specifier shall be used in a friend declaration |
| 6183 | // for a class.* |
| 6184 | // |
| 6185 | // * The class-key of the elaborated-type-specifier is required. |
| 6186 | if (!ActiveTemplateInstantiations.empty()) { |
| 6187 | // Do not complain about the form of friend template types during |
| 6188 | // template instantiation; we will already have complained when the |
| 6189 | // template was declared. |
| 6190 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 6191 | // If we evaluated the type to a record type, suggest putting |
| 6192 | // a tag in front. |
| 6193 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 6194 | RecordDecl *RD = RT->getDecl(); |
| 6195 | |
| 6196 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 6197 | |
| 6198 | Diag(TypeRange.getBegin(), diag::ext_unelaborated_friend_type) |
| 6199 | << (unsigned) RD->getTagKind() |
| 6200 | << T |
| 6201 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 6202 | InsertionText); |
| 6203 | } else { |
| 6204 | Diag(FriendLoc, diag::ext_nonclass_type_friend) |
| 6205 | << T |
| 6206 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
| 6207 | } |
| 6208 | } else if (T->getAs<EnumType>()) { |
| 6209 | Diag(FriendLoc, diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6210 | << T |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6211 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6212 | } |
| 6213 | } |
| 6214 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 6215 | // C++0x [class.friend]p3: |
| 6216 | // If the type specifier in a friend declaration designates a (possibly |
| 6217 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 6218 | // the friend declaration is ignored. |
| 6219 | |
| 6220 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 6221 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6222 | |
| 6223 | return FriendDecl::Create(Context, CurContext, FriendLoc, TSInfo, FriendLoc); |
| 6224 | } |
| 6225 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6226 | /// Handle a friend type declaration. This works in tandem with |
| 6227 | /// ActOnTag. |
| 6228 | /// |
| 6229 | /// Notes on friend class templates: |
| 6230 | /// |
| 6231 | /// We generally treat friend class declarations as if they were |
| 6232 | /// declaring a class. So, for example, the elaborated type specifier |
| 6233 | /// in a friend declaration is required to obey the restrictions of a |
| 6234 | /// class-head (i.e. no typedefs in the scope chain), template |
| 6235 | /// parameters are required to match up with simple template-ids, &c. |
| 6236 | /// However, unlike when declaring a template specialization, it's |
| 6237 | /// okay to refer to a template specialization without an empty |
| 6238 | /// template parameter declaration, e.g. |
| 6239 | /// friend class A<T>::B<unsigned>; |
| 6240 | /// We permit this as a special case; if there are any template |
| 6241 | /// parameters present at all, require proper matching, i.e. |
| 6242 | /// template <> template <class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6243 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6244 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6245 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6246 | |
| 6247 | assert(DS.isFriendSpecified()); |
| 6248 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 6249 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6250 | // Try to convert the decl specifier to a type. This works for |
| 6251 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 6252 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 6253 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6254 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 6255 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 6256 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6257 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6258 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6259 | // This is definitely an error in C++98. It's probably meant to |
| 6260 | // be forbidden in C++0x, too, but the specification is just |
| 6261 | // poorly written. |
| 6262 | // |
| 6263 | // The problem is with declarations like the following: |
| 6264 | // template <T> friend A<T>::foo; |
| 6265 | // where deciding whether a class C is a friend or not now hinges |
| 6266 | // on whether there exists an instantiation of A that causes |
| 6267 | // 'foo' to equal C. There are restrictions on class-heads |
| 6268 | // (which we declare (by fiat) elaborated friend declarations to |
| 6269 | // be) that makes this tractable. |
| 6270 | // |
| 6271 | // FIXME: handle "template <> friend class A<T>;", which |
| 6272 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 6273 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6274 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 6275 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6276 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6277 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6278 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6279 | // C++98 [class.friend]p1: A friend of a class is a function |
| 6280 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 6281 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 6282 | // deadline. It's also a very silly restriction that seriously |
| 6283 | // affects inner classes and which nobody else seems to implement; |
| 6284 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6285 | // |
| 6286 | // But note that we could warn about it: it's always useless to |
| 6287 | // friend one of your own members (it's not, however, worthless to |
| 6288 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6289 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6290 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6291 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6292 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6293 | NumTempParamLists, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6294 | (TemplateParameterList**) TempParams.release(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6295 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6296 | DS.getFriendSpecLoc()); |
| 6297 | else |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6298 | D = CheckFriendTypeDecl(DS.getFriendSpecLoc(), TSI); |
| 6299 | |
| 6300 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6301 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6302 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6303 | D->setAccess(AS_public); |
| 6304 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6305 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6306 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6307 | } |
| 6308 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6309 | Decl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, bool IsDefinition, |
| 6310 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6311 | const DeclSpec &DS = D.getDeclSpec(); |
| 6312 | |
| 6313 | assert(DS.isFriendSpecified()); |
| 6314 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 6315 | |
| 6316 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6317 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 6318 | QualType T = TInfo->getType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6319 | |
| 6320 | // C++ [class.friend]p1 |
| 6321 | // A friend of a class is a function or class.... |
| 6322 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6323 | // It *doesn't* see through dependent types, which is correct |
| 6324 | // according to [temp.arg.type]p3: |
| 6325 | // If a declaration acquires a function type through a |
| 6326 | // type dependent on a template-parameter and this causes |
| 6327 | // a declaration that does not use the syntactic form of a |
| 6328 | // function declarator to have a function type, the program |
| 6329 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6330 | if (!T->isFunctionType()) { |
| 6331 | Diag(Loc, diag::err_unexpected_friend); |
| 6332 | |
| 6333 | // It might be worthwhile to try to recover by creating an |
| 6334 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6335 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6336 | } |
| 6337 | |
| 6338 | // C++ [namespace.memdef]p3 |
| 6339 | // - If a friend declaration in a non-local class first declares a |
| 6340 | // class or function, the friend class or function is a member |
| 6341 | // of the innermost enclosing namespace. |
| 6342 | // - The name of the friend is not found by simple name lookup |
| 6343 | // until a matching declaration is provided in that namespace |
| 6344 | // scope (either before or after the class declaration granting |
| 6345 | // friendship). |
| 6346 | // - If a friend function is called, its name may be found by the |
| 6347 | // name lookup that considers functions from namespaces and |
| 6348 | // classes associated with the types of the function arguments. |
| 6349 | // - When looking for a prior declaration of a class or a function |
| 6350 | // declared as a friend, scopes outside the innermost enclosing |
| 6351 | // namespace scope are not considered. |
| 6352 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6353 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6354 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6355 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6356 | assert(Name); |
| 6357 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6358 | // The context we found the declaration in, or in which we should |
| 6359 | // create the declaration. |
| 6360 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 6361 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6362 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6363 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6364 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6365 | // FIXME: there are different rules in local classes |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6366 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6367 | // There are four cases here. |
| 6368 | // - 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] | 6369 | // appropriate scope and look for a function or function template |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6370 | // there as appropriate. |
| 6371 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 6372 | if (SS.isInvalid() || !SS.isSet()) { |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6373 | // C++0x [namespace.memdef]p3: |
| 6374 | // If the name in a friend declaration is neither qualified nor |
| 6375 | // a template-id and the declaration is a function or an |
| 6376 | // elaborated-type-specifier, the lookup to determine whether |
| 6377 | // the entity has been previously declared shall not consider |
| 6378 | // any scopes outside the innermost enclosing namespace. |
| 6379 | // C++0x [class.friend]p11: |
| 6380 | // If a friend declaration appears in a local class and the name |
| 6381 | // specified is an unqualified name, a prior declaration is |
| 6382 | // looked up without considering scopes that are outside the |
| 6383 | // innermost enclosing non-class scope. For a friend function |
| 6384 | // declaration, if there is no prior declaration, the program is |
| 6385 | // ill-formed. |
| 6386 | bool isLocal = cast<CXXRecordDecl>(CurContext)->isLocalClass(); |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 6387 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6388 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6389 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6390 | DC = CurContext; |
| 6391 | while (true) { |
| 6392 | // Skip class contexts. If someone can cite chapter and verse |
| 6393 | // for this behavior, that would be nice --- it's what GCC and |
| 6394 | // EDG do, and it seems like a reasonable intent, but the spec |
| 6395 | // really only says that checks for unqualified existing |
| 6396 | // declarations should stop at the nearest enclosing namespace, |
| 6397 | // not that they should only consider the nearest enclosing |
| 6398 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6399 | while (DC->isRecord()) |
| 6400 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6401 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6402 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6403 | |
| 6404 | // TODO: decide what we think about using declarations. |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6405 | if (isLocal || !Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6406 | break; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6407 | |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 6408 | if (isTemplateId) { |
| 6409 | if (isa<TranslationUnitDecl>(DC)) break; |
| 6410 | } else { |
| 6411 | if (DC->isFileContext()) break; |
| 6412 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6413 | DC = DC->getParent(); |
| 6414 | } |
| 6415 | |
| 6416 | // C++ [class.friend]p1: A friend of a class is a function or |
| 6417 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 6418 | // C++0x changes this for both friend types and functions. |
| 6419 | // Most C++ 98 compilers do seem to give an error here, so |
| 6420 | // we do, too. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6421 | if (!Previous.empty() && DC->Equals(CurContext) |
| 6422 | && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6423 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6424 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 6425 | DCScope = getScopeForDeclContext(S, DC); |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6426 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6427 | // - There's a non-dependent scope specifier, in which case we |
| 6428 | // compute it and do a previous lookup there for a function |
| 6429 | // or function template. |
| 6430 | } else if (!SS.getScopeRep()->isDependent()) { |
| 6431 | DC = computeDeclContext(SS); |
| 6432 | if (!DC) return 0; |
| 6433 | |
| 6434 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 6435 | |
| 6436 | LookupQualifiedName(Previous, DC); |
| 6437 | |
| 6438 | // Ignore things found implicitly in the wrong scope. |
| 6439 | // TODO: better diagnostics for this case. Suggesting the right |
| 6440 | // qualified scope would be nice... |
| 6441 | LookupResult::Filter F = Previous.makeFilter(); |
| 6442 | while (F.hasNext()) { |
| 6443 | NamedDecl *D = F.next(); |
| 6444 | if (!DC->InEnclosingNamespaceSetOf( |
| 6445 | D->getDeclContext()->getRedeclContext())) |
| 6446 | F.erase(); |
| 6447 | } |
| 6448 | F.done(); |
| 6449 | |
| 6450 | if (Previous.empty()) { |
| 6451 | D.setInvalidType(); |
| 6452 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 6453 | return 0; |
| 6454 | } |
| 6455 | |
| 6456 | // C++ [class.friend]p1: A friend of a class is a function or |
| 6457 | // class that is not a member of the class . . . |
| 6458 | if (DC->Equals(CurContext)) |
| 6459 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 6460 | |
| 6461 | // - There's a scope specifier that does not match any template |
| 6462 | // parameter lists, in which case we use some arbitrary context, |
| 6463 | // create a method or method template, and wait for instantiation. |
| 6464 | // - There's a scope specifier that does match some template |
| 6465 | // parameter lists, which we don't handle right now. |
| 6466 | } else { |
| 6467 | DC = CurContext; |
| 6468 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6469 | } |
| 6470 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6471 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6472 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6473 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 6474 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 6475 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6476 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6477 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 6478 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6479 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6480 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6481 | } |
| 6482 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6483 | bool Redeclaration = false; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 6484 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, T, TInfo, Previous, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6485 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 6486 | IsDefinition, |
| 6487 | Redeclaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6488 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6489 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6490 | assert(ND->getDeclContext() == DC); |
| 6491 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 6492 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6493 | // Add the function declaration to the appropriate lookup tables, |
| 6494 | // adjusting the redeclarations list as necessary. We don't |
| 6495 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6496 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6497 | // Also update the scope-based lookup if the target context's |
| 6498 | // lookup context is in lexical scope. |
| 6499 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6500 | DC = DC->getRedeclContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6501 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6502 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6503 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6504 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6505 | |
| 6506 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6507 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6508 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 6509 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6510 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6511 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6512 | if (ND->isInvalidDecl()) |
| 6513 | FrD->setInvalidDecl(); |
| 6514 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6515 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 6516 | } |
| 6517 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6518 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 6519 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6520 | |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6521 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 6522 | if (!Fn) { |
| 6523 | Diag(DelLoc, diag::err_deleted_non_function); |
| 6524 | return; |
| 6525 | } |
| 6526 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 6527 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 6528 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 6529 | // If the declaration wasn't the first, we delete the function anyway for |
| 6530 | // recovery. |
| 6531 | } |
| 6532 | Fn->setDeleted(); |
| 6533 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 6534 | |
| 6535 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 6536 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 6537 | ++CI) { |
| 6538 | Stmt *SubStmt = *CI; |
| 6539 | if (!SubStmt) |
| 6540 | continue; |
| 6541 | if (isa<ReturnStmt>(SubStmt)) |
| 6542 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 6543 | diag::err_return_in_constructor_handler); |
| 6544 | if (!isa<Expr>(SubStmt)) |
| 6545 | SearchForReturnInStmt(Self, SubStmt); |
| 6546 | } |
| 6547 | } |
| 6548 | |
| 6549 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 6550 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 6551 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 6552 | SearchForReturnInStmt(*this, Handler); |
| 6553 | } |
| 6554 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6555 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6556 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6557 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6558 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 6559 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6560 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 6561 | if (Context.hasSameType(NewTy, OldTy) || |
| 6562 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6563 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6564 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6565 | // Check if the return types are covariant |
| 6566 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6567 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6568 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 6569 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 6570 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6571 | NewClassTy = NewPT->getPointeeType(); |
| 6572 | OldClassTy = OldPT->getPointeeType(); |
| 6573 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 6574 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 6575 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 6576 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 6577 | NewClassTy = NewRT->getPointeeType(); |
| 6578 | OldClassTy = OldRT->getPointeeType(); |
| 6579 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6580 | } |
| 6581 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6582 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6583 | // The return types aren't either both pointers or references to a class type. |
| 6584 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6585 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6586 | diag::err_different_return_type_for_overriding_virtual_function) |
| 6587 | << New->getDeclName() << NewTy << OldTy; |
| 6588 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6589 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6590 | return true; |
| 6591 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6592 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 6593 | // C++ [class.virtual]p6: |
| 6594 | // If the return type of D::f differs from the return type of B::f, the |
| 6595 | // class type in the return type of D::f shall be complete at the point of |
| 6596 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 6597 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 6598 | if (!RT->isBeingDefined() && |
| 6599 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 6600 | PDiag(diag::err_covariant_return_incomplete) |
| 6601 | << New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 6602 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 6603 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 6604 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6605 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6606 | // Check if the new class derives from the old class. |
| 6607 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 6608 | Diag(New->getLocation(), |
| 6609 | diag::err_covariant_return_not_derived) |
| 6610 | << New->getDeclName() << NewTy << OldTy; |
| 6611 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6612 | return true; |
| 6613 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6614 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6615 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 6616 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 6617 | diag::err_covariant_return_inaccessible_base, |
| 6618 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 6619 | // FIXME: Should this point to the return type? |
| 6620 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6621 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6622 | return true; |
| 6623 | } |
| 6624 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6625 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6626 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 6627 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6628 | Diag(New->getLocation(), |
| 6629 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6630 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6631 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6632 | return true; |
| 6633 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6634 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6635 | |
| 6636 | // The new class type must have the same or less qualifiers as the old type. |
| 6637 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 6638 | Diag(New->getLocation(), |
| 6639 | diag::err_covariant_return_type_class_type_more_qualified) |
| 6640 | << New->getDeclName() << NewTy << OldTy; |
| 6641 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6642 | return true; |
| 6643 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6644 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6645 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6646 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6647 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 6648 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
| 6649 | const CXXMethodDecl *Old) |
| 6650 | { |
| 6651 | if (Old->hasAttr<FinalAttr>()) { |
| 6652 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 6653 | << New->getDeclName(); |
| 6654 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6655 | return true; |
| 6656 | } |
| 6657 | |
| 6658 | return false; |
| 6659 | } |
| 6660 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 6661 | /// \brief Mark the given method pure. |
| 6662 | /// |
| 6663 | /// \param Method the method to be marked pure. |
| 6664 | /// |
| 6665 | /// \param InitRange the source range that covers the "0" initializer. |
| 6666 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
| 6667 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 6668 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 6669 | return false; |
| 6670 | } |
| 6671 | |
| 6672 | if (!Method->isInvalidDecl()) |
| 6673 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 6674 | << Method->getDeclName() << InitRange; |
| 6675 | return true; |
| 6676 | } |
| 6677 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6678 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 6679 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 6680 | /// is a fresh scope pushed for just this purpose. |
| 6681 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6682 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 6683 | /// static data member of class X, names should be looked up in the scope of |
| 6684 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6685 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6686 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6687 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6688 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6689 | // We should only get called for declarations with scope specifiers, like: |
| 6690 | // int foo::bar; |
| 6691 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6692 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6693 | } |
| 6694 | |
| 6695 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6696 | /// initializer for the out-of-line declaration 'D'. |
| 6697 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6698 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6699 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6700 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6701 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6702 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6703 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6704 | |
| 6705 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 6706 | /// C++ if/switch/while/for statement. |
| 6707 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6708 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6709 | // C++ 6.4p2: |
| 6710 | // The declarator shall not specify a function or an array. |
| 6711 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 6712 | // new class or enumeration. |
| 6713 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 6714 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 6715 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6716 | TagDecl *OwnedTag = 0; |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6717 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag); |
| 6718 | QualType Ty = TInfo->getType(); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6719 | |
| 6720 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 6721 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 6722 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 6723 | Diag(D.getIdentifierLoc(), diag::err_invalid_use_of_function_type) |
| 6724 | << D.getSourceRange(); |
| 6725 | return DeclResult(); |
| 6726 | } else if (OwnedTag && OwnedTag->isDefinition()) { |
| 6727 | // The type-specifier-seq shall not declare a new class or enumeration. |
| 6728 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition); |
| 6729 | } |
| 6730 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6731 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6732 | if (!Dcl) |
| 6733 | return DeclResult(); |
| 6734 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6735 | return Dcl; |
| 6736 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6737 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6738 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 6739 | bool DefinitionRequired) { |
| 6740 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 6741 | // not have a vtable. |
| 6742 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 6743 | CurContext->isDependentContext() || |
| 6744 | ExprEvalContexts.back().Context == Unevaluated) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 6745 | return; |
| 6746 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6747 | // Try to insert this class into the map. |
| 6748 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 6749 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 6750 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 6751 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 6752 | // If we already had an entry, check to see if we are promoting this vtable |
| 6753 | // to required a definition. If so, we need to reappend to the VTableUses |
| 6754 | // list, since we may have already processed the first entry. |
| 6755 | if (DefinitionRequired && !Pos.first->second) { |
| 6756 | Pos.first->second = true; |
| 6757 | } else { |
| 6758 | // Otherwise, we can early exit. |
| 6759 | return; |
| 6760 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6761 | } |
| 6762 | |
| 6763 | // Local classes need to have their virtual members marked |
| 6764 | // immediately. For all other classes, we mark their virtual members |
| 6765 | // at the end of the translation unit. |
| 6766 | if (Class->isLocalClass()) |
| 6767 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 6768 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6769 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 6770 | } |
| 6771 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6772 | bool Sema::DefineUsedVTables() { |
| 6773 | // If any dynamic classes have their key function defined within |
| 6774 | // this translation unit, then those vtables are considered "used" and must |
| 6775 | // be emitted. |
| 6776 | for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) { |
| 6777 | if (const CXXMethodDecl *KeyFunction |
| 6778 | = Context.getKeyFunction(DynamicClasses[I])) { |
| 6779 | const FunctionDecl *Definition = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 6780 | if (KeyFunction->hasBody(Definition)) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6781 | MarkVTableUsed(Definition->getLocation(), DynamicClasses[I], true); |
| 6782 | } |
| 6783 | } |
| 6784 | |
| 6785 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6786 | return false; |
| 6787 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6788 | // Note: The VTableUses vector could grow as a result of marking |
| 6789 | // the members of a class as "used", so we check the size each |
| 6790 | // time through the loop and prefer indices (with are stable) to |
| 6791 | // iterators (which are not). |
| 6792 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 6793 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6794 | if (!Class) |
| 6795 | continue; |
| 6796 | |
| 6797 | SourceLocation Loc = VTableUses[I].second; |
| 6798 | |
| 6799 | // If this class has a key function, but that key function is |
| 6800 | // defined in another translation unit, we don't need to emit the |
| 6801 | // vtable even though we're using it. |
| 6802 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 6803 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6804 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 6805 | case TSK_Undeclared: |
| 6806 | case TSK_ExplicitSpecialization: |
| 6807 | case TSK_ExplicitInstantiationDeclaration: |
| 6808 | // The key function is in another translation unit. |
| 6809 | continue; |
| 6810 | |
| 6811 | case TSK_ExplicitInstantiationDefinition: |
| 6812 | case TSK_ImplicitInstantiation: |
| 6813 | // We will be instantiating the key function. |
| 6814 | break; |
| 6815 | } |
| 6816 | } else if (!KeyFunction) { |
| 6817 | // If we have a class with no key function that is the subject |
| 6818 | // of an explicit instantiation declaration, suppress the |
| 6819 | // vtable; it will live with the explicit instantiation |
| 6820 | // definition. |
| 6821 | bool IsExplicitInstantiationDeclaration |
| 6822 | = Class->getTemplateSpecializationKind() |
| 6823 | == TSK_ExplicitInstantiationDeclaration; |
| 6824 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 6825 | REnd = Class->redecls_end(); |
| 6826 | R != REnd; ++R) { |
| 6827 | TemplateSpecializationKind TSK |
| 6828 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 6829 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 6830 | IsExplicitInstantiationDeclaration = true; |
| 6831 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 6832 | IsExplicitInstantiationDeclaration = false; |
| 6833 | break; |
| 6834 | } |
| 6835 | } |
| 6836 | |
| 6837 | if (IsExplicitInstantiationDeclaration) |
| 6838 | continue; |
| 6839 | } |
| 6840 | |
| 6841 | // Mark all of the virtual members of this class as referenced, so |
| 6842 | // that we can build a vtable. Then, tell the AST consumer that a |
| 6843 | // vtable for this class is required. |
| 6844 | MarkVirtualMembersReferenced(Loc, Class); |
| 6845 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 6846 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 6847 | |
| 6848 | // Optionally warn if we're emitting a weak vtable. |
| 6849 | if (Class->getLinkage() == ExternalLinkage && |
| 6850 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 6851 | if (!KeyFunction || (KeyFunction->hasBody() && KeyFunction->isInlined())) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6852 | Diag(Class->getLocation(), diag::warn_weak_vtable) << Class; |
| 6853 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6854 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6855 | VTableUses.clear(); |
| 6856 | |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6857 | return true; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6858 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6859 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 6860 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 6861 | const CXXRecordDecl *RD) { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6862 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 6863 | e = RD->method_end(); i != e; ++i) { |
| 6864 | CXXMethodDecl *MD = *i; |
| 6865 | |
| 6866 | // C++ [basic.def.odr]p2: |
| 6867 | // [...] A virtual member function is used if it is not pure. [...] |
| 6868 | if (MD->isVirtual() && !MD->isPure()) |
| 6869 | MarkDeclarationReferenced(Loc, MD); |
| 6870 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 6871 | |
| 6872 | // Only classes that have virtual bases need a VTT. |
| 6873 | if (RD->getNumVBases() == 0) |
| 6874 | return; |
| 6875 | |
| 6876 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 6877 | e = RD->bases_end(); i != e; ++i) { |
| 6878 | const CXXRecordDecl *Base = |
| 6879 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 6880 | if (Base->getNumVBases() == 0) |
| 6881 | continue; |
| 6882 | MarkVirtualMembersReferenced(Loc, Base); |
| 6883 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6884 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 6885 | |
| 6886 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 6887 | /// Objective-C implementation whose ivars need be initialized. |
| 6888 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 6889 | if (!getLangOptions().CPlusPlus) |
| 6890 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 6891 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 6892 | llvm::SmallVector<ObjCIvarDecl*, 8> ivars; |
| 6893 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 6894 | if (ivars.empty()) |
| 6895 | return; |
| 6896 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
| 6897 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 6898 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 6899 | if (Field->isInvalidDecl()) |
| 6900 | continue; |
| 6901 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 6902 | CXXBaseOrMemberInitializer *Member; |
| 6903 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 6904 | InitializationKind InitKind = |
| 6905 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 6906 | |
| 6907 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6908 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6909 | InitSeq.Perform(*this, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6910 | MemberInit = MaybeCreateCXXExprWithTemporaries(MemberInit.get()); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 6911 | // Note, MemberInit could actually come back empty if no initialization |
| 6912 | // is required (e.g., because it would call a trivial default constructor) |
| 6913 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 6914 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 6915 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 6916 | Member = |
| 6917 | new (Context) CXXBaseOrMemberInitializer(Context, |
| 6918 | Field, SourceLocation(), |
| 6919 | SourceLocation(), |
| 6920 | MemberInit.takeAs<Expr>(), |
| 6921 | SourceLocation()); |
| 6922 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 6923 | |
| 6924 | // Be sure that the destructor is accessible and is marked as referenced. |
| 6925 | if (const RecordType *RecordTy |
| 6926 | = Context.getBaseElementType(Field->getType()) |
| 6927 | ->getAs<RecordType>()) { |
| 6928 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 6929 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 6930 | MarkDeclarationReferenced(Field->getLocation(), Destructor); |
| 6931 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 6932 | PDiag(diag::err_access_dtor_ivar) |
| 6933 | << Context.getBaseElementType(Field->getType())); |
| 6934 | } |
| 6935 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 6936 | } |
| 6937 | ObjCImplementation->setIvarInitializers(Context, |
| 6938 | AllToInit.data(), AllToInit.size()); |
| 6939 | } |
| 6940 | } |