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 | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 112 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 113 | diag::err_typecheck_decl_incomplete_type)) { |
| 114 | Param->setInvalidDecl(); |
| 115 | return true; |
| 116 | } |
| 117 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 118 | Expr *Arg = (Expr *)DefaultArg.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 120 | // C++ [dcl.fct.default]p5 |
| 121 | // A default argument expression is implicitly converted (clause |
| 122 | // 4) to the parameter type. The default argument expression has |
| 123 | // the same semantic constraints as the initializer expression in |
| 124 | // a declaration of a variable of the parameter type, using the |
| 125 | // copy-initialization semantics (8.5). |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 126 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Param); |
| 127 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 128 | EqualLoc); |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 129 | InitializationSequence InitSeq(*this, Entity, Kind, &Arg, 1); |
| 130 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
| 131 | MultiExprArg(*this, (void**)&Arg, 1)); |
| 132 | if (Result.isInvalid()) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 133 | return true; |
Eli Friedman | 4a2c19b | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 134 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 135 | |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 136 | Arg = MaybeCreateCXXExprWithTemporaries(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 138 | // Okay: add the default argument to the parameter |
| 139 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 141 | DefaultArg.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 143 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 146 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 147 | /// provided for a function parameter is well-formed. If so, attach it |
| 148 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 149 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | Sema::ActOnParamDefaultArgument(DeclPtrTy param, SourceLocation EqualLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 151 | ExprArg defarg) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 152 | if (!param || !defarg.get()) |
| 153 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 155 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 156 | UnparsedDefaultArgLocs.erase(Param); |
| 157 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 158 | ExprOwningPtr<Expr> DefaultArg(this, defarg.takeAs<Expr>()); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 159 | |
| 160 | // Default arguments are only permitted in C++ |
| 161 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 162 | Diag(EqualLoc, diag::err_param_default_argument) |
| 163 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 164 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 168 | // Check that the default argument is well-formed |
| 169 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); |
| 170 | if (DefaultArgChecker.Visit(DefaultArg.get())) { |
| 171 | Param->setInvalidDecl(); |
| 172 | return; |
| 173 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 175 | SetParamDefaultArgument(Param, move(DefaultArg), EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 178 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 179 | /// argument for a function parameter, but we can't parse it yet |
| 180 | /// because we're inside a class definition. Note that this default |
| 181 | /// argument will be parsed later. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | void Sema::ActOnParamUnparsedDefaultArgument(DeclPtrTy param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 183 | SourceLocation EqualLoc, |
| 184 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 185 | if (!param) |
| 186 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 188 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 189 | if (Param) |
| 190 | Param->setUnparsedDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 192 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 195 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 196 | /// the default argument for the parameter param failed. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 197 | void Sema::ActOnParamDefaultArgumentError(DeclPtrTy param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 198 | if (!param) |
| 199 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 201 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 203 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 205 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 208 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 209 | /// arguments in the declarator, which is not a function declaration |
| 210 | /// or definition and therefore is not permitted to have default |
| 211 | /// arguments. This routine should be invoked for every declarator |
| 212 | /// that is not a function declaration or definition. |
| 213 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 214 | // C++ [dcl.fct.default]p3 |
| 215 | // A default argument expression shall be specified only in the |
| 216 | // parameter-declaration-clause of a function declaration or in a |
| 217 | // template-parameter (14.1). It shall not be specified for a |
| 218 | // parameter pack. If it is specified in a |
| 219 | // parameter-declaration-clause, it shall not occur within a |
| 220 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 221 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 222 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 223 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 224 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 225 | ParmVarDecl *Param = |
| 226 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 227 | if (Param->hasUnparsedDefaultArg()) { |
| 228 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 229 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 230 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 231 | delete Toks; |
| 232 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 233 | } else if (Param->getDefaultArg()) { |
| 234 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 235 | << Param->getDefaultArg()->getSourceRange(); |
| 236 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 243 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 244 | // function, once we already know that they have the same |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 245 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 246 | // error, false otherwise. |
| 247 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 248 | bool Invalid = false; |
| 249 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 250 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 251 | // For non-template functions, default arguments can be added in |
| 252 | // later declarations of a function in the same |
| 253 | // scope. Declarations in different scopes have completely |
| 254 | // distinct sets of default arguments. That is, declarations in |
| 255 | // inner scopes do not acquire default arguments from |
| 256 | // declarations in outer scopes, and vice versa. In a given |
| 257 | // function declaration, all parameters subsequent to a |
| 258 | // parameter with a default argument shall have default |
| 259 | // arguments supplied in this or previous declarations. A |
| 260 | // default argument shall not be redefined by a later |
| 261 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 262 | // |
| 263 | // C++ [dcl.fct.default]p6: |
| 264 | // Except for member functions of class templates, the default arguments |
| 265 | // in a member function definition that appears outside of the class |
| 266 | // definition are added to the set of default arguments provided by the |
| 267 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 268 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 269 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 270 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 271 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 272 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 273 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 274 | // hint here. Alternatively, we could walk the type-source information |
| 275 | // for NewParam to find the last source location in the type... but it |
| 276 | // isn't worth the effort right now. This is the kind of test case that |
| 277 | // is hard to get right: |
| 278 | |
| 279 | // int f(int); |
| 280 | // void g(int (*fp)(int) = f); |
| 281 | // void g(int (*fp)(int) = &f); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | Diag(NewParam->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 283 | diag::err_param_default_argument_redefinition) |
Douglas Gregor | 4f123ff | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 284 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 285 | |
| 286 | // Look for the function declaration where the default argument was |
| 287 | // actually written, which may be a declaration prior to Old. |
| 288 | for (FunctionDecl *Older = Old->getPreviousDeclaration(); |
| 289 | Older; Older = Older->getPreviousDeclaration()) { |
| 290 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 291 | break; |
| 292 | |
| 293 | OldParam = Older->getParamDecl(p); |
| 294 | } |
| 295 | |
| 296 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 297 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 298 | Invalid = true; |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 299 | } else if (OldParam->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 300 | // Merge the old default argument into the new parameter |
John McCall | bf73b35 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 301 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 302 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 303 | NewParam->setUninstantiatedDefaultArg( |
| 304 | OldParam->getUninstantiatedDefaultArg()); |
| 305 | else |
| 306 | NewParam->setDefaultArg(OldParam->getDefaultArg()); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 307 | } else if (NewParam->hasDefaultArg()) { |
| 308 | if (New->getDescribedFunctionTemplate()) { |
| 309 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 310 | Diag(NewParam->getLocation(), |
| 311 | diag::err_param_default_argument_template_redecl) |
| 312 | << NewParam->getDefaultArgRange(); |
| 313 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 314 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 315 | } else if (New->getTemplateSpecializationKind() |
| 316 | != TSK_ImplicitInstantiation && |
| 317 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 318 | // C++ [temp.expr.spec]p21: |
| 319 | // Default function arguments shall not be specified in a declaration |
| 320 | // or a definition for one of the following explicit specializations: |
| 321 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 322 | // - the explicit specialization of a member function template; |
| 323 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 324 | // template where the class template specialization to which the |
| 325 | // member function specialization belongs is implicitly |
| 326 | // instantiated. |
| 327 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 328 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 329 | << New->getDeclName() |
| 330 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 331 | } else if (New->getDeclContext()->isDependentContext()) { |
| 332 | // C++ [dcl.fct.default]p6 (DR217): |
| 333 | // Default arguments for a member function of a class template shall |
| 334 | // be specified on the initial declaration of the member function |
| 335 | // within the class template. |
| 336 | // |
| 337 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 338 | // leads me to the conclusion that one cannot add default function |
| 339 | // arguments for an out-of-line definition of a member function of a |
| 340 | // dependent type. |
| 341 | int WhichKind = 2; |
| 342 | if (CXXRecordDecl *Record |
| 343 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 344 | if (Record->getDescribedClassTemplate()) |
| 345 | WhichKind = 0; |
| 346 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 347 | WhichKind = 1; |
| 348 | else |
| 349 | WhichKind = 2; |
| 350 | } |
| 351 | |
| 352 | Diag(NewParam->getLocation(), |
| 353 | diag::err_param_default_argument_member_template_redecl) |
| 354 | << WhichKind |
| 355 | << NewParam->getDefaultArgRange(); |
| 356 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 360 | if (CheckEquivalentExceptionSpec(Old, New)) |
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) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 426 | assert(getLangOptions().CPlusPlus && "No class names in C!"); |
| 427 | |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 428 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 429 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 430 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 431 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 432 | } else |
| 433 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 434 | |
Douglas Gregor | 6f7a17b | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 435 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 436 | return &II == CurDecl->getIdentifier(); |
| 437 | else |
| 438 | return false; |
| 439 | } |
| 440 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 442 | /// |
| 443 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 444 | /// and returns NULL otherwise. |
| 445 | CXXBaseSpecifier * |
| 446 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 447 | SourceRange SpecifierRange, |
| 448 | bool Virtual, AccessSpecifier Access, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 449 | QualType BaseType, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 450 | SourceLocation BaseLoc) { |
| 451 | // C++ [class.union]p1: |
| 452 | // A union shall not have base classes. |
| 453 | if (Class->isUnion()) { |
| 454 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 455 | << SpecifierRange; |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | if (BaseType->isDependentType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 461 | Class->getTagKind() == RecordDecl::TK_class, |
| 462 | Access, BaseType); |
| 463 | |
| 464 | // Base specifiers must be record types. |
| 465 | if (!BaseType->isRecordType()) { |
| 466 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | // C++ [class.union]p1: |
| 471 | // A union shall not be used as a base class. |
| 472 | if (BaseType->isUnionType()) { |
| 473 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | // C++ [class.derived]p2: |
| 478 | // The class-name in a base-specifier shall not be an incompletely |
| 479 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 481 | PDiag(diag::err_incomplete_base_class) |
| 482 | << SpecifierRange)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 483 | return 0; |
| 484 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 485 | // 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] | 486 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 487 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 488 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 489 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 490 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 491 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 492 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 493 | // C++0x CWG Issue #817 indicates that [[final]] classes shouldn't be bases. |
| 494 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
| 495 | Diag(BaseLoc, diag::err_final_base) << BaseType.getAsString(); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 496 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 497 | << BaseType; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 498 | return 0; |
| 499 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 500 | |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 501 | SetClassDeclAttributesFromBase(Class, CXXBaseDecl, Virtual); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 502 | |
| 503 | // Create the base specifier. |
| 504 | // FIXME: Allocate via ASTContext? |
| 505 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
| 506 | Class->getTagKind() == RecordDecl::TK_class, |
| 507 | Access, BaseType); |
| 508 | } |
| 509 | |
| 510 | void Sema::SetClassDeclAttributesFromBase(CXXRecordDecl *Class, |
| 511 | const CXXRecordDecl *BaseClass, |
| 512 | bool BaseIsVirtual) { |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 513 | // A class with a non-empty base class is not empty. |
| 514 | // FIXME: Standard ref? |
| 515 | if (!BaseClass->isEmpty()) |
| 516 | Class->setEmpty(false); |
| 517 | |
| 518 | // C++ [class.virtual]p1: |
| 519 | // A class that [...] inherits a virtual function is called a polymorphic |
| 520 | // class. |
| 521 | if (BaseClass->isPolymorphic()) |
| 522 | Class->setPolymorphic(true); |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 523 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 524 | // C++ [dcl.init.aggr]p1: |
| 525 | // An aggregate is [...] a class with [...] no base classes [...]. |
| 526 | Class->setAggregate(false); |
Eli Friedman | d013733 | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 527 | |
| 528 | // C++ [class]p4: |
| 529 | // A POD-struct is an aggregate class... |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 530 | Class->setPOD(false); |
| 531 | |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 532 | if (BaseIsVirtual) { |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 533 | // C++ [class.ctor]p5: |
| 534 | // A constructor is trivial if its class has no virtual base classes. |
| 535 | Class->setHasTrivialConstructor(false); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 536 | |
| 537 | // C++ [class.copy]p6: |
| 538 | // A copy constructor is trivial if its class has no virtual base classes. |
| 539 | Class->setHasTrivialCopyConstructor(false); |
| 540 | |
| 541 | // C++ [class.copy]p11: |
| 542 | // A copy assignment operator is trivial if its class has no virtual |
| 543 | // base classes. |
| 544 | Class->setHasTrivialCopyAssignment(false); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 545 | |
| 546 | // C++0x [meta.unary.prop] is_empty: |
| 547 | // T is a class type, but not a union type, with ... no virtual base |
| 548 | // classes |
| 549 | Class->setEmpty(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 550 | } else { |
| 551 | // C++ [class.ctor]p5: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | // A constructor is trivial if all the direct base classes of its |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 553 | // class have trivial constructors. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 554 | if (!BaseClass->hasTrivialConstructor()) |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 555 | Class->setHasTrivialConstructor(false); |
| 556 | |
| 557 | // C++ [class.copy]p6: |
| 558 | // A copy constructor is trivial if all the direct base classes of its |
| 559 | // class have trivial copy constructors. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 560 | if (!BaseClass->hasTrivialCopyConstructor()) |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 561 | Class->setHasTrivialCopyConstructor(false); |
| 562 | |
| 563 | // C++ [class.copy]p11: |
| 564 | // A copy assignment operator is trivial if all the direct base classes |
| 565 | // of its class have trivial copy assignment operators. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 566 | if (!BaseClass->hasTrivialCopyAssignment()) |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 567 | Class->setHasTrivialCopyAssignment(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 568 | } |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 569 | |
| 570 | // C++ [class.ctor]p3: |
| 571 | // A destructor is trivial if all the direct base classes of its class |
| 572 | // have trivial destructors. |
Anders Carlsson | 51f9404 | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 573 | if (!BaseClass->hasTrivialDestructor()) |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 574 | Class->setHasTrivialDestructor(false); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 577 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 578 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | /// example: |
| 580 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 581 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | Sema::BaseResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 583 | Sema::ActOnBaseSpecifier(DeclPtrTy classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 584 | bool Virtual, AccessSpecifier Access, |
| 585 | TypeTy *basetype, SourceLocation BaseLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 586 | if (!classdecl) |
| 587 | return true; |
| 588 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 589 | AdjustDeclIfTemplate(classdecl); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 590 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl.getAs<Decl>()); |
| 591 | if (!Class) |
| 592 | return true; |
| 593 | |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 594 | QualType BaseType = GetTypeFromParser(basetype); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 595 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
| 596 | Virtual, Access, |
| 597 | BaseType, BaseLoc)) |
| 598 | return BaseSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 600 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 601 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 602 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 603 | /// \brief Performs the actual work of attaching the given base class |
| 604 | /// specifiers to a C++ class. |
| 605 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 606 | unsigned NumBases) { |
| 607 | if (NumBases == 0) |
| 608 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 609 | |
| 610 | // Used to keep track of which base types we have already seen, so |
| 611 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 612 | // that the key is always the unqualified canonical type of the base |
| 613 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 614 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 615 | |
| 616 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 617 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 618 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 619 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 621 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 622 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 623 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 624 | if (KnownBaseTypes[NewBaseType]) { |
| 625 | // C++ [class.mi]p3: |
| 626 | // A class shall not be specified as a direct base class of a |
| 627 | // derived class more than once. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 628 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 629 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 630 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 631 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 632 | |
| 633 | // Delete the duplicate base class specifier; we're going to |
| 634 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 635 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 636 | |
| 637 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 638 | } else { |
| 639 | // Okay, add this new base class. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 640 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 641 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
| 645 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 646 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 647 | |
| 648 | // Delete the remaining (good) base class specifiers, since their |
| 649 | // data has been copied into the CXXRecordDecl. |
| 650 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 651 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 652 | |
| 653 | return Invalid; |
| 654 | } |
| 655 | |
| 656 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 657 | /// class, after checking whether there are any duplicate base |
| 658 | /// classes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | void Sema::ActOnBaseSpecifiers(DeclPtrTy ClassDecl, BaseTy **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 660 | unsigned NumBases) { |
| 661 | if (!ClassDecl || !Bases || !NumBases) |
| 662 | return; |
| 663 | |
| 664 | AdjustDeclIfTemplate(ClassDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 665 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl.getAs<Decl>()), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 666 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 667 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 668 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 669 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 670 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 671 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 672 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 673 | return ICT->getDecl(); |
| 674 | else |
| 675 | return 0; |
| 676 | } |
| 677 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 678 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 679 | /// derived from the type \p Base. |
| 680 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 681 | if (!getLangOptions().CPlusPlus) |
| 682 | return false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 683 | |
| 684 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 685 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 686 | return false; |
| 687 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 688 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 689 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 690 | return false; |
| 691 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 692 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 693 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 697 | /// derived from the type \p Base. |
| 698 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 699 | if (!getLangOptions().CPlusPlus) |
| 700 | return false; |
| 701 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 702 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 703 | if (!DerivedRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 704 | return false; |
| 705 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 706 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 707 | if (!BaseRD) |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 708 | return false; |
| 709 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 710 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 711 | } |
| 712 | |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 713 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
| 714 | CXXBaseSpecifierArray &BasePathArray) { |
| 715 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 716 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 717 | |
| 718 | const CXXBasePath &Path = Paths.front(); |
| 719 | |
| 720 | // We first go backward and check if we have a virtual base. |
| 721 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 722 | // the nearest virtual base. |
| 723 | unsigned Start = 0; |
| 724 | for (unsigned I = Path.size(); I != 0; --I) { |
| 725 | if (Path[I - 1].Base->isVirtual()) { |
| 726 | Start = I - 1; |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | // Now add all bases. |
| 732 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
| 733 | BasePathArray.push_back(Path[I].Base); |
| 734 | } |
| 735 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 736 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 737 | /// conversion (where Derived and Base are class types) is |
| 738 | /// well-formed, meaning that the conversion is unambiguous (and |
| 739 | /// that all of the base classes are accessible). Returns true |
| 740 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 741 | /// otherwise. Loc is the location where this routine should point to |
| 742 | /// if there is an error, and Range is the source range to highlight |
| 743 | /// if there is an error. |
| 744 | bool |
| 745 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 746 | unsigned InaccessibleBaseID, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 747 | unsigned AmbigiousBaseConvID, |
| 748 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 749 | DeclarationName Name, |
| 750 | CXXBaseSpecifierArray *BasePath) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 751 | // First, determine whether the path from Derived to Base is |
| 752 | // ambiguous. This is slightly more expensive than checking whether |
| 753 | // the Derived to Base conversion exists, because here we need to |
| 754 | // explore multiple paths to determine if there is an ambiguity. |
| 755 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 756 | /*DetectVirtual=*/false); |
| 757 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 758 | assert(DerivationOkay && |
| 759 | "Can only be used with a derived-to-base conversion"); |
| 760 | (void)DerivationOkay; |
| 761 | |
| 762 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 763 | if (InaccessibleBaseID) { |
| 764 | // Check that the base class can be accessed. |
| 765 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 766 | InaccessibleBaseID)) { |
| 767 | case AR_inaccessible: |
| 768 | return true; |
| 769 | case AR_accessible: |
| 770 | case AR_dependent: |
| 771 | case AR_delayed: |
| 772 | break; |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 773 | } |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 774 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 775 | |
| 776 | // Build a base path if necessary. |
| 777 | if (BasePath) |
| 778 | BuildBasePathArray(Paths, *BasePath); |
| 779 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | // We know that the derived-to-base conversion is ambiguous, and |
| 783 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 784 | // search just one more time to compute all of the possible paths so |
| 785 | // that we can print them out. This is more expensive than any of |
| 786 | // the previous derived-to-base checks we've done, but at this point |
| 787 | // performance isn't as much of an issue. |
| 788 | Paths.clear(); |
| 789 | Paths.setRecordingPaths(true); |
| 790 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 791 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 792 | (void)StillOkay; |
| 793 | |
| 794 | // Build up a textual representation of the ambiguous paths, e.g., |
| 795 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 796 | // conversions in the diagnostic. We only print one of the paths |
| 797 | // to each base class subobject. |
| 798 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 799 | |
| 800 | Diag(Loc, AmbigiousBaseConvID) |
| 801 | << Derived << Base << PathDisplayStr << Range << Name; |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | bool |
| 806 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 807 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 808 | CXXBaseSpecifierArray *BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 809 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 810 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 811 | IgnoreAccess ? 0 |
| 812 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 813 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 814 | Loc, Range, DeclarationName(), |
| 815 | BasePath); |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | |
| 819 | /// @brief Builds a string representing ambiguous paths from a |
| 820 | /// specific derived class to different subobjects of the same base |
| 821 | /// class. |
| 822 | /// |
| 823 | /// This function builds a string that can be used in error messages |
| 824 | /// to show the different paths that one can take through the |
| 825 | /// inheritance hierarchy to go from the derived class to different |
| 826 | /// subobjects of a base class. The result looks something like this: |
| 827 | /// @code |
| 828 | /// struct D -> struct B -> struct A |
| 829 | /// struct D -> struct C -> struct A |
| 830 | /// @endcode |
| 831 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 832 | std::string PathDisplayStr; |
| 833 | std::set<unsigned> DisplayedPaths; |
| 834 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 835 | Path != Paths.end(); ++Path) { |
| 836 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 837 | // We haven't displayed a path to this particular base |
| 838 | // class subobject yet. |
| 839 | PathDisplayStr += "\n "; |
| 840 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 841 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 842 | Element != Path->end(); ++Element) |
| 843 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | return PathDisplayStr; |
| 848 | } |
| 849 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 850 | //===----------------------------------------------------------------------===// |
| 851 | // C++ class member Handling |
| 852 | //===----------------------------------------------------------------------===// |
| 853 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 854 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 855 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 856 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
Chris Lattner | b6688e0 | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 857 | /// any. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 858 | Sema::DeclPtrTy |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 859 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 860 | MultiTemplateParamsArg TemplateParameterLists, |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 861 | ExprTy *BW, ExprTy *InitExpr, bool IsDefinition, |
| 862 | bool Deleted) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 863 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 864 | DeclarationName Name = GetNameForDeclarator(D); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 865 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 866 | Expr *Init = static_cast<Expr*>(InitExpr); |
| 867 | SourceLocation Loc = D.getIdentifierLoc(); |
| 868 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 869 | bool isFunc = D.isFunctionDeclarator(); |
| 870 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 871 | assert(!DS.isFriendSpecified()); |
| 872 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 873 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 874 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 875 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 876 | // data members and cannot be applied to names declared const or static, |
| 877 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 878 | switch (DS.getStorageClassSpec()) { |
| 879 | case DeclSpec::SCS_unspecified: |
| 880 | case DeclSpec::SCS_typedef: |
| 881 | case DeclSpec::SCS_static: |
| 882 | // FALL THROUGH. |
| 883 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 884 | case DeclSpec::SCS_mutable: |
| 885 | if (isFunc) { |
| 886 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 887 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 888 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 889 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 891 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 892 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 893 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 894 | } else { |
| 895 | QualType T = GetTypeForDeclarator(D, S); |
| 896 | diag::kind err = static_cast<diag::kind>(0); |
| 897 | if (T->isReferenceType()) |
| 898 | err = diag::err_mutable_reference; |
| 899 | else if (T.isConstQualified()) |
| 900 | err = diag::err_mutable_const; |
| 901 | if (err != 0) { |
| 902 | if (DS.getStorageClassSpecLoc().isValid()) |
| 903 | Diag(DS.getStorageClassSpecLoc(), err); |
| 904 | else |
| 905 | Diag(DS.getThreadSpecLoc(), err); |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 906 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 907 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 908 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 909 | } |
| 910 | } |
| 911 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 912 | default: |
| 913 | if (DS.getStorageClassSpecLoc().isValid()) |
| 914 | Diag(DS.getStorageClassSpecLoc(), |
| 915 | diag::err_storageclass_invalid_for_member); |
| 916 | else |
| 917 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 918 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 919 | } |
| 920 | |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 921 | if (!isFunc && |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 922 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename && |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 923 | D.getNumTypeObjects() == 0) { |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 924 | // Check also for this case: |
| 925 | // |
| 926 | // typedef int f(); |
| 927 | // f a; |
| 928 | // |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 929 | QualType TDType = GetTypeFromParser(DS.getTypeRep()); |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 930 | isFunc = TDType->isFunctionType(); |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 931 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 932 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 933 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 934 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 935 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 936 | |
| 937 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 938 | if (isInstField) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 939 | // FIXME: Check for template parameters! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 940 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 941 | AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 942 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 943 | } else { |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 944 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition) |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 945 | .getAs<Decl>(); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 946 | if (!Member) { |
| 947 | if (BitWidth) DeleteExpr(BitWidth); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 948 | return DeclPtrTy(); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 949 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 950 | |
| 951 | // Non-instance-fields can't have a bitfield. |
| 952 | if (BitWidth) { |
| 953 | if (Member->isInvalidDecl()) { |
| 954 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 955 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 956 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 957 | // "static member 'A' cannot be a bit-field" |
| 958 | Diag(Loc, diag::err_static_not_bitfield) |
| 959 | << Name << BitWidth->getSourceRange(); |
| 960 | } else if (isa<TypedefDecl>(Member)) { |
| 961 | // "typedef member 'x' cannot be a bit-field" |
| 962 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 963 | << Name << BitWidth->getSourceRange(); |
| 964 | } else { |
| 965 | // A function typedef ("typedef int f(); f a;"). |
| 966 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 967 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 968 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 969 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 970 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 972 | DeleteExpr(BitWidth); |
| 973 | BitWidth = 0; |
| 974 | Member->setInvalidDecl(); |
| 975 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 976 | |
| 977 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 979 | // If we have declared a member function template, set the access of the |
| 980 | // templated declaration as well. |
| 981 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 982 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 983 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 984 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 985 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 986 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 987 | if (Init) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 988 | AddInitializerToDecl(DeclPtrTy::make(Member), ExprArg(*this, Init), false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 989 | if (Deleted) // FIXME: Source location is not very good. |
| 990 | SetDeclDeleted(DeclPtrTy::make(Member), D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 991 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 992 | if (isInstField) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 993 | FieldCollector->Add(cast<FieldDecl>(Member)); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 994 | return DeclPtrTy(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 995 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 996 | return DeclPtrTy::make(Member); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 999 | /// \brief Find the direct and/or virtual base specifiers that |
| 1000 | /// correspond to the given base type, for use in base initialization |
| 1001 | /// within a constructor. |
| 1002 | static bool FindBaseInitializer(Sema &SemaRef, |
| 1003 | CXXRecordDecl *ClassDecl, |
| 1004 | QualType BaseType, |
| 1005 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 1006 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 1007 | // First, check for a direct base class. |
| 1008 | DirectBaseSpec = 0; |
| 1009 | for (CXXRecordDecl::base_class_const_iterator Base |
| 1010 | = ClassDecl->bases_begin(); |
| 1011 | Base != ClassDecl->bases_end(); ++Base) { |
| 1012 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 1013 | // We found a direct base of this type. That's what we're |
| 1014 | // initializing. |
| 1015 | DirectBaseSpec = &*Base; |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | // Check for a virtual base class. |
| 1021 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1022 | // there are no virtual bases. |
| 1023 | VirtualBaseSpec = 0; |
| 1024 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1025 | // We haven't found a base yet; search the class hierarchy for a |
| 1026 | // virtual base class. |
| 1027 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1028 | /*DetectVirtual=*/false); |
| 1029 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1030 | BaseType, Paths)) { |
| 1031 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1032 | Path != Paths.end(); ++Path) { |
| 1033 | if (Path->back().Base->isVirtual()) { |
| 1034 | VirtualBaseSpec = Path->back().Base; |
| 1035 | break; |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | return DirectBaseSpec || VirtualBaseSpec; |
| 1042 | } |
| 1043 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1044 | /// ActOnMemInitializer - Handle a C++ member initializer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | Sema::MemInitResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1046 | Sema::ActOnMemInitializer(DeclPtrTy ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1047 | Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1048 | CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1049 | IdentifierInfo *MemberOrBase, |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1050 | TypeTy *TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1051 | SourceLocation IdLoc, |
| 1052 | SourceLocation LParenLoc, |
| 1053 | ExprTy **Args, unsigned NumArgs, |
| 1054 | SourceLocation *CommaLocs, |
| 1055 | SourceLocation RParenLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1056 | if (!ConstructorD) |
| 1057 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1059 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
| 1061 | CXXConstructorDecl *Constructor |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1062 | = dyn_cast<CXXConstructorDecl>(ConstructorD.getAs<Decl>()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1063 | if (!Constructor) { |
| 1064 | // The user wrote a constructor initializer on a function that is |
| 1065 | // not a C++ constructor. Ignore the error for now, because we may |
| 1066 | // have more member initializers coming; we'll diagnose it just |
| 1067 | // once in ActOnMemInitializers. |
| 1068 | return true; |
| 1069 | } |
| 1070 | |
| 1071 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1072 | |
| 1073 | // C++ [class.base.init]p2: |
| 1074 | // Names in a mem-initializer-id are looked up in the scope of the |
| 1075 | // constructor’s class and, if not found in that scope, are looked |
| 1076 | // up in the scope containing the constructor’s |
| 1077 | // definition. [Note: if the constructor’s class contains a member |
| 1078 | // with the same name as a direct or virtual base class of the |
| 1079 | // class, a mem-initializer-id naming the member or base class and |
| 1080 | // composed of a single identifier refers to the class member. A |
| 1081 | // mem-initializer-id for the hidden base class may be specified |
| 1082 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1083 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1084 | // Look for a member, first. |
| 1085 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1087 | = ClassDecl->lookup(MemberOrBase); |
| 1088 | if (Result.first != Result.second) |
| 1089 | Member = dyn_cast<FieldDecl>(*Result.first); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1090 | |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1091 | // FIXME: Handle members of an anonymous union. |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1092 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1093 | if (Member) |
| 1094 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1095 | LParenLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1096 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1097 | // 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] | 1098 | QualType BaseType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1099 | TypeSourceInfo *TInfo = 0; |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1100 | |
| 1101 | if (TemplateTypeTy) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1102 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1103 | } else { |
| 1104 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1105 | LookupParsedName(R, S, &SS); |
| 1106 | |
| 1107 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1108 | if (!TyD) { |
| 1109 | if (R.isAmbiguous()) return true; |
| 1110 | |
John McCall | fd22544 | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1111 | // We don't want access-control diagnostics here. |
| 1112 | R.suppressDiagnostics(); |
| 1113 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1114 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1115 | bool NotUnknownSpecialization = false; |
| 1116 | DeclContext *DC = computeDeclContext(SS, false); |
| 1117 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1118 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1119 | |
| 1120 | if (!NotUnknownSpecialization) { |
| 1121 | // When the scope specifier can refer to a member of an unknown |
| 1122 | // specialization, we take it as a type name. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 1123 | BaseType = CheckTypenameType(ETK_None, |
| 1124 | (NestedNameSpecifier *)SS.getScopeRep(), |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1125 | *MemberOrBase, SS.getRange()); |
Douglas Gregor | a50ce32 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1126 | if (BaseType.isNull()) |
| 1127 | return true; |
| 1128 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1129 | R.clear(); |
| 1130 | } |
| 1131 | } |
| 1132 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1133 | // If no results were found, try to correct typos. |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1134 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1135 | CorrectTypo(R, S, &SS, ClassDecl, 0, CTC_NoKeywords) && |
| 1136 | R.isSingleResult()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1137 | if (FieldDecl *Member = R.getAsSingle<FieldDecl>()) { |
| 1138 | if (Member->getDeclContext()->getLookupContext()->Equals(ClassDecl)) { |
| 1139 | // We have found a non-static data member with a similar |
| 1140 | // name to what was typed; complain and initialize that |
| 1141 | // member. |
| 1142 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1143 | << MemberOrBase << true << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1144 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1145 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1146 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 1147 | << Member->getDeclName(); |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1148 | |
| 1149 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 1150 | LParenLoc, RParenLoc); |
| 1151 | } |
| 1152 | } else if (TypeDecl *Type = R.getAsSingle<TypeDecl>()) { |
| 1153 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1154 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1155 | if (FindBaseInitializer(*this, ClassDecl, |
| 1156 | Context.getTypeDeclType(Type), |
| 1157 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1158 | // We have found a direct or virtual base class with a |
| 1159 | // similar name to what was typed; complain and initialize |
| 1160 | // that base class. |
| 1161 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1162 | << MemberOrBase << false << R.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1163 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1164 | R.getLookupName().getAsString()); |
Douglas Gregor | 0d535c8 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1165 | |
| 1166 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1167 | : VirtualBaseSpec; |
| 1168 | Diag(BaseSpec->getSourceRange().getBegin(), |
| 1169 | diag::note_base_class_specified_here) |
| 1170 | << BaseSpec->getType() |
| 1171 | << BaseSpec->getSourceRange(); |
| 1172 | |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1173 | TyD = Type; |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1178 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | fe0241e | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1179 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1180 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1181 | return true; |
| 1182 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1185 | if (BaseType.isNull()) { |
| 1186 | BaseType = Context.getTypeDeclType(TyD); |
| 1187 | if (SS.isSet()) { |
| 1188 | NestedNameSpecifier *Qualifier = |
| 1189 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1190 | |
Douglas Gregor | 7a886e1 | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1191 | // FIXME: preserve source range information |
| 1192 | BaseType = Context.getQualifiedNameType(Qualifier, BaseType); |
| 1193 | } |
John McCall | 2b19441 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1194 | } |
| 1195 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1197 | if (!TInfo) |
| 1198 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1199 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1200 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1201 | LParenLoc, RParenLoc, ClassDecl); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1204 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1205 | /// containing the field that is being initialized. Returns true if there is an |
| 1206 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1207 | /// otherwise. |
| 1208 | static bool InitExprContainsUninitializedFields(const Stmt* S, |
| 1209 | const FieldDecl* LhsField, |
| 1210 | SourceLocation* L) { |
| 1211 | const MemberExpr* ME = dyn_cast<MemberExpr>(S); |
| 1212 | if (ME) { |
| 1213 | const NamedDecl* RhsField = ME->getMemberDecl(); |
| 1214 | if (RhsField == LhsField) { |
| 1215 | // Initializing a field with itself. Throw a warning. |
| 1216 | // But wait; there are exceptions! |
| 1217 | // Exception #1: The field may not belong to this record. |
| 1218 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
| 1219 | const Expr* base = ME->getBase(); |
| 1220 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1221 | // Even though the field matches, it does not belong to this record. |
| 1222 | return false; |
| 1223 | } |
| 1224 | // None of the exceptions triggered; return true to indicate an |
| 1225 | // uninitialized field was used. |
| 1226 | *L = ME->getMemberLoc(); |
| 1227 | return true; |
| 1228 | } |
| 1229 | } |
| 1230 | bool found = false; |
| 1231 | for (Stmt::const_child_iterator it = S->child_begin(); |
| 1232 | it != S->child_end() && found == false; |
| 1233 | ++it) { |
| 1234 | if (isa<CallExpr>(S)) { |
| 1235 | // Do not descend into function calls or constructors, as the use |
| 1236 | // of an uninitialized field may be valid. One would have to inspect |
| 1237 | // the contents of the function/ctor to determine if it is safe or not. |
| 1238 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1239 | // may be safe, depending on what the function/ctor does. |
| 1240 | continue; |
| 1241 | } |
| 1242 | found = InitExprContainsUninitializedFields(*it, LhsField, L); |
| 1243 | } |
| 1244 | return found; |
| 1245 | } |
| 1246 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1247 | Sema::MemInitResult |
| 1248 | Sema::BuildMemberInitializer(FieldDecl *Member, Expr **Args, |
| 1249 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1250 | SourceLocation LParenLoc, |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1251 | SourceLocation RParenLoc) { |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1252 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1253 | // foo(foo) |
| 1254 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1255 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1256 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1257 | SourceLocation L; |
| 1258 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1259 | // FIXME: Return true in the case when other fields are used before being |
| 1260 | // uninitialized. For example, let this field be the i'th field. When |
| 1261 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1262 | // fields are used, as they are not yet initialized. |
| 1263 | // Right now we are only handling the case where the i'th field uses |
| 1264 | // itself in its initializer. |
| 1265 | Diag(L, diag::warn_field_is_uninit); |
| 1266 | } |
| 1267 | } |
| 1268 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1269 | bool HasDependentArg = false; |
| 1270 | for (unsigned i = 0; i < NumArgs; i++) |
| 1271 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1272 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1273 | QualType FieldType = Member->getType(); |
| 1274 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 1275 | FieldType = Array->getElementType(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1276 | if (FieldType->isDependentType() || HasDependentArg) { |
| 1277 | // Can't check initialization for a member of dependent type or when |
| 1278 | // any of the arguments are type-dependent expressions. |
| 1279 | OwningExprResult Init |
| 1280 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1281 | RParenLoc)); |
| 1282 | |
| 1283 | // Erase any temporaries within this evaluation context; we're not |
| 1284 | // going to track them in the AST, since we'll be rebuilding the |
| 1285 | // ASTs during template instantiation. |
| 1286 | ExprTemporaries.erase( |
| 1287 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1288 | ExprTemporaries.end()); |
| 1289 | |
| 1290 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1291 | LParenLoc, |
| 1292 | Init.takeAs<Expr>(), |
| 1293 | RParenLoc); |
| 1294 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1295 | } |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1296 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1297 | if (Member->isInvalidDecl()) |
| 1298 | return true; |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1299 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1300 | // Initialize the member. |
| 1301 | InitializedEntity MemberEntity = |
| 1302 | InitializedEntity::InitializeMember(Member, 0); |
| 1303 | InitializationKind Kind = |
| 1304 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc); |
| 1305 | |
| 1306 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 1307 | |
| 1308 | OwningExprResult MemberInit = |
| 1309 | InitSeq.Perform(*this, MemberEntity, Kind, |
| 1310 | MultiExprArg(*this, (void**)Args, NumArgs), 0); |
| 1311 | if (MemberInit.isInvalid()) |
| 1312 | return true; |
| 1313 | |
| 1314 | // C++0x [class.base.init]p7: |
| 1315 | // The initialization of each base and member constitutes a |
| 1316 | // full-expression. |
| 1317 | MemberInit = MaybeCreateCXXExprWithTemporaries(move(MemberInit)); |
| 1318 | if (MemberInit.isInvalid()) |
| 1319 | return true; |
| 1320 | |
| 1321 | // If we are in a dependent context, template instantiation will |
| 1322 | // perform this type-checking again. Just save the arguments that we |
| 1323 | // received in a ParenListExpr. |
| 1324 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1325 | // of the information that we have about the member |
| 1326 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1327 | // and this approach is far more likely to get the corner cases right. |
| 1328 | if (CurContext->isDependentContext()) { |
| 1329 | // Bump the reference count of all of the arguments. |
| 1330 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1331 | Args[I]->Retain(); |
| 1332 | |
| 1333 | OwningExprResult Init |
| 1334 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1335 | RParenLoc)); |
| 1336 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1337 | LParenLoc, |
| 1338 | Init.takeAs<Expr>(), |
| 1339 | RParenLoc); |
| 1340 | } |
| 1341 | |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1342 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1343 | LParenLoc, |
| 1344 | MemberInit.takeAs<Expr>(), |
| 1345 | RParenLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | Sema::MemInitResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1349 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1350 | Expr **Args, unsigned NumArgs, |
| 1351 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
| 1352 | CXXRecordDecl *ClassDecl) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1353 | bool HasDependentArg = false; |
| 1354 | for (unsigned i = 0; i < NumArgs; i++) |
| 1355 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1356 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1357 | SourceLocation BaseLoc = BaseTInfo->getTypeLoc().getSourceRange().getBegin(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1358 | if (BaseType->isDependentType() || HasDependentArg) { |
| 1359 | // Can't check initialization for a base of dependent type or when |
| 1360 | // any of the arguments are type-dependent expressions. |
| 1361 | OwningExprResult BaseInit |
| 1362 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1363 | RParenLoc)); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1364 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1365 | // Erase any temporaries within this evaluation context; we're not |
| 1366 | // going to track them in the AST, since we'll be rebuilding the |
| 1367 | // ASTs during template instantiation. |
| 1368 | ExprTemporaries.erase( |
| 1369 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1370 | ExprTemporaries.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1372 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1373 | /*IsVirtual=*/false, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1374 | LParenLoc, |
| 1375 | BaseInit.takeAs<Expr>(), |
| 1376 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1377 | } |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1378 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1379 | if (!BaseType->isRecordType()) |
| 1380 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 1381 | << BaseType << BaseTInfo->getTypeLoc().getSourceRange(); |
| 1382 | |
| 1383 | // C++ [class.base.init]p2: |
| 1384 | // [...] Unless the mem-initializer-id names a nonstatic data |
| 1385 | // member of the constructor’s class or a direct or virtual base |
| 1386 | // of that class, the mem-initializer is ill-formed. A |
| 1387 | // mem-initializer-list can initialize a base class using any |
| 1388 | // name that denotes that base class type. |
| 1389 | |
| 1390 | // Check for direct and virtual base classes. |
| 1391 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1392 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1393 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 1394 | VirtualBaseSpec); |
| 1395 | |
| 1396 | // C++ [base.class.init]p2: |
| 1397 | // If a mem-initializer-id is ambiguous because it designates both |
| 1398 | // a direct non-virtual base class and an inherited virtual base |
| 1399 | // class, the mem-initializer is ill-formed. |
| 1400 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1401 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
| 1402 | << BaseType << BaseTInfo->getTypeLoc().getSourceRange(); |
| 1403 | // C++ [base.class.init]p2: |
| 1404 | // Unless the mem-initializer-id names a nonstatic data membeer of the |
| 1405 | // constructor's class ot a direst or virtual base of that class, the |
| 1406 | // mem-initializer is ill-formed. |
| 1407 | if (!DirectBaseSpec && !VirtualBaseSpec) |
| 1408 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 1409 | << BaseType << ClassDecl->getNameAsCString() |
| 1410 | << BaseTInfo->getTypeLoc().getSourceRange(); |
| 1411 | |
| 1412 | CXXBaseSpecifier *BaseSpec |
| 1413 | = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
| 1414 | if (!BaseSpec) |
| 1415 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 1416 | |
| 1417 | // Initialize the base. |
| 1418 | InitializedEntity BaseEntity = |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1419 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1420 | InitializationKind Kind = |
| 1421 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc); |
| 1422 | |
| 1423 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 1424 | |
| 1425 | OwningExprResult BaseInit = |
| 1426 | InitSeq.Perform(*this, BaseEntity, Kind, |
| 1427 | MultiExprArg(*this, (void**)Args, NumArgs), 0); |
| 1428 | if (BaseInit.isInvalid()) |
| 1429 | return true; |
| 1430 | |
| 1431 | // C++0x [class.base.init]p7: |
| 1432 | // The initialization of each base and member constitutes a |
| 1433 | // full-expression. |
| 1434 | BaseInit = MaybeCreateCXXExprWithTemporaries(move(BaseInit)); |
| 1435 | if (BaseInit.isInvalid()) |
| 1436 | return true; |
| 1437 | |
| 1438 | // If we are in a dependent context, template instantiation will |
| 1439 | // perform this type-checking again. Just save the arguments that we |
| 1440 | // received in a ParenListExpr. |
| 1441 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1442 | // of the information that we have about the base |
| 1443 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1444 | // and this approach is far more likely to get the corner cases right. |
| 1445 | if (CurContext->isDependentContext()) { |
| 1446 | // Bump the reference count of all of the arguments. |
| 1447 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1448 | Args[I]->Retain(); |
| 1449 | |
| 1450 | OwningExprResult Init |
| 1451 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1452 | RParenLoc)); |
| 1453 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1454 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1455 | LParenLoc, |
| 1456 | Init.takeAs<Expr>(), |
| 1457 | RParenLoc); |
| 1458 | } |
| 1459 | |
| 1460 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1461 | BaseSpec->isVirtual(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1462 | LParenLoc, |
| 1463 | BaseInit.takeAs<Expr>(), |
| 1464 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1467 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 1468 | /// initialize its base or member. |
| 1469 | enum ImplicitInitializerKind { |
| 1470 | IIK_Default, |
| 1471 | IIK_Copy, |
| 1472 | IIK_Move |
| 1473 | }; |
| 1474 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1475 | static bool |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1476 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1477 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1478 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1479 | bool IsInheritedVirtualBase, |
| 1480 | CXXBaseOrMemberInitializer *&CXXBaseInit) { |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1481 | InitializedEntity InitEntity |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1482 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 1483 | IsInheritedVirtualBase); |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1484 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1485 | Sema::OwningExprResult BaseInit(SemaRef); |
| 1486 | |
| 1487 | switch (ImplicitInitKind) { |
| 1488 | case IIK_Default: { |
| 1489 | InitializationKind InitKind |
| 1490 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 1491 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1492 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
| 1493 | Sema::MultiExprArg(SemaRef, 0, 0)); |
| 1494 | break; |
| 1495 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1496 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1497 | case IIK_Copy: { |
| 1498 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1499 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1500 | |
| 1501 | Expr *CopyCtorArg = |
| 1502 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
| 1503 | SourceLocation(), ParamType, 0); |
| 1504 | |
Anders Carlsson | c795750 | 2010-04-24 22:02:54 +0000 | [diff] [blame^] | 1505 | // Cast to the base class to avoid ambiguities. |
| 1506 | CXXBaseSpecifierArray BasePath; |
| 1507 | BasePath.push_back(BaseSpec); |
| 1508 | SemaRef.ImpCastExprToType(CopyCtorArg, BaseSpec->getType(), |
| 1509 | CastExpr::CK_UncheckedDerivedToBase, |
| 1510 | /*isLvalue=*/true, BasePath); |
| 1511 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1512 | InitializationKind InitKind |
| 1513 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1514 | SourceLocation(), SourceLocation()); |
| 1515 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1516 | &CopyCtorArg, 1); |
| 1517 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
| 1518 | Sema::MultiExprArg(SemaRef, |
| 1519 | (void**)&CopyCtorArg, 1)); |
| 1520 | break; |
| 1521 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1522 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1523 | case IIK_Move: |
| 1524 | assert(false && "Unhandled initializer kind!"); |
| 1525 | } |
| 1526 | |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1527 | BaseInit = SemaRef.MaybeCreateCXXExprWithTemporaries(move(BaseInit)); |
| 1528 | if (BaseInit.isInvalid()) |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1529 | return true; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1530 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1531 | CXXBaseInit = |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1532 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
| 1533 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 1534 | SourceLocation()), |
| 1535 | BaseSpec->isVirtual(), |
| 1536 | SourceLocation(), |
| 1537 | BaseInit.takeAs<Expr>(), |
| 1538 | SourceLocation()); |
| 1539 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1540 | return false; |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1543 | static bool |
| 1544 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1545 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1546 | FieldDecl *Field, |
| 1547 | CXXBaseOrMemberInitializer *&CXXMemberInit) { |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1548 | if (ImplicitInitKind == IIK_Copy) { |
| 1549 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1550 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1551 | |
| 1552 | Expr *MemberExprBase = |
| 1553 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
| 1554 | SourceLocation(), ParamType, 0); |
| 1555 | |
| 1556 | |
| 1557 | Expr *CopyCtorArg = |
| 1558 | MemberExpr::Create(SemaRef.Context, MemberExprBase, /*IsArrow=*/false, |
| 1559 | 0, SourceRange(), Field, |
| 1560 | DeclAccessPair::make(Field, Field->getAccess()), |
| 1561 | SourceLocation(), 0, |
| 1562 | Field->getType().getNonReferenceType()); |
| 1563 | |
| 1564 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 1565 | InitializationKind InitKind = |
| 1566 | InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1567 | SourceLocation(), SourceLocation()); |
| 1568 | |
| 1569 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1570 | &CopyCtorArg, 1); |
| 1571 | |
| 1572 | Sema::OwningExprResult MemberInit = |
| 1573 | InitSeq.Perform(SemaRef, InitEntity, InitKind, |
| 1574 | Sema::MultiExprArg(SemaRef, (void**)&CopyCtorArg, 1), 0); |
| 1575 | if (MemberInit.isInvalid()) |
| 1576 | return true; |
| 1577 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1578 | CXXMemberInit = 0; |
| 1579 | return false; |
| 1580 | } |
| 1581 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1582 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 1583 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1584 | QualType FieldBaseElementType = |
| 1585 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 1586 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1587 | if (FieldBaseElementType->isRecordType()) { |
| 1588 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1589 | InitializationKind InitKind = |
| 1590 | InitializationKind::CreateDefault(Constructor->getLocation()); |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1591 | |
| 1592 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1593 | Sema::OwningExprResult MemberInit = |
| 1594 | InitSeq.Perform(SemaRef, InitEntity, InitKind, |
| 1595 | Sema::MultiExprArg(SemaRef, 0, 0)); |
| 1596 | MemberInit = SemaRef.MaybeCreateCXXExprWithTemporaries(move(MemberInit)); |
| 1597 | if (MemberInit.isInvalid()) |
| 1598 | return true; |
| 1599 | |
| 1600 | CXXMemberInit = |
| 1601 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
| 1602 | Field, SourceLocation(), |
| 1603 | SourceLocation(), |
| 1604 | MemberInit.takeAs<Expr>(), |
| 1605 | SourceLocation()); |
| 1606 | return false; |
| 1607 | } |
Anders Carlsson | 114a297 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 1608 | |
| 1609 | if (FieldBaseElementType->isReferenceType()) { |
| 1610 | SemaRef.Diag(Constructor->getLocation(), |
| 1611 | diag::err_uninitialized_member_in_ctor) |
| 1612 | << (int)Constructor->isImplicit() |
| 1613 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1614 | << 0 << Field->getDeclName(); |
| 1615 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1616 | return true; |
| 1617 | } |
| 1618 | |
| 1619 | if (FieldBaseElementType.isConstQualified()) { |
| 1620 | SemaRef.Diag(Constructor->getLocation(), |
| 1621 | diag::err_uninitialized_member_in_ctor) |
| 1622 | << (int)Constructor->isImplicit() |
| 1623 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1624 | << 1 << Field->getDeclName(); |
| 1625 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1626 | return true; |
| 1627 | } |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1628 | |
| 1629 | // Nothing to initialize. |
| 1630 | CXXMemberInit = 0; |
| 1631 | return false; |
| 1632 | } |
| 1633 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1634 | bool |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1635 | Sema::SetBaseOrMemberInitializers(CXXConstructorDecl *Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1636 | CXXBaseOrMemberInitializer **Initializers, |
| 1637 | unsigned NumInitializers, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1638 | bool AnyErrors) { |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1639 | if (Constructor->getDeclContext()->isDependentContext()) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1640 | // Just store the initializers as written, they will be checked during |
| 1641 | // instantiation. |
| 1642 | if (NumInitializers > 0) { |
| 1643 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1644 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1645 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
| 1646 | memcpy(baseOrMemberInitializers, Initializers, |
| 1647 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
| 1648 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
| 1649 | } |
| 1650 | |
| 1651 | return false; |
| 1652 | } |
| 1653 | |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1654 | ImplicitInitializerKind ImplicitInitKind = IIK_Default; |
| 1655 | |
| 1656 | // FIXME: Handle implicit move constructors. |
| 1657 | if (Constructor->isImplicit() && Constructor->isCopyConstructor()) |
| 1658 | ImplicitInitKind = IIK_Copy; |
| 1659 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1660 | // We need to build the initializer AST according to order of construction |
| 1661 | // and not what user specified in the Initializers list. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1662 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | d606848 | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 1663 | if (!ClassDecl) |
| 1664 | return true; |
| 1665 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1666 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
| 1667 | llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1668 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1669 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1670 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1671 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1672 | |
| 1673 | if (Member->isBaseInitializer()) |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1674 | AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1675 | else |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1676 | AllBaseFields[Member->getMember()] = Member; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1679 | // Keep track of the direct virtual bases. |
| 1680 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 1681 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 1682 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 1683 | if (I->isVirtual()) |
| 1684 | DirectVBases.insert(I); |
| 1685 | } |
| 1686 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1687 | // Push virtual bases before others. |
| 1688 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 1689 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 1690 | |
| 1691 | if (CXXBaseOrMemberInitializer *Value |
| 1692 | = AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 1693 | AllToInit.push_back(Value); |
| 1694 | } else if (!AnyErrors) { |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1695 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1696 | CXXBaseOrMemberInitializer *CXXBaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1697 | if (BuildImplicitBaseInitializer(*this, Constructor, ImplicitInitKind, |
| 1698 | VBase, IsInheritedVirtualBase, |
| 1699 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1700 | HadError = true; |
| 1701 | continue; |
| 1702 | } |
Anders Carlsson | 84688f2 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1703 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1704 | AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1705 | } |
| 1706 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1708 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1709 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1710 | // Virtuals are in the virtual base list and already constructed. |
| 1711 | if (Base->isVirtual()) |
| 1712 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1714 | if (CXXBaseOrMemberInitializer *Value |
| 1715 | = AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 1716 | AllToInit.push_back(Value); |
| 1717 | } else if (!AnyErrors) { |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1718 | CXXBaseOrMemberInitializer *CXXBaseInit; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1719 | if (BuildImplicitBaseInitializer(*this, Constructor, ImplicitInitKind, |
| 1720 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1721 | CXXBaseInit)) { |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1722 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1723 | continue; |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1724 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1725 | |
Anders Carlsson | bcc12fd | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1726 | AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1727 | } |
| 1728 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1730 | // non-static data members. |
| 1731 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1732 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 1733 | if ((*Field)->isAnonymousStructOrUnion()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | if (const RecordType *FieldClassType = |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1735 | Field->getType()->getAs<RecordType>()) { |
| 1736 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | afe7ec2 | 2009-11-13 18:34:26 +0000 | [diff] [blame] | 1737 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1739 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1740 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*FA)) { |
| 1741 | // 'Member' is the anonymous union field and 'AnonUnionMember' is |
| 1742 | // set to the anonymous union data member used in the initializer |
| 1743 | // list. |
| 1744 | Value->setMember(*Field); |
| 1745 | Value->setAnonUnionMember(*FA); |
| 1746 | AllToInit.push_back(Value); |
| 1747 | break; |
| 1748 | } |
| 1749 | } |
| 1750 | } |
| 1751 | continue; |
| 1752 | } |
| 1753 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*Field)) { |
| 1754 | AllToInit.push_back(Value); |
| 1755 | continue; |
| 1756 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | |
Anders Carlsson | defefd2 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1758 | if (AnyErrors) |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame] | 1759 | continue; |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame] | 1760 | |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1761 | CXXBaseOrMemberInitializer *Member; |
Anders Carlsson | e5ef740 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1762 | if (BuildImplicitMemberInitializer(*this, Constructor, ImplicitInitKind, |
| 1763 | *Field, Member)) { |
Anders Carlsson | ddfb75f | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1764 | HadError = true; |
| 1765 | continue; |
| 1766 | } |
| 1767 | |
| 1768 | // If the member doesn't need to be initialized, it will be null. |
| 1769 | if (Member) |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1770 | AllToInit.push_back(Member); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1771 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1773 | NumInitializers = AllToInit.size(); |
| 1774 | if (NumInitializers > 0) { |
| 1775 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1776 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1777 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1778 | memcpy(baseOrMemberInitializers, AllToInit.data(), |
| 1779 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1780 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
Rafael Espindola | 961b167 | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 1781 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1782 | // Constructors implicitly reference the base and member |
| 1783 | // destructors. |
| 1784 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 1785 | Constructor->getParent()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1786 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1787 | |
| 1788 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1791 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 1792 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1793 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1794 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 1795 | return static_cast<void *>(RT->getDecl()); |
| 1796 | } |
| 1797 | return static_cast<void *>(Field); |
| 1798 | } |
| 1799 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1800 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 1801 | return Context.getCanonicalType(BaseType).getTypePtr(); |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1804 | static void *GetKeyForMember(ASTContext &Context, |
| 1805 | CXXBaseOrMemberInitializer *Member, |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1806 | bool MemberMaybeAnon = false) { |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1807 | if (!Member->isMemberInitializer()) |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1808 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1809 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1810 | // For fields injected into the class via declaration of an anonymous union, |
| 1811 | // use its anonymous union class declaration as the unique key. |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1812 | FieldDecl *Field = Member->getMember(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1814 | // After SetBaseOrMemberInitializers call, Field is the anonymous union |
| 1815 | // data member of the class. Data member used in the initializer list is |
| 1816 | // in AnonUnionMember field. |
| 1817 | if (MemberMaybeAnon && Field->isAnonymousStructOrUnion()) |
| 1818 | Field = Member->getAnonUnionMember(); |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1819 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1820 | // If the field is a member of an anonymous struct or union, our key |
| 1821 | // is the anonymous record decl that's a direct child of the class. |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1822 | RecordDecl *RD = Field->getParent(); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1823 | if (RD->isAnonymousStructOrUnion()) { |
| 1824 | while (true) { |
| 1825 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 1826 | if (Parent->isAnonymousStructOrUnion()) |
| 1827 | RD = Parent; |
| 1828 | else |
| 1829 | break; |
| 1830 | } |
| 1831 | |
Anders Carlsson | ee11b2d | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1832 | return static_cast<void *>(RD); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1833 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | |
Anders Carlsson | 8f1a240 | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1835 | return static_cast<void *>(Field); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1838 | static void |
| 1839 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1840 | const CXXConstructorDecl *Constructor, |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1841 | CXXBaseOrMemberInitializer **Inits, |
| 1842 | unsigned NumInits) { |
| 1843 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1844 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1846 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order) |
| 1847 | == Diagnostic::Ignored) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1848 | return; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1849 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1850 | // Build the list of bases and members in the order that they'll |
| 1851 | // actually be initialized. The explicit initializers should be in |
| 1852 | // this same order but may be missing things. |
| 1853 | llvm::SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1855 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1856 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1857 | // 1. Virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1858 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1859 | ClassDecl->vbases_begin(), |
| 1860 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1861 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1863 | // 2. Non-virtual bases. |
Anders Carlsson | 071d610 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1864 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1865 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1866 | if (Base->isVirtual()) |
| 1867 | continue; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1868 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1869 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1871 | // 3. Direct fields. |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1872 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1873 | E = ClassDecl->field_end(); Field != E; ++Field) |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1874 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1876 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 1877 | unsigned IdealIndex = 0; |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1878 | |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1879 | CXXBaseOrMemberInitializer *PrevInit = 0; |
| 1880 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
| 1881 | CXXBaseOrMemberInitializer *Init = Inits[InitIndex]; |
| 1882 | void *InitKey = GetKeyForMember(SemaRef.Context, Init, true); |
| 1883 | |
| 1884 | // Scan forward to try to find this initializer in the idealized |
| 1885 | // initializers list. |
| 1886 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 1887 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1888 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1889 | |
| 1890 | // If we didn't find this initializer, it must be because we |
| 1891 | // scanned past it on a previous iteration. That can only |
| 1892 | // happen if we're out of order; emit a warning. |
| 1893 | if (IdealIndex == NumIdealInits) { |
| 1894 | assert(PrevInit && "initializer not found in initializer list"); |
| 1895 | |
| 1896 | Sema::SemaDiagnosticBuilder D = |
| 1897 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 1898 | diag::warn_initializer_out_of_order); |
| 1899 | |
| 1900 | if (PrevInit->isMemberInitializer()) |
| 1901 | D << 0 << PrevInit->getMember()->getDeclName(); |
| 1902 | else |
| 1903 | D << 1 << PrevInit->getBaseClassInfo()->getType(); |
| 1904 | |
| 1905 | if (Init->isMemberInitializer()) |
| 1906 | D << 0 << Init->getMember()->getDeclName(); |
| 1907 | else |
| 1908 | D << 1 << Init->getBaseClassInfo()->getType(); |
| 1909 | |
| 1910 | // Move back to the initializer's location in the ideal list. |
| 1911 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 1912 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1913 | break; |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1914 | |
| 1915 | assert(IdealIndex != NumIdealInits && |
| 1916 | "initializer not found in initializer list"); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1917 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1918 | |
| 1919 | PrevInit = Init; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1920 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1923 | namespace { |
| 1924 | bool CheckRedundantInit(Sema &S, |
| 1925 | CXXBaseOrMemberInitializer *Init, |
| 1926 | CXXBaseOrMemberInitializer *&PrevInit) { |
| 1927 | if (!PrevInit) { |
| 1928 | PrevInit = Init; |
| 1929 | return false; |
| 1930 | } |
| 1931 | |
| 1932 | if (FieldDecl *Field = Init->getMember()) |
| 1933 | S.Diag(Init->getSourceLocation(), |
| 1934 | diag::err_multiple_mem_initialization) |
| 1935 | << Field->getDeclName() |
| 1936 | << Init->getSourceRange(); |
| 1937 | else { |
| 1938 | Type *BaseClass = Init->getBaseClass(); |
| 1939 | assert(BaseClass && "neither field nor base"); |
| 1940 | S.Diag(Init->getSourceLocation(), |
| 1941 | diag::err_multiple_base_initialization) |
| 1942 | << QualType(BaseClass, 0) |
| 1943 | << Init->getSourceRange(); |
| 1944 | } |
| 1945 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 1946 | << 0 << PrevInit->getSourceRange(); |
| 1947 | |
| 1948 | return true; |
| 1949 | } |
| 1950 | |
| 1951 | typedef std::pair<NamedDecl *, CXXBaseOrMemberInitializer *> UnionEntry; |
| 1952 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 1953 | |
| 1954 | bool CheckRedundantUnionInit(Sema &S, |
| 1955 | CXXBaseOrMemberInitializer *Init, |
| 1956 | RedundantUnionMap &Unions) { |
| 1957 | FieldDecl *Field = Init->getMember(); |
| 1958 | RecordDecl *Parent = Field->getParent(); |
| 1959 | if (!Parent->isAnonymousStructOrUnion()) |
| 1960 | return false; |
| 1961 | |
| 1962 | NamedDecl *Child = Field; |
| 1963 | do { |
| 1964 | if (Parent->isUnion()) { |
| 1965 | UnionEntry &En = Unions[Parent]; |
| 1966 | if (En.first && En.first != Child) { |
| 1967 | S.Diag(Init->getSourceLocation(), |
| 1968 | diag::err_multiple_mem_union_initialization) |
| 1969 | << Field->getDeclName() |
| 1970 | << Init->getSourceRange(); |
| 1971 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 1972 | << 0 << En.second->getSourceRange(); |
| 1973 | return true; |
| 1974 | } else if (!En.first) { |
| 1975 | En.first = Child; |
| 1976 | En.second = Init; |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | Child = Parent; |
| 1981 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 1982 | } while (Parent->isAnonymousStructOrUnion()); |
| 1983 | |
| 1984 | return false; |
| 1985 | } |
| 1986 | } |
| 1987 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1988 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
| 1989 | void Sema::ActOnMemInitializers(DeclPtrTy ConstructorDecl, |
| 1990 | SourceLocation ColonLoc, |
| 1991 | MemInitTy **meminits, unsigned NumMemInits, |
| 1992 | bool AnyErrors) { |
| 1993 | if (!ConstructorDecl) |
| 1994 | return; |
| 1995 | |
| 1996 | AdjustDeclIfTemplate(ConstructorDecl); |
| 1997 | |
| 1998 | CXXConstructorDecl *Constructor |
| 1999 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl.getAs<Decl>()); |
| 2000 | |
| 2001 | if (!Constructor) { |
| 2002 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 2003 | return; |
| 2004 | } |
| 2005 | |
| 2006 | CXXBaseOrMemberInitializer **MemInits = |
| 2007 | reinterpret_cast<CXXBaseOrMemberInitializer **>(meminits); |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2008 | |
| 2009 | // Mapping for the duplicate initializers check. |
| 2010 | // For member initializers, this is keyed with a FieldDecl*. |
| 2011 | // For base initializers, this is keyed with a Type*. |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2012 | llvm::DenseMap<void*, CXXBaseOrMemberInitializer *> Members; |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2013 | |
| 2014 | // Mapping for the inconsistent anonymous-union initializers check. |
| 2015 | RedundantUnionMap MemberUnions; |
| 2016 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2017 | bool HadError = false; |
| 2018 | for (unsigned i = 0; i < NumMemInits; i++) { |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2019 | CXXBaseOrMemberInitializer *Init = MemInits[i]; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2020 | |
John McCall | 3c3ccdb | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2021 | if (Init->isMemberInitializer()) { |
| 2022 | FieldDecl *Field = Init->getMember(); |
| 2023 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 2024 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 2025 | HadError = true; |
| 2026 | } else { |
| 2027 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 2028 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 2029 | HadError = true; |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2030 | } |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Anders Carlsson | ea356fb | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2033 | if (HadError) |
| 2034 | return; |
| 2035 | |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2036 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2037 | |
| 2038 | SetBaseOrMemberInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | 58cfbde | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2041 | void |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2042 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 2043 | CXXRecordDecl *ClassDecl) { |
| 2044 | // Ignore dependent contexts. |
| 2045 | if (ClassDecl->isDependentContext()) |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2046 | return; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2047 | |
| 2048 | // FIXME: all the access-control diagnostics are positioned on the |
| 2049 | // field/base declaration. That's probably good; that said, the |
| 2050 | // user might reasonably want to know why the destructor is being |
| 2051 | // emitted, and we currently don't say. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2052 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2053 | // Non-static data members. |
| 2054 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 2055 | E = ClassDecl->field_end(); I != E; ++I) { |
| 2056 | FieldDecl *Field = *I; |
| 2057 | |
| 2058 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 2059 | |
| 2060 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 2061 | if (!RT) |
| 2062 | continue; |
| 2063 | |
| 2064 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 2065 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2066 | continue; |
| 2067 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2068 | CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor(Context); |
| 2069 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2070 | PDiag(diag::err_access_dtor_field) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2071 | << Field->getDeclName() |
| 2072 | << FieldType); |
| 2073 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2074 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2077 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 2078 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2079 | // Bases. |
| 2080 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2081 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2082 | // Bases are always records in a well-formed non-dependent class. |
| 2083 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 2084 | |
| 2085 | // Remember direct virtual bases. |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2086 | if (Base->isVirtual()) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2087 | DirectVirtualBases.insert(RT); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2088 | |
| 2089 | // Ignore trivial destructors. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2090 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2091 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2092 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2093 | |
| 2094 | CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context); |
| 2095 | |
| 2096 | // FIXME: caret should be on the start of the class name |
| 2097 | CheckDestructorAccess(Base->getSourceRange().getBegin(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2098 | PDiag(diag::err_access_dtor_base) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2099 | << Base->getType() |
| 2100 | << Base->getSourceRange()); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2101 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2102 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2106 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2107 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2108 | |
| 2109 | // Bases are always records in a well-formed non-dependent class. |
| 2110 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 2111 | |
| 2112 | // Ignore direct virtual bases. |
| 2113 | if (DirectVirtualBases.count(RT)) |
| 2114 | continue; |
| 2115 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2116 | // Ignore trivial destructors. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2117 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2118 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2119 | continue; |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2120 | |
| 2121 | CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context); |
| 2122 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2123 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2124 | << VBase->getType()); |
| 2125 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2126 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2127 | } |
| 2128 | } |
| 2129 | |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 2130 | void Sema::ActOnDefaultCtorInitializers(DeclPtrTy CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2131 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2132 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2133 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2134 | if (CXXConstructorDecl *Constructor |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2135 | = dyn_cast<CXXConstructorDecl>(CDtorDecl.getAs<Decl>())) |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2136 | SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2139 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2140 | unsigned DiagID, AbstractDiagSelID SelID, |
| 2141 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2142 | if (SelID == -1) |
| 2143 | return RequireNonAbstractType(Loc, T, |
| 2144 | PDiag(DiagID), CurrentRD); |
| 2145 | else |
| 2146 | return RequireNonAbstractType(Loc, T, |
| 2147 | PDiag(DiagID) << SelID, CurrentRD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2150 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
| 2151 | const PartialDiagnostic &PD, |
| 2152 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2153 | if (!getLangOptions().CPlusPlus) |
| 2154 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 2156 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2157 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2158 | CurrentRD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2160 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2161 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2162 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2163 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2164 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2165 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2166 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, CurrentRD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2167 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2169 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2170 | if (!RT) |
| 2171 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2173 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2174 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2175 | if (CurrentRD && CurrentRD != RD) |
| 2176 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2178 | // FIXME: is this reasonable? It matches current behavior, but.... |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2179 | if (!RD->getDefinition()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2180 | return false; |
| 2181 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2182 | if (!RD->isAbstract()) |
| 2183 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2185 | Diag(Loc, PD) << RD->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2186 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2187 | // Check if we've already emitted the list of pure virtual functions for this |
| 2188 | // class. |
| 2189 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
| 2190 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2192 | CXXFinalOverriderMap FinalOverriders; |
| 2193 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2195 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2196 | MEnd = FinalOverriders.end(); |
| 2197 | M != MEnd; |
| 2198 | ++M) { |
| 2199 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2200 | SOEnd = M->second.end(); |
| 2201 | SO != SOEnd; ++SO) { |
| 2202 | // C++ [class.abstract]p4: |
| 2203 | // A class is abstract if it contains or inherits at least one |
| 2204 | // pure virtual function for which the final overrider is pure |
| 2205 | // virtual. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2207 | // |
| 2208 | if (SO->second.size() != 1) |
| 2209 | continue; |
| 2210 | |
| 2211 | if (!SO->second.front().Method->isPure()) |
| 2212 | continue; |
| 2213 | |
| 2214 | Diag(SO->second.front().Method->getLocation(), |
| 2215 | diag::note_pure_virtual_function) |
| 2216 | << SO->second.front().Method->getDeclName(); |
| 2217 | } |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | if (!PureVirtualClassDiagSet) |
| 2221 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 2222 | PureVirtualClassDiagSet->insert(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2223 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2224 | return true; |
| 2225 | } |
| 2226 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2227 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 2228 | class AbstractClassUsageDiagnoser |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2229 | : public DeclVisitor<AbstractClassUsageDiagnoser, bool> { |
| 2230 | Sema &SemaRef; |
| 2231 | CXXRecordDecl *AbstractClass; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2233 | bool VisitDeclContext(const DeclContext *DC) { |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2234 | bool Invalid = false; |
| 2235 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2236 | for (CXXRecordDecl::decl_iterator I = DC->decls_begin(), |
| 2237 | E = DC->decls_end(); I != E; ++I) |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2238 | Invalid |= Visit(*I); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2239 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2240 | return Invalid; |
| 2241 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2242 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2243 | public: |
| 2244 | AbstractClassUsageDiagnoser(Sema& SemaRef, CXXRecordDecl *ac) |
| 2245 | : SemaRef(SemaRef), AbstractClass(ac) { |
| 2246 | Visit(SemaRef.Context.getTranslationUnitDecl()); |
| 2247 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2248 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2249 | bool VisitFunctionDecl(const FunctionDecl *FD) { |
| 2250 | if (FD->isThisDeclarationADefinition()) { |
| 2251 | // No need to do the check if we're in a definition, because it requires |
| 2252 | // that the return/param types are complete. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2253 | // because that requires |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2254 | return VisitDeclContext(FD); |
| 2255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2257 | // Check the return type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2258 | QualType RTy = FD->getType()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2259 | bool Invalid = |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2260 | SemaRef.RequireNonAbstractType(FD->getLocation(), RTy, |
| 2261 | diag::err_abstract_type_in_decl, |
| 2262 | Sema::AbstractReturnType, |
| 2263 | AbstractClass); |
| 2264 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2265 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2266 | E = FD->param_end(); I != E; ++I) { |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2267 | const ParmVarDecl *VD = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2268 | Invalid |= |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2269 | SemaRef.RequireNonAbstractType(VD->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | VD->getOriginalType(), |
| 2271 | diag::err_abstract_type_in_decl, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2272 | Sema::AbstractParamType, |
| 2273 | AbstractClass); |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | return Invalid; |
| 2277 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2279 | bool VisitDecl(const Decl* D) { |
| 2280 | if (const DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 2281 | return VisitDeclContext(DC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2282 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2283 | return false; |
| 2284 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2285 | }; |
| 2286 | } |
| 2287 | |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2288 | /// \brief Perform semantic checks on a class definition that has been |
| 2289 | /// completing, introducing implicitly-declared members, checking for |
| 2290 | /// abstract types, etc. |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2291 | void Sema::CheckCompletedCXXClass(Scope *S, CXXRecordDecl *Record) { |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2292 | if (!Record || Record->isInvalidDecl()) |
| 2293 | return; |
| 2294 | |
Eli Friedman | ff2d878 | 2009-12-16 20:00:27 +0000 | [diff] [blame] | 2295 | if (!Record->isDependentType()) |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2296 | AddImplicitlyDeclaredMembersToClass(S, Record); |
Douglas Gregor | 159ef1e | 2010-01-06 04:44:19 +0000 | [diff] [blame] | 2297 | |
Eli Friedman | ff2d878 | 2009-12-16 20:00:27 +0000 | [diff] [blame] | 2298 | if (Record->isInvalidDecl()) |
| 2299 | return; |
| 2300 | |
John McCall | 233a641 | 2010-01-28 07:38:46 +0000 | [diff] [blame] | 2301 | // Set access bits correctly on the directly-declared conversions. |
| 2302 | UnresolvedSetImpl *Convs = Record->getConversionFunctions(); |
| 2303 | for (UnresolvedSetIterator I = Convs->begin(), E = Convs->end(); I != E; ++I) |
| 2304 | Convs->setAccess(I, (*I)->getAccess()); |
| 2305 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2306 | // Determine whether we need to check for final overriders. We do |
| 2307 | // this either when there are virtual base classes (in which case we |
| 2308 | // may end up finding multiple final overriders for a given virtual |
| 2309 | // function) or any of the base classes is abstract (in which case |
| 2310 | // we might detect that this class is abstract). |
| 2311 | bool CheckFinalOverriders = false; |
| 2312 | if (Record->isPolymorphic() && !Record->isInvalidDecl() && |
| 2313 | !Record->isDependentType()) { |
| 2314 | if (Record->getNumVBases()) |
| 2315 | CheckFinalOverriders = true; |
| 2316 | else if (!Record->isAbstract()) { |
| 2317 | for (CXXRecordDecl::base_class_const_iterator B = Record->bases_begin(), |
| 2318 | BEnd = Record->bases_end(); |
| 2319 | B != BEnd; ++B) { |
| 2320 | CXXRecordDecl *BaseDecl |
| 2321 | = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl()); |
| 2322 | if (BaseDecl->isAbstract()) { |
| 2323 | CheckFinalOverriders = true; |
| 2324 | break; |
| 2325 | } |
| 2326 | } |
| 2327 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Douglas Gregor | 7b2fc9d | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2330 | if (CheckFinalOverriders) { |
| 2331 | CXXFinalOverriderMap FinalOverriders; |
| 2332 | Record->getFinalOverriders(FinalOverriders); |
| 2333 | |
| 2334 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2335 | MEnd = FinalOverriders.end(); |
| 2336 | M != MEnd; ++M) { |
| 2337 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2338 | SOEnd = M->second.end(); |
| 2339 | SO != SOEnd; ++SO) { |
| 2340 | assert(SO->second.size() > 0 && |
| 2341 | "All virtual functions have overridding virtual functions"); |
| 2342 | if (SO->second.size() == 1) { |
| 2343 | // C++ [class.abstract]p4: |
| 2344 | // A class is abstract if it contains or inherits at least one |
| 2345 | // pure virtual function for which the final overrider is pure |
| 2346 | // virtual. |
| 2347 | if (SO->second.front().Method->isPure()) |
| 2348 | Record->setAbstract(true); |
| 2349 | continue; |
| 2350 | } |
| 2351 | |
| 2352 | // C++ [class.virtual]p2: |
| 2353 | // In a derived class, if a virtual member function of a base |
| 2354 | // class subobject has more than one final overrider the |
| 2355 | // program is ill-formed. |
| 2356 | Diag(Record->getLocation(), diag::err_multiple_final_overriders) |
| 2357 | << (NamedDecl *)M->first << Record; |
| 2358 | Diag(M->first->getLocation(), diag::note_overridden_virtual_function); |
| 2359 | for (OverridingMethods::overriding_iterator OM = SO->second.begin(), |
| 2360 | OMEnd = SO->second.end(); |
| 2361 | OM != OMEnd; ++OM) |
| 2362 | Diag(OM->Method->getLocation(), diag::note_final_overrider) |
| 2363 | << (NamedDecl *)M->first << OM->Method->getParent(); |
| 2364 | |
| 2365 | Record->setInvalidDecl(); |
| 2366 | } |
| 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | if (Record->isAbstract() && !Record->isInvalidDecl()) |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2371 | (void)AbstractClassUsageDiagnoser(*this, Record); |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2372 | |
| 2373 | // If this is not an aggregate type and has no user-declared constructor, |
| 2374 | // complain about any non-static data members of reference or const scalar |
| 2375 | // type, since they will never get initializers. |
| 2376 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 2377 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor()) { |
| 2378 | bool Complained = false; |
| 2379 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 2380 | FEnd = Record->field_end(); |
| 2381 | F != FEnd; ++F) { |
| 2382 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 1deea66 | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 2383 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 325e593 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2384 | if (!Complained) { |
| 2385 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 2386 | << Record->getTagKind() << Record; |
| 2387 | Complained = true; |
| 2388 | } |
| 2389 | |
| 2390 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 2391 | << F->getType()->isReferenceType() |
| 2392 | << F->getDeclName(); |
| 2393 | } |
| 2394 | } |
| 2395 | } |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2398 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2399 | DeclPtrTy TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2400 | SourceLocation LBrac, |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2401 | SourceLocation RBrac, |
| 2402 | AttributeList *AttrList) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2403 | if (!TagDecl) |
| 2404 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2406 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2407 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2408 | ActOnFields(S, RLoc, TagDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2409 | (DeclPtrTy*)FieldCollector->getCurFields(), |
Douglas Gregor | 0b4c9b5 | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2410 | FieldCollector->getCurNumFields(), LBrac, RBrac, AttrList); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2411 | |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2412 | CheckCompletedCXXClass(S, |
Douglas Gregor | 1ab537b | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2413 | dyn_cast_or_null<CXXRecordDecl>(TagDecl.getAs<Decl>())); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2416 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 2417 | /// special functions, such as the default constructor, copy |
| 2418 | /// constructor, or destructor, to the given C++ class (C++ |
| 2419 | /// [special]p1). This routine can only be executed just before the |
| 2420 | /// definition of the class is complete. |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2421 | /// |
| 2422 | /// The scope, if provided, is the class scope. |
| 2423 | void Sema::AddImplicitlyDeclaredMembersToClass(Scope *S, |
| 2424 | CXXRecordDecl *ClassDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2425 | CanQualType ClassType |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2426 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2427 | |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2428 | // FIXME: Implicit declarations have exception specifications, which are |
| 2429 | // the union of the specifications of the implicitly called functions. |
| 2430 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2431 | if (!ClassDecl->hasUserDeclaredConstructor()) { |
| 2432 | // C++ [class.ctor]p5: |
| 2433 | // A default constructor for a class X is a constructor of class X |
| 2434 | // that can be called without an argument. If there is no |
| 2435 | // user-declared constructor for class X, a default constructor is |
| 2436 | // implicitly declared. An implicitly-declared default constructor |
| 2437 | // is an inline public member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2438 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2439 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2440 | CXXConstructorDecl *DefaultCon = |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2441 | CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2442 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2443 | Context.getFunctionType(Context.VoidTy, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2444 | 0, 0, false, 0, |
| 2445 | /*FIXME*/false, false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2446 | 0, 0, |
| 2447 | FunctionType::ExtInfo()), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2448 | /*TInfo=*/0, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2449 | /*isExplicit=*/false, |
| 2450 | /*isInline=*/true, |
| 2451 | /*isImplicitlyDeclared=*/true); |
| 2452 | DefaultCon->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2453 | DefaultCon->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2454 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2455 | if (S) |
| 2456 | PushOnScopeChains(DefaultCon, S, true); |
| 2457 | else |
| 2458 | ClassDecl->addDecl(DefaultCon); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
| 2462 | // C++ [class.copy]p4: |
| 2463 | // If the class definition does not explicitly declare a copy |
| 2464 | // constructor, one is declared implicitly. |
| 2465 | |
| 2466 | // C++ [class.copy]p5: |
| 2467 | // The implicitly-declared copy constructor for a class X will |
| 2468 | // have the form |
| 2469 | // |
| 2470 | // X::X(const X&) |
| 2471 | // |
| 2472 | // if |
| 2473 | bool HasConstCopyConstructor = true; |
| 2474 | |
| 2475 | // -- each direct or virtual base class B of X has a copy |
| 2476 | // constructor whose first parameter is of type const B& or |
| 2477 | // const volatile B&, and |
| 2478 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2479 | HasConstCopyConstructor && Base != ClassDecl->bases_end(); ++Base) { |
| 2480 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2481 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2482 | HasConstCopyConstructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2483 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 2484 | } |
| 2485 | |
| 2486 | // -- for all the nonstatic data members of X that are of a |
| 2487 | // class type M (or array thereof), each such class type |
| 2488 | // has a copy constructor whose first parameter is of type |
| 2489 | // const M& or const volatile M&. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2490 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 2491 | HasConstCopyConstructor && Field != ClassDecl->field_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2492 | ++Field) { |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2493 | QualType FieldType = (*Field)->getType(); |
| 2494 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2495 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2496 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2497 | const CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2498 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2499 | HasConstCopyConstructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2500 | = FieldClassDecl->hasConstCopyConstructor(Context); |
| 2501 | } |
| 2502 | } |
| 2503 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2504 | // Otherwise, the implicitly declared copy constructor will have |
| 2505 | // the form |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2506 | // |
| 2507 | // X::X(X&) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2508 | QualType ArgType = ClassType; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2509 | if (HasConstCopyConstructor) |
| 2510 | ArgType = ArgType.withConst(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2511 | ArgType = Context.getLValueReferenceType(ArgType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2512 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2513 | // An implicitly-declared copy constructor is an inline public |
| 2514 | // member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2515 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2516 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2517 | CXXConstructorDecl *CopyConstructor |
| 2518 | = CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2519 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2520 | Context.getFunctionType(Context.VoidTy, |
| 2521 | &ArgType, 1, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2522 | false, 0, |
| 2523 | /*FIXME:*/false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2524 | false, 0, 0, |
| 2525 | FunctionType::ExtInfo()), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2526 | /*TInfo=*/0, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2527 | /*isExplicit=*/false, |
| 2528 | /*isInline=*/true, |
| 2529 | /*isImplicitlyDeclared=*/true); |
| 2530 | CopyConstructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2531 | CopyConstructor->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2532 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2533 | |
| 2534 | // Add the parameter to the constructor. |
| 2535 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 2536 | ClassDecl->getLocation(), |
| 2537 | /*IdentifierInfo=*/0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2538 | ArgType, /*TInfo=*/0, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2539 | VarDecl::None, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2540 | VarDecl::None, 0); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2541 | CopyConstructor->setParams(&FromParam, 1); |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2542 | if (S) |
| 2543 | PushOnScopeChains(CopyConstructor, S, true); |
| 2544 | else |
| 2545 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2548 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 2549 | // Note: The following rules are largely analoguous to the copy |
| 2550 | // constructor rules. Note that virtual bases are not taken into account |
| 2551 | // for determining the argument type of the operator. Note also that |
| 2552 | // operators taking an object instead of a reference are allowed. |
| 2553 | // |
| 2554 | // C++ [class.copy]p10: |
| 2555 | // If the class definition does not explicitly declare a copy |
| 2556 | // assignment operator, one is declared implicitly. |
| 2557 | // The implicitly-defined copy assignment operator for a class X |
| 2558 | // will have the form |
| 2559 | // |
| 2560 | // X& X::operator=(const X&) |
| 2561 | // |
| 2562 | // if |
| 2563 | bool HasConstCopyAssignment = true; |
| 2564 | |
| 2565 | // -- each direct base class B of X has a copy assignment operator |
| 2566 | // whose parameter is of type const B&, const volatile B& or B, |
| 2567 | // and |
| 2568 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2569 | HasConstCopyAssignment && Base != ClassDecl->bases_end(); ++Base) { |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 2570 | assert(!Base->getType()->isDependentType() && |
| 2571 | "Cannot generate implicit members for class with dependent bases."); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2572 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2573 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2574 | const CXXMethodDecl *MD = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | HasConstCopyAssignment = BaseClassDecl->hasConstCopyAssignment(Context, |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2576 | MD); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | // -- for all the nonstatic data members of X that are of a class |
| 2580 | // type M (or array thereof), each such class type has a copy |
| 2581 | // assignment operator whose parameter is of type const M&, |
| 2582 | // const volatile M& or M. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2583 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 2584 | HasConstCopyAssignment && Field != ClassDecl->field_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2585 | ++Field) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2586 | QualType FieldType = (*Field)->getType(); |
| 2587 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2588 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2589 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2590 | const CXXRecordDecl *FieldClassDecl |
| 2591 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2592 | const CXXMethodDecl *MD = 0; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2593 | HasConstCopyAssignment |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2594 | = FieldClassDecl->hasConstCopyAssignment(Context, MD); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | // Otherwise, the implicitly declared copy assignment operator will |
| 2599 | // have the form |
| 2600 | // |
| 2601 | // X& X::operator=(X&) |
| 2602 | QualType ArgType = ClassType; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2603 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2604 | if (HasConstCopyAssignment) |
| 2605 | ArgType = ArgType.withConst(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2606 | ArgType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2607 | |
| 2608 | // An implicitly-declared copy assignment operator is an inline public |
| 2609 | // member of its class. |
| 2610 | DeclarationName Name = |
| 2611 | Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 2612 | CXXMethodDecl *CopyAssignment = |
| 2613 | CXXMethodDecl::Create(Context, ClassDecl, ClassDecl->getLocation(), Name, |
| 2614 | Context.getFunctionType(RetType, &ArgType, 1, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2615 | false, 0, |
| 2616 | /*FIXME:*/false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2617 | false, 0, 0, |
| 2618 | FunctionType::ExtInfo()), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2619 | /*TInfo=*/0, /*isStatic=*/false, |
| 2620 | /*StorageClassAsWritten=*/FunctionDecl::None, |
| 2621 | /*isInline=*/true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2622 | CopyAssignment->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2623 | CopyAssignment->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2624 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 2625 | CopyAssignment->setCopyAssignment(true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2626 | |
| 2627 | // Add the parameter to the operator. |
| 2628 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 2629 | ClassDecl->getLocation(), |
| 2630 | /*IdentifierInfo=*/0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2631 | ArgType, /*TInfo=*/0, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2632 | VarDecl::None, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2633 | VarDecl::None, 0); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2634 | CopyAssignment->setParams(&FromParam, 1); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2635 | |
| 2636 | // Don't call addedAssignmentOperator. There is no way to distinguish an |
| 2637 | // implicit from an explicit assignment operator. |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2638 | if (S) |
| 2639 | PushOnScopeChains(CopyAssignment, S, true); |
| 2640 | else |
| 2641 | ClassDecl->addDecl(CopyAssignment); |
Eli Friedman | ca6affd | 2009-12-02 06:59:20 +0000 | [diff] [blame] | 2642 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2645 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2646 | // C++ [class.dtor]p2: |
| 2647 | // If a class has no user-declared destructor, a destructor is |
| 2648 | // declared implicitly. An implicitly-declared destructor is an |
| 2649 | // inline public member of its class. |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2650 | QualType Ty = Context.getFunctionType(Context.VoidTy, |
| 2651 | 0, 0, false, 0, |
| 2652 | /*FIXME:*/false, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2653 | false, 0, 0, FunctionType::ExtInfo()); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2654 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2655 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2656 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | CXXDestructorDecl *Destructor |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2658 | = CXXDestructorDecl::Create(Context, ClassDecl, |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2659 | ClassDecl->getLocation(), Name, Ty, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2660 | /*isInline=*/true, |
| 2661 | /*isImplicitlyDeclared=*/true); |
| 2662 | Destructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2663 | Destructor->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2664 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | 6275e0c | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2665 | if (S) |
| 2666 | PushOnScopeChains(Destructor, S, true); |
| 2667 | else |
| 2668 | ClassDecl->addDecl(Destructor); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2669 | |
| 2670 | // This could be uniqued if it ever proves significant. |
| 2671 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
Anders Carlsson | d5a942b | 2009-11-26 21:25:09 +0000 | [diff] [blame] | 2672 | |
| 2673 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2674 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2677 | void Sema::ActOnReenterTemplateScope(Scope *S, DeclPtrTy TemplateD) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 2678 | Decl *D = TemplateD.getAs<Decl>(); |
| 2679 | if (!D) |
| 2680 | return; |
| 2681 | |
| 2682 | TemplateParameterList *Params = 0; |
| 2683 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 2684 | Params = Template->getTemplateParameters(); |
| 2685 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2686 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 2687 | Params = PartialSpec->getTemplateParameters(); |
| 2688 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2689 | return; |
| 2690 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2691 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2692 | ParamEnd = Params->end(); |
| 2693 | Param != ParamEnd; ++Param) { |
| 2694 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 2695 | if (Named->getDeclName()) { |
| 2696 | S->AddDecl(DeclPtrTy::make(Named)); |
| 2697 | IdResolver.AddDecl(Named); |
| 2698 | } |
| 2699 | } |
| 2700 | } |
| 2701 | |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2702 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, DeclPtrTy RecordD) { |
| 2703 | if (!RecordD) return; |
| 2704 | AdjustDeclIfTemplate(RecordD); |
| 2705 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD.getAs<Decl>()); |
| 2706 | PushDeclContext(S, Record); |
| 2707 | } |
| 2708 | |
| 2709 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, DeclPtrTy RecordD) { |
| 2710 | if (!RecordD) return; |
| 2711 | PopDeclContext(); |
| 2712 | } |
| 2713 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2714 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 2715 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 2716 | /// parsing those parts of the given Method declaration that could |
| 2717 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 2718 | /// arguments. This action should enter the scope of the given |
| 2719 | /// Method declaration as if we had just parsed the qualified method |
| 2720 | /// name. However, it should not bring the parameters into scope; |
| 2721 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2722 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
| 2725 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 2726 | /// C++ method declaration. We're (re-)introducing the given |
| 2727 | /// function parameter into scope for use in parsing later parts of |
| 2728 | /// the method declaration. For example, we could see an |
| 2729 | /// ActOnParamDefaultArgument event for this parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2730 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, DeclPtrTy ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2731 | if (!ParamD) |
| 2732 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2733 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2734 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2735 | |
| 2736 | // If this parameter has an unparsed default argument, clear it out |
| 2737 | // to make way for the parsed default argument. |
| 2738 | if (Param->hasUnparsedDefaultArg()) |
| 2739 | Param->setDefaultArg(0); |
| 2740 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2741 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2742 | if (Param->getDeclName()) |
| 2743 | IdResolver.AddDecl(Param); |
| 2744 | } |
| 2745 | |
| 2746 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 2747 | /// processing the delayed method declaration for Method. The method |
| 2748 | /// declaration is now considered finished. There may be a separate |
| 2749 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 2750 | /// immediately!) for this method, if it was also defined inside the |
| 2751 | /// class body. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2752 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2753 | if (!MethodD) |
| 2754 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2756 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2757 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2758 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2759 | |
| 2760 | // Now that we have our default arguments, check the constructor |
| 2761 | // again. It could produce additional diagnostics or affect whether |
| 2762 | // the class has implicitly-declared destructors, among other |
| 2763 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2764 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 2765 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2766 | |
| 2767 | // Check the default arguments, which we may have added. |
| 2768 | if (!Method->isInvalidDecl()) |
| 2769 | CheckCXXDefaultArguments(Method); |
| 2770 | } |
| 2771 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2772 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2773 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2774 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2775 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 2776 | /// will be updated to reflect a well-formed type for the constructor and |
| 2777 | /// returned. |
| 2778 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
| 2779 | FunctionDecl::StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2780 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2781 | |
| 2782 | // C++ [class.ctor]p3: |
| 2783 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 2784 | // constructor can be invoked for a const, volatile or const |
| 2785 | // volatile object. A constructor shall not be declared const, |
| 2786 | // volatile, or const volatile (9.3.2). |
| 2787 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2788 | if (!D.isInvalidType()) |
| 2789 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2790 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 2791 | << SourceRange(D.getIdentifierLoc()); |
| 2792 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2793 | } |
| 2794 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2795 | if (!D.isInvalidType()) |
| 2796 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2797 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2798 | << SourceRange(D.getIdentifierLoc()); |
| 2799 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2800 | SC = FunctionDecl::None; |
| 2801 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2802 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2803 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2804 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2805 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2806 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2807 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2808 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2809 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2810 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2811 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2812 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2813 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2814 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2815 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2816 | // Rebuild the function type "R" without any type qualifiers (in |
| 2817 | // case any of the errors above fired) and with "void" as the |
| 2818 | // return type, since constructors don't have return types. We |
| 2819 | // *always* have to do this, because GetTypeForDeclarator will |
| 2820 | // put in a result type of "int" when none was specified. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2821 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2822 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 2823 | Proto->getNumArgs(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2824 | Proto->isVariadic(), 0, |
| 2825 | Proto->hasExceptionSpec(), |
| 2826 | Proto->hasAnyExceptionSpec(), |
| 2827 | Proto->getNumExceptions(), |
| 2828 | Proto->exception_begin(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2829 | Proto->getExtInfo()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2832 | /// CheckConstructor - Checks a fully-formed constructor for |
| 2833 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 2834 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2835 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2836 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2837 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 2838 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2839 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2840 | |
| 2841 | // C++ [class.copy]p3: |
| 2842 | // A declaration of a constructor for a class X is ill-formed if |
| 2843 | // its first parameter is of type (optionally cv-qualified) X and |
| 2844 | // either there are no other parameters or else all other |
| 2845 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2846 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2847 | ((Constructor->getNumParams() == 1) || |
| 2848 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2849 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 2850 | Constructor->getTemplateSpecializationKind() |
| 2851 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2852 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 2853 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 2854 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2855 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
| 2856 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2857 | << FixItHint::CreateInsertion(ParamLoc, " const &"); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2858 | |
| 2859 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 2860 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2861 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2862 | } |
| 2863 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2864 | |
John McCall | 3d04336 | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2865 | // Notify the class that we've added a constructor. In principle we |
| 2866 | // don't need to do this for out-of-line declarations; in practice |
| 2867 | // we only instantiate the most recent declaration of a method, so |
| 2868 | // we have to call this for everything but friends. |
| 2869 | if (!Constructor->getFriendObjectKind()) |
| 2870 | ClassDecl->addedConstructor(Context, Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2873 | /// CheckDestructor - Checks a fully-formed destructor for well-formedness, |
| 2874 | /// issuing any diagnostics required. Returns true on error. |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2875 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2876 | CXXRecordDecl *RD = Destructor->getParent(); |
| 2877 | |
| 2878 | if (Destructor->isVirtual()) { |
| 2879 | SourceLocation Loc; |
| 2880 | |
| 2881 | if (!Destructor->isImplicit()) |
| 2882 | Loc = Destructor->getLocation(); |
| 2883 | else |
| 2884 | Loc = RD->getLocation(); |
| 2885 | |
| 2886 | // If we have a virtual destructor, look up the deallocation function |
| 2887 | FunctionDecl *OperatorDelete = 0; |
| 2888 | DeclarationName Name = |
| 2889 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2890 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2891 | return true; |
| 2892 | |
| 2893 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2894 | } |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2895 | |
| 2896 | return false; |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2899 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2900 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 2901 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 2902 | FTI.ArgInfo[0].Param && |
| 2903 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType()); |
| 2904 | } |
| 2905 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2906 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 2907 | /// the well-formednes of the destructor declarator @p D with type @p |
| 2908 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2909 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 2910 | /// will be updated to reflect a well-formed type for the destructor and |
| 2911 | /// returned. |
| 2912 | QualType Sema::CheckDestructorDeclarator(Declarator &D, |
| 2913 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2914 | // C++ [class.dtor]p1: |
| 2915 | // [...] A typedef-name that names a class is a class-name |
| 2916 | // (7.1.3); however, a typedef-name that names a class shall not |
| 2917 | // be used as the identifier in the declarator for a destructor |
| 2918 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2919 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2920 | if (isa<TypedefType>(DeclaratorType)) { |
| 2921 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2922 | << DeclaratorType; |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2923 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
| 2926 | // C++ [class.dtor]p2: |
| 2927 | // A destructor is used to destroy objects of its class type. A |
| 2928 | // destructor takes no parameters, and no return type can be |
| 2929 | // specified for it (not even void). The address of a destructor |
| 2930 | // shall not be taken. A destructor shall not be static. A |
| 2931 | // destructor can be invoked for a const, volatile or const |
| 2932 | // volatile object. A destructor shall not be declared const, |
| 2933 | // volatile or const volatile (9.3.2). |
| 2934 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2935 | if (!D.isInvalidType()) |
| 2936 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 2937 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2938 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2939 | SC = FunctionDecl::None; |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2940 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2941 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2942 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2943 | // Destructors don't have return types, but the parser will |
| 2944 | // happily parse something like: |
| 2945 | // |
| 2946 | // class X { |
| 2947 | // float ~X(); |
| 2948 | // }; |
| 2949 | // |
| 2950 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2951 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 2952 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2953 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2954 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2955 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2956 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2957 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2958 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2959 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2960 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2961 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2962 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2963 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2964 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2965 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2966 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2967 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
| 2970 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2971 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2972 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 2973 | |
| 2974 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2975 | FTI.freeArgs(); |
| 2976 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2979 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2980 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2981 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2982 | D.setInvalidType(); |
| 2983 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2984 | |
| 2985 | // Rebuild the function type "R" without any type qualifiers or |
| 2986 | // parameters (in case any of the errors above fired) and with |
| 2987 | // "void" as the return type, since destructors don't have return |
| 2988 | // types. We *always* have to do this, because GetTypeForDeclarator |
| 2989 | // will put in a result type of "int" when none was specified. |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2990 | // FIXME: Exceptions! |
| 2991 | return Context.getFunctionType(Context.VoidTy, 0, 0, false, 0, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2992 | false, false, 0, 0, FunctionType::ExtInfo()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2995 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 2996 | /// well-formednes of the conversion function declarator @p D with |
| 2997 | /// type @p R. If there are any errors in the declarator, this routine |
| 2998 | /// will emit diagnostics and return true. Otherwise, it will return |
| 2999 | /// false. Either way, the type @p R will be updated to reflect a |
| 3000 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3001 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3002 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3003 | // C++ [class.conv.fct]p1: |
| 3004 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3005 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3006 | // parameter returning conversion-type-id." |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3007 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3008 | if (!D.isInvalidType()) |
| 3009 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 3010 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 3011 | << SourceRange(D.getIdentifierLoc()); |
| 3012 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3013 | SC = FunctionDecl::None; |
| 3014 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3015 | |
| 3016 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 3017 | |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3018 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3019 | // Conversion functions don't have return types, but the parser will |
| 3020 | // happily parse something like: |
| 3021 | // |
| 3022 | // class X { |
| 3023 | // float operator bool(); |
| 3024 | // }; |
| 3025 | // |
| 3026 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3027 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 3028 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 3029 | << SourceRange(D.getIdentifierLoc()); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3030 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3033 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 3034 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3035 | // Make sure we don't have any parameters. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3036 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3037 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 3038 | |
| 3039 | // Delete the parameters. |
Chris Lattner | 1833a83 | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 3040 | D.getTypeObject(0).Fun.freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3041 | D.setInvalidType(); |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3042 | } else if (Proto->isVariadic()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3043 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3044 | D.setInvalidType(); |
| 3045 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3046 | |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3047 | // Diagnose "&operator bool()" and other such nonsense. This |
| 3048 | // is actually a gcc extension which we don't support. |
| 3049 | if (Proto->getResultType() != ConvType) { |
| 3050 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 3051 | << Proto->getResultType(); |
| 3052 | D.setInvalidType(); |
| 3053 | ConvType = Proto->getResultType(); |
| 3054 | } |
| 3055 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3056 | // C++ [class.conv.fct]p4: |
| 3057 | // The conversion-type-id shall not represent a function type nor |
| 3058 | // an array type. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3059 | if (ConvType->isArrayType()) { |
| 3060 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 3061 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3062 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3063 | } else if (ConvType->isFunctionType()) { |
| 3064 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 3065 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3066 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
| 3069 | // Rebuild the function type "R" without any parameters (in case any |
| 3070 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3071 | // return type. |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3072 | if (D.isInvalidType()) { |
| 3073 | R = Context.getFunctionType(ConvType, 0, 0, false, |
| 3074 | Proto->getTypeQuals(), |
| 3075 | Proto->hasExceptionSpec(), |
| 3076 | Proto->hasAnyExceptionSpec(), |
| 3077 | Proto->getNumExceptions(), |
| 3078 | Proto->exception_begin(), |
| 3079 | Proto->getExtInfo()); |
| 3080 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3081 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3082 | // C++0x explicit conversion operators. |
| 3083 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3084 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3085 | diag::warn_explicit_conversion_functions) |
| 3086 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3089 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 3090 | /// the declaration of the given C++ conversion function. This routine |
| 3091 | /// is responsible for recording the conversion function in the C++ |
| 3092 | /// class, if possible. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3093 | Sema::DeclPtrTy Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3094 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 3095 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 3096 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3097 | |
| 3098 | // Make sure we aren't redeclaring the conversion function. |
| 3099 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3100 | |
| 3101 | // C++ [class.conv.fct]p1: |
| 3102 | // [...] A conversion function is never used to convert a |
| 3103 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 3104 | // same object type (or a reference to it), to a (possibly |
| 3105 | // cv-qualified) base class of that type (or a reference to it), |
| 3106 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3107 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 3108 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3109 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3110 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3111 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3112 | ConvType = ConvTypeRef->getPointeeType(); |
| 3113 | if (ConvType->isRecordType()) { |
| 3114 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 3115 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3116 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3117 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3118 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3119 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3120 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3121 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3122 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3123 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3126 | if (Conversion->getPrimaryTemplate()) { |
| 3127 | // ignore specializations |
| 3128 | } else if (Conversion->getPreviousDeclaration()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | if (FunctionTemplateDecl *ConversionTemplate |
Douglas Gregor | 0c55106 | 2010-01-11 18:53:25 +0000 | [diff] [blame] | 3130 | = Conversion->getDescribedFunctionTemplate()) { |
| 3131 | if (ClassDecl->replaceConversion( |
| 3132 | ConversionTemplate->getPreviousDeclaration(), |
| 3133 | ConversionTemplate)) |
| 3134 | return DeclPtrTy::make(ConversionTemplate); |
| 3135 | } else if (ClassDecl->replaceConversion(Conversion->getPreviousDeclaration(), |
| 3136 | Conversion)) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3137 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 3138 | assert(Conversion->isInvalidDecl() && "Conversion should not get here."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3139 | } else if (FunctionTemplateDecl *ConversionTemplate |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3140 | = Conversion->getDescribedFunctionTemplate()) |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 3141 | ClassDecl->addConversionFunction(ConversionTemplate); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3142 | else |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 3143 | ClassDecl->addConversionFunction(Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3144 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3145 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3148 | //===----------------------------------------------------------------------===// |
| 3149 | // Namespace Handling |
| 3150 | //===----------------------------------------------------------------------===// |
| 3151 | |
| 3152 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 3153 | /// definition. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3154 | Sema::DeclPtrTy Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 3155 | SourceLocation IdentLoc, |
| 3156 | IdentifierInfo *II, |
Anders Carlsson | 2a3503d | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 3157 | SourceLocation LBrace, |
| 3158 | AttributeList *AttrList) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3159 | NamespaceDecl *Namespc = |
| 3160 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 3161 | Namespc->setLBracLoc(LBrace); |
| 3162 | |
| 3163 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 3164 | |
Anders Carlsson | 2a3503d | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 3165 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 3166 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3167 | if (II) { |
| 3168 | // C++ [namespace.def]p2: |
| 3169 | // The identifier in an original-namespace-definition shall not have been |
| 3170 | // previously defined in the declarative region in which the |
| 3171 | // original-namespace-definition appears. The identifier in an |
| 3172 | // original-namespace-definition is the name of the namespace. Subsequently |
| 3173 | // in that declarative region, it is treated as an original-namespace-name. |
| 3174 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3175 | NamedDecl *PrevDecl |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 3176 | = LookupSingleName(DeclRegionScope, II, IdentLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 3177 | ForRedeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3178 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3179 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 3180 | // This is an extended namespace definition. |
| 3181 | // Attach this namespace decl to the chain of extended namespace |
| 3182 | // definitions. |
| 3183 | OrigNS->setNextNamespace(Namespc); |
| 3184 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3185 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | // Remove the previous declaration from the scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3187 | if (DeclRegionScope->isDeclScope(DeclPtrTy::make(OrigNS))) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 3188 | IdResolver.RemoveDecl(OrigNS); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3189 | DeclRegionScope->RemoveDecl(DeclPtrTy::make(OrigNS)); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3190 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3191 | } else if (PrevDecl) { |
| 3192 | // This is an invalid name redefinition. |
| 3193 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 3194 | << Namespc->getDeclName(); |
| 3195 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3196 | Namespc->setInvalidDecl(); |
| 3197 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3198 | } else if (II->isStr("std") && |
| 3199 | CurContext->getLookupContext()->isTranslationUnit()) { |
| 3200 | // This is the first "real" definition of the namespace "std", so update |
| 3201 | // our cache of the "std" namespace to point at this definition. |
| 3202 | if (StdNamespace) { |
| 3203 | // We had already defined a dummy namespace "std". Link this new |
| 3204 | // namespace definition to the dummy namespace "std". |
| 3205 | StdNamespace->setNextNamespace(Namespc); |
| 3206 | StdNamespace->setLocation(IdentLoc); |
| 3207 | Namespc->setOriginalNamespace(StdNamespace->getOriginalNamespace()); |
| 3208 | } |
| 3209 | |
| 3210 | // Make our StdNamespace cache point at the first real definition of the |
| 3211 | // "std" namespace. |
| 3212 | StdNamespace = Namespc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3213 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3214 | |
| 3215 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 3216 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3217 | // Anonymous namespaces. |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3218 | assert(Namespc->isAnonymousNamespace()); |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3219 | |
| 3220 | // Link the anonymous namespace into its parent. |
| 3221 | NamespaceDecl *PrevDecl; |
| 3222 | DeclContext *Parent = CurContext->getLookupContext(); |
| 3223 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 3224 | PrevDecl = TU->getAnonymousNamespace(); |
| 3225 | TU->setAnonymousNamespace(Namespc); |
| 3226 | } else { |
| 3227 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 3228 | PrevDecl = ND->getAnonymousNamespace(); |
| 3229 | ND->setAnonymousNamespace(Namespc); |
| 3230 | } |
| 3231 | |
| 3232 | // Link the anonymous namespace with its previous declaration. |
| 3233 | if (PrevDecl) { |
| 3234 | assert(PrevDecl->isAnonymousNamespace()); |
| 3235 | assert(!PrevDecl->getNextNamespace()); |
| 3236 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 3237 | PrevDecl->setNextNamespace(Namespc); |
| 3238 | } |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3239 | |
Douglas Gregor | a418147 | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 3240 | CurContext->addDecl(Namespc); |
| 3241 | |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3242 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 3243 | // behaves as if it were replaced by |
| 3244 | // namespace unique { /* empty body */ } |
| 3245 | // using namespace unique; |
| 3246 | // namespace unique { namespace-body } |
| 3247 | // where all occurrences of 'unique' in a translation unit are |
| 3248 | // replaced by the same identifier and this identifier differs |
| 3249 | // from all other identifiers in the entire program. |
| 3250 | |
| 3251 | // We just create the namespace with an empty name and then add an |
| 3252 | // implicit using declaration, just like the standard suggests. |
| 3253 | // |
| 3254 | // CodeGen enforces the "universally unique" aspect by giving all |
| 3255 | // declarations semantically contained within an anonymous |
| 3256 | // namespace internal linkage. |
| 3257 | |
John McCall | 5fdd764 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3258 | if (!PrevDecl) { |
| 3259 | UsingDirectiveDecl* UD |
| 3260 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 3261 | /* 'using' */ LBrace, |
| 3262 | /* 'namespace' */ SourceLocation(), |
| 3263 | /* qualifier */ SourceRange(), |
| 3264 | /* NNS */ NULL, |
| 3265 | /* identifier */ SourceLocation(), |
| 3266 | Namespc, |
| 3267 | /* Ancestor */ CurContext); |
| 3268 | UD->setImplicit(); |
| 3269 | CurContext->addDecl(UD); |
| 3270 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 3274 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3275 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 3276 | // for the namespace has the declarations that showed up in that particular |
| 3277 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3278 | PushDeclContext(NamespcScope, Namespc); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3279 | return DeclPtrTy::make(Namespc); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3282 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 3283 | /// is a namespace alias, returns the namespace it points to. |
| 3284 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 3285 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 3286 | return AD->getNamespace(); |
| 3287 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 3288 | } |
| 3289 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3290 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 3291 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3292 | void Sema::ActOnFinishNamespaceDef(DeclPtrTy D, SourceLocation RBrace) { |
| 3293 | Decl *Dcl = D.getAs<Decl>(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3294 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 3295 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 3296 | Namespc->setRBracLoc(RBrace); |
| 3297 | PopDeclContext(); |
| 3298 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3299 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3300 | Sema::DeclPtrTy Sema::ActOnUsingDirective(Scope *S, |
| 3301 | SourceLocation UsingLoc, |
| 3302 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3303 | CXXScopeSpec &SS, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3304 | SourceLocation IdentLoc, |
| 3305 | IdentifierInfo *NamespcName, |
| 3306 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3307 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3308 | assert(NamespcName && "Invalid NamespcName."); |
| 3309 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3310 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3311 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3312 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3313 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 3314 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3315 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 3316 | LookupParsedName(R, S, &SS); |
| 3317 | if (R.isAmbiguous()) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3318 | return DeclPtrTy(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3319 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3320 | if (!R.empty()) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3321 | NamedDecl *Named = R.getFoundDecl(); |
| 3322 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 3323 | && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3324 | // C++ [namespace.udir]p1: |
| 3325 | // A using-directive specifies that the names in the nominated |
| 3326 | // namespace can be used in the scope in which the |
| 3327 | // using-directive appears after the using-directive. During |
| 3328 | // unqualified name lookup (3.4.1), the names appear as if they |
| 3329 | // were declared in the nearest enclosing namespace which |
| 3330 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3331 | // namespace. [Note: in this context, "contains" means "contains |
| 3332 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3333 | |
| 3334 | // Find enclosing context containing both using-directive and |
| 3335 | // nominated namespace. |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3336 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3337 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 3338 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 3339 | CommonAncestor = CommonAncestor->getParent(); |
| 3340 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3341 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 3342 | SS.getRange(), |
| 3343 | (NestedNameSpecifier *)SS.getScopeRep(), |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3344 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3345 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3346 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 3347 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3350 | // FIXME: We ignore attributes for now. |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3351 | delete AttrList; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3352 | return DeclPtrTy::make(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 3356 | // If scope has associated entity, then using directive is at namespace |
| 3357 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 3358 | // it's lookup structure. |
| 3359 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3360 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3361 | else |
| 3362 | // Otherwise it is block-sope. using-directives will affect lookup |
| 3363 | // only to the end of scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3364 | S->PushUsingDirective(DeclPtrTy::make(UDir)); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3365 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3366 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3367 | |
| 3368 | Sema::DeclPtrTy Sema::ActOnUsingDeclaration(Scope *S, |
Anders Carlsson | 595adc1 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 3369 | AccessSpecifier AS, |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3370 | bool HasUsingKeyword, |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3371 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3372 | CXXScopeSpec &SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3373 | UnqualifiedId &Name, |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3374 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3375 | bool IsTypeName, |
| 3376 | SourceLocation TypenameLoc) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3377 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3378 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3379 | switch (Name.getKind()) { |
| 3380 | case UnqualifiedId::IK_Identifier: |
| 3381 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3382 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3383 | case UnqualifiedId::IK_ConversionFunctionId: |
| 3384 | break; |
| 3385 | |
| 3386 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3387 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3388 | // C++0x inherited constructors. |
| 3389 | if (getLangOptions().CPlusPlus0x) break; |
| 3390 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3391 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 3392 | << SS.getRange(); |
| 3393 | return DeclPtrTy(); |
| 3394 | |
| 3395 | case UnqualifiedId::IK_DestructorName: |
| 3396 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 3397 | << SS.getRange(); |
| 3398 | return DeclPtrTy(); |
| 3399 | |
| 3400 | case UnqualifiedId::IK_TemplateId: |
| 3401 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 3402 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
| 3403 | return DeclPtrTy(); |
| 3404 | } |
| 3405 | |
| 3406 | DeclarationName TargetName = GetNameFromUnqualifiedId(Name); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3407 | if (!TargetName) |
| 3408 | return DeclPtrTy(); |
| 3409 | |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3410 | // Warn about using declarations. |
| 3411 | // TODO: store that the declaration was written without 'using' and |
| 3412 | // talk about access decls instead of using decls in the |
| 3413 | // diagnostics. |
| 3414 | if (!HasUsingKeyword) { |
| 3415 | UsingLoc = Name.getSourceRange().getBegin(); |
| 3416 | |
| 3417 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3418 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | 60fa3cf | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3421 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3422 | Name.getSourceRange().getBegin(), |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3423 | TargetName, AttrList, |
| 3424 | /* IsInstantiation */ false, |
| 3425 | IsTypeName, TypenameLoc); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3426 | if (UD) |
| 3427 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3428 | |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3429 | return DeclPtrTy::make(UD); |
| 3430 | } |
| 3431 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3432 | /// Determines whether to create a using shadow decl for a particular |
| 3433 | /// decl, given the set of decls existing prior to this using lookup. |
| 3434 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 3435 | const LookupResult &Previous) { |
| 3436 | // Diagnose finding a decl which is not from a base class of the |
| 3437 | // current class. We do this now because there are cases where this |
| 3438 | // function will silently decide not to build a shadow decl, which |
| 3439 | // will pre-empt further diagnostics. |
| 3440 | // |
| 3441 | // We don't need to do this in C++0x because we do the check once on |
| 3442 | // the qualifier. |
| 3443 | // |
| 3444 | // FIXME: diagnose the following if we care enough: |
| 3445 | // struct A { int foo; }; |
| 3446 | // struct B : A { using A::foo; }; |
| 3447 | // template <class T> struct C : A {}; |
| 3448 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 3449 | // This is invalid (during instantiation) in C++03 because B::foo |
| 3450 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 3451 | // We can't diagnose it immediately because C<T> is an unknown |
| 3452 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 3453 | // to A::foo, which will look well-formed when we instantiate. |
| 3454 | // The right solution is to not collapse the shadow-decl chain. |
| 3455 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 3456 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 3457 | |
| 3458 | // Handle enums and anonymous structs. |
| 3459 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 3460 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 3461 | while (OrigRec->isAnonymousStructOrUnion()) |
| 3462 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 3463 | |
| 3464 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 3465 | if (OrigDC == CurContext) { |
| 3466 | Diag(Using->getLocation(), |
| 3467 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3468 | << Using->getNestedNameRange(); |
| 3469 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3470 | return true; |
| 3471 | } |
| 3472 | |
| 3473 | Diag(Using->getNestedNameRange().getBegin(), |
| 3474 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3475 | << Using->getTargetNestedNameDecl() |
| 3476 | << cast<CXXRecordDecl>(CurContext) |
| 3477 | << Using->getNestedNameRange(); |
| 3478 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3479 | return true; |
| 3480 | } |
| 3481 | } |
| 3482 | |
| 3483 | if (Previous.empty()) return false; |
| 3484 | |
| 3485 | NamedDecl *Target = Orig; |
| 3486 | if (isa<UsingShadowDecl>(Target)) |
| 3487 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3488 | |
John McCall | d7533ec | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3489 | // If the target happens to be one of the previous declarations, we |
| 3490 | // don't have a conflict. |
| 3491 | // |
| 3492 | // FIXME: but we might be increasing its access, in which case we |
| 3493 | // should redeclare it. |
| 3494 | NamedDecl *NonTag = 0, *Tag = 0; |
| 3495 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 3496 | I != E; ++I) { |
| 3497 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3498 | if (D->getCanonicalDecl() == Target->getCanonicalDecl()) |
| 3499 | return false; |
| 3500 | |
| 3501 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 3502 | } |
| 3503 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3504 | if (Target->isFunctionOrFunctionTemplate()) { |
| 3505 | FunctionDecl *FD; |
| 3506 | if (isa<FunctionTemplateDecl>(Target)) |
| 3507 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 3508 | else |
| 3509 | FD = cast<FunctionDecl>(Target); |
| 3510 | |
| 3511 | NamedDecl *OldDecl = 0; |
| 3512 | switch (CheckOverload(FD, Previous, OldDecl)) { |
| 3513 | case Ovl_Overload: |
| 3514 | return false; |
| 3515 | |
| 3516 | case Ovl_NonFunction: |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3517 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3518 | break; |
| 3519 | |
| 3520 | // We found a decl with the exact signature. |
| 3521 | case Ovl_Match: |
| 3522 | if (isa<UsingShadowDecl>(OldDecl)) { |
| 3523 | // Silently ignore the possible conflict. |
| 3524 | return false; |
| 3525 | } |
| 3526 | |
| 3527 | // If we're in a record, we want to hide the target, so we |
| 3528 | // return true (without a diagnostic) to tell the caller not to |
| 3529 | // build a shadow decl. |
| 3530 | if (CurContext->isRecord()) |
| 3531 | return true; |
| 3532 | |
| 3533 | // If we're not in a record, this is an error. |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3534 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3535 | break; |
| 3536 | } |
| 3537 | |
| 3538 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3539 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 3540 | return true; |
| 3541 | } |
| 3542 | |
| 3543 | // Target is not a function. |
| 3544 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3545 | if (isa<TagDecl>(Target)) { |
| 3546 | // No conflict between a tag and a non-tag. |
| 3547 | if (!Tag) return false; |
| 3548 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3549 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3550 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3551 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 3552 | return true; |
| 3553 | } |
| 3554 | |
| 3555 | // No conflict between a tag and a non-tag. |
| 3556 | if (!NonTag) return false; |
| 3557 | |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3558 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3559 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3560 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 3561 | return true; |
| 3562 | } |
| 3563 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3564 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3565 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3566 | UsingDecl *UD, |
| 3567 | NamedDecl *Orig) { |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3568 | |
| 3569 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3570 | NamedDecl *Target = Orig; |
| 3571 | if (isa<UsingShadowDecl>(Target)) { |
| 3572 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3573 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | UsingShadowDecl *Shadow |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3577 | = UsingShadowDecl::Create(Context, CurContext, |
| 3578 | UD->getLocation(), UD, Target); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3579 | UD->addShadowDecl(Shadow); |
| 3580 | |
| 3581 | if (S) |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3582 | PushOnScopeChains(Shadow, S); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3583 | else |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3584 | CurContext->addDecl(Shadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3585 | Shadow->setAccess(UD->getAccess()); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3586 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3587 | // Register it as a conversion if appropriate. |
| 3588 | if (Shadow->getDeclName().getNameKind() |
| 3589 | == DeclarationName::CXXConversionFunctionName) |
| 3590 | cast<CXXRecordDecl>(CurContext)->addConversionFunction(Shadow); |
| 3591 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3592 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 3593 | Shadow->setInvalidDecl(); |
| 3594 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3595 | return Shadow; |
| 3596 | } |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3597 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3598 | /// Hides a using shadow declaration. This is required by the current |
| 3599 | /// using-decl implementation when a resolvable using declaration in a |
| 3600 | /// class is followed by a declaration which would hide or override |
| 3601 | /// one or more of the using decl's targets; for example: |
| 3602 | /// |
| 3603 | /// struct Base { void foo(int); }; |
| 3604 | /// struct Derived : Base { |
| 3605 | /// using Base::foo; |
| 3606 | /// void foo(int); |
| 3607 | /// }; |
| 3608 | /// |
| 3609 | /// The governing language is C++03 [namespace.udecl]p12: |
| 3610 | /// |
| 3611 | /// When a using-declaration brings names from a base class into a |
| 3612 | /// derived class scope, member functions in the derived class |
| 3613 | /// override and/or hide member functions with the same name and |
| 3614 | /// parameter types in a base class (rather than conflicting). |
| 3615 | /// |
| 3616 | /// There are two ways to implement this: |
| 3617 | /// (1) optimistically create shadow decls when they're not hidden |
| 3618 | /// by existing declarations, or |
| 3619 | /// (2) don't create any shadow decls (or at least don't make them |
| 3620 | /// visible) until we've fully parsed/instantiated the class. |
| 3621 | /// The problem with (1) is that we might have to retroactively remove |
| 3622 | /// a shadow decl, which requires several O(n) operations because the |
| 3623 | /// decl structures are (very reasonably) not designed for removal. |
| 3624 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 3625 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3626 | if (Shadow->getDeclName().getNameKind() == |
| 3627 | DeclarationName::CXXConversionFunctionName) |
| 3628 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 3629 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3630 | // Remove it from the DeclContext... |
| 3631 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3632 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3633 | // ...and the scope, if applicable... |
| 3634 | if (S) { |
| 3635 | S->RemoveDecl(DeclPtrTy::make(static_cast<Decl*>(Shadow))); |
| 3636 | IdResolver.RemoveDecl(Shadow); |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3639 | // ...and the using decl. |
| 3640 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 3641 | |
| 3642 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3643 | // be possible for this to happen, because...? |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3646 | /// Builds a using declaration. |
| 3647 | /// |
| 3648 | /// \param IsInstantiation - Whether this call arises from an |
| 3649 | /// instantiation of an unresolved using declaration. We treat |
| 3650 | /// the lookup differently for these declarations. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3651 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 3652 | SourceLocation UsingLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3653 | CXXScopeSpec &SS, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3654 | SourceLocation IdentLoc, |
| 3655 | DeclarationName Name, |
| 3656 | AttributeList *AttrList, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3657 | bool IsInstantiation, |
| 3658 | bool IsTypeName, |
| 3659 | SourceLocation TypenameLoc) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3660 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3661 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 3662 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3663 | // FIXME: We ignore attributes for now. |
| 3664 | delete AttrList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3665 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3666 | if (SS.isEmpty()) { |
| 3667 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3668 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3669 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3670 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3671 | // Do the redeclaration lookup in the current scope. |
| 3672 | LookupResult Previous(*this, Name, IdentLoc, LookupUsingDeclName, |
| 3673 | ForRedeclaration); |
| 3674 | Previous.setHideTags(false); |
| 3675 | if (S) { |
| 3676 | LookupName(Previous, S); |
| 3677 | |
| 3678 | // It is really dumb that we have to do this. |
| 3679 | LookupResult::Filter F = Previous.makeFilter(); |
| 3680 | while (F.hasNext()) { |
| 3681 | NamedDecl *D = F.next(); |
| 3682 | if (!isDeclInScope(D, CurContext, S)) |
| 3683 | F.erase(); |
| 3684 | } |
| 3685 | F.done(); |
| 3686 | } else { |
| 3687 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 3688 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 3689 | LookupQualifiedName(Previous, CurContext); |
| 3690 | } |
| 3691 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3692 | NestedNameSpecifier *NNS = |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3693 | static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3694 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3695 | // Check for invalid redeclarations. |
| 3696 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 3697 | return 0; |
| 3698 | |
| 3699 | // Check for bad qualifiers. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3700 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 3701 | return 0; |
| 3702 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3703 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3704 | NamedDecl *D; |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3705 | if (!LookupContext) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3706 | if (IsTypeName) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3707 | // FIXME: not all declaration name kinds are legal here |
| 3708 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 3709 | UsingLoc, TypenameLoc, |
| 3710 | SS.getRange(), NNS, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3711 | IdentLoc, Name); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3712 | } else { |
| 3713 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, |
| 3714 | UsingLoc, SS.getRange(), NNS, |
| 3715 | IdentLoc, Name); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3716 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3717 | } else { |
| 3718 | D = UsingDecl::Create(Context, CurContext, IdentLoc, |
| 3719 | SS.getRange(), UsingLoc, NNS, Name, |
| 3720 | IsTypeName); |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3721 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3722 | D->setAccess(AS); |
| 3723 | CurContext->addDecl(D); |
| 3724 | |
| 3725 | if (!LookupContext) return D; |
| 3726 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3727 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3728 | if (RequireCompleteDeclContext(SS)) { |
| 3729 | UD->setInvalidDecl(); |
| 3730 | return UD; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3733 | // Look up the target name. |
| 3734 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3735 | LookupResult R(*this, Name, IdentLoc, LookupOrdinaryName); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3736 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3737 | // Unlike most lookups, we don't always want to hide tag |
| 3738 | // declarations: tag names are visible through the using declaration |
| 3739 | // even if hidden by ordinary names, *except* in a dependent context |
| 3740 | // where it's important for the sanity of two-phase lookup. |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3741 | if (!IsInstantiation) |
| 3742 | R.setHideTags(false); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3743 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3744 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3745 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3746 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 3747 | Diag(IdentLoc, diag::err_no_member) |
| 3748 | << Name << LookupContext << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3749 | UD->setInvalidDecl(); |
| 3750 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3753 | if (R.isAmbiguous()) { |
| 3754 | UD->setInvalidDecl(); |
| 3755 | return UD; |
| 3756 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3757 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3758 | if (IsTypeName) { |
| 3759 | // 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] | 3760 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3761 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 3762 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 3763 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 3764 | diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3765 | UD->setInvalidDecl(); |
| 3766 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3767 | } |
| 3768 | } else { |
| 3769 | // If we asked for a non-typename and we got a type, error out, |
| 3770 | // but only if this is an instantiation of an unresolved using |
| 3771 | // decl. Otherwise just silently find the type name. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3772 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3773 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 3774 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3775 | UD->setInvalidDecl(); |
| 3776 | return UD; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3777 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3780 | // C++0x N2914 [namespace.udecl]p6: |
| 3781 | // A using-declaration shall not name a namespace. |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3782 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3783 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 3784 | << SS.getRange(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3785 | UD->setInvalidDecl(); |
| 3786 | return UD; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3787 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3788 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3789 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 3790 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 3791 | BuildUsingShadowDecl(S, UD, *I); |
| 3792 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3793 | |
| 3794 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3795 | } |
| 3796 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3797 | /// Checks that the given using declaration is not an invalid |
| 3798 | /// redeclaration. Note that this is checking only for the using decl |
| 3799 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 3800 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 3801 | bool isTypeName, |
| 3802 | const CXXScopeSpec &SS, |
| 3803 | SourceLocation NameLoc, |
| 3804 | const LookupResult &Prev) { |
| 3805 | // C++03 [namespace.udecl]p8: |
| 3806 | // C++0x [namespace.udecl]p10: |
| 3807 | // A using-declaration is a declaration and can therefore be used |
| 3808 | // repeatedly where (and only where) multiple declarations are |
| 3809 | // allowed. |
| 3810 | // That's only in file contexts. |
| 3811 | if (CurContext->getLookupContext()->isFileContext()) |
| 3812 | return false; |
| 3813 | |
| 3814 | NestedNameSpecifier *Qual |
| 3815 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 3816 | |
| 3817 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 3818 | NamedDecl *D = *I; |
| 3819 | |
| 3820 | bool DTypename; |
| 3821 | NestedNameSpecifier *DQual; |
| 3822 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 3823 | DTypename = UD->isTypeName(); |
| 3824 | DQual = UD->getTargetNestedNameDecl(); |
| 3825 | } else if (UnresolvedUsingValueDecl *UD |
| 3826 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 3827 | DTypename = false; |
| 3828 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3829 | } else if (UnresolvedUsingTypenameDecl *UD |
| 3830 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 3831 | DTypename = true; |
| 3832 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3833 | } else continue; |
| 3834 | |
| 3835 | // using decls differ if one says 'typename' and the other doesn't. |
| 3836 | // FIXME: non-dependent using decls? |
| 3837 | if (isTypeName != DTypename) continue; |
| 3838 | |
| 3839 | // using decls differ if they name different scopes (but note that |
| 3840 | // template instantiation can cause this check to trigger when it |
| 3841 | // didn't before instantiation). |
| 3842 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 3843 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 3844 | continue; |
| 3845 | |
| 3846 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3847 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3848 | return true; |
| 3849 | } |
| 3850 | |
| 3851 | return false; |
| 3852 | } |
| 3853 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3854 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3855 | /// Checks that the given nested-name qualifier used in a using decl |
| 3856 | /// in the current context is appropriately related to the current |
| 3857 | /// scope. If an error is found, diagnoses it and returns true. |
| 3858 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 3859 | const CXXScopeSpec &SS, |
| 3860 | SourceLocation NameLoc) { |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3861 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3862 | |
John McCall | 604e7f1 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3863 | if (!CurContext->isRecord()) { |
| 3864 | // C++03 [namespace.udecl]p3: |
| 3865 | // C++0x [namespace.udecl]p8: |
| 3866 | // A using-declaration for a class member shall be a member-declaration. |
| 3867 | |
| 3868 | // If we weren't able to compute a valid scope, it must be a |
| 3869 | // dependent class scope. |
| 3870 | if (!NamedContext || NamedContext->isRecord()) { |
| 3871 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 3872 | << SS.getRange(); |
| 3873 | return true; |
| 3874 | } |
| 3875 | |
| 3876 | // Otherwise, everything is known to be fine. |
| 3877 | return false; |
| 3878 | } |
| 3879 | |
| 3880 | // The current scope is a record. |
| 3881 | |
| 3882 | // If the named context is dependent, we can't decide much. |
| 3883 | if (!NamedContext) { |
| 3884 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 3885 | // nested-name-specifier does not refer to a base class, which is |
| 3886 | // still possible in some cases. |
| 3887 | |
| 3888 | // Otherwise we have to conservatively report that things might be |
| 3889 | // okay. |
| 3890 | return false; |
| 3891 | } |
| 3892 | |
| 3893 | if (!NamedContext->isRecord()) { |
| 3894 | // Ideally this would point at the last name in the specifier, |
| 3895 | // but we don't have that level of source info. |
| 3896 | Diag(SS.getRange().getBegin(), |
| 3897 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 3898 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 3899 | return true; |
| 3900 | } |
| 3901 | |
| 3902 | if (getLangOptions().CPlusPlus0x) { |
| 3903 | // C++0x [namespace.udecl]p3: |
| 3904 | // In a using-declaration used as a member-declaration, the |
| 3905 | // nested-name-specifier shall name a base class of the class |
| 3906 | // being defined. |
| 3907 | |
| 3908 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 3909 | cast<CXXRecordDecl>(NamedContext))) { |
| 3910 | if (CurContext == NamedContext) { |
| 3911 | Diag(NameLoc, |
| 3912 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3913 | << SS.getRange(); |
| 3914 | return true; |
| 3915 | } |
| 3916 | |
| 3917 | Diag(SS.getRange().getBegin(), |
| 3918 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3919 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 3920 | << cast<CXXRecordDecl>(CurContext) |
| 3921 | << SS.getRange(); |
| 3922 | return true; |
| 3923 | } |
| 3924 | |
| 3925 | return false; |
| 3926 | } |
| 3927 | |
| 3928 | // C++03 [namespace.udecl]p4: |
| 3929 | // A using-declaration used as a member-declaration shall refer |
| 3930 | // to a member of a base class of the class being defined [etc.]. |
| 3931 | |
| 3932 | // Salient point: SS doesn't have to name a base class as long as |
| 3933 | // lookup only finds members from base classes. Therefore we can |
| 3934 | // diagnose here only if we can prove that that can't happen, |
| 3935 | // i.e. if the class hierarchies provably don't intersect. |
| 3936 | |
| 3937 | // TODO: it would be nice if "definitely valid" results were cached |
| 3938 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 3939 | // need to be repeated. |
| 3940 | |
| 3941 | struct UserData { |
| 3942 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 3943 | |
| 3944 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 3945 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 3946 | Data->Bases.insert(Base); |
| 3947 | return true; |
| 3948 | } |
| 3949 | |
| 3950 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 3951 | return !Class->forallBases(collect, this); |
| 3952 | } |
| 3953 | |
| 3954 | /// Returns true if the base is dependent or is one of the |
| 3955 | /// accumulated base classes. |
| 3956 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 3957 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 3958 | return !Data->Bases.count(Base); |
| 3959 | } |
| 3960 | |
| 3961 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 3962 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 3963 | } |
| 3964 | }; |
| 3965 | |
| 3966 | UserData Data; |
| 3967 | |
| 3968 | // Returns false if we find a dependent base. |
| 3969 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 3970 | return false; |
| 3971 | |
| 3972 | // Returns false if the class has a dependent base or if it or one |
| 3973 | // of its bases is present in the base set of the current context. |
| 3974 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 3975 | return false; |
| 3976 | |
| 3977 | Diag(SS.getRange().getBegin(), |
| 3978 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3979 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 3980 | << cast<CXXRecordDecl>(CurContext) |
| 3981 | << SS.getRange(); |
| 3982 | |
| 3983 | return true; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3986 | Sema::DeclPtrTy Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 3987 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3988 | SourceLocation AliasLoc, |
| 3989 | IdentifierInfo *Alias, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3990 | CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 3991 | SourceLocation IdentLoc, |
| 3992 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3993 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 3994 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3995 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 3996 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 3997 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 3998 | // Check if we have a previous declaration with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3999 | if (NamedDecl *PrevDecl |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4000 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 4001 | ForRedeclaration)) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4002 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4003 | // 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] | 4004 | // namespace, so don't create a new one. |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4005 | // FIXME: At some point, we'll want to create the (redundant) |
| 4006 | // declaration to maintain better source information. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4007 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | c67b032 | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4008 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4009 | return DeclPtrTy(); |
| 4010 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4012 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 4013 | diag::err_redefinition_different_kind; |
| 4014 | Diag(AliasLoc, DiagID) << Alias; |
| 4015 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4016 | return DeclPtrTy(); |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4019 | if (R.isAmbiguous()) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4020 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4021 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4022 | if (R.empty()) { |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 4023 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4024 | return DeclPtrTy(); |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 4025 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4026 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4027 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4028 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 4029 | Alias, SS.getRange(), |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 4030 | (NestedNameSpecifier *)SS.getScopeRep(), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4031 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4032 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 4033 | PushOnScopeChains(AliasDecl, S); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 4034 | return DeclPtrTy::make(AliasDecl); |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4037 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 4038 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4039 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
| 4040 | !Constructor->isUsed()) && |
| 4041 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4042 | |
Anders Carlsson | f6513ed | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 4043 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4044 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 4045 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4046 | DeclContext *PreviousContext = CurContext; |
| 4047 | CurContext = Constructor; |
Anders Carlsson | ec3332b | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 4048 | if (SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false)) { |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4049 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Anders Carlsson | 3b8c53b | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 4050 | << CXXConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4051 | Constructor->setInvalidDecl(); |
| 4052 | } else { |
| 4053 | Constructor->setUsed(); |
| 4054 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4055 | CurContext = PreviousContext; |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4058 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 4059 | CXXDestructorDecl *Destructor) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4060 | assert((Destructor->isImplicit() && !Destructor->isUsed()) && |
| 4061 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4062 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4063 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4064 | |
| 4065 | DeclContext *PreviousContext = CurContext; |
| 4066 | CurContext = Destructor; |
| 4067 | |
John McCall | ef027fe | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 4068 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 4069 | Destructor->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4070 | |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4071 | // FIXME: If CheckDestructor fails, we should emit a note about where the |
| 4072 | // implicit destructor was needed. |
| 4073 | if (CheckDestructor(Destructor)) { |
| 4074 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4075 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 4076 | |
| 4077 | Destructor->setInvalidDecl(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4078 | CurContext = PreviousContext; |
| 4079 | |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4080 | return; |
| 4081 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4082 | CurContext = PreviousContext; |
Anders Carlsson | 3790980 | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4083 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4084 | Destructor->setUsed(); |
| 4085 | } |
| 4086 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4087 | void Sema::DefineImplicitOverloadedAssign(SourceLocation CurrentLocation, |
| 4088 | CXXMethodDecl *MethodDecl) { |
| 4089 | assert((MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 4090 | MethodDecl->getOverloadedOperator() == OO_Equal && |
| 4091 | !MethodDecl->isUsed()) && |
| 4092 | "DefineImplicitOverloadedAssign - call it for implicit assignment op"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4093 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4094 | CXXRecordDecl *ClassDecl |
| 4095 | = cast<CXXRecordDecl>(MethodDecl->getDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4096 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4097 | DeclContext *PreviousContext = CurContext; |
| 4098 | CurContext = MethodDecl; |
| 4099 | |
Fariborz Jahanian | c6249b9 | 2009-06-26 16:08:57 +0000 | [diff] [blame] | 4100 | // C++[class.copy] p12 |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4101 | // Before the implicitly-declared copy assignment operator for a class is |
| 4102 | // implicitly defined, all implicitly-declared copy assignment operators |
| 4103 | // for its direct base classes and its nonstatic data members shall have |
| 4104 | // been implicitly defined. |
| 4105 | bool err = false; |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 4106 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4107 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4108 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4109 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4110 | if (CXXMethodDecl *BaseAssignOpMethod = |
Anders Carlsson | b6cc91b | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 4111 | getAssignOperatorMethod(CurrentLocation, MethodDecl->getParamDecl(0), |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4112 | BaseClassDecl)) { |
| 4113 | CheckDirectMemberAccess(Base->getSourceRange().getBegin(), |
| 4114 | BaseAssignOpMethod, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4115 | PDiag(diag::err_access_assign_base) |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4116 | << Base->getType()); |
| 4117 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4118 | MarkDeclarationReferenced(CurrentLocation, BaseAssignOpMethod); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4119 | } |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4120 | } |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 4121 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4122 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4123 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 4124 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 4125 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4126 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4127 | CXXRecordDecl *FieldClassDecl |
| 4128 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4129 | if (CXXMethodDecl *FieldAssignOpMethod = |
Anders Carlsson | b6cc91b | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 4130 | getAssignOperatorMethod(CurrentLocation, MethodDecl->getParamDecl(0), |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4131 | FieldClassDecl)) { |
| 4132 | CheckDirectMemberAccess(Field->getLocation(), |
| 4133 | FieldAssignOpMethod, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4134 | PDiag(diag::err_access_assign_field) |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4135 | << Field->getDeclName() << Field->getType()); |
| 4136 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4137 | MarkDeclarationReferenced(CurrentLocation, FieldAssignOpMethod); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4138 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 4139 | } else if (FieldType->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 5e09d4c | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 4141 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 4142 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4143 | Diag(CurrentLocation, diag::note_first_required_here); |
| 4144 | err = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 4145 | } else if (FieldType.isConstQualified()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4146 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 5e09d4c | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 4147 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 4148 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4149 | Diag(CurrentLocation, diag::note_first_required_here); |
| 4150 | err = true; |
| 4151 | } |
| 4152 | } |
| 4153 | if (!err) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4154 | MethodDecl->setUsed(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4155 | |
| 4156 | CurContext = PreviousContext; |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4157 | } |
| 4158 | |
| 4159 | CXXMethodDecl * |
Anders Carlsson | b6cc91b | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 4160 | Sema::getAssignOperatorMethod(SourceLocation CurrentLocation, |
| 4161 | ParmVarDecl *ParmDecl, |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4162 | CXXRecordDecl *ClassDecl) { |
| 4163 | QualType LHSType = Context.getTypeDeclType(ClassDecl); |
| 4164 | QualType RHSType(LHSType); |
| 4165 | // If class's assignment operator argument is const/volatile qualified, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4166 | // look for operator = (const/volatile B&). Otherwise, look for |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4167 | // operator = (B&). |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4168 | RHSType = Context.getCVRQualifiedType(RHSType, |
| 4169 | ParmDecl->getType().getCVRQualifiers()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4170 | ExprOwningPtr<Expr> LHS(this, new (Context) DeclRefExpr(ParmDecl, |
Anders Carlsson | b6cc91b | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 4171 | LHSType, |
| 4172 | SourceLocation())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4173 | ExprOwningPtr<Expr> RHS(this, new (Context) DeclRefExpr(ParmDecl, |
Anders Carlsson | b6cc91b | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 4174 | RHSType, |
| 4175 | CurrentLocation)); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4176 | Expr *Args[2] = { &*LHS, &*RHS }; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4177 | OverloadCandidateSet CandidateSet(CurrentLocation); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4178 | AddMemberOperatorCandidates(clang::OO_Equal, SourceLocation(), Args, 2, |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4179 | CandidateSet); |
| 4180 | OverloadCandidateSet::iterator Best; |
Anders Carlsson | b6cc91b | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 4181 | if (BestViableFunction(CandidateSet, CurrentLocation, Best) == OR_Success) |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4182 | return cast<CXXMethodDecl>(Best->Function); |
| 4183 | assert(false && |
| 4184 | "getAssignOperatorMethod - copy assignment operator method not found"); |
| 4185 | return 0; |
| 4186 | } |
| 4187 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4188 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 4189 | CXXConstructorDecl *CopyConstructor, |
| 4190 | unsigned TypeQuals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | assert((CopyConstructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 4192 | CopyConstructor->isCopyConstructor(TypeQuals) && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4193 | !CopyConstructor->isUsed()) && |
| 4194 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4195 | |
Anders Carlsson | 63010a7 | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 4196 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4197 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4198 | |
| 4199 | DeclContext *PreviousContext = CurContext; |
| 4200 | CurContext = CopyConstructor; |
| 4201 | |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 4202 | // C++ [class.copy] p209 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4203 | // Before the implicitly-declared copy constructor for a class is |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4204 | // implicitly defined, all the implicitly-declared copy constructors |
| 4205 | // for its base class and its non-static data members shall have been |
| 4206 | // implicitly defined. |
| 4207 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 4208 | Base != ClassDecl->bases_end(); ++Base) { |
| 4209 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4210 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4211 | if (CXXConstructorDecl *BaseCopyCtor = |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4212 | BaseClassDecl->getCopyConstructor(Context, TypeQuals)) { |
| 4213 | CheckDirectMemberAccess(Base->getSourceRange().getBegin(), |
| 4214 | BaseCopyCtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4215 | PDiag(diag::err_access_copy_base) |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4216 | << Base->getType()); |
| 4217 | |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 4218 | MarkDeclarationReferenced(CurrentLocation, BaseCopyCtor); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4219 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4220 | } |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4221 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4222 | FieldEnd = ClassDecl->field_end(); |
| 4223 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4224 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 4225 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 4226 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4227 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4228 | CXXRecordDecl *FieldClassDecl |
| 4229 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4230 | if (CXXConstructorDecl *FieldCopyCtor = |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4231 | FieldClassDecl->getCopyConstructor(Context, TypeQuals)) { |
| 4232 | CheckDirectMemberAccess(Field->getLocation(), |
| 4233 | FieldCopyCtor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4234 | PDiag(diag::err_access_copy_field) |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4235 | << Field->getDeclName() << Field->getType()); |
| 4236 | |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 4237 | MarkDeclarationReferenced(CurrentLocation, FieldCopyCtor); |
John McCall | b020748 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4238 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4239 | } |
| 4240 | } |
| 4241 | CopyConstructor->setUsed(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4242 | |
| 4243 | CurContext = PreviousContext; |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 4246 | Sema::OwningExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 4247 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4248 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4249 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4250 | bool RequiresZeroInit, |
| 4251 | bool BaseInitialization) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 4252 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4253 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4254 | // C++0x [class.copy]p34: |
| 4255 | // When certain criteria are met, an implementation is allowed to |
| 4256 | // omit the copy/move construction of a class object, even if the |
| 4257 | // copy/move constructor and/or destructor for the object have |
| 4258 | // side effects. [...] |
| 4259 | // - when a temporary class object that has not been bound to a |
| 4260 | // reference (12.2) would be copied/moved to a class object |
| 4261 | // with the same cv-unqualified type, the copy/move operation |
| 4262 | // can be omitted by constructing the temporary object |
| 4263 | // directly into the target of the omitted copy/move |
| 4264 | if (Constructor->isCopyConstructor() && ExprArgs.size() >= 1) { |
| 4265 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
| 4266 | Elidable = SubExpr->isTemporaryObject() && |
| 4267 | Context.hasSameUnqualifiedType(SubExpr->getType(), |
| 4268 | Context.getTypeDeclType(Constructor->getParent())); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 4269 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4270 | |
| 4271 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4272 | Elidable, move(ExprArgs), RequiresZeroInit, |
| 4273 | BaseInitialization); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 4276 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 4277 | /// including handling of its default argument expressions. |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 4278 | Sema::OwningExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 4279 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 4280 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4281 | MultiExprArg ExprArgs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4282 | bool RequiresZeroInit, |
| 4283 | bool BaseInitialization) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 4284 | unsigned NumExprs = ExprArgs.size(); |
| 4285 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4286 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4287 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4288 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4289 | Constructor, Elidable, Exprs, NumExprs, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4290 | RequiresZeroInit, BaseInitialization)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4293 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 4294 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 4295 | MultiExprArg Exprs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4296 | OwningExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 4297 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 4298 | move(Exprs)); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 4299 | if (TempResult.isInvalid()) |
| 4300 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4301 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 4302 | Expr *Temp = TempResult.takeAs<Expr>(); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 4303 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 4304 | Temp = MaybeCreateCXXExprWithTemporaries(Temp); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4305 | VD->setInit(Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4306 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 4307 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 4310 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
| 4311 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Douglas Gregor | 333de06 | 2010-02-25 18:11:54 +0000 | [diff] [blame] | 4312 | if (!ClassDecl->isInvalidDecl() && !VD->isInvalidDecl() && |
| 4313 | !ClassDecl->hasTrivialDestructor()) { |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 4314 | CXXDestructorDecl *Destructor = ClassDecl->getDestructor(Context); |
| 4315 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4316 | CheckDestructorAccess(VD->getLocation(), Destructor, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4317 | PDiag(diag::err_access_dtor_var) |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4318 | << VD->getDeclName() |
| 4319 | << VD->getType()); |
John McCall | 4f9506a | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 4320 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4323 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4324 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 4325 | /// e.g: "int x(1);" |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4326 | void Sema::AddCXXDirectInitializerToDecl(DeclPtrTy Dcl, |
| 4327 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4328 | MultiExprArg Exprs, |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4329 | SourceLocation *CommaLocs, |
| 4330 | SourceLocation RParenLoc) { |
Daniel Dunbar | 5184626 | 2009-12-24 19:19:26 +0000 | [diff] [blame] | 4331 | assert(Exprs.size() != 0 && Exprs.get() && "missing expressions"); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4332 | Decl *RealDecl = Dcl.getAs<Decl>(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4333 | |
| 4334 | // If there is no declaration, there was an error parsing it. Just ignore |
| 4335 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4336 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4337 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4338 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4339 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 4340 | if (!VDecl) { |
| 4341 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 4342 | RealDecl->setInvalidDecl(); |
| 4343 | return; |
| 4344 | } |
| 4345 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 4346 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 4347 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4348 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 4349 | // |
| 4350 | // Clients that want to distinguish between the two forms, can check for |
| 4351 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 4352 | // A major benefit is that clients that don't particularly care about which |
| 4353 | // exactly form was it (like the CodeGen) can handle both cases without |
| 4354 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 4355 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4356 | // C++ 8.5p11: |
| 4357 | // The form of initialization (using parentheses or '=') is generally |
| 4358 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 4359 | // class type. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4360 | QualType DeclInitType = VDecl->getType(); |
| 4361 | if (const ArrayType *Array = Context.getAsArrayType(DeclInitType)) |
Fariborz Jahanian | 680a3f3 | 2009-10-28 19:04:36 +0000 | [diff] [blame] | 4362 | DeclInitType = Context.getBaseElementType(Array); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 4363 | |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 4364 | if (!VDecl->getType()->isDependentType() && |
| 4365 | RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 4366 | diag::err_typecheck_decl_incomplete_type)) { |
| 4367 | VDecl->setInvalidDecl(); |
| 4368 | return; |
| 4369 | } |
| 4370 | |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4371 | // The variable can not have an abstract class type. |
| 4372 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 4373 | diag::err_abstract_type_in_decl, |
| 4374 | AbstractVariableType)) |
| 4375 | VDecl->setInvalidDecl(); |
| 4376 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4377 | const VarDecl *Def; |
| 4378 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4379 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| 4380 | << VDecl->getDeclName(); |
| 4381 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4382 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 4383 | return; |
| 4384 | } |
Douglas Gregor | 4dffad6 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 4385 | |
| 4386 | // If either the declaration has a dependent type or if any of the |
| 4387 | // expressions is type-dependent, we represent the initialization |
| 4388 | // via a ParenListExpr for later use during template instantiation. |
| 4389 | if (VDecl->getType()->isDependentType() || |
| 4390 | Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { |
| 4391 | // Let clients know that initialization was done with a direct initializer. |
| 4392 | VDecl->setCXXDirectInitializer(true); |
| 4393 | |
| 4394 | // Store the initialization expressions as a ParenListExpr. |
| 4395 | unsigned NumExprs = Exprs.size(); |
| 4396 | VDecl->setInit(new (Context) ParenListExpr(Context, LParenLoc, |
| 4397 | (Expr **)Exprs.release(), |
| 4398 | NumExprs, RParenLoc)); |
| 4399 | return; |
| 4400 | } |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4401 | |
| 4402 | // Capture the variable that is being initialized and the style of |
| 4403 | // initialization. |
| 4404 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 4405 | |
| 4406 | // FIXME: Poor source location information. |
| 4407 | InitializationKind Kind |
| 4408 | = InitializationKind::CreateDirect(VDecl->getLocation(), |
| 4409 | LParenLoc, RParenLoc); |
| 4410 | |
| 4411 | InitializationSequence InitSeq(*this, Entity, Kind, |
| 4412 | (Expr**)Exprs.get(), Exprs.size()); |
| 4413 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(Exprs)); |
| 4414 | if (Result.isInvalid()) { |
| 4415 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4416 | return; |
| 4417 | } |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4418 | |
| 4419 | Result = MaybeCreateCXXExprWithTemporaries(move(Result)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4420 | VDecl->setInit(Result.takeAs<Expr>()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4421 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 4422 | |
John McCall | 68c6c9a | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 4423 | if (const RecordType *Record = VDecl->getType()->getAs<RecordType>()) |
| 4424 | FinalizeVarWithDestructor(VDecl, Record); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4425 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4426 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4427 | /// \brief Given a constructor and the set of arguments provided for the |
| 4428 | /// constructor, convert the arguments and add any required default arguments |
| 4429 | /// to form a proper call to this constructor. |
| 4430 | /// |
| 4431 | /// \returns true if an error occurred, false otherwise. |
| 4432 | bool |
| 4433 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 4434 | MultiExprArg ArgsPtr, |
| 4435 | SourceLocation Loc, |
| 4436 | ASTOwningVector<&ActionBase::DeleteExpr> &ConvertedArgs) { |
| 4437 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 4438 | unsigned NumArgs = ArgsPtr.size(); |
| 4439 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 4440 | |
| 4441 | const FunctionProtoType *Proto |
| 4442 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 4443 | assert(Proto && "Constructor without a prototype?"); |
| 4444 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4445 | |
| 4446 | // 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] | 4447 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4448 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4449 | else |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4450 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4451 | |
| 4452 | VariadicCallType CallType = |
| 4453 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 4454 | llvm::SmallVector<Expr *, 8> AllArgs; |
| 4455 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 4456 | Proto, 0, Args, NumArgs, AllArgs, |
| 4457 | CallType); |
| 4458 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 4459 | ConvertedArgs.push_back(AllArgs[i]); |
| 4460 | return Invalid; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4461 | } |
| 4462 | |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4463 | static inline bool |
| 4464 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 4465 | const FunctionDecl *FnDecl) { |
| 4466 | const DeclContext *DC = FnDecl->getDeclContext()->getLookupContext(); |
| 4467 | if (isa<NamespaceDecl>(DC)) { |
| 4468 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4469 | diag::err_operator_new_delete_declared_in_namespace) |
| 4470 | << FnDecl->getDeclName(); |
| 4471 | } |
| 4472 | |
| 4473 | if (isa<TranslationUnitDecl>(DC) && |
| 4474 | FnDecl->getStorageClass() == FunctionDecl::Static) { |
| 4475 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4476 | diag::err_operator_new_delete_declared_static) |
| 4477 | << FnDecl->getDeclName(); |
| 4478 | } |
| 4479 | |
Anders Carlsson | fcfdb2b | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 4480 | return false; |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4483 | static inline bool |
| 4484 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 4485 | CanQualType ExpectedResultType, |
| 4486 | CanQualType ExpectedFirstParamType, |
| 4487 | unsigned DependentParamTypeDiag, |
| 4488 | unsigned InvalidParamTypeDiag) { |
| 4489 | QualType ResultType = |
| 4490 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 4491 | |
| 4492 | // Check that the result type is not dependent. |
| 4493 | if (ResultType->isDependentType()) |
| 4494 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4495 | diag::err_operator_new_delete_dependent_result_type) |
| 4496 | << FnDecl->getDeclName() << ExpectedResultType; |
| 4497 | |
| 4498 | // Check that the result type is what we expect. |
| 4499 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 4500 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4501 | diag::err_operator_new_delete_invalid_result_type) |
| 4502 | << FnDecl->getDeclName() << ExpectedResultType; |
| 4503 | |
| 4504 | // A function template must have at least 2 parameters. |
| 4505 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 4506 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4507 | diag::err_operator_new_delete_template_too_few_parameters) |
| 4508 | << FnDecl->getDeclName(); |
| 4509 | |
| 4510 | // The function decl must have at least 1 parameter. |
| 4511 | if (FnDecl->getNumParams() == 0) |
| 4512 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4513 | diag::err_operator_new_delete_too_few_parameters) |
| 4514 | << FnDecl->getDeclName(); |
| 4515 | |
| 4516 | // Check the the first parameter type is not dependent. |
| 4517 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 4518 | if (FirstParamType->isDependentType()) |
| 4519 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 4520 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 4521 | |
| 4522 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 4523 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4524 | ExpectedFirstParamType) |
| 4525 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 4526 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 4527 | |
| 4528 | return false; |
| 4529 | } |
| 4530 | |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4531 | static bool |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4532 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4533 | // C++ [basic.stc.dynamic.allocation]p1: |
| 4534 | // A program is ill-formed if an allocation function is declared in a |
| 4535 | // namespace scope other than global scope or declared static in global |
| 4536 | // scope. |
| 4537 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 4538 | return true; |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4539 | |
| 4540 | CanQualType SizeTy = |
| 4541 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 4542 | |
| 4543 | // C++ [basic.stc.dynamic.allocation]p1: |
| 4544 | // The return type shall be void*. The first parameter shall have type |
| 4545 | // std::size_t. |
| 4546 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 4547 | SizeTy, |
| 4548 | diag::err_operator_new_dependent_param_type, |
| 4549 | diag::err_operator_new_param_type)) |
| 4550 | return true; |
| 4551 | |
| 4552 | // C++ [basic.stc.dynamic.allocation]p1: |
| 4553 | // The first parameter shall not have an associated default argument. |
| 4554 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4555 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4556 | diag::err_operator_new_default_arg) |
| 4557 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 4558 | |
| 4559 | return false; |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | static bool |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4563 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 4564 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 4565 | // A program is ill-formed if deallocation functions are declared in a |
| 4566 | // namespace scope other than global scope or declared static in global |
| 4567 | // scope. |
Anders Carlsson | 20d45d2 | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4568 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 4569 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4570 | |
| 4571 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 4572 | // Each deallocation function shall return void and its first parameter |
| 4573 | // shall be void*. |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4574 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 4575 | SemaRef.Context.VoidPtrTy, |
| 4576 | diag::err_operator_delete_dependent_param_type, |
| 4577 | diag::err_operator_delete_param_type)) |
| 4578 | return true; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4579 | |
Anders Carlsson | 46991d6 | 2009-12-12 00:16:02 +0000 | [diff] [blame] | 4580 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 4581 | if (FirstParamType->isDependentType()) |
| 4582 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4583 | diag::err_operator_delete_dependent_param_type) |
| 4584 | << FnDecl->getDeclName() << SemaRef.Context.VoidPtrTy; |
| 4585 | |
| 4586 | if (SemaRef.Context.getCanonicalType(FirstParamType) != |
| 4587 | SemaRef.Context.VoidPtrTy) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4588 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4589 | diag::err_operator_delete_param_type) |
| 4590 | << FnDecl->getDeclName() << SemaRef.Context.VoidPtrTy; |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4591 | |
| 4592 | return false; |
| 4593 | } |
| 4594 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4595 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 4596 | /// of this overloaded operator is well-formed. If so, returns false; |
| 4597 | /// otherwise, emits appropriate diagnostics and returns true. |
| 4598 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4599 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4600 | "Expected an overloaded operator declaration"); |
| 4601 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4602 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 4603 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4604 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4605 | // The allocation and deallocation functions, operator new, |
| 4606 | // operator new[], operator delete and operator delete[], are |
| 4607 | // described completely in 3.7.3. The attributes and restrictions |
| 4608 | // found in the rest of this subclause do not apply to them unless |
| 4609 | // explicitly stated in 3.7.3. |
Anders Carlsson | 1152c39 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 4610 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 9d59ecb | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4611 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 4612 | |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4613 | if (Op == OO_New || Op == OO_Array_New) |
| 4614 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4615 | |
| 4616 | // C++ [over.oper]p6: |
| 4617 | // An operator function shall either be a non-static member |
| 4618 | // function or be a non-member function and have at least one |
| 4619 | // parameter whose type is a class, a reference to a class, an |
| 4620 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4621 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4622 | if (MethodDecl->isStatic()) |
| 4623 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4624 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4625 | } else { |
| 4626 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4627 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 4628 | ParamEnd = FnDecl->param_end(); |
| 4629 | Param != ParamEnd; ++Param) { |
| 4630 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 4631 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 4632 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4633 | ClassOrEnumParam = true; |
| 4634 | break; |
| 4635 | } |
| 4636 | } |
| 4637 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4638 | if (!ClassOrEnumParam) |
| 4639 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4640 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4641 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | // C++ [over.oper]p8: |
| 4645 | // An operator function cannot have default arguments (8.3.6), |
| 4646 | // except where explicitly stated below. |
| 4647 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4648 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4649 | // (C++ [over.call]p1). |
| 4650 | if (Op != OO_Call) { |
| 4651 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 4652 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4653 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4654 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4655 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 156c78e | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4656 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4657 | } |
| 4658 | } |
| 4659 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4660 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 4661 | { false, false, false } |
| 4662 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 4663 | , { Unary, Binary, MemberOnly } |
| 4664 | #include "clang/Basic/OperatorKinds.def" |
| 4665 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4666 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4667 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 4668 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 4669 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4670 | |
| 4671 | // C++ [over.oper]p8: |
| 4672 | // [...] Operator functions cannot have more or fewer parameters |
| 4673 | // than the number required for the corresponding operator, as |
| 4674 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4675 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4676 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4677 | if (Op != OO_Call && |
| 4678 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 4679 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 4680 | (NumParams < 1) || (NumParams > 2))) { |
| 4681 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4682 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4683 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4684 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4685 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4686 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4687 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 4688 | assert(CanBeBinaryOperator && |
| 4689 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4690 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4691 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4692 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4693 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4694 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4695 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4696 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4697 | // Overloaded operators other than operator() cannot be variadic. |
| 4698 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4699 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4700 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4701 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4705 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 4706 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4707 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4708 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
| 4711 | // C++ [over.inc]p1: |
| 4712 | // The user-defined function called operator++ implements the |
| 4713 | // prefix and postfix ++ operator. If this function is a member |
| 4714 | // function with no parameters, or a non-member function with one |
| 4715 | // parameter of class or enumeration type, it defines the prefix |
| 4716 | // increment operator ++ for objects of that type. If the function |
| 4717 | // is a member function with one parameter (which shall be of type |
| 4718 | // int) or a non-member function with two parameters (the second |
| 4719 | // of which shall be of type int), it defines the postfix |
| 4720 | // increment operator ++ for objects of that type. |
| 4721 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 4722 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 4723 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4724 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4725 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 4726 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 4727 | if (!ParamIsInt) |
| 4728 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4729 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4730 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4733 | // Notify the class if it got an assignment operator. |
| 4734 | if (Op == OO_Equal) { |
| 4735 | // Would have returned earlier otherwise. |
| 4736 | assert(isa<CXXMethodDecl>(FnDecl) && |
| 4737 | "Overloaded = not member, but not filtered."); |
| 4738 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 4739 | Method->getParent()->addedAssignmentOperator(Context, Method); |
| 4740 | } |
| 4741 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4742 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4743 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 4744 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 4745 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 4746 | /// of this literal operator function is well-formed. If so, returns |
| 4747 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 4748 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 4749 | DeclContext *DC = FnDecl->getDeclContext(); |
| 4750 | Decl::Kind Kind = DC->getDeclKind(); |
| 4751 | if (Kind != Decl::TranslationUnit && Kind != Decl::Namespace && |
| 4752 | Kind != Decl::LinkageSpec) { |
| 4753 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 4754 | << FnDecl->getDeclName(); |
| 4755 | return true; |
| 4756 | } |
| 4757 | |
| 4758 | bool Valid = false; |
| 4759 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 4760 | // template <char...> type operator "" name() is the only valid template |
| 4761 | // signature, and the only valid signature with no parameters. |
| 4762 | if (FnDecl->param_size() == 0) { |
| 4763 | if (FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate()) { |
| 4764 | // Must have only one template parameter |
| 4765 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 4766 | if (Params->size() == 1) { |
| 4767 | NonTypeTemplateParmDecl *PmDecl = |
| 4768 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 4769 | |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 4770 | // The template parameter must be a char parameter pack. |
| 4771 | // FIXME: This test will always fail because non-type parameter packs |
| 4772 | // have not been implemented. |
| 4773 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 4774 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 4775 | Valid = true; |
| 4776 | } |
| 4777 | } |
| 4778 | } else { |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 4779 | // Check the first parameter |
Sean Hunt | 216c278 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 4780 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 4781 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 4782 | QualType T = (*Param)->getType(); |
| 4783 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 4784 | // unsigned long long int, long double, and any character type are allowed |
| 4785 | // as the only parameters. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 4786 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 4787 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 4788 | Context.hasSameType(T, Context.CharTy) || |
| 4789 | Context.hasSameType(T, Context.WCharTy) || |
| 4790 | Context.hasSameType(T, Context.Char16Ty) || |
| 4791 | Context.hasSameType(T, Context.Char32Ty)) { |
| 4792 | if (++Param == FnDecl->param_end()) |
| 4793 | Valid = true; |
| 4794 | goto FinishedParams; |
| 4795 | } |
| 4796 | |
Sean Hunt | 30019c0 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 4797 | // Otherwise it must be a pointer to const; let's strip those qualifiers. |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 4798 | const PointerType *PT = T->getAs<PointerType>(); |
| 4799 | if (!PT) |
| 4800 | goto FinishedParams; |
| 4801 | T = PT->getPointeeType(); |
| 4802 | if (!T.isConstQualified()) |
| 4803 | goto FinishedParams; |
| 4804 | T = T.getUnqualifiedType(); |
| 4805 | |
| 4806 | // Move on to the second parameter; |
| 4807 | ++Param; |
| 4808 | |
| 4809 | // If there is no second parameter, the first must be a const char * |
| 4810 | if (Param == FnDecl->param_end()) { |
| 4811 | if (Context.hasSameType(T, Context.CharTy)) |
| 4812 | Valid = true; |
| 4813 | goto FinishedParams; |
| 4814 | } |
| 4815 | |
| 4816 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 4817 | // are allowed as the first parameter to a two-parameter function |
| 4818 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 4819 | Context.hasSameType(T, Context.WCharTy) || |
| 4820 | Context.hasSameType(T, Context.Char16Ty) || |
| 4821 | Context.hasSameType(T, Context.Char32Ty))) |
| 4822 | goto FinishedParams; |
| 4823 | |
| 4824 | // The second and final parameter must be an std::size_t |
| 4825 | T = (*Param)->getType().getUnqualifiedType(); |
| 4826 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 4827 | ++Param == FnDecl->param_end()) |
| 4828 | Valid = true; |
| 4829 | } |
| 4830 | |
| 4831 | // FIXME: This diagnostic is absolutely terrible. |
| 4832 | FinishedParams: |
| 4833 | if (!Valid) { |
| 4834 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 4835 | << FnDecl->getDeclName(); |
| 4836 | return true; |
| 4837 | } |
| 4838 | |
| 4839 | return false; |
| 4840 | } |
| 4841 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4842 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 4843 | /// linkage specification, including the language and (if present) |
| 4844 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 4845 | /// the location of the language string literal, which is provided |
| 4846 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 4847 | /// the '{' brace. Otherwise, this linkage specification does not |
| 4848 | /// have any braces. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4849 | Sema::DeclPtrTy Sema::ActOnStartLinkageSpecification(Scope *S, |
| 4850 | SourceLocation ExternLoc, |
| 4851 | SourceLocation LangLoc, |
| 4852 | const char *Lang, |
| 4853 | unsigned StrSize, |
| 4854 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4855 | LinkageSpecDecl::LanguageIDs Language; |
| 4856 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 4857 | Language = LinkageSpecDecl::lang_c; |
| 4858 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 4859 | Language = LinkageSpecDecl::lang_cxx; |
| 4860 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4861 | Diag(LangLoc, diag::err_bad_language); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4862 | return DeclPtrTy(); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4863 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4864 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4865 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4866 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4867 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4868 | LangLoc, Language, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4869 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4870 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4871 | PushDeclContext(S, D); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4872 | return DeclPtrTy::make(D); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4873 | } |
| 4874 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4875 | /// ActOnFinishLinkageSpecification - Completely the definition of |
| 4876 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 4877 | /// valid, it's the position of the closing '}' brace in a linkage |
| 4878 | /// specification that uses braces. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4879 | Sema::DeclPtrTy Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 4880 | DeclPtrTy LinkageSpec, |
| 4881 | SourceLocation RBraceLoc) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4882 | if (LinkageSpec) |
| 4883 | PopDeclContext(); |
| 4884 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4887 | /// \brief Perform semantic analysis for the variable declaration that |
| 4888 | /// occurs within a C++ catch clause, returning the newly-created |
| 4889 | /// variable. |
| 4890 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, QualType ExDeclType, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4891 | TypeSourceInfo *TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4892 | IdentifierInfo *Name, |
| 4893 | SourceLocation Loc, |
| 4894 | SourceRange Range) { |
| 4895 | bool Invalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4896 | |
| 4897 | // Arrays and functions decay. |
| 4898 | if (ExDeclType->isArrayType()) |
| 4899 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 4900 | else if (ExDeclType->isFunctionType()) |
| 4901 | ExDeclType = Context.getPointerType(ExDeclType); |
| 4902 | |
| 4903 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 4904 | // The exception-declaration shall not denote a pointer or reference to an |
| 4905 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4906 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4907 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4908 | Diag(Loc, diag::err_catch_rvalue_ref) << Range; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4909 | Invalid = true; |
| 4910 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4911 | |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 4912 | // GCC allows catching pointers and references to incomplete types |
| 4913 | // as an extension; so do we, but we warn by default. |
| 4914 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4915 | QualType BaseType = ExDeclType; |
| 4916 | 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] | 4917 | unsigned DK = diag::err_catch_incomplete; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 4918 | bool IncompleteCatchIsInvalid = true; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4919 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4920 | BaseType = Ptr->getPointeeType(); |
| 4921 | Mode = 1; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 4922 | DK = diag::ext_catch_incomplete_ptr; |
| 4923 | IncompleteCatchIsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4924 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4925 | // 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] | 4926 | BaseType = Ref->getPointeeType(); |
| 4927 | Mode = 2; |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 4928 | DK = diag::ext_catch_incomplete_ref; |
| 4929 | IncompleteCatchIsInvalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4930 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4931 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | a276291 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 4932 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK) && |
| 4933 | IncompleteCatchIsInvalid) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4934 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4935 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4936 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4937 | RequireNonAbstractType(Loc, ExDeclType, |
| 4938 | diag::err_abstract_type_in_decl, |
| 4939 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 4940 | Invalid = true; |
| 4941 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4942 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4943 | Name, ExDeclType, TInfo, VarDecl::None, |
| 4944 | VarDecl::None); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4945 | |
Douglas Gregor | 6d18289 | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 4946 | if (!Invalid) { |
| 4947 | if (const RecordType *RecordTy = ExDeclType->getAs<RecordType>()) { |
| 4948 | // C++ [except.handle]p16: |
| 4949 | // The object declared in an exception-declaration or, if the |
| 4950 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 4951 | // copy-initialized (8.5) from the exception object. [...] |
| 4952 | // The object is destroyed when the handler exits, after the destruction |
| 4953 | // of any automatic objects initialized within the handler. |
| 4954 | // |
| 4955 | // We just pretend to initialize the object with itself, then make sure |
| 4956 | // it can be destroyed later. |
| 4957 | InitializedEntity Entity = InitializedEntity::InitializeVariable(ExDecl); |
| 4958 | Expr *ExDeclRef = DeclRefExpr::Create(Context, 0, SourceRange(), ExDecl, |
| 4959 | Loc, ExDeclType, 0); |
| 4960 | InitializationKind Kind = InitializationKind::CreateCopy(Loc, |
| 4961 | SourceLocation()); |
| 4962 | InitializationSequence InitSeq(*this, Entity, Kind, &ExDeclRef, 1); |
| 4963 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
| 4964 | MultiExprArg(*this, (void**)&ExDeclRef, 1)); |
| 4965 | if (Result.isInvalid()) |
| 4966 | Invalid = true; |
| 4967 | else |
| 4968 | FinalizeVarWithDestructor(ExDecl, RecordTy); |
| 4969 | } |
| 4970 | } |
| 4971 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4972 | if (Invalid) |
| 4973 | ExDecl->setInvalidDecl(); |
| 4974 | |
| 4975 | return ExDecl; |
| 4976 | } |
| 4977 | |
| 4978 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 4979 | /// handler. |
| 4980 | Sema::DeclPtrTy Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4981 | TypeSourceInfo *TInfo = 0; |
| 4982 | QualType ExDeclType = GetTypeForDeclarator(D, S, &TInfo); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4983 | |
| 4984 | bool Invalid = D.isInvalidType(); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4985 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4986 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 4987 | LookupOrdinaryName, |
| 4988 | ForRedeclaration)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4989 | // The scope should be freshly made just for us. There is just no way |
| 4990 | // it contains any previous declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4991 | assert(!S->isDeclScope(DeclPtrTy::make(PrevDecl))); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4992 | if (PrevDecl->isTemplateParameter()) { |
| 4993 | // Maybe we will complain about the shadowed template parameter. |
| 4994 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4995 | } |
| 4996 | } |
| 4997 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4998 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4999 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 5000 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5001 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5002 | } |
| 5003 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5004 | VarDecl *ExDecl = BuildExceptionDeclaration(S, ExDeclType, TInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5005 | D.getIdentifier(), |
| 5006 | D.getIdentifierLoc(), |
| 5007 | D.getDeclSpec().getSourceRange()); |
| 5008 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5009 | if (Invalid) |
| 5010 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5011 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5012 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5013 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5014 | PushOnScopeChains(ExDecl, S); |
| 5015 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5016 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5017 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5018 | ProcessDeclAttributes(S, ExDecl, D); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5019 | return DeclPtrTy::make(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5020 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5021 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5022 | Sema::DeclPtrTy Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5023 | ExprArg assertexpr, |
| 5024 | ExprArg assertmessageexpr) { |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5025 | Expr *AssertExpr = (Expr *)assertexpr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5026 | StringLiteral *AssertMessage = |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5027 | cast<StringLiteral>((Expr *)assertmessageexpr.get()); |
| 5028 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5029 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 5030 | llvm::APSInt Value(32); |
| 5031 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 5032 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 5033 | AssertExpr->getSourceRange(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5034 | return DeclPtrTy(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5035 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5036 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5037 | if (Value == 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5038 | Diag(AssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | 8d04258 | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 5039 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5040 | } |
| 5041 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5042 | |
Anders Carlsson | 77d8142 | 2009-03-15 17:35:16 +0000 | [diff] [blame] | 5043 | assertexpr.release(); |
| 5044 | assertmessageexpr.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5045 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5046 | AssertExpr, AssertMessage); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5047 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5048 | CurContext->addDecl(Decl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5049 | return DeclPtrTy::make(Decl); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5050 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5051 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5052 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 5053 | /// |
| 5054 | /// \returns A friend declaration that. |
| 5055 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation FriendLoc, |
| 5056 | TypeSourceInfo *TSInfo) { |
| 5057 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 5058 | |
| 5059 | QualType T = TSInfo->getType(); |
| 5060 | SourceRange TypeRange = TSInfo->getTypeLoc().getSourceRange(); |
| 5061 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 5062 | if (!getLangOptions().CPlusPlus0x) { |
| 5063 | // C++03 [class.friend]p2: |
| 5064 | // An elaborated-type-specifier shall be used in a friend declaration |
| 5065 | // for a class.* |
| 5066 | // |
| 5067 | // * The class-key of the elaborated-type-specifier is required. |
| 5068 | if (!ActiveTemplateInstantiations.empty()) { |
| 5069 | // Do not complain about the form of friend template types during |
| 5070 | // template instantiation; we will already have complained when the |
| 5071 | // template was declared. |
| 5072 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 5073 | // If we evaluated the type to a record type, suggest putting |
| 5074 | // a tag in front. |
| 5075 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 5076 | RecordDecl *RD = RT->getDecl(); |
| 5077 | |
| 5078 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 5079 | |
| 5080 | Diag(TypeRange.getBegin(), diag::ext_unelaborated_friend_type) |
| 5081 | << (unsigned) RD->getTagKind() |
| 5082 | << T |
| 5083 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 5084 | InsertionText); |
| 5085 | } else { |
| 5086 | Diag(FriendLoc, diag::ext_nonclass_type_friend) |
| 5087 | << T |
| 5088 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
| 5089 | } |
| 5090 | } else if (T->getAs<EnumType>()) { |
| 5091 | Diag(FriendLoc, diag::ext_enum_friend) |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5092 | << T |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5093 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5094 | } |
| 5095 | } |
| 5096 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 5097 | // C++0x [class.friend]p3: |
| 5098 | // If the type specifier in a friend declaration designates a (possibly |
| 5099 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 5100 | // the friend declaration is ignored. |
| 5101 | |
| 5102 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 5103 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5104 | |
| 5105 | return FriendDecl::Create(Context, CurContext, FriendLoc, TSInfo, FriendLoc); |
| 5106 | } |
| 5107 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5108 | /// Handle a friend type declaration. This works in tandem with |
| 5109 | /// ActOnTag. |
| 5110 | /// |
| 5111 | /// Notes on friend class templates: |
| 5112 | /// |
| 5113 | /// We generally treat friend class declarations as if they were |
| 5114 | /// declaring a class. So, for example, the elaborated type specifier |
| 5115 | /// in a friend declaration is required to obey the restrictions of a |
| 5116 | /// class-head (i.e. no typedefs in the scope chain), template |
| 5117 | /// parameters are required to match up with simple template-ids, &c. |
| 5118 | /// However, unlike when declaring a template specialization, it's |
| 5119 | /// okay to refer to a template specialization without an empty |
| 5120 | /// template parameter declaration, e.g. |
| 5121 | /// friend class A<T>::B<unsigned>; |
| 5122 | /// We permit this as a special case; if there are any template |
| 5123 | /// parameters present at all, require proper matching, i.e. |
| 5124 | /// template <> template <class T> friend class A<int>::B; |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 5125 | Sema::DeclPtrTy Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5126 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5127 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5128 | |
| 5129 | assert(DS.isFriendSpecified()); |
| 5130 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 5131 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5132 | // Try to convert the decl specifier to a type. This works for |
| 5133 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 5134 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 5135 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5136 | TypeSourceInfo *TSI; |
| 5137 | QualType T = GetTypeForDeclarator(TheDeclarator, S, &TSI); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 5138 | if (TheDeclarator.isInvalidType()) |
| 5139 | return DeclPtrTy(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5140 | |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5141 | if (!TSI) |
| 5142 | TSI = Context.getTrivialTypeSourceInfo(T, DS.getSourceRange().getBegin()); |
| 5143 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5144 | // This is definitely an error in C++98. It's probably meant to |
| 5145 | // be forbidden in C++0x, too, but the specification is just |
| 5146 | // poorly written. |
| 5147 | // |
| 5148 | // The problem is with declarations like the following: |
| 5149 | // template <T> friend A<T>::foo; |
| 5150 | // where deciding whether a class C is a friend or not now hinges |
| 5151 | // on whether there exists an instantiation of A that causes |
| 5152 | // 'foo' to equal C. There are restrictions on class-heads |
| 5153 | // (which we declare (by fiat) elaborated friend declarations to |
| 5154 | // be) that makes this tractable. |
| 5155 | // |
| 5156 | // FIXME: handle "template <> friend class A<T>;", which |
| 5157 | // is possibly well-formed? Who even knows? |
Douglas Gregor | 4033642 | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 5158 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5159 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 5160 | << DS.getSourceRange(); |
| 5161 | return DeclPtrTy(); |
| 5162 | } |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5163 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5164 | // C++98 [class.friend]p1: A friend of a class is a function |
| 5165 | // or class that is not a member of the class . . . |
John McCall | a236a55 | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 5166 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 5167 | // deadline. It's also a very silly restriction that seriously |
| 5168 | // affects inner classes and which nobody else seems to implement; |
| 5169 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5170 | // |
| 5171 | // But note that we could warn about it: it's always useless to |
| 5172 | // friend one of your own members (it's not, however, worthless to |
| 5173 | // friend a member of an arbitrary specialization of your template). |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5174 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5175 | Decl *D; |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5176 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5177 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5178 | NumTempParamLists, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5179 | (TemplateParameterList**) TempParams.release(), |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5180 | TSI, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5181 | DS.getFriendSpecLoc()); |
| 5182 | else |
Douglas Gregor | 1d86935 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5183 | D = CheckFriendTypeDecl(DS.getFriendSpecLoc(), TSI); |
| 5184 | |
| 5185 | if (!D) |
| 5186 | return DeclPtrTy(); |
| 5187 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5188 | D->setAccess(AS_public); |
| 5189 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5190 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5191 | return DeclPtrTy::make(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5192 | } |
| 5193 | |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 5194 | Sema::DeclPtrTy |
| 5195 | Sema::ActOnFriendFunctionDecl(Scope *S, |
| 5196 | Declarator &D, |
| 5197 | bool IsDefinition, |
| 5198 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5199 | const DeclSpec &DS = D.getDeclSpec(); |
| 5200 | |
| 5201 | assert(DS.isFriendSpecified()); |
| 5202 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 5203 | |
| 5204 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5205 | TypeSourceInfo *TInfo = 0; |
| 5206 | QualType T = GetTypeForDeclarator(D, S, &TInfo); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5207 | |
| 5208 | // C++ [class.friend]p1 |
| 5209 | // A friend of a class is a function or class.... |
| 5210 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5211 | // It *doesn't* see through dependent types, which is correct |
| 5212 | // according to [temp.arg.type]p3: |
| 5213 | // If a declaration acquires a function type through a |
| 5214 | // type dependent on a template-parameter and this causes |
| 5215 | // a declaration that does not use the syntactic form of a |
| 5216 | // function declarator to have a function type, the program |
| 5217 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5218 | if (!T->isFunctionType()) { |
| 5219 | Diag(Loc, diag::err_unexpected_friend); |
| 5220 | |
| 5221 | // It might be worthwhile to try to recover by creating an |
| 5222 | // appropriate declaration. |
| 5223 | return DeclPtrTy(); |
| 5224 | } |
| 5225 | |
| 5226 | // C++ [namespace.memdef]p3 |
| 5227 | // - If a friend declaration in a non-local class first declares a |
| 5228 | // class or function, the friend class or function is a member |
| 5229 | // of the innermost enclosing namespace. |
| 5230 | // - The name of the friend is not found by simple name lookup |
| 5231 | // until a matching declaration is provided in that namespace |
| 5232 | // scope (either before or after the class declaration granting |
| 5233 | // friendship). |
| 5234 | // - If a friend function is called, its name may be found by the |
| 5235 | // name lookup that considers functions from namespaces and |
| 5236 | // classes associated with the types of the function arguments. |
| 5237 | // - When looking for a prior declaration of a class or a function |
| 5238 | // declared as a friend, scopes outside the innermost enclosing |
| 5239 | // namespace scope are not considered. |
| 5240 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5241 | CXXScopeSpec &ScopeQual = D.getCXXScopeSpec(); |
| 5242 | DeclarationName Name = GetNameForDeclarator(D); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5243 | assert(Name); |
| 5244 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5245 | // The context we found the declaration in, or in which we should |
| 5246 | // create the declaration. |
| 5247 | DeclContext *DC; |
| 5248 | |
| 5249 | // FIXME: handle local classes |
| 5250 | |
| 5251 | // Recover from invalid scope qualifiers as if they just weren't there. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5252 | LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName, |
| 5253 | ForRedeclaration); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5254 | if (!ScopeQual.isInvalid() && ScopeQual.isSet()) { |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5255 | // FIXME: RequireCompleteDeclContext |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5256 | DC = computeDeclContext(ScopeQual); |
| 5257 | |
| 5258 | // FIXME: handle dependent contexts |
| 5259 | if (!DC) return DeclPtrTy(); |
| 5260 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5261 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5262 | |
| 5263 | // If searching in that context implicitly found a declaration in |
| 5264 | // a different context, treat it like it wasn't found at all. |
| 5265 | // TODO: better diagnostics for this case. Suggesting the right |
| 5266 | // qualified scope would be nice... |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5267 | // FIXME: getRepresentativeDecl() is not right here at all |
| 5268 | if (Previous.empty() || |
| 5269 | !Previous.getRepresentativeDecl()->getDeclContext()->Equals(DC)) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5270 | D.setInvalidType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5271 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 5272 | return DeclPtrTy(); |
| 5273 | } |
| 5274 | |
| 5275 | // C++ [class.friend]p1: A friend of a class is a function or |
| 5276 | // class that is not a member of the class . . . |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5277 | if (DC->Equals(CurContext)) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5278 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 5279 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5280 | // Otherwise walk out to the nearest namespace scope looking for matches. |
| 5281 | } else { |
| 5282 | // TODO: handle local class contexts. |
| 5283 | |
| 5284 | DC = CurContext; |
| 5285 | while (true) { |
| 5286 | // Skip class contexts. If someone can cite chapter and verse |
| 5287 | // for this behavior, that would be nice --- it's what GCC and |
| 5288 | // EDG do, and it seems like a reasonable intent, but the spec |
| 5289 | // really only says that checks for unqualified existing |
| 5290 | // declarations should stop at the nearest enclosing namespace, |
| 5291 | // not that they should only consider the nearest enclosing |
| 5292 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5293 | while (DC->isRecord()) |
| 5294 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5295 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5296 | LookupQualifiedName(Previous, DC); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5297 | |
| 5298 | // TODO: decide what we think about using declarations. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5299 | if (!Previous.empty()) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5300 | break; |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5301 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5302 | if (DC->isFileContext()) break; |
| 5303 | DC = DC->getParent(); |
| 5304 | } |
| 5305 | |
| 5306 | // C++ [class.friend]p1: A friend of a class is a function or |
| 5307 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 5308 | // C++0x changes this for both friend types and functions. |
| 5309 | // Most C++ 98 compilers do seem to give an error here, so |
| 5310 | // we do, too. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5311 | if (!Previous.empty() && DC->Equals(CurContext) |
| 5312 | && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5313 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 5314 | } |
| 5315 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5316 | if (DC->isFileContext()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5317 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5318 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 5319 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 5320 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5321 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5322 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 5323 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5324 | return DeclPtrTy(); |
| 5325 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5326 | } |
| 5327 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5328 | bool Redeclaration = false; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5329 | NamedDecl *ND = ActOnFunctionDeclarator(S, D, DC, T, TInfo, Previous, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5330 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 5331 | IsDefinition, |
| 5332 | Redeclaration); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5333 | if (!ND) return DeclPtrTy(); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5334 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5335 | assert(ND->getDeclContext() == DC); |
| 5336 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 5337 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5338 | // Add the function declaration to the appropriate lookup tables, |
| 5339 | // adjusting the redeclarations list as necessary. We don't |
| 5340 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5341 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5342 | // Also update the scope-based lookup if the target context's |
| 5343 | // lookup context is in lexical scope. |
| 5344 | if (!CurContext->isDependentContext()) { |
| 5345 | DC = DC->getLookupContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5346 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5347 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5348 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5349 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5350 | |
| 5351 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5352 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5353 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 5354 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5355 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5356 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5357 | return DeclPtrTy::make(ND); |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 5358 | } |
| 5359 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5360 | void Sema::SetDeclDeleted(DeclPtrTy dcl, SourceLocation DelLoc) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 5361 | AdjustDeclIfTemplate(dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5362 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5363 | Decl *Dcl = dcl.getAs<Decl>(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5364 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 5365 | if (!Fn) { |
| 5366 | Diag(DelLoc, diag::err_deleted_non_function); |
| 5367 | return; |
| 5368 | } |
| 5369 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 5370 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 5371 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 5372 | // If the declaration wasn't the first, we delete the function anyway for |
| 5373 | // recovery. |
| 5374 | } |
| 5375 | Fn->setDeleted(); |
| 5376 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 5377 | |
| 5378 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 5379 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 5380 | ++CI) { |
| 5381 | Stmt *SubStmt = *CI; |
| 5382 | if (!SubStmt) |
| 5383 | continue; |
| 5384 | if (isa<ReturnStmt>(SubStmt)) |
| 5385 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 5386 | diag::err_return_in_constructor_handler); |
| 5387 | if (!isa<Expr>(SubStmt)) |
| 5388 | SearchForReturnInStmt(Self, SubStmt); |
| 5389 | } |
| 5390 | } |
| 5391 | |
| 5392 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 5393 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 5394 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 5395 | SearchForReturnInStmt(*this, Handler); |
| 5396 | } |
| 5397 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5398 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5399 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5400 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5401 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 5402 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5403 | |
Chandler Carruth | 7385779 | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 5404 | if (Context.hasSameType(NewTy, OldTy) || |
| 5405 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5406 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5407 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5408 | // Check if the return types are covariant |
| 5409 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5410 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5411 | /// Both types must be pointers or references to classes. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 5412 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 5413 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5414 | NewClassTy = NewPT->getPointeeType(); |
| 5415 | OldClassTy = OldPT->getPointeeType(); |
| 5416 | } |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 5417 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 5418 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 5419 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 5420 | NewClassTy = NewRT->getPointeeType(); |
| 5421 | OldClassTy = OldRT->getPointeeType(); |
| 5422 | } |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5423 | } |
| 5424 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5425 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5426 | // The return types aren't either both pointers or references to a class type. |
| 5427 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5428 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5429 | diag::err_different_return_type_for_overriding_virtual_function) |
| 5430 | << New->getDeclName() << NewTy << OldTy; |
| 5431 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5432 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5433 | return true; |
| 5434 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5435 | |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 5436 | // C++ [class.virtual]p6: |
| 5437 | // If the return type of D::f differs from the return type of B::f, the |
| 5438 | // class type in the return type of D::f shall be complete at the point of |
| 5439 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 5440 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 5441 | if (!RT->isBeingDefined() && |
| 5442 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 5443 | PDiag(diag::err_covariant_return_incomplete) |
| 5444 | << New->getDeclName())) |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 5445 | return true; |
Anders Carlsson | ac4c939 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 5446 | } |
Anders Carlsson | be2e205 | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 5447 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5448 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5449 | // Check if the new class derives from the old class. |
| 5450 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 5451 | Diag(New->getLocation(), |
| 5452 | diag::err_covariant_return_not_derived) |
| 5453 | << New->getDeclName() << NewTy << OldTy; |
| 5454 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5455 | return true; |
| 5456 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5457 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5458 | // Check if we the conversion from derived to base is valid. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5459 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | e25a96c | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 5460 | diag::err_covariant_return_inaccessible_base, |
| 5461 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 5462 | // FIXME: Should this point to the return type? |
| 5463 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5464 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5465 | return true; |
| 5466 | } |
| 5467 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5468 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5469 | // The qualifiers of the return types must be the same. |
Anders Carlsson | f2a04bf | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 5470 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5471 | Diag(New->getLocation(), |
| 5472 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5473 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5474 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5475 | return true; |
| 5476 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5477 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5478 | |
| 5479 | // The new class type must have the same or less qualifiers as the old type. |
| 5480 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 5481 | Diag(New->getLocation(), |
| 5482 | diag::err_covariant_return_type_class_type_more_qualified) |
| 5483 | << New->getDeclName() << NewTy << OldTy; |
| 5484 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5485 | return true; |
| 5486 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5487 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5488 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5489 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5490 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5491 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
| 5492 | const CXXMethodDecl *Old) |
| 5493 | { |
| 5494 | if (Old->hasAttr<FinalAttr>()) { |
| 5495 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 5496 | << New->getDeclName(); |
| 5497 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5498 | return true; |
| 5499 | } |
| 5500 | |
| 5501 | return false; |
| 5502 | } |
| 5503 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 5504 | /// \brief Mark the given method pure. |
| 5505 | /// |
| 5506 | /// \param Method the method to be marked pure. |
| 5507 | /// |
| 5508 | /// \param InitRange the source range that covers the "0" initializer. |
| 5509 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
| 5510 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 5511 | Method->setPure(); |
| 5512 | |
| 5513 | // A class is abstract if at least one function is pure virtual. |
| 5514 | Method->getParent()->setAbstract(true); |
| 5515 | return false; |
| 5516 | } |
| 5517 | |
| 5518 | if (!Method->isInvalidDecl()) |
| 5519 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 5520 | << Method->getDeclName() << InitRange; |
| 5521 | return true; |
| 5522 | } |
| 5523 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5524 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 5525 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 5526 | /// is a fresh scope pushed for just this purpose. |
| 5527 | /// |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5528 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 5529 | /// static data member of class X, names should be looked up in the scope of |
| 5530 | /// class X. |
| 5531 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5532 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5533 | Decl *D = Dcl.getAs<Decl>(); |
| 5534 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5535 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5536 | // We should only get called for declarations with scope specifiers, like: |
| 5537 | // int foo::bar; |
| 5538 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5539 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5543 | /// initializer for the out-of-line declaration 'Dcl'. |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5544 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5545 | // If there is no declaration, there was an error parsing it. |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5546 | Decl *D = Dcl.getAs<Decl>(); |
| 5547 | if (D == 0) return; |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5548 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5549 | assert(D->isOutOfLine()); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5550 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5551 | } |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 5552 | |
| 5553 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 5554 | /// C++ if/switch/while/for statement. |
| 5555 | /// e.g: "if (int x = f()) {...}" |
| 5556 | Action::DeclResult |
| 5557 | Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
| 5558 | // C++ 6.4p2: |
| 5559 | // The declarator shall not specify a function or an array. |
| 5560 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 5561 | // new class or enumeration. |
| 5562 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 5563 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 5564 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5565 | TypeSourceInfo *TInfo = 0; |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 5566 | TagDecl *OwnedTag = 0; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5567 | QualType Ty = GetTypeForDeclarator(D, S, &TInfo, &OwnedTag); |
Douglas Gregor | 99e9b4d | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 5568 | |
| 5569 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 5570 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 5571 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 5572 | Diag(D.getIdentifierLoc(), diag::err_invalid_use_of_function_type) |
| 5573 | << D.getSourceRange(); |
| 5574 | return DeclResult(); |
| 5575 | } else if (OwnedTag && OwnedTag->isDefinition()) { |
| 5576 | // The type-specifier-seq shall not declare a new class or enumeration. |
| 5577 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition); |
| 5578 | } |
| 5579 | |
| 5580 | DeclPtrTy Dcl = ActOnDeclarator(S, D); |
| 5581 | if (!Dcl) |
| 5582 | return DeclResult(); |
| 5583 | |
| 5584 | VarDecl *VD = cast<VarDecl>(Dcl.getAs<Decl>()); |
| 5585 | VD->setDeclaredInCondition(true); |
| 5586 | return Dcl; |
| 5587 | } |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5588 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 5589 | static bool needsVTable(CXXMethodDecl *MD, ASTContext &Context) { |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5590 | // Ignore dependent types. |
| 5591 | if (MD->isDependentContext()) |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5592 | return false; |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 5593 | |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 5594 | // Ignore declarations that are not definitions. |
| 5595 | if (!MD->isThisDeclarationADefinition()) |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5596 | return false; |
| 5597 | |
| 5598 | CXXRecordDecl *RD = MD->getParent(); |
| 5599 | |
| 5600 | // Ignore classes without a vtable. |
| 5601 | if (!RD->isDynamicClass()) |
| 5602 | return false; |
| 5603 | |
| 5604 | switch (MD->getParent()->getTemplateSpecializationKind()) { |
| 5605 | case TSK_Undeclared: |
| 5606 | case TSK_ExplicitSpecialization: |
| 5607 | // Classes that aren't instantiations of templates don't need their |
| 5608 | // virtual methods marked until we see the definition of the key |
| 5609 | // function. |
| 5610 | break; |
| 5611 | |
| 5612 | case TSK_ImplicitInstantiation: |
| 5613 | // This is a constructor of a class template; mark all of the virtual |
| 5614 | // members as referenced to ensure that they get instantiatied. |
| 5615 | if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) |
| 5616 | return true; |
| 5617 | break; |
| 5618 | |
| 5619 | case TSK_ExplicitInstantiationDeclaration: |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5620 | return false; |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5621 | |
| 5622 | case TSK_ExplicitInstantiationDefinition: |
| 5623 | // This is method of a explicit instantiation; mark all of the virtual |
| 5624 | // members as referenced to ensure that they get instantiatied. |
| 5625 | return true; |
Douglas Gregor | 4b0f21c | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 5626 | } |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5627 | |
| 5628 | // Consider only out-of-line definitions of member functions. When we see |
| 5629 | // an inline definition, it's too early to compute the key function. |
| 5630 | if (!MD->isOutOfLine()) |
| 5631 | return false; |
| 5632 | |
| 5633 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(RD); |
| 5634 | |
| 5635 | // If there is no key function, we will need a copy of the vtable. |
| 5636 | if (!KeyFunction) |
| 5637 | return true; |
| 5638 | |
| 5639 | // If this is the key function, we need to mark virtual members. |
| 5640 | if (KeyFunction->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 5641 | return true; |
| 5642 | |
| 5643 | return false; |
| 5644 | } |
| 5645 | |
| 5646 | void Sema::MaybeMarkVirtualMembersReferenced(SourceLocation Loc, |
| 5647 | CXXMethodDecl *MD) { |
| 5648 | CXXRecordDecl *RD = MD->getParent(); |
| 5649 | |
Douglas Gregor | 159ef1e | 2010-01-06 04:44:19 +0000 | [diff] [blame] | 5650 | // We will need to mark all of the virtual members as referenced to build the |
| 5651 | // vtable. |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 5652 | if (!needsVTable(MD, Context)) |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 5653 | return; |
| 5654 | |
| 5655 | TemplateSpecializationKind kind = RD->getTemplateSpecializationKind(); |
| 5656 | if (kind == TSK_ImplicitInstantiation) |
| 5657 | ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(RD, Loc)); |
| 5658 | else |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5659 | MarkVirtualMembersReferenced(Loc, RD); |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | bool Sema::ProcessPendingClassesWithUnmarkedVirtualMembers() { |
| 5663 | if (ClassesWithUnmarkedVirtualMembers.empty()) |
| 5664 | return false; |
| 5665 | |
Douglas Gregor | 159ef1e | 2010-01-06 04:44:19 +0000 | [diff] [blame] | 5666 | while (!ClassesWithUnmarkedVirtualMembers.empty()) { |
| 5667 | CXXRecordDecl *RD = ClassesWithUnmarkedVirtualMembers.back().first; |
| 5668 | SourceLocation Loc = ClassesWithUnmarkedVirtualMembers.back().second; |
| 5669 | ClassesWithUnmarkedVirtualMembers.pop_back(); |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5670 | MarkVirtualMembersReferenced(Loc, RD); |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5671 | } |
| 5672 | |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 5676 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 5677 | const CXXRecordDecl *RD) { |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5678 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 5679 | e = RD->method_end(); i != e; ++i) { |
| 5680 | CXXMethodDecl *MD = *i; |
| 5681 | |
| 5682 | // C++ [basic.def.odr]p2: |
| 5683 | // [...] A virtual member function is used if it is not pure. [...] |
| 5684 | if (MD->isVirtual() && !MD->isPure()) |
| 5685 | MarkDeclarationReferenced(Loc, MD); |
| 5686 | } |
Rafael Espindola | 3e1ae93 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 5687 | |
| 5688 | // Only classes that have virtual bases need a VTT. |
| 5689 | if (RD->getNumVBases() == 0) |
| 5690 | return; |
| 5691 | |
| 5692 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 5693 | e = RD->bases_end(); i != e; ++i) { |
| 5694 | const CXXRecordDecl *Base = |
| 5695 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 5696 | if (i->isVirtual()) |
| 5697 | continue; |
| 5698 | if (Base->getNumVBases() == 0) |
| 5699 | continue; |
| 5700 | MarkVirtualMembersReferenced(Loc, Base); |
| 5701 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5702 | } |