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 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 15 | #include "SemaInit.h" |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 16 | #include "Lookup.h" |
Argyrios Kyrtzidis | 2f67f37 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecordLayout.h" |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeOrdering.h" |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 25 | #include "clang/Parse/DeclSpec.h" |
| 26 | #include "clang/Parse/Template.h" |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 27 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 153d967 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 30 | #include <map> |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 31 | #include <set> |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace clang; |
| 34 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
| 36 | // CheckDefaultArgumentVisitor |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 39 | namespace { |
| 40 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 41 | /// the default argument of a parameter to determine whether it |
| 42 | /// contains any ill-formed subexpressions. For example, this will |
| 43 | /// diagnose the use of local variables or parameters within the |
| 44 | /// default argument expression. |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 45 | class CheckDefaultArgumentVisitor |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 46 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 47 | Expr *DefaultArg; |
| 48 | Sema *S; |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 49 | |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 50 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 52 | : DefaultArg(defarg), S(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 | bool VisitExpr(Expr *Node); |
| 55 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 97a9c81 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 56 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 57 | }; |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 58 | |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 59 | /// VisitExpr - Visit all of the children of this expression. |
| 60 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 61 | bool IsInvalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | for (Stmt::child_iterator I = Node->child_begin(), |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 63 | E = Node->child_end(); I != E; ++I) |
| 64 | IsInvalid |= Visit(*I); |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 65 | return IsInvalid; |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 68 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 69 | /// determine whether this declaration can be used in the default |
| 70 | /// argument expression. |
| 71 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 72 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 73 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 74 | // C++ [dcl.fct.default]p9 |
| 75 | // Default arguments are evaluated each time the function is |
| 76 | // called. The order of evaluation of function arguments is |
| 77 | // unspecified. Consequently, parameters of a function shall not |
| 78 | // be used in default argument expressions, even if they are not |
| 79 | // evaluated. Parameters of a function declared before a default |
| 80 | // argument expression are in scope and can hide namespace and |
| 81 | // class member names. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 83 | diag::err_param_default_argument_references_param) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 84 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 85 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 86 | // C++ [dcl.fct.default]p7 |
| 87 | // Local variables shall not be used in default argument |
| 88 | // expressions. |
Steve Naroff | 08899ff | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 89 | if (VDecl->isBlockVarDecl()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 91 | diag::err_param_default_argument_references_local) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 92 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 93 | } |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 94 | |
Douglas Gregor | 8e12c38 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 95 | return false; |
| 96 | } |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 97 | |
Douglas Gregor | 97a9c81 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 98 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 99 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 100 | // C++ [dcl.fct.default]p8: |
| 101 | // The keyword this shall not be used in a default argument of a |
| 102 | // member function. |
| 103 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 104 | diag::err_param_default_argument_references_this) |
| 105 | << ThisE->getSourceRange(); |
Chris Lattner | b0d3844 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 106 | } |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 109 | bool |
| 110 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, ExprArg DefaultArg, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | SourceLocation EqualLoc) { |
Anders Carlsson | 114056f | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 112 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 113 | diag::err_typecheck_decl_incomplete_type)) { |
| 114 | Param->setInvalidDecl(); |
| 115 | return true; |
| 116 | } |
| 117 | |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 118 | Expr *Arg = (Expr *)DefaultArg.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 120 | // C++ [dcl.fct.default]p5 |
| 121 | // A default argument expression is implicitly converted (clause |
| 122 | // 4) to the parameter type. The default argument expression has |
| 123 | // the same semantic constraints as the initializer expression in |
| 124 | // a declaration of a variable of the parameter type, using the |
| 125 | // copy-initialization semantics (8.5). |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 126 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Param); |
| 127 | InitializationKind Kind = InitializationKind::CreateCopy(Param->getLocation(), |
| 128 | EqualLoc); |
Eli Friedman | 5f101b9 | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 129 | InitializationSequence InitSeq(*this, Entity, Kind, &Arg, 1); |
| 130 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
| 131 | MultiExprArg(*this, (void**)&Arg, 1)); |
| 132 | if (Result.isInvalid()) |
Anders Carlsson | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 133 | return true; |
Eli Friedman | 5f101b9 | 2009-12-22 02:46:13 +0000 | [diff] [blame] | 134 | Arg = Result.takeAs<Expr>(); |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 135 | |
Anders Carlsson | 6e997b2 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 136 | Arg = MaybeCreateCXXExprWithTemporaries(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 138 | // Okay: add the default argument to the parameter |
| 139 | Param->setDefaultArg(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 | DefaultArg.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Anders Carlsson | 4562f1f | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 143 | return false; |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 146 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 147 | /// provided for a function parameter is well-formed. If so, attach it |
| 148 | /// to the parameter declaration. |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 149 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | Sema::ActOnParamDefaultArgument(DeclPtrTy param, SourceLocation EqualLoc, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 151 | ExprArg defarg) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 152 | if (!param || !defarg.get()) |
| 153 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 155 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 156 | UnparsedDefaultArgLocs.erase(Param); |
| 157 | |
Anders Carlsson | 3cbc859 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 158 | ExprOwningPtr<Expr> DefaultArg(this, defarg.takeAs<Expr>()); |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 159 | |
| 160 | // Default arguments are only permitted in C++ |
| 161 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 162 | Diag(EqualLoc, diag::err_param_default_argument) |
| 163 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 164 | Param->setInvalidDecl(); |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
Anders Carlsson | f1c2695 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 168 | // Check that the default argument is well-formed |
| 169 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); |
| 170 | if (DefaultArgChecker.Visit(DefaultArg.get())) { |
| 171 | Param->setInvalidDecl(); |
| 172 | return; |
| 173 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 175 | SetParamDefaultArgument(Param, move(DefaultArg), EqualLoc); |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 178 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 179 | /// argument for a function parameter, but we can't parse it yet |
| 180 | /// because we're inside a class definition. Note that this default |
| 181 | /// argument will be parsed later. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | void Sema::ActOnParamUnparsedDefaultArgument(DeclPtrTy param, |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 183 | SourceLocation EqualLoc, |
| 184 | SourceLocation ArgLoc) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 185 | if (!param) |
| 186 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 188 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 189 | if (Param) |
| 190 | Param->setUnparsedDefaultArg(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 192 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 195 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 196 | /// the default argument for the parameter param failed. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 197 | void Sema::ActOnParamDefaultArgumentError(DeclPtrTy param) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 198 | if (!param) |
| 199 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 201 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 203 | Param->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 205 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 208 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 209 | /// arguments in the declarator, which is not a function declaration |
| 210 | /// or definition and therefore is not permitted to have default |
| 211 | /// arguments. This routine should be invoked for every declarator |
| 212 | /// that is not a function declaration or definition. |
| 213 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 214 | // C++ [dcl.fct.default]p3 |
| 215 | // A default argument expression shall be specified only in the |
| 216 | // parameter-declaration-clause of a function declaration or in a |
| 217 | // template-parameter (14.1). It shall not be specified for a |
| 218 | // parameter pack. If it is specified in a |
| 219 | // parameter-declaration-clause, it shall not occur within a |
| 220 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 221 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 222 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 223 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 224 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 225 | ParmVarDecl *Param = |
| 226 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param.getAs<Decl>()); |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 227 | if (Param->hasUnparsedDefaultArg()) { |
| 228 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 229 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 230 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 231 | delete Toks; |
| 232 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 233 | } else if (Param->getDefaultArg()) { |
| 234 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 235 | << Param->getDefaultArg()->getSourceRange(); |
| 236 | Param->setDefaultArg(0); |
Douglas Gregor | caa8ace | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 243 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 244 | // function, once we already know that they have the same |
Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 245 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 246 | // error, false otherwise. |
| 247 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 248 | bool Invalid = false; |
| 249 | |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 250 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 251 | // For non-template functions, default arguments can be added in |
| 252 | // later declarations of a function in the same |
| 253 | // scope. Declarations in different scopes have completely |
| 254 | // distinct sets of default arguments. That is, declarations in |
| 255 | // inner scopes do not acquire default arguments from |
| 256 | // declarations in outer scopes, and vice versa. In a given |
| 257 | // function declaration, all parameters subsequent to a |
| 258 | // parameter with a default argument shall have default |
| 259 | // arguments supplied in this or previous declarations. A |
| 260 | // default argument shall not be redefined by a later |
| 261 | // declaration (not even to the same value). |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 262 | // |
| 263 | // C++ [dcl.fct.default]p6: |
| 264 | // Except for member functions of class templates, the default arguments |
| 265 | // in a member function definition that appears outside of the class |
| 266 | // definition are added to the set of default arguments provided by the |
| 267 | // member function declaration in the class definition. |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 268 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 269 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 270 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 271 | |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 272 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Douglas Gregor | 08dc584 | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 273 | // FIXME: If we knew where the '=' was, we could easily provide a fix-it |
| 274 | // hint here. Alternatively, we could walk the type-source information |
| 275 | // for NewParam to find the last source location in the type... but it |
| 276 | // isn't worth the effort right now. This is the kind of test case that |
| 277 | // is hard to get right: |
| 278 | |
| 279 | // int f(int); |
| 280 | // void g(int (*fp)(int) = f); |
| 281 | // void g(int (*fp)(int) = &f); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | Diag(NewParam->getLocation(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 283 | diag::err_param_default_argument_redefinition) |
Douglas Gregor | 08dc584 | 2010-01-13 00:12:48 +0000 | [diff] [blame] | 284 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 285 | |
| 286 | // Look for the function declaration where the default argument was |
| 287 | // actually written, which may be a declaration prior to Old. |
| 288 | for (FunctionDecl *Older = Old->getPreviousDeclaration(); |
| 289 | Older; Older = Older->getPreviousDeclaration()) { |
| 290 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 291 | break; |
| 292 | |
| 293 | OldParam = Older->getParamDecl(p); |
| 294 | } |
| 295 | |
| 296 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 297 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 298 | Invalid = true; |
Douglas Gregor | 4f15f4d | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 299 | } else if (OldParam->hasDefaultArg()) { |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 300 | // Merge the old default argument into the new parameter |
John McCall | f3cd665 | 2010-03-12 18:31:32 +0000 | [diff] [blame] | 301 | NewParam->setHasInheritedDefaultArg(); |
Douglas Gregor | 4f15f4d | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 302 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 303 | NewParam->setUninstantiatedDefaultArg( |
| 304 | OldParam->getUninstantiatedDefaultArg()); |
| 305 | else |
| 306 | NewParam->setDefaultArg(OldParam->getDefaultArg()); |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 307 | } else if (NewParam->hasDefaultArg()) { |
| 308 | if (New->getDescribedFunctionTemplate()) { |
| 309 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 310 | Diag(NewParam->getLocation(), |
| 311 | diag::err_param_default_argument_template_redecl) |
| 312 | << NewParam->getDefaultArgRange(); |
| 313 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 314 | << false; |
Douglas Gregor | 62e10f0 | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 315 | } else if (New->getTemplateSpecializationKind() |
| 316 | != TSK_ImplicitInstantiation && |
| 317 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 318 | // C++ [temp.expr.spec]p21: |
| 319 | // Default function arguments shall not be specified in a declaration |
| 320 | // or a definition for one of the following explicit specializations: |
| 321 | // - the explicit specialization of a function template; |
Douglas Gregor | 3362bde | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 322 | // - the explicit specialization of a member function template; |
| 323 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 62e10f0 | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 324 | // template where the class template specialization to which the |
| 325 | // member function specialization belongs is implicitly |
| 326 | // instantiated. |
| 327 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 328 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 329 | << New->getDeclName() |
| 330 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | c732aba | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 331 | } else if (New->getDeclContext()->isDependentContext()) { |
| 332 | // C++ [dcl.fct.default]p6 (DR217): |
| 333 | // Default arguments for a member function of a class template shall |
| 334 | // be specified on the initial declaration of the member function |
| 335 | // within the class template. |
| 336 | // |
| 337 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 338 | // leads me to the conclusion that one cannot add default function |
| 339 | // arguments for an out-of-line definition of a member function of a |
| 340 | // dependent type. |
| 341 | int WhichKind = 2; |
| 342 | if (CXXRecordDecl *Record |
| 343 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 344 | if (Record->getDescribedClassTemplate()) |
| 345 | WhichKind = 0; |
| 346 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 347 | WhichKind = 1; |
| 348 | else |
| 349 | WhichKind = 2; |
| 350 | } |
| 351 | |
| 352 | Diag(NewParam->getLocation(), |
| 353 | diag::err_param_default_argument_member_template_redecl) |
| 354 | << WhichKind |
| 355 | << NewParam->getDefaultArgRange(); |
| 356 | } |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 360 | if (CheckEquivalentExceptionSpec(Old, New)) |
Sebastian Redl | 4f4d7b5 | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 361 | Invalid = true; |
Sebastian Redl | 4f4d7b5 | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 362 | |
Douglas Gregor | 75a45ba | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 363 | return Invalid; |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 367 | /// function declaration are well-formed according to C++ |
| 368 | /// [dcl.fct.default]. |
| 369 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 370 | unsigned NumParams = FD->getNumParams(); |
| 371 | unsigned p; |
| 372 | |
| 373 | // Find first parameter with a default argument |
| 374 | for (p = 0; p < NumParams; ++p) { |
| 375 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5a53238 | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 376 | if (Param->hasDefaultArg()) |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | |
| 380 | // C++ [dcl.fct.default]p4: |
| 381 | // In a given function declaration, all parameters |
| 382 | // subsequent to a parameter with a default argument shall |
| 383 | // have default arguments supplied in this or previous |
| 384 | // declarations. A default argument shall not be redefined |
| 385 | // by a later declaration (not even to the same value). |
| 386 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | for (; p < NumParams; ++p) { |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 388 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5a53238 | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 389 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 390 | if (Param->isInvalidDecl()) |
| 391 | /* We already complained about this parameter. */; |
| 392 | else if (Param->getIdentifier()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 393 | Diag(Param->getLocation(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 394 | diag::err_param_default_argument_missing_name) |
Chris Lattner | b91fd17 | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 395 | << Param->getIdentifier(); |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 396 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | Diag(Param->getLocation(), |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 398 | diag::err_param_default_argument_missing); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 400 | LastMissingDefaultArg = p; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | if (LastMissingDefaultArg > 0) { |
| 405 | // Some default arguments were missing. Clear out all of the |
| 406 | // default arguments up to (and including) the last missing |
| 407 | // default argument, so that we leave the function parameters |
| 408 | // in a semantically valid state. |
| 409 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 410 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 411 | if (Param->hasDefaultArg()) { |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 412 | if (!Param->hasUnparsedDefaultArg()) |
| 413 | Param->getDefaultArg()->Destroy(Context); |
Chris Lattner | 199abbc | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 414 | Param->setDefaultArg(0); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 419 | |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 420 | /// isCurrentClassName - Determine whether the identifier II is the |
| 421 | /// name of the class type currently being defined. In the case of |
| 422 | /// nested classes, this will only return true if II is the name of |
| 423 | /// the innermost class. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 424 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 425 | const CXXScopeSpec *SS) { |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 426 | assert(getLangOptions().CPlusPlus && "No class names in C!"); |
| 427 | |
Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 428 | CXXRecordDecl *CurDecl; |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 429 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | e5bbb7d | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 430 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 431 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 432 | } else |
| 433 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 434 | |
Douglas Gregor | 1aa3edb | 2010-02-05 06:12:42 +0000 | [diff] [blame] | 435 | if (CurDecl && CurDecl->getIdentifier()) |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 436 | return &II == CurDecl->getIdentifier(); |
| 437 | else |
| 438 | return false; |
| 439 | } |
| 440 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 442 | /// |
| 443 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 444 | /// and returns NULL otherwise. |
| 445 | CXXBaseSpecifier * |
| 446 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 447 | SourceRange SpecifierRange, |
| 448 | bool Virtual, AccessSpecifier Access, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 449 | QualType BaseType, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 450 | SourceLocation BaseLoc) { |
| 451 | // C++ [class.union]p1: |
| 452 | // A union shall not have base classes. |
| 453 | if (Class->isUnion()) { |
| 454 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 455 | << SpecifierRange; |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | if (BaseType->isDependentType()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 461 | Class->getTagKind() == RecordDecl::TK_class, |
| 462 | Access, BaseType); |
| 463 | |
| 464 | // Base specifiers must be record types. |
| 465 | if (!BaseType->isRecordType()) { |
| 466 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | // C++ [class.union]p1: |
| 471 | // A union shall not be used as a base class. |
| 472 | if (BaseType->isUnionType()) { |
| 473 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | // C++ [class.derived]p2: |
| 478 | // The class-name in a base-specifier shall not be an incompletely |
| 479 | // defined class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 481 | PDiag(diag::err_incomplete_base_class) |
| 482 | << SpecifierRange)) |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 483 | return 0; |
| 484 | |
Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 485 | // If the base class is polymorphic or isn't empty, the new one is/isn't, too. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 486 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 487 | assert(BaseDecl && "Record type has no declaration"); |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 488 | BaseDecl = BaseDecl->getDefinition(); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 489 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 490 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 491 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
Eli Friedman | 89c038e | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 492 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 493 | // C++0x CWG Issue #817 indicates that [[final]] classes shouldn't be bases. |
| 494 | if (CXXBaseDecl->hasAttr<FinalAttr>()) { |
| 495 | Diag(BaseLoc, diag::err_final_base) << BaseType.getAsString(); |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 496 | Diag(CXXBaseDecl->getLocation(), diag::note_previous_decl) |
| 497 | << BaseType; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 498 | return 0; |
| 499 | } |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 500 | |
Eli Friedman | 89c038e | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 501 | SetClassDeclAttributesFromBase(Class, CXXBaseDecl, Virtual); |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 502 | |
| 503 | // Create the base specifier. |
| 504 | // FIXME: Allocate via ASTContext? |
| 505 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
| 506 | Class->getTagKind() == RecordDecl::TK_class, |
| 507 | Access, BaseType); |
| 508 | } |
| 509 | |
| 510 | void Sema::SetClassDeclAttributesFromBase(CXXRecordDecl *Class, |
| 511 | const CXXRecordDecl *BaseClass, |
| 512 | bool BaseIsVirtual) { |
Eli Friedman | 89c038e | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 513 | // A class with a non-empty base class is not empty. |
| 514 | // FIXME: Standard ref? |
| 515 | if (!BaseClass->isEmpty()) |
| 516 | Class->setEmpty(false); |
| 517 | |
| 518 | // C++ [class.virtual]p1: |
| 519 | // A class that [...] inherits a virtual function is called a polymorphic |
| 520 | // class. |
| 521 | if (BaseClass->isPolymorphic()) |
| 522 | Class->setPolymorphic(true); |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 523 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 524 | // C++ [dcl.init.aggr]p1: |
| 525 | // An aggregate is [...] a class with [...] no base classes [...]. |
| 526 | Class->setAggregate(false); |
Eli Friedman | 89c038e | 2009-12-05 23:03:49 +0000 | [diff] [blame] | 527 | |
| 528 | // C++ [class]p4: |
| 529 | // A POD-struct is an aggregate class... |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 530 | Class->setPOD(false); |
| 531 | |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 532 | if (BaseIsVirtual) { |
Anders Carlsson | fe63dc5 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 533 | // C++ [class.ctor]p5: |
| 534 | // A constructor is trivial if its class has no virtual base classes. |
| 535 | Class->setHasTrivialConstructor(false); |
Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 536 | |
| 537 | // C++ [class.copy]p6: |
| 538 | // A copy constructor is trivial if its class has no virtual base classes. |
| 539 | Class->setHasTrivialCopyConstructor(false); |
| 540 | |
| 541 | // C++ [class.copy]p11: |
| 542 | // A copy assignment operator is trivial if its class has no virtual |
| 543 | // base classes. |
| 544 | Class->setHasTrivialCopyAssignment(false); |
Eli Friedman | c96d496 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 545 | |
| 546 | // C++0x [meta.unary.prop] is_empty: |
| 547 | // T is a class type, but not a union type, with ... no virtual base |
| 548 | // classes |
| 549 | Class->setEmpty(false); |
Anders Carlsson | fe63dc5 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 550 | } else { |
| 551 | // C++ [class.ctor]p5: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | // A constructor is trivial if all the direct base classes of its |
Anders Carlsson | fe63dc5 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 553 | // class have trivial constructors. |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 554 | if (!BaseClass->hasTrivialConstructor()) |
Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 555 | Class->setHasTrivialConstructor(false); |
| 556 | |
| 557 | // C++ [class.copy]p6: |
| 558 | // A copy constructor is trivial if all the direct base classes of its |
| 559 | // class have trivial copy constructors. |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 560 | if (!BaseClass->hasTrivialCopyConstructor()) |
Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 561 | Class->setHasTrivialCopyConstructor(false); |
| 562 | |
| 563 | // C++ [class.copy]p11: |
| 564 | // A copy assignment operator is trivial if all the direct base classes |
| 565 | // of its class have trivial copy assignment operators. |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 566 | if (!BaseClass->hasTrivialCopyAssignment()) |
Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 567 | Class->setHasTrivialCopyAssignment(false); |
Anders Carlsson | fe63dc5 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 568 | } |
Anders Carlsson | 6dc3575 | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 569 | |
| 570 | // C++ [class.ctor]p3: |
| 571 | // A destructor is trivial if all the direct base classes of its class |
| 572 | // have trivial destructors. |
Anders Carlsson | ae3c5cf | 2009-12-03 17:49:57 +0000 | [diff] [blame] | 573 | if (!BaseClass->hasTrivialDestructor()) |
Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 574 | Class->setHasTrivialDestructor(false); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 577 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 578 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | /// example: |
| 580 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 581 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | Sema::BaseResult |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 583 | Sema::ActOnBaseSpecifier(DeclPtrTy classdecl, SourceRange SpecifierRange, |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 584 | bool Virtual, AccessSpecifier Access, |
| 585 | TypeTy *basetype, SourceLocation BaseLoc) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 586 | if (!classdecl) |
| 587 | return true; |
| 588 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 589 | AdjustDeclIfTemplate(classdecl); |
Douglas Gregor | beab56e | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 590 | CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(classdecl.getAs<Decl>()); |
| 591 | if (!Class) |
| 592 | return true; |
| 593 | |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 594 | QualType BaseType = GetTypeFromParser(basetype); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 595 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
| 596 | Virtual, Access, |
| 597 | BaseType, BaseLoc)) |
| 598 | return BaseSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 600 | return true; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 601 | } |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 602 | |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 603 | /// \brief Performs the actual work of attaching the given base class |
| 604 | /// specifiers to a C++ class. |
| 605 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 606 | unsigned NumBases) { |
| 607 | if (NumBases == 0) |
| 608 | return false; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 609 | |
| 610 | // Used to keep track of which base types we have already seen, so |
| 611 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 612 | // that the key is always the unqualified canonical type of the base |
| 613 | // class. |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 614 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 615 | |
| 616 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 617 | unsigned NumGoodBases = 0; |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 618 | bool Invalid = false; |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 619 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | QualType NewBaseType |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 621 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 622 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 623 | |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 624 | if (KnownBaseTypes[NewBaseType]) { |
| 625 | // C++ [class.mi]p3: |
| 626 | // A class shall not be specified as a direct base class of a |
| 627 | // derived class more than once. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 628 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 629 | diag::err_duplicate_base_class) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 630 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 631 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 632 | |
| 633 | // Delete the duplicate base class specifier; we're going to |
| 634 | // overwrite its pointer later. |
Douglas Gregor | b77af8f | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 635 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 636 | |
| 637 | Invalid = true; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 638 | } else { |
| 639 | // Okay, add this new base class. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 640 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 641 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | 29a9247 | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
| 645 | // Attach the remaining base class specifiers to the derived class. |
Douglas Gregor | 4a62bdf | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 646 | Class->setBases(Bases, NumGoodBases); |
Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 647 | |
| 648 | // Delete the remaining (good) base class specifiers, since their |
| 649 | // data has been copied into the CXXRecordDecl. |
| 650 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | b77af8f | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 651 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 652 | |
| 653 | return Invalid; |
| 654 | } |
| 655 | |
| 656 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 657 | /// class, after checking whether there are any duplicate base |
| 658 | /// classes. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | void Sema::ActOnBaseSpecifiers(DeclPtrTy ClassDecl, BaseTy **Bases, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 660 | unsigned NumBases) { |
| 661 | if (!ClassDecl || !Bases || !NumBases) |
| 662 | return; |
| 663 | |
| 664 | AdjustDeclIfTemplate(ClassDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 665 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl.getAs<Decl>()), |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 666 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | 556877c | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 667 | } |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 668 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 669 | static CXXRecordDecl *GetClassForType(QualType T) { |
| 670 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 671 | return cast<CXXRecordDecl>(RT->getDecl()); |
| 672 | else if (const InjectedClassNameType *ICT = T->getAs<InjectedClassNameType>()) |
| 673 | return ICT->getDecl(); |
| 674 | else |
| 675 | return 0; |
| 676 | } |
| 677 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 678 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 679 | /// derived from the type \p Base. |
| 680 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 681 | if (!getLangOptions().CPlusPlus) |
| 682 | return false; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 683 | |
| 684 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 685 | if (!DerivedRD) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 686 | return false; |
| 687 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 688 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 689 | if (!BaseRD) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 690 | return false; |
| 691 | |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 692 | // FIXME: instantiate DerivedRD if necessary. We need a PoI for this. |
| 693 | return DerivedRD->hasDefinition() && DerivedRD->isDerivedFrom(BaseRD); |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 697 | /// derived from the type \p Base. |
| 698 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 699 | if (!getLangOptions().CPlusPlus) |
| 700 | return false; |
| 701 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 702 | CXXRecordDecl *DerivedRD = GetClassForType(Derived); |
| 703 | if (!DerivedRD) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 704 | return false; |
| 705 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 706 | CXXRecordDecl *BaseRD = GetClassForType(Base); |
| 707 | if (!BaseRD) |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 708 | return false; |
| 709 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 710 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 711 | } |
| 712 | |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 713 | void Sema::BuildBasePathArray(const CXXBasePaths &Paths, |
| 714 | CXXBaseSpecifierArray &BasePathArray) { |
| 715 | assert(BasePathArray.empty() && "Base path array must be empty!"); |
| 716 | assert(Paths.isRecordingPaths() && "Must record paths!"); |
| 717 | |
| 718 | const CXXBasePath &Path = Paths.front(); |
| 719 | |
| 720 | // We first go backward and check if we have a virtual base. |
| 721 | // FIXME: It would be better if CXXBasePath had the base specifier for |
| 722 | // the nearest virtual base. |
| 723 | unsigned Start = 0; |
| 724 | for (unsigned I = Path.size(); I != 0; --I) { |
| 725 | if (Path[I - 1].Base->isVirtual()) { |
| 726 | Start = I - 1; |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | // Now add all bases. |
| 732 | for (unsigned I = Start, E = Path.size(); I != E; ++I) |
| 733 | BasePathArray.push_back(Path[I].Base); |
| 734 | } |
| 735 | |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 736 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 737 | /// conversion (where Derived and Base are class types) is |
| 738 | /// well-formed, meaning that the conversion is unambiguous (and |
| 739 | /// that all of the base classes are accessible). Returns true |
| 740 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 741 | /// otherwise. Loc is the location where this routine should point to |
| 742 | /// if there is an error, and Range is the source range to highlight |
| 743 | /// if there is an error. |
| 744 | bool |
| 745 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 746 | unsigned InaccessibleBaseID, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 747 | unsigned AmbigiousBaseConvID, |
| 748 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 749 | DeclarationName Name, |
| 750 | CXXBaseSpecifierArray *BasePath) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 751 | // First, determine whether the path from Derived to Base is |
| 752 | // ambiguous. This is slightly more expensive than checking whether |
| 753 | // the Derived to Base conversion exists, because here we need to |
| 754 | // explore multiple paths to determine if there is an ambiguity. |
| 755 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 756 | /*DetectVirtual=*/false); |
| 757 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 758 | assert(DerivationOkay && |
| 759 | "Can only be used with a derived-to-base conversion"); |
| 760 | (void)DerivationOkay; |
| 761 | |
| 762 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 763 | if (InaccessibleBaseID) { |
| 764 | // Check that the base class can be accessed. |
| 765 | switch (CheckBaseClassAccess(Loc, Base, Derived, Paths.front(), |
| 766 | InaccessibleBaseID)) { |
| 767 | case AR_inaccessible: |
| 768 | return true; |
| 769 | case AR_accessible: |
| 770 | case AR_dependent: |
| 771 | case AR_delayed: |
| 772 | break; |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 773 | } |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 774 | } |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 775 | |
| 776 | // Build a base path if necessary. |
| 777 | if (BasePath) |
| 778 | BuildBasePathArray(Paths, *BasePath); |
| 779 | return false; |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | // We know that the derived-to-base conversion is ambiguous, and |
| 783 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 784 | // search just one more time to compute all of the possible paths so |
| 785 | // that we can print them out. This is more expensive than any of |
| 786 | // the previous derived-to-base checks we've done, but at this point |
| 787 | // performance isn't as much of an issue. |
| 788 | Paths.clear(); |
| 789 | Paths.setRecordingPaths(true); |
| 790 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 791 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 792 | (void)StillOkay; |
| 793 | |
| 794 | // Build up a textual representation of the ambiguous paths, e.g., |
| 795 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 796 | // conversions in the diagnostic. We only print one of the paths |
| 797 | // to each base class subobject. |
| 798 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 799 | |
| 800 | Diag(Loc, AmbigiousBaseConvID) |
| 801 | << Derived << Base << PathDisplayStr << Range << Name; |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | bool |
| 806 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 807 | SourceLocation Loc, SourceRange Range, |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 808 | CXXBaseSpecifierArray *BasePath, |
Sebastian Redl | 7c35368 | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 809 | bool IgnoreAccess) { |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 810 | return CheckDerivedToBaseConversion(Derived, Base, |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 811 | IgnoreAccess ? 0 |
| 812 | : diag::err_upcast_to_inaccessible_base, |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 813 | diag::err_ambiguous_derived_to_base_conv, |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 814 | Loc, Range, DeclarationName(), |
| 815 | BasePath); |
Douglas Gregor | 36d1b14 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | |
| 819 | /// @brief Builds a string representing ambiguous paths from a |
| 820 | /// specific derived class to different subobjects of the same base |
| 821 | /// class. |
| 822 | /// |
| 823 | /// This function builds a string that can be used in error messages |
| 824 | /// to show the different paths that one can take through the |
| 825 | /// inheritance hierarchy to go from the derived class to different |
| 826 | /// subobjects of a base class. The result looks something like this: |
| 827 | /// @code |
| 828 | /// struct D -> struct B -> struct A |
| 829 | /// struct D -> struct C -> struct A |
| 830 | /// @endcode |
| 831 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 832 | std::string PathDisplayStr; |
| 833 | std::set<unsigned> DisplayedPaths; |
| 834 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 835 | Path != Paths.end(); ++Path) { |
| 836 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 837 | // We haven't displayed a path to this particular base |
| 838 | // class subobject yet. |
| 839 | PathDisplayStr += "\n "; |
| 840 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 841 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 842 | Element != Path->end(); ++Element) |
| 843 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | return PathDisplayStr; |
| 848 | } |
| 849 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 850 | //===----------------------------------------------------------------------===// |
| 851 | // C++ class member Handling |
| 852 | //===----------------------------------------------------------------------===// |
| 853 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 854 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 855 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 856 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
Chris Lattner | eb4373d | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 857 | /// any. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 858 | Sema::DeclPtrTy |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 859 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 860 | MultiTemplateParamsArg TemplateParameterLists, |
Sebastian Redl | d6f7850 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 861 | ExprTy *BW, ExprTy *InitExpr, bool IsDefinition, |
| 862 | bool Deleted) { |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 863 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 864 | DeclarationName Name = GetNameForDeclarator(D); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 865 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 866 | Expr *Init = static_cast<Expr*>(InitExpr); |
| 867 | SourceLocation Loc = D.getIdentifierLoc(); |
| 868 | |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 869 | bool isFunc = D.isFunctionDeclarator(); |
| 870 | |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 871 | assert(!DS.isFriendSpecified()); |
| 872 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 873 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 874 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 875 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 876 | // data members and cannot be applied to names declared const or static, |
| 877 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 878 | switch (DS.getStorageClassSpec()) { |
| 879 | case DeclSpec::SCS_unspecified: |
| 880 | case DeclSpec::SCS_typedef: |
| 881 | case DeclSpec::SCS_static: |
| 882 | // FALL THROUGH. |
| 883 | break; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 884 | case DeclSpec::SCS_mutable: |
| 885 | if (isFunc) { |
| 886 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 887 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 888 | else |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 889 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | |
Sebastian Redl | 8071edb | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 891 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 892 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 893 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 894 | } else { |
| 895 | QualType T = GetTypeForDeclarator(D, S); |
| 896 | diag::kind err = static_cast<diag::kind>(0); |
| 897 | if (T->isReferenceType()) |
| 898 | err = diag::err_mutable_reference; |
| 899 | else if (T.isConstQualified()) |
| 900 | err = diag::err_mutable_const; |
| 901 | if (err != 0) { |
| 902 | if (DS.getStorageClassSpecLoc().isValid()) |
| 903 | Diag(DS.getStorageClassSpecLoc(), err); |
| 904 | else |
| 905 | Diag(DS.getThreadSpecLoc(), err); |
Sebastian Redl | 8071edb | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 906 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 907 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 908 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 909 | } |
| 910 | } |
| 911 | break; |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 912 | default: |
| 913 | if (DS.getStorageClassSpecLoc().isValid()) |
| 914 | Diag(DS.getStorageClassSpecLoc(), |
| 915 | diag::err_storageclass_invalid_for_member); |
| 916 | else |
| 917 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 918 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 919 | } |
| 920 | |
Argyrios Kyrtzidis | 2e3e756 | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 921 | if (!isFunc && |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 922 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename && |
Argyrios Kyrtzidis | 2e3e756 | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 923 | D.getNumTypeObjects() == 0) { |
Argyrios Kyrtzidis | 1207d31 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 924 | // Check also for this case: |
| 925 | // |
| 926 | // typedef int f(); |
| 927 | // f a; |
| 928 | // |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 929 | QualType TDType = GetTypeFromParser(DS.getTypeRep()); |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 930 | isFunc = TDType->isFunctionType(); |
Argyrios Kyrtzidis | 1207d31 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 931 | } |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 932 | |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 933 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 934 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | 1207d31 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 935 | !isFunc); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 936 | |
| 937 | Decl *Member; |
Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 938 | if (isInstField) { |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 939 | // FIXME: Check for template parameters! |
Douglas Gregor | 4261e4c | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 940 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 941 | AS); |
Chris Lattner | 97e277e | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 942 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 943 | } else { |
Sebastian Redl | d6f7850 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 944 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), IsDefinition) |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 945 | .getAs<Decl>(); |
Chris Lattner | 97e277e | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 946 | if (!Member) { |
| 947 | if (BitWidth) DeleteExpr(BitWidth); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 948 | return DeclPtrTy(); |
Chris Lattner | 97e277e | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 949 | } |
Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 950 | |
| 951 | // Non-instance-fields can't have a bitfield. |
| 952 | if (BitWidth) { |
| 953 | if (Member->isInvalidDecl()) { |
| 954 | // don't emit another diagnostic. |
Douglas Gregor | 212cab3 | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 955 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 956 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 957 | // "static member 'A' cannot be a bit-field" |
| 958 | Diag(Loc, diag::err_static_not_bitfield) |
| 959 | << Name << BitWidth->getSourceRange(); |
| 960 | } else if (isa<TypedefDecl>(Member)) { |
| 961 | // "typedef member 'x' cannot be a bit-field" |
| 962 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 963 | << Name << BitWidth->getSourceRange(); |
| 964 | } else { |
| 965 | // A function typedef ("typedef int f(); f a;"). |
| 966 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 967 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 968 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 1efa437 | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 969 | << BitWidth->getSourceRange(); |
Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 970 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | |
Chris Lattner | d26760a | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 972 | DeleteExpr(BitWidth); |
| 973 | BitWidth = 0; |
| 974 | Member->setInvalidDecl(); |
| 975 | } |
Douglas Gregor | 4261e4c | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 976 | |
| 977 | Member->setAccess(AS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 979 | // If we have declared a member function template, set the access of the |
| 980 | // templated declaration as well. |
| 981 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 982 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 73bf7b4 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 983 | } |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 984 | |
Douglas Gregor | 92751d4 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 985 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 986 | |
Douglas Gregor | 0c88030 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 987 | if (Init) |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 988 | AddInitializerToDecl(DeclPtrTy::make(Member), ExprArg(*this, Init), false); |
Sebastian Redl | 42e92c4 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 989 | if (Deleted) // FIXME: Source location is not very good. |
| 990 | SetDeclDeleted(DeclPtrTy::make(Member), D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 991 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 992 | if (isInstField) { |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 993 | FieldCollector->Add(cast<FieldDecl>(Member)); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 994 | return DeclPtrTy(); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 995 | } |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 996 | return DeclPtrTy::make(Member); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 999 | /// \brief Find the direct and/or virtual base specifiers that |
| 1000 | /// correspond to the given base type, for use in base initialization |
| 1001 | /// within a constructor. |
| 1002 | static bool FindBaseInitializer(Sema &SemaRef, |
| 1003 | CXXRecordDecl *ClassDecl, |
| 1004 | QualType BaseType, |
| 1005 | const CXXBaseSpecifier *&DirectBaseSpec, |
| 1006 | const CXXBaseSpecifier *&VirtualBaseSpec) { |
| 1007 | // First, check for a direct base class. |
| 1008 | DirectBaseSpec = 0; |
| 1009 | for (CXXRecordDecl::base_class_const_iterator Base |
| 1010 | = ClassDecl->bases_begin(); |
| 1011 | Base != ClassDecl->bases_end(); ++Base) { |
| 1012 | if (SemaRef.Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
| 1013 | // We found a direct base of this type. That's what we're |
| 1014 | // initializing. |
| 1015 | DirectBaseSpec = &*Base; |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | // Check for a virtual base class. |
| 1021 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1022 | // there are no virtual bases. |
| 1023 | VirtualBaseSpec = 0; |
| 1024 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1025 | // We haven't found a base yet; search the class hierarchy for a |
| 1026 | // virtual base class. |
| 1027 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1028 | /*DetectVirtual=*/false); |
| 1029 | if (SemaRef.IsDerivedFrom(SemaRef.Context.getTypeDeclType(ClassDecl), |
| 1030 | BaseType, Paths)) { |
| 1031 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 1032 | Path != Paths.end(); ++Path) { |
| 1033 | if (Path->back().Base->isVirtual()) { |
| 1034 | VirtualBaseSpec = Path->back().Base; |
| 1035 | break; |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | return DirectBaseSpec || VirtualBaseSpec; |
| 1042 | } |
| 1043 | |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1044 | /// ActOnMemInitializer - Handle a C++ member initializer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | Sema::MemInitResult |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1046 | Sema::ActOnMemInitializer(DeclPtrTy ConstructorD, |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1047 | Scope *S, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1048 | CXXScopeSpec &SS, |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1049 | IdentifierInfo *MemberOrBase, |
Fariborz Jahanian | c1fc3ec | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1050 | TypeTy *TemplateTypeTy, |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1051 | SourceLocation IdLoc, |
| 1052 | SourceLocation LParenLoc, |
| 1053 | ExprTy **Args, unsigned NumArgs, |
| 1054 | SourceLocation *CommaLocs, |
| 1055 | SourceLocation RParenLoc) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1056 | if (!ConstructorD) |
| 1057 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | |
Douglas Gregor | c8c277a | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1059 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
| 1061 | CXXConstructorDecl *Constructor |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1062 | = dyn_cast<CXXConstructorDecl>(ConstructorD.getAs<Decl>()); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1063 | if (!Constructor) { |
| 1064 | // The user wrote a constructor initializer on a function that is |
| 1065 | // not a C++ constructor. Ignore the error for now, because we may |
| 1066 | // have more member initializers coming; we'll diagnose it just |
| 1067 | // once in ActOnMemInitializers. |
| 1068 | return true; |
| 1069 | } |
| 1070 | |
| 1071 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1072 | |
| 1073 | // C++ [class.base.init]p2: |
| 1074 | // Names in a mem-initializer-id are looked up in the scope of the |
| 1075 | // constructor’s class and, if not found in that scope, are looked |
| 1076 | // up in the scope containing the constructor’s |
| 1077 | // definition. [Note: if the constructor’s class contains a member |
| 1078 | // with the same name as a direct or virtual base class of the |
| 1079 | // class, a mem-initializer-id naming the member or base class and |
| 1080 | // composed of a single identifier refers to the class member. A |
| 1081 | // mem-initializer-id for the hidden base class may be specified |
| 1082 | // using a qualified name. ] |
Fariborz Jahanian | c1fc3ec | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 1083 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | 302bb66 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1084 | // Look for a member, first. |
| 1085 | FieldDecl *Member = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | DeclContext::lookup_result Result |
Fariborz Jahanian | 302bb66 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1087 | = ClassDecl->lookup(MemberOrBase); |
| 1088 | if (Result.first != Result.second) |
| 1089 | Member = dyn_cast<FieldDecl>(*Result.first); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1090 | |
Fariborz Jahanian | 302bb66 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 1091 | // FIXME: Handle members of an anonymous union. |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1092 | |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1093 | if (Member) |
| 1094 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1095 | LParenLoc, RParenLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1096 | } |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1097 | // It didn't name a member, so see if it names a class. |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1098 | QualType BaseType; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1099 | TypeSourceInfo *TInfo = 0; |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1100 | |
| 1101 | if (TemplateTypeTy) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1102 | BaseType = GetTypeFromParser(TemplateTypeTy, &TInfo); |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1103 | } else { |
| 1104 | LookupResult R(*this, MemberOrBase, IdLoc, LookupOrdinaryName); |
| 1105 | LookupParsedName(R, S, &SS); |
| 1106 | |
| 1107 | TypeDecl *TyD = R.getAsSingle<TypeDecl>(); |
| 1108 | if (!TyD) { |
| 1109 | if (R.isAmbiguous()) return true; |
| 1110 | |
John McCall | da6841b | 2010-04-09 19:01:14 +0000 | [diff] [blame] | 1111 | // We don't want access-control diagnostics here. |
| 1112 | R.suppressDiagnostics(); |
| 1113 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1114 | if (SS.isSet() && isDependentScopeSpecifier(SS)) { |
| 1115 | bool NotUnknownSpecialization = false; |
| 1116 | DeclContext *DC = computeDeclContext(SS, false); |
| 1117 | if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(DC)) |
| 1118 | NotUnknownSpecialization = !Record->hasAnyDependentBases(); |
| 1119 | |
| 1120 | if (!NotUnknownSpecialization) { |
| 1121 | // When the scope specifier can refer to a member of an unknown |
| 1122 | // specialization, we take it as a type name. |
Douglas Gregor | bbdf20a | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 1123 | BaseType = CheckTypenameType(ETK_None, |
| 1124 | (NestedNameSpecifier *)SS.getScopeRep(), |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1125 | *MemberOrBase, SS.getRange()); |
Douglas Gregor | 281c486 | 2010-03-07 23:26:22 +0000 | [diff] [blame] | 1126 | if (BaseType.isNull()) |
| 1127 | return true; |
| 1128 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1129 | R.clear(); |
| 1130 | } |
| 1131 | } |
| 1132 | |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1133 | // If no results were found, try to correct typos. |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1134 | if (R.empty() && BaseType.isNull() && |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1135 | CorrectTypo(R, S, &SS, ClassDecl, 0, CTC_NoKeywords) && |
| 1136 | R.isSingleResult()) { |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1137 | if (FieldDecl *Member = R.getAsSingle<FieldDecl>()) { |
| 1138 | if (Member->getDeclContext()->getLookupContext()->Equals(ClassDecl)) { |
| 1139 | // We have found a non-static data member with a similar |
| 1140 | // name to what was typed; complain and initialize that |
| 1141 | // member. |
| 1142 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1143 | << MemberOrBase << true << R.getLookupName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1144 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1145 | R.getLookupName().getAsString()); |
Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1146 | Diag(Member->getLocation(), diag::note_previous_decl) |
| 1147 | << Member->getDeclName(); |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1148 | |
| 1149 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 1150 | LParenLoc, RParenLoc); |
| 1151 | } |
| 1152 | } else if (TypeDecl *Type = R.getAsSingle<TypeDecl>()) { |
| 1153 | const CXXBaseSpecifier *DirectBaseSpec; |
| 1154 | const CXXBaseSpecifier *VirtualBaseSpec; |
| 1155 | if (FindBaseInitializer(*this, ClassDecl, |
| 1156 | Context.getTypeDeclType(Type), |
| 1157 | DirectBaseSpec, VirtualBaseSpec)) { |
| 1158 | // We have found a direct or virtual base class with a |
| 1159 | // similar name to what was typed; complain and initialize |
| 1160 | // that base class. |
| 1161 | Diag(R.getNameLoc(), diag::err_mem_init_not_member_or_class_suggest) |
| 1162 | << MemberOrBase << false << R.getLookupName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1163 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1164 | R.getLookupName().getAsString()); |
Douglas Gregor | 43a0857 | 2010-01-07 00:26:25 +0000 | [diff] [blame] | 1165 | |
| 1166 | const CXXBaseSpecifier *BaseSpec = DirectBaseSpec? DirectBaseSpec |
| 1167 | : VirtualBaseSpec; |
| 1168 | Diag(BaseSpec->getSourceRange().getBegin(), |
| 1169 | diag::note_base_class_specified_here) |
| 1170 | << BaseSpec->getType() |
| 1171 | << BaseSpec->getSourceRange(); |
| 1172 | |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1173 | TyD = Type; |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1178 | if (!TyD && BaseType.isNull()) { |
Douglas Gregor | 15e77a2 | 2009-12-31 09:10:24 +0000 | [diff] [blame] | 1179 | Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 1180 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
| 1181 | return true; |
| 1182 | } |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1185 | if (BaseType.isNull()) { |
| 1186 | BaseType = Context.getTypeDeclType(TyD); |
| 1187 | if (SS.isSet()) { |
| 1188 | NestedNameSpecifier *Qualifier = |
| 1189 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1190 | |
Douglas Gregor | a3b624a | 2010-01-19 06:46:48 +0000 | [diff] [blame] | 1191 | // FIXME: preserve source range information |
| 1192 | BaseType = Context.getQualifiedNameType(Qualifier, BaseType); |
| 1193 | } |
John McCall | b5a0d31 | 2009-12-21 10:41:20 +0000 | [diff] [blame] | 1194 | } |
| 1195 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1197 | if (!TInfo) |
| 1198 | TInfo = Context.getTrivialTypeSourceInfo(BaseType, IdLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1199 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1200 | return BuildBaseInitializer(BaseType, TInfo, (Expr **)Args, NumArgs, |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1201 | LParenLoc, RParenLoc, ClassDecl); |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
John McCall | e22a04a | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1204 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 1205 | /// containing the field that is being initialized. Returns true if there is an |
| 1206 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 1207 | /// otherwise. |
| 1208 | static bool InitExprContainsUninitializedFields(const Stmt* S, |
| 1209 | const FieldDecl* LhsField, |
| 1210 | SourceLocation* L) { |
| 1211 | const MemberExpr* ME = dyn_cast<MemberExpr>(S); |
| 1212 | if (ME) { |
| 1213 | const NamedDecl* RhsField = ME->getMemberDecl(); |
| 1214 | if (RhsField == LhsField) { |
| 1215 | // Initializing a field with itself. Throw a warning. |
| 1216 | // But wait; there are exceptions! |
| 1217 | // Exception #1: The field may not belong to this record. |
| 1218 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
| 1219 | const Expr* base = ME->getBase(); |
| 1220 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1221 | // Even though the field matches, it does not belong to this record. |
| 1222 | return false; |
| 1223 | } |
| 1224 | // None of the exceptions triggered; return true to indicate an |
| 1225 | // uninitialized field was used. |
| 1226 | *L = ME->getMemberLoc(); |
| 1227 | return true; |
| 1228 | } |
| 1229 | } |
| 1230 | bool found = false; |
| 1231 | for (Stmt::const_child_iterator it = S->child_begin(); |
| 1232 | it != S->child_end() && found == false; |
| 1233 | ++it) { |
| 1234 | if (isa<CallExpr>(S)) { |
| 1235 | // Do not descend into function calls or constructors, as the use |
| 1236 | // of an uninitialized field may be valid. One would have to inspect |
| 1237 | // the contents of the function/ctor to determine if it is safe or not. |
| 1238 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1239 | // may be safe, depending on what the function/ctor does. |
| 1240 | continue; |
| 1241 | } |
| 1242 | found = InitExprContainsUninitializedFields(*it, LhsField, L); |
| 1243 | } |
| 1244 | return found; |
| 1245 | } |
| 1246 | |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1247 | Sema::MemInitResult |
| 1248 | Sema::BuildMemberInitializer(FieldDecl *Member, Expr **Args, |
| 1249 | unsigned NumArgs, SourceLocation IdLoc, |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1250 | SourceLocation LParenLoc, |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1251 | SourceLocation RParenLoc) { |
John McCall | e22a04a | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1252 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1253 | // foo(foo) |
| 1254 | // where foo is not also a parameter to the constructor. |
John McCall | c90f6d7 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1255 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | e22a04a | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1256 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1257 | SourceLocation L; |
| 1258 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1259 | // FIXME: Return true in the case when other fields are used before being |
| 1260 | // uninitialized. For example, let this field be the i'th field. When |
| 1261 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1262 | // fields are used, as they are not yet initialized. |
| 1263 | // Right now we are only handling the case where the i'th field uses |
| 1264 | // itself in its initializer. |
| 1265 | Diag(L, diag::warn_field_is_uninit); |
| 1266 | } |
| 1267 | } |
| 1268 | |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1269 | bool HasDependentArg = false; |
| 1270 | for (unsigned i = 0; i < NumArgs; i++) |
| 1271 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1272 | |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1273 | QualType FieldType = Member->getType(); |
| 1274 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 1275 | FieldType = Array->getElementType(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1276 | if (FieldType->isDependentType() || HasDependentArg) { |
| 1277 | // Can't check initialization for a member of dependent type or when |
| 1278 | // any of the arguments are type-dependent expressions. |
| 1279 | OwningExprResult Init |
| 1280 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1281 | RParenLoc)); |
| 1282 | |
| 1283 | // Erase any temporaries within this evaluation context; we're not |
| 1284 | // going to track them in the AST, since we'll be rebuilding the |
| 1285 | // ASTs during template instantiation. |
| 1286 | ExprTemporaries.erase( |
| 1287 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1288 | ExprTemporaries.end()); |
| 1289 | |
| 1290 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1291 | LParenLoc, |
| 1292 | Init.takeAs<Expr>(), |
| 1293 | RParenLoc); |
| 1294 | |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1295 | } |
Anders Carlsson | 1fe64cb | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1296 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1297 | if (Member->isInvalidDecl()) |
| 1298 | return true; |
Anders Carlsson | 1fe64cb | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1299 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1300 | // Initialize the member. |
| 1301 | InitializedEntity MemberEntity = |
| 1302 | InitializedEntity::InitializeMember(Member, 0); |
| 1303 | InitializationKind Kind = |
| 1304 | InitializationKind::CreateDirect(IdLoc, LParenLoc, RParenLoc); |
| 1305 | |
| 1306 | InitializationSequence InitSeq(*this, MemberEntity, Kind, Args, NumArgs); |
| 1307 | |
| 1308 | OwningExprResult MemberInit = |
| 1309 | InitSeq.Perform(*this, MemberEntity, Kind, |
| 1310 | MultiExprArg(*this, (void**)Args, NumArgs), 0); |
| 1311 | if (MemberInit.isInvalid()) |
| 1312 | return true; |
| 1313 | |
| 1314 | // C++0x [class.base.init]p7: |
| 1315 | // The initialization of each base and member constitutes a |
| 1316 | // full-expression. |
| 1317 | MemberInit = MaybeCreateCXXExprWithTemporaries(move(MemberInit)); |
| 1318 | if (MemberInit.isInvalid()) |
| 1319 | return true; |
| 1320 | |
| 1321 | // If we are in a dependent context, template instantiation will |
| 1322 | // perform this type-checking again. Just save the arguments that we |
| 1323 | // received in a ParenListExpr. |
| 1324 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1325 | // of the information that we have about the member |
| 1326 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1327 | // and this approach is far more likely to get the corner cases right. |
| 1328 | if (CurContext->isDependentContext()) { |
| 1329 | // Bump the reference count of all of the arguments. |
| 1330 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1331 | Args[I]->Retain(); |
| 1332 | |
| 1333 | OwningExprResult Init |
| 1334 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1335 | RParenLoc)); |
| 1336 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
| 1337 | LParenLoc, |
| 1338 | Init.takeAs<Expr>(), |
| 1339 | RParenLoc); |
| 1340 | } |
| 1341 | |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1342 | return new (Context) CXXBaseOrMemberInitializer(Context, Member, IdLoc, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1343 | LParenLoc, |
| 1344 | MemberInit.takeAs<Expr>(), |
| 1345 | RParenLoc); |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | Sema::MemInitResult |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1349 | Sema::BuildBaseInitializer(QualType BaseType, TypeSourceInfo *BaseTInfo, |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1350 | Expr **Args, unsigned NumArgs, |
| 1351 | SourceLocation LParenLoc, SourceLocation RParenLoc, |
| 1352 | CXXRecordDecl *ClassDecl) { |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1353 | bool HasDependentArg = false; |
| 1354 | for (unsigned i = 0; i < NumArgs; i++) |
| 1355 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1356 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1357 | SourceLocation BaseLoc = BaseTInfo->getTypeLoc().getSourceRange().getBegin(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1358 | if (BaseType->isDependentType() || HasDependentArg) { |
| 1359 | // Can't check initialization for a base of dependent type or when |
| 1360 | // any of the arguments are type-dependent expressions. |
| 1361 | OwningExprResult BaseInit |
| 1362 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1363 | RParenLoc)); |
Eli Friedman | 8e1433b | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1364 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1365 | // Erase any temporaries within this evaluation context; we're not |
| 1366 | // going to track them in the AST, since we'll be rebuilding the |
| 1367 | // ASTs during template instantiation. |
| 1368 | ExprTemporaries.erase( |
| 1369 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 1370 | ExprTemporaries.end()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1372 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 1c0f8bb | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1373 | /*IsVirtual=*/false, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1374 | LParenLoc, |
| 1375 | BaseInit.takeAs<Expr>(), |
| 1376 | RParenLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1377 | } |
Anders Carlsson | 1fe64cb | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1378 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1379 | if (!BaseType->isRecordType()) |
| 1380 | return Diag(BaseLoc, diag::err_base_init_does_not_name_class) |
| 1381 | << BaseType << BaseTInfo->getTypeLoc().getSourceRange(); |
| 1382 | |
| 1383 | // C++ [class.base.init]p2: |
| 1384 | // [...] Unless the mem-initializer-id names a nonstatic data |
| 1385 | // member of the constructor’s class or a direct or virtual base |
| 1386 | // of that class, the mem-initializer is ill-formed. A |
| 1387 | // mem-initializer-list can initialize a base class using any |
| 1388 | // name that denotes that base class type. |
| 1389 | |
| 1390 | // Check for direct and virtual base classes. |
| 1391 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1392 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1393 | FindBaseInitializer(*this, ClassDecl, BaseType, DirectBaseSpec, |
| 1394 | VirtualBaseSpec); |
| 1395 | |
| 1396 | // C++ [base.class.init]p2: |
| 1397 | // If a mem-initializer-id is ambiguous because it designates both |
| 1398 | // a direct non-virtual base class and an inherited virtual base |
| 1399 | // class, the mem-initializer is ill-formed. |
| 1400 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1401 | return Diag(BaseLoc, diag::err_base_init_direct_and_virtual) |
| 1402 | << BaseType << BaseTInfo->getTypeLoc().getSourceRange(); |
| 1403 | // C++ [base.class.init]p2: |
| 1404 | // Unless the mem-initializer-id names a nonstatic data membeer of the |
| 1405 | // constructor's class ot a direst or virtual base of that class, the |
| 1406 | // mem-initializer is ill-formed. |
| 1407 | if (!DirectBaseSpec && !VirtualBaseSpec) |
| 1408 | return Diag(BaseLoc, diag::err_not_direct_base_or_virtual) |
John McCall | 1e67dd6 | 2010-04-27 01:43:38 +0000 | [diff] [blame] | 1409 | << BaseType << Context.getTypeDeclType(ClassDecl) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1410 | << BaseTInfo->getTypeLoc().getSourceRange(); |
| 1411 | |
| 1412 | CXXBaseSpecifier *BaseSpec |
| 1413 | = const_cast<CXXBaseSpecifier *>(DirectBaseSpec); |
| 1414 | if (!BaseSpec) |
| 1415 | BaseSpec = const_cast<CXXBaseSpecifier *>(VirtualBaseSpec); |
| 1416 | |
| 1417 | // Initialize the base. |
| 1418 | InitializedEntity BaseEntity = |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1419 | InitializedEntity::InitializeBase(Context, BaseSpec, VirtualBaseSpec); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1420 | InitializationKind Kind = |
| 1421 | InitializationKind::CreateDirect(BaseLoc, LParenLoc, RParenLoc); |
| 1422 | |
| 1423 | InitializationSequence InitSeq(*this, BaseEntity, Kind, Args, NumArgs); |
| 1424 | |
| 1425 | OwningExprResult BaseInit = |
| 1426 | InitSeq.Perform(*this, BaseEntity, Kind, |
| 1427 | MultiExprArg(*this, (void**)Args, NumArgs), 0); |
| 1428 | if (BaseInit.isInvalid()) |
| 1429 | return true; |
| 1430 | |
| 1431 | // C++0x [class.base.init]p7: |
| 1432 | // The initialization of each base and member constitutes a |
| 1433 | // full-expression. |
| 1434 | BaseInit = MaybeCreateCXXExprWithTemporaries(move(BaseInit)); |
| 1435 | if (BaseInit.isInvalid()) |
| 1436 | return true; |
| 1437 | |
| 1438 | // If we are in a dependent context, template instantiation will |
| 1439 | // perform this type-checking again. Just save the arguments that we |
| 1440 | // received in a ParenListExpr. |
| 1441 | // FIXME: This isn't quite ideal, since our ASTs don't capture all |
| 1442 | // of the information that we have about the base |
| 1443 | // initializer. However, deconstructing the ASTs is a dicey process, |
| 1444 | // and this approach is far more likely to get the corner cases right. |
| 1445 | if (CurContext->isDependentContext()) { |
| 1446 | // Bump the reference count of all of the arguments. |
| 1447 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1448 | Args[I]->Retain(); |
| 1449 | |
| 1450 | OwningExprResult Init |
| 1451 | = Owned(new (Context) ParenListExpr(Context, LParenLoc, Args, NumArgs, |
| 1452 | RParenLoc)); |
| 1453 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 1c0f8bb | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1454 | BaseSpec->isVirtual(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1455 | LParenLoc, |
| 1456 | Init.takeAs<Expr>(), |
| 1457 | RParenLoc); |
| 1458 | } |
| 1459 | |
| 1460 | return new (Context) CXXBaseOrMemberInitializer(Context, BaseTInfo, |
Anders Carlsson | 1c0f8bb | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 1461 | BaseSpec->isVirtual(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1462 | LParenLoc, |
| 1463 | BaseInit.takeAs<Expr>(), |
| 1464 | RParenLoc); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1467 | /// ImplicitInitializerKind - How an implicit base or member initializer should |
| 1468 | /// initialize its base or member. |
| 1469 | enum ImplicitInitializerKind { |
| 1470 | IIK_Default, |
| 1471 | IIK_Copy, |
| 1472 | IIK_Move |
| 1473 | }; |
| 1474 | |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1475 | static bool |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1476 | BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1477 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1478 | CXXBaseSpecifier *BaseSpec, |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1479 | bool IsInheritedVirtualBase, |
| 1480 | CXXBaseOrMemberInitializer *&CXXBaseInit) { |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1481 | InitializedEntity InitEntity |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1482 | = InitializedEntity::InitializeBase(SemaRef.Context, BaseSpec, |
| 1483 | IsInheritedVirtualBase); |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1484 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1485 | Sema::OwningExprResult BaseInit(SemaRef); |
| 1486 | |
| 1487 | switch (ImplicitInitKind) { |
| 1488 | case IIK_Default: { |
| 1489 | InitializationKind InitKind |
| 1490 | = InitializationKind::CreateDefault(Constructor->getLocation()); |
| 1491 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1492 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
| 1493 | Sema::MultiExprArg(SemaRef, 0, 0)); |
| 1494 | break; |
| 1495 | } |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1496 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1497 | case IIK_Copy: { |
| 1498 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1499 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1500 | |
| 1501 | Expr *CopyCtorArg = |
| 1502 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
| 1503 | SourceLocation(), ParamType, 0); |
| 1504 | |
Anders Carlsson | af13c7b | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1505 | // Cast to the base class to avoid ambiguities. |
Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame^] | 1506 | QualType ArgTy = |
| 1507 | SemaRef.Context.getQualifiedType(BaseSpec->getType().getUnqualifiedType(), |
| 1508 | ParamType.getQualifiers()); |
| 1509 | SemaRef.ImpCastExprToType(CopyCtorArg, ArgTy, |
Anders Carlsson | af13c7b | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1510 | CastExpr::CK_UncheckedDerivedToBase, |
Anders Carlsson | 36db0d9 | 2010-04-24 22:54:32 +0000 | [diff] [blame] | 1511 | /*isLvalue=*/true, |
| 1512 | CXXBaseSpecifierArray(BaseSpec)); |
Anders Carlsson | af13c7b | 2010-04-24 22:02:54 +0000 | [diff] [blame] | 1513 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1514 | InitializationKind InitKind |
| 1515 | = InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1516 | SourceLocation(), SourceLocation()); |
| 1517 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1518 | &CopyCtorArg, 1); |
| 1519 | BaseInit = InitSeq.Perform(SemaRef, InitEntity, InitKind, |
| 1520 | Sema::MultiExprArg(SemaRef, |
| 1521 | (void**)&CopyCtorArg, 1)); |
| 1522 | break; |
| 1523 | } |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1524 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1525 | case IIK_Move: |
| 1526 | assert(false && "Unhandled initializer kind!"); |
| 1527 | } |
| 1528 | |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1529 | BaseInit = SemaRef.MaybeCreateCXXExprWithTemporaries(move(BaseInit)); |
| 1530 | if (BaseInit.isInvalid()) |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1531 | return true; |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1532 | |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1533 | CXXBaseInit = |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1534 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
| 1535 | SemaRef.Context.getTrivialTypeSourceInfo(BaseSpec->getType(), |
| 1536 | SourceLocation()), |
| 1537 | BaseSpec->isVirtual(), |
| 1538 | SourceLocation(), |
| 1539 | BaseInit.takeAs<Expr>(), |
| 1540 | SourceLocation()); |
| 1541 | |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1542 | return false; |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1545 | static bool |
| 1546 | BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor, |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1547 | ImplicitInitializerKind ImplicitInitKind, |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1548 | FieldDecl *Field, |
| 1549 | CXXBaseOrMemberInitializer *&CXXMemberInit) { |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1550 | if (ImplicitInitKind == IIK_Copy) { |
Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame^] | 1551 | // FIXME: We should not return early here, but will do so until |
| 1552 | // we know how to handle copy initialization of arrays. |
| 1553 | CXXMemberInit = 0; |
| 1554 | return false; |
| 1555 | |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1556 | ParmVarDecl *Param = Constructor->getParamDecl(0); |
| 1557 | QualType ParamType = Param->getType().getNonReferenceType(); |
| 1558 | |
| 1559 | Expr *MemberExprBase = |
| 1560 | DeclRefExpr::Create(SemaRef.Context, 0, SourceRange(), Param, |
| 1561 | SourceLocation(), ParamType, 0); |
| 1562 | |
| 1563 | |
| 1564 | Expr *CopyCtorArg = |
| 1565 | MemberExpr::Create(SemaRef.Context, MemberExprBase, /*IsArrow=*/false, |
| 1566 | 0, SourceRange(), Field, |
| 1567 | DeclAccessPair::make(Field, Field->getAccess()), |
| 1568 | SourceLocation(), 0, |
| 1569 | Field->getType().getNonReferenceType()); |
| 1570 | |
| 1571 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 1572 | InitializationKind InitKind = |
| 1573 | InitializationKind::CreateDirect(Constructor->getLocation(), |
| 1574 | SourceLocation(), SourceLocation()); |
| 1575 | |
| 1576 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, |
| 1577 | &CopyCtorArg, 1); |
| 1578 | |
| 1579 | Sema::OwningExprResult MemberInit = |
| 1580 | InitSeq.Perform(SemaRef, InitEntity, InitKind, |
| 1581 | Sema::MultiExprArg(SemaRef, (void**)&CopyCtorArg, 1), 0); |
| 1582 | if (MemberInit.isInvalid()) |
| 1583 | return true; |
| 1584 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1585 | CXXMemberInit = 0; |
| 1586 | return false; |
| 1587 | } |
| 1588 | |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1589 | assert(ImplicitInitKind == IIK_Default && "Unhandled implicit init kind!"); |
| 1590 | |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1591 | QualType FieldBaseElementType = |
| 1592 | SemaRef.Context.getBaseElementType(Field->getType()); |
| 1593 | |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1594 | if (FieldBaseElementType->isRecordType()) { |
| 1595 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 1596 | InitializationKind InitKind = |
| 1597 | InitializationKind::CreateDefault(Constructor->getLocation()); |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1598 | |
| 1599 | InitializationSequence InitSeq(SemaRef, InitEntity, InitKind, 0, 0); |
| 1600 | Sema::OwningExprResult MemberInit = |
| 1601 | InitSeq.Perform(SemaRef, InitEntity, InitKind, |
| 1602 | Sema::MultiExprArg(SemaRef, 0, 0)); |
| 1603 | MemberInit = SemaRef.MaybeCreateCXXExprWithTemporaries(move(MemberInit)); |
| 1604 | if (MemberInit.isInvalid()) |
| 1605 | return true; |
| 1606 | |
| 1607 | CXXMemberInit = |
| 1608 | new (SemaRef.Context) CXXBaseOrMemberInitializer(SemaRef.Context, |
| 1609 | Field, SourceLocation(), |
| 1610 | SourceLocation(), |
| 1611 | MemberInit.takeAs<Expr>(), |
| 1612 | SourceLocation()); |
| 1613 | return false; |
| 1614 | } |
Anders Carlsson | dca6be0 | 2010-04-23 03:07:47 +0000 | [diff] [blame] | 1615 | |
| 1616 | if (FieldBaseElementType->isReferenceType()) { |
| 1617 | SemaRef.Diag(Constructor->getLocation(), |
| 1618 | diag::err_uninitialized_member_in_ctor) |
| 1619 | << (int)Constructor->isImplicit() |
| 1620 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1621 | << 0 << Field->getDeclName(); |
| 1622 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1623 | return true; |
| 1624 | } |
| 1625 | |
| 1626 | if (FieldBaseElementType.isConstQualified()) { |
| 1627 | SemaRef.Diag(Constructor->getLocation(), |
| 1628 | diag::err_uninitialized_member_in_ctor) |
| 1629 | << (int)Constructor->isImplicit() |
| 1630 | << SemaRef.Context.getTagDeclType(Constructor->getParent()) |
| 1631 | << 1 << Field->getDeclName(); |
| 1632 | SemaRef.Diag(Field->getLocation(), diag::note_declared_at); |
| 1633 | return true; |
| 1634 | } |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1635 | |
| 1636 | // Nothing to initialize. |
| 1637 | CXXMemberInit = 0; |
| 1638 | return false; |
| 1639 | } |
| 1640 | |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1641 | bool |
Anders Carlsson | 561f793 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1642 | Sema::SetBaseOrMemberInitializers(CXXConstructorDecl *Constructor, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1643 | CXXBaseOrMemberInitializer **Initializers, |
| 1644 | unsigned NumInitializers, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 1645 | bool AnyErrors) { |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1646 | if (Constructor->getDeclContext()->isDependentContext()) { |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1647 | // Just store the initializers as written, they will be checked during |
| 1648 | // instantiation. |
| 1649 | if (NumInitializers > 0) { |
| 1650 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1651 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1652 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
| 1653 | memcpy(baseOrMemberInitializers, Initializers, |
| 1654 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
| 1655 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
| 1656 | } |
| 1657 | |
| 1658 | return false; |
| 1659 | } |
| 1660 | |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1661 | ImplicitInitializerKind ImplicitInitKind = IIK_Default; |
| 1662 | |
| 1663 | // FIXME: Handle implicit move constructors. |
| 1664 | if (Constructor->isImplicit() && Constructor->isCopyConstructor()) |
| 1665 | ImplicitInitKind = IIK_Copy; |
| 1666 | |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1667 | // We need to build the initializer AST according to order of construction |
| 1668 | // and not what user specified in the Initializers list. |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1669 | CXXRecordDecl *ClassDecl = Constructor->getParent()->getDefinition(); |
Douglas Gregor | c14922f | 2010-03-26 22:43:07 +0000 | [diff] [blame] | 1670 | if (!ClassDecl) |
| 1671 | return true; |
| 1672 | |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1673 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
| 1674 | llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1675 | bool HadError = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1677 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1678 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1679 | |
| 1680 | if (Member->isBaseInitializer()) |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1681 | AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1682 | else |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1683 | AllBaseFields[Member->getMember()] = Member; |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1686 | // Keep track of the direct virtual bases. |
| 1687 | llvm::SmallPtrSet<CXXBaseSpecifier *, 16> DirectVBases; |
| 1688 | for (CXXRecordDecl::base_class_iterator I = ClassDecl->bases_begin(), |
| 1689 | E = ClassDecl->bases_end(); I != E; ++I) { |
| 1690 | if (I->isVirtual()) |
| 1691 | DirectVBases.insert(I); |
| 1692 | } |
| 1693 | |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1694 | // Push virtual bases before others. |
| 1695 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 1696 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 1697 | |
| 1698 | if (CXXBaseOrMemberInitializer *Value |
| 1699 | = AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
| 1700 | AllToInit.push_back(Value); |
| 1701 | } else if (!AnyErrors) { |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 1702 | bool IsInheritedVirtualBase = !DirectVBases.count(VBase); |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1703 | CXXBaseOrMemberInitializer *CXXBaseInit; |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1704 | if (BuildImplicitBaseInitializer(*this, Constructor, ImplicitInitKind, |
| 1705 | VBase, IsInheritedVirtualBase, |
| 1706 | CXXBaseInit)) { |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1707 | HadError = true; |
| 1708 | continue; |
| 1709 | } |
Anders Carlsson | cedc0a4 | 2010-04-20 23:11:20 +0000 | [diff] [blame] | 1710 | |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1711 | AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1712 | } |
| 1713 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1715 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1716 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1717 | // Virtuals are in the virtual base list and already constructed. |
| 1718 | if (Base->isVirtual()) |
| 1719 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1721 | if (CXXBaseOrMemberInitializer *Value |
| 1722 | = AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
| 1723 | AllToInit.push_back(Value); |
| 1724 | } else if (!AnyErrors) { |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1725 | CXXBaseOrMemberInitializer *CXXBaseInit; |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1726 | if (BuildImplicitBaseInitializer(*this, Constructor, ImplicitInitKind, |
| 1727 | Base, /*IsInheritedVirtualBase=*/false, |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1728 | CXXBaseInit)) { |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1729 | HadError = true; |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1730 | continue; |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1731 | } |
Fariborz Jahanian | 59a1cd4 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1732 | |
Anders Carlsson | db0a965 | 2010-04-02 06:26:44 +0000 | [diff] [blame] | 1733 | AllToInit.push_back(CXXBaseInit); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1734 | } |
| 1735 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1736 | |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1737 | // non-static data members. |
| 1738 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1739 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 1740 | if ((*Field)->isAnonymousStructOrUnion()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1741 | if (const RecordType *FieldClassType = |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1742 | Field->getType()->getAs<RecordType>()) { |
| 1743 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 07eae02 | 2009-11-13 18:34:26 +0000 | [diff] [blame] | 1744 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1745 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1746 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1747 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*FA)) { |
| 1748 | // 'Member' is the anonymous union field and 'AnonUnionMember' is |
| 1749 | // set to the anonymous union data member used in the initializer |
| 1750 | // list. |
| 1751 | Value->setMember(*Field); |
| 1752 | Value->setAnonUnionMember(*FA); |
| 1753 | AllToInit.push_back(Value); |
| 1754 | break; |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | continue; |
| 1759 | } |
| 1760 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*Field)) { |
| 1761 | AllToInit.push_back(Value); |
| 1762 | continue; |
| 1763 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | |
Anders Carlsson | 6bd91c3 | 2010-04-23 02:00:02 +0000 | [diff] [blame] | 1765 | if (AnyErrors) |
Douglas Gregor | 2de8f41 | 2009-11-04 17:16:11 +0000 | [diff] [blame] | 1766 | continue; |
Douglas Gregor | 2de8f41 | 2009-11-04 17:16:11 +0000 | [diff] [blame] | 1767 | |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1768 | CXXBaseOrMemberInitializer *Member; |
Anders Carlsson | 1b00e24 | 2010-04-23 03:10:23 +0000 | [diff] [blame] | 1769 | if (BuildImplicitMemberInitializer(*this, Constructor, ImplicitInitKind, |
| 1770 | *Field, Member)) { |
Anders Carlsson | 3c1db57 | 2010-04-23 02:15:47 +0000 | [diff] [blame] | 1771 | HadError = true; |
| 1772 | continue; |
| 1773 | } |
| 1774 | |
| 1775 | // If the member doesn't need to be initialized, it will be null. |
| 1776 | if (Member) |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1777 | AllToInit.push_back(Member); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1778 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1779 | |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1780 | NumInitializers = AllToInit.size(); |
| 1781 | if (NumInitializers > 0) { |
| 1782 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1783 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1784 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1785 | memcpy(baseOrMemberInitializers, AllToInit.data(), |
| 1786 | NumInitializers * sizeof(CXXBaseOrMemberInitializer*)); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1787 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
Rafael Espindola | 13327bb | 2010-03-13 18:12:56 +0000 | [diff] [blame] | 1788 | |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 1789 | // Constructors implicitly reference the base and member |
| 1790 | // destructors. |
| 1791 | MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(), |
| 1792 | Constructor->getParent()); |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1793 | } |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1794 | |
| 1795 | return HadError; |
Fariborz Jahanian | 3501bce | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1798 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 1799 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1800 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1801 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 1802 | return static_cast<void *>(RT->getDecl()); |
| 1803 | } |
| 1804 | return static_cast<void *>(Field); |
| 1805 | } |
| 1806 | |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1807 | static void *GetKeyForBase(ASTContext &Context, QualType BaseType) { |
| 1808 | return Context.getCanonicalType(BaseType).getTypePtr(); |
Anders Carlsson | bcec05c | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1811 | static void *GetKeyForMember(ASTContext &Context, |
| 1812 | CXXBaseOrMemberInitializer *Member, |
Anders Carlsson | bcec05c | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1813 | bool MemberMaybeAnon = false) { |
Anders Carlsson | a942dcd | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1814 | if (!Member->isMemberInitializer()) |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 1815 | return GetKeyForBase(Context, QualType(Member->getBaseClass(), 0)); |
Anders Carlsson | a942dcd | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1816 | |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1817 | // For fields injected into the class via declaration of an anonymous union, |
| 1818 | // use its anonymous union class declaration as the unique key. |
Anders Carlsson | a942dcd | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1819 | FieldDecl *Field = Member->getMember(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | |
Anders Carlsson | a942dcd | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1821 | // After SetBaseOrMemberInitializers call, Field is the anonymous union |
| 1822 | // data member of the class. Data member used in the initializer list is |
| 1823 | // in AnonUnionMember field. |
| 1824 | if (MemberMaybeAnon && Field->isAnonymousStructOrUnion()) |
| 1825 | Field = Member->getAnonUnionMember(); |
Anders Carlsson | 83ac312 | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1826 | |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1827 | // If the field is a member of an anonymous struct or union, our key |
| 1828 | // 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] | 1829 | RecordDecl *RD = Field->getParent(); |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1830 | if (RD->isAnonymousStructOrUnion()) { |
| 1831 | while (true) { |
| 1832 | RecordDecl *Parent = cast<RecordDecl>(RD->getDeclContext()); |
| 1833 | if (Parent->isAnonymousStructOrUnion()) |
| 1834 | RD = Parent; |
| 1835 | else |
| 1836 | break; |
| 1837 | } |
| 1838 | |
Anders Carlsson | 83ac312 | 2010-03-30 16:19:37 +0000 | [diff] [blame] | 1839 | return static_cast<void *>(RD); |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1840 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
Anders Carlsson | a942dcd | 2010-03-30 15:39:27 +0000 | [diff] [blame] | 1842 | return static_cast<void *>(Field); |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1845 | static void |
| 1846 | DiagnoseBaseOrMemInitializerOrder(Sema &SemaRef, |
Anders Carlsson | 96b8fc6 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1847 | const CXXConstructorDecl *Constructor, |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1848 | CXXBaseOrMemberInitializer **Inits, |
| 1849 | unsigned NumInits) { |
| 1850 | if (Constructor->getDeclContext()->isDependentContext()) |
Anders Carlsson | 35d6e3e | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1851 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1853 | if (SemaRef.Diags.getDiagnosticLevel(diag::warn_initializer_out_of_order) |
| 1854 | == Diagnostic::Ignored) |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1855 | return; |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1856 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1857 | // Build the list of bases and members in the order that they'll |
| 1858 | // actually be initialized. The explicit initializers should be in |
| 1859 | // this same order but may be missing things. |
| 1860 | llvm::SmallVector<const void*, 32> IdealInitKeys; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | |
Anders Carlsson | 96b8fc6 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1862 | const CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 1863 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1864 | // 1. Virtual bases. |
Anders Carlsson | 96b8fc6 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1865 | for (CXXRecordDecl::base_class_const_iterator VBase = |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1866 | ClassDecl->vbases_begin(), |
| 1867 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1868 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, VBase->getType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1870 | // 2. Non-virtual bases. |
Anders Carlsson | 96b8fc6 | 2010-04-02 03:38:04 +0000 | [diff] [blame] | 1871 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(), |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1872 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1873 | if (Base->isVirtual()) |
| 1874 | continue; |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1875 | IdealInitKeys.push_back(GetKeyForBase(SemaRef.Context, Base->getType())); |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1876 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1878 | // 3. Direct fields. |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1879 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1880 | E = ClassDecl->field_end(); Field != E; ++Field) |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1881 | IdealInitKeys.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1882 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1883 | unsigned NumIdealInits = IdealInitKeys.size(); |
| 1884 | unsigned IdealIndex = 0; |
Eli Friedman | 952c15d | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1885 | |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1886 | CXXBaseOrMemberInitializer *PrevInit = 0; |
| 1887 | for (unsigned InitIndex = 0; InitIndex != NumInits; ++InitIndex) { |
| 1888 | CXXBaseOrMemberInitializer *Init = Inits[InitIndex]; |
| 1889 | void *InitKey = GetKeyForMember(SemaRef.Context, Init, true); |
| 1890 | |
| 1891 | // Scan forward to try to find this initializer in the idealized |
| 1892 | // initializers list. |
| 1893 | for (; IdealIndex != NumIdealInits; ++IdealIndex) |
| 1894 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1895 | break; |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1896 | |
| 1897 | // If we didn't find this initializer, it must be because we |
| 1898 | // scanned past it on a previous iteration. That can only |
| 1899 | // happen if we're out of order; emit a warning. |
| 1900 | if (IdealIndex == NumIdealInits) { |
| 1901 | assert(PrevInit && "initializer not found in initializer list"); |
| 1902 | |
| 1903 | Sema::SemaDiagnosticBuilder D = |
| 1904 | SemaRef.Diag(PrevInit->getSourceLocation(), |
| 1905 | diag::warn_initializer_out_of_order); |
| 1906 | |
| 1907 | if (PrevInit->isMemberInitializer()) |
| 1908 | D << 0 << PrevInit->getMember()->getDeclName(); |
| 1909 | else |
| 1910 | D << 1 << PrevInit->getBaseClassInfo()->getType(); |
| 1911 | |
| 1912 | if (Init->isMemberInitializer()) |
| 1913 | D << 0 << Init->getMember()->getDeclName(); |
| 1914 | else |
| 1915 | D << 1 << Init->getBaseClassInfo()->getType(); |
| 1916 | |
| 1917 | // Move back to the initializer's location in the ideal list. |
| 1918 | for (IdealIndex = 0; IdealIndex != NumIdealInits; ++IdealIndex) |
| 1919 | if (InitKey == IdealInitKeys[IdealIndex]) |
Anders Carlsson | e0eebb3 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1920 | break; |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1921 | |
| 1922 | assert(IdealIndex != NumIdealInits && |
| 1923 | "initializer not found in initializer list"); |
Fariborz Jahanian | 341583c | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1924 | } |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 1925 | |
| 1926 | PrevInit = Init; |
Fariborz Jahanian | 341583c | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1927 | } |
Anders Carlsson | 75fdaa4 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 1930 | namespace { |
| 1931 | bool CheckRedundantInit(Sema &S, |
| 1932 | CXXBaseOrMemberInitializer *Init, |
| 1933 | CXXBaseOrMemberInitializer *&PrevInit) { |
| 1934 | if (!PrevInit) { |
| 1935 | PrevInit = Init; |
| 1936 | return false; |
| 1937 | } |
| 1938 | |
| 1939 | if (FieldDecl *Field = Init->getMember()) |
| 1940 | S.Diag(Init->getSourceLocation(), |
| 1941 | diag::err_multiple_mem_initialization) |
| 1942 | << Field->getDeclName() |
| 1943 | << Init->getSourceRange(); |
| 1944 | else { |
| 1945 | Type *BaseClass = Init->getBaseClass(); |
| 1946 | assert(BaseClass && "neither field nor base"); |
| 1947 | S.Diag(Init->getSourceLocation(), |
| 1948 | diag::err_multiple_base_initialization) |
| 1949 | << QualType(BaseClass, 0) |
| 1950 | << Init->getSourceRange(); |
| 1951 | } |
| 1952 | S.Diag(PrevInit->getSourceLocation(), diag::note_previous_initializer) |
| 1953 | << 0 << PrevInit->getSourceRange(); |
| 1954 | |
| 1955 | return true; |
| 1956 | } |
| 1957 | |
| 1958 | typedef std::pair<NamedDecl *, CXXBaseOrMemberInitializer *> UnionEntry; |
| 1959 | typedef llvm::DenseMap<RecordDecl*, UnionEntry> RedundantUnionMap; |
| 1960 | |
| 1961 | bool CheckRedundantUnionInit(Sema &S, |
| 1962 | CXXBaseOrMemberInitializer *Init, |
| 1963 | RedundantUnionMap &Unions) { |
| 1964 | FieldDecl *Field = Init->getMember(); |
| 1965 | RecordDecl *Parent = Field->getParent(); |
| 1966 | if (!Parent->isAnonymousStructOrUnion()) |
| 1967 | return false; |
| 1968 | |
| 1969 | NamedDecl *Child = Field; |
| 1970 | do { |
| 1971 | if (Parent->isUnion()) { |
| 1972 | UnionEntry &En = Unions[Parent]; |
| 1973 | if (En.first && En.first != Child) { |
| 1974 | S.Diag(Init->getSourceLocation(), |
| 1975 | diag::err_multiple_mem_union_initialization) |
| 1976 | << Field->getDeclName() |
| 1977 | << Init->getSourceRange(); |
| 1978 | S.Diag(En.second->getSourceLocation(), diag::note_previous_initializer) |
| 1979 | << 0 << En.second->getSourceRange(); |
| 1980 | return true; |
| 1981 | } else if (!En.first) { |
| 1982 | En.first = Child; |
| 1983 | En.second = Init; |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | Child = Parent; |
| 1988 | Parent = cast<RecordDecl>(Parent->getDeclContext()); |
| 1989 | } while (Parent->isAnonymousStructOrUnion()); |
| 1990 | |
| 1991 | return false; |
| 1992 | } |
| 1993 | } |
| 1994 | |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 1995 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
| 1996 | void Sema::ActOnMemInitializers(DeclPtrTy ConstructorDecl, |
| 1997 | SourceLocation ColonLoc, |
| 1998 | MemInitTy **meminits, unsigned NumMemInits, |
| 1999 | bool AnyErrors) { |
| 2000 | if (!ConstructorDecl) |
| 2001 | return; |
| 2002 | |
| 2003 | AdjustDeclIfTemplate(ConstructorDecl); |
| 2004 | |
| 2005 | CXXConstructorDecl *Constructor |
| 2006 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl.getAs<Decl>()); |
| 2007 | |
| 2008 | if (!Constructor) { |
| 2009 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 2010 | return; |
| 2011 | } |
| 2012 | |
| 2013 | CXXBaseOrMemberInitializer **MemInits = |
| 2014 | reinterpret_cast<CXXBaseOrMemberInitializer **>(meminits); |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2015 | |
| 2016 | // Mapping for the duplicate initializers check. |
| 2017 | // For member initializers, this is keyed with a FieldDecl*. |
| 2018 | // For base initializers, this is keyed with a Type*. |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2019 | llvm::DenseMap<void*, CXXBaseOrMemberInitializer *> Members; |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2020 | |
| 2021 | // Mapping for the inconsistent anonymous-union initializers check. |
| 2022 | RedundantUnionMap MemberUnions; |
| 2023 | |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2024 | bool HadError = false; |
| 2025 | for (unsigned i = 0; i < NumMemInits; i++) { |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2026 | CXXBaseOrMemberInitializer *Init = MemInits[i]; |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2027 | |
John McCall | 23eebd9 | 2010-04-10 09:28:51 +0000 | [diff] [blame] | 2028 | if (Init->isMemberInitializer()) { |
| 2029 | FieldDecl *Field = Init->getMember(); |
| 2030 | if (CheckRedundantInit(*this, Init, Members[Field]) || |
| 2031 | CheckRedundantUnionInit(*this, Init, MemberUnions)) |
| 2032 | HadError = true; |
| 2033 | } else { |
| 2034 | void *Key = GetKeyForBase(Context, QualType(Init->getBaseClass(), 0)); |
| 2035 | if (CheckRedundantInit(*this, Init, Members[Key])) |
| 2036 | HadError = true; |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2037 | } |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Anders Carlsson | 7b3f278 | 2010-04-02 05:42:15 +0000 | [diff] [blame] | 2040 | if (HadError) |
| 2041 | return; |
| 2042 | |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2043 | DiagnoseBaseOrMemInitializerOrder(*this, Constructor, MemInits, NumMemInits); |
Anders Carlsson | 4c8cb01 | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2044 | |
| 2045 | SetBaseOrMemberInitializers(Constructor, MemInits, NumMemInits, AnyErrors); |
Anders Carlsson | e857b29 | 2010-04-02 03:37:03 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2048 | void |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2049 | Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location, |
| 2050 | CXXRecordDecl *ClassDecl) { |
| 2051 | // Ignore dependent contexts. |
| 2052 | if (ClassDecl->isDependentContext()) |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2053 | return; |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2054 | |
| 2055 | // FIXME: all the access-control diagnostics are positioned on the |
| 2056 | // field/base declaration. That's probably good; that said, the |
| 2057 | // user might reasonably want to know why the destructor is being |
| 2058 | // emitted, and we currently don't say. |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2059 | |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2060 | // Non-static data members. |
| 2061 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 2062 | E = ClassDecl->field_end(); I != E; ++I) { |
| 2063 | FieldDecl *Field = *I; |
| 2064 | |
| 2065 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 2066 | |
| 2067 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 2068 | if (!RT) |
| 2069 | continue; |
| 2070 | |
| 2071 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 2072 | if (FieldClassDecl->hasTrivialDestructor()) |
| 2073 | continue; |
| 2074 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2075 | CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor(Context); |
| 2076 | CheckDestructorAccess(Field->getLocation(), Dtor, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2077 | PDiag(diag::err_access_dtor_field) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2078 | << Field->getDeclName() |
| 2079 | << FieldType); |
| 2080 | |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2081 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2084 | llvm::SmallPtrSet<const RecordType *, 8> DirectVirtualBases; |
| 2085 | |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2086 | // Bases. |
| 2087 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 2088 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2089 | // Bases are always records in a well-formed non-dependent class. |
| 2090 | const RecordType *RT = Base->getType()->getAs<RecordType>(); |
| 2091 | |
| 2092 | // Remember direct virtual bases. |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2093 | if (Base->isVirtual()) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2094 | DirectVirtualBases.insert(RT); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2095 | |
| 2096 | // Ignore trivial destructors. |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2097 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2098 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2099 | continue; |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2100 | |
| 2101 | CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context); |
| 2102 | |
| 2103 | // FIXME: caret should be on the start of the class name |
| 2104 | CheckDestructorAccess(Base->getSourceRange().getBegin(), Dtor, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2105 | PDiag(diag::err_access_dtor_base) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2106 | << Base->getType() |
| 2107 | << Base->getSourceRange()); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2108 | |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2109 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | // Virtual bases. |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2113 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 2114 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2115 | |
| 2116 | // Bases are always records in a well-formed non-dependent class. |
| 2117 | const RecordType *RT = VBase->getType()->getAs<RecordType>(); |
| 2118 | |
| 2119 | // Ignore direct virtual bases. |
| 2120 | if (DirectVirtualBases.count(RT)) |
| 2121 | continue; |
| 2122 | |
Anders Carlsson | dee9a30 | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 2123 | // Ignore trivial destructors. |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2124 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2125 | if (BaseClassDecl->hasTrivialDestructor()) |
| 2126 | continue; |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2127 | |
| 2128 | CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context); |
| 2129 | CheckDestructorAccess(ClassDecl->getLocation(), Dtor, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2130 | PDiag(diag::err_access_dtor_vbase) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2131 | << VBase->getType()); |
| 2132 | |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 2133 | MarkDeclarationReferenced(Location, const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 37d0656 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 2134 | } |
| 2135 | } |
| 2136 | |
Fariborz Jahanian | aee31ac | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 2137 | void Sema::ActOnDefaultCtorInitializers(DeclPtrTy CDtorDecl) { |
Fariborz Jahanian | 16094c2 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2138 | if (!CDtorDecl) |
Fariborz Jahanian | 49c8179 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2139 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2140 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | if (CXXConstructorDecl *Constructor |
Fariborz Jahanian | 16094c2 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 2142 | = dyn_cast<CXXConstructorDecl>(CDtorDecl.getAs<Decl>())) |
Anders Carlsson | 4c8cb01 | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 2143 | SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false); |
Fariborz Jahanian | 49c8179 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2147 | unsigned DiagID, AbstractDiagSelID SelID, |
| 2148 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | eabf770 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2149 | if (SelID == -1) |
| 2150 | return RequireNonAbstractType(Loc, T, |
| 2151 | PDiag(DiagID), CurrentRD); |
| 2152 | else |
| 2153 | return RequireNonAbstractType(Loc, T, |
| 2154 | PDiag(DiagID) << SelID, CurrentRD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
Anders Carlsson | eabf770 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2157 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
| 2158 | const PartialDiagnostic &PD, |
| 2159 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2160 | if (!getLangOptions().CPlusPlus) |
| 2161 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | |
Anders Carlsson | eb0c532 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 2163 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Anders Carlsson | eabf770 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2164 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2165 | CurrentRD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2166 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2167 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2168 | // Find the innermost pointer type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2169 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2170 | PT = T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | |
Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2172 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Anders Carlsson | eabf770 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2173 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, CurrentRD); |
Anders Carlsson | 8f0d218 | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 2174 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2175 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2176 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2177 | if (!RT) |
| 2178 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2180 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2181 | |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2182 | if (CurrentRD && CurrentRD != RD) |
| 2183 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2185 | // FIXME: is this reasonable? It matches current behavior, but.... |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 2186 | if (!RD->getDefinition()) |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2187 | return false; |
| 2188 | |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2189 | if (!RD->isAbstract()) |
| 2190 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Anders Carlsson | eabf770 | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 2192 | Diag(Loc, PD) << RD->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2193 | |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2194 | // Check if we've already emitted the list of pure virtual functions for this |
| 2195 | // class. |
| 2196 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
| 2197 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2198 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2199 | CXXFinalOverriderMap FinalOverriders; |
| 2200 | RD->getFinalOverriders(FinalOverriders); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2201 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2202 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2203 | MEnd = FinalOverriders.end(); |
| 2204 | M != MEnd; |
| 2205 | ++M) { |
| 2206 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2207 | SOEnd = M->second.end(); |
| 2208 | SO != SOEnd; ++SO) { |
| 2209 | // C++ [class.abstract]p4: |
| 2210 | // A class is abstract if it contains or inherits at least one |
| 2211 | // pure virtual function for which the final overrider is pure |
| 2212 | // virtual. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2213 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2214 | // |
| 2215 | if (SO->second.size() != 1) |
| 2216 | continue; |
| 2217 | |
| 2218 | if (!SO->second.front().Method->isPure()) |
| 2219 | continue; |
| 2220 | |
| 2221 | Diag(SO->second.front().Method->getLocation(), |
| 2222 | diag::note_pure_virtual_function) |
| 2223 | << SO->second.front().Method->getDeclName(); |
| 2224 | } |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | if (!PureVirtualClassDiagSet) |
| 2228 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 2229 | PureVirtualClassDiagSet->insert(RD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2230 | |
Anders Carlsson | 576cc6f | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 2231 | return true; |
| 2232 | } |
| 2233 | |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2234 | namespace { |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 2235 | class AbstractClassUsageDiagnoser |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2236 | : public DeclVisitor<AbstractClassUsageDiagnoser, bool> { |
| 2237 | Sema &SemaRef; |
| 2238 | CXXRecordDecl *AbstractClass; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2239 | |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2240 | bool VisitDeclContext(const DeclContext *DC) { |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2241 | bool Invalid = false; |
| 2242 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2243 | for (CXXRecordDecl::decl_iterator I = DC->decls_begin(), |
| 2244 | E = DC->decls_end(); I != E; ++I) |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2245 | Invalid |= Visit(*I); |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2246 | |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2247 | return Invalid; |
| 2248 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2250 | public: |
| 2251 | AbstractClassUsageDiagnoser(Sema& SemaRef, CXXRecordDecl *ac) |
| 2252 | : SemaRef(SemaRef), AbstractClass(ac) { |
| 2253 | Visit(SemaRef.Context.getTranslationUnitDecl()); |
| 2254 | } |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2255 | |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2256 | bool VisitFunctionDecl(const FunctionDecl *FD) { |
| 2257 | if (FD->isThisDeclarationADefinition()) { |
| 2258 | // No need to do the check if we're in a definition, because it requires |
| 2259 | // that the return/param types are complete. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2260 | // because that requires |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2261 | return VisitDeclContext(FD); |
| 2262 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2263 | |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2264 | // Check the return type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2265 | QualType RTy = FD->getType()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2266 | bool Invalid = |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2267 | SemaRef.RequireNonAbstractType(FD->getLocation(), RTy, |
| 2268 | diag::err_abstract_type_in_decl, |
| 2269 | Sema::AbstractReturnType, |
| 2270 | AbstractClass); |
| 2271 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2273 | E = FD->param_end(); I != E; ++I) { |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2274 | const ParmVarDecl *VD = *I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2275 | Invalid |= |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2276 | SemaRef.RequireNonAbstractType(VD->getLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2277 | VD->getOriginalType(), |
| 2278 | diag::err_abstract_type_in_decl, |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2279 | Sema::AbstractParamType, |
| 2280 | AbstractClass); |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | return Invalid; |
| 2284 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2285 | |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2286 | bool VisitDecl(const Decl* D) { |
| 2287 | if (const DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 2288 | return VisitDeclContext(DC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2289 | |
Anders Carlsson | b57738b | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 2290 | return false; |
| 2291 | } |
Anders Carlsson | b5a27b4 | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 2292 | }; |
| 2293 | } |
| 2294 | |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2295 | /// \brief Perform semantic checks on a class definition that has been |
| 2296 | /// completing, introducing implicitly-declared members, checking for |
| 2297 | /// abstract types, etc. |
Douglas Gregor | b93b606 | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2298 | void Sema::CheckCompletedCXXClass(Scope *S, CXXRecordDecl *Record) { |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2299 | if (!Record || Record->isInvalidDecl()) |
| 2300 | return; |
| 2301 | |
Eli Friedman | 5dd02a0f | 2009-12-16 20:00:27 +0000 | [diff] [blame] | 2302 | if (!Record->isDependentType()) |
Douglas Gregor | b93b606 | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2303 | AddImplicitlyDeclaredMembersToClass(S, Record); |
Douglas Gregor | 0a0f04d | 2010-01-06 04:44:19 +0000 | [diff] [blame] | 2304 | |
Eli Friedman | 5dd02a0f | 2009-12-16 20:00:27 +0000 | [diff] [blame] | 2305 | if (Record->isInvalidDecl()) |
| 2306 | return; |
| 2307 | |
John McCall | 2cb9416 | 2010-01-28 07:38:46 +0000 | [diff] [blame] | 2308 | // Set access bits correctly on the directly-declared conversions. |
| 2309 | UnresolvedSetImpl *Convs = Record->getConversionFunctions(); |
| 2310 | for (UnresolvedSetIterator I = Convs->begin(), E = Convs->end(); I != E; ++I) |
| 2311 | Convs->setAccess(I, (*I)->getAccess()); |
| 2312 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2313 | // Determine whether we need to check for final overriders. We do |
| 2314 | // this either when there are virtual base classes (in which case we |
| 2315 | // may end up finding multiple final overriders for a given virtual |
| 2316 | // function) or any of the base classes is abstract (in which case |
| 2317 | // we might detect that this class is abstract). |
| 2318 | bool CheckFinalOverriders = false; |
| 2319 | if (Record->isPolymorphic() && !Record->isInvalidDecl() && |
| 2320 | !Record->isDependentType()) { |
| 2321 | if (Record->getNumVBases()) |
| 2322 | CheckFinalOverriders = true; |
| 2323 | else if (!Record->isAbstract()) { |
| 2324 | for (CXXRecordDecl::base_class_const_iterator B = Record->bases_begin(), |
| 2325 | BEnd = Record->bases_end(); |
| 2326 | B != BEnd; ++B) { |
| 2327 | CXXRecordDecl *BaseDecl |
| 2328 | = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl()); |
| 2329 | if (BaseDecl->isAbstract()) { |
| 2330 | CheckFinalOverriders = true; |
| 2331 | break; |
| 2332 | } |
| 2333 | } |
| 2334 | } |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Douglas Gregor | 4165bd6 | 2010-03-23 23:47:56 +0000 | [diff] [blame] | 2337 | if (CheckFinalOverriders) { |
| 2338 | CXXFinalOverriderMap FinalOverriders; |
| 2339 | Record->getFinalOverriders(FinalOverriders); |
| 2340 | |
| 2341 | for (CXXFinalOverriderMap::iterator M = FinalOverriders.begin(), |
| 2342 | MEnd = FinalOverriders.end(); |
| 2343 | M != MEnd; ++M) { |
| 2344 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 2345 | SOEnd = M->second.end(); |
| 2346 | SO != SOEnd; ++SO) { |
| 2347 | assert(SO->second.size() > 0 && |
| 2348 | "All virtual functions have overridding virtual functions"); |
| 2349 | if (SO->second.size() == 1) { |
| 2350 | // C++ [class.abstract]p4: |
| 2351 | // A class is abstract if it contains or inherits at least one |
| 2352 | // pure virtual function for which the final overrider is pure |
| 2353 | // virtual. |
| 2354 | if (SO->second.front().Method->isPure()) |
| 2355 | Record->setAbstract(true); |
| 2356 | continue; |
| 2357 | } |
| 2358 | |
| 2359 | // C++ [class.virtual]p2: |
| 2360 | // In a derived class, if a virtual member function of a base |
| 2361 | // class subobject has more than one final overrider the |
| 2362 | // program is ill-formed. |
| 2363 | Diag(Record->getLocation(), diag::err_multiple_final_overriders) |
| 2364 | << (NamedDecl *)M->first << Record; |
| 2365 | Diag(M->first->getLocation(), diag::note_overridden_virtual_function); |
| 2366 | for (OverridingMethods::overriding_iterator OM = SO->second.begin(), |
| 2367 | OMEnd = SO->second.end(); |
| 2368 | OM != OMEnd; ++OM) |
| 2369 | Diag(OM->Method->getLocation(), diag::note_final_overrider) |
| 2370 | << (NamedDecl *)M->first << OM->Method->getParent(); |
| 2371 | |
| 2372 | Record->setInvalidDecl(); |
| 2373 | } |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | if (Record->isAbstract() && !Record->isInvalidDecl()) |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2378 | (void)AbstractClassUsageDiagnoser(*this, Record); |
Douglas Gregor | 454a5b6 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2379 | |
| 2380 | // If this is not an aggregate type and has no user-declared constructor, |
| 2381 | // complain about any non-static data members of reference or const scalar |
| 2382 | // type, since they will never get initializers. |
| 2383 | if (!Record->isInvalidDecl() && !Record->isDependentType() && |
| 2384 | !Record->isAggregate() && !Record->hasUserDeclaredConstructor()) { |
| 2385 | bool Complained = false; |
| 2386 | for (RecordDecl::field_iterator F = Record->field_begin(), |
| 2387 | FEnd = Record->field_end(); |
| 2388 | F != FEnd; ++F) { |
| 2389 | if (F->getType()->isReferenceType() || |
Benjamin Kramer | 659d7fc | 2010-04-16 17:43:15 +0000 | [diff] [blame] | 2390 | (F->getType().isConstQualified() && F->getType()->isScalarType())) { |
Douglas Gregor | 454a5b6 | 2010-04-15 00:00:53 +0000 | [diff] [blame] | 2391 | if (!Complained) { |
| 2392 | Diag(Record->getLocation(), diag::warn_no_constructor_for_refconst) |
| 2393 | << Record->getTagKind() << Record; |
| 2394 | Complained = true; |
| 2395 | } |
| 2396 | |
| 2397 | Diag(F->getLocation(), diag::note_refconst_member_not_initialized) |
| 2398 | << F->getType()->isReferenceType() |
| 2399 | << F->getDeclName(); |
| 2400 | } |
| 2401 | } |
| 2402 | } |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2405 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2406 | DeclPtrTy TagDecl, |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2407 | SourceLocation LBrac, |
Douglas Gregor | c48a10d | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2408 | SourceLocation RBrac, |
| 2409 | AttributeList *AttrList) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2410 | if (!TagDecl) |
| 2411 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2412 | |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2413 | AdjustDeclIfTemplate(TagDecl); |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2414 | |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2415 | ActOnFields(S, RLoc, TagDecl, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2416 | (DeclPtrTy*)FieldCollector->getCurFields(), |
Douglas Gregor | c48a10d | 2010-03-29 14:42:08 +0000 | [diff] [blame] | 2417 | FieldCollector->getCurNumFields(), LBrac, RBrac, AttrList); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2418 | |
Douglas Gregor | b93b606 | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2419 | CheckCompletedCXXClass(S, |
Douglas Gregor | c99f155 | 2009-12-03 18:33:45 +0000 | [diff] [blame] | 2420 | dyn_cast_or_null<CXXRecordDecl>(TagDecl.getAs<Decl>())); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2423 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 2424 | /// special functions, such as the default constructor, copy |
| 2425 | /// constructor, or destructor, to the given C++ class (C++ |
| 2426 | /// [special]p1). This routine can only be executed just before the |
| 2427 | /// definition of the class is complete. |
Douglas Gregor | b93b606 | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2428 | /// |
| 2429 | /// The scope, if provided, is the class scope. |
| 2430 | void Sema::AddImplicitlyDeclaredMembersToClass(Scope *S, |
| 2431 | CXXRecordDecl *ClassDecl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2432 | CanQualType ClassType |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2433 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2434 | |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2435 | // FIXME: Implicit declarations have exception specifications, which are |
| 2436 | // the union of the specifications of the implicitly called functions. |
| 2437 | |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2438 | if (!ClassDecl->hasUserDeclaredConstructor()) { |
| 2439 | // C++ [class.ctor]p5: |
| 2440 | // A default constructor for a class X is a constructor of class X |
| 2441 | // that can be called without an argument. If there is no |
| 2442 | // user-declared constructor for class X, a default constructor is |
| 2443 | // implicitly declared. An implicitly-declared default constructor |
| 2444 | // is an inline public member of its class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2445 | DeclarationName Name |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2446 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | CXXConstructorDecl *DefaultCon = |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2448 | CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2449 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2450 | Context.getFunctionType(Context.VoidTy, |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2451 | 0, 0, false, 0, |
| 2452 | /*FIXME*/false, false, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2453 | 0, 0, |
| 2454 | FunctionType::ExtInfo()), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2455 | /*TInfo=*/0, |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2456 | /*isExplicit=*/false, |
| 2457 | /*isInline=*/true, |
| 2458 | /*isImplicitlyDeclared=*/true); |
| 2459 | DefaultCon->setAccess(AS_public); |
Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2460 | DefaultCon->setImplicit(); |
Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2461 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Douglas Gregor | b93b606 | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2462 | if (S) |
| 2463 | PushOnScopeChains(DefaultCon, S, true); |
| 2464 | else |
| 2465 | ClassDecl->addDecl(DefaultCon); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
| 2468 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
| 2469 | // C++ [class.copy]p4: |
| 2470 | // If the class definition does not explicitly declare a copy |
| 2471 | // constructor, one is declared implicitly. |
| 2472 | |
| 2473 | // C++ [class.copy]p5: |
| 2474 | // The implicitly-declared copy constructor for a class X will |
| 2475 | // have the form |
| 2476 | // |
| 2477 | // X::X(const X&) |
| 2478 | // |
| 2479 | // if |
| 2480 | bool HasConstCopyConstructor = true; |
| 2481 | |
| 2482 | // -- each direct or virtual base class B of X has a copy |
| 2483 | // constructor whose first parameter is of type const B& or |
| 2484 | // const volatile B&, and |
| 2485 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2486 | HasConstCopyConstructor && Base != ClassDecl->bases_end(); ++Base) { |
| 2487 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2488 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2489 | HasConstCopyConstructor |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2490 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 2491 | } |
| 2492 | |
| 2493 | // -- for all the nonstatic data members of X that are of a |
| 2494 | // class type M (or array thereof), each such class type |
| 2495 | // has a copy constructor whose first parameter is of type |
| 2496 | // const M& or const volatile M&. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2497 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 2498 | HasConstCopyConstructor && Field != ClassDecl->field_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2499 | ++Field) { |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2500 | QualType FieldType = (*Field)->getType(); |
| 2501 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2502 | FieldType = Array->getElementType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2503 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2504 | const CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2505 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2506 | HasConstCopyConstructor |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2507 | = FieldClassDecl->hasConstCopyConstructor(Context); |
| 2508 | } |
| 2509 | } |
| 2510 | |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2511 | // Otherwise, the implicitly declared copy constructor will have |
| 2512 | // the form |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2513 | // |
| 2514 | // X::X(X&) |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2515 | QualType ArgType = ClassType; |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2516 | if (HasConstCopyConstructor) |
| 2517 | ArgType = ArgType.withConst(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2518 | ArgType = Context.getLValueReferenceType(ArgType); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2519 | |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2520 | // An implicitly-declared copy constructor is an inline public |
| 2521 | // member of its class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2522 | DeclarationName Name |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2523 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2524 | CXXConstructorDecl *CopyConstructor |
| 2525 | = CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2526 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2527 | Context.getFunctionType(Context.VoidTy, |
| 2528 | &ArgType, 1, |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2529 | false, 0, |
| 2530 | /*FIXME:*/false, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2531 | false, 0, 0, |
| 2532 | FunctionType::ExtInfo()), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2533 | /*TInfo=*/0, |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2534 | /*isExplicit=*/false, |
| 2535 | /*isInline=*/true, |
| 2536 | /*isImplicitlyDeclared=*/true); |
| 2537 | CopyConstructor->setAccess(AS_public); |
Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2538 | CopyConstructor->setImplicit(); |
Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2539 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2540 | |
| 2541 | // Add the parameter to the constructor. |
| 2542 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 2543 | ClassDecl->getLocation(), |
| 2544 | /*IdentifierInfo=*/0, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2545 | ArgType, /*TInfo=*/0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2546 | VarDecl::None, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2547 | VarDecl::None, 0); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2548 | CopyConstructor->setParams(&FromParam, 1); |
Douglas Gregor | b93b606 | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2549 | if (S) |
| 2550 | PushOnScopeChains(CopyConstructor, S, true); |
| 2551 | else |
| 2552 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2555 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 2556 | // Note: The following rules are largely analoguous to the copy |
| 2557 | // constructor rules. Note that virtual bases are not taken into account |
| 2558 | // for determining the argument type of the operator. Note also that |
| 2559 | // operators taking an object instead of a reference are allowed. |
| 2560 | // |
| 2561 | // C++ [class.copy]p10: |
| 2562 | // If the class definition does not explicitly declare a copy |
| 2563 | // assignment operator, one is declared implicitly. |
| 2564 | // The implicitly-defined copy assignment operator for a class X |
| 2565 | // will have the form |
| 2566 | // |
| 2567 | // X& X::operator=(const X&) |
| 2568 | // |
| 2569 | // if |
| 2570 | bool HasConstCopyAssignment = true; |
| 2571 | |
| 2572 | // -- each direct base class B of X has a copy assignment operator |
| 2573 | // whose parameter is of type const B&, const volatile B& or B, |
| 2574 | // and |
| 2575 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2576 | HasConstCopyAssignment && Base != ClassDecl->bases_end(); ++Base) { |
Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 2577 | assert(!Base->getType()->isDependentType() && |
| 2578 | "Cannot generate implicit members for class with dependent bases."); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2579 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2580 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | bbd5e8c | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2581 | const CXXMethodDecl *MD = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2582 | HasConstCopyAssignment = BaseClassDecl->hasConstCopyAssignment(Context, |
Fariborz Jahanian | bbd5e8c | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2583 | MD); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | // -- for all the nonstatic data members of X that are of a class |
| 2587 | // type M (or array thereof), each such class type has a copy |
| 2588 | // assignment operator whose parameter is of type const M&, |
| 2589 | // const volatile M& or M. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2590 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 2591 | HasConstCopyAssignment && Field != ClassDecl->field_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2592 | ++Field) { |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2593 | QualType FieldType = (*Field)->getType(); |
| 2594 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2595 | FieldType = Array->getElementType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2596 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2597 | const CXXRecordDecl *FieldClassDecl |
| 2598 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Fariborz Jahanian | bbd5e8c | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2599 | const CXXMethodDecl *MD = 0; |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2600 | HasConstCopyAssignment |
Fariborz Jahanian | bbd5e8c | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2601 | = FieldClassDecl->hasConstCopyAssignment(Context, MD); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | // Otherwise, the implicitly declared copy assignment operator will |
| 2606 | // have the form |
| 2607 | // |
| 2608 | // X& X::operator=(X&) |
| 2609 | QualType ArgType = ClassType; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2610 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2611 | if (HasConstCopyAssignment) |
| 2612 | ArgType = ArgType.withConst(); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2613 | ArgType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2614 | |
| 2615 | // An implicitly-declared copy assignment operator is an inline public |
| 2616 | // member of its class. |
| 2617 | DeclarationName Name = |
| 2618 | Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 2619 | CXXMethodDecl *CopyAssignment = |
| 2620 | CXXMethodDecl::Create(Context, ClassDecl, ClassDecl->getLocation(), Name, |
| 2621 | Context.getFunctionType(RetType, &ArgType, 1, |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2622 | false, 0, |
| 2623 | /*FIXME:*/false, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2624 | false, 0, 0, |
| 2625 | FunctionType::ExtInfo()), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2626 | /*TInfo=*/0, /*isStatic=*/false, |
| 2627 | /*StorageClassAsWritten=*/FunctionDecl::None, |
| 2628 | /*isInline=*/true); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2629 | CopyAssignment->setAccess(AS_public); |
Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2630 | CopyAssignment->setImplicit(); |
Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2631 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Fariborz Jahanian | de7d4c2 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 2632 | CopyAssignment->setCopyAssignment(true); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2633 | |
| 2634 | // Add the parameter to the operator. |
| 2635 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 2636 | ClassDecl->getLocation(), |
| 2637 | /*IdentifierInfo=*/0, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2638 | ArgType, /*TInfo=*/0, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2639 | VarDecl::None, |
Argyrios Kyrtzidis | 60ed560 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2640 | VarDecl::None, 0); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2641 | CopyAssignment->setParams(&FromParam, 1); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2642 | |
| 2643 | // Don't call addedAssignmentOperator. There is no way to distinguish an |
| 2644 | // implicit from an explicit assignment operator. |
Douglas Gregor | b93b606 | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2645 | if (S) |
| 2646 | PushOnScopeChains(CopyAssignment, S, true); |
| 2647 | else |
| 2648 | ClassDecl->addDecl(CopyAssignment); |
Eli Friedman | 81bce6b | 2009-12-02 06:59:20 +0000 | [diff] [blame] | 2649 | AddOverriddenMethods(ClassDecl, CopyAssignment); |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Douglas Gregor | 1349b45 | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2652 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2653 | // C++ [class.dtor]p2: |
| 2654 | // If a class has no user-declared destructor, a destructor is |
| 2655 | // declared implicitly. An implicitly-declared destructor is an |
| 2656 | // inline public member of its class. |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2657 | QualType Ty = Context.getFunctionType(Context.VoidTy, |
| 2658 | 0, 0, false, 0, |
| 2659 | /*FIXME:*/false, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2660 | false, 0, 0, FunctionType::ExtInfo()); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2661 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2662 | DeclarationName Name |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2663 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2664 | CXXDestructorDecl *Destructor |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2665 | = CXXDestructorDecl::Create(Context, ClassDecl, |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2666 | ClassDecl->getLocation(), Name, Ty, |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2667 | /*isInline=*/true, |
| 2668 | /*isImplicitlyDeclared=*/true); |
| 2669 | Destructor->setAccess(AS_public); |
Douglas Gregor | f4d3327 | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2670 | Destructor->setImplicit(); |
Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2671 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Douglas Gregor | b93b606 | 2010-04-12 17:09:20 +0000 | [diff] [blame] | 2672 | if (S) |
| 2673 | PushOnScopeChains(Destructor, S, true); |
| 2674 | else |
| 2675 | ClassDecl->addDecl(Destructor); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2676 | |
| 2677 | // This could be uniqued if it ever proves significant. |
| 2678 | Destructor->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(Ty)); |
Anders Carlsson | 859d7bf | 2009-11-26 21:25:09 +0000 | [diff] [blame] | 2679 | |
| 2680 | AddOverriddenMethods(ClassDecl, Destructor); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2681 | } |
Douglas Gregor | 0537942 | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2684 | void Sema::ActOnReenterTemplateScope(Scope *S, DeclPtrTy TemplateD) { |
Douglas Gregor | e61ef62 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 2685 | Decl *D = TemplateD.getAs<Decl>(); |
| 2686 | if (!D) |
| 2687 | return; |
| 2688 | |
| 2689 | TemplateParameterList *Params = 0; |
| 2690 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 2691 | Params = Template->getTemplateParameters(); |
| 2692 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2693 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 2694 | Params = PartialSpec->getTemplateParameters(); |
| 2695 | else |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2696 | return; |
| 2697 | |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2698 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2699 | ParamEnd = Params->end(); |
| 2700 | Param != ParamEnd; ++Param) { |
| 2701 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 2702 | if (Named->getDeclName()) { |
| 2703 | S->AddDecl(DeclPtrTy::make(Named)); |
| 2704 | IdResolver.AddDecl(Named); |
| 2705 | } |
| 2706 | } |
| 2707 | } |
| 2708 | |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 2709 | void Sema::ActOnStartDelayedMemberDeclarations(Scope *S, DeclPtrTy RecordD) { |
| 2710 | if (!RecordD) return; |
| 2711 | AdjustDeclIfTemplate(RecordD); |
| 2712 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordD.getAs<Decl>()); |
| 2713 | PushDeclContext(S, Record); |
| 2714 | } |
| 2715 | |
| 2716 | void Sema::ActOnFinishDelayedMemberDeclarations(Scope *S, DeclPtrTy RecordD) { |
| 2717 | if (!RecordD) return; |
| 2718 | PopDeclContext(); |
| 2719 | } |
| 2720 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2721 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 2722 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 2723 | /// parsing those parts of the given Method declaration that could |
| 2724 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 2725 | /// arguments. This action should enter the scope of the given |
| 2726 | /// Method declaration as if we had just parsed the qualified method |
| 2727 | /// name. However, it should not bring the parameters into scope; |
| 2728 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2729 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 2733 | /// C++ method declaration. We're (re-)introducing the given |
| 2734 | /// function parameter into scope for use in parsing later parts of |
| 2735 | /// the method declaration. For example, we could see an |
| 2736 | /// ActOnParamDefaultArgument event for this parameter. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2737 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, DeclPtrTy ParamD) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2738 | if (!ParamD) |
| 2739 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2740 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2741 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD.getAs<Decl>()); |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2742 | |
| 2743 | // If this parameter has an unparsed default argument, clear it out |
| 2744 | // to make way for the parsed default argument. |
| 2745 | if (Param->hasUnparsedDefaultArg()) |
| 2746 | Param->setDefaultArg(0); |
| 2747 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2748 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2749 | if (Param->getDeclName()) |
| 2750 | IdResolver.AddDecl(Param); |
| 2751 | } |
| 2752 | |
| 2753 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 2754 | /// processing the delayed method declaration for Method. The method |
| 2755 | /// declaration is now considered finished. There may be a separate |
| 2756 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 2757 | /// immediately!) for this method, if it was also defined inside the |
| 2758 | /// class body. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2759 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 71a5718 | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2760 | if (!MethodD) |
| 2761 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2762 | |
Douglas Gregor | c8c277a | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2763 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2764 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2765 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2766 | |
| 2767 | // Now that we have our default arguments, check the constructor |
| 2768 | // again. It could produce additional diagnostics or affect whether |
| 2769 | // the class has implicitly-declared destructors, among other |
| 2770 | // things. |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2771 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 2772 | CheckConstructor(Constructor); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2773 | |
| 2774 | // Check the default arguments, which we may have added. |
| 2775 | if (!Method->isInvalidDecl()) |
| 2776 | CheckCXXDefaultArguments(Method); |
| 2777 | } |
| 2778 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2779 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2780 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2781 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2782 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 2783 | /// will be updated to reflect a well-formed type for the constructor and |
| 2784 | /// returned. |
| 2785 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
| 2786 | FunctionDecl::StorageClass &SC) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2787 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2788 | |
| 2789 | // C++ [class.ctor]p3: |
| 2790 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 2791 | // constructor can be invoked for a const, volatile or const |
| 2792 | // volatile object. A constructor shall not be declared const, |
| 2793 | // volatile, or const volatile (9.3.2). |
| 2794 | if (isVirtual) { |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2795 | if (!D.isInvalidType()) |
| 2796 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2797 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 2798 | << SourceRange(D.getIdentifierLoc()); |
| 2799 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2800 | } |
| 2801 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2802 | if (!D.isInvalidType()) |
| 2803 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2804 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2805 | << SourceRange(D.getIdentifierLoc()); |
| 2806 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2807 | SC = FunctionDecl::None; |
| 2808 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2809 | |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2810 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2811 | if (FTI.TypeQuals != 0) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2812 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2813 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2814 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2815 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2816 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2817 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2818 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2819 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2820 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2821 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2822 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2823 | // Rebuild the function type "R" without any type qualifiers (in |
| 2824 | // case any of the errors above fired) and with "void" as the |
| 2825 | // return type, since constructors don't have return types. We |
| 2826 | // *always* have to do this, because GetTypeForDeclarator will |
| 2827 | // put in a result type of "int" when none was specified. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2828 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2829 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 2830 | Proto->getNumArgs(), |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2831 | Proto->isVariadic(), 0, |
| 2832 | Proto->hasExceptionSpec(), |
| 2833 | Proto->hasAnyExceptionSpec(), |
| 2834 | Proto->getNumExceptions(), |
| 2835 | Proto->exception_begin(), |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2836 | Proto->getExtInfo()); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2839 | /// CheckConstructor - Checks a fully-formed constructor for |
| 2840 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 2841 | /// the constructor declarator is invalid. |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2842 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2843 | CXXRecordDecl *ClassDecl |
Douglas Gregor | f4d17c4 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2844 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 2845 | if (!ClassDecl) |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2846 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2847 | |
| 2848 | // C++ [class.copy]p3: |
| 2849 | // A declaration of a constructor for a class X is ill-formed if |
| 2850 | // its first parameter is of type (optionally cv-qualified) X and |
| 2851 | // either there are no other parameters or else all other |
| 2852 | // parameters have default arguments. |
Douglas Gregor | f4d17c4 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2853 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2854 | ((Constructor->getNumParams() == 1) || |
| 2855 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2856 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 2857 | Constructor->getTemplateSpecializationKind() |
| 2858 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2859 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 2860 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 2861 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2862 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
| 2863 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2864 | << FixItHint::CreateInsertion(ParamLoc, " const &"); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2865 | |
| 2866 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 2867 | // to fix the type. |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2868 | Constructor->setInvalidDecl(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2869 | } |
| 2870 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2871 | |
John McCall | 43314ab | 2010-04-13 07:45:41 +0000 | [diff] [blame] | 2872 | // Notify the class that we've added a constructor. In principle we |
| 2873 | // don't need to do this for out-of-line declarations; in practice |
| 2874 | // we only instantiate the most recent declaration of a method, so |
| 2875 | // we have to call this for everything but friends. |
| 2876 | if (!Constructor->getFriendObjectKind()) |
| 2877 | ClassDecl->addedConstructor(Context, Constructor); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2880 | /// CheckDestructor - Checks a fully-formed destructor for well-formedness, |
| 2881 | /// issuing any diagnostics required. Returns true on error. |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2882 | bool Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2883 | CXXRecordDecl *RD = Destructor->getParent(); |
| 2884 | |
| 2885 | if (Destructor->isVirtual()) { |
| 2886 | SourceLocation Loc; |
| 2887 | |
| 2888 | if (!Destructor->isImplicit()) |
| 2889 | Loc = Destructor->getLocation(); |
| 2890 | else |
| 2891 | Loc = RD->getLocation(); |
| 2892 | |
| 2893 | // If we have a virtual destructor, look up the deallocation function |
| 2894 | FunctionDecl *OperatorDelete = 0; |
| 2895 | DeclarationName Name = |
| 2896 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 2897 | if (FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2898 | return true; |
| 2899 | |
| 2900 | Destructor->setOperatorDelete(OperatorDelete); |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2901 | } |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 2902 | |
| 2903 | return false; |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2906 | static inline bool |
Anders Carlsson | 5e96547 | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2907 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 2908 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 2909 | FTI.ArgInfo[0].Param && |
| 2910 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType()); |
| 2911 | } |
| 2912 | |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2913 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 2914 | /// the well-formednes of the destructor declarator @p D with type @p |
| 2915 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2916 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 2917 | /// will be updated to reflect a well-formed type for the destructor and |
| 2918 | /// returned. |
| 2919 | QualType Sema::CheckDestructorDeclarator(Declarator &D, |
| 2920 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2921 | // C++ [class.dtor]p1: |
| 2922 | // [...] A typedef-name that names a class is a class-name |
| 2923 | // (7.1.3); however, a typedef-name that names a class shall not |
| 2924 | // be used as the identifier in the declarator for a destructor |
| 2925 | // declaration. |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2926 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2927 | if (isa<TypedefType>(DeclaratorType)) { |
| 2928 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2929 | << DeclaratorType; |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2930 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
| 2933 | // C++ [class.dtor]p2: |
| 2934 | // A destructor is used to destroy objects of its class type. A |
| 2935 | // destructor takes no parameters, and no return type can be |
| 2936 | // specified for it (not even void). The address of a destructor |
| 2937 | // shall not be taken. A destructor shall not be static. A |
| 2938 | // destructor can be invoked for a const, volatile or const |
| 2939 | // volatile object. A destructor shall not be declared const, |
| 2940 | // volatile or const volatile (9.3.2). |
| 2941 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2942 | if (!D.isInvalidType()) |
| 2943 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 2944 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2945 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2946 | SC = FunctionDecl::None; |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2947 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2948 | } |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2949 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2950 | // Destructors don't have return types, but the parser will |
| 2951 | // happily parse something like: |
| 2952 | // |
| 2953 | // class X { |
| 2954 | // float ~X(); |
| 2955 | // }; |
| 2956 | // |
| 2957 | // The return type will be eliminated later. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2958 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 2959 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2960 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2961 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2962 | |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2963 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2964 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2965 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2966 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2967 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2968 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2969 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2970 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2971 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2972 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2973 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2974 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | // Make sure we don't have any parameters. |
Anders Carlsson | 5e96547 | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2978 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2979 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 2980 | |
| 2981 | // Delete the parameters. |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2982 | FTI.freeArgs(); |
| 2983 | D.setInvalidType(); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2986 | // Make sure the destructor isn't variadic. |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2987 | if (FTI.isVariadic) { |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2988 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 38378bf | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2989 | D.setInvalidType(); |
| 2990 | } |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2991 | |
| 2992 | // Rebuild the function type "R" without any type qualifiers or |
| 2993 | // parameters (in case any of the errors above fired) and with |
| 2994 | // "void" as the return type, since destructors don't have return |
| 2995 | // types. We *always* have to do this, because GetTypeForDeclarator |
| 2996 | // will put in a result type of "int" when none was specified. |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 2997 | // FIXME: Exceptions! |
| 2998 | return Context.getFunctionType(Context.VoidTy, 0, 0, false, 0, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2999 | false, false, 0, 0, FunctionType::ExtInfo()); |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3002 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 3003 | /// well-formednes of the conversion function declarator @p D with |
| 3004 | /// type @p R. If there are any errors in the declarator, this routine |
| 3005 | /// will emit diagnostics and return true. Otherwise, it will return |
| 3006 | /// false. Either way, the type @p R will be updated to reflect a |
| 3007 | /// well-formed type for the conversion operator. |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3008 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3009 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3010 | // C++ [class.conv.fct]p1: |
| 3011 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3012 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3013 | // parameter returning conversion-type-id." |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3014 | if (SC == FunctionDecl::Static) { |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3015 | if (!D.isInvalidType()) |
| 3016 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 3017 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 3018 | << SourceRange(D.getIdentifierLoc()); |
| 3019 | D.setInvalidType(); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3020 | SC = FunctionDecl::None; |
| 3021 | } |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3022 | |
| 3023 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 3024 | |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3025 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3026 | // Conversion functions don't have return types, but the parser will |
| 3027 | // happily parse something like: |
| 3028 | // |
| 3029 | // class X { |
| 3030 | // float operator bool(); |
| 3031 | // }; |
| 3032 | // |
| 3033 | // The return type will be changed later anyway. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3034 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 3035 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 3036 | << SourceRange(D.getIdentifierLoc()); |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3037 | D.setInvalidType(); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3040 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
| 3041 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3042 | // Make sure we don't have any parameters. |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3043 | if (Proto->getNumArgs() > 0) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3044 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 3045 | |
| 3046 | // Delete the parameters. |
Chris Lattner | 5742c1e | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 3047 | D.getTypeObject(0).Fun.freeArgs(); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3048 | D.setInvalidType(); |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3049 | } else if (Proto->isVariadic()) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3050 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3051 | D.setInvalidType(); |
| 3052 | } |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3053 | |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3054 | // Diagnose "&operator bool()" and other such nonsense. This |
| 3055 | // is actually a gcc extension which we don't support. |
| 3056 | if (Proto->getResultType() != ConvType) { |
| 3057 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_complex_decl) |
| 3058 | << Proto->getResultType(); |
| 3059 | D.setInvalidType(); |
| 3060 | ConvType = Proto->getResultType(); |
| 3061 | } |
| 3062 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3063 | // C++ [class.conv.fct]p4: |
| 3064 | // The conversion-type-id shall not represent a function type nor |
| 3065 | // an array type. |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3066 | if (ConvType->isArrayType()) { |
| 3067 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 3068 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3069 | D.setInvalidType(); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3070 | } else if (ConvType->isFunctionType()) { |
| 3071 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 3072 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | b41df4f | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 3073 | D.setInvalidType(); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | // Rebuild the function type "R" without any parameters (in case any |
| 3077 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3078 | // return type. |
John McCall | 212fa2e | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 3079 | if (D.isInvalidType()) { |
| 3080 | R = Context.getFunctionType(ConvType, 0, 0, false, |
| 3081 | Proto->getTypeQuals(), |
| 3082 | Proto->hasExceptionSpec(), |
| 3083 | Proto->hasAnyExceptionSpec(), |
| 3084 | Proto->getNumExceptions(), |
| 3085 | Proto->exception_begin(), |
| 3086 | Proto->getExtInfo()); |
| 3087 | } |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3088 | |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3089 | // C++0x explicit conversion operators. |
| 3090 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3091 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3092 | diag::warn_explicit_conversion_functions) |
| 3093 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3096 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 3097 | /// the declaration of the given C++ conversion function. This routine |
| 3098 | /// is responsible for recording the conversion function in the C++ |
| 3099 | /// class, if possible. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3100 | Sema::DeclPtrTy Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3101 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 3102 | |
Douglas Gregor | 4287b37 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 3103 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3104 | |
| 3105 | // Make sure we aren't redeclaring the conversion function. |
| 3106 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3107 | |
| 3108 | // C++ [class.conv.fct]p1: |
| 3109 | // [...] A conversion function is never used to convert a |
| 3110 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 3111 | // same object type (or a reference to it), to a (possibly |
| 3112 | // cv-qualified) base class of that type (or a reference to it), |
| 3113 | // or to (possibly cv-qualified) void. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3114 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 3115 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3116 | QualType ClassType |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3117 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3118 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3119 | ConvType = ConvTypeRef->getPointeeType(); |
| 3120 | if (ConvType->isRecordType()) { |
| 3121 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 3122 | if (ConvType == ClassType) |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3123 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3124 | << ClassType; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3125 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3126 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3127 | << ClassType << ConvType; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3128 | } else if (ConvType->isVoidType()) { |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 3129 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3130 | << ClassType << ConvType; |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3133 | if (Conversion->getPrimaryTemplate()) { |
| 3134 | // ignore specializations |
| 3135 | } else if (Conversion->getPreviousDeclaration()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3136 | if (FunctionTemplateDecl *ConversionTemplate |
Douglas Gregor | 133bc74 | 2010-01-11 18:53:25 +0000 | [diff] [blame] | 3137 | = Conversion->getDescribedFunctionTemplate()) { |
| 3138 | if (ClassDecl->replaceConversion( |
| 3139 | ConversionTemplate->getPreviousDeclaration(), |
| 3140 | ConversionTemplate)) |
| 3141 | return DeclPtrTy::make(ConversionTemplate); |
| 3142 | } else if (ClassDecl->replaceConversion(Conversion->getPreviousDeclaration(), |
| 3143 | Conversion)) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 3144 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 1dc9826 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 3145 | assert(Conversion->isInvalidDecl() && "Conversion should not get here."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3146 | } else if (FunctionTemplateDecl *ConversionTemplate |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3147 | = Conversion->getDescribedFunctionTemplate()) |
Fariborz Jahanian | edca0bc | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 3148 | ClassDecl->addConversionFunction(ConversionTemplate); |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3149 | else |
Fariborz Jahanian | edca0bc | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 3150 | ClassDecl->addConversionFunction(Conversion); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3151 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3152 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3155 | //===----------------------------------------------------------------------===// |
| 3156 | // Namespace Handling |
| 3157 | //===----------------------------------------------------------------------===// |
| 3158 | |
| 3159 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 3160 | /// definition. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3161 | Sema::DeclPtrTy Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 3162 | SourceLocation IdentLoc, |
| 3163 | IdentifierInfo *II, |
Anders Carlsson | a7bcade | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 3164 | SourceLocation LBrace, |
| 3165 | AttributeList *AttrList) { |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3166 | NamespaceDecl *Namespc = |
| 3167 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 3168 | Namespc->setLBracLoc(LBrace); |
| 3169 | |
| 3170 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 3171 | |
Anders Carlsson | a7bcade | 2010-02-07 01:09:23 +0000 | [diff] [blame] | 3172 | ProcessDeclAttributeList(DeclRegionScope, Namespc, AttrList); |
| 3173 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3174 | if (II) { |
| 3175 | // C++ [namespace.def]p2: |
| 3176 | // The identifier in an original-namespace-definition shall not have been |
| 3177 | // previously defined in the declarative region in which the |
| 3178 | // original-namespace-definition appears. The identifier in an |
| 3179 | // original-namespace-definition is the name of the namespace. Subsequently |
| 3180 | // in that declarative region, it is treated as an original-namespace-name. |
| 3181 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3182 | NamedDecl *PrevDecl |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 3183 | = LookupSingleName(DeclRegionScope, II, IdentLoc, LookupOrdinaryName, |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 3184 | ForRedeclaration); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3185 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3186 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 3187 | // This is an extended namespace definition. |
| 3188 | // Attach this namespace decl to the chain of extended namespace |
| 3189 | // definitions. |
| 3190 | OrigNS->setNextNamespace(Namespc); |
| 3191 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3192 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3193 | // Remove the previous declaration from the scope. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3194 | if (DeclRegionScope->isDeclScope(DeclPtrTy::make(OrigNS))) { |
Douglas Gregor | 7a4fad1 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 3195 | IdResolver.RemoveDecl(OrigNS); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3196 | DeclRegionScope->RemoveDecl(DeclPtrTy::make(OrigNS)); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3197 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3198 | } else if (PrevDecl) { |
| 3199 | // This is an invalid name redefinition. |
| 3200 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 3201 | << Namespc->getDeclName(); |
| 3202 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3203 | Namespc->setInvalidDecl(); |
| 3204 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 87f5406 | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 3205 | } else if (II->isStr("std") && |
| 3206 | CurContext->getLookupContext()->isTranslationUnit()) { |
| 3207 | // This is the first "real" definition of the namespace "std", so update |
| 3208 | // our cache of the "std" namespace to point at this definition. |
| 3209 | if (StdNamespace) { |
| 3210 | // We had already defined a dummy namespace "std". Link this new |
| 3211 | // namespace definition to the dummy namespace "std". |
| 3212 | StdNamespace->setNextNamespace(Namespc); |
| 3213 | StdNamespace->setLocation(IdentLoc); |
| 3214 | Namespc->setOriginalNamespace(StdNamespace->getOriginalNamespace()); |
| 3215 | } |
| 3216 | |
| 3217 | // Make our StdNamespace cache point at the first real definition of the |
| 3218 | // "std" namespace. |
| 3219 | StdNamespace = Namespc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3220 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3221 | |
| 3222 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 3223 | } else { |
John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3224 | // Anonymous namespaces. |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3225 | assert(Namespc->isAnonymousNamespace()); |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3226 | |
| 3227 | // Link the anonymous namespace into its parent. |
| 3228 | NamespaceDecl *PrevDecl; |
| 3229 | DeclContext *Parent = CurContext->getLookupContext(); |
| 3230 | if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(Parent)) { |
| 3231 | PrevDecl = TU->getAnonymousNamespace(); |
| 3232 | TU->setAnonymousNamespace(Namespc); |
| 3233 | } else { |
| 3234 | NamespaceDecl *ND = cast<NamespaceDecl>(Parent); |
| 3235 | PrevDecl = ND->getAnonymousNamespace(); |
| 3236 | ND->setAnonymousNamespace(Namespc); |
| 3237 | } |
| 3238 | |
| 3239 | // Link the anonymous namespace with its previous declaration. |
| 3240 | if (PrevDecl) { |
| 3241 | assert(PrevDecl->isAnonymousNamespace()); |
| 3242 | assert(!PrevDecl->getNextNamespace()); |
| 3243 | Namespc->setOriginalNamespace(PrevDecl->getOriginalNamespace()); |
| 3244 | PrevDecl->setNextNamespace(Namespc); |
| 3245 | } |
John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3246 | |
Douglas Gregor | f9f54ea | 2010-03-24 00:46:35 +0000 | [diff] [blame] | 3247 | CurContext->addDecl(Namespc); |
| 3248 | |
John McCall | 4fa5342 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 3249 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 3250 | // behaves as if it were replaced by |
| 3251 | // namespace unique { /* empty body */ } |
| 3252 | // using namespace unique; |
| 3253 | // namespace unique { namespace-body } |
| 3254 | // where all occurrences of 'unique' in a translation unit are |
| 3255 | // replaced by the same identifier and this identifier differs |
| 3256 | // from all other identifiers in the entire program. |
| 3257 | |
| 3258 | // We just create the namespace with an empty name and then add an |
| 3259 | // implicit using declaration, just like the standard suggests. |
| 3260 | // |
| 3261 | // CodeGen enforces the "universally unique" aspect by giving all |
| 3262 | // declarations semantically contained within an anonymous |
| 3263 | // namespace internal linkage. |
| 3264 | |
John McCall | 0db4225 | 2009-12-16 02:06:49 +0000 | [diff] [blame] | 3265 | if (!PrevDecl) { |
| 3266 | UsingDirectiveDecl* UD |
| 3267 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 3268 | /* 'using' */ LBrace, |
| 3269 | /* 'namespace' */ SourceLocation(), |
| 3270 | /* qualifier */ SourceRange(), |
| 3271 | /* NNS */ NULL, |
| 3272 | /* identifier */ SourceLocation(), |
| 3273 | Namespc, |
| 3274 | /* Ancestor */ CurContext); |
| 3275 | UD->setImplicit(); |
| 3276 | CurContext->addDecl(UD); |
| 3277 | } |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 3281 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3282 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 3283 | // for the namespace has the declarations that showed up in that particular |
| 3284 | // namespace definition. |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3285 | PushDeclContext(NamespcScope, Namespc); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3286 | return DeclPtrTy::make(Namespc); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3289 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 3290 | /// is a namespace alias, returns the namespace it points to. |
| 3291 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 3292 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 3293 | return AD->getNamespace(); |
| 3294 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 3295 | } |
| 3296 | |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3297 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 3298 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3299 | void Sema::ActOnFinishNamespaceDef(DeclPtrTy D, SourceLocation RBrace) { |
| 3300 | Decl *Dcl = D.getAs<Decl>(); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 3301 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 3302 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 3303 | Namespc->setRBracLoc(RBrace); |
| 3304 | PopDeclContext(); |
| 3305 | } |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3306 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3307 | Sema::DeclPtrTy Sema::ActOnUsingDirective(Scope *S, |
| 3308 | SourceLocation UsingLoc, |
| 3309 | SourceLocation NamespcLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3310 | CXXScopeSpec &SS, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3311 | SourceLocation IdentLoc, |
| 3312 | IdentifierInfo *NamespcName, |
| 3313 | AttributeList *AttrList) { |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3314 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3315 | assert(NamespcName && "Invalid NamespcName."); |
| 3316 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3317 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3318 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3319 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3320 | |
Douglas Gregor | 3407432 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 3321 | // Lookup namespace name. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3322 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 3323 | LookupParsedName(R, S, &SS); |
| 3324 | if (R.isAmbiguous()) |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3325 | return DeclPtrTy(); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3326 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3327 | if (!R.empty()) { |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3328 | NamedDecl *Named = R.getFoundDecl(); |
| 3329 | assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named)) |
| 3330 | && "expected namespace decl"); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3331 | // C++ [namespace.udir]p1: |
| 3332 | // A using-directive specifies that the names in the nominated |
| 3333 | // namespace can be used in the scope in which the |
| 3334 | // using-directive appears after the using-directive. During |
| 3335 | // unqualified name lookup (3.4.1), the names appear as if they |
| 3336 | // were declared in the nearest enclosing namespace which |
| 3337 | // contains both the using-directive and the nominated |
Eli Friedman | 44b83ee | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3338 | // namespace. [Note: in this context, "contains" means "contains |
| 3339 | // directly or indirectly". ] |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3340 | |
| 3341 | // Find enclosing context containing both using-directive and |
| 3342 | // nominated namespace. |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3343 | NamespaceDecl *NS = getNamespaceDecl(Named); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3344 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 3345 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 3346 | CommonAncestor = CommonAncestor->getParent(); |
| 3347 | |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3348 | UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc, |
Douglas Gregor | 3bc6e4c | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 3349 | SS.getRange(), |
| 3350 | (NestedNameSpecifier *)SS.getScopeRep(), |
Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 3351 | IdentLoc, Named, CommonAncestor); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3352 | PushUsingDirective(S, UDir); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3353 | } else { |
Chris Lattner | 8dca2e9 | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 3354 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3357 | // FIXME: We ignore attributes for now. |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3358 | delete AttrList; |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3359 | return DeclPtrTy::make(UDir); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 3363 | // If scope has associated entity, then using directive is at namespace |
| 3364 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 3365 | // it's lookup structure. |
| 3366 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3367 | Ctx->addDecl(UDir); |
Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 3368 | else |
| 3369 | // Otherwise it is block-sope. using-directives will affect lookup |
| 3370 | // only to the end of scope. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3371 | S->PushUsingDirective(DeclPtrTy::make(UDir)); |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 3372 | } |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3373 | |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3374 | |
| 3375 | Sema::DeclPtrTy Sema::ActOnUsingDeclaration(Scope *S, |
Anders Carlsson | 7b194b7 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 3376 | AccessSpecifier AS, |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3377 | bool HasUsingKeyword, |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3378 | SourceLocation UsingLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3379 | CXXScopeSpec &SS, |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3380 | UnqualifiedId &Name, |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3381 | AttributeList *AttrList, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3382 | bool IsTypeName, |
| 3383 | SourceLocation TypenameLoc) { |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3384 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3385 | |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3386 | switch (Name.getKind()) { |
| 3387 | case UnqualifiedId::IK_Identifier: |
| 3388 | case UnqualifiedId::IK_OperatorFunctionId: |
Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 3389 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3390 | case UnqualifiedId::IK_ConversionFunctionId: |
| 3391 | break; |
| 3392 | |
| 3393 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3394 | case UnqualifiedId::IK_ConstructorTemplateId: |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3395 | // C++0x inherited constructors. |
| 3396 | if (getLangOptions().CPlusPlus0x) break; |
| 3397 | |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3398 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 3399 | << SS.getRange(); |
| 3400 | return DeclPtrTy(); |
| 3401 | |
| 3402 | case UnqualifiedId::IK_DestructorName: |
| 3403 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 3404 | << SS.getRange(); |
| 3405 | return DeclPtrTy(); |
| 3406 | |
| 3407 | case UnqualifiedId::IK_TemplateId: |
| 3408 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 3409 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
| 3410 | return DeclPtrTy(); |
| 3411 | } |
| 3412 | |
| 3413 | DeclarationName TargetName = GetNameFromUnqualifiedId(Name); |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3414 | if (!TargetName) |
| 3415 | return DeclPtrTy(); |
| 3416 | |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3417 | // Warn about using declarations. |
| 3418 | // TODO: store that the declaration was written without 'using' and |
| 3419 | // talk about access decls instead of using decls in the |
| 3420 | // diagnostics. |
| 3421 | if (!HasUsingKeyword) { |
| 3422 | UsingLoc = Name.getSourceRange().getBegin(); |
| 3423 | |
| 3424 | Diag(UsingLoc, diag::warn_access_decl_deprecated) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3425 | << FixItHint::CreateInsertion(SS.getRange().getBegin(), "using "); |
John McCall | a009726 | 2009-12-11 02:10:03 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3428 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Douglas Gregor | 220f427 | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 3429 | Name.getSourceRange().getBegin(), |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3430 | TargetName, AttrList, |
| 3431 | /* IsInstantiation */ false, |
| 3432 | IsTypeName, TypenameLoc); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3433 | if (UD) |
| 3434 | PushOnScopeChains(UD, S, /*AddToContext*/ false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3435 | |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3436 | return DeclPtrTy::make(UD); |
| 3437 | } |
| 3438 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3439 | /// Determines whether to create a using shadow decl for a particular |
| 3440 | /// decl, given the set of decls existing prior to this using lookup. |
| 3441 | bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, |
| 3442 | const LookupResult &Previous) { |
| 3443 | // Diagnose finding a decl which is not from a base class of the |
| 3444 | // current class. We do this now because there are cases where this |
| 3445 | // function will silently decide not to build a shadow decl, which |
| 3446 | // will pre-empt further diagnostics. |
| 3447 | // |
| 3448 | // We don't need to do this in C++0x because we do the check once on |
| 3449 | // the qualifier. |
| 3450 | // |
| 3451 | // FIXME: diagnose the following if we care enough: |
| 3452 | // struct A { int foo; }; |
| 3453 | // struct B : A { using A::foo; }; |
| 3454 | // template <class T> struct C : A {}; |
| 3455 | // template <class T> struct D : C<T> { using B::foo; } // <--- |
| 3456 | // This is invalid (during instantiation) in C++03 because B::foo |
| 3457 | // resolves to the using decl in B, which is not a base class of D<T>. |
| 3458 | // We can't diagnose it immediately because C<T> is an unknown |
| 3459 | // specialization. The UsingShadowDecl in D<T> then points directly |
| 3460 | // to A::foo, which will look well-formed when we instantiate. |
| 3461 | // The right solution is to not collapse the shadow-decl chain. |
| 3462 | if (!getLangOptions().CPlusPlus0x && CurContext->isRecord()) { |
| 3463 | DeclContext *OrigDC = Orig->getDeclContext(); |
| 3464 | |
| 3465 | // Handle enums and anonymous structs. |
| 3466 | if (isa<EnumDecl>(OrigDC)) OrigDC = OrigDC->getParent(); |
| 3467 | CXXRecordDecl *OrigRec = cast<CXXRecordDecl>(OrigDC); |
| 3468 | while (OrigRec->isAnonymousStructOrUnion()) |
| 3469 | OrigRec = cast<CXXRecordDecl>(OrigRec->getDeclContext()); |
| 3470 | |
| 3471 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom(OrigRec)) { |
| 3472 | if (OrigDC == CurContext) { |
| 3473 | Diag(Using->getLocation(), |
| 3474 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3475 | << Using->getNestedNameRange(); |
| 3476 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3477 | return true; |
| 3478 | } |
| 3479 | |
| 3480 | Diag(Using->getNestedNameRange().getBegin(), |
| 3481 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3482 | << Using->getTargetNestedNameDecl() |
| 3483 | << cast<CXXRecordDecl>(CurContext) |
| 3484 | << Using->getNestedNameRange(); |
| 3485 | Diag(Orig->getLocation(), diag::note_using_decl_target); |
| 3486 | return true; |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | if (Previous.empty()) return false; |
| 3491 | |
| 3492 | NamedDecl *Target = Orig; |
| 3493 | if (isa<UsingShadowDecl>(Target)) |
| 3494 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3495 | |
John McCall | a17e83e | 2009-12-11 02:33:26 +0000 | [diff] [blame] | 3496 | // If the target happens to be one of the previous declarations, we |
| 3497 | // don't have a conflict. |
| 3498 | // |
| 3499 | // FIXME: but we might be increasing its access, in which case we |
| 3500 | // should redeclare it. |
| 3501 | NamedDecl *NonTag = 0, *Tag = 0; |
| 3502 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 3503 | I != E; ++I) { |
| 3504 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3505 | if (D->getCanonicalDecl() == Target->getCanonicalDecl()) |
| 3506 | return false; |
| 3507 | |
| 3508 | (isa<TagDecl>(D) ? Tag : NonTag) = D; |
| 3509 | } |
| 3510 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3511 | if (Target->isFunctionOrFunctionTemplate()) { |
| 3512 | FunctionDecl *FD; |
| 3513 | if (isa<FunctionTemplateDecl>(Target)) |
| 3514 | FD = cast<FunctionTemplateDecl>(Target)->getTemplatedDecl(); |
| 3515 | else |
| 3516 | FD = cast<FunctionDecl>(Target); |
| 3517 | |
| 3518 | NamedDecl *OldDecl = 0; |
| 3519 | switch (CheckOverload(FD, Previous, OldDecl)) { |
| 3520 | case Ovl_Overload: |
| 3521 | return false; |
| 3522 | |
| 3523 | case Ovl_NonFunction: |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3524 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3525 | break; |
| 3526 | |
| 3527 | // We found a decl with the exact signature. |
| 3528 | case Ovl_Match: |
| 3529 | if (isa<UsingShadowDecl>(OldDecl)) { |
| 3530 | // Silently ignore the possible conflict. |
| 3531 | return false; |
| 3532 | } |
| 3533 | |
| 3534 | // If we're in a record, we want to hide the target, so we |
| 3535 | // return true (without a diagnostic) to tell the caller not to |
| 3536 | // build a shadow decl. |
| 3537 | if (CurContext->isRecord()) |
| 3538 | return true; |
| 3539 | |
| 3540 | // If we're not in a record, this is an error. |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3541 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3542 | break; |
| 3543 | } |
| 3544 | |
| 3545 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3546 | Diag(OldDecl->getLocation(), diag::note_using_decl_conflict); |
| 3547 | return true; |
| 3548 | } |
| 3549 | |
| 3550 | // Target is not a function. |
| 3551 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3552 | if (isa<TagDecl>(Target)) { |
| 3553 | // No conflict between a tag and a non-tag. |
| 3554 | if (!Tag) return false; |
| 3555 | |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3556 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3557 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3558 | Diag(Tag->getLocation(), diag::note_using_decl_conflict); |
| 3559 | return true; |
| 3560 | } |
| 3561 | |
| 3562 | // No conflict between a tag and a non-tag. |
| 3563 | if (!NonTag) return false; |
| 3564 | |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3565 | Diag(Using->getLocation(), diag::err_using_decl_conflict); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3566 | Diag(Target->getLocation(), diag::note_using_decl_target); |
| 3567 | Diag(NonTag->getLocation(), diag::note_using_decl_conflict); |
| 3568 | return true; |
| 3569 | } |
| 3570 | |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3571 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3572 | UsingShadowDecl *Sema::BuildUsingShadowDecl(Scope *S, |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3573 | UsingDecl *UD, |
| 3574 | NamedDecl *Orig) { |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3575 | |
| 3576 | // If we resolved to another shadow declaration, just coalesce them. |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3577 | NamedDecl *Target = Orig; |
| 3578 | if (isa<UsingShadowDecl>(Target)) { |
| 3579 | Target = cast<UsingShadowDecl>(Target)->getTargetDecl(); |
| 3580 | assert(!isa<UsingShadowDecl>(Target) && "nested shadow declaration"); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | UsingShadowDecl *Shadow |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3584 | = UsingShadowDecl::Create(Context, CurContext, |
| 3585 | UD->getLocation(), UD, Target); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3586 | UD->addShadowDecl(Shadow); |
| 3587 | |
| 3588 | if (S) |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3589 | PushOnScopeChains(Shadow, S); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3590 | else |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3591 | CurContext->addDecl(Shadow); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3592 | Shadow->setAccess(UD->getAccess()); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3593 | |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3594 | // Register it as a conversion if appropriate. |
| 3595 | if (Shadow->getDeclName().getNameKind() |
| 3596 | == DeclarationName::CXXConversionFunctionName) |
| 3597 | cast<CXXRecordDecl>(CurContext)->addConversionFunction(Shadow); |
| 3598 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3599 | if (Orig->isInvalidDecl() || UD->isInvalidDecl()) |
| 3600 | Shadow->setInvalidDecl(); |
| 3601 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3602 | return Shadow; |
| 3603 | } |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3604 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3605 | /// Hides a using shadow declaration. This is required by the current |
| 3606 | /// using-decl implementation when a resolvable using declaration in a |
| 3607 | /// class is followed by a declaration which would hide or override |
| 3608 | /// one or more of the using decl's targets; for example: |
| 3609 | /// |
| 3610 | /// struct Base { void foo(int); }; |
| 3611 | /// struct Derived : Base { |
| 3612 | /// using Base::foo; |
| 3613 | /// void foo(int); |
| 3614 | /// }; |
| 3615 | /// |
| 3616 | /// The governing language is C++03 [namespace.udecl]p12: |
| 3617 | /// |
| 3618 | /// When a using-declaration brings names from a base class into a |
| 3619 | /// derived class scope, member functions in the derived class |
| 3620 | /// override and/or hide member functions with the same name and |
| 3621 | /// parameter types in a base class (rather than conflicting). |
| 3622 | /// |
| 3623 | /// There are two ways to implement this: |
| 3624 | /// (1) optimistically create shadow decls when they're not hidden |
| 3625 | /// by existing declarations, or |
| 3626 | /// (2) don't create any shadow decls (or at least don't make them |
| 3627 | /// visible) until we've fully parsed/instantiated the class. |
| 3628 | /// The problem with (1) is that we might have to retroactively remove |
| 3629 | /// a shadow decl, which requires several O(n) operations because the |
| 3630 | /// decl structures are (very reasonably) not designed for removal. |
| 3631 | /// (2) avoids this but is very fiddly and phase-dependent. |
| 3632 | void Sema::HideUsingShadowDecl(Scope *S, UsingShadowDecl *Shadow) { |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3633 | if (Shadow->getDeclName().getNameKind() == |
| 3634 | DeclarationName::CXXConversionFunctionName) |
| 3635 | cast<CXXRecordDecl>(Shadow->getDeclContext())->removeConversion(Shadow); |
| 3636 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3637 | // Remove it from the DeclContext... |
| 3638 | Shadow->getDeclContext()->removeDecl(Shadow); |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3639 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3640 | // ...and the scope, if applicable... |
| 3641 | if (S) { |
| 3642 | S->RemoveDecl(DeclPtrTy::make(static_cast<Decl*>(Shadow))); |
| 3643 | IdResolver.RemoveDecl(Shadow); |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3646 | // ...and the using decl. |
| 3647 | Shadow->getUsingDecl()->removeShadowDecl(Shadow); |
| 3648 | |
| 3649 | // TODO: complain somehow if Shadow was used. It shouldn't |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3650 | // be possible for this to happen, because...? |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3653 | /// Builds a using declaration. |
| 3654 | /// |
| 3655 | /// \param IsInstantiation - Whether this call arises from an |
| 3656 | /// instantiation of an unresolved using declaration. We treat |
| 3657 | /// the lookup differently for these declarations. |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3658 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 3659 | SourceLocation UsingLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3660 | CXXScopeSpec &SS, |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3661 | SourceLocation IdentLoc, |
| 3662 | DeclarationName Name, |
| 3663 | AttributeList *AttrList, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3664 | bool IsInstantiation, |
| 3665 | bool IsTypeName, |
| 3666 | SourceLocation TypenameLoc) { |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3667 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 3668 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 561154d | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 3669 | |
Anders Carlsson | f038fc2 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3670 | // FIXME: We ignore attributes for now. |
| 3671 | delete AttrList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3673 | if (SS.isEmpty()) { |
| 3674 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | 696a3f1 | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 3675 | return 0; |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3676 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3677 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3678 | // Do the redeclaration lookup in the current scope. |
| 3679 | LookupResult Previous(*this, Name, IdentLoc, LookupUsingDeclName, |
| 3680 | ForRedeclaration); |
| 3681 | Previous.setHideTags(false); |
| 3682 | if (S) { |
| 3683 | LookupName(Previous, S); |
| 3684 | |
| 3685 | // It is really dumb that we have to do this. |
| 3686 | LookupResult::Filter F = Previous.makeFilter(); |
| 3687 | while (F.hasNext()) { |
| 3688 | NamedDecl *D = F.next(); |
| 3689 | if (!isDeclInScope(D, CurContext, S)) |
| 3690 | F.erase(); |
| 3691 | } |
| 3692 | F.done(); |
| 3693 | } else { |
| 3694 | assert(IsInstantiation && "no scope in non-instantiation"); |
| 3695 | assert(CurContext->isRecord() && "scope not record in instantiation"); |
| 3696 | LookupQualifiedName(Previous, CurContext); |
| 3697 | } |
| 3698 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3699 | NestedNameSpecifier *NNS = |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3700 | static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 3701 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3702 | // Check for invalid redeclarations. |
| 3703 | if (CheckUsingDeclRedeclaration(UsingLoc, IsTypeName, SS, IdentLoc, Previous)) |
| 3704 | return 0; |
| 3705 | |
| 3706 | // Check for bad qualifiers. |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3707 | if (CheckUsingDeclQualifier(UsingLoc, SS, IdentLoc)) |
| 3708 | return 0; |
| 3709 | |
John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3710 | DeclContext *LookupContext = computeDeclContext(SS); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3711 | NamedDecl *D; |
John McCall | 84c16cf | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 3712 | if (!LookupContext) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3713 | if (IsTypeName) { |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3714 | // FIXME: not all declaration name kinds are legal here |
| 3715 | D = UnresolvedUsingTypenameDecl::Create(Context, CurContext, |
| 3716 | UsingLoc, TypenameLoc, |
| 3717 | SS.getRange(), NNS, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3718 | IdentLoc, Name); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3719 | } else { |
| 3720 | D = UnresolvedUsingValueDecl::Create(Context, CurContext, |
| 3721 | UsingLoc, SS.getRange(), NNS, |
| 3722 | IdentLoc, Name); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3723 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3724 | } else { |
| 3725 | D = UsingDecl::Create(Context, CurContext, IdentLoc, |
| 3726 | SS.getRange(), UsingLoc, NNS, Name, |
| 3727 | IsTypeName); |
Anders Carlsson | f038fc2 | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 3728 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3729 | D->setAccess(AS); |
| 3730 | CurContext->addDecl(D); |
| 3731 | |
| 3732 | if (!LookupContext) return D; |
| 3733 | UsingDecl *UD = cast<UsingDecl>(D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3735 | if (RequireCompleteDeclContext(SS, LookupContext)) { |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3736 | UD->setInvalidDecl(); |
| 3737 | return UD; |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3740 | // Look up the target name. |
| 3741 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3742 | LookupResult R(*this, Name, IdentLoc, LookupOrdinaryName); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3743 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3744 | // Unlike most lookups, we don't always want to hide tag |
| 3745 | // declarations: tag names are visible through the using declaration |
| 3746 | // even if hidden by ordinary names, *except* in a dependent context |
| 3747 | // where it's important for the sanity of two-phase lookup. |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3748 | if (!IsInstantiation) |
| 3749 | R.setHideTags(false); |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3750 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3751 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3752 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3753 | if (R.empty()) { |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 3754 | Diag(IdentLoc, diag::err_no_member) |
| 3755 | << Name << LookupContext << SS.getRange(); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3756 | UD->setInvalidDecl(); |
| 3757 | return UD; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3760 | if (R.isAmbiguous()) { |
| 3761 | UD->setInvalidDecl(); |
| 3762 | return UD; |
| 3763 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3764 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3765 | if (IsTypeName) { |
| 3766 | // 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] | 3767 | if (!R.getAsSingle<TypeDecl>()) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3768 | Diag(IdentLoc, diag::err_using_typename_non_type); |
| 3769 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 3770 | Diag((*I)->getUnderlyingDecl()->getLocation(), |
| 3771 | diag::note_using_decl_target); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3772 | UD->setInvalidDecl(); |
| 3773 | return UD; |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3774 | } |
| 3775 | } else { |
| 3776 | // If we asked for a non-typename and we got a type, error out, |
| 3777 | // but only if this is an instantiation of an unresolved using |
| 3778 | // decl. Otherwise just silently find the type name. |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3779 | if (IsInstantiation && R.getAsSingle<TypeDecl>()) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3780 | Diag(IdentLoc, diag::err_using_dependent_value_is_type); |
| 3781 | Diag(R.getFoundDecl()->getLocation(), diag::note_using_decl_target); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3782 | UD->setInvalidDecl(); |
| 3783 | return UD; |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 3784 | } |
Anders Carlsson | 59140b3 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
Anders Carlsson | 5a9c5ac | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3787 | // C++0x N2914 [namespace.udecl]p6: |
| 3788 | // A using-declaration shall not name a namespace. |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3789 | if (R.getAsSingle<NamespaceDecl>()) { |
Anders Carlsson | 5a9c5ac | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3790 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 3791 | << SS.getRange(); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3792 | UD->setInvalidDecl(); |
| 3793 | return UD; |
Anders Carlsson | 5a9c5ac | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 3794 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3795 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3796 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 3797 | if (!CheckUsingShadowDecl(UD, *I, Previous)) |
| 3798 | BuildUsingShadowDecl(S, UD, *I); |
| 3799 | } |
John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 3800 | |
| 3801 | return UD; |
Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3804 | /// Checks that the given using declaration is not an invalid |
| 3805 | /// redeclaration. Note that this is checking only for the using decl |
| 3806 | /// itself, not for any ill-formedness among the UsingShadowDecls. |
| 3807 | bool Sema::CheckUsingDeclRedeclaration(SourceLocation UsingLoc, |
| 3808 | bool isTypeName, |
| 3809 | const CXXScopeSpec &SS, |
| 3810 | SourceLocation NameLoc, |
| 3811 | const LookupResult &Prev) { |
| 3812 | // C++03 [namespace.udecl]p8: |
| 3813 | // C++0x [namespace.udecl]p10: |
| 3814 | // A using-declaration is a declaration and can therefore be used |
| 3815 | // repeatedly where (and only where) multiple declarations are |
| 3816 | // allowed. |
| 3817 | // That's only in file contexts. |
| 3818 | if (CurContext->getLookupContext()->isFileContext()) |
| 3819 | return false; |
| 3820 | |
| 3821 | NestedNameSpecifier *Qual |
| 3822 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 3823 | |
| 3824 | for (LookupResult::iterator I = Prev.begin(), E = Prev.end(); I != E; ++I) { |
| 3825 | NamedDecl *D = *I; |
| 3826 | |
| 3827 | bool DTypename; |
| 3828 | NestedNameSpecifier *DQual; |
| 3829 | if (UsingDecl *UD = dyn_cast<UsingDecl>(D)) { |
| 3830 | DTypename = UD->isTypeName(); |
| 3831 | DQual = UD->getTargetNestedNameDecl(); |
| 3832 | } else if (UnresolvedUsingValueDecl *UD |
| 3833 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
| 3834 | DTypename = false; |
| 3835 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3836 | } else if (UnresolvedUsingTypenameDecl *UD |
| 3837 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 3838 | DTypename = true; |
| 3839 | DQual = UD->getTargetNestedNameSpecifier(); |
| 3840 | } else continue; |
| 3841 | |
| 3842 | // using decls differ if one says 'typename' and the other doesn't. |
| 3843 | // FIXME: non-dependent using decls? |
| 3844 | if (isTypeName != DTypename) continue; |
| 3845 | |
| 3846 | // using decls differ if they name different scopes (but note that |
| 3847 | // template instantiation can cause this check to trigger when it |
| 3848 | // didn't before instantiation). |
| 3849 | if (Context.getCanonicalNestedNameSpecifier(Qual) != |
| 3850 | Context.getCanonicalNestedNameSpecifier(DQual)) |
| 3851 | continue; |
| 3852 | |
| 3853 | Diag(NameLoc, diag::err_using_decl_redeclaration) << SS.getRange(); |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 3854 | Diag(D->getLocation(), diag::note_using_decl) << 1; |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 3855 | return true; |
| 3856 | } |
| 3857 | |
| 3858 | return false; |
| 3859 | } |
| 3860 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3861 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3862 | /// Checks that the given nested-name qualifier used in a using decl |
| 3863 | /// in the current context is appropriately related to the current |
| 3864 | /// scope. If an error is found, diagnoses it and returns true. |
| 3865 | bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc, |
| 3866 | const CXXScopeSpec &SS, |
| 3867 | SourceLocation NameLoc) { |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3868 | DeclContext *NamedContext = computeDeclContext(SS); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3869 | |
John McCall | 3969e30 | 2009-12-08 07:46:18 +0000 | [diff] [blame] | 3870 | if (!CurContext->isRecord()) { |
| 3871 | // C++03 [namespace.udecl]p3: |
| 3872 | // C++0x [namespace.udecl]p8: |
| 3873 | // A using-declaration for a class member shall be a member-declaration. |
| 3874 | |
| 3875 | // If we weren't able to compute a valid scope, it must be a |
| 3876 | // dependent class scope. |
| 3877 | if (!NamedContext || NamedContext->isRecord()) { |
| 3878 | Diag(NameLoc, diag::err_using_decl_can_not_refer_to_class_member) |
| 3879 | << SS.getRange(); |
| 3880 | return true; |
| 3881 | } |
| 3882 | |
| 3883 | // Otherwise, everything is known to be fine. |
| 3884 | return false; |
| 3885 | } |
| 3886 | |
| 3887 | // The current scope is a record. |
| 3888 | |
| 3889 | // If the named context is dependent, we can't decide much. |
| 3890 | if (!NamedContext) { |
| 3891 | // FIXME: in C++0x, we can diagnose if we can prove that the |
| 3892 | // nested-name-specifier does not refer to a base class, which is |
| 3893 | // still possible in some cases. |
| 3894 | |
| 3895 | // Otherwise we have to conservatively report that things might be |
| 3896 | // okay. |
| 3897 | return false; |
| 3898 | } |
| 3899 | |
| 3900 | if (!NamedContext->isRecord()) { |
| 3901 | // Ideally this would point at the last name in the specifier, |
| 3902 | // but we don't have that level of source info. |
| 3903 | Diag(SS.getRange().getBegin(), |
| 3904 | diag::err_using_decl_nested_name_specifier_is_not_class) |
| 3905 | << (NestedNameSpecifier*) SS.getScopeRep() << SS.getRange(); |
| 3906 | return true; |
| 3907 | } |
| 3908 | |
| 3909 | if (getLangOptions().CPlusPlus0x) { |
| 3910 | // C++0x [namespace.udecl]p3: |
| 3911 | // In a using-declaration used as a member-declaration, the |
| 3912 | // nested-name-specifier shall name a base class of the class |
| 3913 | // being defined. |
| 3914 | |
| 3915 | if (cast<CXXRecordDecl>(CurContext)->isProvablyNotDerivedFrom( |
| 3916 | cast<CXXRecordDecl>(NamedContext))) { |
| 3917 | if (CurContext == NamedContext) { |
| 3918 | Diag(NameLoc, |
| 3919 | diag::err_using_decl_nested_name_specifier_is_current_class) |
| 3920 | << SS.getRange(); |
| 3921 | return true; |
| 3922 | } |
| 3923 | |
| 3924 | Diag(SS.getRange().getBegin(), |
| 3925 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3926 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 3927 | << cast<CXXRecordDecl>(CurContext) |
| 3928 | << SS.getRange(); |
| 3929 | return true; |
| 3930 | } |
| 3931 | |
| 3932 | return false; |
| 3933 | } |
| 3934 | |
| 3935 | // C++03 [namespace.udecl]p4: |
| 3936 | // A using-declaration used as a member-declaration shall refer |
| 3937 | // to a member of a base class of the class being defined [etc.]. |
| 3938 | |
| 3939 | // Salient point: SS doesn't have to name a base class as long as |
| 3940 | // lookup only finds members from base classes. Therefore we can |
| 3941 | // diagnose here only if we can prove that that can't happen, |
| 3942 | // i.e. if the class hierarchies provably don't intersect. |
| 3943 | |
| 3944 | // TODO: it would be nice if "definitely valid" results were cached |
| 3945 | // in the UsingDecl and UsingShadowDecl so that these checks didn't |
| 3946 | // need to be repeated. |
| 3947 | |
| 3948 | struct UserData { |
| 3949 | llvm::DenseSet<const CXXRecordDecl*> Bases; |
| 3950 | |
| 3951 | static bool collect(const CXXRecordDecl *Base, void *OpaqueData) { |
| 3952 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 3953 | Data->Bases.insert(Base); |
| 3954 | return true; |
| 3955 | } |
| 3956 | |
| 3957 | bool hasDependentBases(const CXXRecordDecl *Class) { |
| 3958 | return !Class->forallBases(collect, this); |
| 3959 | } |
| 3960 | |
| 3961 | /// Returns true if the base is dependent or is one of the |
| 3962 | /// accumulated base classes. |
| 3963 | static bool doesNotContain(const CXXRecordDecl *Base, void *OpaqueData) { |
| 3964 | UserData *Data = reinterpret_cast<UserData*>(OpaqueData); |
| 3965 | return !Data->Bases.count(Base); |
| 3966 | } |
| 3967 | |
| 3968 | bool mightShareBases(const CXXRecordDecl *Class) { |
| 3969 | return Bases.count(Class) || !Class->forallBases(doesNotContain, this); |
| 3970 | } |
| 3971 | }; |
| 3972 | |
| 3973 | UserData Data; |
| 3974 | |
| 3975 | // Returns false if we find a dependent base. |
| 3976 | if (Data.hasDependentBases(cast<CXXRecordDecl>(CurContext))) |
| 3977 | return false; |
| 3978 | |
| 3979 | // Returns false if the class has a dependent base or if it or one |
| 3980 | // of its bases is present in the base set of the current context. |
| 3981 | if (Data.mightShareBases(cast<CXXRecordDecl>(NamedContext))) |
| 3982 | return false; |
| 3983 | |
| 3984 | Diag(SS.getRange().getBegin(), |
| 3985 | diag::err_using_decl_nested_name_specifier_is_not_base_class) |
| 3986 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 3987 | << cast<CXXRecordDecl>(CurContext) |
| 3988 | << SS.getRange(); |
| 3989 | |
| 3990 | return true; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3993 | Sema::DeclPtrTy Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 47952ae | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 3994 | SourceLocation NamespaceLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3995 | SourceLocation AliasLoc, |
| 3996 | IdentifierInfo *Alias, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3997 | CXXScopeSpec &SS, |
Anders Carlsson | 47952ae | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 3998 | SourceLocation IdentLoc, |
| 3999 | IdentifierInfo *Ident) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4000 | |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4001 | // Lookup the namespace name. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4002 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 4003 | LookupParsedName(R, S, &SS); |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4004 | |
Anders Carlsson | dca83c4 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4005 | // Check if we have a previous declaration with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4006 | if (NamedDecl *PrevDecl |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4007 | = LookupSingleName(S, Alias, AliasLoc, LookupOrdinaryName, |
| 4008 | ForRedeclaration)) { |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4009 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4010 | // 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] | 4011 | // namespace, so don't create a new one. |
Douglas Gregor | 4667eff | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4012 | // FIXME: At some point, we'll want to create the (redundant) |
| 4013 | // declaration to maintain better source information. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4014 | if (!R.isAmbiguous() && !R.empty() && |
Douglas Gregor | 4667eff | 2010-03-26 22:59:39 +0000 | [diff] [blame] | 4015 | AD->getNamespace()->Equals(getNamespaceDecl(R.getFoundDecl()))) |
Anders Carlsson | bb1e472 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 4016 | return DeclPtrTy(); |
| 4017 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4018 | |
Anders Carlsson | dca83c4 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4019 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 4020 | diag::err_redefinition_different_kind; |
| 4021 | Diag(AliasLoc, DiagID) << Alias; |
| 4022 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4023 | return DeclPtrTy(); |
Anders Carlsson | dca83c4 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 4024 | } |
| 4025 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4026 | if (R.isAmbiguous()) |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4027 | return DeclPtrTy(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4028 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4029 | if (R.empty()) { |
Anders Carlsson | ac2c965 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 4030 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4031 | return DeclPtrTy(); |
Anders Carlsson | ac2c965 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 4032 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4033 | |
Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4034 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4035 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 4036 | Alias, SS.getRange(), |
Douglas Gregor | 1823193 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 4037 | (NestedNameSpecifier *)SS.getScopeRep(), |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4038 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4039 | |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 4040 | PushOnScopeChains(AliasDecl, S); |
Anders Carlsson | ff25fdf | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 4041 | return DeclPtrTy::make(AliasDecl); |
Anders Carlsson | 9205d55 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4044 | namespace { |
| 4045 | /// \brief Scoped object used to handle the state changes required in Sema |
| 4046 | /// to implicitly define the body of a C++ member function; |
| 4047 | class ImplicitlyDefinedFunctionScope { |
| 4048 | Sema &S; |
| 4049 | DeclContext *PreviousContext; |
| 4050 | |
| 4051 | public: |
| 4052 | ImplicitlyDefinedFunctionScope(Sema &S, CXXMethodDecl *Method) |
| 4053 | : S(S), PreviousContext(S.CurContext) |
| 4054 | { |
| 4055 | S.CurContext = Method; |
| 4056 | S.PushFunctionScope(); |
| 4057 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 4058 | } |
| 4059 | |
| 4060 | ~ImplicitlyDefinedFunctionScope() { |
| 4061 | S.PopExpressionEvaluationContext(); |
| 4062 | S.PopFunctionOrBlockScope(); |
| 4063 | S.CurContext = PreviousContext; |
| 4064 | } |
| 4065 | }; |
| 4066 | } |
| 4067 | |
Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4068 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 4069 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 18eb69a | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 4070 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
| 4071 | !Constructor->isUsed()) && |
| 4072 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4073 | |
Anders Carlsson | 423f5d8 | 2010-04-23 16:04:08 +0000 | [diff] [blame] | 4074 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4075 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | d7686ef | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 4076 | |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4077 | ImplicitlyDefinedFunctionScope Scope(*this, Constructor); |
Anders Carlsson | 4c8cb01 | 2010-04-02 03:43:34 +0000 | [diff] [blame] | 4078 | if (SetBaseOrMemberInitializers(Constructor, 0, 0, /*AnyErrors=*/false)) { |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4079 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
Anders Carlsson | 05bf009 | 2010-04-22 05:40:53 +0000 | [diff] [blame] | 4080 | << CXXConstructor << Context.getTagDeclType(ClassDecl); |
Eli Friedman | 9cf6b59 | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 4081 | Constructor->setInvalidDecl(); |
| 4082 | } else { |
| 4083 | Constructor->setUsed(); |
| 4084 | } |
Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 4085 | } |
| 4086 | |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4087 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | d94105a | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 4088 | CXXDestructorDecl *Destructor) { |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4089 | assert((Destructor->isImplicit() && !Destructor->isUsed()) && |
| 4090 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 2a50e95 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 4091 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4092 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4093 | |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4094 | ImplicitlyDefinedFunctionScope Scope(*this, Destructor); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4095 | |
John McCall | a630995 | 2010-03-16 21:39:52 +0000 | [diff] [blame] | 4096 | MarkBaseAndMemberDestructorsReferenced(Destructor->getLocation(), |
| 4097 | Destructor->getParent()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4098 | |
Anders Carlsson | 26a807d | 2009-11-30 21:24:50 +0000 | [diff] [blame] | 4099 | // FIXME: If CheckDestructor fails, we should emit a note about where the |
| 4100 | // implicit destructor was needed. |
| 4101 | if (CheckDestructor(Destructor)) { |
| 4102 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4103 | << CXXDestructor << Context.getTagDeclType(ClassDecl); |
| 4104 | |
| 4105 | Destructor->setInvalidDecl(); |
| 4106 | return; |
| 4107 | } |
| 4108 | |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4109 | Destructor->setUsed(); |
| 4110 | } |
| 4111 | |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4112 | void Sema::DefineImplicitCopyAssignment(SourceLocation CurrentLocation, |
| 4113 | CXXMethodDecl *MethodDecl) { |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4114 | assert((MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 4115 | MethodDecl->getOverloadedOperator() == OO_Equal && |
| 4116 | !MethodDecl->isUsed()) && |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4117 | "DefineImplicitCopyAssignment called for wrong function"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4118 | |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4119 | CXXRecordDecl *ClassDecl |
| 4120 | = cast<CXXRecordDecl>(MethodDecl->getDeclContext()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4121 | |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4122 | ImplicitlyDefinedFunctionScope Scope(*this, MethodDecl); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4123 | |
Fariborz Jahanian | ebe772e | 2009-06-26 16:08:57 +0000 | [diff] [blame] | 4124 | // C++[class.copy] p12 |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4125 | // Before the implicitly-declared copy assignment operator for a class is |
| 4126 | // implicitly defined, all implicitly-declared copy assignment operators |
| 4127 | // for its direct base classes and its nonstatic data members shall have |
| 4128 | // been implicitly defined. |
| 4129 | bool err = false; |
Fariborz Jahanian | 5f12b53 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 4130 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 4131 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4132 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4133 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4134 | if (CXXMethodDecl *BaseAssignOpMethod = |
Anders Carlsson | efa4732 | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 4135 | getAssignOperatorMethod(CurrentLocation, MethodDecl->getParamDecl(0), |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4136 | BaseClassDecl)) { |
| 4137 | CheckDirectMemberAccess(Base->getSourceRange().getBegin(), |
| 4138 | BaseAssignOpMethod, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4139 | PDiag(diag::err_access_assign_base) |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4140 | << Base->getType()); |
| 4141 | |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4142 | MarkDeclarationReferenced(CurrentLocation, BaseAssignOpMethod); |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4143 | } |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4144 | } |
Fariborz Jahanian | 5f12b53 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 4145 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4146 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4147 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 4148 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 4149 | FieldType = Array->getElementType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4150 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4151 | CXXRecordDecl *FieldClassDecl |
| 4152 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4153 | if (CXXMethodDecl *FieldAssignOpMethod = |
Anders Carlsson | efa4732 | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 4154 | getAssignOperatorMethod(CurrentLocation, MethodDecl->getParamDecl(0), |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4155 | FieldClassDecl)) { |
| 4156 | CheckDirectMemberAccess(Field->getLocation(), |
| 4157 | FieldAssignOpMethod, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4158 | PDiag(diag::err_access_assign_field) |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4159 | << Field->getDeclName() << Field->getType()); |
| 4160 | |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4161 | MarkDeclarationReferenced(CurrentLocation, FieldAssignOpMethod); |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4162 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 4163 | } else if (FieldType->isReferenceType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4164 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 17973e6 | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 4165 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 4166 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4167 | Diag(CurrentLocation, diag::note_first_required_here); |
| 4168 | err = true; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 4169 | } else if (FieldType.isConstQualified()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4170 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 17973e6 | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 4171 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 4172 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4173 | Diag(CurrentLocation, diag::note_first_required_here); |
| 4174 | err = true; |
| 4175 | } |
| 4176 | } |
| 4177 | if (!err) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4178 | MethodDecl->setUsed(); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | CXXMethodDecl * |
Anders Carlsson | efa4732 | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 4182 | Sema::getAssignOperatorMethod(SourceLocation CurrentLocation, |
| 4183 | ParmVarDecl *ParmDecl, |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4184 | CXXRecordDecl *ClassDecl) { |
| 4185 | QualType LHSType = Context.getTypeDeclType(ClassDecl); |
| 4186 | QualType RHSType(LHSType); |
| 4187 | // If class's assignment operator argument is const/volatile qualified, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4188 | // look for operator = (const/volatile B&). Otherwise, look for |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4189 | // operator = (B&). |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4190 | RHSType = Context.getCVRQualifiedType(RHSType, |
| 4191 | ParmDecl->getType().getCVRQualifiers()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4192 | ExprOwningPtr<Expr> LHS(this, new (Context) DeclRefExpr(ParmDecl, |
Anders Carlsson | efa4732 | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 4193 | LHSType, |
| 4194 | SourceLocation())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4195 | ExprOwningPtr<Expr> RHS(this, new (Context) DeclRefExpr(ParmDecl, |
Anders Carlsson | efa4732 | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 4196 | RHSType, |
| 4197 | CurrentLocation)); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4198 | Expr *Args[2] = { &*LHS, &*RHS }; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4199 | OverloadCandidateSet CandidateSet(CurrentLocation); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4200 | AddMemberOperatorCandidates(clang::OO_Equal, SourceLocation(), Args, 2, |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4201 | CandidateSet); |
| 4202 | OverloadCandidateSet::iterator Best; |
Anders Carlsson | efa4732 | 2009-12-09 03:01:51 +0000 | [diff] [blame] | 4203 | if (BestViableFunction(CandidateSet, CurrentLocation, Best) == OR_Success) |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 4204 | return cast<CXXMethodDecl>(Best->Function); |
| 4205 | assert(false && |
| 4206 | "getAssignOperatorMethod - copy assignment operator method not found"); |
| 4207 | return 0; |
| 4208 | } |
| 4209 | |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4210 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 4211 | CXXConstructorDecl *CopyConstructor, |
| 4212 | unsigned TypeQuals) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4213 | assert((CopyConstructor->isImplicit() && |
Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 4214 | CopyConstructor->isCopyConstructor(TypeQuals) && |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4215 | !CopyConstructor->isUsed()) && |
| 4216 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4217 | |
Anders Carlsson | 7a0ffdb | 2010-04-23 16:24:12 +0000 | [diff] [blame] | 4218 | CXXRecordDecl *ClassDecl = CopyConstructor->getParent(); |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4219 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4220 | |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 4221 | ImplicitlyDefinedFunctionScope Scope(*this, CopyConstructor); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4222 | |
Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame^] | 4223 | if (SetBaseOrMemberInitializers(CopyConstructor, 0, 0, /*AnyErrors=*/false)) { |
| 4224 | Diag(CurrentLocation, diag::note_member_synthesized_at) |
| 4225 | << CXXCopyConstructor << Context.getTagDeclType(ClassDecl); |
| 4226 | CopyConstructor->setInvalidDecl(); |
| 4227 | } else { |
| 4228 | CopyConstructor->setUsed(); |
Anders Carlsson | 53e1ba9 | 2010-04-25 00:52:09 +0000 | [diff] [blame] | 4229 | } |
Anders Carlsson | 7911150 | 2010-05-01 16:39:01 +0000 | [diff] [blame^] | 4230 | |
| 4231 | // FIXME: Once SetBaseOrMemberInitializers can handle copy initialization of |
| 4232 | // fields, this code below should be removed. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4233 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 4234 | FieldEnd = ClassDecl->field_end(); |
| 4235 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4236 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 4237 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 4238 | FieldType = Array->getElementType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4239 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4240 | CXXRecordDecl *FieldClassDecl |
| 4241 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4242 | if (CXXConstructorDecl *FieldCopyCtor = |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4243 | FieldClassDecl->getCopyConstructor(Context, TypeQuals)) { |
| 4244 | CheckDirectMemberAccess(Field->getLocation(), |
| 4245 | FieldCopyCtor, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4246 | PDiag(diag::err_access_copy_field) |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4247 | << Field->getDeclName() << Field->getType()); |
| 4248 | |
Fariborz Jahanian | a83edb0 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 4249 | MarkDeclarationReferenced(CurrentLocation, FieldCopyCtor); |
John McCall | ab8c273 | 2010-03-16 06:11:48 +0000 | [diff] [blame] | 4250 | } |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4251 | } |
| 4252 | } |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
Anders Carlsson | 6eb5557 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 4255 | Sema::OwningExprResult |
Anders Carlsson | 1b4ebfa | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 4256 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4257 | CXXConstructorDecl *Constructor, |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4258 | MultiExprArg ExprArgs, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4259 | bool RequiresZeroInit, |
| 4260 | bool BaseInitialization) { |
Anders Carlsson | 250aada | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 4261 | bool Elidable = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4262 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4263 | // C++0x [class.copy]p34: |
| 4264 | // When certain criteria are met, an implementation is allowed to |
| 4265 | // omit the copy/move construction of a class object, even if the |
| 4266 | // copy/move constructor and/or destructor for the object have |
| 4267 | // side effects. [...] |
| 4268 | // - when a temporary class object that has not been bound to a |
| 4269 | // reference (12.2) would be copied/moved to a class object |
| 4270 | // with the same cv-unqualified type, the copy/move operation |
| 4271 | // can be omitted by constructing the temporary object |
| 4272 | // directly into the target of the omitted copy/move |
| 4273 | if (Constructor->isCopyConstructor() && ExprArgs.size() >= 1) { |
| 4274 | Expr *SubExpr = ((Expr **)ExprArgs.get())[0]; |
| 4275 | Elidable = SubExpr->isTemporaryObject() && |
| 4276 | Context.hasSameUnqualifiedType(SubExpr->getType(), |
| 4277 | Context.getTypeDeclType(Constructor->getParent())); |
Anders Carlsson | 250aada | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 4278 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4279 | |
| 4280 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4281 | Elidable, move(ExprArgs), RequiresZeroInit, |
| 4282 | BaseInitialization); |
Anders Carlsson | 250aada | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
Fariborz Jahanian | aa890bf | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 4285 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 4286 | /// including handling of its default argument expressions. |
Anders Carlsson | 6eb5557 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 4287 | Sema::OwningExprResult |
Anders Carlsson | 1b4ebfa | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 4288 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 4289 | CXXConstructorDecl *Constructor, bool Elidable, |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4290 | MultiExprArg ExprArgs, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4291 | bool RequiresZeroInit, |
| 4292 | bool BaseInitialization) { |
Anders Carlsson | 5995a3e | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 4293 | unsigned NumExprs = ExprArgs.size(); |
| 4294 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4295 | |
Douglas Gregor | 27381f3 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4296 | MarkDeclarationReferenced(ConstructLoc, Constructor); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4297 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, ConstructLoc, |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4298 | Constructor, Elidable, Exprs, NumExprs, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4299 | RequiresZeroInit, BaseInitialization)); |
Fariborz Jahanian | aa890bf | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4302 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | aa890bf | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 4303 | CXXConstructorDecl *Constructor, |
Anders Carlsson | 5995a3e | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 4304 | MultiExprArg Exprs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4305 | OwningExprResult TempResult = |
Fariborz Jahanian | 57277c5 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 4306 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Anders Carlsson | 5995a3e | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 4307 | move(Exprs)); |
Anders Carlsson | c1eb79b | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 4308 | if (TempResult.isInvalid()) |
| 4309 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4310 | |
Anders Carlsson | 6eb5557 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 4311 | Expr *Temp = TempResult.takeAs<Expr>(); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 4312 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
Anders Carlsson | 6e997b2 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 4313 | Temp = MaybeCreateCXXExprWithTemporaries(Temp); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4314 | VD->setInit(Temp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4315 | |
Anders Carlsson | c1eb79b | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 4316 | return false; |
Anders Carlsson | e6840d8 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 4317 | } |
| 4318 | |
John McCall | 03c4848 | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 4319 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) { |
| 4320 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Record->getDecl()); |
Douglas Gregor | 422f155 | 2010-02-25 18:11:54 +0000 | [diff] [blame] | 4321 | if (!ClassDecl->isInvalidDecl() && !VD->isInvalidDecl() && |
| 4322 | !ClassDecl->hasTrivialDestructor()) { |
John McCall | 6781b05 | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 4323 | CXXDestructorDecl *Destructor = ClassDecl->getDestructor(Context); |
| 4324 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4325 | CheckDestructorAccess(VD->getLocation(), Destructor, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4326 | PDiag(diag::err_access_dtor_var) |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 4327 | << VD->getDeclName() |
| 4328 | << VD->getType()); |
John McCall | 6781b05 | 2010-02-02 08:45:54 +0000 | [diff] [blame] | 4329 | } |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4332 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4333 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 4334 | /// e.g: "int x(1);" |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4335 | void Sema::AddCXXDirectInitializerToDecl(DeclPtrTy Dcl, |
| 4336 | SourceLocation LParenLoc, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 4337 | MultiExprArg Exprs, |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4338 | SourceLocation *CommaLocs, |
| 4339 | SourceLocation RParenLoc) { |
Daniel Dunbar | 2db411f | 2009-12-24 19:19:26 +0000 | [diff] [blame] | 4340 | assert(Exprs.size() != 0 && Exprs.get() && "missing expressions"); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4341 | Decl *RealDecl = Dcl.getAs<Decl>(); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4342 | |
| 4343 | // If there is no declaration, there was an error parsing it. Just ignore |
| 4344 | // the initializer. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4345 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4346 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4347 | |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4348 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 4349 | if (!VDecl) { |
| 4350 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 4351 | RealDecl->setInvalidDecl(); |
| 4352 | return; |
| 4353 | } |
| 4354 | |
Douglas Gregor | 402250f | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 4355 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | 997d00d | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 4356 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4357 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 4358 | // |
| 4359 | // Clients that want to distinguish between the two forms, can check for |
| 4360 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 4361 | // A major benefit is that clients that don't particularly care about which |
| 4362 | // exactly form was it (like the CodeGen) can handle both cases without |
| 4363 | // special case code. |
Argyrios Kyrtzidis | 153d967 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 4364 | |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4365 | // C++ 8.5p11: |
| 4366 | // The form of initialization (using parentheses or '=') is generally |
| 4367 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 153d967 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 4368 | // class type. |
Douglas Gregor | c28b57d | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4369 | QualType DeclInitType = VDecl->getType(); |
| 4370 | if (const ArrayType *Array = Context.getAsArrayType(DeclInitType)) |
Fariborz Jahanian | d264ee0 | 2009-10-28 19:04:36 +0000 | [diff] [blame] | 4371 | DeclInitType = Context.getBaseElementType(Array); |
Argyrios Kyrtzidis | 153d967 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 4372 | |
Douglas Gregor | 50dc219 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 4373 | if (!VDecl->getType()->isDependentType() && |
| 4374 | RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 4375 | diag::err_typecheck_decl_incomplete_type)) { |
| 4376 | VDecl->setInvalidDecl(); |
| 4377 | return; |
| 4378 | } |
| 4379 | |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4380 | // The variable can not have an abstract class type. |
| 4381 | if (RequireNonAbstractType(VDecl->getLocation(), VDecl->getType(), |
| 4382 | diag::err_abstract_type_in_decl, |
| 4383 | AbstractVariableType)) |
| 4384 | VDecl->setInvalidDecl(); |
| 4385 | |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 4386 | const VarDecl *Def; |
| 4387 | if ((Def = VDecl->getDefinition()) && Def != VDecl) { |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4388 | Diag(VDecl->getLocation(), diag::err_redefinition) |
| 4389 | << VDecl->getDeclName(); |
| 4390 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4391 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 153d967 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 4392 | return; |
| 4393 | } |
Douglas Gregor | 50dc219 | 2010-02-11 22:55:30 +0000 | [diff] [blame] | 4394 | |
| 4395 | // If either the declaration has a dependent type or if any of the |
| 4396 | // expressions is type-dependent, we represent the initialization |
| 4397 | // via a ParenListExpr for later use during template instantiation. |
| 4398 | if (VDecl->getType()->isDependentType() || |
| 4399 | Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { |
| 4400 | // Let clients know that initialization was done with a direct initializer. |
| 4401 | VDecl->setCXXDirectInitializer(true); |
| 4402 | |
| 4403 | // Store the initialization expressions as a ParenListExpr. |
| 4404 | unsigned NumExprs = Exprs.size(); |
| 4405 | VDecl->setInit(new (Context) ParenListExpr(Context, LParenLoc, |
| 4406 | (Expr **)Exprs.release(), |
| 4407 | NumExprs, RParenLoc)); |
| 4408 | return; |
| 4409 | } |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4410 | |
| 4411 | // Capture the variable that is being initialized and the style of |
| 4412 | // initialization. |
| 4413 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VDecl); |
| 4414 | |
| 4415 | // FIXME: Poor source location information. |
| 4416 | InitializationKind Kind |
| 4417 | = InitializationKind::CreateDirect(VDecl->getLocation(), |
| 4418 | LParenLoc, RParenLoc); |
| 4419 | |
| 4420 | InitializationSequence InitSeq(*this, Entity, Kind, |
| 4421 | (Expr**)Exprs.get(), Exprs.size()); |
| 4422 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(Exprs)); |
| 4423 | if (Result.isInvalid()) { |
| 4424 | VDecl->setInvalidDecl(); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4425 | return; |
| 4426 | } |
Douglas Gregor | b6ea608 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4427 | |
| 4428 | Result = MaybeCreateCXXExprWithTemporaries(move(Result)); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4429 | VDecl->setInit(Result.takeAs<Expr>()); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4430 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | 997d00d | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 4431 | |
John McCall | 03c4848 | 2010-02-02 09:10:11 +0000 | [diff] [blame] | 4432 | if (const RecordType *Record = VDecl->getType()->getAs<RecordType>()) |
| 4433 | FinalizeVarWithDestructor(VDecl, Record); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4434 | } |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4435 | |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4436 | /// \brief Given a constructor and the set of arguments provided for the |
| 4437 | /// constructor, convert the arguments and add any required default arguments |
| 4438 | /// to form a proper call to this constructor. |
| 4439 | /// |
| 4440 | /// \returns true if an error occurred, false otherwise. |
| 4441 | bool |
| 4442 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 4443 | MultiExprArg ArgsPtr, |
| 4444 | SourceLocation Loc, |
| 4445 | ASTOwningVector<&ActionBase::DeleteExpr> &ConvertedArgs) { |
| 4446 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 4447 | unsigned NumArgs = ArgsPtr.size(); |
| 4448 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 4449 | |
| 4450 | const FunctionProtoType *Proto |
| 4451 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 4452 | assert(Proto && "Constructor without a prototype?"); |
| 4453 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4454 | |
| 4455 | // 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] | 4456 | if (NumArgs < NumArgsInProto) |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4457 | ConvertedArgs.reserve(NumArgsInProto); |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4458 | else |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4459 | ConvertedArgs.reserve(NumArgs); |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4460 | |
| 4461 | VariadicCallType CallType = |
| 4462 | Proto->isVariadic() ? VariadicConstructor : VariadicDoesNotApply; |
| 4463 | llvm::SmallVector<Expr *, 8> AllArgs; |
| 4464 | bool Invalid = GatherArgumentsForCall(Loc, Constructor, |
| 4465 | Proto, 0, Args, NumArgs, AllArgs, |
| 4466 | CallType); |
| 4467 | for (unsigned i =0, size = AllArgs.size(); i < size; i++) |
| 4468 | ConvertedArgs.push_back(AllArgs[i]); |
| 4469 | return Invalid; |
Douglas Gregor | c28b57d | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4472 | static inline bool |
| 4473 | CheckOperatorNewDeleteDeclarationScope(Sema &SemaRef, |
| 4474 | const FunctionDecl *FnDecl) { |
| 4475 | const DeclContext *DC = FnDecl->getDeclContext()->getLookupContext(); |
| 4476 | if (isa<NamespaceDecl>(DC)) { |
| 4477 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4478 | diag::err_operator_new_delete_declared_in_namespace) |
| 4479 | << FnDecl->getDeclName(); |
| 4480 | } |
| 4481 | |
| 4482 | if (isa<TranslationUnitDecl>(DC) && |
| 4483 | FnDecl->getStorageClass() == FunctionDecl::Static) { |
| 4484 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4485 | diag::err_operator_new_delete_declared_static) |
| 4486 | << FnDecl->getDeclName(); |
| 4487 | } |
| 4488 | |
Anders Carlsson | 60659a8 | 2009-12-12 02:43:16 +0000 | [diff] [blame] | 4489 | return false; |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4492 | static inline bool |
| 4493 | CheckOperatorNewDeleteTypes(Sema &SemaRef, const FunctionDecl *FnDecl, |
| 4494 | CanQualType ExpectedResultType, |
| 4495 | CanQualType ExpectedFirstParamType, |
| 4496 | unsigned DependentParamTypeDiag, |
| 4497 | unsigned InvalidParamTypeDiag) { |
| 4498 | QualType ResultType = |
| 4499 | FnDecl->getType()->getAs<FunctionType>()->getResultType(); |
| 4500 | |
| 4501 | // Check that the result type is not dependent. |
| 4502 | if (ResultType->isDependentType()) |
| 4503 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4504 | diag::err_operator_new_delete_dependent_result_type) |
| 4505 | << FnDecl->getDeclName() << ExpectedResultType; |
| 4506 | |
| 4507 | // Check that the result type is what we expect. |
| 4508 | if (SemaRef.Context.getCanonicalType(ResultType) != ExpectedResultType) |
| 4509 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4510 | diag::err_operator_new_delete_invalid_result_type) |
| 4511 | << FnDecl->getDeclName() << ExpectedResultType; |
| 4512 | |
| 4513 | // A function template must have at least 2 parameters. |
| 4514 | if (FnDecl->getDescribedFunctionTemplate() && FnDecl->getNumParams() < 2) |
| 4515 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4516 | diag::err_operator_new_delete_template_too_few_parameters) |
| 4517 | << FnDecl->getDeclName(); |
| 4518 | |
| 4519 | // The function decl must have at least 1 parameter. |
| 4520 | if (FnDecl->getNumParams() == 0) |
| 4521 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4522 | diag::err_operator_new_delete_too_few_parameters) |
| 4523 | << FnDecl->getDeclName(); |
| 4524 | |
| 4525 | // Check the the first parameter type is not dependent. |
| 4526 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 4527 | if (FirstParamType->isDependentType()) |
| 4528 | return SemaRef.Diag(FnDecl->getLocation(), DependentParamTypeDiag) |
| 4529 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 4530 | |
| 4531 | // Check that the first parameter type is what we expect. |
Douglas Gregor | 684d7bd | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 4532 | if (SemaRef.Context.getCanonicalType(FirstParamType).getUnqualifiedType() != |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4533 | ExpectedFirstParamType) |
| 4534 | return SemaRef.Diag(FnDecl->getLocation(), InvalidParamTypeDiag) |
| 4535 | << FnDecl->getDeclName() << ExpectedFirstParamType; |
| 4536 | |
| 4537 | return false; |
| 4538 | } |
| 4539 | |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4540 | static bool |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4541 | CheckOperatorNewDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4542 | // C++ [basic.stc.dynamic.allocation]p1: |
| 4543 | // A program is ill-formed if an allocation function is declared in a |
| 4544 | // namespace scope other than global scope or declared static in global |
| 4545 | // scope. |
| 4546 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 4547 | return true; |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4548 | |
| 4549 | CanQualType SizeTy = |
| 4550 | SemaRef.Context.getCanonicalType(SemaRef.Context.getSizeType()); |
| 4551 | |
| 4552 | // C++ [basic.stc.dynamic.allocation]p1: |
| 4553 | // The return type shall be void*. The first parameter shall have type |
| 4554 | // std::size_t. |
| 4555 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidPtrTy, |
| 4556 | SizeTy, |
| 4557 | diag::err_operator_new_dependent_param_type, |
| 4558 | diag::err_operator_new_param_type)) |
| 4559 | return true; |
| 4560 | |
| 4561 | // C++ [basic.stc.dynamic.allocation]p1: |
| 4562 | // The first parameter shall not have an associated default argument. |
| 4563 | if (FnDecl->getParamDecl(0)->hasDefaultArg()) |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4564 | return SemaRef.Diag(FnDecl->getLocation(), |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4565 | diag::err_operator_new_default_arg) |
| 4566 | << FnDecl->getDeclName() << FnDecl->getParamDecl(0)->getDefaultArgRange(); |
| 4567 | |
| 4568 | return false; |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | static bool |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4572 | CheckOperatorDeleteDeclaration(Sema &SemaRef, const FunctionDecl *FnDecl) { |
| 4573 | // C++ [basic.stc.dynamic.deallocation]p1: |
| 4574 | // A program is ill-formed if deallocation functions are declared in a |
| 4575 | // namespace scope other than global scope or declared static in global |
| 4576 | // scope. |
Anders Carlsson | e363c8e | 2009-12-12 00:32:00 +0000 | [diff] [blame] | 4577 | if (CheckOperatorNewDeleteDeclarationScope(SemaRef, FnDecl)) |
| 4578 | return true; |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4579 | |
| 4580 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 4581 | // Each deallocation function shall return void and its first parameter |
| 4582 | // shall be void*. |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4583 | if (CheckOperatorNewDeleteTypes(SemaRef, FnDecl, SemaRef.Context.VoidTy, |
| 4584 | SemaRef.Context.VoidPtrTy, |
| 4585 | diag::err_operator_delete_dependent_param_type, |
| 4586 | diag::err_operator_delete_param_type)) |
| 4587 | return true; |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4588 | |
Anders Carlsson | c0b2ce1 | 2009-12-12 00:16:02 +0000 | [diff] [blame] | 4589 | QualType FirstParamType = FnDecl->getParamDecl(0)->getType(); |
| 4590 | if (FirstParamType->isDependentType()) |
| 4591 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4592 | diag::err_operator_delete_dependent_param_type) |
| 4593 | << FnDecl->getDeclName() << SemaRef.Context.VoidPtrTy; |
| 4594 | |
| 4595 | if (SemaRef.Context.getCanonicalType(FirstParamType) != |
| 4596 | SemaRef.Context.VoidPtrTy) |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4597 | return SemaRef.Diag(FnDecl->getLocation(), |
| 4598 | diag::err_operator_delete_param_type) |
| 4599 | << FnDecl->getDeclName() << SemaRef.Context.VoidPtrTy; |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4600 | |
| 4601 | return false; |
| 4602 | } |
| 4603 | |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4604 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 4605 | /// of this overloaded operator is well-formed. If so, returns false; |
| 4606 | /// otherwise, emits appropriate diagnostics and returns true. |
| 4607 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4608 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4609 | "Expected an overloaded operator declaration"); |
| 4610 | |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4611 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 4612 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4613 | // C++ [over.oper]p5: |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4614 | // The allocation and deallocation functions, operator new, |
| 4615 | // operator new[], operator delete and operator delete[], are |
| 4616 | // described completely in 3.7.3. The attributes and restrictions |
| 4617 | // found in the rest of this subclause do not apply to them unless |
| 4618 | // explicitly stated in 3.7.3. |
Anders Carlsson | f1f4695 | 2009-12-11 23:31:21 +0000 | [diff] [blame] | 4619 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Anders Carlsson | 12308f4 | 2009-12-11 23:23:22 +0000 | [diff] [blame] | 4620 | return CheckOperatorDeleteDeclaration(*this, FnDecl); |
Fariborz Jahanian | 4e08894 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 4621 | |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4622 | if (Op == OO_New || Op == OO_Array_New) |
| 4623 | return CheckOperatorNewDeclaration(*this, FnDecl); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4624 | |
| 4625 | // C++ [over.oper]p6: |
| 4626 | // An operator function shall either be a non-static member |
| 4627 | // function or be a non-member function and have at least one |
| 4628 | // parameter whose type is a class, a reference to a class, an |
| 4629 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4630 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4631 | if (MethodDecl->isStatic()) |
| 4632 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4633 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4634 | } else { |
| 4635 | bool ClassOrEnumParam = false; |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4636 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 4637 | ParamEnd = FnDecl->param_end(); |
| 4638 | Param != ParamEnd; ++Param) { |
| 4639 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 173e0b7a | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 4640 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 4641 | ParamType->isEnumeralType()) { |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4642 | ClassOrEnumParam = true; |
| 4643 | break; |
| 4644 | } |
| 4645 | } |
| 4646 | |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4647 | if (!ClassOrEnumParam) |
| 4648 | return Diag(FnDecl->getLocation(), |
Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4649 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4650 | << FnDecl->getDeclName(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | // C++ [over.oper]p8: |
| 4654 | // An operator function cannot have default arguments (8.3.6), |
| 4655 | // except where explicitly stated below. |
| 4656 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | // Only the function-call operator allows default arguments |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4658 | // (C++ [over.call]p1). |
| 4659 | if (Op != OO_Call) { |
| 4660 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 4661 | Param != FnDecl->param_end(); ++Param) { |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4662 | if ((*Param)->hasDefaultArg()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4663 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4664 | diag::err_operator_overload_default_arg) |
Anders Carlsson | 7e0b207 | 2009-12-13 17:53:43 +0000 | [diff] [blame] | 4665 | << FnDecl->getDeclName() << (*Param)->getDefaultArgRange(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4666 | } |
| 4667 | } |
| 4668 | |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4669 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 4670 | { false, false, false } |
| 4671 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 4672 | , { Unary, Binary, MemberOnly } |
| 4673 | #include "clang/Basic/OperatorKinds.def" |
| 4674 | }; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4675 | |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4676 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 4677 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 4678 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4679 | |
| 4680 | // C++ [over.oper]p8: |
| 4681 | // [...] Operator functions cannot have more or fewer parameters |
| 4682 | // than the number required for the corresponding operator, as |
| 4683 | // described in the rest of this subclause. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4684 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4685 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4686 | if (Op != OO_Call && |
| 4687 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 4688 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 4689 | (NumParams < 1) || (NumParams > 2))) { |
| 4690 | // We have the wrong number of parameters. |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4691 | unsigned ErrorKind; |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4692 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4693 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4694 | } else if (CanBeUnaryOperator) { |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4695 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4696 | } else { |
Chris Lattner | 2b78690 | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 4697 | assert(CanBeBinaryOperator && |
| 4698 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4699 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 6cf0806 | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4700 | } |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4701 | |
Chris Lattner | c5bab9f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4702 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4703 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4704 | } |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4705 | |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4706 | // Overloaded operators other than operator() cannot be variadic. |
| 4707 | if (Op != OO_Call && |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4708 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4709 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4710 | << FnDecl->getDeclName(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | // Some operators must be non-static member functions. |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4714 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 4715 | return Diag(FnDecl->getLocation(), |
Chris Lattner | 651d42d | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4716 | diag::err_operator_overload_must_be_member) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4717 | << FnDecl->getDeclName(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | // C++ [over.inc]p1: |
| 4721 | // The user-defined function called operator++ implements the |
| 4722 | // prefix and postfix ++ operator. If this function is a member |
| 4723 | // function with no parameters, or a non-member function with one |
| 4724 | // parameter of class or enumeration type, it defines the prefix |
| 4725 | // increment operator ++ for objects of that type. If the function |
| 4726 | // is a member function with one parameter (which shall be of type |
| 4727 | // int) or a non-member function with two parameters (the second |
| 4728 | // of which shall be of type int), it defines the postfix |
| 4729 | // increment operator ++ for objects of that type. |
| 4730 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 4731 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 4732 | bool ParamIsInt = false; |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4733 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4734 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 4735 | |
Chris Lattner | 2b78690 | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 4736 | if (!ParamIsInt) |
| 4737 | return Diag(LastParam->getLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4738 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4739 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4742 | // Notify the class if it got an assignment operator. |
| 4743 | if (Op == OO_Equal) { |
| 4744 | // Would have returned earlier otherwise. |
| 4745 | assert(isa<CXXMethodDecl>(FnDecl) && |
| 4746 | "Overloaded = not member, but not filtered."); |
| 4747 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 4748 | Method->getParent()->addedAssignmentOperator(Context, Method); |
| 4749 | } |
| 4750 | |
Douglas Gregor | d69246b | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4751 | return false; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4752 | } |
Chris Lattner | 3b024a3 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 4753 | |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 4754 | /// CheckLiteralOperatorDeclaration - Check whether the declaration |
| 4755 | /// of this literal operator function is well-formed. If so, returns |
| 4756 | /// false; otherwise, emits appropriate diagnostics and returns true. |
| 4757 | bool Sema::CheckLiteralOperatorDeclaration(FunctionDecl *FnDecl) { |
| 4758 | DeclContext *DC = FnDecl->getDeclContext(); |
| 4759 | Decl::Kind Kind = DC->getDeclKind(); |
| 4760 | if (Kind != Decl::TranslationUnit && Kind != Decl::Namespace && |
| 4761 | Kind != Decl::LinkageSpec) { |
| 4762 | Diag(FnDecl->getLocation(), diag::err_literal_operator_outside_namespace) |
| 4763 | << FnDecl->getDeclName(); |
| 4764 | return true; |
| 4765 | } |
| 4766 | |
| 4767 | bool Valid = false; |
| 4768 | |
Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 4769 | // template <char...> type operator "" name() is the only valid template |
| 4770 | // signature, and the only valid signature with no parameters. |
| 4771 | if (FnDecl->param_size() == 0) { |
| 4772 | if (FunctionTemplateDecl *TpDecl = FnDecl->getDescribedFunctionTemplate()) { |
| 4773 | // Must have only one template parameter |
| 4774 | TemplateParameterList *Params = TpDecl->getTemplateParameters(); |
| 4775 | if (Params->size() == 1) { |
| 4776 | NonTypeTemplateParmDecl *PmDecl = |
| 4777 | cast<NonTypeTemplateParmDecl>(Params->getParam(0)); |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 4778 | |
Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 4779 | // The template parameter must be a char parameter pack. |
| 4780 | // FIXME: This test will always fail because non-type parameter packs |
| 4781 | // have not been implemented. |
| 4782 | if (PmDecl && PmDecl->isTemplateParameterPack() && |
| 4783 | Context.hasSameType(PmDecl->getType(), Context.CharTy)) |
| 4784 | Valid = true; |
| 4785 | } |
| 4786 | } |
| 4787 | } else { |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 4788 | // Check the first parameter |
Alexis Hunt | 7dd2617 | 2010-04-07 23:11:06 +0000 | [diff] [blame] | 4789 | FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 4790 | |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 4791 | QualType T = (*Param)->getType(); |
| 4792 | |
Alexis Hunt | 079a6f7 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 4793 | // unsigned long long int, long double, and any character type are allowed |
| 4794 | // as the only parameters. |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 4795 | if (Context.hasSameType(T, Context.UnsignedLongLongTy) || |
| 4796 | Context.hasSameType(T, Context.LongDoubleTy) || |
| 4797 | Context.hasSameType(T, Context.CharTy) || |
| 4798 | Context.hasSameType(T, Context.WCharTy) || |
| 4799 | Context.hasSameType(T, Context.Char16Ty) || |
| 4800 | Context.hasSameType(T, Context.Char32Ty)) { |
| 4801 | if (++Param == FnDecl->param_end()) |
| 4802 | Valid = true; |
| 4803 | goto FinishedParams; |
| 4804 | } |
| 4805 | |
Alexis Hunt | 079a6f7 | 2010-04-07 22:57:35 +0000 | [diff] [blame] | 4806 | // 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] | 4807 | const PointerType *PT = T->getAs<PointerType>(); |
| 4808 | if (!PT) |
| 4809 | goto FinishedParams; |
| 4810 | T = PT->getPointeeType(); |
| 4811 | if (!T.isConstQualified()) |
| 4812 | goto FinishedParams; |
| 4813 | T = T.getUnqualifiedType(); |
| 4814 | |
| 4815 | // Move on to the second parameter; |
| 4816 | ++Param; |
| 4817 | |
| 4818 | // If there is no second parameter, the first must be a const char * |
| 4819 | if (Param == FnDecl->param_end()) { |
| 4820 | if (Context.hasSameType(T, Context.CharTy)) |
| 4821 | Valid = true; |
| 4822 | goto FinishedParams; |
| 4823 | } |
| 4824 | |
| 4825 | // const char *, const wchar_t*, const char16_t*, and const char32_t* |
| 4826 | // are allowed as the first parameter to a two-parameter function |
| 4827 | if (!(Context.hasSameType(T, Context.CharTy) || |
| 4828 | Context.hasSameType(T, Context.WCharTy) || |
| 4829 | Context.hasSameType(T, Context.Char16Ty) || |
| 4830 | Context.hasSameType(T, Context.Char32Ty))) |
| 4831 | goto FinishedParams; |
| 4832 | |
| 4833 | // The second and final parameter must be an std::size_t |
| 4834 | T = (*Param)->getType().getUnqualifiedType(); |
| 4835 | if (Context.hasSameType(T, Context.getSizeType()) && |
| 4836 | ++Param == FnDecl->param_end()) |
| 4837 | Valid = true; |
| 4838 | } |
| 4839 | |
| 4840 | // FIXME: This diagnostic is absolutely terrible. |
| 4841 | FinishedParams: |
| 4842 | if (!Valid) { |
| 4843 | Diag(FnDecl->getLocation(), diag::err_literal_operator_params) |
| 4844 | << FnDecl->getDeclName(); |
| 4845 | return true; |
| 4846 | } |
| 4847 | |
| 4848 | return false; |
| 4849 | } |
| 4850 | |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4851 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 4852 | /// linkage specification, including the language and (if present) |
| 4853 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 4854 | /// the location of the language string literal, which is provided |
| 4855 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 4856 | /// the '{' brace. Otherwise, this linkage specification does not |
| 4857 | /// have any braces. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4858 | Sema::DeclPtrTy Sema::ActOnStartLinkageSpecification(Scope *S, |
| 4859 | SourceLocation ExternLoc, |
| 4860 | SourceLocation LangLoc, |
| 4861 | const char *Lang, |
| 4862 | unsigned StrSize, |
| 4863 | SourceLocation LBraceLoc) { |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4864 | LinkageSpecDecl::LanguageIDs Language; |
| 4865 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 4866 | Language = LinkageSpecDecl::lang_c; |
| 4867 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 4868 | Language = LinkageSpecDecl::lang_cxx; |
| 4869 | else { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4870 | Diag(LangLoc, diag::err_bad_language); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4871 | return DeclPtrTy(); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4872 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4873 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4874 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4875 | |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4876 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4877 | LangLoc, Language, |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4878 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4879 | CurContext->addDecl(D); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4880 | PushDeclContext(S, D); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4881 | return DeclPtrTy::make(D); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4884 | /// ActOnFinishLinkageSpecification - Completely the definition of |
| 4885 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 4886 | /// valid, it's the position of the closing '}' brace in a linkage |
| 4887 | /// specification that uses braces. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4888 | Sema::DeclPtrTy Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 4889 | DeclPtrTy LinkageSpec, |
| 4890 | SourceLocation RBraceLoc) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4891 | if (LinkageSpec) |
| 4892 | PopDeclContext(); |
| 4893 | return LinkageSpec; |
Chris Lattner | 3b024a3 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 4894 | } |
| 4895 | |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4896 | /// \brief Perform semantic analysis for the variable declaration that |
| 4897 | /// occurs within a C++ catch clause, returning the newly-created |
| 4898 | /// variable. |
| 4899 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, QualType ExDeclType, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4900 | TypeSourceInfo *TInfo, |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4901 | IdentifierInfo *Name, |
| 4902 | SourceLocation Loc, |
| 4903 | SourceRange Range) { |
| 4904 | bool Invalid = false; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4905 | |
| 4906 | // Arrays and functions decay. |
| 4907 | if (ExDeclType->isArrayType()) |
| 4908 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 4909 | else if (ExDeclType->isFunctionType()) |
| 4910 | ExDeclType = Context.getPointerType(ExDeclType); |
| 4911 | |
| 4912 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 4913 | // The exception-declaration shall not denote a pointer or reference to an |
| 4914 | // incomplete type, other than [cv] void*. |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4915 | // N2844 forbids rvalue references. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4916 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4917 | Diag(Loc, diag::err_catch_rvalue_ref) << Range; |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4918 | Invalid = true; |
| 4919 | } |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4920 | |
Douglas Gregor | 104ee00 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 4921 | // GCC allows catching pointers and references to incomplete types |
| 4922 | // as an extension; so do we, but we warn by default. |
| 4923 | |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4924 | QualType BaseType = ExDeclType; |
| 4925 | 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] | 4926 | unsigned DK = diag::err_catch_incomplete; |
Douglas Gregor | 104ee00 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 4927 | bool IncompleteCatchIsInvalid = true; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4928 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4929 | BaseType = Ptr->getPointeeType(); |
| 4930 | Mode = 1; |
Douglas Gregor | 104ee00 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 4931 | DK = diag::ext_catch_incomplete_ptr; |
| 4932 | IncompleteCatchIsInvalid = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4933 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4934 | // 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] | 4935 | BaseType = Ref->getPointeeType(); |
| 4936 | Mode = 2; |
Douglas Gregor | 104ee00 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 4937 | DK = diag::ext_catch_incomplete_ref; |
| 4938 | IncompleteCatchIsInvalid = false; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4939 | } |
Sebastian Redl | b28b407 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4940 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | 104ee00 | 2010-03-08 01:47:36 +0000 | [diff] [blame] | 4941 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK) && |
| 4942 | IncompleteCatchIsInvalid) |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4943 | Invalid = true; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4944 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4945 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4946 | RequireNonAbstractType(Loc, ExDeclType, |
| 4947 | diag::err_abstract_type_in_decl, |
| 4948 | AbstractVariableType)) |
Sebastian Redl | 2f38ba5 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 4949 | Invalid = true; |
| 4950 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4951 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 4952 | Name, ExDeclType, TInfo, VarDecl::None, |
| 4953 | VarDecl::None); |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4954 | |
Douglas Gregor | 6de584c | 2010-03-05 23:38:39 +0000 | [diff] [blame] | 4955 | if (!Invalid) { |
| 4956 | if (const RecordType *RecordTy = ExDeclType->getAs<RecordType>()) { |
| 4957 | // C++ [except.handle]p16: |
| 4958 | // The object declared in an exception-declaration or, if the |
| 4959 | // exception-declaration does not specify a name, a temporary (12.2) is |
| 4960 | // copy-initialized (8.5) from the exception object. [...] |
| 4961 | // The object is destroyed when the handler exits, after the destruction |
| 4962 | // of any automatic objects initialized within the handler. |
| 4963 | // |
| 4964 | // We just pretend to initialize the object with itself, then make sure |
| 4965 | // it can be destroyed later. |
| 4966 | InitializedEntity Entity = InitializedEntity::InitializeVariable(ExDecl); |
| 4967 | Expr *ExDeclRef = DeclRefExpr::Create(Context, 0, SourceRange(), ExDecl, |
| 4968 | Loc, ExDeclType, 0); |
| 4969 | InitializationKind Kind = InitializationKind::CreateCopy(Loc, |
| 4970 | SourceLocation()); |
| 4971 | InitializationSequence InitSeq(*this, Entity, Kind, &ExDeclRef, 1); |
| 4972 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
| 4973 | MultiExprArg(*this, (void**)&ExDeclRef, 1)); |
| 4974 | if (Result.isInvalid()) |
| 4975 | Invalid = true; |
| 4976 | else |
| 4977 | FinalizeVarWithDestructor(ExDecl, RecordTy); |
| 4978 | } |
| 4979 | } |
| 4980 | |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4981 | if (Invalid) |
| 4982 | ExDecl->setInvalidDecl(); |
| 4983 | |
| 4984 | return ExDecl; |
| 4985 | } |
| 4986 | |
| 4987 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 4988 | /// handler. |
| 4989 | Sema::DeclPtrTy Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4990 | TypeSourceInfo *TInfo = 0; |
| 4991 | QualType ExDeclType = GetTypeForDeclarator(D, S, &TInfo); |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4992 | |
| 4993 | bool Invalid = D.isInvalidType(); |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4994 | IdentifierInfo *II = D.getIdentifier(); |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 4995 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, D.getIdentifierLoc(), |
Douglas Gregor | b8eaf29 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 4996 | LookupOrdinaryName, |
| 4997 | ForRedeclaration)) { |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4998 | // The scope should be freshly made just for us. There is just no way |
| 4999 | // it contains any previous declaration. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5000 | assert(!S->isDeclScope(DeclPtrTy::make(PrevDecl))); |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5001 | if (PrevDecl->isTemplateParameter()) { |
| 5002 | // Maybe we will complain about the shadowed template parameter. |
| 5003 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5004 | } |
| 5005 | } |
| 5006 | |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5007 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5008 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 5009 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5010 | Invalid = true; |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5011 | } |
| 5012 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5013 | VarDecl *ExDecl = BuildExceptionDeclaration(S, ExDeclType, TInfo, |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5014 | D.getIdentifier(), |
| 5015 | D.getIdentifierLoc(), |
| 5016 | D.getDeclSpec().getSourceRange()); |
| 5017 | |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 5018 | if (Invalid) |
| 5019 | ExDecl->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5020 | |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5021 | // Add the exception declaration into this scope. |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5022 | if (II) |
Douglas Gregor | 5e16fbe | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 5023 | PushOnScopeChains(ExDecl, S); |
| 5024 | else |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5025 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5026 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5027 | ProcessDeclAttributes(S, ExDecl, D); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5028 | return DeclPtrTy::make(ExDecl); |
Sebastian Redl | 54c04d4 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 5029 | } |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5030 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5031 | Sema::DeclPtrTy Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5032 | ExprArg assertexpr, |
| 5033 | ExprArg assertmessageexpr) { |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5034 | Expr *AssertExpr = (Expr *)assertexpr.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5035 | StringLiteral *AssertMessage = |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5036 | cast<StringLiteral>((Expr *)assertmessageexpr.get()); |
| 5037 | |
Anders Carlsson | 54b2698 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5038 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 5039 | llvm::APSInt Value(32); |
| 5040 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 5041 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 5042 | AssertExpr->getSourceRange(); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5043 | return DeclPtrTy(); |
Anders Carlsson | 54b2698 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5044 | } |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5045 | |
Anders Carlsson | 54b2698 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5046 | if (Value == 0) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5047 | Diag(AssertLoc, diag::err_static_assert_failed) |
Benjamin Kramer | b11118b | 2009-12-11 13:33:18 +0000 | [diff] [blame] | 5048 | << AssertMessage->getString() << AssertExpr->getSourceRange(); |
Anders Carlsson | 54b2698 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 5049 | } |
| 5050 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5051 | |
Anders Carlsson | 78e2bc0 | 2009-03-15 17:35:16 +0000 | [diff] [blame] | 5052 | assertexpr.release(); |
| 5053 | assertmessageexpr.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5054 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5055 | AssertExpr, AssertMessage); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5056 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5057 | CurContext->addDecl(Decl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5058 | return DeclPtrTy::make(Decl); |
Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 5059 | } |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5060 | |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5061 | /// \brief Perform semantic analysis of the given friend type declaration. |
| 5062 | /// |
| 5063 | /// \returns A friend declaration that. |
| 5064 | FriendDecl *Sema::CheckFriendTypeDecl(SourceLocation FriendLoc, |
| 5065 | TypeSourceInfo *TSInfo) { |
| 5066 | assert(TSInfo && "NULL TypeSourceInfo for friend type declaration"); |
| 5067 | |
| 5068 | QualType T = TSInfo->getType(); |
| 5069 | SourceRange TypeRange = TSInfo->getTypeLoc().getSourceRange(); |
| 5070 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 5071 | if (!getLangOptions().CPlusPlus0x) { |
| 5072 | // C++03 [class.friend]p2: |
| 5073 | // An elaborated-type-specifier shall be used in a friend declaration |
| 5074 | // for a class.* |
| 5075 | // |
| 5076 | // * The class-key of the elaborated-type-specifier is required. |
| 5077 | if (!ActiveTemplateInstantiations.empty()) { |
| 5078 | // Do not complain about the form of friend template types during |
| 5079 | // template instantiation; we will already have complained when the |
| 5080 | // template was declared. |
| 5081 | } else if (!T->isElaboratedTypeSpecifier()) { |
| 5082 | // If we evaluated the type to a record type, suggest putting |
| 5083 | // a tag in front. |
| 5084 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 5085 | RecordDecl *RD = RT->getDecl(); |
| 5086 | |
| 5087 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 5088 | |
| 5089 | Diag(TypeRange.getBegin(), diag::ext_unelaborated_friend_type) |
| 5090 | << (unsigned) RD->getTagKind() |
| 5091 | << T |
| 5092 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(FriendLoc), |
| 5093 | InsertionText); |
| 5094 | } else { |
| 5095 | Diag(FriendLoc, diag::ext_nonclass_type_friend) |
| 5096 | << T |
| 5097 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
| 5098 | } |
| 5099 | } else if (T->getAs<EnumType>()) { |
| 5100 | Diag(FriendLoc, diag::ext_enum_friend) |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5101 | << T |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5102 | << SourceRange(FriendLoc, TypeRange.getEnd()); |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5103 | } |
| 5104 | } |
| 5105 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 5106 | // C++0x [class.friend]p3: |
| 5107 | // If the type specifier in a friend declaration designates a (possibly |
| 5108 | // cv-qualified) class type, that class is declared as a friend; otherwise, |
| 5109 | // the friend declaration is ignored. |
| 5110 | |
| 5111 | // FIXME: C++0x has some syntactic restrictions on friend type declarations |
| 5112 | // in [class.friend]p3 that we do not implement. |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5113 | |
| 5114 | return FriendDecl::Create(Context, CurContext, FriendLoc, TSInfo, FriendLoc); |
| 5115 | } |
| 5116 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5117 | /// Handle a friend type declaration. This works in tandem with |
| 5118 | /// ActOnTag. |
| 5119 | /// |
| 5120 | /// Notes on friend class templates: |
| 5121 | /// |
| 5122 | /// We generally treat friend class declarations as if they were |
| 5123 | /// declaring a class. So, for example, the elaborated type specifier |
| 5124 | /// in a friend declaration is required to obey the restrictions of a |
| 5125 | /// class-head (i.e. no typedefs in the scope chain), template |
| 5126 | /// parameters are required to match up with simple template-ids, &c. |
| 5127 | /// However, unlike when declaring a template specialization, it's |
| 5128 | /// okay to refer to a template specialization without an empty |
| 5129 | /// template parameter declaration, e.g. |
| 5130 | /// friend class A<T>::B<unsigned>; |
| 5131 | /// We permit this as a special case; if there are any template |
| 5132 | /// parameters present at all, require proper matching, i.e. |
| 5133 | /// template <> template <class T> friend class A<int>::B; |
Chris Lattner | 1fb66f4 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 5134 | Sema::DeclPtrTy Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5135 | MultiTemplateParamsArg TempParams) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5136 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5137 | |
| 5138 | assert(DS.isFriendSpecified()); |
| 5139 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 5140 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5141 | // Try to convert the decl specifier to a type. This works for |
| 5142 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 5143 | // for a TUK_Friend. |
Chris Lattner | 1fb66f4 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 5144 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5145 | TypeSourceInfo *TSI; |
| 5146 | QualType T = GetTypeForDeclarator(TheDeclarator, S, &TSI); |
Chris Lattner | 1fb66f4 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 5147 | if (TheDeclarator.isInvalidType()) |
| 5148 | return DeclPtrTy(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5149 | |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5150 | if (!TSI) |
| 5151 | TSI = Context.getTrivialTypeSourceInfo(T, DS.getSourceRange().getBegin()); |
| 5152 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5153 | // This is definitely an error in C++98. It's probably meant to |
| 5154 | // be forbidden in C++0x, too, but the specification is just |
| 5155 | // poorly written. |
| 5156 | // |
| 5157 | // The problem is with declarations like the following: |
| 5158 | // template <T> friend A<T>::foo; |
| 5159 | // where deciding whether a class C is a friend or not now hinges |
| 5160 | // on whether there exists an instantiation of A that causes |
| 5161 | // 'foo' to equal C. There are restrictions on class-heads |
| 5162 | // (which we declare (by fiat) elaborated friend declarations to |
| 5163 | // be) that makes this tractable. |
| 5164 | // |
| 5165 | // FIXME: handle "template <> friend class A<T>;", which |
| 5166 | // is possibly well-formed? Who even knows? |
Douglas Gregor | e677daf | 2010-03-31 22:19:08 +0000 | [diff] [blame] | 5167 | if (TempParams.size() && !T->isElaboratedTypeSpecifier()) { |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5168 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 5169 | << DS.getSourceRange(); |
| 5170 | return DeclPtrTy(); |
| 5171 | } |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5172 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5173 | // C++98 [class.friend]p1: A friend of a class is a function |
| 5174 | // or class that is not a member of the class . . . |
John McCall | 463e10c | 2009-12-22 00:59:39 +0000 | [diff] [blame] | 5175 | // This is fixed in DR77, which just barely didn't make the C++03 |
| 5176 | // deadline. It's also a very silly restriction that seriously |
| 5177 | // affects inner classes and which nobody else seems to implement; |
| 5178 | // thus we never diagnose it, not even in -pedantic. |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5179 | // |
| 5180 | // But note that we could warn about it: it's always useless to |
| 5181 | // friend one of your own members (it's not, however, worthless to |
| 5182 | // friend a member of an arbitrary specialization of your template). |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5183 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5184 | Decl *D; |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5185 | if (unsigned NumTempParamLists = TempParams.size()) |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5186 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5187 | NumTempParamLists, |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5188 | (TemplateParameterList**) TempParams.release(), |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5189 | TSI, |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5190 | DS.getFriendSpecLoc()); |
| 5191 | else |
Douglas Gregor | afb9bc1 | 2010-04-07 16:53:43 +0000 | [diff] [blame] | 5192 | D = CheckFriendTypeDecl(DS.getFriendSpecLoc(), TSI); |
| 5193 | |
| 5194 | if (!D) |
| 5195 | return DeclPtrTy(); |
| 5196 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5197 | D->setAccess(AS_public); |
| 5198 | CurContext->addDecl(D); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5199 | |
John McCall | 11083da | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 5200 | return DeclPtrTy::make(D); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
John McCall | 2f212b3 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 5203 | Sema::DeclPtrTy |
| 5204 | Sema::ActOnFriendFunctionDecl(Scope *S, |
| 5205 | Declarator &D, |
| 5206 | bool IsDefinition, |
| 5207 | MultiTemplateParamsArg TemplateParams) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5208 | const DeclSpec &DS = D.getDeclSpec(); |
| 5209 | |
| 5210 | assert(DS.isFriendSpecified()); |
| 5211 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 5212 | |
| 5213 | SourceLocation Loc = D.getIdentifierLoc(); |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5214 | TypeSourceInfo *TInfo = 0; |
| 5215 | QualType T = GetTypeForDeclarator(D, S, &TInfo); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5216 | |
| 5217 | // C++ [class.friend]p1 |
| 5218 | // A friend of a class is a function or class.... |
| 5219 | // Note that this sees through typedefs, which is intended. |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5220 | // It *doesn't* see through dependent types, which is correct |
| 5221 | // according to [temp.arg.type]p3: |
| 5222 | // If a declaration acquires a function type through a |
| 5223 | // type dependent on a template-parameter and this causes |
| 5224 | // a declaration that does not use the syntactic form of a |
| 5225 | // function declarator to have a function type, the program |
| 5226 | // is ill-formed. |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5227 | if (!T->isFunctionType()) { |
| 5228 | Diag(Loc, diag::err_unexpected_friend); |
| 5229 | |
| 5230 | // It might be worthwhile to try to recover by creating an |
| 5231 | // appropriate declaration. |
| 5232 | return DeclPtrTy(); |
| 5233 | } |
| 5234 | |
| 5235 | // C++ [namespace.memdef]p3 |
| 5236 | // - If a friend declaration in a non-local class first declares a |
| 5237 | // class or function, the friend class or function is a member |
| 5238 | // of the innermost enclosing namespace. |
| 5239 | // - The name of the friend is not found by simple name lookup |
| 5240 | // until a matching declaration is provided in that namespace |
| 5241 | // scope (either before or after the class declaration granting |
| 5242 | // friendship). |
| 5243 | // - If a friend function is called, its name may be found by the |
| 5244 | // name lookup that considers functions from namespaces and |
| 5245 | // classes associated with the types of the function arguments. |
| 5246 | // - When looking for a prior declaration of a class or a function |
| 5247 | // declared as a friend, scopes outside the innermost enclosing |
| 5248 | // namespace scope are not considered. |
| 5249 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5250 | CXXScopeSpec &ScopeQual = D.getCXXScopeSpec(); |
| 5251 | DeclarationName Name = GetNameForDeclarator(D); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5252 | assert(Name); |
| 5253 | |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5254 | // The context we found the declaration in, or in which we should |
| 5255 | // create the declaration. |
| 5256 | DeclContext *DC; |
| 5257 | |
| 5258 | // FIXME: handle local classes |
| 5259 | |
| 5260 | // Recover from invalid scope qualifiers as if they just weren't there. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5261 | LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName, |
| 5262 | ForRedeclaration); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5263 | if (!ScopeQual.isInvalid() && ScopeQual.isSet()) { |
| 5264 | DC = computeDeclContext(ScopeQual); |
| 5265 | |
| 5266 | // FIXME: handle dependent contexts |
| 5267 | if (!DC) return DeclPtrTy(); |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5268 | if (RequireCompleteDeclContext(ScopeQual, DC)) return DeclPtrTy(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5269 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5270 | LookupQualifiedName(Previous, DC); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5271 | |
| 5272 | // If searching in that context implicitly found a declaration in |
| 5273 | // a different context, treat it like it wasn't found at all. |
| 5274 | // TODO: better diagnostics for this case. Suggesting the right |
| 5275 | // qualified scope would be nice... |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5276 | // FIXME: getRepresentativeDecl() is not right here at all |
| 5277 | if (Previous.empty() || |
| 5278 | !Previous.getRepresentativeDecl()->getDeclContext()->Equals(DC)) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5279 | D.setInvalidType(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5280 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 5281 | return DeclPtrTy(); |
| 5282 | } |
| 5283 | |
| 5284 | // C++ [class.friend]p1: A friend of a class is a function or |
| 5285 | // class that is not a member of the class . . . |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5286 | if (DC->Equals(CurContext)) |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5287 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 5288 | |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5289 | // Otherwise walk out to the nearest namespace scope looking for matches. |
| 5290 | } else { |
| 5291 | // TODO: handle local class contexts. |
| 5292 | |
| 5293 | DC = CurContext; |
| 5294 | while (true) { |
| 5295 | // Skip class contexts. If someone can cite chapter and verse |
| 5296 | // for this behavior, that would be nice --- it's what GCC and |
| 5297 | // EDG do, and it seems like a reasonable intent, but the spec |
| 5298 | // really only says that checks for unqualified existing |
| 5299 | // declarations should stop at the nearest enclosing namespace, |
| 5300 | // not that they should only consider the nearest enclosing |
| 5301 | // namespace. |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5302 | while (DC->isRecord()) |
| 5303 | DC = DC->getParent(); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5304 | |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5305 | LookupQualifiedName(Previous, DC); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5306 | |
| 5307 | // TODO: decide what we think about using declarations. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5308 | if (!Previous.empty()) |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5309 | break; |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5310 | |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5311 | if (DC->isFileContext()) break; |
| 5312 | DC = DC->getParent(); |
| 5313 | } |
| 5314 | |
| 5315 | // C++ [class.friend]p1: A friend of a class is a function or |
| 5316 | // class that is not a member of the class . . . |
John McCall | 93343b9 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 5317 | // C++0x changes this for both friend types and functions. |
| 5318 | // Most C++ 98 compilers do seem to give an error here, so |
| 5319 | // we do, too. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5320 | if (!Previous.empty() && DC->Equals(CurContext) |
| 5321 | && !getLangOptions().CPlusPlus0x) |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5322 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 5323 | } |
| 5324 | |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5325 | if (DC->isFileContext()) { |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5326 | // This implies that it has to be an operator or function. |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5327 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 5328 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 5329 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5330 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5331 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 5332 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5333 | return DeclPtrTy(); |
| 5334 | } |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5335 | } |
| 5336 | |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5337 | bool Redeclaration = false; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5338 | NamedDecl *ND = ActOnFunctionDeclarator(S, D, DC, T, TInfo, Previous, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5339 | move(TemplateParams), |
John McCall | d1e9d83 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 5340 | IsDefinition, |
| 5341 | Redeclaration); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5342 | if (!ND) return DeclPtrTy(); |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5343 | |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5344 | assert(ND->getDeclContext() == DC); |
| 5345 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 5ed6e8f | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 5346 | |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5347 | // Add the function declaration to the appropriate lookup tables, |
| 5348 | // adjusting the redeclarations list as necessary. We don't |
| 5349 | // want to do this yet if the friending class is dependent. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5350 | // |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5351 | // Also update the scope-based lookup if the target context's |
| 5352 | // lookup context is in lexical scope. |
| 5353 | if (!CurContext->isDependentContext()) { |
| 5354 | DC = DC->getLookupContext(); |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5355 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5356 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5357 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | 759e32b | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 5358 | } |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5359 | |
| 5360 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5361 | D.getIdentifierLoc(), ND, |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5362 | DS.getFriendSpecLoc()); |
John McCall | 75c03bb | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 5363 | FrD->setAccess(AS_public); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5364 | CurContext->addDecl(FrD); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 5365 | |
Douglas Gregor | a29a3ff | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 5366 | return DeclPtrTy::make(ND); |
Anders Carlsson | 3881170 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 5367 | } |
| 5368 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5369 | void Sema::SetDeclDeleted(DeclPtrTy dcl, SourceLocation DelLoc) { |
Douglas Gregor | c8c277a | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 5370 | AdjustDeclIfTemplate(dcl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5371 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5372 | Decl *Dcl = dcl.getAs<Decl>(); |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5373 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 5374 | if (!Fn) { |
| 5375 | Diag(DelLoc, diag::err_deleted_non_function); |
| 5376 | return; |
| 5377 | } |
| 5378 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 5379 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 5380 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 5381 | // If the declaration wasn't the first, we delete the function anyway for |
| 5382 | // recovery. |
| 5383 | } |
| 5384 | Fn->setDeleted(); |
| 5385 | } |
Sebastian Redl | 4c01866 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 5386 | |
| 5387 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 5388 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 5389 | ++CI) { |
| 5390 | Stmt *SubStmt = *CI; |
| 5391 | if (!SubStmt) |
| 5392 | continue; |
| 5393 | if (isa<ReturnStmt>(SubStmt)) |
| 5394 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 5395 | diag::err_return_in_constructor_handler); |
| 5396 | if (!isa<Expr>(SubStmt)) |
| 5397 | SearchForReturnInStmt(Self, SubStmt); |
| 5398 | } |
| 5399 | } |
| 5400 | |
| 5401 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 5402 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 5403 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 5404 | SearchForReturnInStmt(*this, Handler); |
| 5405 | } |
| 5406 | } |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5407 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5408 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5409 | const CXXMethodDecl *Old) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5410 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 5411 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5412 | |
Chandler Carruth | 284bb2e | 2010-02-15 11:53:20 +0000 | [diff] [blame] | 5413 | if (Context.hasSameType(NewTy, OldTy) || |
| 5414 | NewTy->isDependentType() || OldTy->isDependentType()) |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5415 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5416 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5417 | // Check if the return types are covariant |
| 5418 | QualType NewClassTy, OldClassTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5419 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5420 | /// Both types must be pointers or references to classes. |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 5421 | if (const PointerType *NewPT = NewTy->getAs<PointerType>()) { |
| 5422 | if (const PointerType *OldPT = OldTy->getAs<PointerType>()) { |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5423 | NewClassTy = NewPT->getPointeeType(); |
| 5424 | OldClassTy = OldPT->getPointeeType(); |
| 5425 | } |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 5426 | } else if (const ReferenceType *NewRT = NewTy->getAs<ReferenceType>()) { |
| 5427 | if (const ReferenceType *OldRT = OldTy->getAs<ReferenceType>()) { |
| 5428 | if (NewRT->getTypeClass() == OldRT->getTypeClass()) { |
| 5429 | NewClassTy = NewRT->getPointeeType(); |
| 5430 | OldClassTy = OldRT->getPointeeType(); |
| 5431 | } |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5432 | } |
| 5433 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5434 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5435 | // The return types aren't either both pointers or references to a class type. |
| 5436 | if (NewClassTy.isNull()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5437 | Diag(New->getLocation(), |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5438 | diag::err_different_return_type_for_overriding_virtual_function) |
| 5439 | << New->getDeclName() << NewTy << OldTy; |
| 5440 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5441 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5442 | return true; |
| 5443 | } |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5444 | |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 5445 | // C++ [class.virtual]p6: |
| 5446 | // If the return type of D::f differs from the return type of B::f, the |
| 5447 | // class type in the return type of D::f shall be complete at the point of |
| 5448 | // declaration of D::f or shall be the class type D. |
Anders Carlsson | 0c9dd84 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 5449 | if (const RecordType *RT = NewClassTy->getAs<RecordType>()) { |
| 5450 | if (!RT->isBeingDefined() && |
| 5451 | RequireCompleteType(New->getLocation(), NewClassTy, |
| 5452 | PDiag(diag::err_covariant_return_incomplete) |
| 5453 | << New->getDeclName())) |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 5454 | return true; |
Anders Carlsson | 0c9dd84 | 2009-12-31 18:54:35 +0000 | [diff] [blame] | 5455 | } |
Anders Carlsson | e60365b | 2009-12-31 18:34:24 +0000 | [diff] [blame] | 5456 | |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5457 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5458 | // Check if the new class derives from the old class. |
| 5459 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 5460 | Diag(New->getLocation(), |
| 5461 | diag::err_covariant_return_not_derived) |
| 5462 | << New->getDeclName() << NewTy << OldTy; |
| 5463 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5464 | return true; |
| 5465 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5466 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5467 | // Check if we the conversion from derived to base is valid. |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 5468 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | 7afe424 | 2010-04-24 17:11:09 +0000 | [diff] [blame] | 5469 | diag::err_covariant_return_inaccessible_base, |
| 5470 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 5471 | // FIXME: Should this point to the return type? |
| 5472 | New->getLocation(), SourceRange(), New->getDeclName(), 0)) { |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5473 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5474 | return true; |
| 5475 | } |
| 5476 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5477 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5478 | // The qualifiers of the return types must be the same. |
Anders Carlsson | 7caa4cb | 2010-01-22 17:37:20 +0000 | [diff] [blame] | 5479 | if (NewTy.getLocalCVRQualifiers() != OldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5480 | Diag(New->getLocation(), |
| 5481 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5482 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5483 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5484 | return true; |
| 5485 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5486 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5487 | |
| 5488 | // The new class type must have the same or less qualifiers as the old type. |
| 5489 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 5490 | Diag(New->getLocation(), |
| 5491 | diag::err_covariant_return_type_class_type_more_qualified) |
| 5492 | << New->getDeclName() << NewTy << OldTy; |
| 5493 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5494 | return true; |
| 5495 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5496 | |
Anders Carlsson | 8fb0b8a | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 5497 | return false; |
Anders Carlsson | f2a2e33 | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 5498 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5499 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5500 | bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New, |
| 5501 | const CXXMethodDecl *Old) |
| 5502 | { |
| 5503 | if (Old->hasAttr<FinalAttr>()) { |
| 5504 | Diag(New->getLocation(), diag::err_final_function_overridden) |
| 5505 | << New->getDeclName(); |
| 5506 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 5507 | return true; |
| 5508 | } |
| 5509 | |
| 5510 | return false; |
| 5511 | } |
| 5512 | |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 5513 | /// \brief Mark the given method pure. |
| 5514 | /// |
| 5515 | /// \param Method the method to be marked pure. |
| 5516 | /// |
| 5517 | /// \param InitRange the source range that covers the "0" initializer. |
| 5518 | bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) { |
| 5519 | if (Method->isVirtual() || Method->getParent()->isDependentContext()) { |
| 5520 | Method->setPure(); |
| 5521 | |
| 5522 | // A class is abstract if at least one function is pure virtual. |
| 5523 | Method->getParent()->setAbstract(true); |
| 5524 | return false; |
| 5525 | } |
| 5526 | |
| 5527 | if (!Method->isInvalidDecl()) |
| 5528 | Diag(Method->getLocation(), diag::err_non_virtual_pure) |
| 5529 | << Method->getDeclName() << InitRange; |
| 5530 | return true; |
| 5531 | } |
| 5532 | |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5533 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse |
| 5534 | /// an initializer for the out-of-line declaration 'Dcl'. The scope |
| 5535 | /// is a fresh scope pushed for just this purpose. |
| 5536 | /// |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5537 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 5538 | /// static data member of class X, names should be looked up in the scope of |
| 5539 | /// class X. |
| 5540 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5541 | // If there is no declaration, there was an error parsing it. |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5542 | Decl *D = Dcl.getAs<Decl>(); |
| 5543 | if (D == 0) return; |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5544 | |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5545 | // We should only get called for declarations with scope specifiers, like: |
| 5546 | // int foo::bar; |
| 5547 | assert(D->isOutOfLine()); |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5548 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5552 | /// initializer for the out-of-line declaration 'Dcl'. |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5553 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5554 | // If there is no declaration, there was an error parsing it. |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5555 | Decl *D = Dcl.getAs<Decl>(); |
| 5556 | if (D == 0) return; |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5557 | |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 5558 | assert(D->isOutOfLine()); |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 5559 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 5560 | } |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 5561 | |
| 5562 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 5563 | /// C++ if/switch/while/for statement. |
| 5564 | /// e.g: "if (int x = f()) {...}" |
| 5565 | Action::DeclResult |
| 5566 | Sema::ActOnCXXConditionDeclaration(Scope *S, Declarator &D) { |
| 5567 | // C++ 6.4p2: |
| 5568 | // The declarator shall not specify a function or an array. |
| 5569 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 5570 | // new class or enumeration. |
| 5571 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 5572 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 5573 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5574 | TypeSourceInfo *TInfo = 0; |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 5575 | TagDecl *OwnedTag = 0; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5576 | QualType Ty = GetTypeForDeclarator(D, S, &TInfo, &OwnedTag); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 5577 | |
| 5578 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 5579 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 5580 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 5581 | Diag(D.getIdentifierLoc(), diag::err_invalid_use_of_function_type) |
| 5582 | << D.getSourceRange(); |
| 5583 | return DeclResult(); |
| 5584 | } else if (OwnedTag && OwnedTag->isDefinition()) { |
| 5585 | // The type-specifier-seq shall not declare a new class or enumeration. |
| 5586 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_condition); |
| 5587 | } |
| 5588 | |
| 5589 | DeclPtrTy Dcl = ActOnDeclarator(S, D); |
| 5590 | if (!Dcl) |
| 5591 | return DeclResult(); |
| 5592 | |
| 5593 | VarDecl *VD = cast<VarDecl>(Dcl.getAs<Decl>()); |
| 5594 | VD->setDeclaredInCondition(true); |
| 5595 | return Dcl; |
| 5596 | } |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5597 | |
Anders Carlsson | 11e5140 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 5598 | static bool needsVTable(CXXMethodDecl *MD, ASTContext &Context) { |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5599 | // Ignore dependent types. |
| 5600 | if (MD->isDependentContext()) |
Rafael Espindola | 70e040d | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5601 | return false; |
Anders Carlsson | 5ebf8b4 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 5602 | |
Douglas Gregor | ccecc1b | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 5603 | // Ignore declarations that are not definitions. |
| 5604 | if (!MD->isThisDeclarationADefinition()) |
Rafael Espindola | 70e040d | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5605 | return false; |
| 5606 | |
| 5607 | CXXRecordDecl *RD = MD->getParent(); |
| 5608 | |
| 5609 | // Ignore classes without a vtable. |
| 5610 | if (!RD->isDynamicClass()) |
| 5611 | return false; |
| 5612 | |
| 5613 | switch (MD->getParent()->getTemplateSpecializationKind()) { |
| 5614 | case TSK_Undeclared: |
| 5615 | case TSK_ExplicitSpecialization: |
| 5616 | // Classes that aren't instantiations of templates don't need their |
| 5617 | // virtual methods marked until we see the definition of the key |
| 5618 | // function. |
| 5619 | break; |
| 5620 | |
| 5621 | case TSK_ImplicitInstantiation: |
| 5622 | // This is a constructor of a class template; mark all of the virtual |
| 5623 | // members as referenced to ensure that they get instantiatied. |
| 5624 | if (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)) |
| 5625 | return true; |
| 5626 | break; |
| 5627 | |
| 5628 | case TSK_ExplicitInstantiationDeclaration: |
Rafael Espindola | 8d04f06 | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5629 | return false; |
Rafael Espindola | 70e040d | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5630 | |
| 5631 | case TSK_ExplicitInstantiationDefinition: |
| 5632 | // This is method of a explicit instantiation; mark all of the virtual |
| 5633 | // members as referenced to ensure that they get instantiatied. |
| 5634 | return true; |
Douglas Gregor | ccecc1b | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 5635 | } |
Rafael Espindola | 70e040d | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5636 | |
| 5637 | // Consider only out-of-line definitions of member functions. When we see |
| 5638 | // an inline definition, it's too early to compute the key function. |
| 5639 | if (!MD->isOutOfLine()) |
| 5640 | return false; |
| 5641 | |
| 5642 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(RD); |
| 5643 | |
| 5644 | // If there is no key function, we will need a copy of the vtable. |
| 5645 | if (!KeyFunction) |
| 5646 | return true; |
| 5647 | |
| 5648 | // If this is the key function, we need to mark virtual members. |
| 5649 | if (KeyFunction->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 5650 | return true; |
| 5651 | |
| 5652 | return false; |
| 5653 | } |
| 5654 | |
| 5655 | void Sema::MaybeMarkVirtualMembersReferenced(SourceLocation Loc, |
| 5656 | CXXMethodDecl *MD) { |
| 5657 | CXXRecordDecl *RD = MD->getParent(); |
| 5658 | |
Douglas Gregor | 0a0f04d | 2010-01-06 04:44:19 +0000 | [diff] [blame] | 5659 | // We will need to mark all of the virtual members as referenced to build the |
| 5660 | // vtable. |
Anders Carlsson | 11e5140 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 5661 | if (!needsVTable(MD, Context)) |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 5662 | return; |
| 5663 | |
| 5664 | TemplateSpecializationKind kind = RD->getTemplateSpecializationKind(); |
| 5665 | if (kind == TSK_ImplicitInstantiation) |
| 5666 | ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(RD, Loc)); |
| 5667 | else |
Rafael Espindola | 70e040d | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 5668 | MarkVirtualMembersReferenced(Loc, RD); |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5669 | } |
| 5670 | |
| 5671 | bool Sema::ProcessPendingClassesWithUnmarkedVirtualMembers() { |
| 5672 | if (ClassesWithUnmarkedVirtualMembers.empty()) |
| 5673 | return false; |
| 5674 | |
Douglas Gregor | 0a0f04d | 2010-01-06 04:44:19 +0000 | [diff] [blame] | 5675 | while (!ClassesWithUnmarkedVirtualMembers.empty()) { |
| 5676 | CXXRecordDecl *RD = ClassesWithUnmarkedVirtualMembers.back().first; |
| 5677 | SourceLocation Loc = ClassesWithUnmarkedVirtualMembers.back().second; |
| 5678 | ClassesWithUnmarkedVirtualMembers.pop_back(); |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5679 | MarkVirtualMembersReferenced(Loc, RD); |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5680 | } |
| 5681 | |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5682 | return true; |
Anders Carlsson | f98849e | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 5683 | } |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5684 | |
Rafael Espindola | 5b33408 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 5685 | void Sema::MarkVirtualMembersReferenced(SourceLocation Loc, |
| 5686 | const CXXRecordDecl *RD) { |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5687 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 5688 | e = RD->method_end(); i != e; ++i) { |
| 5689 | CXXMethodDecl *MD = *i; |
| 5690 | |
| 5691 | // C++ [basic.def.odr]p2: |
| 5692 | // [...] A virtual member function is used if it is not pure. [...] |
| 5693 | if (MD->isVirtual() && !MD->isPure()) |
| 5694 | MarkDeclarationReferenced(Loc, MD); |
| 5695 | } |
Rafael Espindola | 5b33408 | 2010-03-26 00:36:59 +0000 | [diff] [blame] | 5696 | |
| 5697 | // Only classes that have virtual bases need a VTT. |
| 5698 | if (RD->getNumVBases() == 0) |
| 5699 | return; |
| 5700 | |
| 5701 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 5702 | e = RD->bases_end(); i != e; ++i) { |
| 5703 | const CXXRecordDecl *Base = |
| 5704 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 5705 | if (i->isVirtual()) |
| 5706 | continue; |
| 5707 | if (Base->getNumVBases() == 0) |
| 5708 | continue; |
| 5709 | MarkVirtualMembersReferenced(Loc, Base); |
| 5710 | } |
Anders Carlsson | 82fccd0 | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 5711 | } |
Fariborz Jahanian | c83726e | 2010-04-28 16:11:27 +0000 | [diff] [blame] | 5712 | |
| 5713 | /// SetIvarInitializers - This routine builds initialization ASTs for the |
| 5714 | /// Objective-C implementation whose ivars need be initialized. |
| 5715 | void Sema::SetIvarInitializers(ObjCImplementationDecl *ObjCImplementation) { |
| 5716 | if (!getLangOptions().CPlusPlus) |
| 5717 | return; |
| 5718 | if (const ObjCInterfaceDecl *OID = ObjCImplementation->getClassInterface()) { |
| 5719 | llvm::SmallVector<ObjCIvarDecl*, 8> ivars; |
| 5720 | CollectIvarsToConstructOrDestruct(OID, ivars); |
| 5721 | if (ivars.empty()) |
| 5722 | return; |
| 5723 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
| 5724 | for (unsigned i = 0; i < ivars.size(); i++) { |
| 5725 | FieldDecl *Field = ivars[i]; |
| 5726 | CXXBaseOrMemberInitializer *Member; |
| 5727 | InitializedEntity InitEntity = InitializedEntity::InitializeMember(Field); |
| 5728 | InitializationKind InitKind = |
| 5729 | InitializationKind::CreateDefault(ObjCImplementation->getLocation()); |
| 5730 | |
| 5731 | InitializationSequence InitSeq(*this, InitEntity, InitKind, 0, 0); |
| 5732 | Sema::OwningExprResult MemberInit = |
| 5733 | InitSeq.Perform(*this, InitEntity, InitKind, |
| 5734 | Sema::MultiExprArg(*this, 0, 0)); |
| 5735 | MemberInit = MaybeCreateCXXExprWithTemporaries(move(MemberInit)); |
| 5736 | // Note, MemberInit could actually come back empty if no initialization |
| 5737 | // is required (e.g., because it would call a trivial default constructor) |
| 5738 | if (!MemberInit.get() || MemberInit.isInvalid()) |
| 5739 | continue; |
| 5740 | |
| 5741 | Member = |
| 5742 | new (Context) CXXBaseOrMemberInitializer(Context, |
| 5743 | Field, SourceLocation(), |
| 5744 | SourceLocation(), |
| 5745 | MemberInit.takeAs<Expr>(), |
| 5746 | SourceLocation()); |
| 5747 | AllToInit.push_back(Member); |
| 5748 | } |
| 5749 | ObjCImplementation->setIvarInitializers(Context, |
| 5750 | AllToInit.data(), AllToInit.size()); |
| 5751 | } |
| 5752 | } |