Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 1 | //===------ SemaDeclCXX.cpp - Semantic Analysis for C++ Declarations ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 15 | #include "clang/Sema/CXXFieldCollector.h" |
| 16 | #include "clang/Sema/Scope.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Initialization.h" |
| 18 | #include "clang/Sema/Lookup.h" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 21 | #include "clang/AST/CharUnits.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
| 25 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DeclSpec.h" |
| 29 | #include "clang/Sema/ParsedTemplate.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 30 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
John McCall | 50df6ae | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 34 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 35 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace clang; |
| 38 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // CheckDefaultArgumentVisitor |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 45 | /// the default argument of a parameter to determine whether it |
| 46 | /// contains any ill-formed subexpressions. For example, this will |
| 47 | /// diagnose the use of local variables or parameters within the |
| 48 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 49 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 50 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 51 | Expr *DefaultArg; |
| 52 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 54 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 56 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 58 | bool VisitExpr(Expr *Node); |
| 59 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 60 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 61 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 63 | /// VisitExpr - Visit all of the children of this expression. |
| 64 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 65 | bool IsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | for (Stmt::child_iterator I = Node->child_begin(), |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 67 | E = Node->child_end(); I != E; ++I) |
| 68 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 69 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 72 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 73 | /// determine whether this declaration can be used in the default |
| 74 | /// argument expression. |
| 75 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 76 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 77 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 78 | // C++ [dcl.fct.default]p9 |
| 79 | // Default arguments are evaluated each time the function is |
| 80 | // called. The order of evaluation of function arguments is |
| 81 | // unspecified. Consequently, parameters of a function shall not |
| 82 | // be used in default argument expressions, even if they are not |
| 83 | // evaluated. Parameters of a function declared before a default |
| 84 | // argument expression are in scope and can hide namespace and |
| 85 | // class member names. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 87 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 88 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 89 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 90 | // C++ [dcl.fct.default]p7 |
| 91 | // Local variables shall not be used in default argument |
| 92 | // expressions. |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 93 | if (VDecl->isLocalVarDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 95 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 96 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 97 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 99 | return false; |
| 100 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 102 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 103 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 104 | // C++ [dcl.fct.default]p8: |
| 105 | // The keyword this shall not be used in a default argument of a |
| 106 | // member function. |
| 107 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 108 | diag::err_param_default_argument_references_this) |
| 109 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 110 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 113 | bool |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 114 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | SourceLocation EqualLoc) { |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 116 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 117 | diag::err_typecheck_decl_incomplete_type)) { |
| 118 | Param->setInvalidDecl(); |
| 119 | return true; |
| 120 | } |
| 121 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 122 | // C++ [dcl.fct.default]p5 |
| 123 | // A default argument expression is implicitly converted (clause |
| 124 | // 4) to the parameter type. The default argument expression has |
| 125 | // the same semantic constraints as the initializer expression in |
| 126 | // a declaration of a variable of the parameter type, using the |
| 127 | // copy-initialization semantics (8.5). |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 128 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 129 | Param); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 130 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 131 | EqualLoc); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 132 | InitializationSequence InitSeq(*this, Entity, Kind, &Arg, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 133 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
Nico Weber | 6bb4dcb | 2010-11-28 22:53:37 +0000 | [diff] [blame] | 134 | MultiExprArg(*this, &Arg, 1)); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 135 | if (Result.isInvalid()) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 136 | return true; |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 137 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 138 | |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 139 | CheckImplicitConversions(Arg, EqualLoc); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 140 | Arg = MaybeCreateExprWithCleanups(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 142 | // Okay: add the default argument to the parameter |
| 143 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Douglas Gregor | 8cfb7a3 | 2010-10-12 18:23:32 +0000 | [diff] [blame] | 145 | // We have already instantiated this parameter; provide each of the |
| 146 | // instantiations with the uninstantiated default argument. |
| 147 | UnparsedDefaultArgInstantiationsMap::iterator InstPos |
| 148 | = UnparsedDefaultArgInstantiations.find(Param); |
| 149 | if (InstPos != UnparsedDefaultArgInstantiations.end()) { |
| 150 | for (unsigned I = 0, N = InstPos->second.size(); I != N; ++I) |
| 151 | InstPos->second[I]->setUninstantiatedDefaultArg(Arg); |
| 152 | |
| 153 | // We're done tracking this parameter's instantiations. |
| 154 | UnparsedDefaultArgInstantiations.erase(InstPos); |
| 155 | } |
| 156 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 157 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 160 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 161 | /// provided for a function parameter is well-formed. If so, attach it |
| 162 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 163 | void |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 164 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 165 | Expr *DefaultArg) { |
| 166 | if (!param || !DefaultArg) |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 167 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 169 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 170 | UnparsedDefaultArgLocs.erase(Param); |
| 171 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 172 | // Default arguments are only permitted in C++ |
| 173 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 174 | Diag(EqualLoc, diag::err_param_default_argument) |
| 175 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 176 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | |
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() |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 314 | // strips off any top-level ExprWithCleanups. |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 315 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 316 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 317 | NewParam->setUninstantiatedDefaultArg( |
| 318 | OldParam->getUninstantiatedDefaultArg()); |
| 319 | else |
John McCall | 3d6c178 | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 320 | NewParam->setDefaultArg(OldParam->getInit()); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 321 | } else if (NewParam->hasDefaultArg()) { |
| 322 | if (New->getDescribedFunctionTemplate()) { |
| 323 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 324 | Diag(NewParam->getLocation(), |
| 325 | diag::err_param_default_argument_template_redecl) |
| 326 | << NewParam->getDefaultArgRange(); |
| 327 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 328 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 329 | } else if (New->getTemplateSpecializationKind() |
| 330 | != TSK_ImplicitInstantiation && |
| 331 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 332 | // C++ [temp.expr.spec]p21: |
| 333 | // Default function arguments shall not be specified in a declaration |
| 334 | // or a definition for one of the following explicit specializations: |
| 335 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 336 | // - the explicit specialization of a member function template; |
| 337 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 338 | // template where the class template specialization to which the |
| 339 | // member function specialization belongs is implicitly |
| 340 | // instantiated. |
| 341 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 342 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 343 | << New->getDeclName() |
| 344 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 345 | } else if (New->getDeclContext()->isDependentContext()) { |
| 346 | // C++ [dcl.fct.default]p6 (DR217): |
| 347 | // Default arguments for a member function of a class template shall |
| 348 | // be specified on the initial declaration of the member function |
| 349 | // within the class template. |
| 350 | // |
| 351 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 352 | // leads me to the conclusion that one cannot add default function |
| 353 | // arguments for an out-of-line definition of a member function of a |
| 354 | // dependent type. |
| 355 | int WhichKind = 2; |
| 356 | if (CXXRecordDecl *Record |
| 357 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 358 | if (Record->getDescribedClassTemplate()) |
| 359 | WhichKind = 0; |
| 360 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 361 | WhichKind = 1; |
| 362 | else |
| 363 | WhichKind = 2; |
| 364 | } |
| 365 | |
| 366 | Diag(NewParam->getLocation(), |
| 367 | diag::err_param_default_argument_member_template_redecl) |
| 368 | << WhichKind |
| 369 | << NewParam->getDefaultArgRange(); |
| 370 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 374 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 375 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 376 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 377 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 381 | /// function declaration are well-formed according to C++ |
| 382 | /// [dcl.fct.default]. |
| 383 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 384 | unsigned NumParams = FD->getNumParams(); |
| 385 | unsigned p; |
| 386 | |
| 387 | // Find first parameter with a default argument |
| 388 | for (p = 0; p < NumParams; ++p) { |
| 389 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 390 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | |
| 394 | // C++ [dcl.fct.default]p4: |
| 395 | // In a given function declaration, all parameters |
| 396 | // subsequent to a parameter with a default argument shall |
| 397 | // have default arguments supplied in this or previous |
| 398 | // declarations. A default argument shall not be redefined |
| 399 | // by a later declaration (not even to the same value). |
| 400 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 402 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 403 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 404 | if (Param->isInvalidDecl()) |
| 405 | /* We already complained about this parameter. */; |
| 406 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 408 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 409 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 410 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 412 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 414 | LastMissingDefaultArg = p; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (LastMissingDefaultArg > 0) { |
| 419 | // Some default arguments were missing. Clear out all of the |
| 420 | // default arguments up to (and including) the last missing |
| 421 | // default argument, so that we leave the function parameters |
| 422 | // in a semantically valid state. |
| 423 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 424 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 425 | if (Param->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 426 | Param->setDefaultArg(0); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 431 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 432 | /// isCurrentClassName - Determine whether the identifier II is the |
| 433 | /// name of the class type currently being defined. In the case of |
| 434 | /// nested classes, this will only return true if II is the name of |
| 435 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 436 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 437 | const CXXScopeSpec *SS) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 438 | assert(getLangOptions().CPlusPlus && "No class names in C!"); |
| 439 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 440 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 441 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 442 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 443 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 444 | } else |
| 445 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 446 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 447 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 448 | return &II == CurDecl->getIdentifier(); |
| 449 | else |
| 450 | return false; |
| 451 | } |
| 452 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 454 | /// |
| 455 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 456 | /// and returns NULL otherwise. |
| 457 | CXXBaseSpecifier * |
| 458 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 459 | SourceRange SpecifierRange, |
| 460 | bool Virtual, AccessSpecifier Access, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 461 | TypeSourceInfo *TInfo) { |
| 462 | QualType BaseType = TInfo->getType(); |
| 463 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 464 | // C++ [class.union]p1: |
| 465 | // A union shall not have base classes. |
| 466 | if (Class->isUnion()) { |
| 467 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 468 | << SpecifierRange; |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | if (BaseType->isDependentType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 474 | Class->getTagKind() == TTK_Class, |
| 475 | Access, TInfo); |
| 476 | |
| 477 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 478 | |
| 479 | // Base specifiers must be record types. |
| 480 | if (!BaseType->isRecordType()) { |
| 481 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | // C++ [class.union]p1: |
| 486 | // A union shall not be used as a base class. |
| 487 | if (BaseType->isUnionType()) { |
| 488 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | // C++ [class.derived]p2: |
| 493 | // The class-name in a base-specifier shall not be an incompletely |
| 494 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 496 | PDiag(diag::err_incomplete_base_class) |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 497 | << SpecifierRange)) { |
| 498 | Class->setInvalidDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 499 | return 0; |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 500 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 501 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 502 | // If the base class is polymorphic or isn't empty, the new one is/isn't, too. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 503 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 504 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 505 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 506 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 507 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 508 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 509 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 510 | // C++0x CWG Issue #817 indicates that [[final]] classes shouldn't be bases. |
| 511 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
| 512 | Diag(BaseLoc, diag::err_final_base) << BaseType.getAsString(); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 513 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 514 | << BaseType; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 515 | return 0; |
| 516 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 517 | |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 518 | if (BaseDecl->isInvalidDecl()) |
| 519 | Class->setInvalidDecl(); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 520 | |
| 521 | // Create the base specifier. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 522 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 523 | Class->getTagKind() == TTK_Class, |
| 524 | Access, TInfo); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 527 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 528 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | /// example: |
| 530 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 531 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 532 | BaseResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 533 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 534 | bool Virtual, AccessSpecifier Access, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 535 | ParsedType basetype, SourceLocation BaseLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 536 | if (!classdecl) |
| 537 | return true; |
| 538 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 539 | AdjustDeclIfTemplate(classdecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 540 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 541 | if (!Class) |
| 542 | return true; |
| 543 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 544 | TypeSourceInfo *TInfo = 0; |
| 545 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 546 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 547 | Virtual, Access, TInfo)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 548 | return BaseSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 550 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 551 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 553 | /// \brief Performs the actual work of attaching the given base class |
| 554 | /// specifiers to a C++ class. |
| 555 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 556 | unsigned NumBases) { |
| 557 | if (NumBases == 0) |
| 558 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 559 | |
| 560 | // Used to keep track of which base types we have already seen, so |
| 561 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 562 | // that the key is always the unqualified canonical type of the base |
| 563 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 564 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 565 | |
| 566 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 567 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 568 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 569 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 571 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 572 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Fariborz Jahanian | 0ed5c5d | 2010-05-20 23:34:56 +0000 | [diff] [blame] | 573 | if (!Class->hasObjectMember()) { |
| 574 | if (const RecordType *FDTTy = |
| 575 | NewBaseType.getTypePtr()->getAs<RecordType>()) |
| 576 | if (FDTTy->getDecl()->hasObjectMember()) |
| 577 | Class->setHasObjectMember(true); |
| 578 | } |
| 579 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 580 | if (KnownBaseTypes[NewBaseType]) { |
| 581 | // C++ [class.mi]p3: |
| 582 | // A class shall not be specified as a direct base class of a |
| 583 | // derived class more than once. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 584 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 585 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 586 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 587 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 588 | |
| 589 | // Delete the duplicate base class specifier; we're going to |
| 590 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 591 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 592 | |
| 593 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 594 | } else { |
| 595 | // Okay, add this new base class. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 596 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 597 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
| 601 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 602 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 603 | |
| 604 | // Delete the remaining (good) base class specifiers, since their |
| 605 | // data has been copied into the CXXRecordDecl. |
| 606 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 607 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 608 | |
| 609 | return Invalid; |
| 610 | } |
| 611 | |
| 612 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 613 | /// class, after checking whether there are any duplicate base |
| 614 | /// classes. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 615 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, BaseTy **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 616 | unsigned NumBases) { |
| 617 | if (!ClassDecl || !Bases || !NumBases) |
| 618 | return; |
| 619 | |
| 620 | AdjustDeclIfTemplate(ClassDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 621 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 622 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 623 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 624 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 625 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 626 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 627 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 628 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 629 | return ICT->getDecl(); |
| 630 | else |
| 631 | return 0; |
| 632 | } |
| 633 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 634 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 635 | /// derived from the type \p Base. |
| 636 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 637 | if (!getLangOptions().CPlusPlus) |
| 638 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 639 | |
| 640 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 641 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 642 | return false; |
| 643 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 644 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 645 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 646 | return false; |
| 647 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 648 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 649 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 653 | /// derived from the type \p Base. |
| 654 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 655 | if (!getLangOptions().CPlusPlus) |
| 656 | return false; |
| 657 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 658 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 659 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 660 | return false; |
| 661 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 662 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 663 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 664 | return false; |
| 665 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 666 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 667 | } |
| 668 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 669 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 670 | CXXCastPath &BasePathArray) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 671 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 672 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 673 | |
| 674 | const CXXBasePath &Path = Paths.front(); |
| 675 | |
| 676 | // We first go backward and check if we have a virtual base. |
| 677 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 678 | // the nearest virtual base. |
| 679 | unsigned Start = 0; |
| 680 | for (unsigned I = Path.size(); I != 0; --I) { |
| 681 | if (Path[I - 1].Base->isVirtual()) { |
| 682 | Start = I - 1; |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | // Now add all bases. |
| 688 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 689 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 692 | /// \brief Determine whether the given base path includes a virtual |
| 693 | /// base class. |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 694 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 695 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 696 | BEnd = BasePath.end(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 697 | B != BEnd; ++B) |
| 698 | if ((*B)->isVirtual()) |
| 699 | return true; |
| 700 | |
| 701 | return false; |
| 702 | } |
| 703 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 704 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 705 | /// conversion (where Derived and Base are class types) is |
| 706 | /// well-formed, meaning that the conversion is unambiguous (and |
| 707 | /// that all of the base classes are accessible). Returns true |
| 708 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 709 | /// otherwise. Loc is the location where this routine should point to |
| 710 | /// if there is an error, and Range is the source range to highlight |
| 711 | /// if there is an error. |
| 712 | bool |
| 713 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 714 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 715 | unsigned AmbigiousBaseConvID, |
| 716 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 717 | DeclarationName Name, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 718 | CXXCastPath *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 719 | // First, determine whether the path from Derived to Base is |
| 720 | // ambiguous. This is slightly more expensive than checking whether |
| 721 | // the Derived to Base conversion exists, because here we need to |
| 722 | // explore multiple paths to determine if there is an ambiguity. |
| 723 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 724 | /*DetectVirtual=*/false); |
| 725 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 726 | assert(DerivationOkay && |
| 727 | "Can only be used with a derived-to-base conversion"); |
| 728 | (void)DerivationOkay; |
| 729 | |
| 730 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 731 | if (InaccessibleBaseID) { |
| 732 | // Check that the base class can be accessed. |
| 733 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 734 | InaccessibleBaseID)) { |
| 735 | case AR_inaccessible: |
| 736 | return true; |
| 737 | case AR_accessible: |
| 738 | case AR_dependent: |
| 739 | case AR_delayed: |
| 740 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 741 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 742 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 743 | |
| 744 | // Build a base path if necessary. |
| 745 | if (BasePath) |
| 746 | BuildBasePathArray(Paths, *BasePath); |
| 747 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | // We know that the derived-to-base conversion is ambiguous, and |
| 751 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 752 | // search just one more time to compute all of the possible paths so |
| 753 | // that we can print them out. This is more expensive than any of |
| 754 | // the previous derived-to-base checks we've done, but at this point |
| 755 | // performance isn't as much of an issue. |
| 756 | Paths.clear(); |
| 757 | Paths.setRecordingPaths(true); |
| 758 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 759 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 760 | (void)StillOkay; |
| 761 | |
| 762 | // Build up a textual representation of the ambiguous paths, e.g., |
| 763 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 764 | // conversions in the diagnostic. We only print one of the paths |
| 765 | // to each base class subobject. |
| 766 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 767 | |
| 768 | Diag(Loc, AmbigiousBaseConvID) |
| 769 | << Derived << Base << PathDisplayStr << Range << Name; |
| 770 | return true; |
| 771 | } |
| 772 | |
| 773 | bool |
| 774 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 775 | SourceLocation Loc, SourceRange Range, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 776 | CXXCastPath *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 777 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 778 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 779 | IgnoreAccess ? 0 |
| 780 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 781 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 782 | Loc, Range, DeclarationName(), |
| 783 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | |
| 787 | /// @brief Builds a string representing ambiguous paths from a |
| 788 | /// specific derived class to different subobjects of the same base |
| 789 | /// class. |
| 790 | /// |
| 791 | /// This function builds a string that can be used in error messages |
| 792 | /// to show the different paths that one can take through the |
| 793 | /// inheritance hierarchy to go from the derived class to different |
| 794 | /// subobjects of a base class. The result looks something like this: |
| 795 | /// @code |
| 796 | /// struct D -> struct B -> struct A |
| 797 | /// struct D -> struct C -> struct A |
| 798 | /// @endcode |
| 799 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 800 | std::string PathDisplayStr; |
| 801 | std::set<unsigned> DisplayedPaths; |
| 802 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 803 | Path != Paths.end(); ++Path) { |
| 804 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 805 | // We haven't displayed a path to this particular base |
| 806 | // class subobject yet. |
| 807 | PathDisplayStr += "\n "; |
| 808 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 809 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 810 | Element != Path->end(); ++Element) |
| 811 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | return PathDisplayStr; |
| 816 | } |
| 817 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 818 | //===----------------------------------------------------------------------===// |
| 819 | // C++ class member Handling |
| 820 | //===----------------------------------------------------------------------===// |
| 821 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 822 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 823 | Decl *Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 824 | SourceLocation ASLoc, |
| 825 | SourceLocation ColonLoc) { |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 826 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 827 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 828 | ASLoc, ColonLoc); |
| 829 | CurContext->addHiddenDecl(ASDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 830 | return ASDecl; |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 833 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 834 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 835 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
Chris Lattner | b6688e0 | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 836 | /// any. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 837 | Decl * |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 838 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 839 | MultiTemplateParamsArg TemplateParameterLists, |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 840 | ExprTy *BW, ExprTy *InitExpr, bool IsDefinition, |
| 841 | bool Deleted) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 842 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 843 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 844 | DeclarationName Name = NameInfo.getName(); |
| 845 | SourceLocation Loc = NameInfo.getLoc(); |
Douglas Gregor | 90ba6d5 | 2010-11-09 03:31:16 +0000 | [diff] [blame] | 846 | |
| 847 | // For anonymous bitfields, the location should point to the type. |
| 848 | if (Loc.isInvalid()) |
| 849 | Loc = D.getSourceRange().getBegin(); |
| 850 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 851 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 852 | Expr *Init = static_cast<Expr*>(InitExpr); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 853 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 854 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 855 | assert(!DS.isFriendSpecified()); |
| 856 | |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 857 | bool isFunc = false; |
| 858 | if (D.isFunctionDeclarator()) |
| 859 | isFunc = true; |
| 860 | else if (D.getNumTypeObjects() == 0 && |
| 861 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 862 | QualType TDType = GetTypeFromParser(DS.getRepAsType()); |
John McCall | 4bde1e1 | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 863 | isFunc = TDType->isFunctionType(); |
| 864 | } |
| 865 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 866 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 867 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 868 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 869 | // data members and cannot be applied to names declared const or static, |
| 870 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 871 | switch (DS.getStorageClassSpec()) { |
| 872 | case DeclSpec::SCS_unspecified: |
| 873 | case DeclSpec::SCS_typedef: |
| 874 | case DeclSpec::SCS_static: |
| 875 | // FALL THROUGH. |
| 876 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 877 | case DeclSpec::SCS_mutable: |
| 878 | if (isFunc) { |
| 879 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 880 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 881 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 882 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 884 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 885 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 886 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 887 | } |
| 888 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 889 | default: |
| 890 | if (DS.getStorageClassSpecLoc().isValid()) |
| 891 | Diag(DS.getStorageClassSpecLoc(), |
| 892 | diag::err_storageclass_invalid_for_member); |
| 893 | else |
| 894 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 895 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 896 | } |
| 897 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 898 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 899 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 900 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 901 | |
| 902 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 903 | if (isInstField) { |
Douglas Gregor | 922fff2 | 2010-10-13 22:19:53 +0000 | [diff] [blame] | 904 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 905 | |
| 906 | |
| 907 | if (SS.isSet() && !SS.isInvalid()) { |
| 908 | // The user provided a superfluous scope specifier inside a class |
| 909 | // definition: |
| 910 | // |
| 911 | // class X { |
| 912 | // int X::member; |
| 913 | // }; |
| 914 | DeclContext *DC = 0; |
| 915 | if ((DC = computeDeclContext(SS, false)) && DC->Equals(CurContext)) |
| 916 | Diag(D.getIdentifierLoc(), diag::warn_member_extra_qualification) |
| 917 | << Name << FixItHint::CreateRemoval(SS.getRange()); |
| 918 | else |
| 919 | Diag(D.getIdentifierLoc(), diag::err_member_qualification) |
| 920 | << Name << SS.getRange(); |
| 921 | |
| 922 | SS.clear(); |
| 923 | } |
| 924 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 925 | // FIXME: Check for template parameters! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 926 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 927 | AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 928 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 929 | } else { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 930 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 931 | if (!Member) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 932 | return 0; |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 933 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 934 | |
| 935 | // Non-instance-fields can't have a bitfield. |
| 936 | if (BitWidth) { |
| 937 | if (Member->isInvalidDecl()) { |
| 938 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 939 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 940 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 941 | // "static member 'A' cannot be a bit-field" |
| 942 | Diag(Loc, diag::err_static_not_bitfield) |
| 943 | << Name << BitWidth->getSourceRange(); |
| 944 | } else if (isa<TypedefDecl>(Member)) { |
| 945 | // "typedef member 'x' cannot be a bit-field" |
| 946 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 947 | << Name << BitWidth->getSourceRange(); |
| 948 | } else { |
| 949 | // A function typedef ("typedef int f(); f a;"). |
| 950 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 951 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 953 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 954 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 956 | BitWidth = 0; |
| 957 | Member->setInvalidDecl(); |
| 958 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 959 | |
| 960 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 962 | // If we have declared a member function template, set the access of the |
| 963 | // templated declaration as well. |
| 964 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 965 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 966 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 967 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 968 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 969 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 970 | if (Init) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 971 | AddInitializerToDecl(Member, Init, false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 972 | if (Deleted) // FIXME: Source location is not very good. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 973 | SetDeclDeleted(Member, D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 974 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 975 | if (isInstField) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 976 | FieldCollector->Add(cast<FieldDecl>(Member)); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 977 | return 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 978 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 979 | return Member; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 982 | /// \brief Find the direct and/or virtual base specifiers that |
| 983 | /// correspond to the given base type, for use in base initialization |
| 984 | /// within a constructor. |
| 985 | static bool FindBaseInitializer(Sema &SemaRef, |
| 986 | CXXRecordDecl *ClassDecl, |
| 987 | QualType BaseType, |
| 988 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 989 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 990 | // First, check for a direct base class. |
| 991 | DirectBaseSpec = 0; |
| 992 | for (CXXRecordDecl::base_class_const_iterator Base |
| 993 | = ClassDecl->bases_begin(); |
| 994 | Base != ClassDecl->bases_end(); ++Base) { |
| 995 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 996 | // We found a direct base of this type. That's what we're |
| 997 | // initializing. |
| 998 | DirectBaseSpec = &*Base; |
| 999 | break; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | // Check for a virtual base class. |
| 1004 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1005 | // there are no virtual bases. |
| 1006 | VirtualBaseSpec = 0; |
| 1007 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1008 | // We haven't found a base yet; search the class hierarchy for a |
| 1009 | // virtual base class. |
| 1010 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1011 | /*DetectVirtual=*/false); |
| 1012 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1013 | BaseType, Paths)) { |
| 1014 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1015 | Path != Paths.end(); ++Path) { |
| 1016 | if (Path->back().Base->isVirtual()) { |
| 1017 | VirtualBaseSpec = Path->back().Base; |
| 1018 | break; |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | return DirectBaseSpec || VirtualBaseSpec; |
| 1025 | } |
| 1026 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1027 | /// ActOnMemInitializer - Handle a C++ member initializer. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1028 | MemInitResult |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1029 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1030 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1031 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1032 | IdentifierInfo *MemberOrBase, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1033 | ParsedType TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1034 | SourceLocation IdLoc, |
| 1035 | SourceLocation LParenLoc, |
| 1036 | ExprTy **Args, unsigned NumArgs, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1037 | SourceLocation RParenLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1038 | if (!ConstructorD) |
| 1039 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1041 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1042 | |
| 1043 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1044 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1045 | if (!Constructor) { |
| 1046 | // The user wrote a constructor initializer on a function that is |
| 1047 | // not a C++ constructor. Ignore the error for now, because we may |
| 1048 | // have more member initializers coming; we'll diagnose it just |
| 1049 | // once in ActOnMemInitializers. |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
| 1053 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1054 | |
| 1055 | // C++ [class.base.init]p2: |
| 1056 | // Names in a mem-initializer-id are looked up in the scope of the |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1057 | // constructor's class and, if not found in that scope, are looked |
| 1058 | // up in the scope containing the constructor's definition. |
| 1059 | // [Note: if the constructor's class contains a member with the |
| 1060 | // same name as a direct or virtual base class of the class, a |
| 1061 | // mem-initializer-id naming the member or base class and composed |
| 1062 | // of a single identifier refers to the class member. A |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1063 | // mem-initializer-id for the hidden base class may be specified |
| 1064 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1065 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1066 | // Look for a member, first. |
| 1067 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1068 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1069 | = ClassDecl->lookup(MemberOrBase); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1070 | if (Result.first != Result.second) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1071 | Member = dyn_cast<FieldDecl>(*Result.first); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1072 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1073 | if (Member) |
| 1074 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1075 | LParenLoc, RParenLoc); |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1076 | // Handle anonymous union case. |
| 1077 | if (IndirectFieldDecl* IndirectField |
| 1078 | = dyn_cast<IndirectFieldDecl>(*Result.first)) |
| 1079 | return BuildMemberInitializer(IndirectField, (Expr**)Args, |
| 1080 | NumArgs, IdLoc, |
| 1081 | LParenLoc, RParenLoc); |
| 1082 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1083 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1084 | // 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] | 1085 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1086 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1087 | |
| 1088 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1089 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1090 | } else { |
| 1091 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1092 | LookupParsedName(R, S, &SS); |
| 1093 | |
| 1094 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1095 | if (!TyD) { |
| 1096 | if (R.isAmbiguous()) return true; |
| 1097 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1098 | // We don't want access-control diagnostics here. |
| 1099 | R.suppressDiagnostics(); |
| 1100 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1101 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1102 | bool NotUnknownSpecialization = false; |
| 1103 | DeclContext *DC = computeDeclContext(SS, false); |
| 1104 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1105 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1106 | |
| 1107 | if (!NotUnknownSpecialization) { |
| 1108 | // When the scope specifier can refer to a member of an unknown |
| 1109 | // specialization, we take it as a type name. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 1110 | BaseType = CheckTypenameType(ETK_None, |
| 1111 | (NestedNameSpecifier *)SS.getScopeRep(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 1112 | *MemberOrBase, SourceLocation(), |
| 1113 | SS.getRange(), IdLoc); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1114 | if (BaseType.isNull()) |
| 1115 | return true; |
| 1116 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1117 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 1118 | R.setLookupName(MemberOrBase); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1122 | // If no results were found, try to correct typos. |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1123 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1124 | CorrectTypo(R, S, &SS, ClassDecl, 0, CTC_NoKeywords) && |
| 1125 | R.isSingleResult()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1126 | if (FieldDecl *Member = R.getAsSingle<FieldDecl>()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1127 | if (Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl)) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1128 | // We have found a non-static data member with a similar |
| 1129 | // name to what was typed; complain and initialize that |
| 1130 | // member. |
| 1131 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1132 | << MemberOrBase << true << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1133 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1134 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1135 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 1136 | << Member->getDeclName(); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1137 | |
| 1138 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 1139 | LParenLoc, RParenLoc); |
| 1140 | } |
| 1141 | } else if (TypeDecl *Type = R.getAsSingle<TypeDecl>()) { |
| 1142 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1143 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1144 | if (FindBaseInitializer(*this, ClassDecl, |
| 1145 | Context.getTypeDeclType(Type), |
| 1146 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1147 | // We have found a direct or virtual base class with a |
| 1148 | // similar name to what was typed; complain and initialize |
| 1149 | // that base class. |
| 1150 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1151 | << MemberOrBase << false << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1152 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1153 | R.getLookupName().getAsString()); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1154 | |
| 1155 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1156 | : VirtualBaseSpec; |
| 1157 | Diag(BaseSpec->getSourceRange().getBegin(), |
| 1158 | diag::note_base_class_specified_here) |
| 1159 | << BaseSpec->getType() |
| 1160 | << BaseSpec->getSourceRange(); |
| 1161 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1162 | TyD = Type; |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1167 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1168 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1169 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1170 | return true; |
| 1171 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1174 | if (BaseType.isNull()) { |
| 1175 | BaseType = Context.getTypeDeclType(TyD); |
| 1176 | if (SS.isSet()) { |
| 1177 | NestedNameSpecifier *Qualifier = |
| 1178 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1179 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1180 | // FIXME: preserve source range information |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1181 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1182 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1183 | } |
| 1184 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1185 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1186 | if (!TInfo) |
| 1187 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1188 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1189 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1190 | LParenLoc, RParenLoc, ClassDecl); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1193 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1194 | /// containing the field that is being initialized. Returns true if there is an |
| 1195 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1196 | /// otherwise. |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1197 | static bool InitExprContainsUninitializedFields(const Stmt *S, |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1198 | const ValueDecl *LhsField, |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1199 | SourceLocation *L) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1200 | assert(isa<FieldDecl>(LhsField) || isa<IndirectFieldDecl>(LhsField)); |
| 1201 | |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1202 | if (isa<CallExpr>(S)) { |
| 1203 | // Do not descend into function calls or constructors, as the use |
| 1204 | // of an uninitialized field may be valid. One would have to inspect |
| 1205 | // the contents of the function/ctor to determine if it is safe or not. |
| 1206 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1207 | // may be safe, depending on what the function/ctor does. |
| 1208 | return false; |
| 1209 | } |
| 1210 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 1211 | const NamedDecl *RhsField = ME->getMemberDecl(); |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1212 | |
| 1213 | if (const VarDecl *VD = dyn_cast<VarDecl>(RhsField)) { |
| 1214 | // The member expression points to a static data member. |
| 1215 | assert(VD->isStaticDataMember() && |
| 1216 | "Member points to non-static data member!"); |
Nick Lewycky | edd5911 | 2010-10-06 18:37:39 +0000 | [diff] [blame] | 1217 | (void)VD; |
Anders Carlsson | 175ffbf | 2010-10-06 02:43:25 +0000 | [diff] [blame] | 1218 | return false; |
| 1219 | } |
| 1220 | |
| 1221 | if (isa<EnumConstantDecl>(RhsField)) { |
| 1222 | // The member expression points to an enum. |
| 1223 | return false; |
| 1224 | } |
| 1225 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1226 | if (RhsField == LhsField) { |
| 1227 | // Initializing a field with itself. Throw a warning. |
| 1228 | // But wait; there are exceptions! |
| 1229 | // Exception #1: The field may not belong to this record. |
| 1230 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1231 | const Expr *base = ME->getBase(); |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1232 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1233 | // Even though the field matches, it does not belong to this record. |
| 1234 | return false; |
| 1235 | } |
| 1236 | // None of the exceptions triggered; return true to indicate an |
| 1237 | // uninitialized field was used. |
| 1238 | *L = ME->getMemberLoc(); |
| 1239 | return true; |
| 1240 | } |
Argyrios Kyrtzidis | ff8819b | 2010-09-21 10:47:20 +0000 | [diff] [blame] | 1241 | } else if (isa<SizeOfAlignOfExpr>(S)) { |
| 1242 | // sizeof/alignof doesn't reference contents, do not warn. |
| 1243 | return false; |
| 1244 | } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(S)) { |
| 1245 | // address-of doesn't reference contents (the pointer may be dereferenced |
| 1246 | // in the same expression but it would be rare; and weird). |
| 1247 | if (UOE->getOpcode() == UO_AddrOf) |
| 1248 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1249 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1250 | for (Stmt::const_child_iterator it = S->child_begin(), e = S->child_end(); |
| 1251 | it != e; ++it) { |
| 1252 | if (!*it) { |
| 1253 | // An expression such as 'member(arg ?: "")' may trigger this. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1254 | continue; |
| 1255 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1256 | if (InitExprContainsUninitializedFields(*it, LhsField, L)) |
| 1257 | return true; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1258 | } |
Nick Lewycky | 43ad182 | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1259 | return false; |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1262 | MemInitResult |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1263 | Sema::BuildMemberInitializer(ValueDecl *Member, Expr **Args, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1264 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1265 | SourceLocation LParenLoc, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1266 | SourceLocation RParenLoc) { |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1267 | FieldDecl *DirectMember = dyn_cast<FieldDecl>(Member); |
| 1268 | IndirectFieldDecl *IndirectMember = dyn_cast<IndirectFieldDecl>(Member); |
| 1269 | assert((DirectMember || IndirectMember) && |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1270 | "Member must be a FieldDecl or IndirectFieldDecl"); |
| 1271 | |
Douglas Gregor | 464b2f0 | 2010-11-05 22:21:31 +0000 | [diff] [blame] | 1272 | if (Member->isInvalidDecl()) |
| 1273 | return true; |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1274 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1275 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1276 | // foo(foo) |
| 1277 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1278 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1279 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1280 | SourceLocation L; |
| 1281 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1282 | // FIXME: Return true in the case when other fields are used before being |
| 1283 | // uninitialized. For example, let this field be the i'th field. When |
| 1284 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1285 | // fields are used, as they are not yet initialized. |
| 1286 | // Right now we are only handling the case where the i'th field uses |
| 1287 | // itself in its initializer. |
| 1288 | Diag(L, diag::warn_field_is_uninit); |
| 1289 | } |
| 1290 | } |
| 1291 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1292 | bool HasDependentArg = false; |
| 1293 | for (unsigned i = 0; i < NumArgs; i++) |
| 1294 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1295 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1296 | Expr *Init; |
Eli Friedman | 0f2b97d | 2010-07-24 21:19:15 +0000 | [diff] [blame] | 1297 | if (Member->getType()->isDependentType() || HasDependentArg) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1298 | // Can't check initialization for a member of dependent type or when |
| 1299 | // any of the arguments are type-dependent expressions. |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1300 | Init = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1301 | RParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1302 | |
| 1303 | // Erase any temporaries within this evaluation context; we're not |
| 1304 | // going to track them in the AST, since we'll be rebuilding the |
| 1305 | // ASTs during template instantiation. |
| 1306 | ExprTemporaries.erase( |
| 1307 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1308 | ExprTemporaries.end()); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1309 | } else { |
| 1310 | // Initialize the member. |
| 1311 | InitializedEntity MemberEntity = |
| 1312 | DirectMember ? InitializedEntity::InitializeMember(DirectMember, 0) |
| 1313 | : InitializedEntity::InitializeMember(IndirectMember, 0); |
| 1314 | InitializationKind Kind = |
| 1315 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1316 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1317 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 1318 | |
| 1319 | ExprResult MemberInit = |
| 1320 | InitSeq.Perform(*this, MemberEntity, Kind, |
| 1321 | MultiExprArg(*this, Args, NumArgs), 0); |
| 1322 | if (MemberInit.isInvalid()) |
| 1323 | return true; |
| 1324 | |
| 1325 | CheckImplicitConversions(MemberInit.get(), LParenLoc); |
| 1326 | |
| 1327 | // C++0x [class.base.init]p7: |
| 1328 | // The initialization of each base and member constitutes a |
| 1329 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1330 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1331 | if (MemberInit.isInvalid()) |
| 1332 | return true; |
| 1333 | |
| 1334 | // If we are in a dependent context, template instantiation will |
| 1335 | // perform this type-checking again. Just save the arguments that we |
| 1336 | // received in a ParenListExpr. |
| 1337 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1338 | // of the information that we have about the member |
| 1339 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1340 | // and this approach is far more likely to get the corner cases right. |
| 1341 | if (CurContext->isDependentContext()) |
| 1342 | Init = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1343 | RParenLoc); |
| 1344 | else |
| 1345 | Init = MemberInit.get(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Chandler Carruth | 894aed9 | 2010-12-06 09:23:57 +0000 | [diff] [blame] | 1348 | if (DirectMember) { |
| 1349 | return new (Context) CXXBaseOrMemberInitializer(Context, DirectMember, |
| 1350 | IdLoc, LParenLoc, Init, |
| 1351 | RParenLoc); |
| 1352 | } else { |
| 1353 | return new (Context) CXXBaseOrMemberInitializer(Context, IndirectMember, |
| 1354 | IdLoc, LParenLoc, Init, |
| 1355 | RParenLoc); |
| 1356 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1359 | MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1360 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1361 | Expr **Args, unsigned NumArgs, |
| 1362 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
| 1363 | CXXRecordDecl *ClassDecl) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1364 | bool HasDependentArg = false; |
| 1365 | for (unsigned i = 0; i < NumArgs; i++) |
| 1366 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1367 | |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1368 | SourceLocation BaseLoc |
| 1369 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1370 | |
| 1371 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 1372 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 1373 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1374 | |
| 1375 | // C++ [class.base.init]p2: |
| 1376 | // [...] Unless the mem-initializer-id names a nonstatic data |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1377 | // member of the constructor's class or a direct or virtual base |
Douglas Gregor | 3956b1a | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1378 | // of that class, the mem-initializer is ill-formed. A |
| 1379 | // mem-initializer-list can initialize a base class using any |
| 1380 | // name that denotes that base class type. |
| 1381 | bool Dependent = BaseType->isDependentType() || HasDependentArg; |
| 1382 | |
| 1383 | // Check for direct and virtual base classes. |
| 1384 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1385 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1386 | if (!Dependent) { |
| 1387 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 1388 | VirtualBaseSpec); |
| 1389 | |
| 1390 | // C++ [base.class.init]p2: |
| 1391 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 1392 | // constructor's class or a direct or virtual base of that class, the |
| 1393 | // mem-initializer is ill-formed. |
| 1394 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 1395 | // If the class has any dependent bases, then it's possible that |
| 1396 | // one of those types will resolve to the same type as |
| 1397 | // BaseType. Therefore, just treat this as a dependent base |
| 1398 | // class initialization. FIXME: Should we try to check the |
| 1399 | // initialization anyway? It seems odd. |
| 1400 | if (ClassDecl->hasAnyDependentBases()) |
| 1401 | Dependent = true; |
| 1402 | else |
| 1403 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 1404 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 1405 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | if (Dependent) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1410 | // Can't check initialization for a base of dependent type or when |
| 1411 | // any of the arguments are type-dependent expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1412 | ExprResult BaseInit |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1413 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1414 | RParenLoc)); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1415 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1416 | // Erase any temporaries within this evaluation context; we're not |
| 1417 | // going to track them in the AST, since we'll be rebuilding the |
| 1418 | // ASTs during template instantiation. |
| 1419 | ExprTemporaries.erase( |
| 1420 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1421 | ExprTemporaries.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1423 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1424 | /*IsVirtual=*/false, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1425 | LParenLoc, |
| 1426 | BaseInit.takeAs<Expr>(), |
| 1427 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1428 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1429 | |
| 1430 | // C++ [base.class.init]p2: |
| 1431 | // If a mem-initializer-id is ambiguous because it designates both |
| 1432 | // a direct non-virtual base class and an inherited virtual base |
| 1433 | // class, the mem-initializer is ill-formed. |
| 1434 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1435 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1436 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1437 | |
| 1438 | CXXBaseSpecifier *BaseSpec |
| 1439 | = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
| 1440 | if (!BaseSpec) |
| 1441 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 1442 | |
| 1443 | // Initialize the base. |
| 1444 | InitializedEntity BaseEntity = |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1445 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1446 | InitializationKind Kind = |
| 1447 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc); |
| 1448 | |
| 1449 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 1450 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1451 | ExprResult BaseInit = |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1452 | InitSeq.Perform(*this, BaseEntity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1453 | MultiExprArg(*this, Args, NumArgs), 0); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1454 | if (BaseInit.isInvalid()) |
| 1455 | return true; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 1456 | |
| 1457 | CheckImplicitConversions(BaseInit.get(), LParenLoc); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1458 | |
| 1459 | // C++0x [class.base.init]p7: |
| 1460 | // The initialization of each base and member constitutes a |
| 1461 | // full-expression. |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1462 | BaseInit = MaybeCreateExprWithCleanups(BaseInit); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1463 | if (BaseInit.isInvalid()) |
| 1464 | return true; |
| 1465 | |
| 1466 | // If we are in a dependent context, template instantiation will |
| 1467 | // perform this type-checking again. Just save the arguments that we |
| 1468 | // received in a ParenListExpr. |
| 1469 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1470 | // of the information that we have about the base |
| 1471 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1472 | // and this approach is far more likely to get the corner cases right. |
| 1473 | if (CurContext->isDependentContext()) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1474 | ExprResult Init |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1475 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1476 | RParenLoc)); |
| 1477 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1478 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1479 | LParenLoc, |
| 1480 | Init.takeAs<Expr>(), |
| 1481 | RParenLoc); |
| 1482 | } |
| 1483 | |
| 1484 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1485 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1486 | LParenLoc, |
| 1487 | BaseInit.takeAs<Expr>(), |
| 1488 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1491 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 1492 | /// initialize its base or member. |
| 1493 | enum ImplicitInitializerKind { |
| 1494 | IIK_Default, |
| 1495 | IIK_Copy, |
| 1496 | IIK_Move |
| 1497 | }; |
| 1498 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1499 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1500 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1501 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1502 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1503 | bool IsInheritedVirtualBase, |
| 1504 | CXXBaseOrMemberInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1505 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1506 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 1507 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1508 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1509 | ExprResult BaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1510 | |
| 1511 | switch (ImplicitInitKind) { |
| 1512 | case IIK_Default: { |
| 1513 | InitializationKind InitKind |
| 1514 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 1515 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1516 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1517 | MultiExprArg(SemaRef, 0, 0)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1518 | break; |
| 1519 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1520 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1521 | case IIK_Copy: { |
| 1522 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1523 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1524 | |
| 1525 | Expr *CopyCtorArg = |
| 1526 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1527 | Constructor->getLocation(), ParamType, |
| 1528 | VK_LValue, 0); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1529 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1530 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 1531 | QualType ArgTy = |
| 1532 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 1533 | ParamType.getQualifiers()); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1534 | |
| 1535 | CXXCastPath BasePath; |
| 1536 | BasePath.push_back(BaseSpec); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1537 | SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1538 | CK_UncheckedDerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1539 | VK_LValue, &BasePath); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1540 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1541 | InitializationKind InitKind |
| 1542 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1543 | SourceLocation(), SourceLocation()); |
| 1544 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1545 | &CopyCtorArg, 1); |
| 1546 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1547 | MultiExprArg(&CopyCtorArg, 1)); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1548 | break; |
| 1549 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1550 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1551 | case IIK_Move: |
| 1552 | assert(false && "Unhandled initializer kind!"); |
| 1553 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1554 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1555 | BaseInit = SemaRef.MaybeCreateExprWithCleanups(BaseInit); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1556 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1557 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1558 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1559 | CXXBaseInit = |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1560 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
| 1561 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 1562 | SourceLocation()), |
| 1563 | BaseSpec->isVirtual(), |
| 1564 | SourceLocation(), |
| 1565 | BaseInit.takeAs<Expr>(), |
| 1566 | SourceLocation()); |
| 1567 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1568 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1571 | static bool |
| 1572 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1573 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1574 | FieldDecl *Field, |
| 1575 | CXXBaseOrMemberInitializer *&CXXMemberInit) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 1576 | if (Field->isInvalidDecl()) |
| 1577 | return true; |
| 1578 | |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1579 | SourceLocation Loc = Constructor->getLocation(); |
| 1580 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1581 | if (ImplicitInitKind == IIK_Copy) { |
| 1582 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1583 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1584 | |
| 1585 | Expr *MemberExprBase = |
| 1586 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1587 | Loc, ParamType, VK_LValue, 0); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1588 | |
| 1589 | // Build a reference to this field within the parameter. |
| 1590 | CXXScopeSpec SS; |
| 1591 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 1592 | Sema::LookupMemberName); |
| 1593 | MemberLookup.addDecl(Field, AS_public); |
| 1594 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1595 | ExprResult CopyCtorArg |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1596 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1597 | ParamType, Loc, |
| 1598 | /*IsArrow=*/false, |
| 1599 | SS, |
| 1600 | /*FirstQualifierInScope=*/0, |
| 1601 | MemberLookup, |
| 1602 | /*TemplateArgs=*/0); |
| 1603 | if (CopyCtorArg.isInvalid()) |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1604 | return true; |
| 1605 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1606 | // When the field we are copying is an array, create index variables for |
| 1607 | // each dimension of the array. We use these index variables to subscript |
| 1608 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 1609 | // necessary iteration with these index variables. |
| 1610 | llvm::SmallVector<VarDecl *, 4> IndexVariables; |
| 1611 | QualType BaseType = Field->getType(); |
| 1612 | QualType SizeType = SemaRef.Context.getSizeType(); |
| 1613 | while (const ConstantArrayType *Array |
| 1614 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
| 1615 | // Create the iteration variable for this array index. |
| 1616 | IdentifierInfo *IterationVarName = 0; |
| 1617 | { |
| 1618 | llvm::SmallString<8> Str; |
| 1619 | llvm::raw_svector_ostream OS(Str); |
| 1620 | OS << "__i" << IndexVariables.size(); |
| 1621 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 1622 | } |
| 1623 | VarDecl *IterationVar |
| 1624 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, |
| 1625 | IterationVarName, SizeType, |
| 1626 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1627 | SC_None, SC_None); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1628 | IndexVariables.push_back(IterationVar); |
| 1629 | |
| 1630 | // Create a reference to the iteration variable. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1631 | ExprResult IterationVarRef |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1632 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, VK_RValue, Loc); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1633 | assert(!IterationVarRef.isInvalid() && |
| 1634 | "Reference to invented variable cannot fail!"); |
| 1635 | |
| 1636 | // Subscript the array with this iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1637 | CopyCtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CopyCtorArg.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1638 | Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1639 | IterationVarRef.take(), |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1640 | Loc); |
| 1641 | if (CopyCtorArg.isInvalid()) |
| 1642 | return true; |
| 1643 | |
| 1644 | BaseType = Array->getElementType(); |
| 1645 | } |
| 1646 | |
| 1647 | // Construct the entity that we will be initializing. For an array, this |
| 1648 | // will be first element in the array, which may require several levels |
| 1649 | // of array-subscript entities. |
| 1650 | llvm::SmallVector<InitializedEntity, 4> Entities; |
| 1651 | Entities.reserve(1 + IndexVariables.size()); |
| 1652 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
| 1653 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 1654 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 1655 | 0, |
| 1656 | Entities.back())); |
| 1657 | |
| 1658 | // Direct-initialize to use the copy constructor. |
| 1659 | InitializationKind InitKind = |
| 1660 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 1661 | |
| 1662 | Expr *CopyCtorArgE = CopyCtorArg.takeAs<Expr>(); |
| 1663 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
| 1664 | &CopyCtorArgE, 1); |
| 1665 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1666 | ExprResult MemberInit |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1667 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1668 | MultiExprArg(&CopyCtorArgE, 1)); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1669 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1670 | if (MemberInit.isInvalid()) |
| 1671 | return true; |
| 1672 | |
| 1673 | CXXMemberInit |
| 1674 | = CXXBaseOrMemberInitializer::Create(SemaRef.Context, Field, Loc, Loc, |
| 1675 | MemberInit.takeAs<Expr>(), Loc, |
| 1676 | IndexVariables.data(), |
| 1677 | IndexVariables.size()); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1678 | return false; |
| 1679 | } |
| 1680 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1681 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 1682 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1683 | QualType FieldBaseElementType = |
| 1684 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 1685 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1686 | if (FieldBaseElementType->isRecordType()) { |
| 1687 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1688 | InitializationKind InitKind = |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1689 | InitializationKind::CreateDefault(Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1690 | |
| 1691 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1692 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1693 | InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1694 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 1695 | MemberInit = SemaRef.MaybeCreateExprWithCleanups(MemberInit); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1696 | if (MemberInit.isInvalid()) |
| 1697 | return true; |
| 1698 | |
| 1699 | CXXMemberInit = |
| 1700 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1701 | Field, Loc, Loc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1702 | MemberInit.get(), |
Chandler Carruth | f186b54 | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1703 | Loc); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1704 | return false; |
| 1705 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 1706 | |
| 1707 | if (FieldBaseElementType->isReferenceType()) { |
| 1708 | SemaRef.Diag(Constructor->getLocation(), |
| 1709 | diag::err_uninitialized_member_in_ctor) |
| 1710 | << (int)Constructor->isImplicit() |
| 1711 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1712 | << 0 << Field->getDeclName(); |
| 1713 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1714 | return true; |
| 1715 | } |
| 1716 | |
| 1717 | if (FieldBaseElementType.isConstQualified()) { |
| 1718 | SemaRef.Diag(Constructor->getLocation(), |
| 1719 | diag::err_uninitialized_member_in_ctor) |
| 1720 | << (int)Constructor->isImplicit() |
| 1721 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1722 | << 1 << Field->getDeclName(); |
| 1723 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1724 | return true; |
| 1725 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1726 | |
| 1727 | // Nothing to initialize. |
| 1728 | CXXMemberInit = 0; |
| 1729 | return false; |
| 1730 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1731 | |
| 1732 | namespace { |
| 1733 | struct BaseAndFieldInfo { |
| 1734 | Sema &S; |
| 1735 | CXXConstructorDecl *Ctor; |
| 1736 | bool AnyErrorsInInits; |
| 1737 | ImplicitInitializerKind IIK; |
| 1738 | llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; |
| 1739 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 8> AllToInit; |
| 1740 | |
| 1741 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 1742 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
| 1743 | // FIXME: Handle implicit move constructors. |
| 1744 | if (Ctor->isImplicit() && Ctor->isCopyConstructor()) |
| 1745 | IIK = IIK_Copy; |
| 1746 | else |
| 1747 | IIK = IIK_Default; |
| 1748 | } |
| 1749 | }; |
| 1750 | } |
| 1751 | |
| 1752 | static bool CollectFieldInitializer(BaseAndFieldInfo &Info, |
| 1753 | FieldDecl *Top, FieldDecl *Field) { |
| 1754 | |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1755 | // Overwhelmingly common case: we have a direct initializer for this field. |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1756 | if (CXXBaseOrMemberInitializer *Init = Info.AllBaseFields.lookup(Field)) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1757 | Info.AllToInit.push_back(Init); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1758 | return false; |
| 1759 | } |
| 1760 | |
| 1761 | if (Info.IIK == IIK_Default && Field->isAnonymousStructOrUnion()) { |
| 1762 | const RecordType *FieldClassType = Field->getType()->getAs<RecordType>(); |
| 1763 | assert(FieldClassType && "anonymous struct/union without record type"); |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1764 | CXXRecordDecl *FieldClassDecl |
| 1765 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1766 | |
| 1767 | // Even though union members never have non-trivial default |
| 1768 | // constructions in C++03, we still build member initializers for aggregate |
| 1769 | // record types which can be union members, and C++0x allows non-trivial |
| 1770 | // default constructors for union members, so we ensure that only one |
| 1771 | // member is initialized for these. |
| 1772 | if (FieldClassDecl->isUnion()) { |
| 1773 | // First check for an explicit initializer for one field. |
| 1774 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 1775 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1776 | if (CXXBaseOrMemberInitializer *Init = Info.AllBaseFields.lookup(*FA)) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1777 | Info.AllToInit.push_back(Init); |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1778 | |
| 1779 | // Once we've initialized a field of an anonymous union, the union |
| 1780 | // field in the class is also initialized, so exit immediately. |
| 1781 | return false; |
Argyrios Kyrtzidis | 881b36c | 2010-08-16 17:27:13 +0000 | [diff] [blame] | 1782 | } else if ((*FA)->isAnonymousStructOrUnion()) { |
| 1783 | if (CollectFieldInitializer(Info, Top, *FA)) |
| 1784 | return true; |
Chandler Carruth | e861c60 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | // Fallthrough and construct a default initializer for the union as |
| 1789 | // a whole, which can call its default constructor if such a thing exists |
| 1790 | // (C++0x perhaps). FIXME: It's not clear that this is the correct |
| 1791 | // behavior going forward with C++0x, when anonymous unions there are |
| 1792 | // finalized, we should revisit this. |
| 1793 | } else { |
| 1794 | // For structs, we simply descend through to initialize all members where |
| 1795 | // necessary. |
| 1796 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 1797 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1798 | if (CollectFieldInitializer(Info, Top, *FA)) |
| 1799 | return true; |
| 1800 | } |
| 1801 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | // Don't try to build an implicit initializer if there were semantic |
| 1805 | // errors in any of the initializers (and therefore we might be |
| 1806 | // missing some that the user actually wrote). |
| 1807 | if (Info.AnyErrorsInInits) |
| 1808 | return false; |
| 1809 | |
| 1810 | CXXBaseOrMemberInitializer *Init = 0; |
| 1811 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, Init)) |
| 1812 | return true; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1813 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1814 | if (Init) |
| 1815 | Info.AllToInit.push_back(Init); |
| 1816 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1817 | return false; |
| 1818 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1819 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1820 | bool |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1821 | Sema::SetBaseOrMemberInitializers(CXXConstructorDecl *Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1822 | CXXBaseOrMemberInitializer **Initializers, |
| 1823 | unsigned NumInitializers, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1824 | bool AnyErrors) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1825 | if (Constructor->getDeclContext()->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1826 | // Just store the initializers as written, they will be checked during |
| 1827 | // instantiation. |
| 1828 | if (NumInitializers > 0) { |
| 1829 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1830 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1831 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
| 1832 | memcpy(baseOrMemberInitializers, Initializers, |
| 1833 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
| 1834 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
| 1835 | } |
| 1836 | |
| 1837 | return false; |
| 1838 | } |
| 1839 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1840 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1841 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1842 | // We need to build the initializer AST according to order of construction |
| 1843 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1844 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 1845 | if (!ClassDecl) |
| 1846 | return true; |
| 1847 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1848 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1849 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1850 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1851 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1852 | |
| 1853 | if (Member->isBaseInitializer()) |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1854 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1855 | else |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1856 | Info.AllBaseFields[Member->getAnyMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1859 | // Keep track of the direct virtual bases. |
| 1860 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 1861 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 1862 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 1863 | if (I->isVirtual()) |
| 1864 | DirectVBases.insert(I); |
| 1865 | } |
| 1866 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1867 | // Push virtual bases before others. |
| 1868 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 1869 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 1870 | |
| 1871 | if (CXXBaseOrMemberInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1872 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 1873 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1874 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1875 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1876 | CXXBaseOrMemberInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1877 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1878 | VBase, IsInheritedVirtualBase, |
| 1879 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1880 | HadError = true; |
| 1881 | continue; |
| 1882 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1883 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1884 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1885 | } |
| 1886 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1887 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1888 | // Non-virtual bases. |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1889 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1890 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1891 | // Virtuals are in the virtual base list and already constructed. |
| 1892 | if (Base->isVirtual()) |
| 1893 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1895 | if (CXXBaseOrMemberInitializer *Value |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1896 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 1897 | Info.AllToInit.push_back(Value); |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1898 | } else if (!AnyErrors) { |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1899 | CXXBaseOrMemberInitializer *CXXBaseInit; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1900 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1901 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1902 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1903 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1904 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1905 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1906 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1907 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1908 | } |
| 1909 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1911 | // Fields. |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1912 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 1913 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 1914 | if ((*Field)->getType()->isIncompleteArrayType()) { |
| 1915 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 1916 | "Incomplete array type is not valid"); |
| 1917 | continue; |
| 1918 | } |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1919 | if (CollectFieldInitializer(Info, *Field, *Field)) |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1920 | HadError = true; |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 1921 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1922 | |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1923 | NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1924 | if (NumInitializers > 0) { |
| 1925 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1926 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1927 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
John McCall | f1860e5 | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1928 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1929 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1930 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 1931 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1932 | // Constructors implicitly reference the base and member |
| 1933 | // destructors. |
| 1934 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 1935 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1936 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1937 | |
| 1938 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1941 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 1942 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1943 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1944 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 1945 | return static_cast<void *>(RT->getDecl()); |
| 1946 | } |
| 1947 | return static_cast<void *>(Field); |
| 1948 | } |
| 1949 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1950 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 1951 | return Context.getCanonicalType(BaseType).getTypePtr(); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1954 | static void *GetKeyForMember(ASTContext &Context, |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1955 | CXXBaseOrMemberInitializer *Member) { |
| 1956 | if (!Member->isAnyMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1957 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1958 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1959 | // For fields injected into the class via declaration of an anonymous union, |
| 1960 | // use its anonymous union class declaration as the unique key. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1961 | FieldDecl *Field = Member->getAnyMember(); |
| 1962 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1963 | // If the field is a member of an anonymous struct or union, our key |
| 1964 | // 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] | 1965 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1966 | if (RD->isAnonymousStructOrUnion()) { |
| 1967 | while (true) { |
| 1968 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 1969 | if (Parent->isAnonymousStructOrUnion()) |
| 1970 | RD = Parent; |
| 1971 | else |
| 1972 | break; |
| 1973 | } |
| 1974 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1975 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1976 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1978 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1981 | static void |
| 1982 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1983 | const CXXConstructorDecl *Constructor, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1984 | CXXBaseOrMemberInitializer **Inits, |
| 1985 | unsigned NumInits) { |
| 1986 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1987 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1989 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order) |
| 1990 | == Diagnostic::Ignored) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1991 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1992 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1993 | // Build the list of bases and members in the order that they'll |
| 1994 | // actually be initialized. The explicit initializers should be in |
| 1995 | // this same order but may be missing things. |
| 1996 | llvm::SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1998 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1999 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2000 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2001 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2002 | ClassDecl->vbases_begin(), |
| 2003 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2004 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
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 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 2007 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2008 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2009 | if (Base->isVirtual()) |
| 2010 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2011 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2012 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2014 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2015 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 2016 | E = ClassDecl->field_end(); Field != E; ++Field) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2017 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2019 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 2020 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 2021 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2022 | CXXBaseOrMemberInitializer *PrevInit = 0; |
| 2023 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
| 2024 | CXXBaseOrMemberInitializer *Init = Inits[InitIndex]; |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2025 | void *InitKey = GetKeyForMember(SemaRef.Context, Init); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2026 | |
| 2027 | // Scan forward to try to find this initializer in the idealized |
| 2028 | // initializers list. |
| 2029 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2030 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2031 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2032 | |
| 2033 | // If we didn't find this initializer, it must be because we |
| 2034 | // scanned past it on a previous iteration. That can only |
| 2035 | // happen if we're out of order; emit a warning. |
Douglas Gregor | fe2d379 | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 2036 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2037 | Sema::SemaDiagnosticBuilder D = |
| 2038 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 2039 | diag::warn_initializer_out_of_order); |
| 2040 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2041 | if (PrevInit->isAnyMemberInitializer()) |
| 2042 | D << 0 << PrevInit->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2043 | else |
| 2044 | D << 1 << PrevInit->getBaseClassInfo()->getType(); |
| 2045 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2046 | if (Init->isAnyMemberInitializer()) |
| 2047 | D << 0 << Init->getAnyMember()->getDeclName(); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2048 | else |
| 2049 | D << 1 << Init->getBaseClassInfo()->getType(); |
| 2050 | |
| 2051 | // Move back to the initializer's location in the ideal list. |
| 2052 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2053 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2054 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2055 | |
| 2056 | assert(IdealIndex != NumIdealInits && |
| 2057 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2058 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2059 | |
| 2060 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2061 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2064 | namespace { |
| 2065 | bool CheckRedundantInit(Sema &S, |
| 2066 | CXXBaseOrMemberInitializer *Init, |
| 2067 | CXXBaseOrMemberInitializer *&PrevInit) { |
| 2068 | if (!PrevInit) { |
| 2069 | PrevInit = Init; |
| 2070 | return false; |
| 2071 | } |
| 2072 | |
| 2073 | if (FieldDecl *Field = Init->getMember()) |
| 2074 | S.Diag(Init->getSourceLocation(), |
| 2075 | diag::err_multiple_mem_initialization) |
| 2076 | << Field->getDeclName() |
| 2077 | << Init->getSourceRange(); |
| 2078 | else { |
| 2079 | Type *BaseClass = Init->getBaseClass(); |
| 2080 | assert(BaseClass && "neither field nor base"); |
| 2081 | S.Diag(Init->getSourceLocation(), |
| 2082 | diag::err_multiple_base_initialization) |
| 2083 | << QualType(BaseClass, 0) |
| 2084 | << Init->getSourceRange(); |
| 2085 | } |
| 2086 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 2087 | << 0 << PrevInit->getSourceRange(); |
| 2088 | |
| 2089 | return true; |
| 2090 | } |
| 2091 | |
| 2092 | typedef std::pair<NamedDecl *, CXXBaseOrMemberInitializer *> UnionEntry; |
| 2093 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 2094 | |
| 2095 | bool CheckRedundantUnionInit(Sema &S, |
| 2096 | CXXBaseOrMemberInitializer *Init, |
| 2097 | RedundantUnionMap &Unions) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2098 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2099 | RecordDecl *Parent = Field->getParent(); |
| 2100 | if (!Parent->isAnonymousStructOrUnion()) |
| 2101 | return false; |
| 2102 | |
| 2103 | NamedDecl *Child = Field; |
| 2104 | do { |
| 2105 | if (Parent->isUnion()) { |
| 2106 | UnionEntry &En = Unions[Parent]; |
| 2107 | if (En.first && En.first != Child) { |
| 2108 | S.Diag(Init->getSourceLocation(), |
| 2109 | diag::err_multiple_mem_union_initialization) |
| 2110 | << Field->getDeclName() |
| 2111 | << Init->getSourceRange(); |
| 2112 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 2113 | << 0 << En.second->getSourceRange(); |
| 2114 | return true; |
| 2115 | } else if (!En.first) { |
| 2116 | En.first = Child; |
| 2117 | En.second = Init; |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | Child = Parent; |
| 2122 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 2123 | } while (Parent->isAnonymousStructOrUnion()); |
| 2124 | |
| 2125 | return false; |
| 2126 | } |
| 2127 | } |
| 2128 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2129 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2130 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2131 | SourceLocation ColonLoc, |
| 2132 | MemInitTy **meminits, unsigned NumMemInits, |
| 2133 | bool AnyErrors) { |
| 2134 | if (!ConstructorDecl) |
| 2135 | return; |
| 2136 | |
| 2137 | AdjustDeclIfTemplate(ConstructorDecl); |
| 2138 | |
| 2139 | CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2140 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2141 | |
| 2142 | if (!Constructor) { |
| 2143 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 2144 | return; |
| 2145 | } |
| 2146 | |
| 2147 | CXXBaseOrMemberInitializer **MemInits = |
| 2148 | reinterpret_cast<CXXBaseOrMemberInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2149 | |
| 2150 | // Mapping for the duplicate initializers check. |
| 2151 | // For member initializers, this is keyed with a FieldDecl*. |
| 2152 | // For base initializers, this is keyed with a Type*. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2153 | llvm::DenseMap<void*, CXXBaseOrMemberInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2154 | |
| 2155 | // Mapping for the inconsistent anonymous-union initializers check. |
| 2156 | RedundantUnionMap MemberUnions; |
| 2157 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2158 | bool HadError = false; |
| 2159 | for (unsigned i = 0; i < NumMemInits; i++) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2160 | CXXBaseOrMemberInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2161 | |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 2162 | // Set the source order index. |
| 2163 | Init->setSourceOrder(i); |
| 2164 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2165 | if (Init->isAnyMemberInitializer()) { |
| 2166 | FieldDecl *Field = Init->getAnyMember(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2167 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 2168 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 2169 | HadError = true; |
| 2170 | } else { |
| 2171 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 2172 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 2173 | HadError = true; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2174 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2177 | if (HadError) |
| 2178 | return; |
| 2179 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2180 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2181 | |
| 2182 | SetBaseOrMemberInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2185 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2186 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 2187 | CXXRecordDecl *ClassDecl) { |
| 2188 | // Ignore dependent contexts. |
| 2189 | if (ClassDecl->isDependentContext()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2190 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2191 | |
| 2192 | // FIXME: all the access-control diagnostics are positioned on the |
| 2193 | // field/base declaration. That's probably good; that said, the |
| 2194 | // user might reasonably want to know why the destructor is being |
| 2195 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2196 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2197 | // Non-static data members. |
| 2198 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 2199 | E = ClassDecl->field_end(); I != E; ++I) { |
| 2200 | FieldDecl *Field = *I; |
Fariborz Jahanian | 9614dc0 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 2201 | if (Field->isInvalidDecl()) |
| 2202 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2203 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 2204 | |
| 2205 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 2206 | if (!RT) |
| 2207 | continue; |
| 2208 | |
| 2209 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 2210 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2211 | continue; |
| 2212 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2213 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2214 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2215 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2216 | << Field->getDeclName() |
| 2217 | << FieldType); |
| 2218 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2219 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2222 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 2223 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2224 | // Bases. |
| 2225 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2226 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2227 | // Bases are always records in a well-formed non-dependent class. |
| 2228 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 2229 | |
| 2230 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2231 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2232 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2233 | |
| 2234 | // Ignore trivial destructors. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2235 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2236 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2237 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2238 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2239 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2240 | |
| 2241 | // FIXME: caret should be on the start of the class name |
| 2242 | CheckDestructorAccess(Base->getSourceRange().getBegin(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2243 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2244 | << Base->getType() |
| 2245 | << Base->getSourceRange()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2246 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2247 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2251 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2252 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2253 | |
| 2254 | // Bases are always records in a well-formed non-dependent class. |
| 2255 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 2256 | |
| 2257 | // Ignore direct virtual bases. |
| 2258 | if (DirectVirtualBases.count(RT)) |
| 2259 | continue; |
| 2260 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2261 | // Ignore trivial destructors. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2262 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2263 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2264 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2265 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2266 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2267 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2268 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2269 | << VBase->getType()); |
| 2270 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2271 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2272 | } |
| 2273 | } |
| 2274 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2275 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2276 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2277 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2279 | if (CXXConstructorDecl *Constructor |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2280 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2281 | SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2285 | unsigned DiagID, AbstractDiagSelID SelID) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2286 | if (SelID == -1) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2287 | return RequireNonAbstractType(Loc, T, PDiag(DiagID)); |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2288 | else |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2289 | return RequireNonAbstractType(Loc, T, PDiag(DiagID) << SelID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2292 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2293 | const PartialDiagnostic &PD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2294 | if (!getLangOptions().CPlusPlus) |
| 2295 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 2297 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2298 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2299 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2300 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2301 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2302 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2303 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2304 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2305 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2306 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2307 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2308 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2309 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2310 | if (!RT) |
| 2311 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2313 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2314 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2315 | // We can't answer whether something is abstract until it has a |
| 2316 | // definition. If it's currently being defined, we'll walk back |
| 2317 | // over all the declarations when we have a full definition. |
| 2318 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 2319 | if (!Def || Def->isBeingDefined()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2320 | return false; |
| 2321 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2322 | if (!RD->isAbstract()) |
| 2323 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2325 | Diag(Loc, PD) << RD->getDeclName(); |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2326 | DiagnoseAbstractType(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2327 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2328 | return true; |
| 2329 | } |
| 2330 | |
| 2331 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 2332 | // Check if we've already emitted the list of pure virtual functions |
| 2333 | // for this class. |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2334 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2335 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2336 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2337 | CXXFinalOverriderMap FinalOverriders; |
| 2338 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2339 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2340 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 2341 | // more than once. |
| 2342 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 2343 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2344 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2345 | MEnd = FinalOverriders.end(); |
| 2346 | M != MEnd; |
| 2347 | ++M) { |
| 2348 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2349 | SOEnd = M->second.end(); |
| 2350 | SO != SOEnd; ++SO) { |
| 2351 | // C++ [class.abstract]p4: |
| 2352 | // A class is abstract if it contains or inherits at least one |
| 2353 | // pure virtual function for which the final overrider is pure |
| 2354 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2356 | // |
| 2357 | if (SO->second.size() != 1) |
| 2358 | continue; |
| 2359 | |
| 2360 | if (!SO->second.front().Method->isPure()) |
| 2361 | continue; |
| 2362 | |
Anders Carlsson | ffdb2d2 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2363 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 2364 | continue; |
| 2365 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2366 | Diag(SO->second.front().Method->getLocation(), |
| 2367 | diag::note_pure_virtual_function) |
| 2368 | << SO->second.front().Method->getDeclName(); |
| 2369 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | if (!PureVirtualClassDiagSet) |
| 2373 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 2374 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2377 | namespace { |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2378 | struct AbstractUsageInfo { |
| 2379 | Sema &S; |
| 2380 | CXXRecordDecl *Record; |
| 2381 | CanQualType AbstractType; |
| 2382 | bool Invalid; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2383 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2384 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 2385 | : S(S), Record(Record), |
| 2386 | AbstractType(S.Context.getCanonicalType( |
| 2387 | S.Context.getTypeDeclType(Record))), |
| 2388 | Invalid(false) {} |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2389 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2390 | void DiagnoseAbstractType() { |
| 2391 | if (Invalid) return; |
| 2392 | S.DiagnoseAbstractType(Record); |
| 2393 | Invalid = true; |
| 2394 | } |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2395 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2396 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 2397 | }; |
| 2398 | |
| 2399 | struct CheckAbstractUsage { |
| 2400 | AbstractUsageInfo &Info; |
| 2401 | const NamedDecl *Ctx; |
| 2402 | |
| 2403 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 2404 | : Info(Info), Ctx(Ctx) {} |
| 2405 | |
| 2406 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2407 | switch (TL.getTypeLocClass()) { |
| 2408 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 2409 | #define TYPELOC(CLASS, PARENT) \ |
| 2410 | case TypeLoc::CLASS: Check(cast<CLASS##TypeLoc>(TL), Sel); break; |
| 2411 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2412 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2413 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2414 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2415 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2416 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 2417 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2418 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 2419 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2420 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2421 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2422 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2423 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2424 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 2425 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2426 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2427 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2428 | // Visit the type parameters from a permissive context. |
| 2429 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2430 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 2431 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 2432 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 2433 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 2434 | // TODO: other template argument types? |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2435 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2436 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2437 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2438 | // Visit pointee types from a permissive context. |
| 2439 | #define CheckPolymorphic(Type) \ |
| 2440 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 2441 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 2442 | } |
| 2443 | CheckPolymorphic(PointerTypeLoc) |
| 2444 | CheckPolymorphic(ReferenceTypeLoc) |
| 2445 | CheckPolymorphic(MemberPointerTypeLoc) |
| 2446 | CheckPolymorphic(BlockPointerTypeLoc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2448 | /// Handle all the types we haven't given a more specific |
| 2449 | /// implementation for above. |
| 2450 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2451 | // Every other kind of type that we haven't called out already |
| 2452 | // that has an inner type is either (1) sugar or (2) contains that |
| 2453 | // inner type in some way as a subobject. |
| 2454 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 2455 | return Visit(Next, Sel); |
| 2456 | |
| 2457 | // If there's no inner type and we're in a permissive context, |
| 2458 | // don't diagnose. |
| 2459 | if (Sel == Sema::AbstractNone) return; |
| 2460 | |
| 2461 | // Check whether the type matches the abstract type. |
| 2462 | QualType T = TL.getType(); |
| 2463 | if (T->isArrayType()) { |
| 2464 | Sel = Sema::AbstractArrayType; |
| 2465 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2466 | } |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2467 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 2468 | if (CT != Info.AbstractType) return; |
| 2469 | |
| 2470 | // It matched; do some magic. |
| 2471 | if (Sel == Sema::AbstractArrayType) { |
| 2472 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 2473 | << T << TL.getSourceRange(); |
| 2474 | } else { |
| 2475 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 2476 | << Sel << T << TL.getSourceRange(); |
| 2477 | } |
| 2478 | Info.DiagnoseAbstractType(); |
| 2479 | } |
| 2480 | }; |
| 2481 | |
| 2482 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 2483 | Sema::AbstractDiagSelID Sel) { |
| 2484 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 2485 | } |
| 2486 | |
| 2487 | } |
| 2488 | |
| 2489 | /// Check for invalid uses of an abstract type in a method declaration. |
| 2490 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2491 | CXXMethodDecl *MD) { |
| 2492 | // No need to do the check on definitions, which require that |
| 2493 | // the return/param types be complete. |
| 2494 | if (MD->isThisDeclarationADefinition()) |
| 2495 | return; |
| 2496 | |
| 2497 | // For safety's sake, just ignore it if we don't have type source |
| 2498 | // information. This should never happen for non-implicit methods, |
| 2499 | // but... |
| 2500 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 2501 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 2502 | } |
| 2503 | |
| 2504 | /// Check for invalid uses of an abstract type within a class definition. |
| 2505 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2506 | CXXRecordDecl *RD) { |
| 2507 | for (CXXRecordDecl::decl_iterator |
| 2508 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 2509 | Decl *D = *I; |
| 2510 | if (D->isImplicit()) continue; |
| 2511 | |
| 2512 | // Methods and method templates. |
| 2513 | if (isa<CXXMethodDecl>(D)) { |
| 2514 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 2515 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 2516 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 2517 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 2518 | |
| 2519 | // Fields and static variables. |
| 2520 | } else if (isa<FieldDecl>(D)) { |
| 2521 | FieldDecl *FD = cast<FieldDecl>(D); |
| 2522 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 2523 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 2524 | } else if (isa<VarDecl>(D)) { |
| 2525 | VarDecl *VD = cast<VarDecl>(D); |
| 2526 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 2527 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 2528 | |
| 2529 | // Nested classes and class templates. |
| 2530 | } else if (isa<CXXRecordDecl>(D)) { |
| 2531 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 2532 | } else if (isa<ClassTemplateDecl>(D)) { |
| 2533 | CheckAbstractClassUsage(Info, |
| 2534 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 2535 | } |
| 2536 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2539 | /// \brief Perform semantic checks on a class definition that has been |
| 2540 | /// completing, introducing implicitly-declared members, checking for |
| 2541 | /// abstract types, etc. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2542 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 2543 | if (!Record) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2544 | return; |
| 2545 | |
John McCall | 94c3b56 | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2546 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 2547 | AbstractUsageInfo Info(*this, Record); |
| 2548 | CheckAbstractClassUsage(Info, Record); |
| 2549 | } |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2550 | |
| 2551 | // If this is not an aggregate type and has no user-declared constructor, |
| 2552 | // complain about any non-static data members of reference or const scalar |
| 2553 | // type, since they will never get initializers. |
| 2554 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 2555 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor()) { |
| 2556 | bool Complained = false; |
| 2557 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 2558 | FEnd = Record->field_end(); |
| 2559 | F != FEnd; ++F) { |
| 2560 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 2561 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2562 | if (!Complained) { |
| 2563 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 2564 | << Record->getTagKind() << Record; |
| 2565 | Complained = true; |
| 2566 | } |
| 2567 | |
| 2568 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 2569 | << F->getType()->isReferenceType() |
| 2570 | << F->getDeclName(); |
| 2571 | } |
| 2572 | } |
| 2573 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2574 | |
| 2575 | if (Record->isDynamicClass()) |
| 2576 | DynamicClasses.push_back(Record); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 2577 | |
| 2578 | if (Record->getIdentifier()) { |
| 2579 | // C++ [class.mem]p13: |
| 2580 | // If T is the name of a class, then each of the following shall have a |
| 2581 | // name different from T: |
| 2582 | // - every member of every anonymous union that is a member of class T. |
| 2583 | // |
| 2584 | // C++ [class.mem]p14: |
| 2585 | // In addition, if class T has a user-declared constructor (12.1), every |
| 2586 | // non-static data member of class T shall have a name different from T. |
| 2587 | for (DeclContext::lookup_result R = Record->lookup(Record->getDeclName()); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2588 | R.first != R.second; ++R.first) { |
| 2589 | NamedDecl *D = *R.first; |
| 2590 | if ((isa<FieldDecl>(D) && Record->hasUserDeclaredConstructor()) || |
| 2591 | isa<IndirectFieldDecl>(D)) { |
| 2592 | Diag(D->getLocation(), diag::err_member_name_of_class) |
| 2593 | << D->getDeclName(); |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 2594 | break; |
| 2595 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2596 | } |
Douglas Gregor | a6e937c | 2010-10-15 13:21:21 +0000 | [diff] [blame] | 2597 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2600 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2601 | Decl *TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2602 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2603 | SourceLocation RBrac, |
| 2604 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2605 | if (!TagDecl) |
| 2606 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2607 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2608 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2609 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2610 | ActOnFields(S, RLoc, TagDecl, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2611 | // strict aliasing violation! |
| 2612 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2613 | FieldCollector->getCurNumFields(), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2614 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2615 | CheckCompletedCXXClass( |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2616 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2619 | namespace { |
| 2620 | /// \brief Helper class that collects exception specifications for |
| 2621 | /// implicitly-declared special member functions. |
| 2622 | class ImplicitExceptionSpecification { |
| 2623 | ASTContext &Context; |
| 2624 | bool AllowsAllExceptions; |
| 2625 | llvm::SmallPtrSet<CanQualType, 4> ExceptionsSeen; |
| 2626 | llvm::SmallVector<QualType, 4> Exceptions; |
| 2627 | |
| 2628 | public: |
| 2629 | explicit ImplicitExceptionSpecification(ASTContext &Context) |
| 2630 | : Context(Context), AllowsAllExceptions(false) { } |
| 2631 | |
| 2632 | /// \brief Whether the special member function should have any |
| 2633 | /// exception specification at all. |
| 2634 | bool hasExceptionSpecification() const { |
| 2635 | return !AllowsAllExceptions; |
| 2636 | } |
| 2637 | |
| 2638 | /// \brief Whether the special member function should have a |
| 2639 | /// throw(...) exception specification (a Microsoft extension). |
| 2640 | bool hasAnyExceptionSpecification() const { |
| 2641 | return false; |
| 2642 | } |
| 2643 | |
| 2644 | /// \brief The number of exceptions in the exception specification. |
| 2645 | unsigned size() const { return Exceptions.size(); } |
| 2646 | |
| 2647 | /// \brief The set of exceptions in the exception specification. |
| 2648 | const QualType *data() const { return Exceptions.data(); } |
| 2649 | |
| 2650 | /// \brief Note that |
| 2651 | void CalledDecl(CXXMethodDecl *Method) { |
| 2652 | // If we already know that we allow all exceptions, do nothing. |
Douglas Gregor | 4681ca8 | 2010-07-01 15:29:53 +0000 | [diff] [blame] | 2653 | if (AllowsAllExceptions || !Method) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2654 | return; |
| 2655 | |
| 2656 | const FunctionProtoType *Proto |
| 2657 | = Method->getType()->getAs<FunctionProtoType>(); |
| 2658 | |
| 2659 | // If this function can throw any exceptions, make a note of that. |
| 2660 | if (!Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec()) { |
| 2661 | AllowsAllExceptions = true; |
| 2662 | ExceptionsSeen.clear(); |
| 2663 | Exceptions.clear(); |
| 2664 | return; |
| 2665 | } |
| 2666 | |
| 2667 | // Record the exceptions in this function's exception specification. |
| 2668 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 2669 | EEnd = Proto->exception_end(); |
| 2670 | E != EEnd; ++E) |
| 2671 | if (ExceptionsSeen.insert(Context.getCanonicalType(*E))) |
| 2672 | Exceptions.push_back(*E); |
| 2673 | } |
| 2674 | }; |
| 2675 | } |
| 2676 | |
| 2677 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2678 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 2679 | /// special functions, such as the default constructor, copy |
| 2680 | /// constructor, or destructor, to the given C++ class (C++ |
| 2681 | /// [special]p1). This routine can only be executed just before the |
| 2682 | /// definition of the class is complete. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2683 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 2684 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 2685 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2686 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 2687 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 2688 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2689 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 2690 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 2691 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
| 2692 | |
| 2693 | // If we have a dynamic class, then the copy assignment operator may be |
| 2694 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
| 2695 | // it shows up in the right place in the vtable and that we diagnose |
| 2696 | // problems with the implicit exception specification. |
| 2697 | if (ClassDecl->isDynamicClass()) |
| 2698 | DeclareImplicitCopyAssignment(ClassDecl); |
| 2699 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2700 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 2701 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 2702 | ++ASTContext::NumImplicitDestructors; |
| 2703 | |
| 2704 | // If we have a dynamic class, then the destructor may be virtual, so we |
| 2705 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 2706 | // shows up in the right place in the vtable and that we diagnose problems |
| 2707 | // with the implicit exception specification. |
| 2708 | if (ClassDecl->isDynamicClass()) |
| 2709 | DeclareImplicitDestructor(ClassDecl); |
| 2710 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2713 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 2714 | if (!D) |
| 2715 | return; |
| 2716 | |
| 2717 | TemplateParameterList *Params = 0; |
| 2718 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 2719 | Params = Template->getTemplateParameters(); |
| 2720 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2721 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 2722 | Params = PartialSpec->getTemplateParameters(); |
| 2723 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2724 | return; |
| 2725 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2726 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2727 | ParamEnd = Params->end(); |
| 2728 | Param != ParamEnd; ++Param) { |
| 2729 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 2730 | if (Named->getDeclName()) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2731 | S->AddDecl(Named); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2732 | IdResolver.AddDecl(Named); |
| 2733 | } |
| 2734 | } |
| 2735 | } |
| 2736 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2737 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2738 | if (!RecordD) return; |
| 2739 | AdjustDeclIfTemplate(RecordD); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2740 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2741 | PushDeclContext(S, Record); |
| 2742 | } |
| 2743 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2744 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2745 | if (!RecordD) return; |
| 2746 | PopDeclContext(); |
| 2747 | } |
| 2748 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2749 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 2750 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 2751 | /// parsing those parts of the given Method declaration that could |
| 2752 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 2753 | /// arguments. This action should enter the scope of the given |
| 2754 | /// Method declaration as if we had just parsed the qualified method |
| 2755 | /// name. However, it should not bring the parameters into scope; |
| 2756 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2757 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 2761 | /// C++ method declaration. We're (re-)introducing the given |
| 2762 | /// function parameter into scope for use in parsing later parts of |
| 2763 | /// the method declaration. For example, we could see an |
| 2764 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2765 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2766 | if (!ParamD) |
| 2767 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2768 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2769 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2770 | |
| 2771 | // If this parameter has an unparsed default argument, clear it out |
| 2772 | // to make way for the parsed default argument. |
| 2773 | if (Param->hasUnparsedDefaultArg()) |
| 2774 | Param->setDefaultArg(0); |
| 2775 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2776 | S->AddDecl(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2777 | if (Param->getDeclName()) |
| 2778 | IdResolver.AddDecl(Param); |
| 2779 | } |
| 2780 | |
| 2781 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 2782 | /// processing the delayed method declaration for Method. The method |
| 2783 | /// declaration is now considered finished. There may be a separate |
| 2784 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 2785 | /// immediately!) for this method, if it was also defined inside the |
| 2786 | /// class body. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2787 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2788 | if (!MethodD) |
| 2789 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2791 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2792 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2793 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2794 | |
| 2795 | // Now that we have our default arguments, check the constructor |
| 2796 | // again. It could produce additional diagnostics or affect whether |
| 2797 | // the class has implicitly-declared destructors, among other |
| 2798 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2799 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 2800 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2801 | |
| 2802 | // Check the default arguments, which we may have added. |
| 2803 | if (!Method->isInvalidDecl()) |
| 2804 | CheckCXXDefaultArguments(Method); |
| 2805 | } |
| 2806 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2807 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2808 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2809 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2810 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 2811 | /// will be updated to reflect a well-formed type for the constructor and |
| 2812 | /// returned. |
| 2813 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2814 | StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2815 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2816 | |
| 2817 | // C++ [class.ctor]p3: |
| 2818 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 2819 | // constructor can be invoked for a const, volatile or const |
| 2820 | // volatile object. A constructor shall not be declared const, |
| 2821 | // volatile, or const volatile (9.3.2). |
| 2822 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2823 | if (!D.isInvalidType()) |
| 2824 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2825 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 2826 | << SourceRange(D.getIdentifierLoc()); |
| 2827 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2828 | } |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2829 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2830 | if (!D.isInvalidType()) |
| 2831 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2832 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2833 | << SourceRange(D.getIdentifierLoc()); |
| 2834 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2835 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2836 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2837 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2838 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2839 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2840 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2841 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2842 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2843 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2844 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2845 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2846 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2847 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2848 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2849 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2851 | // Rebuild the function type "R" without any type qualifiers (in |
| 2852 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2853 | // return type, since constructors don't have return types. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2854 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2855 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 2856 | Proto->getNumArgs(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2857 | Proto->isVariadic(), 0, |
| 2858 | Proto->hasExceptionSpec(), |
| 2859 | Proto->hasAnyExceptionSpec(), |
| 2860 | Proto->getNumExceptions(), |
| 2861 | Proto->exception_begin(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2862 | Proto->getExtInfo()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2865 | /// CheckConstructor - Checks a fully-formed constructor for |
| 2866 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 2867 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2868 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2869 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2870 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 2871 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2872 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2873 | |
| 2874 | // C++ [class.copy]p3: |
| 2875 | // A declaration of a constructor for a class X is ill-formed if |
| 2876 | // its first parameter is of type (optionally cv-qualified) X and |
| 2877 | // either there are no other parameters or else all other |
| 2878 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2879 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2880 | ((Constructor->getNumParams() == 1) || |
| 2881 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2882 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 2883 | Constructor->getTemplateSpecializationKind() |
| 2884 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2885 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 2886 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 2887 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2888 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 2889 | const char *ConstRef |
| 2890 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 2891 | : " const &"; |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2892 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | aeb4a28 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 2893 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2894 | |
| 2895 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 2896 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2897 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2898 | } |
| 2899 | } |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
John McCall | 1544282 | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 2902 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 2903 | /// well-formedness, issuing any diagnostics required. Returns true |
| 2904 | /// on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2905 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2906 | CXXRecordDecl *RD = Destructor->getParent(); |
| 2907 | |
| 2908 | if (Destructor->isVirtual()) { |
| 2909 | SourceLocation Loc; |
| 2910 | |
| 2911 | if (!Destructor->isImplicit()) |
| 2912 | Loc = Destructor->getLocation(); |
| 2913 | else |
| 2914 | Loc = RD->getLocation(); |
| 2915 | |
| 2916 | // If we have a virtual destructor, look up the deallocation function |
| 2917 | FunctionDecl *OperatorDelete = 0; |
| 2918 | DeclarationName Name = |
| 2919 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2920 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2921 | return true; |
John McCall | 5efd91a | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 2922 | |
| 2923 | MarkDeclarationReferenced(Loc, OperatorDelete); |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2924 | |
| 2925 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2926 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2927 | |
| 2928 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2932 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 2933 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 2934 | FTI.ArgInfo[0].Param && |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2935 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2938 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 2939 | /// the well-formednes of the destructor declarator @p D with type @p |
| 2940 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2941 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 2942 | /// will be updated to reflect a well-formed type for the destructor and |
| 2943 | /// returned. |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2944 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2945 | StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2946 | // C++ [class.dtor]p1: |
| 2947 | // [...] A typedef-name that names a class is a class-name |
| 2948 | // (7.1.3); however, a typedef-name that names a class shall not |
| 2949 | // be used as the identifier in the declarator for a destructor |
| 2950 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2951 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2952 | if (isa<TypedefType>(DeclaratorType)) |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2953 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2954 | << DeclaratorType; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2955 | |
| 2956 | // C++ [class.dtor]p2: |
| 2957 | // A destructor is used to destroy objects of its class type. A |
| 2958 | // destructor takes no parameters, and no return type can be |
| 2959 | // specified for it (not even void). The address of a destructor |
| 2960 | // shall not be taken. A destructor shall not be static. A |
| 2961 | // destructor can be invoked for a const, volatile or const |
| 2962 | // volatile object. A destructor shall not be declared const, |
| 2963 | // volatile or const volatile (9.3.2). |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2964 | if (SC == SC_Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2965 | if (!D.isInvalidType()) |
| 2966 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 2967 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2968 | << SourceRange(D.getIdentifierLoc()) |
| 2969 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 2970 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2971 | SC = SC_None; |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2972 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2973 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2974 | // Destructors don't have return types, but the parser will |
| 2975 | // happily parse something like: |
| 2976 | // |
| 2977 | // class X { |
| 2978 | // float ~X(); |
| 2979 | // }; |
| 2980 | // |
| 2981 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2982 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 2983 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2984 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2985 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2986 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2987 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2988 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2989 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2990 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2991 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2992 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2993 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2994 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2995 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2996 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2997 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2998 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 3002 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3003 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 3004 | |
| 3005 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3006 | FTI.freeArgs(); |
| 3007 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3010 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3011 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3012 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 3013 | D.setInvalidType(); |
| 3014 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3015 | |
| 3016 | // Rebuild the function type "R" without any type qualifiers or |
| 3017 | // parameters (in case any of the errors above fired) and with |
| 3018 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3019 | // types. |
| 3020 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 3021 | if (!Proto) |
| 3022 | return QualType(); |
| 3023 | |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 3024 | return Context.getFunctionType(Context.VoidTy, 0, 0, false, 0, |
Douglas Gregor | d92ec47 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 3025 | Proto->hasExceptionSpec(), |
| 3026 | Proto->hasAnyExceptionSpec(), |
| 3027 | Proto->getNumExceptions(), |
| 3028 | Proto->exception_begin(), |
| 3029 | Proto->getExtInfo()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3032 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 3033 | /// well-formednes of the conversion function declarator @p D with |
| 3034 | /// type @p R. If there are any errors in the declarator, this routine |
| 3035 | /// will emit diagnostics and return true. Otherwise, it will return |
| 3036 | /// false. Either way, the type @p R will be updated to reflect a |
| 3037 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3038 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3039 | StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3040 | // C++ [class.conv.fct]p1: |
| 3041 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3042 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | // parameter returning conversion-type-id." |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3044 | if (SC == SC_Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3045 | if (!D.isInvalidType()) |
| 3046 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 3047 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 3048 | << SourceRange(D.getIdentifierLoc()); |
| 3049 | D.setInvalidType(); |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 3050 | SC = SC_None; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3051 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3052 | |
| 3053 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 3054 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3055 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3056 | // Conversion functions don't have return types, but the parser will |
| 3057 | // happily parse something like: |
| 3058 | // |
| 3059 | // class X { |
| 3060 | // float operator bool(); |
| 3061 | // }; |
| 3062 | // |
| 3063 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3064 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 3065 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 3066 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3067 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3070 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 3071 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3072 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3073 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3074 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 3075 | |
| 3076 | // Delete the parameters. |
Chris Lattner | 1833a83 | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 3077 | D.getTypeObject(0).Fun.freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3078 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3079 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3080 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3081 | D.setInvalidType(); |
| 3082 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3083 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3084 | // Diagnose "&operator bool()" and other such nonsense. This |
| 3085 | // is actually a gcc extension which we don't support. |
| 3086 | if (Proto->getResultType() != ConvType) { |
| 3087 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 3088 | << Proto->getResultType(); |
| 3089 | D.setInvalidType(); |
| 3090 | ConvType = Proto->getResultType(); |
| 3091 | } |
| 3092 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3093 | // C++ [class.conv.fct]p4: |
| 3094 | // The conversion-type-id shall not represent a function type nor |
| 3095 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3096 | if (ConvType->isArrayType()) { |
| 3097 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 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 | } else if (ConvType->isFunctionType()) { |
| 3101 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 3102 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3103 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | // Rebuild the function type "R" without any parameters (in case any |
| 3107 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3108 | // return type. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3109 | if (D.isInvalidType()) { |
| 3110 | R = Context.getFunctionType(ConvType, 0, 0, false, |
| 3111 | Proto->getTypeQuals(), |
| 3112 | Proto->hasExceptionSpec(), |
| 3113 | Proto->hasAnyExceptionSpec(), |
| 3114 | Proto->getNumExceptions(), |
| 3115 | Proto->exception_begin(), |
| 3116 | Proto->getExtInfo()); |
| 3117 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3118 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3119 | // C++0x explicit conversion operators. |
| 3120 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3121 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3122 | diag::warn_explicit_conversion_functions) |
| 3123 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3126 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 3127 | /// the declaration of the given C++ conversion function. This routine |
| 3128 | /// is responsible for recording the conversion function in the C++ |
| 3129 | /// class, if possible. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3130 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3131 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 3132 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 3133 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3134 | |
| 3135 | // Make sure we aren't redeclaring the conversion function. |
| 3136 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3137 | |
| 3138 | // C++ [class.conv.fct]p1: |
| 3139 | // [...] A conversion function is never used to convert a |
| 3140 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 3141 | // same object type (or a reference to it), to a (possibly |
| 3142 | // cv-qualified) base class of that type (or a reference to it), |
| 3143 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3144 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 3145 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3146 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3147 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3148 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3149 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 3150 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 3151 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | 1034170 | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 3152 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 3153 | else if (ConvType->isRecordType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3154 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 3155 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3156 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3157 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3158 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3159 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3160 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3161 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3162 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3163 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 3166 | if (FunctionTemplateDecl *ConversionTemplate |
| 3167 | = Conversion->getDescribedFunctionTemplate()) |
| 3168 | return ConversionTemplate; |
| 3169 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3170 | return Conversion; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3173 | //===----------------------------------------------------------------------===// |
| 3174 | // Namespace Handling |
| 3175 | //===----------------------------------------------------------------------===// |
| 3176 | |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3177 | |
| 3178 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3179 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 3180 | /// definition. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3181 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 3182 | SourceLocation InlineLoc, |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3183 | SourceLocation IdentLoc, |
| 3184 | IdentifierInfo *II, |
| 3185 | SourceLocation LBrace, |
| 3186 | AttributeList *AttrList) { |
Douglas Gregor | 21e09b6 | 2010-08-19 20:55:47 +0000 | [diff] [blame] | 3187 | // anonymous namespace starts at its left brace |
| 3188 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, |
| 3189 | (II ? IdentLoc : LBrace) , II); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3190 | Namespc->setLBracLoc(LBrace); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3191 | Namespc->setInline(InlineLoc.isValid()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3192 | |
| 3193 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 3194 | |
Anders Carlsson | 2a3503d | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 3195 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 3196 | |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3197 | if (const VisibilityAttr *attr = Namespc->getAttr<VisibilityAttr>()) |
John McCall | ea31864 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3198 | PushVisibilityAttr(attr); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3199 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3200 | if (II) { |
| 3201 | // C++ [namespace.def]p2: |
Douglas Gregor | fe7574b | 2010-10-22 15:24:46 +0000 | [diff] [blame] | 3202 | // The identifier in an original-namespace-definition shall not |
| 3203 | // have been previously defined in the declarative region in |
| 3204 | // which the original-namespace-definition appears. The |
| 3205 | // identifier in an original-namespace-definition is the name of |
| 3206 | // the namespace. Subsequently in that declarative region, it is |
| 3207 | // treated as an original-namespace-name. |
| 3208 | // |
| 3209 | // Since namespace names are unique in their scope, and we don't |
| 3210 | // look through using directives, just |
| 3211 | DeclContext::lookup_result R = CurContext->getRedeclContext()->lookup(II); |
| 3212 | NamedDecl *PrevDecl = R.first == R.second? 0 : *R.first; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3213 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3214 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 3215 | // This is an extended namespace definition. |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3216 | if (Namespc->isInline() != OrigNS->isInline()) { |
| 3217 | // inline-ness must match |
| 3218 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 3219 | << Namespc->isInline(); |
| 3220 | Diag(OrigNS->getLocation(), diag::note_previous_definition); |
| 3221 | Namespc->setInvalidDecl(); |
| 3222 | // Recover by ignoring the new namespace's inline status. |
| 3223 | Namespc->setInline(OrigNS->isInline()); |
| 3224 | } |
| 3225 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3226 | // Attach this namespace decl to the chain of extended namespace |
| 3227 | // definitions. |
| 3228 | OrigNS->setNextNamespace(Namespc); |
| 3229 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3230 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3231 | // Remove the previous declaration from the scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3232 | if (DeclRegionScope->isDeclScope(OrigNS)) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 3233 | IdResolver.RemoveDecl(OrigNS); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3234 | DeclRegionScope->RemoveDecl(OrigNS); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3235 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3236 | } else if (PrevDecl) { |
| 3237 | // This is an invalid name redefinition. |
| 3238 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 3239 | << Namespc->getDeclName(); |
| 3240 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3241 | Namespc->setInvalidDecl(); |
| 3242 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3243 | } else if (II->isStr("std") && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3244 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3245 | // This is the first "real" definition of the namespace "std", so update |
| 3246 | // our cache of the "std" namespace to point at this definition. |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3247 | if (NamespaceDecl *StdNS = getStdNamespace()) { |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3248 | // We had already defined a dummy namespace "std". Link this new |
| 3249 | // namespace definition to the dummy namespace "std". |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3250 | StdNS->setNextNamespace(Namespc); |
| 3251 | StdNS->setLocation(IdentLoc); |
| 3252 | Namespc->setOriginalNamespace(StdNS->getOriginalNamespace()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | // Make our StdNamespace cache point at the first real definition of the |
| 3256 | // "std" namespace. |
| 3257 | StdNamespace = Namespc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3258 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3259 | |
| 3260 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 3261 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3262 | // Anonymous namespaces. |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3263 | assert(Namespc->isAnonymousNamespace()); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3264 | |
| 3265 | // Link the anonymous namespace into its parent. |
| 3266 | NamespaceDecl *PrevDecl; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3267 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3268 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 3269 | PrevDecl = TU->getAnonymousNamespace(); |
| 3270 | TU->setAnonymousNamespace(Namespc); |
| 3271 | } else { |
| 3272 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 3273 | PrevDecl = ND->getAnonymousNamespace(); |
| 3274 | ND->setAnonymousNamespace(Namespc); |
| 3275 | } |
| 3276 | |
| 3277 | // Link the anonymous namespace with its previous declaration. |
| 3278 | if (PrevDecl) { |
| 3279 | assert(PrevDecl->isAnonymousNamespace()); |
| 3280 | assert(!PrevDecl->getNextNamespace()); |
| 3281 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 3282 | PrevDecl->setNextNamespace(Namespc); |
Sebastian Redl | 4e4d570 | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3283 | |
| 3284 | if (Namespc->isInline() != PrevDecl->isInline()) { |
| 3285 | // inline-ness must match |
| 3286 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 3287 | << Namespc->isInline(); |
| 3288 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3289 | Namespc->setInvalidDecl(); |
| 3290 | // Recover by ignoring the new namespace's inline status. |
| 3291 | Namespc->setInline(PrevDecl->isInline()); |
| 3292 | } |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3293 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3294 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 3295 | CurContext->addDecl(Namespc); |
| 3296 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3297 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 3298 | // behaves as if it were replaced by |
| 3299 | // namespace unique { /* empty body */ } |
| 3300 | // using namespace unique; |
| 3301 | // namespace unique { namespace-body } |
| 3302 | // where all occurrences of 'unique' in a translation unit are |
| 3303 | // replaced by the same identifier and this identifier differs |
| 3304 | // from all other identifiers in the entire program. |
| 3305 | |
| 3306 | // We just create the namespace with an empty name and then add an |
| 3307 | // implicit using declaration, just like the standard suggests. |
| 3308 | // |
| 3309 | // CodeGen enforces the "universally unique" aspect by giving all |
| 3310 | // declarations semantically contained within an anonymous |
| 3311 | // namespace internal linkage. |
| 3312 | |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3313 | if (!PrevDecl) { |
| 3314 | UsingDirectiveDecl* UD |
| 3315 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 3316 | /* 'using' */ LBrace, |
| 3317 | /* 'namespace' */ SourceLocation(), |
| 3318 | /* qualifier */ SourceRange(), |
| 3319 | /* NNS */ NULL, |
| 3320 | /* identifier */ SourceLocation(), |
| 3321 | Namespc, |
| 3322 | /* Ancestor */ CurContext); |
| 3323 | UD->setImplicit(); |
| 3324 | CurContext->addDecl(UD); |
| 3325 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 3329 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3330 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 3331 | // for the namespace has the declarations that showed up in that particular |
| 3332 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3333 | PushDeclContext(NamespcScope, Namespc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3334 | return Namespc; |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3337 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 3338 | /// is a namespace alias, returns the namespace it points to. |
| 3339 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 3340 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 3341 | return AD->getNamespace(); |
| 3342 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 3343 | } |
| 3344 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3345 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 3346 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3347 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3348 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 3349 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 3350 | Namespc->setRBracLoc(RBrace); |
| 3351 | PopDeclContext(); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3352 | if (Namespc->hasAttr<VisibilityAttr>()) |
| 3353 | PopPragmaVisibility(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3354 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3355 | |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 3356 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 3357 | return cast_or_null<CXXRecordDecl>( |
| 3358 | StdBadAlloc.get(Context.getExternalSource())); |
| 3359 | } |
| 3360 | |
| 3361 | NamespaceDecl *Sema::getStdNamespace() const { |
| 3362 | return cast_or_null<NamespaceDecl>( |
| 3363 | StdNamespace.get(Context.getExternalSource())); |
| 3364 | } |
| 3365 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3366 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 3367 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 3368 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3369 | if (!StdNamespace) { |
| 3370 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 3371 | StdNamespace = NamespaceDecl::Create(Context, |
| 3372 | Context.getTranslationUnitDecl(), |
| 3373 | SourceLocation(), |
| 3374 | &PP.getIdentifierTable().get("std")); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3375 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3378 | return getStdNamespace(); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3381 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3382 | SourceLocation UsingLoc, |
| 3383 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3384 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3385 | SourceLocation IdentLoc, |
| 3386 | IdentifierInfo *NamespcName, |
| 3387 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3388 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3389 | assert(NamespcName && "Invalid NamespcName."); |
| 3390 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 3391 | |
| 3392 | // This can only happen along a recovery path. |
| 3393 | while (S->getFlags() & Scope::TemplateParamScope) |
| 3394 | S = S->getParent(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3395 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3396 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3397 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3398 | NestedNameSpecifier *Qualifier = 0; |
| 3399 | if (SS.isSet()) |
| 3400 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3401 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 3402 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3403 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 3404 | LookupParsedName(R, S, &SS); |
| 3405 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3406 | return 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3407 | |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3408 | if (R.empty()) { |
| 3409 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 3410 | // "std" hasn't been defined yet, for GCC compatibility. |
| 3411 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 3412 | NamespcName->isStr("std")) { |
| 3413 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 3414 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3415 | R.resolveKind(); |
| 3416 | } |
| 3417 | // Otherwise, attempt typo correction. |
| 3418 | else if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 3419 | CTC_NoKeywords, 0)) { |
| 3420 | if (R.getAsSingle<NamespaceDecl>() || |
| 3421 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 3422 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 3423 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 3424 | << NamespcName << DC << Corrected << SS.getRange() |
| 3425 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 3426 | else |
| 3427 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 3428 | << NamespcName << Corrected |
| 3429 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 3430 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 3431 | << Corrected; |
| 3432 | |
| 3433 | NamespcName = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3434 | } else { |
| 3435 | R.clear(); |
| 3436 | R.setLookupName(NamespcName); |
Douglas Gregor | 6699220 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3437 | } |
| 3438 | } |
| 3439 | } |
| 3440 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3441 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3442 | NamedDecl *Named = R.getFoundDecl(); |
| 3443 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 3444 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3445 | // C++ [namespace.udir]p1: |
| 3446 | // A using-directive specifies that the names in the nominated |
| 3447 | // namespace can be used in the scope in which the |
| 3448 | // using-directive appears after the using-directive. During |
| 3449 | // unqualified name lookup (3.4.1), the names appear as if they |
| 3450 | // were declared in the nearest enclosing namespace which |
| 3451 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3452 | // namespace. [Note: in this context, "contains" means "contains |
| 3453 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3454 | |
| 3455 | // Find enclosing context containing both using-directive and |
| 3456 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3457 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3458 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 3459 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 3460 | CommonAncestor = CommonAncestor->getParent(); |
| 3461 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3462 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 3463 | SS.getRange(), |
| 3464 | (NestedNameSpecifier *)SS.getScopeRep(), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3465 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3466 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3467 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 3468 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3471 | // FIXME: We ignore attributes for now. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3472 | return UDir; |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
| 3475 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 3476 | // If scope has associated entity, then using directive is at namespace |
| 3477 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 3478 | // it's lookup structure. |
| 3479 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3480 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3481 | else |
| 3482 | // Otherwise it is block-sope. using-directives will affect lookup |
| 3483 | // only to the end of scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3484 | S->PushUsingDirective(UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3485 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3486 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3487 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3488 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 3489 | AccessSpecifier AS, |
| 3490 | bool HasUsingKeyword, |
| 3491 | SourceLocation UsingLoc, |
| 3492 | CXXScopeSpec &SS, |
| 3493 | UnqualifiedId &Name, |
| 3494 | AttributeList *AttrList, |
| 3495 | bool IsTypeName, |
| 3496 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3497 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3498 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3499 | switch (Name.getKind()) { |
| 3500 | case UnqualifiedId::IK_Identifier: |
| 3501 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3502 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3503 | case UnqualifiedId::IK_ConversionFunctionId: |
| 3504 | break; |
| 3505 | |
| 3506 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3507 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3508 | // C++0x inherited constructors. |
| 3509 | if (getLangOptions().CPlusPlus0x) break; |
| 3510 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3511 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 3512 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3513 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3514 | |
| 3515 | case UnqualifiedId::IK_DestructorName: |
| 3516 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 3517 | << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3518 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3519 | |
| 3520 | case UnqualifiedId::IK_TemplateId: |
| 3521 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 3522 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3523 | return 0; |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3524 | } |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3525 | |
| 3526 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 3527 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3528 | if (!TargetName) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3529 | return 0; |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3530 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3531 | // Warn about using declarations. |
| 3532 | // TODO: store that the declaration was written without 'using' and |
| 3533 | // talk about access decls instead of using decls in the |
| 3534 | // diagnostics. |
| 3535 | if (!HasUsingKeyword) { |
| 3536 | UsingLoc = Name.getSourceRange().getBegin(); |
| 3537 | |
| 3538 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3539 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3542 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3543 | TargetNameInfo, AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3544 | /* IsInstantiation */ false, |
| 3545 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3546 | if (UD) |
| 3547 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3548 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3549 | return UD; |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 3552 | /// \brief Determine whether a using declaration considers the given |
| 3553 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 3554 | /// the same entity or are both typedefs of the same type. |
| 3555 | static bool |
| 3556 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 3557 | bool &SuppressRedeclaration) { |
| 3558 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 3559 | SuppressRedeclaration = false; |
| 3560 | return true; |
| 3561 | } |
| 3562 | |
| 3563 | if (TypedefDecl *TD1 = dyn_cast<TypedefDecl>(D1)) |
| 3564 | if (TypedefDecl *TD2 = dyn_cast<TypedefDecl>(D2)) { |
| 3565 | SuppressRedeclaration = true; |
| 3566 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 3567 | TD2->getUnderlyingType()); |
| 3568 | } |
| 3569 | |
| 3570 | return false; |
| 3571 | } |
| 3572 | |
| 3573 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3574 | /// Determines whether to create a using shadow decl for a particular |
| 3575 | /// decl, given the set of decls existing prior to this using lookup. |
| 3576 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 3577 | const LookupResult &Previous) { |
| 3578 | // Diagnose finding a decl which is not from a base class of the |
| 3579 | // current class. We do this now because there are cases where this |
| 3580 | // function will silently decide not to build a shadow decl, which |
| 3581 | // will pre-empt further diagnostics. |
| 3582 | // |
| 3583 | // We don't need to do this in C++0x because we do the check once on |
| 3584 | // the qualifier. |
| 3585 | // |
| 3586 | // FIXME: diagnose the following if we care enough: |
| 3587 | // struct A { int foo; }; |
| 3588 | // struct B : A { using A::foo; }; |
| 3589 | // template <class T> struct C : A {}; |
| 3590 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 3591 | // This is invalid (during instantiation) in C++03 because B::foo |
| 3592 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 3593 | // We can't diagnose it immediately because C<T> is an unknown |
| 3594 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 3595 | // to A::foo, which will look well-formed when we instantiate. |
| 3596 | // The right solution is to not collapse the shadow-decl chain. |
| 3597 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 3598 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 3599 | |
| 3600 | // Handle enums and anonymous structs. |
| 3601 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 3602 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 3603 | while (OrigRec->isAnonymousStructOrUnion()) |
| 3604 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 3605 | |
| 3606 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 3607 | if (OrigDC == CurContext) { |
| 3608 | Diag(Using->getLocation(), |
| 3609 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3610 | << Using->getNestedNameRange(); |
| 3611 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3612 | return true; |
| 3613 | } |
| 3614 | |
| 3615 | Diag(Using->getNestedNameRange().getBegin(), |
| 3616 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3617 | << Using->getTargetNestedNameDecl() |
| 3618 | << cast<CXXRecordDecl>(CurContext) |
| 3619 | << Using->getNestedNameRange(); |
| 3620 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3621 | return true; |
| 3622 | } |
| 3623 | } |
| 3624 | |
| 3625 | if (Previous.empty()) return false; |
| 3626 | |
| 3627 | NamedDecl *Target = Orig; |
| 3628 | if (isa<UsingShadowDecl>(Target)) |
| 3629 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3630 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3631 | // If the target happens to be one of the previous declarations, we |
| 3632 | // don't have a conflict. |
| 3633 | // |
| 3634 | // FIXME: but we might be increasing its access, in which case we |
| 3635 | // should redeclare it. |
| 3636 | NamedDecl *NonTag = 0, *Tag = 0; |
| 3637 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 3638 | I != E; ++I) { |
| 3639 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 09acc98 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 3640 | bool Result; |
| 3641 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 3642 | return Result; |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3643 | |
| 3644 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 3645 | } |
| 3646 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3647 | if (Target->isFunctionOrFunctionTemplate()) { |
| 3648 | FunctionDecl *FD; |
| 3649 | if (isa<FunctionTemplateDecl>(Target)) |
| 3650 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 3651 | else |
| 3652 | FD = cast<FunctionDecl>(Target); |
| 3653 | |
| 3654 | NamedDecl *OldDecl = 0; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3655 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3656 | case Ovl_Overload: |
| 3657 | return false; |
| 3658 | |
| 3659 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3660 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3661 | break; |
| 3662 | |
| 3663 | // We found a decl with the exact signature. |
| 3664 | case Ovl_Match: |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3665 | // If we're in a record, we want to hide the target, so we |
| 3666 | // return true (without a diagnostic) to tell the caller not to |
| 3667 | // build a shadow decl. |
| 3668 | if (CurContext->isRecord()) |
| 3669 | return true; |
| 3670 | |
| 3671 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3672 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3673 | break; |
| 3674 | } |
| 3675 | |
| 3676 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3677 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 3678 | return true; |
| 3679 | } |
| 3680 | |
| 3681 | // Target is not a function. |
| 3682 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3683 | if (isa<TagDecl>(Target)) { |
| 3684 | // No conflict between a tag and a non-tag. |
| 3685 | if (!Tag) 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(Tag->getLocation(), diag::note_using_decl_conflict); |
| 3690 | return true; |
| 3691 | } |
| 3692 | |
| 3693 | // No conflict between a tag and a non-tag. |
| 3694 | if (!NonTag) return false; |
| 3695 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3696 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3697 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3698 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 3699 | return true; |
| 3700 | } |
| 3701 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3702 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3703 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3704 | UsingDecl *UD, |
| 3705 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3706 | |
| 3707 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3708 | NamedDecl *Target = Orig; |
| 3709 | if (isa<UsingShadowDecl>(Target)) { |
| 3710 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3711 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
| 3714 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3715 | = UsingShadowDecl::Create(Context, CurContext, |
| 3716 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3717 | UD->addShadowDecl(Shadow); |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 3718 | |
| 3719 | Shadow->setAccess(UD->getAccess()); |
| 3720 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 3721 | Shadow->setInvalidDecl(); |
| 3722 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3723 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3724 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3725 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3726 | CurContext->addDecl(Shadow); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3727 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3728 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3729 | return Shadow; |
| 3730 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3731 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3732 | /// Hides a using shadow declaration. This is required by the current |
| 3733 | /// using-decl implementation when a resolvable using declaration in a |
| 3734 | /// class is followed by a declaration which would hide or override |
| 3735 | /// one or more of the using decl's targets; for example: |
| 3736 | /// |
| 3737 | /// struct Base { void foo(int); }; |
| 3738 | /// struct Derived : Base { |
| 3739 | /// using Base::foo; |
| 3740 | /// void foo(int); |
| 3741 | /// }; |
| 3742 | /// |
| 3743 | /// The governing language is C++03 [namespace.udecl]p12: |
| 3744 | /// |
| 3745 | /// When a using-declaration brings names from a base class into a |
| 3746 | /// derived class scope, member functions in the derived class |
| 3747 | /// override and/or hide member functions with the same name and |
| 3748 | /// parameter types in a base class (rather than conflicting). |
| 3749 | /// |
| 3750 | /// There are two ways to implement this: |
| 3751 | /// (1) optimistically create shadow decls when they're not hidden |
| 3752 | /// by existing declarations, or |
| 3753 | /// (2) don't create any shadow decls (or at least don't make them |
| 3754 | /// visible) until we've fully parsed/instantiated the class. |
| 3755 | /// The problem with (1) is that we might have to retroactively remove |
| 3756 | /// a shadow decl, which requires several O(n) operations because the |
| 3757 | /// decl structures are (very reasonably) not designed for removal. |
| 3758 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 3759 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3760 | if (Shadow->getDeclName().getNameKind() == |
| 3761 | DeclarationName::CXXConversionFunctionName) |
| 3762 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 3763 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3764 | // Remove it from the DeclContext... |
| 3765 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3766 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3767 | // ...and the scope, if applicable... |
| 3768 | if (S) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3769 | S->RemoveDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3770 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3773 | // ...and the using decl. |
| 3774 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 3775 | |
| 3776 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3777 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3780 | /// Builds a using declaration. |
| 3781 | /// |
| 3782 | /// \param IsInstantiation - Whether this call arises from an |
| 3783 | /// instantiation of an unresolved using declaration. We treat |
| 3784 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3785 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 3786 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3787 | CXXScopeSpec &SS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3788 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3789 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3790 | bool IsInstantiation, |
| 3791 | bool IsTypeName, |
| 3792 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3793 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3794 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3795 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 3796 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3797 | // FIXME: We ignore attributes for now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3799 | if (SS.isEmpty()) { |
| 3800 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3801 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3802 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3803 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3804 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3805 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3806 | ForRedeclaration); |
| 3807 | Previous.setHideTags(false); |
| 3808 | if (S) { |
| 3809 | LookupName(Previous, S); |
| 3810 | |
| 3811 | // It is really dumb that we have to do this. |
| 3812 | LookupResult::Filter F = Previous.makeFilter(); |
| 3813 | while (F.hasNext()) { |
| 3814 | NamedDecl *D = F.next(); |
| 3815 | if (!isDeclInScope(D, CurContext, S)) |
| 3816 | F.erase(); |
| 3817 | } |
| 3818 | F.done(); |
| 3819 | } else { |
| 3820 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 3821 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 3822 | LookupQualifiedName(Previous, CurContext); |
| 3823 | } |
| 3824 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3825 | NestedNameSpecifier *NNS = |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3826 | static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3827 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3828 | // Check for invalid redeclarations. |
| 3829 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 3830 | return 0; |
| 3831 | |
| 3832 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3833 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 3834 | return 0; |
| 3835 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3836 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3837 | NamedDecl *D; |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3838 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3839 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3840 | // FIXME: not all declaration name kinds are legal here |
| 3841 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 3842 | UsingLoc, TypenameLoc, |
| 3843 | SS.getRange(), NNS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3844 | IdentLoc, NameInfo.getName()); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3845 | } else { |
| 3846 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3847 | UsingLoc, SS.getRange(), |
| 3848 | NNS, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3849 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3850 | } else { |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3851 | D = UsingDecl::Create(Context, CurContext, |
| 3852 | SS.getRange(), UsingLoc, NNS, NameInfo, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3853 | IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3854 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3855 | D->setAccess(AS); |
| 3856 | CurContext->addDecl(D); |
| 3857 | |
| 3858 | if (!LookupContext) return D; |
| 3859 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3860 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3861 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3862 | UD->setInvalidDecl(); |
| 3863 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3864 | } |
| 3865 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3866 | // Look up the target name. |
| 3867 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3868 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3869 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3870 | // Unlike most lookups, we don't always want to hide tag |
| 3871 | // declarations: tag names are visible through the using declaration |
| 3872 | // even if hidden by ordinary names, *except* in a dependent context |
| 3873 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3874 | if (!IsInstantiation) |
| 3875 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3876 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3877 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3879 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 3880 | Diag(IdentLoc, diag::err_no_member) |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3881 | << NameInfo.getName() << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3882 | UD->setInvalidDecl(); |
| 3883 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3886 | if (R.isAmbiguous()) { |
| 3887 | UD->setInvalidDecl(); |
| 3888 | return UD; |
| 3889 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3890 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3891 | if (IsTypeName) { |
| 3892 | // 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] | 3893 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3894 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 3895 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 3896 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 3897 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3898 | UD->setInvalidDecl(); |
| 3899 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3900 | } |
| 3901 | } else { |
| 3902 | // If we asked for a non-typename and we got a type, error out, |
| 3903 | // but only if this is an instantiation of an unresolved using |
| 3904 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3905 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3906 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 3907 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3908 | UD->setInvalidDecl(); |
| 3909 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3910 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3913 | // C++0x N2914 [namespace.udecl]p6: |
| 3914 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3915 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3916 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 3917 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3918 | UD->setInvalidDecl(); |
| 3919 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3920 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3921 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3922 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 3923 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 3924 | BuildUsingShadowDecl(S, UD, *I); |
| 3925 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3926 | |
| 3927 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3928 | } |
| 3929 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3930 | /// Checks that the given using declaration is not an invalid |
| 3931 | /// redeclaration. Note that this is checking only for the using decl |
| 3932 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 3933 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 3934 | bool isTypeName, |
| 3935 | const CXXScopeSpec &SS, |
| 3936 | SourceLocation NameLoc, |
| 3937 | const LookupResult &Prev) { |
| 3938 | // C++03 [namespace.udecl]p8: |
| 3939 | // C++0x [namespace.udecl]p10: |
| 3940 | // A using-declaration is a declaration and can therefore be used |
| 3941 | // repeatedly where (and only where) multiple declarations are |
| 3942 | // allowed. |
Douglas Gregor | a97badf | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 3943 | // |
John McCall | 8a72621 | 2010-11-29 18:01:58 +0000 | [diff] [blame] | 3944 | // That's in non-member contexts. |
| 3945 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3946 | return false; |
| 3947 | |
| 3948 | NestedNameSpecifier *Qual |
| 3949 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 3950 | |
| 3951 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 3952 | NamedDecl *D = *I; |
| 3953 | |
| 3954 | bool DTypename; |
| 3955 | NestedNameSpecifier *DQual; |
| 3956 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 3957 | DTypename = UD->isTypeName(); |
| 3958 | DQual = UD->getTargetNestedNameDecl(); |
| 3959 | } else if (UnresolvedUsingValueDecl *UD |
| 3960 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 3961 | DTypename = false; |
| 3962 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3963 | } else if (UnresolvedUsingTypenameDecl *UD |
| 3964 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 3965 | DTypename = true; |
| 3966 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3967 | } else continue; |
| 3968 | |
| 3969 | // using decls differ if one says 'typename' and the other doesn't. |
| 3970 | // FIXME: non-dependent using decls? |
| 3971 | if (isTypeName != DTypename) continue; |
| 3972 | |
| 3973 | // using decls differ if they name different scopes (but note that |
| 3974 | // template instantiation can cause this check to trigger when it |
| 3975 | // didn't before instantiation). |
| 3976 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 3977 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 3978 | continue; |
| 3979 | |
| 3980 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3981 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3982 | return true; |
| 3983 | } |
| 3984 | |
| 3985 | return false; |
| 3986 | } |
| 3987 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3988 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3989 | /// Checks that the given nested-name qualifier used in a using decl |
| 3990 | /// in the current context is appropriately related to the current |
| 3991 | /// scope. If an error is found, diagnoses it and returns true. |
| 3992 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 3993 | const CXXScopeSpec &SS, |
| 3994 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3995 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3996 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3997 | if (!CurContext->isRecord()) { |
| 3998 | // C++03 [namespace.udecl]p3: |
| 3999 | // C++0x [namespace.udecl]p8: |
| 4000 | // A using-declaration for a class member shall be a member-declaration. |
| 4001 | |
| 4002 | // If we weren't able to compute a valid scope, it must be a |
| 4003 | // dependent class scope. |
| 4004 | if (!NamedContext || NamedContext->isRecord()) { |
| 4005 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 4006 | << SS.getRange(); |
| 4007 | return true; |
| 4008 | } |
| 4009 | |
| 4010 | // Otherwise, everything is known to be fine. |
| 4011 | return false; |
| 4012 | } |
| 4013 | |
| 4014 | // The current scope is a record. |
| 4015 | |
| 4016 | // If the named context is dependent, we can't decide much. |
| 4017 | if (!NamedContext) { |
| 4018 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 4019 | // nested-name-specifier does not refer to a base class, which is |
| 4020 | // still possible in some cases. |
| 4021 | |
| 4022 | // Otherwise we have to conservatively report that things might be |
| 4023 | // okay. |
| 4024 | return false; |
| 4025 | } |
| 4026 | |
| 4027 | if (!NamedContext->isRecord()) { |
| 4028 | // Ideally this would point at the last name in the specifier, |
| 4029 | // but we don't have that level of source info. |
| 4030 | Diag(SS.getRange().getBegin(), |
| 4031 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 4032 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 4033 | return true; |
| 4034 | } |
| 4035 | |
| 4036 | if (getLangOptions().CPlusPlus0x) { |
| 4037 | // C++0x [namespace.udecl]p3: |
| 4038 | // In a using-declaration used as a member-declaration, the |
| 4039 | // nested-name-specifier shall name a base class of the class |
| 4040 | // being defined. |
| 4041 | |
| 4042 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 4043 | cast<CXXRecordDecl>(NamedContext))) { |
| 4044 | if (CurContext == NamedContext) { |
| 4045 | Diag(NameLoc, |
| 4046 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 4047 | << SS.getRange(); |
| 4048 | return true; |
| 4049 | } |
| 4050 | |
| 4051 | Diag(SS.getRange().getBegin(), |
| 4052 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 4053 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 4054 | << cast<CXXRecordDecl>(CurContext) |
| 4055 | << SS.getRange(); |
| 4056 | return true; |
| 4057 | } |
| 4058 | |
| 4059 | return false; |
| 4060 | } |
| 4061 | |
| 4062 | // C++03 [namespace.udecl]p4: |
| 4063 | // A using-declaration used as a member-declaration shall refer |
| 4064 | // to a member of a base class of the class being defined [etc.]. |
| 4065 | |
| 4066 | // Salient point: SS doesn't have to name a base class as long as |
| 4067 | // lookup only finds members from base classes. Therefore we can |
| 4068 | // diagnose here only if we can prove that that can't happen, |
| 4069 | // i.e. if the class hierarchies provably don't intersect. |
| 4070 | |
| 4071 | // TODO: it would be nice if "definitely valid" results were cached |
| 4072 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 4073 | // need to be repeated. |
| 4074 | |
| 4075 | struct UserData { |
| 4076 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 4077 | |
| 4078 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 4079 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 4080 | Data->Bases.insert(Base); |
| 4081 | return true; |
| 4082 | } |
| 4083 | |
| 4084 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 4085 | return !Class->forallBases(collect, this); |
| 4086 | } |
| 4087 | |
| 4088 | /// Returns true if the base is dependent or is one of the |
| 4089 | /// accumulated base classes. |
| 4090 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 4091 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 4092 | return !Data->Bases.count(Base); |
| 4093 | } |
| 4094 | |
| 4095 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 4096 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 4097 | } |
| 4098 | }; |
| 4099 | |
| 4100 | UserData Data; |
| 4101 | |
| 4102 | // Returns false if we find a dependent base. |
| 4103 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 4104 | return false; |
| 4105 | |
| 4106 | // Returns false if the class has a dependent base or if it or one |
| 4107 | // of its bases is present in the base set of the current context. |
| 4108 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 4109 | return false; |
| 4110 | |
| 4111 | Diag(SS.getRange().getBegin(), |
| 4112 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 4113 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 4114 | << cast<CXXRecordDecl>(CurContext) |
| 4115 | << SS.getRange(); |
| 4116 | |
| 4117 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4120 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 4121 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4122 | SourceLocation AliasLoc, |
| 4123 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4124 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 4125 | SourceLocation IdentLoc, |
| 4126 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4127 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4128 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4129 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 4130 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4131 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4132 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | ae37475 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 4133 | NamedDecl *PrevDecl |
| 4134 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 4135 | ForRedeclaration); |
| 4136 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 4137 | PrevDecl = 0; |
| 4138 | |
| 4139 | if (PrevDecl) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4140 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4141 | // 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] | 4142 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4143 | // FIXME: At some point, we'll want to create the (redundant) |
| 4144 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4145 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4146 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4147 | return 0; |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4148 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4149 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4150 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 4151 | diag::err_redefinition_different_kind; |
| 4152 | Diag(AliasLoc, DiagID) << Alias; |
| 4153 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4154 | return 0; |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4157 | if (R.isAmbiguous()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4158 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4159 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4160 | if (R.empty()) { |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4161 | if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 4162 | CTC_NoKeywords, 0)) { |
| 4163 | if (R.getAsSingle<NamespaceDecl>() || |
| 4164 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 4165 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 4166 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 4167 | << Ident << DC << Corrected << SS.getRange() |
| 4168 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4169 | else |
| 4170 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 4171 | << Ident << Corrected |
| 4172 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4173 | |
| 4174 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 4175 | << Corrected; |
| 4176 | |
| 4177 | Ident = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 4178 | } else { |
| 4179 | R.clear(); |
| 4180 | R.setLookupName(Ident); |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4181 | } |
| 4182 | } |
| 4183 | |
| 4184 | if (R.empty()) { |
| 4185 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4186 | return 0; |
Douglas Gregor | 0e8c4b9 | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4187 | } |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 4188 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4189 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4190 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 4192 | Alias, SS.getRange(), |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 4193 | (NestedNameSpecifier *)SS.getScopeRep(), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4194 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4195 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 4196 | PushOnScopeChains(AliasDecl, S); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4197 | return AliasDecl; |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4200 | namespace { |
| 4201 | /// \brief Scoped object used to handle the state changes required in Sema |
| 4202 | /// to implicitly define the body of a C++ member function; |
| 4203 | class ImplicitlyDefinedFunctionScope { |
| 4204 | Sema &S; |
| 4205 | DeclContext *PreviousContext; |
| 4206 | |
| 4207 | public: |
| 4208 | ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method) |
| 4209 | : S(S), PreviousContext(S.CurContext) |
| 4210 | { |
| 4211 | S.CurContext = Method; |
| 4212 | S.PushFunctionScope(); |
| 4213 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 4214 | } |
| 4215 | |
| 4216 | ~ImplicitlyDefinedFunctionScope() { |
| 4217 | S.PopExpressionEvaluationContext(); |
| 4218 | S.PopFunctionOrBlockScope(); |
| 4219 | S.CurContext = PreviousContext; |
| 4220 | } |
| 4221 | }; |
| 4222 | } |
| 4223 | |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4224 | static CXXConstructorDecl *getDefaultConstructorUnsafe(Sema &Self, |
| 4225 | CXXRecordDecl *D) { |
| 4226 | ASTContext &Context = Self.Context; |
| 4227 | QualType ClassType = Context.getTypeDeclType(D); |
| 4228 | DeclarationName ConstructorName |
| 4229 | = Context.DeclarationNames.getCXXConstructorName( |
| 4230 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
| 4231 | |
| 4232 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 4233 | for (llvm::tie(Con, ConEnd) = D->lookup(ConstructorName); |
| 4234 | Con != ConEnd; ++Con) { |
| 4235 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 4236 | if (isa<FunctionTemplateDecl>(*Con)) |
| 4237 | continue; |
| 4238 | |
| 4239 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 4240 | if (Constructor->isDefaultConstructor()) |
| 4241 | return Constructor; |
| 4242 | } |
| 4243 | return 0; |
| 4244 | } |
| 4245 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4246 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 4247 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4248 | // C++ [class.ctor]p5: |
| 4249 | // A default constructor for a class X is a constructor of class X |
| 4250 | // that can be called without an argument. If there is no |
| 4251 | // user-declared constructor for class X, a default constructor is |
| 4252 | // implicitly declared. An implicitly-declared default constructor |
| 4253 | // is an inline public member of its class. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4254 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
| 4255 | "Should not build implicit default constructor!"); |
| 4256 | |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4257 | // C++ [except.spec]p14: |
| 4258 | // An implicitly declared special member function (Clause 12) shall have an |
| 4259 | // exception-specification. [...] |
| 4260 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4261 | |
| 4262 | // Direct base-class destructors. |
| 4263 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4264 | BEnd = ClassDecl->bases_end(); |
| 4265 | B != BEnd; ++B) { |
| 4266 | if (B->isVirtual()) // Handled below. |
| 4267 | continue; |
| 4268 | |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4269 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 4270 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4271 | if (!BaseClassDecl->hasDeclaredDefaultConstructor()) |
| 4272 | ExceptSpec.CalledDecl(DeclareImplicitDefaultConstructor(BaseClassDecl)); |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4273 | else if (CXXConstructorDecl *Constructor |
| 4274 | = getDefaultConstructorUnsafe(*this, BaseClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4275 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4276 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4277 | } |
| 4278 | |
| 4279 | // Virtual base-class destructors. |
| 4280 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 4281 | BEnd = ClassDecl->vbases_end(); |
| 4282 | B != BEnd; ++B) { |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4283 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 4284 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4285 | if (!BaseClassDecl->hasDeclaredDefaultConstructor()) |
| 4286 | ExceptSpec.CalledDecl(DeclareImplicitDefaultConstructor(BaseClassDecl)); |
| 4287 | else if (CXXConstructorDecl *Constructor |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4288 | = getDefaultConstructorUnsafe(*this, BaseClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4289 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4290 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
| 4293 | // Field destructors. |
| 4294 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4295 | FEnd = ClassDecl->field_end(); |
| 4296 | F != FEnd; ++F) { |
| 4297 | if (const RecordType *RecordTy |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4298 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 4299 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4300 | if (!FieldClassDecl->hasDeclaredDefaultConstructor()) |
| 4301 | ExceptSpec.CalledDecl( |
| 4302 | DeclareImplicitDefaultConstructor(FieldClassDecl)); |
| 4303 | else if (CXXConstructorDecl *Constructor |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4304 | = getDefaultConstructorUnsafe(*this, FieldClassDecl)) |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4305 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4306 | } |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | |
| 4310 | // Create the actual constructor declaration. |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4311 | CanQualType ClassType |
| 4312 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 4313 | DeclarationName Name |
| 4314 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4315 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4316 | CXXConstructorDecl *DefaultCon |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4317 | = CXXConstructorDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4318 | Context.getFunctionType(Context.VoidTy, |
| 4319 | 0, 0, false, 0, |
Douglas Gregor | eb8c670 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4320 | ExceptSpec.hasExceptionSpecification(), |
| 4321 | ExceptSpec.hasAnyExceptionSpecification(), |
| 4322 | ExceptSpec.size(), |
| 4323 | ExceptSpec.data(), |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4324 | FunctionType::ExtInfo()), |
| 4325 | /*TInfo=*/0, |
| 4326 | /*isExplicit=*/false, |
| 4327 | /*isInline=*/true, |
| 4328 | /*isImplicitlyDeclared=*/true); |
| 4329 | DefaultCon->setAccess(AS_public); |
| 4330 | DefaultCon->setImplicit(); |
| 4331 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4332 | |
| 4333 | // Note that we have declared this constructor. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4334 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
| 4335 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4336 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4337 | PushOnScopeChains(DefaultCon, S, false); |
| 4338 | ClassDecl->addDecl(DefaultCon); |
| 4339 | |
Douglas Gregor | 32df23e | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4340 | return DefaultCon; |
| 4341 | } |
| 4342 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4343 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 4344 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4345 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4346 | !Constructor->isUsed(false)) && |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4347 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4348 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 4349 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4350 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 4351 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4352 | ImplicitlyDefinedFunctionScope Scope(*this, Constructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 4353 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4354 | if (SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false) || |
| 4355 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4356 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 4357 | << CXXConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4358 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4359 | return; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4360 | } |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4361 | |
| 4362 | SourceLocation Loc = Constructor->getLocation(); |
| 4363 | Constructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 4364 | |
| 4365 | Constructor->setUsed(); |
| 4366 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4369 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4370 | // C++ [class.dtor]p2: |
| 4371 | // If a class has no user-declared destructor, a destructor is |
| 4372 | // declared implicitly. An implicitly-declared destructor is an |
| 4373 | // inline public member of its class. |
| 4374 | |
| 4375 | // C++ [except.spec]p14: |
| 4376 | // An implicitly declared special member function (Clause 12) shall have |
| 4377 | // an exception-specification. |
| 4378 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4379 | |
| 4380 | // Direct base-class destructors. |
| 4381 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4382 | BEnd = ClassDecl->bases_end(); |
| 4383 | B != BEnd; ++B) { |
| 4384 | if (B->isVirtual()) // Handled below. |
| 4385 | continue; |
| 4386 | |
| 4387 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 4388 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4389 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | // Virtual base-class destructors. |
| 4393 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 4394 | BEnd = ClassDecl->vbases_end(); |
| 4395 | B != BEnd; ++B) { |
| 4396 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 4397 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4398 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
| 4401 | // Field destructors. |
| 4402 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4403 | FEnd = ClassDecl->field_end(); |
| 4404 | F != FEnd; ++F) { |
| 4405 | if (const RecordType *RecordTy |
| 4406 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
| 4407 | ExceptSpec.CalledDecl( |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4408 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4411 | // Create the actual destructor declaration. |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4412 | QualType Ty = Context.getFunctionType(Context.VoidTy, |
| 4413 | 0, 0, false, 0, |
| 4414 | ExceptSpec.hasExceptionSpecification(), |
| 4415 | ExceptSpec.hasAnyExceptionSpecification(), |
| 4416 | ExceptSpec.size(), |
| 4417 | ExceptSpec.data(), |
| 4418 | FunctionType::ExtInfo()); |
| 4419 | |
| 4420 | CanQualType ClassType |
| 4421 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 4422 | DeclarationName Name |
| 4423 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4424 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4425 | CXXDestructorDecl *Destructor |
Craig Silverstein | b41d899 | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 4426 | = CXXDestructorDecl::Create(Context, ClassDecl, NameInfo, Ty, 0, |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4427 | /*isInline=*/true, |
| 4428 | /*isImplicitlyDeclared=*/true); |
| 4429 | Destructor->setAccess(AS_public); |
| 4430 | Destructor->setImplicit(); |
| 4431 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4432 | |
| 4433 | // Note that we have declared this destructor. |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4434 | ++ASTContext::NumImplicitDestructorsDeclared; |
| 4435 | |
| 4436 | // Introduce this destructor into its scope. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4437 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4438 | PushOnScopeChains(Destructor, S, false); |
| 4439 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4440 | |
| 4441 | // This could be uniqued if it ever proves significant. |
| 4442 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
| 4443 | |
| 4444 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 4923aa2 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4445 | |
Douglas Gregor | fabd43a | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4446 | return Destructor; |
| 4447 | } |
| 4448 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4449 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 4450 | CXXDestructorDecl *Destructor) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4451 | assert((Destructor->isImplicit() && !Destructor->isUsed(false)) && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4452 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4453 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4454 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4455 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4456 | if (Destructor->isInvalidDecl()) |
| 4457 | return; |
| 4458 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4459 | ImplicitlyDefinedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4460 | |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 4461 | DiagnosticErrorTrap Trap(Diags); |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 4462 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 4463 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4464 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4465 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4466 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4467 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 4468 | |
| 4469 | Destructor->setInvalidDecl(); |
| 4470 | return; |
| 4471 | } |
| 4472 | |
Douglas Gregor | 4ada9d3 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4473 | SourceLocation Loc = Destructor->getLocation(); |
| 4474 | Destructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 4475 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4476 | Destructor->setUsed(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4477 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4480 | /// \brief Builds a statement that copies the given entity from \p From to |
| 4481 | /// \c To. |
| 4482 | /// |
| 4483 | /// This routine is used to copy the members of a class with an |
| 4484 | /// implicitly-declared copy assignment operator. When the entities being |
| 4485 | /// copied are arrays, this routine builds for loops to copy them. |
| 4486 | /// |
| 4487 | /// \param S The Sema object used for type-checking. |
| 4488 | /// |
| 4489 | /// \param Loc The location where the implicit copy is being generated. |
| 4490 | /// |
| 4491 | /// \param T The type of the expressions being copied. Both expressions must |
| 4492 | /// have this type. |
| 4493 | /// |
| 4494 | /// \param To The expression we are copying to. |
| 4495 | /// |
| 4496 | /// \param From The expression we are copying from. |
| 4497 | /// |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4498 | /// \param CopyingBaseSubobject Whether we're copying a base subobject. |
| 4499 | /// Otherwise, it's a non-static member subobject. |
| 4500 | /// |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4501 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 4502 | /// |
| 4503 | /// \returns A statement or a loop that copies the expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4504 | static StmtResult |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4505 | BuildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4506 | Expr *To, Expr *From, |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4507 | bool CopyingBaseSubobject, unsigned Depth = 0) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4508 | // C++0x [class.copy]p30: |
| 4509 | // Each subobject is assigned in the manner appropriate to its type: |
| 4510 | // |
| 4511 | // - if the subobject is of class type, the copy assignment operator |
| 4512 | // for the class is used (as if by explicit qualification; that is, |
| 4513 | // ignoring any possible virtual overriding functions in more derived |
| 4514 | // classes); |
| 4515 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 4516 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4517 | |
| 4518 | // Look for operator=. |
| 4519 | DeclarationName Name |
| 4520 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 4521 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 4522 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| 4523 | |
| 4524 | // Filter out any result that isn't a copy-assignment operator. |
| 4525 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 4526 | while (F.hasNext()) { |
| 4527 | NamedDecl *D = F.next(); |
| 4528 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 4529 | if (Method->isCopyAssignmentOperator()) |
| 4530 | continue; |
| 4531 | |
| 4532 | F.erase(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4533 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4534 | F.done(); |
| 4535 | |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4536 | // Suppress the protected check (C++ [class.protected]) for each of the |
| 4537 | // assignment operators we found. This strange dance is required when |
| 4538 | // we're assigning via a base classes's copy-assignment operator. To |
| 4539 | // ensure that we're getting the right base class subobject (without |
| 4540 | // ambiguities), we need to cast "this" to that subobject type; to |
| 4541 | // ensure that we don't go through the virtual call mechanism, we need |
| 4542 | // to qualify the operator= name with the base class (see below). However, |
| 4543 | // this means that if the base class has a protected copy assignment |
| 4544 | // operator, the protected member access check will fail. So, we |
| 4545 | // rewrite "protected" access to "public" access in this case, since we |
| 4546 | // know by construction that we're calling from a derived class. |
| 4547 | if (CopyingBaseSubobject) { |
| 4548 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 4549 | L != LEnd; ++L) { |
| 4550 | if (L.getAccess() == AS_protected) |
| 4551 | L.setAccess(AS_public); |
| 4552 | } |
| 4553 | } |
| 4554 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4555 | // Create the nested-name-specifier that will be used to qualify the |
| 4556 | // reference to operator=; this is required to suppress the virtual |
| 4557 | // call mechanism. |
| 4558 | CXXScopeSpec SS; |
| 4559 | SS.setRange(Loc); |
| 4560 | SS.setScopeRep(NestedNameSpecifier::Create(S.Context, 0, false, |
| 4561 | T.getTypePtr())); |
| 4562 | |
| 4563 | // Create the reference to operator=. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4564 | ExprResult OpEqualRef |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4565 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4566 | /*FirstQualifierInScope=*/0, OpLookup, |
| 4567 | /*TemplateArgs=*/0, |
| 4568 | /*SuppressQualifierCheck=*/true); |
| 4569 | if (OpEqualRef.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 | // Build the call to the assignment operator. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4573 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4574 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4575 | OpEqualRef.takeAs<Expr>(), |
| 4576 | Loc, &From, 1, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4577 | if (Call.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4578 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4579 | |
| 4580 | return S.Owned(Call.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4581 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4582 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4583 | // - if the subobject is of scalar type, the built-in assignment |
| 4584 | // operator is used. |
| 4585 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| 4586 | if (!ArrayTy) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4587 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4588 | if (Assignment.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4589 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4590 | |
| 4591 | return S.Owned(Assignment.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4592 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4593 | |
| 4594 | // - if the subobject is an array, each element is assigned, in the |
| 4595 | // manner appropriate to the element type; |
| 4596 | |
| 4597 | // Construct a loop over the array bounds, e.g., |
| 4598 | // |
| 4599 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 4600 | // |
| 4601 | // that will copy each of the array elements. |
| 4602 | QualType SizeType = S.Context.getSizeType(); |
| 4603 | |
| 4604 | // Create the iteration variable. |
| 4605 | IdentifierInfo *IterationVarName = 0; |
| 4606 | { |
| 4607 | llvm::SmallString<8> Str; |
| 4608 | llvm::raw_svector_ostream OS(Str); |
| 4609 | OS << "__i" << Depth; |
| 4610 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 4611 | } |
| 4612 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, |
| 4613 | IterationVarName, SizeType, |
| 4614 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4615 | SC_None, SC_None); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4616 | |
| 4617 | // Initialize the iteration variable to zero. |
| 4618 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4619 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4620 | |
| 4621 | // Create a reference to the iteration variable; we'll use this several |
| 4622 | // times throughout. |
| 4623 | Expr *IterationVarRef |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4624 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_RValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4625 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
| 4626 | |
| 4627 | // Create the DeclStmt that holds the iteration variable. |
| 4628 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| 4629 | |
| 4630 | // Create the comparison against the array bound. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 4631 | llvm::APInt Upper |
| 4632 | = ArrayTy->getSize().zextOrTrunc(S.Context.getTypeSize(SizeType)); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4633 | Expr *Comparison |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4634 | = new (S.Context) BinaryOperator(IterationVarRef, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4635 | IntegerLiteral::Create(S.Context, Upper, SizeType, Loc), |
| 4636 | BO_NE, S.Context.BoolTy, |
| 4637 | VK_RValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4638 | |
| 4639 | // Create the pre-increment of the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4640 | Expr *Increment |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4641 | = new (S.Context) UnaryOperator(IterationVarRef, UO_PreInc, SizeType, |
| 4642 | VK_LValue, OK_Ordinary, Loc); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4643 | |
| 4644 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4645 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
| 4646 | IterationVarRef, Loc)); |
| 4647 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
| 4648 | IterationVarRef, Loc)); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4649 | |
| 4650 | // Build the copy for an individual element of the array. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4651 | StmtResult Copy = BuildSingleCopyAssign(S, Loc, ArrayTy->getElementType(), |
| 4652 | To, From, CopyingBaseSubobject, |
| 4653 | Depth + 1); |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 4654 | if (Copy.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4655 | return StmtError(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4656 | |
| 4657 | // Construct the loop that copies all elements of this array. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4658 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4659 | S.MakeFullExpr(Comparison), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4660 | 0, S.MakeFullExpr(Increment), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4661 | Loc, Copy.take()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4664 | /// \brief Determine whether the given class has a copy assignment operator |
| 4665 | /// that accepts a const-qualified argument. |
| 4666 | static bool hasConstCopyAssignment(Sema &S, const CXXRecordDecl *CClass) { |
| 4667 | CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(CClass); |
| 4668 | |
| 4669 | if (!Class->hasDeclaredCopyAssignment()) |
| 4670 | S.DeclareImplicitCopyAssignment(Class); |
| 4671 | |
| 4672 | QualType ClassType = S.Context.getCanonicalType(S.Context.getTypeDeclType(Class)); |
| 4673 | DeclarationName OpName |
| 4674 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 4675 | |
| 4676 | DeclContext::lookup_const_iterator Op, OpEnd; |
| 4677 | for (llvm::tie(Op, OpEnd) = Class->lookup(OpName); Op != OpEnd; ++Op) { |
| 4678 | // C++ [class.copy]p9: |
| 4679 | // A user-declared copy assignment operator is a non-static non-template |
| 4680 | // member function of class X with exactly one parameter of type X, X&, |
| 4681 | // const X&, volatile X& or const volatile X&. |
| 4682 | const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); |
| 4683 | if (!Method) |
| 4684 | continue; |
| 4685 | |
| 4686 | if (Method->isStatic()) |
| 4687 | continue; |
| 4688 | if (Method->getPrimaryTemplate()) |
| 4689 | continue; |
| 4690 | const FunctionProtoType *FnType = |
| 4691 | Method->getType()->getAs<FunctionProtoType>(); |
| 4692 | assert(FnType && "Overloaded operator has no prototype."); |
| 4693 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 4694 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 4695 | continue; |
| 4696 | bool AcceptsConst = true; |
| 4697 | QualType ArgType = FnType->getArgType(0); |
| 4698 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()){ |
| 4699 | ArgType = Ref->getPointeeType(); |
| 4700 | // Is it a non-const lvalue reference? |
| 4701 | if (!ArgType.isConstQualified()) |
| 4702 | AcceptsConst = false; |
| 4703 | } |
| 4704 | if (!S.Context.hasSameUnqualifiedType(ArgType, ClassType)) |
| 4705 | continue; |
| 4706 | |
| 4707 | // We have a single argument of type cv X or cv X&, i.e. we've found the |
| 4708 | // copy assignment operator. Return whether it accepts const arguments. |
| 4709 | return AcceptsConst; |
| 4710 | } |
| 4711 | assert(Class->isInvalidDecl() && |
| 4712 | "No copy assignment operator declared in valid code."); |
| 4713 | return false; |
| 4714 | } |
| 4715 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4716 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4717 | // Note: The following rules are largely analoguous to the copy |
| 4718 | // constructor rules. Note that virtual bases are not taken into account |
| 4719 | // for determining the argument type of the operator. Note also that |
| 4720 | // operators taking an object instead of a reference are allowed. |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4721 | |
| 4722 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4723 | // C++ [class.copy]p10: |
| 4724 | // If the class definition does not explicitly declare a copy |
| 4725 | // assignment operator, one is declared implicitly. |
| 4726 | // The implicitly-defined copy assignment operator for a class X |
| 4727 | // will have the form |
| 4728 | // |
| 4729 | // X& X::operator=(const X&) |
| 4730 | // |
| 4731 | // if |
| 4732 | bool HasConstCopyAssignment = true; |
| 4733 | |
| 4734 | // -- each direct base class B of X has a copy assignment operator |
| 4735 | // whose parameter is of type const B&, const volatile B& or B, |
| 4736 | // and |
| 4737 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4738 | BaseEnd = ClassDecl->bases_end(); |
| 4739 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
| 4740 | assert(!Base->getType()->isDependentType() && |
| 4741 | "Cannot generate implicit members for class with dependent bases."); |
| 4742 | const CXXRecordDecl *BaseClassDecl |
| 4743 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4744 | HasConstCopyAssignment = hasConstCopyAssignment(*this, BaseClassDecl); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | // -- for all the nonstatic data members of X that are of a class |
| 4748 | // type M (or array thereof), each such class type has a copy |
| 4749 | // assignment operator whose parameter is of type const M&, |
| 4750 | // const volatile M& or M. |
| 4751 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4752 | FieldEnd = ClassDecl->field_end(); |
| 4753 | HasConstCopyAssignment && Field != FieldEnd; |
| 4754 | ++Field) { |
| 4755 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 4756 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 4757 | const CXXRecordDecl *FieldClassDecl |
| 4758 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4759 | HasConstCopyAssignment = hasConstCopyAssignment(*this, FieldClassDecl); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4760 | } |
| 4761 | } |
| 4762 | |
| 4763 | // Otherwise, the implicitly declared copy assignment operator will |
| 4764 | // have the form |
| 4765 | // |
| 4766 | // X& X::operator=(X&) |
| 4767 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 4768 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 4769 | if (HasConstCopyAssignment) |
| 4770 | ArgType = ArgType.withConst(); |
| 4771 | ArgType = Context.getLValueReferenceType(ArgType); |
| 4772 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4773 | // C++ [except.spec]p14: |
| 4774 | // An implicitly declared special member function (Clause 12) shall have an |
| 4775 | // exception-specification. [...] |
| 4776 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4777 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4778 | BaseEnd = ClassDecl->bases_end(); |
| 4779 | Base != BaseEnd; ++Base) { |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4780 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4781 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4782 | |
| 4783 | if (!BaseClassDecl->hasDeclaredCopyAssignment()) |
| 4784 | DeclareImplicitCopyAssignment(BaseClassDecl); |
| 4785 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4786 | if (CXXMethodDecl *CopyAssign |
| 4787 | = BaseClassDecl->getCopyAssignmentOperator(HasConstCopyAssignment)) |
| 4788 | ExceptSpec.CalledDecl(CopyAssign); |
| 4789 | } |
| 4790 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4791 | FieldEnd = ClassDecl->field_end(); |
| 4792 | Field != FieldEnd; |
| 4793 | ++Field) { |
| 4794 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 4795 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4796 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4797 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4798 | |
| 4799 | if (!FieldClassDecl->hasDeclaredCopyAssignment()) |
| 4800 | DeclareImplicitCopyAssignment(FieldClassDecl); |
| 4801 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4802 | if (CXXMethodDecl *CopyAssign |
| 4803 | = FieldClassDecl->getCopyAssignmentOperator(HasConstCopyAssignment)) |
| 4804 | ExceptSpec.CalledDecl(CopyAssign); |
| 4805 | } |
| 4806 | } |
| 4807 | |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4808 | // An implicitly-declared copy assignment operator is an inline public |
| 4809 | // member of its class. |
| 4810 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4811 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4812 | CXXMethodDecl *CopyAssignment |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4813 | = CXXMethodDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4814 | Context.getFunctionType(RetType, &ArgType, 1, |
| 4815 | false, 0, |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4816 | ExceptSpec.hasExceptionSpecification(), |
| 4817 | ExceptSpec.hasAnyExceptionSpecification(), |
| 4818 | ExceptSpec.size(), |
| 4819 | ExceptSpec.data(), |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4820 | FunctionType::ExtInfo()), |
| 4821 | /*TInfo=*/0, /*isStatic=*/false, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4822 | /*StorageClassAsWritten=*/SC_None, |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4823 | /*isInline=*/true); |
| 4824 | CopyAssignment->setAccess(AS_public); |
| 4825 | CopyAssignment->setImplicit(); |
| 4826 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4827 | |
| 4828 | // Add the parameter to the operator. |
| 4829 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 4830 | ClassDecl->getLocation(), |
| 4831 | /*Id=*/0, |
| 4832 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4833 | SC_None, |
| 4834 | SC_None, 0); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4835 | CopyAssignment->setParams(&FromParam, 1); |
| 4836 | |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4837 | // Note that we have added this copy-assignment operator. |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4838 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
| 4839 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4840 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4841 | PushOnScopeChains(CopyAssignment, S, false); |
| 4842 | ClassDecl->addDecl(CopyAssignment); |
Douglas Gregor | d3c3590 | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4843 | |
| 4844 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 4845 | return CopyAssignment; |
| 4846 | } |
| 4847 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4848 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 4849 | CXXMethodDecl *CopyAssignOperator) { |
| 4850 | assert((CopyAssignOperator->isImplicit() && |
| 4851 | CopyAssignOperator->isOverloadedOperator() && |
| 4852 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4853 | !CopyAssignOperator->isUsed(false)) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4854 | "DefineImplicitCopyAssignment called for wrong function"); |
| 4855 | |
| 4856 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 4857 | |
| 4858 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 4859 | CopyAssignOperator->setInvalidDecl(); |
| 4860 | return; |
| 4861 | } |
| 4862 | |
| 4863 | CopyAssignOperator->setUsed(); |
| 4864 | |
| 4865 | ImplicitlyDefinedFunctionScope Scope(*this, CopyAssignOperator); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 4866 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4867 | |
| 4868 | // C++0x [class.copy]p30: |
| 4869 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 4870 | // for a non-union class X performs memberwise copy assignment of its |
| 4871 | // subobjects. The direct base classes of X are assigned first, in the |
| 4872 | // order of their declaration in the base-specifier-list, and then the |
| 4873 | // immediate non-static data members of X are assigned, in the order in |
| 4874 | // which they were declared in the class definition. |
| 4875 | |
| 4876 | // The statements that form the synthesized function body. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4877 | ASTOwningVector<Stmt*> Statements(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4878 | |
| 4879 | // The parameter for the "other" object, which we are copying from. |
| 4880 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 4881 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 4882 | QualType OtherRefType = Other->getType(); |
| 4883 | if (const LValueReferenceType *OtherRef |
| 4884 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 4885 | OtherRefType = OtherRef->getPointeeType(); |
| 4886 | OtherQuals = OtherRefType.getQualifiers(); |
| 4887 | } |
| 4888 | |
| 4889 | // Our location for everything implicitly-generated. |
| 4890 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 4891 | |
| 4892 | // Construct a reference to the "other" object. We'll be using this |
| 4893 | // throughout the generated ASTs. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4894 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, VK_LValue, Loc).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4895 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 4896 | |
| 4897 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 4898 | // ASTs. |
| 4899 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 4900 | assert(This && "Reference to this cannot fail!"); |
| 4901 | |
| 4902 | // Assign base classes. |
| 4903 | bool Invalid = false; |
| 4904 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4905 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 4906 | // Form the assignment: |
| 4907 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 4908 | QualType BaseType = Base->getType().getUnqualifiedType(); |
| 4909 | CXXRecordDecl *BaseClassDecl = 0; |
| 4910 | if (const RecordType *BaseRecordT = BaseType->getAs<RecordType>()) |
| 4911 | BaseClassDecl = cast<CXXRecordDecl>(BaseRecordT->getDecl()); |
| 4912 | else { |
| 4913 | Invalid = true; |
| 4914 | continue; |
| 4915 | } |
| 4916 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4917 | CXXCastPath BasePath; |
| 4918 | BasePath.push_back(Base); |
| 4919 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4920 | // Construct the "from" expression, which is an implicit cast to the |
| 4921 | // appropriately-qualified base type. |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4922 | Expr *From = OtherRef; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4923 | ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4924 | CK_UncheckedDerivedToBase, |
| 4925 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4926 | |
| 4927 | // Dereference "this". |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4928 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4929 | |
| 4930 | // Implicitly cast "this" to the appropriately-qualified base type. |
| 4931 | Expr *ToE = To.takeAs<Expr>(); |
| 4932 | ImpCastExprToType(ToE, |
| 4933 | Context.getCVRQualifiedType(BaseType, |
| 4934 | CopyAssignOperator->getTypeQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4935 | CK_UncheckedDerivedToBase, |
| 4936 | VK_LValue, &BasePath); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4937 | To = Owned(ToE); |
| 4938 | |
| 4939 | // Build the copy. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4940 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4941 | To.get(), From, |
| 4942 | /*CopyingBaseSubobject=*/true); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4943 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 4944 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4945 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 4946 | CopyAssignOperator->setInvalidDecl(); |
| 4947 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
| 4950 | // Success! Record the copy. |
| 4951 | Statements.push_back(Copy.takeAs<Expr>()); |
| 4952 | } |
| 4953 | |
| 4954 | // \brief Reference to the __builtin_memcpy function. |
| 4955 | Expr *BuiltinMemCpyRef = 0; |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 4956 | // \brief Reference to the __builtin_objc_memmove_collectable function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4957 | Expr *CollectableMemCpyRef = 0; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4958 | |
| 4959 | // Assign non-static members. |
| 4960 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4961 | FieldEnd = ClassDecl->field_end(); |
| 4962 | Field != FieldEnd; ++Field) { |
| 4963 | // Check for members of reference type; we can't copy those. |
| 4964 | if (Field->getType()->isReferenceType()) { |
| 4965 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 4966 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 4967 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 4968 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4969 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4970 | Invalid = true; |
| 4971 | continue; |
| 4972 | } |
| 4973 | |
| 4974 | // Check for members of const-qualified, non-class type. |
| 4975 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 4976 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 4977 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 4978 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 4979 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 4980 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4981 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4982 | Invalid = true; |
| 4983 | continue; |
| 4984 | } |
| 4985 | |
| 4986 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 4142ceb | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 4987 | if (FieldType->isIncompleteArrayType()) { |
| 4988 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 4989 | "Incomplete array type is not valid"); |
| 4990 | continue; |
| 4991 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4992 | |
| 4993 | // Build references to the field in the object we're copying from and to. |
| 4994 | CXXScopeSpec SS; // Intentionally empty |
| 4995 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 4996 | LookupMemberName); |
| 4997 | MemberLookup.addDecl(*Field); |
| 4998 | MemberLookup.resolveKind(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4999 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5000 | Loc, /*IsArrow=*/false, |
| 5001 | SS, 0, MemberLookup, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5002 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5003 | Loc, /*IsArrow=*/true, |
| 5004 | SS, 0, MemberLookup, 0); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5005 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 5006 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 5007 | |
| 5008 | // If the field should be copied with __builtin_memcpy rather than via |
| 5009 | // explicit assignments, do so. This optimization only applies for arrays |
| 5010 | // of scalars and arrays of class type with trivial copy-assignment |
| 5011 | // operators. |
| 5012 | if (FieldType->isArrayType() && |
| 5013 | (!BaseType->isRecordType() || |
| 5014 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()) |
| 5015 | ->hasTrivialCopyAssignment())) { |
| 5016 | // Compute the size of the memory buffer to be copied. |
| 5017 | QualType SizeType = Context.getSizeType(); |
| 5018 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 5019 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 5020 | for (const ConstantArrayType *Array |
| 5021 | = Context.getAsConstantArrayType(FieldType); |
| 5022 | Array; |
| 5023 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 5024 | llvm::APInt ArraySize |
| 5025 | = Array->getSize().zextOrTrunc(Size.getBitWidth()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5026 | Size *= ArraySize; |
| 5027 | } |
| 5028 | |
| 5029 | // Take the address of the field references for "from" and "to". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5030 | From = CreateBuiltinUnaryOp(Loc, UO_AddrOf, From.get()); |
| 5031 | To = CreateBuiltinUnaryOp(Loc, UO_AddrOf, To.get()); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5032 | |
| 5033 | bool NeedsCollectableMemCpy = |
| 5034 | (BaseType->isRecordType() && |
| 5035 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()); |
| 5036 | |
| 5037 | if (NeedsCollectableMemCpy) { |
| 5038 | if (!CollectableMemCpyRef) { |
Fariborz Jahanian | 8e2eab2 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 5039 | // Create a reference to the __builtin_objc_memmove_collectable function. |
| 5040 | LookupResult R(*this, |
| 5041 | &Context.Idents.get("__builtin_objc_memmove_collectable"), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5042 | Loc, LookupOrdinaryName); |
| 5043 | LookupName(R, TUScope, true); |
| 5044 | |
| 5045 | FunctionDecl *CollectableMemCpy = R.getAsSingle<FunctionDecl>(); |
| 5046 | if (!CollectableMemCpy) { |
| 5047 | // Something went horribly wrong earlier, and we will have |
| 5048 | // complained about it. |
| 5049 | Invalid = true; |
| 5050 | continue; |
| 5051 | } |
| 5052 | |
| 5053 | CollectableMemCpyRef = BuildDeclRefExpr(CollectableMemCpy, |
| 5054 | CollectableMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5055 | VK_LValue, Loc, 0).take(); |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5056 | assert(CollectableMemCpyRef && "Builtin reference cannot fail"); |
| 5057 | } |
| 5058 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5059 | // Create a reference to the __builtin_memcpy builtin function. |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5060 | else if (!BuiltinMemCpyRef) { |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5061 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 5062 | LookupOrdinaryName); |
| 5063 | LookupName(R, TUScope, true); |
| 5064 | |
| 5065 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 5066 | if (!BuiltinMemCpy) { |
| 5067 | // Something went horribly wrong earlier, and we will have complained |
| 5068 | // about it. |
| 5069 | Invalid = true; |
| 5070 | continue; |
| 5071 | } |
| 5072 | |
| 5073 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 5074 | BuiltinMemCpy->getType(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5075 | VK_LValue, Loc, 0).take(); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5076 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 5077 | } |
| 5078 | |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5079 | ASTOwningVector<Expr*> CallArgs(*this); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5080 | CallArgs.push_back(To.takeAs<Expr>()); |
| 5081 | CallArgs.push_back(From.takeAs<Expr>()); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5082 | CallArgs.push_back(IntegerLiteral::Create(Context, Size, SizeType, Loc)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5083 | ExprResult Call = ExprError(); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5084 | if (NeedsCollectableMemCpy) |
| 5085 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5086 | CollectableMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5087 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5088 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5089 | else |
| 5090 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5091 | BuiltinMemCpyRef, |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5092 | Loc, move_arg(CallArgs), |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5093 | Loc); |
Fariborz Jahanian | ff2d05f | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5094 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5095 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 5096 | Statements.push_back(Call.takeAs<Expr>()); |
| 5097 | continue; |
| 5098 | } |
| 5099 | |
| 5100 | // Build the copy of this field. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5101 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, FieldType, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5102 | To.get(), From.get(), |
Douglas Gregor | 6cdc161 | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 5103 | /*CopyingBaseSubobject=*/false); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5104 | if (Copy.isInvalid()) { |
Douglas Gregor | 60a8fbb | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 5105 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5106 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5107 | CopyAssignOperator->setInvalidDecl(); |
| 5108 | return; |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | // Success! Record the copy. |
| 5112 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 5113 | } |
| 5114 | |
| 5115 | if (!Invalid) { |
| 5116 | // Add a "return *this;" |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5117 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5118 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5119 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5120 | if (Return.isInvalid()) |
| 5121 | Invalid = true; |
| 5122 | else { |
| 5123 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5124 | |
| 5125 | if (Trap.hasErrorOccurred()) { |
| 5126 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5127 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5128 | Invalid = true; |
| 5129 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5130 | } |
| 5131 | } |
| 5132 | |
| 5133 | if (Invalid) { |
| 5134 | CopyAssignOperator->setInvalidDecl(); |
| 5135 | return; |
| 5136 | } |
| 5137 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5138 | StmtResult Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5139 | /*isStmtExpr=*/false); |
| 5140 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 5141 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5144 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 5145 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5146 | // C++ [class.copy]p4: |
| 5147 | // If the class definition does not explicitly declare a copy |
| 5148 | // constructor, one is declared implicitly. |
| 5149 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5150 | // C++ [class.copy]p5: |
| 5151 | // The implicitly-declared copy constructor for a class X will |
| 5152 | // have the form |
| 5153 | // |
| 5154 | // X::X(const X&) |
| 5155 | // |
| 5156 | // if |
| 5157 | bool HasConstCopyConstructor = true; |
| 5158 | |
| 5159 | // -- each direct or virtual base class B of X has a copy |
| 5160 | // constructor whose first parameter is of type const B& or |
| 5161 | // const volatile B&, and |
| 5162 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5163 | BaseEnd = ClassDecl->bases_end(); |
| 5164 | HasConstCopyConstructor && Base != BaseEnd; |
| 5165 | ++Base) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5166 | // Virtual bases are handled below. |
| 5167 | if (Base->isVirtual()) |
| 5168 | continue; |
| 5169 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5170 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5171 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5172 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5173 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5174 | |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5175 | HasConstCopyConstructor |
| 5176 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 5177 | } |
| 5178 | |
| 5179 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 5180 | BaseEnd = ClassDecl->vbases_end(); |
| 5181 | HasConstCopyConstructor && Base != BaseEnd; |
| 5182 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5183 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5184 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5185 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5186 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5187 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5188 | HasConstCopyConstructor |
| 5189 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 5190 | } |
| 5191 | |
| 5192 | // -- for all the nonstatic data members of X that are of a |
| 5193 | // class type M (or array thereof), each such class type |
| 5194 | // has a copy constructor whose first parameter is of type |
| 5195 | // const M& or const volatile M&. |
| 5196 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5197 | FieldEnd = ClassDecl->field_end(); |
| 5198 | HasConstCopyConstructor && Field != FieldEnd; |
| 5199 | ++Field) { |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5200 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5201 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5202 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5203 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5204 | if (!FieldClassDecl->hasDeclaredCopyConstructor()) |
| 5205 | DeclareImplicitCopyConstructor(FieldClassDecl); |
| 5206 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5207 | HasConstCopyConstructor |
Douglas Gregor | 598a854 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5208 | = FieldClassDecl->hasConstCopyConstructor(Context); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5209 | } |
| 5210 | } |
| 5211 | |
| 5212 | // Otherwise, the implicitly declared copy constructor will have |
| 5213 | // the form |
| 5214 | // |
| 5215 | // X::X(X&) |
| 5216 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 5217 | QualType ArgType = ClassType; |
| 5218 | if (HasConstCopyConstructor) |
| 5219 | ArgType = ArgType.withConst(); |
| 5220 | ArgType = Context.getLValueReferenceType(ArgType); |
| 5221 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5222 | // C++ [except.spec]p14: |
| 5223 | // An implicitly declared special member function (Clause 12) shall have an |
| 5224 | // exception-specification. [...] |
| 5225 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 5226 | unsigned Quals = HasConstCopyConstructor? Qualifiers::Const : 0; |
| 5227 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5228 | BaseEnd = ClassDecl->bases_end(); |
| 5229 | Base != BaseEnd; |
| 5230 | ++Base) { |
| 5231 | // Virtual bases are handled below. |
| 5232 | if (Base->isVirtual()) |
| 5233 | continue; |
| 5234 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5235 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5236 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5237 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5238 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5239 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5240 | if (CXXConstructorDecl *CopyConstructor |
| 5241 | = BaseClassDecl->getCopyConstructor(Context, Quals)) |
| 5242 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5243 | } |
| 5244 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 5245 | BaseEnd = ClassDecl->vbases_end(); |
| 5246 | Base != BaseEnd; |
| 5247 | ++Base) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5248 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5249 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5250 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5251 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5252 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5253 | if (CXXConstructorDecl *CopyConstructor |
| 5254 | = BaseClassDecl->getCopyConstructor(Context, Quals)) |
| 5255 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5256 | } |
| 5257 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5258 | FieldEnd = ClassDecl->field_end(); |
| 5259 | Field != FieldEnd; |
| 5260 | ++Field) { |
| 5261 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 5262 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5263 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5264 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5265 | if (!FieldClassDecl->hasDeclaredCopyConstructor()) |
| 5266 | DeclareImplicitCopyConstructor(FieldClassDecl); |
| 5267 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5268 | if (CXXConstructorDecl *CopyConstructor |
| 5269 | = FieldClassDecl->getCopyConstructor(Context, Quals)) |
| 5270 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5271 | } |
| 5272 | } |
| 5273 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5274 | // An implicitly-declared copy constructor is an inline public |
| 5275 | // member of its class. |
| 5276 | DeclarationName Name |
| 5277 | = Context.DeclarationNames.getCXXConstructorName( |
| 5278 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5279 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5280 | CXXConstructorDecl *CopyConstructor |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5281 | = CXXConstructorDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5282 | Context.getFunctionType(Context.VoidTy, |
| 5283 | &ArgType, 1, |
| 5284 | false, 0, |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5285 | ExceptSpec.hasExceptionSpecification(), |
| 5286 | ExceptSpec.hasAnyExceptionSpecification(), |
| 5287 | ExceptSpec.size(), |
| 5288 | ExceptSpec.data(), |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5289 | FunctionType::ExtInfo()), |
| 5290 | /*TInfo=*/0, |
| 5291 | /*isExplicit=*/false, |
| 5292 | /*isInline=*/true, |
| 5293 | /*isImplicitlyDeclared=*/true); |
| 5294 | CopyConstructor->setAccess(AS_public); |
| 5295 | CopyConstructor->setImplicit(); |
| 5296 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
| 5297 | |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5298 | // Note that we have declared this constructor. |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5299 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 5300 | |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5301 | // Add the parameter to the constructor. |
| 5302 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 5303 | ClassDecl->getLocation(), |
| 5304 | /*IdentifierInfo=*/0, |
| 5305 | ArgType, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5306 | SC_None, |
| 5307 | SC_None, 0); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5308 | CopyConstructor->setParams(&FromParam, 1); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5309 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5310 | PushOnScopeChains(CopyConstructor, S, false); |
| 5311 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 4a0c26f | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5312 | |
| 5313 | return CopyConstructor; |
| 5314 | } |
| 5315 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5316 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 5317 | CXXConstructorDecl *CopyConstructor, |
| 5318 | unsigned TypeQuals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5319 | assert((CopyConstructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 5320 | CopyConstructor->isCopyConstructor(TypeQuals) && |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 5321 | !CopyConstructor->isUsed(false)) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5322 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5323 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 5324 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5325 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5326 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5327 | ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor); |
Argyrios Kyrtzidis | 9c4eb1f | 2010-11-19 00:19:12 +0000 | [diff] [blame] | 5328 | DiagnosticErrorTrap Trap(Diags); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5329 | |
Douglas Gregor | c63d2c8 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5330 | if (SetBaseOrMemberInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) || |
| 5331 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 5332 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5333 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 5334 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5335 | } else { |
| 5336 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 5337 | CopyConstructor->getLocation(), |
| 5338 | MultiStmtArg(*this, 0, 0), |
| 5339 | /*isStmtExpr=*/false) |
| 5340 | .takeAs<Stmt>()); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 5341 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5342 | |
| 5343 | CopyConstructor->setUsed(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5344 | } |
| 5345 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5346 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 5347 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5348 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5349 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5350 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5351 | unsigned ConstructKind, |
| 5352 | SourceRange ParenRange) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5353 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5354 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5355 | // C++0x [class.copy]p34: |
| 5356 | // When certain criteria are met, an implementation is allowed to |
| 5357 | // omit the copy/move construction of a class object, even if the |
| 5358 | // copy/move constructor and/or destructor for the object have |
| 5359 | // side effects. [...] |
| 5360 | // - when a temporary class object that has not been bound to a |
| 5361 | // reference (12.2) would be copied/moved to a class object |
| 5362 | // with the same cv-unqualified type, the copy/move operation |
| 5363 | // can be omitted by constructing the temporary object |
| 5364 | // directly into the target of the omitted copy/move |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5365 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
| 5366 | Constructor->isCopyConstructor() && ExprArgs.size() >= 1) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5367 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5368 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5369 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5370 | |
| 5371 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5372 | Elidable, move(ExprArgs), RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5373 | ConstructKind, ParenRange); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5374 | } |
| 5375 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5376 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 5377 | /// including handling of its default argument expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5378 | ExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 5379 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 5380 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5381 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5382 | bool RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5383 | unsigned ConstructKind, |
| 5384 | SourceRange ParenRange) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 5385 | unsigned NumExprs = ExprArgs.size(); |
| 5386 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5387 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5388 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5389 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5390 | Constructor, Elidable, Exprs, NumExprs, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5391 | RequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5392 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind), |
| 5393 | ParenRange)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5394 | } |
| 5395 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5396 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5397 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 5398 | MultiExprArg Exprs) { |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5399 | // FIXME: Provide the correct paren SourceRange when available. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5400 | ExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 5401 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5402 | move(Exprs), false, CXXConstructExpr::CK_Complete, |
| 5403 | SourceRange()); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 5404 | if (TempResult.isInvalid()) |
| 5405 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5406 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 5407 | Expr *Temp = TempResult.takeAs<Expr>(); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5408 | CheckImplicitConversions(Temp, VD->getLocation()); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5409 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 5410 | Temp = MaybeCreateExprWithCleanups(Temp); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5411 | VD->setInit(Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5412 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 5413 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 5414 | } |
| 5415 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 5416 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
| 5417 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Douglas Gregor | 333de06 | 2010-02-25 18:11:54 +0000 | [diff] [blame] | 5418 | if (!ClassDecl->isInvalidDecl() && !VD->isInvalidDecl() && |
Douglas Gregor | fb2db46 | 2010-05-22 17:12:29 +0000 | [diff] [blame] | 5419 | !ClassDecl->hasTrivialDestructor() && !ClassDecl->isDependentContext()) { |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5420 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 5421 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5422 | CheckDestructorAccess(VD->getLocation(), Destructor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5423 | PDiag(diag::err_access_dtor_var) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5424 | << VD->getDeclName() |
| 5425 | << VD->getType()); |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 5426 | |
John McCall | ae79222 | 2010-09-18 05:25:11 +0000 | [diff] [blame] | 5427 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 5428 | if (!VD->isInvalidDecl() && VD->hasGlobalStorage() && !VD->isStaticLocal()) |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 5429 | Diag(VD->getLocation(), diag::warn_global_destructor); |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 5430 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5433 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5434 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 5435 | /// e.g: "int x(1);" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5436 | void Sema::AddCXXDirectInitializerToDecl(Decl *RealDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5437 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5438 | MultiExprArg Exprs, |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5439 | SourceLocation RParenLoc) { |
Daniel Dunbar | 5184626 | 2009-12-24 19:19:26 +0000 | [diff] [blame] | 5440 | assert(Exprs.size() != 0 && Exprs.get() && "missing expressions"); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5441 | |
| 5442 | // If there is no declaration, there was an error parsing it. Just ignore |
| 5443 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5444 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5445 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5446 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5447 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 5448 | if (!VDecl) { |
| 5449 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 5450 | RealDecl->setInvalidDecl(); |
| 5451 | return; |
| 5452 | } |
| 5453 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 5454 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 5455 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5456 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 5457 | // |
| 5458 | // Clients that want to distinguish between the two forms, can check for |
| 5459 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 5460 | // A major benefit is that clients that don't particularly care about which |
| 5461 | // exactly form was it (like the CodeGen) can handle both cases without |
| 5462 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 5463 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5464 | // C++ 8.5p11: |
| 5465 | // The form of initialization (using parentheses or '=') is generally |
| 5466 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 5467 | // class type. |
| 5468 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 5469 | if (!VDecl->getType()->isDependentType() && |
| 5470 | RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 5471 | diag::err_typecheck_decl_incomplete_type)) { |
| 5472 | VDecl->setInvalidDecl(); |
| 5473 | return; |
| 5474 | } |
| 5475 | |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5476 | // The variable can not have an abstract class type. |
| 5477 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 5478 | diag::err_abstract_type_in_decl, |
| 5479 | AbstractVariableType)) |
| 5480 | VDecl->setInvalidDecl(); |
| 5481 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 5482 | const VarDecl *Def; |
| 5483 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5484 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| 5485 | << VDecl->getDeclName(); |
| 5486 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5487 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 5488 | return; |
| 5489 | } |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 5490 | |
Douglas Gregor | 3a91abf | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 5491 | // C++ [class.static.data]p4 |
| 5492 | // If a static data member is of const integral or const |
| 5493 | // enumeration type, its declaration in the class definition can |
| 5494 | // specify a constant-initializer which shall be an integral |
| 5495 | // constant expression (5.19). In that case, the member can appear |
| 5496 | // in integral constant expressions. The member shall still be |
| 5497 | // defined in a namespace scope if it is used in the program and the |
| 5498 | // namespace scope definition shall not contain an initializer. |
| 5499 | // |
| 5500 | // We already performed a redefinition check above, but for static |
| 5501 | // data members we also need to check whether there was an in-class |
| 5502 | // declaration with an initializer. |
| 5503 | const VarDecl* PrevInit = 0; |
| 5504 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { |
| 5505 | Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName(); |
| 5506 | Diag(PrevInit->getLocation(), diag::note_previous_definition); |
| 5507 | return; |
| 5508 | } |
| 5509 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 5510 | // If either the declaration has a dependent type or if any of the |
| 5511 | // expressions is type-dependent, we represent the initialization |
| 5512 | // via a ParenListExpr for later use during template instantiation. |
| 5513 | if (VDecl->getType()->isDependentType() || |
| 5514 | Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { |
| 5515 | // Let clients know that initialization was done with a direct initializer. |
| 5516 | VDecl->setCXXDirectInitializer(true); |
| 5517 | |
| 5518 | // Store the initialization expressions as a ParenListExpr. |
| 5519 | unsigned NumExprs = Exprs.size(); |
| 5520 | VDecl->setInit(new (Context) ParenListExpr(Context, LParenLoc, |
| 5521 | (Expr **)Exprs.release(), |
| 5522 | NumExprs, RParenLoc)); |
| 5523 | return; |
| 5524 | } |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5525 | |
| 5526 | // Capture the variable that is being initialized and the style of |
| 5527 | // initialization. |
| 5528 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 5529 | |
| 5530 | // FIXME: Poor source location information. |
| 5531 | InitializationKind Kind |
| 5532 | = InitializationKind::CreateDirect(VDecl->getLocation(), |
| 5533 | LParenLoc, RParenLoc); |
| 5534 | |
| 5535 | InitializationSequence InitSeq(*this, Entity, Kind, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5536 | Exprs.get(), Exprs.size()); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5537 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(Exprs)); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5538 | if (Result.isInvalid()) { |
| 5539 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5540 | return; |
| 5541 | } |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 5542 | |
| 5543 | CheckImplicitConversions(Result.get(), LParenLoc); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5544 | |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 5545 | Result = MaybeCreateExprWithCleanups(Result); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5546 | VDecl->setInit(Result.takeAs<Expr>()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5547 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 5548 | |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 5549 | if (!VDecl->isInvalidDecl() && |
| 5550 | !VDecl->getDeclContext()->isDependentContext() && |
Sebastian Redl | 36281c6 | 2010-09-08 04:46:19 +0000 | [diff] [blame] | 5551 | VDecl->hasGlobalStorage() && !VDecl->isStaticLocal() && |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 5552 | !VDecl->getInit()->isConstantInitializer(Context, |
| 5553 | VDecl->getType()->isReferenceType())) |
| 5554 | Diag(VDecl->getLocation(), diag::warn_global_constructor) |
| 5555 | << VDecl->getInit()->getSourceRange(); |
| 5556 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 5557 | if (const RecordType *Record = VDecl->getType()->getAs<RecordType>()) |
| 5558 | FinalizeVarWithDestructor(VDecl, Record); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5559 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 5560 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5561 | /// \brief Given a constructor and the set of arguments provided for the |
| 5562 | /// constructor, convert the arguments and add any required default arguments |
| 5563 | /// to form a proper call to this constructor. |
| 5564 | /// |
| 5565 | /// \returns true if an error occurred, false otherwise. |
| 5566 | bool |
| 5567 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 5568 | MultiExprArg ArgsPtr, |
| 5569 | SourceLocation Loc, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5570 | ASTOwningVector<Expr*> &ConvertedArgs) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5571 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 5572 | unsigned NumArgs = ArgsPtr.size(); |
| 5573 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 5574 | |
| 5575 | const FunctionProtoType *Proto |
| 5576 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 5577 | assert(Proto && "Constructor without a prototype?"); |
| 5578 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5579 | |
| 5580 | // 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] | 5581 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5582 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 5583 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5584 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 5585 | |
| 5586 | VariadicCallType CallType = |
| 5587 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 5588 | llvm::SmallVector<Expr *, 8> AllArgs; |
| 5589 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 5590 | Proto, 0, Args, NumArgs, AllArgs, |
| 5591 | CallType); |
| 5592 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 5593 | ConvertedArgs.push_back(AllArgs[i]); |
| 5594 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 5595 | } |
| 5596 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5597 | static inline bool |
| 5598 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 5599 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5600 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5601 | if (isa<NamespaceDecl>(DC)) { |
| 5602 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5603 | diag::err_operator_new_delete_declared_in_namespace) |
| 5604 | << FnDecl->getDeclName(); |
| 5605 | } |
| 5606 | |
| 5607 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5608 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5609 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5610 | diag::err_operator_new_delete_declared_static) |
| 5611 | << FnDecl->getDeclName(); |
| 5612 | } |
| 5613 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 5614 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5617 | static inline bool |
| 5618 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 5619 | CanQualType ExpectedResultType, |
| 5620 | CanQualType ExpectedFirstParamType, |
| 5621 | unsigned DependentParamTypeDiag, |
| 5622 | unsigned InvalidParamTypeDiag) { |
| 5623 | QualType ResultType = |
| 5624 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5625 | |
| 5626 | // Check that the result type is not dependent. |
| 5627 | if (ResultType->isDependentType()) |
| 5628 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5629 | diag::err_operator_new_delete_dependent_result_type) |
| 5630 | << FnDecl->getDeclName() << ExpectedResultType; |
| 5631 | |
| 5632 | // Check that the result type is what we expect. |
| 5633 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 5634 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5635 | diag::err_operator_new_delete_invalid_result_type) |
| 5636 | << FnDecl->getDeclName() << ExpectedResultType; |
| 5637 | |
| 5638 | // A function template must have at least 2 parameters. |
| 5639 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 5640 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5641 | diag::err_operator_new_delete_template_too_few_parameters) |
| 5642 | << FnDecl->getDeclName(); |
| 5643 | |
| 5644 | // The function decl must have at least 1 parameter. |
| 5645 | if (FnDecl->getNumParams() == 0) |
| 5646 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5647 | diag::err_operator_new_delete_too_few_parameters) |
| 5648 | << FnDecl->getDeclName(); |
| 5649 | |
| 5650 | // Check the the first parameter type is not dependent. |
| 5651 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 5652 | if (FirstParamType->isDependentType()) |
| 5653 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 5654 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 5655 | |
| 5656 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 5657 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5658 | ExpectedFirstParamType) |
| 5659 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 5660 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 5661 | |
| 5662 | return false; |
| 5663 | } |
| 5664 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5665 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5666 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5667 | // C++ [basic.stc.dynamic.allocation]p1: |
| 5668 | // A program is ill-formed if an allocation function is declared in a |
| 5669 | // namespace scope other than global scope or declared static in global |
| 5670 | // scope. |
| 5671 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 5672 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5673 | |
| 5674 | CanQualType SizeTy = |
| 5675 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 5676 | |
| 5677 | // C++ [basic.stc.dynamic.allocation]p1: |
| 5678 | // The return type shall be void*. The first parameter shall have type |
| 5679 | // std::size_t. |
| 5680 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 5681 | SizeTy, |
| 5682 | diag::err_operator_new_dependent_param_type, |
| 5683 | diag::err_operator_new_param_type)) |
| 5684 | return true; |
| 5685 | |
| 5686 | // C++ [basic.stc.dynamic.allocation]p1: |
| 5687 | // The first parameter shall not have an associated default argument. |
| 5688 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5689 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5690 | diag::err_operator_new_default_arg) |
| 5691 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 5692 | |
| 5693 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5694 | } |
| 5695 | |
| 5696 | static bool |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5697 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 5698 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 5699 | // A program is ill-formed if deallocation functions are declared in a |
| 5700 | // namespace scope other than global scope or declared static in global |
| 5701 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5702 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 5703 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5704 | |
| 5705 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 5706 | // Each deallocation function shall return void and its first parameter |
| 5707 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5708 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 5709 | SemaRef.Context.VoidPtrTy, |
| 5710 | diag::err_operator_delete_dependent_param_type, |
| 5711 | diag::err_operator_delete_param_type)) |
| 5712 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5713 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5714 | return false; |
| 5715 | } |
| 5716 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5717 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 5718 | /// of this overloaded operator is well-formed. If so, returns false; |
| 5719 | /// otherwise, emits appropriate diagnostics and returns true. |
| 5720 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5721 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5722 | "Expected an overloaded operator declaration"); |
| 5723 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5724 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 5725 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5726 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5727 | // The allocation and deallocation functions, operator new, |
| 5728 | // operator new[], operator delete and operator delete[], are |
| 5729 | // described completely in 3.7.3. The attributes and restrictions |
| 5730 | // found in the rest of this subclause do not apply to them unless |
| 5731 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 5732 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5733 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 5734 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5735 | if (Op == OO_New || Op == OO_Array_New) |
| 5736 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5737 | |
| 5738 | // C++ [over.oper]p6: |
| 5739 | // An operator function shall either be a non-static member |
| 5740 | // function or be a non-member function and have at least one |
| 5741 | // parameter whose type is a class, a reference to a class, an |
| 5742 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5743 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 5744 | if (MethodDecl->isStatic()) |
| 5745 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5746 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5747 | } else { |
| 5748 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5749 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 5750 | ParamEnd = FnDecl->param_end(); |
| 5751 | Param != ParamEnd; ++Param) { |
| 5752 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 5753 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 5754 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5755 | ClassOrEnumParam = true; |
| 5756 | break; |
| 5757 | } |
| 5758 | } |
| 5759 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5760 | if (!ClassOrEnumParam) |
| 5761 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5762 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5763 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5764 | } |
| 5765 | |
| 5766 | // C++ [over.oper]p8: |
| 5767 | // An operator function cannot have default arguments (8.3.6), |
| 5768 | // except where explicitly stated below. |
| 5769 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5770 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5771 | // (C++ [over.call]p1). |
| 5772 | if (Op != OO_Call) { |
| 5773 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 5774 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5775 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5776 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5777 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5778 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5779 | } |
| 5780 | } |
| 5781 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5782 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 5783 | { false, false, false } |
| 5784 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 5785 | , { Unary, Binary, MemberOnly } |
| 5786 | #include "clang/Basic/OperatorKinds.def" |
| 5787 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5788 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5789 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 5790 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 5791 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5792 | |
| 5793 | // C++ [over.oper]p8: |
| 5794 | // [...] Operator functions cannot have more or fewer parameters |
| 5795 | // than the number required for the corresponding operator, as |
| 5796 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5797 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5798 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5799 | if (Op != OO_Call && |
| 5800 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 5801 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 5802 | (NumParams < 1) || (NumParams > 2))) { |
| 5803 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5804 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5805 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5806 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5807 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5808 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5809 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 5810 | assert(CanBeBinaryOperator && |
| 5811 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5812 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5813 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5814 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5815 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5816 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5817 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5818 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5819 | // Overloaded operators other than operator() cannot be variadic. |
| 5820 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5821 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5822 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5823 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5824 | } |
| 5825 | |
| 5826 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5827 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 5828 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5829 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5830 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5831 | } |
| 5832 | |
| 5833 | // C++ [over.inc]p1: |
| 5834 | // The user-defined function called operator++ implements the |
| 5835 | // prefix and postfix ++ operator. If this function is a member |
| 5836 | // function with no parameters, or a non-member function with one |
| 5837 | // parameter of class or enumeration type, it defines the prefix |
| 5838 | // increment operator ++ for objects of that type. If the function |
| 5839 | // is a member function with one parameter (which shall be of type |
| 5840 | // int) or a non-member function with two parameters (the second |
| 5841 | // of which shall be of type int), it defines the postfix |
| 5842 | // increment operator ++ for objects of that type. |
| 5843 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 5844 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 5845 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5846 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5847 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 5848 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 5849 | if (!ParamIsInt) |
| 5850 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5851 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5852 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5853 | } |
| 5854 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5855 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5856 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 5857 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5858 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 5859 | /// of this literal operator function is well-formed. If so, returns |
| 5860 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 5861 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 5862 | DeclContext *DC = FnDecl->getDeclContext(); |
| 5863 | Decl::Kind Kind = DC->getDeclKind(); |
| 5864 | if (Kind != Decl::TranslationUnit && Kind != Decl::Namespace && |
| 5865 | Kind != Decl::LinkageSpec) { |
| 5866 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 5867 | << FnDecl->getDeclName(); |
| 5868 | return true; |
| 5869 | } |
| 5870 | |
| 5871 | bool Valid = false; |
| 5872 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5873 | // template <char...> type operator "" name() is the only valid template |
| 5874 | // signature, and the only valid signature with no parameters. |
| 5875 | if (FnDecl->param_size() == 0) { |
| 5876 | if (FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate()) { |
| 5877 | // Must have only one template parameter |
| 5878 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 5879 | if (Params->size() == 1) { |
| 5880 | NonTypeTemplateParmDecl *PmDecl = |
| 5881 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5882 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5883 | // The template parameter must be a char parameter pack. |
| 5884 | // FIXME: This test will always fail because non-type parameter packs |
| 5885 | // have not been implemented. |
| 5886 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 5887 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 5888 | Valid = true; |
| 5889 | } |
| 5890 | } |
| 5891 | } else { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5892 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5893 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 5894 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5895 | QualType T = (*Param)->getType(); |
| 5896 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 5897 | // unsigned long long int, long double, and any character type are allowed |
| 5898 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5899 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 5900 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 5901 | Context.hasSameType(T, Context.CharTy) || |
| 5902 | Context.hasSameType(T, Context.WCharTy) || |
| 5903 | Context.hasSameType(T, Context.Char16Ty) || |
| 5904 | Context.hasSameType(T, Context.Char32Ty)) { |
| 5905 | if (++Param == FnDecl->param_end()) |
| 5906 | Valid = true; |
| 5907 | goto FinishedParams; |
| 5908 | } |
| 5909 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 5910 | // 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] | 5911 | const PointerType *PT = T->getAs<PointerType>(); |
| 5912 | if (!PT) |
| 5913 | goto FinishedParams; |
| 5914 | T = PT->getPointeeType(); |
| 5915 | if (!T.isConstQualified()) |
| 5916 | goto FinishedParams; |
| 5917 | T = T.getUnqualifiedType(); |
| 5918 | |
| 5919 | // Move on to the second parameter; |
| 5920 | ++Param; |
| 5921 | |
| 5922 | // If there is no second parameter, the first must be a const char * |
| 5923 | if (Param == FnDecl->param_end()) { |
| 5924 | if (Context.hasSameType(T, Context.CharTy)) |
| 5925 | Valid = true; |
| 5926 | goto FinishedParams; |
| 5927 | } |
| 5928 | |
| 5929 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 5930 | // are allowed as the first parameter to a two-parameter function |
| 5931 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 5932 | Context.hasSameType(T, Context.WCharTy) || |
| 5933 | Context.hasSameType(T, Context.Char16Ty) || |
| 5934 | Context.hasSameType(T, Context.Char32Ty))) |
| 5935 | goto FinishedParams; |
| 5936 | |
| 5937 | // The second and final parameter must be an std::size_t |
| 5938 | T = (*Param)->getType().getUnqualifiedType(); |
| 5939 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 5940 | ++Param == FnDecl->param_end()) |
| 5941 | Valid = true; |
| 5942 | } |
| 5943 | |
| 5944 | // FIXME: This diagnostic is absolutely terrible. |
| 5945 | FinishedParams: |
| 5946 | if (!Valid) { |
| 5947 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 5948 | << FnDecl->getDeclName(); |
| 5949 | return true; |
| 5950 | } |
| 5951 | |
| 5952 | return false; |
| 5953 | } |
| 5954 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5955 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 5956 | /// linkage specification, including the language and (if present) |
| 5957 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 5958 | /// the location of the language string literal, which is provided |
| 5959 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 5960 | /// the '{' brace. Otherwise, this linkage specification does not |
| 5961 | /// have any braces. |
Chris Lattner | 7d64271 | 2010-11-09 20:15:55 +0000 | [diff] [blame] | 5962 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, SourceLocation ExternLoc, |
| 5963 | SourceLocation LangLoc, |
| 5964 | llvm::StringRef Lang, |
| 5965 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5966 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 5967 | if (Lang == "\"C\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5968 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | d566381 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 5969 | else if (Lang == "\"C++\"") |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5970 | Language = LinkageSpecDecl::lang_cxx; |
| 5971 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5972 | Diag(LangLoc, diag::err_bad_language); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5973 | return 0; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5974 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5975 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5976 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5977 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5978 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5979 | LangLoc, Language, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5980 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5981 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5982 | PushDeclContext(S, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5983 | return D; |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5984 | } |
| 5985 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 5986 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5987 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 5988 | /// valid, it's the position of the closing '}' brace in a linkage |
| 5989 | /// specification that uses braces. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5990 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 5991 | Decl *LinkageSpec, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5992 | SourceLocation RBraceLoc) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5993 | if (LinkageSpec) |
| 5994 | PopDeclContext(); |
| 5995 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 5996 | } |
| 5997 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5998 | /// \brief Perform semantic analysis for the variable declaration that |
| 5999 | /// occurs within a C++ catch clause, returning the newly-created |
| 6000 | /// variable. |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6001 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 6002 | TypeSourceInfo *TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6003 | IdentifierInfo *Name, |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6004 | SourceLocation Loc) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6005 | bool Invalid = false; |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6006 | QualType ExDeclType = TInfo->getType(); |
| 6007 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6008 | // Arrays and functions decay. |
| 6009 | if (ExDeclType->isArrayType()) |
| 6010 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 6011 | else if (ExDeclType->isFunctionType()) |
| 6012 | ExDeclType = Context.getPointerType(ExDeclType); |
| 6013 | |
| 6014 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 6015 | // The exception-declaration shall not denote a pointer or reference to an |
| 6016 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6017 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6018 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6019 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6020 | Invalid = true; |
| 6021 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6022 | |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6023 | // GCC allows catching pointers and references to incomplete types |
| 6024 | // as an extension; so do we, but we warn by default. |
| 6025 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6026 | QualType BaseType = ExDeclType; |
| 6027 | 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] | 6028 | unsigned DK = diag::err_catch_incomplete; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6029 | bool IncompleteCatchIsInvalid = true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6030 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6031 | BaseType = Ptr->getPointeeType(); |
| 6032 | Mode = 1; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6033 | DK = diag::ext_catch_incomplete_ptr; |
| 6034 | IncompleteCatchIsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6035 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6036 | // 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] | 6037 | BaseType = Ref->getPointeeType(); |
| 6038 | Mode = 2; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6039 | DK = diag::ext_catch_incomplete_ref; |
| 6040 | IncompleteCatchIsInvalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6041 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 6042 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 6043 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK) && |
| 6044 | IncompleteCatchIsInvalid) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6045 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6046 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6047 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6048 | RequireNonAbstractType(Loc, ExDeclType, |
| 6049 | diag::err_abstract_type_in_decl, |
| 6050 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 6051 | Invalid = true; |
| 6052 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6053 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 6054 | // of ObjC types, and no runtime supports catching ObjC types by value. |
| 6055 | if (!Invalid && getLangOptions().ObjC1) { |
| 6056 | QualType T = ExDeclType; |
| 6057 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 6058 | T = RT->getPointeeType(); |
| 6059 | |
| 6060 | if (T->isObjCObjectType()) { |
| 6061 | Diag(Loc, diag::err_objc_object_catch); |
| 6062 | Invalid = true; |
| 6063 | } else if (T->isObjCObjectPointerType()) { |
| 6064 | if (!getLangOptions().NeXTRuntime) { |
| 6065 | Diag(Loc, diag::err_objc_pointer_cxx_catch_gnu); |
| 6066 | Invalid = true; |
| 6067 | } else if (!getLangOptions().ObjCNonFragileABI) { |
| 6068 | Diag(Loc, diag::err_objc_pointer_cxx_catch_fragile); |
| 6069 | Invalid = true; |
| 6070 | } |
| 6071 | } |
| 6072 | } |
| 6073 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6074 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6075 | Name, ExDeclType, TInfo, SC_None, |
| 6076 | SC_None); |
Douglas Gregor | 324b54d | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 6077 | ExDecl->setExceptionVariable(true); |
| 6078 | |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6079 | if (!Invalid) { |
| 6080 | if (const RecordType *RecordTy = ExDeclType->getAs<RecordType>()) { |
| 6081 | // C++ [except.handle]p16: |
| 6082 | // The object declared in an exception-declaration or, if the |
| 6083 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 6084 | // copy-initialized (8.5) from the exception object. [...] |
| 6085 | // The object is destroyed when the handler exits, after the destruction |
| 6086 | // of any automatic objects initialized within the handler. |
| 6087 | // |
| 6088 | // We just pretend to initialize the object with itself, then make sure |
| 6089 | // it can be destroyed later. |
| 6090 | InitializedEntity Entity = InitializedEntity::InitializeVariable(ExDecl); |
| 6091 | Expr *ExDeclRef = DeclRefExpr::Create(Context, 0, SourceRange(), ExDecl, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6092 | Loc, ExDeclType, VK_LValue, 0); |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6093 | InitializationKind Kind = InitializationKind::CreateCopy(Loc, |
| 6094 | SourceLocation()); |
| 6095 | InitializationSequence InitSeq(*this, Entity, Kind, &ExDeclRef, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6096 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 6097 | MultiExprArg(*this, &ExDeclRef, 1)); |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6098 | if (Result.isInvalid()) |
| 6099 | Invalid = true; |
| 6100 | else |
| 6101 | FinalizeVarWithDestructor(ExDecl, RecordTy); |
| 6102 | } |
| 6103 | } |
| 6104 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6105 | if (Invalid) |
| 6106 | ExDecl->setInvalidDecl(); |
| 6107 | |
| 6108 | return ExDecl; |
| 6109 | } |
| 6110 | |
| 6111 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 6112 | /// handler. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6113 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6114 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 6115 | QualType ExDeclType = TInfo->getType(); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6116 | |
| 6117 | bool Invalid = D.isInvalidType(); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6118 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6119 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 6120 | LookupOrdinaryName, |
| 6121 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6122 | // The scope should be freshly made just for us. There is just no way |
| 6123 | // it contains any previous declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6124 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6125 | if (PrevDecl->isTemplateParameter()) { |
| 6126 | // Maybe we will complain about the shadowed template parameter. |
| 6127 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6128 | } |
| 6129 | } |
| 6130 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6131 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6132 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 6133 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6134 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6135 | } |
| 6136 | |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6137 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6138 | D.getIdentifier(), |
Douglas Gregor | 83cb942 | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6139 | D.getIdentifierLoc()); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6140 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6141 | if (Invalid) |
| 6142 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6143 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6144 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6145 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6146 | PushOnScopeChains(ExDecl, S); |
| 6147 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6148 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6149 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6150 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6151 | return ExDecl; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6152 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6153 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6154 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6155 | Expr *AssertExpr, |
| 6156 | Expr *AssertMessageExpr_) { |
| 6157 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr_); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6158 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6159 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 6160 | llvm::APSInt Value(32); |
| 6161 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 6162 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 6163 | AssertExpr->getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6164 | return 0; |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6165 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6166 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6167 | if (Value == 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6168 | Diag(AssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 6169 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6170 | } |
| 6171 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6172 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6173 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6174 | AssertExpr, AssertMessage); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6175 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6176 | CurContext->addDecl(Decl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6177 | return Decl; |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6178 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6179 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6180 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 6181 | /// |
| 6182 | /// \returns A friend declaration that. |
| 6183 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation FriendLoc, |
| 6184 | TypeSourceInfo *TSInfo) { |
| 6185 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 6186 | |
| 6187 | QualType T = TSInfo->getType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6188 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6189 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 6190 | if (!getLangOptions().CPlusPlus0x) { |
| 6191 | // C++03 [class.friend]p2: |
| 6192 | // An elaborated-type-specifier shall be used in a friend declaration |
| 6193 | // for a class.* |
| 6194 | // |
| 6195 | // * The class-key of the elaborated-type-specifier is required. |
| 6196 | if (!ActiveTemplateInstantiations.empty()) { |
| 6197 | // Do not complain about the form of friend template types during |
| 6198 | // template instantiation; we will already have complained when the |
| 6199 | // template was declared. |
| 6200 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 6201 | // If we evaluated the type to a record type, suggest putting |
| 6202 | // a tag in front. |
| 6203 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 6204 | RecordDecl *RD = RT->getDecl(); |
| 6205 | |
| 6206 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 6207 | |
| 6208 | Diag(TypeRange.getBegin(), diag::ext_unelaborated_friend_type) |
| 6209 | << (unsigned) RD->getTagKind() |
| 6210 | << T |
| 6211 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 6212 | InsertionText); |
| 6213 | } else { |
| 6214 | Diag(FriendLoc, diag::ext_nonclass_type_friend) |
| 6215 | << T |
| 6216 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
| 6217 | } |
| 6218 | } else if (T->getAs<EnumType>()) { |
| 6219 | Diag(FriendLoc, diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6220 | << T |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6221 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6222 | } |
| 6223 | } |
| 6224 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 6225 | // C++0x [class.friend]p3: |
| 6226 | // If the type specifier in a friend declaration designates a (possibly |
| 6227 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 6228 | // the friend declaration is ignored. |
| 6229 | |
| 6230 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 6231 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6232 | |
| 6233 | return FriendDecl::Create(Context, CurContext, FriendLoc, TSInfo, FriendLoc); |
| 6234 | } |
| 6235 | |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 6236 | /// Handle a friend tag declaration where the scope specifier was |
| 6237 | /// templated. |
| 6238 | Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, |
| 6239 | unsigned TagSpec, SourceLocation TagLoc, |
| 6240 | CXXScopeSpec &SS, |
| 6241 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 6242 | AttributeList *Attr, |
| 6243 | MultiTemplateParamsArg TempParamLists) { |
| 6244 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6245 | |
| 6246 | bool isExplicitSpecialization = false; |
| 6247 | unsigned NumMatchedTemplateParamLists = TempParamLists.size(); |
| 6248 | bool Invalid = false; |
| 6249 | |
| 6250 | if (TemplateParameterList *TemplateParams |
| 6251 | = MatchTemplateParametersToScopeSpecifier(TagLoc, SS, |
| 6252 | TempParamLists.get(), |
| 6253 | TempParamLists.size(), |
| 6254 | /*friend*/ true, |
| 6255 | isExplicitSpecialization, |
| 6256 | Invalid)) { |
| 6257 | --NumMatchedTemplateParamLists; |
| 6258 | |
| 6259 | if (TemplateParams->size() > 0) { |
| 6260 | // This is a declaration of a class template. |
| 6261 | if (Invalid) |
| 6262 | return 0; |
| 6263 | |
| 6264 | return CheckClassTemplate(S, TagSpec, TUK_Friend, TagLoc, |
| 6265 | SS, Name, NameLoc, Attr, |
| 6266 | TemplateParams, AS_public).take(); |
| 6267 | } else { |
| 6268 | // The "template<>" header is extraneous. |
| 6269 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_tag_noparams) |
| 6270 | << TypeWithKeyword::getTagTypeKindName(Kind) << Name; |
| 6271 | isExplicitSpecialization = true; |
| 6272 | } |
| 6273 | } |
| 6274 | |
| 6275 | if (Invalid) return 0; |
| 6276 | |
| 6277 | assert(SS.isNotEmpty() && "valid templated tag with no SS and no direct?"); |
| 6278 | |
| 6279 | bool isAllExplicitSpecializations = true; |
| 6280 | for (unsigned I = 0; I != NumMatchedTemplateParamLists; ++I) { |
| 6281 | if (TempParamLists.get()[I]->size()) { |
| 6282 | isAllExplicitSpecializations = false; |
| 6283 | break; |
| 6284 | } |
| 6285 | } |
| 6286 | |
| 6287 | // FIXME: don't ignore attributes. |
| 6288 | |
| 6289 | // If it's explicit specializations all the way down, just forget |
| 6290 | // about the template header and build an appropriate non-templated |
| 6291 | // friend. TODO: for source fidelity, remember the headers. |
| 6292 | if (isAllExplicitSpecializations) { |
| 6293 | ElaboratedTypeKeyword Keyword |
| 6294 | = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 6295 | QualType T = CheckTypenameType(Keyword, SS.getScopeRep(), *Name, |
| 6296 | TagLoc, SS.getRange(), NameLoc); |
| 6297 | if (T.isNull()) |
| 6298 | return 0; |
| 6299 | |
| 6300 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6301 | if (isa<DependentNameType>(T)) { |
| 6302 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 6303 | TL.setKeywordLoc(TagLoc); |
| 6304 | TL.setQualifierRange(SS.getRange()); |
| 6305 | TL.setNameLoc(NameLoc); |
| 6306 | } else { |
| 6307 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
| 6308 | TL.setKeywordLoc(TagLoc); |
| 6309 | TL.setQualifierRange(SS.getRange()); |
| 6310 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(NameLoc); |
| 6311 | } |
| 6312 | |
| 6313 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 6314 | TSI, FriendLoc); |
| 6315 | Friend->setAccess(AS_public); |
| 6316 | CurContext->addDecl(Friend); |
| 6317 | return Friend; |
| 6318 | } |
| 6319 | |
| 6320 | // Handle the case of a templated-scope friend class. e.g. |
| 6321 | // template <class T> class A<T>::B; |
| 6322 | // FIXME: we don't support these right now. |
| 6323 | ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 6324 | QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); |
| 6325 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6326 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
| 6327 | TL.setKeywordLoc(TagLoc); |
| 6328 | TL.setQualifierRange(SS.getRange()); |
| 6329 | TL.setNameLoc(NameLoc); |
| 6330 | |
| 6331 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, |
| 6332 | TSI, FriendLoc); |
| 6333 | Friend->setAccess(AS_public); |
| 6334 | Friend->setUnsupportedFriend(true); |
| 6335 | CurContext->addDecl(Friend); |
| 6336 | return Friend; |
| 6337 | } |
| 6338 | |
| 6339 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6340 | /// Handle a friend type declaration. This works in tandem with |
| 6341 | /// ActOnTag. |
| 6342 | /// |
| 6343 | /// Notes on friend class templates: |
| 6344 | /// |
| 6345 | /// We generally treat friend class declarations as if they were |
| 6346 | /// declaring a class. So, for example, the elaborated type specifier |
| 6347 | /// in a friend declaration is required to obey the restrictions of a |
| 6348 | /// class-head (i.e. no typedefs in the scope chain), template |
| 6349 | /// parameters are required to match up with simple template-ids, &c. |
| 6350 | /// However, unlike when declaring a template specialization, it's |
| 6351 | /// okay to refer to a template specialization without an empty |
| 6352 | /// template parameter declaration, e.g. |
| 6353 | /// friend class A<T>::B<unsigned>; |
| 6354 | /// We permit this as a special case; if there are any template |
| 6355 | /// parameters present at all, require proper matching, i.e. |
| 6356 | /// template <> template <class T> friend class A<int>::B; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6357 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 6358 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6359 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6360 | |
| 6361 | assert(DS.isFriendSpecified()); |
| 6362 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 6363 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6364 | // Try to convert the decl specifier to a type. This works for |
| 6365 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 6366 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 6367 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6368 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 6369 | QualType T = TSI->getType(); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 6370 | if (TheDeclarator.isInvalidType()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6371 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6372 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6373 | // This is definitely an error in C++98. It's probably meant to |
| 6374 | // be forbidden in C++0x, too, but the specification is just |
| 6375 | // poorly written. |
| 6376 | // |
| 6377 | // The problem is with declarations like the following: |
| 6378 | // template <T> friend A<T>::foo; |
| 6379 | // where deciding whether a class C is a friend or not now hinges |
| 6380 | // on whether there exists an instantiation of A that causes |
| 6381 | // 'foo' to equal C. There are restrictions on class-heads |
| 6382 | // (which we declare (by fiat) elaborated friend declarations to |
| 6383 | // be) that makes this tractable. |
| 6384 | // |
| 6385 | // FIXME: handle "template <> friend class A<T>;", which |
| 6386 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 6387 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6388 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 6389 | << DS.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6390 | return 0; |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6391 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6392 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6393 | // C++98 [class.friend]p1: A friend of a class is a function |
| 6394 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 6395 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 6396 | // deadline. It's also a very silly restriction that seriously |
| 6397 | // affects inner classes and which nobody else seems to implement; |
| 6398 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6399 | // |
| 6400 | // But note that we could warn about it: it's always useless to |
| 6401 | // friend one of your own members (it's not, however, worthless to |
| 6402 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6403 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6404 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6405 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6406 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6407 | NumTempParamLists, |
John McCall | be04b6d | 2010-10-16 07:23:36 +0000 | [diff] [blame] | 6408 | TempParams.release(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6409 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6410 | DS.getFriendSpecLoc()); |
| 6411 | else |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6412 | D = CheckFriendTypeDecl(DS.getFriendSpecLoc(), TSI); |
| 6413 | |
| 6414 | if (!D) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6415 | return 0; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6416 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6417 | D->setAccess(AS_public); |
| 6418 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6419 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6420 | return D; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6421 | } |
| 6422 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6423 | Decl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, bool IsDefinition, |
| 6424 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6425 | const DeclSpec &DS = D.getDeclSpec(); |
| 6426 | |
| 6427 | assert(DS.isFriendSpecified()); |
| 6428 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 6429 | |
| 6430 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6431 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 6432 | QualType T = TInfo->getType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6433 | |
| 6434 | // C++ [class.friend]p1 |
| 6435 | // A friend of a class is a function or class.... |
| 6436 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6437 | // It *doesn't* see through dependent types, which is correct |
| 6438 | // according to [temp.arg.type]p3: |
| 6439 | // If a declaration acquires a function type through a |
| 6440 | // type dependent on a template-parameter and this causes |
| 6441 | // a declaration that does not use the syntactic form of a |
| 6442 | // function declarator to have a function type, the program |
| 6443 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6444 | if (!T->isFunctionType()) { |
| 6445 | Diag(Loc, diag::err_unexpected_friend); |
| 6446 | |
| 6447 | // It might be worthwhile to try to recover by creating an |
| 6448 | // appropriate declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6449 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6450 | } |
| 6451 | |
| 6452 | // C++ [namespace.memdef]p3 |
| 6453 | // - If a friend declaration in a non-local class first declares a |
| 6454 | // class or function, the friend class or function is a member |
| 6455 | // of the innermost enclosing namespace. |
| 6456 | // - The name of the friend is not found by simple name lookup |
| 6457 | // until a matching declaration is provided in that namespace |
| 6458 | // scope (either before or after the class declaration granting |
| 6459 | // friendship). |
| 6460 | // - If a friend function is called, its name may be found by the |
| 6461 | // name lookup that considers functions from namespaces and |
| 6462 | // classes associated with the types of the function arguments. |
| 6463 | // - When looking for a prior declaration of a class or a function |
| 6464 | // declared as a friend, scopes outside the innermost enclosing |
| 6465 | // namespace scope are not considered. |
| 6466 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6467 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6468 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6469 | DeclarationName Name = NameInfo.getName(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6470 | assert(Name); |
| 6471 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6472 | // The context we found the declaration in, or in which we should |
| 6473 | // create the declaration. |
| 6474 | DeclContext *DC; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 6475 | Scope *DCScope = S; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6476 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6477 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6478 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6479 | // FIXME: there are different rules in local classes |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6480 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6481 | // There are four cases here. |
| 6482 | // - 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] | 6483 | // appropriate scope and look for a function or function template |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6484 | // there as appropriate. |
| 6485 | // Recover from invalid scope qualifiers as if they just weren't there. |
| 6486 | if (SS.isInvalid() || !SS.isSet()) { |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6487 | // C++0x [namespace.memdef]p3: |
| 6488 | // If the name in a friend declaration is neither qualified nor |
| 6489 | // a template-id and the declaration is a function or an |
| 6490 | // elaborated-type-specifier, the lookup to determine whether |
| 6491 | // the entity has been previously declared shall not consider |
| 6492 | // any scopes outside the innermost enclosing namespace. |
| 6493 | // C++0x [class.friend]p11: |
| 6494 | // If a friend declaration appears in a local class and the name |
| 6495 | // specified is an unqualified name, a prior declaration is |
| 6496 | // looked up without considering scopes that are outside the |
| 6497 | // innermost enclosing non-class scope. For a friend function |
| 6498 | // declaration, if there is no prior declaration, the program is |
| 6499 | // ill-formed. |
| 6500 | bool isLocal = cast<CXXRecordDecl>(CurContext)->isLocalClass(); |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 6501 | bool isTemplateId = D.getName().getKind() == UnqualifiedId::IK_TemplateId; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6502 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6503 | // Find the appropriate context according to the above. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6504 | DC = CurContext; |
| 6505 | while (true) { |
| 6506 | // Skip class contexts. If someone can cite chapter and verse |
| 6507 | // for this behavior, that would be nice --- it's what GCC and |
| 6508 | // EDG do, and it seems like a reasonable intent, but the spec |
| 6509 | // really only says that checks for unqualified existing |
| 6510 | // declarations should stop at the nearest enclosing namespace, |
| 6511 | // not that they should only consider the nearest enclosing |
| 6512 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6513 | while (DC->isRecord()) |
| 6514 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6515 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6516 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6517 | |
| 6518 | // TODO: decide what we think about using declarations. |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6519 | if (isLocal || !Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6520 | break; |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6521 | |
John McCall | 8a40737 | 2010-10-14 22:22:28 +0000 | [diff] [blame] | 6522 | if (isTemplateId) { |
| 6523 | if (isa<TranslationUnitDecl>(DC)) break; |
| 6524 | } else { |
| 6525 | if (DC->isFileContext()) break; |
| 6526 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6527 | DC = DC->getParent(); |
| 6528 | } |
| 6529 | |
| 6530 | // C++ [class.friend]p1: A friend of a class is a function or |
| 6531 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 6532 | // C++0x changes this for both friend types and functions. |
| 6533 | // Most C++ 98 compilers do seem to give an error here, so |
| 6534 | // we do, too. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6535 | if (!Previous.empty() && DC->Equals(CurContext) |
| 6536 | && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6537 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6538 | |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 6539 | DCScope = getScopeForDeclContext(S, DC); |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6540 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6541 | // - There's a non-dependent scope specifier, in which case we |
| 6542 | // compute it and do a previous lookup there for a function |
| 6543 | // or function template. |
| 6544 | } else if (!SS.getScopeRep()->isDependent()) { |
| 6545 | DC = computeDeclContext(SS); |
| 6546 | if (!DC) return 0; |
| 6547 | |
| 6548 | if (RequireCompleteDeclContext(SS, DC)) return 0; |
| 6549 | |
| 6550 | LookupQualifiedName(Previous, DC); |
| 6551 | |
| 6552 | // Ignore things found implicitly in the wrong scope. |
| 6553 | // TODO: better diagnostics for this case. Suggesting the right |
| 6554 | // qualified scope would be nice... |
| 6555 | LookupResult::Filter F = Previous.makeFilter(); |
| 6556 | while (F.hasNext()) { |
| 6557 | NamedDecl *D = F.next(); |
| 6558 | if (!DC->InEnclosingNamespaceSetOf( |
| 6559 | D->getDeclContext()->getRedeclContext())) |
| 6560 | F.erase(); |
| 6561 | } |
| 6562 | F.done(); |
| 6563 | |
| 6564 | if (Previous.empty()) { |
| 6565 | D.setInvalidType(); |
| 6566 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 6567 | return 0; |
| 6568 | } |
| 6569 | |
| 6570 | // C++ [class.friend]p1: A friend of a class is a function or |
| 6571 | // class that is not a member of the class . . . |
| 6572 | if (DC->Equals(CurContext)) |
| 6573 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 6574 | |
| 6575 | // - There's a scope specifier that does not match any template |
| 6576 | // parameter lists, in which case we use some arbitrary context, |
| 6577 | // create a method or method template, and wait for instantiation. |
| 6578 | // - There's a scope specifier that does match some template |
| 6579 | // parameter lists, which we don't handle right now. |
| 6580 | } else { |
| 6581 | DC = CurContext; |
| 6582 | assert(isa<CXXRecordDecl>(DC) && "friend declaration not in class?"); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6583 | } |
| 6584 | |
John McCall | 29ae6e5 | 2010-10-13 05:45:15 +0000 | [diff] [blame] | 6585 | if (!DC->isRecord()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6586 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6587 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 6588 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 6589 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6590 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6591 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 6592 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6593 | return 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6594 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6595 | } |
| 6596 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6597 | bool Redeclaration = false; |
John McCall | 380aaa4 | 2010-10-13 06:22:15 +0000 | [diff] [blame] | 6598 | NamedDecl *ND = ActOnFunctionDeclarator(DCScope, D, DC, T, TInfo, Previous, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6599 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 6600 | IsDefinition, |
| 6601 | Redeclaration); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6602 | if (!ND) return 0; |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6603 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6604 | assert(ND->getDeclContext() == DC); |
| 6605 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 6606 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6607 | // Add the function declaration to the appropriate lookup tables, |
| 6608 | // adjusting the redeclarations list as necessary. We don't |
| 6609 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6610 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6611 | // Also update the scope-based lookup if the target context's |
| 6612 | // lookup context is in lexical scope. |
| 6613 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6614 | DC = DC->getRedeclContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6615 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6616 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6617 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6618 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6619 | |
| 6620 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6621 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6622 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 6623 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6624 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6625 | |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6626 | if (ND->isInvalidDecl()) |
| 6627 | FrD->setInvalidDecl(); |
John McCall | 6102ca1 | 2010-10-16 06:59:13 +0000 | [diff] [blame] | 6628 | else { |
| 6629 | FunctionDecl *FD; |
| 6630 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 6631 | FD = FTD->getTemplatedDecl(); |
| 6632 | else |
| 6633 | FD = cast<FunctionDecl>(ND); |
| 6634 | |
| 6635 | // Mark templated-scope function declarations as unsupported. |
| 6636 | if (FD->getNumTemplateParameterLists()) |
| 6637 | FrD->setUnsupportedFriend(true); |
| 6638 | } |
John McCall | 337ec3d | 2010-10-12 23:13:28 +0000 | [diff] [blame] | 6639 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6640 | return ND; |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 6641 | } |
| 6642 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6643 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 6644 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6645 | |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6646 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 6647 | if (!Fn) { |
| 6648 | Diag(DelLoc, diag::err_deleted_non_function); |
| 6649 | return; |
| 6650 | } |
| 6651 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 6652 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 6653 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 6654 | // If the declaration wasn't the first, we delete the function anyway for |
| 6655 | // recovery. |
| 6656 | } |
| 6657 | Fn->setDeleted(); |
| 6658 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 6659 | |
| 6660 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 6661 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 6662 | ++CI) { |
| 6663 | Stmt *SubStmt = *CI; |
| 6664 | if (!SubStmt) |
| 6665 | continue; |
| 6666 | if (isa<ReturnStmt>(SubStmt)) |
| 6667 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 6668 | diag::err_return_in_constructor_handler); |
| 6669 | if (!isa<Expr>(SubStmt)) |
| 6670 | SearchForReturnInStmt(Self, SubStmt); |
| 6671 | } |
| 6672 | } |
| 6673 | |
| 6674 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 6675 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 6676 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 6677 | SearchForReturnInStmt(*this, Handler); |
| 6678 | } |
| 6679 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6680 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6681 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6682 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6683 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 6684 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6685 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 6686 | if (Context.hasSameType(NewTy, OldTy) || |
| 6687 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6688 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6689 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6690 | // Check if the return types are covariant |
| 6691 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6692 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6693 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 6694 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 6695 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6696 | NewClassTy = NewPT->getPointeeType(); |
| 6697 | OldClassTy = OldPT->getPointeeType(); |
| 6698 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 6699 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 6700 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 6701 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 6702 | NewClassTy = NewRT->getPointeeType(); |
| 6703 | OldClassTy = OldRT->getPointeeType(); |
| 6704 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6705 | } |
| 6706 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6707 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6708 | // The return types aren't either both pointers or references to a class type. |
| 6709 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6710 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6711 | diag::err_different_return_type_for_overriding_virtual_function) |
| 6712 | << New->getDeclName() << NewTy << OldTy; |
| 6713 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6714 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6715 | return true; |
| 6716 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6717 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 6718 | // C++ [class.virtual]p6: |
| 6719 | // If the return type of D::f differs from the return type of B::f, the |
| 6720 | // class type in the return type of D::f shall be complete at the point of |
| 6721 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 6722 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 6723 | if (!RT->isBeingDefined() && |
| 6724 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 6725 | PDiag(diag::err_covariant_return_incomplete) |
| 6726 | << New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 6727 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 6728 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 6729 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6730 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6731 | // Check if the new class derives from the old class. |
| 6732 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 6733 | Diag(New->getLocation(), |
| 6734 | diag::err_covariant_return_not_derived) |
| 6735 | << New->getDeclName() << NewTy << OldTy; |
| 6736 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6737 | return true; |
| 6738 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6739 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6740 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 6741 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 6742 | diag::err_covariant_return_inaccessible_base, |
| 6743 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 6744 | // FIXME: Should this point to the return type? |
| 6745 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6746 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6747 | return true; |
| 6748 | } |
| 6749 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6750 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6751 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 6752 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6753 | Diag(New->getLocation(), |
| 6754 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6755 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6756 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6757 | return true; |
| 6758 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6759 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6760 | |
| 6761 | // The new class type must have the same or less qualifiers as the old type. |
| 6762 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 6763 | Diag(New->getLocation(), |
| 6764 | diag::err_covariant_return_type_class_type_more_qualified) |
| 6765 | << New->getDeclName() << NewTy << OldTy; |
| 6766 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6767 | return true; |
| 6768 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6769 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6770 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6771 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6772 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 6773 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
| 6774 | const CXXMethodDecl *Old) |
| 6775 | { |
| 6776 | if (Old->hasAttr<FinalAttr>()) { |
| 6777 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 6778 | << New->getDeclName(); |
| 6779 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6780 | return true; |
| 6781 | } |
| 6782 | |
| 6783 | return false; |
| 6784 | } |
| 6785 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 6786 | /// \brief Mark the given method pure. |
| 6787 | /// |
| 6788 | /// \param Method the method to be marked pure. |
| 6789 | /// |
| 6790 | /// \param InitRange the source range that covers the "0" initializer. |
| 6791 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
| 6792 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 6793 | Method->setPure(); |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 6794 | return false; |
| 6795 | } |
| 6796 | |
| 6797 | if (!Method->isInvalidDecl()) |
| 6798 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 6799 | << Method->getDeclName() << InitRange; |
| 6800 | return true; |
| 6801 | } |
| 6802 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6803 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 6804 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 6805 | /// is a fresh scope pushed for just this purpose. |
| 6806 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6807 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 6808 | /// static data member of class X, names should be looked up in the scope of |
| 6809 | /// class X. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6810 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6811 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6812 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6813 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6814 | // We should only get called for declarations with scope specifiers, like: |
| 6815 | // int foo::bar; |
| 6816 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6817 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6818 | } |
| 6819 | |
| 6820 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6821 | /// initializer for the out-of-line declaration 'D'. |
| 6822 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6823 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6824 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6825 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6826 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6827 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6828 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6829 | |
| 6830 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 6831 | /// C++ if/switch/while/for statement. |
| 6832 | /// e.g: "if (int x = f()) {...}" |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6833 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6834 | // C++ 6.4p2: |
| 6835 | // The declarator shall not specify a function or an array. |
| 6836 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 6837 | // new class or enumeration. |
| 6838 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 6839 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 6840 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6841 | TagDecl *OwnedTag = 0; |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6842 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag); |
| 6843 | QualType Ty = TInfo->getType(); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6844 | |
| 6845 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 6846 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 6847 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 6848 | Diag(D.getIdentifierLoc(), diag::err_invalid_use_of_function_type) |
| 6849 | << D.getSourceRange(); |
| 6850 | return DeclResult(); |
| 6851 | } else if (OwnedTag && OwnedTag->isDefinition()) { |
| 6852 | // The type-specifier-seq shall not declare a new class or enumeration. |
| 6853 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition); |
| 6854 | } |
| 6855 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6856 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6857 | if (!Dcl) |
| 6858 | return DeclResult(); |
| 6859 | |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6860 | return Dcl; |
| 6861 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6862 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6863 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 6864 | bool DefinitionRequired) { |
| 6865 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 6866 | // not have a vtable. |
| 6867 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 6868 | CurContext->isDependentContext() || |
| 6869 | ExprEvalContexts.back().Context == Unevaluated) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 6870 | return; |
| 6871 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6872 | // Try to insert this class into the map. |
| 6873 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 6874 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 6875 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 6876 | if (!Pos.second) { |
Daniel Dunbar | b9aefa7 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 6877 | // If we already had an entry, check to see if we are promoting this vtable |
| 6878 | // to required a definition. If so, we need to reappend to the VTableUses |
| 6879 | // list, since we may have already processed the first entry. |
| 6880 | if (DefinitionRequired && !Pos.first->second) { |
| 6881 | Pos.first->second = true; |
| 6882 | } else { |
| 6883 | // Otherwise, we can early exit. |
| 6884 | return; |
| 6885 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6886 | } |
| 6887 | |
| 6888 | // Local classes need to have their virtual members marked |
| 6889 | // immediately. For all other classes, we mark their virtual members |
| 6890 | // at the end of the translation unit. |
| 6891 | if (Class->isLocalClass()) |
| 6892 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 380c213 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 6893 | else |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6894 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | bbbe074 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 6895 | } |
| 6896 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6897 | bool Sema::DefineUsedVTables() { |
| 6898 | // If any dynamic classes have their key function defined within |
| 6899 | // this translation unit, then those vtables are considered "used" and must |
| 6900 | // be emitted. |
| 6901 | for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) { |
| 6902 | if (const CXXMethodDecl *KeyFunction |
| 6903 | = Context.getKeyFunction(DynamicClasses[I])) { |
| 6904 | const FunctionDecl *Definition = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 6905 | if (KeyFunction->hasBody(Definition)) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6906 | MarkVTableUsed(Definition->getLocation(), DynamicClasses[I], true); |
| 6907 | } |
| 6908 | } |
| 6909 | |
| 6910 | if (VTableUses.empty()) |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6911 | return false; |
| 6912 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6913 | // Note: The VTableUses vector could grow as a result of marking |
| 6914 | // the members of a class as "used", so we check the size each |
| 6915 | // time through the loop and prefer indices (with are stable) to |
| 6916 | // iterators (which are not). |
| 6917 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | e669f89 | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 6918 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6919 | if (!Class) |
| 6920 | continue; |
| 6921 | |
| 6922 | SourceLocation Loc = VTableUses[I].second; |
| 6923 | |
| 6924 | // If this class has a key function, but that key function is |
| 6925 | // defined in another translation unit, we don't need to emit the |
| 6926 | // vtable even though we're using it. |
| 6927 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(Class); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 6928 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6929 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 6930 | case TSK_Undeclared: |
| 6931 | case TSK_ExplicitSpecialization: |
| 6932 | case TSK_ExplicitInstantiationDeclaration: |
| 6933 | // The key function is in another translation unit. |
| 6934 | continue; |
| 6935 | |
| 6936 | case TSK_ExplicitInstantiationDefinition: |
| 6937 | case TSK_ImplicitInstantiation: |
| 6938 | // We will be instantiating the key function. |
| 6939 | break; |
| 6940 | } |
| 6941 | } else if (!KeyFunction) { |
| 6942 | // If we have a class with no key function that is the subject |
| 6943 | // of an explicit instantiation declaration, suppress the |
| 6944 | // vtable; it will live with the explicit instantiation |
| 6945 | // definition. |
| 6946 | bool IsExplicitInstantiationDeclaration |
| 6947 | = Class->getTemplateSpecializationKind() |
| 6948 | == TSK_ExplicitInstantiationDeclaration; |
| 6949 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 6950 | REnd = Class->redecls_end(); |
| 6951 | R != REnd; ++R) { |
| 6952 | TemplateSpecializationKind TSK |
| 6953 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 6954 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 6955 | IsExplicitInstantiationDeclaration = true; |
| 6956 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 6957 | IsExplicitInstantiationDeclaration = false; |
| 6958 | break; |
| 6959 | } |
| 6960 | } |
| 6961 | |
| 6962 | if (IsExplicitInstantiationDeclaration) |
| 6963 | continue; |
| 6964 | } |
| 6965 | |
| 6966 | // Mark all of the virtual members of this class as referenced, so |
| 6967 | // that we can build a vtable. Then, tell the AST consumer that a |
| 6968 | // vtable for this class is required. |
| 6969 | MarkVirtualMembersReferenced(Loc, Class); |
| 6970 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 6971 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 6972 | |
| 6973 | // Optionally warn if we're emitting a weak vtable. |
| 6974 | if (Class->getLinkage() == ExternalLinkage && |
| 6975 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 6976 | if (!KeyFunction || (KeyFunction->hasBody() && KeyFunction->isInlined())) |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6977 | Diag(Class->getLocation(), diag::warn_weak_vtable) << Class; |
| 6978 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6979 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6980 | VTableUses.clear(); |
| 6981 | |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6982 | return true; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6983 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6984 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 6985 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 6986 | const CXXRecordDecl *RD) { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6987 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 6988 | e = RD->method_end(); i != e; ++i) { |
| 6989 | CXXMethodDecl *MD = *i; |
| 6990 | |
| 6991 | // C++ [basic.def.odr]p2: |
| 6992 | // [...] A virtual member function is used if it is not pure. [...] |
| 6993 | if (MD->isVirtual() && !MD->isPure()) |
| 6994 | MarkDeclarationReferenced(Loc, MD); |
| 6995 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 6996 | |
| 6997 | // Only classes that have virtual bases need a VTT. |
| 6998 | if (RD->getNumVBases() == 0) |
| 6999 | return; |
| 7000 | |
| 7001 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 7002 | e = RD->bases_end(); i != e; ++i) { |
| 7003 | const CXXRecordDecl *Base = |
| 7004 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 7005 | if (Base->getNumVBases() == 0) |
| 7006 | continue; |
| 7007 | MarkVirtualMembersReferenced(Loc, Base); |
| 7008 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7009 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7010 | |
| 7011 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 7012 | /// Objective-C implementation whose ivars need be initialized. |
| 7013 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 7014 | if (!getLangOptions().CPlusPlus) |
| 7015 | return; |
Fariborz Jahanian | 2c18bb7 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 7016 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7017 | llvm::SmallVector<ObjCIvarDecl*, 8> ivars; |
| 7018 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 7019 | if (ivars.empty()) |
| 7020 | return; |
| 7021 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
| 7022 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 7023 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 7024 | if (Field->isInvalidDecl()) |
| 7025 | continue; |
| 7026 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7027 | CXXBaseOrMemberInitializer *Member; |
| 7028 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 7029 | InitializationKind InitKind = |
| 7030 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 7031 | |
| 7032 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7033 | ExprResult MemberInit = |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 7034 | InitSeq.Perform(*this, InitEntity, InitKind, MultiExprArg()); |
Douglas Gregor | 53c374f | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 7035 | MemberInit = MaybeCreateExprWithCleanups(MemberInit); |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7036 | // Note, MemberInit could actually come back empty if no initialization |
| 7037 | // is required (e.g., because it would call a trivial default constructor) |
| 7038 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 7039 | continue; |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 7040 | |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7041 | Member = |
| 7042 | new (Context) CXXBaseOrMemberInitializer(Context, |
| 7043 | Field, SourceLocation(), |
| 7044 | SourceLocation(), |
| 7045 | MemberInit.takeAs<Expr>(), |
| 7046 | SourceLocation()); |
| 7047 | AllToInit.push_back(Member); |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 7048 | |
| 7049 | // Be sure that the destructor is accessible and is marked as referenced. |
| 7050 | if (const RecordType *RecordTy |
| 7051 | = Context.getBaseElementType(Field->getType()) |
| 7052 | ->getAs<RecordType>()) { |
| 7053 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 7054 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Douglas Gregor | 68dd3ee | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 7055 | MarkDeclarationReferenced(Field->getLocation(), Destructor); |
| 7056 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 7057 | PDiag(diag::err_access_dtor_ivar) |
| 7058 | << Context.getBaseElementType(Field->getType())); |
| 7059 | } |
| 7060 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 7061 | } |
| 7062 | ObjCImplementation->setIvarInitializers(Context, |
| 7063 | AllToInit.data(), AllToInit.size()); |
| 7064 | } |
| 7065 | } |