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" |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecordLayout.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 | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeOrdering.h" |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 25 | #include "clang/Parse/DeclSpec.h" |
| 26 | #include "clang/Parse/Template.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 27 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 30 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 31 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace clang; |
| 34 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
| 36 | // CheckDefaultArgumentVisitor |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 39 | namespace { |
| 40 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 41 | /// the default argument of a parameter to determine whether it |
| 42 | /// contains any ill-formed subexpressions. For example, this will |
| 43 | /// diagnose the use of local variables or parameters within the |
| 44 | /// default argument expression. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 45 | class CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 46 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 47 | Expr *DefaultArg; |
| 48 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 50 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 52 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 54 | bool VisitExpr(Expr *Node); |
| 55 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 56 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 57 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 59 | /// VisitExpr - Visit all of the children of this expression. |
| 60 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 61 | bool IsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | for (Stmt::child_iterator I = Node->child_begin(), |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 63 | E = Node->child_end(); I != E; ++I) |
| 64 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 65 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 68 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 69 | /// determine whether this declaration can be used in the default |
| 70 | /// argument expression. |
| 71 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 72 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 73 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 74 | // C++ [dcl.fct.default]p9 |
| 75 | // Default arguments are evaluated each time the function is |
| 76 | // called. The order of evaluation of function arguments is |
| 77 | // unspecified. Consequently, parameters of a function shall not |
| 78 | // be used in default argument expressions, even if they are not |
| 79 | // evaluated. Parameters of a function declared before a default |
| 80 | // argument expression are in scope and can hide namespace and |
| 81 | // class member names. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 83 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 84 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 85 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 86 | // C++ [dcl.fct.default]p7 |
| 87 | // Local variables shall not be used in default argument |
| 88 | // expressions. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 89 | if (VDecl->isBlockVarDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 91 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 92 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 93 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 94 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 95 | return false; |
| 96 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 97 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 98 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 99 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 100 | // C++ [dcl.fct.default]p8: |
| 101 | // The keyword this shall not be used in a default argument of a |
| 102 | // member function. |
| 103 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 104 | diag::err_param_default_argument_references_this) |
| 105 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 106 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 109 | bool |
| 110 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, ExprArg DefaultArg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | SourceLocation EqualLoc) { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 112 | QualType ParamType = Param->getType(); |
| 113 | |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 114 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 115 | diag::err_typecheck_decl_incomplete_type)) { |
| 116 | Param->setInvalidDecl(); |
| 117 | return true; |
| 118 | } |
| 119 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 120 | Expr *Arg = (Expr *)DefaultArg.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 122 | // C++ [dcl.fct.default]p5 |
| 123 | // A default argument expression is implicitly converted (clause |
| 124 | // 4) to the parameter type. The default argument expression has |
| 125 | // the same semantic constraints as the initializer expression in |
| 126 | // a declaration of a variable of the parameter type, using the |
| 127 | // copy-initialization semantics (8.5). |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 128 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Param); |
| 129 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 130 | EqualLoc); |
| 131 | if (CheckInitializerTypes(Arg, ParamType, Entity, Kind)) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 132 | return true; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 133 | |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 134 | Arg = MaybeCreateCXXExprWithTemporaries(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 136 | // Okay: add the default argument to the parameter |
| 137 | Param->setDefaultArg(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 | DefaultArg.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 141 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 144 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 145 | /// provided for a function parameter is well-formed. If so, attach it |
| 146 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 147 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | Sema::ActOnParamDefaultArgument(DeclPtrTy param, SourceLocation EqualLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 149 | ExprArg defarg) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 150 | if (!param || !defarg.get()) |
| 151 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 153 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 154 | UnparsedDefaultArgLocs.erase(Param); |
| 155 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 156 | ExprOwningPtr<Expr> DefaultArg(this, defarg.takeAs<Expr>()); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 157 | QualType ParamType = Param->getType(); |
| 158 | |
| 159 | // Default arguments are only permitted in C++ |
| 160 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 161 | Diag(EqualLoc, diag::err_param_default_argument) |
| 162 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 163 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 164 | return; |
| 165 | } |
| 166 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 167 | // Check that the default argument is well-formed |
| 168 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); |
| 169 | if (DefaultArgChecker.Visit(DefaultArg.get())) { |
| 170 | Param->setInvalidDecl(); |
| 171 | return; |
| 172 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 174 | SetParamDefaultArgument(Param, move(DefaultArg), EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 177 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 178 | /// argument for a function parameter, but we can't parse it yet |
| 179 | /// because we're inside a class definition. Note that this default |
| 180 | /// argument will be parsed later. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | void Sema::ActOnParamUnparsedDefaultArgument(DeclPtrTy param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 182 | SourceLocation EqualLoc, |
| 183 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 184 | if (!param) |
| 185 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 187 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 188 | if (Param) |
| 189 | Param->setUnparsedDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 191 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 194 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 195 | /// the default argument for the parameter param failed. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 196 | void Sema::ActOnParamDefaultArgumentError(DeclPtrTy param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 197 | if (!param) |
| 198 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 200 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
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 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 204 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 207 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 208 | /// arguments in the declarator, which is not a function declaration |
| 209 | /// or definition and therefore is not permitted to have default |
| 210 | /// arguments. This routine should be invoked for every declarator |
| 211 | /// that is not a function declaration or definition. |
| 212 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 213 | // C++ [dcl.fct.default]p3 |
| 214 | // A default argument expression shall be specified only in the |
| 215 | // parameter-declaration-clause of a function declaration or in a |
| 216 | // template-parameter (14.1). It shall not be specified for a |
| 217 | // parameter pack. If it is specified in a |
| 218 | // parameter-declaration-clause, it shall not occur within a |
| 219 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 220 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 221 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 222 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 223 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 224 | ParmVarDecl *Param = |
| 225 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 226 | if (Param->hasUnparsedDefaultArg()) { |
| 227 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 228 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 229 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 230 | delete Toks; |
| 231 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 232 | } else if (Param->getDefaultArg()) { |
| 233 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 234 | << Param->getDefaultArg()->getSourceRange(); |
| 235 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 242 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 243 | // function, once we already know that they have the same |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 244 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 245 | // error, false otherwise. |
| 246 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 247 | bool Invalid = false; |
| 248 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 249 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 250 | // For non-template functions, default arguments can be added in |
| 251 | // later declarations of a function in the same |
| 252 | // scope. Declarations in different scopes have completely |
| 253 | // distinct sets of default arguments. That is, declarations in |
| 254 | // inner scopes do not acquire default arguments from |
| 255 | // declarations in outer scopes, and vice versa. In a given |
| 256 | // function declaration, all parameters subsequent to a |
| 257 | // parameter with a default argument shall have default |
| 258 | // arguments supplied in this or previous declarations. A |
| 259 | // default argument shall not be redefined by a later |
| 260 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 261 | // |
| 262 | // C++ [dcl.fct.default]p6: |
| 263 | // Except for member functions of class templates, the default arguments |
| 264 | // in a member function definition that appears outside of the class |
| 265 | // definition are added to the set of default arguments provided by the |
| 266 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 267 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 268 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 269 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 270 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 271 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Anders Carlsson | ad26b73 | 2009-11-10 03:24:44 +0000 | [diff] [blame] | 272 | // FIXME: If the parameter doesn't have an identifier then the location |
| 273 | // points to the '=' which means that the fixit hint won't remove any |
| 274 | // extra spaces between the type and the '='. |
| 275 | SourceLocation Begin = NewParam->getLocation(); |
Anders Carlsson | 4881b99 | 2009-11-10 03:32:44 +0000 | [diff] [blame] | 276 | if (NewParam->getIdentifier()) |
| 277 | Begin = PP.getLocForEndOfToken(Begin); |
Anders Carlsson | ad26b73 | 2009-11-10 03:24:44 +0000 | [diff] [blame] | 278 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | Diag(NewParam->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 280 | diag::err_param_default_argument_redefinition) |
Anders Carlsson | ad26b73 | 2009-11-10 03:24:44 +0000 | [diff] [blame] | 281 | << NewParam->getDefaultArgRange() |
| 282 | << CodeModificationHint::CreateRemoval(SourceRange(Begin, |
| 283 | NewParam->getLocEnd())); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 284 | |
| 285 | // Look for the function declaration where the default argument was |
| 286 | // actually written, which may be a declaration prior to Old. |
| 287 | for (FunctionDecl *Older = Old->getPreviousDeclaration(); |
| 288 | Older; Older = Older->getPreviousDeclaration()) { |
| 289 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 290 | break; |
| 291 | |
| 292 | OldParam = Older->getParamDecl(p); |
| 293 | } |
| 294 | |
| 295 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 296 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 297 | Invalid = true; |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 298 | } else if (OldParam->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 299 | // Merge the old default argument into the new parameter |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 300 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 301 | NewParam->setUninstantiatedDefaultArg( |
| 302 | OldParam->getUninstantiatedDefaultArg()); |
| 303 | else |
| 304 | NewParam->setDefaultArg(OldParam->getDefaultArg()); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 305 | } else if (NewParam->hasDefaultArg()) { |
| 306 | if (New->getDescribedFunctionTemplate()) { |
| 307 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 308 | Diag(NewParam->getLocation(), |
| 309 | diag::err_param_default_argument_template_redecl) |
| 310 | << NewParam->getDefaultArgRange(); |
| 311 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 312 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 313 | } else if (New->getTemplateSpecializationKind() |
| 314 | != TSK_ImplicitInstantiation && |
| 315 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 316 | // C++ [temp.expr.spec]p21: |
| 317 | // Default function arguments shall not be specified in a declaration |
| 318 | // or a definition for one of the following explicit specializations: |
| 319 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 320 | // - the explicit specialization of a member function template; |
| 321 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 322 | // template where the class template specialization to which the |
| 323 | // member function specialization belongs is implicitly |
| 324 | // instantiated. |
| 325 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 326 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 327 | << New->getDeclName() |
| 328 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 329 | } else if (New->getDeclContext()->isDependentContext()) { |
| 330 | // C++ [dcl.fct.default]p6 (DR217): |
| 331 | // Default arguments for a member function of a class template shall |
| 332 | // be specified on the initial declaration of the member function |
| 333 | // within the class template. |
| 334 | // |
| 335 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 336 | // leads me to the conclusion that one cannot add default function |
| 337 | // arguments for an out-of-line definition of a member function of a |
| 338 | // dependent type. |
| 339 | int WhichKind = 2; |
| 340 | if (CXXRecordDecl *Record |
| 341 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 342 | if (Record->getDescribedClassTemplate()) |
| 343 | WhichKind = 0; |
| 344 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 345 | WhichKind = 1; |
| 346 | else |
| 347 | WhichKind = 2; |
| 348 | } |
| 349 | |
| 350 | Diag(NewParam->getLocation(), |
| 351 | diag::err_param_default_argument_member_template_redecl) |
| 352 | << WhichKind |
| 353 | << NewParam->getDefaultArgRange(); |
| 354 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 358 | if (CheckEquivalentExceptionSpec( |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 359 | Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 360 | New->getType()->getAs<FunctionProtoType>(), New->getLocation())) |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 361 | Invalid = true; |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 362 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 363 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 367 | /// function declaration are well-formed according to C++ |
| 368 | /// [dcl.fct.default]. |
| 369 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 370 | unsigned NumParams = FD->getNumParams(); |
| 371 | unsigned p; |
| 372 | |
| 373 | // Find first parameter with a default argument |
| 374 | for (p = 0; p < NumParams; ++p) { |
| 375 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 376 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | |
| 380 | // C++ [dcl.fct.default]p4: |
| 381 | // In a given function declaration, all parameters |
| 382 | // subsequent to a parameter with a default argument shall |
| 383 | // have default arguments supplied in this or previous |
| 384 | // declarations. A default argument shall not be redefined |
| 385 | // by a later declaration (not even to the same value). |
| 386 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 388 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 389 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 390 | if (Param->isInvalidDecl()) |
| 391 | /* We already complained about this parameter. */; |
| 392 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 394 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 395 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 396 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 398 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 400 | LastMissingDefaultArg = p; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | if (LastMissingDefaultArg > 0) { |
| 405 | // Some default arguments were missing. Clear out all of the |
| 406 | // default arguments up to (and including) the last missing |
| 407 | // default argument, so that we leave the function parameters |
| 408 | // in a semantically valid state. |
| 409 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 410 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 411 | if (Param->hasDefaultArg()) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 412 | if (!Param->hasUnparsedDefaultArg()) |
| 413 | Param->getDefaultArg()->Destroy(Context); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 414 | Param->setDefaultArg(0); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 419 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 420 | /// isCurrentClassName - Determine whether the identifier II is the |
| 421 | /// name of the class type currently being defined. In the case of |
| 422 | /// nested classes, this will only return true if II is the name of |
| 423 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 424 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 425 | const CXXScopeSpec *SS) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 426 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 427 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 428 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 429 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 430 | } else |
| 431 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 432 | |
| 433 | if (CurDecl) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 434 | return &II == CurDecl->getIdentifier(); |
| 435 | else |
| 436 | return false; |
| 437 | } |
| 438 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 440 | /// |
| 441 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 442 | /// and returns NULL otherwise. |
| 443 | CXXBaseSpecifier * |
| 444 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 445 | SourceRange SpecifierRange, |
| 446 | bool Virtual, AccessSpecifier Access, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | QualType BaseType, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 448 | SourceLocation BaseLoc) { |
| 449 | // C++ [class.union]p1: |
| 450 | // A union shall not have base classes. |
| 451 | if (Class->isUnion()) { |
| 452 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 453 | << SpecifierRange; |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | if (BaseType->isDependentType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 459 | Class->getTagKind() == RecordDecl::TK_class, |
| 460 | Access, BaseType); |
| 461 | |
| 462 | // Base specifiers must be record types. |
| 463 | if (!BaseType->isRecordType()) { |
| 464 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | // C++ [class.union]p1: |
| 469 | // A union shall not be used as a base class. |
| 470 | if (BaseType->isUnionType()) { |
| 471 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | // C++ [class.derived]p2: |
| 476 | // The class-name in a base-specifier shall not be an incompletely |
| 477 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 479 | PDiag(diag::err_incomplete_base_class) |
| 480 | << SpecifierRange)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 481 | return 0; |
| 482 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 483 | // 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] | 484 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 485 | assert(BaseDecl && "Record type has no declaration"); |
| 486 | BaseDecl = BaseDecl->getDefinition(Context); |
| 487 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 488 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 489 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 490 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 491 | // C++0x CWG Issue #817 indicates that [[final]] classes shouldn't be bases. |
| 492 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
| 493 | Diag(BaseLoc, diag::err_final_base) << BaseType.getAsString(); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 494 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 495 | << BaseType; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 496 | return 0; |
| 497 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 498 | |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 499 | SetClassDeclAttributesFromBase(Class, CXXBaseDecl, Virtual); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 500 | |
| 501 | // Create the base specifier. |
| 502 | // FIXME: Allocate via ASTContext? |
| 503 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
| 504 | Class->getTagKind() == RecordDecl::TK_class, |
| 505 | Access, BaseType); |
| 506 | } |
| 507 | |
| 508 | void Sema::SetClassDeclAttributesFromBase(CXXRecordDecl *Class, |
| 509 | const CXXRecordDecl *BaseClass, |
| 510 | bool BaseIsVirtual) { |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 511 | // A class with a non-empty base class is not empty. |
| 512 | // FIXME: Standard ref? |
| 513 | if (!BaseClass->isEmpty()) |
| 514 | Class->setEmpty(false); |
| 515 | |
| 516 | // C++ [class.virtual]p1: |
| 517 | // A class that [...] inherits a virtual function is called a polymorphic |
| 518 | // class. |
| 519 | if (BaseClass->isPolymorphic()) |
| 520 | Class->setPolymorphic(true); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 521 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 522 | // C++ [dcl.init.aggr]p1: |
| 523 | // An aggregate is [...] a class with [...] no base classes [...]. |
| 524 | Class->setAggregate(false); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 525 | |
| 526 | // C++ [class]p4: |
| 527 | // A POD-struct is an aggregate class... |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 528 | Class->setPOD(false); |
| 529 | |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 530 | if (BaseIsVirtual) { |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 531 | // C++ [class.ctor]p5: |
| 532 | // A constructor is trivial if its class has no virtual base classes. |
| 533 | Class->setHasTrivialConstructor(false); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 534 | |
| 535 | // C++ [class.copy]p6: |
| 536 | // A copy constructor is trivial if its class has no virtual base classes. |
| 537 | Class->setHasTrivialCopyConstructor(false); |
| 538 | |
| 539 | // C++ [class.copy]p11: |
| 540 | // A copy assignment operator is trivial if its class has no virtual |
| 541 | // base classes. |
| 542 | Class->setHasTrivialCopyAssignment(false); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 543 | |
| 544 | // C++0x [meta.unary.prop] is_empty: |
| 545 | // T is a class type, but not a union type, with ... no virtual base |
| 546 | // classes |
| 547 | Class->setEmpty(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 548 | } else { |
| 549 | // C++ [class.ctor]p5: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | // A constructor is trivial if all the direct base classes of its |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 551 | // class have trivial constructors. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 552 | if (!BaseClass->hasTrivialConstructor()) |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 553 | Class->setHasTrivialConstructor(false); |
| 554 | |
| 555 | // C++ [class.copy]p6: |
| 556 | // A copy constructor is trivial if all the direct base classes of its |
| 557 | // class have trivial copy constructors. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 558 | if (!BaseClass->hasTrivialCopyConstructor()) |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 559 | Class->setHasTrivialCopyConstructor(false); |
| 560 | |
| 561 | // C++ [class.copy]p11: |
| 562 | // A copy assignment operator is trivial if all the direct base classes |
| 563 | // of its class have trivial copy assignment operators. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 564 | if (!BaseClass->hasTrivialCopyAssignment()) |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 565 | Class->setHasTrivialCopyAssignment(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 566 | } |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 567 | |
| 568 | // C++ [class.ctor]p3: |
| 569 | // A destructor is trivial if all the direct base classes of its class |
| 570 | // have trivial destructors. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 571 | if (!BaseClass->hasTrivialDestructor()) |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 572 | Class->setHasTrivialDestructor(false); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 575 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 576 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | /// example: |
| 578 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 579 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | Sema::BaseResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 581 | Sema::ActOnBaseSpecifier(DeclPtrTy classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 582 | bool Virtual, AccessSpecifier Access, |
| 583 | TypeTy *basetype, SourceLocation BaseLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 584 | if (!classdecl) |
| 585 | return true; |
| 586 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 587 | AdjustDeclIfTemplate(classdecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 588 | CXXRecordDecl *Class = cast<CXXRecordDecl>(classdecl.getAs<Decl>()); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 589 | QualType BaseType = GetTypeFromParser(basetype); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 590 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
| 591 | Virtual, Access, |
| 592 | BaseType, BaseLoc)) |
| 593 | return BaseSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 595 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 596 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 597 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 598 | /// \brief Performs the actual work of attaching the given base class |
| 599 | /// specifiers to a C++ class. |
| 600 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 601 | unsigned NumBases) { |
| 602 | if (NumBases == 0) |
| 603 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 604 | |
| 605 | // Used to keep track of which base types we have already seen, so |
| 606 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 607 | // that the key is always the unqualified canonical type of the base |
| 608 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 609 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 610 | |
| 611 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 612 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 613 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 614 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 616 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 617 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 618 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 619 | if (KnownBaseTypes[NewBaseType]) { |
| 620 | // C++ [class.mi]p3: |
| 621 | // A class shall not be specified as a direct base class of a |
| 622 | // derived class more than once. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 623 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 624 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 625 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 626 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 627 | |
| 628 | // Delete the duplicate base class specifier; we're going to |
| 629 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 630 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 631 | |
| 632 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 633 | } else { |
| 634 | // Okay, add this new base class. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 635 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 636 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
| 640 | // Attach the remaining base class specifiers to the derived class. |
Fariborz Jahanian | 5ffcd7b | 2009-07-02 18:26:15 +0000 | [diff] [blame] | 641 | Class->setBases(Context, Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 642 | |
| 643 | // Delete the remaining (good) base class specifiers, since their |
| 644 | // data has been copied into the CXXRecordDecl. |
| 645 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 646 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 647 | |
| 648 | return Invalid; |
| 649 | } |
| 650 | |
| 651 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 652 | /// class, after checking whether there are any duplicate base |
| 653 | /// classes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | void Sema::ActOnBaseSpecifiers(DeclPtrTy ClassDecl, BaseTy **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 655 | unsigned NumBases) { |
| 656 | if (!ClassDecl || !Bases || !NumBases) |
| 657 | return; |
| 658 | |
| 659 | AdjustDeclIfTemplate(ClassDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 660 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl.getAs<Decl>()), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 661 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 662 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 663 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 664 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 665 | /// derived from the type \p Base. |
| 666 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 667 | if (!getLangOptions().CPlusPlus) |
| 668 | return false; |
| 669 | |
| 670 | const RecordType *DerivedRT = Derived->getAs<RecordType>(); |
| 671 | if (!DerivedRT) |
| 672 | return false; |
| 673 | |
| 674 | const RecordType *BaseRT = Base->getAs<RecordType>(); |
| 675 | if (!BaseRT) |
| 676 | return false; |
| 677 | |
| 678 | CXXRecordDecl *DerivedRD = cast<CXXRecordDecl>(DerivedRT->getDecl()); |
| 679 | CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl()); |
| 680 | return DerivedRD->isDerivedFrom(BaseRD); |
| 681 | } |
| 682 | |
| 683 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 684 | /// derived from the type \p Base. |
| 685 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 686 | if (!getLangOptions().CPlusPlus) |
| 687 | return false; |
| 688 | |
| 689 | const RecordType *DerivedRT = Derived->getAs<RecordType>(); |
| 690 | if (!DerivedRT) |
| 691 | return false; |
| 692 | |
| 693 | const RecordType *BaseRT = Base->getAs<RecordType>(); |
| 694 | if (!BaseRT) |
| 695 | return false; |
| 696 | |
| 697 | CXXRecordDecl *DerivedRD = cast<CXXRecordDecl>(DerivedRT->getDecl()); |
| 698 | CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl()); |
| 699 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 700 | } |
| 701 | |
| 702 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 703 | /// conversion (where Derived and Base are class types) is |
| 704 | /// well-formed, meaning that the conversion is unambiguous (and |
| 705 | /// that all of the base classes are accessible). Returns true |
| 706 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 707 | /// otherwise. Loc is the location where this routine should point to |
| 708 | /// if there is an error, and Range is the source range to highlight |
| 709 | /// if there is an error. |
| 710 | bool |
| 711 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
| 712 | unsigned InaccessibleBaseID, |
| 713 | unsigned AmbigiousBaseConvID, |
| 714 | SourceLocation Loc, SourceRange Range, |
| 715 | DeclarationName Name) { |
| 716 | // First, determine whether the path from Derived to Base is |
| 717 | // ambiguous. This is slightly more expensive than checking whether |
| 718 | // the Derived to Base conversion exists, because here we need to |
| 719 | // explore multiple paths to determine if there is an ambiguity. |
| 720 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 721 | /*DetectVirtual=*/false); |
| 722 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 723 | assert(DerivationOkay && |
| 724 | "Can only be used with a derived-to-base conversion"); |
| 725 | (void)DerivationOkay; |
| 726 | |
| 727 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 728 | if (InaccessibleBaseID == 0) |
| 729 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 730 | // Check that the base class can be accessed. |
| 731 | return CheckBaseClassAccess(Derived, Base, InaccessibleBaseID, Paths, Loc, |
| 732 | Name); |
| 733 | } |
| 734 | |
| 735 | // We know that the derived-to-base conversion is ambiguous, and |
| 736 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 737 | // search just one more time to compute all of the possible paths so |
| 738 | // that we can print them out. This is more expensive than any of |
| 739 | // the previous derived-to-base checks we've done, but at this point |
| 740 | // performance isn't as much of an issue. |
| 741 | Paths.clear(); |
| 742 | Paths.setRecordingPaths(true); |
| 743 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 744 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 745 | (void)StillOkay; |
| 746 | |
| 747 | // Build up a textual representation of the ambiguous paths, e.g., |
| 748 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 749 | // conversions in the diagnostic. We only print one of the paths |
| 750 | // to each base class subobject. |
| 751 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 752 | |
| 753 | Diag(Loc, AmbigiousBaseConvID) |
| 754 | << Derived << Base << PathDisplayStr << Range << Name; |
| 755 | return true; |
| 756 | } |
| 757 | |
| 758 | bool |
| 759 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 760 | SourceLocation Loc, SourceRange Range, |
| 761 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 762 | return CheckDerivedToBaseConversion(Derived, Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 763 | IgnoreAccess ? 0 : |
| 764 | diag::err_conv_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 765 | diag::err_ambiguous_derived_to_base_conv, |
| 766 | Loc, Range, DeclarationName()); |
| 767 | } |
| 768 | |
| 769 | |
| 770 | /// @brief Builds a string representing ambiguous paths from a |
| 771 | /// specific derived class to different subobjects of the same base |
| 772 | /// class. |
| 773 | /// |
| 774 | /// This function builds a string that can be used in error messages |
| 775 | /// to show the different paths that one can take through the |
| 776 | /// inheritance hierarchy to go from the derived class to different |
| 777 | /// subobjects of a base class. The result looks something like this: |
| 778 | /// @code |
| 779 | /// struct D -> struct B -> struct A |
| 780 | /// struct D -> struct C -> struct A |
| 781 | /// @endcode |
| 782 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 783 | std::string PathDisplayStr; |
| 784 | std::set<unsigned> DisplayedPaths; |
| 785 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 786 | Path != Paths.end(); ++Path) { |
| 787 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 788 | // We haven't displayed a path to this particular base |
| 789 | // class subobject yet. |
| 790 | PathDisplayStr += "\n "; |
| 791 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 792 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 793 | Element != Path->end(); ++Element) |
| 794 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | return PathDisplayStr; |
| 799 | } |
| 800 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 801 | //===----------------------------------------------------------------------===// |
| 802 | // C++ class member Handling |
| 803 | //===----------------------------------------------------------------------===// |
| 804 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 805 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 806 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 807 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
Chris Lattner | b6688e0 | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 808 | /// any. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 809 | Sema::DeclPtrTy |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 810 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 811 | MultiTemplateParamsArg TemplateParameterLists, |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 812 | ExprTy *BW, ExprTy *InitExpr, bool IsDefinition, |
| 813 | bool Deleted) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 814 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 815 | DeclarationName Name = GetNameForDeclarator(D); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 816 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 817 | Expr *Init = static_cast<Expr*>(InitExpr); |
| 818 | SourceLocation Loc = D.getIdentifierLoc(); |
| 819 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 820 | bool isFunc = D.isFunctionDeclarator(); |
| 821 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 822 | assert(!DS.isFriendSpecified()); |
| 823 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 824 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 825 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 826 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 827 | // data members and cannot be applied to names declared const or static, |
| 828 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 829 | switch (DS.getStorageClassSpec()) { |
| 830 | case DeclSpec::SCS_unspecified: |
| 831 | case DeclSpec::SCS_typedef: |
| 832 | case DeclSpec::SCS_static: |
| 833 | // FALL THROUGH. |
| 834 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 835 | case DeclSpec::SCS_mutable: |
| 836 | if (isFunc) { |
| 837 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 838 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 839 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 840 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 841 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 842 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 843 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 844 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 845 | } else { |
| 846 | QualType T = GetTypeForDeclarator(D, S); |
| 847 | diag::kind err = static_cast<diag::kind>(0); |
| 848 | if (T->isReferenceType()) |
| 849 | err = diag::err_mutable_reference; |
| 850 | else if (T.isConstQualified()) |
| 851 | err = diag::err_mutable_const; |
| 852 | if (err != 0) { |
| 853 | if (DS.getStorageClassSpecLoc().isValid()) |
| 854 | Diag(DS.getStorageClassSpecLoc(), err); |
| 855 | else |
| 856 | Diag(DS.getThreadSpecLoc(), err); |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 857 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 858 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 859 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 860 | } |
| 861 | } |
| 862 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 863 | default: |
| 864 | if (DS.getStorageClassSpecLoc().isValid()) |
| 865 | Diag(DS.getStorageClassSpecLoc(), |
| 866 | diag::err_storageclass_invalid_for_member); |
| 867 | else |
| 868 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 869 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 870 | } |
| 871 | |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 872 | if (!isFunc && |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 873 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename && |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 874 | D.getNumTypeObjects() == 0) { |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 875 | // Check also for this case: |
| 876 | // |
| 877 | // typedef int f(); |
| 878 | // f a; |
| 879 | // |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 880 | QualType TDType = GetTypeFromParser(DS.getTypeRep()); |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 881 | isFunc = TDType->isFunctionType(); |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 882 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 883 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 884 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 885 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 886 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 887 | |
| 888 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 889 | if (isInstField) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 890 | // FIXME: Check for template parameters! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 891 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 892 | AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 893 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 894 | } else { |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 895 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition) |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 896 | .getAs<Decl>(); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 897 | if (!Member) { |
| 898 | if (BitWidth) DeleteExpr(BitWidth); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 899 | return DeclPtrTy(); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 900 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 901 | |
| 902 | // Non-instance-fields can't have a bitfield. |
| 903 | if (BitWidth) { |
| 904 | if (Member->isInvalidDecl()) { |
| 905 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 906 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 907 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 908 | // "static member 'A' cannot be a bit-field" |
| 909 | Diag(Loc, diag::err_static_not_bitfield) |
| 910 | << Name << BitWidth->getSourceRange(); |
| 911 | } else if (isa<TypedefDecl>(Member)) { |
| 912 | // "typedef member 'x' cannot be a bit-field" |
| 913 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 914 | << Name << BitWidth->getSourceRange(); |
| 915 | } else { |
| 916 | // A function typedef ("typedef int f(); f a;"). |
| 917 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 918 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 920 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 921 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 923 | DeleteExpr(BitWidth); |
| 924 | BitWidth = 0; |
| 925 | Member->setInvalidDecl(); |
| 926 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 927 | |
| 928 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 930 | // If we have declared a member function template, set the access of the |
| 931 | // templated declaration as well. |
| 932 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 933 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 934 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 935 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 936 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 937 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 938 | if (Init) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 939 | AddInitializerToDecl(DeclPtrTy::make(Member), ExprArg(*this, Init), false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 940 | if (Deleted) // FIXME: Source location is not very good. |
| 941 | SetDeclDeleted(DeclPtrTy::make(Member), D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 942 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 943 | if (isInstField) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 944 | FieldCollector->Add(cast<FieldDecl>(Member)); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 945 | return DeclPtrTy(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 946 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 947 | return DeclPtrTy::make(Member); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 950 | /// ActOnMemInitializer - Handle a C++ member initializer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | Sema::MemInitResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 952 | Sema::ActOnMemInitializer(DeclPtrTy ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 953 | Scope *S, |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 954 | const CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 955 | IdentifierInfo *MemberOrBase, |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 956 | TypeTy *TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 957 | SourceLocation IdLoc, |
| 958 | SourceLocation LParenLoc, |
| 959 | ExprTy **Args, unsigned NumArgs, |
| 960 | SourceLocation *CommaLocs, |
| 961 | SourceLocation RParenLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 962 | if (!ConstructorD) |
| 963 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 965 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | |
| 967 | CXXConstructorDecl *Constructor |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 968 | = dyn_cast<CXXConstructorDecl>(ConstructorD.getAs<Decl>()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 969 | if (!Constructor) { |
| 970 | // The user wrote a constructor initializer on a function that is |
| 971 | // not a C++ constructor. Ignore the error for now, because we may |
| 972 | // have more member initializers coming; we'll diagnose it just |
| 973 | // once in ActOnMemInitializers. |
| 974 | return true; |
| 975 | } |
| 976 | |
| 977 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 978 | |
| 979 | // C++ [class.base.init]p2: |
| 980 | // Names in a mem-initializer-id are looked up in the scope of the |
| 981 | // constructor’s class and, if not found in that scope, are looked |
| 982 | // up in the scope containing the constructor’s |
| 983 | // definition. [Note: if the constructor’s class contains a member |
| 984 | // with the same name as a direct or virtual base class of the |
| 985 | // class, a mem-initializer-id naming the member or base class and |
| 986 | // composed of a single identifier refers to the class member. A |
| 987 | // mem-initializer-id for the hidden base class may be specified |
| 988 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 989 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 990 | // Look for a member, first. |
| 991 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 993 | = ClassDecl->lookup(MemberOrBase); |
| 994 | if (Result.first != Result.second) |
| 995 | Member = dyn_cast<FieldDecl>(*Result.first); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 996 | |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 997 | // FIXME: Handle members of an anonymous union. |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 998 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 999 | if (Member) |
| 1000 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1001 | LParenLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1002 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1003 | // 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] | 1004 | QualType BaseType; |
| 1005 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1006 | TypeSourceInfo *TInfo = 0; |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1007 | if (TemplateTypeTy) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1008 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1009 | else |
| 1010 | BaseType = QualType::getFromOpaquePtr(getTypeName(*MemberOrBase, IdLoc, |
| 1011 | S, &SS)); |
| 1012 | if (BaseType.isNull()) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1013 | return Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1014 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1016 | if (!TInfo) |
| 1017 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1018 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1019 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1020 | LParenLoc, RParenLoc, ClassDecl); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1023 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1024 | /// containing the field that is being initialized. Returns true if there is an |
| 1025 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1026 | /// otherwise. |
| 1027 | static bool InitExprContainsUninitializedFields(const Stmt* S, |
| 1028 | const FieldDecl* LhsField, |
| 1029 | SourceLocation* L) { |
| 1030 | const MemberExpr* ME = dyn_cast<MemberExpr>(S); |
| 1031 | if (ME) { |
| 1032 | const NamedDecl* RhsField = ME->getMemberDecl(); |
| 1033 | if (RhsField == LhsField) { |
| 1034 | // Initializing a field with itself. Throw a warning. |
| 1035 | // But wait; there are exceptions! |
| 1036 | // Exception #1: The field may not belong to this record. |
| 1037 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
| 1038 | const Expr* base = ME->getBase(); |
| 1039 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1040 | // Even though the field matches, it does not belong to this record. |
| 1041 | return false; |
| 1042 | } |
| 1043 | // None of the exceptions triggered; return true to indicate an |
| 1044 | // uninitialized field was used. |
| 1045 | *L = ME->getMemberLoc(); |
| 1046 | return true; |
| 1047 | } |
| 1048 | } |
| 1049 | bool found = false; |
| 1050 | for (Stmt::const_child_iterator it = S->child_begin(); |
| 1051 | it != S->child_end() && found == false; |
| 1052 | ++it) { |
| 1053 | if (isa<CallExpr>(S)) { |
| 1054 | // Do not descend into function calls or constructors, as the use |
| 1055 | // of an uninitialized field may be valid. One would have to inspect |
| 1056 | // the contents of the function/ctor to determine if it is safe or not. |
| 1057 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1058 | // may be safe, depending on what the function/ctor does. |
| 1059 | continue; |
| 1060 | } |
| 1061 | found = InitExprContainsUninitializedFields(*it, LhsField, L); |
| 1062 | } |
| 1063 | return found; |
| 1064 | } |
| 1065 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1066 | Sema::MemInitResult |
| 1067 | Sema::BuildMemberInitializer(FieldDecl *Member, Expr **Args, |
| 1068 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1069 | SourceLocation LParenLoc, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1070 | SourceLocation RParenLoc) { |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1071 | // FIXME: CXXBaseOrMemberInitializer should only contain a single |
| 1072 | // subexpression so we can wrap it in a CXXExprWithTemporaries if necessary. |
| 1073 | ExprTemporaries.clear(); |
| 1074 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1075 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1076 | // foo(foo) |
| 1077 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1078 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1079 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1080 | SourceLocation L; |
| 1081 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1082 | // FIXME: Return true in the case when other fields are used before being |
| 1083 | // uninitialized. For example, let this field be the i'th field. When |
| 1084 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1085 | // fields are used, as they are not yet initialized. |
| 1086 | // Right now we are only handling the case where the i'th field uses |
| 1087 | // itself in its initializer. |
| 1088 | Diag(L, diag::warn_field_is_uninit); |
| 1089 | } |
| 1090 | } |
| 1091 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1092 | bool HasDependentArg = false; |
| 1093 | for (unsigned i = 0; i < NumArgs; i++) |
| 1094 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1095 | |
| 1096 | CXXConstructorDecl *C = 0; |
| 1097 | QualType FieldType = Member->getType(); |
| 1098 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 1099 | FieldType = Array->getElementType(); |
| 1100 | if (FieldType->isDependentType()) { |
| 1101 | // Can't check init for dependent type. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1102 | } else if (FieldType->isRecordType()) { |
| 1103 | // Member is a record (struct/union/class), so pass the initializer |
| 1104 | // arguments down to the record's constructor. |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1105 | if (!HasDependentArg) { |
| 1106 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 1107 | |
| 1108 | C = PerformInitializationByConstructor(FieldType, |
| 1109 | MultiExprArg(*this, |
| 1110 | (void**)Args, |
| 1111 | NumArgs), |
| 1112 | IdLoc, |
| 1113 | SourceRange(IdLoc, RParenLoc), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1114 | Member->getDeclName(), |
| 1115 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc), |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1116 | ConstructorArgs); |
| 1117 | |
| 1118 | if (C) { |
| 1119 | // Take over the constructor arguments as our own. |
| 1120 | NumArgs = ConstructorArgs.size(); |
| 1121 | Args = (Expr **)ConstructorArgs.take(); |
| 1122 | } |
| 1123 | } |
Fariborz Jahanian | 636a0ff | 2009-09-02 17:10:17 +0000 | [diff] [blame] | 1124 | } else if (NumArgs != 1 && NumArgs != 0) { |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1125 | // The member type is not a record type (or an array of record |
| 1126 | // types), so it can be only be default- or copy-initialized. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | return Diag(IdLoc, diag::err_mem_initializer_mismatch) |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1128 | << Member->getDeclName() << SourceRange(IdLoc, RParenLoc); |
| 1129 | } else if (!HasDependentArg) { |
Fariborz Jahanian | 636a0ff | 2009-09-02 17:10:17 +0000 | [diff] [blame] | 1130 | Expr *NewExp; |
| 1131 | if (NumArgs == 0) { |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1132 | if (FieldType->isReferenceType()) { |
| 1133 | Diag(IdLoc, diag::err_null_intialized_reference_member) |
| 1134 | << Member->getDeclName(); |
| 1135 | return Diag(Member->getLocation(), diag::note_declared_at); |
| 1136 | } |
Fariborz Jahanian | 636a0ff | 2009-09-02 17:10:17 +0000 | [diff] [blame] | 1137 | NewExp = new (Context) CXXZeroInitValueExpr(FieldType, IdLoc, RParenLoc); |
| 1138 | NumArgs = 1; |
| 1139 | } |
| 1140 | else |
| 1141 | NewExp = (Expr*)Args[0]; |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1142 | if (PerformCopyInitialization(NewExp, FieldType, AA_Passing)) |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1143 | return true; |
| 1144 | Args[0] = NewExp; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1145 | } |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1146 | |
| 1147 | // FIXME: CXXBaseOrMemberInitializer should only contain a single |
| 1148 | // subexpression so we can wrap it in a CXXExprWithTemporaries if necessary. |
| 1149 | ExprTemporaries.clear(); |
| 1150 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1151 | // FIXME: Perform direct initialization of the member. |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1152 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1153 | C, LParenLoc, (Expr **)Args, |
| 1154 | NumArgs, RParenLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | Sema::MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1158 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1159 | Expr **Args, unsigned NumArgs, |
| 1160 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
| 1161 | CXXRecordDecl *ClassDecl) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1162 | bool HasDependentArg = false; |
| 1163 | for (unsigned i = 0; i < NumArgs; i++) |
| 1164 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1165 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1166 | SourceLocation BaseLoc = BaseTInfo->getTypeLoc().getSourceRange().getBegin(); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1167 | if (!BaseType->isDependentType()) { |
| 1168 | if (!BaseType->isRecordType()) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1169 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1170 | << BaseType << BaseTInfo->getTypeLoc().getSourceRange(); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1171 | |
| 1172 | // C++ [class.base.init]p2: |
| 1173 | // [...] Unless the mem-initializer-id names a nonstatic data |
| 1174 | // member of the constructor’s class or a direct or virtual base |
| 1175 | // of that class, the mem-initializer is ill-formed. A |
| 1176 | // mem-initializer-list can initialize a base class using any |
| 1177 | // name that denotes that base class type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1179 | // First, check for a direct base class. |
| 1180 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1181 | for (CXXRecordDecl::base_class_const_iterator Base = |
| 1182 | ClassDecl->bases_begin(); Base != ClassDecl->bases_end(); ++Base) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1183 | if (Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1184 | // We found a direct base of this type. That's what we're |
| 1185 | // initializing. |
| 1186 | DirectBaseSpec = &*Base; |
| 1187 | break; |
| 1188 | } |
| 1189 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1190 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1191 | // Check for a virtual base class. |
| 1192 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1193 | // there are no virtual bases. |
| 1194 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1195 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1196 | // We haven't found a base yet; search the class hierarchy for a |
| 1197 | // virtual base class. |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1198 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1199 | /*DetectVirtual=*/false); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1200 | if (IsDerivedFrom(Context.getTypeDeclType(ClassDecl), BaseType, Paths)) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1201 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1202 | Path != Paths.end(); ++Path) { |
| 1203 | if (Path->back().Base->isVirtual()) { |
| 1204 | VirtualBaseSpec = Path->back().Base; |
| 1205 | break; |
| 1206 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1207 | } |
| 1208 | } |
| 1209 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1210 | |
| 1211 | // C++ [base.class.init]p2: |
| 1212 | // If a mem-initializer-id is ambiguous because it designates both |
| 1213 | // a direct non-virtual base class and an inherited virtual base |
| 1214 | // class, the mem-initializer is ill-formed. |
| 1215 | if (DirectBaseSpec && VirtualBaseSpec) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1216 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1217 | << BaseType << BaseTInfo->getTypeLoc().getSourceRange(); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1218 | // C++ [base.class.init]p2: |
| 1219 | // Unless the mem-initializer-id names a nonstatic data membeer of the |
| 1220 | // constructor's class ot a direst or virtual base of that class, the |
| 1221 | // mem-initializer is ill-formed. |
| 1222 | if (!DirectBaseSpec && !VirtualBaseSpec) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1223 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 1224 | << BaseType << ClassDecl->getNameAsCString() |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1225 | << BaseTInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 1228 | CXXConstructorDecl *C = 0; |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1229 | if (!BaseType->isDependentType() && !HasDependentArg) { |
| 1230 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
Douglas Gregor | 3eaa9ff | 2009-11-08 07:12:55 +0000 | [diff] [blame] | 1231 | Context.getCanonicalType(BaseType).getUnqualifiedType()); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1232 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 1233 | |
| 1234 | C = PerformInitializationByConstructor(BaseType, |
| 1235 | MultiExprArg(*this, |
| 1236 | (void**)Args, NumArgs), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1237 | BaseLoc, |
| 1238 | SourceRange(BaseLoc, RParenLoc), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1239 | Name, |
| 1240 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc), |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1241 | ConstructorArgs); |
| 1242 | if (C) { |
| 1243 | // Take over the constructor arguments as our own. |
| 1244 | NumArgs = ConstructorArgs.size(); |
| 1245 | Args = (Expr **)ConstructorArgs.take(); |
| 1246 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1249 | // FIXME: CXXBaseOrMemberInitializer should only contain a single |
| 1250 | // subexpression so we can wrap it in a CXXExprWithTemporaries if necessary. |
| 1251 | ExprTemporaries.clear(); |
| 1252 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1253 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, C, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1254 | LParenLoc, (Expr **)Args, |
| 1255 | NumArgs, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1258 | bool |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1259 | Sema::SetBaseOrMemberInitializers(CXXConstructorDecl *Constructor, |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1260 | CXXBaseOrMemberInitializer **Initializers, |
| 1261 | unsigned NumInitializers, |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1262 | bool IsImplicitConstructor) { |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1263 | // We need to build the initializer AST according to order of construction |
| 1264 | // and not what user specified in the Initializers list. |
| 1265 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 1266 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
| 1267 | llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; |
| 1268 | bool HasDependentBaseInit = false; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1269 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1271 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1272 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
| 1273 | if (Member->isBaseInitializer()) { |
| 1274 | if (Member->getBaseClass()->isDependentType()) |
| 1275 | HasDependentBaseInit = true; |
| 1276 | AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
| 1277 | } else { |
| 1278 | AllBaseFields[Member->getMember()] = Member; |
| 1279 | } |
| 1280 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1282 | if (HasDependentBaseInit) { |
| 1283 | // FIXME. This does not preserve the ordering of the initializers. |
| 1284 | // Try (with -Wreorder) |
| 1285 | // template<class X> struct A {}; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | // template<class X> struct B : A<X> { |
| 1287 | // B() : x1(10), A<X>() {} |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1288 | // int x1; |
| 1289 | // }; |
| 1290 | // B<int> x; |
| 1291 | // On seeing one dependent type, we should essentially exit this routine |
| 1292 | // while preserving user-declared initializer list. When this routine is |
| 1293 | // called during instantiatiation process, this routine will rebuild the |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1294 | // ordered initializer list correctly. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1296 | // If we have a dependent base initialization, we can't determine the |
| 1297 | // association between initializers and bases; just dump the known |
| 1298 | // initializers into the list, and don't try to deal with other bases. |
| 1299 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1300 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
| 1301 | if (Member->isBaseInitializer()) |
| 1302 | AllToInit.push_back(Member); |
| 1303 | } |
| 1304 | } else { |
| 1305 | // Push virtual bases before others. |
| 1306 | for (CXXRecordDecl::base_class_iterator VBase = |
| 1307 | ClassDecl->vbases_begin(), |
| 1308 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 1309 | if (VBase->getType()->isDependentType()) |
| 1310 | continue; |
Douglas Gregor | c07a494 | 2009-11-15 08:51:10 +0000 | [diff] [blame] | 1311 | if (CXXBaseOrMemberInitializer *Value |
| 1312 | = AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1313 | AllToInit.push_back(Value); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1314 | } |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1315 | else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | CXXRecordDecl *VBaseDecl = |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1317 | cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1318 | assert(VBaseDecl && "SetBaseOrMemberInitializers - VBaseDecl null"); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1319 | CXXConstructorDecl *Ctor = VBaseDecl->getDefaultConstructor(Context); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1320 | if (!Ctor) { |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1321 | Diag(Constructor->getLocation(), diag::err_missing_default_ctor) |
| 1322 | << (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl) |
| 1323 | << 0 << VBase->getType(); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1324 | Diag(VBaseDecl->getLocation(), diag::note_previous_decl) |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1325 | << Context.getTagDeclType(VBaseDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1326 | HadError = true; |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1327 | continue; |
| 1328 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1329 | |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1330 | ASTOwningVector<&ActionBase::DeleteExpr> CtorArgs(*this); |
| 1331 | if (CompleteConstructorCall(Ctor, MultiExprArg(*this, 0, 0), |
| 1332 | Constructor->getLocation(), CtorArgs)) |
| 1333 | continue; |
| 1334 | |
| 1335 | MarkDeclarationReferenced(Constructor->getLocation(), Ctor); |
| 1336 | |
Anders Carlsson | 8db68da | 2009-11-13 20:11:49 +0000 | [diff] [blame] | 1337 | // FIXME: CXXBaseOrMemberInitializer should only contain a single |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1338 | // subexpression so we can wrap it in a CXXExprWithTemporaries if |
| 1339 | // necessary. |
| 1340 | // FIXME: Is there any better source-location information we can give? |
Anders Carlsson | 8db68da | 2009-11-13 20:11:49 +0000 | [diff] [blame] | 1341 | ExprTemporaries.clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | CXXBaseOrMemberInitializer *Member = |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1343 | new (Context) CXXBaseOrMemberInitializer(Context, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1344 | Context.getTrivialTypeSourceInfo(VBase->getType(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1345 | SourceLocation()), |
| 1346 | Ctor, |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1347 | SourceLocation(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1348 | CtorArgs.takeAs<Expr>(), |
| 1349 | CtorArgs.size(), |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1350 | SourceLocation()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1351 | AllToInit.push_back(Member); |
| 1352 | } |
| 1353 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1355 | for (CXXRecordDecl::base_class_iterator Base = |
| 1356 | ClassDecl->bases_begin(), |
| 1357 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1358 | // Virtuals are in the virtual base list and already constructed. |
| 1359 | if (Base->isVirtual()) |
| 1360 | continue; |
| 1361 | // Skip dependent types. |
| 1362 | if (Base->getType()->isDependentType()) |
| 1363 | continue; |
Douglas Gregor | c07a494 | 2009-11-15 08:51:10 +0000 | [diff] [blame] | 1364 | if (CXXBaseOrMemberInitializer *Value |
| 1365 | = AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1366 | AllToInit.push_back(Value); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1367 | } |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1368 | else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | CXXRecordDecl *BaseDecl = |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1370 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1371 | assert(BaseDecl && "SetBaseOrMemberInitializers - BaseDecl null"); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1372 | CXXConstructorDecl *Ctor = BaseDecl->getDefaultConstructor(Context); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1373 | if (!Ctor) { |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1374 | Diag(Constructor->getLocation(), diag::err_missing_default_ctor) |
| 1375 | << (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl) |
| 1376 | << 0 << Base->getType(); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1377 | Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1378 | << Context.getTagDeclType(BaseDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1379 | HadError = true; |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1380 | continue; |
| 1381 | } |
| 1382 | |
| 1383 | ASTOwningVector<&ActionBase::DeleteExpr> CtorArgs(*this); |
| 1384 | if (CompleteConstructorCall(Ctor, MultiExprArg(*this, 0, 0), |
| 1385 | Constructor->getLocation(), CtorArgs)) |
| 1386 | continue; |
| 1387 | |
| 1388 | MarkDeclarationReferenced(Constructor->getLocation(), Ctor); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1389 | |
Anders Carlsson | 8db68da | 2009-11-13 20:11:49 +0000 | [diff] [blame] | 1390 | // FIXME: CXXBaseOrMemberInitializer should only contain a single |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1391 | // subexpression so we can wrap it in a CXXExprWithTemporaries if |
| 1392 | // necessary. |
| 1393 | // FIXME: Is there any better source-location information we can give? |
Anders Carlsson | 8db68da | 2009-11-13 20:11:49 +0000 | [diff] [blame] | 1394 | ExprTemporaries.clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | CXXBaseOrMemberInitializer *Member = |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1396 | new (Context) CXXBaseOrMemberInitializer(Context, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1397 | Context.getTrivialTypeSourceInfo(Base->getType(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1398 | SourceLocation()), |
| 1399 | Ctor, |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1400 | SourceLocation(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1401 | CtorArgs.takeAs<Expr>(), |
| 1402 | CtorArgs.size(), |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1403 | SourceLocation()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1404 | AllToInit.push_back(Member); |
| 1405 | } |
| 1406 | } |
| 1407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1409 | // non-static data members. |
| 1410 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1411 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 1412 | if ((*Field)->isAnonymousStructOrUnion()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | if (const RecordType *FieldClassType = |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1414 | Field->getType()->getAs<RecordType>()) { |
| 1415 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | afe7ec2 | 2009-11-13 18:34:26 +0000 | [diff] [blame] | 1416 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1418 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1419 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*FA)) { |
| 1420 | // 'Member' is the anonymous union field and 'AnonUnionMember' is |
| 1421 | // set to the anonymous union data member used in the initializer |
| 1422 | // list. |
| 1423 | Value->setMember(*Field); |
| 1424 | Value->setAnonUnionMember(*FA); |
| 1425 | AllToInit.push_back(Value); |
| 1426 | break; |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | continue; |
| 1431 | } |
| 1432 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*Field)) { |
| 1433 | AllToInit.push_back(Value); |
| 1434 | continue; |
| 1435 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1436 | |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1437 | if ((*Field)->getType()->isDependentType()) |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame] | 1438 | continue; |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame] | 1439 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1440 | QualType FT = Context.getBaseElementType((*Field)->getType()); |
| 1441 | if (const RecordType* RT = FT->getAs<RecordType>()) { |
| 1442 | CXXConstructorDecl *Ctor = |
| 1443 | cast<CXXRecordDecl>(RT->getDecl())->getDefaultConstructor(Context); |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame] | 1444 | if (!Ctor) { |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1445 | Diag(Constructor->getLocation(), diag::err_missing_default_ctor) |
| 1446 | << (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl) |
| 1447 | << 1 << (*Field)->getDeclName(); |
| 1448 | Diag(Field->getLocation(), diag::note_field_decl); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1449 | Diag(RT->getDecl()->getLocation(), diag::note_previous_decl) |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1450 | << Context.getTagDeclType(RT->getDecl()); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1451 | HadError = true; |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1452 | continue; |
| 1453 | } |
Eli Friedman | e73d3bc | 2009-11-16 23:07:59 +0000 | [diff] [blame] | 1454 | |
| 1455 | if (FT.isConstQualified() && Ctor->isTrivial()) { |
| 1456 | Diag(Constructor->getLocation(), diag::err_unintialized_member_in_ctor) |
| 1457 | << (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl) |
| 1458 | << 1 << (*Field)->getDeclName(); |
| 1459 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 1460 | HadError = true; |
| 1461 | } |
| 1462 | |
| 1463 | // Don't create initializers for trivial constructors, since they don't |
| 1464 | // actually need to be run. |
| 1465 | if (Ctor->isTrivial()) |
| 1466 | continue; |
| 1467 | |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1468 | ASTOwningVector<&ActionBase::DeleteExpr> CtorArgs(*this); |
| 1469 | if (CompleteConstructorCall(Ctor, MultiExprArg(*this, 0, 0), |
| 1470 | Constructor->getLocation(), CtorArgs)) |
| 1471 | continue; |
| 1472 | |
Anders Carlsson | 8db68da | 2009-11-13 20:11:49 +0000 | [diff] [blame] | 1473 | // FIXME: CXXBaseOrMemberInitializer should only contain a single |
| 1474 | // subexpression so we can wrap it in a CXXExprWithTemporaries if necessary. |
| 1475 | ExprTemporaries.clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | CXXBaseOrMemberInitializer *Member = |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1477 | new (Context) CXXBaseOrMemberInitializer(Context, |
| 1478 | *Field, SourceLocation(), |
| 1479 | Ctor, |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1480 | SourceLocation(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1481 | CtorArgs.takeAs<Expr>(), |
| 1482 | CtorArgs.size(), |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1483 | SourceLocation()); |
| 1484 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1485 | AllToInit.push_back(Member); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1486 | MarkDeclarationReferenced(Constructor->getLocation(), Ctor); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1487 | } |
| 1488 | else if (FT->isReferenceType()) { |
| 1489 | Diag(Constructor->getLocation(), diag::err_unintialized_member_in_ctor) |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1490 | << (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl) |
| 1491 | << 0 << (*Field)->getDeclName(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1492 | Diag((*Field)->getLocation(), diag::note_declared_at); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1493 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1494 | } |
| 1495 | else if (FT.isConstQualified()) { |
| 1496 | Diag(Constructor->getLocation(), diag::err_unintialized_member_in_ctor) |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1497 | << (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl) |
| 1498 | << 1 << (*Field)->getDeclName(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1499 | Diag((*Field)->getLocation(), diag::note_declared_at); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1500 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1501 | } |
| 1502 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1504 | NumInitializers = AllToInit.size(); |
| 1505 | if (NumInitializers > 0) { |
| 1506 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1507 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1508 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1510 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
| 1511 | for (unsigned Idx = 0; Idx < NumInitializers; ++Idx) |
| 1512 | baseOrMemberInitializers[Idx] = AllToInit[Idx]; |
| 1513 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1514 | |
| 1515 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1518 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 1519 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1520 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1521 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 1522 | return static_cast<void *>(RT->getDecl()); |
| 1523 | } |
| 1524 | return static_cast<void *>(Field); |
| 1525 | } |
| 1526 | |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1527 | static void *GetKeyForBase(QualType BaseType) { |
| 1528 | if (const RecordType *RT = BaseType->getAs<RecordType>()) |
| 1529 | return (void *)RT; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1530 | |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1531 | assert(0 && "Unexpected base type!"); |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | static void *GetKeyForMember(CXXBaseOrMemberInitializer *Member, |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1536 | bool MemberMaybeAnon = false) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1537 | // For fields injected into the class via declaration of an anonymous union, |
| 1538 | // use its anonymous union class declaration as the unique key. |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1539 | if (Member->isMemberInitializer()) { |
| 1540 | FieldDecl *Field = Member->getMember(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1542 | // After SetBaseOrMemberInitializers call, Field is the anonymous union |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | // data member of the class. Data member used in the initializer list is |
Fariborz Jahanian | e649412 | 2009-08-11 18:49:54 +0000 | [diff] [blame] | 1544 | // in AnonUnionMember field. |
| 1545 | if (MemberMaybeAnon && Field->isAnonymousStructOrUnion()) |
| 1546 | Field = Member->getAnonUnionMember(); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1547 | if (Field->getDeclContext()->isRecord()) { |
| 1548 | RecordDecl *RD = cast<RecordDecl>(Field->getDeclContext()); |
| 1549 | if (RD->isAnonymousStructOrUnion()) |
| 1550 | return static_cast<void *>(RD); |
| 1551 | } |
| 1552 | return static_cast<void *>(Field); |
| 1553 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1555 | return GetKeyForBase(QualType(Member->getBaseClass(), 0)); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1558 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | void Sema::ActOnMemInitializers(DeclPtrTy ConstructorDecl, |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1560 | SourceLocation ColonLoc, |
| 1561 | MemInitTy **MemInits, unsigned NumMemInits) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1562 | if (!ConstructorDecl) |
| 1563 | return; |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1564 | |
| 1565 | AdjustDeclIfTemplate(ConstructorDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | |
| 1567 | CXXConstructorDecl *Constructor |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1568 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl.getAs<Decl>()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1570 | if (!Constructor) { |
| 1571 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 1572 | return; |
| 1573 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1575 | if (!Constructor->isDependentContext()) { |
| 1576 | llvm::DenseMap<void*, CXXBaseOrMemberInitializer *>Members; |
| 1577 | bool err = false; |
| 1578 | for (unsigned i = 0; i < NumMemInits; i++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | CXXBaseOrMemberInitializer *Member = |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1580 | static_cast<CXXBaseOrMemberInitializer*>(MemInits[i]); |
| 1581 | void *KeyToMember = GetKeyForMember(Member); |
| 1582 | CXXBaseOrMemberInitializer *&PrevMember = Members[KeyToMember]; |
| 1583 | if (!PrevMember) { |
| 1584 | PrevMember = Member; |
| 1585 | continue; |
| 1586 | } |
| 1587 | if (FieldDecl *Field = Member->getMember()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | Diag(Member->getSourceLocation(), |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1589 | diag::error_multiple_mem_initialization) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1590 | << Field->getNameAsString() |
| 1591 | << Member->getSourceRange(); |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1592 | else { |
| 1593 | Type *BaseClass = Member->getBaseClass(); |
| 1594 | assert(BaseClass && "ActOnMemInitializers - neither field or base"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | Diag(Member->getSourceLocation(), |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1596 | diag::error_multiple_base_initialization) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1597 | << QualType(BaseClass, 0) |
| 1598 | << Member->getSourceRange(); |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1599 | } |
| 1600 | Diag(PrevMember->getSourceLocation(), diag::note_previous_initializer) |
| 1601 | << 0; |
| 1602 | err = true; |
| 1603 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1605 | if (err) |
| 1606 | return; |
| 1607 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1609 | SetBaseOrMemberInitializers(Constructor, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | reinterpret_cast<CXXBaseOrMemberInitializer **>(MemInits), |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1611 | NumMemInits, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1612 | |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1613 | if (Constructor->isDependentContext()) |
| 1614 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1615 | |
| 1616 | if (Diags.getDiagnosticLevel(diag::warn_base_initialized) == |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1617 | Diagnostic::Ignored && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | Diags.getDiagnosticLevel(diag::warn_field_initialized) == |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1619 | Diagnostic::Ignored) |
| 1620 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1622 | // Also issue warning if order of ctor-initializer list does not match order |
| 1623 | // of 1) base class declarations and 2) order of non-static data members. |
| 1624 | llvm::SmallVector<const void*, 32> AllBaseOrMembers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1626 | CXXRecordDecl *ClassDecl |
| 1627 | = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 1628 | // Push virtual bases before others. |
| 1629 | for (CXXRecordDecl::base_class_iterator VBase = |
| 1630 | ClassDecl->vbases_begin(), |
| 1631 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1632 | AllBaseOrMembers.push_back(GetKeyForBase(VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1634 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1635 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1636 | // Virtuals are alread in the virtual base list and are constructed |
| 1637 | // first. |
| 1638 | if (Base->isVirtual()) |
| 1639 | continue; |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1640 | AllBaseOrMembers.push_back(GetKeyForBase(Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1641 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1643 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1644 | E = ClassDecl->field_end(); Field != E; ++Field) |
| 1645 | AllBaseOrMembers.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1647 | int Last = AllBaseOrMembers.size(); |
| 1648 | int curIndex = 0; |
| 1649 | CXXBaseOrMemberInitializer *PrevMember = 0; |
| 1650 | for (unsigned i = 0; i < NumMemInits; i++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | CXXBaseOrMemberInitializer *Member = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1652 | static_cast<CXXBaseOrMemberInitializer*>(MemInits[i]); |
| 1653 | void *MemberInCtorList = GetKeyForMember(Member, true); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1654 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1655 | for (; curIndex < Last; curIndex++) |
| 1656 | if (MemberInCtorList == AllBaseOrMembers[curIndex]) |
| 1657 | break; |
| 1658 | if (curIndex == Last) { |
| 1659 | assert(PrevMember && "Member not in member list?!"); |
| 1660 | // Initializer as specified in ctor-initializer list is out of order. |
| 1661 | // Issue a warning diagnostic. |
| 1662 | if (PrevMember->isBaseInitializer()) { |
| 1663 | // Diagnostics is for an initialized base class. |
| 1664 | Type *BaseClass = PrevMember->getBaseClass(); |
| 1665 | Diag(PrevMember->getSourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | diag::warn_base_initialized) |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 1667 | << QualType(BaseClass, 0); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1668 | } else { |
| 1669 | FieldDecl *Field = PrevMember->getMember(); |
| 1670 | Diag(PrevMember->getSourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | diag::warn_field_initialized) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1672 | << Field->getNameAsString(); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1673 | } |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1674 | // Also the note! |
| 1675 | if (FieldDecl *Field = Member->getMember()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | Diag(Member->getSourceLocation(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1677 | diag::note_fieldorbase_initialized_here) << 0 |
| 1678 | << Field->getNameAsString(); |
| 1679 | else { |
| 1680 | Type *BaseClass = Member->getBaseClass(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | Diag(Member->getSourceLocation(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1682 | diag::note_fieldorbase_initialized_here) << 1 |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 1683 | << QualType(BaseClass, 0); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1684 | } |
| 1685 | for (curIndex = 0; curIndex < Last; curIndex++) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | if (MemberInCtorList == AllBaseOrMembers[curIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1687 | break; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1688 | } |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1689 | PrevMember = Member; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1690 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1693 | void |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 1694 | Sema::MarkBaseAndMemberDestructorsReferenced(CXXDestructorDecl *Destructor) { |
| 1695 | // Ignore dependent destructors. |
| 1696 | if (Destructor->isDependentContext()) |
| 1697 | return; |
| 1698 | |
| 1699 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 1701 | // Non-static data members. |
| 1702 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 1703 | E = ClassDecl->field_end(); I != E; ++I) { |
| 1704 | FieldDecl *Field = *I; |
| 1705 | |
| 1706 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 1707 | |
| 1708 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 1709 | if (!RT) |
| 1710 | continue; |
| 1711 | |
| 1712 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1713 | if (FieldClassDecl->hasTrivialDestructor()) |
| 1714 | continue; |
| 1715 | |
| 1716 | const CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor(Context); |
| 1717 | MarkDeclarationReferenced(Destructor->getLocation(), |
| 1718 | const_cast<CXXDestructorDecl*>(Dtor)); |
| 1719 | } |
| 1720 | |
| 1721 | // Bases. |
| 1722 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1723 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1724 | // Ignore virtual bases. |
| 1725 | if (Base->isVirtual()) |
| 1726 | continue; |
| 1727 | |
| 1728 | // Ignore trivial destructors. |
| 1729 | CXXRecordDecl *BaseClassDecl |
| 1730 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 1731 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1732 | continue; |
| 1733 | |
| 1734 | const CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context); |
| 1735 | MarkDeclarationReferenced(Destructor->getLocation(), |
| 1736 | const_cast<CXXDestructorDecl*>(Dtor)); |
| 1737 | } |
| 1738 | |
| 1739 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1740 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 1741 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 1742 | // Ignore trivial destructors. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1743 | CXXRecordDecl *BaseClassDecl |
| 1744 | = cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl()); |
| 1745 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1746 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 1747 | |
| 1748 | const CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context); |
| 1749 | MarkDeclarationReferenced(Destructor->getLocation(), |
| 1750 | const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1751 | } |
| 1752 | } |
| 1753 | |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 1754 | void Sema::ActOnDefaultCtorInitializers(DeclPtrTy CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 1755 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 1756 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1758 | AdjustDeclIfTemplate(CDtorDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | |
| 1760 | if (CXXConstructorDecl *Constructor |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 1761 | = dyn_cast<CXXConstructorDecl>(CDtorDecl.getAs<Decl>())) |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1762 | SetBaseOrMemberInitializers(Constructor, 0, 0, false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1765 | namespace { |
| 1766 | /// PureVirtualMethodCollector - traverses a class and its superclasses |
| 1767 | /// and determines if it has any pure virtual methods. |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 1768 | class PureVirtualMethodCollector { |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1769 | ASTContext &Context; |
| 1770 | |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 1771 | public: |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1772 | typedef llvm::SmallVector<const CXXMethodDecl*, 8> MethodList; |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 1773 | |
| 1774 | private: |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1775 | MethodList Methods; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1776 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1777 | void Collect(const CXXRecordDecl* RD, MethodList& Methods); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1779 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1780 | PureVirtualMethodCollector(ASTContext &Ctx, const CXXRecordDecl* RD) |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1781 | : Context(Ctx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1783 | MethodList List; |
| 1784 | Collect(RD, List); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1786 | // Copy the temporary list to methods, and make sure to ignore any |
| 1787 | // null entries. |
| 1788 | for (size_t i = 0, e = List.size(); i != e; ++i) { |
| 1789 | if (List[i]) |
| 1790 | Methods.push_back(List[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | } |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1794 | bool empty() const { return Methods.empty(); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1795 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1796 | MethodList::const_iterator methods_begin() { return Methods.begin(); } |
| 1797 | MethodList::const_iterator methods_end() { return Methods.end(); } |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1798 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | |
| 1800 | void PureVirtualMethodCollector::Collect(const CXXRecordDecl* RD, |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1801 | MethodList& Methods) { |
| 1802 | // First, collect the pure virtual methods for the base classes. |
| 1803 | for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(), |
| 1804 | BaseEnd = RD->bases_end(); Base != BaseEnd; ++Base) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1805 | if (const RecordType *RT = Base->getType()->getAs<RecordType>()) { |
Chris Lattner | 64540d7 | 2009-03-29 05:01:10 +0000 | [diff] [blame] | 1806 | const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1807 | if (BaseDecl && BaseDecl->isAbstract()) |
| 1808 | Collect(BaseDecl, Methods); |
| 1809 | } |
| 1810 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1812 | // Next, zero out any pure virtual methods that this class overrides. |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1813 | typedef llvm::SmallPtrSet<const CXXMethodDecl*, 4> MethodSetTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1814 | |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1815 | MethodSetTy OverriddenMethods; |
| 1816 | size_t MethodsSize = Methods.size(); |
| 1817 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | for (RecordDecl::decl_iterator i = RD->decls_begin(), e = RD->decls_end(); |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1819 | i != e; ++i) { |
| 1820 | // Traverse the record, looking for methods. |
| 1821 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*i)) { |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 1822 | // If the method is pure virtual, add it to the methods vector. |
Anders Carlsson | 2782302 | 2009-10-18 19:34:08 +0000 | [diff] [blame] | 1823 | if (MD->isPure()) |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1824 | Methods.push_back(MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1825 | |
Anders Carlsson | 2782302 | 2009-10-18 19:34:08 +0000 | [diff] [blame] | 1826 | // Record all the overridden methods in our set. |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1827 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 1828 | E = MD->end_overridden_methods(); I != E; ++I) { |
| 1829 | // Keep track of the overridden methods. |
| 1830 | OverriddenMethods.insert(*I); |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1831 | } |
| 1832 | } |
| 1833 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | |
| 1835 | // Now go through the methods and zero out all the ones we know are |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1836 | // overridden. |
| 1837 | for (size_t i = 0, e = MethodsSize; i != e; ++i) { |
| 1838 | if (OverriddenMethods.count(Methods[i])) |
| 1839 | Methods[i] = 0; |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1840 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1842 | } |
| 1843 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1844 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1845 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1847 | unsigned DiagID, AbstractDiagSelID SelID, |
| 1848 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1849 | if (SelID == -1) |
| 1850 | return RequireNonAbstractType(Loc, T, |
| 1851 | PDiag(DiagID), CurrentRD); |
| 1852 | else |
| 1853 | return RequireNonAbstractType(Loc, T, |
| 1854 | PDiag(DiagID) << SelID, CurrentRD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1857 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
| 1858 | const PartialDiagnostic &PD, |
| 1859 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1860 | if (!getLangOptions().CPlusPlus) |
| 1861 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 1863 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1864 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1865 | CurrentRD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1866 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1867 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1868 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1869 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1870 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1871 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1872 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1873 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, CurrentRD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1874 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1876 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1877 | if (!RT) |
| 1878 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1880 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 1881 | if (!RD) |
| 1882 | return false; |
| 1883 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1884 | if (CurrentRD && CurrentRD != RD) |
| 1885 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1886 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1887 | if (!RD->isAbstract()) |
| 1888 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1890 | Diag(Loc, PD) << RD->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1891 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1892 | // Check if we've already emitted the list of pure virtual functions for this |
| 1893 | // class. |
| 1894 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
| 1895 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1896 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1897 | PureVirtualMethodCollector Collector(Context, RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1898 | |
| 1899 | for (PureVirtualMethodCollector::MethodList::const_iterator I = |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1900 | Collector.methods_begin(), E = Collector.methods_end(); I != E; ++I) { |
| 1901 | const CXXMethodDecl *MD = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1902 | |
| 1903 | Diag(MD->getLocation(), diag::note_pure_virtual_function) << |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1904 | MD->getDeclName(); |
| 1905 | } |
| 1906 | |
| 1907 | if (!PureVirtualClassDiagSet) |
| 1908 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 1909 | PureVirtualClassDiagSet->insert(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1911 | return true; |
| 1912 | } |
| 1913 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1914 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 1915 | class AbstractClassUsageDiagnoser |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1916 | : public DeclVisitor<AbstractClassUsageDiagnoser, bool> { |
| 1917 | Sema &SemaRef; |
| 1918 | CXXRecordDecl *AbstractClass; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1920 | bool VisitDeclContext(const DeclContext *DC) { |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1921 | bool Invalid = false; |
| 1922 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1923 | for (CXXRecordDecl::decl_iterator I = DC->decls_begin(), |
| 1924 | E = DC->decls_end(); I != E; ++I) |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1925 | Invalid |= Visit(*I); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1926 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1927 | return Invalid; |
| 1928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1930 | public: |
| 1931 | AbstractClassUsageDiagnoser(Sema& SemaRef, CXXRecordDecl *ac) |
| 1932 | : SemaRef(SemaRef), AbstractClass(ac) { |
| 1933 | Visit(SemaRef.Context.getTranslationUnitDecl()); |
| 1934 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1935 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1936 | bool VisitFunctionDecl(const FunctionDecl *FD) { |
| 1937 | if (FD->isThisDeclarationADefinition()) { |
| 1938 | // No need to do the check if we're in a definition, because it requires |
| 1939 | // that the return/param types are complete. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1940 | // because that requires |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1941 | return VisitDeclContext(FD); |
| 1942 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1944 | // Check the return type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1945 | QualType RTy = FD->getType()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | bool Invalid = |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1947 | SemaRef.RequireNonAbstractType(FD->getLocation(), RTy, |
| 1948 | diag::err_abstract_type_in_decl, |
| 1949 | Sema::AbstractReturnType, |
| 1950 | AbstractClass); |
| 1951 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1953 | E = FD->param_end(); I != E; ++I) { |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1954 | const ParmVarDecl *VD = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1955 | Invalid |= |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1956 | SemaRef.RequireNonAbstractType(VD->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | VD->getOriginalType(), |
| 1958 | diag::err_abstract_type_in_decl, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1959 | Sema::AbstractParamType, |
| 1960 | AbstractClass); |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | return Invalid; |
| 1964 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1966 | bool VisitDecl(const Decl* D) { |
| 1967 | if (const DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 1968 | return VisitDeclContext(DC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1970 | return false; |
| 1971 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1972 | }; |
| 1973 | } |
| 1974 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 1975 | /// \brief Perform semantic checks on a class definition that has been |
| 1976 | /// completing, introducing implicitly-declared members, checking for |
| 1977 | /// abstract types, etc. |
| 1978 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
| 1979 | if (!Record || Record->isInvalidDecl()) |
| 1980 | return; |
| 1981 | |
Eli Friedman | ff2d878 | 2009-12-16 20:00:27 +0000 | [diff] [blame^] | 1982 | if (!Record->isDependentType()) |
| 1983 | AddImplicitlyDeclaredMembersToClass(Record); |
| 1984 | |
| 1985 | if (Record->isInvalidDecl()) |
| 1986 | return; |
| 1987 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 1988 | if (!Record->isAbstract()) { |
| 1989 | // Collect all the pure virtual methods and see if this is an abstract |
| 1990 | // class after all. |
| 1991 | PureVirtualMethodCollector Collector(Context, Record); |
| 1992 | if (!Collector.empty()) |
| 1993 | Record->setAbstract(true); |
| 1994 | } |
| 1995 | |
| 1996 | if (Record->isAbstract()) |
| 1997 | (void)AbstractClassUsageDiagnoser(*this, Record); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2000 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2001 | DeclPtrTy TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2002 | SourceLocation LBrac, |
| 2003 | SourceLocation RBrac) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2004 | if (!TagDecl) |
| 2005 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2007 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2008 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2009 | ActOnFields(S, RLoc, TagDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2010 | (DeclPtrTy*)FieldCollector->getCurFields(), |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2011 | FieldCollector->getCurNumFields(), LBrac, RBrac, 0); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2012 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2013 | CheckCompletedCXXClass( |
| 2014 | dyn_cast_or_null<CXXRecordDecl>(TagDecl.getAs<Decl>())); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2017 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 2018 | /// special functions, such as the default constructor, copy |
| 2019 | /// constructor, or destructor, to the given C++ class (C++ |
| 2020 | /// [special]p1). This routine can only be executed just before the |
| 2021 | /// definition of the class is complete. |
| 2022 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2023 | CanQualType ClassType |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2024 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2025 | |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2026 | // FIXME: Implicit declarations have exception specifications, which are |
| 2027 | // the union of the specifications of the implicitly called functions. |
| 2028 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2029 | if (!ClassDecl->hasUserDeclaredConstructor()) { |
| 2030 | // C++ [class.ctor]p5: |
| 2031 | // A default constructor for a class X is a constructor of class X |
| 2032 | // that can be called without an argument. If there is no |
| 2033 | // user-declared constructor for class X, a default constructor is |
| 2034 | // implicitly declared. An implicitly-declared default constructor |
| 2035 | // is an inline public member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2037 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | CXXConstructorDecl *DefaultCon = |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2039 | CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2040 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2041 | Context.getFunctionType(Context.VoidTy, |
| 2042 | 0, 0, false, 0), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2043 | /*TInfo=*/0, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2044 | /*isExplicit=*/false, |
| 2045 | /*isInline=*/true, |
| 2046 | /*isImplicitlyDeclared=*/true); |
| 2047 | DefaultCon->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2048 | DefaultCon->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2049 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2050 | ClassDecl->addDecl(DefaultCon); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
| 2054 | // C++ [class.copy]p4: |
| 2055 | // If the class definition does not explicitly declare a copy |
| 2056 | // constructor, one is declared implicitly. |
| 2057 | |
| 2058 | // C++ [class.copy]p5: |
| 2059 | // The implicitly-declared copy constructor for a class X will |
| 2060 | // have the form |
| 2061 | // |
| 2062 | // X::X(const X&) |
| 2063 | // |
| 2064 | // if |
| 2065 | bool HasConstCopyConstructor = true; |
| 2066 | |
| 2067 | // -- each direct or virtual base class B of X has a copy |
| 2068 | // constructor whose first parameter is of type const B& or |
| 2069 | // const volatile B&, and |
| 2070 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2071 | HasConstCopyConstructor && Base != ClassDecl->bases_end(); ++Base) { |
| 2072 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2073 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2074 | HasConstCopyConstructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2075 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 2076 | } |
| 2077 | |
| 2078 | // -- for all the nonstatic data members of X that are of a |
| 2079 | // class type M (or array thereof), each such class type |
| 2080 | // has a copy constructor whose first parameter is of type |
| 2081 | // const M& or const volatile M&. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2082 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 2083 | HasConstCopyConstructor && Field != ClassDecl->field_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2084 | ++Field) { |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2085 | QualType FieldType = (*Field)->getType(); |
| 2086 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2087 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2088 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | const CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2090 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | HasConstCopyConstructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2092 | = FieldClassDecl->hasConstCopyConstructor(Context); |
| 2093 | } |
| 2094 | } |
| 2095 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2096 | // Otherwise, the implicitly declared copy constructor will have |
| 2097 | // the form |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2098 | // |
| 2099 | // X::X(X&) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2100 | QualType ArgType = ClassType; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2101 | if (HasConstCopyConstructor) |
| 2102 | ArgType = ArgType.withConst(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2103 | ArgType = Context.getLValueReferenceType(ArgType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2104 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2105 | // An implicitly-declared copy constructor is an inline public |
| 2106 | // member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2108 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2109 | CXXConstructorDecl *CopyConstructor |
| 2110 | = CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2111 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2112 | Context.getFunctionType(Context.VoidTy, |
| 2113 | &ArgType, 1, |
| 2114 | false, 0), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2115 | /*TInfo=*/0, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2116 | /*isExplicit=*/false, |
| 2117 | /*isInline=*/true, |
| 2118 | /*isImplicitlyDeclared=*/true); |
| 2119 | CopyConstructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2120 | CopyConstructor->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2121 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2122 | |
| 2123 | // Add the parameter to the constructor. |
| 2124 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 2125 | ClassDecl->getLocation(), |
| 2126 | /*IdentifierInfo=*/0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2127 | ArgType, /*TInfo=*/0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2128 | VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 2129 | CopyConstructor->setParams(Context, &FromParam, 1); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2130 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2133 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 2134 | // Note: The following rules are largely analoguous to the copy |
| 2135 | // constructor rules. Note that virtual bases are not taken into account |
| 2136 | // for determining the argument type of the operator. Note also that |
| 2137 | // operators taking an object instead of a reference are allowed. |
| 2138 | // |
| 2139 | // C++ [class.copy]p10: |
| 2140 | // If the class definition does not explicitly declare a copy |
| 2141 | // assignment operator, one is declared implicitly. |
| 2142 | // The implicitly-defined copy assignment operator for a class X |
| 2143 | // will have the form |
| 2144 | // |
| 2145 | // X& X::operator=(const X&) |
| 2146 | // |
| 2147 | // if |
| 2148 | bool HasConstCopyAssignment = true; |
| 2149 | |
| 2150 | // -- each direct base class B of X has a copy assignment operator |
| 2151 | // whose parameter is of type const B&, const volatile B& or B, |
| 2152 | // and |
| 2153 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2154 | HasConstCopyAssignment && Base != ClassDecl->bases_end(); ++Base) { |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 2155 | assert(!Base->getType()->isDependentType() && |
| 2156 | "Cannot generate implicit members for class with dependent bases."); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2157 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2158 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2159 | const CXXMethodDecl *MD = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2160 | HasConstCopyAssignment = BaseClassDecl->hasConstCopyAssignment(Context, |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2161 | MD); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | // -- for all the nonstatic data members of X that are of a class |
| 2165 | // type M (or array thereof), each such class type has a copy |
| 2166 | // assignment operator whose parameter is of type const M&, |
| 2167 | // const volatile M& or M. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2168 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 2169 | HasConstCopyAssignment && Field != ClassDecl->field_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2170 | ++Field) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2171 | QualType FieldType = (*Field)->getType(); |
| 2172 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2173 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2174 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2175 | const CXXRecordDecl *FieldClassDecl |
| 2176 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2177 | const CXXMethodDecl *MD = 0; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2178 | HasConstCopyAssignment |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2179 | = FieldClassDecl->hasConstCopyAssignment(Context, MD); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | // Otherwise, the implicitly declared copy assignment operator will |
| 2184 | // have the form |
| 2185 | // |
| 2186 | // X& X::operator=(X&) |
| 2187 | QualType ArgType = ClassType; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2188 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2189 | if (HasConstCopyAssignment) |
| 2190 | ArgType = ArgType.withConst(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2191 | ArgType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2192 | |
| 2193 | // An implicitly-declared copy assignment operator is an inline public |
| 2194 | // member of its class. |
| 2195 | DeclarationName Name = |
| 2196 | Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 2197 | CXXMethodDecl *CopyAssignment = |
| 2198 | CXXMethodDecl::Create(Context, ClassDecl, ClassDecl->getLocation(), Name, |
| 2199 | Context.getFunctionType(RetType, &ArgType, 1, |
| 2200 | false, 0), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2201 | /*TInfo=*/0, /*isStatic=*/false, /*isInline=*/true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2202 | CopyAssignment->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2203 | CopyAssignment->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2204 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 2205 | CopyAssignment->setCopyAssignment(true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2206 | |
| 2207 | // Add the parameter to the operator. |
| 2208 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 2209 | ClassDecl->getLocation(), |
| 2210 | /*IdentifierInfo=*/0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2211 | ArgType, /*TInfo=*/0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2212 | VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 2213 | CopyAssignment->setParams(Context, &FromParam, 1); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2214 | |
| 2215 | // Don't call addedAssignmentOperator. There is no way to distinguish an |
| 2216 | // implicit from an explicit assignment operator. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2217 | ClassDecl->addDecl(CopyAssignment); |
Eli Friedman | ca6affd | 2009-12-02 06:59:20 +0000 | [diff] [blame] | 2218 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2221 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2222 | // C++ [class.dtor]p2: |
| 2223 | // If a class has no user-declared destructor, a destructor is |
| 2224 | // declared implicitly. An implicitly-declared destructor is an |
| 2225 | // inline public member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2226 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2227 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2228 | CXXDestructorDecl *Destructor |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2229 | = CXXDestructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2230 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2231 | Context.getFunctionType(Context.VoidTy, |
| 2232 | 0, 0, false, 0), |
| 2233 | /*isInline=*/true, |
| 2234 | /*isImplicitlyDeclared=*/true); |
| 2235 | Destructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2236 | Destructor->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2237 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2238 | ClassDecl->addDecl(Destructor); |
Anders Carlsson | d5a942b | 2009-11-26 21:25:09 +0000 | [diff] [blame] | 2239 | |
| 2240 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2241 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2244 | void Sema::ActOnReenterTemplateScope(Scope *S, DeclPtrTy TemplateD) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 2245 | Decl *D = TemplateD.getAs<Decl>(); |
| 2246 | if (!D) |
| 2247 | return; |
| 2248 | |
| 2249 | TemplateParameterList *Params = 0; |
| 2250 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 2251 | Params = Template->getTemplateParameters(); |
| 2252 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2253 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 2254 | Params = PartialSpec->getTemplateParameters(); |
| 2255 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2256 | return; |
| 2257 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2258 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2259 | ParamEnd = Params->end(); |
| 2260 | Param != ParamEnd; ++Param) { |
| 2261 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 2262 | if (Named->getDeclName()) { |
| 2263 | S->AddDecl(DeclPtrTy::make(Named)); |
| 2264 | IdResolver.AddDecl(Named); |
| 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2269 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 2270 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 2271 | /// parsing those parts of the given Method declaration that could |
| 2272 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 2273 | /// arguments. This action should enter the scope of the given |
| 2274 | /// Method declaration as if we had just parsed the qualified method |
| 2275 | /// name. However, it should not bring the parameters into scope; |
| 2276 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2277 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2278 | if (!MethodD) |
| 2279 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2280 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2281 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2282 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2283 | CXXScopeSpec SS; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2284 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2285 | QualType ClassTy |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2286 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 2287 | SS.setScopeRep( |
| 2288 | NestedNameSpecifier::Create(Context, 0, false, ClassTy.getTypePtr())); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2289 | ActOnCXXEnterDeclaratorScope(S, SS); |
| 2290 | } |
| 2291 | |
| 2292 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 2293 | /// C++ method declaration. We're (re-)introducing the given |
| 2294 | /// function parameter into scope for use in parsing later parts of |
| 2295 | /// the method declaration. For example, we could see an |
| 2296 | /// ActOnParamDefaultArgument event for this parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2297 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, DeclPtrTy ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2298 | if (!ParamD) |
| 2299 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2300 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2301 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2302 | |
| 2303 | // If this parameter has an unparsed default argument, clear it out |
| 2304 | // to make way for the parsed default argument. |
| 2305 | if (Param->hasUnparsedDefaultArg()) |
| 2306 | Param->setDefaultArg(0); |
| 2307 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2308 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2309 | if (Param->getDeclName()) |
| 2310 | IdResolver.AddDecl(Param); |
| 2311 | } |
| 2312 | |
| 2313 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 2314 | /// processing the delayed method declaration for Method. The method |
| 2315 | /// declaration is now considered finished. There may be a separate |
| 2316 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 2317 | /// immediately!) for this method, if it was also defined inside the |
| 2318 | /// class body. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2319 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2320 | if (!MethodD) |
| 2321 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2322 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2323 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2325 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2326 | CXXScopeSpec SS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2327 | QualType ClassTy |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2328 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 2329 | SS.setScopeRep( |
| 2330 | NestedNameSpecifier::Create(Context, 0, false, ClassTy.getTypePtr())); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2331 | ActOnCXXExitDeclaratorScope(S, SS); |
| 2332 | |
| 2333 | // Now that we have our default arguments, check the constructor |
| 2334 | // again. It could produce additional diagnostics or affect whether |
| 2335 | // the class has implicitly-declared destructors, among other |
| 2336 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2337 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 2338 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2339 | |
| 2340 | // Check the default arguments, which we may have added. |
| 2341 | if (!Method->isInvalidDecl()) |
| 2342 | CheckCXXDefaultArguments(Method); |
| 2343 | } |
| 2344 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2345 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2346 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2347 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2348 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 2349 | /// will be updated to reflect a well-formed type for the constructor and |
| 2350 | /// returned. |
| 2351 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
| 2352 | FunctionDecl::StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2353 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2354 | |
| 2355 | // C++ [class.ctor]p3: |
| 2356 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 2357 | // constructor can be invoked for a const, volatile or const |
| 2358 | // volatile object. A constructor shall not be declared const, |
| 2359 | // volatile, or const volatile (9.3.2). |
| 2360 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2361 | if (!D.isInvalidType()) |
| 2362 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2363 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 2364 | << SourceRange(D.getIdentifierLoc()); |
| 2365 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2366 | } |
| 2367 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2368 | if (!D.isInvalidType()) |
| 2369 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2370 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2371 | << SourceRange(D.getIdentifierLoc()); |
| 2372 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2373 | SC = FunctionDecl::None; |
| 2374 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2375 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2376 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2377 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2378 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2379 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2380 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2381 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2382 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2383 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2384 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2385 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2386 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2387 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2388 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2389 | // Rebuild the function type "R" without any type qualifiers (in |
| 2390 | // case any of the errors above fired) and with "void" as the |
| 2391 | // return type, since constructors don't have return types. We |
| 2392 | // *always* have to do this, because GetTypeForDeclarator will |
| 2393 | // put in a result type of "int" when none was specified. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2394 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2395 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 2396 | Proto->getNumArgs(), |
| 2397 | Proto->isVariadic(), 0); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2400 | /// CheckConstructor - Checks a fully-formed constructor for |
| 2401 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 2402 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2403 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2405 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 2406 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2407 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2408 | |
| 2409 | // C++ [class.copy]p3: |
| 2410 | // A declaration of a constructor for a class X is ill-formed if |
| 2411 | // its first parameter is of type (optionally cv-qualified) X and |
| 2412 | // either there are no other parameters or else all other |
| 2413 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2414 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2415 | ((Constructor->getNumParams() == 1) || |
| 2416 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2417 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 2418 | Constructor->getTemplateSpecializationKind() |
| 2419 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2420 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 2421 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 2422 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2423 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
| 2424 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 2425 | << CodeModificationHint::CreateInsertion(ParamLoc, " const &"); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2426 | |
| 2427 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 2428 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2429 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2430 | } |
| 2431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2432 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2433 | // Notify the class that we've added a constructor. |
| 2434 | ClassDecl->addedConstructor(Context, Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2437 | /// CheckDestructor - Checks a fully-formed destructor for well-formedness, |
| 2438 | /// issuing any diagnostics required. Returns true on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2439 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2440 | CXXRecordDecl *RD = Destructor->getParent(); |
| 2441 | |
| 2442 | if (Destructor->isVirtual()) { |
| 2443 | SourceLocation Loc; |
| 2444 | |
| 2445 | if (!Destructor->isImplicit()) |
| 2446 | Loc = Destructor->getLocation(); |
| 2447 | else |
| 2448 | Loc = RD->getLocation(); |
| 2449 | |
| 2450 | // If we have a virtual destructor, look up the deallocation function |
| 2451 | FunctionDecl *OperatorDelete = 0; |
| 2452 | DeclarationName Name = |
| 2453 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2454 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2455 | return true; |
| 2456 | |
| 2457 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2458 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2459 | |
| 2460 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2463 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2464 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 2465 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 2466 | FTI.ArgInfo[0].Param && |
| 2467 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType()); |
| 2468 | } |
| 2469 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2470 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 2471 | /// the well-formednes of the destructor declarator @p D with type @p |
| 2472 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2473 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 2474 | /// will be updated to reflect a well-formed type for the destructor and |
| 2475 | /// returned. |
| 2476 | QualType Sema::CheckDestructorDeclarator(Declarator &D, |
| 2477 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2478 | // C++ [class.dtor]p1: |
| 2479 | // [...] A typedef-name that names a class is a class-name |
| 2480 | // (7.1.3); however, a typedef-name that names a class shall not |
| 2481 | // be used as the identifier in the declarator for a destructor |
| 2482 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2483 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2484 | if (isa<TypedefType>(DeclaratorType)) { |
| 2485 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2486 | << DeclaratorType; |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2487 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | // C++ [class.dtor]p2: |
| 2491 | // A destructor is used to destroy objects of its class type. A |
| 2492 | // destructor takes no parameters, and no return type can be |
| 2493 | // specified for it (not even void). The address of a destructor |
| 2494 | // shall not be taken. A destructor shall not be static. A |
| 2495 | // destructor can be invoked for a const, volatile or const |
| 2496 | // volatile object. A destructor shall not be declared const, |
| 2497 | // volatile or const volatile (9.3.2). |
| 2498 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2499 | if (!D.isInvalidType()) |
| 2500 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 2501 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2502 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2503 | SC = FunctionDecl::None; |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2504 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2505 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2506 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2507 | // Destructors don't have return types, but the parser will |
| 2508 | // happily parse something like: |
| 2509 | // |
| 2510 | // class X { |
| 2511 | // float ~X(); |
| 2512 | // }; |
| 2513 | // |
| 2514 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2515 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 2516 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2517 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2518 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2519 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2520 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2521 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2522 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2523 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2524 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2525 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2526 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2527 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2528 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2529 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2530 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2531 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2535 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2536 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 2537 | |
| 2538 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2539 | FTI.freeArgs(); |
| 2540 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2543 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2544 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2545 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2546 | D.setInvalidType(); |
| 2547 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2548 | |
| 2549 | // Rebuild the function type "R" without any type qualifiers or |
| 2550 | // parameters (in case any of the errors above fired) and with |
| 2551 | // "void" as the return type, since destructors don't have return |
| 2552 | // types. We *always* have to do this, because GetTypeForDeclarator |
| 2553 | // will put in a result type of "int" when none was specified. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2554 | return Context.getFunctionType(Context.VoidTy, 0, 0, false, 0); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2557 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 2558 | /// well-formednes of the conversion function declarator @p D with |
| 2559 | /// type @p R. If there are any errors in the declarator, this routine |
| 2560 | /// will emit diagnostics and return true. Otherwise, it will return |
| 2561 | /// false. Either way, the type @p R will be updated to reflect a |
| 2562 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2563 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2564 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2565 | // C++ [class.conv.fct]p1: |
| 2566 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2567 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2568 | // parameter returning conversion-type-id." |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2569 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2570 | if (!D.isInvalidType()) |
| 2571 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 2572 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2573 | << SourceRange(D.getIdentifierLoc()); |
| 2574 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2575 | SC = FunctionDecl::None; |
| 2576 | } |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2577 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2578 | // Conversion functions don't have return types, but the parser will |
| 2579 | // happily parse something like: |
| 2580 | // |
| 2581 | // class X { |
| 2582 | // float operator bool(); |
| 2583 | // }; |
| 2584 | // |
| 2585 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2586 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 2587 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2588 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | // Make sure we don't have any parameters. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2592 | if (R->getAs<FunctionProtoType>()->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2593 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 2594 | |
| 2595 | // Delete the parameters. |
Chris Lattner | 1833a83 | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 2596 | D.getTypeObject(0).Fun.freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2597 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2600 | // Make sure the conversion function isn't variadic. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2601 | if (R->getAs<FunctionProtoType>()->isVariadic() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2602 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2603 | D.setInvalidType(); |
| 2604 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2605 | |
| 2606 | // C++ [class.conv.fct]p4: |
| 2607 | // The conversion-type-id shall not represent a function type nor |
| 2608 | // an array type. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2609 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2610 | if (ConvType->isArrayType()) { |
| 2611 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 2612 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2613 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2614 | } else if (ConvType->isFunctionType()) { |
| 2615 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 2616 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2617 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | // Rebuild the function type "R" without any parameters (in case any |
| 2621 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2622 | // return type. |
| 2623 | R = Context.getFunctionType(ConvType, 0, 0, false, |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2624 | R->getAs<FunctionProtoType>()->getTypeQuals()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2625 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2626 | // C++0x explicit conversion operators. |
| 2627 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2628 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2629 | diag::warn_explicit_conversion_functions) |
| 2630 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2633 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 2634 | /// the declaration of the given C++ conversion function. This routine |
| 2635 | /// is responsible for recording the conversion function in the C++ |
| 2636 | /// class, if possible. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2637 | Sema::DeclPtrTy Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2638 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 2639 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 2640 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2641 | |
| 2642 | // Make sure we aren't redeclaring the conversion function. |
| 2643 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2644 | |
| 2645 | // C++ [class.conv.fct]p1: |
| 2646 | // [...] A conversion function is never used to convert a |
| 2647 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 2648 | // same object type (or a reference to it), to a (possibly |
| 2649 | // cv-qualified) base class of that type (or a reference to it), |
| 2650 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2651 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 2652 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2654 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2655 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2656 | ConvType = ConvTypeRef->getPointeeType(); |
| 2657 | if (ConvType->isRecordType()) { |
| 2658 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 2659 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2660 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2661 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2662 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2663 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2664 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2665 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2666 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2667 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2670 | if (Conversion->getPreviousDeclaration()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2671 | const NamedDecl *ExpectedPrevDecl = Conversion->getPreviousDeclaration(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2672 | if (FunctionTemplateDecl *ConversionTemplate |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2673 | = Conversion->getDescribedFunctionTemplate()) |
| 2674 | ExpectedPrevDecl = ConversionTemplate->getPreviousDeclaration(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2675 | if (ClassDecl->replaceConversion(ExpectedPrevDecl, Conversion)) |
| 2676 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2677 | assert(Conversion->isInvalidDecl() && "Conversion should not get here."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2678 | } else if (FunctionTemplateDecl *ConversionTemplate |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2679 | = Conversion->getDescribedFunctionTemplate()) |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 2680 | ClassDecl->addConversionFunction(ConversionTemplate); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2681 | else if (!Conversion->getPrimaryTemplate()) // ignore specializations |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 2682 | ClassDecl->addConversionFunction(Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2683 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2684 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2687 | //===----------------------------------------------------------------------===// |
| 2688 | // Namespace Handling |
| 2689 | //===----------------------------------------------------------------------===// |
| 2690 | |
| 2691 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 2692 | /// definition. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2693 | Sema::DeclPtrTy Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 2694 | SourceLocation IdentLoc, |
| 2695 | IdentifierInfo *II, |
| 2696 | SourceLocation LBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2697 | NamespaceDecl *Namespc = |
| 2698 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 2699 | Namespc->setLBracLoc(LBrace); |
| 2700 | |
| 2701 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 2702 | |
| 2703 | if (II) { |
| 2704 | // C++ [namespace.def]p2: |
| 2705 | // The identifier in an original-namespace-definition shall not have been |
| 2706 | // previously defined in the declarative region in which the |
| 2707 | // original-namespace-definition appears. The identifier in an |
| 2708 | // original-namespace-definition is the name of the namespace. Subsequently |
| 2709 | // in that declarative region, it is treated as an original-namespace-name. |
| 2710 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2711 | NamedDecl *PrevDecl |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2712 | = LookupSingleName(DeclRegionScope, II, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 2713 | ForRedeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2715 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 2716 | // This is an extended namespace definition. |
| 2717 | // Attach this namespace decl to the chain of extended namespace |
| 2718 | // definitions. |
| 2719 | OrigNS->setNextNamespace(Namespc); |
| 2720 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2721 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | // Remove the previous declaration from the scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2723 | if (DeclRegionScope->isDeclScope(DeclPtrTy::make(OrigNS))) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 2724 | IdResolver.RemoveDecl(OrigNS); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2725 | DeclRegionScope->RemoveDecl(DeclPtrTy::make(OrigNS)); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2726 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2727 | } else if (PrevDecl) { |
| 2728 | // This is an invalid name redefinition. |
| 2729 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 2730 | << Namespc->getDeclName(); |
| 2731 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 2732 | Namespc->setInvalidDecl(); |
| 2733 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2734 | } else if (II->isStr("std") && |
| 2735 | CurContext->getLookupContext()->isTranslationUnit()) { |
| 2736 | // This is the first "real" definition of the namespace "std", so update |
| 2737 | // our cache of the "std" namespace to point at this definition. |
| 2738 | if (StdNamespace) { |
| 2739 | // We had already defined a dummy namespace "std". Link this new |
| 2740 | // namespace definition to the dummy namespace "std". |
| 2741 | StdNamespace->setNextNamespace(Namespc); |
| 2742 | StdNamespace->setLocation(IdentLoc); |
| 2743 | Namespc->setOriginalNamespace(StdNamespace->getOriginalNamespace()); |
| 2744 | } |
| 2745 | |
| 2746 | // Make our StdNamespace cache point at the first real definition of the |
| 2747 | // "std" namespace. |
| 2748 | StdNamespace = Namespc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2749 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2750 | |
| 2751 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 2752 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 2753 | // Anonymous namespaces. |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 2754 | assert(Namespc->isAnonymousNamespace()); |
| 2755 | CurContext->addDecl(Namespc); |
| 2756 | |
| 2757 | // Link the anonymous namespace into its parent. |
| 2758 | NamespaceDecl *PrevDecl; |
| 2759 | DeclContext *Parent = CurContext->getLookupContext(); |
| 2760 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 2761 | PrevDecl = TU->getAnonymousNamespace(); |
| 2762 | TU->setAnonymousNamespace(Namespc); |
| 2763 | } else { |
| 2764 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 2765 | PrevDecl = ND->getAnonymousNamespace(); |
| 2766 | ND->setAnonymousNamespace(Namespc); |
| 2767 | } |
| 2768 | |
| 2769 | // Link the anonymous namespace with its previous declaration. |
| 2770 | if (PrevDecl) { |
| 2771 | assert(PrevDecl->isAnonymousNamespace()); |
| 2772 | assert(!PrevDecl->getNextNamespace()); |
| 2773 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 2774 | PrevDecl->setNextNamespace(Namespc); |
| 2775 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 2776 | |
| 2777 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 2778 | // behaves as if it were replaced by |
| 2779 | // namespace unique { /* empty body */ } |
| 2780 | // using namespace unique; |
| 2781 | // namespace unique { namespace-body } |
| 2782 | // where all occurrences of 'unique' in a translation unit are |
| 2783 | // replaced by the same identifier and this identifier differs |
| 2784 | // from all other identifiers in the entire program. |
| 2785 | |
| 2786 | // We just create the namespace with an empty name and then add an |
| 2787 | // implicit using declaration, just like the standard suggests. |
| 2788 | // |
| 2789 | // CodeGen enforces the "universally unique" aspect by giving all |
| 2790 | // declarations semantically contained within an anonymous |
| 2791 | // namespace internal linkage. |
| 2792 | |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 2793 | if (!PrevDecl) { |
| 2794 | UsingDirectiveDecl* UD |
| 2795 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 2796 | /* 'using' */ LBrace, |
| 2797 | /* 'namespace' */ SourceLocation(), |
| 2798 | /* qualifier */ SourceRange(), |
| 2799 | /* NNS */ NULL, |
| 2800 | /* identifier */ SourceLocation(), |
| 2801 | Namespc, |
| 2802 | /* Ancestor */ CurContext); |
| 2803 | UD->setImplicit(); |
| 2804 | CurContext->addDecl(UD); |
| 2805 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 2809 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2810 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 2811 | // for the namespace has the declarations that showed up in that particular |
| 2812 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2813 | PushDeclContext(NamespcScope, Namespc); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2814 | return DeclPtrTy::make(Namespc); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 2817 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 2818 | /// is a namespace alias, returns the namespace it points to. |
| 2819 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 2820 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 2821 | return AD->getNamespace(); |
| 2822 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 2823 | } |
| 2824 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2825 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 2826 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2827 | void Sema::ActOnFinishNamespaceDef(DeclPtrTy D, SourceLocation RBrace) { |
| 2828 | Decl *Dcl = D.getAs<Decl>(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2829 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 2830 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 2831 | Namespc->setRBracLoc(RBrace); |
| 2832 | PopDeclContext(); |
| 2833 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2834 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2835 | Sema::DeclPtrTy Sema::ActOnUsingDirective(Scope *S, |
| 2836 | SourceLocation UsingLoc, |
| 2837 | SourceLocation NamespcLoc, |
| 2838 | const CXXScopeSpec &SS, |
| 2839 | SourceLocation IdentLoc, |
| 2840 | IdentifierInfo *NamespcName, |
| 2841 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2842 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 2843 | assert(NamespcName && "Invalid NamespcName."); |
| 2844 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2845 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2846 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2847 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2848 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 2849 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2850 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 2851 | LookupParsedName(R, S, &SS); |
| 2852 | if (R.isAmbiguous()) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2853 | return DeclPtrTy(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2854 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2855 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 2856 | NamedDecl *Named = R.getFoundDecl(); |
| 2857 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 2858 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2859 | // C++ [namespace.udir]p1: |
| 2860 | // A using-directive specifies that the names in the nominated |
| 2861 | // namespace can be used in the scope in which the |
| 2862 | // using-directive appears after the using-directive. During |
| 2863 | // unqualified name lookup (3.4.1), the names appear as if they |
| 2864 | // were declared in the nearest enclosing namespace which |
| 2865 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2866 | // namespace. [Note: in this context, "contains" means "contains |
| 2867 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2868 | |
| 2869 | // Find enclosing context containing both using-directive and |
| 2870 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 2871 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2872 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 2873 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 2874 | CommonAncestor = CommonAncestor->getParent(); |
| 2875 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 2876 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 2877 | SS.getRange(), |
| 2878 | (NestedNameSpecifier *)SS.getScopeRep(), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 2879 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2880 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2881 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 2882 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2885 | // FIXME: We ignore attributes for now. |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2886 | delete AttrList; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2887 | return DeclPtrTy::make(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
| 2890 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 2891 | // If scope has associated entity, then using directive is at namespace |
| 2892 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 2893 | // it's lookup structure. |
| 2894 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2895 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2896 | else |
| 2897 | // Otherwise it is block-sope. using-directives will affect lookup |
| 2898 | // only to the end of scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2899 | S->PushUsingDirective(DeclPtrTy::make(UDir)); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2900 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2901 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2902 | |
| 2903 | Sema::DeclPtrTy Sema::ActOnUsingDeclaration(Scope *S, |
Anders Carlsson | 595adc1 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 2904 | AccessSpecifier AS, |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2905 | bool HasUsingKeyword, |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2906 | SourceLocation UsingLoc, |
| 2907 | const CXXScopeSpec &SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 2908 | UnqualifiedId &Name, |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2909 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2910 | bool IsTypeName, |
| 2911 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2912 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2913 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 2914 | switch (Name.getKind()) { |
| 2915 | case UnqualifiedId::IK_Identifier: |
| 2916 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 2917 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 2918 | case UnqualifiedId::IK_ConversionFunctionId: |
| 2919 | break; |
| 2920 | |
| 2921 | case UnqualifiedId::IK_ConstructorName: |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 2922 | // C++0x inherited constructors. |
| 2923 | if (getLangOptions().CPlusPlus0x) break; |
| 2924 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 2925 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 2926 | << SS.getRange(); |
| 2927 | return DeclPtrTy(); |
| 2928 | |
| 2929 | case UnqualifiedId::IK_DestructorName: |
| 2930 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 2931 | << SS.getRange(); |
| 2932 | return DeclPtrTy(); |
| 2933 | |
| 2934 | case UnqualifiedId::IK_TemplateId: |
| 2935 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 2936 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
| 2937 | return DeclPtrTy(); |
| 2938 | } |
| 2939 | |
| 2940 | DeclarationName TargetName = GetNameFromUnqualifiedId(Name); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 2941 | if (!TargetName) |
| 2942 | return DeclPtrTy(); |
| 2943 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 2944 | // Warn about using declarations. |
| 2945 | // TODO: store that the declaration was written without 'using' and |
| 2946 | // talk about access decls instead of using decls in the |
| 2947 | // diagnostics. |
| 2948 | if (!HasUsingKeyword) { |
| 2949 | UsingLoc = Name.getSourceRange().getBegin(); |
| 2950 | |
| 2951 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
| 2952 | << CodeModificationHint::CreateInsertion(SS.getRange().getBegin(), |
| 2953 | "using "); |
| 2954 | } |
| 2955 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 2956 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 2957 | Name.getSourceRange().getBegin(), |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2958 | TargetName, AttrList, |
| 2959 | /* IsInstantiation */ false, |
| 2960 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2961 | if (UD) |
| 2962 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2963 | |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2964 | return DeclPtrTy::make(UD); |
| 2965 | } |
| 2966 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2967 | /// Determines whether to create a using shadow decl for a particular |
| 2968 | /// decl, given the set of decls existing prior to this using lookup. |
| 2969 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 2970 | const LookupResult &Previous) { |
| 2971 | // Diagnose finding a decl which is not from a base class of the |
| 2972 | // current class. We do this now because there are cases where this |
| 2973 | // function will silently decide not to build a shadow decl, which |
| 2974 | // will pre-empt further diagnostics. |
| 2975 | // |
| 2976 | // We don't need to do this in C++0x because we do the check once on |
| 2977 | // the qualifier. |
| 2978 | // |
| 2979 | // FIXME: diagnose the following if we care enough: |
| 2980 | // struct A { int foo; }; |
| 2981 | // struct B : A { using A::foo; }; |
| 2982 | // template <class T> struct C : A {}; |
| 2983 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 2984 | // This is invalid (during instantiation) in C++03 because B::foo |
| 2985 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 2986 | // We can't diagnose it immediately because C<T> is an unknown |
| 2987 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 2988 | // to A::foo, which will look well-formed when we instantiate. |
| 2989 | // The right solution is to not collapse the shadow-decl chain. |
| 2990 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 2991 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 2992 | |
| 2993 | // Handle enums and anonymous structs. |
| 2994 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 2995 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 2996 | while (OrigRec->isAnonymousStructOrUnion()) |
| 2997 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 2998 | |
| 2999 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 3000 | if (OrigDC == CurContext) { |
| 3001 | Diag(Using->getLocation(), |
| 3002 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3003 | << Using->getNestedNameRange(); |
| 3004 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3005 | return true; |
| 3006 | } |
| 3007 | |
| 3008 | Diag(Using->getNestedNameRange().getBegin(), |
| 3009 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3010 | << Using->getTargetNestedNameDecl() |
| 3011 | << cast<CXXRecordDecl>(CurContext) |
| 3012 | << Using->getNestedNameRange(); |
| 3013 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3014 | return true; |
| 3015 | } |
| 3016 | } |
| 3017 | |
| 3018 | if (Previous.empty()) return false; |
| 3019 | |
| 3020 | NamedDecl *Target = Orig; |
| 3021 | if (isa<UsingShadowDecl>(Target)) |
| 3022 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3023 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3024 | // If the target happens to be one of the previous declarations, we |
| 3025 | // don't have a conflict. |
| 3026 | // |
| 3027 | // FIXME: but we might be increasing its access, in which case we |
| 3028 | // should redeclare it. |
| 3029 | NamedDecl *NonTag = 0, *Tag = 0; |
| 3030 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 3031 | I != E; ++I) { |
| 3032 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3033 | if (D->getCanonicalDecl() == Target->getCanonicalDecl()) |
| 3034 | return false; |
| 3035 | |
| 3036 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 3037 | } |
| 3038 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3039 | if (Target->isFunctionOrFunctionTemplate()) { |
| 3040 | FunctionDecl *FD; |
| 3041 | if (isa<FunctionTemplateDecl>(Target)) |
| 3042 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 3043 | else |
| 3044 | FD = cast<FunctionDecl>(Target); |
| 3045 | |
| 3046 | NamedDecl *OldDecl = 0; |
| 3047 | switch (CheckOverload(FD, Previous, OldDecl)) { |
| 3048 | case Ovl_Overload: |
| 3049 | return false; |
| 3050 | |
| 3051 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3052 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3053 | break; |
| 3054 | |
| 3055 | // We found a decl with the exact signature. |
| 3056 | case Ovl_Match: |
| 3057 | if (isa<UsingShadowDecl>(OldDecl)) { |
| 3058 | // Silently ignore the possible conflict. |
| 3059 | return false; |
| 3060 | } |
| 3061 | |
| 3062 | // If we're in a record, we want to hide the target, so we |
| 3063 | // return true (without a diagnostic) to tell the caller not to |
| 3064 | // build a shadow decl. |
| 3065 | if (CurContext->isRecord()) |
| 3066 | return true; |
| 3067 | |
| 3068 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3069 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3070 | break; |
| 3071 | } |
| 3072 | |
| 3073 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3074 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 3075 | return true; |
| 3076 | } |
| 3077 | |
| 3078 | // Target is not a function. |
| 3079 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3080 | if (isa<TagDecl>(Target)) { |
| 3081 | // No conflict between a tag and a non-tag. |
| 3082 | if (!Tag) return false; |
| 3083 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3084 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3085 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3086 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 3087 | return true; |
| 3088 | } |
| 3089 | |
| 3090 | // No conflict between a tag and a non-tag. |
| 3091 | if (!NonTag) return false; |
| 3092 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3093 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3094 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3095 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 3096 | return true; |
| 3097 | } |
| 3098 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3099 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3100 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3101 | UsingDecl *UD, |
| 3102 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3103 | |
| 3104 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3105 | NamedDecl *Target = Orig; |
| 3106 | if (isa<UsingShadowDecl>(Target)) { |
| 3107 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3108 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3112 | = UsingShadowDecl::Create(Context, CurContext, |
| 3113 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3114 | UD->addShadowDecl(Shadow); |
| 3115 | |
| 3116 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3117 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3118 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3119 | CurContext->addDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3120 | Shadow->setAccess(UD->getAccess()); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3121 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3122 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 3123 | Shadow->setInvalidDecl(); |
| 3124 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3125 | return Shadow; |
| 3126 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3127 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3128 | /// Hides a using shadow declaration. This is required by the current |
| 3129 | /// using-decl implementation when a resolvable using declaration in a |
| 3130 | /// class is followed by a declaration which would hide or override |
| 3131 | /// one or more of the using decl's targets; for example: |
| 3132 | /// |
| 3133 | /// struct Base { void foo(int); }; |
| 3134 | /// struct Derived : Base { |
| 3135 | /// using Base::foo; |
| 3136 | /// void foo(int); |
| 3137 | /// }; |
| 3138 | /// |
| 3139 | /// The governing language is C++03 [namespace.udecl]p12: |
| 3140 | /// |
| 3141 | /// When a using-declaration brings names from a base class into a |
| 3142 | /// derived class scope, member functions in the derived class |
| 3143 | /// override and/or hide member functions with the same name and |
| 3144 | /// parameter types in a base class (rather than conflicting). |
| 3145 | /// |
| 3146 | /// There are two ways to implement this: |
| 3147 | /// (1) optimistically create shadow decls when they're not hidden |
| 3148 | /// by existing declarations, or |
| 3149 | /// (2) don't create any shadow decls (or at least don't make them |
| 3150 | /// visible) until we've fully parsed/instantiated the class. |
| 3151 | /// The problem with (1) is that we might have to retroactively remove |
| 3152 | /// a shadow decl, which requires several O(n) operations because the |
| 3153 | /// decl structures are (very reasonably) not designed for removal. |
| 3154 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 3155 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
| 3156 | // Remove it from the DeclContext... |
| 3157 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3158 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3159 | // ...and the scope, if applicable... |
| 3160 | if (S) { |
| 3161 | S->RemoveDecl(DeclPtrTy::make(static_cast<Decl*>(Shadow))); |
| 3162 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3165 | // ...and the using decl. |
| 3166 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 3167 | |
| 3168 | // TODO: complain somehow if Shadow was used. It shouldn't |
| 3169 | // be possible for this to happen, because |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3172 | /// Builds a using declaration. |
| 3173 | /// |
| 3174 | /// \param IsInstantiation - Whether this call arises from an |
| 3175 | /// instantiation of an unresolved using declaration. We treat |
| 3176 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3177 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 3178 | SourceLocation UsingLoc, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3179 | const CXXScopeSpec &SS, |
| 3180 | SourceLocation IdentLoc, |
| 3181 | DeclarationName Name, |
| 3182 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3183 | bool IsInstantiation, |
| 3184 | bool IsTypeName, |
| 3185 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3186 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3187 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 3188 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3189 | // FIXME: We ignore attributes for now. |
| 3190 | delete AttrList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3191 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3192 | if (SS.isEmpty()) { |
| 3193 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3194 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3195 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3196 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3197 | // Do the redeclaration lookup in the current scope. |
| 3198 | LookupResult Previous(*this, Name, IdentLoc, LookupUsingDeclName, |
| 3199 | ForRedeclaration); |
| 3200 | Previous.setHideTags(false); |
| 3201 | if (S) { |
| 3202 | LookupName(Previous, S); |
| 3203 | |
| 3204 | // It is really dumb that we have to do this. |
| 3205 | LookupResult::Filter F = Previous.makeFilter(); |
| 3206 | while (F.hasNext()) { |
| 3207 | NamedDecl *D = F.next(); |
| 3208 | if (!isDeclInScope(D, CurContext, S)) |
| 3209 | F.erase(); |
| 3210 | } |
| 3211 | F.done(); |
| 3212 | } else { |
| 3213 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 3214 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 3215 | LookupQualifiedName(Previous, CurContext); |
| 3216 | } |
| 3217 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3218 | NestedNameSpecifier *NNS = |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3219 | static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3220 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3221 | // Check for invalid redeclarations. |
| 3222 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 3223 | return 0; |
| 3224 | |
| 3225 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3226 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 3227 | return 0; |
| 3228 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3229 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3230 | NamedDecl *D; |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3231 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3232 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3233 | // FIXME: not all declaration name kinds are legal here |
| 3234 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 3235 | UsingLoc, TypenameLoc, |
| 3236 | SS.getRange(), NNS, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3237 | IdentLoc, Name); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3238 | } else { |
| 3239 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, |
| 3240 | UsingLoc, SS.getRange(), NNS, |
| 3241 | IdentLoc, Name); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3242 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3243 | } else { |
| 3244 | D = UsingDecl::Create(Context, CurContext, IdentLoc, |
| 3245 | SS.getRange(), UsingLoc, NNS, Name, |
| 3246 | IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3247 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3248 | D->setAccess(AS); |
| 3249 | CurContext->addDecl(D); |
| 3250 | |
| 3251 | if (!LookupContext) return D; |
| 3252 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3253 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3254 | if (RequireCompleteDeclContext(SS)) { |
| 3255 | UD->setInvalidDecl(); |
| 3256 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3259 | // Look up the target name. |
| 3260 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3261 | LookupResult R(*this, Name, IdentLoc, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3262 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3263 | // Unlike most lookups, we don't always want to hide tag |
| 3264 | // declarations: tag names are visible through the using declaration |
| 3265 | // even if hidden by ordinary names, *except* in a dependent context |
| 3266 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3267 | if (!IsInstantiation) |
| 3268 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3269 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3270 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3271 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3272 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 3273 | Diag(IdentLoc, diag::err_no_member) |
| 3274 | << Name << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3275 | UD->setInvalidDecl(); |
| 3276 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3279 | if (R.isAmbiguous()) { |
| 3280 | UD->setInvalidDecl(); |
| 3281 | return UD; |
| 3282 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3283 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3284 | if (IsTypeName) { |
| 3285 | // 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] | 3286 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3287 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 3288 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 3289 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 3290 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3291 | UD->setInvalidDecl(); |
| 3292 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3293 | } |
| 3294 | } else { |
| 3295 | // If we asked for a non-typename and we got a type, error out, |
| 3296 | // but only if this is an instantiation of an unresolved using |
| 3297 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3298 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3299 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 3300 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3301 | UD->setInvalidDecl(); |
| 3302 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3303 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3306 | // C++0x N2914 [namespace.udecl]p6: |
| 3307 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3308 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3309 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 3310 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3311 | UD->setInvalidDecl(); |
| 3312 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3313 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3314 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3315 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 3316 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 3317 | BuildUsingShadowDecl(S, UD, *I); |
| 3318 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3319 | |
| 3320 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3323 | /// Checks that the given using declaration is not an invalid |
| 3324 | /// redeclaration. Note that this is checking only for the using decl |
| 3325 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 3326 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 3327 | bool isTypeName, |
| 3328 | const CXXScopeSpec &SS, |
| 3329 | SourceLocation NameLoc, |
| 3330 | const LookupResult &Prev) { |
| 3331 | // C++03 [namespace.udecl]p8: |
| 3332 | // C++0x [namespace.udecl]p10: |
| 3333 | // A using-declaration is a declaration and can therefore be used |
| 3334 | // repeatedly where (and only where) multiple declarations are |
| 3335 | // allowed. |
| 3336 | // That's only in file contexts. |
| 3337 | if (CurContext->getLookupContext()->isFileContext()) |
| 3338 | return false; |
| 3339 | |
| 3340 | NestedNameSpecifier *Qual |
| 3341 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 3342 | |
| 3343 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 3344 | NamedDecl *D = *I; |
| 3345 | |
| 3346 | bool DTypename; |
| 3347 | NestedNameSpecifier *DQual; |
| 3348 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 3349 | DTypename = UD->isTypeName(); |
| 3350 | DQual = UD->getTargetNestedNameDecl(); |
| 3351 | } else if (UnresolvedUsingValueDecl *UD |
| 3352 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 3353 | DTypename = false; |
| 3354 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3355 | } else if (UnresolvedUsingTypenameDecl *UD |
| 3356 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 3357 | DTypename = true; |
| 3358 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3359 | } else continue; |
| 3360 | |
| 3361 | // using decls differ if one says 'typename' and the other doesn't. |
| 3362 | // FIXME: non-dependent using decls? |
| 3363 | if (isTypeName != DTypename) continue; |
| 3364 | |
| 3365 | // using decls differ if they name different scopes (but note that |
| 3366 | // template instantiation can cause this check to trigger when it |
| 3367 | // didn't before instantiation). |
| 3368 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 3369 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 3370 | continue; |
| 3371 | |
| 3372 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3373 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3374 | return true; |
| 3375 | } |
| 3376 | |
| 3377 | return false; |
| 3378 | } |
| 3379 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3380 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3381 | /// Checks that the given nested-name qualifier used in a using decl |
| 3382 | /// in the current context is appropriately related to the current |
| 3383 | /// scope. If an error is found, diagnoses it and returns true. |
| 3384 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 3385 | const CXXScopeSpec &SS, |
| 3386 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3387 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3388 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3389 | if (!CurContext->isRecord()) { |
| 3390 | // C++03 [namespace.udecl]p3: |
| 3391 | // C++0x [namespace.udecl]p8: |
| 3392 | // A using-declaration for a class member shall be a member-declaration. |
| 3393 | |
| 3394 | // If we weren't able to compute a valid scope, it must be a |
| 3395 | // dependent class scope. |
| 3396 | if (!NamedContext || NamedContext->isRecord()) { |
| 3397 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 3398 | << SS.getRange(); |
| 3399 | return true; |
| 3400 | } |
| 3401 | |
| 3402 | // Otherwise, everything is known to be fine. |
| 3403 | return false; |
| 3404 | } |
| 3405 | |
| 3406 | // The current scope is a record. |
| 3407 | |
| 3408 | // If the named context is dependent, we can't decide much. |
| 3409 | if (!NamedContext) { |
| 3410 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 3411 | // nested-name-specifier does not refer to a base class, which is |
| 3412 | // still possible in some cases. |
| 3413 | |
| 3414 | // Otherwise we have to conservatively report that things might be |
| 3415 | // okay. |
| 3416 | return false; |
| 3417 | } |
| 3418 | |
| 3419 | if (!NamedContext->isRecord()) { |
| 3420 | // Ideally this would point at the last name in the specifier, |
| 3421 | // but we don't have that level of source info. |
| 3422 | Diag(SS.getRange().getBegin(), |
| 3423 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 3424 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 3425 | return true; |
| 3426 | } |
| 3427 | |
| 3428 | if (getLangOptions().CPlusPlus0x) { |
| 3429 | // C++0x [namespace.udecl]p3: |
| 3430 | // In a using-declaration used as a member-declaration, the |
| 3431 | // nested-name-specifier shall name a base class of the class |
| 3432 | // being defined. |
| 3433 | |
| 3434 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 3435 | cast<CXXRecordDecl>(NamedContext))) { |
| 3436 | if (CurContext == NamedContext) { |
| 3437 | Diag(NameLoc, |
| 3438 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3439 | << SS.getRange(); |
| 3440 | return true; |
| 3441 | } |
| 3442 | |
| 3443 | Diag(SS.getRange().getBegin(), |
| 3444 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3445 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 3446 | << cast<CXXRecordDecl>(CurContext) |
| 3447 | << SS.getRange(); |
| 3448 | return true; |
| 3449 | } |
| 3450 | |
| 3451 | return false; |
| 3452 | } |
| 3453 | |
| 3454 | // C++03 [namespace.udecl]p4: |
| 3455 | // A using-declaration used as a member-declaration shall refer |
| 3456 | // to a member of a base class of the class being defined [etc.]. |
| 3457 | |
| 3458 | // Salient point: SS doesn't have to name a base class as long as |
| 3459 | // lookup only finds members from base classes. Therefore we can |
| 3460 | // diagnose here only if we can prove that that can't happen, |
| 3461 | // i.e. if the class hierarchies provably don't intersect. |
| 3462 | |
| 3463 | // TODO: it would be nice if "definitely valid" results were cached |
| 3464 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 3465 | // need to be repeated. |
| 3466 | |
| 3467 | struct UserData { |
| 3468 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 3469 | |
| 3470 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 3471 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 3472 | Data->Bases.insert(Base); |
| 3473 | return true; |
| 3474 | } |
| 3475 | |
| 3476 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 3477 | return !Class->forallBases(collect, this); |
| 3478 | } |
| 3479 | |
| 3480 | /// Returns true if the base is dependent or is one of the |
| 3481 | /// accumulated base classes. |
| 3482 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 3483 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 3484 | return !Data->Bases.count(Base); |
| 3485 | } |
| 3486 | |
| 3487 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 3488 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 3489 | } |
| 3490 | }; |
| 3491 | |
| 3492 | UserData Data; |
| 3493 | |
| 3494 | // Returns false if we find a dependent base. |
| 3495 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 3496 | return false; |
| 3497 | |
| 3498 | // Returns false if the class has a dependent base or if it or one |
| 3499 | // of its bases is present in the base set of the current context. |
| 3500 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 3501 | return false; |
| 3502 | |
| 3503 | Diag(SS.getRange().getBegin(), |
| 3504 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3505 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 3506 | << cast<CXXRecordDecl>(CurContext) |
| 3507 | << SS.getRange(); |
| 3508 | |
| 3509 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3512 | Sema::DeclPtrTy Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 3513 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3514 | SourceLocation AliasLoc, |
| 3515 | IdentifierInfo *Alias, |
| 3516 | const CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 3517 | SourceLocation IdentLoc, |
| 3518 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3519 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 3520 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3521 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 3522 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 3523 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 3524 | // Check if we have a previous declaration with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3525 | if (NamedDecl *PrevDecl |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 3526 | = LookupSingleName(S, Alias, LookupOrdinaryName, ForRedeclaration)) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 3527 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3528 | // 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] | 3529 | // namespace, so don't create a new one. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3530 | if (!R.isAmbiguous() && !R.empty() && |
| 3531 | AD->getNamespace() == getNamespaceDecl(R.getFoundDecl())) |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 3532 | return DeclPtrTy(); |
| 3533 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3534 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 3535 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 3536 | diag::err_redefinition_different_kind; |
| 3537 | Diag(AliasLoc, DiagID) << Alias; |
| 3538 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3539 | return DeclPtrTy(); |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3542 | if (R.isAmbiguous()) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3543 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3544 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3545 | if (R.empty()) { |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 3546 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3547 | return DeclPtrTy(); |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 3548 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3549 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 3550 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3551 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 3552 | Alias, SS.getRange(), |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 3553 | (NestedNameSpecifier *)SS.getScopeRep(), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3554 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3555 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3556 | CurContext->addDecl(AliasDecl); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 3557 | return DeclPtrTy::make(AliasDecl); |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 3560 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 3561 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 3562 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
| 3563 | !Constructor->isUsed()) && |
| 3564 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3565 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3566 | CXXRecordDecl *ClassDecl |
| 3567 | = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 3568 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 3569 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3570 | if (SetBaseOrMemberInitializers(Constructor, 0, 0, true)) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 3571 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 3572 | << CXXDefaultConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3573 | Constructor->setInvalidDecl(); |
| 3574 | } else { |
| 3575 | Constructor->setUsed(); |
| 3576 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3579 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 3580 | CXXDestructorDecl *Destructor) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3581 | assert((Destructor->isImplicit() && !Destructor->isUsed()) && |
| 3582 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 3583 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3584 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
| 3585 | // C++ [class.dtor] p5 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3586 | // Before the implicitly-declared default destructor for a class is |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3587 | // implicitly defined, all the implicitly-declared default destructors |
| 3588 | // for its base class and its non-static data members shall have been |
| 3589 | // implicitly defined. |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 3590 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3591 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3592 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3593 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3594 | if (!BaseClassDecl->hasTrivialDestructor()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3595 | if (CXXDestructorDecl *BaseDtor = |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3596 | const_cast<CXXDestructorDecl*>(BaseClassDecl->getDestructor(Context))) |
| 3597 | MarkDeclarationReferenced(CurrentLocation, BaseDtor); |
| 3598 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3599 | assert(false && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3600 | "DefineImplicitDestructor - missing dtor in a base class"); |
| 3601 | } |
| 3602 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3603 | |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 3604 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3605 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3606 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 3607 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 3608 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3609 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3610 | CXXRecordDecl *FieldClassDecl |
| 3611 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 3612 | if (!FieldClassDecl->hasTrivialDestructor()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | if (CXXDestructorDecl *FieldDtor = |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3614 | const_cast<CXXDestructorDecl*>( |
| 3615 | FieldClassDecl->getDestructor(Context))) |
| 3616 | MarkDeclarationReferenced(CurrentLocation, FieldDtor); |
| 3617 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3618 | assert(false && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3619 | "DefineImplicitDestructor - missing dtor in class of a data member"); |
| 3620 | } |
| 3621 | } |
| 3622 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 3623 | |
| 3624 | // FIXME: If CheckDestructor fails, we should emit a note about where the |
| 3625 | // implicit destructor was needed. |
| 3626 | if (CheckDestructor(Destructor)) { |
| 3627 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 3628 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 3629 | |
| 3630 | Destructor->setInvalidDecl(); |
| 3631 | return; |
| 3632 | } |
| 3633 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3634 | Destructor->setUsed(); |
| 3635 | } |
| 3636 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3637 | void Sema::DefineImplicitOverloadedAssign(SourceLocation CurrentLocation, |
| 3638 | CXXMethodDecl *MethodDecl) { |
| 3639 | assert((MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 3640 | MethodDecl->getOverloadedOperator() == OO_Equal && |
| 3641 | !MethodDecl->isUsed()) && |
| 3642 | "DefineImplicitOverloadedAssign - call it for implicit assignment op"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3643 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3644 | CXXRecordDecl *ClassDecl |
| 3645 | = cast<CXXRecordDecl>(MethodDecl->getDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | |
Fariborz Jahanian | c6249b9 | 2009-06-26 16:08:57 +0000 | [diff] [blame] | 3647 | // C++[class.copy] p12 |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3648 | // Before the implicitly-declared copy assignment operator for a class is |
| 3649 | // implicitly defined, all implicitly-declared copy assignment operators |
| 3650 | // for its direct base classes and its nonstatic data members shall have |
| 3651 | // been implicitly defined. |
| 3652 | bool err = false; |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 3653 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3654 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3655 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3656 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3657 | if (CXXMethodDecl *BaseAssignOpMethod = |
Anders Carlsson | b6cc91b | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 3658 | getAssignOperatorMethod(CurrentLocation, MethodDecl->getParamDecl(0), |
| 3659 | BaseClassDecl)) |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3660 | MarkDeclarationReferenced(CurrentLocation, BaseAssignOpMethod); |
| 3661 | } |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 3662 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3663 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3664 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 3665 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 3666 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3667 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3668 | CXXRecordDecl *FieldClassDecl |
| 3669 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3670 | if (CXXMethodDecl *FieldAssignOpMethod = |
Anders Carlsson | b6cc91b | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 3671 | getAssignOperatorMethod(CurrentLocation, MethodDecl->getParamDecl(0), |
| 3672 | FieldClassDecl)) |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3673 | MarkDeclarationReferenced(CurrentLocation, FieldAssignOpMethod); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3674 | } else if (FieldType->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3675 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 5e09d4c | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 3676 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 3677 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3678 | Diag(CurrentLocation, diag::note_first_required_here); |
| 3679 | err = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3680 | } else if (FieldType.isConstQualified()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3681 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 5e09d4c | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 3682 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 3683 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3684 | Diag(CurrentLocation, diag::note_first_required_here); |
| 3685 | err = true; |
| 3686 | } |
| 3687 | } |
| 3688 | if (!err) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3689 | MethodDecl->setUsed(); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | CXXMethodDecl * |
Anders Carlsson | b6cc91b | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 3693 | Sema::getAssignOperatorMethod(SourceLocation CurrentLocation, |
| 3694 | ParmVarDecl *ParmDecl, |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3695 | CXXRecordDecl *ClassDecl) { |
| 3696 | QualType LHSType = Context.getTypeDeclType(ClassDecl); |
| 3697 | QualType RHSType(LHSType); |
| 3698 | // If class's assignment operator argument is const/volatile qualified, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3699 | // look for operator = (const/volatile B&). Otherwise, look for |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3700 | // operator = (B&). |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3701 | RHSType = Context.getCVRQualifiedType(RHSType, |
| 3702 | ParmDecl->getType().getCVRQualifiers()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3703 | ExprOwningPtr<Expr> LHS(this, new (Context) DeclRefExpr(ParmDecl, |
Anders Carlsson | b6cc91b | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 3704 | LHSType, |
| 3705 | SourceLocation())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3706 | ExprOwningPtr<Expr> RHS(this, new (Context) DeclRefExpr(ParmDecl, |
Anders Carlsson | b6cc91b | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 3707 | RHSType, |
| 3708 | CurrentLocation)); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3709 | Expr *Args[2] = { &*LHS, &*RHS }; |
| 3710 | OverloadCandidateSet CandidateSet; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3711 | AddMemberOperatorCandidates(clang::OO_Equal, SourceLocation(), Args, 2, |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3712 | CandidateSet); |
| 3713 | OverloadCandidateSet::iterator Best; |
Anders Carlsson | b6cc91b | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 3714 | if (BestViableFunction(CandidateSet, CurrentLocation, Best) == OR_Success) |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3715 | return cast<CXXMethodDecl>(Best->Function); |
| 3716 | assert(false && |
| 3717 | "getAssignOperatorMethod - copy assignment operator method not found"); |
| 3718 | return 0; |
| 3719 | } |
| 3720 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3721 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 3722 | CXXConstructorDecl *CopyConstructor, |
| 3723 | unsigned TypeQuals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3724 | assert((CopyConstructor->isImplicit() && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3725 | CopyConstructor->isCopyConstructor(Context, TypeQuals) && |
| 3726 | !CopyConstructor->isUsed()) && |
| 3727 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3728 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3729 | CXXRecordDecl *ClassDecl |
| 3730 | = cast<CXXRecordDecl>(CopyConstructor->getDeclContext()); |
| 3731 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 3732 | // C++ [class.copy] p209 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3733 | // Before the implicitly-declared copy constructor for a class is |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3734 | // implicitly defined, all the implicitly-declared copy constructors |
| 3735 | // for its base class and its non-static data members shall have been |
| 3736 | // implicitly defined. |
| 3737 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 3738 | Base != ClassDecl->bases_end(); ++Base) { |
| 3739 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3740 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3741 | if (CXXConstructorDecl *BaseCopyCtor = |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3742 | BaseClassDecl->getCopyConstructor(Context, TypeQuals)) |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 3743 | MarkDeclarationReferenced(CurrentLocation, BaseCopyCtor); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3744 | } |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3745 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3746 | FieldEnd = ClassDecl->field_end(); |
| 3747 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3748 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 3749 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 3750 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3751 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3752 | CXXRecordDecl *FieldClassDecl |
| 3753 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3754 | if (CXXConstructorDecl *FieldCopyCtor = |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3755 | FieldClassDecl->getCopyConstructor(Context, TypeQuals)) |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 3756 | MarkDeclarationReferenced(CurrentLocation, FieldCopyCtor); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3757 | } |
| 3758 | } |
| 3759 | CopyConstructor->setUsed(); |
| 3760 | } |
| 3761 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 3762 | Sema::OwningExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 3763 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3764 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3765 | MultiExprArg ExprArgs, |
| 3766 | bool RequiresZeroInit) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3767 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3768 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3769 | // C++ [class.copy]p15: |
| 3770 | // Whenever a temporary class object is copied using a copy constructor, and |
| 3771 | // this object and the copy have the same cv-unqualified type, an |
| 3772 | // implementation is permitted to treat the original and the copy as two |
| 3773 | // different ways of referring to the same object and not perform a copy at |
| 3774 | // all, even if the class copy constructor or destructor have side effects. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3775 | |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3776 | // FIXME: Is this enough? |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3777 | if (Constructor->isCopyConstructor(Context)) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3778 | Expr *E = ((Expr **)ExprArgs.get())[0]; |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3779 | while (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 3780 | E = BE->getSubExpr(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3781 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 3782 | if (ICE->getCastKind() == CastExpr::CK_NoOp) |
| 3783 | E = ICE->getSubExpr(); |
Eli Friedman | 0336843 | 2009-12-06 09:26:33 +0000 | [diff] [blame] | 3784 | |
| 3785 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) |
| 3786 | Elidable = !CE->getCallReturnType()->isReferenceType(); |
| 3787 | else if (isa<CXXTemporaryObjectExpr>(E)) |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3788 | Elidable = true; |
| 3789 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3790 | |
| 3791 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3792 | Elidable, move(ExprArgs), RequiresZeroInit); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 3795 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 3796 | /// including handling of its default argument expressions. |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 3797 | Sema::OwningExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 3798 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 3799 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3800 | MultiExprArg ExprArgs, |
| 3801 | bool RequiresZeroInit) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3802 | unsigned NumExprs = ExprArgs.size(); |
| 3803 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3804 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3805 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3806 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3807 | Constructor, Elidable, Exprs, NumExprs, |
| 3808 | RequiresZeroInit)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 3811 | Sema::OwningExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3812 | Sema::BuildCXXTemporaryObjectExpr(CXXConstructorDecl *Constructor, |
| 3813 | QualType Ty, |
| 3814 | SourceLocation TyBeginLoc, |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 3815 | MultiExprArg Args, |
| 3816 | SourceLocation RParenLoc) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3817 | unsigned NumExprs = Args.size(); |
| 3818 | Expr **Exprs = (Expr **)Args.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3819 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3820 | MarkDeclarationReferenced(TyBeginLoc, Constructor); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3821 | return Owned(new (Context) CXXTemporaryObjectExpr(Context, Constructor, Ty, |
| 3822 | TyBeginLoc, Exprs, |
| 3823 | NumExprs, RParenLoc)); |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
| 3826 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3827 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 3828 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3829 | MultiExprArg Exprs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3830 | OwningExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 3831 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3832 | move(Exprs)); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 3833 | if (TempResult.isInvalid()) |
| 3834 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3835 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 3836 | Expr *Temp = TempResult.takeAs<Expr>(); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 3837 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 3838 | Temp = MaybeCreateCXXExprWithTemporaries(Temp); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 3839 | VD->setInit(Context, Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3840 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 3841 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3844 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, QualType DeclInitType) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3845 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>( |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3846 | DeclInitType->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3847 | if (!ClassDecl->hasTrivialDestructor()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3848 | if (CXXDestructorDecl *Destructor = |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3849 | const_cast<CXXDestructorDecl*>(ClassDecl->getDestructor(Context))) |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 3850 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3853 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3854 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 3855 | /// e.g: "int x(1);" |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3856 | void Sema::AddCXXDirectInitializerToDecl(DeclPtrTy Dcl, |
| 3857 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3858 | MultiExprArg Exprs, |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3859 | SourceLocation *CommaLocs, |
| 3860 | SourceLocation RParenLoc) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3861 | unsigned NumExprs = Exprs.size(); |
| 3862 | assert(NumExprs != 0 && Exprs.get() && "missing expressions"); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3863 | Decl *RealDecl = Dcl.getAs<Decl>(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3864 | |
| 3865 | // If there is no declaration, there was an error parsing it. Just ignore |
| 3866 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3867 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3868 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3869 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3870 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 3871 | if (!VDecl) { |
| 3872 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 3873 | RealDecl->setInvalidDecl(); |
| 3874 | return; |
| 3875 | } |
| 3876 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3877 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 3878 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3879 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 3880 | // |
| 3881 | // Clients that want to distinguish between the two forms, can check for |
| 3882 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 3883 | // A major benefit is that clients that don't particularly care about which |
| 3884 | // exactly form was it (like the CodeGen) can handle both cases without |
| 3885 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3886 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3887 | // If either the declaration has a dependent type or if any of the expressions |
| 3888 | // is type-dependent, we represent the initialization via a ParenListExpr for |
| 3889 | // later use during template instantiation. |
| 3890 | if (VDecl->getType()->isDependentType() || |
| 3891 | Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { |
| 3892 | // Let clients know that initialization was done with a direct initializer. |
| 3893 | VDecl->setCXXDirectInitializer(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3894 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3895 | // Store the initialization expressions as a ParenListExpr. |
| 3896 | unsigned NumExprs = Exprs.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3897 | VDecl->setInit(Context, |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3898 | new (Context) ParenListExpr(Context, LParenLoc, |
| 3899 | (Expr **)Exprs.release(), |
| 3900 | NumExprs, RParenLoc)); |
| 3901 | return; |
| 3902 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3903 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3904 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3905 | // C++ 8.5p11: |
| 3906 | // The form of initialization (using parentheses or '=') is generally |
| 3907 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3908 | // class type. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3909 | QualType DeclInitType = VDecl->getType(); |
| 3910 | if (const ArrayType *Array = Context.getAsArrayType(DeclInitType)) |
Fariborz Jahanian | 680a3f3 | 2009-10-28 19:04:36 +0000 | [diff] [blame] | 3911 | DeclInitType = Context.getBaseElementType(Array); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3912 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 3913 | // FIXME: This isn't the right place to complete the type. |
| 3914 | if (RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
| 3915 | diag::err_typecheck_decl_incomplete_type)) { |
| 3916 | VDecl->setInvalidDecl(); |
| 3917 | return; |
| 3918 | } |
| 3919 | |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3920 | if (VDecl->getType()->isRecordType()) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3921 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 3922 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3923 | CXXConstructorDecl *Constructor |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3924 | = PerformInitializationByConstructor(DeclInitType, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3925 | move(Exprs), |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 3926 | VDecl->getLocation(), |
| 3927 | SourceRange(VDecl->getLocation(), |
| 3928 | RParenLoc), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3929 | VDecl->getDeclName(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3930 | InitializationKind::CreateDirect(VDecl->getLocation(), |
| 3931 | LParenLoc, |
| 3932 | RParenLoc), |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3933 | ConstructorArgs); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3934 | if (!Constructor) |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3935 | RealDecl->setInvalidDecl(); |
Anders Carlsson | ca29ad9 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 3936 | else { |
Anders Carlsson | ca29ad9 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 3937 | VDecl->setCXXDirectInitializer(true); |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 3938 | if (InitializeVarWithConstructor(VDecl, Constructor, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3939 | move_arg(ConstructorArgs))) |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 3940 | RealDecl->setInvalidDecl(); |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 3941 | FinalizeVarWithDestructor(VDecl, DeclInitType); |
Anders Carlsson | ca29ad9 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 3942 | } |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3943 | return; |
| 3944 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3945 | |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 3946 | if (NumExprs > 1) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3947 | Diag(CommaLocs[0], diag::err_builtin_direct_init_more_than_one_arg) |
| 3948 | << SourceRange(VDecl->getLocation(), RParenLoc); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3949 | RealDecl->setInvalidDecl(); |
| 3950 | return; |
| 3951 | } |
| 3952 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3953 | // Let clients know that initialization was done with a direct initializer. |
| 3954 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 3955 | |
| 3956 | assert(NumExprs == 1 && "Expected 1 expression"); |
| 3957 | // Set the init expression, handles conversions. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3958 | AddInitializerToDecl(Dcl, ExprArg(*this, Exprs.release()[0]), |
| 3959 | /*DirectInit=*/true); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3960 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3961 | |
Douglas Gregor | 19aeac6 | 2009-11-14 03:27:21 +0000 | [diff] [blame] | 3962 | /// \brief Add the applicable constructor candidates for an initialization |
| 3963 | /// by constructor. |
| 3964 | static void AddConstructorInitializationCandidates(Sema &SemaRef, |
| 3965 | QualType ClassType, |
| 3966 | Expr **Args, |
| 3967 | unsigned NumArgs, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3968 | InitializationKind Kind, |
Douglas Gregor | 19aeac6 | 2009-11-14 03:27:21 +0000 | [diff] [blame] | 3969 | OverloadCandidateSet &CandidateSet) { |
| 3970 | // C++ [dcl.init]p14: |
| 3971 | // If the initialization is direct-initialization, or if it is |
| 3972 | // copy-initialization where the cv-unqualified version of the |
| 3973 | // source type is the same class as, or a derived class of, the |
| 3974 | // class of the destination, constructors are considered. The |
| 3975 | // applicable constructors are enumerated (13.3.1.3), and the |
| 3976 | // best one is chosen through overload resolution (13.3). The |
| 3977 | // constructor so selected is called to initialize the object, |
| 3978 | // with the initializer expression(s) as its argument(s). If no |
| 3979 | // constructor applies, or the overload resolution is ambiguous, |
| 3980 | // the initialization is ill-formed. |
| 3981 | const RecordType *ClassRec = ClassType->getAs<RecordType>(); |
| 3982 | assert(ClassRec && "Can only initialize a class type here"); |
| 3983 | |
| 3984 | // FIXME: When we decide not to synthesize the implicitly-declared |
| 3985 | // constructors, we'll need to make them appear here. |
| 3986 | |
| 3987 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassRec->getDecl()); |
| 3988 | DeclarationName ConstructorName |
| 3989 | = SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 3990 | SemaRef.Context.getCanonicalType(ClassType).getUnqualifiedType()); |
| 3991 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 3992 | for (llvm::tie(Con, ConEnd) = ClassDecl->lookup(ConstructorName); |
| 3993 | Con != ConEnd; ++Con) { |
| 3994 | // Find the constructor (which may be a template). |
| 3995 | CXXConstructorDecl *Constructor = 0; |
| 3996 | FunctionTemplateDecl *ConstructorTmpl= dyn_cast<FunctionTemplateDecl>(*Con); |
| 3997 | if (ConstructorTmpl) |
| 3998 | Constructor |
| 3999 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 4000 | else |
| 4001 | Constructor = cast<CXXConstructorDecl>(*Con); |
| 4002 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4003 | if ((Kind.getKind() == InitializationKind::IK_Direct) || |
| 4004 | (Kind.getKind() == InitializationKind::IK_Value) || |
| 4005 | (Kind.getKind() == InitializationKind::IK_Copy && |
Douglas Gregor | 19aeac6 | 2009-11-14 03:27:21 +0000 | [diff] [blame] | 4006 | Constructor->isConvertingConstructor(/*AllowExplicit=*/false)) || |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4007 | ((Kind.getKind() == InitializationKind::IK_Default) && |
| 4008 | Constructor->isDefaultConstructor())) { |
Douglas Gregor | 19aeac6 | 2009-11-14 03:27:21 +0000 | [diff] [blame] | 4009 | if (ConstructorTmpl) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4010 | SemaRef.AddTemplateOverloadCandidate(ConstructorTmpl, |
| 4011 | /*ExplicitArgs*/ 0, |
Douglas Gregor | 19aeac6 | 2009-11-14 03:27:21 +0000 | [diff] [blame] | 4012 | Args, NumArgs, CandidateSet); |
| 4013 | else |
| 4014 | SemaRef.AddOverloadCandidate(Constructor, Args, NumArgs, CandidateSet); |
| 4015 | } |
| 4016 | } |
| 4017 | } |
| 4018 | |
| 4019 | /// \brief Attempt to perform initialization by constructor |
| 4020 | /// (C++ [dcl.init]p14), which may occur as part of direct-initialization or |
| 4021 | /// copy-initialization. |
| 4022 | /// |
| 4023 | /// This routine determines whether initialization by constructor is possible, |
| 4024 | /// but it does not emit any diagnostics in the case where the initialization |
| 4025 | /// is ill-formed. |
| 4026 | /// |
| 4027 | /// \param ClassType the type of the object being initialized, which must have |
| 4028 | /// class type. |
| 4029 | /// |
| 4030 | /// \param Args the arguments provided to initialize the object |
| 4031 | /// |
| 4032 | /// \param NumArgs the number of arguments provided to initialize the object |
| 4033 | /// |
| 4034 | /// \param Kind the type of initialization being performed |
| 4035 | /// |
| 4036 | /// \returns the constructor used to initialize the object, if successful. |
| 4037 | /// Otherwise, emits a diagnostic and returns NULL. |
| 4038 | CXXConstructorDecl * |
| 4039 | Sema::TryInitializationByConstructor(QualType ClassType, |
| 4040 | Expr **Args, unsigned NumArgs, |
| 4041 | SourceLocation Loc, |
| 4042 | InitializationKind Kind) { |
| 4043 | // Build the overload candidate set |
| 4044 | OverloadCandidateSet CandidateSet; |
| 4045 | AddConstructorInitializationCandidates(*this, ClassType, Args, NumArgs, Kind, |
| 4046 | CandidateSet); |
| 4047 | |
| 4048 | // Determine whether we found a constructor we can use. |
| 4049 | OverloadCandidateSet::iterator Best; |
| 4050 | switch (BestViableFunction(CandidateSet, Loc, Best)) { |
| 4051 | case OR_Success: |
| 4052 | case OR_Deleted: |
| 4053 | // We found a constructor. Return it. |
| 4054 | return cast<CXXConstructorDecl>(Best->Function); |
| 4055 | |
| 4056 | case OR_No_Viable_Function: |
| 4057 | case OR_Ambiguous: |
| 4058 | // Overload resolution failed. Return nothing. |
| 4059 | return 0; |
| 4060 | } |
| 4061 | |
| 4062 | // Silence GCC warning |
| 4063 | return 0; |
| 4064 | } |
| 4065 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4066 | /// \brief Perform initialization by constructor (C++ [dcl.init]p14), which |
| 4067 | /// may occur as part of direct-initialization or copy-initialization. |
| 4068 | /// |
| 4069 | /// \param ClassType the type of the object being initialized, which must have |
| 4070 | /// class type. |
| 4071 | /// |
| 4072 | /// \param ArgsPtr the arguments provided to initialize the object |
| 4073 | /// |
| 4074 | /// \param Loc the source location where the initialization occurs |
| 4075 | /// |
| 4076 | /// \param Range the source range that covers the entire initialization |
| 4077 | /// |
| 4078 | /// \param InitEntity the name of the entity being initialized, if known |
| 4079 | /// |
| 4080 | /// \param Kind the type of initialization being performed |
| 4081 | /// |
| 4082 | /// \param ConvertedArgs a vector that will be filled in with the |
| 4083 | /// appropriately-converted arguments to the constructor (if initialization |
| 4084 | /// succeeded). |
| 4085 | /// |
| 4086 | /// \returns the constructor used to initialize the object, if successful. |
| 4087 | /// Otherwise, emits a diagnostic and returns NULL. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4088 | CXXConstructorDecl * |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 4089 | Sema::PerformInitializationByConstructor(QualType ClassType, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4090 | MultiExprArg ArgsPtr, |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 4091 | SourceLocation Loc, SourceRange Range, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4092 | DeclarationName InitEntity, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4093 | InitializationKind Kind, |
| 4094 | ASTOwningVector<&ActionBase::DeleteExpr> &ConvertedArgs) { |
Douglas Gregor | 19aeac6 | 2009-11-14 03:27:21 +0000 | [diff] [blame] | 4095 | |
| 4096 | // Build the overload candidate set |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4097 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 4098 | unsigned NumArgs = ArgsPtr.size(); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4099 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 19aeac6 | 2009-11-14 03:27:21 +0000 | [diff] [blame] | 4100 | AddConstructorInitializationCandidates(*this, ClassType, Args, NumArgs, Kind, |
| 4101 | CandidateSet); |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 4102 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4103 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4104 | switch (BestViableFunction(CandidateSet, Loc, Best)) { |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4105 | case OR_Success: |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4106 | // We found a constructor. Break out so that we can convert the arguments |
| 4107 | // appropriately. |
| 4108 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4109 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4110 | case OR_No_Viable_Function: |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 4111 | if (InitEntity) |
| 4112 | Diag(Loc, diag::err_ovl_no_viable_function_in_init) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4113 | << InitEntity << Range; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 4114 | else |
| 4115 | Diag(Loc, diag::err_ovl_no_viable_function_in_init) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4116 | << ClassType << Range; |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4117 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4118 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4119 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4120 | case OR_Ambiguous: |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 4121 | if (InitEntity) |
| 4122 | Diag(Loc, diag::err_ovl_ambiguous_init) << InitEntity << Range; |
| 4123 | else |
| 4124 | Diag(Loc, diag::err_ovl_ambiguous_init) << ClassType << Range; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4125 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4126 | return 0; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4127 | |
| 4128 | case OR_Deleted: |
| 4129 | if (InitEntity) |
| 4130 | Diag(Loc, diag::err_ovl_deleted_init) |
| 4131 | << Best->Function->isDeleted() |
| 4132 | << InitEntity << Range; |
Fariborz Jahanian | 6a587cb | 2009-11-25 21:53:11 +0000 | [diff] [blame] | 4133 | else { |
| 4134 | const CXXRecordDecl *RD = |
| 4135 | cast<CXXRecordDecl>(ClassType->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4136 | Diag(Loc, diag::err_ovl_deleted_init) |
| 4137 | << Best->Function->isDeleted() |
Fariborz Jahanian | 6a587cb | 2009-11-25 21:53:11 +0000 | [diff] [blame] | 4138 | << RD->getDeclName() << Range; |
| 4139 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4140 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4141 | return 0; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4142 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4143 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4144 | // Convert the arguments, fill in default arguments, etc. |
| 4145 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
| 4146 | if (CompleteConstructorCall(Constructor, move(ArgsPtr), Loc, ConvertedArgs)) |
| 4147 | return 0; |
| 4148 | |
| 4149 | return Constructor; |
| 4150 | } |
| 4151 | |
| 4152 | /// \brief Given a constructor and the set of arguments provided for the |
| 4153 | /// constructor, convert the arguments and add any required default arguments |
| 4154 | /// to form a proper call to this constructor. |
| 4155 | /// |
| 4156 | /// \returns true if an error occurred, false otherwise. |
| 4157 | bool |
| 4158 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 4159 | MultiExprArg ArgsPtr, |
| 4160 | SourceLocation Loc, |
| 4161 | ASTOwningVector<&ActionBase::DeleteExpr> &ConvertedArgs) { |
| 4162 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 4163 | unsigned NumArgs = ArgsPtr.size(); |
| 4164 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 4165 | |
| 4166 | const FunctionProtoType *Proto |
| 4167 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 4168 | assert(Proto && "Constructor without a prototype?"); |
| 4169 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4170 | |
| 4171 | // 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] | 4172 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4173 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4174 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4175 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4176 | |
| 4177 | VariadicCallType CallType = |
| 4178 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 4179 | llvm::SmallVector<Expr *, 8> AllArgs; |
| 4180 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 4181 | Proto, 0, Args, NumArgs, AllArgs, |
| 4182 | CallType); |
| 4183 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 4184 | ConvertedArgs.push_back(AllArgs[i]); |
| 4185 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4188 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 4189 | /// determine whether they are reference-related, |
| 4190 | /// reference-compatible, reference-compatible with added |
| 4191 | /// qualification, or incompatible, for use in C++ initialization by |
| 4192 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 4193 | /// type, and the first type (T1) is the pointee type of the reference |
| 4194 | /// type being initialized. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4195 | Sema::ReferenceCompareResult |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 4196 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 4197 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4198 | bool& DerivedToBase) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 4199 | assert(!OrigT1->isReferenceType() && |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4200 | "T1 must be the pointee type of the reference type"); |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 4201 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4202 | |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 4203 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 4204 | QualType T2 = Context.getCanonicalType(OrigT2); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4205 | QualType UnqualT1 = T1.getLocalUnqualifiedType(); |
| 4206 | QualType UnqualT2 = T2.getLocalUnqualifiedType(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4207 | |
| 4208 | // C++ [dcl.init.ref]p4: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4209 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4210 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4211 | // T1 is a base class of T2. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4212 | if (UnqualT1 == UnqualT2) |
| 4213 | DerivedToBase = false; |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 4214 | else if (!RequireCompleteType(Loc, OrigT1, PDiag()) && |
| 4215 | !RequireCompleteType(Loc, OrigT2, PDiag()) && |
| 4216 | IsDerivedFrom(UnqualT2, UnqualT1)) |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4217 | DerivedToBase = true; |
| 4218 | else |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4219 | return Ref_Incompatible; |
| 4220 | |
| 4221 | // At this point, we know that T1 and T2 are reference-related (at |
| 4222 | // least). |
| 4223 | |
| 4224 | // C++ [dcl.init.ref]p4: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4225 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4226 | // reference-related to T2 and cv1 is the same cv-qualification |
| 4227 | // as, or greater cv-qualification than, cv2. For purposes of |
| 4228 | // overload resolution, cases for which cv1 is greater |
| 4229 | // cv-qualification than cv2 are identified as |
| 4230 | // reference-compatible with added qualification (see 13.3.3.2). |
| 4231 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 4232 | return Ref_Compatible; |
| 4233 | else if (T1.isMoreQualifiedThan(T2)) |
| 4234 | return Ref_Compatible_With_Added_Qualification; |
| 4235 | else |
| 4236 | return Ref_Related; |
| 4237 | } |
| 4238 | |
| 4239 | /// CheckReferenceInit - Check the initialization of a reference |
| 4240 | /// variable with the given initializer (C++ [dcl.init.ref]). Init is |
| 4241 | /// the initializer (either a simple initializer or an initializer |
Douglas Gregor | 3205a78 | 2008-10-29 23:31:03 +0000 | [diff] [blame] | 4242 | /// list), and DeclType is the type of the declaration. When ICS is |
| 4243 | /// non-null, this routine will compute the implicit conversion |
| 4244 | /// sequence according to C++ [over.ics.ref] and will not produce any |
| 4245 | /// diagnostics; when ICS is null, it will emit diagnostics when any |
| 4246 | /// errors are found. Either way, a return value of true indicates |
| 4247 | /// that there was a failure, a return value of false indicates that |
| 4248 | /// the reference initialization succeeded. |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 4249 | /// |
| 4250 | /// When @p SuppressUserConversions, user-defined conversions are |
| 4251 | /// suppressed. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4252 | /// When @p AllowExplicit, we also permit explicit user-defined |
| 4253 | /// conversion functions. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 4254 | /// When @p ForceRValue, we unconditionally treat the initializer as an rvalue. |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 4255 | /// When @p IgnoreBaseAccess, we don't do access control on to-base conversion. |
| 4256 | /// This is used when this is called from a C-style cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4257 | bool |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4258 | Sema::CheckReferenceInit(Expr *&Init, QualType DeclType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 4259 | SourceLocation DeclLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 4260 | bool SuppressUserConversions, |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 4261 | bool AllowExplicit, bool ForceRValue, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 4262 | ImplicitConversionSequence *ICS, |
| 4263 | bool IgnoreBaseAccess) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4264 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 4265 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4266 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4267 | QualType T2 = Init->getType(); |
| 4268 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4269 | // If the initializer is the address of an overloaded function, try |
| 4270 | // to resolve the overloaded function. If all goes well, T2 is the |
| 4271 | // type of the resulting function. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4272 | if (Context.getCanonicalType(T2) == Context.OverloadTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4273 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Init, DeclType, |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4274 | ICS != 0); |
| 4275 | if (Fn) { |
| 4276 | // Since we're performing this reference-initialization for |
| 4277 | // real, update the initializer with the resulting function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4278 | if (!ICS) { |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 4279 | if (DiagnoseUseOfDecl(Fn, DeclLoc)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4280 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4281 | |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 4282 | Init = FixOverloadedFunctionReference(Init, Fn); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4283 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4284 | |
| 4285 | T2 = Fn->getType(); |
| 4286 | } |
| 4287 | } |
| 4288 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4289 | // Compute some basic properties of the types and the initializer. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4290 | bool isRValRef = DeclType->isRValueReferenceType(); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4291 | bool DerivedToBase = false; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 4292 | Expr::isLvalueResult InitLvalue = ForceRValue ? Expr::LV_InvalidExpression : |
| 4293 | Init->isLvalue(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | ReferenceCompareResult RefRelationship |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 4295 | = CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4296 | |
| 4297 | // Most paths end in a failed conversion. |
| 4298 | if (ICS) |
| 4299 | ICS->ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4300 | |
| 4301 | // C++ [dcl.init.ref]p5: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4302 | // A reference to type "cv1 T1" is initialized by an expression |
| 4303 | // of type "cv2 T2" as follows: |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4304 | |
| 4305 | // -- If the initializer expression |
| 4306 | |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4307 | // Rvalue references cannot bind to lvalues (N2812). |
| 4308 | // There is absolutely no situation where they can. In particular, note that |
| 4309 | // this is ill-formed, even if B has a user-defined conversion to A&&: |
| 4310 | // B b; |
| 4311 | // A&& r = b; |
| 4312 | if (isRValRef && InitLvalue == Expr::LV_Valid) { |
| 4313 | if (!ICS) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 4314 | Diag(DeclLoc, diag::err_lvalue_to_rvalue_ref) |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4315 | << Init->getSourceRange(); |
| 4316 | return true; |
| 4317 | } |
| 4318 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4319 | bool BindsDirectly = false; |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4320 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 4321 | // reference-compatible with "cv2 T2," or |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4322 | // |
| 4323 | // Note that the bit-field check is skipped if we are just computing |
| 4324 | // the implicit conversion sequence (C++ [over.best.ics]p2). |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 4325 | if (InitLvalue == Expr::LV_Valid && (ICS || !Init->getBitField()) && |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4326 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4327 | BindsDirectly = true; |
| 4328 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4329 | if (ICS) { |
| 4330 | // C++ [over.ics.ref]p1: |
| 4331 | // When a parameter of reference type binds directly (8.5.3) |
| 4332 | // to an argument expression, the implicit conversion sequence |
| 4333 | // is the identity conversion, unless the argument expression |
| 4334 | // has a type that is a derived class of the parameter type, |
| 4335 | // in which case the implicit conversion sequence is a |
| 4336 | // derived-to-base Conversion (13.3.3.1). |
| 4337 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 4338 | ICS->Standard.First = ICK_Identity; |
| 4339 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 4340 | ICS->Standard.Third = ICK_Identity; |
| 4341 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4342 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 4343 | ICS->Standard.ReferenceBinding = true; |
| 4344 | ICS->Standard.DirectBinding = true; |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4345 | ICS->Standard.RRefBinding = false; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 4346 | ICS->Standard.CopyConstructor = 0; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4347 | |
| 4348 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 4349 | // derived-to-base conversions is suppressed when we're |
| 4350 | // computing the implicit conversion sequence (C++ |
| 4351 | // [over.best.ics]p2). |
| 4352 | return false; |
| 4353 | } else { |
| 4354 | // Perform the conversion. |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4355 | CastExpr::CastKind CK = CastExpr::CK_NoOp; |
| 4356 | if (DerivedToBase) |
| 4357 | CK = CastExpr::CK_DerivedToBase; |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 4358 | else if(CheckExceptionSpecCompatibility(Init, T1)) |
| 4359 | return true; |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4360 | ImpCastExprToType(Init, T1, CK, /*isLvalue=*/true); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4361 | } |
| 4362 | } |
| 4363 | |
| 4364 | // -- has a class type (i.e., T2 is a class type) and can be |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4365 | // implicitly converted to an lvalue of type "cv3 T3," |
| 4366 | // where "cv1 T1" is reference-compatible with "cv3 T3" |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4367 | // 92) (this conversion is selected by enumerating the |
| 4368 | // applicable conversion functions (13.3.1.6) and choosing |
| 4369 | // the best one through overload resolution (13.3)), |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4370 | if (!isRValRef && !SuppressUserConversions && T2->isRecordType() && |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4371 | !RequireCompleteType(DeclLoc, T2, 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4372 | CXXRecordDecl *T2RecordDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4373 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4374 | |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 4375 | OverloadCandidateSet CandidateSet; |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4376 | const UnresolvedSet *Conversions |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 4377 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 4378 | for (UnresolvedSet::iterator I = Conversions->begin(), |
| 4379 | E = Conversions->end(); I != E; ++I) { |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4380 | NamedDecl *D = *I; |
| 4381 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4382 | if (isa<UsingShadowDecl>(D)) |
| 4383 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 4384 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4385 | FunctionTemplateDecl *ConvTemplate |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4386 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4387 | CXXConversionDecl *Conv; |
| 4388 | if (ConvTemplate) |
| 4389 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 4390 | else |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4391 | Conv = cast<CXXConversionDecl>(D); |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 4392 | |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 4393 | // If the conversion function doesn't return a reference type, |
| 4394 | // it can't be considered for this conversion. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4395 | if (Conv->getConversionType()->isLValueReferenceType() && |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4396 | (AllowExplicit || !Conv->isExplicit())) { |
| 4397 | if (ConvTemplate) |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4398 | AddTemplateConversionCandidate(ConvTemplate, ActingDC, |
| 4399 | Init, DeclType, CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4400 | else |
John McCall | 701c89e | 2009-12-03 04:06:58 +0000 | [diff] [blame] | 4401 | AddConversionCandidate(Conv, ActingDC, Init, DeclType, CandidateSet); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4402 | } |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 4403 | } |
| 4404 | |
| 4405 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 4406 | switch (BestViableFunction(CandidateSet, DeclLoc, Best)) { |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 4407 | case OR_Success: |
| 4408 | // This is a direct binding. |
| 4409 | BindsDirectly = true; |
| 4410 | |
| 4411 | if (ICS) { |
| 4412 | // C++ [over.ics.ref]p1: |
| 4413 | // |
| 4414 | // [...] If the parameter binds directly to the result of |
| 4415 | // applying a conversion function to the argument |
| 4416 | // expression, the implicit conversion sequence is a |
| 4417 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 4418 | // second standard conversion sequence either an identity |
| 4419 | // conversion or, if the conversion function returns an |
| 4420 | // entity of a type that is a derived class of the parameter |
| 4421 | // type, a derived-to-base Conversion. |
| 4422 | ICS->ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
| 4423 | ICS->UserDefined.Before = Best->Conversions[0].Standard; |
| 4424 | ICS->UserDefined.After = Best->FinalConversion; |
| 4425 | ICS->UserDefined.ConversionFunction = Best->Function; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 4426 | ICS->UserDefined.EllipsisConversion = false; |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 4427 | assert(ICS->UserDefined.After.ReferenceBinding && |
| 4428 | ICS->UserDefined.After.DirectBinding && |
| 4429 | "Expected a direct reference binding!"); |
| 4430 | return false; |
| 4431 | } else { |
Fariborz Jahanian | 8f489d6 | 2009-09-23 22:34:00 +0000 | [diff] [blame] | 4432 | OwningExprResult InitConversion = |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 4433 | BuildCXXCastArgument(DeclLoc, QualType(), |
Fariborz Jahanian | 8f489d6 | 2009-09-23 22:34:00 +0000 | [diff] [blame] | 4434 | CastExpr::CK_UserDefinedConversion, |
| 4435 | cast<CXXMethodDecl>(Best->Function), |
| 4436 | Owned(Init)); |
| 4437 | Init = InitConversion.takeAs<Expr>(); |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 4438 | |
| 4439 | if (CheckExceptionSpecCompatibility(Init, T1)) |
| 4440 | return true; |
Fariborz Jahanian | 8f489d6 | 2009-09-23 22:34:00 +0000 | [diff] [blame] | 4441 | ImpCastExprToType(Init, T1, CastExpr::CK_UserDefinedConversion, |
| 4442 | /*isLvalue=*/true); |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 4443 | } |
| 4444 | break; |
| 4445 | |
| 4446 | case OR_Ambiguous: |
Fariborz Jahanian | d9290cb | 2009-10-14 00:52:43 +0000 | [diff] [blame] | 4447 | if (ICS) { |
| 4448 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 4449 | Cand != CandidateSet.end(); ++Cand) |
| 4450 | if (Cand->Viable) |
| 4451 | ICS->ConversionFunctionSet.push_back(Cand->Function); |
| 4452 | break; |
| 4453 | } |
| 4454 | Diag(DeclLoc, diag::err_ref_init_ambiguous) << DeclType << Init->getType() |
| 4455 | << Init->getSourceRange(); |
| 4456 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 4457 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4458 | |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 4459 | case OR_No_Viable_Function: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4460 | case OR_Deleted: |
| 4461 | // There was no suitable conversion, or we found a deleted |
| 4462 | // conversion; continue with other checks. |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 4463 | break; |
| 4464 | } |
| 4465 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4466 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4467 | if (BindsDirectly) { |
| 4468 | // C++ [dcl.init.ref]p4: |
| 4469 | // [...] In all cases where the reference-related or |
| 4470 | // reference-compatible relationship of two types is used to |
| 4471 | // establish the validity of a reference binding, and T1 is a |
| 4472 | // base class of T2, a program that necessitates such a binding |
| 4473 | // is ill-formed if T1 is an inaccessible (clause 11) or |
| 4474 | // ambiguous (10.2) base class of T2. |
| 4475 | // |
| 4476 | // Note that we only check this condition when we're allowed to |
| 4477 | // complain about errors, because we should not be checking for |
| 4478 | // ambiguity (or inaccessibility) unless the reference binding |
| 4479 | // actually happens. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4480 | if (DerivedToBase) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 4481 | return CheckDerivedToBaseConversion(T2, T1, DeclLoc, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 4482 | Init->getSourceRange(), |
| 4483 | IgnoreBaseAccess); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4484 | else |
| 4485 | return false; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | // -- Otherwise, the reference shall be to a non-volatile const |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4489 | // type (i.e., cv1 shall be const), or the reference shall be an |
| 4490 | // rvalue reference and the initializer expression shall be an rvalue. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4491 | if (!isRValRef && T1.getCVRQualifiers() != Qualifiers::Const) { |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4492 | if (!ICS) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 4493 | Diag(DeclLoc, diag::err_not_reference_to_const_init) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 4494 | << T1 << int(InitLvalue != Expr::LV_Valid) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4495 | << T2 << Init->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4496 | return true; |
| 4497 | } |
| 4498 | |
| 4499 | // -- If the initializer expression is an rvalue, with T2 a |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4500 | // class type, and "cv1 T1" is reference-compatible with |
| 4501 | // "cv2 T2," the reference is bound in one of the |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4502 | // following ways (the choice is implementation-defined): |
| 4503 | // |
| 4504 | // -- The reference is bound to the object represented by |
| 4505 | // the rvalue (see 3.10) or to a sub-object within that |
| 4506 | // object. |
| 4507 | // |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4508 | // -- A temporary of type "cv1 T2" [sic] is created, and |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4509 | // a constructor is called to copy the entire rvalue |
| 4510 | // object into the temporary. The reference is bound to |
| 4511 | // the temporary or to a sub-object within the |
| 4512 | // temporary. |
| 4513 | // |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4514 | // The constructor that would be used to make the copy |
| 4515 | // shall be callable whether or not the copy is actually |
| 4516 | // done. |
| 4517 | // |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4518 | // Note that C++0x [dcl.init.ref]p5 takes away this implementation |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4519 | // freedom, so we will always take the first option and never build |
| 4520 | // a temporary in this case. FIXME: We will, however, have to check |
| 4521 | // for the presence of a copy constructor in C++98/03 mode. |
| 4522 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4523 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
| 4524 | if (ICS) { |
| 4525 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 4526 | ICS->Standard.First = ICK_Identity; |
| 4527 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 4528 | ICS->Standard.Third = ICK_Identity; |
| 4529 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4530 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 4531 | ICS->Standard.ReferenceBinding = true; |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4532 | ICS->Standard.DirectBinding = false; |
| 4533 | ICS->Standard.RRefBinding = isRValRef; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 4534 | ICS->Standard.CopyConstructor = 0; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4535 | } else { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4536 | CastExpr::CastKind CK = CastExpr::CK_NoOp; |
| 4537 | if (DerivedToBase) |
| 4538 | CK = CastExpr::CK_DerivedToBase; |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 4539 | else if(CheckExceptionSpecCompatibility(Init, T1)) |
| 4540 | return true; |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4541 | ImpCastExprToType(Init, T1, CK, /*isLvalue=*/false); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4542 | } |
| 4543 | return false; |
| 4544 | } |
| 4545 | |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4546 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4547 | // initialized from the initializer expression using the |
| 4548 | // rules for a non-reference copy initialization (8.5). The |
| 4549 | // reference is then bound to the temporary. If T1 is |
| 4550 | // reference-related to T2, cv1 must be the same |
| 4551 | // cv-qualification as, or greater cv-qualification than, |
| 4552 | // cv2; otherwise, the program is ill-formed. |
| 4553 | if (RefRelationship == Ref_Related) { |
| 4554 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4555 | // we would be reference-compatible or reference-compatible with |
| 4556 | // added qualification. But that wasn't the case, so the reference |
| 4557 | // initialization fails. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4558 | if (!ICS) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 4559 | Diag(DeclLoc, diag::err_reference_init_drops_quals) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 4560 | << T1 << int(InitLvalue != Expr::LV_Valid) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4561 | << T2 << Init->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4562 | return true; |
| 4563 | } |
| 4564 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 4565 | // If at least one of the types is a class type, the types are not |
| 4566 | // related, and we aren't allowed any user conversions, the |
| 4567 | // reference binding fails. This case is important for breaking |
| 4568 | // recursion, since TryImplicitConversion below will attempt to |
| 4569 | // create a temporary through the use of a copy constructor. |
| 4570 | if (SuppressUserConversions && RefRelationship == Ref_Incompatible && |
| 4571 | (T1->isRecordType() || T2->isRecordType())) { |
| 4572 | if (!ICS) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 4573 | Diag(DeclLoc, diag::err_typecheck_convert_incompatible) |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 4574 | << DeclType << Init->getType() << AA_Initializing << Init->getSourceRange(); |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 4575 | return true; |
| 4576 | } |
| 4577 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4578 | // Actually try to convert the initializer to T1. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4579 | if (ICS) { |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4580 | // C++ [over.ics.ref]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4581 | // |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4582 | // When a parameter of reference type is not bound directly to |
| 4583 | // an argument expression, the conversion sequence is the one |
| 4584 | // required to convert the argument expression to the |
| 4585 | // underlying type of the reference according to |
| 4586 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4587 | // to copy-initializing a temporary of the underlying type with |
| 4588 | // the argument expression. Any difference in top-level |
| 4589 | // cv-qualification is subsumed by the initialization itself |
| 4590 | // and does not constitute a conversion. |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4591 | *ICS = TryImplicitConversion(Init, T1, SuppressUserConversions, |
| 4592 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 4593 | /*ForceRValue=*/false, |
| 4594 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4595 | |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4596 | // Of course, that's still a reference binding. |
| 4597 | if (ICS->ConversionKind == ImplicitConversionSequence::StandardConversion) { |
| 4598 | ICS->Standard.ReferenceBinding = true; |
| 4599 | ICS->Standard.RRefBinding = isRValRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4600 | } else if (ICS->ConversionKind == |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4601 | ImplicitConversionSequence::UserDefinedConversion) { |
| 4602 | ICS->UserDefined.After.ReferenceBinding = true; |
| 4603 | ICS->UserDefined.After.RRefBinding = isRValRef; |
| 4604 | } |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4605 | return ICS->ConversionKind == ImplicitConversionSequence::BadConversion; |
| 4606 | } else { |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 4607 | ImplicitConversionSequence Conversions; |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 4608 | bool badConversion = PerformImplicitConversion(Init, T1, AA_Initializing, |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 4609 | false, false, |
| 4610 | Conversions); |
| 4611 | if (badConversion) { |
| 4612 | if ((Conversions.ConversionKind == |
| 4613 | ImplicitConversionSequence::BadConversion) |
Fariborz Jahanian | 82ad87b | 2009-09-28 22:03:07 +0000 | [diff] [blame] | 4614 | && !Conversions.ConversionFunctionSet.empty()) { |
Fariborz Jahanian | 7ad2d56 | 2009-09-24 00:42:43 +0000 | [diff] [blame] | 4615 | Diag(DeclLoc, |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 4616 | diag::err_lvalue_to_rvalue_ambig_ref) << Init->getSourceRange(); |
| 4617 | for (int j = Conversions.ConversionFunctionSet.size()-1; |
| 4618 | j >= 0; j--) { |
| 4619 | FunctionDecl *Func = Conversions.ConversionFunctionSet[j]; |
| 4620 | Diag(Func->getLocation(), diag::err_ovl_candidate); |
| 4621 | } |
| 4622 | } |
Fariborz Jahanian | 893f955 | 2009-09-30 21:23:30 +0000 | [diff] [blame] | 4623 | else { |
| 4624 | if (isRValRef) |
| 4625 | Diag(DeclLoc, diag::err_lvalue_to_rvalue_ref) |
| 4626 | << Init->getSourceRange(); |
| 4627 | else |
| 4628 | Diag(DeclLoc, diag::err_invalid_initialization) |
| 4629 | << DeclType << Init->getType() << Init->getSourceRange(); |
| 4630 | } |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 4631 | } |
| 4632 | return badConversion; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4633 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4634 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4635 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4636 | static inline bool |
| 4637 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 4638 | const FunctionDecl *FnDecl) { |
| 4639 | const DeclContext *DC = FnDecl->getDeclContext()->getLookupContext(); |
| 4640 | if (isa<NamespaceDecl>(DC)) { |
| 4641 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4642 | diag::err_operator_new_delete_declared_in_namespace) |
| 4643 | << FnDecl->getDeclName(); |
| 4644 | } |
| 4645 | |
| 4646 | if (isa<TranslationUnitDecl>(DC) && |
| 4647 | FnDecl->getStorageClass() == FunctionDecl::Static) { |
| 4648 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4649 | diag::err_operator_new_delete_declared_static) |
| 4650 | << FnDecl->getDeclName(); |
| 4651 | } |
| 4652 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 4653 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4654 | } |
| 4655 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4656 | static inline bool |
| 4657 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 4658 | CanQualType ExpectedResultType, |
| 4659 | CanQualType ExpectedFirstParamType, |
| 4660 | unsigned DependentParamTypeDiag, |
| 4661 | unsigned InvalidParamTypeDiag) { |
| 4662 | QualType ResultType = |
| 4663 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 4664 | |
| 4665 | // Check that the result type is not dependent. |
| 4666 | if (ResultType->isDependentType()) |
| 4667 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4668 | diag::err_operator_new_delete_dependent_result_type) |
| 4669 | << FnDecl->getDeclName() << ExpectedResultType; |
| 4670 | |
| 4671 | // Check that the result type is what we expect. |
| 4672 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 4673 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4674 | diag::err_operator_new_delete_invalid_result_type) |
| 4675 | << FnDecl->getDeclName() << ExpectedResultType; |
| 4676 | |
| 4677 | // A function template must have at least 2 parameters. |
| 4678 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 4679 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4680 | diag::err_operator_new_delete_template_too_few_parameters) |
| 4681 | << FnDecl->getDeclName(); |
| 4682 | |
| 4683 | // The function decl must have at least 1 parameter. |
| 4684 | if (FnDecl->getNumParams() == 0) |
| 4685 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4686 | diag::err_operator_new_delete_too_few_parameters) |
| 4687 | << FnDecl->getDeclName(); |
| 4688 | |
| 4689 | // Check the the first parameter type is not dependent. |
| 4690 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 4691 | if (FirstParamType->isDependentType()) |
| 4692 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 4693 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 4694 | |
| 4695 | // Check that the first parameter type is what we expect. |
| 4696 | if (SemaRef.Context.getCanonicalType(FirstParamType) != |
| 4697 | ExpectedFirstParamType) |
| 4698 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 4699 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 4700 | |
| 4701 | return false; |
| 4702 | } |
| 4703 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4704 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4705 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4706 | // C++ [basic.stc.dynamic.allocation]p1: |
| 4707 | // A program is ill-formed if an allocation function is declared in a |
| 4708 | // namespace scope other than global scope or declared static in global |
| 4709 | // scope. |
| 4710 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 4711 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4712 | |
| 4713 | CanQualType SizeTy = |
| 4714 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 4715 | |
| 4716 | // C++ [basic.stc.dynamic.allocation]p1: |
| 4717 | // The return type shall be void*. The first parameter shall have type |
| 4718 | // std::size_t. |
| 4719 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 4720 | SizeTy, |
| 4721 | diag::err_operator_new_dependent_param_type, |
| 4722 | diag::err_operator_new_param_type)) |
| 4723 | return true; |
| 4724 | |
| 4725 | // C++ [basic.stc.dynamic.allocation]p1: |
| 4726 | // The first parameter shall not have an associated default argument. |
| 4727 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4728 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4729 | diag::err_operator_new_default_arg) |
| 4730 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 4731 | |
| 4732 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4733 | } |
| 4734 | |
| 4735 | static bool |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4736 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 4737 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 4738 | // A program is ill-formed if deallocation functions are declared in a |
| 4739 | // namespace scope other than global scope or declared static in global |
| 4740 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4741 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 4742 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4743 | |
| 4744 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 4745 | // Each deallocation function shall return void and its first parameter |
| 4746 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4747 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 4748 | SemaRef.Context.VoidPtrTy, |
| 4749 | diag::err_operator_delete_dependent_param_type, |
| 4750 | diag::err_operator_delete_param_type)) |
| 4751 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4752 | |
Anders Carlsson | 46991d6 | 2009-12-12 00:16:02 +0000 | [diff] [blame] | 4753 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 4754 | if (FirstParamType->isDependentType()) |
| 4755 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4756 | diag::err_operator_delete_dependent_param_type) |
| 4757 | << FnDecl->getDeclName() << SemaRef.Context.VoidPtrTy; |
| 4758 | |
| 4759 | if (SemaRef.Context.getCanonicalType(FirstParamType) != |
| 4760 | SemaRef.Context.VoidPtrTy) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4761 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4762 | diag::err_operator_delete_param_type) |
| 4763 | << FnDecl->getDeclName() << SemaRef.Context.VoidPtrTy; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4764 | |
| 4765 | return false; |
| 4766 | } |
| 4767 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4768 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 4769 | /// of this overloaded operator is well-formed. If so, returns false; |
| 4770 | /// otherwise, emits appropriate diagnostics and returns true. |
| 4771 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4772 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4773 | "Expected an overloaded operator declaration"); |
| 4774 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4775 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 4776 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4777 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4778 | // The allocation and deallocation functions, operator new, |
| 4779 | // operator new[], operator delete and operator delete[], are |
| 4780 | // described completely in 3.7.3. The attributes and restrictions |
| 4781 | // found in the rest of this subclause do not apply to them unless |
| 4782 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 4783 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4784 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 4785 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4786 | if (Op == OO_New || Op == OO_Array_New) |
| 4787 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4788 | |
| 4789 | // C++ [over.oper]p6: |
| 4790 | // An operator function shall either be a non-static member |
| 4791 | // function or be a non-member function and have at least one |
| 4792 | // parameter whose type is a class, a reference to a class, an |
| 4793 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4794 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4795 | if (MethodDecl->isStatic()) |
| 4796 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4797 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4798 | } else { |
| 4799 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4800 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 4801 | ParamEnd = FnDecl->param_end(); |
| 4802 | Param != ParamEnd; ++Param) { |
| 4803 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 4804 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 4805 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4806 | ClassOrEnumParam = true; |
| 4807 | break; |
| 4808 | } |
| 4809 | } |
| 4810 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4811 | if (!ClassOrEnumParam) |
| 4812 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4813 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4814 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
| 4817 | // C++ [over.oper]p8: |
| 4818 | // An operator function cannot have default arguments (8.3.6), |
| 4819 | // except where explicitly stated below. |
| 4820 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4821 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4822 | // (C++ [over.call]p1). |
| 4823 | if (Op != OO_Call) { |
| 4824 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 4825 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4826 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4827 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4828 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4829 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4830 | } |
| 4831 | } |
| 4832 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4833 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 4834 | { false, false, false } |
| 4835 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 4836 | , { Unary, Binary, MemberOnly } |
| 4837 | #include "clang/Basic/OperatorKinds.def" |
| 4838 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4839 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4840 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 4841 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 4842 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4843 | |
| 4844 | // C++ [over.oper]p8: |
| 4845 | // [...] Operator functions cannot have more or fewer parameters |
| 4846 | // than the number required for the corresponding operator, as |
| 4847 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4848 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4849 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4850 | if (Op != OO_Call && |
| 4851 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 4852 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 4853 | (NumParams < 1) || (NumParams > 2))) { |
| 4854 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4855 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4856 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4857 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4858 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4859 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4860 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 4861 | assert(CanBeBinaryOperator && |
| 4862 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4863 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4864 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4865 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4866 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4867 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4868 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4869 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4870 | // Overloaded operators other than operator() cannot be variadic. |
| 4871 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4872 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4873 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4874 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4878 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 4879 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4880 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4881 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | // C++ [over.inc]p1: |
| 4885 | // The user-defined function called operator++ implements the |
| 4886 | // prefix and postfix ++ operator. If this function is a member |
| 4887 | // function with no parameters, or a non-member function with one |
| 4888 | // parameter of class or enumeration type, it defines the prefix |
| 4889 | // increment operator ++ for objects of that type. If the function |
| 4890 | // is a member function with one parameter (which shall be of type |
| 4891 | // int) or a non-member function with two parameters (the second |
| 4892 | // of which shall be of type int), it defines the postfix |
| 4893 | // increment operator ++ for objects of that type. |
| 4894 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 4895 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 4896 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4897 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4898 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 4899 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 4900 | if (!ParamIsInt) |
| 4901 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4902 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4903 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4906 | // Notify the class if it got an assignment operator. |
| 4907 | if (Op == OO_Equal) { |
| 4908 | // Would have returned earlier otherwise. |
| 4909 | assert(isa<CXXMethodDecl>(FnDecl) && |
| 4910 | "Overloaded = not member, but not filtered."); |
| 4911 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 4912 | Method->getParent()->addedAssignmentOperator(Context, Method); |
| 4913 | } |
| 4914 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4915 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4916 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 4917 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4918 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 4919 | /// linkage specification, including the language and (if present) |
| 4920 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 4921 | /// the location of the language string literal, which is provided |
| 4922 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 4923 | /// the '{' brace. Otherwise, this linkage specification does not |
| 4924 | /// have any braces. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4925 | Sema::DeclPtrTy Sema::ActOnStartLinkageSpecification(Scope *S, |
| 4926 | SourceLocation ExternLoc, |
| 4927 | SourceLocation LangLoc, |
| 4928 | const char *Lang, |
| 4929 | unsigned StrSize, |
| 4930 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4931 | LinkageSpecDecl::LanguageIDs Language; |
| 4932 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 4933 | Language = LinkageSpecDecl::lang_c; |
| 4934 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 4935 | Language = LinkageSpecDecl::lang_cxx; |
| 4936 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4937 | Diag(LangLoc, diag::err_bad_language); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4938 | return DeclPtrTy(); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4939 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4940 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4941 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4942 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4943 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4944 | LangLoc, Language, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4945 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4946 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4947 | PushDeclContext(S, D); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4948 | return DeclPtrTy::make(D); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4949 | } |
| 4950 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4951 | /// ActOnFinishLinkageSpecification - Completely the definition of |
| 4952 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 4953 | /// valid, it's the position of the closing '}' brace in a linkage |
| 4954 | /// specification that uses braces. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4955 | Sema::DeclPtrTy Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 4956 | DeclPtrTy LinkageSpec, |
| 4957 | SourceLocation RBraceLoc) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4958 | if (LinkageSpec) |
| 4959 | PopDeclContext(); |
| 4960 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4963 | /// \brief Perform semantic analysis for the variable declaration that |
| 4964 | /// occurs within a C++ catch clause, returning the newly-created |
| 4965 | /// variable. |
| 4966 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, QualType ExDeclType, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4967 | TypeSourceInfo *TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4968 | IdentifierInfo *Name, |
| 4969 | SourceLocation Loc, |
| 4970 | SourceRange Range) { |
| 4971 | bool Invalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4972 | |
| 4973 | // Arrays and functions decay. |
| 4974 | if (ExDeclType->isArrayType()) |
| 4975 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 4976 | else if (ExDeclType->isFunctionType()) |
| 4977 | ExDeclType = Context.getPointerType(ExDeclType); |
| 4978 | |
| 4979 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 4980 | // The exception-declaration shall not denote a pointer or reference to an |
| 4981 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4982 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4983 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4984 | Diag(Loc, diag::err_catch_rvalue_ref) << Range; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4985 | Invalid = true; |
| 4986 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4987 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4988 | QualType BaseType = ExDeclType; |
| 4989 | 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] | 4990 | unsigned DK = diag::err_catch_incomplete; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4991 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4992 | BaseType = Ptr->getPointeeType(); |
| 4993 | Mode = 1; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4994 | DK = diag::err_catch_incomplete_ptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4995 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4996 | // 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] | 4997 | BaseType = Ref->getPointeeType(); |
| 4998 | Mode = 2; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4999 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5000 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 5001 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5002 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5003 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5004 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5005 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5006 | RequireNonAbstractType(Loc, ExDeclType, |
| 5007 | diag::err_abstract_type_in_decl, |
| 5008 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 5009 | Invalid = true; |
| 5010 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5011 | // FIXME: Need to test for ability to copy-construct and destroy the |
| 5012 | // exception variable. |
| 5013 | |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 5014 | // FIXME: Need to check for abstract classes. |
| 5015 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5016 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5017 | Name, ExDeclType, TInfo, VarDecl::None); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5018 | |
| 5019 | if (Invalid) |
| 5020 | ExDecl->setInvalidDecl(); |
| 5021 | |
| 5022 | return ExDecl; |
| 5023 | } |
| 5024 | |
| 5025 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 5026 | /// handler. |
| 5027 | Sema::DeclPtrTy Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5028 | TypeSourceInfo *TInfo = 0; |
| 5029 | QualType ExDeclType = GetTypeForDeclarator(D, S, &TInfo); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5030 | |
| 5031 | bool Invalid = D.isInvalidType(); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5032 | IdentifierInfo *II = D.getIdentifier(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5033 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, LookupOrdinaryName)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5034 | // The scope should be freshly made just for us. There is just no way |
| 5035 | // it contains any previous declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5036 | assert(!S->isDeclScope(DeclPtrTy::make(PrevDecl))); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5037 | if (PrevDecl->isTemplateParameter()) { |
| 5038 | // Maybe we will complain about the shadowed template parameter. |
| 5039 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5040 | } |
| 5041 | } |
| 5042 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5043 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5044 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 5045 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5046 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5049 | VarDecl *ExDecl = BuildExceptionDeclaration(S, ExDeclType, TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5050 | D.getIdentifier(), |
| 5051 | D.getIdentifierLoc(), |
| 5052 | D.getDeclSpec().getSourceRange()); |
| 5053 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5054 | if (Invalid) |
| 5055 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5056 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5057 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5058 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5059 | PushOnScopeChains(ExDecl, S); |
| 5060 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5061 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5062 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5063 | ProcessDeclAttributes(S, ExDecl, D); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5064 | return DeclPtrTy::make(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5065 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5066 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5067 | Sema::DeclPtrTy Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5068 | ExprArg assertexpr, |
| 5069 | ExprArg assertmessageexpr) { |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5070 | Expr *AssertExpr = (Expr *)assertexpr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5071 | StringLiteral *AssertMessage = |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5072 | cast<StringLiteral>((Expr *)assertmessageexpr.get()); |
| 5073 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5074 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 5075 | llvm::APSInt Value(32); |
| 5076 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 5077 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 5078 | AssertExpr->getSourceRange(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5079 | return DeclPtrTy(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5080 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5081 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5082 | if (Value == 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5083 | Diag(AssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 5084 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5085 | } |
| 5086 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5087 | |
Anders Carlsson | 77d8142 | 2009-03-15 17:35:16 +0000 | [diff] [blame] | 5088 | assertexpr.release(); |
| 5089 | assertmessageexpr.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5090 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5091 | AssertExpr, AssertMessage); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5092 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5093 | CurContext->addDecl(Decl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5094 | return DeclPtrTy::make(Decl); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5095 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5096 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5097 | /// Handle a friend type declaration. This works in tandem with |
| 5098 | /// ActOnTag. |
| 5099 | /// |
| 5100 | /// Notes on friend class templates: |
| 5101 | /// |
| 5102 | /// We generally treat friend class declarations as if they were |
| 5103 | /// declaring a class. So, for example, the elaborated type specifier |
| 5104 | /// in a friend declaration is required to obey the restrictions of a |
| 5105 | /// class-head (i.e. no typedefs in the scope chain), template |
| 5106 | /// parameters are required to match up with simple template-ids, &c. |
| 5107 | /// However, unlike when declaring a template specialization, it's |
| 5108 | /// okay to refer to a template specialization without an empty |
| 5109 | /// template parameter declaration, e.g. |
| 5110 | /// friend class A<T>::B<unsigned>; |
| 5111 | /// We permit this as a special case; if there are any template |
| 5112 | /// parameters present at all, require proper matching, i.e. |
| 5113 | /// template <> template <class T> friend class A<int>::B; |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 5114 | Sema::DeclPtrTy Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5115 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5116 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5117 | |
| 5118 | assert(DS.isFriendSpecified()); |
| 5119 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 5120 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5121 | // Try to convert the decl specifier to a type. This works for |
| 5122 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 5123 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 5124 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 5125 | QualType T = GetTypeForDeclarator(TheDeclarator, S); |
| 5126 | if (TheDeclarator.isInvalidType()) |
| 5127 | return DeclPtrTy(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5128 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5129 | // This is definitely an error in C++98. It's probably meant to |
| 5130 | // be forbidden in C++0x, too, but the specification is just |
| 5131 | // poorly written. |
| 5132 | // |
| 5133 | // The problem is with declarations like the following: |
| 5134 | // template <T> friend A<T>::foo; |
| 5135 | // where deciding whether a class C is a friend or not now hinges |
| 5136 | // on whether there exists an instantiation of A that causes |
| 5137 | // 'foo' to equal C. There are restrictions on class-heads |
| 5138 | // (which we declare (by fiat) elaborated friend declarations to |
| 5139 | // be) that makes this tractable. |
| 5140 | // |
| 5141 | // FIXME: handle "template <> friend class A<T>;", which |
| 5142 | // is possibly well-formed? Who even knows? |
| 5143 | if (TempParams.size() && !isa<ElaboratedType>(T)) { |
| 5144 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 5145 | << DS.getSourceRange(); |
| 5146 | return DeclPtrTy(); |
| 5147 | } |
| 5148 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5149 | // C++ [class.friend]p2: |
| 5150 | // An elaborated-type-specifier shall be used in a friend declaration |
| 5151 | // for a class.* |
| 5152 | // * The class-key of the elaborated-type-specifier is required. |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 5153 | // This is one of the rare places in Clang where it's legitimate to |
| 5154 | // ask about the "spelling" of the type. |
| 5155 | if (!getLangOptions().CPlusPlus0x && !isa<ElaboratedType>(T)) { |
| 5156 | // If we evaluated the type to a record type, suggest putting |
| 5157 | // a tag in front. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5158 | if (const RecordType *RT = T->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 5159 | RecordDecl *RD = RT->getDecl(); |
| 5160 | |
| 5161 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 5162 | |
John McCall | e3af023 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 5163 | Diag(DS.getTypeSpecTypeLoc(), diag::err_unelaborated_friend_type) |
| 5164 | << (unsigned) RD->getTagKind() |
| 5165 | << T |
| 5166 | << SourceRange(DS.getFriendSpecLoc()) |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 5167 | << CodeModificationHint::CreateInsertion(DS.getTypeSpecTypeLoc(), |
| 5168 | InsertionText); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5169 | return DeclPtrTy(); |
| 5170 | }else { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 5171 | Diag(DS.getFriendSpecLoc(), diag::err_unexpected_friend) |
| 5172 | << DS.getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5173 | return DeclPtrTy(); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5174 | } |
| 5175 | } |
| 5176 | |
John McCall | e3af023 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 5177 | // Enum types cannot be friends. |
| 5178 | if (T->getAs<EnumType>()) { |
| 5179 | Diag(DS.getTypeSpecTypeLoc(), diag::err_enum_friend) |
| 5180 | << SourceRange(DS.getFriendSpecLoc()); |
| 5181 | return DeclPtrTy(); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 5182 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5183 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5184 | // C++98 [class.friend]p1: A friend of a class is a function |
| 5185 | // or class that is not a member of the class . . . |
| 5186 | // But that's a silly restriction which nobody implements for |
| 5187 | // inner classes, and C++0x removes it anyway, so we only report |
| 5188 | // this (as a warning) if we're being pedantic. |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 5189 | if (!getLangOptions().CPlusPlus0x) |
| 5190 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 5191 | if (RT->getDecl()->getDeclContext() == CurContext) |
| 5192 | Diag(DS.getFriendSpecLoc(), diag::ext_friend_inner_class); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5193 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5194 | Decl *D; |
| 5195 | if (TempParams.size()) |
| 5196 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
| 5197 | TempParams.size(), |
| 5198 | (TemplateParameterList**) TempParams.release(), |
| 5199 | T.getTypePtr(), |
| 5200 | DS.getFriendSpecLoc()); |
| 5201 | else |
| 5202 | D = FriendDecl::Create(Context, CurContext, Loc, T.getTypePtr(), |
| 5203 | DS.getFriendSpecLoc()); |
| 5204 | D->setAccess(AS_public); |
| 5205 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5206 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5207 | return DeclPtrTy::make(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5208 | } |
| 5209 | |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 5210 | Sema::DeclPtrTy |
| 5211 | Sema::ActOnFriendFunctionDecl(Scope *S, |
| 5212 | Declarator &D, |
| 5213 | bool IsDefinition, |
| 5214 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5215 | const DeclSpec &DS = D.getDeclSpec(); |
| 5216 | |
| 5217 | assert(DS.isFriendSpecified()); |
| 5218 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 5219 | |
| 5220 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5221 | TypeSourceInfo *TInfo = 0; |
| 5222 | QualType T = GetTypeForDeclarator(D, S, &TInfo); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5223 | |
| 5224 | // C++ [class.friend]p1 |
| 5225 | // A friend of a class is a function or class.... |
| 5226 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5227 | // It *doesn't* see through dependent types, which is correct |
| 5228 | // according to [temp.arg.type]p3: |
| 5229 | // If a declaration acquires a function type through a |
| 5230 | // type dependent on a template-parameter and this causes |
| 5231 | // a declaration that does not use the syntactic form of a |
| 5232 | // function declarator to have a function type, the program |
| 5233 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5234 | if (!T->isFunctionType()) { |
| 5235 | Diag(Loc, diag::err_unexpected_friend); |
| 5236 | |
| 5237 | // It might be worthwhile to try to recover by creating an |
| 5238 | // appropriate declaration. |
| 5239 | return DeclPtrTy(); |
| 5240 | } |
| 5241 | |
| 5242 | // C++ [namespace.memdef]p3 |
| 5243 | // - If a friend declaration in a non-local class first declares a |
| 5244 | // class or function, the friend class or function is a member |
| 5245 | // of the innermost enclosing namespace. |
| 5246 | // - The name of the friend is not found by simple name lookup |
| 5247 | // until a matching declaration is provided in that namespace |
| 5248 | // scope (either before or after the class declaration granting |
| 5249 | // friendship). |
| 5250 | // - If a friend function is called, its name may be found by the |
| 5251 | // name lookup that considers functions from namespaces and |
| 5252 | // classes associated with the types of the function arguments. |
| 5253 | // - When looking for a prior declaration of a class or a function |
| 5254 | // declared as a friend, scopes outside the innermost enclosing |
| 5255 | // namespace scope are not considered. |
| 5256 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5257 | CXXScopeSpec &ScopeQual = D.getCXXScopeSpec(); |
| 5258 | DeclarationName Name = GetNameForDeclarator(D); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5259 | assert(Name); |
| 5260 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5261 | // The context we found the declaration in, or in which we should |
| 5262 | // create the declaration. |
| 5263 | DeclContext *DC; |
| 5264 | |
| 5265 | // FIXME: handle local classes |
| 5266 | |
| 5267 | // Recover from invalid scope qualifiers as if they just weren't there. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5268 | LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName, |
| 5269 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5270 | if (!ScopeQual.isInvalid() && ScopeQual.isSet()) { |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5271 | // FIXME: RequireCompleteDeclContext |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5272 | DC = computeDeclContext(ScopeQual); |
| 5273 | |
| 5274 | // FIXME: handle dependent contexts |
| 5275 | if (!DC) return DeclPtrTy(); |
| 5276 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5277 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5278 | |
| 5279 | // If searching in that context implicitly found a declaration in |
| 5280 | // a different context, treat it like it wasn't found at all. |
| 5281 | // TODO: better diagnostics for this case. Suggesting the right |
| 5282 | // qualified scope would be nice... |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5283 | // FIXME: getRepresentativeDecl() is not right here at all |
| 5284 | if (Previous.empty() || |
| 5285 | !Previous.getRepresentativeDecl()->getDeclContext()->Equals(DC)) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5286 | D.setInvalidType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5287 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 5288 | return DeclPtrTy(); |
| 5289 | } |
| 5290 | |
| 5291 | // C++ [class.friend]p1: A friend of a class is a function or |
| 5292 | // class that is not a member of the class . . . |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5293 | if (DC->Equals(CurContext)) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5294 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 5295 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5296 | // Otherwise walk out to the nearest namespace scope looking for matches. |
| 5297 | } else { |
| 5298 | // TODO: handle local class contexts. |
| 5299 | |
| 5300 | DC = CurContext; |
| 5301 | while (true) { |
| 5302 | // Skip class contexts. If someone can cite chapter and verse |
| 5303 | // for this behavior, that would be nice --- it's what GCC and |
| 5304 | // EDG do, and it seems like a reasonable intent, but the spec |
| 5305 | // really only says that checks for unqualified existing |
| 5306 | // declarations should stop at the nearest enclosing namespace, |
| 5307 | // not that they should only consider the nearest enclosing |
| 5308 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5309 | while (DC->isRecord()) |
| 5310 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5311 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5312 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5313 | |
| 5314 | // TODO: decide what we think about using declarations. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5315 | if (!Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5316 | break; |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5317 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5318 | if (DC->isFileContext()) break; |
| 5319 | DC = DC->getParent(); |
| 5320 | } |
| 5321 | |
| 5322 | // C++ [class.friend]p1: A friend of a class is a function or |
| 5323 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 5324 | // C++0x changes this for both friend types and functions. |
| 5325 | // Most C++ 98 compilers do seem to give an error here, so |
| 5326 | // we do, too. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5327 | if (!Previous.empty() && DC->Equals(CurContext) |
| 5328 | && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5329 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 5330 | } |
| 5331 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5332 | if (DC->isFileContext()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5333 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5334 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 5335 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 5336 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5337 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5338 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 5339 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5340 | return DeclPtrTy(); |
| 5341 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5342 | } |
| 5343 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5344 | bool Redeclaration = false; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5345 | NamedDecl *ND = ActOnFunctionDeclarator(S, D, DC, T, TInfo, Previous, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5346 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 5347 | IsDefinition, |
| 5348 | Redeclaration); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5349 | if (!ND) return DeclPtrTy(); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5350 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5351 | assert(ND->getDeclContext() == DC); |
| 5352 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 5353 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5354 | // Add the function declaration to the appropriate lookup tables, |
| 5355 | // adjusting the redeclarations list as necessary. We don't |
| 5356 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5357 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5358 | // Also update the scope-based lookup if the target context's |
| 5359 | // lookup context is in lexical scope. |
| 5360 | if (!CurContext->isDependentContext()) { |
| 5361 | DC = DC->getLookupContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5362 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5363 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5364 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5365 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5366 | |
| 5367 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5368 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5369 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 5370 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5371 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5372 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5373 | return DeclPtrTy::make(ND); |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 5374 | } |
| 5375 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5376 | void Sema::SetDeclDeleted(DeclPtrTy dcl, SourceLocation DelLoc) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 5377 | AdjustDeclIfTemplate(dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5378 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5379 | Decl *Dcl = dcl.getAs<Decl>(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5380 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 5381 | if (!Fn) { |
| 5382 | Diag(DelLoc, diag::err_deleted_non_function); |
| 5383 | return; |
| 5384 | } |
| 5385 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 5386 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 5387 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 5388 | // If the declaration wasn't the first, we delete the function anyway for |
| 5389 | // recovery. |
| 5390 | } |
| 5391 | Fn->setDeleted(); |
| 5392 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 5393 | |
| 5394 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 5395 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 5396 | ++CI) { |
| 5397 | Stmt *SubStmt = *CI; |
| 5398 | if (!SubStmt) |
| 5399 | continue; |
| 5400 | if (isa<ReturnStmt>(SubStmt)) |
| 5401 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 5402 | diag::err_return_in_constructor_handler); |
| 5403 | if (!isa<Expr>(SubStmt)) |
| 5404 | SearchForReturnInStmt(Self, SubStmt); |
| 5405 | } |
| 5406 | } |
| 5407 | |
| 5408 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 5409 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 5410 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 5411 | SearchForReturnInStmt(*this, Handler); |
| 5412 | } |
| 5413 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5414 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5415 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5416 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5417 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 5418 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5419 | |
| 5420 | QualType CNewTy = Context.getCanonicalType(NewTy); |
| 5421 | QualType COldTy = Context.getCanonicalType(OldTy); |
| 5422 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5423 | if (CNewTy == COldTy && |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5424 | CNewTy.getLocalCVRQualifiers() == COldTy.getLocalCVRQualifiers()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5425 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5426 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5427 | // Check if the return types are covariant |
| 5428 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5429 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5430 | /// Both types must be pointers or references to classes. |
| 5431 | if (PointerType *NewPT = dyn_cast<PointerType>(NewTy)) { |
| 5432 | if (PointerType *OldPT = dyn_cast<PointerType>(OldTy)) { |
| 5433 | NewClassTy = NewPT->getPointeeType(); |
| 5434 | OldClassTy = OldPT->getPointeeType(); |
| 5435 | } |
| 5436 | } else if (ReferenceType *NewRT = dyn_cast<ReferenceType>(NewTy)) { |
| 5437 | if (ReferenceType *OldRT = dyn_cast<ReferenceType>(OldTy)) { |
| 5438 | NewClassTy = NewRT->getPointeeType(); |
| 5439 | OldClassTy = OldRT->getPointeeType(); |
| 5440 | } |
| 5441 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5442 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5443 | // The return types aren't either both pointers or references to a class type. |
| 5444 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5445 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5446 | diag::err_different_return_type_for_overriding_virtual_function) |
| 5447 | << New->getDeclName() << NewTy << OldTy; |
| 5448 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5449 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5450 | return true; |
| 5451 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5452 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5453 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5454 | // Check if the new class derives from the old class. |
| 5455 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 5456 | Diag(New->getLocation(), |
| 5457 | diag::err_covariant_return_not_derived) |
| 5458 | << New->getDeclName() << NewTy << OldTy; |
| 5459 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5460 | return true; |
| 5461 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5462 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5463 | // Check if we the conversion from derived to base is valid. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5464 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5465 | diag::err_covariant_return_inaccessible_base, |
| 5466 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 5467 | // FIXME: Should this point to the return type? |
| 5468 | New->getLocation(), SourceRange(), New->getDeclName())) { |
| 5469 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5470 | return true; |
| 5471 | } |
| 5472 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5473 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5474 | // The qualifiers of the return types must be the same. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5475 | if (CNewTy.getLocalCVRQualifiers() != COldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5476 | Diag(New->getLocation(), |
| 5477 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5478 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5479 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5480 | return true; |
| 5481 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5482 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5483 | |
| 5484 | // The new class type must have the same or less qualifiers as the old type. |
| 5485 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 5486 | Diag(New->getLocation(), |
| 5487 | diag::err_covariant_return_type_class_type_more_qualified) |
| 5488 | << New->getDeclName() << NewTy << OldTy; |
| 5489 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5490 | return true; |
| 5491 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5492 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5493 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5494 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5495 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5496 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
| 5497 | const CXXMethodDecl *Old) |
| 5498 | { |
| 5499 | if (Old->hasAttr<FinalAttr>()) { |
| 5500 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 5501 | << New->getDeclName(); |
| 5502 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5503 | return true; |
| 5504 | } |
| 5505 | |
| 5506 | return false; |
| 5507 | } |
| 5508 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 5509 | /// \brief Mark the given method pure. |
| 5510 | /// |
| 5511 | /// \param Method the method to be marked pure. |
| 5512 | /// |
| 5513 | /// \param InitRange the source range that covers the "0" initializer. |
| 5514 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
| 5515 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 5516 | Method->setPure(); |
| 5517 | |
| 5518 | // A class is abstract if at least one function is pure virtual. |
| 5519 | Method->getParent()->setAbstract(true); |
| 5520 | return false; |
| 5521 | } |
| 5522 | |
| 5523 | if (!Method->isInvalidDecl()) |
| 5524 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 5525 | << Method->getDeclName() << InitRange; |
| 5526 | return true; |
| 5527 | } |
| 5528 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5529 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse an |
| 5530 | /// initializer for the declaration 'Dcl'. |
| 5531 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 5532 | /// static data member of class X, names should be looked up in the scope of |
| 5533 | /// class X. |
| 5534 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 5535 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5536 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5537 | Decl *D = Dcl.getAs<Decl>(); |
| 5538 | // If there is no declaration, there was an error parsing it. |
| 5539 | if (D == 0) |
| 5540 | return; |
| 5541 | |
| 5542 | // Check whether it is a declaration with a nested name specifier like |
| 5543 | // int foo::bar; |
| 5544 | if (!D->isOutOfLine()) |
| 5545 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5546 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5547 | // C++ [basic.lookup.unqual]p13 |
| 5548 | // |
| 5549 | // A name used in the definition of a static data member of class X |
| 5550 | // (after the qualified-id of the static member) is looked up as if the name |
| 5551 | // was used in a member function of X. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5552 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5553 | // Change current context into the context of the initializing declaration. |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 5554 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
| 5557 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
| 5558 | /// initializer for the declaration 'Dcl'. |
| 5559 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 5560 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5561 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5562 | Decl *D = Dcl.getAs<Decl>(); |
| 5563 | // If there is no declaration, there was an error parsing it. |
| 5564 | if (D == 0) |
| 5565 | return; |
| 5566 | |
| 5567 | // Check whether it is a declaration with a nested name specifier like |
| 5568 | // int foo::bar; |
| 5569 | if (!D->isOutOfLine()) |
| 5570 | return; |
| 5571 | |
| 5572 | assert(S->getEntity() == D->getDeclContext() && "Context imbalance!"); |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 5573 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5574 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 5575 | |
| 5576 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 5577 | /// C++ if/switch/while/for statement. |
| 5578 | /// e.g: "if (int x = f()) {...}" |
| 5579 | Action::DeclResult |
| 5580 | Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
| 5581 | // C++ 6.4p2: |
| 5582 | // The declarator shall not specify a function or an array. |
| 5583 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 5584 | // new class or enumeration. |
| 5585 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 5586 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 5587 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5588 | TypeSourceInfo *TInfo = 0; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 5589 | TagDecl *OwnedTag = 0; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5590 | QualType Ty = GetTypeForDeclarator(D, S, &TInfo, &OwnedTag); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 5591 | |
| 5592 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 5593 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 5594 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 5595 | Diag(D.getIdentifierLoc(), diag::err_invalid_use_of_function_type) |
| 5596 | << D.getSourceRange(); |
| 5597 | return DeclResult(); |
| 5598 | } else if (OwnedTag && OwnedTag->isDefinition()) { |
| 5599 | // The type-specifier-seq shall not declare a new class or enumeration. |
| 5600 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition); |
| 5601 | } |
| 5602 | |
| 5603 | DeclPtrTy Dcl = ActOnDeclarator(S, D); |
| 5604 | if (!Dcl) |
| 5605 | return DeclResult(); |
| 5606 | |
| 5607 | VarDecl *VD = cast<VarDecl>(Dcl.getAs<Decl>()); |
| 5608 | VD->setDeclaredInCondition(true); |
| 5609 | return Dcl; |
| 5610 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5611 | |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5612 | void Sema::MaybeMarkVirtualMembersReferenced(SourceLocation Loc, |
| 5613 | CXXMethodDecl *MD) { |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5614 | // Ignore dependent types. |
| 5615 | if (MD->isDependentContext()) |
| 5616 | return; |
| 5617 | |
| 5618 | CXXRecordDecl *RD = MD->getParent(); |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 5619 | |
| 5620 | // Ignore classes without a vtable. |
| 5621 | if (!RD->isDynamicClass()) |
| 5622 | return; |
| 5623 | |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5624 | if (!MD->isOutOfLine()) { |
| 5625 | // The only inline functions we care about are constructors. We also defer |
| 5626 | // marking the virtual members as referenced until we've reached the end |
| 5627 | // of the translation unit. We do this because we need to know the key |
| 5628 | // function of the class in order to determine the key function. |
| 5629 | if (isa<CXXConstructorDecl>(MD)) |
| 5630 | ClassesWithUnmarkedVirtualMembers.insert(std::make_pair(RD, Loc)); |
| 5631 | return; |
| 5632 | } |
| 5633 | |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 5634 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(RD); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5635 | |
| 5636 | if (!KeyFunction) { |
| 5637 | // This record does not have a key function, so we assume that the vtable |
| 5638 | // will be emitted when it's used by the constructor. |
| 5639 | if (!isa<CXXConstructorDecl>(MD)) |
| 5640 | return; |
| 5641 | } else if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl()) { |
| 5642 | // We don't have the right key function. |
| 5643 | return; |
| 5644 | } |
| 5645 | |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5646 | // Mark the members as referenced. |
| 5647 | MarkVirtualMembersReferenced(Loc, RD); |
| 5648 | ClassesWithUnmarkedVirtualMembers.erase(RD); |
| 5649 | } |
| 5650 | |
| 5651 | bool Sema::ProcessPendingClassesWithUnmarkedVirtualMembers() { |
| 5652 | if (ClassesWithUnmarkedVirtualMembers.empty()) |
| 5653 | return false; |
| 5654 | |
| 5655 | for (std::map<CXXRecordDecl *, SourceLocation>::iterator i = |
| 5656 | ClassesWithUnmarkedVirtualMembers.begin(), |
| 5657 | e = ClassesWithUnmarkedVirtualMembers.end(); i != e; ++i) { |
| 5658 | CXXRecordDecl *RD = i->first; |
| 5659 | |
| 5660 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(RD); |
| 5661 | if (KeyFunction) { |
| 5662 | // We know that the class has a key function. If the key function was |
| 5663 | // declared in this translation unit, then it the class decl would not |
| 5664 | // have been in the ClassesWithUnmarkedVirtualMembers map. |
| 5665 | continue; |
| 5666 | } |
| 5667 | |
| 5668 | SourceLocation Loc = i->second; |
| 5669 | MarkVirtualMembersReferenced(Loc, RD); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5670 | } |
| 5671 | |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5672 | ClassesWithUnmarkedVirtualMembers.clear(); |
| 5673 | return true; |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5674 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5675 | |
| 5676 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, CXXRecordDecl *RD) { |
| 5677 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 5678 | e = RD->method_end(); i != e; ++i) { |
| 5679 | CXXMethodDecl *MD = *i; |
| 5680 | |
| 5681 | // C++ [basic.def.odr]p2: |
| 5682 | // [...] A virtual member function is used if it is not pure. [...] |
| 5683 | if (MD->isVirtual() && !MD->isPure()) |
| 5684 | MarkDeclarationReferenced(Loc, MD); |
| 5685 | } |
| 5686 | } |
| 5687 | |