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 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 15 | #include "SemaInit.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 16 | #include "Lookup.h" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 19 | #include "clang/AST/CharUnits.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
| 23 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 25 | #include "clang/AST/TypeOrdering.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 26 | #include "clang/Parse/DeclSpec.h" |
| 27 | #include "clang/Parse/Template.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 29 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 31 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 32 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace clang; |
| 35 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
| 37 | // CheckDefaultArgumentVisitor |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 42 | /// the default argument of a parameter to determine whether it |
| 43 | /// contains any ill-formed subexpressions. For example, this will |
| 44 | /// diagnose the use of local variables or parameters within the |
| 45 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 46 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 47 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 48 | Expr *DefaultArg; |
| 49 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 51 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 53 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 55 | bool VisitExpr(Expr *Node); |
| 56 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 57 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 58 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 60 | /// VisitExpr - Visit all of the children of this expression. |
| 61 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 62 | bool IsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | for (Stmt::child_iterator I = Node->child_begin(), |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 64 | E = Node->child_end(); I != E; ++I) |
| 65 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 66 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 69 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 70 | /// determine whether this declaration can be used in the default |
| 71 | /// argument expression. |
| 72 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 73 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 74 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 75 | // C++ [dcl.fct.default]p9 |
| 76 | // Default arguments are evaluated each time the function is |
| 77 | // called. The order of evaluation of function arguments is |
| 78 | // unspecified. Consequently, parameters of a function shall not |
| 79 | // be used in default argument expressions, even if they are not |
| 80 | // evaluated. Parameters of a function declared before a default |
| 81 | // argument expression are in scope and can hide namespace and |
| 82 | // class member names. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 84 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 85 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 86 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 87 | // C++ [dcl.fct.default]p7 |
| 88 | // Local variables shall not be used in default argument |
| 89 | // expressions. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 90 | if (VDecl->isBlockVarDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 92 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 93 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 94 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 95 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 96 | return false; |
| 97 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 99 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 100 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 101 | // C++ [dcl.fct.default]p8: |
| 102 | // The keyword this shall not be used in a default argument of a |
| 103 | // member function. |
| 104 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 105 | diag::err_param_default_argument_references_this) |
| 106 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 107 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 110 | bool |
| 111 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, ExprArg DefaultArg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | SourceLocation EqualLoc) { |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 113 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 114 | diag::err_typecheck_decl_incomplete_type)) { |
| 115 | Param->setInvalidDecl(); |
| 116 | return true; |
| 117 | } |
| 118 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 119 | Expr *Arg = (Expr *)DefaultArg.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 121 | // C++ [dcl.fct.default]p5 |
| 122 | // A default argument expression is implicitly converted (clause |
| 123 | // 4) to the parameter type. The default argument expression has |
| 124 | // the same semantic constraints as the initializer expression in |
| 125 | // a declaration of a variable of the parameter type, using the |
| 126 | // copy-initialization semantics (8.5). |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 127 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Param); |
| 128 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 129 | EqualLoc); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 130 | InitializationSequence InitSeq(*this, Entity, Kind, &Arg, 1); |
| 131 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
| 132 | MultiExprArg(*this, (void**)&Arg, 1)); |
| 133 | if (Result.isInvalid()) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 134 | return true; |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 135 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 136 | |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 137 | Arg = MaybeCreateCXXExprWithTemporaries(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 139 | // Okay: add the default argument to the parameter |
| 140 | Param->setDefaultArg(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 | DefaultArg.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 144 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 147 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 148 | /// provided for a function parameter is well-formed. If so, attach it |
| 149 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 150 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | Sema::ActOnParamDefaultArgument(DeclPtrTy param, SourceLocation EqualLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 152 | ExprArg defarg) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 153 | if (!param || !defarg.get()) |
| 154 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 156 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 157 | UnparsedDefaultArgLocs.erase(Param); |
| 158 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 159 | ExprOwningPtr<Expr> DefaultArg(this, defarg.takeAs<Expr>()); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 160 | |
| 161 | // Default arguments are only permitted in C++ |
| 162 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 163 | Diag(EqualLoc, diag::err_param_default_argument) |
| 164 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 165 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 169 | // Check that the default argument is well-formed |
| 170 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); |
| 171 | if (DefaultArgChecker.Visit(DefaultArg.get())) { |
| 172 | Param->setInvalidDecl(); |
| 173 | return; |
| 174 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 176 | SetParamDefaultArgument(Param, move(DefaultArg), EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 179 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 180 | /// argument for a function parameter, but we can't parse it yet |
| 181 | /// because we're inside a class definition. Note that this default |
| 182 | /// argument will be parsed later. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | void Sema::ActOnParamUnparsedDefaultArgument(DeclPtrTy param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 184 | SourceLocation EqualLoc, |
| 185 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 186 | if (!param) |
| 187 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 189 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 190 | if (Param) |
| 191 | Param->setUnparsedDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 193 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 196 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 197 | /// the default argument for the parameter param failed. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 198 | void Sema::ActOnParamDefaultArgumentError(DeclPtrTy param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 199 | if (!param) |
| 200 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 202 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
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 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 206 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 209 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 210 | /// arguments in the declarator, which is not a function declaration |
| 211 | /// or definition and therefore is not permitted to have default |
| 212 | /// arguments. This routine should be invoked for every declarator |
| 213 | /// that is not a function declaration or definition. |
| 214 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 215 | // C++ [dcl.fct.default]p3 |
| 216 | // A default argument expression shall be specified only in the |
| 217 | // parameter-declaration-clause of a function declaration or in a |
| 218 | // template-parameter (14.1). It shall not be specified for a |
| 219 | // parameter pack. If it is specified in a |
| 220 | // parameter-declaration-clause, it shall not occur within a |
| 221 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 222 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 223 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 224 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 225 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 226 | ParmVarDecl *Param = |
| 227 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 228 | if (Param->hasUnparsedDefaultArg()) { |
| 229 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 230 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 231 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 232 | delete Toks; |
| 233 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 234 | } else if (Param->getDefaultArg()) { |
| 235 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 236 | << Param->getDefaultArg()->getSourceRange(); |
| 237 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 244 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 245 | // function, once we already know that they have the same |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 246 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 247 | // error, false otherwise. |
| 248 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 249 | bool Invalid = false; |
| 250 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 251 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 252 | // For non-template functions, default arguments can be added in |
| 253 | // later declarations of a function in the same |
| 254 | // scope. Declarations in different scopes have completely |
| 255 | // distinct sets of default arguments. That is, declarations in |
| 256 | // inner scopes do not acquire default arguments from |
| 257 | // declarations in outer scopes, and vice versa. In a given |
| 258 | // function declaration, all parameters subsequent to a |
| 259 | // parameter with a default argument shall have default |
| 260 | // arguments supplied in this or previous declarations. A |
| 261 | // default argument shall not be redefined by a later |
| 262 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 263 | // |
| 264 | // C++ [dcl.fct.default]p6: |
| 265 | // Except for member functions of class templates, the default arguments |
| 266 | // in a member function definition that appears outside of the class |
| 267 | // definition are added to the set of default arguments provided by the |
| 268 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 269 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 270 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 271 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 272 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 273 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 274 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 275 | // hint here. Alternatively, we could walk the type-source information |
| 276 | // for NewParam to find the last source location in the type... but it |
| 277 | // isn't worth the effort right now. This is the kind of test case that |
| 278 | // is hard to get right: |
| 279 | |
| 280 | // int f(int); |
| 281 | // void g(int (*fp)(int) = f); |
| 282 | // void g(int (*fp)(int) = &f); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | Diag(NewParam->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 284 | diag::err_param_default_argument_redefinition) |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 285 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 286 | |
| 287 | // Look for the function declaration where the default argument was |
| 288 | // actually written, which may be a declaration prior to Old. |
| 289 | for (FunctionDecl *Older = Old->getPreviousDeclaration(); |
| 290 | Older; Older = Older->getPreviousDeclaration()) { |
| 291 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 292 | break; |
| 293 | |
| 294 | OldParam = Older->getParamDecl(p); |
| 295 | } |
| 296 | |
| 297 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 298 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 299 | Invalid = true; |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 300 | } else if (OldParam->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 301 | // Merge the old default argument into the new parameter |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 302 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 303 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 304 | NewParam->setUninstantiatedDefaultArg( |
| 305 | OldParam->getUninstantiatedDefaultArg()); |
| 306 | else |
| 307 | NewParam->setDefaultArg(OldParam->getDefaultArg()); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 308 | } else if (NewParam->hasDefaultArg()) { |
| 309 | if (New->getDescribedFunctionTemplate()) { |
| 310 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 311 | Diag(NewParam->getLocation(), |
| 312 | diag::err_param_default_argument_template_redecl) |
| 313 | << NewParam->getDefaultArgRange(); |
| 314 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 315 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 316 | } else if (New->getTemplateSpecializationKind() |
| 317 | != TSK_ImplicitInstantiation && |
| 318 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 319 | // C++ [temp.expr.spec]p21: |
| 320 | // Default function arguments shall not be specified in a declaration |
| 321 | // or a definition for one of the following explicit specializations: |
| 322 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 323 | // - the explicit specialization of a member function template; |
| 324 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 325 | // template where the class template specialization to which the |
| 326 | // member function specialization belongs is implicitly |
| 327 | // instantiated. |
| 328 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 329 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 330 | << New->getDeclName() |
| 331 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 332 | } else if (New->getDeclContext()->isDependentContext()) { |
| 333 | // C++ [dcl.fct.default]p6 (DR217): |
| 334 | // Default arguments for a member function of a class template shall |
| 335 | // be specified on the initial declaration of the member function |
| 336 | // within the class template. |
| 337 | // |
| 338 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 339 | // leads me to the conclusion that one cannot add default function |
| 340 | // arguments for an out-of-line definition of a member function of a |
| 341 | // dependent type. |
| 342 | int WhichKind = 2; |
| 343 | if (CXXRecordDecl *Record |
| 344 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 345 | if (Record->getDescribedClassTemplate()) |
| 346 | WhichKind = 0; |
| 347 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 348 | WhichKind = 1; |
| 349 | else |
| 350 | WhichKind = 2; |
| 351 | } |
| 352 | |
| 353 | Diag(NewParam->getLocation(), |
| 354 | diag::err_param_default_argument_member_template_redecl) |
| 355 | << WhichKind |
| 356 | << NewParam->getDefaultArgRange(); |
| 357 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 361 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 362 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 363 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 364 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 368 | /// function declaration are well-formed according to C++ |
| 369 | /// [dcl.fct.default]. |
| 370 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 371 | unsigned NumParams = FD->getNumParams(); |
| 372 | unsigned p; |
| 373 | |
| 374 | // Find first parameter with a default argument |
| 375 | for (p = 0; p < NumParams; ++p) { |
| 376 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 377 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | |
| 381 | // C++ [dcl.fct.default]p4: |
| 382 | // In a given function declaration, all parameters |
| 383 | // subsequent to a parameter with a default argument shall |
| 384 | // have default arguments supplied in this or previous |
| 385 | // declarations. A default argument shall not be redefined |
| 386 | // by a later declaration (not even to the same value). |
| 387 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 389 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 390 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 391 | if (Param->isInvalidDecl()) |
| 392 | /* We already complained about this parameter. */; |
| 393 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 395 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 396 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 397 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 399 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 401 | LastMissingDefaultArg = p; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | if (LastMissingDefaultArg > 0) { |
| 406 | // Some default arguments were missing. Clear out all of the |
| 407 | // default arguments up to (and including) the last missing |
| 408 | // default argument, so that we leave the function parameters |
| 409 | // in a semantically valid state. |
| 410 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 411 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 412 | if (Param->hasDefaultArg()) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 413 | if (!Param->hasUnparsedDefaultArg()) |
| 414 | Param->getDefaultArg()->Destroy(Context); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 415 | Param->setDefaultArg(0); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 420 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 421 | /// isCurrentClassName - Determine whether the identifier II is the |
| 422 | /// name of the class type currently being defined. In the case of |
| 423 | /// nested classes, this will only return true if II is the name of |
| 424 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 425 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 426 | const CXXScopeSpec *SS) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 427 | assert(getLangOptions().CPlusPlus && "No class names in C!"); |
| 428 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 429 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 430 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 431 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 432 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 433 | } else |
| 434 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 435 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 436 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 437 | return &II == CurDecl->getIdentifier(); |
| 438 | else |
| 439 | return false; |
| 440 | } |
| 441 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 443 | /// |
| 444 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 445 | /// and returns NULL otherwise. |
| 446 | CXXBaseSpecifier * |
| 447 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 448 | SourceRange SpecifierRange, |
| 449 | bool Virtual, AccessSpecifier Access, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | QualType BaseType, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 451 | SourceLocation BaseLoc) { |
| 452 | // C++ [class.union]p1: |
| 453 | // A union shall not have base classes. |
| 454 | if (Class->isUnion()) { |
| 455 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 456 | << SpecifierRange; |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | if (BaseType->isDependentType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 462 | Class->getTagKind() == RecordDecl::TK_class, |
| 463 | Access, BaseType); |
| 464 | |
| 465 | // Base specifiers must be record types. |
| 466 | if (!BaseType->isRecordType()) { |
| 467 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | // C++ [class.union]p1: |
| 472 | // A union shall not be used as a base class. |
| 473 | if (BaseType->isUnionType()) { |
| 474 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | // C++ [class.derived]p2: |
| 479 | // The class-name in a base-specifier shall not be an incompletely |
| 480 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 482 | PDiag(diag::err_incomplete_base_class) |
| 483 | << SpecifierRange)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 484 | return 0; |
| 485 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 486 | // 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] | 487 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 488 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 489 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 490 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 491 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 492 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 493 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 494 | // C++0x CWG Issue #817 indicates that [[final]] classes shouldn't be bases. |
| 495 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
| 496 | Diag(BaseLoc, diag::err_final_base) << BaseType.getAsString(); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 497 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 498 | << BaseType; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 499 | return 0; |
| 500 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 501 | |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 502 | SetClassDeclAttributesFromBase(Class, CXXBaseDecl, Virtual); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 503 | |
| 504 | // Create the base specifier. |
| 505 | // FIXME: Allocate via ASTContext? |
| 506 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
| 507 | Class->getTagKind() == RecordDecl::TK_class, |
| 508 | Access, BaseType); |
| 509 | } |
| 510 | |
| 511 | void Sema::SetClassDeclAttributesFromBase(CXXRecordDecl *Class, |
| 512 | const CXXRecordDecl *BaseClass, |
| 513 | bool BaseIsVirtual) { |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 514 | // A class with a non-empty base class is not empty. |
| 515 | // FIXME: Standard ref? |
| 516 | if (!BaseClass->isEmpty()) |
| 517 | Class->setEmpty(false); |
| 518 | |
| 519 | // C++ [class.virtual]p1: |
| 520 | // A class that [...] inherits a virtual function is called a polymorphic |
| 521 | // class. |
| 522 | if (BaseClass->isPolymorphic()) |
| 523 | Class->setPolymorphic(true); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 524 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 525 | // C++ [dcl.init.aggr]p1: |
| 526 | // An aggregate is [...] a class with [...] no base classes [...]. |
| 527 | Class->setAggregate(false); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 528 | |
| 529 | // C++ [class]p4: |
| 530 | // A POD-struct is an aggregate class... |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 531 | Class->setPOD(false); |
| 532 | |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 533 | if (BaseIsVirtual) { |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 534 | // C++ [class.ctor]p5: |
| 535 | // A constructor is trivial if its class has no virtual base classes. |
| 536 | Class->setHasTrivialConstructor(false); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 537 | |
| 538 | // C++ [class.copy]p6: |
| 539 | // A copy constructor is trivial if its class has no virtual base classes. |
| 540 | Class->setHasTrivialCopyConstructor(false); |
| 541 | |
| 542 | // C++ [class.copy]p11: |
| 543 | // A copy assignment operator is trivial if its class has no virtual |
| 544 | // base classes. |
| 545 | Class->setHasTrivialCopyAssignment(false); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 546 | |
| 547 | // C++0x [meta.unary.prop] is_empty: |
| 548 | // T is a class type, but not a union type, with ... no virtual base |
| 549 | // classes |
| 550 | Class->setEmpty(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 551 | } else { |
| 552 | // C++ [class.ctor]p5: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | // A constructor is trivial if all the direct base classes of its |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 554 | // class have trivial constructors. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 555 | if (!BaseClass->hasTrivialConstructor()) |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 556 | Class->setHasTrivialConstructor(false); |
| 557 | |
| 558 | // C++ [class.copy]p6: |
| 559 | // A copy constructor is trivial if all the direct base classes of its |
| 560 | // class have trivial copy constructors. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 561 | if (!BaseClass->hasTrivialCopyConstructor()) |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 562 | Class->setHasTrivialCopyConstructor(false); |
| 563 | |
| 564 | // C++ [class.copy]p11: |
| 565 | // A copy assignment operator is trivial if all the direct base classes |
| 566 | // of its class have trivial copy assignment operators. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 567 | if (!BaseClass->hasTrivialCopyAssignment()) |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 568 | Class->setHasTrivialCopyAssignment(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 569 | } |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 570 | |
| 571 | // C++ [class.ctor]p3: |
| 572 | // A destructor is trivial if all the direct base classes of its class |
| 573 | // have trivial destructors. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 574 | if (!BaseClass->hasTrivialDestructor()) |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 575 | Class->setHasTrivialDestructor(false); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 578 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 579 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | /// example: |
| 581 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 582 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | Sema::BaseResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 584 | Sema::ActOnBaseSpecifier(DeclPtrTy classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 585 | bool Virtual, AccessSpecifier Access, |
| 586 | TypeTy *basetype, SourceLocation BaseLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 587 | if (!classdecl) |
| 588 | return true; |
| 589 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 590 | AdjustDeclIfTemplate(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 591 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl.getAs<Decl>()); |
| 592 | if (!Class) |
| 593 | return true; |
| 594 | |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 595 | QualType BaseType = GetTypeFromParser(basetype); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 596 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
| 597 | Virtual, Access, |
| 598 | BaseType, BaseLoc)) |
| 599 | return BaseSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 601 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 602 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 603 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 604 | /// \brief Performs the actual work of attaching the given base class |
| 605 | /// specifiers to a C++ class. |
| 606 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 607 | unsigned NumBases) { |
| 608 | if (NumBases == 0) |
| 609 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 610 | |
| 611 | // Used to keep track of which base types we have already seen, so |
| 612 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 613 | // that the key is always the unqualified canonical type of the base |
| 614 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 615 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 616 | |
| 617 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 618 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 619 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 620 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 622 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 623 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 624 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 625 | if (KnownBaseTypes[NewBaseType]) { |
| 626 | // C++ [class.mi]p3: |
| 627 | // A class shall not be specified as a direct base class of a |
| 628 | // derived class more than once. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 629 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 630 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 631 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 632 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 633 | |
| 634 | // Delete the duplicate base class specifier; we're going to |
| 635 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 636 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 637 | |
| 638 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 639 | } else { |
| 640 | // Okay, add this new base class. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 641 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 642 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | |
| 646 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 647 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 648 | |
| 649 | // Delete the remaining (good) base class specifiers, since their |
| 650 | // data has been copied into the CXXRecordDecl. |
| 651 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 652 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 653 | |
| 654 | return Invalid; |
| 655 | } |
| 656 | |
| 657 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 658 | /// class, after checking whether there are any duplicate base |
| 659 | /// classes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | void Sema::ActOnBaseSpecifiers(DeclPtrTy ClassDecl, BaseTy **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 661 | unsigned NumBases) { |
| 662 | if (!ClassDecl || !Bases || !NumBases) |
| 663 | return; |
| 664 | |
| 665 | AdjustDeclIfTemplate(ClassDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 666 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl.getAs<Decl>()), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 667 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 668 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 669 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 670 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 671 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 672 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 673 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 674 | return ICT->getDecl(); |
| 675 | else |
| 676 | return 0; |
| 677 | } |
| 678 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 679 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 680 | /// derived from the type \p Base. |
| 681 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 682 | if (!getLangOptions().CPlusPlus) |
| 683 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 684 | |
| 685 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 686 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 687 | return false; |
| 688 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 689 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 690 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 691 | return false; |
| 692 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 693 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 694 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 698 | /// derived from the type \p Base. |
| 699 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 700 | if (!getLangOptions().CPlusPlus) |
| 701 | return false; |
| 702 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 703 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 704 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 705 | return false; |
| 706 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 707 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 708 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 709 | return false; |
| 710 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 711 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 712 | } |
| 713 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 714 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
| 715 | CXXBaseSpecifierArray &BasePathArray) { |
| 716 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 717 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 718 | |
| 719 | const CXXBasePath &Path = Paths.front(); |
| 720 | |
| 721 | // We first go backward and check if we have a virtual base. |
| 722 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 723 | // the nearest virtual base. |
| 724 | unsigned Start = 0; |
| 725 | for (unsigned I = Path.size(); I != 0; --I) { |
| 726 | if (Path[I - 1].Base->isVirtual()) { |
| 727 | Start = I - 1; |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | // Now add all bases. |
| 733 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
| 734 | BasePathArray.push_back(Path[I].Base); |
| 735 | } |
| 736 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 737 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 738 | /// conversion (where Derived and Base are class types) is |
| 739 | /// well-formed, meaning that the conversion is unambiguous (and |
| 740 | /// that all of the base classes are accessible). Returns true |
| 741 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 742 | /// otherwise. Loc is the location where this routine should point to |
| 743 | /// if there is an error, and Range is the source range to highlight |
| 744 | /// if there is an error. |
| 745 | bool |
| 746 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 747 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 748 | unsigned AmbigiousBaseConvID, |
| 749 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 750 | DeclarationName Name, |
| 751 | CXXBaseSpecifierArray *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 752 | // First, determine whether the path from Derived to Base is |
| 753 | // ambiguous. This is slightly more expensive than checking whether |
| 754 | // the Derived to Base conversion exists, because here we need to |
| 755 | // explore multiple paths to determine if there is an ambiguity. |
| 756 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 757 | /*DetectVirtual=*/false); |
| 758 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 759 | assert(DerivationOkay && |
| 760 | "Can only be used with a derived-to-base conversion"); |
| 761 | (void)DerivationOkay; |
| 762 | |
| 763 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 764 | if (InaccessibleBaseID) { |
| 765 | // Check that the base class can be accessed. |
| 766 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 767 | InaccessibleBaseID)) { |
| 768 | case AR_inaccessible: |
| 769 | return true; |
| 770 | case AR_accessible: |
| 771 | case AR_dependent: |
| 772 | case AR_delayed: |
| 773 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 774 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 775 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 776 | |
| 777 | // Build a base path if necessary. |
| 778 | if (BasePath) |
| 779 | BuildBasePathArray(Paths, *BasePath); |
| 780 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | // We know that the derived-to-base conversion is ambiguous, and |
| 784 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 785 | // search just one more time to compute all of the possible paths so |
| 786 | // that we can print them out. This is more expensive than any of |
| 787 | // the previous derived-to-base checks we've done, but at this point |
| 788 | // performance isn't as much of an issue. |
| 789 | Paths.clear(); |
| 790 | Paths.setRecordingPaths(true); |
| 791 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 792 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 793 | (void)StillOkay; |
| 794 | |
| 795 | // Build up a textual representation of the ambiguous paths, e.g., |
| 796 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 797 | // conversions in the diagnostic. We only print one of the paths |
| 798 | // to each base class subobject. |
| 799 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 800 | |
| 801 | Diag(Loc, AmbigiousBaseConvID) |
| 802 | << Derived << Base << PathDisplayStr << Range << Name; |
| 803 | return true; |
| 804 | } |
| 805 | |
| 806 | bool |
| 807 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 808 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 809 | CXXBaseSpecifierArray *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 810 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 811 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 812 | IgnoreAccess ? 0 |
| 813 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 814 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 815 | Loc, Range, DeclarationName(), |
| 816 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | |
| 820 | /// @brief Builds a string representing ambiguous paths from a |
| 821 | /// specific derived class to different subobjects of the same base |
| 822 | /// class. |
| 823 | /// |
| 824 | /// This function builds a string that can be used in error messages |
| 825 | /// to show the different paths that one can take through the |
| 826 | /// inheritance hierarchy to go from the derived class to different |
| 827 | /// subobjects of a base class. The result looks something like this: |
| 828 | /// @code |
| 829 | /// struct D -> struct B -> struct A |
| 830 | /// struct D -> struct C -> struct A |
| 831 | /// @endcode |
| 832 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 833 | std::string PathDisplayStr; |
| 834 | std::set<unsigned> DisplayedPaths; |
| 835 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 836 | Path != Paths.end(); ++Path) { |
| 837 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 838 | // We haven't displayed a path to this particular base |
| 839 | // class subobject yet. |
| 840 | PathDisplayStr += "\n "; |
| 841 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 842 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 843 | Element != Path->end(); ++Element) |
| 844 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | return PathDisplayStr; |
| 849 | } |
| 850 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 851 | //===----------------------------------------------------------------------===// |
| 852 | // C++ class member Handling |
| 853 | //===----------------------------------------------------------------------===// |
| 854 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 855 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 856 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 857 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
Chris Lattner | b6688e0 | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 858 | /// any. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 859 | Sema::DeclPtrTy |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 860 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 861 | MultiTemplateParamsArg TemplateParameterLists, |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 862 | ExprTy *BW, ExprTy *InitExpr, bool IsDefinition, |
| 863 | bool Deleted) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 864 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 865 | DeclarationName Name = GetNameForDeclarator(D); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 866 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 867 | Expr *Init = static_cast<Expr*>(InitExpr); |
| 868 | SourceLocation Loc = D.getIdentifierLoc(); |
| 869 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 870 | bool isFunc = D.isFunctionDeclarator(); |
| 871 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 872 | assert(!DS.isFriendSpecified()); |
| 873 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 874 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 875 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 876 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 877 | // data members and cannot be applied to names declared const or static, |
| 878 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 879 | switch (DS.getStorageClassSpec()) { |
| 880 | case DeclSpec::SCS_unspecified: |
| 881 | case DeclSpec::SCS_typedef: |
| 882 | case DeclSpec::SCS_static: |
| 883 | // FALL THROUGH. |
| 884 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 885 | case DeclSpec::SCS_mutable: |
| 886 | if (isFunc) { |
| 887 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 888 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 889 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 890 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 892 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 893 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 894 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 895 | } else { |
| 896 | QualType T = GetTypeForDeclarator(D, S); |
| 897 | diag::kind err = static_cast<diag::kind>(0); |
| 898 | if (T->isReferenceType()) |
| 899 | err = diag::err_mutable_reference; |
| 900 | else if (T.isConstQualified()) |
| 901 | err = diag::err_mutable_const; |
| 902 | if (err != 0) { |
| 903 | if (DS.getStorageClassSpecLoc().isValid()) |
| 904 | Diag(DS.getStorageClassSpecLoc(), err); |
| 905 | else |
| 906 | Diag(DS.getThreadSpecLoc(), err); |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 907 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 908 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 909 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 910 | } |
| 911 | } |
| 912 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 913 | default: |
| 914 | if (DS.getStorageClassSpecLoc().isValid()) |
| 915 | Diag(DS.getStorageClassSpecLoc(), |
| 916 | diag::err_storageclass_invalid_for_member); |
| 917 | else |
| 918 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 919 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 920 | } |
| 921 | |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 922 | if (!isFunc && |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 923 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename && |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 924 | D.getNumTypeObjects() == 0) { |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 925 | // Check also for this case: |
| 926 | // |
| 927 | // typedef int f(); |
| 928 | // f a; |
| 929 | // |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 930 | QualType TDType = GetTypeFromParser(DS.getTypeRep()); |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 931 | isFunc = TDType->isFunctionType(); |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 932 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 933 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 934 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 935 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 936 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 937 | |
| 938 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 939 | if (isInstField) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 940 | // FIXME: Check for template parameters! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 941 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 942 | AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 943 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 944 | } else { |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 945 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition) |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 946 | .getAs<Decl>(); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 947 | if (!Member) { |
| 948 | if (BitWidth) DeleteExpr(BitWidth); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 949 | return DeclPtrTy(); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 950 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 951 | |
| 952 | // Non-instance-fields can't have a bitfield. |
| 953 | if (BitWidth) { |
| 954 | if (Member->isInvalidDecl()) { |
| 955 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 956 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 957 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 958 | // "static member 'A' cannot be a bit-field" |
| 959 | Diag(Loc, diag::err_static_not_bitfield) |
| 960 | << Name << BitWidth->getSourceRange(); |
| 961 | } else if (isa<TypedefDecl>(Member)) { |
| 962 | // "typedef member 'x' cannot be a bit-field" |
| 963 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 964 | << Name << BitWidth->getSourceRange(); |
| 965 | } else { |
| 966 | // A function typedef ("typedef int f(); f a;"). |
| 967 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 968 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 970 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 971 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 973 | DeleteExpr(BitWidth); |
| 974 | BitWidth = 0; |
| 975 | Member->setInvalidDecl(); |
| 976 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 977 | |
| 978 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 980 | // If we have declared a member function template, set the access of the |
| 981 | // templated declaration as well. |
| 982 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 983 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 984 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 985 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 986 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 987 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 988 | if (Init) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 989 | AddInitializerToDecl(DeclPtrTy::make(Member), ExprArg(*this, Init), false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 990 | if (Deleted) // FIXME: Source location is not very good. |
| 991 | SetDeclDeleted(DeclPtrTy::make(Member), D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 992 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 993 | if (isInstField) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 994 | FieldCollector->Add(cast<FieldDecl>(Member)); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 995 | return DeclPtrTy(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 996 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 997 | return DeclPtrTy::make(Member); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1000 | /// \brief Find the direct and/or virtual base specifiers that |
| 1001 | /// correspond to the given base type, for use in base initialization |
| 1002 | /// within a constructor. |
| 1003 | static bool FindBaseInitializer(Sema &SemaRef, |
| 1004 | CXXRecordDecl *ClassDecl, |
| 1005 | QualType BaseType, |
| 1006 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 1007 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 1008 | // First, check for a direct base class. |
| 1009 | DirectBaseSpec = 0; |
| 1010 | for (CXXRecordDecl::base_class_const_iterator Base |
| 1011 | = ClassDecl->bases_begin(); |
| 1012 | Base != ClassDecl->bases_end(); ++Base) { |
| 1013 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 1014 | // We found a direct base of this type. That's what we're |
| 1015 | // initializing. |
| 1016 | DirectBaseSpec = &*Base; |
| 1017 | break; |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | // Check for a virtual base class. |
| 1022 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1023 | // there are no virtual bases. |
| 1024 | VirtualBaseSpec = 0; |
| 1025 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1026 | // We haven't found a base yet; search the class hierarchy for a |
| 1027 | // virtual base class. |
| 1028 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1029 | /*DetectVirtual=*/false); |
| 1030 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1031 | BaseType, Paths)) { |
| 1032 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1033 | Path != Paths.end(); ++Path) { |
| 1034 | if (Path->back().Base->isVirtual()) { |
| 1035 | VirtualBaseSpec = Path->back().Base; |
| 1036 | break; |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | return DirectBaseSpec || VirtualBaseSpec; |
| 1043 | } |
| 1044 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1045 | /// ActOnMemInitializer - Handle a C++ member initializer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | Sema::MemInitResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1047 | Sema::ActOnMemInitializer(DeclPtrTy ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1048 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1049 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1050 | IdentifierInfo *MemberOrBase, |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1051 | TypeTy *TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1052 | SourceLocation IdLoc, |
| 1053 | SourceLocation LParenLoc, |
| 1054 | ExprTy **Args, unsigned NumArgs, |
| 1055 | SourceLocation *CommaLocs, |
| 1056 | SourceLocation RParenLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1057 | if (!ConstructorD) |
| 1058 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1060 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | |
| 1062 | CXXConstructorDecl *Constructor |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1063 | = dyn_cast<CXXConstructorDecl>(ConstructorD.getAs<Decl>()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1064 | if (!Constructor) { |
| 1065 | // The user wrote a constructor initializer on a function that is |
| 1066 | // not a C++ constructor. Ignore the error for now, because we may |
| 1067 | // have more member initializers coming; we'll diagnose it just |
| 1068 | // once in ActOnMemInitializers. |
| 1069 | return true; |
| 1070 | } |
| 1071 | |
| 1072 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1073 | |
| 1074 | // C++ [class.base.init]p2: |
| 1075 | // Names in a mem-initializer-id are looked up in the scope of the |
| 1076 | // constructor’s class and, if not found in that scope, are looked |
| 1077 | // up in the scope containing the constructor’s |
| 1078 | // definition. [Note: if the constructor’s class contains a member |
| 1079 | // with the same name as a direct or virtual base class of the |
| 1080 | // class, a mem-initializer-id naming the member or base class and |
| 1081 | // composed of a single identifier refers to the class member. A |
| 1082 | // mem-initializer-id for the hidden base class may be specified |
| 1083 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1084 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1085 | // Look for a member, first. |
| 1086 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1087 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1088 | = ClassDecl->lookup(MemberOrBase); |
| 1089 | if (Result.first != Result.second) |
| 1090 | Member = dyn_cast<FieldDecl>(*Result.first); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1091 | |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1092 | // FIXME: Handle members of an anonymous union. |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1093 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1094 | if (Member) |
| 1095 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1096 | LParenLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1097 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1098 | // 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] | 1099 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1100 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1101 | |
| 1102 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1103 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1104 | } else { |
| 1105 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1106 | LookupParsedName(R, S, &SS); |
| 1107 | |
| 1108 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1109 | if (!TyD) { |
| 1110 | if (R.isAmbiguous()) return true; |
| 1111 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1112 | // We don't want access-control diagnostics here. |
| 1113 | R.suppressDiagnostics(); |
| 1114 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1115 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1116 | bool NotUnknownSpecialization = false; |
| 1117 | DeclContext *DC = computeDeclContext(SS, false); |
| 1118 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1119 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1120 | |
| 1121 | if (!NotUnknownSpecialization) { |
| 1122 | // When the scope specifier can refer to a member of an unknown |
| 1123 | // specialization, we take it as a type name. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 1124 | BaseType = CheckTypenameType(ETK_None, |
| 1125 | (NestedNameSpecifier *)SS.getScopeRep(), |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1126 | *MemberOrBase, SS.getRange()); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1127 | if (BaseType.isNull()) |
| 1128 | return true; |
| 1129 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1130 | R.clear(); |
| 1131 | } |
| 1132 | } |
| 1133 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1134 | // If no results were found, try to correct typos. |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1135 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1136 | CorrectTypo(R, S, &SS, ClassDecl, 0, CTC_NoKeywords) && |
| 1137 | R.isSingleResult()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1138 | if (FieldDecl *Member = R.getAsSingle<FieldDecl>()) { |
| 1139 | if (Member->getDeclContext()->getLookupContext()->Equals(ClassDecl)) { |
| 1140 | // We have found a non-static data member with a similar |
| 1141 | // name to what was typed; complain and initialize that |
| 1142 | // member. |
| 1143 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1144 | << MemberOrBase << true << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1145 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1146 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1147 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 1148 | << Member->getDeclName(); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1149 | |
| 1150 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 1151 | LParenLoc, RParenLoc); |
| 1152 | } |
| 1153 | } else if (TypeDecl *Type = R.getAsSingle<TypeDecl>()) { |
| 1154 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1155 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1156 | if (FindBaseInitializer(*this, ClassDecl, |
| 1157 | Context.getTypeDeclType(Type), |
| 1158 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1159 | // We have found a direct or virtual base class with a |
| 1160 | // similar name to what was typed; complain and initialize |
| 1161 | // that base class. |
| 1162 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1163 | << MemberOrBase << false << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1164 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1165 | R.getLookupName().getAsString()); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1166 | |
| 1167 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1168 | : VirtualBaseSpec; |
| 1169 | Diag(BaseSpec->getSourceRange().getBegin(), |
| 1170 | diag::note_base_class_specified_here) |
| 1171 | << BaseSpec->getType() |
| 1172 | << BaseSpec->getSourceRange(); |
| 1173 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1174 | TyD = Type; |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1179 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1180 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1181 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1182 | return true; |
| 1183 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1186 | if (BaseType.isNull()) { |
| 1187 | BaseType = Context.getTypeDeclType(TyD); |
| 1188 | if (SS.isSet()) { |
| 1189 | NestedNameSpecifier *Qualifier = |
| 1190 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1191 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1192 | // FIXME: preserve source range information |
| 1193 | BaseType = Context.getQualifiedNameType(Qualifier, BaseType); |
| 1194 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1195 | } |
| 1196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1198 | if (!TInfo) |
| 1199 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1200 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1201 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1202 | LParenLoc, RParenLoc, ClassDecl); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1205 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1206 | /// containing the field that is being initialized. Returns true if there is an |
| 1207 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1208 | /// otherwise. |
| 1209 | static bool InitExprContainsUninitializedFields(const Stmt* S, |
| 1210 | const FieldDecl* LhsField, |
| 1211 | SourceLocation* L) { |
| 1212 | const MemberExpr* ME = dyn_cast<MemberExpr>(S); |
| 1213 | if (ME) { |
| 1214 | const NamedDecl* RhsField = ME->getMemberDecl(); |
| 1215 | if (RhsField == LhsField) { |
| 1216 | // Initializing a field with itself. Throw a warning. |
| 1217 | // But wait; there are exceptions! |
| 1218 | // Exception #1: The field may not belong to this record. |
| 1219 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
| 1220 | const Expr* base = ME->getBase(); |
| 1221 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1222 | // Even though the field matches, it does not belong to this record. |
| 1223 | return false; |
| 1224 | } |
| 1225 | // None of the exceptions triggered; return true to indicate an |
| 1226 | // uninitialized field was used. |
| 1227 | *L = ME->getMemberLoc(); |
| 1228 | return true; |
| 1229 | } |
| 1230 | } |
| 1231 | bool found = false; |
| 1232 | for (Stmt::const_child_iterator it = S->child_begin(); |
| 1233 | it != S->child_end() && found == false; |
| 1234 | ++it) { |
| 1235 | if (isa<CallExpr>(S)) { |
| 1236 | // Do not descend into function calls or constructors, as the use |
| 1237 | // of an uninitialized field may be valid. One would have to inspect |
| 1238 | // the contents of the function/ctor to determine if it is safe or not. |
| 1239 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1240 | // may be safe, depending on what the function/ctor does. |
| 1241 | continue; |
| 1242 | } |
| 1243 | found = InitExprContainsUninitializedFields(*it, LhsField, L); |
| 1244 | } |
| 1245 | return found; |
| 1246 | } |
| 1247 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1248 | Sema::MemInitResult |
| 1249 | Sema::BuildMemberInitializer(FieldDecl *Member, Expr **Args, |
| 1250 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1251 | SourceLocation LParenLoc, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1252 | SourceLocation RParenLoc) { |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1253 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1254 | // foo(foo) |
| 1255 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1256 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1257 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1258 | SourceLocation L; |
| 1259 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1260 | // FIXME: Return true in the case when other fields are used before being |
| 1261 | // uninitialized. For example, let this field be the i'th field. When |
| 1262 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1263 | // fields are used, as they are not yet initialized. |
| 1264 | // Right now we are only handling the case where the i'th field uses |
| 1265 | // itself in its initializer. |
| 1266 | Diag(L, diag::warn_field_is_uninit); |
| 1267 | } |
| 1268 | } |
| 1269 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1270 | bool HasDependentArg = false; |
| 1271 | for (unsigned i = 0; i < NumArgs; i++) |
| 1272 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1273 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1274 | QualType FieldType = Member->getType(); |
| 1275 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 1276 | FieldType = Array->getElementType(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1277 | if (FieldType->isDependentType() || HasDependentArg) { |
| 1278 | // Can't check initialization for a member of dependent type or when |
| 1279 | // any of the arguments are type-dependent expressions. |
| 1280 | OwningExprResult Init |
| 1281 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1282 | RParenLoc)); |
| 1283 | |
| 1284 | // Erase any temporaries within this evaluation context; we're not |
| 1285 | // going to track them in the AST, since we'll be rebuilding the |
| 1286 | // ASTs during template instantiation. |
| 1287 | ExprTemporaries.erase( |
| 1288 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1289 | ExprTemporaries.end()); |
| 1290 | |
| 1291 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1292 | LParenLoc, |
| 1293 | Init.takeAs<Expr>(), |
| 1294 | RParenLoc); |
| 1295 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1296 | } |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1297 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1298 | if (Member->isInvalidDecl()) |
| 1299 | return true; |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1300 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1301 | // Initialize the member. |
| 1302 | InitializedEntity MemberEntity = |
| 1303 | InitializedEntity::InitializeMember(Member, 0); |
| 1304 | InitializationKind Kind = |
| 1305 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc); |
| 1306 | |
| 1307 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 1308 | |
| 1309 | OwningExprResult MemberInit = |
| 1310 | InitSeq.Perform(*this, MemberEntity, Kind, |
| 1311 | MultiExprArg(*this, (void**)Args, NumArgs), 0); |
| 1312 | if (MemberInit.isInvalid()) |
| 1313 | return true; |
| 1314 | |
| 1315 | // C++0x [class.base.init]p7: |
| 1316 | // The initialization of each base and member constitutes a |
| 1317 | // full-expression. |
| 1318 | MemberInit = MaybeCreateCXXExprWithTemporaries(move(MemberInit)); |
| 1319 | if (MemberInit.isInvalid()) |
| 1320 | return true; |
| 1321 | |
| 1322 | // If we are in a dependent context, template instantiation will |
| 1323 | // perform this type-checking again. Just save the arguments that we |
| 1324 | // received in a ParenListExpr. |
| 1325 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1326 | // of the information that we have about the member |
| 1327 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1328 | // and this approach is far more likely to get the corner cases right. |
| 1329 | if (CurContext->isDependentContext()) { |
| 1330 | // Bump the reference count of all of the arguments. |
| 1331 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1332 | Args[I]->Retain(); |
| 1333 | |
| 1334 | OwningExprResult Init |
| 1335 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1336 | RParenLoc)); |
| 1337 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1338 | LParenLoc, |
| 1339 | Init.takeAs<Expr>(), |
| 1340 | RParenLoc); |
| 1341 | } |
| 1342 | |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1343 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1344 | LParenLoc, |
| 1345 | MemberInit.takeAs<Expr>(), |
| 1346 | RParenLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | Sema::MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1350 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1351 | Expr **Args, unsigned NumArgs, |
| 1352 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
| 1353 | CXXRecordDecl *ClassDecl) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1354 | bool HasDependentArg = false; |
| 1355 | for (unsigned i = 0; i < NumArgs; i++) |
| 1356 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1357 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1358 | SourceLocation BaseLoc = BaseTInfo->getTypeLoc().getSourceRange().getBegin(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1359 | if (BaseType->isDependentType() || HasDependentArg) { |
| 1360 | // Can't check initialization for a base of dependent type or when |
| 1361 | // any of the arguments are type-dependent expressions. |
| 1362 | OwningExprResult BaseInit |
| 1363 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1364 | RParenLoc)); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1365 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1366 | // Erase any temporaries within this evaluation context; we're not |
| 1367 | // going to track them in the AST, since we'll be rebuilding the |
| 1368 | // ASTs during template instantiation. |
| 1369 | ExprTemporaries.erase( |
| 1370 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1371 | ExprTemporaries.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1373 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1374 | /*IsVirtual=*/false, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1375 | LParenLoc, |
| 1376 | BaseInit.takeAs<Expr>(), |
| 1377 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1378 | } |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1379 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1380 | if (!BaseType->isRecordType()) |
| 1381 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 1382 | << BaseType << BaseTInfo->getTypeLoc().getSourceRange(); |
| 1383 | |
| 1384 | // C++ [class.base.init]p2: |
| 1385 | // [...] Unless the mem-initializer-id names a nonstatic data |
| 1386 | // member of the constructor’s class or a direct or virtual base |
| 1387 | // of that class, the mem-initializer is ill-formed. A |
| 1388 | // mem-initializer-list can initialize a base class using any |
| 1389 | // name that denotes that base class type. |
| 1390 | |
| 1391 | // Check for direct and virtual base classes. |
| 1392 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1393 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1394 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 1395 | VirtualBaseSpec); |
| 1396 | |
| 1397 | // C++ [base.class.init]p2: |
| 1398 | // If a mem-initializer-id is ambiguous because it designates both |
| 1399 | // a direct non-virtual base class and an inherited virtual base |
| 1400 | // class, the mem-initializer is ill-formed. |
| 1401 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1402 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
| 1403 | << BaseType << BaseTInfo->getTypeLoc().getSourceRange(); |
| 1404 | // C++ [base.class.init]p2: |
| 1405 | // Unless the mem-initializer-id names a nonstatic data membeer of the |
| 1406 | // constructor's class ot a direst or virtual base of that class, the |
| 1407 | // mem-initializer is ill-formed. |
| 1408 | if (!DirectBaseSpec && !VirtualBaseSpec) |
| 1409 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
John McCall | 110acc1 | 2010-04-27 01:43:38 +0000 | [diff] [blame] | 1410 | << BaseType << Context.getTypeDeclType(ClassDecl) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1411 | << BaseTInfo->getTypeLoc().getSourceRange(); |
| 1412 | |
| 1413 | CXXBaseSpecifier *BaseSpec |
| 1414 | = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
| 1415 | if (!BaseSpec) |
| 1416 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 1417 | |
| 1418 | // Initialize the base. |
| 1419 | InitializedEntity BaseEntity = |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1420 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1421 | InitializationKind Kind = |
| 1422 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc); |
| 1423 | |
| 1424 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 1425 | |
| 1426 | OwningExprResult BaseInit = |
| 1427 | InitSeq.Perform(*this, BaseEntity, Kind, |
| 1428 | MultiExprArg(*this, (void**)Args, NumArgs), 0); |
| 1429 | if (BaseInit.isInvalid()) |
| 1430 | return true; |
| 1431 | |
| 1432 | // C++0x [class.base.init]p7: |
| 1433 | // The initialization of each base and member constitutes a |
| 1434 | // full-expression. |
| 1435 | BaseInit = MaybeCreateCXXExprWithTemporaries(move(BaseInit)); |
| 1436 | if (BaseInit.isInvalid()) |
| 1437 | return true; |
| 1438 | |
| 1439 | // If we are in a dependent context, template instantiation will |
| 1440 | // perform this type-checking again. Just save the arguments that we |
| 1441 | // received in a ParenListExpr. |
| 1442 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1443 | // of the information that we have about the base |
| 1444 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1445 | // and this approach is far more likely to get the corner cases right. |
| 1446 | if (CurContext->isDependentContext()) { |
| 1447 | // Bump the reference count of all of the arguments. |
| 1448 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1449 | Args[I]->Retain(); |
| 1450 | |
| 1451 | OwningExprResult Init |
| 1452 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1453 | RParenLoc)); |
| 1454 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1455 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1456 | LParenLoc, |
| 1457 | Init.takeAs<Expr>(), |
| 1458 | RParenLoc); |
| 1459 | } |
| 1460 | |
| 1461 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1462 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1463 | LParenLoc, |
| 1464 | BaseInit.takeAs<Expr>(), |
| 1465 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1468 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 1469 | /// initialize its base or member. |
| 1470 | enum ImplicitInitializerKind { |
| 1471 | IIK_Default, |
| 1472 | IIK_Copy, |
| 1473 | IIK_Move |
| 1474 | }; |
| 1475 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1476 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1477 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1478 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1479 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1480 | bool IsInheritedVirtualBase, |
| 1481 | CXXBaseOrMemberInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1482 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1483 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 1484 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1485 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1486 | Sema::OwningExprResult BaseInit(SemaRef); |
| 1487 | |
| 1488 | switch (ImplicitInitKind) { |
| 1489 | case IIK_Default: { |
| 1490 | InitializationKind InitKind |
| 1491 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 1492 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1493 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
| 1494 | Sema::MultiExprArg(SemaRef, 0, 0)); |
| 1495 | break; |
| 1496 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1497 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1498 | case IIK_Copy: { |
| 1499 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1500 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1501 | |
| 1502 | Expr *CopyCtorArg = |
| 1503 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
| 1504 | SourceLocation(), ParamType, 0); |
| 1505 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1506 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 1507 | QualType ArgTy = |
| 1508 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 1509 | ParamType.getQualifiers()); |
| 1510 | SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1511 | CastExpr::CK_UncheckedDerivedToBase, |
Anders Carlsson | 8f2abbc | 2010-04-24 22:54:32 +0000 | [diff] [blame] | 1512 | /*isLvalue=*/true, |
| 1513 | CXXBaseSpecifierArray(BaseSpec)); |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1514 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1515 | InitializationKind InitKind |
| 1516 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1517 | SourceLocation(), SourceLocation()); |
| 1518 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1519 | &CopyCtorArg, 1); |
| 1520 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
| 1521 | Sema::MultiExprArg(SemaRef, |
| 1522 | (void**)&CopyCtorArg, 1)); |
| 1523 | break; |
| 1524 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1525 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1526 | case IIK_Move: |
| 1527 | assert(false && "Unhandled initializer kind!"); |
| 1528 | } |
| 1529 | |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1530 | BaseInit = SemaRef.MaybeCreateCXXExprWithTemporaries(move(BaseInit)); |
| 1531 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1532 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1533 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1534 | CXXBaseInit = |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1535 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
| 1536 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 1537 | SourceLocation()), |
| 1538 | BaseSpec->isVirtual(), |
| 1539 | SourceLocation(), |
| 1540 | BaseInit.takeAs<Expr>(), |
| 1541 | SourceLocation()); |
| 1542 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1543 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1546 | static bool |
| 1547 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1548 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1549 | FieldDecl *Field, |
| 1550 | CXXBaseOrMemberInitializer *&CXXMemberInit) { |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1551 | if (ImplicitInitKind == IIK_Copy) { |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 1552 | // FIXME: We should not return early here, but will do so until |
| 1553 | // we know how to handle copy initialization of arrays. |
| 1554 | CXXMemberInit = 0; |
| 1555 | return false; |
| 1556 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1557 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1558 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1559 | |
| 1560 | Expr *MemberExprBase = |
| 1561 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
| 1562 | SourceLocation(), ParamType, 0); |
| 1563 | |
| 1564 | |
| 1565 | Expr *CopyCtorArg = |
| 1566 | MemberExpr::Create(SemaRef.Context, MemberExprBase, /*IsArrow=*/false, |
| 1567 | 0, SourceRange(), Field, |
| 1568 | DeclAccessPair::make(Field, Field->getAccess()), |
| 1569 | SourceLocation(), 0, |
| 1570 | Field->getType().getNonReferenceType()); |
| 1571 | |
| 1572 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 1573 | InitializationKind InitKind = |
| 1574 | InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1575 | SourceLocation(), SourceLocation()); |
| 1576 | |
| 1577 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1578 | &CopyCtorArg, 1); |
| 1579 | |
| 1580 | Sema::OwningExprResult MemberInit = |
| 1581 | InitSeq.Perform(SemaRef, InitEntity, InitKind, |
| 1582 | Sema::MultiExprArg(SemaRef, (void**)&CopyCtorArg, 1), 0); |
| 1583 | if (MemberInit.isInvalid()) |
| 1584 | return true; |
| 1585 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1586 | CXXMemberInit = 0; |
| 1587 | return false; |
| 1588 | } |
| 1589 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1590 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 1591 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1592 | QualType FieldBaseElementType = |
| 1593 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 1594 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1595 | if (FieldBaseElementType->isRecordType()) { |
| 1596 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1597 | InitializationKind InitKind = |
| 1598 | InitializationKind::CreateDefault(Constructor->getLocation()); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1599 | |
| 1600 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1601 | Sema::OwningExprResult MemberInit = |
| 1602 | InitSeq.Perform(SemaRef, InitEntity, InitKind, |
| 1603 | Sema::MultiExprArg(SemaRef, 0, 0)); |
| 1604 | MemberInit = SemaRef.MaybeCreateCXXExprWithTemporaries(move(MemberInit)); |
| 1605 | if (MemberInit.isInvalid()) |
| 1606 | return true; |
| 1607 | |
| 1608 | CXXMemberInit = |
| 1609 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
| 1610 | Field, SourceLocation(), |
| 1611 | SourceLocation(), |
| 1612 | MemberInit.takeAs<Expr>(), |
| 1613 | SourceLocation()); |
| 1614 | return false; |
| 1615 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 1616 | |
| 1617 | if (FieldBaseElementType->isReferenceType()) { |
| 1618 | SemaRef.Diag(Constructor->getLocation(), |
| 1619 | diag::err_uninitialized_member_in_ctor) |
| 1620 | << (int)Constructor->isImplicit() |
| 1621 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1622 | << 0 << Field->getDeclName(); |
| 1623 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1624 | return true; |
| 1625 | } |
| 1626 | |
| 1627 | if (FieldBaseElementType.isConstQualified()) { |
| 1628 | SemaRef.Diag(Constructor->getLocation(), |
| 1629 | diag::err_uninitialized_member_in_ctor) |
| 1630 | << (int)Constructor->isImplicit() |
| 1631 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1632 | << 1 << Field->getDeclName(); |
| 1633 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1634 | return true; |
| 1635 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1636 | |
| 1637 | // Nothing to initialize. |
| 1638 | CXXMemberInit = 0; |
| 1639 | return false; |
| 1640 | } |
| 1641 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1642 | bool |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1643 | Sema::SetBaseOrMemberInitializers(CXXConstructorDecl *Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1644 | CXXBaseOrMemberInitializer **Initializers, |
| 1645 | unsigned NumInitializers, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1646 | bool AnyErrors) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1647 | if (Constructor->getDeclContext()->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1648 | // Just store the initializers as written, they will be checked during |
| 1649 | // instantiation. |
| 1650 | if (NumInitializers > 0) { |
| 1651 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1652 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1653 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
| 1654 | memcpy(baseOrMemberInitializers, Initializers, |
| 1655 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
| 1656 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
| 1657 | } |
| 1658 | |
| 1659 | return false; |
| 1660 | } |
| 1661 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1662 | ImplicitInitializerKind ImplicitInitKind = IIK_Default; |
| 1663 | |
| 1664 | // FIXME: Handle implicit move constructors. |
| 1665 | if (Constructor->isImplicit() && Constructor->isCopyConstructor()) |
| 1666 | ImplicitInitKind = IIK_Copy; |
| 1667 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1668 | // We need to build the initializer AST according to order of construction |
| 1669 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1670 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 1671 | if (!ClassDecl) |
| 1672 | return true; |
| 1673 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1674 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
| 1675 | llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1676 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1678 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1679 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1680 | |
| 1681 | if (Member->isBaseInitializer()) |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1682 | AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1683 | else |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1684 | AllBaseFields[Member->getMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1687 | // Keep track of the direct virtual bases. |
| 1688 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 1689 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 1690 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 1691 | if (I->isVirtual()) |
| 1692 | DirectVBases.insert(I); |
| 1693 | } |
| 1694 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1695 | // Push virtual bases before others. |
| 1696 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 1697 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 1698 | |
| 1699 | if (CXXBaseOrMemberInitializer *Value |
| 1700 | = AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 1701 | AllToInit.push_back(Value); |
| 1702 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1703 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1704 | CXXBaseOrMemberInitializer *CXXBaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1705 | if (BuildImplicitBaseInitializer(*this, Constructor, ImplicitInitKind, |
| 1706 | VBase, IsInheritedVirtualBase, |
| 1707 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1708 | HadError = true; |
| 1709 | continue; |
| 1710 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1711 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1712 | AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1713 | } |
| 1714 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1716 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1717 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1718 | // Virtuals are in the virtual base list and already constructed. |
| 1719 | if (Base->isVirtual()) |
| 1720 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1721 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1722 | if (CXXBaseOrMemberInitializer *Value |
| 1723 | = AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 1724 | AllToInit.push_back(Value); |
| 1725 | } else if (!AnyErrors) { |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1726 | CXXBaseOrMemberInitializer *CXXBaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1727 | if (BuildImplicitBaseInitializer(*this, Constructor, ImplicitInitKind, |
| 1728 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1729 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1730 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1731 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1732 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1733 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1734 | AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1735 | } |
| 1736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1738 | // non-static data members. |
| 1739 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1740 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 1741 | if ((*Field)->isAnonymousStructOrUnion()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | if (const RecordType *FieldClassType = |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1743 | Field->getType()->getAs<RecordType>()) { |
| 1744 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | afe7ec2 | 2009-11-13 18:34:26 +0000 | [diff] [blame] | 1745 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1746 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1747 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1748 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*FA)) { |
| 1749 | // 'Member' is the anonymous union field and 'AnonUnionMember' is |
| 1750 | // set to the anonymous union data member used in the initializer |
| 1751 | // list. |
| 1752 | Value->setMember(*Field); |
| 1753 | Value->setAnonUnionMember(*FA); |
| 1754 | AllToInit.push_back(Value); |
| 1755 | break; |
| 1756 | } |
| 1757 | } |
| 1758 | } |
| 1759 | continue; |
| 1760 | } |
| 1761 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*Field)) { |
| 1762 | AllToInit.push_back(Value); |
| 1763 | continue; |
| 1764 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1766 | if (AnyErrors) |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame] | 1767 | continue; |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame] | 1768 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1769 | CXXBaseOrMemberInitializer *Member; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1770 | if (BuildImplicitMemberInitializer(*this, Constructor, ImplicitInitKind, |
| 1771 | *Field, Member)) { |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1772 | HadError = true; |
| 1773 | continue; |
| 1774 | } |
| 1775 | |
| 1776 | // If the member doesn't need to be initialized, it will be null. |
| 1777 | if (Member) |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1778 | AllToInit.push_back(Member); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1779 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1780 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1781 | NumInitializers = AllToInit.size(); |
| 1782 | if (NumInitializers > 0) { |
| 1783 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1784 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1785 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1786 | memcpy(baseOrMemberInitializers, AllToInit.data(), |
| 1787 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1788 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 1789 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1790 | // Constructors implicitly reference the base and member |
| 1791 | // destructors. |
| 1792 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 1793 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1794 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1795 | |
| 1796 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1799 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 1800 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1801 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1802 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 1803 | return static_cast<void *>(RT->getDecl()); |
| 1804 | } |
| 1805 | return static_cast<void *>(Field); |
| 1806 | } |
| 1807 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1808 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 1809 | return Context.getCanonicalType(BaseType).getTypePtr(); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1812 | static void *GetKeyForMember(ASTContext &Context, |
| 1813 | CXXBaseOrMemberInitializer *Member, |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1814 | bool MemberMaybeAnon = false) { |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1815 | if (!Member->isMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1816 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1817 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1818 | // For fields injected into the class via declaration of an anonymous union, |
| 1819 | // use its anonymous union class declaration as the unique key. |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1820 | FieldDecl *Field = Member->getMember(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1821 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1822 | // After SetBaseOrMemberInitializers call, Field is the anonymous union |
| 1823 | // data member of the class. Data member used in the initializer list is |
| 1824 | // in AnonUnionMember field. |
| 1825 | if (MemberMaybeAnon && Field->isAnonymousStructOrUnion()) |
| 1826 | Field = Member->getAnonUnionMember(); |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1827 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1828 | // If the field is a member of an anonymous struct or union, our key |
| 1829 | // 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] | 1830 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1831 | if (RD->isAnonymousStructOrUnion()) { |
| 1832 | while (true) { |
| 1833 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 1834 | if (Parent->isAnonymousStructOrUnion()) |
| 1835 | RD = Parent; |
| 1836 | else |
| 1837 | break; |
| 1838 | } |
| 1839 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1840 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1841 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1842 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1843 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1846 | static void |
| 1847 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1848 | const CXXConstructorDecl *Constructor, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1849 | CXXBaseOrMemberInitializer **Inits, |
| 1850 | unsigned NumInits) { |
| 1851 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1852 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1854 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order) |
| 1855 | == Diagnostic::Ignored) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1856 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1857 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1858 | // Build the list of bases and members in the order that they'll |
| 1859 | // actually be initialized. The explicit initializers should be in |
| 1860 | // this same order but may be missing things. |
| 1861 | llvm::SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1863 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1864 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1865 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1866 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1867 | ClassDecl->vbases_begin(), |
| 1868 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1869 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1871 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1872 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1873 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1874 | if (Base->isVirtual()) |
| 1875 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1876 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1877 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1879 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1880 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1881 | E = ClassDecl->field_end(); Field != E; ++Field) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1882 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1883 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1884 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 1885 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1886 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1887 | CXXBaseOrMemberInitializer *PrevInit = 0; |
| 1888 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
| 1889 | CXXBaseOrMemberInitializer *Init = Inits[InitIndex]; |
| 1890 | void *InitKey = GetKeyForMember(SemaRef.Context, Init, true); |
| 1891 | |
| 1892 | // Scan forward to try to find this initializer in the idealized |
| 1893 | // initializers list. |
| 1894 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 1895 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1896 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1897 | |
| 1898 | // If we didn't find this initializer, it must be because we |
| 1899 | // scanned past it on a previous iteration. That can only |
| 1900 | // happen if we're out of order; emit a warning. |
| 1901 | if (IdealIndex == NumIdealInits) { |
| 1902 | assert(PrevInit && "initializer not found in initializer list"); |
| 1903 | |
| 1904 | Sema::SemaDiagnosticBuilder D = |
| 1905 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 1906 | diag::warn_initializer_out_of_order); |
| 1907 | |
| 1908 | if (PrevInit->isMemberInitializer()) |
| 1909 | D << 0 << PrevInit->getMember()->getDeclName(); |
| 1910 | else |
| 1911 | D << 1 << PrevInit->getBaseClassInfo()->getType(); |
| 1912 | |
| 1913 | if (Init->isMemberInitializer()) |
| 1914 | D << 0 << Init->getMember()->getDeclName(); |
| 1915 | else |
| 1916 | D << 1 << Init->getBaseClassInfo()->getType(); |
| 1917 | |
| 1918 | // Move back to the initializer's location in the ideal list. |
| 1919 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 1920 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1921 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1922 | |
| 1923 | assert(IdealIndex != NumIdealInits && |
| 1924 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1925 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1926 | |
| 1927 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1928 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1931 | namespace { |
| 1932 | bool CheckRedundantInit(Sema &S, |
| 1933 | CXXBaseOrMemberInitializer *Init, |
| 1934 | CXXBaseOrMemberInitializer *&PrevInit) { |
| 1935 | if (!PrevInit) { |
| 1936 | PrevInit = Init; |
| 1937 | return false; |
| 1938 | } |
| 1939 | |
| 1940 | if (FieldDecl *Field = Init->getMember()) |
| 1941 | S.Diag(Init->getSourceLocation(), |
| 1942 | diag::err_multiple_mem_initialization) |
| 1943 | << Field->getDeclName() |
| 1944 | << Init->getSourceRange(); |
| 1945 | else { |
| 1946 | Type *BaseClass = Init->getBaseClass(); |
| 1947 | assert(BaseClass && "neither field nor base"); |
| 1948 | S.Diag(Init->getSourceLocation(), |
| 1949 | diag::err_multiple_base_initialization) |
| 1950 | << QualType(BaseClass, 0) |
| 1951 | << Init->getSourceRange(); |
| 1952 | } |
| 1953 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 1954 | << 0 << PrevInit->getSourceRange(); |
| 1955 | |
| 1956 | return true; |
| 1957 | } |
| 1958 | |
| 1959 | typedef std::pair<NamedDecl *, CXXBaseOrMemberInitializer *> UnionEntry; |
| 1960 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 1961 | |
| 1962 | bool CheckRedundantUnionInit(Sema &S, |
| 1963 | CXXBaseOrMemberInitializer *Init, |
| 1964 | RedundantUnionMap &Unions) { |
| 1965 | FieldDecl *Field = Init->getMember(); |
| 1966 | RecordDecl *Parent = Field->getParent(); |
| 1967 | if (!Parent->isAnonymousStructOrUnion()) |
| 1968 | return false; |
| 1969 | |
| 1970 | NamedDecl *Child = Field; |
| 1971 | do { |
| 1972 | if (Parent->isUnion()) { |
| 1973 | UnionEntry &En = Unions[Parent]; |
| 1974 | if (En.first && En.first != Child) { |
| 1975 | S.Diag(Init->getSourceLocation(), |
| 1976 | diag::err_multiple_mem_union_initialization) |
| 1977 | << Field->getDeclName() |
| 1978 | << Init->getSourceRange(); |
| 1979 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 1980 | << 0 << En.second->getSourceRange(); |
| 1981 | return true; |
| 1982 | } else if (!En.first) { |
| 1983 | En.first = Child; |
| 1984 | En.second = Init; |
| 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | Child = Parent; |
| 1989 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 1990 | } while (Parent->isAnonymousStructOrUnion()); |
| 1991 | |
| 1992 | return false; |
| 1993 | } |
| 1994 | } |
| 1995 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1996 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
| 1997 | void Sema::ActOnMemInitializers(DeclPtrTy ConstructorDecl, |
| 1998 | SourceLocation ColonLoc, |
| 1999 | MemInitTy **meminits, unsigned NumMemInits, |
| 2000 | bool AnyErrors) { |
| 2001 | if (!ConstructorDecl) |
| 2002 | return; |
| 2003 | |
| 2004 | AdjustDeclIfTemplate(ConstructorDecl); |
| 2005 | |
| 2006 | CXXConstructorDecl *Constructor |
| 2007 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl.getAs<Decl>()); |
| 2008 | |
| 2009 | if (!Constructor) { |
| 2010 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 2011 | return; |
| 2012 | } |
| 2013 | |
| 2014 | CXXBaseOrMemberInitializer **MemInits = |
| 2015 | reinterpret_cast<CXXBaseOrMemberInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2016 | |
| 2017 | // Mapping for the duplicate initializers check. |
| 2018 | // For member initializers, this is keyed with a FieldDecl*. |
| 2019 | // For base initializers, this is keyed with a Type*. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2020 | llvm::DenseMap<void*, CXXBaseOrMemberInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2021 | |
| 2022 | // Mapping for the inconsistent anonymous-union initializers check. |
| 2023 | RedundantUnionMap MemberUnions; |
| 2024 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2025 | bool HadError = false; |
| 2026 | for (unsigned i = 0; i < NumMemInits; i++) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2027 | CXXBaseOrMemberInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2028 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2029 | if (Init->isMemberInitializer()) { |
| 2030 | FieldDecl *Field = Init->getMember(); |
| 2031 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 2032 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 2033 | HadError = true; |
| 2034 | } else { |
| 2035 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 2036 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 2037 | HadError = true; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2038 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2041 | if (HadError) |
| 2042 | return; |
| 2043 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2044 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2045 | |
| 2046 | SetBaseOrMemberInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2049 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2050 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 2051 | CXXRecordDecl *ClassDecl) { |
| 2052 | // Ignore dependent contexts. |
| 2053 | if (ClassDecl->isDependentContext()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2054 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2055 | |
| 2056 | // FIXME: all the access-control diagnostics are positioned on the |
| 2057 | // field/base declaration. That's probably good; that said, the |
| 2058 | // user might reasonably want to know why the destructor is being |
| 2059 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2060 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2061 | // Non-static data members. |
| 2062 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 2063 | E = ClassDecl->field_end(); I != E; ++I) { |
| 2064 | FieldDecl *Field = *I; |
| 2065 | |
| 2066 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 2067 | |
| 2068 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 2069 | if (!RT) |
| 2070 | continue; |
| 2071 | |
| 2072 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 2073 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2074 | continue; |
| 2075 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2076 | CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor(Context); |
| 2077 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2078 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2079 | << Field->getDeclName() |
| 2080 | << FieldType); |
| 2081 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2082 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2085 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 2086 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2087 | // Bases. |
| 2088 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2089 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2090 | // Bases are always records in a well-formed non-dependent class. |
| 2091 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 2092 | |
| 2093 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2094 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2095 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2096 | |
| 2097 | // Ignore trivial destructors. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2098 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2099 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2100 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2101 | |
| 2102 | CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context); |
| 2103 | |
| 2104 | // FIXME: caret should be on the start of the class name |
| 2105 | CheckDestructorAccess(Base->getSourceRange().getBegin(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2106 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2107 | << Base->getType() |
| 2108 | << Base->getSourceRange()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2109 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2110 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2114 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2115 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2116 | |
| 2117 | // Bases are always records in a well-formed non-dependent class. |
| 2118 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 2119 | |
| 2120 | // Ignore direct virtual bases. |
| 2121 | if (DirectVirtualBases.count(RT)) |
| 2122 | continue; |
| 2123 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2124 | // Ignore trivial destructors. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2125 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2126 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2127 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2128 | |
| 2129 | CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context); |
| 2130 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2131 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2132 | << VBase->getType()); |
| 2133 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2134 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2135 | } |
| 2136 | } |
| 2137 | |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 2138 | void Sema::ActOnDefaultCtorInitializers(DeclPtrTy CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2139 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2140 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2142 | if (CXXConstructorDecl *Constructor |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2143 | = dyn_cast<CXXConstructorDecl>(CDtorDecl.getAs<Decl>())) |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2144 | SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2147 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2148 | unsigned DiagID, AbstractDiagSelID SelID, |
| 2149 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2150 | if (SelID == -1) |
| 2151 | return RequireNonAbstractType(Loc, T, |
| 2152 | PDiag(DiagID), CurrentRD); |
| 2153 | else |
| 2154 | return RequireNonAbstractType(Loc, T, |
| 2155 | PDiag(DiagID) << SelID, CurrentRD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2158 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
| 2159 | const PartialDiagnostic &PD, |
| 2160 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2161 | if (!getLangOptions().CPlusPlus) |
| 2162 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 2164 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2165 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2166 | CurrentRD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2167 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2168 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2169 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2170 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2171 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2173 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2174 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, CurrentRD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2175 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2177 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2178 | if (!RT) |
| 2179 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2180 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2181 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2182 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2183 | if (CurrentRD && CurrentRD != RD) |
| 2184 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2186 | // FIXME: is this reasonable? It matches current behavior, but.... |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2187 | if (!RD->getDefinition()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2188 | return false; |
| 2189 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2190 | if (!RD->isAbstract()) |
| 2191 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2192 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2193 | Diag(Loc, PD) << RD->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2195 | // Check if we've already emitted the list of pure virtual functions for this |
| 2196 | // class. |
| 2197 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
| 2198 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2200 | CXXFinalOverriderMap FinalOverriders; |
| 2201 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2202 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2203 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2204 | MEnd = FinalOverriders.end(); |
| 2205 | M != MEnd; |
| 2206 | ++M) { |
| 2207 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2208 | SOEnd = M->second.end(); |
| 2209 | SO != SOEnd; ++SO) { |
| 2210 | // C++ [class.abstract]p4: |
| 2211 | // A class is abstract if it contains or inherits at least one |
| 2212 | // pure virtual function for which the final overrider is pure |
| 2213 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2215 | // |
| 2216 | if (SO->second.size() != 1) |
| 2217 | continue; |
| 2218 | |
| 2219 | if (!SO->second.front().Method->isPure()) |
| 2220 | continue; |
| 2221 | |
| 2222 | Diag(SO->second.front().Method->getLocation(), |
| 2223 | diag::note_pure_virtual_function) |
| 2224 | << SO->second.front().Method->getDeclName(); |
| 2225 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | if (!PureVirtualClassDiagSet) |
| 2229 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 2230 | PureVirtualClassDiagSet->insert(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2231 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2232 | return true; |
| 2233 | } |
| 2234 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2235 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 2236 | class AbstractClassUsageDiagnoser |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2237 | : public DeclVisitor<AbstractClassUsageDiagnoser, bool> { |
| 2238 | Sema &SemaRef; |
| 2239 | CXXRecordDecl *AbstractClass; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2241 | bool VisitDeclContext(const DeclContext *DC) { |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2242 | bool Invalid = false; |
| 2243 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2244 | for (CXXRecordDecl::decl_iterator I = DC->decls_begin(), |
| 2245 | E = DC->decls_end(); I != E; ++I) |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2246 | Invalid |= Visit(*I); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2247 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2248 | return Invalid; |
| 2249 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2250 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2251 | public: |
| 2252 | AbstractClassUsageDiagnoser(Sema& SemaRef, CXXRecordDecl *ac) |
| 2253 | : SemaRef(SemaRef), AbstractClass(ac) { |
| 2254 | Visit(SemaRef.Context.getTranslationUnitDecl()); |
| 2255 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2256 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2257 | bool VisitFunctionDecl(const FunctionDecl *FD) { |
| 2258 | if (FD->isThisDeclarationADefinition()) { |
| 2259 | // No need to do the check if we're in a definition, because it requires |
| 2260 | // that the return/param types are complete. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2261 | // because that requires |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2262 | return VisitDeclContext(FD); |
| 2263 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2264 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2265 | // Check the return type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2266 | QualType RTy = FD->getType()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | bool Invalid = |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2268 | SemaRef.RequireNonAbstractType(FD->getLocation(), RTy, |
| 2269 | diag::err_abstract_type_in_decl, |
| 2270 | Sema::AbstractReturnType, |
| 2271 | AbstractClass); |
| 2272 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2273 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2274 | E = FD->param_end(); I != E; ++I) { |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2275 | const ParmVarDecl *VD = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2276 | Invalid |= |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2277 | SemaRef.RequireNonAbstractType(VD->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | VD->getOriginalType(), |
| 2279 | diag::err_abstract_type_in_decl, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2280 | Sema::AbstractParamType, |
| 2281 | AbstractClass); |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | return Invalid; |
| 2285 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2286 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2287 | bool VisitDecl(const Decl* D) { |
| 2288 | if (const DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 2289 | return VisitDeclContext(DC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2290 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2291 | return false; |
| 2292 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2293 | }; |
| 2294 | } |
| 2295 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2296 | /// \brief Perform semantic checks on a class definition that has been |
| 2297 | /// completing, introducing implicitly-declared members, checking for |
| 2298 | /// abstract types, etc. |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2299 | void Sema::CheckCompletedCXXClass(Scope *S, CXXRecordDecl *Record) { |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2300 | if (!Record || Record->isInvalidDecl()) |
| 2301 | return; |
| 2302 | |
Eli Friedman | ff2d878 | 2009-12-16 20:00:27 +0000 | [diff] [blame] | 2303 | if (!Record->isDependentType()) |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2304 | AddImplicitlyDeclaredMembersToClass(S, Record); |
Douglas Gregor | 159ef1e | 2010-01-06 04:44:19 +0000 | [diff] [blame] | 2305 | |
Eli Friedman | ff2d878 | 2009-12-16 20:00:27 +0000 | [diff] [blame] | 2306 | if (Record->isInvalidDecl()) |
| 2307 | return; |
| 2308 | |
John McCall | 233a641 | 2010-01-28 07:38:46 +0000 | [diff] [blame] | 2309 | // Set access bits correctly on the directly-declared conversions. |
| 2310 | UnresolvedSetImpl *Convs = Record->getConversionFunctions(); |
| 2311 | for (UnresolvedSetIterator I = Convs->begin(), E = Convs->end(); I != E; ++I) |
| 2312 | Convs->setAccess(I, (*I)->getAccess()); |
| 2313 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2314 | // Determine whether we need to check for final overriders. We do |
| 2315 | // this either when there are virtual base classes (in which case we |
| 2316 | // may end up finding multiple final overriders for a given virtual |
| 2317 | // function) or any of the base classes is abstract (in which case |
| 2318 | // we might detect that this class is abstract). |
| 2319 | bool CheckFinalOverriders = false; |
| 2320 | if (Record->isPolymorphic() && !Record->isInvalidDecl() && |
| 2321 | !Record->isDependentType()) { |
| 2322 | if (Record->getNumVBases()) |
| 2323 | CheckFinalOverriders = true; |
| 2324 | else if (!Record->isAbstract()) { |
| 2325 | for (CXXRecordDecl::base_class_const_iterator B = Record->bases_begin(), |
| 2326 | BEnd = Record->bases_end(); |
| 2327 | B != BEnd; ++B) { |
| 2328 | CXXRecordDecl *BaseDecl |
| 2329 | = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl()); |
| 2330 | if (BaseDecl->isAbstract()) { |
| 2331 | CheckFinalOverriders = true; |
| 2332 | break; |
| 2333 | } |
| 2334 | } |
| 2335 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2338 | if (CheckFinalOverriders) { |
| 2339 | CXXFinalOverriderMap FinalOverriders; |
| 2340 | Record->getFinalOverriders(FinalOverriders); |
| 2341 | |
| 2342 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2343 | MEnd = FinalOverriders.end(); |
| 2344 | M != MEnd; ++M) { |
| 2345 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2346 | SOEnd = M->second.end(); |
| 2347 | SO != SOEnd; ++SO) { |
| 2348 | assert(SO->second.size() > 0 && |
| 2349 | "All virtual functions have overridding virtual functions"); |
| 2350 | if (SO->second.size() == 1) { |
| 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. |
| 2355 | if (SO->second.front().Method->isPure()) |
| 2356 | Record->setAbstract(true); |
| 2357 | continue; |
| 2358 | } |
| 2359 | |
| 2360 | // C++ [class.virtual]p2: |
| 2361 | // In a derived class, if a virtual member function of a base |
| 2362 | // class subobject has more than one final overrider the |
| 2363 | // program is ill-formed. |
| 2364 | Diag(Record->getLocation(), diag::err_multiple_final_overriders) |
| 2365 | << (NamedDecl *)M->first << Record; |
| 2366 | Diag(M->first->getLocation(), diag::note_overridden_virtual_function); |
| 2367 | for (OverridingMethods::overriding_iterator OM = SO->second.begin(), |
| 2368 | OMEnd = SO->second.end(); |
| 2369 | OM != OMEnd; ++OM) |
| 2370 | Diag(OM->Method->getLocation(), diag::note_final_overrider) |
| 2371 | << (NamedDecl *)M->first << OM->Method->getParent(); |
| 2372 | |
| 2373 | Record->setInvalidDecl(); |
| 2374 | } |
| 2375 | } |
| 2376 | } |
| 2377 | |
| 2378 | if (Record->isAbstract() && !Record->isInvalidDecl()) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2379 | (void)AbstractClassUsageDiagnoser(*this, Record); |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2380 | |
| 2381 | // If this is not an aggregate type and has no user-declared constructor, |
| 2382 | // complain about any non-static data members of reference or const scalar |
| 2383 | // type, since they will never get initializers. |
| 2384 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 2385 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor()) { |
| 2386 | bool Complained = false; |
| 2387 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 2388 | FEnd = Record->field_end(); |
| 2389 | F != FEnd; ++F) { |
| 2390 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 2391 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2392 | if (!Complained) { |
| 2393 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 2394 | << Record->getTagKind() << Record; |
| 2395 | Complained = true; |
| 2396 | } |
| 2397 | |
| 2398 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 2399 | << F->getType()->isReferenceType() |
| 2400 | << F->getDeclName(); |
| 2401 | } |
| 2402 | } |
| 2403 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2406 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2407 | DeclPtrTy TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2408 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2409 | SourceLocation RBrac, |
| 2410 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2411 | if (!TagDecl) |
| 2412 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2414 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2415 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2416 | ActOnFields(S, RLoc, TagDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2417 | (DeclPtrTy*)FieldCollector->getCurFields(), |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2418 | FieldCollector->getCurNumFields(), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2419 | |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2420 | CheckCompletedCXXClass(S, |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2421 | dyn_cast_or_null<CXXRecordDecl>(TagDecl.getAs<Decl>())); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2424 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 2425 | /// special functions, such as the default constructor, copy |
| 2426 | /// constructor, or destructor, to the given C++ class (C++ |
| 2427 | /// [special]p1). This routine can only be executed just before the |
| 2428 | /// definition of the class is complete. |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2429 | /// |
| 2430 | /// The scope, if provided, is the class scope. |
| 2431 | void Sema::AddImplicitlyDeclaredMembersToClass(Scope *S, |
| 2432 | CXXRecordDecl *ClassDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2433 | CanQualType ClassType |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2434 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2435 | |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2436 | // FIXME: Implicit declarations have exception specifications, which are |
| 2437 | // the union of the specifications of the implicitly called functions. |
| 2438 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2439 | if (!ClassDecl->hasUserDeclaredConstructor()) { |
| 2440 | // C++ [class.ctor]p5: |
| 2441 | // A default constructor for a class X is a constructor of class X |
| 2442 | // that can be called without an argument. If there is no |
| 2443 | // user-declared constructor for class X, a default constructor is |
| 2444 | // implicitly declared. An implicitly-declared default constructor |
| 2445 | // is an inline public member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2446 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2447 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2448 | CXXConstructorDecl *DefaultCon = |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2449 | CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2450 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2451 | Context.getFunctionType(Context.VoidTy, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2452 | 0, 0, false, 0, |
| 2453 | /*FIXME*/false, false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2454 | 0, 0, |
| 2455 | FunctionType::ExtInfo()), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2456 | /*TInfo=*/0, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2457 | /*isExplicit=*/false, |
| 2458 | /*isInline=*/true, |
| 2459 | /*isImplicitlyDeclared=*/true); |
| 2460 | DefaultCon->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2461 | DefaultCon->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2462 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2463 | if (S) |
| 2464 | PushOnScopeChains(DefaultCon, S, true); |
| 2465 | else |
| 2466 | ClassDecl->addDecl(DefaultCon); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
| 2470 | // C++ [class.copy]p4: |
| 2471 | // If the class definition does not explicitly declare a copy |
| 2472 | // constructor, one is declared implicitly. |
| 2473 | |
| 2474 | // C++ [class.copy]p5: |
| 2475 | // The implicitly-declared copy constructor for a class X will |
| 2476 | // have the form |
| 2477 | // |
| 2478 | // X::X(const X&) |
| 2479 | // |
| 2480 | // if |
| 2481 | bool HasConstCopyConstructor = true; |
| 2482 | |
| 2483 | // -- each direct or virtual base class B of X has a copy |
| 2484 | // constructor whose first parameter is of type const B& or |
| 2485 | // const volatile B&, and |
| 2486 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2487 | HasConstCopyConstructor && Base != ClassDecl->bases_end(); ++Base) { |
| 2488 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2489 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2490 | HasConstCopyConstructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2491 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 2492 | } |
| 2493 | |
| 2494 | // -- for all the nonstatic data members of X that are of a |
| 2495 | // class type M (or array thereof), each such class type |
| 2496 | // has a copy constructor whose first parameter is of type |
| 2497 | // const M& or const volatile M&. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2498 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 2499 | HasConstCopyConstructor && Field != ClassDecl->field_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2500 | ++Field) { |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2501 | QualType FieldType = (*Field)->getType(); |
| 2502 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2503 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2504 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2505 | const CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2506 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2507 | HasConstCopyConstructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2508 | = FieldClassDecl->hasConstCopyConstructor(Context); |
| 2509 | } |
| 2510 | } |
| 2511 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2512 | // Otherwise, the implicitly declared copy constructor will have |
| 2513 | // the form |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2514 | // |
| 2515 | // X::X(X&) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2516 | QualType ArgType = ClassType; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2517 | if (HasConstCopyConstructor) |
| 2518 | ArgType = ArgType.withConst(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2519 | ArgType = Context.getLValueReferenceType(ArgType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2520 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2521 | // An implicitly-declared copy constructor is an inline public |
| 2522 | // member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2523 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2524 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2525 | CXXConstructorDecl *CopyConstructor |
| 2526 | = CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2527 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2528 | Context.getFunctionType(Context.VoidTy, |
| 2529 | &ArgType, 1, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2530 | false, 0, |
| 2531 | /*FIXME:*/false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2532 | false, 0, 0, |
| 2533 | FunctionType::ExtInfo()), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2534 | /*TInfo=*/0, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2535 | /*isExplicit=*/false, |
| 2536 | /*isInline=*/true, |
| 2537 | /*isImplicitlyDeclared=*/true); |
| 2538 | CopyConstructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2539 | CopyConstructor->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2540 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2541 | |
| 2542 | // Add the parameter to the constructor. |
| 2543 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 2544 | ClassDecl->getLocation(), |
| 2545 | /*IdentifierInfo=*/0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2546 | ArgType, /*TInfo=*/0, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2547 | VarDecl::None, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2548 | VarDecl::None, 0); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2549 | CopyConstructor->setParams(&FromParam, 1); |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2550 | if (S) |
| 2551 | PushOnScopeChains(CopyConstructor, S, true); |
| 2552 | else |
| 2553 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2556 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 2557 | // Note: The following rules are largely analoguous to the copy |
| 2558 | // constructor rules. Note that virtual bases are not taken into account |
| 2559 | // for determining the argument type of the operator. Note also that |
| 2560 | // operators taking an object instead of a reference are allowed. |
| 2561 | // |
| 2562 | // C++ [class.copy]p10: |
| 2563 | // If the class definition does not explicitly declare a copy |
| 2564 | // assignment operator, one is declared implicitly. |
| 2565 | // The implicitly-defined copy assignment operator for a class X |
| 2566 | // will have the form |
| 2567 | // |
| 2568 | // X& X::operator=(const X&) |
| 2569 | // |
| 2570 | // if |
| 2571 | bool HasConstCopyAssignment = true; |
| 2572 | |
| 2573 | // -- each direct base class B of X has a copy assignment operator |
| 2574 | // whose parameter is of type const B&, const volatile B& or B, |
| 2575 | // and |
| 2576 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2577 | HasConstCopyAssignment && Base != ClassDecl->bases_end(); ++Base) { |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 2578 | assert(!Base->getType()->isDependentType() && |
| 2579 | "Cannot generate implicit members for class with dependent bases."); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2580 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2581 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2582 | const CXXMethodDecl *MD = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2583 | HasConstCopyAssignment = BaseClassDecl->hasConstCopyAssignment(Context, |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2584 | MD); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | // -- for all the nonstatic data members of X that are of a class |
| 2588 | // type M (or array thereof), each such class type has a copy |
| 2589 | // assignment operator whose parameter is of type const M&, |
| 2590 | // const volatile M& or M. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2591 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 2592 | HasConstCopyAssignment && Field != ClassDecl->field_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2593 | ++Field) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2594 | QualType FieldType = (*Field)->getType(); |
| 2595 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2596 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2597 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2598 | const CXXRecordDecl *FieldClassDecl |
| 2599 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2600 | const CXXMethodDecl *MD = 0; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2601 | HasConstCopyAssignment |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2602 | = FieldClassDecl->hasConstCopyAssignment(Context, MD); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2603 | } |
| 2604 | } |
| 2605 | |
| 2606 | // Otherwise, the implicitly declared copy assignment operator will |
| 2607 | // have the form |
| 2608 | // |
| 2609 | // X& X::operator=(X&) |
| 2610 | QualType ArgType = ClassType; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2611 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2612 | if (HasConstCopyAssignment) |
| 2613 | ArgType = ArgType.withConst(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2614 | ArgType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2615 | |
| 2616 | // An implicitly-declared copy assignment operator is an inline public |
| 2617 | // member of its class. |
| 2618 | DeclarationName Name = |
| 2619 | Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 2620 | CXXMethodDecl *CopyAssignment = |
| 2621 | CXXMethodDecl::Create(Context, ClassDecl, ClassDecl->getLocation(), Name, |
| 2622 | Context.getFunctionType(RetType, &ArgType, 1, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2623 | false, 0, |
| 2624 | /*FIXME:*/false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2625 | false, 0, 0, |
| 2626 | FunctionType::ExtInfo()), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2627 | /*TInfo=*/0, /*isStatic=*/false, |
| 2628 | /*StorageClassAsWritten=*/FunctionDecl::None, |
| 2629 | /*isInline=*/true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2630 | CopyAssignment->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2631 | CopyAssignment->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2632 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 2633 | CopyAssignment->setCopyAssignment(true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2634 | |
| 2635 | // Add the parameter to the operator. |
| 2636 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 2637 | ClassDecl->getLocation(), |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 2638 | /*Id=*/0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2639 | ArgType, /*TInfo=*/0, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2640 | VarDecl::None, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2641 | VarDecl::None, 0); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2642 | CopyAssignment->setParams(&FromParam, 1); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2643 | |
| 2644 | // Don't call addedAssignmentOperator. There is no way to distinguish an |
| 2645 | // implicit from an explicit assignment operator. |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2646 | if (S) |
| 2647 | PushOnScopeChains(CopyAssignment, S, true); |
| 2648 | else |
| 2649 | ClassDecl->addDecl(CopyAssignment); |
Eli Friedman | ca6affd | 2009-12-02 06:59:20 +0000 | [diff] [blame] | 2650 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2653 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2654 | // C++ [class.dtor]p2: |
| 2655 | // If a class has no user-declared destructor, a destructor is |
| 2656 | // declared implicitly. An implicitly-declared destructor is an |
| 2657 | // inline public member of its class. |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2658 | QualType Ty = Context.getFunctionType(Context.VoidTy, |
| 2659 | 0, 0, false, 0, |
| 2660 | /*FIXME:*/false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2661 | false, 0, 0, FunctionType::ExtInfo()); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2662 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2663 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2664 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2665 | CXXDestructorDecl *Destructor |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2666 | = CXXDestructorDecl::Create(Context, ClassDecl, |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2667 | ClassDecl->getLocation(), Name, Ty, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2668 | /*isInline=*/true, |
| 2669 | /*isImplicitlyDeclared=*/true); |
| 2670 | Destructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2671 | Destructor->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2672 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2673 | if (S) |
| 2674 | PushOnScopeChains(Destructor, S, true); |
| 2675 | else |
| 2676 | ClassDecl->addDecl(Destructor); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2677 | |
| 2678 | // This could be uniqued if it ever proves significant. |
| 2679 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
Anders Carlsson | d5a942b | 2009-11-26 21:25:09 +0000 | [diff] [blame] | 2680 | |
| 2681 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2682 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2685 | void Sema::ActOnReenterTemplateScope(Scope *S, DeclPtrTy TemplateD) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 2686 | Decl *D = TemplateD.getAs<Decl>(); |
| 2687 | if (!D) |
| 2688 | return; |
| 2689 | |
| 2690 | TemplateParameterList *Params = 0; |
| 2691 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 2692 | Params = Template->getTemplateParameters(); |
| 2693 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2694 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 2695 | Params = PartialSpec->getTemplateParameters(); |
| 2696 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2697 | return; |
| 2698 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2699 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2700 | ParamEnd = Params->end(); |
| 2701 | Param != ParamEnd; ++Param) { |
| 2702 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 2703 | if (Named->getDeclName()) { |
| 2704 | S->AddDecl(DeclPtrTy::make(Named)); |
| 2705 | IdResolver.AddDecl(Named); |
| 2706 | } |
| 2707 | } |
| 2708 | } |
| 2709 | |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2710 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, DeclPtrTy RecordD) { |
| 2711 | if (!RecordD) return; |
| 2712 | AdjustDeclIfTemplate(RecordD); |
| 2713 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD.getAs<Decl>()); |
| 2714 | PushDeclContext(S, Record); |
| 2715 | } |
| 2716 | |
| 2717 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, DeclPtrTy RecordD) { |
| 2718 | if (!RecordD) return; |
| 2719 | PopDeclContext(); |
| 2720 | } |
| 2721 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2722 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 2723 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 2724 | /// parsing those parts of the given Method declaration that could |
| 2725 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 2726 | /// arguments. This action should enter the scope of the given |
| 2727 | /// Method declaration as if we had just parsed the qualified method |
| 2728 | /// name. However, it should not bring the parameters into scope; |
| 2729 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2730 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 2734 | /// C++ method declaration. We're (re-)introducing the given |
| 2735 | /// function parameter into scope for use in parsing later parts of |
| 2736 | /// the method declaration. For example, we could see an |
| 2737 | /// ActOnParamDefaultArgument event for this parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2738 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, DeclPtrTy ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2739 | if (!ParamD) |
| 2740 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2741 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2742 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2743 | |
| 2744 | // If this parameter has an unparsed default argument, clear it out |
| 2745 | // to make way for the parsed default argument. |
| 2746 | if (Param->hasUnparsedDefaultArg()) |
| 2747 | Param->setDefaultArg(0); |
| 2748 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2749 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2750 | if (Param->getDeclName()) |
| 2751 | IdResolver.AddDecl(Param); |
| 2752 | } |
| 2753 | |
| 2754 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 2755 | /// processing the delayed method declaration for Method. The method |
| 2756 | /// declaration is now considered finished. There may be a separate |
| 2757 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 2758 | /// immediately!) for this method, if it was also defined inside the |
| 2759 | /// class body. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2760 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2761 | if (!MethodD) |
| 2762 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2763 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2764 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2766 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2767 | |
| 2768 | // Now that we have our default arguments, check the constructor |
| 2769 | // again. It could produce additional diagnostics or affect whether |
| 2770 | // the class has implicitly-declared destructors, among other |
| 2771 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2772 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 2773 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2774 | |
| 2775 | // Check the default arguments, which we may have added. |
| 2776 | if (!Method->isInvalidDecl()) |
| 2777 | CheckCXXDefaultArguments(Method); |
| 2778 | } |
| 2779 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2780 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2781 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2782 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2783 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 2784 | /// will be updated to reflect a well-formed type for the constructor and |
| 2785 | /// returned. |
| 2786 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
| 2787 | FunctionDecl::StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2788 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2789 | |
| 2790 | // C++ [class.ctor]p3: |
| 2791 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 2792 | // constructor can be invoked for a const, volatile or const |
| 2793 | // volatile object. A constructor shall not be declared const, |
| 2794 | // volatile, or const volatile (9.3.2). |
| 2795 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2796 | if (!D.isInvalidType()) |
| 2797 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2798 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 2799 | << SourceRange(D.getIdentifierLoc()); |
| 2800 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2801 | } |
| 2802 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2803 | if (!D.isInvalidType()) |
| 2804 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2805 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2806 | << SourceRange(D.getIdentifierLoc()); |
| 2807 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2808 | SC = FunctionDecl::None; |
| 2809 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2810 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2811 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2812 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2813 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2814 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2815 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2816 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2817 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2818 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2819 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2820 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2821 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2822 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2823 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2824 | // Rebuild the function type "R" without any type qualifiers (in |
| 2825 | // case any of the errors above fired) and with "void" as the |
| 2826 | // return type, since constructors don't have return types. We |
| 2827 | // *always* have to do this, because GetTypeForDeclarator will |
| 2828 | // put in a result type of "int" when none was specified. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2829 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2830 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 2831 | Proto->getNumArgs(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2832 | Proto->isVariadic(), 0, |
| 2833 | Proto->hasExceptionSpec(), |
| 2834 | Proto->hasAnyExceptionSpec(), |
| 2835 | Proto->getNumExceptions(), |
| 2836 | Proto->exception_begin(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2837 | Proto->getExtInfo()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2840 | /// CheckConstructor - Checks a fully-formed constructor for |
| 2841 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 2842 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2843 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2844 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2845 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 2846 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2847 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2848 | |
| 2849 | // C++ [class.copy]p3: |
| 2850 | // A declaration of a constructor for a class X is ill-formed if |
| 2851 | // its first parameter is of type (optionally cv-qualified) X and |
| 2852 | // either there are no other parameters or else all other |
| 2853 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2854 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | ((Constructor->getNumParams() == 1) || |
| 2856 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2857 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 2858 | Constructor->getTemplateSpecializationKind() |
| 2859 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2860 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 2861 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 2862 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2863 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
| 2864 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2865 | << FixItHint::CreateInsertion(ParamLoc, " const &"); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2866 | |
| 2867 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 2868 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2869 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2870 | } |
| 2871 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2872 | |
John McCall | 3d04336 | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2873 | // Notify the class that we've added a constructor. In principle we |
| 2874 | // don't need to do this for out-of-line declarations; in practice |
| 2875 | // we only instantiate the most recent declaration of a method, so |
| 2876 | // we have to call this for everything but friends. |
| 2877 | if (!Constructor->getFriendObjectKind()) |
| 2878 | ClassDecl->addedConstructor(Context, Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2881 | /// CheckDestructor - Checks a fully-formed destructor for well-formedness, |
| 2882 | /// issuing any diagnostics required. Returns true on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2883 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2884 | CXXRecordDecl *RD = Destructor->getParent(); |
| 2885 | |
| 2886 | if (Destructor->isVirtual()) { |
| 2887 | SourceLocation Loc; |
| 2888 | |
| 2889 | if (!Destructor->isImplicit()) |
| 2890 | Loc = Destructor->getLocation(); |
| 2891 | else |
| 2892 | Loc = RD->getLocation(); |
| 2893 | |
| 2894 | // If we have a virtual destructor, look up the deallocation function |
| 2895 | FunctionDecl *OperatorDelete = 0; |
| 2896 | DeclarationName Name = |
| 2897 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2898 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2899 | return true; |
| 2900 | |
| 2901 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2902 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2903 | |
| 2904 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2908 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 2909 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 2910 | FTI.ArgInfo[0].Param && |
| 2911 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType()); |
| 2912 | } |
| 2913 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2914 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 2915 | /// the well-formednes of the destructor declarator @p D with type @p |
| 2916 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2917 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 2918 | /// will be updated to reflect a well-formed type for the destructor and |
| 2919 | /// returned. |
| 2920 | QualType Sema::CheckDestructorDeclarator(Declarator &D, |
| 2921 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2922 | // C++ [class.dtor]p1: |
| 2923 | // [...] A typedef-name that names a class is a class-name |
| 2924 | // (7.1.3); however, a typedef-name that names a class shall not |
| 2925 | // be used as the identifier in the declarator for a destructor |
| 2926 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2927 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2928 | if (isa<TypedefType>(DeclaratorType)) { |
| 2929 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2930 | << DeclaratorType; |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2931 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | // C++ [class.dtor]p2: |
| 2935 | // A destructor is used to destroy objects of its class type. A |
| 2936 | // destructor takes no parameters, and no return type can be |
| 2937 | // specified for it (not even void). The address of a destructor |
| 2938 | // shall not be taken. A destructor shall not be static. A |
| 2939 | // destructor can be invoked for a const, volatile or const |
| 2940 | // volatile object. A destructor shall not be declared const, |
| 2941 | // volatile or const volatile (9.3.2). |
| 2942 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2943 | if (!D.isInvalidType()) |
| 2944 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 2945 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2946 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2947 | SC = FunctionDecl::None; |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2948 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2949 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2950 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2951 | // Destructors don't have return types, but the parser will |
| 2952 | // happily parse something like: |
| 2953 | // |
| 2954 | // class X { |
| 2955 | // float ~X(); |
| 2956 | // }; |
| 2957 | // |
| 2958 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2959 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 2960 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2961 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2962 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2963 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2964 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2965 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2966 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2967 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2968 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2969 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2970 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2971 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2972 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2973 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2974 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2975 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2979 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2980 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 2981 | |
| 2982 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2983 | FTI.freeArgs(); |
| 2984 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2987 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2988 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2989 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2990 | D.setInvalidType(); |
| 2991 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2992 | |
| 2993 | // Rebuild the function type "R" without any type qualifiers or |
| 2994 | // parameters (in case any of the errors above fired) and with |
| 2995 | // "void" as the return type, since destructors don't have return |
| 2996 | // types. We *always* have to do this, because GetTypeForDeclarator |
| 2997 | // will put in a result type of "int" when none was specified. |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2998 | // FIXME: Exceptions! |
| 2999 | return Context.getFunctionType(Context.VoidTy, 0, 0, false, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 3000 | false, false, 0, 0, FunctionType::ExtInfo()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3003 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 3004 | /// well-formednes of the conversion function declarator @p D with |
| 3005 | /// type @p R. If there are any errors in the declarator, this routine |
| 3006 | /// will emit diagnostics and return true. Otherwise, it will return |
| 3007 | /// false. Either way, the type @p R will be updated to reflect a |
| 3008 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3009 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3010 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3011 | // C++ [class.conv.fct]p1: |
| 3012 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3013 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3014 | // parameter returning conversion-type-id." |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3015 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3016 | if (!D.isInvalidType()) |
| 3017 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 3018 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 3019 | << SourceRange(D.getIdentifierLoc()); |
| 3020 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3021 | SC = FunctionDecl::None; |
| 3022 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3023 | |
| 3024 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 3025 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3026 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3027 | // Conversion functions don't have return types, but the parser will |
| 3028 | // happily parse something like: |
| 3029 | // |
| 3030 | // class X { |
| 3031 | // float operator bool(); |
| 3032 | // }; |
| 3033 | // |
| 3034 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3035 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 3036 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 3037 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3038 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3041 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 3042 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3043 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3044 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3045 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 3046 | |
| 3047 | // Delete the parameters. |
Chris Lattner | 1833a83 | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 3048 | D.getTypeObject(0).Fun.freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3049 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3050 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3051 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3052 | D.setInvalidType(); |
| 3053 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3054 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3055 | // Diagnose "&operator bool()" and other such nonsense. This |
| 3056 | // is actually a gcc extension which we don't support. |
| 3057 | if (Proto->getResultType() != ConvType) { |
| 3058 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 3059 | << Proto->getResultType(); |
| 3060 | D.setInvalidType(); |
| 3061 | ConvType = Proto->getResultType(); |
| 3062 | } |
| 3063 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3064 | // C++ [class.conv.fct]p4: |
| 3065 | // The conversion-type-id shall not represent a function type nor |
| 3066 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3067 | if (ConvType->isArrayType()) { |
| 3068 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 3069 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3070 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3071 | } else if (ConvType->isFunctionType()) { |
| 3072 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 3073 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3074 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | // Rebuild the function type "R" without any parameters (in case any |
| 3078 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3079 | // return type. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3080 | if (D.isInvalidType()) { |
| 3081 | R = Context.getFunctionType(ConvType, 0, 0, false, |
| 3082 | Proto->getTypeQuals(), |
| 3083 | Proto->hasExceptionSpec(), |
| 3084 | Proto->hasAnyExceptionSpec(), |
| 3085 | Proto->getNumExceptions(), |
| 3086 | Proto->exception_begin(), |
| 3087 | Proto->getExtInfo()); |
| 3088 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3089 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3090 | // C++0x explicit conversion operators. |
| 3091 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3093 | diag::warn_explicit_conversion_functions) |
| 3094 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3097 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 3098 | /// the declaration of the given C++ conversion function. This routine |
| 3099 | /// is responsible for recording the conversion function in the C++ |
| 3100 | /// class, if possible. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3101 | Sema::DeclPtrTy Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3102 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 3103 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 3104 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3105 | |
| 3106 | // Make sure we aren't redeclaring the conversion function. |
| 3107 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3108 | |
| 3109 | // C++ [class.conv.fct]p1: |
| 3110 | // [...] A conversion function is never used to convert a |
| 3111 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 3112 | // same object type (or a reference to it), to a (possibly |
| 3113 | // cv-qualified) base class of that type (or a reference to it), |
| 3114 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3115 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 3116 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3118 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3119 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3120 | ConvType = ConvTypeRef->getPointeeType(); |
| 3121 | if (ConvType->isRecordType()) { |
| 3122 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 3123 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3124 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3125 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3126 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3127 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3128 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3129 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3130 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3131 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3134 | if (Conversion->getPrimaryTemplate()) { |
| 3135 | // ignore specializations |
| 3136 | } else if (Conversion->getPreviousDeclaration()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3137 | if (FunctionTemplateDecl *ConversionTemplate |
Douglas Gregor | 0c55106 | 2010-01-11 18:53:25 +0000 | [diff] [blame] | 3138 | = Conversion->getDescribedFunctionTemplate()) { |
| 3139 | if (ClassDecl->replaceConversion( |
| 3140 | ConversionTemplate->getPreviousDeclaration(), |
| 3141 | ConversionTemplate)) |
| 3142 | return DeclPtrTy::make(ConversionTemplate); |
| 3143 | } else if (ClassDecl->replaceConversion(Conversion->getPreviousDeclaration(), |
| 3144 | Conversion)) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3145 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 3146 | assert(Conversion->isInvalidDecl() && "Conversion should not get here."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3147 | } else if (FunctionTemplateDecl *ConversionTemplate |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3148 | = Conversion->getDescribedFunctionTemplate()) |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 3149 | ClassDecl->addConversionFunction(ConversionTemplate); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3150 | else |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 3151 | ClassDecl->addConversionFunction(Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3152 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3153 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3156 | //===----------------------------------------------------------------------===// |
| 3157 | // Namespace Handling |
| 3158 | //===----------------------------------------------------------------------===// |
| 3159 | |
| 3160 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 3161 | /// definition. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3162 | Sema::DeclPtrTy Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 3163 | SourceLocation IdentLoc, |
| 3164 | IdentifierInfo *II, |
Anders Carlsson | 2a3503d | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 3165 | SourceLocation LBrace, |
| 3166 | AttributeList *AttrList) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3167 | NamespaceDecl *Namespc = |
| 3168 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 3169 | Namespc->setLBracLoc(LBrace); |
| 3170 | |
| 3171 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 3172 | |
Anders Carlsson | 2a3503d | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 3173 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 3174 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3175 | if (II) { |
| 3176 | // C++ [namespace.def]p2: |
| 3177 | // The identifier in an original-namespace-definition shall not have been |
| 3178 | // previously defined in the declarative region in which the |
| 3179 | // original-namespace-definition appears. The identifier in an |
| 3180 | // original-namespace-definition is the name of the namespace. Subsequently |
| 3181 | // in that declarative region, it is treated as an original-namespace-name. |
| 3182 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3183 | NamedDecl *PrevDecl |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 3184 | = LookupSingleName(DeclRegionScope, II, IdentLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 3185 | ForRedeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3187 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 3188 | // This is an extended namespace definition. |
| 3189 | // Attach this namespace decl to the chain of extended namespace |
| 3190 | // definitions. |
| 3191 | OrigNS->setNextNamespace(Namespc); |
| 3192 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3193 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3194 | // Remove the previous declaration from the scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3195 | if (DeclRegionScope->isDeclScope(DeclPtrTy::make(OrigNS))) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 3196 | IdResolver.RemoveDecl(OrigNS); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3197 | DeclRegionScope->RemoveDecl(DeclPtrTy::make(OrigNS)); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3198 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3199 | } else if (PrevDecl) { |
| 3200 | // This is an invalid name redefinition. |
| 3201 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 3202 | << Namespc->getDeclName(); |
| 3203 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3204 | Namespc->setInvalidDecl(); |
| 3205 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3206 | } else if (II->isStr("std") && |
| 3207 | CurContext->getLookupContext()->isTranslationUnit()) { |
| 3208 | // This is the first "real" definition of the namespace "std", so update |
| 3209 | // our cache of the "std" namespace to point at this definition. |
| 3210 | if (StdNamespace) { |
| 3211 | // We had already defined a dummy namespace "std". Link this new |
| 3212 | // namespace definition to the dummy namespace "std". |
| 3213 | StdNamespace->setNextNamespace(Namespc); |
| 3214 | StdNamespace->setLocation(IdentLoc); |
| 3215 | Namespc->setOriginalNamespace(StdNamespace->getOriginalNamespace()); |
| 3216 | } |
| 3217 | |
| 3218 | // Make our StdNamespace cache point at the first real definition of the |
| 3219 | // "std" namespace. |
| 3220 | StdNamespace = Namespc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3221 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3222 | |
| 3223 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 3224 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3225 | // Anonymous namespaces. |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3226 | assert(Namespc->isAnonymousNamespace()); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3227 | |
| 3228 | // Link the anonymous namespace into its parent. |
| 3229 | NamespaceDecl *PrevDecl; |
| 3230 | DeclContext *Parent = CurContext->getLookupContext(); |
| 3231 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 3232 | PrevDecl = TU->getAnonymousNamespace(); |
| 3233 | TU->setAnonymousNamespace(Namespc); |
| 3234 | } else { |
| 3235 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 3236 | PrevDecl = ND->getAnonymousNamespace(); |
| 3237 | ND->setAnonymousNamespace(Namespc); |
| 3238 | } |
| 3239 | |
| 3240 | // Link the anonymous namespace with its previous declaration. |
| 3241 | if (PrevDecl) { |
| 3242 | assert(PrevDecl->isAnonymousNamespace()); |
| 3243 | assert(!PrevDecl->getNextNamespace()); |
| 3244 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 3245 | PrevDecl->setNextNamespace(Namespc); |
| 3246 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3247 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 3248 | CurContext->addDecl(Namespc); |
| 3249 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3250 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 3251 | // behaves as if it were replaced by |
| 3252 | // namespace unique { /* empty body */ } |
| 3253 | // using namespace unique; |
| 3254 | // namespace unique { namespace-body } |
| 3255 | // where all occurrences of 'unique' in a translation unit are |
| 3256 | // replaced by the same identifier and this identifier differs |
| 3257 | // from all other identifiers in the entire program. |
| 3258 | |
| 3259 | // We just create the namespace with an empty name and then add an |
| 3260 | // implicit using declaration, just like the standard suggests. |
| 3261 | // |
| 3262 | // CodeGen enforces the "universally unique" aspect by giving all |
| 3263 | // declarations semantically contained within an anonymous |
| 3264 | // namespace internal linkage. |
| 3265 | |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3266 | if (!PrevDecl) { |
| 3267 | UsingDirectiveDecl* UD |
| 3268 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 3269 | /* 'using' */ LBrace, |
| 3270 | /* 'namespace' */ SourceLocation(), |
| 3271 | /* qualifier */ SourceRange(), |
| 3272 | /* NNS */ NULL, |
| 3273 | /* identifier */ SourceLocation(), |
| 3274 | Namespc, |
| 3275 | /* Ancestor */ CurContext); |
| 3276 | UD->setImplicit(); |
| 3277 | CurContext->addDecl(UD); |
| 3278 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 3282 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3283 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 3284 | // for the namespace has the declarations that showed up in that particular |
| 3285 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3286 | PushDeclContext(NamespcScope, Namespc); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3287 | return DeclPtrTy::make(Namespc); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3290 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 3291 | /// is a namespace alias, returns the namespace it points to. |
| 3292 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 3293 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 3294 | return AD->getNamespace(); |
| 3295 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 3296 | } |
| 3297 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3298 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 3299 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3300 | void Sema::ActOnFinishNamespaceDef(DeclPtrTy D, SourceLocation RBrace) { |
| 3301 | Decl *Dcl = D.getAs<Decl>(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3302 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 3303 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 3304 | Namespc->setRBracLoc(RBrace); |
| 3305 | PopDeclContext(); |
| 3306 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3307 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3308 | Sema::DeclPtrTy Sema::ActOnUsingDirective(Scope *S, |
| 3309 | SourceLocation UsingLoc, |
| 3310 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3311 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3312 | SourceLocation IdentLoc, |
| 3313 | IdentifierInfo *NamespcName, |
| 3314 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3315 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3316 | assert(NamespcName && "Invalid NamespcName."); |
| 3317 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3318 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3319 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3320 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3321 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 3322 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3323 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 3324 | LookupParsedName(R, S, &SS); |
| 3325 | if (R.isAmbiguous()) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3326 | return DeclPtrTy(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3327 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3328 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3329 | NamedDecl *Named = R.getFoundDecl(); |
| 3330 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 3331 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3332 | // C++ [namespace.udir]p1: |
| 3333 | // A using-directive specifies that the names in the nominated |
| 3334 | // namespace can be used in the scope in which the |
| 3335 | // using-directive appears after the using-directive. During |
| 3336 | // unqualified name lookup (3.4.1), the names appear as if they |
| 3337 | // were declared in the nearest enclosing namespace which |
| 3338 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3339 | // namespace. [Note: in this context, "contains" means "contains |
| 3340 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3341 | |
| 3342 | // Find enclosing context containing both using-directive and |
| 3343 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3344 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3345 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 3346 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 3347 | CommonAncestor = CommonAncestor->getParent(); |
| 3348 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3349 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 3350 | SS.getRange(), |
| 3351 | (NestedNameSpecifier *)SS.getScopeRep(), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3352 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3353 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3354 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 3355 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3358 | // FIXME: We ignore attributes for now. |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3359 | delete AttrList; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3360 | return DeclPtrTy::make(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 3364 | // If scope has associated entity, then using directive is at namespace |
| 3365 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 3366 | // it's lookup structure. |
| 3367 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3368 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3369 | else |
| 3370 | // Otherwise it is block-sope. using-directives will affect lookup |
| 3371 | // only to the end of scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3372 | S->PushUsingDirective(DeclPtrTy::make(UDir)); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3373 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3374 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3375 | |
| 3376 | Sema::DeclPtrTy Sema::ActOnUsingDeclaration(Scope *S, |
Anders Carlsson | 595adc1 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 3377 | AccessSpecifier AS, |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3378 | bool HasUsingKeyword, |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3379 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3380 | CXXScopeSpec &SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3381 | UnqualifiedId &Name, |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3382 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3383 | bool IsTypeName, |
| 3384 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3385 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3386 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3387 | switch (Name.getKind()) { |
| 3388 | case UnqualifiedId::IK_Identifier: |
| 3389 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3390 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3391 | case UnqualifiedId::IK_ConversionFunctionId: |
| 3392 | break; |
| 3393 | |
| 3394 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3395 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3396 | // C++0x inherited constructors. |
| 3397 | if (getLangOptions().CPlusPlus0x) break; |
| 3398 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3399 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 3400 | << SS.getRange(); |
| 3401 | return DeclPtrTy(); |
| 3402 | |
| 3403 | case UnqualifiedId::IK_DestructorName: |
| 3404 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 3405 | << SS.getRange(); |
| 3406 | return DeclPtrTy(); |
| 3407 | |
| 3408 | case UnqualifiedId::IK_TemplateId: |
| 3409 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 3410 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
| 3411 | return DeclPtrTy(); |
| 3412 | } |
| 3413 | |
| 3414 | DeclarationName TargetName = GetNameFromUnqualifiedId(Name); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3415 | if (!TargetName) |
| 3416 | return DeclPtrTy(); |
| 3417 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3418 | // Warn about using declarations. |
| 3419 | // TODO: store that the declaration was written without 'using' and |
| 3420 | // talk about access decls instead of using decls in the |
| 3421 | // diagnostics. |
| 3422 | if (!HasUsingKeyword) { |
| 3423 | UsingLoc = Name.getSourceRange().getBegin(); |
| 3424 | |
| 3425 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3426 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3429 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3430 | Name.getSourceRange().getBegin(), |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3431 | TargetName, AttrList, |
| 3432 | /* IsInstantiation */ false, |
| 3433 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3434 | if (UD) |
| 3435 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3437 | return DeclPtrTy::make(UD); |
| 3438 | } |
| 3439 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3440 | /// Determines whether to create a using shadow decl for a particular |
| 3441 | /// decl, given the set of decls existing prior to this using lookup. |
| 3442 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 3443 | const LookupResult &Previous) { |
| 3444 | // Diagnose finding a decl which is not from a base class of the |
| 3445 | // current class. We do this now because there are cases where this |
| 3446 | // function will silently decide not to build a shadow decl, which |
| 3447 | // will pre-empt further diagnostics. |
| 3448 | // |
| 3449 | // We don't need to do this in C++0x because we do the check once on |
| 3450 | // the qualifier. |
| 3451 | // |
| 3452 | // FIXME: diagnose the following if we care enough: |
| 3453 | // struct A { int foo; }; |
| 3454 | // struct B : A { using A::foo; }; |
| 3455 | // template <class T> struct C : A {}; |
| 3456 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 3457 | // This is invalid (during instantiation) in C++03 because B::foo |
| 3458 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 3459 | // We can't diagnose it immediately because C<T> is an unknown |
| 3460 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 3461 | // to A::foo, which will look well-formed when we instantiate. |
| 3462 | // The right solution is to not collapse the shadow-decl chain. |
| 3463 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 3464 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 3465 | |
| 3466 | // Handle enums and anonymous structs. |
| 3467 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 3468 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 3469 | while (OrigRec->isAnonymousStructOrUnion()) |
| 3470 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 3471 | |
| 3472 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 3473 | if (OrigDC == CurContext) { |
| 3474 | Diag(Using->getLocation(), |
| 3475 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3476 | << Using->getNestedNameRange(); |
| 3477 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3478 | return true; |
| 3479 | } |
| 3480 | |
| 3481 | Diag(Using->getNestedNameRange().getBegin(), |
| 3482 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3483 | << Using->getTargetNestedNameDecl() |
| 3484 | << cast<CXXRecordDecl>(CurContext) |
| 3485 | << Using->getNestedNameRange(); |
| 3486 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3487 | return true; |
| 3488 | } |
| 3489 | } |
| 3490 | |
| 3491 | if (Previous.empty()) return false; |
| 3492 | |
| 3493 | NamedDecl *Target = Orig; |
| 3494 | if (isa<UsingShadowDecl>(Target)) |
| 3495 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3496 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3497 | // If the target happens to be one of the previous declarations, we |
| 3498 | // don't have a conflict. |
| 3499 | // |
| 3500 | // FIXME: but we might be increasing its access, in which case we |
| 3501 | // should redeclare it. |
| 3502 | NamedDecl *NonTag = 0, *Tag = 0; |
| 3503 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 3504 | I != E; ++I) { |
| 3505 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3506 | if (D->getCanonicalDecl() == Target->getCanonicalDecl()) |
| 3507 | return false; |
| 3508 | |
| 3509 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 3510 | } |
| 3511 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3512 | if (Target->isFunctionOrFunctionTemplate()) { |
| 3513 | FunctionDecl *FD; |
| 3514 | if (isa<FunctionTemplateDecl>(Target)) |
| 3515 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 3516 | else |
| 3517 | FD = cast<FunctionDecl>(Target); |
| 3518 | |
| 3519 | NamedDecl *OldDecl = 0; |
| 3520 | switch (CheckOverload(FD, Previous, OldDecl)) { |
| 3521 | case Ovl_Overload: |
| 3522 | return false; |
| 3523 | |
| 3524 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3525 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3526 | break; |
| 3527 | |
| 3528 | // We found a decl with the exact signature. |
| 3529 | case Ovl_Match: |
| 3530 | if (isa<UsingShadowDecl>(OldDecl)) { |
| 3531 | // Silently ignore the possible conflict. |
| 3532 | return false; |
| 3533 | } |
| 3534 | |
| 3535 | // If we're in a record, we want to hide the target, so we |
| 3536 | // return true (without a diagnostic) to tell the caller not to |
| 3537 | // build a shadow decl. |
| 3538 | if (CurContext->isRecord()) |
| 3539 | return true; |
| 3540 | |
| 3541 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3542 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3543 | break; |
| 3544 | } |
| 3545 | |
| 3546 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3547 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 3548 | return true; |
| 3549 | } |
| 3550 | |
| 3551 | // Target is not a function. |
| 3552 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3553 | if (isa<TagDecl>(Target)) { |
| 3554 | // No conflict between a tag and a non-tag. |
| 3555 | if (!Tag) return false; |
| 3556 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3557 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3558 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3559 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 3560 | return true; |
| 3561 | } |
| 3562 | |
| 3563 | // No conflict between a tag and a non-tag. |
| 3564 | if (!NonTag) return false; |
| 3565 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3566 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3567 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3568 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 3569 | return true; |
| 3570 | } |
| 3571 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3572 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3573 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3574 | UsingDecl *UD, |
| 3575 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3576 | |
| 3577 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3578 | NamedDecl *Target = Orig; |
| 3579 | if (isa<UsingShadowDecl>(Target)) { |
| 3580 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3581 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3582 | } |
| 3583 | |
| 3584 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3585 | = UsingShadowDecl::Create(Context, CurContext, |
| 3586 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3587 | UD->addShadowDecl(Shadow); |
| 3588 | |
| 3589 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3590 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3591 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3592 | CurContext->addDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3593 | Shadow->setAccess(UD->getAccess()); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3594 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3595 | // Register it as a conversion if appropriate. |
| 3596 | if (Shadow->getDeclName().getNameKind() |
| 3597 | == DeclarationName::CXXConversionFunctionName) |
| 3598 | cast<CXXRecordDecl>(CurContext)->addConversionFunction(Shadow); |
| 3599 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3600 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 3601 | Shadow->setInvalidDecl(); |
| 3602 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3603 | return Shadow; |
| 3604 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3605 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3606 | /// Hides a using shadow declaration. This is required by the current |
| 3607 | /// using-decl implementation when a resolvable using declaration in a |
| 3608 | /// class is followed by a declaration which would hide or override |
| 3609 | /// one or more of the using decl's targets; for example: |
| 3610 | /// |
| 3611 | /// struct Base { void foo(int); }; |
| 3612 | /// struct Derived : Base { |
| 3613 | /// using Base::foo; |
| 3614 | /// void foo(int); |
| 3615 | /// }; |
| 3616 | /// |
| 3617 | /// The governing language is C++03 [namespace.udecl]p12: |
| 3618 | /// |
| 3619 | /// When a using-declaration brings names from a base class into a |
| 3620 | /// derived class scope, member functions in the derived class |
| 3621 | /// override and/or hide member functions with the same name and |
| 3622 | /// parameter types in a base class (rather than conflicting). |
| 3623 | /// |
| 3624 | /// There are two ways to implement this: |
| 3625 | /// (1) optimistically create shadow decls when they're not hidden |
| 3626 | /// by existing declarations, or |
| 3627 | /// (2) don't create any shadow decls (or at least don't make them |
| 3628 | /// visible) until we've fully parsed/instantiated the class. |
| 3629 | /// The problem with (1) is that we might have to retroactively remove |
| 3630 | /// a shadow decl, which requires several O(n) operations because the |
| 3631 | /// decl structures are (very reasonably) not designed for removal. |
| 3632 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 3633 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3634 | if (Shadow->getDeclName().getNameKind() == |
| 3635 | DeclarationName::CXXConversionFunctionName) |
| 3636 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 3637 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3638 | // Remove it from the DeclContext... |
| 3639 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3640 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3641 | // ...and the scope, if applicable... |
| 3642 | if (S) { |
| 3643 | S->RemoveDecl(DeclPtrTy::make(static_cast<Decl*>(Shadow))); |
| 3644 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3647 | // ...and the using decl. |
| 3648 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 3649 | |
| 3650 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3651 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3654 | /// Builds a using declaration. |
| 3655 | /// |
| 3656 | /// \param IsInstantiation - Whether this call arises from an |
| 3657 | /// instantiation of an unresolved using declaration. We treat |
| 3658 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3659 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 3660 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3661 | CXXScopeSpec &SS, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3662 | SourceLocation IdentLoc, |
| 3663 | DeclarationName Name, |
| 3664 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3665 | bool IsInstantiation, |
| 3666 | bool IsTypeName, |
| 3667 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3668 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3669 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 3670 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3671 | // FIXME: We ignore attributes for now. |
| 3672 | delete AttrList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3674 | if (SS.isEmpty()) { |
| 3675 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3676 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3677 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3678 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3679 | // Do the redeclaration lookup in the current scope. |
| 3680 | LookupResult Previous(*this, Name, IdentLoc, LookupUsingDeclName, |
| 3681 | ForRedeclaration); |
| 3682 | Previous.setHideTags(false); |
| 3683 | if (S) { |
| 3684 | LookupName(Previous, S); |
| 3685 | |
| 3686 | // It is really dumb that we have to do this. |
| 3687 | LookupResult::Filter F = Previous.makeFilter(); |
| 3688 | while (F.hasNext()) { |
| 3689 | NamedDecl *D = F.next(); |
| 3690 | if (!isDeclInScope(D, CurContext, S)) |
| 3691 | F.erase(); |
| 3692 | } |
| 3693 | F.done(); |
| 3694 | } else { |
| 3695 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 3696 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 3697 | LookupQualifiedName(Previous, CurContext); |
| 3698 | } |
| 3699 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3700 | NestedNameSpecifier *NNS = |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3701 | static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3702 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3703 | // Check for invalid redeclarations. |
| 3704 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 3705 | return 0; |
| 3706 | |
| 3707 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3708 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 3709 | return 0; |
| 3710 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3711 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3712 | NamedDecl *D; |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3713 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3714 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3715 | // FIXME: not all declaration name kinds are legal here |
| 3716 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 3717 | UsingLoc, TypenameLoc, |
| 3718 | SS.getRange(), NNS, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3719 | IdentLoc, Name); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3720 | } else { |
| 3721 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, |
| 3722 | UsingLoc, SS.getRange(), NNS, |
| 3723 | IdentLoc, Name); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3724 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3725 | } else { |
| 3726 | D = UsingDecl::Create(Context, CurContext, IdentLoc, |
| 3727 | SS.getRange(), UsingLoc, NNS, Name, |
| 3728 | IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3729 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3730 | D->setAccess(AS); |
| 3731 | CurContext->addDecl(D); |
| 3732 | |
| 3733 | if (!LookupContext) return D; |
| 3734 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3735 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3736 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3737 | UD->setInvalidDecl(); |
| 3738 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3741 | // Look up the target name. |
| 3742 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3743 | LookupResult R(*this, Name, IdentLoc, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3744 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3745 | // Unlike most lookups, we don't always want to hide tag |
| 3746 | // declarations: tag names are visible through the using declaration |
| 3747 | // even if hidden by ordinary names, *except* in a dependent context |
| 3748 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3749 | if (!IsInstantiation) |
| 3750 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3751 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3752 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3753 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3754 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 3755 | Diag(IdentLoc, diag::err_no_member) |
| 3756 | << Name << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3757 | UD->setInvalidDecl(); |
| 3758 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3759 | } |
| 3760 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3761 | if (R.isAmbiguous()) { |
| 3762 | UD->setInvalidDecl(); |
| 3763 | return UD; |
| 3764 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3765 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3766 | if (IsTypeName) { |
| 3767 | // 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] | 3768 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3769 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 3770 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 3771 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 3772 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3773 | UD->setInvalidDecl(); |
| 3774 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3775 | } |
| 3776 | } else { |
| 3777 | // If we asked for a non-typename and we got a type, error out, |
| 3778 | // but only if this is an instantiation of an unresolved using |
| 3779 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3780 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3781 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 3782 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3783 | UD->setInvalidDecl(); |
| 3784 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3785 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3788 | // C++0x N2914 [namespace.udecl]p6: |
| 3789 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3790 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3791 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 3792 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3793 | UD->setInvalidDecl(); |
| 3794 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3795 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3796 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3797 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 3798 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 3799 | BuildUsingShadowDecl(S, UD, *I); |
| 3800 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3801 | |
| 3802 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3805 | /// Checks that the given using declaration is not an invalid |
| 3806 | /// redeclaration. Note that this is checking only for the using decl |
| 3807 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 3808 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 3809 | bool isTypeName, |
| 3810 | const CXXScopeSpec &SS, |
| 3811 | SourceLocation NameLoc, |
| 3812 | const LookupResult &Prev) { |
| 3813 | // C++03 [namespace.udecl]p8: |
| 3814 | // C++0x [namespace.udecl]p10: |
| 3815 | // A using-declaration is a declaration and can therefore be used |
| 3816 | // repeatedly where (and only where) multiple declarations are |
| 3817 | // allowed. |
| 3818 | // That's only in file contexts. |
| 3819 | if (CurContext->getLookupContext()->isFileContext()) |
| 3820 | return false; |
| 3821 | |
| 3822 | NestedNameSpecifier *Qual |
| 3823 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 3824 | |
| 3825 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 3826 | NamedDecl *D = *I; |
| 3827 | |
| 3828 | bool DTypename; |
| 3829 | NestedNameSpecifier *DQual; |
| 3830 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 3831 | DTypename = UD->isTypeName(); |
| 3832 | DQual = UD->getTargetNestedNameDecl(); |
| 3833 | } else if (UnresolvedUsingValueDecl *UD |
| 3834 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 3835 | DTypename = false; |
| 3836 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3837 | } else if (UnresolvedUsingTypenameDecl *UD |
| 3838 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 3839 | DTypename = true; |
| 3840 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3841 | } else continue; |
| 3842 | |
| 3843 | // using decls differ if one says 'typename' and the other doesn't. |
| 3844 | // FIXME: non-dependent using decls? |
| 3845 | if (isTypeName != DTypename) continue; |
| 3846 | |
| 3847 | // using decls differ if they name different scopes (but note that |
| 3848 | // template instantiation can cause this check to trigger when it |
| 3849 | // didn't before instantiation). |
| 3850 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 3851 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 3852 | continue; |
| 3853 | |
| 3854 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3855 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3856 | return true; |
| 3857 | } |
| 3858 | |
| 3859 | return false; |
| 3860 | } |
| 3861 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3862 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3863 | /// Checks that the given nested-name qualifier used in a using decl |
| 3864 | /// in the current context is appropriately related to the current |
| 3865 | /// scope. If an error is found, diagnoses it and returns true. |
| 3866 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 3867 | const CXXScopeSpec &SS, |
| 3868 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3869 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3870 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3871 | if (!CurContext->isRecord()) { |
| 3872 | // C++03 [namespace.udecl]p3: |
| 3873 | // C++0x [namespace.udecl]p8: |
| 3874 | // A using-declaration for a class member shall be a member-declaration. |
| 3875 | |
| 3876 | // If we weren't able to compute a valid scope, it must be a |
| 3877 | // dependent class scope. |
| 3878 | if (!NamedContext || NamedContext->isRecord()) { |
| 3879 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 3880 | << SS.getRange(); |
| 3881 | return true; |
| 3882 | } |
| 3883 | |
| 3884 | // Otherwise, everything is known to be fine. |
| 3885 | return false; |
| 3886 | } |
| 3887 | |
| 3888 | // The current scope is a record. |
| 3889 | |
| 3890 | // If the named context is dependent, we can't decide much. |
| 3891 | if (!NamedContext) { |
| 3892 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 3893 | // nested-name-specifier does not refer to a base class, which is |
| 3894 | // still possible in some cases. |
| 3895 | |
| 3896 | // Otherwise we have to conservatively report that things might be |
| 3897 | // okay. |
| 3898 | return false; |
| 3899 | } |
| 3900 | |
| 3901 | if (!NamedContext->isRecord()) { |
| 3902 | // Ideally this would point at the last name in the specifier, |
| 3903 | // but we don't have that level of source info. |
| 3904 | Diag(SS.getRange().getBegin(), |
| 3905 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 3906 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 3907 | return true; |
| 3908 | } |
| 3909 | |
| 3910 | if (getLangOptions().CPlusPlus0x) { |
| 3911 | // C++0x [namespace.udecl]p3: |
| 3912 | // In a using-declaration used as a member-declaration, the |
| 3913 | // nested-name-specifier shall name a base class of the class |
| 3914 | // being defined. |
| 3915 | |
| 3916 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 3917 | cast<CXXRecordDecl>(NamedContext))) { |
| 3918 | if (CurContext == NamedContext) { |
| 3919 | Diag(NameLoc, |
| 3920 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3921 | << SS.getRange(); |
| 3922 | return true; |
| 3923 | } |
| 3924 | |
| 3925 | Diag(SS.getRange().getBegin(), |
| 3926 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3927 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 3928 | << cast<CXXRecordDecl>(CurContext) |
| 3929 | << SS.getRange(); |
| 3930 | return true; |
| 3931 | } |
| 3932 | |
| 3933 | return false; |
| 3934 | } |
| 3935 | |
| 3936 | // C++03 [namespace.udecl]p4: |
| 3937 | // A using-declaration used as a member-declaration shall refer |
| 3938 | // to a member of a base class of the class being defined [etc.]. |
| 3939 | |
| 3940 | // Salient point: SS doesn't have to name a base class as long as |
| 3941 | // lookup only finds members from base classes. Therefore we can |
| 3942 | // diagnose here only if we can prove that that can't happen, |
| 3943 | // i.e. if the class hierarchies provably don't intersect. |
| 3944 | |
| 3945 | // TODO: it would be nice if "definitely valid" results were cached |
| 3946 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 3947 | // need to be repeated. |
| 3948 | |
| 3949 | struct UserData { |
| 3950 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 3951 | |
| 3952 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 3953 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 3954 | Data->Bases.insert(Base); |
| 3955 | return true; |
| 3956 | } |
| 3957 | |
| 3958 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 3959 | return !Class->forallBases(collect, this); |
| 3960 | } |
| 3961 | |
| 3962 | /// Returns true if the base is dependent or is one of the |
| 3963 | /// accumulated base classes. |
| 3964 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 3965 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 3966 | return !Data->Bases.count(Base); |
| 3967 | } |
| 3968 | |
| 3969 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 3970 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 3971 | } |
| 3972 | }; |
| 3973 | |
| 3974 | UserData Data; |
| 3975 | |
| 3976 | // Returns false if we find a dependent base. |
| 3977 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 3978 | return false; |
| 3979 | |
| 3980 | // Returns false if the class has a dependent base or if it or one |
| 3981 | // of its bases is present in the base set of the current context. |
| 3982 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 3983 | return false; |
| 3984 | |
| 3985 | Diag(SS.getRange().getBegin(), |
| 3986 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3987 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 3988 | << cast<CXXRecordDecl>(CurContext) |
| 3989 | << SS.getRange(); |
| 3990 | |
| 3991 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3994 | Sema::DeclPtrTy Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 3995 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3996 | SourceLocation AliasLoc, |
| 3997 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3998 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 3999 | SourceLocation IdentLoc, |
| 4000 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4001 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4002 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4003 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 4004 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4005 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4006 | // Check if we have a previous declaration with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4007 | if (NamedDecl *PrevDecl |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4008 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 4009 | ForRedeclaration)) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4010 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | // 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] | 4012 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4013 | // FIXME: At some point, we'll want to create the (redundant) |
| 4014 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4015 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4016 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4017 | return DeclPtrTy(); |
| 4018 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4019 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4020 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 4021 | diag::err_redefinition_different_kind; |
| 4022 | Diag(AliasLoc, DiagID) << Alias; |
| 4023 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4024 | return DeclPtrTy(); |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4027 | if (R.isAmbiguous()) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4028 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4029 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4030 | if (R.empty()) { |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 4031 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4032 | return DeclPtrTy(); |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 4033 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4034 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4035 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4036 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 4037 | Alias, SS.getRange(), |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 4038 | (NestedNameSpecifier *)SS.getScopeRep(), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4039 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4040 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 4041 | PushOnScopeChains(AliasDecl, S); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 4042 | return DeclPtrTy::make(AliasDecl); |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4045 | namespace { |
| 4046 | /// \brief Scoped object used to handle the state changes required in Sema |
| 4047 | /// to implicitly define the body of a C++ member function; |
| 4048 | class ImplicitlyDefinedFunctionScope { |
| 4049 | Sema &S; |
| 4050 | DeclContext *PreviousContext; |
| 4051 | |
| 4052 | public: |
| 4053 | ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method) |
| 4054 | : S(S), PreviousContext(S.CurContext) |
| 4055 | { |
| 4056 | S.CurContext = Method; |
| 4057 | S.PushFunctionScope(); |
| 4058 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 4059 | } |
| 4060 | |
| 4061 | ~ImplicitlyDefinedFunctionScope() { |
| 4062 | S.PopExpressionEvaluationContext(); |
| 4063 | S.PopFunctionOrBlockScope(); |
| 4064 | S.CurContext = PreviousContext; |
| 4065 | } |
| 4066 | }; |
| 4067 | } |
| 4068 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4069 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 4070 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4071 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
| 4072 | !Constructor->isUsed()) && |
| 4073 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4074 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 4075 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4076 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 4077 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4078 | ImplicitlyDefinedFunctionScope Scope(*this, Constructor); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 4079 | if (SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false)) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4080 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 4081 | << CXXConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4082 | Constructor->setInvalidDecl(); |
| 4083 | } else { |
| 4084 | Constructor->setUsed(); |
| 4085 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4088 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 4089 | CXXDestructorDecl *Destructor) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4090 | assert((Destructor->isImplicit() && !Destructor->isUsed()) && |
| 4091 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4092 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4093 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4094 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4095 | ImplicitlyDefinedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4096 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 4097 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 4098 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4099 | |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4100 | // FIXME: If CheckDestructor fails, we should emit a note about where the |
| 4101 | // implicit destructor was needed. |
| 4102 | if (CheckDestructor(Destructor)) { |
| 4103 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4104 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 4105 | |
| 4106 | Destructor->setInvalidDecl(); |
| 4107 | return; |
| 4108 | } |
| 4109 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4110 | Destructor->setUsed(); |
| 4111 | } |
| 4112 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4113 | /// \brief Builds a statement that copies the given entity from \p From to |
| 4114 | /// \c To. |
| 4115 | /// |
| 4116 | /// This routine is used to copy the members of a class with an |
| 4117 | /// implicitly-declared copy assignment operator. When the entities being |
| 4118 | /// copied are arrays, this routine builds for loops to copy them. |
| 4119 | /// |
| 4120 | /// \param S The Sema object used for type-checking. |
| 4121 | /// |
| 4122 | /// \param Loc The location where the implicit copy is being generated. |
| 4123 | /// |
| 4124 | /// \param T The type of the expressions being copied. Both expressions must |
| 4125 | /// have this type. |
| 4126 | /// |
| 4127 | /// \param To The expression we are copying to. |
| 4128 | /// |
| 4129 | /// \param From The expression we are copying from. |
| 4130 | /// |
| 4131 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 4132 | /// |
| 4133 | /// \returns A statement or a loop that copies the expressions. |
| 4134 | static Sema::OwningStmtResult |
| 4135 | BuildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
| 4136 | Sema::OwningExprResult To, Sema::OwningExprResult From, |
| 4137 | unsigned Depth = 0) { |
| 4138 | typedef Sema::OwningStmtResult OwningStmtResult; |
| 4139 | typedef Sema::OwningExprResult OwningExprResult; |
| 4140 | |
| 4141 | // C++0x [class.copy]p30: |
| 4142 | // Each subobject is assigned in the manner appropriate to its type: |
| 4143 | // |
| 4144 | // - if the subobject is of class type, the copy assignment operator |
| 4145 | // for the class is used (as if by explicit qualification; that is, |
| 4146 | // ignoring any possible virtual overriding functions in more derived |
| 4147 | // classes); |
| 4148 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 4149 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4150 | |
| 4151 | // Look for operator=. |
| 4152 | DeclarationName Name |
| 4153 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 4154 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 4155 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| 4156 | |
| 4157 | // Filter out any result that isn't a copy-assignment operator. |
| 4158 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 4159 | while (F.hasNext()) { |
| 4160 | NamedDecl *D = F.next(); |
| 4161 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 4162 | if (Method->isCopyAssignmentOperator()) |
| 4163 | continue; |
| 4164 | |
| 4165 | F.erase(); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4166 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4167 | F.done(); |
| 4168 | |
| 4169 | // Create the nested-name-specifier that will be used to qualify the |
| 4170 | // reference to operator=; this is required to suppress the virtual |
| 4171 | // call mechanism. |
| 4172 | CXXScopeSpec SS; |
| 4173 | SS.setRange(Loc); |
| 4174 | SS.setScopeRep(NestedNameSpecifier::Create(S.Context, 0, false, |
| 4175 | T.getTypePtr())); |
| 4176 | |
| 4177 | // Create the reference to operator=. |
| 4178 | OwningExprResult OpEqualRef |
| 4179 | = S.BuildMemberReferenceExpr(move(To), T, Loc, /*isArrow=*/false, SS, |
| 4180 | /*FirstQualifierInScope=*/0, OpLookup, |
| 4181 | /*TemplateArgs=*/0, |
| 4182 | /*SuppressQualifierCheck=*/true); |
| 4183 | if (OpEqualRef.isInvalid()) |
| 4184 | return S.StmtError(); |
| 4185 | |
| 4186 | // Build the call to the assignment operator. |
| 4187 | Expr *FromE = From.takeAs<Expr>(); |
| 4188 | OwningExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
| 4189 | OpEqualRef.takeAs<Expr>(), |
| 4190 | Loc, &FromE, 1, 0, Loc); |
| 4191 | if (Call.isInvalid()) |
| 4192 | return S.StmtError(); |
| 4193 | |
| 4194 | return S.Owned(Call.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4195 | } |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4196 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4197 | // - if the subobject is of scalar type, the built-in assignment |
| 4198 | // operator is used. |
| 4199 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| 4200 | if (!ArrayTy) { |
| 4201 | OwningExprResult Assignment = S.CreateBuiltinBinOp(Loc, |
| 4202 | BinaryOperator::Assign, |
| 4203 | To.takeAs<Expr>(), |
| 4204 | From.takeAs<Expr>()); |
| 4205 | if (Assignment.isInvalid()) |
| 4206 | return S.StmtError(); |
| 4207 | |
| 4208 | return S.Owned(Assignment.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4209 | } |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4210 | |
| 4211 | // - if the subobject is an array, each element is assigned, in the |
| 4212 | // manner appropriate to the element type; |
| 4213 | |
| 4214 | // Construct a loop over the array bounds, e.g., |
| 4215 | // |
| 4216 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 4217 | // |
| 4218 | // that will copy each of the array elements. |
| 4219 | QualType SizeType = S.Context.getSizeType(); |
| 4220 | |
| 4221 | // Create the iteration variable. |
| 4222 | IdentifierInfo *IterationVarName = 0; |
| 4223 | { |
| 4224 | llvm::SmallString<8> Str; |
| 4225 | llvm::raw_svector_ostream OS(Str); |
| 4226 | OS << "__i" << Depth; |
| 4227 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 4228 | } |
| 4229 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, |
| 4230 | IterationVarName, SizeType, |
| 4231 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
| 4232 | VarDecl::None, VarDecl::None); |
| 4233 | |
| 4234 | // Initialize the iteration variable to zero. |
| 4235 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
| 4236 | IterationVar->setInit(new (S.Context) IntegerLiteral(Zero, SizeType, Loc)); |
| 4237 | |
| 4238 | // Create a reference to the iteration variable; we'll use this several |
| 4239 | // times throughout. |
| 4240 | Expr *IterationVarRef |
| 4241 | = S.BuildDeclRefExpr(IterationVar, SizeType, Loc).takeAs<Expr>(); |
| 4242 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
| 4243 | |
| 4244 | // Create the DeclStmt that holds the iteration variable. |
| 4245 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| 4246 | |
| 4247 | // Create the comparison against the array bound. |
| 4248 | llvm::APInt Upper = ArrayTy->getSize(); |
| 4249 | Upper.zextOrTrunc(S.Context.getTypeSize(SizeType)); |
| 4250 | OwningExprResult Comparison |
| 4251 | = S.Owned(new (S.Context) BinaryOperator(IterationVarRef->Retain(), |
| 4252 | new (S.Context) IntegerLiteral(Upper, SizeType, Loc), |
| 4253 | BinaryOperator::NE, S.Context.BoolTy, Loc)); |
| 4254 | |
| 4255 | // Create the pre-increment of the iteration variable. |
| 4256 | OwningExprResult Increment |
| 4257 | = S.Owned(new (S.Context) UnaryOperator(IterationVarRef->Retain(), |
| 4258 | UnaryOperator::PreInc, |
| 4259 | SizeType, Loc)); |
| 4260 | |
| 4261 | // Subscript the "from" and "to" expressions with the iteration variable. |
| 4262 | From = S.CreateBuiltinArraySubscriptExpr(move(From), Loc, |
| 4263 | S.Owned(IterationVarRef->Retain()), |
| 4264 | Loc); |
| 4265 | To = S.CreateBuiltinArraySubscriptExpr(move(To), Loc, |
| 4266 | S.Owned(IterationVarRef->Retain()), |
| 4267 | Loc); |
| 4268 | assert(!From.isInvalid() && "Builtin subscripting can't fail!"); |
| 4269 | assert(!To.isInvalid() && "Builtin subscripting can't fail!"); |
| 4270 | |
| 4271 | // Build the copy for an individual element of the array. |
| 4272 | OwningStmtResult Copy = BuildSingleCopyAssign(S, Loc, |
| 4273 | ArrayTy->getElementType(), |
| 4274 | move(To), move(From), Depth+1); |
| 4275 | if (Copy.isInvalid()) { |
| 4276 | InitStmt->Destroy(S.Context); |
| 4277 | return S.StmtError(); |
| 4278 | } |
| 4279 | |
| 4280 | // Construct the loop that copies all elements of this array. |
| 4281 | return S.ActOnForStmt(Loc, Loc, S.Owned(InitStmt), |
| 4282 | S.MakeFullExpr(Comparison), |
| 4283 | Sema::DeclPtrTy(), |
| 4284 | S.MakeFullExpr(Increment), |
| 4285 | Loc, move(Copy)); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4288 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 4289 | CXXMethodDecl *CopyAssignOperator) { |
| 4290 | assert((CopyAssignOperator->isImplicit() && |
| 4291 | CopyAssignOperator->isOverloadedOperator() && |
| 4292 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
| 4293 | !CopyAssignOperator->isUsed()) && |
| 4294 | "DefineImplicitCopyAssignment called for wrong function"); |
| 4295 | |
| 4296 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 4297 | |
| 4298 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 4299 | CopyAssignOperator->setInvalidDecl(); |
| 4300 | return; |
| 4301 | } |
| 4302 | |
| 4303 | CopyAssignOperator->setUsed(); |
| 4304 | |
| 4305 | ImplicitlyDefinedFunctionScope Scope(*this, CopyAssignOperator); |
| 4306 | |
| 4307 | // C++0x [class.copy]p30: |
| 4308 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 4309 | // for a non-union class X performs memberwise copy assignment of its |
| 4310 | // subobjects. The direct base classes of X are assigned first, in the |
| 4311 | // order of their declaration in the base-specifier-list, and then the |
| 4312 | // immediate non-static data members of X are assigned, in the order in |
| 4313 | // which they were declared in the class definition. |
| 4314 | |
| 4315 | // The statements that form the synthesized function body. |
| 4316 | ASTOwningVector<&ActionBase::DeleteStmt> Statements(*this); |
| 4317 | |
| 4318 | // The parameter for the "other" object, which we are copying from. |
| 4319 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 4320 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 4321 | QualType OtherRefType = Other->getType(); |
| 4322 | if (const LValueReferenceType *OtherRef |
| 4323 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 4324 | OtherRefType = OtherRef->getPointeeType(); |
| 4325 | OtherQuals = OtherRefType.getQualifiers(); |
| 4326 | } |
| 4327 | |
| 4328 | // Our location for everything implicitly-generated. |
| 4329 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 4330 | |
| 4331 | // Construct a reference to the "other" object. We'll be using this |
| 4332 | // throughout the generated ASTs. |
| 4333 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, Loc).takeAs<Expr>(); |
| 4334 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 4335 | |
| 4336 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 4337 | // ASTs. |
| 4338 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 4339 | assert(This && "Reference to this cannot fail!"); |
| 4340 | |
| 4341 | // Assign base classes. |
| 4342 | bool Invalid = false; |
| 4343 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4344 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 4345 | // Form the assignment: |
| 4346 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 4347 | QualType BaseType = Base->getType().getUnqualifiedType(); |
| 4348 | CXXRecordDecl *BaseClassDecl = 0; |
| 4349 | if (const RecordType *BaseRecordT = BaseType->getAs<RecordType>()) |
| 4350 | BaseClassDecl = cast<CXXRecordDecl>(BaseRecordT->getDecl()); |
| 4351 | else { |
| 4352 | Invalid = true; |
| 4353 | continue; |
| 4354 | } |
| 4355 | |
| 4356 | // Construct the "from" expression, which is an implicit cast to the |
| 4357 | // appropriately-qualified base type. |
| 4358 | Expr *From = OtherRef->Retain(); |
| 4359 | ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
| 4360 | CastExpr::CK_UncheckedDerivedToBase, /*isLvalue=*/true, |
| 4361 | CXXBaseSpecifierArray(Base)); |
| 4362 | |
| 4363 | // Dereference "this". |
| 4364 | OwningExprResult To = CreateBuiltinUnaryOp(Loc, UnaryOperator::Deref, |
| 4365 | Owned(This->Retain())); |
| 4366 | |
| 4367 | // Implicitly cast "this" to the appropriately-qualified base type. |
| 4368 | Expr *ToE = To.takeAs<Expr>(); |
| 4369 | ImpCastExprToType(ToE, |
| 4370 | Context.getCVRQualifiedType(BaseType, |
| 4371 | CopyAssignOperator->getTypeQualifiers()), |
| 4372 | CastExpr::CK_UncheckedDerivedToBase, |
| 4373 | /*isLvalue=*/true, CXXBaseSpecifierArray(Base)); |
| 4374 | To = Owned(ToE); |
| 4375 | |
| 4376 | // Build the copy. |
| 4377 | OwningStmtResult Copy = BuildSingleCopyAssign(*this, Loc, BaseType, |
| 4378 | move(To), Owned(From)); |
| 4379 | if (Copy.isInvalid()) { |
| 4380 | Invalid = true; |
| 4381 | continue; |
| 4382 | } |
| 4383 | |
| 4384 | // Success! Record the copy. |
| 4385 | Statements.push_back(Copy.takeAs<Expr>()); |
| 4386 | } |
| 4387 | |
| 4388 | // \brief Reference to the __builtin_memcpy function. |
| 4389 | Expr *BuiltinMemCpyRef = 0; |
| 4390 | |
| 4391 | // Assign non-static members. |
| 4392 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4393 | FieldEnd = ClassDecl->field_end(); |
| 4394 | Field != FieldEnd; ++Field) { |
| 4395 | // Check for members of reference type; we can't copy those. |
| 4396 | if (Field->getType()->isReferenceType()) { |
| 4397 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 4398 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 4399 | Diag(Field->getLocation(), diag::note_declared_at); |
| 4400 | Diag(Loc, diag::note_first_required_here); |
| 4401 | Invalid = true; |
| 4402 | continue; |
| 4403 | } |
| 4404 | |
| 4405 | // Check for members of const-qualified, non-class type. |
| 4406 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 4407 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 4408 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 4409 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 4410 | Diag(Field->getLocation(), diag::note_declared_at); |
| 4411 | Diag(Loc, diag::note_first_required_here); |
| 4412 | Invalid = true; |
| 4413 | continue; |
| 4414 | } |
| 4415 | |
| 4416 | QualType FieldType = Field->getType().getNonReferenceType(); |
| 4417 | |
| 4418 | // Build references to the field in the object we're copying from and to. |
| 4419 | CXXScopeSpec SS; // Intentionally empty |
| 4420 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 4421 | LookupMemberName); |
| 4422 | MemberLookup.addDecl(*Field); |
| 4423 | MemberLookup.resolveKind(); |
| 4424 | OwningExprResult From = BuildMemberReferenceExpr(Owned(OtherRef->Retain()), |
| 4425 | OtherRefType, |
| 4426 | Loc, /*IsArrow=*/false, |
| 4427 | SS, 0, MemberLookup, 0); |
| 4428 | OwningExprResult To = BuildMemberReferenceExpr(Owned(This->Retain()), |
| 4429 | This->getType(), |
| 4430 | Loc, /*IsArrow=*/true, |
| 4431 | SS, 0, MemberLookup, 0); |
| 4432 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 4433 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 4434 | |
| 4435 | // If the field should be copied with __builtin_memcpy rather than via |
| 4436 | // explicit assignments, do so. This optimization only applies for arrays |
| 4437 | // of scalars and arrays of class type with trivial copy-assignment |
| 4438 | // operators. |
| 4439 | if (FieldType->isArrayType() && |
| 4440 | (!BaseType->isRecordType() || |
| 4441 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()) |
| 4442 | ->hasTrivialCopyAssignment())) { |
| 4443 | // Compute the size of the memory buffer to be copied. |
| 4444 | QualType SizeType = Context.getSizeType(); |
| 4445 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 4446 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 4447 | for (const ConstantArrayType *Array |
| 4448 | = Context.getAsConstantArrayType(FieldType); |
| 4449 | Array; |
| 4450 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
| 4451 | llvm::APInt ArraySize = Array->getSize(); |
| 4452 | ArraySize.zextOrTrunc(Size.getBitWidth()); |
| 4453 | Size *= ArraySize; |
| 4454 | } |
| 4455 | |
| 4456 | // Take the address of the field references for "from" and "to". |
| 4457 | From = CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(From)); |
| 4458 | To = CreateBuiltinUnaryOp(Loc, UnaryOperator::AddrOf, move(To)); |
| 4459 | |
| 4460 | // Create a reference to the __builtin_memcpy builtin function. |
| 4461 | if (!BuiltinMemCpyRef) { |
| 4462 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 4463 | LookupOrdinaryName); |
| 4464 | LookupName(R, TUScope, true); |
| 4465 | |
| 4466 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 4467 | if (!BuiltinMemCpy) { |
| 4468 | // Something went horribly wrong earlier, and we will have complained |
| 4469 | // about it. |
| 4470 | Invalid = true; |
| 4471 | continue; |
| 4472 | } |
| 4473 | |
| 4474 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 4475 | BuiltinMemCpy->getType(), |
| 4476 | Loc, 0).takeAs<Expr>(); |
| 4477 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 4478 | } |
| 4479 | |
| 4480 | ASTOwningVector<&ActionBase::DeleteExpr> CallArgs(*this); |
| 4481 | CallArgs.push_back(To.takeAs<Expr>()); |
| 4482 | CallArgs.push_back(From.takeAs<Expr>()); |
| 4483 | CallArgs.push_back(new (Context) IntegerLiteral(Size, SizeType, Loc)); |
| 4484 | llvm::SmallVector<SourceLocation, 4> Commas; // FIXME: Silly |
| 4485 | Commas.push_back(Loc); |
| 4486 | Commas.push_back(Loc); |
| 4487 | OwningExprResult Call = ActOnCallExpr(/*Scope=*/0, |
| 4488 | Owned(BuiltinMemCpyRef->Retain()), |
| 4489 | Loc, move_arg(CallArgs), |
| 4490 | Commas.data(), Loc); |
| 4491 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 4492 | Statements.push_back(Call.takeAs<Expr>()); |
| 4493 | continue; |
| 4494 | } |
| 4495 | |
| 4496 | // Build the copy of this field. |
| 4497 | OwningStmtResult Copy = BuildSingleCopyAssign(*this, Loc, FieldType, |
| 4498 | move(To), move(From)); |
| 4499 | if (Copy.isInvalid()) { |
| 4500 | Invalid = true; |
| 4501 | continue; |
| 4502 | } |
| 4503 | |
| 4504 | // Success! Record the copy. |
| 4505 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 4506 | } |
| 4507 | |
| 4508 | if (!Invalid) { |
| 4509 | // Add a "return *this;" |
| 4510 | OwningExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UnaryOperator::Deref, |
| 4511 | Owned(This->Retain())); |
| 4512 | |
| 4513 | OwningStmtResult Return = ActOnReturnStmt(Loc, move(ThisObj)); |
| 4514 | if (Return.isInvalid()) |
| 4515 | Invalid = true; |
| 4516 | else { |
| 4517 | Statements.push_back(Return.takeAs<Stmt>()); |
| 4518 | } |
| 4519 | } |
| 4520 | |
| 4521 | if (Invalid) { |
| 4522 | CopyAssignOperator->setInvalidDecl(); |
| 4523 | return; |
| 4524 | } |
| 4525 | |
| 4526 | OwningStmtResult Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
| 4527 | /*isStmtExpr=*/false); |
| 4528 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 4529 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4532 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 4533 | CXXConstructorDecl *CopyConstructor, |
| 4534 | unsigned TypeQuals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4535 | assert((CopyConstructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 4536 | CopyConstructor->isCopyConstructor(TypeQuals) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4537 | !CopyConstructor->isUsed()) && |
| 4538 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4539 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 4540 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4541 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4542 | |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4543 | ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4544 | |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 4545 | if (SetBaseOrMemberInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false)) { |
| 4546 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4547 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
| 4548 | CopyConstructor->setInvalidDecl(); |
| 4549 | } else { |
| 4550 | CopyConstructor->setUsed(); |
Anders Carlsson | 8e142cc | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 4551 | } |
Anders Carlsson | 59b7f15 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 4552 | |
| 4553 | // FIXME: Once SetBaseOrMemberInitializers can handle copy initialization of |
| 4554 | // fields, this code below should be removed. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4555 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4556 | FieldEnd = ClassDecl->field_end(); |
| 4557 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4558 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 4559 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 4560 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4561 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4562 | CXXRecordDecl *FieldClassDecl |
| 4563 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4564 | if (CXXConstructorDecl *FieldCopyCtor = |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4565 | FieldClassDecl->getCopyConstructor(Context, TypeQuals)) { |
| 4566 | CheckDirectMemberAccess(Field->getLocation(), |
| 4567 | FieldCopyCtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4568 | PDiag(diag::err_access_copy_field) |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4569 | << Field->getDeclName() << Field->getType()); |
| 4570 | |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 4571 | MarkDeclarationReferenced(CurrentLocation, FieldCopyCtor); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4572 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4573 | } |
| 4574 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 4577 | Sema::OwningExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 4578 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4579 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4580 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4581 | bool RequiresZeroInit, |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame^] | 4582 | CXXConstructExpr::ConstructionKind ConstructKind) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 4583 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4584 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4585 | // C++0x [class.copy]p34: |
| 4586 | // When certain criteria are met, an implementation is allowed to |
| 4587 | // omit the copy/move construction of a class object, even if the |
| 4588 | // copy/move constructor and/or destructor for the object have |
| 4589 | // side effects. [...] |
| 4590 | // - when a temporary class object that has not been bound to a |
| 4591 | // reference (12.2) would be copied/moved to a class object |
| 4592 | // with the same cv-unqualified type, the copy/move operation |
| 4593 | // can be omitted by constructing the temporary object |
| 4594 | // directly into the target of the omitted copy/move |
| 4595 | if (Constructor->isCopyConstructor() && ExprArgs.size() >= 1) { |
| 4596 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
| 4597 | Elidable = SubExpr->isTemporaryObject() && |
| 4598 | Context.hasSameUnqualifiedType(SubExpr->getType(), |
| 4599 | Context.getTypeDeclType(Constructor->getParent())); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 4600 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4601 | |
| 4602 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4603 | Elidable, move(ExprArgs), RequiresZeroInit, |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame^] | 4604 | ConstructKind); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 4607 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 4608 | /// including handling of its default argument expressions. |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 4609 | Sema::OwningExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 4610 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 4611 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4612 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4613 | bool RequiresZeroInit, |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame^] | 4614 | CXXConstructExpr::ConstructionKind ConstructKind) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 4615 | unsigned NumExprs = ExprArgs.size(); |
| 4616 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4617 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4618 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4619 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4620 | Constructor, Elidable, Exprs, NumExprs, |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame^] | 4621 | RequiresZeroInit, ConstructKind)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 4622 | } |
| 4623 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4624 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 4625 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 4626 | MultiExprArg Exprs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4627 | OwningExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 4628 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 4629 | move(Exprs)); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 4630 | if (TempResult.isInvalid()) |
| 4631 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4632 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 4633 | Expr *Temp = TempResult.takeAs<Expr>(); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 4634 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 4635 | Temp = MaybeCreateCXXExprWithTemporaries(Temp); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4636 | VD->setInit(Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4637 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 4638 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 4641 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
| 4642 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Douglas Gregor | 333de06 | 2010-02-25 18:11:54 +0000 | [diff] [blame] | 4643 | if (!ClassDecl->isInvalidDecl() && !VD->isInvalidDecl() && |
| 4644 | !ClassDecl->hasTrivialDestructor()) { |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 4645 | CXXDestructorDecl *Destructor = ClassDecl->getDestructor(Context); |
| 4646 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4647 | CheckDestructorAccess(VD->getLocation(), Destructor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4648 | PDiag(diag::err_access_dtor_var) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4649 | << VD->getDeclName() |
| 4650 | << VD->getType()); |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 4651 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4654 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4655 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 4656 | /// e.g: "int x(1);" |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4657 | void Sema::AddCXXDirectInitializerToDecl(DeclPtrTy Dcl, |
| 4658 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4659 | MultiExprArg Exprs, |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4660 | SourceLocation *CommaLocs, |
| 4661 | SourceLocation RParenLoc) { |
Daniel Dunbar | 5184626 | 2009-12-24 19:19:26 +0000 | [diff] [blame] | 4662 | assert(Exprs.size() != 0 && Exprs.get() && "missing expressions"); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4663 | Decl *RealDecl = Dcl.getAs<Decl>(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4664 | |
| 4665 | // If there is no declaration, there was an error parsing it. Just ignore |
| 4666 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4667 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4668 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4670 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 4671 | if (!VDecl) { |
| 4672 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 4673 | RealDecl->setInvalidDecl(); |
| 4674 | return; |
| 4675 | } |
| 4676 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 4677 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 4678 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4679 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 4680 | // |
| 4681 | // Clients that want to distinguish between the two forms, can check for |
| 4682 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 4683 | // A major benefit is that clients that don't particularly care about which |
| 4684 | // exactly form was it (like the CodeGen) can handle both cases without |
| 4685 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 4686 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4687 | // C++ 8.5p11: |
| 4688 | // The form of initialization (using parentheses or '=') is generally |
| 4689 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 4690 | // class type. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4691 | QualType DeclInitType = VDecl->getType(); |
| 4692 | if (const ArrayType *Array = Context.getAsArrayType(DeclInitType)) |
Fariborz Jahanian | 680a3f3 | 2009-10-28 19:04:36 +0000 | [diff] [blame] | 4693 | DeclInitType = Context.getBaseElementType(Array); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 4694 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 4695 | if (!VDecl->getType()->isDependentType() && |
| 4696 | RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 4697 | diag::err_typecheck_decl_incomplete_type)) { |
| 4698 | VDecl->setInvalidDecl(); |
| 4699 | return; |
| 4700 | } |
| 4701 | |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4702 | // The variable can not have an abstract class type. |
| 4703 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 4704 | diag::err_abstract_type_in_decl, |
| 4705 | AbstractVariableType)) |
| 4706 | VDecl->setInvalidDecl(); |
| 4707 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4708 | const VarDecl *Def; |
| 4709 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4710 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| 4711 | << VDecl->getDeclName(); |
| 4712 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4713 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 4714 | return; |
| 4715 | } |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 4716 | |
| 4717 | // If either the declaration has a dependent type or if any of the |
| 4718 | // expressions is type-dependent, we represent the initialization |
| 4719 | // via a ParenListExpr for later use during template instantiation. |
| 4720 | if (VDecl->getType()->isDependentType() || |
| 4721 | Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { |
| 4722 | // Let clients know that initialization was done with a direct initializer. |
| 4723 | VDecl->setCXXDirectInitializer(true); |
| 4724 | |
| 4725 | // Store the initialization expressions as a ParenListExpr. |
| 4726 | unsigned NumExprs = Exprs.size(); |
| 4727 | VDecl->setInit(new (Context) ParenListExpr(Context, LParenLoc, |
| 4728 | (Expr **)Exprs.release(), |
| 4729 | NumExprs, RParenLoc)); |
| 4730 | return; |
| 4731 | } |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4732 | |
| 4733 | // Capture the variable that is being initialized and the style of |
| 4734 | // initialization. |
| 4735 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 4736 | |
| 4737 | // FIXME: Poor source location information. |
| 4738 | InitializationKind Kind |
| 4739 | = InitializationKind::CreateDirect(VDecl->getLocation(), |
| 4740 | LParenLoc, RParenLoc); |
| 4741 | |
| 4742 | InitializationSequence InitSeq(*this, Entity, Kind, |
| 4743 | (Expr**)Exprs.get(), Exprs.size()); |
| 4744 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(Exprs)); |
| 4745 | if (Result.isInvalid()) { |
| 4746 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4747 | return; |
| 4748 | } |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4749 | |
| 4750 | Result = MaybeCreateCXXExprWithTemporaries(move(Result)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4751 | VDecl->setInit(Result.takeAs<Expr>()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4752 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 4753 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 4754 | if (const RecordType *Record = VDecl->getType()->getAs<RecordType>()) |
| 4755 | FinalizeVarWithDestructor(VDecl, Record); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4756 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4757 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4758 | /// \brief Given a constructor and the set of arguments provided for the |
| 4759 | /// constructor, convert the arguments and add any required default arguments |
| 4760 | /// to form a proper call to this constructor. |
| 4761 | /// |
| 4762 | /// \returns true if an error occurred, false otherwise. |
| 4763 | bool |
| 4764 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 4765 | MultiExprArg ArgsPtr, |
| 4766 | SourceLocation Loc, |
| 4767 | ASTOwningVector<&ActionBase::DeleteExpr> &ConvertedArgs) { |
| 4768 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 4769 | unsigned NumArgs = ArgsPtr.size(); |
| 4770 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 4771 | |
| 4772 | const FunctionProtoType *Proto |
| 4773 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 4774 | assert(Proto && "Constructor without a prototype?"); |
| 4775 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4776 | |
| 4777 | // 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] | 4778 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4779 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4780 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4781 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4782 | |
| 4783 | VariadicCallType CallType = |
| 4784 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 4785 | llvm::SmallVector<Expr *, 8> AllArgs; |
| 4786 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 4787 | Proto, 0, Args, NumArgs, AllArgs, |
| 4788 | CallType); |
| 4789 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 4790 | ConvertedArgs.push_back(AllArgs[i]); |
| 4791 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4794 | static inline bool |
| 4795 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 4796 | const FunctionDecl *FnDecl) { |
| 4797 | const DeclContext *DC = FnDecl->getDeclContext()->getLookupContext(); |
| 4798 | if (isa<NamespaceDecl>(DC)) { |
| 4799 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4800 | diag::err_operator_new_delete_declared_in_namespace) |
| 4801 | << FnDecl->getDeclName(); |
| 4802 | } |
| 4803 | |
| 4804 | if (isa<TranslationUnitDecl>(DC) && |
| 4805 | FnDecl->getStorageClass() == FunctionDecl::Static) { |
| 4806 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4807 | diag::err_operator_new_delete_declared_static) |
| 4808 | << FnDecl->getDeclName(); |
| 4809 | } |
| 4810 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 4811 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4812 | } |
| 4813 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4814 | static inline bool |
| 4815 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 4816 | CanQualType ExpectedResultType, |
| 4817 | CanQualType ExpectedFirstParamType, |
| 4818 | unsigned DependentParamTypeDiag, |
| 4819 | unsigned InvalidParamTypeDiag) { |
| 4820 | QualType ResultType = |
| 4821 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 4822 | |
| 4823 | // Check that the result type is not dependent. |
| 4824 | if (ResultType->isDependentType()) |
| 4825 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4826 | diag::err_operator_new_delete_dependent_result_type) |
| 4827 | << FnDecl->getDeclName() << ExpectedResultType; |
| 4828 | |
| 4829 | // Check that the result type is what we expect. |
| 4830 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 4831 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4832 | diag::err_operator_new_delete_invalid_result_type) |
| 4833 | << FnDecl->getDeclName() << ExpectedResultType; |
| 4834 | |
| 4835 | // A function template must have at least 2 parameters. |
| 4836 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 4837 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4838 | diag::err_operator_new_delete_template_too_few_parameters) |
| 4839 | << FnDecl->getDeclName(); |
| 4840 | |
| 4841 | // The function decl must have at least 1 parameter. |
| 4842 | if (FnDecl->getNumParams() == 0) |
| 4843 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4844 | diag::err_operator_new_delete_too_few_parameters) |
| 4845 | << FnDecl->getDeclName(); |
| 4846 | |
| 4847 | // Check the the first parameter type is not dependent. |
| 4848 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 4849 | if (FirstParamType->isDependentType()) |
| 4850 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 4851 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 4852 | |
| 4853 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 4854 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4855 | ExpectedFirstParamType) |
| 4856 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 4857 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 4858 | |
| 4859 | return false; |
| 4860 | } |
| 4861 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4862 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4863 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4864 | // C++ [basic.stc.dynamic.allocation]p1: |
| 4865 | // A program is ill-formed if an allocation function is declared in a |
| 4866 | // namespace scope other than global scope or declared static in global |
| 4867 | // scope. |
| 4868 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 4869 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4870 | |
| 4871 | CanQualType SizeTy = |
| 4872 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 4873 | |
| 4874 | // C++ [basic.stc.dynamic.allocation]p1: |
| 4875 | // The return type shall be void*. The first parameter shall have type |
| 4876 | // std::size_t. |
| 4877 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 4878 | SizeTy, |
| 4879 | diag::err_operator_new_dependent_param_type, |
| 4880 | diag::err_operator_new_param_type)) |
| 4881 | return true; |
| 4882 | |
| 4883 | // C++ [basic.stc.dynamic.allocation]p1: |
| 4884 | // The first parameter shall not have an associated default argument. |
| 4885 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4886 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4887 | diag::err_operator_new_default_arg) |
| 4888 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 4889 | |
| 4890 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | static bool |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4894 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 4895 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 4896 | // A program is ill-formed if deallocation functions are declared in a |
| 4897 | // namespace scope other than global scope or declared static in global |
| 4898 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4899 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 4900 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4901 | |
| 4902 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 4903 | // Each deallocation function shall return void and its first parameter |
| 4904 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4905 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 4906 | SemaRef.Context.VoidPtrTy, |
| 4907 | diag::err_operator_delete_dependent_param_type, |
| 4908 | diag::err_operator_delete_param_type)) |
| 4909 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4910 | |
Anders Carlsson | 46991d6 | 2009-12-12 00:16:02 +0000 | [diff] [blame] | 4911 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 4912 | if (FirstParamType->isDependentType()) |
| 4913 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4914 | diag::err_operator_delete_dependent_param_type) |
| 4915 | << FnDecl->getDeclName() << SemaRef.Context.VoidPtrTy; |
| 4916 | |
| 4917 | if (SemaRef.Context.getCanonicalType(FirstParamType) != |
| 4918 | SemaRef.Context.VoidPtrTy) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4919 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4920 | diag::err_operator_delete_param_type) |
| 4921 | << FnDecl->getDeclName() << SemaRef.Context.VoidPtrTy; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4922 | |
| 4923 | return false; |
| 4924 | } |
| 4925 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4926 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 4927 | /// of this overloaded operator is well-formed. If so, returns false; |
| 4928 | /// otherwise, emits appropriate diagnostics and returns true. |
| 4929 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4930 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4931 | "Expected an overloaded operator declaration"); |
| 4932 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4933 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 4934 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4935 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4936 | // The allocation and deallocation functions, operator new, |
| 4937 | // operator new[], operator delete and operator delete[], are |
| 4938 | // described completely in 3.7.3. The attributes and restrictions |
| 4939 | // found in the rest of this subclause do not apply to them unless |
| 4940 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 4941 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4942 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 4943 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4944 | if (Op == OO_New || Op == OO_Array_New) |
| 4945 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4946 | |
| 4947 | // C++ [over.oper]p6: |
| 4948 | // An operator function shall either be a non-static member |
| 4949 | // function or be a non-member function and have at least one |
| 4950 | // parameter whose type is a class, a reference to a class, an |
| 4951 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4952 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4953 | if (MethodDecl->isStatic()) |
| 4954 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4955 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4956 | } else { |
| 4957 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4958 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 4959 | ParamEnd = FnDecl->param_end(); |
| 4960 | Param != ParamEnd; ++Param) { |
| 4961 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 4962 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 4963 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4964 | ClassOrEnumParam = true; |
| 4965 | break; |
| 4966 | } |
| 4967 | } |
| 4968 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4969 | if (!ClassOrEnumParam) |
| 4970 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4971 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4972 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | // C++ [over.oper]p8: |
| 4976 | // An operator function cannot have default arguments (8.3.6), |
| 4977 | // except where explicitly stated below. |
| 4978 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4979 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4980 | // (C++ [over.call]p1). |
| 4981 | if (Op != OO_Call) { |
| 4982 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 4983 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4984 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4985 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4986 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4987 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4988 | } |
| 4989 | } |
| 4990 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4991 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 4992 | { false, false, false } |
| 4993 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 4994 | , { Unary, Binary, MemberOnly } |
| 4995 | #include "clang/Basic/OperatorKinds.def" |
| 4996 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4997 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4998 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 4999 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 5000 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5001 | |
| 5002 | // C++ [over.oper]p8: |
| 5003 | // [...] Operator functions cannot have more or fewer parameters |
| 5004 | // than the number required for the corresponding operator, as |
| 5005 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5006 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5007 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5008 | if (Op != OO_Call && |
| 5009 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 5010 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 5011 | (NumParams < 1) || (NumParams > 2))) { |
| 5012 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5013 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5014 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5015 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5016 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5017 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5018 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 5019 | assert(CanBeBinaryOperator && |
| 5020 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5021 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5022 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5023 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5024 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5025 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5026 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5027 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5028 | // Overloaded operators other than operator() cannot be variadic. |
| 5029 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5030 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5031 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5032 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5036 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 5037 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5038 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5039 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5040 | } |
| 5041 | |
| 5042 | // C++ [over.inc]p1: |
| 5043 | // The user-defined function called operator++ implements the |
| 5044 | // prefix and postfix ++ operator. If this function is a member |
| 5045 | // function with no parameters, or a non-member function with one |
| 5046 | // parameter of class or enumeration type, it defines the prefix |
| 5047 | // increment operator ++ for objects of that type. If the function |
| 5048 | // is a member function with one parameter (which shall be of type |
| 5049 | // int) or a non-member function with two parameters (the second |
| 5050 | // of which shall be of type int), it defines the postfix |
| 5051 | // increment operator ++ for objects of that type. |
| 5052 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 5053 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 5054 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5055 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5056 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 5057 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 5058 | if (!ParamIsInt) |
| 5059 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5060 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5061 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5062 | } |
| 5063 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5064 | // Notify the class if it got an assignment operator. |
| 5065 | if (Op == OO_Equal) { |
| 5066 | // Would have returned earlier otherwise. |
| 5067 | assert(isa<CXXMethodDecl>(FnDecl) && |
| 5068 | "Overloaded = not member, but not filtered."); |
| 5069 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 5070 | Method->getParent()->addedAssignmentOperator(Context, Method); |
| 5071 | } |
| 5072 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5073 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5074 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 5075 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5076 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 5077 | /// of this literal operator function is well-formed. If so, returns |
| 5078 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 5079 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 5080 | DeclContext *DC = FnDecl->getDeclContext(); |
| 5081 | Decl::Kind Kind = DC->getDeclKind(); |
| 5082 | if (Kind != Decl::TranslationUnit && Kind != Decl::Namespace && |
| 5083 | Kind != Decl::LinkageSpec) { |
| 5084 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 5085 | << FnDecl->getDeclName(); |
| 5086 | return true; |
| 5087 | } |
| 5088 | |
| 5089 | bool Valid = false; |
| 5090 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5091 | // template <char...> type operator "" name() is the only valid template |
| 5092 | // signature, and the only valid signature with no parameters. |
| 5093 | if (FnDecl->param_size() == 0) { |
| 5094 | if (FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate()) { |
| 5095 | // Must have only one template parameter |
| 5096 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 5097 | if (Params->size() == 1) { |
| 5098 | NonTypeTemplateParmDecl *PmDecl = |
| 5099 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5100 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5101 | // The template parameter must be a char parameter pack. |
| 5102 | // FIXME: This test will always fail because non-type parameter packs |
| 5103 | // have not been implemented. |
| 5104 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 5105 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 5106 | Valid = true; |
| 5107 | } |
| 5108 | } |
| 5109 | } else { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5110 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5111 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 5112 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5113 | QualType T = (*Param)->getType(); |
| 5114 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 5115 | // unsigned long long int, long double, and any character type are allowed |
| 5116 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5117 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 5118 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 5119 | Context.hasSameType(T, Context.CharTy) || |
| 5120 | Context.hasSameType(T, Context.WCharTy) || |
| 5121 | Context.hasSameType(T, Context.Char16Ty) || |
| 5122 | Context.hasSameType(T, Context.Char32Ty)) { |
| 5123 | if (++Param == FnDecl->param_end()) |
| 5124 | Valid = true; |
| 5125 | goto FinishedParams; |
| 5126 | } |
| 5127 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 5128 | // 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] | 5129 | const PointerType *PT = T->getAs<PointerType>(); |
| 5130 | if (!PT) |
| 5131 | goto FinishedParams; |
| 5132 | T = PT->getPointeeType(); |
| 5133 | if (!T.isConstQualified()) |
| 5134 | goto FinishedParams; |
| 5135 | T = T.getUnqualifiedType(); |
| 5136 | |
| 5137 | // Move on to the second parameter; |
| 5138 | ++Param; |
| 5139 | |
| 5140 | // If there is no second parameter, the first must be a const char * |
| 5141 | if (Param == FnDecl->param_end()) { |
| 5142 | if (Context.hasSameType(T, Context.CharTy)) |
| 5143 | Valid = true; |
| 5144 | goto FinishedParams; |
| 5145 | } |
| 5146 | |
| 5147 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 5148 | // are allowed as the first parameter to a two-parameter function |
| 5149 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 5150 | Context.hasSameType(T, Context.WCharTy) || |
| 5151 | Context.hasSameType(T, Context.Char16Ty) || |
| 5152 | Context.hasSameType(T, Context.Char32Ty))) |
| 5153 | goto FinishedParams; |
| 5154 | |
| 5155 | // The second and final parameter must be an std::size_t |
| 5156 | T = (*Param)->getType().getUnqualifiedType(); |
| 5157 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 5158 | ++Param == FnDecl->param_end()) |
| 5159 | Valid = true; |
| 5160 | } |
| 5161 | |
| 5162 | // FIXME: This diagnostic is absolutely terrible. |
| 5163 | FinishedParams: |
| 5164 | if (!Valid) { |
| 5165 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 5166 | << FnDecl->getDeclName(); |
| 5167 | return true; |
| 5168 | } |
| 5169 | |
| 5170 | return false; |
| 5171 | } |
| 5172 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5173 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 5174 | /// linkage specification, including the language and (if present) |
| 5175 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 5176 | /// the location of the language string literal, which is provided |
| 5177 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 5178 | /// the '{' brace. Otherwise, this linkage specification does not |
| 5179 | /// have any braces. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5180 | Sema::DeclPtrTy Sema::ActOnStartLinkageSpecification(Scope *S, |
| 5181 | SourceLocation ExternLoc, |
| 5182 | SourceLocation LangLoc, |
| 5183 | const char *Lang, |
| 5184 | unsigned StrSize, |
| 5185 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5186 | LinkageSpecDecl::LanguageIDs Language; |
| 5187 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 5188 | Language = LinkageSpecDecl::lang_c; |
| 5189 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 5190 | Language = LinkageSpecDecl::lang_cxx; |
| 5191 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5192 | Diag(LangLoc, diag::err_bad_language); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5193 | return DeclPtrTy(); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5194 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5195 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5196 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5197 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5198 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5199 | LangLoc, Language, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5200 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5201 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5202 | PushDeclContext(S, D); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5203 | return DeclPtrTy::make(D); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5204 | } |
| 5205 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5206 | /// ActOnFinishLinkageSpecification - Completely the definition of |
| 5207 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 5208 | /// valid, it's the position of the closing '}' brace in a linkage |
| 5209 | /// specification that uses braces. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5210 | Sema::DeclPtrTy Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 5211 | DeclPtrTy LinkageSpec, |
| 5212 | SourceLocation RBraceLoc) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5213 | if (LinkageSpec) |
| 5214 | PopDeclContext(); |
| 5215 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 5216 | } |
| 5217 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5218 | /// \brief Perform semantic analysis for the variable declaration that |
| 5219 | /// occurs within a C++ catch clause, returning the newly-created |
| 5220 | /// variable. |
| 5221 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, QualType ExDeclType, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5222 | TypeSourceInfo *TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5223 | IdentifierInfo *Name, |
| 5224 | SourceLocation Loc, |
| 5225 | SourceRange Range) { |
| 5226 | bool Invalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5227 | |
| 5228 | // Arrays and functions decay. |
| 5229 | if (ExDeclType->isArrayType()) |
| 5230 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 5231 | else if (ExDeclType->isFunctionType()) |
| 5232 | ExDeclType = Context.getPointerType(ExDeclType); |
| 5233 | |
| 5234 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 5235 | // The exception-declaration shall not denote a pointer or reference to an |
| 5236 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 5237 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5238 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5239 | Diag(Loc, diag::err_catch_rvalue_ref) << Range; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 5240 | Invalid = true; |
| 5241 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5242 | |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 5243 | // GCC allows catching pointers and references to incomplete types |
| 5244 | // as an extension; so do we, but we warn by default. |
| 5245 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5246 | QualType BaseType = ExDeclType; |
| 5247 | 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] | 5248 | unsigned DK = diag::err_catch_incomplete; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 5249 | bool IncompleteCatchIsInvalid = true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5250 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5251 | BaseType = Ptr->getPointeeType(); |
| 5252 | Mode = 1; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 5253 | DK = diag::ext_catch_incomplete_ptr; |
| 5254 | IncompleteCatchIsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5255 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 5256 | // 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] | 5257 | BaseType = Ref->getPointeeType(); |
| 5258 | Mode = 2; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 5259 | DK = diag::ext_catch_incomplete_ref; |
| 5260 | IncompleteCatchIsInvalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5261 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 5262 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 5263 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK) && |
| 5264 | IncompleteCatchIsInvalid) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5265 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5266 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5267 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5268 | RequireNonAbstractType(Loc, ExDeclType, |
| 5269 | diag::err_abstract_type_in_decl, |
| 5270 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 5271 | Invalid = true; |
| 5272 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5273 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 5274 | Name, ExDeclType, TInfo, VarDecl::None, |
| 5275 | VarDecl::None); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5276 | |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 5277 | if (!Invalid) { |
| 5278 | if (const RecordType *RecordTy = ExDeclType->getAs<RecordType>()) { |
| 5279 | // C++ [except.handle]p16: |
| 5280 | // The object declared in an exception-declaration or, if the |
| 5281 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 5282 | // copy-initialized (8.5) from the exception object. [...] |
| 5283 | // The object is destroyed when the handler exits, after the destruction |
| 5284 | // of any automatic objects initialized within the handler. |
| 5285 | // |
| 5286 | // We just pretend to initialize the object with itself, then make sure |
| 5287 | // it can be destroyed later. |
| 5288 | InitializedEntity Entity = InitializedEntity::InitializeVariable(ExDecl); |
| 5289 | Expr *ExDeclRef = DeclRefExpr::Create(Context, 0, SourceRange(), ExDecl, |
| 5290 | Loc, ExDeclType, 0); |
| 5291 | InitializationKind Kind = InitializationKind::CreateCopy(Loc, |
| 5292 | SourceLocation()); |
| 5293 | InitializationSequence InitSeq(*this, Entity, Kind, &ExDeclRef, 1); |
| 5294 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
| 5295 | MultiExprArg(*this, (void**)&ExDeclRef, 1)); |
| 5296 | if (Result.isInvalid()) |
| 5297 | Invalid = true; |
| 5298 | else |
| 5299 | FinalizeVarWithDestructor(ExDecl, RecordTy); |
| 5300 | } |
| 5301 | } |
| 5302 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5303 | if (Invalid) |
| 5304 | ExDecl->setInvalidDecl(); |
| 5305 | |
| 5306 | return ExDecl; |
| 5307 | } |
| 5308 | |
| 5309 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 5310 | /// handler. |
| 5311 | Sema::DeclPtrTy Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5312 | TypeSourceInfo *TInfo = 0; |
| 5313 | QualType ExDeclType = GetTypeForDeclarator(D, S, &TInfo); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5314 | |
| 5315 | bool Invalid = D.isInvalidType(); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5316 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 5317 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 5318 | LookupOrdinaryName, |
| 5319 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5320 | // The scope should be freshly made just for us. There is just no way |
| 5321 | // it contains any previous declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5322 | assert(!S->isDeclScope(DeclPtrTy::make(PrevDecl))); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5323 | if (PrevDecl->isTemplateParameter()) { |
| 5324 | // Maybe we will complain about the shadowed template parameter. |
| 5325 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5326 | } |
| 5327 | } |
| 5328 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5329 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5330 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 5331 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5332 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5333 | } |
| 5334 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5335 | VarDecl *ExDecl = BuildExceptionDeclaration(S, ExDeclType, TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5336 | D.getIdentifier(), |
| 5337 | D.getIdentifierLoc(), |
| 5338 | D.getDeclSpec().getSourceRange()); |
| 5339 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5340 | if (Invalid) |
| 5341 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5342 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5343 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5344 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5345 | PushOnScopeChains(ExDecl, S); |
| 5346 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5347 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5348 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5349 | ProcessDeclAttributes(S, ExDecl, D); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5350 | return DeclPtrTy::make(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5351 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5352 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5353 | Sema::DeclPtrTy Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5354 | ExprArg assertexpr, |
| 5355 | ExprArg assertmessageexpr) { |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5356 | Expr *AssertExpr = (Expr *)assertexpr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5357 | StringLiteral *AssertMessage = |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5358 | cast<StringLiteral>((Expr *)assertmessageexpr.get()); |
| 5359 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5360 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 5361 | llvm::APSInt Value(32); |
| 5362 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 5363 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 5364 | AssertExpr->getSourceRange(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5365 | return DeclPtrTy(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5366 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5367 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5368 | if (Value == 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5369 | Diag(AssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 5370 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5371 | } |
| 5372 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5373 | |
Anders Carlsson | 77d8142 | 2009-03-15 17:35:16 +0000 | [diff] [blame] | 5374 | assertexpr.release(); |
| 5375 | assertmessageexpr.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5376 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5377 | AssertExpr, AssertMessage); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5378 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5379 | CurContext->addDecl(Decl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5380 | return DeclPtrTy::make(Decl); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5381 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5382 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5383 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 5384 | /// |
| 5385 | /// \returns A friend declaration that. |
| 5386 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation FriendLoc, |
| 5387 | TypeSourceInfo *TSInfo) { |
| 5388 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 5389 | |
| 5390 | QualType T = TSInfo->getType(); |
| 5391 | SourceRange TypeRange = TSInfo->getTypeLoc().getSourceRange(); |
| 5392 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 5393 | if (!getLangOptions().CPlusPlus0x) { |
| 5394 | // C++03 [class.friend]p2: |
| 5395 | // An elaborated-type-specifier shall be used in a friend declaration |
| 5396 | // for a class.* |
| 5397 | // |
| 5398 | // * The class-key of the elaborated-type-specifier is required. |
| 5399 | if (!ActiveTemplateInstantiations.empty()) { |
| 5400 | // Do not complain about the form of friend template types during |
| 5401 | // template instantiation; we will already have complained when the |
| 5402 | // template was declared. |
| 5403 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 5404 | // If we evaluated the type to a record type, suggest putting |
| 5405 | // a tag in front. |
| 5406 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 5407 | RecordDecl *RD = RT->getDecl(); |
| 5408 | |
| 5409 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 5410 | |
| 5411 | Diag(TypeRange.getBegin(), diag::ext_unelaborated_friend_type) |
| 5412 | << (unsigned) RD->getTagKind() |
| 5413 | << T |
| 5414 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 5415 | InsertionText); |
| 5416 | } else { |
| 5417 | Diag(FriendLoc, diag::ext_nonclass_type_friend) |
| 5418 | << T |
| 5419 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
| 5420 | } |
| 5421 | } else if (T->getAs<EnumType>()) { |
| 5422 | Diag(FriendLoc, diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5423 | << T |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5424 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5425 | } |
| 5426 | } |
| 5427 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 5428 | // C++0x [class.friend]p3: |
| 5429 | // If the type specifier in a friend declaration designates a (possibly |
| 5430 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 5431 | // the friend declaration is ignored. |
| 5432 | |
| 5433 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 5434 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5435 | |
| 5436 | return FriendDecl::Create(Context, CurContext, FriendLoc, TSInfo, FriendLoc); |
| 5437 | } |
| 5438 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5439 | /// Handle a friend type declaration. This works in tandem with |
| 5440 | /// ActOnTag. |
| 5441 | /// |
| 5442 | /// Notes on friend class templates: |
| 5443 | /// |
| 5444 | /// We generally treat friend class declarations as if they were |
| 5445 | /// declaring a class. So, for example, the elaborated type specifier |
| 5446 | /// in a friend declaration is required to obey the restrictions of a |
| 5447 | /// class-head (i.e. no typedefs in the scope chain), template |
| 5448 | /// parameters are required to match up with simple template-ids, &c. |
| 5449 | /// However, unlike when declaring a template specialization, it's |
| 5450 | /// okay to refer to a template specialization without an empty |
| 5451 | /// template parameter declaration, e.g. |
| 5452 | /// friend class A<T>::B<unsigned>; |
| 5453 | /// We permit this as a special case; if there are any template |
| 5454 | /// parameters present at all, require proper matching, i.e. |
| 5455 | /// template <> template <class T> friend class A<int>::B; |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 5456 | Sema::DeclPtrTy Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5457 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5458 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5459 | |
| 5460 | assert(DS.isFriendSpecified()); |
| 5461 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 5462 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5463 | // Try to convert the decl specifier to a type. This works for |
| 5464 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 5465 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 5466 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5467 | TypeSourceInfo *TSI; |
| 5468 | QualType T = GetTypeForDeclarator(TheDeclarator, S, &TSI); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 5469 | if (TheDeclarator.isInvalidType()) |
| 5470 | return DeclPtrTy(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5471 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5472 | if (!TSI) |
| 5473 | TSI = Context.getTrivialTypeSourceInfo(T, DS.getSourceRange().getBegin()); |
| 5474 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5475 | // This is definitely an error in C++98. It's probably meant to |
| 5476 | // be forbidden in C++0x, too, but the specification is just |
| 5477 | // poorly written. |
| 5478 | // |
| 5479 | // The problem is with declarations like the following: |
| 5480 | // template <T> friend A<T>::foo; |
| 5481 | // where deciding whether a class C is a friend or not now hinges |
| 5482 | // on whether there exists an instantiation of A that causes |
| 5483 | // 'foo' to equal C. There are restrictions on class-heads |
| 5484 | // (which we declare (by fiat) elaborated friend declarations to |
| 5485 | // be) that makes this tractable. |
| 5486 | // |
| 5487 | // FIXME: handle "template <> friend class A<T>;", which |
| 5488 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 5489 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5490 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 5491 | << DS.getSourceRange(); |
| 5492 | return DeclPtrTy(); |
| 5493 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5494 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5495 | // C++98 [class.friend]p1: A friend of a class is a function |
| 5496 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 5497 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 5498 | // deadline. It's also a very silly restriction that seriously |
| 5499 | // affects inner classes and which nobody else seems to implement; |
| 5500 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5501 | // |
| 5502 | // But note that we could warn about it: it's always useless to |
| 5503 | // friend one of your own members (it's not, however, worthless to |
| 5504 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5505 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5506 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5507 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5508 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5509 | NumTempParamLists, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5510 | (TemplateParameterList**) TempParams.release(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5511 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5512 | DS.getFriendSpecLoc()); |
| 5513 | else |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5514 | D = CheckFriendTypeDecl(DS.getFriendSpecLoc(), TSI); |
| 5515 | |
| 5516 | if (!D) |
| 5517 | return DeclPtrTy(); |
| 5518 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5519 | D->setAccess(AS_public); |
| 5520 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5521 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5522 | return DeclPtrTy::make(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5523 | } |
| 5524 | |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 5525 | Sema::DeclPtrTy |
| 5526 | Sema::ActOnFriendFunctionDecl(Scope *S, |
| 5527 | Declarator &D, |
| 5528 | bool IsDefinition, |
| 5529 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5530 | const DeclSpec &DS = D.getDeclSpec(); |
| 5531 | |
| 5532 | assert(DS.isFriendSpecified()); |
| 5533 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 5534 | |
| 5535 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5536 | TypeSourceInfo *TInfo = 0; |
| 5537 | QualType T = GetTypeForDeclarator(D, S, &TInfo); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5538 | |
| 5539 | // C++ [class.friend]p1 |
| 5540 | // A friend of a class is a function or class.... |
| 5541 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5542 | // It *doesn't* see through dependent types, which is correct |
| 5543 | // according to [temp.arg.type]p3: |
| 5544 | // If a declaration acquires a function type through a |
| 5545 | // type dependent on a template-parameter and this causes |
| 5546 | // a declaration that does not use the syntactic form of a |
| 5547 | // function declarator to have a function type, the program |
| 5548 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5549 | if (!T->isFunctionType()) { |
| 5550 | Diag(Loc, diag::err_unexpected_friend); |
| 5551 | |
| 5552 | // It might be worthwhile to try to recover by creating an |
| 5553 | // appropriate declaration. |
| 5554 | return DeclPtrTy(); |
| 5555 | } |
| 5556 | |
| 5557 | // C++ [namespace.memdef]p3 |
| 5558 | // - If a friend declaration in a non-local class first declares a |
| 5559 | // class or function, the friend class or function is a member |
| 5560 | // of the innermost enclosing namespace. |
| 5561 | // - The name of the friend is not found by simple name lookup |
| 5562 | // until a matching declaration is provided in that namespace |
| 5563 | // scope (either before or after the class declaration granting |
| 5564 | // friendship). |
| 5565 | // - If a friend function is called, its name may be found by the |
| 5566 | // name lookup that considers functions from namespaces and |
| 5567 | // classes associated with the types of the function arguments. |
| 5568 | // - When looking for a prior declaration of a class or a function |
| 5569 | // declared as a friend, scopes outside the innermost enclosing |
| 5570 | // namespace scope are not considered. |
| 5571 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5572 | CXXScopeSpec &ScopeQual = D.getCXXScopeSpec(); |
| 5573 | DeclarationName Name = GetNameForDeclarator(D); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5574 | assert(Name); |
| 5575 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5576 | // The context we found the declaration in, or in which we should |
| 5577 | // create the declaration. |
| 5578 | DeclContext *DC; |
| 5579 | |
| 5580 | // FIXME: handle local classes |
| 5581 | |
| 5582 | // Recover from invalid scope qualifiers as if they just weren't there. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5583 | LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName, |
| 5584 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5585 | if (!ScopeQual.isInvalid() && ScopeQual.isSet()) { |
| 5586 | DC = computeDeclContext(ScopeQual); |
| 5587 | |
| 5588 | // FIXME: handle dependent contexts |
| 5589 | if (!DC) return DeclPtrTy(); |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5590 | if (RequireCompleteDeclContext(ScopeQual, DC)) return DeclPtrTy(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5591 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5592 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5593 | |
| 5594 | // If searching in that context implicitly found a declaration in |
| 5595 | // a different context, treat it like it wasn't found at all. |
| 5596 | // TODO: better diagnostics for this case. Suggesting the right |
| 5597 | // qualified scope would be nice... |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5598 | // FIXME: getRepresentativeDecl() is not right here at all |
| 5599 | if (Previous.empty() || |
| 5600 | !Previous.getRepresentativeDecl()->getDeclContext()->Equals(DC)) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5601 | D.setInvalidType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5602 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 5603 | return DeclPtrTy(); |
| 5604 | } |
| 5605 | |
| 5606 | // C++ [class.friend]p1: A friend of a class is a function or |
| 5607 | // class that is not a member of the class . . . |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5608 | if (DC->Equals(CurContext)) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5609 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 5610 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5611 | // Otherwise walk out to the nearest namespace scope looking for matches. |
| 5612 | } else { |
| 5613 | // TODO: handle local class contexts. |
| 5614 | |
| 5615 | DC = CurContext; |
| 5616 | while (true) { |
| 5617 | // Skip class contexts. If someone can cite chapter and verse |
| 5618 | // for this behavior, that would be nice --- it's what GCC and |
| 5619 | // EDG do, and it seems like a reasonable intent, but the spec |
| 5620 | // really only says that checks for unqualified existing |
| 5621 | // declarations should stop at the nearest enclosing namespace, |
| 5622 | // not that they should only consider the nearest enclosing |
| 5623 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5624 | while (DC->isRecord()) |
| 5625 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5626 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5627 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5628 | |
| 5629 | // TODO: decide what we think about using declarations. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5630 | if (!Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5631 | break; |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5632 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5633 | if (DC->isFileContext()) break; |
| 5634 | DC = DC->getParent(); |
| 5635 | } |
| 5636 | |
| 5637 | // C++ [class.friend]p1: A friend of a class is a function or |
| 5638 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 5639 | // C++0x changes this for both friend types and functions. |
| 5640 | // Most C++ 98 compilers do seem to give an error here, so |
| 5641 | // we do, too. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5642 | if (!Previous.empty() && DC->Equals(CurContext) |
| 5643 | && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5644 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 5645 | } |
| 5646 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5647 | if (DC->isFileContext()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5648 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5649 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 5650 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 5651 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5652 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5653 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 5654 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5655 | return DeclPtrTy(); |
| 5656 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5659 | bool Redeclaration = false; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5660 | NamedDecl *ND = ActOnFunctionDeclarator(S, D, DC, T, TInfo, Previous, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5661 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 5662 | IsDefinition, |
| 5663 | Redeclaration); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5664 | if (!ND) return DeclPtrTy(); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5665 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5666 | assert(ND->getDeclContext() == DC); |
| 5667 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 5668 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5669 | // Add the function declaration to the appropriate lookup tables, |
| 5670 | // adjusting the redeclarations list as necessary. We don't |
| 5671 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5672 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5673 | // Also update the scope-based lookup if the target context's |
| 5674 | // lookup context is in lexical scope. |
| 5675 | if (!CurContext->isDependentContext()) { |
| 5676 | DC = DC->getLookupContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5677 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5678 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5679 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5680 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5681 | |
| 5682 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5683 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5684 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 5685 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5686 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5687 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5688 | return DeclPtrTy::make(ND); |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 5689 | } |
| 5690 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5691 | void Sema::SetDeclDeleted(DeclPtrTy dcl, SourceLocation DelLoc) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 5692 | AdjustDeclIfTemplate(dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5693 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5694 | Decl *Dcl = dcl.getAs<Decl>(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5695 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 5696 | if (!Fn) { |
| 5697 | Diag(DelLoc, diag::err_deleted_non_function); |
| 5698 | return; |
| 5699 | } |
| 5700 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 5701 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 5702 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 5703 | // If the declaration wasn't the first, we delete the function anyway for |
| 5704 | // recovery. |
| 5705 | } |
| 5706 | Fn->setDeleted(); |
| 5707 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 5708 | |
| 5709 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 5710 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 5711 | ++CI) { |
| 5712 | Stmt *SubStmt = *CI; |
| 5713 | if (!SubStmt) |
| 5714 | continue; |
| 5715 | if (isa<ReturnStmt>(SubStmt)) |
| 5716 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 5717 | diag::err_return_in_constructor_handler); |
| 5718 | if (!isa<Expr>(SubStmt)) |
| 5719 | SearchForReturnInStmt(Self, SubStmt); |
| 5720 | } |
| 5721 | } |
| 5722 | |
| 5723 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 5724 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 5725 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 5726 | SearchForReturnInStmt(*this, Handler); |
| 5727 | } |
| 5728 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5729 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5730 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5731 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5732 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 5733 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5734 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 5735 | if (Context.hasSameType(NewTy, OldTy) || |
| 5736 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5737 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5738 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5739 | // Check if the return types are covariant |
| 5740 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5741 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5742 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 5743 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 5744 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5745 | NewClassTy = NewPT->getPointeeType(); |
| 5746 | OldClassTy = OldPT->getPointeeType(); |
| 5747 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 5748 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 5749 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 5750 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 5751 | NewClassTy = NewRT->getPointeeType(); |
| 5752 | OldClassTy = OldRT->getPointeeType(); |
| 5753 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5754 | } |
| 5755 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5756 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5757 | // The return types aren't either both pointers or references to a class type. |
| 5758 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5759 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5760 | diag::err_different_return_type_for_overriding_virtual_function) |
| 5761 | << New->getDeclName() << NewTy << OldTy; |
| 5762 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5763 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5764 | return true; |
| 5765 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5766 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 5767 | // C++ [class.virtual]p6: |
| 5768 | // If the return type of D::f differs from the return type of B::f, the |
| 5769 | // class type in the return type of D::f shall be complete at the point of |
| 5770 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 5771 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 5772 | if (!RT->isBeingDefined() && |
| 5773 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 5774 | PDiag(diag::err_covariant_return_incomplete) |
| 5775 | << New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 5776 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 5777 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 5778 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5779 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5780 | // Check if the new class derives from the old class. |
| 5781 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 5782 | Diag(New->getLocation(), |
| 5783 | diag::err_covariant_return_not_derived) |
| 5784 | << New->getDeclName() << NewTy << OldTy; |
| 5785 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5786 | return true; |
| 5787 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5788 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5789 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5790 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 5791 | diag::err_covariant_return_inaccessible_base, |
| 5792 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 5793 | // FIXME: Should this point to the return type? |
| 5794 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5795 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5796 | return true; |
| 5797 | } |
| 5798 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5799 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5800 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 5801 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5802 | Diag(New->getLocation(), |
| 5803 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5804 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5805 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5806 | return true; |
| 5807 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5808 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5809 | |
| 5810 | // The new class type must have the same or less qualifiers as the old type. |
| 5811 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 5812 | Diag(New->getLocation(), |
| 5813 | diag::err_covariant_return_type_class_type_more_qualified) |
| 5814 | << New->getDeclName() << NewTy << OldTy; |
| 5815 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5816 | return true; |
| 5817 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5818 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5819 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5820 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5821 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5822 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
| 5823 | const CXXMethodDecl *Old) |
| 5824 | { |
| 5825 | if (Old->hasAttr<FinalAttr>()) { |
| 5826 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 5827 | << New->getDeclName(); |
| 5828 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5829 | return true; |
| 5830 | } |
| 5831 | |
| 5832 | return false; |
| 5833 | } |
| 5834 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 5835 | /// \brief Mark the given method pure. |
| 5836 | /// |
| 5837 | /// \param Method the method to be marked pure. |
| 5838 | /// |
| 5839 | /// \param InitRange the source range that covers the "0" initializer. |
| 5840 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
| 5841 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 5842 | Method->setPure(); |
| 5843 | |
| 5844 | // A class is abstract if at least one function is pure virtual. |
| 5845 | Method->getParent()->setAbstract(true); |
| 5846 | return false; |
| 5847 | } |
| 5848 | |
| 5849 | if (!Method->isInvalidDecl()) |
| 5850 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 5851 | << Method->getDeclName() << InitRange; |
| 5852 | return true; |
| 5853 | } |
| 5854 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5855 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 5856 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 5857 | /// is a fresh scope pushed for just this purpose. |
| 5858 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5859 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 5860 | /// static data member of class X, names should be looked up in the scope of |
| 5861 | /// class X. |
| 5862 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5863 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5864 | Decl *D = Dcl.getAs<Decl>(); |
| 5865 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5866 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5867 | // We should only get called for declarations with scope specifiers, like: |
| 5868 | // int foo::bar; |
| 5869 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5870 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5871 | } |
| 5872 | |
| 5873 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5874 | /// initializer for the out-of-line declaration 'Dcl'. |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5875 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5876 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5877 | Decl *D = Dcl.getAs<Decl>(); |
| 5878 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5879 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5880 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5881 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5882 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 5883 | |
| 5884 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 5885 | /// C++ if/switch/while/for statement. |
| 5886 | /// e.g: "if (int x = f()) {...}" |
| 5887 | Action::DeclResult |
| 5888 | Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
| 5889 | // C++ 6.4p2: |
| 5890 | // The declarator shall not specify a function or an array. |
| 5891 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 5892 | // new class or enumeration. |
| 5893 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 5894 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 5895 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5896 | TypeSourceInfo *TInfo = 0; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 5897 | TagDecl *OwnedTag = 0; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5898 | QualType Ty = GetTypeForDeclarator(D, S, &TInfo, &OwnedTag); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 5899 | |
| 5900 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 5901 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 5902 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 5903 | Diag(D.getIdentifierLoc(), diag::err_invalid_use_of_function_type) |
| 5904 | << D.getSourceRange(); |
| 5905 | return DeclResult(); |
| 5906 | } else if (OwnedTag && OwnedTag->isDefinition()) { |
| 5907 | // The type-specifier-seq shall not declare a new class or enumeration. |
| 5908 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition); |
| 5909 | } |
| 5910 | |
| 5911 | DeclPtrTy Dcl = ActOnDeclarator(S, D); |
| 5912 | if (!Dcl) |
| 5913 | return DeclResult(); |
| 5914 | |
| 5915 | VarDecl *VD = cast<VarDecl>(Dcl.getAs<Decl>()); |
| 5916 | VD->setDeclaredInCondition(true); |
| 5917 | return Dcl; |
| 5918 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5919 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 5920 | static bool needsVTable(CXXMethodDecl *MD, ASTContext &Context) { |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5921 | // Ignore dependent types. |
| 5922 | if (MD->isDependentContext()) |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5923 | return false; |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 5924 | |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 5925 | // Ignore declarations that are not definitions. |
| 5926 | if (!MD->isThisDeclarationADefinition()) |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5927 | return false; |
| 5928 | |
| 5929 | CXXRecordDecl *RD = MD->getParent(); |
| 5930 | |
| 5931 | // Ignore classes without a vtable. |
| 5932 | if (!RD->isDynamicClass()) |
| 5933 | return false; |
| 5934 | |
| 5935 | switch (MD->getParent()->getTemplateSpecializationKind()) { |
| 5936 | case TSK_Undeclared: |
| 5937 | case TSK_ExplicitSpecialization: |
| 5938 | // Classes that aren't instantiations of templates don't need their |
| 5939 | // virtual methods marked until we see the definition of the key |
| 5940 | // function. |
| 5941 | break; |
| 5942 | |
| 5943 | case TSK_ImplicitInstantiation: |
| 5944 | // This is a constructor of a class template; mark all of the virtual |
| 5945 | // members as referenced to ensure that they get instantiatied. |
| 5946 | if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) |
| 5947 | return true; |
| 5948 | break; |
| 5949 | |
| 5950 | case TSK_ExplicitInstantiationDeclaration: |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5951 | return false; |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5952 | |
| 5953 | case TSK_ExplicitInstantiationDefinition: |
| 5954 | // This is method of a explicit instantiation; mark all of the virtual |
| 5955 | // members as referenced to ensure that they get instantiatied. |
| 5956 | return true; |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 5957 | } |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5958 | |
| 5959 | // Consider only out-of-line definitions of member functions. When we see |
| 5960 | // an inline definition, it's too early to compute the key function. |
| 5961 | if (!MD->isOutOfLine()) |
| 5962 | return false; |
| 5963 | |
| 5964 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(RD); |
| 5965 | |
| 5966 | // If there is no key function, we will need a copy of the vtable. |
| 5967 | if (!KeyFunction) |
| 5968 | return true; |
| 5969 | |
| 5970 | // If this is the key function, we need to mark virtual members. |
| 5971 | if (KeyFunction->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 5972 | return true; |
| 5973 | |
| 5974 | return false; |
| 5975 | } |
| 5976 | |
| 5977 | void Sema::MaybeMarkVirtualMembersReferenced(SourceLocation Loc, |
| 5978 | CXXMethodDecl *MD) { |
| 5979 | CXXRecordDecl *RD = MD->getParent(); |
| 5980 | |
Douglas Gregor | 159ef1e | 2010-01-06 04:44:19 +0000 | [diff] [blame] | 5981 | // We will need to mark all of the virtual members as referenced to build the |
| 5982 | // vtable. |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 5983 | if (!needsVTable(MD, Context)) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 5984 | return; |
| 5985 | |
| 5986 | TemplateSpecializationKind kind = RD->getTemplateSpecializationKind(); |
| 5987 | if (kind == TSK_ImplicitInstantiation) |
| 5988 | ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(RD, Loc)); |
| 5989 | else |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5990 | MarkVirtualMembersReferenced(Loc, RD); |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5991 | } |
| 5992 | |
| 5993 | bool Sema::ProcessPendingClassesWithUnmarkedVirtualMembers() { |
| 5994 | if (ClassesWithUnmarkedVirtualMembers.empty()) |
| 5995 | return false; |
| 5996 | |
Douglas Gregor | 159ef1e | 2010-01-06 04:44:19 +0000 | [diff] [blame] | 5997 | while (!ClassesWithUnmarkedVirtualMembers.empty()) { |
| 5998 | CXXRecordDecl *RD = ClassesWithUnmarkedVirtualMembers.back().first; |
| 5999 | SourceLocation Loc = ClassesWithUnmarkedVirtualMembers.back().second; |
| 6000 | ClassesWithUnmarkedVirtualMembers.pop_back(); |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6001 | MarkVirtualMembersReferenced(Loc, RD); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6002 | } |
| 6003 | |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6004 | return true; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6005 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6006 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 6007 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 6008 | const CXXRecordDecl *RD) { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6009 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 6010 | e = RD->method_end(); i != e; ++i) { |
| 6011 | CXXMethodDecl *MD = *i; |
| 6012 | |
| 6013 | // C++ [basic.def.odr]p2: |
| 6014 | // [...] A virtual member function is used if it is not pure. [...] |
| 6015 | if (MD->isVirtual() && !MD->isPure()) |
| 6016 | MarkDeclarationReferenced(Loc, MD); |
| 6017 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 6018 | |
| 6019 | // Only classes that have virtual bases need a VTT. |
| 6020 | if (RD->getNumVBases() == 0) |
| 6021 | return; |
| 6022 | |
| 6023 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 6024 | e = RD->bases_end(); i != e; ++i) { |
| 6025 | const CXXRecordDecl *Base = |
| 6026 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 6027 | if (i->isVirtual()) |
| 6028 | continue; |
| 6029 | if (Base->getNumVBases() == 0) |
| 6030 | continue; |
| 6031 | MarkVirtualMembersReferenced(Loc, Base); |
| 6032 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6033 | } |
Fariborz Jahanian | e4498c6 | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 6034 | |
| 6035 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 6036 | /// Objective-C implementation whose ivars need be initialized. |
| 6037 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 6038 | if (!getLangOptions().CPlusPlus) |
| 6039 | return; |
| 6040 | if (const ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
| 6041 | llvm::SmallVector<ObjCIvarDecl*, 8> ivars; |
| 6042 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 6043 | if (ivars.empty()) |
| 6044 | return; |
| 6045 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
| 6046 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 6047 | FieldDecl *Field = ivars[i]; |
| 6048 | CXXBaseOrMemberInitializer *Member; |
| 6049 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 6050 | InitializationKind InitKind = |
| 6051 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 6052 | |
| 6053 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
| 6054 | Sema::OwningExprResult MemberInit = |
| 6055 | InitSeq.Perform(*this, InitEntity, InitKind, |
| 6056 | Sema::MultiExprArg(*this, 0, 0)); |
| 6057 | MemberInit = MaybeCreateCXXExprWithTemporaries(move(MemberInit)); |
| 6058 | // Note, MemberInit could actually come back empty if no initialization |
| 6059 | // is required (e.g., because it would call a trivial default constructor) |
| 6060 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 6061 | continue; |
| 6062 | |
| 6063 | Member = |
| 6064 | new (Context) CXXBaseOrMemberInitializer(Context, |
| 6065 | Field, SourceLocation(), |
| 6066 | SourceLocation(), |
| 6067 | MemberInit.takeAs<Expr>(), |
| 6068 | SourceLocation()); |
| 6069 | AllToInit.push_back(Member); |
| 6070 | } |
| 6071 | ObjCImplementation->setIvarInitializers(Context, |
| 6072 | AllToInit.data(), AllToInit.size()); |
| 6073 | } |
| 6074 | } |