Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 1 | //===------ SemaDeclCXX.cpp - Semantic Analysis for C++ Declarations ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
John McCall | cc14d1f | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 15 | #include "clang/Sema/CXXFieldCollector.h" |
| 16 | #include "clang/Sema/Scope.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Initialization.h" |
| 18 | #include "clang/Sema/Lookup.h" |
Argyrios Kyrtzidis | 2f67f37 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 21 | #include "clang/AST/CharUnits.h" |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
| 25 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeOrdering.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DeclSpec.h" |
| 29 | #include "clang/Sema/ParsedTemplate.h" |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 30 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 153d967 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
John McCall | a1e130b | 2010-08-25 07:03:20 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/DenseSet.h" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 34 | #include <map> |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 35 | #include <set> |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace clang; |
| 38 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // CheckDefaultArgumentVisitor |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 45 | /// the default argument of a parameter to determine whether it |
| 46 | /// contains any ill-formed subexpressions. For example, this will |
| 47 | /// diagnose the use of local variables or parameters within the |
| 48 | /// default argument expression. |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 49 | class CheckDefaultArgumentVisitor |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 50 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 51 | Expr *DefaultArg; |
| 52 | Sema *S; |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 53 | |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 54 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 56 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 57 | |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 58 | bool VisitExpr(Expr *Node); |
| 59 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 97a9c81 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 60 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 61 | }; |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 62 | |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 63 | /// VisitExpr - Visit all of the children of this expression. |
| 64 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 65 | bool IsInvalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | for (Stmt::child_iterator I = Node->child_begin(), |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 67 | E = Node->child_end(); I != E; ++I) |
| 68 | IsInvalid |= Visit(*I); |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 69 | return IsInvalid; |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 72 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 73 | /// determine whether this declaration can be used in the default |
| 74 | /// argument expression. |
| 75 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 76 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 77 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 78 | // C++ [dcl.fct.default]p9 |
| 79 | // Default arguments are evaluated each time the function is |
| 80 | // called. The order of evaluation of function arguments is |
| 81 | // unspecified. Consequently, parameters of a function shall not |
| 82 | // be used in default argument expressions, even if they are not |
| 83 | // evaluated. Parameters of a function declared before a default |
| 84 | // argument expression are in scope and can hide namespace and |
| 85 | // class member names. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 87 | diag::err_param_default_argument_references_param) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 88 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 89 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 90 | // C++ [dcl.fct.default]p7 |
| 91 | // Local variables shall not be used in default argument |
| 92 | // expressions. |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 93 | if (VDecl->isBlockVarDecl()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 95 | diag::err_param_default_argument_references_local) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 96 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 97 | } |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 8e12c38 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 99 | return false; |
| 100 | } |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | 97a9c81 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 102 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 103 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 104 | // C++ [dcl.fct.default]p8: |
| 105 | // The keyword this shall not be used in a default argument of a |
| 106 | // member function. |
| 107 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 108 | diag::err_param_default_argument_references_this) |
| 109 | << ThisE->getSourceRange(); |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 110 | } |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 113 | bool |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 114 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, Expr *Arg, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | SourceLocation EqualLoc) { |
Anders Carlsson | 114056f | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 116 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 117 | diag::err_typecheck_decl_incomplete_type)) { |
| 118 | Param->setInvalidDecl(); |
| 119 | return true; |
| 120 | } |
| 121 | |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 122 | // C++ [dcl.fct.default]p5 |
| 123 | // A default argument expression is implicitly converted (clause |
| 124 | // 4) to the parameter type. The default argument expression has |
| 125 | // the same semantic constraints as the initializer expression in |
| 126 | // a declaration of a variable of the parameter type, using the |
| 127 | // copy-initialization semantics (8.5). |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 128 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 129 | Param); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 130 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 131 | EqualLoc); |
Eli Friedman | 5f101b9 | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 132 | InitializationSequence InitSeq(*this, Entity, Kind, &Arg, 1); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 133 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 134 | MultiExprArg(*this, &Arg, 1)); |
Eli Friedman | 5f101b9 | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 135 | if (Result.isInvalid()) |
Anders Carlsson | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 136 | return true; |
Eli Friedman | 5f101b9 | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 137 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 138 | |
Anders Carlsson | 6e997b2 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 139 | Arg = MaybeCreateCXXExprWithTemporaries(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 141 | // Okay: add the default argument to the parameter |
| 142 | Param->setDefaultArg(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Anders Carlsson | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 144 | return false; |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 147 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 148 | /// provided for a function parameter is well-formed. If so, attach it |
| 149 | /// to the parameter declaration. |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 150 | void |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 151 | Sema::ActOnParamDefaultArgument(Decl *param, SourceLocation EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 152 | Expr *DefaultArg) { |
| 153 | if (!param || !DefaultArg) |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 154 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 156 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 157 | UnparsedDefaultArgLocs.erase(Param); |
| 158 | |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 159 | // Default arguments are only permitted in C++ |
| 160 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 161 | Diag(EqualLoc, diag::err_param_default_argument) |
| 162 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 163 | Param->setInvalidDecl(); |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 164 | return; |
| 165 | } |
| 166 | |
Anders Carlsson | f1c2695 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 167 | // Check that the default argument is well-formed |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 168 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg, this); |
| 169 | if (DefaultArgChecker.Visit(DefaultArg)) { |
Anders Carlsson | f1c2695 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 170 | Param->setInvalidDecl(); |
| 171 | return; |
| 172 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 174 | SetParamDefaultArgument(Param, DefaultArg, EqualLoc); |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 177 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 178 | /// argument for a function parameter, but we can't parse it yet |
| 179 | /// because we're inside a class definition. Note that this default |
| 180 | /// argument will be parsed later. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 181 | void Sema::ActOnParamUnparsedDefaultArgument(Decl *param, |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 182 | SourceLocation EqualLoc, |
| 183 | SourceLocation ArgLoc) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 184 | if (!param) |
| 185 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 187 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 188 | if (Param) |
| 189 | Param->setUnparsedDefaultArg(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 191 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 194 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 195 | /// the default argument for the parameter param failed. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 196 | void Sema::ActOnParamDefaultArgumentError(Decl *param) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 197 | if (!param) |
| 198 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 200 | ParmVarDecl *Param = cast<ParmVarDecl>(param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 202 | Param->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 204 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 207 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 208 | /// arguments in the declarator, which is not a function declaration |
| 209 | /// or definition and therefore is not permitted to have default |
| 210 | /// arguments. This routine should be invoked for every declarator |
| 211 | /// that is not a function declaration or definition. |
| 212 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 213 | // C++ [dcl.fct.default]p3 |
| 214 | // A default argument expression shall be specified only in the |
| 215 | // parameter-declaration-clause of a function declaration or in a |
| 216 | // template-parameter (14.1). It shall not be specified for a |
| 217 | // parameter pack. If it is specified in a |
| 218 | // parameter-declaration-clause, it shall not occur within a |
| 219 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 220 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 221 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 222 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 223 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 224 | ParmVarDecl *Param = |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 225 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param); |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 226 | if (Param->hasUnparsedDefaultArg()) { |
| 227 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 228 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 229 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 230 | delete Toks; |
| 231 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 232 | } else if (Param->getDefaultArg()) { |
| 233 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 234 | << Param->getDefaultArg()->getSourceRange(); |
| 235 | Param->setDefaultArg(0); |
Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 242 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 243 | // function, once we already know that they have the same |
Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 244 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 245 | // error, false otherwise. |
| 246 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 247 | bool Invalid = false; |
| 248 | |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 249 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 250 | // For non-template functions, default arguments can be added in |
| 251 | // later declarations of a function in the same |
| 252 | // scope. Declarations in different scopes have completely |
| 253 | // distinct sets of default arguments. That is, declarations in |
| 254 | // inner scopes do not acquire default arguments from |
| 255 | // declarations in outer scopes, and vice versa. In a given |
| 256 | // function declaration, all parameters subsequent to a |
| 257 | // parameter with a default argument shall have default |
| 258 | // arguments supplied in this or previous declarations. A |
| 259 | // default argument shall not be redefined by a later |
| 260 | // declaration (not even to the same value). |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 261 | // |
| 262 | // C++ [dcl.fct.default]p6: |
| 263 | // Except for member functions of class templates, the default arguments |
| 264 | // in a member function definition that appears outside of the class |
| 265 | // definition are added to the set of default arguments provided by the |
| 266 | // member function declaration in the class definition. |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 267 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 268 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 269 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 270 | |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 271 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Douglas Gregor | 08dc584 | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 272 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 273 | // hint here. Alternatively, we could walk the type-source information |
| 274 | // for NewParam to find the last source location in the type... but it |
| 275 | // isn't worth the effort right now. This is the kind of test case that |
| 276 | // is hard to get right: |
| 277 | |
| 278 | // int f(int); |
| 279 | // void g(int (*fp)(int) = f); |
| 280 | // void g(int (*fp)(int) = &f); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | Diag(NewParam->getLocation(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 282 | diag::err_param_default_argument_redefinition) |
Douglas Gregor | 08dc584 | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 283 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 284 | |
| 285 | // Look for the function declaration where the default argument was |
| 286 | // actually written, which may be a declaration prior to Old. |
| 287 | for (FunctionDecl *Older = Old->getPreviousDeclaration(); |
| 288 | Older; Older = Older->getPreviousDeclaration()) { |
| 289 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 290 | break; |
| 291 | |
| 292 | OldParam = Older->getParamDecl(p); |
| 293 | } |
| 294 | |
| 295 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 296 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 297 | Invalid = true; |
Douglas Gregor | 4f15f4d | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 298 | } else if (OldParam->hasDefaultArg()) { |
John McCall | e61b02b | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 299 | // Merge the old default argument into the new parameter. |
| 300 | // It's important to use getInit() here; getDefaultArg() |
| 301 | // strips off any top-level CXXExprWithTemporaries. |
John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 302 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | 4f15f4d | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 303 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 304 | NewParam->setUninstantiatedDefaultArg( |
| 305 | OldParam->getUninstantiatedDefaultArg()); |
| 306 | else |
John McCall | e61b02b | 2010-05-04 01:53:42 +0000 | [diff] [blame] | 307 | NewParam->setDefaultArg(OldParam->getInit()); |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 308 | } else if (NewParam->hasDefaultArg()) { |
| 309 | if (New->getDescribedFunctionTemplate()) { |
| 310 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 311 | Diag(NewParam->getLocation(), |
| 312 | diag::err_param_default_argument_template_redecl) |
| 313 | << NewParam->getDefaultArgRange(); |
| 314 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 315 | << false; |
Douglas Gregor | 62e10f0 | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 316 | } else if (New->getTemplateSpecializationKind() |
| 317 | != TSK_ImplicitInstantiation && |
| 318 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 319 | // C++ [temp.expr.spec]p21: |
| 320 | // Default function arguments shall not be specified in a declaration |
| 321 | // or a definition for one of the following explicit specializations: |
| 322 | // - the explicit specialization of a function template; |
Douglas Gregor | 3362bde | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 323 | // - the explicit specialization of a member function template; |
| 324 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 62e10f0 | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 325 | // template where the class template specialization to which the |
| 326 | // member function specialization belongs is implicitly |
| 327 | // instantiated. |
| 328 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 329 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 330 | << New->getDeclName() |
| 331 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 332 | } else if (New->getDeclContext()->isDependentContext()) { |
| 333 | // C++ [dcl.fct.default]p6 (DR217): |
| 334 | // Default arguments for a member function of a class template shall |
| 335 | // be specified on the initial declaration of the member function |
| 336 | // within the class template. |
| 337 | // |
| 338 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 339 | // leads me to the conclusion that one cannot add default function |
| 340 | // arguments for an out-of-line definition of a member function of a |
| 341 | // dependent type. |
| 342 | int WhichKind = 2; |
| 343 | if (CXXRecordDecl *Record |
| 344 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 345 | if (Record->getDescribedClassTemplate()) |
| 346 | WhichKind = 0; |
| 347 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 348 | WhichKind = 1; |
| 349 | else |
| 350 | WhichKind = 2; |
| 351 | } |
| 352 | |
| 353 | Diag(NewParam->getLocation(), |
| 354 | diag::err_param_default_argument_member_template_redecl) |
| 355 | << WhichKind |
| 356 | << NewParam->getDefaultArgRange(); |
| 357 | } |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 361 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4f4d7b5 | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 362 | Invalid = true; |
Sebastian Redl | 4f4d7b5 | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 363 | |
Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 364 | return Invalid; |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 368 | /// function declaration are well-formed according to C++ |
| 369 | /// [dcl.fct.default]. |
| 370 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 371 | unsigned NumParams = FD->getNumParams(); |
| 372 | unsigned p; |
| 373 | |
| 374 | // Find first parameter with a default argument |
| 375 | for (p = 0; p < NumParams; ++p) { |
| 376 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5a53238 | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 377 | if (Param->hasDefaultArg()) |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | |
| 381 | // C++ [dcl.fct.default]p4: |
| 382 | // In a given function declaration, all parameters |
| 383 | // subsequent to a parameter with a default argument shall |
| 384 | // have default arguments supplied in this or previous |
| 385 | // declarations. A default argument shall not be redefined |
| 386 | // by a later declaration (not even to the same value). |
| 387 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | for (; p < NumParams; ++p) { |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 389 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5a53238 | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 390 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 391 | if (Param->isInvalidDecl()) |
| 392 | /* We already complained about this parameter. */; |
| 393 | else if (Param->getIdentifier()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | Diag(Param->getLocation(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 395 | diag::err_param_default_argument_missing_name) |
Chris Lattner | b91fd17 | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 396 | << Param->getIdentifier(); |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 397 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | Diag(Param->getLocation(), |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 399 | diag::err_param_default_argument_missing); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 401 | LastMissingDefaultArg = p; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | if (LastMissingDefaultArg > 0) { |
| 406 | // Some default arguments were missing. Clear out all of the |
| 407 | // default arguments up to (and including) the last missing |
| 408 | // default argument, so that we leave the function parameters |
| 409 | // in a semantically valid state. |
| 410 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 411 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 412 | if (Param->hasDefaultArg()) { |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 413 | Param->setDefaultArg(0); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 418 | |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 419 | /// isCurrentClassName - Determine whether the identifier II is the |
| 420 | /// name of the class type currently being defined. In the case of |
| 421 | /// nested classes, this will only return true if II is the name of |
| 422 | /// the innermost class. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 423 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 424 | const CXXScopeSpec *SS) { |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 425 | assert(getLangOptions().CPlusPlus && "No class names in C!"); |
| 426 | |
Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 427 | CXXRecordDecl *CurDecl; |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 428 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | e5bbb7d | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 429 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 430 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 431 | } else |
| 432 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 433 | |
Douglas Gregor | 1aa3edb | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 434 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 435 | return &II == CurDecl->getIdentifier(); |
| 436 | else |
| 437 | return false; |
| 438 | } |
| 439 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 441 | /// |
| 442 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 443 | /// and returns NULL otherwise. |
| 444 | CXXBaseSpecifier * |
| 445 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 446 | SourceRange SpecifierRange, |
| 447 | bool Virtual, AccessSpecifier Access, |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 448 | TypeSourceInfo *TInfo) { |
| 449 | QualType BaseType = TInfo->getType(); |
| 450 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 461 | Class->getTagKind() == TTK_Class, |
| 462 | Access, TInfo); |
| 463 | |
| 464 | SourceLocation BaseLoc = TInfo->getTypeLoc().getBeginLoc(); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 465 | |
| 466 | // Base specifiers must be record types. |
| 467 | if (!BaseType->isRecordType()) { |
| 468 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | // C++ [class.union]p1: |
| 473 | // A union shall not be used as a base class. |
| 474 | if (BaseType->isUnionType()) { |
| 475 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | // C++ [class.derived]p2: |
| 480 | // The class-name in a base-specifier shall not be an incompletely |
| 481 | // defined class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 483 | PDiag(diag::err_incomplete_base_class) |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 484 | << SpecifierRange)) { |
| 485 | Class->setInvalidDecl(); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 486 | return 0; |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 487 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 488 | |
Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 489 | // If the base class is polymorphic or isn't empty, the new one is/isn't, too. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 490 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 491 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 492 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 493 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 494 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 495 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | 89c038e | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 496 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 497 | // C++0x CWG Issue #817 indicates that [[final]] classes shouldn't be bases. |
| 498 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
| 499 | Diag(BaseLoc, diag::err_final_base) << BaseType.getAsString(); |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 500 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 501 | << BaseType; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 502 | return 0; |
| 503 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 504 | |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 505 | if (BaseDecl->isInvalidDecl()) |
| 506 | Class->setInvalidDecl(); |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 507 | |
| 508 | // Create the base specifier. |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 509 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 510 | Class->getTagKind() == TTK_Class, |
| 511 | Access, TInfo); |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 514 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 515 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | /// example: |
| 517 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 518 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 519 | BaseResult |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 520 | Sema::ActOnBaseSpecifier(Decl *classdecl, SourceRange SpecifierRange, |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 521 | bool Virtual, AccessSpecifier Access, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 522 | ParsedType basetype, SourceLocation BaseLoc) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 523 | if (!classdecl) |
| 524 | return true; |
| 525 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 526 | AdjustDeclIfTemplate(classdecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 527 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl); |
Douglas Gregor | beab56e | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 528 | if (!Class) |
| 529 | return true; |
| 530 | |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 531 | TypeSourceInfo *TInfo = 0; |
| 532 | GetTypeFromParser(basetype, &TInfo); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 533 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 534 | Virtual, Access, TInfo)) |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 535 | return BaseSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 537 | return true; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 538 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 539 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 540 | /// \brief Performs the actual work of attaching the given base class |
| 541 | /// specifiers to a C++ class. |
| 542 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 543 | unsigned NumBases) { |
| 544 | if (NumBases == 0) |
| 545 | return false; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 546 | |
| 547 | // Used to keep track of which base types we have already seen, so |
| 548 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 549 | // that the key is always the unqualified canonical type of the base |
| 550 | // class. |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 551 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 552 | |
| 553 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 554 | unsigned NumGoodBases = 0; |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 555 | bool Invalid = false; |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 556 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | QualType NewBaseType |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 558 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 559 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Fariborz Jahanian | 2792f30 | 2010-05-20 23:34:56 +0000 | [diff] [blame] | 560 | if (!Class->hasObjectMember()) { |
| 561 | if (const RecordType *FDTTy = |
| 562 | NewBaseType.getTypePtr()->getAs<RecordType>()) |
| 563 | if (FDTTy->getDecl()->hasObjectMember()) |
| 564 | Class->setHasObjectMember(true); |
| 565 | } |
| 566 | |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 567 | if (KnownBaseTypes[NewBaseType]) { |
| 568 | // C++ [class.mi]p3: |
| 569 | // A class shall not be specified as a direct base class of a |
| 570 | // derived class more than once. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 571 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 572 | diag::err_duplicate_base_class) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 573 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 574 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 575 | |
| 576 | // Delete the duplicate base class specifier; we're going to |
| 577 | // overwrite its pointer later. |
Douglas Gregor | b77af8f | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 578 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 579 | |
| 580 | Invalid = true; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 581 | } else { |
| 582 | // Okay, add this new base class. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 583 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 584 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
| 588 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 4a62bdf | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 589 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 590 | |
| 591 | // Delete the remaining (good) base class specifiers, since their |
| 592 | // data has been copied into the CXXRecordDecl. |
| 593 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | b77af8f | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 594 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 595 | |
| 596 | return Invalid; |
| 597 | } |
| 598 | |
| 599 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 600 | /// class, after checking whether there are any duplicate base |
| 601 | /// classes. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 602 | void Sema::ActOnBaseSpecifiers(Decl *ClassDecl, BaseTy **Bases, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 603 | unsigned NumBases) { |
| 604 | if (!ClassDecl || !Bases || !NumBases) |
| 605 | return; |
| 606 | |
| 607 | AdjustDeclIfTemplate(ClassDecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 608 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 609 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 610 | } |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 611 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 612 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 613 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 614 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 615 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 616 | return ICT->getDecl(); |
| 617 | else |
| 618 | return 0; |
| 619 | } |
| 620 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 621 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 622 | /// derived from the type \p Base. |
| 623 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 624 | if (!getLangOptions().CPlusPlus) |
| 625 | return false; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 626 | |
| 627 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 628 | if (!DerivedRD) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 629 | return false; |
| 630 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 631 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 632 | if (!BaseRD) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 633 | return false; |
| 634 | |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 635 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 636 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 640 | /// derived from the type \p Base. |
| 641 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 642 | if (!getLangOptions().CPlusPlus) |
| 643 | return false; |
| 644 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 645 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 646 | if (!DerivedRD) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 647 | return false; |
| 648 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 649 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 650 | if (!BaseRD) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 651 | return false; |
| 652 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 653 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 654 | } |
| 655 | |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 656 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 657 | CXXCastPath &BasePathArray) { |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 658 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 659 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 660 | |
| 661 | const CXXBasePath &Path = Paths.front(); |
| 662 | |
| 663 | // We first go backward and check if we have a virtual base. |
| 664 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 665 | // the nearest virtual base. |
| 666 | unsigned Start = 0; |
| 667 | for (unsigned I = Path.size(); I != 0; --I) { |
| 668 | if (Path[I - 1].Base->isVirtual()) { |
| 669 | Start = I - 1; |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | // Now add all bases. |
| 675 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 676 | BasePathArray.push_back(const_cast<CXXBaseSpecifier*>(Path[I].Base)); |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 679 | /// \brief Determine whether the given base path includes a virtual |
| 680 | /// base class. |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 681 | bool Sema::BasePathInvolvesVirtualBase(const CXXCastPath &BasePath) { |
| 682 | for (CXXCastPath::const_iterator B = BasePath.begin(), |
| 683 | BEnd = BasePath.end(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 684 | B != BEnd; ++B) |
| 685 | if ((*B)->isVirtual()) |
| 686 | return true; |
| 687 | |
| 688 | return false; |
| 689 | } |
| 690 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 691 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 692 | /// conversion (where Derived and Base are class types) is |
| 693 | /// well-formed, meaning that the conversion is unambiguous (and |
| 694 | /// that all of the base classes are accessible). Returns true |
| 695 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 696 | /// otherwise. Loc is the location where this routine should point to |
| 697 | /// if there is an error, and Range is the source range to highlight |
| 698 | /// if there is an error. |
| 699 | bool |
| 700 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 701 | unsigned InaccessibleBaseID, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 702 | unsigned AmbigiousBaseConvID, |
| 703 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 704 | DeclarationName Name, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 705 | CXXCastPath *BasePath) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 706 | // First, determine whether the path from Derived to Base is |
| 707 | // ambiguous. This is slightly more expensive than checking whether |
| 708 | // the Derived to Base conversion exists, because here we need to |
| 709 | // explore multiple paths to determine if there is an ambiguity. |
| 710 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 711 | /*DetectVirtual=*/false); |
| 712 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 713 | assert(DerivationOkay && |
| 714 | "Can only be used with a derived-to-base conversion"); |
| 715 | (void)DerivationOkay; |
| 716 | |
| 717 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 718 | if (InaccessibleBaseID) { |
| 719 | // Check that the base class can be accessed. |
| 720 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 721 | InaccessibleBaseID)) { |
| 722 | case AR_inaccessible: |
| 723 | return true; |
| 724 | case AR_accessible: |
| 725 | case AR_dependent: |
| 726 | case AR_delayed: |
| 727 | break; |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 728 | } |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 729 | } |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 730 | |
| 731 | // Build a base path if necessary. |
| 732 | if (BasePath) |
| 733 | BuildBasePathArray(Paths, *BasePath); |
| 734 | return false; |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | // We know that the derived-to-base conversion is ambiguous, and |
| 738 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 739 | // search just one more time to compute all of the possible paths so |
| 740 | // that we can print them out. This is more expensive than any of |
| 741 | // the previous derived-to-base checks we've done, but at this point |
| 742 | // performance isn't as much of an issue. |
| 743 | Paths.clear(); |
| 744 | Paths.setRecordingPaths(true); |
| 745 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 746 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 747 | (void)StillOkay; |
| 748 | |
| 749 | // Build up a textual representation of the ambiguous paths, e.g., |
| 750 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 751 | // conversions in the diagnostic. We only print one of the paths |
| 752 | // to each base class subobject. |
| 753 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 754 | |
| 755 | Diag(Loc, AmbigiousBaseConvID) |
| 756 | << Derived << Base << PathDisplayStr << Range << Name; |
| 757 | return true; |
| 758 | } |
| 759 | |
| 760 | bool |
| 761 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 762 | SourceLocation Loc, SourceRange Range, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 763 | CXXCastPath *BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 764 | bool IgnoreAccess) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 765 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 766 | IgnoreAccess ? 0 |
| 767 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 768 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 769 | Loc, Range, DeclarationName(), |
| 770 | BasePath); |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | |
| 774 | /// @brief Builds a string representing ambiguous paths from a |
| 775 | /// specific derived class to different subobjects of the same base |
| 776 | /// class. |
| 777 | /// |
| 778 | /// This function builds a string that can be used in error messages |
| 779 | /// to show the different paths that one can take through the |
| 780 | /// inheritance hierarchy to go from the derived class to different |
| 781 | /// subobjects of a base class. The result looks something like this: |
| 782 | /// @code |
| 783 | /// struct D -> struct B -> struct A |
| 784 | /// struct D -> struct C -> struct A |
| 785 | /// @endcode |
| 786 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 787 | std::string PathDisplayStr; |
| 788 | std::set<unsigned> DisplayedPaths; |
| 789 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 790 | Path != Paths.end(); ++Path) { |
| 791 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 792 | // We haven't displayed a path to this particular base |
| 793 | // class subobject yet. |
| 794 | PathDisplayStr += "\n "; |
| 795 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 796 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 797 | Element != Path->end(); ++Element) |
| 798 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | return PathDisplayStr; |
| 803 | } |
| 804 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 805 | //===----------------------------------------------------------------------===// |
| 806 | // C++ class member Handling |
| 807 | //===----------------------------------------------------------------------===// |
| 808 | |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 809 | /// ActOnAccessSpecifier - Parsed an access specifier followed by a colon. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 810 | Decl *Sema::ActOnAccessSpecifier(AccessSpecifier Access, |
| 811 | SourceLocation ASLoc, |
| 812 | SourceLocation ColonLoc) { |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 813 | assert(Access != AS_none && "Invalid kind for syntactic access specifier!"); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 814 | AccessSpecDecl *ASDecl = AccessSpecDecl::Create(Context, Access, CurContext, |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 815 | ASLoc, ColonLoc); |
| 816 | CurContext->addHiddenDecl(ASDecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 817 | return ASDecl; |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 820 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 821 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 822 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
Chris Lattner | eb4373d | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 823 | /// any. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 824 | Decl * |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 825 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 826 | MultiTemplateParamsArg TemplateParameterLists, |
Sebastian Redl | d6f7850 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 827 | ExprTy *BW, ExprTy *InitExpr, bool IsDefinition, |
| 828 | bool Deleted) { |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 829 | const DeclSpec &DS = D.getDeclSpec(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 830 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 831 | DeclarationName Name = NameInfo.getName(); |
| 832 | SourceLocation Loc = NameInfo.getLoc(); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 833 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 834 | Expr *Init = static_cast<Expr*>(InitExpr); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 835 | |
John McCall | b1cd7da | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 836 | assert(isa<CXXRecordDecl>(CurContext)); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 837 | assert(!DS.isFriendSpecified()); |
| 838 | |
John McCall | b1cd7da | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 839 | bool isFunc = false; |
| 840 | if (D.isFunctionDeclarator()) |
| 841 | isFunc = true; |
| 842 | else if (D.getNumTypeObjects() == 0 && |
| 843 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 844 | QualType TDType = GetTypeFromParser(DS.getRepAsType()); |
John McCall | b1cd7da | 2010-06-04 08:34:12 +0000 | [diff] [blame] | 845 | isFunc = TDType->isFunctionType(); |
| 846 | } |
| 847 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 848 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 849 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 850 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 851 | // data members and cannot be applied to names declared const or static, |
| 852 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 853 | switch (DS.getStorageClassSpec()) { |
| 854 | case DeclSpec::SCS_unspecified: |
| 855 | case DeclSpec::SCS_typedef: |
| 856 | case DeclSpec::SCS_static: |
| 857 | // FALL THROUGH. |
| 858 | break; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 859 | case DeclSpec::SCS_mutable: |
| 860 | if (isFunc) { |
| 861 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 862 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 863 | else |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 864 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | |
Sebastian Redl | 8071edb | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 866 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 867 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 868 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 869 | } |
| 870 | break; |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 871 | default: |
| 872 | if (DS.getStorageClassSpecLoc().isValid()) |
| 873 | Diag(DS.getStorageClassSpecLoc(), |
| 874 | diag::err_storageclass_invalid_for_member); |
| 875 | else |
| 876 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 877 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 878 | } |
| 879 | |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 880 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 881 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | 1207d31 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 882 | !isFunc); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 883 | |
| 884 | Decl *Member; |
Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 885 | if (isInstField) { |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 886 | // FIXME: Check for template parameters! |
Douglas Gregor | 4261e4c | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 887 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 888 | AS); |
Chris Lattner | 97e277e | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 889 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 890 | } else { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 891 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition); |
Chris Lattner | 97e277e | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 892 | if (!Member) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 893 | return 0; |
Chris Lattner | 97e277e | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 894 | } |
Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 895 | |
| 896 | // Non-instance-fields can't have a bitfield. |
| 897 | if (BitWidth) { |
| 898 | if (Member->isInvalidDecl()) { |
| 899 | // don't emit another diagnostic. |
Douglas Gregor | 212cab3 | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 900 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 901 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 902 | // "static member 'A' cannot be a bit-field" |
| 903 | Diag(Loc, diag::err_static_not_bitfield) |
| 904 | << Name << BitWidth->getSourceRange(); |
| 905 | } else if (isa<TypedefDecl>(Member)) { |
| 906 | // "typedef member 'x' cannot be a bit-field" |
| 907 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 908 | << Name << BitWidth->getSourceRange(); |
| 909 | } else { |
| 910 | // A function typedef ("typedef int f(); f a;"). |
| 911 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 912 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 914 | << BitWidth->getSourceRange(); |
Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 915 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 916 | |
Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 917 | BitWidth = 0; |
| 918 | Member->setInvalidDecl(); |
| 919 | } |
Douglas Gregor | 4261e4c | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 920 | |
| 921 | Member->setAccess(AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 922 | |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 923 | // If we have declared a member function template, set the access of the |
| 924 | // templated declaration as well. |
| 925 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 926 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 927 | } |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 928 | |
Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 929 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 930 | |
Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 931 | if (Init) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 932 | AddInitializerToDecl(Member, Init, false); |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 933 | if (Deleted) // FIXME: Source location is not very good. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 934 | SetDeclDeleted(Member, D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 935 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 936 | if (isInstField) { |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 937 | FieldCollector->Add(cast<FieldDecl>(Member)); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 938 | return 0; |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 939 | } |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 940 | return Member; |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 943 | /// \brief Find the direct and/or virtual base specifiers that |
| 944 | /// correspond to the given base type, for use in base initialization |
| 945 | /// within a constructor. |
| 946 | static bool FindBaseInitializer(Sema &SemaRef, |
| 947 | CXXRecordDecl *ClassDecl, |
| 948 | QualType BaseType, |
| 949 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 950 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 951 | // First, check for a direct base class. |
| 952 | DirectBaseSpec = 0; |
| 953 | for (CXXRecordDecl::base_class_const_iterator Base |
| 954 | = ClassDecl->bases_begin(); |
| 955 | Base != ClassDecl->bases_end(); ++Base) { |
| 956 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 957 | // We found a direct base of this type. That's what we're |
| 958 | // initializing. |
| 959 | DirectBaseSpec = &*Base; |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | // Check for a virtual base class. |
| 965 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 966 | // there are no virtual bases. |
| 967 | VirtualBaseSpec = 0; |
| 968 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 969 | // We haven't found a base yet; search the class hierarchy for a |
| 970 | // virtual base class. |
| 971 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 972 | /*DetectVirtual=*/false); |
| 973 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 974 | BaseType, Paths)) { |
| 975 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 976 | Path != Paths.end(); ++Path) { |
| 977 | if (Path->back().Base->isVirtual()) { |
| 978 | VirtualBaseSpec = Path->back().Base; |
| 979 | break; |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | return DirectBaseSpec || VirtualBaseSpec; |
| 986 | } |
| 987 | |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 988 | /// ActOnMemInitializer - Handle a C++ member initializer. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 989 | MemInitResult |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 990 | Sema::ActOnMemInitializer(Decl *ConstructorD, |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 991 | Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 992 | CXXScopeSpec &SS, |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 993 | IdentifierInfo *MemberOrBase, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 994 | ParsedType TemplateTypeTy, |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 995 | SourceLocation IdLoc, |
| 996 | SourceLocation LParenLoc, |
| 997 | ExprTy **Args, unsigned NumArgs, |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 998 | SourceLocation RParenLoc) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 999 | if (!ConstructorD) |
| 1000 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Douglas Gregor | c8c277a | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1002 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | |
| 1004 | CXXConstructorDecl *Constructor |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1005 | = dyn_cast<CXXConstructorDecl>(ConstructorD); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1006 | if (!Constructor) { |
| 1007 | // The user wrote a constructor initializer on a function that is |
| 1008 | // not a C++ constructor. Ignore the error for now, because we may |
| 1009 | // have more member initializers coming; we'll diagnose it just |
| 1010 | // once in ActOnMemInitializers. |
| 1011 | return true; |
| 1012 | } |
| 1013 | |
| 1014 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1015 | |
| 1016 | // C++ [class.base.init]p2: |
| 1017 | // Names in a mem-initializer-id are looked up in the scope of the |
| 1018 | // constructor’s class and, if not found in that scope, are looked |
| 1019 | // up in the scope containing the constructor’s |
| 1020 | // definition. [Note: if the constructor’s class contains a member |
| 1021 | // with the same name as a direct or virtual base class of the |
| 1022 | // class, a mem-initializer-id naming the member or base class and |
| 1023 | // composed of a single identifier refers to the class member. A |
| 1024 | // mem-initializer-id for the hidden base class may be specified |
| 1025 | // using a qualified name. ] |
Fariborz Jahanian | c1fc3ec | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1026 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | 302bb66 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1027 | // Look for a member, first. |
| 1028 | FieldDecl *Member = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | DeclContext::lookup_result Result |
Fariborz Jahanian | 302bb66 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1030 | = ClassDecl->lookup(MemberOrBase); |
| 1031 | if (Result.first != Result.second) |
| 1032 | Member = dyn_cast<FieldDecl>(*Result.first); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1033 | |
Fariborz Jahanian | 302bb66 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1034 | // FIXME: Handle members of an anonymous union. |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1035 | |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1036 | if (Member) |
| 1037 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1038 | LParenLoc, RParenLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1039 | } |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1040 | // It didn't name a member, so see if it names a class. |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1041 | QualType BaseType; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1042 | TypeSourceInfo *TInfo = 0; |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1043 | |
| 1044 | if (TemplateTypeTy) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1045 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1046 | } else { |
| 1047 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1048 | LookupParsedName(R, S, &SS); |
| 1049 | |
| 1050 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1051 | if (!TyD) { |
| 1052 | if (R.isAmbiguous()) return true; |
| 1053 | |
John McCall | da6841b | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1054 | // We don't want access-control diagnostics here. |
| 1055 | R.suppressDiagnostics(); |
| 1056 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1057 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1058 | bool NotUnknownSpecialization = false; |
| 1059 | DeclContext *DC = computeDeclContext(SS, false); |
| 1060 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1061 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1062 | |
| 1063 | if (!NotUnknownSpecialization) { |
| 1064 | // When the scope specifier can refer to a member of an unknown |
| 1065 | // specialization, we take it as a type name. |
Douglas Gregor | bbdf20a | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 1066 | BaseType = CheckTypenameType(ETK_None, |
| 1067 | (NestedNameSpecifier *)SS.getScopeRep(), |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 1068 | *MemberOrBase, SourceLocation(), |
| 1069 | SS.getRange(), IdLoc); |
Douglas Gregor | 281c486 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1070 | if (BaseType.isNull()) |
| 1071 | return true; |
| 1072 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1073 | R.clear(); |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 1074 | R.setLookupName(MemberOrBase); |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1078 | // If no results were found, try to correct typos. |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1079 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1080 | CorrectTypo(R, S, &SS, ClassDecl, 0, CTC_NoKeywords) && |
| 1081 | R.isSingleResult()) { |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1082 | if (FieldDecl *Member = R.getAsSingle<FieldDecl>()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1083 | if (Member->getDeclContext()->getRedeclContext()->Equals(ClassDecl)) { |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1084 | // We have found a non-static data member with a similar |
| 1085 | // name to what was typed; complain and initialize that |
| 1086 | // member. |
| 1087 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1088 | << MemberOrBase << true << R.getLookupName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1089 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1090 | R.getLookupName().getAsString()); |
Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1091 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 1092 | << Member->getDeclName(); |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1093 | |
| 1094 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 1095 | LParenLoc, RParenLoc); |
| 1096 | } |
| 1097 | } else if (TypeDecl *Type = R.getAsSingle<TypeDecl>()) { |
| 1098 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1099 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1100 | if (FindBaseInitializer(*this, ClassDecl, |
| 1101 | Context.getTypeDeclType(Type), |
| 1102 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1103 | // We have found a direct or virtual base class with a |
| 1104 | // similar name to what was typed; complain and initialize |
| 1105 | // that base class. |
| 1106 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1107 | << MemberOrBase << false << R.getLookupName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1108 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1109 | R.getLookupName().getAsString()); |
Douglas Gregor | 43a0857 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1110 | |
| 1111 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1112 | : VirtualBaseSpec; |
| 1113 | Diag(BaseSpec->getSourceRange().getBegin(), |
| 1114 | diag::note_base_class_specified_here) |
| 1115 | << BaseSpec->getType() |
| 1116 | << BaseSpec->getSourceRange(); |
| 1117 | |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1118 | TyD = Type; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1123 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1124 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1125 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1126 | return true; |
| 1127 | } |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1130 | if (BaseType.isNull()) { |
| 1131 | BaseType = Context.getTypeDeclType(TyD); |
| 1132 | if (SS.isSet()) { |
| 1133 | NestedNameSpecifier *Qualifier = |
| 1134 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1135 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1136 | // FIXME: preserve source range information |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1137 | BaseType = Context.getElaboratedType(ETK_None, Qualifier, BaseType); |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1138 | } |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1139 | } |
| 1140 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1141 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1142 | if (!TInfo) |
| 1143 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1144 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1145 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1146 | LParenLoc, RParenLoc, ClassDecl); |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
John McCall | e22a04a | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1149 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1150 | /// containing the field that is being initialized. Returns true if there is an |
| 1151 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1152 | /// otherwise. |
Nick Lewycky | a2fb98b | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1153 | static bool InitExprContainsUninitializedFields(const Stmt *S, |
| 1154 | const FieldDecl *LhsField, |
| 1155 | SourceLocation *L) { |
| 1156 | if (isa<CallExpr>(S)) { |
| 1157 | // Do not descend into function calls or constructors, as the use |
| 1158 | // of an uninitialized field may be valid. One would have to inspect |
| 1159 | // the contents of the function/ctor to determine if it is safe or not. |
| 1160 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1161 | // may be safe, depending on what the function/ctor does. |
| 1162 | return false; |
| 1163 | } |
| 1164 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 1165 | const NamedDecl *RhsField = ME->getMemberDecl(); |
John McCall | e22a04a | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1166 | if (RhsField == LhsField) { |
| 1167 | // Initializing a field with itself. Throw a warning. |
| 1168 | // But wait; there are exceptions! |
| 1169 | // Exception #1: The field may not belong to this record. |
| 1170 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
Nick Lewycky | a2fb98b | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1171 | const Expr *base = ME->getBase(); |
John McCall | e22a04a | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1172 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1173 | // Even though the field matches, it does not belong to this record. |
| 1174 | return false; |
| 1175 | } |
| 1176 | // None of the exceptions triggered; return true to indicate an |
| 1177 | // uninitialized field was used. |
| 1178 | *L = ME->getMemberLoc(); |
| 1179 | return true; |
| 1180 | } |
Argyrios Kyrtzidis | 03f0e2b | 2010-09-21 10:47:20 +0000 | [diff] [blame] | 1181 | } else if (isa<SizeOfAlignOfExpr>(S)) { |
| 1182 | // sizeof/alignof doesn't reference contents, do not warn. |
| 1183 | return false; |
| 1184 | } else if (const UnaryOperator *UOE = dyn_cast<UnaryOperator>(S)) { |
| 1185 | // address-of doesn't reference contents (the pointer may be dereferenced |
| 1186 | // in the same expression but it would be rare; and weird). |
| 1187 | if (UOE->getOpcode() == UO_AddrOf) |
| 1188 | return false; |
John McCall | e22a04a | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1189 | } |
Nick Lewycky | a2fb98b | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1190 | for (Stmt::const_child_iterator it = S->child_begin(), e = S->child_end(); |
| 1191 | it != e; ++it) { |
| 1192 | if (!*it) { |
| 1193 | // An expression such as 'member(arg ?: "")' may trigger this. |
John McCall | e22a04a | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1194 | continue; |
| 1195 | } |
Nick Lewycky | a2fb98b | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1196 | if (InitExprContainsUninitializedFields(*it, LhsField, L)) |
| 1197 | return true; |
John McCall | e22a04a | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1198 | } |
Nick Lewycky | a2fb98b | 2010-06-15 07:32:55 +0000 | [diff] [blame] | 1199 | return false; |
John McCall | e22a04a | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1202 | MemInitResult |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1203 | Sema::BuildMemberInitializer(FieldDecl *Member, Expr **Args, |
| 1204 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1205 | SourceLocation LParenLoc, |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1206 | SourceLocation RParenLoc) { |
John McCall | e22a04a | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1207 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1208 | // foo(foo) |
| 1209 | // where foo is not also a parameter to the constructor. |
John McCall | c90f6d7 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1210 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | e22a04a | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1211 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1212 | SourceLocation L; |
| 1213 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1214 | // FIXME: Return true in the case when other fields are used before being |
| 1215 | // uninitialized. For example, let this field be the i'th field. When |
| 1216 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1217 | // fields are used, as they are not yet initialized. |
| 1218 | // Right now we are only handling the case where the i'th field uses |
| 1219 | // itself in its initializer. |
| 1220 | Diag(L, diag::warn_field_is_uninit); |
| 1221 | } |
| 1222 | } |
| 1223 | |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1224 | bool HasDependentArg = false; |
| 1225 | for (unsigned i = 0; i < NumArgs; i++) |
| 1226 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1227 | |
Eli Friedman | 9255adf | 2010-07-24 21:19:15 +0000 | [diff] [blame] | 1228 | if (Member->getType()->isDependentType() || HasDependentArg) { |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1229 | // Can't check initialization for a member of dependent type or when |
| 1230 | // any of the arguments are type-dependent expressions. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1231 | Expr *Init |
| 1232 | = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1233 | RParenLoc); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1234 | |
| 1235 | // Erase any temporaries within this evaluation context; we're not |
| 1236 | // going to track them in the AST, since we'll be rebuilding the |
| 1237 | // ASTs during template instantiation. |
| 1238 | ExprTemporaries.erase( |
| 1239 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1240 | ExprTemporaries.end()); |
| 1241 | |
| 1242 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1243 | LParenLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1244 | Init, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1245 | RParenLoc); |
| 1246 | |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1247 | } |
Anders Carlsson | 1fe64cb | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1248 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1249 | if (Member->isInvalidDecl()) |
| 1250 | return true; |
Anders Carlsson | 1fe64cb | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1251 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1252 | // Initialize the member. |
| 1253 | InitializedEntity MemberEntity = |
| 1254 | InitializedEntity::InitializeMember(Member, 0); |
| 1255 | InitializationKind Kind = |
| 1256 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc); |
| 1257 | |
| 1258 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 1259 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1260 | ExprResult MemberInit = |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1261 | InitSeq.Perform(*this, MemberEntity, Kind, |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1262 | MultiExprArg(*this, Args, NumArgs), 0); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1263 | if (MemberInit.isInvalid()) |
| 1264 | return true; |
| 1265 | |
| 1266 | // C++0x [class.base.init]p7: |
| 1267 | // The initialization of each base and member constitutes a |
| 1268 | // full-expression. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1269 | MemberInit = MaybeCreateCXXExprWithTemporaries(MemberInit.get()); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1270 | if (MemberInit.isInvalid()) |
| 1271 | return true; |
| 1272 | |
| 1273 | // If we are in a dependent context, template instantiation will |
| 1274 | // perform this type-checking again. Just save the arguments that we |
| 1275 | // received in a ParenListExpr. |
| 1276 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1277 | // of the information that we have about the member |
| 1278 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1279 | // and this approach is far more likely to get the corner cases right. |
| 1280 | if (CurContext->isDependentContext()) { |
| 1281 | // Bump the reference count of all of the arguments. |
| 1282 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1283 | Args[I]->Retain(); |
| 1284 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1285 | Expr *Init = new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1286 | RParenLoc); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1287 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1288 | LParenLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1289 | Init, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1290 | RParenLoc); |
| 1291 | } |
| 1292 | |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1293 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1294 | LParenLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1295 | MemberInit.get(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1296 | RParenLoc); |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1299 | MemInitResult |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1300 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1301 | Expr **Args, unsigned NumArgs, |
| 1302 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
| 1303 | CXXRecordDecl *ClassDecl) { |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1304 | bool HasDependentArg = false; |
| 1305 | for (unsigned i = 0; i < NumArgs; i++) |
| 1306 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1307 | |
Douglas Gregor | 1c69bf0 | 2010-06-16 16:03:14 +0000 | [diff] [blame] | 1308 | SourceLocation BaseLoc |
| 1309 | = BaseTInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1310 | |
| 1311 | if (!BaseType->isDependentType() && !BaseType->isRecordType()) |
| 1312 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 1313 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1314 | |
| 1315 | // C++ [class.base.init]p2: |
| 1316 | // [...] Unless the mem-initializer-id names a nonstatic data |
| 1317 | // member of the constructor’s class or a direct or virtual base |
| 1318 | // of that class, the mem-initializer is ill-formed. A |
| 1319 | // mem-initializer-list can initialize a base class using any |
| 1320 | // name that denotes that base class type. |
| 1321 | bool Dependent = BaseType->isDependentType() || HasDependentArg; |
| 1322 | |
| 1323 | // Check for direct and virtual base classes. |
| 1324 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1325 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1326 | if (!Dependent) { |
| 1327 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 1328 | VirtualBaseSpec); |
| 1329 | |
| 1330 | // C++ [base.class.init]p2: |
| 1331 | // Unless the mem-initializer-id names a nonstatic data member of the |
| 1332 | // constructor's class or a direct or virtual base of that class, the |
| 1333 | // mem-initializer is ill-formed. |
| 1334 | if (!DirectBaseSpec && !VirtualBaseSpec) { |
| 1335 | // If the class has any dependent bases, then it's possible that |
| 1336 | // one of those types will resolve to the same type as |
| 1337 | // BaseType. Therefore, just treat this as a dependent base |
| 1338 | // class initialization. FIXME: Should we try to check the |
| 1339 | // initialization anyway? It seems odd. |
| 1340 | if (ClassDecl->hasAnyDependentBases()) |
| 1341 | Dependent = true; |
| 1342 | else |
| 1343 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
| 1344 | << BaseType << Context.getTypeDeclType(ClassDecl) |
| 1345 | << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | if (Dependent) { |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1350 | // Can't check initialization for a base of dependent type or when |
| 1351 | // any of the arguments are type-dependent expressions. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1352 | ExprResult BaseInit |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1353 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1354 | RParenLoc)); |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1355 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1356 | // Erase any temporaries within this evaluation context; we're not |
| 1357 | // going to track them in the AST, since we'll be rebuilding the |
| 1358 | // ASTs during template instantiation. |
| 1359 | ExprTemporaries.erase( |
| 1360 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1361 | ExprTemporaries.end()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1363 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 1c0f8bb | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1364 | /*IsVirtual=*/false, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1365 | LParenLoc, |
| 1366 | BaseInit.takeAs<Expr>(), |
| 1367 | RParenLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1368 | } |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1369 | |
| 1370 | // C++ [base.class.init]p2: |
| 1371 | // If a mem-initializer-id is ambiguous because it designates both |
| 1372 | // a direct non-virtual base class and an inherited virtual base |
| 1373 | // class, the mem-initializer is ill-formed. |
| 1374 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1375 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1376 | << BaseType << BaseTInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1377 | |
| 1378 | CXXBaseSpecifier *BaseSpec |
| 1379 | = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
| 1380 | if (!BaseSpec) |
| 1381 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 1382 | |
| 1383 | // Initialize the base. |
| 1384 | InitializedEntity BaseEntity = |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1385 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1386 | InitializationKind Kind = |
| 1387 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc); |
| 1388 | |
| 1389 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 1390 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1391 | ExprResult BaseInit = |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1392 | InitSeq.Perform(*this, BaseEntity, Kind, |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1393 | MultiExprArg(*this, Args, NumArgs), 0); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1394 | if (BaseInit.isInvalid()) |
| 1395 | return true; |
| 1396 | |
| 1397 | // C++0x [class.base.init]p7: |
| 1398 | // The initialization of each base and member constitutes a |
| 1399 | // full-expression. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1400 | BaseInit = MaybeCreateCXXExprWithTemporaries(BaseInit.get()); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1401 | if (BaseInit.isInvalid()) |
| 1402 | return true; |
| 1403 | |
| 1404 | // If we are in a dependent context, template instantiation will |
| 1405 | // perform this type-checking again. Just save the arguments that we |
| 1406 | // received in a ParenListExpr. |
| 1407 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1408 | // of the information that we have about the base |
| 1409 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1410 | // and this approach is far more likely to get the corner cases right. |
| 1411 | if (CurContext->isDependentContext()) { |
| 1412 | // Bump the reference count of all of the arguments. |
| 1413 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1414 | Args[I]->Retain(); |
| 1415 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1416 | ExprResult Init |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1417 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1418 | RParenLoc)); |
| 1419 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 1c0f8bb | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1420 | BaseSpec->isVirtual(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1421 | LParenLoc, |
| 1422 | Init.takeAs<Expr>(), |
| 1423 | RParenLoc); |
| 1424 | } |
| 1425 | |
| 1426 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 1c0f8bb | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1427 | BaseSpec->isVirtual(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1428 | LParenLoc, |
| 1429 | BaseInit.takeAs<Expr>(), |
| 1430 | RParenLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1433 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 1434 | /// initialize its base or member. |
| 1435 | enum ImplicitInitializerKind { |
| 1436 | IIK_Default, |
| 1437 | IIK_Copy, |
| 1438 | IIK_Move |
| 1439 | }; |
| 1440 | |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1441 | static bool |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1442 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1443 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1444 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1445 | bool IsInheritedVirtualBase, |
| 1446 | CXXBaseOrMemberInitializer *&CXXBaseInit) { |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1447 | InitializedEntity InitEntity |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1448 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 1449 | IsInheritedVirtualBase); |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1450 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1451 | ExprResult BaseInit; |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1452 | |
| 1453 | switch (ImplicitInitKind) { |
| 1454 | case IIK_Default: { |
| 1455 | InitializationKind InitKind |
| 1456 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 1457 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1458 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1459 | MultiExprArg(SemaRef, 0, 0)); |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1460 | break; |
| 1461 | } |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1462 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1463 | case IIK_Copy: { |
| 1464 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1465 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1466 | |
| 1467 | Expr *CopyCtorArg = |
| 1468 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
Douglas Gregor | ed088b7 | 2010-05-03 15:43:53 +0000 | [diff] [blame] | 1469 | Constructor->getLocation(), ParamType, 0); |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1470 | |
Anders Carlsson | af13c7b | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1471 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 1472 | QualType ArgTy = |
| 1473 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 1474 | ParamType.getQualifiers()); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1475 | |
| 1476 | CXXCastPath BasePath; |
| 1477 | BasePath.push_back(BaseSpec); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1478 | SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1479 | CK_UncheckedDerivedToBase, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1480 | VK_LValue, &BasePath); |
Anders Carlsson | af13c7b | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1481 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1482 | InitializationKind InitKind |
| 1483 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1484 | SourceLocation(), SourceLocation()); |
| 1485 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1486 | &CopyCtorArg, 1); |
| 1487 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1488 | MultiExprArg(&CopyCtorArg, 1)); |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1489 | break; |
| 1490 | } |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1491 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1492 | case IIK_Move: |
| 1493 | assert(false && "Unhandled initializer kind!"); |
| 1494 | } |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1495 | |
| 1496 | if (BaseInit.isInvalid()) |
| 1497 | return true; |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1498 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1499 | BaseInit = SemaRef.MaybeCreateCXXExprWithTemporaries(BaseInit.get()); |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1500 | if (BaseInit.isInvalid()) |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1501 | return true; |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1502 | |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1503 | CXXBaseInit = |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1504 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
| 1505 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 1506 | SourceLocation()), |
| 1507 | BaseSpec->isVirtual(), |
| 1508 | SourceLocation(), |
| 1509 | BaseInit.takeAs<Expr>(), |
| 1510 | SourceLocation()); |
| 1511 | |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1512 | return false; |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1515 | static bool |
| 1516 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1517 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1518 | FieldDecl *Field, |
| 1519 | CXXBaseOrMemberInitializer *&CXXMemberInit) { |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 1520 | if (Field->isInvalidDecl()) |
| 1521 | return true; |
| 1522 | |
Chandler Carruth | 9c9286b | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1523 | SourceLocation Loc = Constructor->getLocation(); |
| 1524 | |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1525 | if (ImplicitInitKind == IIK_Copy) { |
| 1526 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1527 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1528 | |
| 1529 | Expr *MemberExprBase = |
| 1530 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1531 | Loc, ParamType, 0); |
| 1532 | |
| 1533 | // Build a reference to this field within the parameter. |
| 1534 | CXXScopeSpec SS; |
| 1535 | LookupResult MemberLookup(SemaRef, Field->getDeclName(), Loc, |
| 1536 | Sema::LookupMemberName); |
| 1537 | MemberLookup.addDecl(Field, AS_public); |
| 1538 | MemberLookup.resolveKind(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1539 | ExprResult CopyCtorArg |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1540 | = SemaRef.BuildMemberReferenceExpr(MemberExprBase, |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1541 | ParamType, Loc, |
| 1542 | /*IsArrow=*/false, |
| 1543 | SS, |
| 1544 | /*FirstQualifierInScope=*/0, |
| 1545 | MemberLookup, |
| 1546 | /*TemplateArgs=*/0); |
| 1547 | if (CopyCtorArg.isInvalid()) |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1548 | return true; |
| 1549 | |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1550 | // When the field we are copying is an array, create index variables for |
| 1551 | // each dimension of the array. We use these index variables to subscript |
| 1552 | // the source array, and other clients (e.g., CodeGen) will perform the |
| 1553 | // necessary iteration with these index variables. |
| 1554 | llvm::SmallVector<VarDecl *, 4> IndexVariables; |
| 1555 | QualType BaseType = Field->getType(); |
| 1556 | QualType SizeType = SemaRef.Context.getSizeType(); |
| 1557 | while (const ConstantArrayType *Array |
| 1558 | = SemaRef.Context.getAsConstantArrayType(BaseType)) { |
| 1559 | // Create the iteration variable for this array index. |
| 1560 | IdentifierInfo *IterationVarName = 0; |
| 1561 | { |
| 1562 | llvm::SmallString<8> Str; |
| 1563 | llvm::raw_svector_ostream OS(Str); |
| 1564 | OS << "__i" << IndexVariables.size(); |
| 1565 | IterationVarName = &SemaRef.Context.Idents.get(OS.str()); |
| 1566 | } |
| 1567 | VarDecl *IterationVar |
| 1568 | = VarDecl::Create(SemaRef.Context, SemaRef.CurContext, Loc, |
| 1569 | IterationVarName, SizeType, |
| 1570 | SemaRef.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1571 | SC_None, SC_None); |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1572 | IndexVariables.push_back(IterationVar); |
| 1573 | |
| 1574 | // Create a reference to the iteration variable. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1575 | ExprResult IterationVarRef |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1576 | = SemaRef.BuildDeclRefExpr(IterationVar, SizeType, Loc); |
| 1577 | assert(!IterationVarRef.isInvalid() && |
| 1578 | "Reference to invented variable cannot fail!"); |
| 1579 | |
| 1580 | // Subscript the array with this iteration variable. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1581 | CopyCtorArg = SemaRef.CreateBuiltinArraySubscriptExpr(CopyCtorArg.take(), |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1582 | Loc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1583 | IterationVarRef.take(), |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1584 | Loc); |
| 1585 | if (CopyCtorArg.isInvalid()) |
| 1586 | return true; |
| 1587 | |
| 1588 | BaseType = Array->getElementType(); |
| 1589 | } |
| 1590 | |
| 1591 | // Construct the entity that we will be initializing. For an array, this |
| 1592 | // will be first element in the array, which may require several levels |
| 1593 | // of array-subscript entities. |
| 1594 | llvm::SmallVector<InitializedEntity, 4> Entities; |
| 1595 | Entities.reserve(1 + IndexVariables.size()); |
| 1596 | Entities.push_back(InitializedEntity::InitializeMember(Field)); |
| 1597 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 1598 | Entities.push_back(InitializedEntity::InitializeElement(SemaRef.Context, |
| 1599 | 0, |
| 1600 | Entities.back())); |
| 1601 | |
| 1602 | // Direct-initialize to use the copy constructor. |
| 1603 | InitializationKind InitKind = |
| 1604 | InitializationKind::CreateDirect(Loc, SourceLocation(), SourceLocation()); |
| 1605 | |
| 1606 | Expr *CopyCtorArgE = CopyCtorArg.takeAs<Expr>(); |
| 1607 | InitializationSequence InitSeq(SemaRef, Entities.back(), InitKind, |
| 1608 | &CopyCtorArgE, 1); |
| 1609 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1610 | ExprResult MemberInit |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1611 | = InitSeq.Perform(SemaRef, Entities.back(), InitKind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1612 | MultiExprArg(&CopyCtorArgE, 1)); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1613 | MemberInit = SemaRef.MaybeCreateCXXExprWithTemporaries(MemberInit.get()); |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1614 | if (MemberInit.isInvalid()) |
| 1615 | return true; |
| 1616 | |
| 1617 | CXXMemberInit |
| 1618 | = CXXBaseOrMemberInitializer::Create(SemaRef.Context, Field, Loc, Loc, |
| 1619 | MemberInit.takeAs<Expr>(), Loc, |
| 1620 | IndexVariables.data(), |
| 1621 | IndexVariables.size()); |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1622 | return false; |
| 1623 | } |
| 1624 | |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1625 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 1626 | |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1627 | QualType FieldBaseElementType = |
| 1628 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 1629 | |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1630 | if (FieldBaseElementType->isRecordType()) { |
| 1631 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1632 | InitializationKind InitKind = |
Chandler Carruth | 9c9286b | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1633 | InitializationKind::CreateDefault(Loc); |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1634 | |
| 1635 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1636 | ExprResult MemberInit = |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1637 | InitSeq.Perform(SemaRef, InitEntity, InitKind, MultiExprArg()); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1638 | if (MemberInit.isInvalid()) |
| 1639 | return true; |
| 1640 | |
| 1641 | MemberInit = SemaRef.MaybeCreateCXXExprWithTemporaries(MemberInit.get()); |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1642 | if (MemberInit.isInvalid()) |
| 1643 | return true; |
| 1644 | |
| 1645 | CXXMemberInit = |
| 1646 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
Chandler Carruth | 9c9286b | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1647 | Field, Loc, Loc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1648 | MemberInit.get(), |
Chandler Carruth | 9c9286b | 2010-06-29 23:50:44 +0000 | [diff] [blame] | 1649 | Loc); |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1650 | return false; |
| 1651 | } |
Anders Carlsson | dca6be0 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 1652 | |
| 1653 | if (FieldBaseElementType->isReferenceType()) { |
| 1654 | SemaRef.Diag(Constructor->getLocation(), |
| 1655 | diag::err_uninitialized_member_in_ctor) |
| 1656 | << (int)Constructor->isImplicit() |
| 1657 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1658 | << 0 << Field->getDeclName(); |
| 1659 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1660 | return true; |
| 1661 | } |
| 1662 | |
| 1663 | if (FieldBaseElementType.isConstQualified()) { |
| 1664 | SemaRef.Diag(Constructor->getLocation(), |
| 1665 | diag::err_uninitialized_member_in_ctor) |
| 1666 | << (int)Constructor->isImplicit() |
| 1667 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1668 | << 1 << Field->getDeclName(); |
| 1669 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1670 | return true; |
| 1671 | } |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1672 | |
| 1673 | // Nothing to initialize. |
| 1674 | CXXMemberInit = 0; |
| 1675 | return false; |
| 1676 | } |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1677 | |
| 1678 | namespace { |
| 1679 | struct BaseAndFieldInfo { |
| 1680 | Sema &S; |
| 1681 | CXXConstructorDecl *Ctor; |
| 1682 | bool AnyErrorsInInits; |
| 1683 | ImplicitInitializerKind IIK; |
| 1684 | llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; |
| 1685 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 8> AllToInit; |
| 1686 | |
| 1687 | BaseAndFieldInfo(Sema &S, CXXConstructorDecl *Ctor, bool ErrorsInInits) |
| 1688 | : S(S), Ctor(Ctor), AnyErrorsInInits(ErrorsInInits) { |
| 1689 | // FIXME: Handle implicit move constructors. |
| 1690 | if (Ctor->isImplicit() && Ctor->isCopyConstructor()) |
| 1691 | IIK = IIK_Copy; |
| 1692 | else |
| 1693 | IIK = IIK_Default; |
| 1694 | } |
| 1695 | }; |
| 1696 | } |
| 1697 | |
Chandler Carruth | 139e962 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1698 | static void RecordFieldInitializer(BaseAndFieldInfo &Info, |
| 1699 | FieldDecl *Top, FieldDecl *Field, |
| 1700 | CXXBaseOrMemberInitializer *Init) { |
| 1701 | // If the member doesn't need to be initialized, Init will still be null. |
| 1702 | if (!Init) |
| 1703 | return; |
| 1704 | |
| 1705 | Info.AllToInit.push_back(Init); |
| 1706 | if (Field != Top) { |
| 1707 | Init->setMember(Top); |
| 1708 | Init->setAnonUnionMember(Field); |
| 1709 | } |
| 1710 | } |
| 1711 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1712 | static bool CollectFieldInitializer(BaseAndFieldInfo &Info, |
| 1713 | FieldDecl *Top, FieldDecl *Field) { |
| 1714 | |
Chandler Carruth | 139e962 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1715 | // Overwhelmingly common case: we have a direct initializer for this field. |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1716 | if (CXXBaseOrMemberInitializer *Init = Info.AllBaseFields.lookup(Field)) { |
Chandler Carruth | 139e962 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1717 | RecordFieldInitializer(Info, Top, Field, Init); |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1718 | return false; |
| 1719 | } |
| 1720 | |
| 1721 | if (Info.IIK == IIK_Default && Field->isAnonymousStructOrUnion()) { |
| 1722 | const RecordType *FieldClassType = Field->getType()->getAs<RecordType>(); |
| 1723 | assert(FieldClassType && "anonymous struct/union without record type"); |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1724 | CXXRecordDecl *FieldClassDecl |
| 1725 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Chandler Carruth | 139e962 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1726 | |
| 1727 | // Even though union members never have non-trivial default |
| 1728 | // constructions in C++03, we still build member initializers for aggregate |
| 1729 | // record types which can be union members, and C++0x allows non-trivial |
| 1730 | // default constructors for union members, so we ensure that only one |
| 1731 | // member is initialized for these. |
| 1732 | if (FieldClassDecl->isUnion()) { |
| 1733 | // First check for an explicit initializer for one field. |
| 1734 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 1735 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1736 | if (CXXBaseOrMemberInitializer *Init = Info.AllBaseFields.lookup(*FA)) { |
| 1737 | RecordFieldInitializer(Info, Top, *FA, Init); |
| 1738 | |
| 1739 | // Once we've initialized a field of an anonymous union, the union |
| 1740 | // field in the class is also initialized, so exit immediately. |
| 1741 | return false; |
Argyrios Kyrtzidis | a3ae3eb | 2010-08-16 17:27:13 +0000 | [diff] [blame] | 1742 | } else if ((*FA)->isAnonymousStructOrUnion()) { |
| 1743 | if (CollectFieldInitializer(Info, Top, *FA)) |
| 1744 | return true; |
Chandler Carruth | 139e962 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | // Fallthrough and construct a default initializer for the union as |
| 1749 | // a whole, which can call its default constructor if such a thing exists |
| 1750 | // (C++0x perhaps). FIXME: It's not clear that this is the correct |
| 1751 | // behavior going forward with C++0x, when anonymous unions there are |
| 1752 | // finalized, we should revisit this. |
| 1753 | } else { |
| 1754 | // For structs, we simply descend through to initialize all members where |
| 1755 | // necessary. |
| 1756 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
| 1757 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1758 | if (CollectFieldInitializer(Info, Top, *FA)) |
| 1759 | return true; |
| 1760 | } |
| 1761 | } |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | // Don't try to build an implicit initializer if there were semantic |
| 1765 | // errors in any of the initializers (and therefore we might be |
| 1766 | // missing some that the user actually wrote). |
| 1767 | if (Info.AnyErrorsInInits) |
| 1768 | return false; |
| 1769 | |
| 1770 | CXXBaseOrMemberInitializer *Init = 0; |
| 1771 | if (BuildImplicitMemberInitializer(Info.S, Info.Ctor, Info.IIK, Field, Init)) |
| 1772 | return true; |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1773 | |
Chandler Carruth | 139e962 | 2010-06-30 02:59:29 +0000 | [diff] [blame] | 1774 | RecordFieldInitializer(Info, Top, Field, Init); |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1775 | return false; |
| 1776 | } |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1777 | |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1778 | bool |
Anders Carlsson | 561f793 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1779 | Sema::SetBaseOrMemberInitializers(CXXConstructorDecl *Constructor, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1780 | CXXBaseOrMemberInitializer **Initializers, |
| 1781 | unsigned NumInitializers, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1782 | bool AnyErrors) { |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1783 | if (Constructor->getDeclContext()->isDependentContext()) { |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1784 | // Just store the initializers as written, they will be checked during |
| 1785 | // instantiation. |
| 1786 | if (NumInitializers > 0) { |
| 1787 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1788 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1789 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
| 1790 | memcpy(baseOrMemberInitializers, Initializers, |
| 1791 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
| 1792 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
| 1793 | } |
| 1794 | |
| 1795 | return false; |
| 1796 | } |
| 1797 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1798 | BaseAndFieldInfo Info(*this, Constructor, AnyErrors); |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1799 | |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1800 | // We need to build the initializer AST according to order of construction |
| 1801 | // and not what user specified in the Initializers list. |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1802 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | c14922f | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 1803 | if (!ClassDecl) |
| 1804 | return true; |
| 1805 | |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1806 | bool HadError = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1807 | |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1808 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1809 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1810 | |
| 1811 | if (Member->isBaseInitializer()) |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1812 | Info.AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1813 | else |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1814 | Info.AllBaseFields[Member->getMember()] = Member; |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1817 | // Keep track of the direct virtual bases. |
| 1818 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 1819 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 1820 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 1821 | if (I->isVirtual()) |
| 1822 | DirectVBases.insert(I); |
| 1823 | } |
| 1824 | |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1825 | // Push virtual bases before others. |
| 1826 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 1827 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 1828 | |
| 1829 | if (CXXBaseOrMemberInitializer *Value |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1830 | = Info.AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 1831 | Info.AllToInit.push_back(Value); |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1832 | } else if (!AnyErrors) { |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1833 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1834 | CXXBaseOrMemberInitializer *CXXBaseInit; |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1835 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1836 | VBase, IsInheritedVirtualBase, |
| 1837 | CXXBaseInit)) { |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1838 | HadError = true; |
| 1839 | continue; |
| 1840 | } |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1841 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1842 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1843 | } |
| 1844 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1846 | // Non-virtual bases. |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1847 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1848 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1849 | // Virtuals are in the virtual base list and already constructed. |
| 1850 | if (Base->isVirtual()) |
| 1851 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1853 | if (CXXBaseOrMemberInitializer *Value |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1854 | = Info.AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 1855 | Info.AllToInit.push_back(Value); |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1856 | } else if (!AnyErrors) { |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1857 | CXXBaseOrMemberInitializer *CXXBaseInit; |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1858 | if (BuildImplicitBaseInitializer(*this, Constructor, Info.IIK, |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1859 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1860 | CXXBaseInit)) { |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1861 | HadError = true; |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1862 | continue; |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1863 | } |
Fariborz Jahanian | 59a1cd4 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1864 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1865 | Info.AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1866 | } |
| 1867 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1869 | // Fields. |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1870 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
Fariborz Jahanian | 1138ba6 | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 1871 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 1872 | if ((*Field)->getType()->isIncompleteArrayType()) { |
| 1873 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 1874 | "Incomplete array type is not valid"); |
| 1875 | continue; |
| 1876 | } |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1877 | if (CollectFieldInitializer(Info, *Field, *Field)) |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1878 | HadError = true; |
Fariborz Jahanian | 1138ba6 | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 1879 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1880 | |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1881 | NumInitializers = Info.AllToInit.size(); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1882 | if (NumInitializers > 0) { |
| 1883 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1884 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1885 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
John McCall | bc83b3f | 2010-05-20 23:23:51 +0000 | [diff] [blame] | 1886 | memcpy(baseOrMemberInitializers, Info.AllToInit.data(), |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1887 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1888 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
Rafael Espindola | 13327bb | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 1889 | |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1890 | // Constructors implicitly reference the base and member |
| 1891 | // destructors. |
| 1892 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 1893 | Constructor->getParent()); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1894 | } |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1895 | |
| 1896 | return HadError; |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1899 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 1900 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1901 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1902 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 1903 | return static_cast<void *>(RT->getDecl()); |
| 1904 | } |
| 1905 | return static_cast<void *>(Field); |
| 1906 | } |
| 1907 | |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1908 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 1909 | return Context.getCanonicalType(BaseType).getTypePtr(); |
Anders Carlsson | bcec05c | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1912 | static void *GetKeyForMember(ASTContext &Context, |
| 1913 | CXXBaseOrMemberInitializer *Member, |
Anders Carlsson | bcec05c | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1914 | bool MemberMaybeAnon = false) { |
Anders Carlsson | a942dcd | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1915 | if (!Member->isMemberInitializer()) |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1916 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | a942dcd | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1917 | |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1918 | // For fields injected into the class via declaration of an anonymous union, |
| 1919 | // use its anonymous union class declaration as the unique key. |
Anders Carlsson | a942dcd | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1920 | FieldDecl *Field = Member->getMember(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1921 | |
Anders Carlsson | a942dcd | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1922 | // After SetBaseOrMemberInitializers call, Field is the anonymous union |
| 1923 | // data member of the class. Data member used in the initializer list is |
| 1924 | // in AnonUnionMember field. |
| 1925 | if (MemberMaybeAnon && Field->isAnonymousStructOrUnion()) |
| 1926 | Field = Member->getAnonUnionMember(); |
Anders Carlsson | 83ac312 | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1927 | |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1928 | // If the field is a member of an anonymous struct or union, our key |
| 1929 | // is the anonymous record decl that's a direct child of the class. |
Anders Carlsson | 83ac312 | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1930 | RecordDecl *RD = Field->getParent(); |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1931 | if (RD->isAnonymousStructOrUnion()) { |
| 1932 | while (true) { |
| 1933 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 1934 | if (Parent->isAnonymousStructOrUnion()) |
| 1935 | RD = Parent; |
| 1936 | else |
| 1937 | break; |
| 1938 | } |
| 1939 | |
Anders Carlsson | 83ac312 | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1940 | return static_cast<void *>(RD); |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1941 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | |
Anders Carlsson | a942dcd | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1943 | return static_cast<void *>(Field); |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1946 | static void |
| 1947 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 96b8fc6 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1948 | const CXXConstructorDecl *Constructor, |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1949 | CXXBaseOrMemberInitializer **Inits, |
| 1950 | unsigned NumInits) { |
| 1951 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 35d6e3e | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1952 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1953 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1954 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order) |
| 1955 | == Diagnostic::Ignored) |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1956 | return; |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1957 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1958 | // Build the list of bases and members in the order that they'll |
| 1959 | // actually be initialized. The explicit initializers should be in |
| 1960 | // this same order but may be missing things. |
| 1961 | llvm::SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | |
Anders Carlsson | 96b8fc6 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1963 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1964 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1965 | // 1. Virtual bases. |
Anders Carlsson | 96b8fc6 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1966 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1967 | ClassDecl->vbases_begin(), |
| 1968 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1969 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1971 | // 2. Non-virtual bases. |
Anders Carlsson | 96b8fc6 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1972 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1973 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1974 | if (Base->isVirtual()) |
| 1975 | continue; |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1976 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1977 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1979 | // 3. Direct fields. |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1980 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1981 | E = ClassDecl->field_end(); Field != E; ++Field) |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1982 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1984 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 1985 | unsigned IdealIndex = 0; |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1986 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1987 | CXXBaseOrMemberInitializer *PrevInit = 0; |
| 1988 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
| 1989 | CXXBaseOrMemberInitializer *Init = Inits[InitIndex]; |
| 1990 | void *InitKey = GetKeyForMember(SemaRef.Context, Init, true); |
| 1991 | |
| 1992 | // Scan forward to try to find this initializer in the idealized |
| 1993 | // initializers list. |
| 1994 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 1995 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1996 | break; |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1997 | |
| 1998 | // If we didn't find this initializer, it must be because we |
| 1999 | // scanned past it on a previous iteration. That can only |
| 2000 | // happen if we're out of order; emit a warning. |
Douglas Gregor | aabdfcb | 2010-05-20 23:49:34 +0000 | [diff] [blame] | 2001 | if (IdealIndex == NumIdealInits && PrevInit) { |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2002 | Sema::SemaDiagnosticBuilder D = |
| 2003 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 2004 | diag::warn_initializer_out_of_order); |
| 2005 | |
| 2006 | if (PrevInit->isMemberInitializer()) |
| 2007 | D << 0 << PrevInit->getMember()->getDeclName(); |
| 2008 | else |
| 2009 | D << 1 << PrevInit->getBaseClassInfo()->getType(); |
| 2010 | |
| 2011 | if (Init->isMemberInitializer()) |
| 2012 | D << 0 << Init->getMember()->getDeclName(); |
| 2013 | else |
| 2014 | D << 1 << Init->getBaseClassInfo()->getType(); |
| 2015 | |
| 2016 | // Move back to the initializer's location in the ideal list. |
| 2017 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 2018 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 2019 | break; |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2020 | |
| 2021 | assert(IdealIndex != NumIdealInits && |
| 2022 | "initializer not found in initializer list"); |
Fariborz Jahanian | 341583c | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2023 | } |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 2024 | |
| 2025 | PrevInit = Init; |
Fariborz Jahanian | 341583c | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 2026 | } |
Anders Carlsson | 75fdaa4 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2029 | namespace { |
| 2030 | bool CheckRedundantInit(Sema &S, |
| 2031 | CXXBaseOrMemberInitializer *Init, |
| 2032 | CXXBaseOrMemberInitializer *&PrevInit) { |
| 2033 | if (!PrevInit) { |
| 2034 | PrevInit = Init; |
| 2035 | return false; |
| 2036 | } |
| 2037 | |
| 2038 | if (FieldDecl *Field = Init->getMember()) |
| 2039 | S.Diag(Init->getSourceLocation(), |
| 2040 | diag::err_multiple_mem_initialization) |
| 2041 | << Field->getDeclName() |
| 2042 | << Init->getSourceRange(); |
| 2043 | else { |
| 2044 | Type *BaseClass = Init->getBaseClass(); |
| 2045 | assert(BaseClass && "neither field nor base"); |
| 2046 | S.Diag(Init->getSourceLocation(), |
| 2047 | diag::err_multiple_base_initialization) |
| 2048 | << QualType(BaseClass, 0) |
| 2049 | << Init->getSourceRange(); |
| 2050 | } |
| 2051 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 2052 | << 0 << PrevInit->getSourceRange(); |
| 2053 | |
| 2054 | return true; |
| 2055 | } |
| 2056 | |
| 2057 | typedef std::pair<NamedDecl *, CXXBaseOrMemberInitializer *> UnionEntry; |
| 2058 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 2059 | |
| 2060 | bool CheckRedundantUnionInit(Sema &S, |
| 2061 | CXXBaseOrMemberInitializer *Init, |
| 2062 | RedundantUnionMap &Unions) { |
| 2063 | FieldDecl *Field = Init->getMember(); |
| 2064 | RecordDecl *Parent = Field->getParent(); |
| 2065 | if (!Parent->isAnonymousStructOrUnion()) |
| 2066 | return false; |
| 2067 | |
| 2068 | NamedDecl *Child = Field; |
| 2069 | do { |
| 2070 | if (Parent->isUnion()) { |
| 2071 | UnionEntry &En = Unions[Parent]; |
| 2072 | if (En.first && En.first != Child) { |
| 2073 | S.Diag(Init->getSourceLocation(), |
| 2074 | diag::err_multiple_mem_union_initialization) |
| 2075 | << Field->getDeclName() |
| 2076 | << Init->getSourceRange(); |
| 2077 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 2078 | << 0 << En.second->getSourceRange(); |
| 2079 | return true; |
| 2080 | } else if (!En.first) { |
| 2081 | En.first = Child; |
| 2082 | En.second = Init; |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | Child = Parent; |
| 2087 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 2088 | } while (Parent->isAnonymousStructOrUnion()); |
| 2089 | |
| 2090 | return false; |
| 2091 | } |
| 2092 | } |
| 2093 | |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2094 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2095 | void Sema::ActOnMemInitializers(Decl *ConstructorDecl, |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2096 | SourceLocation ColonLoc, |
| 2097 | MemInitTy **meminits, unsigned NumMemInits, |
| 2098 | bool AnyErrors) { |
| 2099 | if (!ConstructorDecl) |
| 2100 | return; |
| 2101 | |
| 2102 | AdjustDeclIfTemplate(ConstructorDecl); |
| 2103 | |
| 2104 | CXXConstructorDecl *Constructor |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2105 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl); |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2106 | |
| 2107 | if (!Constructor) { |
| 2108 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 2109 | return; |
| 2110 | } |
| 2111 | |
| 2112 | CXXBaseOrMemberInitializer **MemInits = |
| 2113 | reinterpret_cast<CXXBaseOrMemberInitializer **>(meminits); |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2114 | |
| 2115 | // Mapping for the duplicate initializers check. |
| 2116 | // For member initializers, this is keyed with a FieldDecl*. |
| 2117 | // For base initializers, this is keyed with a Type*. |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2118 | llvm::DenseMap<void*, CXXBaseOrMemberInitializer *> Members; |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2119 | |
| 2120 | // Mapping for the inconsistent anonymous-union initializers check. |
| 2121 | RedundantUnionMap MemberUnions; |
| 2122 | |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2123 | bool HadError = false; |
| 2124 | for (unsigned i = 0; i < NumMemInits; i++) { |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2125 | CXXBaseOrMemberInitializer *Init = MemInits[i]; |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2126 | |
Abramo Bagnara | 341d783 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 2127 | // Set the source order index. |
| 2128 | Init->setSourceOrder(i); |
| 2129 | |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2130 | if (Init->isMemberInitializer()) { |
| 2131 | FieldDecl *Field = Init->getMember(); |
| 2132 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 2133 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 2134 | HadError = true; |
| 2135 | } else { |
| 2136 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 2137 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 2138 | HadError = true; |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2139 | } |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2142 | if (HadError) |
| 2143 | return; |
| 2144 | |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2145 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | 4c8cb01 | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2146 | |
| 2147 | SetBaseOrMemberInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2150 | void |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2151 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 2152 | CXXRecordDecl *ClassDecl) { |
| 2153 | // Ignore dependent contexts. |
| 2154 | if (ClassDecl->isDependentContext()) |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2155 | return; |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2156 | |
| 2157 | // FIXME: all the access-control diagnostics are positioned on the |
| 2158 | // field/base declaration. That's probably good; that said, the |
| 2159 | // user might reasonably want to know why the destructor is being |
| 2160 | // emitted, and we currently don't say. |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2161 | |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2162 | // Non-static data members. |
| 2163 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 2164 | E = ClassDecl->field_end(); I != E; ++I) { |
| 2165 | FieldDecl *Field = *I; |
Fariborz Jahanian | 16f94c6 | 2010-05-17 18:15:18 +0000 | [diff] [blame] | 2166 | if (Field->isInvalidDecl()) |
| 2167 | continue; |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2168 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 2169 | |
| 2170 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 2171 | if (!RT) |
| 2172 | continue; |
| 2173 | |
| 2174 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 2175 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2176 | continue; |
| 2177 | |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2178 | CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2179 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2180 | PDiag(diag::err_access_dtor_field) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2181 | << Field->getDeclName() |
| 2182 | << FieldType); |
| 2183 | |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2184 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2187 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 2188 | |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2189 | // Bases. |
| 2190 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2191 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2192 | // Bases are always records in a well-formed non-dependent class. |
| 2193 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 2194 | |
| 2195 | // Remember direct virtual bases. |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2196 | if (Base->isVirtual()) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2197 | DirectVirtualBases.insert(RT); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2198 | |
| 2199 | // Ignore trivial destructors. |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2200 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2201 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2202 | continue; |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2203 | |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2204 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2205 | |
| 2206 | // FIXME: caret should be on the start of the class name |
| 2207 | CheckDestructorAccess(Base->getSourceRange().getBegin(), Dtor, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2208 | PDiag(diag::err_access_dtor_base) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2209 | << Base->getType() |
| 2210 | << Base->getSourceRange()); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2211 | |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2212 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | // Virtual bases. |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2216 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2217 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2218 | |
| 2219 | // Bases are always records in a well-formed non-dependent class. |
| 2220 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 2221 | |
| 2222 | // Ignore direct virtual bases. |
| 2223 | if (DirectVirtualBases.count(RT)) |
| 2224 | continue; |
| 2225 | |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2226 | // Ignore trivial destructors. |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2227 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2228 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2229 | continue; |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2230 | |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 2231 | CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2232 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2233 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2234 | << VBase->getType()); |
| 2235 | |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2236 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2237 | } |
| 2238 | } |
| 2239 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2240 | void Sema::ActOnDefaultCtorInitializers(Decl *CDtorDecl) { |
Fariborz Jahanian | 16094c2 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2241 | if (!CDtorDecl) |
Fariborz Jahanian | 49c8179 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2242 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2244 | if (CXXConstructorDecl *Constructor |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2245 | = dyn_cast<CXXConstructorDecl>(CDtorDecl)) |
Anders Carlsson | 4c8cb01 | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2246 | SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | 49c8179 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2250 | unsigned DiagID, AbstractDiagSelID SelID) { |
Anders Carlsson | eabf770 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2251 | if (SelID == -1) |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2252 | return RequireNonAbstractType(Loc, T, PDiag(DiagID)); |
Anders Carlsson | eabf770 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2253 | else |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2254 | return RequireNonAbstractType(Loc, T, PDiag(DiagID) << SelID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Anders Carlsson | eabf770 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2257 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2258 | const PartialDiagnostic &PD) { |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2259 | if (!getLangOptions().CPlusPlus) |
| 2260 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2261 | |
Anders Carlsson | eb0c532 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 2262 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2263 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2264 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2265 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2266 | // Find the innermost pointer type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2267 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2268 | PT = T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2269 | |
Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2270 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2271 | return RequireNonAbstractType(Loc, AT->getElementType(), PD); |
Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2272 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2273 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2274 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2275 | if (!RT) |
| 2276 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2277 | |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2278 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2279 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2280 | // We can't answer whether something is abstract until it has a |
| 2281 | // definition. If it's currently being defined, we'll walk back |
| 2282 | // over all the declarations when we have a full definition. |
| 2283 | const CXXRecordDecl *Def = RD->getDefinition(); |
| 2284 | if (!Def || Def->isBeingDefined()) |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2285 | return false; |
| 2286 | |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2287 | if (!RD->isAbstract()) |
| 2288 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2289 | |
Anders Carlsson | eabf770 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2290 | Diag(Loc, PD) << RD->getDeclName(); |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2291 | DiagnoseAbstractType(RD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2293 | return true; |
| 2294 | } |
| 2295 | |
| 2296 | void Sema::DiagnoseAbstractType(const CXXRecordDecl *RD) { |
| 2297 | // Check if we've already emitted the list of pure virtual functions |
| 2298 | // for this class. |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2299 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2300 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2302 | CXXFinalOverriderMap FinalOverriders; |
| 2303 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2304 | |
Anders Carlsson | a2f74f3 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2305 | // Keep a set of seen pure methods so we won't diagnose the same method |
| 2306 | // more than once. |
| 2307 | llvm::SmallPtrSet<const CXXMethodDecl *, 8> SeenPureMethods; |
| 2308 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2309 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2310 | MEnd = FinalOverriders.end(); |
| 2311 | M != MEnd; |
| 2312 | ++M) { |
| 2313 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2314 | SOEnd = M->second.end(); |
| 2315 | SO != SOEnd; ++SO) { |
| 2316 | // C++ [class.abstract]p4: |
| 2317 | // A class is abstract if it contains or inherits at least one |
| 2318 | // pure virtual function for which the final overrider is pure |
| 2319 | // virtual. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2321 | // |
| 2322 | if (SO->second.size() != 1) |
| 2323 | continue; |
| 2324 | |
| 2325 | if (!SO->second.front().Method->isPure()) |
| 2326 | continue; |
| 2327 | |
Anders Carlsson | a2f74f3 | 2010-06-03 01:00:02 +0000 | [diff] [blame] | 2328 | if (!SeenPureMethods.insert(SO->second.front().Method)) |
| 2329 | continue; |
| 2330 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2331 | Diag(SO->second.front().Method->getLocation(), |
| 2332 | diag::note_pure_virtual_function) |
| 2333 | << SO->second.front().Method->getDeclName(); |
| 2334 | } |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | if (!PureVirtualClassDiagSet) |
| 2338 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 2339 | PureVirtualClassDiagSet->insert(RD); |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2342 | namespace { |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2343 | struct AbstractUsageInfo { |
| 2344 | Sema &S; |
| 2345 | CXXRecordDecl *Record; |
| 2346 | CanQualType AbstractType; |
| 2347 | bool Invalid; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2348 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2349 | AbstractUsageInfo(Sema &S, CXXRecordDecl *Record) |
| 2350 | : S(S), Record(Record), |
| 2351 | AbstractType(S.Context.getCanonicalType( |
| 2352 | S.Context.getTypeDeclType(Record))), |
| 2353 | Invalid(false) {} |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2354 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2355 | void DiagnoseAbstractType() { |
| 2356 | if (Invalid) return; |
| 2357 | S.DiagnoseAbstractType(Record); |
| 2358 | Invalid = true; |
| 2359 | } |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2360 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2361 | void CheckType(const NamedDecl *D, TypeLoc TL, Sema::AbstractDiagSelID Sel); |
| 2362 | }; |
| 2363 | |
| 2364 | struct CheckAbstractUsage { |
| 2365 | AbstractUsageInfo &Info; |
| 2366 | const NamedDecl *Ctx; |
| 2367 | |
| 2368 | CheckAbstractUsage(AbstractUsageInfo &Info, const NamedDecl *Ctx) |
| 2369 | : Info(Info), Ctx(Ctx) {} |
| 2370 | |
| 2371 | void Visit(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2372 | switch (TL.getTypeLocClass()) { |
| 2373 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 2374 | #define TYPELOC(CLASS, PARENT) \ |
| 2375 | case TypeLoc::CLASS: Check(cast<CLASS##TypeLoc>(TL), Sel); break; |
| 2376 | #include "clang/AST/TypeLocNodes.def" |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2377 | } |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2378 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2379 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2380 | void Check(FunctionProtoTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2381 | Visit(TL.getResultLoc(), Sema::AbstractReturnType); |
| 2382 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2383 | TypeSourceInfo *TSI = TL.getArg(I)->getTypeSourceInfo(); |
| 2384 | if (TSI) Visit(TSI->getTypeLoc(), Sema::AbstractParamType); |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2385 | } |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2386 | } |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2387 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2388 | void Check(ArrayTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2389 | Visit(TL.getElementLoc(), Sema::AbstractArrayType); |
| 2390 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2392 | void Check(TemplateSpecializationTypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2393 | // Visit the type parameters from a permissive context. |
| 2394 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 2395 | TemplateArgumentLoc TAL = TL.getArgLoc(I); |
| 2396 | if (TAL.getArgument().getKind() == TemplateArgument::Type) |
| 2397 | if (TypeSourceInfo *TSI = TAL.getTypeSourceInfo()) |
| 2398 | Visit(TSI->getTypeLoc(), Sema::AbstractNone); |
| 2399 | // TODO: other template argument types? |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2400 | } |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2401 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2403 | // Visit pointee types from a permissive context. |
| 2404 | #define CheckPolymorphic(Type) \ |
| 2405 | void Check(Type TL, Sema::AbstractDiagSelID Sel) { \ |
| 2406 | Visit(TL.getNextTypeLoc(), Sema::AbstractNone); \ |
| 2407 | } |
| 2408 | CheckPolymorphic(PointerTypeLoc) |
| 2409 | CheckPolymorphic(ReferenceTypeLoc) |
| 2410 | CheckPolymorphic(MemberPointerTypeLoc) |
| 2411 | CheckPolymorphic(BlockPointerTypeLoc) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2412 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2413 | /// Handle all the types we haven't given a more specific |
| 2414 | /// implementation for above. |
| 2415 | void Check(TypeLoc TL, Sema::AbstractDiagSelID Sel) { |
| 2416 | // Every other kind of type that we haven't called out already |
| 2417 | // that has an inner type is either (1) sugar or (2) contains that |
| 2418 | // inner type in some way as a subobject. |
| 2419 | if (TypeLoc Next = TL.getNextTypeLoc()) |
| 2420 | return Visit(Next, Sel); |
| 2421 | |
| 2422 | // If there's no inner type and we're in a permissive context, |
| 2423 | // don't diagnose. |
| 2424 | if (Sel == Sema::AbstractNone) return; |
| 2425 | |
| 2426 | // Check whether the type matches the abstract type. |
| 2427 | QualType T = TL.getType(); |
| 2428 | if (T->isArrayType()) { |
| 2429 | Sel = Sema::AbstractArrayType; |
| 2430 | T = Info.S.Context.getBaseElementType(T); |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2431 | } |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2432 | CanQualType CT = T->getCanonicalTypeUnqualified().getUnqualifiedType(); |
| 2433 | if (CT != Info.AbstractType) return; |
| 2434 | |
| 2435 | // It matched; do some magic. |
| 2436 | if (Sel == Sema::AbstractArrayType) { |
| 2437 | Info.S.Diag(Ctx->getLocation(), diag::err_array_of_abstract_type) |
| 2438 | << T << TL.getSourceRange(); |
| 2439 | } else { |
| 2440 | Info.S.Diag(Ctx->getLocation(), diag::err_abstract_type_in_decl) |
| 2441 | << Sel << T << TL.getSourceRange(); |
| 2442 | } |
| 2443 | Info.DiagnoseAbstractType(); |
| 2444 | } |
| 2445 | }; |
| 2446 | |
| 2447 | void AbstractUsageInfo::CheckType(const NamedDecl *D, TypeLoc TL, |
| 2448 | Sema::AbstractDiagSelID Sel) { |
| 2449 | CheckAbstractUsage(*this, D).Visit(TL, Sel); |
| 2450 | } |
| 2451 | |
| 2452 | } |
| 2453 | |
| 2454 | /// Check for invalid uses of an abstract type in a method declaration. |
| 2455 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2456 | CXXMethodDecl *MD) { |
| 2457 | // No need to do the check on definitions, which require that |
| 2458 | // the return/param types be complete. |
| 2459 | if (MD->isThisDeclarationADefinition()) |
| 2460 | return; |
| 2461 | |
| 2462 | // For safety's sake, just ignore it if we don't have type source |
| 2463 | // information. This should never happen for non-implicit methods, |
| 2464 | // but... |
| 2465 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 2466 | Info.CheckType(MD, TSI->getTypeLoc(), Sema::AbstractNone); |
| 2467 | } |
| 2468 | |
| 2469 | /// Check for invalid uses of an abstract type within a class definition. |
| 2470 | static void CheckAbstractClassUsage(AbstractUsageInfo &Info, |
| 2471 | CXXRecordDecl *RD) { |
| 2472 | for (CXXRecordDecl::decl_iterator |
| 2473 | I = RD->decls_begin(), E = RD->decls_end(); I != E; ++I) { |
| 2474 | Decl *D = *I; |
| 2475 | if (D->isImplicit()) continue; |
| 2476 | |
| 2477 | // Methods and method templates. |
| 2478 | if (isa<CXXMethodDecl>(D)) { |
| 2479 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(D)); |
| 2480 | } else if (isa<FunctionTemplateDecl>(D)) { |
| 2481 | FunctionDecl *FD = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 2482 | CheckAbstractClassUsage(Info, cast<CXXMethodDecl>(FD)); |
| 2483 | |
| 2484 | // Fields and static variables. |
| 2485 | } else if (isa<FieldDecl>(D)) { |
| 2486 | FieldDecl *FD = cast<FieldDecl>(D); |
| 2487 | if (TypeSourceInfo *TSI = FD->getTypeSourceInfo()) |
| 2488 | Info.CheckType(FD, TSI->getTypeLoc(), Sema::AbstractFieldType); |
| 2489 | } else if (isa<VarDecl>(D)) { |
| 2490 | VarDecl *VD = cast<VarDecl>(D); |
| 2491 | if (TypeSourceInfo *TSI = VD->getTypeSourceInfo()) |
| 2492 | Info.CheckType(VD, TSI->getTypeLoc(), Sema::AbstractVariableType); |
| 2493 | |
| 2494 | // Nested classes and class templates. |
| 2495 | } else if (isa<CXXRecordDecl>(D)) { |
| 2496 | CheckAbstractClassUsage(Info, cast<CXXRecordDecl>(D)); |
| 2497 | } else if (isa<ClassTemplateDecl>(D)) { |
| 2498 | CheckAbstractClassUsage(Info, |
| 2499 | cast<ClassTemplateDecl>(D)->getTemplatedDecl()); |
| 2500 | } |
| 2501 | } |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2504 | /// \brief Perform semantic checks on a class definition that has been |
| 2505 | /// completing, introducing implicitly-declared members, checking for |
| 2506 | /// abstract types, etc. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2507 | void Sema::CheckCompletedCXXClass(CXXRecordDecl *Record) { |
Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 2508 | if (!Record) |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2509 | return; |
| 2510 | |
John McCall | 02db245d | 2010-08-18 09:41:07 +0000 | [diff] [blame] | 2511 | if (Record->isAbstract() && !Record->isInvalidDecl()) { |
| 2512 | AbstractUsageInfo Info(*this, Record); |
| 2513 | CheckAbstractClassUsage(Info, Record); |
| 2514 | } |
Douglas Gregor | 454a5b6 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2515 | |
| 2516 | // If this is not an aggregate type and has no user-declared constructor, |
| 2517 | // complain about any non-static data members of reference or const scalar |
| 2518 | // type, since they will never get initializers. |
| 2519 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 2520 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor()) { |
| 2521 | bool Complained = false; |
| 2522 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 2523 | FEnd = Record->field_end(); |
| 2524 | F != FEnd; ++F) { |
| 2525 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 659d7fc | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 2526 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 454a5b6 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2527 | if (!Complained) { |
| 2528 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 2529 | << Record->getTagKind() << Record; |
| 2530 | Complained = true; |
| 2531 | } |
| 2532 | |
| 2533 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 2534 | << F->getType()->isReferenceType() |
| 2535 | << F->getDeclName(); |
| 2536 | } |
| 2537 | } |
| 2538 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2539 | |
| 2540 | if (Record->isDynamicClass()) |
| 2541 | DynamicClasses.push_back(Record); |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2544 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2545 | Decl *TagDecl, |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2546 | SourceLocation LBrac, |
Douglas Gregor | c48a10d | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2547 | SourceLocation RBrac, |
| 2548 | AttributeList *AttrList) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2549 | if (!TagDecl) |
| 2550 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2551 | |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2552 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2553 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2554 | ActOnFields(S, RLoc, TagDecl, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2555 | // strict aliasing violation! |
| 2556 | reinterpret_cast<Decl**>(FieldCollector->getCurFields()), |
Douglas Gregor | c48a10d | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2557 | FieldCollector->getCurNumFields(), LBrac, RBrac, AttrList); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2558 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2559 | CheckCompletedCXXClass( |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2560 | dyn_cast_or_null<CXXRecordDecl>(TagDecl)); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2563 | namespace { |
| 2564 | /// \brief Helper class that collects exception specifications for |
| 2565 | /// implicitly-declared special member functions. |
| 2566 | class ImplicitExceptionSpecification { |
| 2567 | ASTContext &Context; |
| 2568 | bool AllowsAllExceptions; |
| 2569 | llvm::SmallPtrSet<CanQualType, 4> ExceptionsSeen; |
| 2570 | llvm::SmallVector<QualType, 4> Exceptions; |
| 2571 | |
| 2572 | public: |
| 2573 | explicit ImplicitExceptionSpecification(ASTContext &Context) |
| 2574 | : Context(Context), AllowsAllExceptions(false) { } |
| 2575 | |
| 2576 | /// \brief Whether the special member function should have any |
| 2577 | /// exception specification at all. |
| 2578 | bool hasExceptionSpecification() const { |
| 2579 | return !AllowsAllExceptions; |
| 2580 | } |
| 2581 | |
| 2582 | /// \brief Whether the special member function should have a |
| 2583 | /// throw(...) exception specification (a Microsoft extension). |
| 2584 | bool hasAnyExceptionSpecification() const { |
| 2585 | return false; |
| 2586 | } |
| 2587 | |
| 2588 | /// \brief The number of exceptions in the exception specification. |
| 2589 | unsigned size() const { return Exceptions.size(); } |
| 2590 | |
| 2591 | /// \brief The set of exceptions in the exception specification. |
| 2592 | const QualType *data() const { return Exceptions.data(); } |
| 2593 | |
| 2594 | /// \brief Note that |
| 2595 | void CalledDecl(CXXMethodDecl *Method) { |
| 2596 | // If we already know that we allow all exceptions, do nothing. |
Douglas Gregor | 3311ed4 | 2010-07-01 15:29:53 +0000 | [diff] [blame] | 2597 | if (AllowsAllExceptions || !Method) |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2598 | return; |
| 2599 | |
| 2600 | const FunctionProtoType *Proto |
| 2601 | = Method->getType()->getAs<FunctionProtoType>(); |
| 2602 | |
| 2603 | // If this function can throw any exceptions, make a note of that. |
| 2604 | if (!Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec()) { |
| 2605 | AllowsAllExceptions = true; |
| 2606 | ExceptionsSeen.clear(); |
| 2607 | Exceptions.clear(); |
| 2608 | return; |
| 2609 | } |
| 2610 | |
| 2611 | // Record the exceptions in this function's exception specification. |
| 2612 | for (FunctionProtoType::exception_iterator E = Proto->exception_begin(), |
| 2613 | EEnd = Proto->exception_end(); |
| 2614 | E != EEnd; ++E) |
| 2615 | if (ExceptionsSeen.insert(Context.getCanonicalType(*E))) |
| 2616 | Exceptions.push_back(*E); |
| 2617 | } |
| 2618 | }; |
| 2619 | } |
| 2620 | |
| 2621 | |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2622 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 2623 | /// special functions, such as the default constructor, copy |
| 2624 | /// constructor, or destructor, to the given C++ class (C++ |
| 2625 | /// [special]p1). This routine can only be executed just before the |
| 2626 | /// definition of the class is complete. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2627 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 2628 | if (!ClassDecl->hasUserDeclaredConstructor()) |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 2629 | ++ASTContext::NumImplicitDefaultConstructors; |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2630 | |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 2631 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 2632 | ++ASTContext::NumImplicitCopyConstructors; |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 2634 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 2635 | ++ASTContext::NumImplicitCopyAssignmentOperators; |
| 2636 | |
| 2637 | // If we have a dynamic class, then the copy assignment operator may be |
| 2638 | // virtual, so we have to declare it immediately. This ensures that, e.g., |
| 2639 | // it shows up in the right place in the vtable and that we diagnose |
| 2640 | // problems with the implicit exception specification. |
| 2641 | if (ClassDecl->isDynamicClass()) |
| 2642 | DeclareImplicitCopyAssignment(ClassDecl); |
| 2643 | } |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2644 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 2645 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
| 2646 | ++ASTContext::NumImplicitDestructors; |
| 2647 | |
| 2648 | // If we have a dynamic class, then the destructor may be virtual, so we |
| 2649 | // have to declare the destructor immediately. This ensures that, e.g., it |
| 2650 | // shows up in the right place in the vtable and that we diagnose problems |
| 2651 | // with the implicit exception specification. |
| 2652 | if (ClassDecl->isDynamicClass()) |
| 2653 | DeclareImplicitDestructor(ClassDecl); |
| 2654 | } |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2657 | void Sema::ActOnReenterTemplateScope(Scope *S, Decl *D) { |
Douglas Gregor | e61ef62 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 2658 | if (!D) |
| 2659 | return; |
| 2660 | |
| 2661 | TemplateParameterList *Params = 0; |
| 2662 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 2663 | Params = Template->getTemplateParameters(); |
| 2664 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2665 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 2666 | Params = PartialSpec->getTemplateParameters(); |
| 2667 | else |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2668 | return; |
| 2669 | |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2670 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2671 | ParamEnd = Params->end(); |
| 2672 | Param != ParamEnd; ++Param) { |
| 2673 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 2674 | if (Named->getDeclName()) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2675 | S->AddDecl(Named); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2676 | IdResolver.AddDecl(Named); |
| 2677 | } |
| 2678 | } |
| 2679 | } |
| 2680 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2681 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2682 | if (!RecordD) return; |
| 2683 | AdjustDeclIfTemplate(RecordD); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2684 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD); |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2685 | PushDeclContext(S, Record); |
| 2686 | } |
| 2687 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2688 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, Decl *RecordD) { |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2689 | if (!RecordD) return; |
| 2690 | PopDeclContext(); |
| 2691 | } |
| 2692 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2693 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 2694 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 2695 | /// parsing those parts of the given Method declaration that could |
| 2696 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 2697 | /// arguments. This action should enter the scope of the given |
| 2698 | /// Method declaration as if we had just parsed the qualified method |
| 2699 | /// name. However, it should not bring the parameters into scope; |
| 2700 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2701 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 2705 | /// C++ method declaration. We're (re-)introducing the given |
| 2706 | /// function parameter into scope for use in parsing later parts of |
| 2707 | /// the method declaration. For example, we could see an |
| 2708 | /// ActOnParamDefaultArgument event for this parameter. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2709 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, Decl *ParamD) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2710 | if (!ParamD) |
| 2711 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2713 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD); |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2714 | |
| 2715 | // If this parameter has an unparsed default argument, clear it out |
| 2716 | // to make way for the parsed default argument. |
| 2717 | if (Param->hasUnparsedDefaultArg()) |
| 2718 | Param->setDefaultArg(0); |
| 2719 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2720 | S->AddDecl(Param); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2721 | if (Param->getDeclName()) |
| 2722 | IdResolver.AddDecl(Param); |
| 2723 | } |
| 2724 | |
| 2725 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 2726 | /// processing the delayed method declaration for Method. The method |
| 2727 | /// declaration is now considered finished. There may be a separate |
| 2728 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 2729 | /// immediately!) for this method, if it was also defined inside the |
| 2730 | /// class body. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2731 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, Decl *MethodD) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2732 | if (!MethodD) |
| 2733 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2734 | |
Douglas Gregor | c8c277a | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2735 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2736 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2737 | FunctionDecl *Method = cast<FunctionDecl>(MethodD); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2738 | |
| 2739 | // Now that we have our default arguments, check the constructor |
| 2740 | // again. It could produce additional diagnostics or affect whether |
| 2741 | // the class has implicitly-declared destructors, among other |
| 2742 | // things. |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2743 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 2744 | CheckConstructor(Constructor); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2745 | |
| 2746 | // Check the default arguments, which we may have added. |
| 2747 | if (!Method->isInvalidDecl()) |
| 2748 | CheckCXXDefaultArguments(Method); |
| 2749 | } |
| 2750 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2751 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2752 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2753 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2754 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 2755 | /// will be updated to reflect a well-formed type for the constructor and |
| 2756 | /// returned. |
| 2757 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2758 | StorageClass &SC) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2759 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2760 | |
| 2761 | // C++ [class.ctor]p3: |
| 2762 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 2763 | // constructor can be invoked for a const, volatile or const |
| 2764 | // volatile object. A constructor shall not be declared const, |
| 2765 | // volatile, or const volatile (9.3.2). |
| 2766 | if (isVirtual) { |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2767 | if (!D.isInvalidType()) |
| 2768 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2769 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 2770 | << SourceRange(D.getIdentifierLoc()); |
| 2771 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2772 | } |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2773 | if (SC == SC_Static) { |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2774 | if (!D.isInvalidType()) |
| 2775 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2776 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2777 | << SourceRange(D.getIdentifierLoc()); |
| 2778 | D.setInvalidType(); |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2779 | SC = SC_None; |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2780 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2781 | |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2782 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2783 | if (FTI.TypeQuals != 0) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2784 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2785 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2786 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2787 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2788 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2789 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2790 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2791 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2792 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2793 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2794 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2795 | // Rebuild the function type "R" without any type qualifiers (in |
| 2796 | // case any of the errors above fired) and with "void" as the |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2797 | // return type, since constructors don't have return types. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2798 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2799 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 2800 | Proto->getNumArgs(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2801 | Proto->isVariadic(), 0, |
| 2802 | Proto->hasExceptionSpec(), |
| 2803 | Proto->hasAnyExceptionSpec(), |
| 2804 | Proto->getNumExceptions(), |
| 2805 | Proto->exception_begin(), |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2806 | Proto->getExtInfo()); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2809 | /// CheckConstructor - Checks a fully-formed constructor for |
| 2810 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 2811 | /// the constructor declarator is invalid. |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2812 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | CXXRecordDecl *ClassDecl |
Douglas Gregor | f4d17c4 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2814 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 2815 | if (!ClassDecl) |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2816 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2817 | |
| 2818 | // C++ [class.copy]p3: |
| 2819 | // A declaration of a constructor for a class X is ill-formed if |
| 2820 | // its first parameter is of type (optionally cv-qualified) X and |
| 2821 | // either there are no other parameters or else all other |
| 2822 | // parameters have default arguments. |
Douglas Gregor | f4d17c4 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2823 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2824 | ((Constructor->getNumParams() == 1) || |
| 2825 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2826 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 2827 | Constructor->getTemplateSpecializationKind() |
| 2828 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2829 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 2830 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 2831 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2832 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
Douglas Gregor | fd42e95 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 2833 | const char *ConstRef |
| 2834 | = Constructor->getParamDecl(0)->getIdentifier() ? "const &" |
| 2835 | : " const &"; |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2836 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | fd42e95 | 2010-05-27 21:28:21 +0000 | [diff] [blame] | 2837 | << FixItHint::CreateInsertion(ParamLoc, ConstRef); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2838 | |
| 2839 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 2840 | // to fix the type. |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2841 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2842 | } |
| 2843 | } |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
John McCall | deb646e | 2010-08-04 01:04:25 +0000 | [diff] [blame] | 2846 | /// CheckDestructor - Checks a fully-formed destructor definition for |
| 2847 | /// well-formedness, issuing any diagnostics required. Returns true |
| 2848 | /// on error. |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2849 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2850 | CXXRecordDecl *RD = Destructor->getParent(); |
| 2851 | |
| 2852 | if (Destructor->isVirtual()) { |
| 2853 | SourceLocation Loc; |
| 2854 | |
| 2855 | if (!Destructor->isImplicit()) |
| 2856 | Loc = Destructor->getLocation(); |
| 2857 | else |
| 2858 | Loc = RD->getLocation(); |
| 2859 | |
| 2860 | // If we have a virtual destructor, look up the deallocation function |
| 2861 | FunctionDecl *OperatorDelete = 0; |
| 2862 | DeclarationName Name = |
| 2863 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2864 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2865 | return true; |
John McCall | 1e5d75d | 2010-07-03 18:33:00 +0000 | [diff] [blame] | 2866 | |
| 2867 | MarkDeclarationReferenced(Loc, OperatorDelete); |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2868 | |
| 2869 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2870 | } |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2871 | |
| 2872 | return false; |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2875 | static inline bool |
Anders Carlsson | 5e96547 | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2876 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 2877 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 2878 | FTI.ArgInfo[0].Param && |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2879 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()); |
Anders Carlsson | 5e96547 | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2882 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 2883 | /// the well-formednes of the destructor declarator @p D with type @p |
| 2884 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2885 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 2886 | /// will be updated to reflect a well-formed type for the destructor and |
| 2887 | /// returned. |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2888 | QualType Sema::CheckDestructorDeclarator(Declarator &D, QualType R, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2889 | StorageClass& SC) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2890 | // C++ [class.dtor]p1: |
| 2891 | // [...] A typedef-name that names a class is a class-name |
| 2892 | // (7.1.3); however, a typedef-name that names a class shall not |
| 2893 | // be used as the identifier in the declarator for a destructor |
| 2894 | // declaration. |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2895 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2896 | if (isa<TypedefType>(DeclaratorType)) |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2897 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2898 | << DeclaratorType; |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2899 | |
| 2900 | // C++ [class.dtor]p2: |
| 2901 | // A destructor is used to destroy objects of its class type. A |
| 2902 | // destructor takes no parameters, and no return type can be |
| 2903 | // specified for it (not even void). The address of a destructor |
| 2904 | // shall not be taken. A destructor shall not be static. A |
| 2905 | // destructor can be invoked for a const, volatile or const |
| 2906 | // volatile object. A destructor shall not be declared const, |
| 2907 | // volatile or const volatile (9.3.2). |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2908 | if (SC == SC_Static) { |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2909 | if (!D.isInvalidType()) |
| 2910 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 2911 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2912 | << SourceRange(D.getIdentifierLoc()) |
| 2913 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 2914 | |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2915 | SC = SC_None; |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2916 | } |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2917 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2918 | // Destructors don't have return types, but the parser will |
| 2919 | // happily parse something like: |
| 2920 | // |
| 2921 | // class X { |
| 2922 | // float ~X(); |
| 2923 | // }; |
| 2924 | // |
| 2925 | // The return type will be eliminated later. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2926 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 2927 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2928 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2929 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2930 | |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2931 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2932 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2933 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2934 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2935 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2936 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2937 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2938 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2939 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2940 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2941 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2942 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | // Make sure we don't have any parameters. |
Anders Carlsson | 5e96547 | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2946 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2947 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 2948 | |
| 2949 | // Delete the parameters. |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2950 | FTI.freeArgs(); |
| 2951 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2954 | // Make sure the destructor isn't variadic. |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2955 | if (FTI.isVariadic) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2956 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2957 | D.setInvalidType(); |
| 2958 | } |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2959 | |
| 2960 | // Rebuild the function type "R" without any type qualifiers or |
| 2961 | // parameters (in case any of the errors above fired) and with |
| 2962 | // "void" as the return type, since destructors don't have return |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2963 | // types. |
| 2964 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 2965 | if (!Proto) |
| 2966 | return QualType(); |
| 2967 | |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2968 | return Context.getFunctionType(Context.VoidTy, 0, 0, false, 0, |
Douglas Gregor | 9575516 | 2010-07-01 05:10:53 +0000 | [diff] [blame] | 2969 | Proto->hasExceptionSpec(), |
| 2970 | Proto->hasAnyExceptionSpec(), |
| 2971 | Proto->getNumExceptions(), |
| 2972 | Proto->exception_begin(), |
| 2973 | Proto->getExtInfo()); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2976 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 2977 | /// well-formednes of the conversion function declarator @p D with |
| 2978 | /// type @p R. If there are any errors in the declarator, this routine |
| 2979 | /// will emit diagnostics and return true. Otherwise, it will return |
| 2980 | /// false. Either way, the type @p R will be updated to reflect a |
| 2981 | /// well-formed type for the conversion operator. |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2982 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2983 | StorageClass& SC) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2984 | // C++ [class.conv.fct]p1: |
| 2985 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2986 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2987 | // parameter returning conversion-type-id." |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2988 | if (SC == SC_Static) { |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2989 | if (!D.isInvalidType()) |
| 2990 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 2991 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2992 | << SourceRange(D.getIdentifierLoc()); |
| 2993 | D.setInvalidType(); |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2994 | SC = SC_None; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2995 | } |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 2996 | |
| 2997 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 2998 | |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2999 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3000 | // Conversion functions don't have return types, but the parser will |
| 3001 | // happily parse something like: |
| 3002 | // |
| 3003 | // class X { |
| 3004 | // float operator bool(); |
| 3005 | // }; |
| 3006 | // |
| 3007 | // The return type will be changed later anyway. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3008 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 3009 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 3010 | << SourceRange(D.getIdentifierLoc()); |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3011 | D.setInvalidType(); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3014 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 3015 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3016 | // Make sure we don't have any parameters. |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3017 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3018 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 3019 | |
| 3020 | // Delete the parameters. |
Chris Lattner | 5742c1e | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 3021 | D.getTypeObject(0).Fun.freeArgs(); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3022 | D.setInvalidType(); |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3023 | } else if (Proto->isVariadic()) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3024 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3025 | D.setInvalidType(); |
| 3026 | } |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3027 | |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3028 | // Diagnose "&operator bool()" and other such nonsense. This |
| 3029 | // is actually a gcc extension which we don't support. |
| 3030 | if (Proto->getResultType() != ConvType) { |
| 3031 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 3032 | << Proto->getResultType(); |
| 3033 | D.setInvalidType(); |
| 3034 | ConvType = Proto->getResultType(); |
| 3035 | } |
| 3036 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3037 | // C++ [class.conv.fct]p4: |
| 3038 | // The conversion-type-id shall not represent a function type nor |
| 3039 | // an array type. |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3040 | if (ConvType->isArrayType()) { |
| 3041 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 3042 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3043 | D.setInvalidType(); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3044 | } else if (ConvType->isFunctionType()) { |
| 3045 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 3046 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3047 | D.setInvalidType(); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | // Rebuild the function type "R" without any parameters (in case any |
| 3051 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3052 | // return type. |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3053 | if (D.isInvalidType()) { |
| 3054 | R = Context.getFunctionType(ConvType, 0, 0, false, |
| 3055 | Proto->getTypeQuals(), |
| 3056 | Proto->hasExceptionSpec(), |
| 3057 | Proto->hasAnyExceptionSpec(), |
| 3058 | Proto->getNumExceptions(), |
| 3059 | Proto->exception_begin(), |
| 3060 | Proto->getExtInfo()); |
| 3061 | } |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3062 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3063 | // C++0x explicit conversion operators. |
| 3064 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3065 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3066 | diag::warn_explicit_conversion_functions) |
| 3067 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3070 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 3071 | /// the declaration of the given C++ conversion function. This routine |
| 3072 | /// is responsible for recording the conversion function in the C++ |
| 3073 | /// class, if possible. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3074 | Decl *Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3075 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 3076 | |
Douglas Gregor | 4287b37 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 3077 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3078 | |
| 3079 | // Make sure we aren't redeclaring the conversion function. |
| 3080 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3081 | |
| 3082 | // C++ [class.conv.fct]p1: |
| 3083 | // [...] A conversion function is never used to convert a |
| 3084 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 3085 | // same object type (or a reference to it), to a (possibly |
| 3086 | // cv-qualified) base class of that type (or a reference to it), |
| 3087 | // or to (possibly cv-qualified) void. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3088 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 3089 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | QualType ClassType |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3091 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3092 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3093 | ConvType = ConvTypeRef->getPointeeType(); |
Douglas Gregor | 6309e3d | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 3094 | if (Conversion->getTemplateSpecializationKind() != TSK_Undeclared && |
| 3095 | Conversion->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | e47191c | 2010-09-13 16:44:26 +0000 | [diff] [blame] | 3096 | /* Suppress diagnostics for instantiations. */; |
Douglas Gregor | 6309e3d | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 3097 | else if (ConvType->isRecordType()) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3098 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 3099 | if (ConvType == ClassType) |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3100 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3101 | << ClassType; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3102 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3103 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3104 | << ClassType << ConvType; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3105 | } else if (ConvType->isVoidType()) { |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3106 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3107 | << ClassType << ConvType; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame^] | 3110 | if (FunctionTemplateDecl *ConversionTemplate |
| 3111 | = Conversion->getDescribedFunctionTemplate()) |
| 3112 | return ConversionTemplate; |
| 3113 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3114 | return Conversion; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3117 | //===----------------------------------------------------------------------===// |
| 3118 | // Namespace Handling |
| 3119 | //===----------------------------------------------------------------------===// |
| 3120 | |
John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3121 | |
| 3122 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3123 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 3124 | /// definition. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3125 | Decl *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 3126 | SourceLocation InlineLoc, |
John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3127 | SourceLocation IdentLoc, |
| 3128 | IdentifierInfo *II, |
| 3129 | SourceLocation LBrace, |
| 3130 | AttributeList *AttrList) { |
Douglas Gregor | 086cae6 | 2010-08-19 20:55:47 +0000 | [diff] [blame] | 3131 | // anonymous namespace starts at its left brace |
| 3132 | NamespaceDecl *Namespc = NamespaceDecl::Create(Context, CurContext, |
| 3133 | (II ? IdentLoc : LBrace) , II); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3134 | Namespc->setLBracLoc(LBrace); |
Sebastian Redl | b5c2baa | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3135 | Namespc->setInline(InlineLoc.isValid()); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3136 | |
| 3137 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 3138 | |
Anders Carlsson | a7bcade | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 3139 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 3140 | |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3141 | if (const VisibilityAttr *attr = Namespc->getAttr<VisibilityAttr>()) |
John McCall | b1be523 | 2010-08-26 09:15:37 +0000 | [diff] [blame] | 3142 | PushVisibilityAttr(attr); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3143 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3144 | if (II) { |
| 3145 | // C++ [namespace.def]p2: |
| 3146 | // The identifier in an original-namespace-definition shall not have been |
| 3147 | // previously defined in the declarative region in which the |
| 3148 | // original-namespace-definition appears. The identifier in an |
| 3149 | // original-namespace-definition is the name of the namespace. Subsequently |
| 3150 | // in that declarative region, it is treated as an original-namespace-name. |
| 3151 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3152 | NamedDecl *PrevDecl |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 3153 | = LookupSingleName(DeclRegionScope, II, IdentLoc, LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 3154 | ForRedeclaration); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3155 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3156 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 3157 | // This is an extended namespace definition. |
Sebastian Redl | b5c2baa | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3158 | if (Namespc->isInline() != OrigNS->isInline()) { |
| 3159 | // inline-ness must match |
| 3160 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 3161 | << Namespc->isInline(); |
| 3162 | Diag(OrigNS->getLocation(), diag::note_previous_definition); |
| 3163 | Namespc->setInvalidDecl(); |
| 3164 | // Recover by ignoring the new namespace's inline status. |
| 3165 | Namespc->setInline(OrigNS->isInline()); |
| 3166 | } |
| 3167 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3168 | // Attach this namespace decl to the chain of extended namespace |
| 3169 | // definitions. |
| 3170 | OrigNS->setNextNamespace(Namespc); |
| 3171 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3172 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3173 | // Remove the previous declaration from the scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3174 | if (DeclRegionScope->isDeclScope(OrigNS)) { |
Douglas Gregor | 7a4fad1 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 3175 | IdResolver.RemoveDecl(OrigNS); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3176 | DeclRegionScope->RemoveDecl(OrigNS); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3177 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3178 | } else if (PrevDecl) { |
| 3179 | // This is an invalid name redefinition. |
| 3180 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 3181 | << Namespc->getDeclName(); |
| 3182 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3183 | Namespc->setInvalidDecl(); |
| 3184 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3185 | } else if (II->isStr("std") && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3186 | CurContext->getRedeclContext()->isTranslationUnit()) { |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3187 | // This is the first "real" definition of the namespace "std", so update |
| 3188 | // our cache of the "std" namespace to point at this definition. |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3189 | if (NamespaceDecl *StdNS = getStdNamespace()) { |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3190 | // We had already defined a dummy namespace "std". Link this new |
| 3191 | // namespace definition to the dummy namespace "std". |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3192 | StdNS->setNextNamespace(Namespc); |
| 3193 | StdNS->setLocation(IdentLoc); |
| 3194 | Namespc->setOriginalNamespace(StdNS->getOriginalNamespace()); |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | // Make our StdNamespace cache point at the first real definition of the |
| 3198 | // "std" namespace. |
| 3199 | StdNamespace = Namespc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3200 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3201 | |
| 3202 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 3203 | } else { |
John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3204 | // Anonymous namespaces. |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3205 | assert(Namespc->isAnonymousNamespace()); |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3206 | |
| 3207 | // Link the anonymous namespace into its parent. |
| 3208 | NamespaceDecl *PrevDecl; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3209 | DeclContext *Parent = CurContext->getRedeclContext(); |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3210 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 3211 | PrevDecl = TU->getAnonymousNamespace(); |
| 3212 | TU->setAnonymousNamespace(Namespc); |
| 3213 | } else { |
| 3214 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 3215 | PrevDecl = ND->getAnonymousNamespace(); |
| 3216 | ND->setAnonymousNamespace(Namespc); |
| 3217 | } |
| 3218 | |
| 3219 | // Link the anonymous namespace with its previous declaration. |
| 3220 | if (PrevDecl) { |
| 3221 | assert(PrevDecl->isAnonymousNamespace()); |
| 3222 | assert(!PrevDecl->getNextNamespace()); |
| 3223 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 3224 | PrevDecl->setNextNamespace(Namespc); |
Sebastian Redl | b5c2baa | 2010-08-31 00:36:36 +0000 | [diff] [blame] | 3225 | |
| 3226 | if (Namespc->isInline() != PrevDecl->isInline()) { |
| 3227 | // inline-ness must match |
| 3228 | Diag(Namespc->getLocation(), diag::err_inline_namespace_mismatch) |
| 3229 | << Namespc->isInline(); |
| 3230 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3231 | Namespc->setInvalidDecl(); |
| 3232 | // Recover by ignoring the new namespace's inline status. |
| 3233 | Namespc->setInline(PrevDecl->isInline()); |
| 3234 | } |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3235 | } |
John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3236 | |
Douglas Gregor | f9f54ea | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 3237 | CurContext->addDecl(Namespc); |
| 3238 | |
John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3239 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 3240 | // behaves as if it were replaced by |
| 3241 | // namespace unique { /* empty body */ } |
| 3242 | // using namespace unique; |
| 3243 | // namespace unique { namespace-body } |
| 3244 | // where all occurrences of 'unique' in a translation unit are |
| 3245 | // replaced by the same identifier and this identifier differs |
| 3246 | // from all other identifiers in the entire program. |
| 3247 | |
| 3248 | // We just create the namespace with an empty name and then add an |
| 3249 | // implicit using declaration, just like the standard suggests. |
| 3250 | // |
| 3251 | // CodeGen enforces the "universally unique" aspect by giving all |
| 3252 | // declarations semantically contained within an anonymous |
| 3253 | // namespace internal linkage. |
| 3254 | |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3255 | if (!PrevDecl) { |
| 3256 | UsingDirectiveDecl* UD |
| 3257 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 3258 | /* 'using' */ LBrace, |
| 3259 | /* 'namespace' */ SourceLocation(), |
| 3260 | /* qualifier */ SourceRange(), |
| 3261 | /* NNS */ NULL, |
| 3262 | /* identifier */ SourceLocation(), |
| 3263 | Namespc, |
| 3264 | /* Ancestor */ CurContext); |
| 3265 | UD->setImplicit(); |
| 3266 | CurContext->addDecl(UD); |
| 3267 | } |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 3271 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3272 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 3273 | // for the namespace has the declarations that showed up in that particular |
| 3274 | // namespace definition. |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3275 | PushDeclContext(NamespcScope, Namespc); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3276 | return Namespc; |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3279 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 3280 | /// is a namespace alias, returns the namespace it points to. |
| 3281 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 3282 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 3283 | return AD->getNamespace(); |
| 3284 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 3285 | } |
| 3286 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3287 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 3288 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3289 | void Sema::ActOnFinishNamespaceDef(Decl *Dcl, SourceLocation RBrace) { |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3290 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 3291 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 3292 | Namespc->setRBracLoc(RBrace); |
| 3293 | PopDeclContext(); |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 3294 | if (Namespc->hasAttr<VisibilityAttr>()) |
| 3295 | PopPragmaVisibility(); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3296 | } |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3297 | |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 3298 | CXXRecordDecl *Sema::getStdBadAlloc() const { |
| 3299 | return cast_or_null<CXXRecordDecl>( |
| 3300 | StdBadAlloc.get(Context.getExternalSource())); |
| 3301 | } |
| 3302 | |
| 3303 | NamespaceDecl *Sema::getStdNamespace() const { |
| 3304 | return cast_or_null<NamespaceDecl>( |
| 3305 | StdNamespace.get(Context.getExternalSource())); |
| 3306 | } |
| 3307 | |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3308 | /// \brief Retrieve the special "std" namespace, which may require us to |
| 3309 | /// implicitly define the namespace. |
Argyrios Kyrtzidis | 4f8e173 | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 3310 | NamespaceDecl *Sema::getOrCreateStdNamespace() { |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3311 | if (!StdNamespace) { |
| 3312 | // The "std" namespace has not yet been defined, so build one implicitly. |
| 3313 | StdNamespace = NamespaceDecl::Create(Context, |
| 3314 | Context.getTranslationUnitDecl(), |
| 3315 | SourceLocation(), |
| 3316 | &PP.getIdentifierTable().get("std")); |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3317 | getStdNamespace()->setImplicit(true); |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 3320 | return getStdNamespace(); |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3323 | Decl *Sema::ActOnUsingDirective(Scope *S, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3324 | SourceLocation UsingLoc, |
| 3325 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3326 | CXXScopeSpec &SS, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3327 | SourceLocation IdentLoc, |
| 3328 | IdentifierInfo *NamespcName, |
| 3329 | AttributeList *AttrList) { |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3330 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3331 | assert(NamespcName && "Invalid NamespcName."); |
| 3332 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3333 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3334 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3335 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3336 | NestedNameSpecifier *Qualifier = 0; |
| 3337 | if (SS.isSet()) |
| 3338 | Qualifier = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3339 | |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 3340 | // Lookup namespace name. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3341 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 3342 | LookupParsedName(R, S, &SS); |
| 3343 | if (R.isAmbiguous()) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3344 | return 0; |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3345 | |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3346 | if (R.empty()) { |
| 3347 | // Allow "using namespace std;" or "using namespace ::std;" even if |
| 3348 | // "std" hasn't been defined yet, for GCC compatibility. |
| 3349 | if ((!Qualifier || Qualifier->getKind() == NestedNameSpecifier::Global) && |
| 3350 | NamespcName->isStr("std")) { |
| 3351 | Diag(IdentLoc, diag::ext_using_undefined_std); |
Argyrios Kyrtzidis | 4f8e173 | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 3352 | R.addDecl(getOrCreateStdNamespace()); |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3353 | R.resolveKind(); |
| 3354 | } |
| 3355 | // Otherwise, attempt typo correction. |
| 3356 | else if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 3357 | CTC_NoKeywords, 0)) { |
| 3358 | if (R.getAsSingle<NamespaceDecl>() || |
| 3359 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 3360 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 3361 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 3362 | << NamespcName << DC << Corrected << SS.getRange() |
| 3363 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 3364 | else |
| 3365 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 3366 | << NamespcName << Corrected |
| 3367 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 3368 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 3369 | << Corrected; |
| 3370 | |
| 3371 | NamespcName = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3372 | } else { |
| 3373 | R.clear(); |
| 3374 | R.setLookupName(NamespcName); |
Douglas Gregor | cdf8702 | 2010-06-29 17:53:46 +0000 | [diff] [blame] | 3375 | } |
| 3376 | } |
| 3377 | } |
| 3378 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3379 | if (!R.empty()) { |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3380 | NamedDecl *Named = R.getFoundDecl(); |
| 3381 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 3382 | && "expected namespace decl"); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3383 | // C++ [namespace.udir]p1: |
| 3384 | // A using-directive specifies that the names in the nominated |
| 3385 | // namespace can be used in the scope in which the |
| 3386 | // using-directive appears after the using-directive. During |
| 3387 | // unqualified name lookup (3.4.1), the names appear as if they |
| 3388 | // were declared in the nearest enclosing namespace which |
| 3389 | // contains both the using-directive and the nominated |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3390 | // namespace. [Note: in this context, "contains" means "contains |
| 3391 | // directly or indirectly". ] |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3392 | |
| 3393 | // Find enclosing context containing both using-directive and |
| 3394 | // nominated namespace. |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3395 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3396 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 3397 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 3398 | CommonAncestor = CommonAncestor->getParent(); |
| 3399 | |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3400 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | 3bc6e4c | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 3401 | SS.getRange(), |
| 3402 | (NestedNameSpecifier *)SS.getScopeRep(), |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3403 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3404 | PushUsingDirective(S, UDir); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3405 | } else { |
Chris Lattner | 8dca2e9 | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 3406 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3409 | // FIXME: We ignore attributes for now. |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3410 | delete AttrList; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3411 | return UDir; |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 3415 | // If scope has associated entity, then using directive is at namespace |
| 3416 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 3417 | // it's lookup structure. |
| 3418 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3419 | Ctx->addDecl(UDir); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3420 | else |
| 3421 | // Otherwise it is block-sope. using-directives will affect lookup |
| 3422 | // only to the end of scope. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3423 | S->PushUsingDirective(UDir); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3424 | } |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3425 | |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3426 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3427 | Decl *Sema::ActOnUsingDeclaration(Scope *S, |
Anders Carlsson | 7b194b7 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 3428 | AccessSpecifier AS, |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3429 | bool HasUsingKeyword, |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3430 | SourceLocation UsingLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3431 | CXXScopeSpec &SS, |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3432 | UnqualifiedId &Name, |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3433 | AttributeList *AttrList, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3434 | bool IsTypeName, |
| 3435 | SourceLocation TypenameLoc) { |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3436 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3437 | |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3438 | switch (Name.getKind()) { |
| 3439 | case UnqualifiedId::IK_Identifier: |
| 3440 | case UnqualifiedId::IK_OperatorFunctionId: |
Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3441 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3442 | case UnqualifiedId::IK_ConversionFunctionId: |
| 3443 | break; |
| 3444 | |
| 3445 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3446 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3447 | // C++0x inherited constructors. |
| 3448 | if (getLangOptions().CPlusPlus0x) break; |
| 3449 | |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3450 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 3451 | << SS.getRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3452 | return 0; |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3453 | |
| 3454 | case UnqualifiedId::IK_DestructorName: |
| 3455 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 3456 | << SS.getRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3457 | return 0; |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3458 | |
| 3459 | case UnqualifiedId::IK_TemplateId: |
| 3460 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 3461 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3462 | return 0; |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3463 | } |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3464 | |
| 3465 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); |
| 3466 | DeclarationName TargetName = TargetNameInfo.getName(); |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3467 | if (!TargetName) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3468 | return 0; |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3469 | |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3470 | // Warn about using declarations. |
| 3471 | // TODO: store that the declaration was written without 'using' and |
| 3472 | // talk about access decls instead of using decls in the |
| 3473 | // diagnostics. |
| 3474 | if (!HasUsingKeyword) { |
| 3475 | UsingLoc = Name.getSourceRange().getBegin(); |
| 3476 | |
| 3477 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3478 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3481 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3482 | TargetNameInfo, AttrList, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3483 | /* IsInstantiation */ false, |
| 3484 | IsTypeName, TypenameLoc); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3485 | if (UD) |
| 3486 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3487 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3488 | return UD; |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Douglas Gregor | 1d9ef84 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 3491 | /// \brief Determine whether a using declaration considers the given |
| 3492 | /// declarations as "equivalent", e.g., if they are redeclarations of |
| 3493 | /// the same entity or are both typedefs of the same type. |
| 3494 | static bool |
| 3495 | IsEquivalentForUsingDecl(ASTContext &Context, NamedDecl *D1, NamedDecl *D2, |
| 3496 | bool &SuppressRedeclaration) { |
| 3497 | if (D1->getCanonicalDecl() == D2->getCanonicalDecl()) { |
| 3498 | SuppressRedeclaration = false; |
| 3499 | return true; |
| 3500 | } |
| 3501 | |
| 3502 | if (TypedefDecl *TD1 = dyn_cast<TypedefDecl>(D1)) |
| 3503 | if (TypedefDecl *TD2 = dyn_cast<TypedefDecl>(D2)) { |
| 3504 | SuppressRedeclaration = true; |
| 3505 | return Context.hasSameType(TD1->getUnderlyingType(), |
| 3506 | TD2->getUnderlyingType()); |
| 3507 | } |
| 3508 | |
| 3509 | return false; |
| 3510 | } |
| 3511 | |
| 3512 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3513 | /// Determines whether to create a using shadow decl for a particular |
| 3514 | /// decl, given the set of decls existing prior to this using lookup. |
| 3515 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 3516 | const LookupResult &Previous) { |
| 3517 | // Diagnose finding a decl which is not from a base class of the |
| 3518 | // current class. We do this now because there are cases where this |
| 3519 | // function will silently decide not to build a shadow decl, which |
| 3520 | // will pre-empt further diagnostics. |
| 3521 | // |
| 3522 | // We don't need to do this in C++0x because we do the check once on |
| 3523 | // the qualifier. |
| 3524 | // |
| 3525 | // FIXME: diagnose the following if we care enough: |
| 3526 | // struct A { int foo; }; |
| 3527 | // struct B : A { using A::foo; }; |
| 3528 | // template <class T> struct C : A {}; |
| 3529 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 3530 | // This is invalid (during instantiation) in C++03 because B::foo |
| 3531 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 3532 | // We can't diagnose it immediately because C<T> is an unknown |
| 3533 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 3534 | // to A::foo, which will look well-formed when we instantiate. |
| 3535 | // The right solution is to not collapse the shadow-decl chain. |
| 3536 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 3537 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 3538 | |
| 3539 | // Handle enums and anonymous structs. |
| 3540 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 3541 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 3542 | while (OrigRec->isAnonymousStructOrUnion()) |
| 3543 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 3544 | |
| 3545 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 3546 | if (OrigDC == CurContext) { |
| 3547 | Diag(Using->getLocation(), |
| 3548 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3549 | << Using->getNestedNameRange(); |
| 3550 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3551 | return true; |
| 3552 | } |
| 3553 | |
| 3554 | Diag(Using->getNestedNameRange().getBegin(), |
| 3555 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3556 | << Using->getTargetNestedNameDecl() |
| 3557 | << cast<CXXRecordDecl>(CurContext) |
| 3558 | << Using->getNestedNameRange(); |
| 3559 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3560 | return true; |
| 3561 | } |
| 3562 | } |
| 3563 | |
| 3564 | if (Previous.empty()) return false; |
| 3565 | |
| 3566 | NamedDecl *Target = Orig; |
| 3567 | if (isa<UsingShadowDecl>(Target)) |
| 3568 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3569 | |
John McCall | a17e83e | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3570 | // If the target happens to be one of the previous declarations, we |
| 3571 | // don't have a conflict. |
| 3572 | // |
| 3573 | // FIXME: but we might be increasing its access, in which case we |
| 3574 | // should redeclare it. |
| 3575 | NamedDecl *NonTag = 0, *Tag = 0; |
| 3576 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 3577 | I != E; ++I) { |
| 3578 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
Douglas Gregor | 1d9ef84 | 2010-07-07 23:08:52 +0000 | [diff] [blame] | 3579 | bool Result; |
| 3580 | if (IsEquivalentForUsingDecl(Context, D, Target, Result)) |
| 3581 | return Result; |
John McCall | a17e83e | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3582 | |
| 3583 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 3584 | } |
| 3585 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3586 | if (Target->isFunctionOrFunctionTemplate()) { |
| 3587 | FunctionDecl *FD; |
| 3588 | if (isa<FunctionTemplateDecl>(Target)) |
| 3589 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 3590 | else |
| 3591 | FD = cast<FunctionDecl>(Target); |
| 3592 | |
| 3593 | NamedDecl *OldDecl = 0; |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3594 | switch (CheckOverload(0, FD, Previous, OldDecl, /*IsForUsingDecl*/ true)) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3595 | case Ovl_Overload: |
| 3596 | return false; |
| 3597 | |
| 3598 | case Ovl_NonFunction: |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3599 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3600 | break; |
| 3601 | |
| 3602 | // We found a decl with the exact signature. |
| 3603 | case Ovl_Match: |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3604 | // If we're in a record, we want to hide the target, so we |
| 3605 | // return true (without a diagnostic) to tell the caller not to |
| 3606 | // build a shadow decl. |
| 3607 | if (CurContext->isRecord()) |
| 3608 | return true; |
| 3609 | |
| 3610 | // If we're not in a record, this is an error. |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3611 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3612 | break; |
| 3613 | } |
| 3614 | |
| 3615 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3616 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 3617 | return true; |
| 3618 | } |
| 3619 | |
| 3620 | // Target is not a function. |
| 3621 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3622 | if (isa<TagDecl>(Target)) { |
| 3623 | // No conflict between a tag and a non-tag. |
| 3624 | if (!Tag) return false; |
| 3625 | |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3626 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3627 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3628 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 3629 | return true; |
| 3630 | } |
| 3631 | |
| 3632 | // No conflict between a tag and a non-tag. |
| 3633 | if (!NonTag) return false; |
| 3634 | |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3635 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3636 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3637 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 3638 | return true; |
| 3639 | } |
| 3640 | |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3641 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3642 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3643 | UsingDecl *UD, |
| 3644 | NamedDecl *Orig) { |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3645 | |
| 3646 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3647 | NamedDecl *Target = Orig; |
| 3648 | if (isa<UsingShadowDecl>(Target)) { |
| 3649 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3650 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | UsingShadowDecl *Shadow |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3654 | = UsingShadowDecl::Create(Context, CurContext, |
| 3655 | UD->getLocation(), UD, Target); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3656 | UD->addShadowDecl(Shadow); |
Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame^] | 3657 | |
| 3658 | Shadow->setAccess(UD->getAccess()); |
| 3659 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 3660 | Shadow->setInvalidDecl(); |
| 3661 | |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3662 | if (S) |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3663 | PushOnScopeChains(Shadow, S); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3664 | else |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3665 | CurContext->addDecl(Shadow); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3666 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3667 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3668 | return Shadow; |
| 3669 | } |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3670 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3671 | /// Hides a using shadow declaration. This is required by the current |
| 3672 | /// using-decl implementation when a resolvable using declaration in a |
| 3673 | /// class is followed by a declaration which would hide or override |
| 3674 | /// one or more of the using decl's targets; for example: |
| 3675 | /// |
| 3676 | /// struct Base { void foo(int); }; |
| 3677 | /// struct Derived : Base { |
| 3678 | /// using Base::foo; |
| 3679 | /// void foo(int); |
| 3680 | /// }; |
| 3681 | /// |
| 3682 | /// The governing language is C++03 [namespace.udecl]p12: |
| 3683 | /// |
| 3684 | /// When a using-declaration brings names from a base class into a |
| 3685 | /// derived class scope, member functions in the derived class |
| 3686 | /// override and/or hide member functions with the same name and |
| 3687 | /// parameter types in a base class (rather than conflicting). |
| 3688 | /// |
| 3689 | /// There are two ways to implement this: |
| 3690 | /// (1) optimistically create shadow decls when they're not hidden |
| 3691 | /// by existing declarations, or |
| 3692 | /// (2) don't create any shadow decls (or at least don't make them |
| 3693 | /// visible) until we've fully parsed/instantiated the class. |
| 3694 | /// The problem with (1) is that we might have to retroactively remove |
| 3695 | /// a shadow decl, which requires several O(n) operations because the |
| 3696 | /// decl structures are (very reasonably) not designed for removal. |
| 3697 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 3698 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3699 | if (Shadow->getDeclName().getNameKind() == |
| 3700 | DeclarationName::CXXConversionFunctionName) |
| 3701 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 3702 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3703 | // Remove it from the DeclContext... |
| 3704 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3705 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3706 | // ...and the scope, if applicable... |
| 3707 | if (S) { |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3708 | S->RemoveDecl(Shadow); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3709 | IdResolver.RemoveDecl(Shadow); |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3712 | // ...and the using decl. |
| 3713 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 3714 | |
| 3715 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3716 | // be possible for this to happen, because...? |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3717 | } |
| 3718 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3719 | /// Builds a using declaration. |
| 3720 | /// |
| 3721 | /// \param IsInstantiation - Whether this call arises from an |
| 3722 | /// instantiation of an unresolved using declaration. We treat |
| 3723 | /// the lookup differently for these declarations. |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3724 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 3725 | SourceLocation UsingLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3726 | CXXScopeSpec &SS, |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3727 | const DeclarationNameInfo &NameInfo, |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3728 | AttributeList *AttrList, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3729 | bool IsInstantiation, |
| 3730 | bool IsTypeName, |
| 3731 | SourceLocation TypenameLoc) { |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3732 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3733 | SourceLocation IdentLoc = NameInfo.getLoc(); |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3734 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 561154d | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 3735 | |
Anders Carlsson | f038fc2 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3736 | // FIXME: We ignore attributes for now. |
| 3737 | delete AttrList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3739 | if (SS.isEmpty()) { |
| 3740 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3741 | return 0; |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3742 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3743 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3744 | // Do the redeclaration lookup in the current scope. |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3745 | LookupResult Previous(*this, NameInfo, LookupUsingDeclName, |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3746 | ForRedeclaration); |
| 3747 | Previous.setHideTags(false); |
| 3748 | if (S) { |
| 3749 | LookupName(Previous, S); |
| 3750 | |
| 3751 | // It is really dumb that we have to do this. |
| 3752 | LookupResult::Filter F = Previous.makeFilter(); |
| 3753 | while (F.hasNext()) { |
| 3754 | NamedDecl *D = F.next(); |
| 3755 | if (!isDeclInScope(D, CurContext, S)) |
| 3756 | F.erase(); |
| 3757 | } |
| 3758 | F.done(); |
| 3759 | } else { |
| 3760 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 3761 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 3762 | LookupQualifiedName(Previous, CurContext); |
| 3763 | } |
| 3764 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3765 | NestedNameSpecifier *NNS = |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3766 | static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3767 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3768 | // Check for invalid redeclarations. |
| 3769 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 3770 | return 0; |
| 3771 | |
| 3772 | // Check for bad qualifiers. |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3773 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 3774 | return 0; |
| 3775 | |
John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3776 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3777 | NamedDecl *D; |
John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3778 | if (!LookupContext) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3779 | if (IsTypeName) { |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3780 | // FIXME: not all declaration name kinds are legal here |
| 3781 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 3782 | UsingLoc, TypenameLoc, |
| 3783 | SS.getRange(), NNS, |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3784 | IdentLoc, NameInfo.getName()); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3785 | } else { |
| 3786 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3787 | UsingLoc, SS.getRange(), |
| 3788 | NNS, NameInfo); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3789 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3790 | } else { |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3791 | D = UsingDecl::Create(Context, CurContext, |
| 3792 | SS.getRange(), UsingLoc, NNS, NameInfo, |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3793 | IsTypeName); |
Anders Carlsson | f038fc2 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3794 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3795 | D->setAccess(AS); |
| 3796 | CurContext->addDecl(D); |
| 3797 | |
| 3798 | if (!LookupContext) return D; |
| 3799 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3800 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3801 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3802 | UD->setInvalidDecl(); |
| 3803 | return UD; |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3806 | // Look up the target name. |
| 3807 | |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3808 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3809 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3810 | // Unlike most lookups, we don't always want to hide tag |
| 3811 | // declarations: tag names are visible through the using declaration |
| 3812 | // even if hidden by ordinary names, *except* in a dependent context |
| 3813 | // where it's important for the sanity of two-phase lookup. |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3814 | if (!IsInstantiation) |
| 3815 | R.setHideTags(false); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3816 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3817 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3818 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3819 | if (R.empty()) { |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 3820 | Diag(IdentLoc, diag::err_no_member) |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 3821 | << NameInfo.getName() << LookupContext << SS.getRange(); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3822 | UD->setInvalidDecl(); |
| 3823 | return UD; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3826 | if (R.isAmbiguous()) { |
| 3827 | UD->setInvalidDecl(); |
| 3828 | return UD; |
| 3829 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3830 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3831 | if (IsTypeName) { |
| 3832 | // If we asked for a typename and got a non-type decl, error out. |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3833 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3834 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 3835 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 3836 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 3837 | diag::note_using_decl_target); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3838 | UD->setInvalidDecl(); |
| 3839 | return UD; |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3840 | } |
| 3841 | } else { |
| 3842 | // If we asked for a non-typename and we got a type, error out, |
| 3843 | // but only if this is an instantiation of an unresolved using |
| 3844 | // decl. Otherwise just silently find the type name. |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3845 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3846 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 3847 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3848 | UD->setInvalidDecl(); |
| 3849 | return UD; |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3850 | } |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
Anders Carlsson | 5a9c5ac | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3853 | // C++0x N2914 [namespace.udecl]p6: |
| 3854 | // A using-declaration shall not name a namespace. |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3855 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 5a9c5ac | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3856 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 3857 | << SS.getRange(); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3858 | UD->setInvalidDecl(); |
| 3859 | return UD; |
Anders Carlsson | 5a9c5ac | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3860 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3861 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3862 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 3863 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 3864 | BuildUsingShadowDecl(S, UD, *I); |
| 3865 | } |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3866 | |
| 3867 | return UD; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3870 | /// Checks that the given using declaration is not an invalid |
| 3871 | /// redeclaration. Note that this is checking only for the using decl |
| 3872 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 3873 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 3874 | bool isTypeName, |
| 3875 | const CXXScopeSpec &SS, |
| 3876 | SourceLocation NameLoc, |
| 3877 | const LookupResult &Prev) { |
| 3878 | // C++03 [namespace.udecl]p8: |
| 3879 | // C++0x [namespace.udecl]p10: |
| 3880 | // A using-declaration is a declaration and can therefore be used |
| 3881 | // repeatedly where (and only where) multiple declarations are |
| 3882 | // allowed. |
Douglas Gregor | 4b718ee | 2010-05-06 23:31:27 +0000 | [diff] [blame] | 3883 | // |
| 3884 | // That's in non-member contexts. |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3885 | if (!CurContext->getRedeclContext()->isRecord()) |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3886 | return false; |
| 3887 | |
| 3888 | NestedNameSpecifier *Qual |
| 3889 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 3890 | |
| 3891 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 3892 | NamedDecl *D = *I; |
| 3893 | |
| 3894 | bool DTypename; |
| 3895 | NestedNameSpecifier *DQual; |
| 3896 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 3897 | DTypename = UD->isTypeName(); |
| 3898 | DQual = UD->getTargetNestedNameDecl(); |
| 3899 | } else if (UnresolvedUsingValueDecl *UD |
| 3900 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 3901 | DTypename = false; |
| 3902 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3903 | } else if (UnresolvedUsingTypenameDecl *UD |
| 3904 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 3905 | DTypename = true; |
| 3906 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3907 | } else continue; |
| 3908 | |
| 3909 | // using decls differ if one says 'typename' and the other doesn't. |
| 3910 | // FIXME: non-dependent using decls? |
| 3911 | if (isTypeName != DTypename) continue; |
| 3912 | |
| 3913 | // using decls differ if they name different scopes (but note that |
| 3914 | // template instantiation can cause this check to trigger when it |
| 3915 | // didn't before instantiation). |
| 3916 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 3917 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 3918 | continue; |
| 3919 | |
| 3920 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3921 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3922 | return true; |
| 3923 | } |
| 3924 | |
| 3925 | return false; |
| 3926 | } |
| 3927 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3928 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3929 | /// Checks that the given nested-name qualifier used in a using decl |
| 3930 | /// in the current context is appropriately related to the current |
| 3931 | /// scope. If an error is found, diagnoses it and returns true. |
| 3932 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 3933 | const CXXScopeSpec &SS, |
| 3934 | SourceLocation NameLoc) { |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3935 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3936 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3937 | if (!CurContext->isRecord()) { |
| 3938 | // C++03 [namespace.udecl]p3: |
| 3939 | // C++0x [namespace.udecl]p8: |
| 3940 | // A using-declaration for a class member shall be a member-declaration. |
| 3941 | |
| 3942 | // If we weren't able to compute a valid scope, it must be a |
| 3943 | // dependent class scope. |
| 3944 | if (!NamedContext || NamedContext->isRecord()) { |
| 3945 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 3946 | << SS.getRange(); |
| 3947 | return true; |
| 3948 | } |
| 3949 | |
| 3950 | // Otherwise, everything is known to be fine. |
| 3951 | return false; |
| 3952 | } |
| 3953 | |
| 3954 | // The current scope is a record. |
| 3955 | |
| 3956 | // If the named context is dependent, we can't decide much. |
| 3957 | if (!NamedContext) { |
| 3958 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 3959 | // nested-name-specifier does not refer to a base class, which is |
| 3960 | // still possible in some cases. |
| 3961 | |
| 3962 | // Otherwise we have to conservatively report that things might be |
| 3963 | // okay. |
| 3964 | return false; |
| 3965 | } |
| 3966 | |
| 3967 | if (!NamedContext->isRecord()) { |
| 3968 | // Ideally this would point at the last name in the specifier, |
| 3969 | // but we don't have that level of source info. |
| 3970 | Diag(SS.getRange().getBegin(), |
| 3971 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 3972 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 3973 | return true; |
| 3974 | } |
| 3975 | |
| 3976 | if (getLangOptions().CPlusPlus0x) { |
| 3977 | // C++0x [namespace.udecl]p3: |
| 3978 | // In a using-declaration used as a member-declaration, the |
| 3979 | // nested-name-specifier shall name a base class of the class |
| 3980 | // being defined. |
| 3981 | |
| 3982 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 3983 | cast<CXXRecordDecl>(NamedContext))) { |
| 3984 | if (CurContext == NamedContext) { |
| 3985 | Diag(NameLoc, |
| 3986 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3987 | << SS.getRange(); |
| 3988 | return true; |
| 3989 | } |
| 3990 | |
| 3991 | Diag(SS.getRange().getBegin(), |
| 3992 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3993 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 3994 | << cast<CXXRecordDecl>(CurContext) |
| 3995 | << SS.getRange(); |
| 3996 | return true; |
| 3997 | } |
| 3998 | |
| 3999 | return false; |
| 4000 | } |
| 4001 | |
| 4002 | // C++03 [namespace.udecl]p4: |
| 4003 | // A using-declaration used as a member-declaration shall refer |
| 4004 | // to a member of a base class of the class being defined [etc.]. |
| 4005 | |
| 4006 | // Salient point: SS doesn't have to name a base class as long as |
| 4007 | // lookup only finds members from base classes. Therefore we can |
| 4008 | // diagnose here only if we can prove that that can't happen, |
| 4009 | // i.e. if the class hierarchies provably don't intersect. |
| 4010 | |
| 4011 | // TODO: it would be nice if "definitely valid" results were cached |
| 4012 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 4013 | // need to be repeated. |
| 4014 | |
| 4015 | struct UserData { |
| 4016 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 4017 | |
| 4018 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 4019 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 4020 | Data->Bases.insert(Base); |
| 4021 | return true; |
| 4022 | } |
| 4023 | |
| 4024 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 4025 | return !Class->forallBases(collect, this); |
| 4026 | } |
| 4027 | |
| 4028 | /// Returns true if the base is dependent or is one of the |
| 4029 | /// accumulated base classes. |
| 4030 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 4031 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 4032 | return !Data->Bases.count(Base); |
| 4033 | } |
| 4034 | |
| 4035 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 4036 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 4037 | } |
| 4038 | }; |
| 4039 | |
| 4040 | UserData Data; |
| 4041 | |
| 4042 | // Returns false if we find a dependent base. |
| 4043 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 4044 | return false; |
| 4045 | |
| 4046 | // Returns false if the class has a dependent base or if it or one |
| 4047 | // of its bases is present in the base set of the current context. |
| 4048 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 4049 | return false; |
| 4050 | |
| 4051 | Diag(SS.getRange().getBegin(), |
| 4052 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 4053 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 4054 | << cast<CXXRecordDecl>(CurContext) |
| 4055 | << SS.getRange(); |
| 4056 | |
| 4057 | return true; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4060 | Decl *Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 47952ae | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 4061 | SourceLocation NamespaceLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4062 | SourceLocation AliasLoc, |
| 4063 | IdentifierInfo *Alias, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4064 | CXXScopeSpec &SS, |
Anders Carlsson | 47952ae | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 4065 | SourceLocation IdentLoc, |
| 4066 | IdentifierInfo *Ident) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4067 | |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4068 | // Lookup the namespace name. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4069 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 4070 | LookupParsedName(R, S, &SS); |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4071 | |
Anders Carlsson | dca83c4 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4072 | // Check if we have a previous declaration with the same name. |
Douglas Gregor | 5cf8d67 | 2010-05-03 15:37:31 +0000 | [diff] [blame] | 4073 | NamedDecl *PrevDecl |
| 4074 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 4075 | ForRedeclaration); |
| 4076 | if (PrevDecl && !isDeclInScope(PrevDecl, CurContext, S)) |
| 4077 | PrevDecl = 0; |
| 4078 | |
| 4079 | if (PrevDecl) { |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4080 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4081 | // We already have an alias with the same name that points to the same |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4082 | // namespace, so don't create a new one. |
Douglas Gregor | 4667eff | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4083 | // FIXME: At some point, we'll want to create the (redundant) |
| 4084 | // declaration to maintain better source information. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4085 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | 4667eff | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4086 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4087 | return 0; |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4088 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4089 | |
Anders Carlsson | dca83c4 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4090 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 4091 | diag::err_redefinition_different_kind; |
| 4092 | Diag(AliasLoc, DiagID) << Alias; |
| 4093 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4094 | return 0; |
Anders Carlsson | dca83c4 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4097 | if (R.isAmbiguous()) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4098 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4099 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4100 | if (R.empty()) { |
Douglas Gregor | 9629e9a | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4101 | if (DeclarationName Corrected = CorrectTypo(R, S, &SS, 0, false, |
| 4102 | CTC_NoKeywords, 0)) { |
| 4103 | if (R.getAsSingle<NamespaceDecl>() || |
| 4104 | R.getAsSingle<NamespaceAliasDecl>()) { |
| 4105 | if (DeclContext *DC = computeDeclContext(SS, false)) |
| 4106 | Diag(IdentLoc, diag::err_using_directive_member_suggest) |
| 4107 | << Ident << DC << Corrected << SS.getRange() |
| 4108 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4109 | else |
| 4110 | Diag(IdentLoc, diag::err_using_directive_suggest) |
| 4111 | << Ident << Corrected |
| 4112 | << FixItHint::CreateReplacement(IdentLoc, Corrected.getAsString()); |
| 4113 | |
| 4114 | Diag(R.getFoundDecl()->getLocation(), diag::note_namespace_defined_here) |
| 4115 | << Corrected; |
| 4116 | |
| 4117 | Ident = Corrected.getAsIdentifierInfo(); |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 4118 | } else { |
| 4119 | R.clear(); |
| 4120 | R.setLookupName(Ident); |
Douglas Gregor | 9629e9a | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | if (R.empty()) { |
| 4125 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4126 | return 0; |
Douglas Gregor | 9629e9a | 2010-06-29 18:55:19 +0000 | [diff] [blame] | 4127 | } |
Anders Carlsson | ac2c965 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 4128 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4129 | |
Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4130 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4131 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 4132 | Alias, SS.getRange(), |
Douglas Gregor | 1823193 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 4133 | (NestedNameSpecifier *)SS.getScopeRep(), |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4134 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4135 | |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 4136 | PushOnScopeChains(AliasDecl, S); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4137 | return AliasDecl; |
Anders Carlsson | 9205d55 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4140 | namespace { |
| 4141 | /// \brief Scoped object used to handle the state changes required in Sema |
| 4142 | /// to implicitly define the body of a C++ member function; |
| 4143 | class ImplicitlyDefinedFunctionScope { |
| 4144 | Sema &S; |
| 4145 | DeclContext *PreviousContext; |
| 4146 | |
| 4147 | public: |
| 4148 | ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method) |
| 4149 | : S(S), PreviousContext(S.CurContext) |
| 4150 | { |
| 4151 | S.CurContext = Method; |
| 4152 | S.PushFunctionScope(); |
| 4153 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 4154 | } |
| 4155 | |
| 4156 | ~ImplicitlyDefinedFunctionScope() { |
| 4157 | S.PopExpressionEvaluationContext(); |
| 4158 | S.PopFunctionOrBlockScope(); |
| 4159 | S.CurContext = PreviousContext; |
| 4160 | } |
| 4161 | }; |
| 4162 | } |
| 4163 | |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4164 | static CXXConstructorDecl *getDefaultConstructorUnsafe(Sema &Self, |
| 4165 | CXXRecordDecl *D) { |
| 4166 | ASTContext &Context = Self.Context; |
| 4167 | QualType ClassType = Context.getTypeDeclType(D); |
| 4168 | DeclarationName ConstructorName |
| 4169 | = Context.DeclarationNames.getCXXConstructorName( |
| 4170 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
| 4171 | |
| 4172 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 4173 | for (llvm::tie(Con, ConEnd) = D->lookup(ConstructorName); |
| 4174 | Con != ConEnd; ++Con) { |
| 4175 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 4176 | if (isa<FunctionTemplateDecl>(*Con)) |
| 4177 | continue; |
| 4178 | |
| 4179 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 4180 | if (Constructor->isDefaultConstructor()) |
| 4181 | return Constructor; |
| 4182 | } |
| 4183 | return 0; |
| 4184 | } |
| 4185 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4186 | CXXConstructorDecl *Sema::DeclareImplicitDefaultConstructor( |
| 4187 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4188 | // C++ [class.ctor]p5: |
| 4189 | // A default constructor for a class X is a constructor of class X |
| 4190 | // that can be called without an argument. If there is no |
| 4191 | // user-declared constructor for class X, a default constructor is |
| 4192 | // implicitly declared. An implicitly-declared default constructor |
| 4193 | // is an inline public member of its class. |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4194 | assert(!ClassDecl->hasUserDeclaredConstructor() && |
| 4195 | "Should not build implicit default constructor!"); |
| 4196 | |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4197 | // C++ [except.spec]p14: |
| 4198 | // An implicitly declared special member function (Clause 12) shall have an |
| 4199 | // exception-specification. [...] |
| 4200 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4201 | |
| 4202 | // Direct base-class destructors. |
| 4203 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4204 | BEnd = ClassDecl->bases_end(); |
| 4205 | B != BEnd; ++B) { |
| 4206 | if (B->isVirtual()) // Handled below. |
| 4207 | continue; |
| 4208 | |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4209 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 4210 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4211 | if (!BaseClassDecl->hasDeclaredDefaultConstructor()) |
| 4212 | ExceptSpec.CalledDecl(DeclareImplicitDefaultConstructor(BaseClassDecl)); |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4213 | else if (CXXConstructorDecl *Constructor |
| 4214 | = getDefaultConstructorUnsafe(*this, BaseClassDecl)) |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4215 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4216 | } |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4217 | } |
| 4218 | |
| 4219 | // Virtual base-class destructors. |
| 4220 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 4221 | BEnd = ClassDecl->vbases_end(); |
| 4222 | B != BEnd; ++B) { |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4223 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) { |
| 4224 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 4225 | if (!BaseClassDecl->hasDeclaredDefaultConstructor()) |
| 4226 | ExceptSpec.CalledDecl(DeclareImplicitDefaultConstructor(BaseClassDecl)); |
| 4227 | else if (CXXConstructorDecl *Constructor |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4228 | = getDefaultConstructorUnsafe(*this, BaseClassDecl)) |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4229 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4230 | } |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | // Field destructors. |
| 4234 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4235 | FEnd = ClassDecl->field_end(); |
| 4236 | F != FEnd; ++F) { |
| 4237 | if (const RecordType *RecordTy |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4238 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) { |
| 4239 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4240 | if (!FieldClassDecl->hasDeclaredDefaultConstructor()) |
| 4241 | ExceptSpec.CalledDecl( |
| 4242 | DeclareImplicitDefaultConstructor(FieldClassDecl)); |
| 4243 | else if (CXXConstructorDecl *Constructor |
Sebastian Redl | c15c326 | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 4244 | = getDefaultConstructorUnsafe(*this, FieldClassDecl)) |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4245 | ExceptSpec.CalledDecl(Constructor); |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4246 | } |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4247 | } |
| 4248 | |
| 4249 | |
| 4250 | // Create the actual constructor declaration. |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4251 | CanQualType ClassType |
| 4252 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 4253 | DeclarationName Name |
| 4254 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4255 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4256 | CXXConstructorDecl *DefaultCon |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4257 | = CXXConstructorDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4258 | Context.getFunctionType(Context.VoidTy, |
| 4259 | 0, 0, false, 0, |
Douglas Gregor | 6d880b1 | 2010-07-01 22:31:05 +0000 | [diff] [blame] | 4260 | ExceptSpec.hasExceptionSpecification(), |
| 4261 | ExceptSpec.hasAnyExceptionSpecification(), |
| 4262 | ExceptSpec.size(), |
| 4263 | ExceptSpec.data(), |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4264 | FunctionType::ExtInfo()), |
| 4265 | /*TInfo=*/0, |
| 4266 | /*isExplicit=*/false, |
| 4267 | /*isInline=*/true, |
| 4268 | /*isImplicitlyDeclared=*/true); |
| 4269 | DefaultCon->setAccess(AS_public); |
| 4270 | DefaultCon->setImplicit(); |
| 4271 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4272 | |
| 4273 | // Note that we have declared this constructor. |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4274 | ++ASTContext::NumImplicitDefaultConstructorsDeclared; |
| 4275 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4276 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4277 | PushOnScopeChains(DefaultCon, S, false); |
| 4278 | ClassDecl->addDecl(DefaultCon); |
| 4279 | |
Douglas Gregor | 4e8b5fb | 2010-07-01 22:02:46 +0000 | [diff] [blame] | 4280 | return DefaultCon; |
| 4281 | } |
| 4282 | |
Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4283 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 4284 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 18eb69a | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4285 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4286 | !Constructor->isUsed(false)) && |
Fariborz Jahanian | 18eb69a | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4287 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4288 | |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 4289 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4290 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | d7686ef | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 4291 | |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4292 | ImplicitlyDefinedFunctionScope Scope(*this, Constructor); |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4293 | ErrorTrap Trap(*this); |
| 4294 | if (SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false) || |
| 4295 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4296 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Anders Carlsson | 05bf009 | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 4297 | << CXXConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4298 | Constructor->setInvalidDecl(); |
Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4299 | return; |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4300 | } |
Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4301 | |
| 4302 | SourceLocation Loc = Constructor->getLocation(); |
| 4303 | Constructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 4304 | |
| 4305 | Constructor->setUsed(); |
| 4306 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4307 | } |
| 4308 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4309 | CXXDestructorDecl *Sema::DeclareImplicitDestructor(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4310 | // C++ [class.dtor]p2: |
| 4311 | // If a class has no user-declared destructor, a destructor is |
| 4312 | // declared implicitly. An implicitly-declared destructor is an |
| 4313 | // inline public member of its class. |
| 4314 | |
| 4315 | // C++ [except.spec]p14: |
| 4316 | // An implicitly declared special member function (Clause 12) shall have |
| 4317 | // an exception-specification. |
| 4318 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4319 | |
| 4320 | // Direct base-class destructors. |
| 4321 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->bases_begin(), |
| 4322 | BEnd = ClassDecl->bases_end(); |
| 4323 | B != BEnd; ++B) { |
| 4324 | if (B->isVirtual()) // Handled below. |
| 4325 | continue; |
| 4326 | |
| 4327 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 4328 | ExceptSpec.CalledDecl( |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4329 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
| 4332 | // Virtual base-class destructors. |
| 4333 | for (CXXRecordDecl::base_class_iterator B = ClassDecl->vbases_begin(), |
| 4334 | BEnd = ClassDecl->vbases_end(); |
| 4335 | B != BEnd; ++B) { |
| 4336 | if (const RecordType *BaseType = B->getType()->getAs<RecordType>()) |
| 4337 | ExceptSpec.CalledDecl( |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4338 | LookupDestructor(cast<CXXRecordDecl>(BaseType->getDecl()))); |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
| 4341 | // Field destructors. |
| 4342 | for (RecordDecl::field_iterator F = ClassDecl->field_begin(), |
| 4343 | FEnd = ClassDecl->field_end(); |
| 4344 | F != FEnd; ++F) { |
| 4345 | if (const RecordType *RecordTy |
| 4346 | = Context.getBaseElementType(F->getType())->getAs<RecordType>()) |
| 4347 | ExceptSpec.CalledDecl( |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4348 | LookupDestructor(cast<CXXRecordDecl>(RecordTy->getDecl()))); |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4351 | // Create the actual destructor declaration. |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4352 | QualType Ty = Context.getFunctionType(Context.VoidTy, |
| 4353 | 0, 0, false, 0, |
| 4354 | ExceptSpec.hasExceptionSpecification(), |
| 4355 | ExceptSpec.hasAnyExceptionSpecification(), |
| 4356 | ExceptSpec.size(), |
| 4357 | ExceptSpec.data(), |
| 4358 | FunctionType::ExtInfo()); |
| 4359 | |
| 4360 | CanQualType ClassType |
| 4361 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
| 4362 | DeclarationName Name |
| 4363 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4364 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4365 | CXXDestructorDecl *Destructor |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4366 | = CXXDestructorDecl::Create(Context, ClassDecl, NameInfo, Ty, |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4367 | /*isInline=*/true, |
| 4368 | /*isImplicitlyDeclared=*/true); |
| 4369 | Destructor->setAccess(AS_public); |
| 4370 | Destructor->setImplicit(); |
| 4371 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4372 | |
| 4373 | // Note that we have declared this destructor. |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4374 | ++ASTContext::NumImplicitDestructorsDeclared; |
| 4375 | |
| 4376 | // Introduce this destructor into its scope. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4377 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4378 | PushOnScopeChains(Destructor, S, false); |
| 4379 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4380 | |
| 4381 | // This could be uniqued if it ever proves significant. |
| 4382 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
| 4383 | |
| 4384 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 4385 | |
Douglas Gregor | f120304 | 2010-07-01 19:09:28 +0000 | [diff] [blame] | 4386 | return Destructor; |
| 4387 | } |
| 4388 | |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4389 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | d94105a | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 4390 | CXXDestructorDecl *Destructor) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4391 | assert((Destructor->isImplicit() && !Destructor->isUsed(false)) && |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4392 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4393 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4394 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4395 | |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4396 | if (Destructor->isInvalidDecl()) |
| 4397 | return; |
| 4398 | |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4399 | ImplicitlyDefinedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4400 | |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4401 | ErrorTrap Trap(*this); |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 4402 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 4403 | Destructor->getParent()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4404 | |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4405 | if (CheckDestructor(Destructor) || Trap.hasErrorOccurred()) { |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4406 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4407 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 4408 | |
| 4409 | Destructor->setInvalidDecl(); |
| 4410 | return; |
| 4411 | } |
| 4412 | |
Douglas Gregor | 7319327 | 2010-09-20 16:48:21 +0000 | [diff] [blame] | 4413 | SourceLocation Loc = Destructor->getLocation(); |
| 4414 | Destructor->setBody(new (Context) CompoundStmt(Context, 0, 0, Loc, Loc)); |
| 4415 | |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4416 | Destructor->setUsed(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4417 | MarkVTableUsed(CurrentLocation, ClassDecl); |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4420 | /// \brief Builds a statement that copies the given entity from \p From to |
| 4421 | /// \c To. |
| 4422 | /// |
| 4423 | /// This routine is used to copy the members of a class with an |
| 4424 | /// implicitly-declared copy assignment operator. When the entities being |
| 4425 | /// copied are arrays, this routine builds for loops to copy them. |
| 4426 | /// |
| 4427 | /// \param S The Sema object used for type-checking. |
| 4428 | /// |
| 4429 | /// \param Loc The location where the implicit copy is being generated. |
| 4430 | /// |
| 4431 | /// \param T The type of the expressions being copied. Both expressions must |
| 4432 | /// have this type. |
| 4433 | /// |
| 4434 | /// \param To The expression we are copying to. |
| 4435 | /// |
| 4436 | /// \param From The expression we are copying from. |
| 4437 | /// |
Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4438 | /// \param CopyingBaseSubobject Whether we're copying a base subobject. |
| 4439 | /// Otherwise, it's a non-static member subobject. |
| 4440 | /// |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4441 | /// \param Depth Internal parameter recording the depth of the recursion. |
| 4442 | /// |
| 4443 | /// \returns A statement or a loop that copies the expressions. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4444 | static StmtResult |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4445 | BuildSingleCopyAssign(Sema &S, SourceLocation Loc, QualType T, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4446 | Expr *To, Expr *From, |
Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4447 | bool CopyingBaseSubobject, unsigned Depth = 0) { |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4448 | // C++0x [class.copy]p30: |
| 4449 | // Each subobject is assigned in the manner appropriate to its type: |
| 4450 | // |
| 4451 | // - if the subobject is of class type, the copy assignment operator |
| 4452 | // for the class is used (as if by explicit qualification; that is, |
| 4453 | // ignoring any possible virtual overriding functions in more derived |
| 4454 | // classes); |
| 4455 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 4456 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 4457 | |
| 4458 | // Look for operator=. |
| 4459 | DeclarationName Name |
| 4460 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 4461 | LookupResult OpLookup(S, Name, Loc, Sema::LookupOrdinaryName); |
| 4462 | S.LookupQualifiedName(OpLookup, ClassDecl, false); |
| 4463 | |
| 4464 | // Filter out any result that isn't a copy-assignment operator. |
| 4465 | LookupResult::Filter F = OpLookup.makeFilter(); |
| 4466 | while (F.hasNext()) { |
| 4467 | NamedDecl *D = F.next(); |
| 4468 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) |
| 4469 | if (Method->isCopyAssignmentOperator()) |
| 4470 | continue; |
| 4471 | |
| 4472 | F.erase(); |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4473 | } |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4474 | F.done(); |
| 4475 | |
Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4476 | // Suppress the protected check (C++ [class.protected]) for each of the |
| 4477 | // assignment operators we found. This strange dance is required when |
| 4478 | // we're assigning via a base classes's copy-assignment operator. To |
| 4479 | // ensure that we're getting the right base class subobject (without |
| 4480 | // ambiguities), we need to cast "this" to that subobject type; to |
| 4481 | // ensure that we don't go through the virtual call mechanism, we need |
| 4482 | // to qualify the operator= name with the base class (see below). However, |
| 4483 | // this means that if the base class has a protected copy assignment |
| 4484 | // operator, the protected member access check will fail. So, we |
| 4485 | // rewrite "protected" access to "public" access in this case, since we |
| 4486 | // know by construction that we're calling from a derived class. |
| 4487 | if (CopyingBaseSubobject) { |
| 4488 | for (LookupResult::iterator L = OpLookup.begin(), LEnd = OpLookup.end(); |
| 4489 | L != LEnd; ++L) { |
| 4490 | if (L.getAccess() == AS_protected) |
| 4491 | L.setAccess(AS_public); |
| 4492 | } |
| 4493 | } |
| 4494 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4495 | // Create the nested-name-specifier that will be used to qualify the |
| 4496 | // reference to operator=; this is required to suppress the virtual |
| 4497 | // call mechanism. |
| 4498 | CXXScopeSpec SS; |
| 4499 | SS.setRange(Loc); |
| 4500 | SS.setScopeRep(NestedNameSpecifier::Create(S.Context, 0, false, |
| 4501 | T.getTypePtr())); |
| 4502 | |
| 4503 | // Create the reference to operator=. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4504 | ExprResult OpEqualRef |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4505 | = S.BuildMemberReferenceExpr(To, T, Loc, /*isArrow=*/false, SS, |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4506 | /*FirstQualifierInScope=*/0, OpLookup, |
| 4507 | /*TemplateArgs=*/0, |
| 4508 | /*SuppressQualifierCheck=*/true); |
| 4509 | if (OpEqualRef.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4510 | return StmtError(); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4511 | |
| 4512 | // Build the call to the assignment operator. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4513 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4514 | ExprResult Call = S.BuildCallToMemberFunction(/*Scope=*/0, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4515 | OpEqualRef.takeAs<Expr>(), |
| 4516 | Loc, &From, 1, Loc); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4517 | if (Call.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4518 | return StmtError(); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4519 | |
| 4520 | return S.Owned(Call.takeAs<Stmt>()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4521 | } |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4522 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4523 | // - if the subobject is of scalar type, the built-in assignment |
| 4524 | // operator is used. |
| 4525 | const ConstantArrayType *ArrayTy = S.Context.getAsConstantArrayType(T); |
| 4526 | if (!ArrayTy) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4527 | ExprResult Assignment = S.CreateBuiltinBinOp(Loc, BO_Assign, To, From); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4528 | if (Assignment.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4529 | return StmtError(); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4530 | |
| 4531 | return S.Owned(Assignment.takeAs<Stmt>()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4532 | } |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4533 | |
| 4534 | // - if the subobject is an array, each element is assigned, in the |
| 4535 | // manner appropriate to the element type; |
| 4536 | |
| 4537 | // Construct a loop over the array bounds, e.g., |
| 4538 | // |
| 4539 | // for (__SIZE_TYPE__ i0 = 0; i0 != array-size; ++i0) |
| 4540 | // |
| 4541 | // that will copy each of the array elements. |
| 4542 | QualType SizeType = S.Context.getSizeType(); |
| 4543 | |
| 4544 | // Create the iteration variable. |
| 4545 | IdentifierInfo *IterationVarName = 0; |
| 4546 | { |
| 4547 | llvm::SmallString<8> Str; |
| 4548 | llvm::raw_svector_ostream OS(Str); |
| 4549 | OS << "__i" << Depth; |
| 4550 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 4551 | } |
| 4552 | VarDecl *IterationVar = VarDecl::Create(S.Context, S.CurContext, Loc, |
| 4553 | IterationVarName, SizeType, |
| 4554 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4555 | SC_None, SC_None); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4556 | |
| 4557 | // Initialize the iteration variable to zero. |
| 4558 | llvm::APInt Zero(S.Context.getTypeSize(SizeType), 0); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4559 | IterationVar->setInit(IntegerLiteral::Create(S.Context, Zero, SizeType, Loc)); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4560 | |
| 4561 | // Create a reference to the iteration variable; we'll use this several |
| 4562 | // times throughout. |
| 4563 | Expr *IterationVarRef |
| 4564 | = S.BuildDeclRefExpr(IterationVar, SizeType, Loc).takeAs<Expr>(); |
| 4565 | assert(IterationVarRef && "Reference to invented variable cannot fail!"); |
| 4566 | |
| 4567 | // Create the DeclStmt that holds the iteration variable. |
| 4568 | Stmt *InitStmt = new (S.Context) DeclStmt(DeclGroupRef(IterationVar),Loc,Loc); |
| 4569 | |
| 4570 | // Create the comparison against the array bound. |
| 4571 | llvm::APInt Upper = ArrayTy->getSize(); |
| 4572 | Upper.zextOrTrunc(S.Context.getTypeSize(SizeType)); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4573 | Expr *Comparison |
| 4574 | = new (S.Context) BinaryOperator(IterationVarRef->Retain(), |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 4575 | IntegerLiteral::Create(S.Context, |
| 4576 | Upper, SizeType, Loc), |
| 4577 | BO_NE, S.Context.BoolTy, Loc); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4578 | |
| 4579 | // Create the pre-increment of the iteration variable. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4580 | Expr *Increment |
| 4581 | = new (S.Context) UnaryOperator(IterationVarRef->Retain(), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4582 | UO_PreInc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4583 | SizeType, Loc); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4584 | |
| 4585 | // Subscript the "from" and "to" expressions with the iteration variable. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4586 | From = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(From, Loc, |
| 4587 | IterationVarRef, Loc)); |
| 4588 | To = AssertSuccess(S.CreateBuiltinArraySubscriptExpr(To, Loc, |
| 4589 | IterationVarRef, Loc)); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4590 | |
| 4591 | // Build the copy for an individual element of the array. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4592 | StmtResult Copy = BuildSingleCopyAssign(S, Loc, |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4593 | ArrayTy->getElementType(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4594 | To, From, |
Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 4595 | CopyingBaseSubobject, Depth+1); |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 4596 | if (Copy.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4597 | return StmtError(); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4598 | |
| 4599 | // Construct the loop that copies all elements of this array. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4600 | return S.ActOnForStmt(Loc, Loc, InitStmt, |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4601 | S.MakeFullExpr(Comparison), |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4602 | 0, S.MakeFullExpr(Increment), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4603 | Loc, Copy.take()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4604 | } |
| 4605 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4606 | /// \brief Determine whether the given class has a copy assignment operator |
| 4607 | /// that accepts a const-qualified argument. |
| 4608 | static bool hasConstCopyAssignment(Sema &S, const CXXRecordDecl *CClass) { |
| 4609 | CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(CClass); |
| 4610 | |
| 4611 | if (!Class->hasDeclaredCopyAssignment()) |
| 4612 | S.DeclareImplicitCopyAssignment(Class); |
| 4613 | |
| 4614 | QualType ClassType = S.Context.getCanonicalType(S.Context.getTypeDeclType(Class)); |
| 4615 | DeclarationName OpName |
| 4616 | = S.Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 4617 | |
| 4618 | DeclContext::lookup_const_iterator Op, OpEnd; |
| 4619 | for (llvm::tie(Op, OpEnd) = Class->lookup(OpName); Op != OpEnd; ++Op) { |
| 4620 | // C++ [class.copy]p9: |
| 4621 | // A user-declared copy assignment operator is a non-static non-template |
| 4622 | // member function of class X with exactly one parameter of type X, X&, |
| 4623 | // const X&, volatile X& or const volatile X&. |
| 4624 | const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); |
| 4625 | if (!Method) |
| 4626 | continue; |
| 4627 | |
| 4628 | if (Method->isStatic()) |
| 4629 | continue; |
| 4630 | if (Method->getPrimaryTemplate()) |
| 4631 | continue; |
| 4632 | const FunctionProtoType *FnType = |
| 4633 | Method->getType()->getAs<FunctionProtoType>(); |
| 4634 | assert(FnType && "Overloaded operator has no prototype."); |
| 4635 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 4636 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 4637 | continue; |
| 4638 | bool AcceptsConst = true; |
| 4639 | QualType ArgType = FnType->getArgType(0); |
| 4640 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()){ |
| 4641 | ArgType = Ref->getPointeeType(); |
| 4642 | // Is it a non-const lvalue reference? |
| 4643 | if (!ArgType.isConstQualified()) |
| 4644 | AcceptsConst = false; |
| 4645 | } |
| 4646 | if (!S.Context.hasSameUnqualifiedType(ArgType, ClassType)) |
| 4647 | continue; |
| 4648 | |
| 4649 | // We have a single argument of type cv X or cv X&, i.e. we've found the |
| 4650 | // copy assignment operator. Return whether it accepts const arguments. |
| 4651 | return AcceptsConst; |
| 4652 | } |
| 4653 | assert(Class->isInvalidDecl() && |
| 4654 | "No copy assignment operator declared in valid code."); |
| 4655 | return false; |
| 4656 | } |
| 4657 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4658 | CXXMethodDecl *Sema::DeclareImplicitCopyAssignment(CXXRecordDecl *ClassDecl) { |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4659 | // Note: The following rules are largely analoguous to the copy |
| 4660 | // constructor rules. Note that virtual bases are not taken into account |
| 4661 | // for determining the argument type of the operator. Note also that |
| 4662 | // operators taking an object instead of a reference are allowed. |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 4663 | |
| 4664 | |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4665 | // C++ [class.copy]p10: |
| 4666 | // If the class definition does not explicitly declare a copy |
| 4667 | // assignment operator, one is declared implicitly. |
| 4668 | // The implicitly-defined copy assignment operator for a class X |
| 4669 | // will have the form |
| 4670 | // |
| 4671 | // X& X::operator=(const X&) |
| 4672 | // |
| 4673 | // if |
| 4674 | bool HasConstCopyAssignment = true; |
| 4675 | |
| 4676 | // -- each direct base class B of X has a copy assignment operator |
| 4677 | // whose parameter is of type const B&, const volatile B& or B, |
| 4678 | // and |
| 4679 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4680 | BaseEnd = ClassDecl->bases_end(); |
| 4681 | HasConstCopyAssignment && Base != BaseEnd; ++Base) { |
| 4682 | assert(!Base->getType()->isDependentType() && |
| 4683 | "Cannot generate implicit members for class with dependent bases."); |
| 4684 | const CXXRecordDecl *BaseClassDecl |
| 4685 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4686 | HasConstCopyAssignment = hasConstCopyAssignment(*this, BaseClassDecl); |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
| 4689 | // -- for all the nonstatic data members of X that are of a class |
| 4690 | // type M (or array thereof), each such class type has a copy |
| 4691 | // assignment operator whose parameter is of type const M&, |
| 4692 | // const volatile M& or M. |
| 4693 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4694 | FieldEnd = ClassDecl->field_end(); |
| 4695 | HasConstCopyAssignment && Field != FieldEnd; |
| 4696 | ++Field) { |
| 4697 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 4698 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 4699 | const CXXRecordDecl *FieldClassDecl |
| 4700 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4701 | HasConstCopyAssignment = hasConstCopyAssignment(*this, FieldClassDecl); |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4702 | } |
| 4703 | } |
| 4704 | |
| 4705 | // Otherwise, the implicitly declared copy assignment operator will |
| 4706 | // have the form |
| 4707 | // |
| 4708 | // X& X::operator=(X&) |
| 4709 | QualType ArgType = Context.getTypeDeclType(ClassDecl); |
| 4710 | QualType RetType = Context.getLValueReferenceType(ArgType); |
| 4711 | if (HasConstCopyAssignment) |
| 4712 | ArgType = ArgType.withConst(); |
| 4713 | ArgType = Context.getLValueReferenceType(ArgType); |
| 4714 | |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4715 | // C++ [except.spec]p14: |
| 4716 | // An implicitly declared special member function (Clause 12) shall have an |
| 4717 | // exception-specification. [...] |
| 4718 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 4719 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4720 | BaseEnd = ClassDecl->bases_end(); |
| 4721 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4722 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4723 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4724 | |
| 4725 | if (!BaseClassDecl->hasDeclaredCopyAssignment()) |
| 4726 | DeclareImplicitCopyAssignment(BaseClassDecl); |
| 4727 | |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4728 | if (CXXMethodDecl *CopyAssign |
| 4729 | = BaseClassDecl->getCopyAssignmentOperator(HasConstCopyAssignment)) |
| 4730 | ExceptSpec.CalledDecl(CopyAssign); |
| 4731 | } |
| 4732 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4733 | FieldEnd = ClassDecl->field_end(); |
| 4734 | Field != FieldEnd; |
| 4735 | ++Field) { |
| 4736 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 4737 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4738 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4739 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4740 | |
| 4741 | if (!FieldClassDecl->hasDeclaredCopyAssignment()) |
| 4742 | DeclareImplicitCopyAssignment(FieldClassDecl); |
| 4743 | |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4744 | if (CXXMethodDecl *CopyAssign |
| 4745 | = FieldClassDecl->getCopyAssignmentOperator(HasConstCopyAssignment)) |
| 4746 | ExceptSpec.CalledDecl(CopyAssign); |
| 4747 | } |
| 4748 | } |
| 4749 | |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4750 | // An implicitly-declared copy assignment operator is an inline public |
| 4751 | // member of its class. |
| 4752 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4753 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4754 | CXXMethodDecl *CopyAssignment |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4755 | = CXXMethodDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4756 | Context.getFunctionType(RetType, &ArgType, 1, |
| 4757 | false, 0, |
Douglas Gregor | 68e1136 | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 4758 | ExceptSpec.hasExceptionSpecification(), |
| 4759 | ExceptSpec.hasAnyExceptionSpecification(), |
| 4760 | ExceptSpec.size(), |
| 4761 | ExceptSpec.data(), |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4762 | FunctionType::ExtInfo()), |
| 4763 | /*TInfo=*/0, /*isStatic=*/false, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4764 | /*StorageClassAsWritten=*/SC_None, |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4765 | /*isInline=*/true); |
| 4766 | CopyAssignment->setAccess(AS_public); |
| 4767 | CopyAssignment->setImplicit(); |
| 4768 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4769 | |
| 4770 | // Add the parameter to the operator. |
| 4771 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 4772 | ClassDecl->getLocation(), |
| 4773 | /*Id=*/0, |
| 4774 | ArgType, /*TInfo=*/0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 4775 | SC_None, |
| 4776 | SC_None, 0); |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4777 | CopyAssignment->setParams(&FromParam, 1); |
| 4778 | |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4779 | // Note that we have added this copy-assignment operator. |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4780 | ++ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
| 4781 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4782 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 4783 | PushOnScopeChains(CopyAssignment, S, false); |
| 4784 | ClassDecl->addDecl(CopyAssignment); |
Douglas Gregor | f56ab7b | 2010-07-01 16:36:15 +0000 | [diff] [blame] | 4785 | |
| 4786 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
| 4787 | return CopyAssignment; |
| 4788 | } |
| 4789 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4790 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 4791 | CXXMethodDecl *CopyAssignOperator) { |
| 4792 | assert((CopyAssignOperator->isImplicit() && |
| 4793 | CopyAssignOperator->isOverloadedOperator() && |
| 4794 | CopyAssignOperator->getOverloadedOperator() == OO_Equal && |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 4795 | !CopyAssignOperator->isUsed(false)) && |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4796 | "DefineImplicitCopyAssignment called for wrong function"); |
| 4797 | |
| 4798 | CXXRecordDecl *ClassDecl = CopyAssignOperator->getParent(); |
| 4799 | |
| 4800 | if (ClassDecl->isInvalidDecl() || CopyAssignOperator->isInvalidDecl()) { |
| 4801 | CopyAssignOperator->setInvalidDecl(); |
| 4802 | return; |
| 4803 | } |
| 4804 | |
| 4805 | CopyAssignOperator->setUsed(); |
| 4806 | |
| 4807 | ImplicitlyDefinedFunctionScope Scope(*this, CopyAssignOperator); |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 4808 | ErrorTrap Trap(*this); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4809 | |
| 4810 | // C++0x [class.copy]p30: |
| 4811 | // The implicitly-defined or explicitly-defaulted copy assignment operator |
| 4812 | // for a non-union class X performs memberwise copy assignment of its |
| 4813 | // subobjects. The direct base classes of X are assigned first, in the |
| 4814 | // order of their declaration in the base-specifier-list, and then the |
| 4815 | // immediate non-static data members of X are assigned, in the order in |
| 4816 | // which they were declared in the class definition. |
| 4817 | |
| 4818 | // The statements that form the synthesized function body. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4819 | ASTOwningVector<Stmt*> Statements(*this); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4820 | |
| 4821 | // The parameter for the "other" object, which we are copying from. |
| 4822 | ParmVarDecl *Other = CopyAssignOperator->getParamDecl(0); |
| 4823 | Qualifiers OtherQuals = Other->getType().getQualifiers(); |
| 4824 | QualType OtherRefType = Other->getType(); |
| 4825 | if (const LValueReferenceType *OtherRef |
| 4826 | = OtherRefType->getAs<LValueReferenceType>()) { |
| 4827 | OtherRefType = OtherRef->getPointeeType(); |
| 4828 | OtherQuals = OtherRefType.getQualifiers(); |
| 4829 | } |
| 4830 | |
| 4831 | // Our location for everything implicitly-generated. |
| 4832 | SourceLocation Loc = CopyAssignOperator->getLocation(); |
| 4833 | |
| 4834 | // Construct a reference to the "other" object. We'll be using this |
| 4835 | // throughout the generated ASTs. |
| 4836 | Expr *OtherRef = BuildDeclRefExpr(Other, OtherRefType, Loc).takeAs<Expr>(); |
| 4837 | assert(OtherRef && "Reference to parameter cannot fail!"); |
| 4838 | |
| 4839 | // Construct the "this" pointer. We'll be using this throughout the generated |
| 4840 | // ASTs. |
| 4841 | Expr *This = ActOnCXXThis(Loc).takeAs<Expr>(); |
| 4842 | assert(This && "Reference to this cannot fail!"); |
| 4843 | |
| 4844 | // Assign base classes. |
| 4845 | bool Invalid = false; |
| 4846 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4847 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 4848 | // Form the assignment: |
| 4849 | // static_cast<Base*>(this)->Base::operator=(static_cast<Base&>(other)); |
| 4850 | QualType BaseType = Base->getType().getUnqualifiedType(); |
| 4851 | CXXRecordDecl *BaseClassDecl = 0; |
| 4852 | if (const RecordType *BaseRecordT = BaseType->getAs<RecordType>()) |
| 4853 | BaseClassDecl = cast<CXXRecordDecl>(BaseRecordT->getDecl()); |
| 4854 | else { |
| 4855 | Invalid = true; |
| 4856 | continue; |
| 4857 | } |
| 4858 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4859 | CXXCastPath BasePath; |
| 4860 | BasePath.push_back(Base); |
| 4861 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4862 | // Construct the "from" expression, which is an implicit cast to the |
| 4863 | // appropriately-qualified base type. |
| 4864 | Expr *From = OtherRef->Retain(); |
| 4865 | ImpCastExprToType(From, Context.getQualifiedType(BaseType, OtherQuals), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4866 | CK_UncheckedDerivedToBase, |
| 4867 | VK_LValue, &BasePath); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4868 | |
| 4869 | // Dereference "this". |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4870 | ExprResult To = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4871 | |
| 4872 | // Implicitly cast "this" to the appropriately-qualified base type. |
| 4873 | Expr *ToE = To.takeAs<Expr>(); |
| 4874 | ImpCastExprToType(ToE, |
| 4875 | Context.getCVRQualifiedType(BaseType, |
| 4876 | CopyAssignOperator->getTypeQualifiers()), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4877 | CK_UncheckedDerivedToBase, |
| 4878 | VK_LValue, &BasePath); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4879 | To = Owned(ToE); |
| 4880 | |
| 4881 | // Build the copy. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4882 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4883 | To.get(), From, |
| 4884 | /*CopyingBaseSubobject=*/true); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4885 | if (Copy.isInvalid()) { |
Douglas Gregor | bf1fb44 | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 4886 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4887 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 4888 | CopyAssignOperator->setInvalidDecl(); |
| 4889 | return; |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4890 | } |
| 4891 | |
| 4892 | // Success! Record the copy. |
| 4893 | Statements.push_back(Copy.takeAs<Expr>()); |
| 4894 | } |
| 4895 | |
| 4896 | // \brief Reference to the __builtin_memcpy function. |
| 4897 | Expr *BuiltinMemCpyRef = 0; |
Fariborz Jahanian | 4a30307 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 4898 | // \brief Reference to the __builtin_objc_memmove_collectable function. |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4899 | Expr *CollectableMemCpyRef = 0; |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4900 | |
| 4901 | // Assign non-static members. |
| 4902 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4903 | FieldEnd = ClassDecl->field_end(); |
| 4904 | Field != FieldEnd; ++Field) { |
| 4905 | // Check for members of reference type; we can't copy those. |
| 4906 | if (Field->getType()->isReferenceType()) { |
| 4907 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 4908 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 4909 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | bf1fb44 | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 4910 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4911 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4912 | Invalid = true; |
| 4913 | continue; |
| 4914 | } |
| 4915 | |
| 4916 | // Check for members of const-qualified, non-class type. |
| 4917 | QualType BaseType = Context.getBaseElementType(Field->getType()); |
| 4918 | if (!BaseType->getAs<RecordType>() && BaseType.isConstQualified()) { |
| 4919 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
| 4920 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 4921 | Diag(Field->getLocation(), diag::note_declared_at); |
Douglas Gregor | bf1fb44 | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 4922 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4923 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4924 | Invalid = true; |
| 4925 | continue; |
| 4926 | } |
| 4927 | |
| 4928 | QualType FieldType = Field->getType().getNonReferenceType(); |
Fariborz Jahanian | 1138ba6 | 2010-05-26 20:19:07 +0000 | [diff] [blame] | 4929 | if (FieldType->isIncompleteArrayType()) { |
| 4930 | assert(ClassDecl->hasFlexibleArrayMember() && |
| 4931 | "Incomplete array type is not valid"); |
| 4932 | continue; |
| 4933 | } |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4934 | |
| 4935 | // Build references to the field in the object we're copying from and to. |
| 4936 | CXXScopeSpec SS; // Intentionally empty |
| 4937 | LookupResult MemberLookup(*this, Field->getDeclName(), Loc, |
| 4938 | LookupMemberName); |
| 4939 | MemberLookup.addDecl(*Field); |
| 4940 | MemberLookup.resolveKind(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4941 | ExprResult From = BuildMemberReferenceExpr(OtherRef, OtherRefType, |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4942 | Loc, /*IsArrow=*/false, |
| 4943 | SS, 0, MemberLookup, 0); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4944 | ExprResult To = BuildMemberReferenceExpr(This, This->getType(), |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4945 | Loc, /*IsArrow=*/true, |
| 4946 | SS, 0, MemberLookup, 0); |
| 4947 | assert(!From.isInvalid() && "Implicit field reference cannot fail"); |
| 4948 | assert(!To.isInvalid() && "Implicit field reference cannot fail"); |
| 4949 | |
| 4950 | // If the field should be copied with __builtin_memcpy rather than via |
| 4951 | // explicit assignments, do so. This optimization only applies for arrays |
| 4952 | // of scalars and arrays of class type with trivial copy-assignment |
| 4953 | // operators. |
| 4954 | if (FieldType->isArrayType() && |
| 4955 | (!BaseType->isRecordType() || |
| 4956 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()) |
| 4957 | ->hasTrivialCopyAssignment())) { |
| 4958 | // Compute the size of the memory buffer to be copied. |
| 4959 | QualType SizeType = Context.getSizeType(); |
| 4960 | llvm::APInt Size(Context.getTypeSize(SizeType), |
| 4961 | Context.getTypeSizeInChars(BaseType).getQuantity()); |
| 4962 | for (const ConstantArrayType *Array |
| 4963 | = Context.getAsConstantArrayType(FieldType); |
| 4964 | Array; |
| 4965 | Array = Context.getAsConstantArrayType(Array->getElementType())) { |
| 4966 | llvm::APInt ArraySize = Array->getSize(); |
| 4967 | ArraySize.zextOrTrunc(Size.getBitWidth()); |
| 4968 | Size *= ArraySize; |
| 4969 | } |
| 4970 | |
| 4971 | // Take the address of the field references for "from" and "to". |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4972 | From = CreateBuiltinUnaryOp(Loc, UO_AddrOf, From.get()); |
| 4973 | To = CreateBuiltinUnaryOp(Loc, UO_AddrOf, To.get()); |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4974 | |
| 4975 | bool NeedsCollectableMemCpy = |
| 4976 | (BaseType->isRecordType() && |
| 4977 | BaseType->getAs<RecordType>()->getDecl()->hasObjectMember()); |
| 4978 | |
| 4979 | if (NeedsCollectableMemCpy) { |
| 4980 | if (!CollectableMemCpyRef) { |
Fariborz Jahanian | 4a30307 | 2010-06-16 16:22:04 +0000 | [diff] [blame] | 4981 | // Create a reference to the __builtin_objc_memmove_collectable function. |
| 4982 | LookupResult R(*this, |
| 4983 | &Context.Idents.get("__builtin_objc_memmove_collectable"), |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4984 | Loc, LookupOrdinaryName); |
| 4985 | LookupName(R, TUScope, true); |
| 4986 | |
| 4987 | FunctionDecl *CollectableMemCpy = R.getAsSingle<FunctionDecl>(); |
| 4988 | if (!CollectableMemCpy) { |
| 4989 | // Something went horribly wrong earlier, and we will have |
| 4990 | // complained about it. |
| 4991 | Invalid = true; |
| 4992 | continue; |
| 4993 | } |
| 4994 | |
| 4995 | CollectableMemCpyRef = BuildDeclRefExpr(CollectableMemCpy, |
| 4996 | CollectableMemCpy->getType(), |
| 4997 | Loc, 0).takeAs<Expr>(); |
| 4998 | assert(CollectableMemCpyRef && "Builtin reference cannot fail"); |
| 4999 | } |
| 5000 | } |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5001 | // Create a reference to the __builtin_memcpy builtin function. |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5002 | else if (!BuiltinMemCpyRef) { |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5003 | LookupResult R(*this, &Context.Idents.get("__builtin_memcpy"), Loc, |
| 5004 | LookupOrdinaryName); |
| 5005 | LookupName(R, TUScope, true); |
| 5006 | |
| 5007 | FunctionDecl *BuiltinMemCpy = R.getAsSingle<FunctionDecl>(); |
| 5008 | if (!BuiltinMemCpy) { |
| 5009 | // Something went horribly wrong earlier, and we will have complained |
| 5010 | // about it. |
| 5011 | Invalid = true; |
| 5012 | continue; |
| 5013 | } |
| 5014 | |
| 5015 | BuiltinMemCpyRef = BuildDeclRefExpr(BuiltinMemCpy, |
| 5016 | BuiltinMemCpy->getType(), |
| 5017 | Loc, 0).takeAs<Expr>(); |
| 5018 | assert(BuiltinMemCpyRef && "Builtin reference cannot fail"); |
| 5019 | } |
| 5020 | |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5021 | ASTOwningVector<Expr*> CallArgs(*this); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5022 | CallArgs.push_back(To.takeAs<Expr>()); |
| 5023 | CallArgs.push_back(From.takeAs<Expr>()); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 5024 | CallArgs.push_back(IntegerLiteral::Create(Context, Size, SizeType, Loc)); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5025 | ExprResult Call = ExprError(); |
Fariborz Jahanian | 00bdca5 | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5026 | if (NeedsCollectableMemCpy) |
| 5027 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5028 | CollectableMemCpyRef, |
Fariborz Jahanian | 00bdca5 | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5029 | Loc, move_arg(CallArgs), |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5030 | Loc); |
Fariborz Jahanian | 00bdca5 | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5031 | else |
| 5032 | Call = ActOnCallExpr(/*Scope=*/0, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5033 | BuiltinMemCpyRef, |
Fariborz Jahanian | 00bdca5 | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5034 | Loc, move_arg(CallArgs), |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5035 | Loc); |
Fariborz Jahanian | 00bdca5 | 2010-06-16 00:16:38 +0000 | [diff] [blame] | 5036 | |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5037 | assert(!Call.isInvalid() && "Call to __builtin_memcpy cannot fail!"); |
| 5038 | Statements.push_back(Call.takeAs<Expr>()); |
| 5039 | continue; |
| 5040 | } |
| 5041 | |
| 5042 | // Build the copy of this field. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5043 | StmtResult Copy = BuildSingleCopyAssign(*this, Loc, FieldType, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5044 | To.get(), From.get(), |
Douglas Gregor | 40c92bb | 2010-05-04 15:20:55 +0000 | [diff] [blame] | 5045 | /*CopyingBaseSubobject=*/false); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5046 | if (Copy.isInvalid()) { |
Douglas Gregor | bf1fb44 | 2010-05-05 22:38:15 +0000 | [diff] [blame] | 5047 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5048 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5049 | CopyAssignOperator->setInvalidDecl(); |
| 5050 | return; |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5051 | } |
| 5052 | |
| 5053 | // Success! Record the copy. |
| 5054 | Statements.push_back(Copy.takeAs<Stmt>()); |
| 5055 | } |
| 5056 | |
| 5057 | if (!Invalid) { |
| 5058 | // Add a "return *this;" |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5059 | ExprResult ThisObj = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5060 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5061 | StmtResult Return = ActOnReturnStmt(Loc, ThisObj.get()); |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5062 | if (Return.isInvalid()) |
| 5063 | Invalid = true; |
| 5064 | else { |
| 5065 | Statements.push_back(Return.takeAs<Stmt>()); |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5066 | |
| 5067 | if (Trap.hasErrorOccurred()) { |
| 5068 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 5069 | << CXXCopyAssignment << Context.getTagDeclType(ClassDecl); |
| 5070 | Invalid = true; |
| 5071 | } |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5072 | } |
| 5073 | } |
| 5074 | |
| 5075 | if (Invalid) { |
| 5076 | CopyAssignOperator->setInvalidDecl(); |
| 5077 | return; |
| 5078 | } |
| 5079 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5080 | StmtResult Body = ActOnCompoundStmt(Loc, Loc, move_arg(Statements), |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 5081 | /*isStmtExpr=*/false); |
| 5082 | assert(!Body.isInvalid() && "Compound statement creation cannot fail"); |
| 5083 | CopyAssignOperator->setBody(Body.takeAs<Stmt>()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 5084 | } |
| 5085 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5086 | CXXConstructorDecl *Sema::DeclareImplicitCopyConstructor( |
| 5087 | CXXRecordDecl *ClassDecl) { |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5088 | // C++ [class.copy]p4: |
| 5089 | // If the class definition does not explicitly declare a copy |
| 5090 | // constructor, one is declared implicitly. |
| 5091 | |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5092 | // C++ [class.copy]p5: |
| 5093 | // The implicitly-declared copy constructor for a class X will |
| 5094 | // have the form |
| 5095 | // |
| 5096 | // X::X(const X&) |
| 5097 | // |
| 5098 | // if |
| 5099 | bool HasConstCopyConstructor = true; |
| 5100 | |
| 5101 | // -- each direct or virtual base class B of X has a copy |
| 5102 | // constructor whose first parameter is of type const B& or |
| 5103 | // const volatile B&, and |
| 5104 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5105 | BaseEnd = ClassDecl->bases_end(); |
| 5106 | HasConstCopyConstructor && Base != BaseEnd; |
| 5107 | ++Base) { |
Douglas Gregor | cfe6822 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5108 | // Virtual bases are handled below. |
| 5109 | if (Base->isVirtual()) |
| 5110 | continue; |
| 5111 | |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5112 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | cfe6822 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5113 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5114 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5115 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5116 | |
Douglas Gregor | cfe6822 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5117 | HasConstCopyConstructor |
| 5118 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 5119 | } |
| 5120 | |
| 5121 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 5122 | BaseEnd = ClassDecl->vbases_end(); |
| 5123 | HasConstCopyConstructor && Base != BaseEnd; |
| 5124 | ++Base) { |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5125 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5126 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5127 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5128 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5129 | |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5130 | HasConstCopyConstructor |
| 5131 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 5132 | } |
| 5133 | |
| 5134 | // -- for all the nonstatic data members of X that are of a |
| 5135 | // class type M (or array thereof), each such class type |
| 5136 | // has a copy constructor whose first parameter is of type |
| 5137 | // const M& or const volatile M&. |
| 5138 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5139 | FieldEnd = ClassDecl->field_end(); |
| 5140 | HasConstCopyConstructor && Field != FieldEnd; |
| 5141 | ++Field) { |
Douglas Gregor | cfe6822 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5142 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5143 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5144 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | cfe6822 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5145 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5146 | if (!FieldClassDecl->hasDeclaredCopyConstructor()) |
| 5147 | DeclareImplicitCopyConstructor(FieldClassDecl); |
| 5148 | |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5149 | HasConstCopyConstructor |
Douglas Gregor | cfe6822 | 2010-07-01 18:27:03 +0000 | [diff] [blame] | 5150 | = FieldClassDecl->hasConstCopyConstructor(Context); |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5151 | } |
| 5152 | } |
| 5153 | |
| 5154 | // Otherwise, the implicitly declared copy constructor will have |
| 5155 | // the form |
| 5156 | // |
| 5157 | // X::X(X&) |
| 5158 | QualType ClassType = Context.getTypeDeclType(ClassDecl); |
| 5159 | QualType ArgType = ClassType; |
| 5160 | if (HasConstCopyConstructor) |
| 5161 | ArgType = ArgType.withConst(); |
| 5162 | ArgType = Context.getLValueReferenceType(ArgType); |
| 5163 | |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5164 | // C++ [except.spec]p14: |
| 5165 | // An implicitly declared special member function (Clause 12) shall have an |
| 5166 | // exception-specification. [...] |
| 5167 | ImplicitExceptionSpecification ExceptSpec(Context); |
| 5168 | unsigned Quals = HasConstCopyConstructor? Qualifiers::Const : 0; |
| 5169 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 5170 | BaseEnd = ClassDecl->bases_end(); |
| 5171 | Base != BaseEnd; |
| 5172 | ++Base) { |
| 5173 | // Virtual bases are handled below. |
| 5174 | if (Base->isVirtual()) |
| 5175 | continue; |
| 5176 | |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5177 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5178 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5179 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5180 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5181 | |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5182 | if (CXXConstructorDecl *CopyConstructor |
| 5183 | = BaseClassDecl->getCopyConstructor(Context, Quals)) |
| 5184 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5185 | } |
| 5186 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->vbases_begin(), |
| 5187 | BaseEnd = ClassDecl->vbases_end(); |
| 5188 | Base != BaseEnd; |
| 5189 | ++Base) { |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5190 | CXXRecordDecl *BaseClassDecl |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5191 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5192 | if (!BaseClassDecl->hasDeclaredCopyConstructor()) |
| 5193 | DeclareImplicitCopyConstructor(BaseClassDecl); |
| 5194 | |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5195 | if (CXXConstructorDecl *CopyConstructor |
| 5196 | = BaseClassDecl->getCopyConstructor(Context, Quals)) |
| 5197 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5198 | } |
| 5199 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 5200 | FieldEnd = ClassDecl->field_end(); |
| 5201 | Field != FieldEnd; |
| 5202 | ++Field) { |
| 5203 | QualType FieldType = Context.getBaseElementType((*Field)->getType()); |
| 5204 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5205 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5206 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5207 | if (!FieldClassDecl->hasDeclaredCopyConstructor()) |
| 5208 | DeclareImplicitCopyConstructor(FieldClassDecl); |
| 5209 | |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5210 | if (CXXConstructorDecl *CopyConstructor |
| 5211 | = FieldClassDecl->getCopyConstructor(Context, Quals)) |
| 5212 | ExceptSpec.CalledDecl(CopyConstructor); |
| 5213 | } |
| 5214 | } |
| 5215 | |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5216 | // An implicitly-declared copy constructor is an inline public |
| 5217 | // member of its class. |
| 5218 | DeclarationName Name |
| 5219 | = Context.DeclarationNames.getCXXConstructorName( |
| 5220 | Context.getCanonicalType(ClassType)); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5221 | DeclarationNameInfo NameInfo(Name, ClassDecl->getLocation()); |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5222 | CXXConstructorDecl *CopyConstructor |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5223 | = CXXConstructorDecl::Create(Context, ClassDecl, NameInfo, |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5224 | Context.getFunctionType(Context.VoidTy, |
| 5225 | &ArgType, 1, |
| 5226 | false, 0, |
Douglas Gregor | 8453ddb | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 5227 | ExceptSpec.hasExceptionSpecification(), |
| 5228 | ExceptSpec.hasAnyExceptionSpecification(), |
| 5229 | ExceptSpec.size(), |
| 5230 | ExceptSpec.data(), |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5231 | FunctionType::ExtInfo()), |
| 5232 | /*TInfo=*/0, |
| 5233 | /*isExplicit=*/false, |
| 5234 | /*isInline=*/true, |
| 5235 | /*isImplicitlyDeclared=*/true); |
| 5236 | CopyConstructor->setAccess(AS_public); |
| 5237 | CopyConstructor->setImplicit(); |
| 5238 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
| 5239 | |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5240 | // Note that we have declared this constructor. |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5241 | ++ASTContext::NumImplicitCopyConstructorsDeclared; |
| 5242 | |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5243 | // Add the parameter to the constructor. |
| 5244 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 5245 | ClassDecl->getLocation(), |
| 5246 | /*IdentifierInfo=*/0, |
| 5247 | ArgType, /*TInfo=*/0, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5248 | SC_None, |
| 5249 | SC_None, 0); |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5250 | CopyConstructor->setParams(&FromParam, 1); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 5251 | if (Scope *S = getScopeForContext(ClassDecl)) |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 5252 | PushOnScopeChains(CopyConstructor, S, false); |
| 5253 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 54be339 | 2010-07-01 17:57:27 +0000 | [diff] [blame] | 5254 | |
| 5255 | return CopyConstructor; |
| 5256 | } |
| 5257 | |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5258 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 5259 | CXXConstructorDecl *CopyConstructor, |
| 5260 | unsigned TypeQuals) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5261 | assert((CopyConstructor->isImplicit() && |
Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 5262 | CopyConstructor->isCopyConstructor(TypeQuals) && |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 5263 | !CopyConstructor->isUsed(false)) && |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5264 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5265 | |
Anders Carlsson | 7a0ffdb | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 5266 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5267 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5268 | |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 5269 | ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor); |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5270 | ErrorTrap Trap(*this); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5271 | |
Douglas Gregor | 54818f0 | 2010-05-12 16:39:35 +0000 | [diff] [blame] | 5272 | if (SetBaseOrMemberInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false) || |
| 5273 | Trap.hasErrorOccurred()) { |
Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 5274 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5275 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame] | 5276 | CopyConstructor->setInvalidDecl(); |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5277 | } else { |
| 5278 | CopyConstructor->setBody(ActOnCompoundStmt(CopyConstructor->getLocation(), |
| 5279 | CopyConstructor->getLocation(), |
| 5280 | MultiStmtArg(*this, 0, 0), |
| 5281 | /*isStmtExpr=*/false) |
| 5282 | .takeAs<Stmt>()); |
Anders Carlsson | 53e1ba9 | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 5283 | } |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 5284 | |
| 5285 | CopyConstructor->setUsed(); |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 5286 | } |
| 5287 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5288 | ExprResult |
Anders Carlsson | 1b4ebfa | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 5289 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5290 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5291 | MultiExprArg ExprArgs, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5292 | bool RequiresZeroInit, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5293 | unsigned ConstructKind) { |
Anders Carlsson | 250aada | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5294 | bool Elidable = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5295 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5296 | // C++0x [class.copy]p34: |
| 5297 | // When certain criteria are met, an implementation is allowed to |
| 5298 | // omit the copy/move construction of a class object, even if the |
| 5299 | // copy/move constructor and/or destructor for the object have |
| 5300 | // side effects. [...] |
| 5301 | // - when a temporary class object that has not been bound to a |
| 5302 | // reference (12.2) would be copied/moved to a class object |
| 5303 | // with the same cv-unqualified type, the copy/move operation |
| 5304 | // can be omitted by constructing the temporary object |
| 5305 | // directly into the target of the omitted copy/move |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5306 | if (ConstructKind == CXXConstructExpr::CK_Complete && |
| 5307 | Constructor->isCopyConstructor() && ExprArgs.size() >= 1) { |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5308 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5309 | Elidable = SubExpr->isTemporaryObject(Context, Constructor->getParent()); |
Anders Carlsson | 250aada | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5310 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5311 | |
| 5312 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5313 | Elidable, move(ExprArgs), RequiresZeroInit, |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 5314 | ConstructKind); |
Anders Carlsson | 250aada | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 5315 | } |
| 5316 | |
Fariborz Jahanian | aa890bf | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5317 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 5318 | /// including handling of its default argument expressions. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5319 | ExprResult |
Anders Carlsson | 1b4ebfa | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 5320 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 5321 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5322 | MultiExprArg ExprArgs, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5323 | bool RequiresZeroInit, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5324 | unsigned ConstructKind) { |
Anders Carlsson | 5995a3e | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 5325 | unsigned NumExprs = ExprArgs.size(); |
| 5326 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5327 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 5328 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5329 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5330 | Constructor, Elidable, Exprs, NumExprs, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5331 | RequiresZeroInit, |
| 5332 | static_cast<CXXConstructExpr::ConstructionKind>(ConstructKind))); |
Fariborz Jahanian | aa890bf | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5333 | } |
| 5334 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5335 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | aa890bf | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 5336 | CXXConstructorDecl *Constructor, |
Anders Carlsson | 5995a3e | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 5337 | MultiExprArg Exprs) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5338 | ExprResult TempResult = |
Fariborz Jahanian | 57277c5 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 5339 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5340 | move(Exprs), false, CXXConstructExpr::CK_Complete); |
Anders Carlsson | c1eb79b | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 5341 | if (TempResult.isInvalid()) |
| 5342 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5343 | |
Anders Carlsson | 6eb5557 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 5344 | Expr *Temp = TempResult.takeAs<Expr>(); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5345 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
Anders Carlsson | 6e997b2 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 5346 | Temp = MaybeCreateCXXExprWithTemporaries(Temp); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5347 | VD->setInit(Temp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5348 | |
Anders Carlsson | c1eb79b | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 5349 | return false; |
Anders Carlsson | e6840d8 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
John McCall | 03c4848 | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 5352 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
| 5353 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Douglas Gregor | 422f155 | 2010-02-25 18:11:54 +0000 | [diff] [blame] | 5354 | if (!ClassDecl->isInvalidDecl() && !VD->isInvalidDecl() && |
Douglas Gregor | 024d80e | 2010-05-22 17:12:29 +0000 | [diff] [blame] | 5355 | !ClassDecl->hasTrivialDestructor() && !ClassDecl->isDependentContext()) { |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5356 | CXXDestructorDecl *Destructor = LookupDestructor(ClassDecl); |
John McCall | 6781b05 | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 5357 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5358 | CheckDestructorAccess(VD->getLocation(), Destructor, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5359 | PDiag(diag::err_access_dtor_var) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5360 | << VD->getDeclName() |
| 5361 | << VD->getType()); |
John McCall | 47e4093 | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 5362 | |
John McCall | 386dfc7 | 2010-09-18 05:25:11 +0000 | [diff] [blame] | 5363 | // TODO: this should be re-enabled for static locals by !CXAAtExit |
| 5364 | if (!VD->isInvalidDecl() && VD->hasGlobalStorage() && !VD->isStaticLocal()) |
John McCall | 47e4093 | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 5365 | Diag(VD->getLocation(), diag::warn_global_destructor); |
John McCall | 6781b05 | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 5366 | } |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 5367 | } |
| 5368 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5369 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5370 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 5371 | /// e.g: "int x(1);" |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5372 | void Sema::AddCXXDirectInitializerToDecl(Decl *RealDecl, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5373 | SourceLocation LParenLoc, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5374 | MultiExprArg Exprs, |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5375 | SourceLocation RParenLoc) { |
Daniel Dunbar | 2db411f | 2009-12-24 19:19:26 +0000 | [diff] [blame] | 5376 | assert(Exprs.size() != 0 && Exprs.get() && "missing expressions"); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5377 | |
| 5378 | // If there is no declaration, there was an error parsing it. Just ignore |
| 5379 | // the initializer. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5380 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5381 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5382 | |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5383 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 5384 | if (!VDecl) { |
| 5385 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 5386 | RealDecl->setInvalidDecl(); |
| 5387 | return; |
| 5388 | } |
| 5389 | |
Douglas Gregor | 402250f | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 5390 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | 997d00d | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 5391 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5392 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 5393 | // |
| 5394 | // Clients that want to distinguish between the two forms, can check for |
| 5395 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 5396 | // A major benefit is that clients that don't particularly care about which |
| 5397 | // exactly form was it (like the CodeGen) can handle both cases without |
| 5398 | // special case code. |
Argyrios Kyrtzidis | 153d967 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 5399 | |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5400 | // C++ 8.5p11: |
| 5401 | // The form of initialization (using parentheses or '=') is generally |
| 5402 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 153d967 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 5403 | // class type. |
| 5404 | |
Douglas Gregor | 50dc219 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 5405 | if (!VDecl->getType()->isDependentType() && |
| 5406 | RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 5407 | diag::err_typecheck_decl_incomplete_type)) { |
| 5408 | VDecl->setInvalidDecl(); |
| 5409 | return; |
| 5410 | } |
| 5411 | |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5412 | // The variable can not have an abstract class type. |
| 5413 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 5414 | diag::err_abstract_type_in_decl, |
| 5415 | AbstractVariableType)) |
| 5416 | VDecl->setInvalidDecl(); |
| 5417 | |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 5418 | const VarDecl *Def; |
| 5419 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5420 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| 5421 | << VDecl->getDeclName(); |
| 5422 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5423 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 153d967 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 5424 | return; |
| 5425 | } |
Douglas Gregor | 50dc219 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 5426 | |
Douglas Gregor | f0f8369 | 2010-08-24 05:27:49 +0000 | [diff] [blame] | 5427 | // C++ [class.static.data]p4 |
| 5428 | // If a static data member is of const integral or const |
| 5429 | // enumeration type, its declaration in the class definition can |
| 5430 | // specify a constant-initializer which shall be an integral |
| 5431 | // constant expression (5.19). In that case, the member can appear |
| 5432 | // in integral constant expressions. The member shall still be |
| 5433 | // defined in a namespace scope if it is used in the program and the |
| 5434 | // namespace scope definition shall not contain an initializer. |
| 5435 | // |
| 5436 | // We already performed a redefinition check above, but for static |
| 5437 | // data members we also need to check whether there was an in-class |
| 5438 | // declaration with an initializer. |
| 5439 | const VarDecl* PrevInit = 0; |
| 5440 | if (VDecl->isStaticDataMember() && VDecl->getAnyInitializer(PrevInit)) { |
| 5441 | Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName(); |
| 5442 | Diag(PrevInit->getLocation(), diag::note_previous_definition); |
| 5443 | return; |
| 5444 | } |
| 5445 | |
Douglas Gregor | 50dc219 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 5446 | // If either the declaration has a dependent type or if any of the |
| 5447 | // expressions is type-dependent, we represent the initialization |
| 5448 | // via a ParenListExpr for later use during template instantiation. |
| 5449 | if (VDecl->getType()->isDependentType() || |
| 5450 | Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { |
| 5451 | // Let clients know that initialization was done with a direct initializer. |
| 5452 | VDecl->setCXXDirectInitializer(true); |
| 5453 | |
| 5454 | // Store the initialization expressions as a ParenListExpr. |
| 5455 | unsigned NumExprs = Exprs.size(); |
| 5456 | VDecl->setInit(new (Context) ParenListExpr(Context, LParenLoc, |
| 5457 | (Expr **)Exprs.release(), |
| 5458 | NumExprs, RParenLoc)); |
| 5459 | return; |
| 5460 | } |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5461 | |
| 5462 | // Capture the variable that is being initialized and the style of |
| 5463 | // initialization. |
| 5464 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 5465 | |
| 5466 | // FIXME: Poor source location information. |
| 5467 | InitializationKind Kind |
| 5468 | = InitializationKind::CreateDirect(VDecl->getLocation(), |
| 5469 | LParenLoc, RParenLoc); |
| 5470 | |
| 5471 | InitializationSequence InitSeq(*this, Entity, Kind, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5472 | Exprs.get(), Exprs.size()); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5473 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(Exprs)); |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5474 | if (Result.isInvalid()) { |
| 5475 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5476 | return; |
| 5477 | } |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 5478 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5479 | Result = MaybeCreateCXXExprWithTemporaries(Result.get()); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5480 | VDecl->setInit(Result.takeAs<Expr>()); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5481 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | 997d00d | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 5482 | |
John McCall | 8b0f4ff | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 5483 | if (!VDecl->isInvalidDecl() && |
| 5484 | !VDecl->getDeclContext()->isDependentContext() && |
Sebastian Redl | 02f1eeb | 2010-09-08 04:46:19 +0000 | [diff] [blame] | 5485 | VDecl->hasGlobalStorage() && !VDecl->isStaticLocal() && |
John McCall | 8b0f4ff | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 5486 | !VDecl->getInit()->isConstantInitializer(Context, |
| 5487 | VDecl->getType()->isReferenceType())) |
| 5488 | Diag(VDecl->getLocation(), diag::warn_global_constructor) |
| 5489 | << VDecl->getInit()->getSourceRange(); |
| 5490 | |
John McCall | 03c4848 | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 5491 | if (const RecordType *Record = VDecl->getType()->getAs<RecordType>()) |
| 5492 | FinalizeVarWithDestructor(VDecl, Record); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5493 | } |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 5494 | |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5495 | /// \brief Given a constructor and the set of arguments provided for the |
| 5496 | /// constructor, convert the arguments and add any required default arguments |
| 5497 | /// to form a proper call to this constructor. |
| 5498 | /// |
| 5499 | /// \returns true if an error occurred, false otherwise. |
| 5500 | bool |
| 5501 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 5502 | MultiExprArg ArgsPtr, |
| 5503 | SourceLocation Loc, |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5504 | ASTOwningVector<Expr*> &ConvertedArgs) { |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5505 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 5506 | unsigned NumArgs = ArgsPtr.size(); |
| 5507 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 5508 | |
| 5509 | const FunctionProtoType *Proto |
| 5510 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 5511 | assert(Proto && "Constructor without a prototype?"); |
| 5512 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5513 | |
| 5514 | // If too few arguments are available, we'll fill in the rest with defaults. |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 5515 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5516 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 5517 | else |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 5518 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 5519 | |
| 5520 | VariadicCallType CallType = |
| 5521 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 5522 | llvm::SmallVector<Expr *, 8> AllArgs; |
| 5523 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 5524 | Proto, 0, Args, NumArgs, AllArgs, |
| 5525 | CallType); |
| 5526 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 5527 | ConvertedArgs.push_back(AllArgs[i]); |
| 5528 | return Invalid; |
Douglas Gregor | c28b57d | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5531 | static inline bool |
| 5532 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 5533 | const FunctionDecl *FnDecl) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5534 | const DeclContext *DC = FnDecl->getDeclContext()->getRedeclContext(); |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5535 | if (isa<NamespaceDecl>(DC)) { |
| 5536 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5537 | diag::err_operator_new_delete_declared_in_namespace) |
| 5538 | << FnDecl->getDeclName(); |
| 5539 | } |
| 5540 | |
| 5541 | if (isa<TranslationUnitDecl>(DC) && |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 5542 | FnDecl->getStorageClass() == SC_Static) { |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5543 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5544 | diag::err_operator_new_delete_declared_static) |
| 5545 | << FnDecl->getDeclName(); |
| 5546 | } |
| 5547 | |
Anders Carlsson | 60659a8 | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 5548 | return false; |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5549 | } |
| 5550 | |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5551 | static inline bool |
| 5552 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 5553 | CanQualType ExpectedResultType, |
| 5554 | CanQualType ExpectedFirstParamType, |
| 5555 | unsigned DependentParamTypeDiag, |
| 5556 | unsigned InvalidParamTypeDiag) { |
| 5557 | QualType ResultType = |
| 5558 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 5559 | |
| 5560 | // Check that the result type is not dependent. |
| 5561 | if (ResultType->isDependentType()) |
| 5562 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5563 | diag::err_operator_new_delete_dependent_result_type) |
| 5564 | << FnDecl->getDeclName() << ExpectedResultType; |
| 5565 | |
| 5566 | // Check that the result type is what we expect. |
| 5567 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 5568 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5569 | diag::err_operator_new_delete_invalid_result_type) |
| 5570 | << FnDecl->getDeclName() << ExpectedResultType; |
| 5571 | |
| 5572 | // A function template must have at least 2 parameters. |
| 5573 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 5574 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5575 | diag::err_operator_new_delete_template_too_few_parameters) |
| 5576 | << FnDecl->getDeclName(); |
| 5577 | |
| 5578 | // The function decl must have at least 1 parameter. |
| 5579 | if (FnDecl->getNumParams() == 0) |
| 5580 | return SemaRef.Diag(FnDecl->getLocation(), |
| 5581 | diag::err_operator_new_delete_too_few_parameters) |
| 5582 | << FnDecl->getDeclName(); |
| 5583 | |
| 5584 | // Check the the first parameter type is not dependent. |
| 5585 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 5586 | if (FirstParamType->isDependentType()) |
| 5587 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 5588 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 5589 | |
| 5590 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 684d7bd | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 5591 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5592 | ExpectedFirstParamType) |
| 5593 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 5594 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 5595 | |
| 5596 | return false; |
| 5597 | } |
| 5598 | |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5599 | static bool |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5600 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5601 | // C++ [basic.stc.dynamic.allocation]p1: |
| 5602 | // A program is ill-formed if an allocation function is declared in a |
| 5603 | // namespace scope other than global scope or declared static in global |
| 5604 | // scope. |
| 5605 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 5606 | return true; |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5607 | |
| 5608 | CanQualType SizeTy = |
| 5609 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 5610 | |
| 5611 | // C++ [basic.stc.dynamic.allocation]p1: |
| 5612 | // The return type shall be void*. The first parameter shall have type |
| 5613 | // std::size_t. |
| 5614 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 5615 | SizeTy, |
| 5616 | diag::err_operator_new_dependent_param_type, |
| 5617 | diag::err_operator_new_param_type)) |
| 5618 | return true; |
| 5619 | |
| 5620 | // C++ [basic.stc.dynamic.allocation]p1: |
| 5621 | // The first parameter shall not have an associated default argument. |
| 5622 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5623 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5624 | diag::err_operator_new_default_arg) |
| 5625 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 5626 | |
| 5627 | return false; |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5628 | } |
| 5629 | |
| 5630 | static bool |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5631 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 5632 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 5633 | // A program is ill-formed if deallocation functions are declared in a |
| 5634 | // namespace scope other than global scope or declared static in global |
| 5635 | // scope. |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 5636 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 5637 | return true; |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5638 | |
| 5639 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 5640 | // Each deallocation function shall return void and its first parameter |
| 5641 | // shall be void*. |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5642 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 5643 | SemaRef.Context.VoidPtrTy, |
| 5644 | diag::err_operator_delete_dependent_param_type, |
| 5645 | diag::err_operator_delete_param_type)) |
| 5646 | return true; |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5647 | |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5648 | return false; |
| 5649 | } |
| 5650 | |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5651 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 5652 | /// of this overloaded operator is well-formed. If so, returns false; |
| 5653 | /// otherwise, emits appropriate diagnostics and returns true. |
| 5654 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5655 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5656 | "Expected an overloaded operator declaration"); |
| 5657 | |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5658 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 5659 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5660 | // C++ [over.oper]p5: |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5661 | // The allocation and deallocation functions, operator new, |
| 5662 | // operator new[], operator delete and operator delete[], are |
| 5663 | // described completely in 3.7.3. The attributes and restrictions |
| 5664 | // found in the rest of this subclause do not apply to them unless |
| 5665 | // explicitly stated in 3.7.3. |
Anders Carlsson | f1f4695 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 5666 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 5667 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | 4e08894 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 5668 | |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5669 | if (Op == OO_New || Op == OO_Array_New) |
| 5670 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5671 | |
| 5672 | // C++ [over.oper]p6: |
| 5673 | // An operator function shall either be a non-static member |
| 5674 | // function or be a non-member function and have at least one |
| 5675 | // parameter whose type is a class, a reference to a class, an |
| 5676 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5677 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 5678 | if (MethodDecl->isStatic()) |
| 5679 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5680 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5681 | } else { |
| 5682 | bool ClassOrEnumParam = false; |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5683 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 5684 | ParamEnd = FnDecl->param_end(); |
| 5685 | Param != ParamEnd; ++Param) { |
| 5686 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 173e0b7a | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 5687 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 5688 | ParamType->isEnumeralType()) { |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5689 | ClassOrEnumParam = true; |
| 5690 | break; |
| 5691 | } |
| 5692 | } |
| 5693 | |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5694 | if (!ClassOrEnumParam) |
| 5695 | return Diag(FnDecl->getLocation(), |
Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5696 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5697 | << FnDecl->getDeclName(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5698 | } |
| 5699 | |
| 5700 | // C++ [over.oper]p8: |
| 5701 | // An operator function cannot have default arguments (8.3.6), |
| 5702 | // except where explicitly stated below. |
| 5703 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5704 | // Only the function-call operator allows default arguments |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5705 | // (C++ [over.call]p1). |
| 5706 | if (Op != OO_Call) { |
| 5707 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 5708 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5709 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5710 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5711 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 5712 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5713 | } |
| 5714 | } |
| 5715 | |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5716 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 5717 | { false, false, false } |
| 5718 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 5719 | , { Unary, Binary, MemberOnly } |
| 5720 | #include "clang/Basic/OperatorKinds.def" |
| 5721 | }; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5722 | |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5723 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 5724 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 5725 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5726 | |
| 5727 | // C++ [over.oper]p8: |
| 5728 | // [...] Operator functions cannot have more or fewer parameters |
| 5729 | // than the number required for the corresponding operator, as |
| 5730 | // described in the rest of this subclause. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5731 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5732 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5733 | if (Op != OO_Call && |
| 5734 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 5735 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 5736 | (NumParams < 1) || (NumParams > 2))) { |
| 5737 | // We have the wrong number of parameters. |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5738 | unsigned ErrorKind; |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5739 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5740 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5741 | } else if (CanBeUnaryOperator) { |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5742 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5743 | } else { |
Chris Lattner | 2b78690 | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 5744 | assert(CanBeBinaryOperator && |
| 5745 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5746 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 5747 | } |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5748 | |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 5749 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5750 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5751 | } |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 5752 | |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5753 | // Overloaded operators other than operator() cannot be variadic. |
| 5754 | if (Op != OO_Call && |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5755 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5756 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5757 | << FnDecl->getDeclName(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5758 | } |
| 5759 | |
| 5760 | // Some operators must be non-static member functions. |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5761 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 5762 | return Diag(FnDecl->getLocation(), |
Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 5763 | diag::err_operator_overload_must_be_member) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5764 | << FnDecl->getDeclName(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
| 5767 | // C++ [over.inc]p1: |
| 5768 | // The user-defined function called operator++ implements the |
| 5769 | // prefix and postfix ++ operator. If this function is a member |
| 5770 | // function with no parameters, or a non-member function with one |
| 5771 | // parameter of class or enumeration type, it defines the prefix |
| 5772 | // increment operator ++ for objects of that type. If the function |
| 5773 | // is a member function with one parameter (which shall be of type |
| 5774 | // int) or a non-member function with two parameters (the second |
| 5775 | // of which shall be of type int), it defines the postfix |
| 5776 | // increment operator ++ for objects of that type. |
| 5777 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 5778 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 5779 | bool ParamIsInt = false; |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5780 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5781 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 5782 | |
Chris Lattner | 2b78690 | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 5783 | if (!ParamIsInt) |
| 5784 | return Diag(LastParam->getLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5785 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5786 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5787 | } |
| 5788 | |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 5789 | return false; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 5790 | } |
Chris Lattner | 3b024a3 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 5791 | |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5792 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 5793 | /// of this literal operator function is well-formed. If so, returns |
| 5794 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 5795 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 5796 | DeclContext *DC = FnDecl->getDeclContext(); |
| 5797 | Decl::Kind Kind = DC->getDeclKind(); |
| 5798 | if (Kind != Decl::TranslationUnit && Kind != Decl::Namespace && |
| 5799 | Kind != Decl::LinkageSpec) { |
| 5800 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 5801 | << FnDecl->getDeclName(); |
| 5802 | return true; |
| 5803 | } |
| 5804 | |
| 5805 | bool Valid = false; |
| 5806 | |
Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5807 | // template <char...> type operator "" name() is the only valid template |
| 5808 | // signature, and the only valid signature with no parameters. |
| 5809 | if (FnDecl->param_size() == 0) { |
| 5810 | if (FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate()) { |
| 5811 | // Must have only one template parameter |
| 5812 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 5813 | if (Params->size() == 1) { |
| 5814 | NonTypeTemplateParmDecl *PmDecl = |
| 5815 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5816 | |
Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5817 | // The template parameter must be a char parameter pack. |
| 5818 | // FIXME: This test will always fail because non-type parameter packs |
| 5819 | // have not been implemented. |
| 5820 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 5821 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 5822 | Valid = true; |
| 5823 | } |
| 5824 | } |
| 5825 | } else { |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5826 | // Check the first parameter |
Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 5827 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 5828 | |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5829 | QualType T = (*Param)->getType(); |
| 5830 | |
Alexis Hunt | 079a6f7 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 5831 | // unsigned long long int, long double, and any character type are allowed |
| 5832 | // as the only parameters. |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5833 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 5834 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 5835 | Context.hasSameType(T, Context.CharTy) || |
| 5836 | Context.hasSameType(T, Context.WCharTy) || |
| 5837 | Context.hasSameType(T, Context.Char16Ty) || |
| 5838 | Context.hasSameType(T, Context.Char32Ty)) { |
| 5839 | if (++Param == FnDecl->param_end()) |
| 5840 | Valid = true; |
| 5841 | goto FinishedParams; |
| 5842 | } |
| 5843 | |
Alexis Hunt | 079a6f7 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 5844 | // Otherwise it must be a pointer to const; let's strip those qualifiers. |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 5845 | const PointerType *PT = T->getAs<PointerType>(); |
| 5846 | if (!PT) |
| 5847 | goto FinishedParams; |
| 5848 | T = PT->getPointeeType(); |
| 5849 | if (!T.isConstQualified()) |
| 5850 | goto FinishedParams; |
| 5851 | T = T.getUnqualifiedType(); |
| 5852 | |
| 5853 | // Move on to the second parameter; |
| 5854 | ++Param; |
| 5855 | |
| 5856 | // If there is no second parameter, the first must be a const char * |
| 5857 | if (Param == FnDecl->param_end()) { |
| 5858 | if (Context.hasSameType(T, Context.CharTy)) |
| 5859 | Valid = true; |
| 5860 | goto FinishedParams; |
| 5861 | } |
| 5862 | |
| 5863 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 5864 | // are allowed as the first parameter to a two-parameter function |
| 5865 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 5866 | Context.hasSameType(T, Context.WCharTy) || |
| 5867 | Context.hasSameType(T, Context.Char16Ty) || |
| 5868 | Context.hasSameType(T, Context.Char32Ty))) |
| 5869 | goto FinishedParams; |
| 5870 | |
| 5871 | // The second and final parameter must be an std::size_t |
| 5872 | T = (*Param)->getType().getUnqualifiedType(); |
| 5873 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 5874 | ++Param == FnDecl->param_end()) |
| 5875 | Valid = true; |
| 5876 | } |
| 5877 | |
| 5878 | // FIXME: This diagnostic is absolutely terrible. |
| 5879 | FinishedParams: |
| 5880 | if (!Valid) { |
| 5881 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 5882 | << FnDecl->getDeclName(); |
| 5883 | return true; |
| 5884 | } |
| 5885 | |
| 5886 | return false; |
| 5887 | } |
| 5888 | |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5889 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 5890 | /// linkage specification, including the language and (if present) |
| 5891 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 5892 | /// the location of the language string literal, which is provided |
| 5893 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 5894 | /// the '{' brace. Otherwise, this linkage specification does not |
| 5895 | /// have any braces. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5896 | Decl *Sema::ActOnStartLinkageSpecification(Scope *S, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5897 | SourceLocation ExternLoc, |
| 5898 | SourceLocation LangLoc, |
Benjamin Kramer | bebee84 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 5899 | llvm::StringRef Lang, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5900 | SourceLocation LBraceLoc) { |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5901 | LinkageSpecDecl::LanguageIDs Language; |
Benjamin Kramer | bebee84 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 5902 | if (Lang == "\"C\"") |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5903 | Language = LinkageSpecDecl::lang_c; |
Benjamin Kramer | bebee84 | 2010-05-03 13:08:54 +0000 | [diff] [blame] | 5904 | else if (Lang == "\"C++\"") |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5905 | Language = LinkageSpecDecl::lang_cxx; |
| 5906 | else { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5907 | Diag(LangLoc, diag::err_bad_language); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5908 | return 0; |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5909 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5910 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5911 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5912 | |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5913 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5914 | LangLoc, Language, |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5915 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5916 | CurContext->addDecl(D); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5917 | PushDeclContext(S, D); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5918 | return D; |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 5919 | } |
| 5920 | |
Abramo Bagnara | ed5b689 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 5921 | /// ActOnFinishLinkageSpecification - Complete the definition of |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5922 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 5923 | /// valid, it's the position of the closing '}' brace in a linkage |
| 5924 | /// specification that uses braces. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5925 | Decl *Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 5926 | Decl *LinkageSpec, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5927 | SourceLocation RBraceLoc) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 5928 | if (LinkageSpec) |
| 5929 | PopDeclContext(); |
| 5930 | return LinkageSpec; |
Chris Lattner | 3b024a3 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 5931 | } |
| 5932 | |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5933 | /// \brief Perform semantic analysis for the variable declaration that |
| 5934 | /// occurs within a C++ catch clause, returning the newly-created |
| 5935 | /// variable. |
Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 5936 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5937 | TypeSourceInfo *TInfo, |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5938 | IdentifierInfo *Name, |
Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 5939 | SourceLocation Loc) { |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5940 | bool Invalid = false; |
Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 5941 | QualType ExDeclType = TInfo->getType(); |
| 5942 | |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5943 | // Arrays and functions decay. |
| 5944 | if (ExDeclType->isArrayType()) |
| 5945 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 5946 | else if (ExDeclType->isFunctionType()) |
| 5947 | ExDeclType = Context.getPointerType(ExDeclType); |
| 5948 | |
| 5949 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 5950 | // The exception-declaration shall not denote a pointer or reference to an |
| 5951 | // incomplete type, other than [cv] void*. |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 5952 | // N2844 forbids rvalue references. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5953 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 5954 | Diag(Loc, diag::err_catch_rvalue_ref); |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 5955 | Invalid = true; |
| 5956 | } |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5957 | |
Douglas Gregor | 104ee00 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 5958 | // GCC allows catching pointers and references to incomplete types |
| 5959 | // as an extension; so do we, but we warn by default. |
| 5960 | |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5961 | QualType BaseType = ExDeclType; |
| 5962 | int Mode = 0; // 0 for direct type, 1 for pointer, 2 for reference |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 5963 | unsigned DK = diag::err_catch_incomplete; |
Douglas Gregor | 104ee00 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 5964 | bool IncompleteCatchIsInvalid = true; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5965 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5966 | BaseType = Ptr->getPointeeType(); |
| 5967 | Mode = 1; |
Douglas Gregor | 104ee00 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 5968 | DK = diag::ext_catch_incomplete_ptr; |
| 5969 | IncompleteCatchIsInvalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5970 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 5971 | // For the purpose of error recovery, we treat rvalue refs like lvalue refs. |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5972 | BaseType = Ref->getPointeeType(); |
| 5973 | Mode = 2; |
Douglas Gregor | 104ee00 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 5974 | DK = diag::ext_catch_incomplete_ref; |
| 5975 | IncompleteCatchIsInvalid = false; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5976 | } |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 5977 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | 104ee00 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 5978 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK) && |
| 5979 | IncompleteCatchIsInvalid) |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5980 | Invalid = true; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5981 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5982 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5983 | RequireNonAbstractType(Loc, ExDeclType, |
| 5984 | diag::err_abstract_type_in_decl, |
| 5985 | AbstractVariableType)) |
Sebastian Redl | 2f38ba5 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 5986 | Invalid = true; |
| 5987 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 5988 | // Only the non-fragile NeXT runtime currently supports C++ catches |
| 5989 | // of ObjC types, and no runtime supports catching ObjC types by value. |
| 5990 | if (!Invalid && getLangOptions().ObjC1) { |
| 5991 | QualType T = ExDeclType; |
| 5992 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 5993 | T = RT->getPointeeType(); |
| 5994 | |
| 5995 | if (T->isObjCObjectType()) { |
| 5996 | Diag(Loc, diag::err_objc_object_catch); |
| 5997 | Invalid = true; |
| 5998 | } else if (T->isObjCObjectPointerType()) { |
| 5999 | if (!getLangOptions().NeXTRuntime) { |
| 6000 | Diag(Loc, diag::err_objc_pointer_cxx_catch_gnu); |
| 6001 | Invalid = true; |
| 6002 | } else if (!getLangOptions().ObjCNonFragileABI) { |
| 6003 | Diag(Loc, diag::err_objc_pointer_cxx_catch_fragile); |
| 6004 | Invalid = true; |
| 6005 | } |
| 6006 | } |
| 6007 | } |
| 6008 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6009 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6010 | Name, ExDeclType, TInfo, SC_None, |
| 6011 | SC_None); |
Douglas Gregor | 3f324d56 | 2010-05-03 18:51:14 +0000 | [diff] [blame] | 6012 | ExDecl->setExceptionVariable(true); |
| 6013 | |
Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6014 | if (!Invalid) { |
| 6015 | if (const RecordType *RecordTy = ExDeclType->getAs<RecordType>()) { |
| 6016 | // C++ [except.handle]p16: |
| 6017 | // The object declared in an exception-declaration or, if the |
| 6018 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 6019 | // copy-initialized (8.5) from the exception object. [...] |
| 6020 | // The object is destroyed when the handler exits, after the destruction |
| 6021 | // of any automatic objects initialized within the handler. |
| 6022 | // |
| 6023 | // We just pretend to initialize the object with itself, then make sure |
| 6024 | // it can be destroyed later. |
| 6025 | InitializedEntity Entity = InitializedEntity::InitializeVariable(ExDecl); |
| 6026 | Expr *ExDeclRef = DeclRefExpr::Create(Context, 0, SourceRange(), ExDecl, |
| 6027 | Loc, ExDeclType, 0); |
| 6028 | InitializationKind Kind = InitializationKind::CreateCopy(Loc, |
| 6029 | SourceLocation()); |
| 6030 | InitializationSequence InitSeq(*this, Entity, Kind, &ExDeclRef, 1); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6031 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 6032 | MultiExprArg(*this, &ExDeclRef, 1)); |
Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 6033 | if (Result.isInvalid()) |
| 6034 | Invalid = true; |
| 6035 | else |
| 6036 | FinalizeVarWithDestructor(ExDecl, RecordTy); |
| 6037 | } |
| 6038 | } |
| 6039 | |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6040 | if (Invalid) |
| 6041 | ExDecl->setInvalidDecl(); |
| 6042 | |
| 6043 | return ExDecl; |
| 6044 | } |
| 6045 | |
| 6046 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 6047 | /// handler. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6048 | Decl *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6049 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 6050 | QualType ExDeclType = TInfo->getType(); |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6051 | |
| 6052 | bool Invalid = D.isInvalidType(); |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6053 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 6054 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 6055 | LookupOrdinaryName, |
| 6056 | ForRedeclaration)) { |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6057 | // The scope should be freshly made just for us. There is just no way |
| 6058 | // it contains any previous declaration. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6059 | assert(!S->isDeclScope(PrevDecl)); |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6060 | if (PrevDecl->isTemplateParameter()) { |
| 6061 | // Maybe we will complain about the shadowed template parameter. |
| 6062 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6063 | } |
| 6064 | } |
| 6065 | |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6066 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6067 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 6068 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6069 | Invalid = true; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6070 | } |
| 6071 | |
Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6072 | VarDecl *ExDecl = BuildExceptionDeclaration(S, TInfo, |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6073 | D.getIdentifier(), |
Douglas Gregor | 9f0e1aa | 2010-09-09 17:09:21 +0000 | [diff] [blame] | 6074 | D.getIdentifierLoc()); |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6075 | |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 6076 | if (Invalid) |
| 6077 | ExDecl->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6078 | |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6079 | // Add the exception declaration into this scope. |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6080 | if (II) |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 6081 | PushOnScopeChains(ExDecl, S); |
| 6082 | else |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6083 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6084 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6085 | ProcessDeclAttributes(S, ExDecl, D); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6086 | return ExDecl; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 6087 | } |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6088 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6089 | Decl *Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6090 | Expr *AssertExpr, |
| 6091 | Expr *AssertMessageExpr_) { |
| 6092 | StringLiteral *AssertMessage = cast<StringLiteral>(AssertMessageExpr_); |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6093 | |
Anders Carlsson | 54b2698 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6094 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 6095 | llvm::APSInt Value(32); |
| 6096 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 6097 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 6098 | AssertExpr->getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6099 | return 0; |
Anders Carlsson | 54b2698 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6100 | } |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6101 | |
Anders Carlsson | 54b2698 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6102 | if (Value == 0) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6103 | Diag(AssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | b11118b | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 6104 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | 54b2698 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 6105 | } |
| 6106 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6107 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6108 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6109 | AssertExpr, AssertMessage); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6110 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6111 | CurContext->addDecl(Decl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6112 | return Decl; |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 6113 | } |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6114 | |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6115 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 6116 | /// |
| 6117 | /// \returns A friend declaration that. |
| 6118 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation FriendLoc, |
| 6119 | TypeSourceInfo *TSInfo) { |
| 6120 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 6121 | |
| 6122 | QualType T = TSInfo->getType(); |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6123 | SourceRange TypeRange = TSInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6124 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 6125 | if (!getLangOptions().CPlusPlus0x) { |
| 6126 | // C++03 [class.friend]p2: |
| 6127 | // An elaborated-type-specifier shall be used in a friend declaration |
| 6128 | // for a class.* |
| 6129 | // |
| 6130 | // * The class-key of the elaborated-type-specifier is required. |
| 6131 | if (!ActiveTemplateInstantiations.empty()) { |
| 6132 | // Do not complain about the form of friend template types during |
| 6133 | // template instantiation; we will already have complained when the |
| 6134 | // template was declared. |
| 6135 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 6136 | // If we evaluated the type to a record type, suggest putting |
| 6137 | // a tag in front. |
| 6138 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 6139 | RecordDecl *RD = RT->getDecl(); |
| 6140 | |
| 6141 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 6142 | |
| 6143 | Diag(TypeRange.getBegin(), diag::ext_unelaborated_friend_type) |
| 6144 | << (unsigned) RD->getTagKind() |
| 6145 | << T |
| 6146 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 6147 | InsertionText); |
| 6148 | } else { |
| 6149 | Diag(FriendLoc, diag::ext_nonclass_type_friend) |
| 6150 | << T |
| 6151 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
| 6152 | } |
| 6153 | } else if (T->getAs<EnumType>()) { |
| 6154 | Diag(FriendLoc, diag::ext_enum_friend) |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6155 | << T |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6156 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6157 | } |
| 6158 | } |
| 6159 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 6160 | // C++0x [class.friend]p3: |
| 6161 | // If the type specifier in a friend declaration designates a (possibly |
| 6162 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 6163 | // the friend declaration is ignored. |
| 6164 | |
| 6165 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 6166 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6167 | |
| 6168 | return FriendDecl::Create(Context, CurContext, FriendLoc, TSInfo, FriendLoc); |
| 6169 | } |
| 6170 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6171 | /// Handle a friend type declaration. This works in tandem with |
| 6172 | /// ActOnTag. |
| 6173 | /// |
| 6174 | /// Notes on friend class templates: |
| 6175 | /// |
| 6176 | /// We generally treat friend class declarations as if they were |
| 6177 | /// declaring a class. So, for example, the elaborated type specifier |
| 6178 | /// in a friend declaration is required to obey the restrictions of a |
| 6179 | /// class-head (i.e. no typedefs in the scope chain), template |
| 6180 | /// parameters are required to match up with simple template-ids, &c. |
| 6181 | /// However, unlike when declaring a template specialization, it's |
| 6182 | /// okay to refer to a template specialization without an empty |
| 6183 | /// template parameter declaration, e.g. |
| 6184 | /// friend class A<T>::B<unsigned>; |
| 6185 | /// We permit this as a special case; if there are any template |
| 6186 | /// parameters present at all, require proper matching, i.e. |
| 6187 | /// template <> template <class T> friend class A<int>::B; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6188 | Decl *Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6189 | MultiTemplateParamsArg TempParams) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6190 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6191 | |
| 6192 | assert(DS.isFriendSpecified()); |
| 6193 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 6194 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6195 | // Try to convert the decl specifier to a type. This works for |
| 6196 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 6197 | // for a TUK_Friend. |
Chris Lattner | 1fb66f4 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 6198 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6199 | TypeSourceInfo *TSI = GetTypeForDeclarator(TheDeclarator, S); |
| 6200 | QualType T = TSI->getType(); |
Chris Lattner | 1fb66f4 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 6201 | if (TheDeclarator.isInvalidType()) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6202 | return 0; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6203 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6204 | // This is definitely an error in C++98. It's probably meant to |
| 6205 | // be forbidden in C++0x, too, but the specification is just |
| 6206 | // poorly written. |
| 6207 | // |
| 6208 | // The problem is with declarations like the following: |
| 6209 | // template <T> friend A<T>::foo; |
| 6210 | // where deciding whether a class C is a friend or not now hinges |
| 6211 | // on whether there exists an instantiation of A that causes |
| 6212 | // 'foo' to equal C. There are restrictions on class-heads |
| 6213 | // (which we declare (by fiat) elaborated friend declarations to |
| 6214 | // be) that makes this tractable. |
| 6215 | // |
| 6216 | // FIXME: handle "template <> friend class A<T>;", which |
| 6217 | // is possibly well-formed? Who even knows? |
Douglas Gregor | e677daf | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 6218 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6219 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 6220 | << DS.getSourceRange(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6221 | return 0; |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6222 | } |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6223 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6224 | // C++98 [class.friend]p1: A friend of a class is a function |
| 6225 | // or class that is not a member of the class . . . |
John McCall | 463e10c | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 6226 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 6227 | // deadline. It's also a very silly restriction that seriously |
| 6228 | // affects inner classes and which nobody else seems to implement; |
| 6229 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6230 | // |
| 6231 | // But note that we could warn about it: it's always useless to |
| 6232 | // friend one of your own members (it's not, however, worthless to |
| 6233 | // friend a member of an arbitrary specialization of your template). |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6234 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6235 | Decl *D; |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6236 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6237 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6238 | NumTempParamLists, |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6239 | (TemplateParameterList**) TempParams.release(), |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 6240 | TSI, |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6241 | DS.getFriendSpecLoc()); |
| 6242 | else |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6243 | D = CheckFriendTypeDecl(DS.getFriendSpecLoc(), TSI); |
| 6244 | |
| 6245 | if (!D) |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6246 | return 0; |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 6247 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 6248 | D->setAccess(AS_public); |
| 6249 | CurContext->addDecl(D); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6250 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6251 | return D; |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6252 | } |
| 6253 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6254 | Decl *Sema::ActOnFriendFunctionDecl(Scope *S, |
| 6255 | Declarator &D, |
| 6256 | bool IsDefinition, |
John McCall | 2f212b3 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 6257 | MultiTemplateParamsArg TemplateParams) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6258 | const DeclSpec &DS = D.getDeclSpec(); |
| 6259 | |
| 6260 | assert(DS.isFriendSpecified()); |
| 6261 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 6262 | |
| 6263 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6264 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 6265 | QualType T = TInfo->getType(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6266 | |
| 6267 | // C++ [class.friend]p1 |
| 6268 | // A friend of a class is a function or class.... |
| 6269 | // Note that this sees through typedefs, which is intended. |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6270 | // It *doesn't* see through dependent types, which is correct |
| 6271 | // according to [temp.arg.type]p3: |
| 6272 | // If a declaration acquires a function type through a |
| 6273 | // type dependent on a template-parameter and this causes |
| 6274 | // a declaration that does not use the syntactic form of a |
| 6275 | // function declarator to have a function type, the program |
| 6276 | // is ill-formed. |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6277 | if (!T->isFunctionType()) { |
| 6278 | Diag(Loc, diag::err_unexpected_friend); |
| 6279 | |
| 6280 | // It might be worthwhile to try to recover by creating an |
| 6281 | // appropriate declaration. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6282 | return 0; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6283 | } |
| 6284 | |
| 6285 | // C++ [namespace.memdef]p3 |
| 6286 | // - If a friend declaration in a non-local class first declares a |
| 6287 | // class or function, the friend class or function is a member |
| 6288 | // of the innermost enclosing namespace. |
| 6289 | // - The name of the friend is not found by simple name lookup |
| 6290 | // until a matching declaration is provided in that namespace |
| 6291 | // scope (either before or after the class declaration granting |
| 6292 | // friendship). |
| 6293 | // - If a friend function is called, its name may be found by the |
| 6294 | // name lookup that considers functions from namespaces and |
| 6295 | // classes associated with the types of the function arguments. |
| 6296 | // - When looking for a prior declaration of a class or a function |
| 6297 | // declared as a friend, scopes outside the innermost enclosing |
| 6298 | // namespace scope are not considered. |
| 6299 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6300 | CXXScopeSpec &ScopeQual = D.getCXXScopeSpec(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6301 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6302 | DeclarationName Name = NameInfo.getName(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6303 | assert(Name); |
| 6304 | |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6305 | // The context we found the declaration in, or in which we should |
| 6306 | // create the declaration. |
| 6307 | DeclContext *DC; |
| 6308 | |
| 6309 | // FIXME: handle local classes |
| 6310 | |
| 6311 | // Recover from invalid scope qualifiers as if they just weren't there. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6312 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName, |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6313 | ForRedeclaration); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6314 | if (!ScopeQual.isInvalid() && ScopeQual.isSet()) { |
| 6315 | DC = computeDeclContext(ScopeQual); |
| 6316 | |
| 6317 | // FIXME: handle dependent contexts |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6318 | if (!DC) return 0; |
| 6319 | if (RequireCompleteDeclContext(ScopeQual, DC)) return 0; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6320 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6321 | LookupQualifiedName(Previous, DC); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6322 | |
John McCall | 4583186 | 2010-05-28 01:41:47 +0000 | [diff] [blame] | 6323 | // Ignore things found implicitly in the wrong scope. |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6324 | // TODO: better diagnostics for this case. Suggesting the right |
| 6325 | // qualified scope would be nice... |
John McCall | 4583186 | 2010-05-28 01:41:47 +0000 | [diff] [blame] | 6326 | LookupResult::Filter F = Previous.makeFilter(); |
| 6327 | while (F.hasNext()) { |
| 6328 | NamedDecl *D = F.next(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6329 | if (!DC->InEnclosingNamespaceSetOf( |
| 6330 | D->getDeclContext()->getRedeclContext())) |
John McCall | 4583186 | 2010-05-28 01:41:47 +0000 | [diff] [blame] | 6331 | F.erase(); |
| 6332 | } |
| 6333 | F.done(); |
| 6334 | |
| 6335 | if (Previous.empty()) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6336 | D.setInvalidType(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6337 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6338 | return 0; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6339 | } |
| 6340 | |
| 6341 | // C++ [class.friend]p1: A friend of a class is a function or |
| 6342 | // class that is not a member of the class . . . |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6343 | if (DC->Equals(CurContext)) |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6344 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 6345 | |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6346 | // Otherwise walk out to the nearest namespace scope looking for matches. |
| 6347 | } else { |
| 6348 | // TODO: handle local class contexts. |
| 6349 | |
| 6350 | DC = CurContext; |
| 6351 | while (true) { |
| 6352 | // Skip class contexts. If someone can cite chapter and verse |
| 6353 | // for this behavior, that would be nice --- it's what GCC and |
| 6354 | // EDG do, and it seems like a reasonable intent, but the spec |
| 6355 | // really only says that checks for unqualified existing |
| 6356 | // declarations should stop at the nearest enclosing namespace, |
| 6357 | // not that they should only consider the nearest enclosing |
| 6358 | // namespace. |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6359 | while (DC->isRecord()) |
| 6360 | DC = DC->getParent(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6361 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6362 | LookupQualifiedName(Previous, DC); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6363 | |
| 6364 | // TODO: decide what we think about using declarations. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6365 | if (!Previous.empty()) |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6366 | break; |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6367 | |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6368 | if (DC->isFileContext()) break; |
| 6369 | DC = DC->getParent(); |
| 6370 | } |
| 6371 | |
| 6372 | // C++ [class.friend]p1: A friend of a class is a function or |
| 6373 | // class that is not a member of the class . . . |
John McCall | 93343b9 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 6374 | // C++0x changes this for both friend types and functions. |
| 6375 | // Most C++ 98 compilers do seem to give an error here, so |
| 6376 | // we do, too. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 6377 | if (!Previous.empty() && DC->Equals(CurContext) |
| 6378 | && !getLangOptions().CPlusPlus0x) |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6379 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 6380 | } |
| 6381 | |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6382 | if (DC->isFileContext()) { |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6383 | // This implies that it has to be an operator or function. |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6384 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 6385 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 6386 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6387 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6388 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 6389 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6390 | return 0; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6391 | } |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6392 | } |
| 6393 | |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6394 | bool Redeclaration = false; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 6395 | NamedDecl *ND = ActOnFunctionDeclarator(S, D, DC, T, TInfo, Previous, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 6396 | move(TemplateParams), |
John McCall | d1e9d83 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 6397 | IsDefinition, |
| 6398 | Redeclaration); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6399 | if (!ND) return 0; |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6400 | |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6401 | assert(ND->getDeclContext() == DC); |
| 6402 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 5ed6e8f | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 6403 | |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6404 | // Add the function declaration to the appropriate lookup tables, |
| 6405 | // adjusting the redeclarations list as necessary. We don't |
| 6406 | // want to do this yet if the friending class is dependent. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6407 | // |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6408 | // Also update the scope-based lookup if the target context's |
| 6409 | // lookup context is in lexical scope. |
| 6410 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 6411 | DC = DC->getRedeclContext(); |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6412 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6413 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6414 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 6415 | } |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6416 | |
| 6417 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 6418 | D.getIdentifierLoc(), ND, |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6419 | DS.getFriendSpecLoc()); |
John McCall | 75c03bb | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 6420 | FrD->setAccess(AS_public); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 6421 | CurContext->addDecl(FrD); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 6422 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6423 | return ND; |
Anders Carlsson | 3881170 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 6424 | } |
| 6425 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6426 | void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { |
| 6427 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6428 | |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 6429 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 6430 | if (!Fn) { |
| 6431 | Diag(DelLoc, diag::err_deleted_non_function); |
| 6432 | return; |
| 6433 | } |
| 6434 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 6435 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 6436 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 6437 | // If the declaration wasn't the first, we delete the function anyway for |
| 6438 | // recovery. |
| 6439 | } |
| 6440 | Fn->setDeleted(); |
| 6441 | } |
Sebastian Redl | 4c01866 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 6442 | |
| 6443 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 6444 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 6445 | ++CI) { |
| 6446 | Stmt *SubStmt = *CI; |
| 6447 | if (!SubStmt) |
| 6448 | continue; |
| 6449 | if (isa<ReturnStmt>(SubStmt)) |
| 6450 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 6451 | diag::err_return_in_constructor_handler); |
| 6452 | if (!isa<Expr>(SubStmt)) |
| 6453 | SearchForReturnInStmt(Self, SubStmt); |
| 6454 | } |
| 6455 | } |
| 6456 | |
| 6457 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 6458 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 6459 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 6460 | SearchForReturnInStmt(*this, Handler); |
| 6461 | } |
| 6462 | } |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6463 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6464 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6465 | const CXXMethodDecl *Old) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6466 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 6467 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6468 | |
Chandler Carruth | 284bb2e | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 6469 | if (Context.hasSameType(NewTy, OldTy) || |
| 6470 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6471 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6472 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6473 | // Check if the return types are covariant |
| 6474 | QualType NewClassTy, OldClassTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6475 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6476 | /// Both types must be pointers or references to classes. |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 6477 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 6478 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6479 | NewClassTy = NewPT->getPointeeType(); |
| 6480 | OldClassTy = OldPT->getPointeeType(); |
| 6481 | } |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 6482 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 6483 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 6484 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 6485 | NewClassTy = NewRT->getPointeeType(); |
| 6486 | OldClassTy = OldRT->getPointeeType(); |
| 6487 | } |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6488 | } |
| 6489 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6490 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6491 | // The return types aren't either both pointers or references to a class type. |
| 6492 | if (NewClassTy.isNull()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6493 | Diag(New->getLocation(), |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6494 | diag::err_different_return_type_for_overriding_virtual_function) |
| 6495 | << New->getDeclName() << NewTy << OldTy; |
| 6496 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6497 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6498 | return true; |
| 6499 | } |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6500 | |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 6501 | // C++ [class.virtual]p6: |
| 6502 | // If the return type of D::f differs from the return type of B::f, the |
| 6503 | // class type in the return type of D::f shall be complete at the point of |
| 6504 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | 0c9dd84 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 6505 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 6506 | if (!RT->isBeingDefined() && |
| 6507 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 6508 | PDiag(diag::err_covariant_return_incomplete) |
| 6509 | << New->getDeclName())) |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 6510 | return true; |
Anders Carlsson | 0c9dd84 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 6511 | } |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 6512 | |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6513 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6514 | // Check if the new class derives from the old class. |
| 6515 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 6516 | Diag(New->getLocation(), |
| 6517 | diag::err_covariant_return_not_derived) |
| 6518 | << New->getDeclName() << NewTy << OldTy; |
| 6519 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6520 | return true; |
| 6521 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6522 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6523 | // Check if we the conversion from derived to base is valid. |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 6524 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 6525 | diag::err_covariant_return_inaccessible_base, |
| 6526 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 6527 | // FIXME: Should this point to the return type? |
| 6528 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6529 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6530 | return true; |
| 6531 | } |
| 6532 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6533 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6534 | // The qualifiers of the return types must be the same. |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 6535 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6536 | Diag(New->getLocation(), |
| 6537 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6538 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6539 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6540 | return true; |
| 6541 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6542 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6543 | |
| 6544 | // The new class type must have the same or less qualifiers as the old type. |
| 6545 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 6546 | Diag(New->getLocation(), |
| 6547 | diag::err_covariant_return_type_class_type_more_qualified) |
| 6548 | << New->getDeclName() << NewTy << OldTy; |
| 6549 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6550 | return true; |
| 6551 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6552 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 6553 | return false; |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 6554 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6555 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 6556 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
| 6557 | const CXXMethodDecl *Old) |
| 6558 | { |
| 6559 | if (Old->hasAttr<FinalAttr>()) { |
| 6560 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 6561 | << New->getDeclName(); |
| 6562 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 6563 | return true; |
| 6564 | } |
| 6565 | |
| 6566 | return false; |
| 6567 | } |
| 6568 | |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 6569 | /// \brief Mark the given method pure. |
| 6570 | /// |
| 6571 | /// \param Method the method to be marked pure. |
| 6572 | /// |
| 6573 | /// \param InitRange the source range that covers the "0" initializer. |
| 6574 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
| 6575 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 6576 | Method->setPure(); |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 6577 | return false; |
| 6578 | } |
| 6579 | |
| 6580 | if (!Method->isInvalidDecl()) |
| 6581 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 6582 | << Method->getDeclName() << InitRange; |
| 6583 | return true; |
| 6584 | } |
| 6585 | |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6586 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 6587 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 6588 | /// is a fresh scope pushed for just this purpose. |
| 6589 | /// |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6590 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 6591 | /// static data member of class X, names should be looked up in the scope of |
| 6592 | /// class X. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6593 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6594 | // If there is no declaration, there was an error parsing it. |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6595 | if (D == 0) return; |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6596 | |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6597 | // We should only get called for declarations with scope specifiers, like: |
| 6598 | // int foo::bar; |
| 6599 | assert(D->isOutOfLine()); |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6600 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6601 | } |
| 6602 | |
| 6603 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6604 | /// initializer for the out-of-line declaration 'D'. |
| 6605 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, Decl *D) { |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6606 | // If there is no declaration, there was an error parsing it. |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6607 | if (D == 0) return; |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6608 | |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 6609 | assert(D->isOutOfLine()); |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 6610 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 6611 | } |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6612 | |
| 6613 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 6614 | /// C++ if/switch/while/for statement. |
| 6615 | /// e.g: "if (int x = f()) {...}" |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6616 | DeclResult Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6617 | // C++ 6.4p2: |
| 6618 | // The declarator shall not specify a function or an array. |
| 6619 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 6620 | // new class or enumeration. |
| 6621 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 6622 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 6623 | |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6624 | TagDecl *OwnedTag = 0; |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6625 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag); |
| 6626 | QualType Ty = TInfo->getType(); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6627 | |
| 6628 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 6629 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 6630 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 6631 | Diag(D.getIdentifierLoc(), diag::err_invalid_use_of_function_type) |
| 6632 | << D.getSourceRange(); |
| 6633 | return DeclResult(); |
| 6634 | } else if (OwnedTag && OwnedTag->isDefinition()) { |
| 6635 | // The type-specifier-seq shall not declare a new class or enumeration. |
| 6636 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition); |
| 6637 | } |
| 6638 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6639 | Decl *Dcl = ActOnDeclarator(S, D); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6640 | if (!Dcl) |
| 6641 | return DeclResult(); |
| 6642 | |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 6643 | return Dcl; |
| 6644 | } |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6645 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6646 | void Sema::MarkVTableUsed(SourceLocation Loc, CXXRecordDecl *Class, |
| 6647 | bool DefinitionRequired) { |
| 6648 | // Ignore any vtable uses in unevaluated operands or for classes that do |
| 6649 | // not have a vtable. |
| 6650 | if (!Class->isDynamicClass() || Class->isDependentContext() || |
| 6651 | CurContext->isDependentContext() || |
| 6652 | ExprEvalContexts.back().Context == Unevaluated) |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 6653 | return; |
| 6654 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6655 | // Try to insert this class into the map. |
| 6656 | Class = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 6657 | std::pair<llvm::DenseMap<CXXRecordDecl *, bool>::iterator, bool> |
| 6658 | Pos = VTablesUsed.insert(std::make_pair(Class, DefinitionRequired)); |
| 6659 | if (!Pos.second) { |
Daniel Dunbar | 5321776 | 2010-05-25 00:33:13 +0000 | [diff] [blame] | 6660 | // If we already had an entry, check to see if we are promoting this vtable |
| 6661 | // to required a definition. If so, we need to reappend to the VTableUses |
| 6662 | // list, since we may have already processed the first entry. |
| 6663 | if (DefinitionRequired && !Pos.first->second) { |
| 6664 | Pos.first->second = true; |
| 6665 | } else { |
| 6666 | // Otherwise, we can early exit. |
| 6667 | return; |
| 6668 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6669 | } |
| 6670 | |
| 6671 | // Local classes need to have their virtual members marked |
| 6672 | // immediately. For all other classes, we mark their virtual members |
| 6673 | // at the end of the translation unit. |
| 6674 | if (Class->isLocalClass()) |
| 6675 | MarkVirtualMembersReferenced(Loc, Class); |
Daniel Dunbar | 0547ad3 | 2010-05-11 21:32:35 +0000 | [diff] [blame] | 6676 | else |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6677 | VTableUses.push_back(std::make_pair(Class, Loc)); |
Douglas Gregor | 0c4aad1 | 2010-05-11 20:24:17 +0000 | [diff] [blame] | 6678 | } |
| 6679 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6680 | bool Sema::DefineUsedVTables() { |
| 6681 | // If any dynamic classes have their key function defined within |
| 6682 | // this translation unit, then those vtables are considered "used" and must |
| 6683 | // be emitted. |
| 6684 | for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) { |
| 6685 | if (const CXXMethodDecl *KeyFunction |
| 6686 | = Context.getKeyFunction(DynamicClasses[I])) { |
| 6687 | const FunctionDecl *Definition = 0; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 6688 | if (KeyFunction->hasBody(Definition)) |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6689 | MarkVTableUsed(Definition->getLocation(), DynamicClasses[I], true); |
| 6690 | } |
| 6691 | } |
| 6692 | |
| 6693 | if (VTableUses.empty()) |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6694 | return false; |
| 6695 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6696 | // Note: The VTableUses vector could grow as a result of marking |
| 6697 | // the members of a class as "used", so we check the size each |
| 6698 | // time through the loop and prefer indices (with are stable) to |
| 6699 | // iterators (which are not). |
| 6700 | for (unsigned I = 0; I != VTableUses.size(); ++I) { |
Daniel Dunbar | 105ce6d | 2010-05-25 00:32:58 +0000 | [diff] [blame] | 6701 | CXXRecordDecl *Class = VTableUses[I].first->getDefinition(); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6702 | if (!Class) |
| 6703 | continue; |
| 6704 | |
| 6705 | SourceLocation Loc = VTableUses[I].second; |
| 6706 | |
| 6707 | // If this class has a key function, but that key function is |
| 6708 | // defined in another translation unit, we don't need to emit the |
| 6709 | // vtable even though we're using it. |
| 6710 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(Class); |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 6711 | if (KeyFunction && !KeyFunction->hasBody()) { |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6712 | switch (KeyFunction->getTemplateSpecializationKind()) { |
| 6713 | case TSK_Undeclared: |
| 6714 | case TSK_ExplicitSpecialization: |
| 6715 | case TSK_ExplicitInstantiationDeclaration: |
| 6716 | // The key function is in another translation unit. |
| 6717 | continue; |
| 6718 | |
| 6719 | case TSK_ExplicitInstantiationDefinition: |
| 6720 | case TSK_ImplicitInstantiation: |
| 6721 | // We will be instantiating the key function. |
| 6722 | break; |
| 6723 | } |
| 6724 | } else if (!KeyFunction) { |
| 6725 | // If we have a class with no key function that is the subject |
| 6726 | // of an explicit instantiation declaration, suppress the |
| 6727 | // vtable; it will live with the explicit instantiation |
| 6728 | // definition. |
| 6729 | bool IsExplicitInstantiationDeclaration |
| 6730 | = Class->getTemplateSpecializationKind() |
| 6731 | == TSK_ExplicitInstantiationDeclaration; |
| 6732 | for (TagDecl::redecl_iterator R = Class->redecls_begin(), |
| 6733 | REnd = Class->redecls_end(); |
| 6734 | R != REnd; ++R) { |
| 6735 | TemplateSpecializationKind TSK |
| 6736 | = cast<CXXRecordDecl>(*R)->getTemplateSpecializationKind(); |
| 6737 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 6738 | IsExplicitInstantiationDeclaration = true; |
| 6739 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 6740 | IsExplicitInstantiationDeclaration = false; |
| 6741 | break; |
| 6742 | } |
| 6743 | } |
| 6744 | |
| 6745 | if (IsExplicitInstantiationDeclaration) |
| 6746 | continue; |
| 6747 | } |
| 6748 | |
| 6749 | // Mark all of the virtual members of this class as referenced, so |
| 6750 | // that we can build a vtable. Then, tell the AST consumer that a |
| 6751 | // vtable for this class is required. |
| 6752 | MarkVirtualMembersReferenced(Loc, Class); |
| 6753 | CXXRecordDecl *Canonical = cast<CXXRecordDecl>(Class->getCanonicalDecl()); |
| 6754 | Consumer.HandleVTable(Class, VTablesUsed[Canonical]); |
| 6755 | |
| 6756 | // Optionally warn if we're emitting a weak vtable. |
| 6757 | if (Class->getLinkage() == ExternalLinkage && |
| 6758 | Class->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 6759 | if (!KeyFunction || (KeyFunction->hasBody() && KeyFunction->isInlined())) |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6760 | Diag(Class->getLocation(), diag::warn_weak_vtable) << Class; |
| 6761 | } |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6762 | } |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6763 | VTableUses.clear(); |
| 6764 | |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6765 | return true; |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 6766 | } |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6767 | |
Rafael Espindola | 5b33408 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 6768 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 6769 | const CXXRecordDecl *RD) { |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6770 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 6771 | e = RD->method_end(); i != e; ++i) { |
| 6772 | CXXMethodDecl *MD = *i; |
| 6773 | |
| 6774 | // C++ [basic.def.odr]p2: |
| 6775 | // [...] A virtual member function is used if it is not pure. [...] |
| 6776 | if (MD->isVirtual() && !MD->isPure()) |
| 6777 | MarkDeclarationReferenced(Loc, MD); |
| 6778 | } |
Rafael Espindola | 5b33408 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 6779 | |
| 6780 | // Only classes that have virtual bases need a VTT. |
| 6781 | if (RD->getNumVBases() == 0) |
| 6782 | return; |
| 6783 | |
| 6784 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 6785 | e = RD->bases_end(); i != e; ++i) { |
| 6786 | const CXXRecordDecl *Base = |
| 6787 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Rafael Espindola | 5b33408 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 6788 | if (Base->getNumVBases() == 0) |
| 6789 | continue; |
| 6790 | MarkVirtualMembersReferenced(Loc, Base); |
| 6791 | } |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 6792 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 6793 | |
| 6794 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 6795 | /// Objective-C implementation whose ivars need be initialized. |
| 6796 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 6797 | if (!getLangOptions().CPlusPlus) |
| 6798 | return; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 6799 | if (ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 6800 | llvm::SmallVector<ObjCIvarDecl*, 8> ivars; |
| 6801 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 6802 | if (ivars.empty()) |
| 6803 | return; |
| 6804 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
| 6805 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 6806 | FieldDecl *Field = ivars[i]; |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 6807 | if (Field->isInvalidDecl()) |
| 6808 | continue; |
| 6809 | |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 6810 | CXXBaseOrMemberInitializer *Member; |
| 6811 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 6812 | InitializationKind InitKind = |
| 6813 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 6814 | |
| 6815 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6816 | ExprResult MemberInit = |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6817 | InitSeq.Perform(*this, InitEntity, InitKind, MultiExprArg()); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6818 | MemberInit = MaybeCreateCXXExprWithTemporaries(MemberInit.get()); |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 6819 | // Note, MemberInit could actually come back empty if no initialization |
| 6820 | // is required (e.g., because it would call a trivial default constructor) |
| 6821 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 6822 | continue; |
| 6823 | |
| 6824 | Member = |
| 6825 | new (Context) CXXBaseOrMemberInitializer(Context, |
| 6826 | Field, SourceLocation(), |
| 6827 | SourceLocation(), |
| 6828 | MemberInit.takeAs<Expr>(), |
| 6829 | SourceLocation()); |
| 6830 | AllToInit.push_back(Member); |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 6831 | |
| 6832 | // Be sure that the destructor is accessible and is marked as referenced. |
| 6833 | if (const RecordType *RecordTy |
| 6834 | = Context.getBaseElementType(Field->getType()) |
| 6835 | ->getAs<RecordType>()) { |
| 6836 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 6837 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Douglas Gregor | 527786e | 2010-05-20 02:24:22 +0000 | [diff] [blame] | 6838 | MarkDeclarationReferenced(Field->getLocation(), Destructor); |
| 6839 | CheckDestructorAccess(Field->getLocation(), Destructor, |
| 6840 | PDiag(diag::err_access_dtor_ivar) |
| 6841 | << Context.getBaseElementType(Field->getType())); |
| 6842 | } |
| 6843 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 6844 | } |
| 6845 | ObjCImplementation->setIvarInitializers(Context, |
| 6846 | AllToInit.data(), AllToInit.size()); |
| 6847 | } |
| 6848 | } |