Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 1 | //===------ SemaDeclCXX.cpp - Semantic Analysis for C++ Declarations ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeOrdering.h" |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 21 | #include "clang/Parse/DeclSpec.h" |
| 22 | #include "clang/Parse/Template.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 24 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 27 | #include <algorithm> // for std::equal |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 28 | #include <map> |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 29 | #include <set> |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
| 32 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | // CheckDefaultArgumentVisitor |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 39 | /// the default argument of a parameter to determine whether it |
| 40 | /// contains any ill-formed subexpressions. For example, this will |
| 41 | /// diagnose the use of local variables or parameters within the |
| 42 | /// default argument expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | class VISIBILITY_HIDDEN CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 44 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 45 | Expr *DefaultArg; |
| 46 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 48 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 50 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 52 | bool VisitExpr(Expr *Node); |
| 53 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 54 | bool VisitCXXThisExpr(CXXThisExpr *ThisE); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 55 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 57 | /// VisitExpr - Visit all of the children of this expression. |
| 58 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 59 | bool IsInvalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | for (Stmt::child_iterator I = Node->child_begin(), |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 61 | E = Node->child_end(); I != E; ++I) |
| 62 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 63 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 66 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 67 | /// determine whether this declaration can be used in the default |
| 68 | /// argument expression. |
| 69 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 70 | NamedDecl *Decl = DRE->getDecl(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 71 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 72 | // C++ [dcl.fct.default]p9 |
| 73 | // Default arguments are evaluated each time the function is |
| 74 | // called. The order of evaluation of function arguments is |
| 75 | // unspecified. Consequently, parameters of a function shall not |
| 76 | // be used in default argument expressions, even if they are not |
| 77 | // evaluated. Parameters of a function declared before a default |
| 78 | // argument expression are in scope and can hide namespace and |
| 79 | // class member names. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 81 | diag::err_param_default_argument_references_param) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 82 | << Param->getDeclName() << DefaultArg->getSourceRange(); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 83 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 84 | // C++ [dcl.fct.default]p7 |
| 85 | // Local variables shall not be used in default argument |
| 86 | // expressions. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 87 | if (VDecl->isBlockVarDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | return S->Diag(DRE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 89 | diag::err_param_default_argument_references_local) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 90 | << VDecl->getDeclName() << DefaultArg->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 91 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 92 | |
Douglas Gregor | 3996f23 | 2008-11-04 13:41:56 +0000 | [diff] [blame] | 93 | return false; |
| 94 | } |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 95 | |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 96 | /// VisitCXXThisExpr - Visit a C++ "this" expression. |
| 97 | bool CheckDefaultArgumentVisitor::VisitCXXThisExpr(CXXThisExpr *ThisE) { |
| 98 | // C++ [dcl.fct.default]p8: |
| 99 | // The keyword this shall not be used in a default argument of a |
| 100 | // member function. |
| 101 | return S->Diag(ThisE->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 102 | diag::err_param_default_argument_references_this) |
| 103 | << ThisE->getSourceRange(); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 104 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 107 | bool |
| 108 | Sema::SetParamDefaultArgument(ParmVarDecl *Param, ExprArg DefaultArg, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | SourceLocation EqualLoc) { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 110 | QualType ParamType = Param->getType(); |
| 111 | |
Anders Carlsson | 5653ca5 | 2009-08-25 13:46:13 +0000 | [diff] [blame] | 112 | if (RequireCompleteType(Param->getLocation(), Param->getType(), |
| 113 | diag::err_typecheck_decl_incomplete_type)) { |
| 114 | Param->setInvalidDecl(); |
| 115 | return true; |
| 116 | } |
| 117 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 118 | Expr *Arg = (Expr *)DefaultArg.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 120 | // C++ [dcl.fct.default]p5 |
| 121 | // A default argument expression is implicitly converted (clause |
| 122 | // 4) to the parameter type. The default argument expression has |
| 123 | // the same semantic constraints as the initializer expression in |
| 124 | // a declaration of a variable of the parameter type, using the |
| 125 | // copy-initialization semantics (8.5). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | if (CheckInitializerTypes(Arg, ParamType, EqualLoc, |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 127 | Param->getDeclName(), /*DirectInit=*/false)) |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 128 | return true; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 129 | |
| 130 | Arg = MaybeCreateCXXExprWithTemporaries(Arg, /*DestroyTemps=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 132 | // Okay: add the default argument to the parameter |
| 133 | Param->setDefaultArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 135 | DefaultArg.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 137 | return false; |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 140 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 141 | /// provided for a function parameter is well-formed. If so, attach it |
| 142 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 143 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | Sema::ActOnParamDefaultArgument(DeclPtrTy param, SourceLocation EqualLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 145 | ExprArg defarg) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 146 | if (!param || !defarg.get()) |
| 147 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 149 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 150 | UnparsedDefaultArgLocs.erase(Param); |
| 151 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 152 | ExprOwningPtr<Expr> DefaultArg(this, defarg.takeAs<Expr>()); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 153 | QualType ParamType = Param->getType(); |
| 154 | |
| 155 | // Default arguments are only permitted in C++ |
| 156 | if (!getLangOptions().CPlusPlus) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 157 | Diag(EqualLoc, diag::err_param_default_argument) |
| 158 | << DefaultArg->getSourceRange(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 159 | Param->setInvalidDecl(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
Anders Carlsson | 66e3067 | 2009-08-25 01:02:06 +0000 | [diff] [blame] | 163 | // Check that the default argument is well-formed |
| 164 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); |
| 165 | if (DefaultArgChecker.Visit(DefaultArg.get())) { |
| 166 | Param->setInvalidDecl(); |
| 167 | return; |
| 168 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 170 | SetParamDefaultArgument(Param, move(DefaultArg), EqualLoc); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 173 | /// ActOnParamUnparsedDefaultArgument - We've seen a default |
| 174 | /// argument for a function parameter, but we can't parse it yet |
| 175 | /// because we're inside a class definition. Note that this default |
| 176 | /// argument will be parsed later. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | void Sema::ActOnParamUnparsedDefaultArgument(DeclPtrTy param, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 178 | SourceLocation EqualLoc, |
| 179 | SourceLocation ArgLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 180 | if (!param) |
| 181 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 183 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 184 | if (Param) |
| 185 | Param->setUnparsedDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 187 | UnparsedDefaultArgLocs[Param] = ArgLoc; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 190 | /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of |
| 191 | /// the default argument for the parameter param failed. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 192 | void Sema::ActOnParamDefaultArgumentError(DeclPtrTy param) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 193 | if (!param) |
| 194 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 196 | ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 198 | Param->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 200 | UnparsedDefaultArgLocs.erase(Param); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 203 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 204 | /// arguments in the declarator, which is not a function declaration |
| 205 | /// or definition and therefore is not permitted to have default |
| 206 | /// arguments. This routine should be invoked for every declarator |
| 207 | /// that is not a function declaration or definition. |
| 208 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 209 | // C++ [dcl.fct.default]p3 |
| 210 | // A default argument expression shall be specified only in the |
| 211 | // parameter-declaration-clause of a function declaration or in a |
| 212 | // template-parameter (14.1). It shall not be specified for a |
| 213 | // parameter pack. If it is specified in a |
| 214 | // parameter-declaration-clause, it shall not occur within a |
| 215 | // declarator or abstract-declarator of a parameter-declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 216 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 217 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 218 | if (chunk.Kind == DeclaratorChunk::Function) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 219 | for (unsigned argIdx = 0, e = chunk.Fun.NumArgs; argIdx != e; ++argIdx) { |
| 220 | ParmVarDecl *Param = |
| 221 | cast<ParmVarDecl>(chunk.Fun.ArgInfo[argIdx].Param.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 222 | if (Param->hasUnparsedDefaultArg()) { |
| 223 | CachedTokens *Toks = chunk.Fun.ArgInfo[argIdx].DefaultArgTokens; |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 224 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 225 | << SourceRange((*Toks)[1].getLocation(), Toks->back().getLocation()); |
| 226 | delete Toks; |
| 227 | chunk.Fun.ArgInfo[argIdx].DefaultArgTokens = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 228 | } else if (Param->getDefaultArg()) { |
| 229 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc) |
| 230 | << Param->getDefaultArg()->getSourceRange(); |
| 231 | Param->setDefaultArg(0); |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 238 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 239 | // function, once we already know that they have the same |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 240 | // type. Subroutine of MergeFunctionDecl. Returns true if there was an |
| 241 | // error, false otherwise. |
| 242 | bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 243 | bool Invalid = false; |
| 244 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 245 | // C++ [dcl.fct.default]p4: |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 246 | // For non-template functions, default arguments can be added in |
| 247 | // later declarations of a function in the same |
| 248 | // scope. Declarations in different scopes have completely |
| 249 | // distinct sets of default arguments. That is, declarations in |
| 250 | // inner scopes do not acquire default arguments from |
| 251 | // declarations in outer scopes, and vice versa. In a given |
| 252 | // function declaration, all parameters subsequent to a |
| 253 | // parameter with a default argument shall have default |
| 254 | // arguments supplied in this or previous declarations. A |
| 255 | // default argument shall not be redefined by a later |
| 256 | // declaration (not even to the same value). |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 257 | // |
| 258 | // C++ [dcl.fct.default]p6: |
| 259 | // Except for member functions of class templates, the default arguments |
| 260 | // in a member function definition that appears outside of the class |
| 261 | // definition are added to the set of default arguments provided by the |
| 262 | // member function declaration in the class definition. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 263 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 264 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 265 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 266 | |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 267 | if (OldParam->hasDefaultArg() && NewParam->hasDefaultArg()) { |
Anders Carlsson | ad26b73 | 2009-11-10 03:24:44 +0000 | [diff] [blame] | 268 | // FIXME: If the parameter doesn't have an identifier then the location |
| 269 | // points to the '=' which means that the fixit hint won't remove any |
| 270 | // extra spaces between the type and the '='. |
| 271 | SourceLocation Begin = NewParam->getLocation(); |
Anders Carlsson | 4881b99 | 2009-11-10 03:32:44 +0000 | [diff] [blame] | 272 | if (NewParam->getIdentifier()) |
| 273 | Begin = PP.getLocForEndOfToken(Begin); |
Anders Carlsson | ad26b73 | 2009-11-10 03:24:44 +0000 | [diff] [blame] | 274 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | Diag(NewParam->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 276 | diag::err_param_default_argument_redefinition) |
Anders Carlsson | ad26b73 | 2009-11-10 03:24:44 +0000 | [diff] [blame] | 277 | << NewParam->getDefaultArgRange() |
| 278 | << CodeModificationHint::CreateRemoval(SourceRange(Begin, |
| 279 | NewParam->getLocEnd())); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 280 | |
| 281 | // Look for the function declaration where the default argument was |
| 282 | // actually written, which may be a declaration prior to Old. |
| 283 | for (FunctionDecl *Older = Old->getPreviousDeclaration(); |
| 284 | Older; Older = Older->getPreviousDeclaration()) { |
| 285 | if (!Older->getParamDecl(p)->hasDefaultArg()) |
| 286 | break; |
| 287 | |
| 288 | OldParam = Older->getParamDecl(p); |
| 289 | } |
| 290 | |
| 291 | Diag(OldParam->getLocation(), diag::note_previous_definition) |
| 292 | << OldParam->getDefaultArgRange(); |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 293 | Invalid = true; |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 294 | } else if (OldParam->hasDefaultArg()) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 295 | // Merge the old default argument into the new parameter |
Douglas Gregor | d85cef5 | 2009-09-17 19:51:30 +0000 | [diff] [blame] | 296 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 297 | NewParam->setUninstantiatedDefaultArg( |
| 298 | OldParam->getUninstantiatedDefaultArg()); |
| 299 | else |
| 300 | NewParam->setDefaultArg(OldParam->getDefaultArg()); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 301 | } else if (NewParam->hasDefaultArg()) { |
| 302 | if (New->getDescribedFunctionTemplate()) { |
| 303 | // Paragraph 4, quoted above, only applies to non-template functions. |
| 304 | Diag(NewParam->getLocation(), |
| 305 | diag::err_param_default_argument_template_redecl) |
| 306 | << NewParam->getDefaultArgRange(); |
| 307 | Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 308 | << false; |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 309 | } else if (New->getTemplateSpecializationKind() |
| 310 | != TSK_ImplicitInstantiation && |
| 311 | New->getTemplateSpecializationKind() != TSK_Undeclared) { |
| 312 | // C++ [temp.expr.spec]p21: |
| 313 | // Default function arguments shall not be specified in a declaration |
| 314 | // or a definition for one of the following explicit specializations: |
| 315 | // - the explicit specialization of a function template; |
Douglas Gregor | 8c638ab | 2009-10-13 23:52:38 +0000 | [diff] [blame] | 316 | // - the explicit specialization of a member function template; |
| 317 | // - the explicit specialization of a member function of a class |
Douglas Gregor | 096ebfd | 2009-10-13 17:02:54 +0000 | [diff] [blame] | 318 | // template where the class template specialization to which the |
| 319 | // member function specialization belongs is implicitly |
| 320 | // instantiated. |
| 321 | Diag(NewParam->getLocation(), diag::err_template_spec_default_arg) |
| 322 | << (New->getTemplateSpecializationKind() ==TSK_ExplicitSpecialization) |
| 323 | << New->getDeclName() |
| 324 | << NewParam->getDefaultArgRange(); |
Douglas Gregor | 6cc1518 | 2009-09-11 18:44:32 +0000 | [diff] [blame] | 325 | } else if (New->getDeclContext()->isDependentContext()) { |
| 326 | // C++ [dcl.fct.default]p6 (DR217): |
| 327 | // Default arguments for a member function of a class template shall |
| 328 | // be specified on the initial declaration of the member function |
| 329 | // within the class template. |
| 330 | // |
| 331 | // Reading the tea leaves a bit in DR217 and its reference to DR205 |
| 332 | // leads me to the conclusion that one cannot add default function |
| 333 | // arguments for an out-of-line definition of a member function of a |
| 334 | // dependent type. |
| 335 | int WhichKind = 2; |
| 336 | if (CXXRecordDecl *Record |
| 337 | = dyn_cast<CXXRecordDecl>(New->getDeclContext())) { |
| 338 | if (Record->getDescribedClassTemplate()) |
| 339 | WhichKind = 0; |
| 340 | else if (isa<ClassTemplatePartialSpecializationDecl>(Record)) |
| 341 | WhichKind = 1; |
| 342 | else |
| 343 | WhichKind = 2; |
| 344 | } |
| 345 | |
| 346 | Diag(NewParam->getLocation(), |
| 347 | diag::err_param_default_argument_member_template_redecl) |
| 348 | << WhichKind |
| 349 | << NewParam->getDefaultArgRange(); |
| 350 | } |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 354 | if (CheckEquivalentExceptionSpec( |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 355 | Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(), |
| 356 | New->getType()->getAs<FunctionProtoType>(), New->getLocation())) { |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 357 | Invalid = true; |
| 358 | } |
| 359 | |
Douglas Gregor | cda9c67 | 2009-02-16 17:45:42 +0000 | [diff] [blame] | 360 | return Invalid; |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 364 | /// function declaration are well-formed according to C++ |
| 365 | /// [dcl.fct.default]. |
| 366 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 367 | unsigned NumParams = FD->getNumParams(); |
| 368 | unsigned p; |
| 369 | |
| 370 | // Find first parameter with a default argument |
| 371 | for (p = 0; p < NumParams; ++p) { |
| 372 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 373 | if (Param->hasDefaultArg()) |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | |
| 377 | // C++ [dcl.fct.default]p4: |
| 378 | // In a given function declaration, all parameters |
| 379 | // subsequent to a parameter with a default argument shall |
| 380 | // have default arguments supplied in this or previous |
| 381 | // declarations. A default argument shall not be redefined |
| 382 | // by a later declaration (not even to the same value). |
| 383 | unsigned LastMissingDefaultArg = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | for (; p < NumParams; ++p) { |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 385 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5f49a0c | 2009-08-25 01:23:32 +0000 | [diff] [blame] | 386 | if (!Param->hasDefaultArg()) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 387 | if (Param->isInvalidDecl()) |
| 388 | /* We already complained about this parameter. */; |
| 389 | else if (Param->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | Diag(Param->getLocation(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 391 | diag::err_param_default_argument_missing_name) |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 392 | << Param->getIdentifier(); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 393 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | Diag(Param->getLocation(), |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 395 | diag::err_param_default_argument_missing); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 397 | LastMissingDefaultArg = p; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if (LastMissingDefaultArg > 0) { |
| 402 | // Some default arguments were missing. Clear out all of the |
| 403 | // default arguments up to (and including) the last missing |
| 404 | // default argument, so that we leave the function parameters |
| 405 | // in a semantically valid state. |
| 406 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 407 | ParmVarDecl *Param = FD->getParamDecl(p); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 408 | if (Param->hasDefaultArg()) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 409 | if (!Param->hasUnparsedDefaultArg()) |
| 410 | Param->getDefaultArg()->Destroy(Context); |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 411 | Param->setDefaultArg(0); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 416 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 417 | /// isCurrentClassName - Determine whether the identifier II is the |
| 418 | /// name of the class type currently being defined. In the case of |
| 419 | /// nested classes, this will only return true if II is the name of |
| 420 | /// the innermost class. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 421 | bool Sema::isCurrentClassName(const IdentifierInfo &II, Scope *, |
| 422 | const CXXScopeSpec *SS) { |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 423 | CXXRecordDecl *CurDecl; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 424 | if (SS && SS->isSet() && !SS->isInvalid()) { |
Douglas Gregor | ac373c4 | 2009-08-21 22:16:40 +0000 | [diff] [blame] | 425 | DeclContext *DC = computeDeclContext(*SS, true); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 426 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(DC); |
| 427 | } else |
| 428 | CurDecl = dyn_cast_or_null<CXXRecordDecl>(CurContext); |
| 429 | |
| 430 | if (CurDecl) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 431 | return &II == CurDecl->getIdentifier(); |
| 432 | else |
| 433 | return false; |
| 434 | } |
| 435 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | /// \brief Check the validity of a C++ base class specifier. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 437 | /// |
| 438 | /// \returns a new CXXBaseSpecifier if well-formed, emits diagnostics |
| 439 | /// and returns NULL otherwise. |
| 440 | CXXBaseSpecifier * |
| 441 | Sema::CheckBaseSpecifier(CXXRecordDecl *Class, |
| 442 | SourceRange SpecifierRange, |
| 443 | bool Virtual, AccessSpecifier Access, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | QualType BaseType, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 445 | SourceLocation BaseLoc) { |
| 446 | // C++ [class.union]p1: |
| 447 | // A union shall not have base classes. |
| 448 | if (Class->isUnion()) { |
| 449 | Diag(Class->getLocation(), diag::err_base_clause_on_union) |
| 450 | << SpecifierRange; |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | if (BaseType->isDependentType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 456 | Class->getTagKind() == RecordDecl::TK_class, |
| 457 | Access, BaseType); |
| 458 | |
| 459 | // Base specifiers must be record types. |
| 460 | if (!BaseType->isRecordType()) { |
| 461 | Diag(BaseLoc, diag::err_base_must_be_class) << SpecifierRange; |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | // C++ [class.union]p1: |
| 466 | // A union shall not be used as a base class. |
| 467 | if (BaseType->isUnionType()) { |
| 468 | Diag(BaseLoc, diag::err_union_as_base_class) << SpecifierRange; |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | // C++ [class.derived]p2: |
| 473 | // The class-name in a base-specifier shall not be an incompletely |
| 474 | // defined class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | if (RequireCompleteType(BaseLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 476 | PDiag(diag::err_incomplete_base_class) |
| 477 | << SpecifierRange)) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 478 | return 0; |
| 479 | |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 480 | // If the base class is polymorphic or isn't empty, the new one is/isn't, too. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 481 | RecordDecl *BaseDecl = BaseType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 482 | assert(BaseDecl && "Record type has no declaration"); |
| 483 | BaseDecl = BaseDecl->getDefinition(Context); |
| 484 | assert(BaseDecl && "Base type is not incomplete, but has no definition"); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 485 | CXXRecordDecl * CXXBaseDecl = cast<CXXRecordDecl>(BaseDecl); |
| 486 | assert(CXXBaseDecl && "Base type is not a C++ type"); |
| 487 | if (!CXXBaseDecl->isEmpty()) |
| 488 | Class->setEmpty(false); |
| 489 | if (CXXBaseDecl->isPolymorphic()) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 490 | Class->setPolymorphic(true); |
| 491 | |
| 492 | // C++ [dcl.init.aggr]p1: |
| 493 | // An aggregate is [...] a class with [...] no base classes [...]. |
| 494 | Class->setAggregate(false); |
| 495 | Class->setPOD(false); |
| 496 | |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 497 | if (Virtual) { |
| 498 | // C++ [class.ctor]p5: |
| 499 | // A constructor is trivial if its class has no virtual base classes. |
| 500 | Class->setHasTrivialConstructor(false); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 501 | |
| 502 | // C++ [class.copy]p6: |
| 503 | // A copy constructor is trivial if its class has no virtual base classes. |
| 504 | Class->setHasTrivialCopyConstructor(false); |
| 505 | |
| 506 | // C++ [class.copy]p11: |
| 507 | // A copy assignment operator is trivial if its class has no virtual |
| 508 | // base classes. |
| 509 | Class->setHasTrivialCopyAssignment(false); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 510 | |
| 511 | // C++0x [meta.unary.prop] is_empty: |
| 512 | // T is a class type, but not a union type, with ... no virtual base |
| 513 | // classes |
| 514 | Class->setEmpty(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 515 | } else { |
| 516 | // C++ [class.ctor]p5: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | // A constructor is trivial if all the direct base classes of its |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 518 | // class have trivial constructors. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 519 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialConstructor()) |
| 520 | Class->setHasTrivialConstructor(false); |
| 521 | |
| 522 | // C++ [class.copy]p6: |
| 523 | // A copy constructor is trivial if all the direct base classes of its |
| 524 | // class have trivial copy constructors. |
| 525 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialCopyConstructor()) |
| 526 | Class->setHasTrivialCopyConstructor(false); |
| 527 | |
| 528 | // C++ [class.copy]p11: |
| 529 | // A copy assignment operator is trivial if all the direct base classes |
| 530 | // of its class have trivial copy assignment operators. |
| 531 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialCopyAssignment()) |
| 532 | Class->setHasTrivialCopyAssignment(false); |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 533 | } |
Anders Carlsson | 072abef | 2009-04-17 02:34:54 +0000 | [diff] [blame] | 534 | |
| 535 | // C++ [class.ctor]p3: |
| 536 | // A destructor is trivial if all the direct base classes of its class |
| 537 | // have trivial destructors. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 538 | if (!cast<CXXRecordDecl>(BaseDecl)->hasTrivialDestructor()) |
| 539 | Class->setHasTrivialDestructor(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 541 | // Create the base specifier. |
| 542 | // FIXME: Allocate via ASTContext? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | return new (Context) CXXBaseSpecifier(SpecifierRange, Virtual, |
| 544 | Class->getTagKind() == RecordDecl::TK_class, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 545 | Access, BaseType); |
| 546 | } |
| 547 | |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 548 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 549 | /// one entry in the base class list of a class specifier, for |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | /// example: |
| 551 | /// class foo : public bar, virtual private baz { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 552 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | Sema::BaseResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 554 | Sema::ActOnBaseSpecifier(DeclPtrTy classdecl, SourceRange SpecifierRange, |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 555 | bool Virtual, AccessSpecifier Access, |
| 556 | TypeTy *basetype, SourceLocation BaseLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 557 | if (!classdecl) |
| 558 | return true; |
| 559 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 560 | AdjustDeclIfTemplate(classdecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 561 | CXXRecordDecl *Class = cast<CXXRecordDecl>(classdecl.getAs<Decl>()); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 562 | QualType BaseType = GetTypeFromParser(basetype); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 563 | if (CXXBaseSpecifier *BaseSpec = CheckBaseSpecifier(Class, SpecifierRange, |
| 564 | Virtual, Access, |
| 565 | BaseType, BaseLoc)) |
| 566 | return BaseSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 568 | return true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 569 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 570 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 571 | /// \brief Performs the actual work of attaching the given base class |
| 572 | /// specifiers to a C++ class. |
| 573 | bool Sema::AttachBaseSpecifiers(CXXRecordDecl *Class, CXXBaseSpecifier **Bases, |
| 574 | unsigned NumBases) { |
| 575 | if (NumBases == 0) |
| 576 | return false; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 577 | |
| 578 | // Used to keep track of which base types we have already seen, so |
| 579 | // that we can properly diagnose redundant direct base types. Note |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 580 | // that the key is always the unqualified canonical type of the base |
| 581 | // class. |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 582 | std::map<QualType, CXXBaseSpecifier*, QualTypeOrdering> KnownBaseTypes; |
| 583 | |
| 584 | // Copy non-redundant base specifiers into permanent storage. |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 585 | unsigned NumGoodBases = 0; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 586 | bool Invalid = false; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 587 | for (unsigned idx = 0; idx < NumBases; ++idx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | QualType NewBaseType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 589 | = Context.getCanonicalType(Bases[idx]->getType()); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 590 | NewBaseType = NewBaseType.getLocalUnqualifiedType(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 591 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 592 | if (KnownBaseTypes[NewBaseType]) { |
| 593 | // C++ [class.mi]p3: |
| 594 | // A class shall not be specified as a direct base class of a |
| 595 | // derived class more than once. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 596 | Diag(Bases[idx]->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 597 | diag::err_duplicate_base_class) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 598 | << KnownBaseTypes[NewBaseType]->getType() |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 599 | << Bases[idx]->getSourceRange(); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 600 | |
| 601 | // Delete the duplicate base class specifier; we're going to |
| 602 | // overwrite its pointer later. |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 603 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 604 | |
| 605 | Invalid = true; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 606 | } else { |
| 607 | // Okay, add this new base class. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 608 | KnownBaseTypes[NewBaseType] = Bases[idx]; |
| 609 | Bases[NumGoodBases++] = Bases[idx]; |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
| 613 | // Attach the remaining base class specifiers to the derived class. |
Fariborz Jahanian | 5ffcd7b | 2009-07-02 18:26:15 +0000 | [diff] [blame] | 614 | Class->setBases(Context, Bases, NumGoodBases); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 615 | |
| 616 | // Delete the remaining (good) base class specifiers, since their |
| 617 | // data has been copied into the CXXRecordDecl. |
| 618 | for (unsigned idx = 0; idx < NumGoodBases; ++idx) |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 619 | Context.Deallocate(Bases[idx]); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 620 | |
| 621 | return Invalid; |
| 622 | } |
| 623 | |
| 624 | /// ActOnBaseSpecifiers - Attach the given base specifiers to the |
| 625 | /// class, after checking whether there are any duplicate base |
| 626 | /// classes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | void Sema::ActOnBaseSpecifiers(DeclPtrTy ClassDecl, BaseTy **Bases, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 628 | unsigned NumBases) { |
| 629 | if (!ClassDecl || !Bases || !NumBases) |
| 630 | return; |
| 631 | |
| 632 | AdjustDeclIfTemplate(ClassDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 633 | AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl.getAs<Decl>()), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 634 | (CXXBaseSpecifier**)(Bases), NumBases); |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 635 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 636 | |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 637 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 638 | /// derived from the type \p Base. |
| 639 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base) { |
| 640 | if (!getLangOptions().CPlusPlus) |
| 641 | return false; |
| 642 | |
| 643 | const RecordType *DerivedRT = Derived->getAs<RecordType>(); |
| 644 | if (!DerivedRT) |
| 645 | return false; |
| 646 | |
| 647 | const RecordType *BaseRT = Base->getAs<RecordType>(); |
| 648 | if (!BaseRT) |
| 649 | return false; |
| 650 | |
| 651 | CXXRecordDecl *DerivedRD = cast<CXXRecordDecl>(DerivedRT->getDecl()); |
| 652 | CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl()); |
| 653 | return DerivedRD->isDerivedFrom(BaseRD); |
| 654 | } |
| 655 | |
| 656 | /// \brief Determine whether the type \p Derived is a C++ class that is |
| 657 | /// derived from the type \p Base. |
| 658 | bool Sema::IsDerivedFrom(QualType Derived, QualType Base, CXXBasePaths &Paths) { |
| 659 | if (!getLangOptions().CPlusPlus) |
| 660 | return false; |
| 661 | |
| 662 | const RecordType *DerivedRT = Derived->getAs<RecordType>(); |
| 663 | if (!DerivedRT) |
| 664 | return false; |
| 665 | |
| 666 | const RecordType *BaseRT = Base->getAs<RecordType>(); |
| 667 | if (!BaseRT) |
| 668 | return false; |
| 669 | |
| 670 | CXXRecordDecl *DerivedRD = cast<CXXRecordDecl>(DerivedRT->getDecl()); |
| 671 | CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl()); |
| 672 | return DerivedRD->isDerivedFrom(BaseRD, Paths); |
| 673 | } |
| 674 | |
| 675 | /// CheckDerivedToBaseConversion - Check whether the Derived-to-Base |
| 676 | /// conversion (where Derived and Base are class types) is |
| 677 | /// well-formed, meaning that the conversion is unambiguous (and |
| 678 | /// that all of the base classes are accessible). Returns true |
| 679 | /// and emits a diagnostic if the code is ill-formed, returns false |
| 680 | /// otherwise. Loc is the location where this routine should point to |
| 681 | /// if there is an error, and Range is the source range to highlight |
| 682 | /// if there is an error. |
| 683 | bool |
| 684 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
| 685 | unsigned InaccessibleBaseID, |
| 686 | unsigned AmbigiousBaseConvID, |
| 687 | SourceLocation Loc, SourceRange Range, |
| 688 | DeclarationName Name) { |
| 689 | // First, determine whether the path from Derived to Base is |
| 690 | // ambiguous. This is slightly more expensive than checking whether |
| 691 | // the Derived to Base conversion exists, because here we need to |
| 692 | // explore multiple paths to determine if there is an ambiguity. |
| 693 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 694 | /*DetectVirtual=*/false); |
| 695 | bool DerivationOkay = IsDerivedFrom(Derived, Base, Paths); |
| 696 | assert(DerivationOkay && |
| 697 | "Can only be used with a derived-to-base conversion"); |
| 698 | (void)DerivationOkay; |
| 699 | |
| 700 | if (!Paths.isAmbiguous(Context.getCanonicalType(Base).getUnqualifiedType())) { |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 701 | if (InaccessibleBaseID == 0) |
| 702 | return false; |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 703 | // Check that the base class can be accessed. |
| 704 | return CheckBaseClassAccess(Derived, Base, InaccessibleBaseID, Paths, Loc, |
| 705 | Name); |
| 706 | } |
| 707 | |
| 708 | // We know that the derived-to-base conversion is ambiguous, and |
| 709 | // we're going to produce a diagnostic. Perform the derived-to-base |
| 710 | // search just one more time to compute all of the possible paths so |
| 711 | // that we can print them out. This is more expensive than any of |
| 712 | // the previous derived-to-base checks we've done, but at this point |
| 713 | // performance isn't as much of an issue. |
| 714 | Paths.clear(); |
| 715 | Paths.setRecordingPaths(true); |
| 716 | bool StillOkay = IsDerivedFrom(Derived, Base, Paths); |
| 717 | assert(StillOkay && "Can only be used with a derived-to-base conversion"); |
| 718 | (void)StillOkay; |
| 719 | |
| 720 | // Build up a textual representation of the ambiguous paths, e.g., |
| 721 | // D -> B -> A, that will be used to illustrate the ambiguous |
| 722 | // conversions in the diagnostic. We only print one of the paths |
| 723 | // to each base class subobject. |
| 724 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 725 | |
| 726 | Diag(Loc, AmbigiousBaseConvID) |
| 727 | << Derived << Base << PathDisplayStr << Range << Name; |
| 728 | return true; |
| 729 | } |
| 730 | |
| 731 | bool |
| 732 | Sema::CheckDerivedToBaseConversion(QualType Derived, QualType Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 733 | SourceLocation Loc, SourceRange Range, |
| 734 | bool IgnoreAccess) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 735 | return CheckDerivedToBaseConversion(Derived, Base, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 736 | IgnoreAccess ? 0 : |
| 737 | diag::err_conv_to_inaccessible_base, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 738 | diag::err_ambiguous_derived_to_base_conv, |
| 739 | Loc, Range, DeclarationName()); |
| 740 | } |
| 741 | |
| 742 | |
| 743 | /// @brief Builds a string representing ambiguous paths from a |
| 744 | /// specific derived class to different subobjects of the same base |
| 745 | /// class. |
| 746 | /// |
| 747 | /// This function builds a string that can be used in error messages |
| 748 | /// to show the different paths that one can take through the |
| 749 | /// inheritance hierarchy to go from the derived class to different |
| 750 | /// subobjects of a base class. The result looks something like this: |
| 751 | /// @code |
| 752 | /// struct D -> struct B -> struct A |
| 753 | /// struct D -> struct C -> struct A |
| 754 | /// @endcode |
| 755 | std::string Sema::getAmbiguousPathsDisplayString(CXXBasePaths &Paths) { |
| 756 | std::string PathDisplayStr; |
| 757 | std::set<unsigned> DisplayedPaths; |
| 758 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
| 759 | Path != Paths.end(); ++Path) { |
| 760 | if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) { |
| 761 | // We haven't displayed a path to this particular base |
| 762 | // class subobject yet. |
| 763 | PathDisplayStr += "\n "; |
| 764 | PathDisplayStr += Context.getTypeDeclType(Paths.getOrigin()).getAsString(); |
| 765 | for (CXXBasePath::const_iterator Element = Path->begin(); |
| 766 | Element != Path->end(); ++Element) |
| 767 | PathDisplayStr += " -> " + Element->Base->getType().getAsString(); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | return PathDisplayStr; |
| 772 | } |
| 773 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 774 | //===----------------------------------------------------------------------===// |
| 775 | // C++ class member Handling |
| 776 | //===----------------------------------------------------------------------===// |
| 777 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 778 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 779 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 780 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
Chris Lattner | b6688e0 | 2009-04-12 22:37:57 +0000 | [diff] [blame] | 781 | /// any. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 782 | Sema::DeclPtrTy |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 783 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 784 | MultiTemplateParamsArg TemplateParameterLists, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 785 | ExprTy *BW, ExprTy *InitExpr, bool Deleted) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 786 | const DeclSpec &DS = D.getDeclSpec(); |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 787 | DeclarationName Name = GetNameForDeclarator(D); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 788 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 789 | Expr *Init = static_cast<Expr*>(InitExpr); |
| 790 | SourceLocation Loc = D.getIdentifierLoc(); |
| 791 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 792 | bool isFunc = D.isFunctionDeclarator(); |
| 793 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 794 | assert(!DS.isFriendSpecified()); |
| 795 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 796 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 797 | // duration (auto, register) or with the extern storage-class-specifier. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 798 | // C++ 7.1.1p8: The mutable specifier can be applied only to names of class |
| 799 | // data members and cannot be applied to names declared const or static, |
| 800 | // and cannot be applied to reference members. |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 801 | switch (DS.getStorageClassSpec()) { |
| 802 | case DeclSpec::SCS_unspecified: |
| 803 | case DeclSpec::SCS_typedef: |
| 804 | case DeclSpec::SCS_static: |
| 805 | // FALL THROUGH. |
| 806 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 807 | case DeclSpec::SCS_mutable: |
| 808 | if (isFunc) { |
| 809 | if (DS.getStorageClassSpecLoc().isValid()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 810 | Diag(DS.getStorageClassSpecLoc(), diag::err_mutable_function); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 811 | else |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 812 | Diag(DS.getThreadSpecLoc(), diag::err_mutable_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 814 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 815 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 816 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 817 | } else { |
| 818 | QualType T = GetTypeForDeclarator(D, S); |
| 819 | diag::kind err = static_cast<diag::kind>(0); |
| 820 | if (T->isReferenceType()) |
| 821 | err = diag::err_mutable_reference; |
| 822 | else if (T.isConstQualified()) |
| 823 | err = diag::err_mutable_const; |
| 824 | if (err != 0) { |
| 825 | if (DS.getStorageClassSpecLoc().isValid()) |
| 826 | Diag(DS.getStorageClassSpecLoc(), err); |
| 827 | else |
| 828 | Diag(DS.getThreadSpecLoc(), err); |
Sebastian Redl | a11f42f | 2008-11-17 23:24:37 +0000 | [diff] [blame] | 829 | // FIXME: It would be nicer if the keyword was ignored only for this |
| 830 | // declarator. Otherwise we could get follow-up errors. |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 831 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 832 | } |
| 833 | } |
| 834 | break; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 835 | default: |
| 836 | if (DS.getStorageClassSpecLoc().isValid()) |
| 837 | Diag(DS.getStorageClassSpecLoc(), |
| 838 | diag::err_storageclass_invalid_for_member); |
| 839 | else |
| 840 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 841 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 842 | } |
| 843 | |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 844 | if (!isFunc && |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 845 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_typename && |
Argyrios Kyrtzidis | d6caa9e | 2008-10-15 20:23:22 +0000 | [diff] [blame] | 846 | D.getNumTypeObjects() == 0) { |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 847 | // Check also for this case: |
| 848 | // |
| 849 | // typedef int f(); |
| 850 | // f a; |
| 851 | // |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 852 | QualType TDType = GetTypeFromParser(DS.getTypeRep()); |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 853 | isFunc = TDType->isFunctionType(); |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 854 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 855 | |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 856 | bool isInstField = ((DS.getStorageClassSpec() == DeclSpec::SCS_unspecified || |
| 857 | DS.getStorageClassSpec() == DeclSpec::SCS_mutable) && |
Argyrios Kyrtzidis | de933f0 | 2008-10-08 22:20:31 +0000 | [diff] [blame] | 858 | !isFunc); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 859 | |
| 860 | Decl *Member; |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 861 | if (isInstField) { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 862 | // FIXME: Check for template parameters! |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 863 | Member = HandleField(S, cast<CXXRecordDecl>(CurContext), Loc, D, BitWidth, |
| 864 | AS); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 865 | assert(Member && "HandleField never returns null"); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 866 | } else { |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 867 | Member = HandleDeclarator(S, D, move(TemplateParameterLists), false) |
| 868 | .getAs<Decl>(); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 869 | if (!Member) { |
| 870 | if (BitWidth) DeleteExpr(BitWidth); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 871 | return DeclPtrTy(); |
Chris Lattner | 6f8ce14 | 2009-03-05 23:03:49 +0000 | [diff] [blame] | 872 | } |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 873 | |
| 874 | // Non-instance-fields can't have a bitfield. |
| 875 | if (BitWidth) { |
| 876 | if (Member->isInvalidDecl()) { |
| 877 | // don't emit another diagnostic. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 878 | } else if (isa<VarDecl>(Member)) { |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 879 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 880 | // "static member 'A' cannot be a bit-field" |
| 881 | Diag(Loc, diag::err_static_not_bitfield) |
| 882 | << Name << BitWidth->getSourceRange(); |
| 883 | } else if (isa<TypedefDecl>(Member)) { |
| 884 | // "typedef member 'x' cannot be a bit-field" |
| 885 | Diag(Loc, diag::err_typedef_not_bitfield) |
| 886 | << Name << BitWidth->getSourceRange(); |
| 887 | } else { |
| 888 | // A function typedef ("typedef int f(); f a;"). |
| 889 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 890 | Diag(Loc, diag::err_not_integral_type_bitfield) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | << Name << cast<ValueDecl>(Member)->getType() |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 892 | << BitWidth->getSourceRange(); |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
Chris Lattner | 8b963ef | 2009-03-05 23:01:03 +0000 | [diff] [blame] | 895 | DeleteExpr(BitWidth); |
| 896 | BitWidth = 0; |
| 897 | Member->setInvalidDecl(); |
| 898 | } |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 899 | |
| 900 | Member->setAccess(AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 901 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 902 | // If we have declared a member function template, set the access of the |
| 903 | // templated declaration as well. |
| 904 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Member)) |
| 905 | FunTmpl->getTemplatedDecl()->setAccess(AS); |
Chris Lattner | 2479366 | 2009-03-05 22:45:59 +0000 | [diff] [blame] | 906 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 907 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 908 | assert((Name || isInstField) && "No identifier for non-field ?"); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 909 | |
Douglas Gregor | 021c3b3 | 2009-03-11 23:00:04 +0000 | [diff] [blame] | 910 | if (Init) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 911 | AddInitializerToDecl(DeclPtrTy::make(Member), ExprArg(*this, Init), false); |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 912 | if (Deleted) // FIXME: Source location is not very good. |
| 913 | SetDeclDeleted(DeclPtrTy::make(Member), D.getSourceRange().getBegin()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 914 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 915 | if (isInstField) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 916 | FieldCollector->Add(cast<FieldDecl>(Member)); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 917 | return DeclPtrTy(); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 918 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 919 | return DeclPtrTy::make(Member); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 922 | /// ActOnMemInitializer - Handle a C++ member initializer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 923 | Sema::MemInitResult |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 924 | Sema::ActOnMemInitializer(DeclPtrTy ConstructorD, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 925 | Scope *S, |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 926 | const CXXScopeSpec &SS, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 927 | IdentifierInfo *MemberOrBase, |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 928 | TypeTy *TemplateTypeTy, |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 929 | SourceLocation IdLoc, |
| 930 | SourceLocation LParenLoc, |
| 931 | ExprTy **Args, unsigned NumArgs, |
| 932 | SourceLocation *CommaLocs, |
| 933 | SourceLocation RParenLoc) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 934 | if (!ConstructorD) |
| 935 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 937 | AdjustDeclIfTemplate(ConstructorD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | |
| 939 | CXXConstructorDecl *Constructor |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 940 | = dyn_cast<CXXConstructorDecl>(ConstructorD.getAs<Decl>()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 941 | if (!Constructor) { |
| 942 | // The user wrote a constructor initializer on a function that is |
| 943 | // not a C++ constructor. Ignore the error for now, because we may |
| 944 | // have more member initializers coming; we'll diagnose it just |
| 945 | // once in ActOnMemInitializers. |
| 946 | return true; |
| 947 | } |
| 948 | |
| 949 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 950 | |
| 951 | // C++ [class.base.init]p2: |
| 952 | // Names in a mem-initializer-id are looked up in the scope of the |
| 953 | // constructor’s class and, if not found in that scope, are looked |
| 954 | // up in the scope containing the constructor’s |
| 955 | // definition. [Note: if the constructor’s class contains a member |
| 956 | // with the same name as a direct or virtual base class of the |
| 957 | // class, a mem-initializer-id naming the member or base class and |
| 958 | // composed of a single identifier refers to the class member. A |
| 959 | // mem-initializer-id for the hidden base class may be specified |
| 960 | // using a qualified name. ] |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 961 | if (!SS.getScopeRep() && !TemplateTypeTy) { |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 962 | // Look for a member, first. |
| 963 | FieldDecl *Member = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | DeclContext::lookup_result Result |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 965 | = ClassDecl->lookup(MemberOrBase); |
| 966 | if (Result.first != Result.second) |
| 967 | Member = dyn_cast<FieldDecl>(*Result.first); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 968 | |
Fariborz Jahanian | bcfad54 | 2009-06-30 23:26:25 +0000 | [diff] [blame] | 969 | // FIXME: Handle members of an anonymous union. |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 970 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 971 | if (Member) |
| 972 | return BuildMemberInitializer(Member, (Expr**)Args, NumArgs, IdLoc, |
| 973 | RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 974 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 975 | // It didn't name a member, so see if it names a class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | TypeTy *BaseTy = TemplateTypeTy ? TemplateTypeTy |
Fariborz Jahanian | 9617433 | 2009-07-01 19:21:19 +0000 | [diff] [blame] | 977 | : getTypeName(*MemberOrBase, IdLoc, S, &SS); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 978 | if (!BaseTy) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 979 | return Diag(IdLoc, diag::err_mem_init_not_member_or_class) |
| 980 | << MemberOrBase << SourceRange(IdLoc, RParenLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 982 | QualType BaseType = GetTypeFromParser(BaseTy); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 983 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 984 | return BuildBaseInitializer(BaseType, (Expr **)Args, NumArgs, IdLoc, |
| 985 | RParenLoc, ClassDecl); |
| 986 | } |
| 987 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 988 | /// Checks an initializer expression for use of uninitialized fields, such as |
| 989 | /// containing the field that is being initialized. Returns true if there is an |
| 990 | /// uninitialized field was used an updates the SourceLocation parameter; false |
| 991 | /// otherwise. |
| 992 | static bool InitExprContainsUninitializedFields(const Stmt* S, |
| 993 | const FieldDecl* LhsField, |
| 994 | SourceLocation* L) { |
| 995 | const MemberExpr* ME = dyn_cast<MemberExpr>(S); |
| 996 | if (ME) { |
| 997 | const NamedDecl* RhsField = ME->getMemberDecl(); |
| 998 | if (RhsField == LhsField) { |
| 999 | // Initializing a field with itself. Throw a warning. |
| 1000 | // But wait; there are exceptions! |
| 1001 | // Exception #1: The field may not belong to this record. |
| 1002 | // e.g. Foo(const Foo& rhs) : A(rhs.A) {} |
| 1003 | const Expr* base = ME->getBase(); |
| 1004 | if (base != NULL && !isa<CXXThisExpr>(base->IgnoreParenCasts())) { |
| 1005 | // Even though the field matches, it does not belong to this record. |
| 1006 | return false; |
| 1007 | } |
| 1008 | // None of the exceptions triggered; return true to indicate an |
| 1009 | // uninitialized field was used. |
| 1010 | *L = ME->getMemberLoc(); |
| 1011 | return true; |
| 1012 | } |
| 1013 | } |
| 1014 | bool found = false; |
| 1015 | for (Stmt::const_child_iterator it = S->child_begin(); |
| 1016 | it != S->child_end() && found == false; |
| 1017 | ++it) { |
| 1018 | if (isa<CallExpr>(S)) { |
| 1019 | // Do not descend into function calls or constructors, as the use |
| 1020 | // of an uninitialized field may be valid. One would have to inspect |
| 1021 | // the contents of the function/ctor to determine if it is safe or not. |
| 1022 | // i.e. Pass-by-value is never safe, but pass-by-reference and pointers |
| 1023 | // may be safe, depending on what the function/ctor does. |
| 1024 | continue; |
| 1025 | } |
| 1026 | found = InitExprContainsUninitializedFields(*it, LhsField, L); |
| 1027 | } |
| 1028 | return found; |
| 1029 | } |
| 1030 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1031 | Sema::MemInitResult |
| 1032 | Sema::BuildMemberInitializer(FieldDecl *Member, Expr **Args, |
| 1033 | unsigned NumArgs, SourceLocation IdLoc, |
| 1034 | SourceLocation RParenLoc) { |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1035 | // FIXME: CXXBaseOrMemberInitializer should only contain a single |
| 1036 | // subexpression so we can wrap it in a CXXExprWithTemporaries if necessary. |
| 1037 | ExprTemporaries.clear(); |
| 1038 | |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1039 | // Diagnose value-uses of fields to initialize themselves, e.g. |
| 1040 | // foo(foo) |
| 1041 | // where foo is not also a parameter to the constructor. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1042 | // TODO: implement -Wuninitialized and fold this into that framework. |
John McCall | b419004 | 2009-11-04 23:02:40 +0000 | [diff] [blame] | 1043 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 1044 | SourceLocation L; |
| 1045 | if (InitExprContainsUninitializedFields(Args[i], Member, &L)) { |
| 1046 | // FIXME: Return true in the case when other fields are used before being |
| 1047 | // uninitialized. For example, let this field be the i'th field. When |
| 1048 | // initializing the i'th field, throw a warning if any of the >= i'th |
| 1049 | // fields are used, as they are not yet initialized. |
| 1050 | // Right now we are only handling the case where the i'th field uses |
| 1051 | // itself in its initializer. |
| 1052 | Diag(L, diag::warn_field_is_uninit); |
| 1053 | } |
| 1054 | } |
| 1055 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1056 | bool HasDependentArg = false; |
| 1057 | for (unsigned i = 0; i < NumArgs; i++) |
| 1058 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1059 | |
| 1060 | CXXConstructorDecl *C = 0; |
| 1061 | QualType FieldType = Member->getType(); |
| 1062 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 1063 | FieldType = Array->getElementType(); |
| 1064 | if (FieldType->isDependentType()) { |
| 1065 | // Can't check init for dependent type. |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1066 | } else if (FieldType->isRecordType()) { |
| 1067 | // Member is a record (struct/union/class), so pass the initializer |
| 1068 | // arguments down to the record's constructor. |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1069 | if (!HasDependentArg) { |
| 1070 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 1071 | |
| 1072 | C = PerformInitializationByConstructor(FieldType, |
| 1073 | MultiExprArg(*this, |
| 1074 | (void**)Args, |
| 1075 | NumArgs), |
| 1076 | IdLoc, |
| 1077 | SourceRange(IdLoc, RParenLoc), |
| 1078 | Member->getDeclName(), IK_Direct, |
| 1079 | ConstructorArgs); |
| 1080 | |
| 1081 | if (C) { |
| 1082 | // Take over the constructor arguments as our own. |
| 1083 | NumArgs = ConstructorArgs.size(); |
| 1084 | Args = (Expr **)ConstructorArgs.take(); |
| 1085 | } |
| 1086 | } |
Fariborz Jahanian | 636a0ff | 2009-09-02 17:10:17 +0000 | [diff] [blame] | 1087 | } else if (NumArgs != 1 && NumArgs != 0) { |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1088 | // The member type is not a record type (or an array of record |
| 1089 | // types), so it can be only be default- or copy-initialized. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | return Diag(IdLoc, diag::err_mem_initializer_mismatch) |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1091 | << Member->getDeclName() << SourceRange(IdLoc, RParenLoc); |
| 1092 | } else if (!HasDependentArg) { |
Fariborz Jahanian | 636a0ff | 2009-09-02 17:10:17 +0000 | [diff] [blame] | 1093 | Expr *NewExp; |
| 1094 | if (NumArgs == 0) { |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1095 | if (FieldType->isReferenceType()) { |
| 1096 | Diag(IdLoc, diag::err_null_intialized_reference_member) |
| 1097 | << Member->getDeclName(); |
| 1098 | return Diag(Member->getLocation(), diag::note_declared_at); |
| 1099 | } |
Fariborz Jahanian | 636a0ff | 2009-09-02 17:10:17 +0000 | [diff] [blame] | 1100 | NewExp = new (Context) CXXZeroInitValueExpr(FieldType, IdLoc, RParenLoc); |
| 1101 | NumArgs = 1; |
| 1102 | } |
| 1103 | else |
| 1104 | NewExp = (Expr*)Args[0]; |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1105 | if (PerformCopyInitialization(NewExp, FieldType, "passing")) |
| 1106 | return true; |
| 1107 | Args[0] = NewExp; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1108 | } |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1109 | |
| 1110 | // FIXME: CXXBaseOrMemberInitializer should only contain a single |
| 1111 | // subexpression so we can wrap it in a CXXExprWithTemporaries if necessary. |
| 1112 | ExprTemporaries.clear(); |
| 1113 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1114 | // FIXME: Perform direct initialization of the member. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | return new (Context) CXXBaseOrMemberInitializer(Member, (Expr **)Args, |
Anders Carlsson | 8c57a66 | 2009-08-29 01:31:33 +0000 | [diff] [blame] | 1116 | NumArgs, C, IdLoc, RParenLoc); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | Sema::MemInitResult |
| 1120 | Sema::BuildBaseInitializer(QualType BaseType, Expr **Args, |
| 1121 | unsigned NumArgs, SourceLocation IdLoc, |
| 1122 | SourceLocation RParenLoc, CXXRecordDecl *ClassDecl) { |
| 1123 | bool HasDependentArg = false; |
| 1124 | for (unsigned i = 0; i < NumArgs; i++) |
| 1125 | HasDependentArg |= Args[i]->isTypeDependent(); |
| 1126 | |
| 1127 | if (!BaseType->isDependentType()) { |
| 1128 | if (!BaseType->isRecordType()) |
| 1129 | return Diag(IdLoc, diag::err_base_init_does_not_name_class) |
| 1130 | << BaseType << SourceRange(IdLoc, RParenLoc); |
| 1131 | |
| 1132 | // C++ [class.base.init]p2: |
| 1133 | // [...] Unless the mem-initializer-id names a nonstatic data |
| 1134 | // member of the constructor’s class or a direct or virtual base |
| 1135 | // of that class, the mem-initializer is ill-formed. A |
| 1136 | // mem-initializer-list can initialize a base class using any |
| 1137 | // name that denotes that base class type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1139 | // First, check for a direct base class. |
| 1140 | const CXXBaseSpecifier *DirectBaseSpec = 0; |
| 1141 | for (CXXRecordDecl::base_class_const_iterator Base = |
| 1142 | ClassDecl->bases_begin(); Base != ClassDecl->bases_end(); ++Base) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1143 | if (Context.hasSameUnqualifiedType(BaseType, Base->getType())) { |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1144 | // We found a direct base of this type. That's what we're |
| 1145 | // initializing. |
| 1146 | DirectBaseSpec = &*Base; |
| 1147 | break; |
| 1148 | } |
| 1149 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1151 | // Check for a virtual base class. |
| 1152 | // FIXME: We might be able to short-circuit this if we know in advance that |
| 1153 | // there are no virtual bases. |
| 1154 | const CXXBaseSpecifier *VirtualBaseSpec = 0; |
| 1155 | if (!DirectBaseSpec || !DirectBaseSpec->isVirtual()) { |
| 1156 | // We haven't found a base yet; search the class hierarchy for a |
| 1157 | // virtual base class. |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1158 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1159 | /*DetectVirtual=*/false); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1160 | if (IsDerivedFrom(Context.getTypeDeclType(ClassDecl), BaseType, Paths)) { |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1161 | for (CXXBasePaths::paths_iterator Path = Paths.begin(); |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1162 | Path != Paths.end(); ++Path) { |
| 1163 | if (Path->back().Base->isVirtual()) { |
| 1164 | VirtualBaseSpec = Path->back().Base; |
| 1165 | break; |
| 1166 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1170 | |
| 1171 | // C++ [base.class.init]p2: |
| 1172 | // If a mem-initializer-id is ambiguous because it designates both |
| 1173 | // a direct non-virtual base class and an inherited virtual base |
| 1174 | // class, the mem-initializer is ill-formed. |
| 1175 | if (DirectBaseSpec && VirtualBaseSpec) |
| 1176 | return Diag(IdLoc, diag::err_base_init_direct_and_virtual) |
| 1177 | << BaseType << SourceRange(IdLoc, RParenLoc); |
| 1178 | // C++ [base.class.init]p2: |
| 1179 | // Unless the mem-initializer-id names a nonstatic data membeer of the |
| 1180 | // constructor's class ot a direst or virtual base of that class, the |
| 1181 | // mem-initializer is ill-formed. |
| 1182 | if (!DirectBaseSpec && !VirtualBaseSpec) |
| 1183 | return Diag(IdLoc, diag::err_not_direct_base_or_virtual) |
| 1184 | << BaseType << ClassDecl->getNameAsCString() |
| 1185 | << SourceRange(IdLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 1188 | CXXConstructorDecl *C = 0; |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1189 | if (!BaseType->isDependentType() && !HasDependentArg) { |
| 1190 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName( |
Douglas Gregor | 3eaa9ff | 2009-11-08 07:12:55 +0000 | [diff] [blame] | 1191 | Context.getCanonicalType(BaseType).getUnqualifiedType()); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1192 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 1193 | |
| 1194 | C = PerformInitializationByConstructor(BaseType, |
| 1195 | MultiExprArg(*this, |
| 1196 | (void**)Args, NumArgs), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | IdLoc, SourceRange(IdLoc, RParenLoc), |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1198 | Name, IK_Direct, |
| 1199 | ConstructorArgs); |
| 1200 | if (C) { |
| 1201 | // Take over the constructor arguments as our own. |
| 1202 | NumArgs = ConstructorArgs.size(); |
| 1203 | Args = (Expr **)ConstructorArgs.take(); |
| 1204 | } |
Eli Friedman | 59c0437 | 2009-07-29 19:44:27 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Anders Carlsson | f8a9a79 | 2009-11-13 19:21:49 +0000 | [diff] [blame] | 1207 | // FIXME: CXXBaseOrMemberInitializer should only contain a single |
| 1208 | // subexpression so we can wrap it in a CXXExprWithTemporaries if necessary. |
| 1209 | ExprTemporaries.clear(); |
| 1210 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | return new (Context) CXXBaseOrMemberInitializer(BaseType, (Expr **)Args, |
Anders Carlsson | 8c57a66 | 2009-08-29 01:31:33 +0000 | [diff] [blame] | 1212 | NumArgs, C, IdLoc, RParenLoc); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1215 | bool |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1216 | Sema::SetBaseOrMemberInitializers(CXXConstructorDecl *Constructor, |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1217 | CXXBaseOrMemberInitializer **Initializers, |
| 1218 | unsigned NumInitializers, |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1219 | bool IsImplicitConstructor) { |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1220 | // We need to build the initializer AST according to order of construction |
| 1221 | // and not what user specified in the Initializers list. |
| 1222 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 1223 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
| 1224 | llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; |
| 1225 | bool HasDependentBaseInit = false; |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1226 | bool HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1228 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1229 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
| 1230 | if (Member->isBaseInitializer()) { |
| 1231 | if (Member->getBaseClass()->isDependentType()) |
| 1232 | HasDependentBaseInit = true; |
| 1233 | AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
| 1234 | } else { |
| 1235 | AllBaseFields[Member->getMember()] = Member; |
| 1236 | } |
| 1237 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1239 | if (HasDependentBaseInit) { |
| 1240 | // FIXME. This does not preserve the ordering of the initializers. |
| 1241 | // Try (with -Wreorder) |
| 1242 | // template<class X> struct A {}; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | // template<class X> struct B : A<X> { |
| 1244 | // B() : x1(10), A<X>() {} |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1245 | // int x1; |
| 1246 | // }; |
| 1247 | // B<int> x; |
| 1248 | // On seeing one dependent type, we should essentially exit this routine |
| 1249 | // while preserving user-declared initializer list. When this routine is |
| 1250 | // called during instantiatiation process, this routine will rebuild the |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1251 | // ordered initializer list correctly. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1253 | // If we have a dependent base initialization, we can't determine the |
| 1254 | // association between initializers and bases; just dump the known |
| 1255 | // initializers into the list, and don't try to deal with other bases. |
| 1256 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 1257 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
| 1258 | if (Member->isBaseInitializer()) |
| 1259 | AllToInit.push_back(Member); |
| 1260 | } |
| 1261 | } else { |
| 1262 | // Push virtual bases before others. |
| 1263 | for (CXXRecordDecl::base_class_iterator VBase = |
| 1264 | ClassDecl->vbases_begin(), |
| 1265 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 1266 | if (VBase->getType()->isDependentType()) |
| 1267 | continue; |
Douglas Gregor | c07a494 | 2009-11-15 08:51:10 +0000 | [diff] [blame] | 1268 | if (CXXBaseOrMemberInitializer *Value |
| 1269 | = AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) { |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1270 | AllToInit.push_back(Value); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1271 | } |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1272 | else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | CXXRecordDecl *VBaseDecl = |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1274 | cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1275 | assert(VBaseDecl && "SetBaseOrMemberInitializers - VBaseDecl null"); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1276 | CXXConstructorDecl *Ctor = VBaseDecl->getDefaultConstructor(Context); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1277 | if (!Ctor) { |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1278 | Diag(Constructor->getLocation(), diag::err_missing_default_ctor) |
| 1279 | << (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl) |
| 1280 | << 0 << VBase->getType(); |
| 1281 | Diag(VBaseDecl->getLocation(), diag::note_previous_class_decl) |
| 1282 | << Context.getTagDeclType(VBaseDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1283 | HadError = true; |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1284 | continue; |
| 1285 | } |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1286 | |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1287 | ASTOwningVector<&ActionBase::DeleteExpr> CtorArgs(*this); |
| 1288 | if (CompleteConstructorCall(Ctor, MultiExprArg(*this, 0, 0), |
| 1289 | Constructor->getLocation(), CtorArgs)) |
| 1290 | continue; |
| 1291 | |
| 1292 | MarkDeclarationReferenced(Constructor->getLocation(), Ctor); |
| 1293 | |
Anders Carlsson | 8db68da | 2009-11-13 20:11:49 +0000 | [diff] [blame] | 1294 | // FIXME: CXXBaseOrMemberInitializer should only contain a single |
| 1295 | // subexpression so we can wrap it in a CXXExprWithTemporaries if necessary. |
| 1296 | ExprTemporaries.clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | CXXBaseOrMemberInitializer *Member = |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1298 | new (Context) CXXBaseOrMemberInitializer(VBase->getType(), |
| 1299 | CtorArgs.takeAs<Expr>(), |
| 1300 | CtorArgs.size(), Ctor, |
| 1301 | SourceLocation(), |
| 1302 | SourceLocation()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1303 | AllToInit.push_back(Member); |
| 1304 | } |
| 1305 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1307 | for (CXXRecordDecl::base_class_iterator Base = |
| 1308 | ClassDecl->bases_begin(), |
| 1309 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1310 | // Virtuals are in the virtual base list and already constructed. |
| 1311 | if (Base->isVirtual()) |
| 1312 | continue; |
| 1313 | // Skip dependent types. |
| 1314 | if (Base->getType()->isDependentType()) |
| 1315 | continue; |
Douglas Gregor | c07a494 | 2009-11-15 08:51:10 +0000 | [diff] [blame] | 1316 | if (CXXBaseOrMemberInitializer *Value |
| 1317 | = AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) { |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1318 | AllToInit.push_back(Value); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1319 | } |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1320 | else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | CXXRecordDecl *BaseDecl = |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1322 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1323 | assert(BaseDecl && "SetBaseOrMemberInitializers - BaseDecl null"); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1324 | CXXConstructorDecl *Ctor = BaseDecl->getDefaultConstructor(Context); |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1325 | if (!Ctor) { |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1326 | Diag(Constructor->getLocation(), diag::err_missing_default_ctor) |
| 1327 | << (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl) |
| 1328 | << 0 << Base->getType(); |
| 1329 | Diag(BaseDecl->getLocation(), diag::note_previous_class_decl) |
| 1330 | << Context.getTagDeclType(BaseDecl); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1331 | HadError = true; |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1332 | continue; |
| 1333 | } |
| 1334 | |
| 1335 | ASTOwningVector<&ActionBase::DeleteExpr> CtorArgs(*this); |
| 1336 | if (CompleteConstructorCall(Ctor, MultiExprArg(*this, 0, 0), |
| 1337 | Constructor->getLocation(), CtorArgs)) |
| 1338 | continue; |
| 1339 | |
| 1340 | MarkDeclarationReferenced(Constructor->getLocation(), Ctor); |
Fariborz Jahanian | 9d43620 | 2009-09-03 21:32:41 +0000 | [diff] [blame] | 1341 | |
Anders Carlsson | 8db68da | 2009-11-13 20:11:49 +0000 | [diff] [blame] | 1342 | // FIXME: CXXBaseOrMemberInitializer should only contain a single |
| 1343 | // subexpression so we can wrap it in a CXXExprWithTemporaries if necessary. |
| 1344 | ExprTemporaries.clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1345 | CXXBaseOrMemberInitializer *Member = |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1346 | new (Context) CXXBaseOrMemberInitializer(Base->getType(), |
| 1347 | CtorArgs.takeAs<Expr>(), |
| 1348 | CtorArgs.size(), Ctor, |
| 1349 | SourceLocation(), |
| 1350 | SourceLocation()); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1351 | AllToInit.push_back(Member); |
| 1352 | } |
| 1353 | } |
| 1354 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1355 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1356 | // non-static data members. |
| 1357 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1358 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 1359 | if ((*Field)->isAnonymousStructOrUnion()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | if (const RecordType *FieldClassType = |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1361 | Field->getType()->getAs<RecordType>()) { |
| 1362 | CXXRecordDecl *FieldClassDecl |
Douglas Gregor | afe7ec2 | 2009-11-13 18:34:26 +0000 | [diff] [blame] | 1363 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | for (RecordDecl::field_iterator FA = FieldClassDecl->field_begin(), |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1365 | EA = FieldClassDecl->field_end(); FA != EA; FA++) { |
| 1366 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*FA)) { |
| 1367 | // 'Member' is the anonymous union field and 'AnonUnionMember' is |
| 1368 | // set to the anonymous union data member used in the initializer |
| 1369 | // list. |
| 1370 | Value->setMember(*Field); |
| 1371 | Value->setAnonUnionMember(*FA); |
| 1372 | AllToInit.push_back(Value); |
| 1373 | break; |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | continue; |
| 1378 | } |
| 1379 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*Field)) { |
| 1380 | AllToInit.push_back(Value); |
| 1381 | continue; |
| 1382 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1384 | if ((*Field)->getType()->isDependentType()) |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame] | 1385 | continue; |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame] | 1386 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1387 | QualType FT = Context.getBaseElementType((*Field)->getType()); |
| 1388 | if (const RecordType* RT = FT->getAs<RecordType>()) { |
| 1389 | CXXConstructorDecl *Ctor = |
| 1390 | cast<CXXRecordDecl>(RT->getDecl())->getDefaultConstructor(Context); |
Douglas Gregor | 1fe6b91 | 2009-11-04 17:16:11 +0000 | [diff] [blame] | 1391 | if (!Ctor) { |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1392 | Diag(Constructor->getLocation(), diag::err_missing_default_ctor) |
| 1393 | << (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl) |
| 1394 | << 1 << (*Field)->getDeclName(); |
| 1395 | Diag(Field->getLocation(), diag::note_field_decl); |
| 1396 | Diag(RT->getDecl()->getLocation(), diag::note_previous_class_decl) |
| 1397 | << Context.getTagDeclType(RT->getDecl()); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1398 | HadError = true; |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1399 | continue; |
| 1400 | } |
Eli Friedman | e73d3bc | 2009-11-16 23:07:59 +0000 | [diff] [blame] | 1401 | |
| 1402 | if (FT.isConstQualified() && Ctor->isTrivial()) { |
| 1403 | Diag(Constructor->getLocation(), diag::err_unintialized_member_in_ctor) |
| 1404 | << (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl) |
| 1405 | << 1 << (*Field)->getDeclName(); |
| 1406 | Diag((*Field)->getLocation(), diag::note_declared_at); |
| 1407 | HadError = true; |
| 1408 | } |
| 1409 | |
| 1410 | // Don't create initializers for trivial constructors, since they don't |
| 1411 | // actually need to be run. |
| 1412 | if (Ctor->isTrivial()) |
| 1413 | continue; |
| 1414 | |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1415 | ASTOwningVector<&ActionBase::DeleteExpr> CtorArgs(*this); |
| 1416 | if (CompleteConstructorCall(Ctor, MultiExprArg(*this, 0, 0), |
| 1417 | Constructor->getLocation(), CtorArgs)) |
| 1418 | continue; |
| 1419 | |
Anders Carlsson | 8db68da | 2009-11-13 20:11:49 +0000 | [diff] [blame] | 1420 | // FIXME: CXXBaseOrMemberInitializer should only contain a single |
| 1421 | // subexpression so we can wrap it in a CXXExprWithTemporaries if necessary. |
| 1422 | ExprTemporaries.clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | CXXBaseOrMemberInitializer *Member = |
Anders Carlsson | 0ebb6d3 | 2009-10-29 15:46:07 +0000 | [diff] [blame] | 1424 | new (Context) CXXBaseOrMemberInitializer(*Field,CtorArgs.takeAs<Expr>(), |
| 1425 | CtorArgs.size(), Ctor, |
| 1426 | SourceLocation(), |
| 1427 | SourceLocation()); |
| 1428 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1429 | AllToInit.push_back(Member); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1430 | MarkDeclarationReferenced(Constructor->getLocation(), Ctor); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1431 | } |
| 1432 | else if (FT->isReferenceType()) { |
| 1433 | Diag(Constructor->getLocation(), diag::err_unintialized_member_in_ctor) |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1434 | << (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl) |
| 1435 | << 0 << (*Field)->getDeclName(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1436 | Diag((*Field)->getLocation(), diag::note_declared_at); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1437 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1438 | } |
| 1439 | else if (FT.isConstQualified()) { |
| 1440 | Diag(Constructor->getLocation(), diag::err_unintialized_member_in_ctor) |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1441 | << (int)IsImplicitConstructor << Context.getTagDeclType(ClassDecl) |
| 1442 | << 1 << (*Field)->getDeclName(); |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1443 | Diag((*Field)->getLocation(), diag::note_declared_at); |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1444 | HadError = true; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1445 | } |
| 1446 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1448 | NumInitializers = AllToInit.size(); |
| 1449 | if (NumInitializers > 0) { |
| 1450 | Constructor->setNumBaseOrMemberInitializers(NumInitializers); |
| 1451 | CXXBaseOrMemberInitializer **baseOrMemberInitializers = |
| 1452 | new (Context) CXXBaseOrMemberInitializer*[NumInitializers]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1454 | Constructor->setBaseOrMemberInitializers(baseOrMemberInitializers); |
| 1455 | for (unsigned Idx = 0; Idx < NumInitializers; ++Idx) |
| 1456 | baseOrMemberInitializers[Idx] = AllToInit[Idx]; |
| 1457 | } |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 1458 | |
| 1459 | return HadError; |
Fariborz Jahanian | 80545ad | 2009-09-03 19:36:46 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1462 | static void *GetKeyForTopLevelField(FieldDecl *Field) { |
| 1463 | // For anonymous unions, use the class declaration as the key. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1464 | if (const RecordType *RT = Field->getType()->getAs<RecordType>()) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1465 | if (RT->getDecl()->isAnonymousStructOrUnion()) |
| 1466 | return static_cast<void *>(RT->getDecl()); |
| 1467 | } |
| 1468 | return static_cast<void *>(Field); |
| 1469 | } |
| 1470 | |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1471 | static void *GetKeyForBase(QualType BaseType) { |
| 1472 | if (const RecordType *RT = BaseType->getAs<RecordType>()) |
| 1473 | return (void *)RT; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1475 | assert(0 && "Unexpected base type!"); |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | static void *GetKeyForMember(CXXBaseOrMemberInitializer *Member, |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1480 | bool MemberMaybeAnon = false) { |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1481 | // For fields injected into the class via declaration of an anonymous union, |
| 1482 | // use its anonymous union class declaration as the unique key. |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1483 | if (Member->isMemberInitializer()) { |
| 1484 | FieldDecl *Field = Member->getMember(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1486 | // After SetBaseOrMemberInitializers call, Field is the anonymous union |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | // data member of the class. Data member used in the initializer list is |
Fariborz Jahanian | e649412 | 2009-08-11 18:49:54 +0000 | [diff] [blame] | 1488 | // in AnonUnionMember field. |
| 1489 | if (MemberMaybeAnon && Field->isAnonymousStructOrUnion()) |
| 1490 | Field = Member->getAnonUnionMember(); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1491 | if (Field->getDeclContext()->isRecord()) { |
| 1492 | RecordDecl *RD = cast<RecordDecl>(Field->getDeclContext()); |
| 1493 | if (RD->isAnonymousStructOrUnion()) |
| 1494 | return static_cast<void *>(RD); |
| 1495 | } |
| 1496 | return static_cast<void *>(Field); |
| 1497 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1499 | return GetKeyForBase(QualType(Member->getBaseClass(), 0)); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
John McCall | 6aee621 | 2009-11-04 23:13:52 +0000 | [diff] [blame] | 1502 | /// ActOnMemInitializers - Handle the member initializers for a constructor. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | void Sema::ActOnMemInitializers(DeclPtrTy ConstructorDecl, |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1504 | SourceLocation ColonLoc, |
| 1505 | MemInitTy **MemInits, unsigned NumMemInits) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1506 | if (!ConstructorDecl) |
| 1507 | return; |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1508 | |
| 1509 | AdjustDeclIfTemplate(ConstructorDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1510 | |
| 1511 | CXXConstructorDecl *Constructor |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1512 | = dyn_cast<CXXConstructorDecl>(ConstructorDecl.getAs<Decl>()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1514 | if (!Constructor) { |
| 1515 | Diag(ColonLoc, diag::err_only_constructors_take_base_inits); |
| 1516 | return; |
| 1517 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1519 | if (!Constructor->isDependentContext()) { |
| 1520 | llvm::DenseMap<void*, CXXBaseOrMemberInitializer *>Members; |
| 1521 | bool err = false; |
| 1522 | for (unsigned i = 0; i < NumMemInits; i++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | CXXBaseOrMemberInitializer *Member = |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1524 | static_cast<CXXBaseOrMemberInitializer*>(MemInits[i]); |
| 1525 | void *KeyToMember = GetKeyForMember(Member); |
| 1526 | CXXBaseOrMemberInitializer *&PrevMember = Members[KeyToMember]; |
| 1527 | if (!PrevMember) { |
| 1528 | PrevMember = Member; |
| 1529 | continue; |
| 1530 | } |
| 1531 | if (FieldDecl *Field = Member->getMember()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | Diag(Member->getSourceLocation(), |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1533 | diag::error_multiple_mem_initialization) |
| 1534 | << Field->getNameAsString(); |
| 1535 | else { |
| 1536 | Type *BaseClass = Member->getBaseClass(); |
| 1537 | assert(BaseClass && "ActOnMemInitializers - neither field or base"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1538 | Diag(Member->getSourceLocation(), |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1539 | diag::error_multiple_base_initialization) |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 1540 | << QualType(BaseClass, 0); |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1541 | } |
| 1542 | Diag(PrevMember->getSourceLocation(), diag::note_previous_initializer) |
| 1543 | << 0; |
| 1544 | err = true; |
| 1545 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1546 | |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1547 | if (err) |
| 1548 | return; |
| 1549 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1551 | SetBaseOrMemberInitializers(Constructor, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | reinterpret_cast<CXXBaseOrMemberInitializer **>(MemInits), |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1553 | NumMemInits, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | |
Anders Carlsson | 8d4c5ea | 2009-08-27 05:57:30 +0000 | [diff] [blame] | 1555 | if (Constructor->isDependentContext()) |
| 1556 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1557 | |
| 1558 | if (Diags.getDiagnosticLevel(diag::warn_base_initialized) == |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1559 | Diagnostic::Ignored && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | Diags.getDiagnosticLevel(diag::warn_field_initialized) == |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1561 | Diagnostic::Ignored) |
| 1562 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1564 | // Also issue warning if order of ctor-initializer list does not match order |
| 1565 | // of 1) base class declarations and 2) order of non-static data members. |
| 1566 | llvm::SmallVector<const void*, 32> AllBaseOrMembers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1568 | CXXRecordDecl *ClassDecl |
| 1569 | = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 1570 | // Push virtual bases before others. |
| 1571 | for (CXXRecordDecl::base_class_iterator VBase = |
| 1572 | ClassDecl->vbases_begin(), |
| 1573 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1574 | AllBaseOrMembers.push_back(GetKeyForBase(VBase->getType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1576 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1577 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1578 | // Virtuals are alread in the virtual base list and are constructed |
| 1579 | // first. |
| 1580 | if (Base->isVirtual()) |
| 1581 | continue; |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1582 | AllBaseOrMembers.push_back(GetKeyForBase(Base->getType())); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1583 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1585 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 1586 | E = ClassDecl->field_end(); Field != E; ++Field) |
| 1587 | AllBaseOrMembers.push_back(GetKeyForTopLevelField(*Field)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1589 | int Last = AllBaseOrMembers.size(); |
| 1590 | int curIndex = 0; |
| 1591 | CXXBaseOrMemberInitializer *PrevMember = 0; |
| 1592 | for (unsigned i = 0; i < NumMemInits; i++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1593 | CXXBaseOrMemberInitializer *Member = |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1594 | static_cast<CXXBaseOrMemberInitializer*>(MemInits[i]); |
| 1595 | void *MemberInCtorList = GetKeyForMember(Member, true); |
Eli Friedman | 6347f42 | 2009-07-21 19:28:10 +0000 | [diff] [blame] | 1596 | |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1597 | for (; curIndex < Last; curIndex++) |
| 1598 | if (MemberInCtorList == AllBaseOrMembers[curIndex]) |
| 1599 | break; |
| 1600 | if (curIndex == Last) { |
| 1601 | assert(PrevMember && "Member not in member list?!"); |
| 1602 | // Initializer as specified in ctor-initializer list is out of order. |
| 1603 | // Issue a warning diagnostic. |
| 1604 | if (PrevMember->isBaseInitializer()) { |
| 1605 | // Diagnostics is for an initialized base class. |
| 1606 | Type *BaseClass = PrevMember->getBaseClass(); |
| 1607 | Diag(PrevMember->getSourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1608 | diag::warn_base_initialized) |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 1609 | << QualType(BaseClass, 0); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1610 | } else { |
| 1611 | FieldDecl *Field = PrevMember->getMember(); |
| 1612 | Diag(PrevMember->getSourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | diag::warn_field_initialized) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1614 | << Field->getNameAsString(); |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1615 | } |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1616 | // Also the note! |
| 1617 | if (FieldDecl *Field = Member->getMember()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | Diag(Member->getSourceLocation(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1619 | diag::note_fieldorbase_initialized_here) << 0 |
| 1620 | << Field->getNameAsString(); |
| 1621 | else { |
| 1622 | Type *BaseClass = Member->getBaseClass(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | Diag(Member->getSourceLocation(), |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1624 | diag::note_fieldorbase_initialized_here) << 1 |
John McCall | bf1cc05 | 2009-09-29 23:03:30 +0000 | [diff] [blame] | 1625 | << QualType(BaseClass, 0); |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1626 | } |
| 1627 | for (curIndex = 0; curIndex < Last; curIndex++) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | if (MemberInCtorList == AllBaseOrMembers[curIndex]) |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1629 | break; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1630 | } |
Anders Carlsson | 5c36fb2 | 2009-08-27 05:45:01 +0000 | [diff] [blame] | 1631 | PrevMember = Member; |
Fariborz Jahanian | eb96e12 | 2009-07-09 19:59:47 +0000 | [diff] [blame] | 1632 | } |
Anders Carlsson | a7b3521 | 2009-03-25 02:58:17 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1635 | void |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 1636 | Sema::MarkBaseAndMemberDestructorsReferenced(CXXDestructorDecl *Destructor) { |
| 1637 | // Ignore dependent destructors. |
| 1638 | if (Destructor->isDependentContext()) |
| 1639 | return; |
| 1640 | |
| 1641 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 1643 | // Non-static data members. |
| 1644 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 1645 | E = ClassDecl->field_end(); I != E; ++I) { |
| 1646 | FieldDecl *Field = *I; |
| 1647 | |
| 1648 | QualType FieldType = Context.getBaseElementType(Field->getType()); |
| 1649 | |
| 1650 | const RecordType* RT = FieldType->getAs<RecordType>(); |
| 1651 | if (!RT) |
| 1652 | continue; |
| 1653 | |
| 1654 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1655 | if (FieldClassDecl->hasTrivialDestructor()) |
| 1656 | continue; |
| 1657 | |
| 1658 | const CXXDestructorDecl *Dtor = FieldClassDecl->getDestructor(Context); |
| 1659 | MarkDeclarationReferenced(Destructor->getLocation(), |
| 1660 | const_cast<CXXDestructorDecl*>(Dtor)); |
| 1661 | } |
| 1662 | |
| 1663 | // Bases. |
| 1664 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 1665 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 1666 | // Ignore virtual bases. |
| 1667 | if (Base->isVirtual()) |
| 1668 | continue; |
| 1669 | |
| 1670 | // Ignore trivial destructors. |
| 1671 | CXXRecordDecl *BaseClassDecl |
| 1672 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 1673 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1674 | continue; |
| 1675 | |
| 1676 | const CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context); |
| 1677 | MarkDeclarationReferenced(Destructor->getLocation(), |
| 1678 | const_cast<CXXDestructorDecl*>(Dtor)); |
| 1679 | } |
| 1680 | |
| 1681 | // Virtual bases. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1682 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 1683 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 1684 | // Ignore trivial destructors. |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1685 | CXXRecordDecl *BaseClassDecl |
| 1686 | = cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl()); |
| 1687 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1688 | continue; |
Anders Carlsson | 9f853df | 2009-11-17 04:44:12 +0000 | [diff] [blame] | 1689 | |
| 1690 | const CXXDestructorDecl *Dtor = BaseClassDecl->getDestructor(Context); |
| 1691 | MarkDeclarationReferenced(Destructor->getLocation(), |
| 1692 | const_cast<CXXDestructorDecl*>(Dtor)); |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 1696 | void Sema::ActOnDefaultCtorInitializers(DeclPtrTy CDtorDecl) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 1697 | if (!CDtorDecl) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 1698 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 1700 | AdjustDeclIfTemplate(CDtorDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | |
| 1702 | if (CXXConstructorDecl *Constructor |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 1703 | = dyn_cast<CXXConstructorDecl>(CDtorDecl.getAs<Decl>())) |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 1704 | SetBaseOrMemberInitializers(Constructor, 0, 0, false); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1707 | namespace { |
| 1708 | /// PureVirtualMethodCollector - traverses a class and its superclasses |
| 1709 | /// and determines if it has any pure virtual methods. |
| 1710 | class VISIBILITY_HIDDEN PureVirtualMethodCollector { |
| 1711 | ASTContext &Context; |
| 1712 | |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 1713 | public: |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1714 | typedef llvm::SmallVector<const CXXMethodDecl*, 8> MethodList; |
Sebastian Redl | dfe292d | 2009-03-22 21:28:55 +0000 | [diff] [blame] | 1715 | |
| 1716 | private: |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1717 | MethodList Methods; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1719 | void Collect(const CXXRecordDecl* RD, MethodList& Methods); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1721 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1722 | PureVirtualMethodCollector(ASTContext &Ctx, const CXXRecordDecl* RD) |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1723 | : Context(Ctx) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1724 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1725 | MethodList List; |
| 1726 | Collect(RD, List); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1727 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1728 | // Copy the temporary list to methods, and make sure to ignore any |
| 1729 | // null entries. |
| 1730 | for (size_t i = 0, e = List.size(); i != e; ++i) { |
| 1731 | if (List[i]) |
| 1732 | Methods.push_back(List[i]); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | } |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1734 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1736 | bool empty() const { return Methods.empty(); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1738 | MethodList::const_iterator methods_begin() { return Methods.begin(); } |
| 1739 | MethodList::const_iterator methods_end() { return Methods.end(); } |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1740 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1741 | |
| 1742 | void PureVirtualMethodCollector::Collect(const CXXRecordDecl* RD, |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1743 | MethodList& Methods) { |
| 1744 | // First, collect the pure virtual methods for the base classes. |
| 1745 | for (CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(), |
| 1746 | BaseEnd = RD->bases_end(); Base != BaseEnd; ++Base) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1747 | if (const RecordType *RT = Base->getType()->getAs<RecordType>()) { |
Chris Lattner | 64540d7 | 2009-03-29 05:01:10 +0000 | [diff] [blame] | 1748 | const CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1749 | if (BaseDecl && BaseDecl->isAbstract()) |
| 1750 | Collect(BaseDecl, Methods); |
| 1751 | } |
| 1752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1754 | // Next, zero out any pure virtual methods that this class overrides. |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1755 | typedef llvm::SmallPtrSet<const CXXMethodDecl*, 4> MethodSetTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1757 | MethodSetTy OverriddenMethods; |
| 1758 | size_t MethodsSize = Methods.size(); |
| 1759 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1760 | for (RecordDecl::decl_iterator i = RD->decls_begin(), e = RD->decls_end(); |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1761 | i != e; ++i) { |
| 1762 | // Traverse the record, looking for methods. |
| 1763 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*i)) { |
Sebastian Redl | 23c7d06 | 2009-07-07 20:29:57 +0000 | [diff] [blame] | 1764 | // If the method is pure virtual, add it to the methods vector. |
Anders Carlsson | 2782302 | 2009-10-18 19:34:08 +0000 | [diff] [blame] | 1765 | if (MD->isPure()) |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1766 | Methods.push_back(MD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | |
Anders Carlsson | 2782302 | 2009-10-18 19:34:08 +0000 | [diff] [blame] | 1768 | // Record all the overridden methods in our set. |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1769 | for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), |
| 1770 | E = MD->end_overridden_methods(); I != E; ++I) { |
| 1771 | // Keep track of the overridden methods. |
| 1772 | OverriddenMethods.insert(*I); |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1773 | } |
| 1774 | } |
| 1775 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1776 | |
| 1777 | // Now go through the methods and zero out all the ones we know are |
Anders Carlsson | 8ff8c22 | 2009-05-17 00:00:05 +0000 | [diff] [blame] | 1778 | // overridden. |
| 1779 | for (size_t i = 0, e = MethodsSize; i != e; ++i) { |
| 1780 | if (OverriddenMethods.count(Methods[i])) |
| 1781 | Methods[i] = 0; |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1784 | } |
| 1785 | } |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1786 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1787 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1789 | unsigned DiagID, AbstractDiagSelID SelID, |
| 1790 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1791 | if (SelID == -1) |
| 1792 | return RequireNonAbstractType(Loc, T, |
| 1793 | PDiag(DiagID), CurrentRD); |
| 1794 | else |
| 1795 | return RequireNonAbstractType(Loc, T, |
| 1796 | PDiag(DiagID) << SelID, CurrentRD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1799 | bool Sema::RequireNonAbstractType(SourceLocation Loc, QualType T, |
| 1800 | const PartialDiagnostic &PD, |
| 1801 | const CXXRecordDecl *CurrentRD) { |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1802 | if (!getLangOptions().CPlusPlus) |
| 1803 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1804 | |
Anders Carlsson | 11f21a0 | 2009-03-23 19:10:31 +0000 | [diff] [blame] | 1805 | if (const ArrayType *AT = Context.getAsArrayType(T)) |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1806 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1807 | CurrentRD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1808 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1809 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1810 | // Find the innermost pointer type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1811 | while (const PointerType *T = PT->getPointeeType()->getAs<PointerType>()) |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1812 | PT = T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1814 | if (const ArrayType *AT = Context.getAsArrayType(PT->getPointeeType())) |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1815 | return RequireNonAbstractType(Loc, AT->getElementType(), PD, CurrentRD); |
Anders Carlsson | 5eff73c | 2009-03-24 01:46:45 +0000 | [diff] [blame] | 1816 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1818 | const RecordType *RT = T->getAs<RecordType>(); |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1819 | if (!RT) |
| 1820 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1821 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1822 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 1823 | if (!RD) |
| 1824 | return false; |
| 1825 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1826 | if (CurrentRD && CurrentRD != RD) |
| 1827 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1829 | if (!RD->isAbstract()) |
| 1830 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1831 | |
Anders Carlsson | a6ec7ad | 2009-08-27 00:13:57 +0000 | [diff] [blame] | 1832 | Diag(Loc, PD) << RD->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1834 | // Check if we've already emitted the list of pure virtual functions for this |
| 1835 | // class. |
| 1836 | if (PureVirtualClassDiagSet && PureVirtualClassDiagSet->count(RD)) |
| 1837 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1839 | PureVirtualMethodCollector Collector(Context, RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | |
| 1841 | for (PureVirtualMethodCollector::MethodList::const_iterator I = |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1842 | Collector.methods_begin(), E = Collector.methods_end(); I != E; ++I) { |
| 1843 | const CXXMethodDecl *MD = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1844 | |
| 1845 | Diag(MD->getLocation(), diag::note_pure_virtual_function) << |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1846 | MD->getDeclName(); |
| 1847 | } |
| 1848 | |
| 1849 | if (!PureVirtualClassDiagSet) |
| 1850 | PureVirtualClassDiagSet.reset(new RecordDeclSetTy); |
| 1851 | PureVirtualClassDiagSet->insert(RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | |
Anders Carlsson | 4681ebd | 2009-03-22 20:18:17 +0000 | [diff] [blame] | 1853 | return true; |
| 1854 | } |
| 1855 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1856 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | class VISIBILITY_HIDDEN AbstractClassUsageDiagnoser |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1858 | : public DeclVisitor<AbstractClassUsageDiagnoser, bool> { |
| 1859 | Sema &SemaRef; |
| 1860 | CXXRecordDecl *AbstractClass; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1862 | bool VisitDeclContext(const DeclContext *DC) { |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1863 | bool Invalid = false; |
| 1864 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1865 | for (CXXRecordDecl::decl_iterator I = DC->decls_begin(), |
| 1866 | E = DC->decls_end(); I != E; ++I) |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1867 | Invalid |= Visit(*I); |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1868 | |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1869 | return Invalid; |
| 1870 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1871 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1872 | public: |
| 1873 | AbstractClassUsageDiagnoser(Sema& SemaRef, CXXRecordDecl *ac) |
| 1874 | : SemaRef(SemaRef), AbstractClass(ac) { |
| 1875 | Visit(SemaRef.Context.getTranslationUnitDecl()); |
| 1876 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1877 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1878 | bool VisitFunctionDecl(const FunctionDecl *FD) { |
| 1879 | if (FD->isThisDeclarationADefinition()) { |
| 1880 | // No need to do the check if we're in a definition, because it requires |
| 1881 | // that the return/param types are complete. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1882 | // because that requires |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1883 | return VisitDeclContext(FD); |
| 1884 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1886 | // Check the return type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1887 | QualType RTy = FD->getType()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1888 | bool Invalid = |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1889 | SemaRef.RequireNonAbstractType(FD->getLocation(), RTy, |
| 1890 | diag::err_abstract_type_in_decl, |
| 1891 | Sema::AbstractReturnType, |
| 1892 | AbstractClass); |
| 1893 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | for (FunctionDecl::param_const_iterator I = FD->param_begin(), |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1895 | E = FD->param_end(); I != E; ++I) { |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1896 | const ParmVarDecl *VD = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1897 | Invalid |= |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1898 | SemaRef.RequireNonAbstractType(VD->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1899 | VD->getOriginalType(), |
| 1900 | diag::err_abstract_type_in_decl, |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1901 | Sema::AbstractParamType, |
| 1902 | AbstractClass); |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | return Invalid; |
| 1906 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1908 | bool VisitDecl(const Decl* D) { |
| 1909 | if (const DeclContext *DC = dyn_cast<DeclContext>(D)) |
| 1910 | return VisitDeclContext(DC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | |
Anders Carlsson | e65a3c8 | 2009-03-24 17:23:42 +0000 | [diff] [blame] | 1912 | return false; |
| 1913 | } |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 1914 | }; |
| 1915 | } |
| 1916 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1917 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1918 | DeclPtrTy TagDecl, |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1919 | SourceLocation LBrac, |
| 1920 | SourceLocation RBrac) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 1921 | if (!TagDecl) |
| 1922 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 1924 | AdjustDeclIfTemplate(TagDecl); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1925 | ActOnFields(S, RLoc, TagDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1926 | (DeclPtrTy*)FieldCollector->getCurFields(), |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1927 | FieldCollector->getCurNumFields(), LBrac, RBrac, 0); |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1928 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1929 | CXXRecordDecl *RD = cast<CXXRecordDecl>(TagDecl.getAs<Decl>()); |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1930 | if (!RD->isAbstract()) { |
| 1931 | // Collect all the pure virtual methods and see if this is an abstract |
| 1932 | // class after all. |
| 1933 | PureVirtualMethodCollector Collector(Context, RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | if (!Collector.empty()) |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1935 | RD->setAbstract(true); |
| 1936 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | |
| 1938 | if (RD->isAbstract()) |
Douglas Gregor | 6490ae5 | 2009-11-17 06:14:37 +0000 | [diff] [blame^] | 1939 | (void)AbstractClassUsageDiagnoser(*this, RD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1940 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1941 | if (!RD->isDependentType() && !RD->isInvalidDecl()) |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 1942 | AddImplicitlyDeclaredMembersToClass(RD); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1945 | /// AddImplicitlyDeclaredMembersToClass - Adds any implicitly-declared |
| 1946 | /// special functions, such as the default constructor, copy |
| 1947 | /// constructor, or destructor, to the given C++ class (C++ |
| 1948 | /// [special]p1). This routine can only be executed just before the |
| 1949 | /// definition of the class is complete. |
| 1950 | void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | CanQualType ClassType |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 1952 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1953 | |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1954 | // FIXME: Implicit declarations have exception specifications, which are |
| 1955 | // the union of the specifications of the implicitly called functions. |
| 1956 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1957 | if (!ClassDecl->hasUserDeclaredConstructor()) { |
| 1958 | // C++ [class.ctor]p5: |
| 1959 | // A default constructor for a class X is a constructor of class X |
| 1960 | // that can be called without an argument. If there is no |
| 1961 | // user-declared constructor for class X, a default constructor is |
| 1962 | // implicitly declared. An implicitly-declared default constructor |
| 1963 | // is an inline public member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1964 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1965 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1966 | CXXConstructorDecl *DefaultCon = |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1967 | CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1968 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1969 | Context.getFunctionType(Context.VoidTy, |
| 1970 | 0, 0, false, 0), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1971 | /*DInfo=*/0, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1972 | /*isExplicit=*/false, |
| 1973 | /*isInline=*/true, |
| 1974 | /*isImplicitlyDeclared=*/true); |
| 1975 | DefaultCon->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 1976 | DefaultCon->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 1977 | DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1978 | ClassDecl->addDecl(DefaultCon); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | if (!ClassDecl->hasUserDeclaredCopyConstructor()) { |
| 1982 | // C++ [class.copy]p4: |
| 1983 | // If the class definition does not explicitly declare a copy |
| 1984 | // constructor, one is declared implicitly. |
| 1985 | |
| 1986 | // C++ [class.copy]p5: |
| 1987 | // The implicitly-declared copy constructor for a class X will |
| 1988 | // have the form |
| 1989 | // |
| 1990 | // X::X(const X&) |
| 1991 | // |
| 1992 | // if |
| 1993 | bool HasConstCopyConstructor = true; |
| 1994 | |
| 1995 | // -- each direct or virtual base class B of X has a copy |
| 1996 | // constructor whose first parameter is of type const B& or |
| 1997 | // const volatile B&, and |
| 1998 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 1999 | HasConstCopyConstructor && Base != ClassDecl->bases_end(); ++Base) { |
| 2000 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2001 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | HasConstCopyConstructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2003 | = BaseClassDecl->hasConstCopyConstructor(Context); |
| 2004 | } |
| 2005 | |
| 2006 | // -- for all the nonstatic data members of X that are of a |
| 2007 | // class type M (or array thereof), each such class type |
| 2008 | // has a copy constructor whose first parameter is of type |
| 2009 | // const M& or const volatile M&. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2010 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 2011 | HasConstCopyConstructor && Field != ClassDecl->field_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2012 | ++Field) { |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2013 | QualType FieldType = (*Field)->getType(); |
| 2014 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2015 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2016 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | const CXXRecordDecl *FieldClassDecl |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2018 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | HasConstCopyConstructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2020 | = FieldClassDecl->hasConstCopyConstructor(Context); |
| 2021 | } |
| 2022 | } |
| 2023 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2024 | // Otherwise, the implicitly declared copy constructor will have |
| 2025 | // the form |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2026 | // |
| 2027 | // X::X(X&) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2028 | QualType ArgType = ClassType; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2029 | if (HasConstCopyConstructor) |
| 2030 | ArgType = ArgType.withConst(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2031 | ArgType = Context.getLValueReferenceType(ArgType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2032 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2033 | // An implicitly-declared copy constructor is an inline public |
| 2034 | // member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2036 | = Context.DeclarationNames.getCXXConstructorName(ClassType); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2037 | CXXConstructorDecl *CopyConstructor |
| 2038 | = CXXConstructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2039 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2040 | Context.getFunctionType(Context.VoidTy, |
| 2041 | &ArgType, 1, |
| 2042 | false, 0), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2043 | /*DInfo=*/0, |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2044 | /*isExplicit=*/false, |
| 2045 | /*isInline=*/true, |
| 2046 | /*isImplicitlyDeclared=*/true); |
| 2047 | CopyConstructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2048 | CopyConstructor->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2049 | CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor()); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2050 | |
| 2051 | // Add the parameter to the constructor. |
| 2052 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyConstructor, |
| 2053 | ClassDecl->getLocation(), |
| 2054 | /*IdentifierInfo=*/0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2055 | ArgType, /*DInfo=*/0, |
| 2056 | VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 2057 | CopyConstructor->setParams(Context, &FromParam, 1); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2058 | ClassDecl->addDecl(CopyConstructor); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2061 | if (!ClassDecl->hasUserDeclaredCopyAssignment()) { |
| 2062 | // Note: The following rules are largely analoguous to the copy |
| 2063 | // constructor rules. Note that virtual bases are not taken into account |
| 2064 | // for determining the argument type of the operator. Note also that |
| 2065 | // operators taking an object instead of a reference are allowed. |
| 2066 | // |
| 2067 | // C++ [class.copy]p10: |
| 2068 | // If the class definition does not explicitly declare a copy |
| 2069 | // assignment operator, one is declared implicitly. |
| 2070 | // The implicitly-defined copy assignment operator for a class X |
| 2071 | // will have the form |
| 2072 | // |
| 2073 | // X& X::operator=(const X&) |
| 2074 | // |
| 2075 | // if |
| 2076 | bool HasConstCopyAssignment = true; |
| 2077 | |
| 2078 | // -- each direct base class B of X has a copy assignment operator |
| 2079 | // whose parameter is of type const B&, const volatile B& or B, |
| 2080 | // and |
| 2081 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2082 | HasConstCopyAssignment && Base != ClassDecl->bases_end(); ++Base) { |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 2083 | assert(!Base->getType()->isDependentType() && |
| 2084 | "Cannot generate implicit members for class with dependent bases."); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2085 | const CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2086 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2087 | const CXXMethodDecl *MD = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2088 | HasConstCopyAssignment = BaseClassDecl->hasConstCopyAssignment(Context, |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2089 | MD); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | // -- for all the nonstatic data members of X that are of a class |
| 2093 | // type M (or array thereof), each such class type has a copy |
| 2094 | // assignment operator whose parameter is of type const M&, |
| 2095 | // const volatile M& or M. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2096 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(); |
| 2097 | HasConstCopyAssignment && Field != ClassDecl->field_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2098 | ++Field) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2099 | QualType FieldType = (*Field)->getType(); |
| 2100 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 2101 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2102 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2103 | const CXXRecordDecl *FieldClassDecl |
| 2104 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2105 | const CXXMethodDecl *MD = 0; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2106 | HasConstCopyAssignment |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 2107 | = FieldClassDecl->hasConstCopyAssignment(Context, MD); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | // Otherwise, the implicitly declared copy assignment operator will |
| 2112 | // have the form |
| 2113 | // |
| 2114 | // X& X::operator=(X&) |
| 2115 | QualType ArgType = ClassType; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2116 | QualType RetType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2117 | if (HasConstCopyAssignment) |
| 2118 | ArgType = ArgType.withConst(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2119 | ArgType = Context.getLValueReferenceType(ArgType); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2120 | |
| 2121 | // An implicitly-declared copy assignment operator is an inline public |
| 2122 | // member of its class. |
| 2123 | DeclarationName Name = |
| 2124 | Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 2125 | CXXMethodDecl *CopyAssignment = |
| 2126 | CXXMethodDecl::Create(Context, ClassDecl, ClassDecl->getLocation(), Name, |
| 2127 | Context.getFunctionType(RetType, &ArgType, 1, |
| 2128 | false, 0), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2129 | /*DInfo=*/0, /*isStatic=*/false, /*isInline=*/true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2130 | CopyAssignment->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2131 | CopyAssignment->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2132 | CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment()); |
Fariborz Jahanian | 2198ba1 | 2009-08-12 21:14:35 +0000 | [diff] [blame] | 2133 | CopyAssignment->setCopyAssignment(true); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2134 | |
| 2135 | // Add the parameter to the operator. |
| 2136 | ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment, |
| 2137 | ClassDecl->getLocation(), |
| 2138 | /*IdentifierInfo=*/0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2139 | ArgType, /*DInfo=*/0, |
| 2140 | VarDecl::None, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 2141 | CopyAssignment->setParams(Context, &FromParam, 1); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2142 | |
| 2143 | // Don't call addedAssignmentOperator. There is no way to distinguish an |
| 2144 | // implicit from an explicit assignment operator. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2145 | ClassDecl->addDecl(CopyAssignment); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 2148 | if (!ClassDecl->hasUserDeclaredDestructor()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2149 | // C++ [class.dtor]p2: |
| 2150 | // If a class has no user-declared destructor, a destructor is |
| 2151 | // declared implicitly. An implicitly-declared destructor is an |
| 2152 | // inline public member of its class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2153 | DeclarationName Name |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2154 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | CXXDestructorDecl *Destructor |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2156 | = CXXDestructorDecl::Create(Context, ClassDecl, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2157 | ClassDecl->getLocation(), Name, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2158 | Context.getFunctionType(Context.VoidTy, |
| 2159 | 0, 0, false, 0), |
| 2160 | /*isInline=*/true, |
| 2161 | /*isImplicitlyDeclared=*/true); |
| 2162 | Destructor->setAccess(AS_public); |
Douglas Gregor | 6b3945f | 2009-01-07 19:46:03 +0000 | [diff] [blame] | 2163 | Destructor->setImplicit(); |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 2164 | Destructor->setTrivial(ClassDecl->hasTrivialDestructor()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2165 | ClassDecl->addDecl(Destructor); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2166 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2169 | void Sema::ActOnReenterTemplateScope(Scope *S, DeclPtrTy TemplateD) { |
Douglas Gregor | 1cdcc57 | 2009-09-10 00:12:48 +0000 | [diff] [blame] | 2170 | Decl *D = TemplateD.getAs<Decl>(); |
| 2171 | if (!D) |
| 2172 | return; |
| 2173 | |
| 2174 | TemplateParameterList *Params = 0; |
| 2175 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
| 2176 | Params = Template->getTemplateParameters(); |
| 2177 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2178 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) |
| 2179 | Params = PartialSpec->getTemplateParameters(); |
| 2180 | else |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2181 | return; |
| 2182 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 2183 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2184 | ParamEnd = Params->end(); |
| 2185 | Param != ParamEnd; ++Param) { |
| 2186 | NamedDecl *Named = cast<NamedDecl>(*Param); |
| 2187 | if (Named->getDeclName()) { |
| 2188 | S->AddDecl(DeclPtrTy::make(Named)); |
| 2189 | IdResolver.AddDecl(Named); |
| 2190 | } |
| 2191 | } |
| 2192 | } |
| 2193 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2194 | /// ActOnStartDelayedCXXMethodDeclaration - We have completed |
| 2195 | /// parsing a top-level (non-nested) C++ class, and we are now |
| 2196 | /// parsing those parts of the given Method declaration that could |
| 2197 | /// not be parsed earlier (C++ [class.mem]p2), such as default |
| 2198 | /// arguments. This action should enter the scope of the given |
| 2199 | /// Method declaration as if we had just parsed the qualified method |
| 2200 | /// name. However, it should not bring the parameters into scope; |
| 2201 | /// that will be performed by ActOnDelayedCXXMethodParameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2202 | void Sema::ActOnStartDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2203 | if (!MethodD) |
| 2204 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2205 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2206 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2207 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2208 | CXXScopeSpec SS; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2209 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2210 | QualType ClassTy |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2211 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 2212 | SS.setScopeRep( |
| 2213 | NestedNameSpecifier::Create(Context, 0, false, ClassTy.getTypePtr())); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2214 | ActOnCXXEnterDeclaratorScope(S, SS); |
| 2215 | } |
| 2216 | |
| 2217 | /// ActOnDelayedCXXMethodParameter - We've already started a delayed |
| 2218 | /// C++ method declaration. We're (re-)introducing the given |
| 2219 | /// function parameter into scope for use in parsing later parts of |
| 2220 | /// the method declaration. For example, we could see an |
| 2221 | /// ActOnParamDefaultArgument event for this parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2222 | void Sema::ActOnDelayedCXXMethodParameter(Scope *S, DeclPtrTy ParamD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2223 | if (!ParamD) |
| 2224 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2225 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2226 | ParmVarDecl *Param = cast<ParmVarDecl>(ParamD.getAs<Decl>()); |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2227 | |
| 2228 | // If this parameter has an unparsed default argument, clear it out |
| 2229 | // to make way for the parsed default argument. |
| 2230 | if (Param->hasUnparsedDefaultArg()) |
| 2231 | Param->setDefaultArg(0); |
| 2232 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2233 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2234 | if (Param->getDeclName()) |
| 2235 | IdResolver.AddDecl(Param); |
| 2236 | } |
| 2237 | |
| 2238 | /// ActOnFinishDelayedCXXMethodDeclaration - We have finished |
| 2239 | /// processing the delayed method declaration for Method. The method |
| 2240 | /// declaration is now considered finished. There may be a separate |
| 2241 | /// ActOnStartOfFunctionDef action later (not necessarily |
| 2242 | /// immediately!) for this method, if it was also defined inside the |
| 2243 | /// class body. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2244 | void Sema::ActOnFinishDelayedCXXMethodDeclaration(Scope *S, DeclPtrTy MethodD) { |
Douglas Gregor | 4c4f7cb | 2009-06-22 23:20:33 +0000 | [diff] [blame] | 2245 | if (!MethodD) |
| 2246 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2247 | |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 2248 | AdjustDeclIfTemplate(MethodD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2250 | FunctionDecl *Method = cast<FunctionDecl>(MethodD.getAs<Decl>()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2251 | CXXScopeSpec SS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | QualType ClassTy |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2253 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
| 2254 | SS.setScopeRep( |
| 2255 | NestedNameSpecifier::Create(Context, 0, false, ClassTy.getTypePtr())); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2256 | ActOnCXXExitDeclaratorScope(S, SS); |
| 2257 | |
| 2258 | // Now that we have our default arguments, check the constructor |
| 2259 | // again. It could produce additional diagnostics or affect whether |
| 2260 | // the class has implicitly-declared destructors, among other |
| 2261 | // things. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2262 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Method)) |
| 2263 | CheckConstructor(Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2264 | |
| 2265 | // Check the default arguments, which we may have added. |
| 2266 | if (!Method->isInvalidDecl()) |
| 2267 | CheckCXXDefaultArguments(Method); |
| 2268 | } |
| 2269 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2270 | /// CheckConstructorDeclarator - Called by ActOnDeclarator to check |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2271 | /// the well-formedness of the constructor declarator @p D with type @p |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2272 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2273 | /// emit diagnostics and set the invalid bit to true. In any case, the type |
| 2274 | /// will be updated to reflect a well-formed type for the constructor and |
| 2275 | /// returned. |
| 2276 | QualType Sema::CheckConstructorDeclarator(Declarator &D, QualType R, |
| 2277 | FunctionDecl::StorageClass &SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2278 | bool isVirtual = D.getDeclSpec().isVirtualSpecified(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2279 | |
| 2280 | // C++ [class.ctor]p3: |
| 2281 | // A constructor shall not be virtual (10.3) or static (9.4). A |
| 2282 | // constructor can be invoked for a const, volatile or const |
| 2283 | // volatile object. A constructor shall not be declared const, |
| 2284 | // volatile, or const volatile (9.3.2). |
| 2285 | if (isVirtual) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2286 | if (!D.isInvalidType()) |
| 2287 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2288 | << "virtual" << SourceRange(D.getDeclSpec().getVirtualSpecLoc()) |
| 2289 | << SourceRange(D.getIdentifierLoc()); |
| 2290 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2291 | } |
| 2292 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2293 | if (!D.isInvalidType()) |
| 2294 | Diag(D.getIdentifierLoc(), diag::err_constructor_cannot_be) |
| 2295 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2296 | << SourceRange(D.getIdentifierLoc()); |
| 2297 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2298 | SC = FunctionDecl::None; |
| 2299 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2300 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2301 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2302 | if (FTI.TypeQuals != 0) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2303 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2304 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2305 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2306 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2307 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2308 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2309 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2310 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_constructor) |
| 2311 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2312 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2313 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2314 | // Rebuild the function type "R" without any type qualifiers (in |
| 2315 | // case any of the errors above fired) and with "void" as the |
| 2316 | // return type, since constructors don't have return types. We |
| 2317 | // *always* have to do this, because GetTypeForDeclarator will |
| 2318 | // put in a result type of "int" when none was specified. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2319 | const FunctionProtoType *Proto = R->getAs<FunctionProtoType>(); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2320 | return Context.getFunctionType(Context.VoidTy, Proto->arg_type_begin(), |
| 2321 | Proto->getNumArgs(), |
| 2322 | Proto->isVariadic(), 0); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2325 | /// CheckConstructor - Checks a fully-formed constructor for |
| 2326 | /// well-formedness, issuing any diagnostics required. Returns true if |
| 2327 | /// the constructor declarator is invalid. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2328 | void Sema::CheckConstructor(CXXConstructorDecl *Constructor) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | CXXRecordDecl *ClassDecl |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2330 | = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 2331 | if (!ClassDecl) |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2332 | return Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2333 | |
| 2334 | // C++ [class.copy]p3: |
| 2335 | // A declaration of a constructor for a class X is ill-formed if |
| 2336 | // its first parameter is of type (optionally cv-qualified) X and |
| 2337 | // either there are no other parameters or else all other |
| 2338 | // parameters have default arguments. |
Douglas Gregor | 3329756 | 2009-03-27 04:38:56 +0000 | [diff] [blame] | 2339 | if (!Constructor->isInvalidDecl() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | ((Constructor->getNumParams() == 1) || |
| 2341 | (Constructor->getNumParams() > 1 && |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2342 | Constructor->getParamDecl(1)->hasDefaultArg())) && |
| 2343 | Constructor->getTemplateSpecializationKind() |
| 2344 | != TSK_ImplicitInstantiation) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2345 | QualType ParamType = Constructor->getParamDecl(0)->getType(); |
| 2346 | QualType ClassTy = Context.getTagDeclType(ClassDecl); |
| 2347 | if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2348 | SourceLocation ParamLoc = Constructor->getParamDecl(0)->getLocation(); |
| 2349 | Diag(ParamLoc, diag::err_constructor_byvalue_arg) |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 2350 | << CodeModificationHint::CreateInsertion(ParamLoc, " const &"); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2351 | |
| 2352 | // FIXME: Rather that making the constructor invalid, we should endeavor |
| 2353 | // to fix the type. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2354 | Constructor->setInvalidDecl(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2355 | } |
| 2356 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2357 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2358 | // Notify the class that we've added a constructor. |
| 2359 | ClassDecl->addedConstructor(Context, Constructor); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 2362 | /// CheckDestructor - Checks a fully-formed destructor for |
| 2363 | /// well-formedness, issuing any diagnostics required. |
| 2364 | void Sema::CheckDestructor(CXXDestructorDecl *Destructor) { |
| 2365 | CXXRecordDecl *RD = Destructor->getParent(); |
| 2366 | |
| 2367 | if (Destructor->isVirtual()) { |
| 2368 | SourceLocation Loc; |
| 2369 | |
| 2370 | if (!Destructor->isImplicit()) |
| 2371 | Loc = Destructor->getLocation(); |
| 2372 | else |
| 2373 | Loc = RD->getLocation(); |
| 2374 | |
| 2375 | // If we have a virtual destructor, look up the deallocation function |
| 2376 | FunctionDecl *OperatorDelete = 0; |
| 2377 | DeclarationName Name = |
| 2378 | Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| 2379 | if (!FindDeallocationFunction(Loc, RD, Name, OperatorDelete)) |
| 2380 | Destructor->setOperatorDelete(OperatorDelete); |
| 2381 | } |
| 2382 | } |
| 2383 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2384 | static inline bool |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2385 | FTIHasSingleVoidArgument(DeclaratorChunk::FunctionTypeInfo &FTI) { |
| 2386 | return (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 2387 | FTI.ArgInfo[0].Param && |
| 2388 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType()); |
| 2389 | } |
| 2390 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2391 | /// CheckDestructorDeclarator - Called by ActOnDeclarator to check |
| 2392 | /// the well-formednes of the destructor declarator @p D with type @p |
| 2393 | /// R. If there are any errors in the declarator, this routine will |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2394 | /// emit diagnostics and set the declarator to invalid. Even if this happens, |
| 2395 | /// will be updated to reflect a well-formed type for the destructor and |
| 2396 | /// returned. |
| 2397 | QualType Sema::CheckDestructorDeclarator(Declarator &D, |
| 2398 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2399 | // C++ [class.dtor]p1: |
| 2400 | // [...] A typedef-name that names a class is a class-name |
| 2401 | // (7.1.3); however, a typedef-name that names a class shall not |
| 2402 | // be used as the identifier in the declarator for a destructor |
| 2403 | // declaration. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2404 | QualType DeclaratorType = GetTypeFromParser(D.getName().DestructorName); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2405 | if (isa<TypedefType>(DeclaratorType)) { |
| 2406 | Diag(D.getIdentifierLoc(), diag::err_destructor_typedef_name) |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2407 | << DeclaratorType; |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2408 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
| 2411 | // C++ [class.dtor]p2: |
| 2412 | // A destructor is used to destroy objects of its class type. A |
| 2413 | // destructor takes no parameters, and no return type can be |
| 2414 | // specified for it (not even void). The address of a destructor |
| 2415 | // shall not be taken. A destructor shall not be static. A |
| 2416 | // destructor can be invoked for a const, volatile or const |
| 2417 | // volatile object. A destructor shall not be declared const, |
| 2418 | // volatile or const volatile (9.3.2). |
| 2419 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2420 | if (!D.isInvalidType()) |
| 2421 | Diag(D.getIdentifierLoc(), diag::err_destructor_cannot_be) |
| 2422 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2423 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2424 | SC = FunctionDecl::None; |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2425 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2426 | } |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2427 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2428 | // Destructors don't have return types, but the parser will |
| 2429 | // happily parse something like: |
| 2430 | // |
| 2431 | // class X { |
| 2432 | // float ~X(); |
| 2433 | // }; |
| 2434 | // |
| 2435 | // The return type will be eliminated later. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2436 | Diag(D.getIdentifierLoc(), diag::err_destructor_return_type) |
| 2437 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2438 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2439 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2440 | |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2441 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
| 2442 | if (FTI.TypeQuals != 0 && !D.isInvalidType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2443 | if (FTI.TypeQuals & Qualifiers::Const) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2444 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2445 | << "const" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2446 | if (FTI.TypeQuals & Qualifiers::Volatile) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2447 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2448 | << "volatile" << SourceRange(D.getIdentifierLoc()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2449 | if (FTI.TypeQuals & Qualifiers::Restrict) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2450 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_destructor) |
| 2451 | << "restrict" << SourceRange(D.getIdentifierLoc()); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2452 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | // Make sure we don't have any parameters. |
Anders Carlsson | 7786d1c | 2009-04-30 23:18:11 +0000 | [diff] [blame] | 2456 | if (FTI.NumArgs > 0 && !FTIHasSingleVoidArgument(FTI)) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2457 | Diag(D.getIdentifierLoc(), diag::err_destructor_with_params); |
| 2458 | |
| 2459 | // Delete the parameters. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2460 | FTI.freeArgs(); |
| 2461 | D.setInvalidType(); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2464 | // Make sure the destructor isn't variadic. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2465 | if (FTI.isVariadic) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2466 | Diag(D.getIdentifierLoc(), diag::err_destructor_variadic); |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2467 | D.setInvalidType(); |
| 2468 | } |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2469 | |
| 2470 | // Rebuild the function type "R" without any type qualifiers or |
| 2471 | // parameters (in case any of the errors above fired) and with |
| 2472 | // "void" as the return type, since destructors don't have return |
| 2473 | // types. We *always* have to do this, because GetTypeForDeclarator |
| 2474 | // will put in a result type of "int" when none was specified. |
Chris Lattner | 6540180 | 2009-04-25 08:28:21 +0000 | [diff] [blame] | 2475 | return Context.getFunctionType(Context.VoidTy, 0, 0, false, 0); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2478 | /// CheckConversionDeclarator - Called by ActOnDeclarator to check the |
| 2479 | /// well-formednes of the conversion function declarator @p D with |
| 2480 | /// type @p R. If there are any errors in the declarator, this routine |
| 2481 | /// will emit diagnostics and return true. Otherwise, it will return |
| 2482 | /// false. Either way, the type @p R will be updated to reflect a |
| 2483 | /// well-formed type for the conversion operator. |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2484 | void Sema::CheckConversionDeclarator(Declarator &D, QualType &R, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2485 | FunctionDecl::StorageClass& SC) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2486 | // C++ [class.conv.fct]p1: |
| 2487 | // Neither parameter types nor return type can be specified. The |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2488 | // type of a conversion function (8.3.5) is "function taking no |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2489 | // parameter returning conversion-type-id." |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2490 | if (SC == FunctionDecl::Static) { |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2491 | if (!D.isInvalidType()) |
| 2492 | Diag(D.getIdentifierLoc(), diag::err_conv_function_not_member) |
| 2493 | << "static" << SourceRange(D.getDeclSpec().getStorageClassSpecLoc()) |
| 2494 | << SourceRange(D.getIdentifierLoc()); |
| 2495 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2496 | SC = FunctionDecl::None; |
| 2497 | } |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2498 | if (D.getDeclSpec().hasTypeSpecifier() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2499 | // Conversion functions don't have return types, but the parser will |
| 2500 | // happily parse something like: |
| 2501 | // |
| 2502 | // class X { |
| 2503 | // float operator bool(); |
| 2504 | // }; |
| 2505 | // |
| 2506 | // The return type will be changed later anyway. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2507 | Diag(D.getIdentifierLoc(), diag::err_conv_function_return_type) |
| 2508 | << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc()) |
| 2509 | << SourceRange(D.getIdentifierLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | // Make sure we don't have any parameters. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2513 | if (R->getAs<FunctionProtoType>()->getNumArgs() > 0) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2514 | Diag(D.getIdentifierLoc(), diag::err_conv_function_with_params); |
| 2515 | |
| 2516 | // Delete the parameters. |
Chris Lattner | 1833a83 | 2009-01-20 21:06:38 +0000 | [diff] [blame] | 2517 | D.getTypeObject(0).Fun.freeArgs(); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2518 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | // Make sure the conversion function isn't variadic. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2522 | if (R->getAs<FunctionProtoType>()->isVariadic() && !D.isInvalidType()) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2523 | Diag(D.getIdentifierLoc(), diag::err_conv_function_variadic); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2524 | D.setInvalidType(); |
| 2525 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2526 | |
| 2527 | // C++ [class.conv.fct]p4: |
| 2528 | // The conversion-type-id shall not represent a function type nor |
| 2529 | // an array type. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2530 | QualType ConvType = GetTypeFromParser(D.getName().ConversionFunctionId); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2531 | if (ConvType->isArrayType()) { |
| 2532 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_array); |
| 2533 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2534 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2535 | } else if (ConvType->isFunctionType()) { |
| 2536 | Diag(D.getIdentifierLoc(), diag::err_conv_function_to_function); |
| 2537 | ConvType = Context.getPointerType(ConvType); |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 2538 | D.setInvalidType(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | // Rebuild the function type "R" without any parameters (in case any |
| 2542 | // of the errors above fired) and with the conversion type as the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2543 | // return type. |
| 2544 | R = Context.getFunctionType(ConvType, 0, 0, false, |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2545 | R->getAs<FunctionProtoType>()->getTypeQuals()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2546 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2547 | // C++0x explicit conversion operators. |
| 2548 | if (D.getDeclSpec().isExplicitSpecified() && !getLangOptions().CPlusPlus0x) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2549 | Diag(D.getDeclSpec().getExplicitSpecLoc(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2550 | diag::warn_explicit_conversion_functions) |
| 2551 | << SourceRange(D.getDeclSpec().getExplicitSpecLoc()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2554 | /// ActOnConversionDeclarator - Called by ActOnDeclarator to complete |
| 2555 | /// the declaration of the given C++ conversion function. This routine |
| 2556 | /// is responsible for recording the conversion function in the C++ |
| 2557 | /// class, if possible. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2558 | Sema::DeclPtrTy Sema::ActOnConversionDeclarator(CXXConversionDecl *Conversion) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2559 | assert(Conversion && "Expected to receive a conversion function declaration"); |
| 2560 | |
Douglas Gregor | 9d35097 | 2008-12-12 08:25:50 +0000 | [diff] [blame] | 2561 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Conversion->getDeclContext()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2562 | |
| 2563 | // Make sure we aren't redeclaring the conversion function. |
| 2564 | QualType ConvType = Context.getCanonicalType(Conversion->getConversionType()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2565 | |
| 2566 | // C++ [class.conv.fct]p1: |
| 2567 | // [...] A conversion function is never used to convert a |
| 2568 | // (possibly cv-qualified) object to the (possibly cv-qualified) |
| 2569 | // same object type (or a reference to it), to a (possibly |
| 2570 | // cv-qualified) base class of that type (or a reference to it), |
| 2571 | // or to (possibly cv-qualified) void. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2572 | // FIXME: Suppress this warning if the conversion function ends up being a |
| 2573 | // virtual function that overrides a virtual function in a base class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2574 | QualType ClassType |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2575 | = Context.getCanonicalType(Context.getTypeDeclType(ClassDecl)); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2576 | if (const ReferenceType *ConvTypeRef = ConvType->getAs<ReferenceType>()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2577 | ConvType = ConvTypeRef->getPointeeType(); |
| 2578 | if (ConvType->isRecordType()) { |
| 2579 | ConvType = Context.getCanonicalType(ConvType).getUnqualifiedType(); |
| 2580 | if (ConvType == ClassType) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2581 | Diag(Conversion->getLocation(), diag::warn_conv_to_self_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2582 | << ClassType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2583 | else if (IsDerivedFrom(ClassType, ConvType)) |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2584 | Diag(Conversion->getLocation(), diag::warn_conv_to_base_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2585 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2586 | } else if (ConvType->isVoidType()) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2587 | Diag(Conversion->getLocation(), diag::warn_conv_to_void_not_used) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 2588 | << ClassType << ConvType; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2591 | if (Conversion->getPreviousDeclaration()) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2592 | const NamedDecl *ExpectedPrevDecl = Conversion->getPreviousDeclaration(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | if (FunctionTemplateDecl *ConversionTemplate |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2594 | = Conversion->getDescribedFunctionTemplate()) |
| 2595 | ExpectedPrevDecl = ConversionTemplate->getPreviousDeclaration(); |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2596 | OverloadedFunctionDecl *Conversions = ClassDecl->getConversionFunctions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2597 | for (OverloadedFunctionDecl::function_iterator |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2598 | Conv = Conversions->function_begin(), |
| 2599 | ConvEnd = Conversions->function_end(); |
| 2600 | Conv != ConvEnd; ++Conv) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2601 | if (*Conv == ExpectedPrevDecl) { |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2602 | *Conv = Conversion; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2603 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 2604 | } |
| 2605 | } |
| 2606 | assert(Conversion->isInvalidDecl() && "Conversion should not get here."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2607 | } else if (FunctionTemplateDecl *ConversionTemplate |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2608 | = Conversion->getDescribedFunctionTemplate()) |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 2609 | ClassDecl->addConversionFunction(ConversionTemplate); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2610 | else if (!Conversion->getPrimaryTemplate()) // ignore specializations |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 2611 | ClassDecl->addConversionFunction(Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2612 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2613 | return DeclPtrTy::make(Conversion); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2616 | //===----------------------------------------------------------------------===// |
| 2617 | // Namespace Handling |
| 2618 | //===----------------------------------------------------------------------===// |
| 2619 | |
| 2620 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 2621 | /// definition. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2622 | Sema::DeclPtrTy Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 2623 | SourceLocation IdentLoc, |
| 2624 | IdentifierInfo *II, |
| 2625 | SourceLocation LBrace) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2626 | NamespaceDecl *Namespc = |
| 2627 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 2628 | Namespc->setLBracLoc(LBrace); |
| 2629 | |
| 2630 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 2631 | |
| 2632 | if (II) { |
| 2633 | // C++ [namespace.def]p2: |
| 2634 | // The identifier in an original-namespace-definition shall not have been |
| 2635 | // previously defined in the declarative region in which the |
| 2636 | // original-namespace-definition appears. The identifier in an |
| 2637 | // original-namespace-definition is the name of the namespace. Subsequently |
| 2638 | // in that declarative region, it is treated as an original-namespace-name. |
| 2639 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2640 | NamedDecl *PrevDecl |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2641 | = LookupSingleName(DeclRegionScope, II, LookupOrdinaryName, |
| 2642 | LookupResult::ForRedeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2643 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2644 | if (NamespaceDecl *OrigNS = dyn_cast_or_null<NamespaceDecl>(PrevDecl)) { |
| 2645 | // This is an extended namespace definition. |
| 2646 | // Attach this namespace decl to the chain of extended namespace |
| 2647 | // definitions. |
| 2648 | OrigNS->setNextNamespace(Namespc); |
| 2649 | Namespc->setOriginalNamespace(OrigNS->getOriginalNamespace()); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2650 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2651 | // Remove the previous declaration from the scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2652 | if (DeclRegionScope->isDeclScope(DeclPtrTy::make(OrigNS))) { |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 2653 | IdResolver.RemoveDecl(OrigNS); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2654 | DeclRegionScope->RemoveDecl(DeclPtrTy::make(OrigNS)); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2655 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2656 | } else if (PrevDecl) { |
| 2657 | // This is an invalid name redefinition. |
| 2658 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind) |
| 2659 | << Namespc->getDeclName(); |
| 2660 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 2661 | Namespc->setInvalidDecl(); |
| 2662 | // Continue on to push Namespc as current DeclContext and return it. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 2663 | } else if (II->isStr("std") && |
| 2664 | CurContext->getLookupContext()->isTranslationUnit()) { |
| 2665 | // This is the first "real" definition of the namespace "std", so update |
| 2666 | // our cache of the "std" namespace to point at this definition. |
| 2667 | if (StdNamespace) { |
| 2668 | // We had already defined a dummy namespace "std". Link this new |
| 2669 | // namespace definition to the dummy namespace "std". |
| 2670 | StdNamespace->setNextNamespace(Namespc); |
| 2671 | StdNamespace->setLocation(IdentLoc); |
| 2672 | Namespc->setOriginalNamespace(StdNamespace->getOriginalNamespace()); |
| 2673 | } |
| 2674 | |
| 2675 | // Make our StdNamespace cache point at the first real definition of the |
| 2676 | // "std" namespace. |
| 2677 | StdNamespace = Namespc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2678 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2679 | |
| 2680 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 2681 | } else { |
John McCall | 9aeed32 | 2009-10-01 00:25:31 +0000 | [diff] [blame] | 2682 | // Anonymous namespaces. |
| 2683 | |
| 2684 | // C++ [namespace.unnamed]p1. An unnamed-namespace-definition |
| 2685 | // behaves as if it were replaced by |
| 2686 | // namespace unique { /* empty body */ } |
| 2687 | // using namespace unique; |
| 2688 | // namespace unique { namespace-body } |
| 2689 | // where all occurrences of 'unique' in a translation unit are |
| 2690 | // replaced by the same identifier and this identifier differs |
| 2691 | // from all other identifiers in the entire program. |
| 2692 | |
| 2693 | // We just create the namespace with an empty name and then add an |
| 2694 | // implicit using declaration, just like the standard suggests. |
| 2695 | // |
| 2696 | // CodeGen enforces the "universally unique" aspect by giving all |
| 2697 | // declarations semantically contained within an anonymous |
| 2698 | // namespace internal linkage. |
| 2699 | |
| 2700 | assert(Namespc->isAnonymousNamespace()); |
| 2701 | CurContext->addDecl(Namespc); |
| 2702 | |
| 2703 | UsingDirectiveDecl* UD |
| 2704 | = UsingDirectiveDecl::Create(Context, CurContext, |
| 2705 | /* 'using' */ LBrace, |
| 2706 | /* 'namespace' */ SourceLocation(), |
| 2707 | /* qualifier */ SourceRange(), |
| 2708 | /* NNS */ NULL, |
| 2709 | /* identifier */ SourceLocation(), |
| 2710 | Namespc, |
| 2711 | /* Ancestor */ CurContext); |
| 2712 | UD->setImplicit(); |
| 2713 | CurContext->addDecl(UD); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 2717 | // redefinition), push it as current DeclContext and try to continue parsing. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2718 | // FIXME: We should be able to push Namespc here, so that the each DeclContext |
| 2719 | // for the namespace has the declarations that showed up in that particular |
| 2720 | // namespace definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2721 | PushDeclContext(NamespcScope, Namespc); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2722 | return DeclPtrTy::make(Namespc); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
| 2725 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 2726 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2727 | void Sema::ActOnFinishNamespaceDef(DeclPtrTy D, SourceLocation RBrace) { |
| 2728 | Decl *Dcl = D.getAs<Decl>(); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 2729 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 2730 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 2731 | Namespc->setRBracLoc(RBrace); |
| 2732 | PopDeclContext(); |
| 2733 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2734 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2735 | Sema::DeclPtrTy Sema::ActOnUsingDirective(Scope *S, |
| 2736 | SourceLocation UsingLoc, |
| 2737 | SourceLocation NamespcLoc, |
| 2738 | const CXXScopeSpec &SS, |
| 2739 | SourceLocation IdentLoc, |
| 2740 | IdentifierInfo *NamespcName, |
| 2741 | AttributeList *AttrList) { |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2742 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 2743 | assert(NamespcName && "Invalid NamespcName."); |
| 2744 | assert(IdentLoc.isValid() && "Invalid NamespceName location."); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2745 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2746 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2747 | UsingDirectiveDecl *UDir = 0; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2748 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 2749 | // Lookup namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2750 | LookupResult R(*this, NamespcName, IdentLoc, LookupNamespaceName); |
| 2751 | LookupParsedName(R, S, &SS); |
| 2752 | if (R.isAmbiguous()) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2753 | return DeclPtrTy(); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2754 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2755 | if (!R.empty()) { |
| 2756 | NamedDecl *NS = R.getFoundDecl(); |
Douglas Gregor | 19aeac6 | 2009-11-14 03:27:21 +0000 | [diff] [blame] | 2757 | // FIXME: Namespace aliases! |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2758 | assert(isa<NamespaceDecl>(NS) && "expected namespace decl"); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2759 | // C++ [namespace.udir]p1: |
| 2760 | // A using-directive specifies that the names in the nominated |
| 2761 | // namespace can be used in the scope in which the |
| 2762 | // using-directive appears after the using-directive. During |
| 2763 | // unqualified name lookup (3.4.1), the names appear as if they |
| 2764 | // were declared in the nearest enclosing namespace which |
| 2765 | // contains both the using-directive and the nominated |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 2766 | // namespace. [Note: in this context, "contains" means "contains |
| 2767 | // directly or indirectly". ] |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2768 | |
| 2769 | // Find enclosing context containing both using-directive and |
| 2770 | // nominated namespace. |
| 2771 | DeclContext *CommonAncestor = cast<DeclContext>(NS); |
| 2772 | while (CommonAncestor && !CommonAncestor->Encloses(CurContext)) |
| 2773 | CommonAncestor = CommonAncestor->getParent(); |
| 2774 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2775 | UDir = UsingDirectiveDecl::Create(Context, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 2776 | CurContext, UsingLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2777 | NamespcLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 2778 | SS.getRange(), |
| 2779 | (NestedNameSpecifier *)SS.getScopeRep(), |
| 2780 | IdentLoc, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2781 | cast<NamespaceDecl>(NS), |
| 2782 | CommonAncestor); |
| 2783 | PushUsingDirective(S, UDir); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2784 | } else { |
Chris Lattner | ead013e | 2009-01-06 07:24:29 +0000 | [diff] [blame] | 2785 | Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2788 | // FIXME: We ignore attributes for now. |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2789 | delete AttrList; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2790 | return DeclPtrTy::make(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) { |
| 2794 | // If scope has associated entity, then using directive is at namespace |
| 2795 | // or translation unit scope. We add UsingDirectiveDecls, into |
| 2796 | // it's lookup structure. |
| 2797 | if (DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2798 | Ctx->addDecl(UDir); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2799 | else |
| 2800 | // Otherwise it is block-sope. using-directives will affect lookup |
| 2801 | // only to the end of scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2802 | S->PushUsingDirective(DeclPtrTy::make(UDir)); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 2803 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2804 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2805 | |
| 2806 | Sema::DeclPtrTy Sema::ActOnUsingDeclaration(Scope *S, |
Anders Carlsson | 595adc1 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 2807 | AccessSpecifier AS, |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2808 | SourceLocation UsingLoc, |
| 2809 | const CXXScopeSpec &SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 2810 | UnqualifiedId &Name, |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2811 | AttributeList *AttrList, |
| 2812 | bool IsTypeName) { |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2813 | assert(S->getFlags() & Scope::DeclScope && "Invalid Scope."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2814 | |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 2815 | switch (Name.getKind()) { |
| 2816 | case UnqualifiedId::IK_Identifier: |
| 2817 | case UnqualifiedId::IK_OperatorFunctionId: |
| 2818 | case UnqualifiedId::IK_ConversionFunctionId: |
| 2819 | break; |
| 2820 | |
| 2821 | case UnqualifiedId::IK_ConstructorName: |
| 2822 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_constructor) |
| 2823 | << SS.getRange(); |
| 2824 | return DeclPtrTy(); |
| 2825 | |
| 2826 | case UnqualifiedId::IK_DestructorName: |
| 2827 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_destructor) |
| 2828 | << SS.getRange(); |
| 2829 | return DeclPtrTy(); |
| 2830 | |
| 2831 | case UnqualifiedId::IK_TemplateId: |
| 2832 | Diag(Name.getSourceRange().getBegin(), diag::err_using_decl_template_id) |
| 2833 | << SourceRange(Name.TemplateId->LAngleLoc, Name.TemplateId->RAngleLoc); |
| 2834 | return DeclPtrTy(); |
| 2835 | } |
| 2836 | |
| 2837 | DeclarationName TargetName = GetNameFromUnqualifiedId(Name); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 2838 | NamedDecl *UD = BuildUsingDeclaration(S, AS, UsingLoc, SS, |
Douglas Gregor | 12c118a | 2009-11-04 16:30:06 +0000 | [diff] [blame] | 2839 | Name.getSourceRange().getBegin(), |
| 2840 | TargetName, AttrList, IsTypeName); |
Anders Carlsson | 595adc1 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 2841 | if (UD) { |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2842 | PushOnScopeChains(UD, S); |
Anders Carlsson | 595adc1 | 2009-08-29 19:54:19 +0000 | [diff] [blame] | 2843 | UD->setAccess(AS); |
| 2844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2845 | |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2846 | return DeclPtrTy::make(UD); |
| 2847 | } |
| 2848 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 2849 | /// Builds a shadow declaration corresponding to a 'using' declaration. |
| 2850 | static UsingShadowDecl *BuildUsingShadowDecl(Sema &SemaRef, Scope *S, |
| 2851 | AccessSpecifier AS, |
| 2852 | UsingDecl *UD, NamedDecl *Orig) { |
| 2853 | // FIXME: diagnose hiding, collisions |
| 2854 | |
| 2855 | // If we resolved to another shadow declaration, just coalesce them. |
| 2856 | if (isa<UsingShadowDecl>(Orig)) { |
| 2857 | Orig = cast<UsingShadowDecl>(Orig)->getTargetDecl(); |
| 2858 | assert(!isa<UsingShadowDecl>(Orig) && "nested shadow declaration"); |
| 2859 | } |
| 2860 | |
| 2861 | UsingShadowDecl *Shadow |
| 2862 | = UsingShadowDecl::Create(SemaRef.Context, SemaRef.CurContext, |
| 2863 | UD->getLocation(), UD, Orig); |
| 2864 | UD->addShadowDecl(Shadow); |
| 2865 | |
| 2866 | if (S) |
| 2867 | SemaRef.PushOnScopeChains(Shadow, S); |
| 2868 | else |
| 2869 | SemaRef.CurContext->addDecl(Shadow); |
| 2870 | Shadow->setAccess(AS); |
| 2871 | |
| 2872 | return Shadow; |
| 2873 | } |
| 2874 | |
| 2875 | NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, |
| 2876 | SourceLocation UsingLoc, |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2877 | const CXXScopeSpec &SS, |
| 2878 | SourceLocation IdentLoc, |
| 2879 | DeclarationName Name, |
| 2880 | AttributeList *AttrList, |
| 2881 | bool IsTypeName) { |
| 2882 | assert(!SS.isInvalid() && "Invalid CXXScopeSpec."); |
| 2883 | assert(IdentLoc.isValid() && "Invalid TargetName location."); |
Eli Friedman | 2a16a13 | 2009-08-27 05:09:36 +0000 | [diff] [blame] | 2884 | |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 2885 | // FIXME: We ignore attributes for now. |
| 2886 | delete AttrList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2888 | if (SS.isEmpty()) { |
| 2889 | Diag(IdentLoc, diag::err_using_requires_qualname); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2890 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2891 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2892 | |
| 2893 | NestedNameSpecifier *NNS = |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2894 | static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 2895 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 2896 | DeclContext *LookupContext = computeDeclContext(SS); |
| 2897 | if (!LookupContext) { |
Anders Carlsson | 550b14b | 2009-08-28 05:49:21 +0000 | [diff] [blame] | 2898 | return UnresolvedUsingDecl::Create(Context, CurContext, UsingLoc, |
| 2899 | SS.getRange(), NNS, |
| 2900 | IdentLoc, Name, IsTypeName); |
| 2901 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2902 | |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2903 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 2904 | // C++0x N2914 [namespace.udecl]p3: |
| 2905 | // A using-declaration used as a member-declaration shall refer to a member |
| 2906 | // of a base class of the class being defined, shall refer to a member of an |
| 2907 | // anonymous union that is a member of a base class of the class being |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2908 | // defined, or shall refer to an enumerator for an enumeration type that is |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2909 | // a member of a base class of the class being defined. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 2910 | |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 2911 | CXXRecordDecl *LookupRD = dyn_cast<CXXRecordDecl>(LookupContext); |
| 2912 | if (!LookupRD || !RD->isDerivedFrom(LookupRD)) { |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2913 | Diag(SS.getRange().getBegin(), |
| 2914 | diag::err_using_decl_nested_name_specifier_is_not_a_base_class) |
| 2915 | << NNS << RD->getDeclName(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2916 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2917 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2918 | } else { |
| 2919 | // C++0x N2914 [namespace.udecl]p8: |
| 2920 | // A using-declaration for a class member shall be a member-declaration. |
John McCall | af8e6ed | 2009-11-12 03:15:40 +0000 | [diff] [blame] | 2921 | if (isa<CXXRecordDecl>(LookupContext)) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 2922 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_class_member) |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2923 | << SS.getRange(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2924 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2925 | } |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 2928 | // Look up the target name. Unlike most lookups, we do not want to |
| 2929 | // hide tag declarations: tag names are visible through the using |
| 2930 | // declaration even if hidden by ordinary names. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2931 | LookupResult R(*this, Name, IdentLoc, LookupOrdinaryName); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 2932 | R.setHideTags(false); |
| 2933 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2934 | LookupQualifiedName(R, LookupContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2935 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2936 | if (R.empty()) { |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 2937 | Diag(IdentLoc, diag::err_no_member) |
| 2938 | << Name << LookupContext << SS.getRange(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2939 | return 0; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 2942 | if (R.isAmbiguous()) |
| 2943 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2944 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 2945 | if (IsTypeName && |
| 2946 | (R.getResultKind() != LookupResult::Found |
| 2947 | || !isa<TypeDecl>(R.getFoundDecl()))) { |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2948 | Diag(IdentLoc, diag::err_using_typename_non_type); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2949 | return 0; |
Anders Carlsson | cf9f921 | 2009-08-28 03:16:11 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 2952 | // C++0x N2914 [namespace.udecl]p6: |
| 2953 | // A using-declaration shall not name a namespace. |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 2954 | if (R.getResultKind() == LookupResult::Found |
| 2955 | && isa<NamespaceDecl>(R.getFoundDecl())) { |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 2956 | Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace) |
| 2957 | << SS.getRange(); |
Anders Carlsson | c72160b | 2009-08-28 05:40:36 +0000 | [diff] [blame] | 2958 | return 0; |
Anders Carlsson | 73b39cf | 2009-08-28 03:35:18 +0000 | [diff] [blame] | 2959 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2960 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 2961 | UsingDecl *UD = UsingDecl::Create(Context, CurContext, IdentLoc, |
| 2962 | SS.getRange(), UsingLoc, NNS, Name, |
| 2963 | IsTypeName); |
| 2964 | |
| 2965 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 2966 | BuildUsingShadowDecl(*this, S, AS, UD, *I); |
| 2967 | |
| 2968 | return UD; |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2971 | /// getNamespaceDecl - Returns the namespace a decl represents. If the decl |
| 2972 | /// is a namespace alias, returns the namespace it points to. |
| 2973 | static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) { |
| 2974 | if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D)) |
| 2975 | return AD->getNamespace(); |
| 2976 | return dyn_cast_or_null<NamespaceDecl>(D); |
| 2977 | } |
| 2978 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2979 | Sema::DeclPtrTy Sema::ActOnNamespaceAliasDef(Scope *S, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 2980 | SourceLocation NamespaceLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2981 | SourceLocation AliasLoc, |
| 2982 | IdentifierInfo *Alias, |
| 2983 | const CXXScopeSpec &SS, |
Anders Carlsson | 03bd5a1 | 2009-03-28 22:53:22 +0000 | [diff] [blame] | 2984 | SourceLocation IdentLoc, |
| 2985 | IdentifierInfo *Ident) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2986 | |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2987 | // Lookup the namespace name. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2988 | LookupResult R(*this, Ident, IdentLoc, LookupNamespaceName); |
| 2989 | LookupParsedName(R, S, &SS); |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2990 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 2991 | // Check if we have a previous declaration with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2992 | if (NamedDecl *PrevDecl |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2993 | = LookupSingleName(S, Alias, LookupOrdinaryName, |
| 2994 | LookupResult::ForRedeclaration)) { |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2995 | if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2996 | // We already have an alias with the same name that points to the same |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 2997 | // namespace, so don't create a new one. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2998 | if (!R.isAmbiguous() && !R.empty() && |
| 2999 | AD->getNamespace() == getNamespaceDecl(R.getFoundDecl())) |
Anders Carlsson | 81c85c4 | 2009-03-28 23:53:49 +0000 | [diff] [blame] | 3000 | return DeclPtrTy(); |
| 3001 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3002 | |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 3003 | unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition : |
| 3004 | diag::err_redefinition_different_kind; |
| 3005 | Diag(AliasLoc, DiagID) << Alias; |
| 3006 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3007 | return DeclPtrTy(); |
Anders Carlsson | 8d7ba40 | 2009-03-28 06:23:46 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 3010 | if (R.isAmbiguous()) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3011 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3012 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3013 | if (R.empty()) { |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 3014 | Diag(NamespaceLoc, diag::err_expected_namespace_name) << SS.getRange(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3015 | return DeclPtrTy(); |
Anders Carlsson | 5721c68 | 2009-03-28 06:42:02 +0000 | [diff] [blame] | 3016 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3017 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 3018 | NamespaceAliasDecl *AliasDecl = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3019 | NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, |
| 3020 | Alias, SS.getRange(), |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 3021 | (NestedNameSpecifier *)SS.getScopeRep(), |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 3022 | IdentLoc, R.getFoundDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3024 | CurContext->addDecl(AliasDecl); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 3025 | return DeclPtrTy::make(AliasDecl); |
Anders Carlsson | dbb0094 | 2009-03-28 05:27:17 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 3028 | void Sema::DefineImplicitDefaultConstructor(SourceLocation CurrentLocation, |
| 3029 | CXXConstructorDecl *Constructor) { |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 3030 | assert((Constructor->isImplicit() && Constructor->isDefaultConstructor() && |
| 3031 | !Constructor->isUsed()) && |
| 3032 | "DefineImplicitDefaultConstructor - call it for implicit default ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3033 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3034 | CXXRecordDecl *ClassDecl |
| 3035 | = cast<CXXRecordDecl>(Constructor->getDeclContext()); |
| 3036 | assert(ClassDecl && "DefineImplicitDefaultConstructor - invalid constructor"); |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 3037 | |
Eli Friedman | 80c30da | 2009-11-09 19:20:36 +0000 | [diff] [blame] | 3038 | if (SetBaseOrMemberInitializers(Constructor, 0, 0, true)) { |
| 3039 | Diag(CurrentLocation, diag::note_ctor_synthesized_at) |
| 3040 | << Context.getTagDeclType(ClassDecl); |
| 3041 | Constructor->setInvalidDecl(); |
| 3042 | } else { |
| 3043 | Constructor->setUsed(); |
| 3044 | } |
Eli Friedman | 49c16da | 2009-11-09 01:05:47 +0000 | [diff] [blame] | 3045 | return; |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3048 | void Sema::DefineImplicitDestructor(SourceLocation CurrentLocation, |
Douglas Gregor | 4fe95f9 | 2009-09-04 19:04:08 +0000 | [diff] [blame] | 3049 | CXXDestructorDecl *Destructor) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3050 | assert((Destructor->isImplicit() && !Destructor->isUsed()) && |
| 3051 | "DefineImplicitDestructor - call it for implicit default dtor"); |
Anders Carlsson | 6d70139 | 2009-11-15 22:49:34 +0000 | [diff] [blame] | 3052 | CXXRecordDecl *ClassDecl = Destructor->getParent(); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3053 | assert(ClassDecl && "DefineImplicitDestructor - invalid destructor"); |
| 3054 | // C++ [class.dtor] p5 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3055 | // Before the implicitly-declared default destructor for a class is |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3056 | // implicitly defined, all the implicitly-declared default destructors |
| 3057 | // for its base class and its non-static data members shall have been |
| 3058 | // implicitly defined. |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 3059 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3060 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3061 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3062 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3063 | if (!BaseClassDecl->hasTrivialDestructor()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3064 | if (CXXDestructorDecl *BaseDtor = |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3065 | const_cast<CXXDestructorDecl*>(BaseClassDecl->getDestructor(Context))) |
| 3066 | MarkDeclarationReferenced(CurrentLocation, BaseDtor); |
| 3067 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3068 | assert(false && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3069 | "DefineImplicitDestructor - missing dtor in a base class"); |
| 3070 | } |
| 3071 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3072 | |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 3073 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3074 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3075 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 3076 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 3077 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3078 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3079 | CXXRecordDecl *FieldClassDecl |
| 3080 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
| 3081 | if (!FieldClassDecl->hasTrivialDestructor()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | if (CXXDestructorDecl *FieldDtor = |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3083 | const_cast<CXXDestructorDecl*>( |
| 3084 | FieldClassDecl->getDestructor(Context))) |
| 3085 | MarkDeclarationReferenced(CurrentLocation, FieldDtor); |
| 3086 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | assert(false && |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3088 | "DefineImplicitDestructor - missing dtor in class of a data member"); |
| 3089 | } |
| 3090 | } |
| 3091 | } |
| 3092 | Destructor->setUsed(); |
| 3093 | } |
| 3094 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3095 | void Sema::DefineImplicitOverloadedAssign(SourceLocation CurrentLocation, |
| 3096 | CXXMethodDecl *MethodDecl) { |
| 3097 | assert((MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 3098 | MethodDecl->getOverloadedOperator() == OO_Equal && |
| 3099 | !MethodDecl->isUsed()) && |
| 3100 | "DefineImplicitOverloadedAssign - call it for implicit assignment op"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3101 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3102 | CXXRecordDecl *ClassDecl |
| 3103 | = cast<CXXRecordDecl>(MethodDecl->getDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3104 | |
Fariborz Jahanian | c6249b9 | 2009-06-26 16:08:57 +0000 | [diff] [blame] | 3105 | // C++[class.copy] p12 |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3106 | // Before the implicitly-declared copy assignment operator for a class is |
| 3107 | // implicitly defined, all implicitly-declared copy assignment operators |
| 3108 | // for its direct base classes and its nonstatic data members shall have |
| 3109 | // been implicitly defined. |
| 3110 | bool err = false; |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 3111 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(), |
| 3112 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3113 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3114 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3115 | if (CXXMethodDecl *BaseAssignOpMethod = |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3116 | getAssignOperatorMethod(MethodDecl->getParamDecl(0), BaseClassDecl)) |
| 3117 | MarkDeclarationReferenced(CurrentLocation, BaseAssignOpMethod); |
| 3118 | } |
Fariborz Jahanian | 514b7b1 | 2009-06-30 16:36:53 +0000 | [diff] [blame] | 3119 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3120 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3121 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 3122 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 3123 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3124 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3125 | CXXRecordDecl *FieldClassDecl |
| 3126 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3127 | if (CXXMethodDecl *FieldAssignOpMethod = |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3128 | getAssignOperatorMethod(MethodDecl->getParamDecl(0), FieldClassDecl)) |
| 3129 | MarkDeclarationReferenced(CurrentLocation, FieldAssignOpMethod); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3130 | } else if (FieldType->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3131 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 5e09d4c | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 3132 | << Context.getTagDeclType(ClassDecl) << 0 << Field->getDeclName(); |
| 3133 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3134 | Diag(CurrentLocation, diag::note_first_required_here); |
| 3135 | err = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3136 | } else if (FieldType.isConstQualified()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3137 | Diag(ClassDecl->getLocation(), diag::err_uninitialized_member_for_assign) |
Anders Carlsson | 5e09d4c | 2009-07-09 17:47:25 +0000 | [diff] [blame] | 3138 | << Context.getTagDeclType(ClassDecl) << 1 << Field->getDeclName(); |
| 3139 | Diag(Field->getLocation(), diag::note_declared_at); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3140 | Diag(CurrentLocation, diag::note_first_required_here); |
| 3141 | err = true; |
| 3142 | } |
| 3143 | } |
| 3144 | if (!err) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3145 | MethodDecl->setUsed(); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | CXXMethodDecl * |
| 3149 | Sema::getAssignOperatorMethod(ParmVarDecl *ParmDecl, |
| 3150 | CXXRecordDecl *ClassDecl) { |
| 3151 | QualType LHSType = Context.getTypeDeclType(ClassDecl); |
| 3152 | QualType RHSType(LHSType); |
| 3153 | // If class's assignment operator argument is const/volatile qualified, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3154 | // look for operator = (const/volatile B&). Otherwise, look for |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3155 | // operator = (B&). |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3156 | RHSType = Context.getCVRQualifiedType(RHSType, |
| 3157 | ParmDecl->getType().getCVRQualifiers()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3158 | ExprOwningPtr<Expr> LHS(this, new (Context) DeclRefExpr(ParmDecl, |
| 3159 | LHSType, |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3160 | SourceLocation())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | ExprOwningPtr<Expr> RHS(this, new (Context) DeclRefExpr(ParmDecl, |
| 3162 | RHSType, |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3163 | SourceLocation())); |
| 3164 | Expr *Args[2] = { &*LHS, &*RHS }; |
| 3165 | OverloadCandidateSet CandidateSet; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3166 | AddMemberOperatorCandidates(clang::OO_Equal, SourceLocation(), Args, 2, |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3167 | CandidateSet); |
| 3168 | OverloadCandidateSet::iterator Best; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3169 | if (BestViableFunction(CandidateSet, |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 3170 | ClassDecl->getLocation(), Best) == OR_Success) |
| 3171 | return cast<CXXMethodDecl>(Best->Function); |
| 3172 | assert(false && |
| 3173 | "getAssignOperatorMethod - copy assignment operator method not found"); |
| 3174 | return 0; |
| 3175 | } |
| 3176 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3177 | void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation, |
| 3178 | CXXConstructorDecl *CopyConstructor, |
| 3179 | unsigned TypeQuals) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3180 | assert((CopyConstructor->isImplicit() && |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3181 | CopyConstructor->isCopyConstructor(Context, TypeQuals) && |
| 3182 | !CopyConstructor->isUsed()) && |
| 3183 | "DefineImplicitCopyConstructor - call it for implicit copy ctor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3184 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3185 | CXXRecordDecl *ClassDecl |
| 3186 | = cast<CXXRecordDecl>(CopyConstructor->getDeclContext()); |
| 3187 | assert(ClassDecl && "DefineImplicitCopyConstructor - invalid constructor"); |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 3188 | // C++ [class.copy] p209 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 | // Before the implicitly-declared copy constructor for a class is |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3190 | // implicitly defined, all the implicitly-declared copy constructors |
| 3191 | // for its base class and its non-static data members shall have been |
| 3192 | // implicitly defined. |
| 3193 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 3194 | Base != ClassDecl->bases_end(); ++Base) { |
| 3195 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3196 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | if (CXXConstructorDecl *BaseCopyCtor = |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3198 | BaseClassDecl->getCopyConstructor(Context, TypeQuals)) |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 3199 | MarkDeclarationReferenced(CurrentLocation, BaseCopyCtor); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3200 | } |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3201 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 3202 | FieldEnd = ClassDecl->field_end(); |
| 3203 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3204 | QualType FieldType = Context.getCanonicalType((*Field)->getType()); |
| 3205 | if (const ArrayType *Array = Context.getAsArrayType(FieldType)) |
| 3206 | FieldType = Array->getElementType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3207 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3208 | CXXRecordDecl *FieldClassDecl |
| 3209 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3210 | if (CXXConstructorDecl *FieldCopyCtor = |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3211 | FieldClassDecl->getCopyConstructor(Context, TypeQuals)) |
Fariborz Jahanian | 220a0f3 | 2009-06-23 23:42:10 +0000 | [diff] [blame] | 3212 | MarkDeclarationReferenced(CurrentLocation, FieldCopyCtor); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 3213 | } |
| 3214 | } |
| 3215 | CopyConstructor->setUsed(); |
| 3216 | } |
| 3217 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 3218 | Sema::OwningExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 3219 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3220 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3221 | MultiExprArg ExprArgs) { |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3222 | bool Elidable = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3223 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3224 | // C++ [class.copy]p15: |
| 3225 | // Whenever a temporary class object is copied using a copy constructor, and |
| 3226 | // this object and the copy have the same cv-unqualified type, an |
| 3227 | // implementation is permitted to treat the original and the copy as two |
| 3228 | // different ways of referring to the same object and not perform a copy at |
| 3229 | // all, even if the class copy constructor or destructor have side effects. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3230 | |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3231 | // FIXME: Is this enough? |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3232 | if (Constructor->isCopyConstructor(Context)) { |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3233 | Expr *E = ((Expr **)ExprArgs.get())[0]; |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3234 | while (CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 3235 | E = BE->getSubExpr(); |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3236 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 3237 | if (ICE->getCastKind() == CastExpr::CK_NoOp) |
| 3238 | E = ICE->getSubExpr(); |
| 3239 | |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3240 | if (isa<CallExpr>(E) || isa<CXXTemporaryObjectExpr>(E)) |
| 3241 | Elidable = true; |
| 3242 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3243 | |
| 3244 | return BuildCXXConstructExpr(ConstructLoc, DeclInitType, Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3245 | Elidable, move(ExprArgs)); |
Anders Carlsson | 9abf2ae | 2009-08-16 05:13:48 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 3248 | /// BuildCXXConstructExpr - Creates a complete call to a constructor, |
| 3249 | /// including handling of its default argument expressions. |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 3250 | Sema::OwningExprResult |
Anders Carlsson | ec8e5ea | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 3251 | Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, |
| 3252 | CXXConstructorDecl *Constructor, bool Elidable, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3253 | MultiExprArg ExprArgs) { |
| 3254 | unsigned NumExprs = ExprArgs.size(); |
| 3255 | Expr **Exprs = (Expr **)ExprArgs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3256 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3257 | return Owned(CXXConstructExpr::Create(Context, DeclInitType, Constructor, |
| 3258 | Elidable, Exprs, NumExprs)); |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 3261 | Sema::OwningExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3262 | Sema::BuildCXXTemporaryObjectExpr(CXXConstructorDecl *Constructor, |
| 3263 | QualType Ty, |
| 3264 | SourceLocation TyBeginLoc, |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 3265 | MultiExprArg Args, |
| 3266 | SourceLocation RParenLoc) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3267 | unsigned NumExprs = Args.size(); |
| 3268 | Expr **Exprs = (Expr **)Args.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3269 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3270 | return Owned(new (Context) CXXTemporaryObjectExpr(Context, Constructor, Ty, |
| 3271 | TyBeginLoc, Exprs, |
| 3272 | NumExprs, RParenLoc)); |
Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3276 | bool Sema::InitializeVarWithConstructor(VarDecl *VD, |
Fariborz Jahanian | b2c352e | 2009-08-05 17:03:54 +0000 | [diff] [blame] | 3277 | CXXConstructorDecl *Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3278 | MultiExprArg Exprs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | OwningExprResult TempResult = |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 3280 | BuildCXXConstructExpr(VD->getLocation(), VD->getType(), Constructor, |
Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 3281 | move(Exprs)); |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 3282 | if (TempResult.isInvalid()) |
| 3283 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3284 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 3285 | Expr *Temp = TempResult.takeAs<Expr>(); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 3286 | MarkDeclarationReferenced(VD->getLocation(), Constructor); |
Fariborz Jahanian | caa499b | 2009-08-05 18:17:32 +0000 | [diff] [blame] | 3287 | Temp = MaybeCreateCXXExprWithTemporaries(Temp, /*DestroyTemps=*/true); |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 3288 | VD->setInit(Context, Temp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 3290 | return false; |
Anders Carlsson | 930e8d0 | 2009-04-16 23:50:50 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3293 | void Sema::FinalizeVarWithDestructor(VarDecl *VD, QualType DeclInitType) { |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3294 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>( |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3295 | DeclInitType->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3296 | if (!ClassDecl->hasTrivialDestructor()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3297 | if (CXXDestructorDecl *Destructor = |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3298 | const_cast<CXXDestructorDecl*>(ClassDecl->getDestructor(Context))) |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 3299 | MarkDeclarationReferenced(VD->getLocation(), Destructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3302 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3303 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 3304 | /// e.g: "int x(1);" |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3305 | void Sema::AddCXXDirectInitializerToDecl(DeclPtrTy Dcl, |
| 3306 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3307 | MultiExprArg Exprs, |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3308 | SourceLocation *CommaLocs, |
| 3309 | SourceLocation RParenLoc) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3310 | unsigned NumExprs = Exprs.size(); |
| 3311 | assert(NumExprs != 0 && Exprs.get() && "missing expressions"); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3312 | Decl *RealDecl = Dcl.getAs<Decl>(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3313 | |
| 3314 | // If there is no declaration, there was an error parsing it. Just ignore |
| 3315 | // the initializer. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3316 | if (RealDecl == 0) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3317 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3318 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3319 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 3320 | if (!VDecl) { |
| 3321 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 3322 | RealDecl->setInvalidDecl(); |
| 3323 | return; |
| 3324 | } |
| 3325 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3326 | // We will represent direct-initialization similarly to copy-initialization: |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 3327 | // int x(1); -as-> int x = 1; |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3328 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 3329 | // |
| 3330 | // Clients that want to distinguish between the two forms, can check for |
| 3331 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 3332 | // A major benefit is that clients that don't particularly care about which |
| 3333 | // exactly form was it (like the CodeGen) can handle both cases without |
| 3334 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3335 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3336 | // If either the declaration has a dependent type or if any of the expressions |
| 3337 | // is type-dependent, we represent the initialization via a ParenListExpr for |
| 3338 | // later use during template instantiation. |
| 3339 | if (VDecl->getType()->isDependentType() || |
| 3340 | Expr::hasAnyTypeDependentArguments((Expr **)Exprs.get(), Exprs.size())) { |
| 3341 | // Let clients know that initialization was done with a direct initializer. |
| 3342 | VDecl->setCXXDirectInitializer(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3343 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3344 | // Store the initialization expressions as a ParenListExpr. |
| 3345 | unsigned NumExprs = Exprs.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3346 | VDecl->setInit(Context, |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3347 | new (Context) ParenListExpr(Context, LParenLoc, |
| 3348 | (Expr **)Exprs.release(), |
| 3349 | NumExprs, RParenLoc)); |
| 3350 | return; |
| 3351 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 3353 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3354 | // C++ 8.5p11: |
| 3355 | // The form of initialization (using parentheses or '=') is generally |
| 3356 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3357 | // class type. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3358 | QualType DeclInitType = VDecl->getType(); |
| 3359 | if (const ArrayType *Array = Context.getAsArrayType(DeclInitType)) |
Fariborz Jahanian | 680a3f3 | 2009-10-28 19:04:36 +0000 | [diff] [blame] | 3360 | DeclInitType = Context.getBaseElementType(Array); |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3361 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 3362 | // FIXME: This isn't the right place to complete the type. |
| 3363 | if (RequireCompleteType(VDecl->getLocation(), VDecl->getType(), |
| 3364 | diag::err_typecheck_decl_incomplete_type)) { |
| 3365 | VDecl->setInvalidDecl(); |
| 3366 | return; |
| 3367 | } |
| 3368 | |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3369 | if (VDecl->getType()->isRecordType()) { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3370 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 3371 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3372 | CXXConstructorDecl *Constructor |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3373 | = PerformInitializationByConstructor(DeclInitType, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3374 | move(Exprs), |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 3375 | VDecl->getLocation(), |
| 3376 | SourceRange(VDecl->getLocation(), |
| 3377 | RParenLoc), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3378 | VDecl->getDeclName(), |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3379 | IK_Direct, |
| 3380 | ConstructorArgs); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3381 | if (!Constructor) |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3382 | RealDecl->setInvalidDecl(); |
Anders Carlsson | ca29ad9 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 3383 | else { |
Anders Carlsson | ca29ad9 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 3384 | VDecl->setCXXDirectInitializer(true); |
Fariborz Jahanian | c0fcce4 | 2009-10-28 18:41:06 +0000 | [diff] [blame] | 3385 | if (InitializeVarWithConstructor(VDecl, Constructor, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3386 | move_arg(ConstructorArgs))) |
Anders Carlsson | fe2de49 | 2009-08-25 05:18:00 +0000 | [diff] [blame] | 3387 | RealDecl->setInvalidDecl(); |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 3388 | FinalizeVarWithDestructor(VDecl, DeclInitType); |
Anders Carlsson | ca29ad9 | 2009-04-15 21:48:18 +0000 | [diff] [blame] | 3389 | } |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 3390 | return; |
| 3391 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3392 | |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 3393 | if (NumExprs > 1) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3394 | Diag(CommaLocs[0], diag::err_builtin_direct_init_more_than_one_arg) |
| 3395 | << SourceRange(VDecl->getLocation(), RParenLoc); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3396 | RealDecl->setInvalidDecl(); |
| 3397 | return; |
| 3398 | } |
| 3399 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3400 | // Let clients know that initialization was done with a direct initializer. |
| 3401 | VDecl->setCXXDirectInitializer(true); |
Argyrios Kyrtzidis | ce8e292 | 2008-10-06 23:08:37 +0000 | [diff] [blame] | 3402 | |
| 3403 | assert(NumExprs == 1 && "Expected 1 expression"); |
| 3404 | // Set the init expression, handles conversions. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 3405 | AddInitializerToDecl(Dcl, ExprArg(*this, Exprs.release()[0]), |
| 3406 | /*DirectInit=*/true); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3407 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3408 | |
Douglas Gregor | 19aeac6 | 2009-11-14 03:27:21 +0000 | [diff] [blame] | 3409 | /// \brief Add the applicable constructor candidates for an initialization |
| 3410 | /// by constructor. |
| 3411 | static void AddConstructorInitializationCandidates(Sema &SemaRef, |
| 3412 | QualType ClassType, |
| 3413 | Expr **Args, |
| 3414 | unsigned NumArgs, |
| 3415 | Sema::InitializationKind Kind, |
| 3416 | OverloadCandidateSet &CandidateSet) { |
| 3417 | // C++ [dcl.init]p14: |
| 3418 | // If the initialization is direct-initialization, or if it is |
| 3419 | // copy-initialization where the cv-unqualified version of the |
| 3420 | // source type is the same class as, or a derived class of, the |
| 3421 | // class of the destination, constructors are considered. The |
| 3422 | // applicable constructors are enumerated (13.3.1.3), and the |
| 3423 | // best one is chosen through overload resolution (13.3). The |
| 3424 | // constructor so selected is called to initialize the object, |
| 3425 | // with the initializer expression(s) as its argument(s). If no |
| 3426 | // constructor applies, or the overload resolution is ambiguous, |
| 3427 | // the initialization is ill-formed. |
| 3428 | const RecordType *ClassRec = ClassType->getAs<RecordType>(); |
| 3429 | assert(ClassRec && "Can only initialize a class type here"); |
| 3430 | |
| 3431 | // FIXME: When we decide not to synthesize the implicitly-declared |
| 3432 | // constructors, we'll need to make them appear here. |
| 3433 | |
| 3434 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassRec->getDecl()); |
| 3435 | DeclarationName ConstructorName |
| 3436 | = SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 3437 | SemaRef.Context.getCanonicalType(ClassType).getUnqualifiedType()); |
| 3438 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 3439 | for (llvm::tie(Con, ConEnd) = ClassDecl->lookup(ConstructorName); |
| 3440 | Con != ConEnd; ++Con) { |
| 3441 | // Find the constructor (which may be a template). |
| 3442 | CXXConstructorDecl *Constructor = 0; |
| 3443 | FunctionTemplateDecl *ConstructorTmpl= dyn_cast<FunctionTemplateDecl>(*Con); |
| 3444 | if (ConstructorTmpl) |
| 3445 | Constructor |
| 3446 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 3447 | else |
| 3448 | Constructor = cast<CXXConstructorDecl>(*Con); |
| 3449 | |
| 3450 | if ((Kind == Sema::IK_Direct) || |
| 3451 | (Kind == Sema::IK_Copy && |
| 3452 | Constructor->isConvertingConstructor(/*AllowExplicit=*/false)) || |
| 3453 | (Kind == Sema::IK_Default && Constructor->isDefaultConstructor())) { |
| 3454 | if (ConstructorTmpl) |
| 3455 | SemaRef.AddTemplateOverloadCandidate(ConstructorTmpl, false, 0, 0, |
| 3456 | Args, NumArgs, CandidateSet); |
| 3457 | else |
| 3458 | SemaRef.AddOverloadCandidate(Constructor, Args, NumArgs, CandidateSet); |
| 3459 | } |
| 3460 | } |
| 3461 | } |
| 3462 | |
| 3463 | /// \brief Attempt to perform initialization by constructor |
| 3464 | /// (C++ [dcl.init]p14), which may occur as part of direct-initialization or |
| 3465 | /// copy-initialization. |
| 3466 | /// |
| 3467 | /// This routine determines whether initialization by constructor is possible, |
| 3468 | /// but it does not emit any diagnostics in the case where the initialization |
| 3469 | /// is ill-formed. |
| 3470 | /// |
| 3471 | /// \param ClassType the type of the object being initialized, which must have |
| 3472 | /// class type. |
| 3473 | /// |
| 3474 | /// \param Args the arguments provided to initialize the object |
| 3475 | /// |
| 3476 | /// \param NumArgs the number of arguments provided to initialize the object |
| 3477 | /// |
| 3478 | /// \param Kind the type of initialization being performed |
| 3479 | /// |
| 3480 | /// \returns the constructor used to initialize the object, if successful. |
| 3481 | /// Otherwise, emits a diagnostic and returns NULL. |
| 3482 | CXXConstructorDecl * |
| 3483 | Sema::TryInitializationByConstructor(QualType ClassType, |
| 3484 | Expr **Args, unsigned NumArgs, |
| 3485 | SourceLocation Loc, |
| 3486 | InitializationKind Kind) { |
| 3487 | // Build the overload candidate set |
| 3488 | OverloadCandidateSet CandidateSet; |
| 3489 | AddConstructorInitializationCandidates(*this, ClassType, Args, NumArgs, Kind, |
| 3490 | CandidateSet); |
| 3491 | |
| 3492 | // Determine whether we found a constructor we can use. |
| 3493 | OverloadCandidateSet::iterator Best; |
| 3494 | switch (BestViableFunction(CandidateSet, Loc, Best)) { |
| 3495 | case OR_Success: |
| 3496 | case OR_Deleted: |
| 3497 | // We found a constructor. Return it. |
| 3498 | return cast<CXXConstructorDecl>(Best->Function); |
| 3499 | |
| 3500 | case OR_No_Viable_Function: |
| 3501 | case OR_Ambiguous: |
| 3502 | // Overload resolution failed. Return nothing. |
| 3503 | return 0; |
| 3504 | } |
| 3505 | |
| 3506 | // Silence GCC warning |
| 3507 | return 0; |
| 3508 | } |
| 3509 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3510 | /// \brief Perform initialization by constructor (C++ [dcl.init]p14), which |
| 3511 | /// may occur as part of direct-initialization or copy-initialization. |
| 3512 | /// |
| 3513 | /// \param ClassType the type of the object being initialized, which must have |
| 3514 | /// class type. |
| 3515 | /// |
| 3516 | /// \param ArgsPtr the arguments provided to initialize the object |
| 3517 | /// |
| 3518 | /// \param Loc the source location where the initialization occurs |
| 3519 | /// |
| 3520 | /// \param Range the source range that covers the entire initialization |
| 3521 | /// |
| 3522 | /// \param InitEntity the name of the entity being initialized, if known |
| 3523 | /// |
| 3524 | /// \param Kind the type of initialization being performed |
| 3525 | /// |
| 3526 | /// \param ConvertedArgs a vector that will be filled in with the |
| 3527 | /// appropriately-converted arguments to the constructor (if initialization |
| 3528 | /// succeeded). |
| 3529 | /// |
| 3530 | /// \returns the constructor used to initialize the object, if successful. |
| 3531 | /// Otherwise, emits a diagnostic and returns NULL. |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3532 | CXXConstructorDecl * |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 3533 | Sema::PerformInitializationByConstructor(QualType ClassType, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3534 | MultiExprArg ArgsPtr, |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 3535 | SourceLocation Loc, SourceRange Range, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3536 | DeclarationName InitEntity, |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3537 | InitializationKind Kind, |
| 3538 | ASTOwningVector<&ActionBase::DeleteExpr> &ConvertedArgs) { |
Douglas Gregor | 19aeac6 | 2009-11-14 03:27:21 +0000 | [diff] [blame] | 3539 | |
| 3540 | // Build the overload candidate set |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3541 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 3542 | unsigned NumArgs = ArgsPtr.size(); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3543 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 19aeac6 | 2009-11-14 03:27:21 +0000 | [diff] [blame] | 3544 | AddConstructorInitializationCandidates(*this, ClassType, Args, NumArgs, Kind, |
| 3545 | CandidateSet); |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 3546 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3547 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3548 | switch (BestViableFunction(CandidateSet, Loc, Best)) { |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3549 | case OR_Success: |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3550 | // We found a constructor. Break out so that we can convert the arguments |
| 3551 | // appropriately. |
| 3552 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3553 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3554 | case OR_No_Viable_Function: |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 3555 | if (InitEntity) |
| 3556 | Diag(Loc, diag::err_ovl_no_viable_function_in_init) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 3557 | << InitEntity << Range; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 3558 | else |
| 3559 | Diag(Loc, diag::err_ovl_no_viable_function_in_init) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 3560 | << ClassType << Range; |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 3561 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3562 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3563 | |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3564 | case OR_Ambiguous: |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 3565 | if (InitEntity) |
| 3566 | Diag(Loc, diag::err_ovl_ambiguous_init) << InitEntity << Range; |
| 3567 | else |
| 3568 | Diag(Loc, diag::err_ovl_ambiguous_init) << ClassType << Range; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3569 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3570 | return 0; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3571 | |
| 3572 | case OR_Deleted: |
| 3573 | if (InitEntity) |
| 3574 | Diag(Loc, diag::err_ovl_deleted_init) |
| 3575 | << Best->Function->isDeleted() |
| 3576 | << InitEntity << Range; |
| 3577 | else |
| 3578 | Diag(Loc, diag::err_ovl_deleted_init) |
| 3579 | << Best->Function->isDeleted() |
| 3580 | << InitEntity << Range; |
| 3581 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 3582 | return 0; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3583 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3584 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3585 | // Convert the arguments, fill in default arguments, etc. |
| 3586 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
| 3587 | if (CompleteConstructorCall(Constructor, move(ArgsPtr), Loc, ConvertedArgs)) |
| 3588 | return 0; |
| 3589 | |
| 3590 | return Constructor; |
| 3591 | } |
| 3592 | |
| 3593 | /// \brief Given a constructor and the set of arguments provided for the |
| 3594 | /// constructor, convert the arguments and add any required default arguments |
| 3595 | /// to form a proper call to this constructor. |
| 3596 | /// |
| 3597 | /// \returns true if an error occurred, false otherwise. |
| 3598 | bool |
| 3599 | Sema::CompleteConstructorCall(CXXConstructorDecl *Constructor, |
| 3600 | MultiExprArg ArgsPtr, |
| 3601 | SourceLocation Loc, |
| 3602 | ASTOwningVector<&ActionBase::DeleteExpr> &ConvertedArgs) { |
| 3603 | // FIXME: This duplicates a lot of code from Sema::ConvertArgumentsForCall. |
| 3604 | unsigned NumArgs = ArgsPtr.size(); |
| 3605 | Expr **Args = (Expr **)ArgsPtr.get(); |
| 3606 | |
| 3607 | const FunctionProtoType *Proto |
| 3608 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 3609 | assert(Proto && "Constructor without a prototype?"); |
| 3610 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3611 | unsigned NumArgsToCheck = NumArgs; |
| 3612 | |
| 3613 | // If too few arguments are available, we'll fill in the rest with defaults. |
| 3614 | if (NumArgs < NumArgsInProto) { |
| 3615 | NumArgsToCheck = NumArgsInProto; |
| 3616 | ConvertedArgs.reserve(NumArgsInProto); |
| 3617 | } else { |
| 3618 | ConvertedArgs.reserve(NumArgs); |
| 3619 | if (NumArgs > NumArgsInProto) |
| 3620 | NumArgsToCheck = NumArgsInProto; |
| 3621 | } |
| 3622 | |
| 3623 | // Convert arguments |
| 3624 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 3625 | QualType ProtoArgType = Proto->getArgType(i); |
| 3626 | |
| 3627 | Expr *Arg; |
| 3628 | if (i < NumArgs) { |
| 3629 | Arg = Args[i]; |
Anders Carlsson | 7171011 | 2009-09-15 21:14:33 +0000 | [diff] [blame] | 3630 | |
| 3631 | // Pass the argument. |
| 3632 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 3633 | return true; |
| 3634 | |
| 3635 | Args[i] = 0; |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3636 | } else { |
| 3637 | ParmVarDecl *Param = Constructor->getParamDecl(i); |
| 3638 | |
| 3639 | OwningExprResult DefArg = BuildCXXDefaultArgExpr(Loc, Constructor, Param); |
| 3640 | if (DefArg.isInvalid()) |
| 3641 | return true; |
| 3642 | |
| 3643 | Arg = DefArg.takeAs<Expr>(); |
| 3644 | } |
| 3645 | |
| 3646 | ConvertedArgs.push_back(Arg); |
| 3647 | } |
| 3648 | |
| 3649 | // If this is a variadic call, handle args passed through "...". |
| 3650 | if (Proto->isVariadic()) { |
| 3651 | // Promote the arguments (C99 6.5.2.2p7). |
| 3652 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 3653 | Expr *Arg = Args[i]; |
| 3654 | if (DefaultVariadicArgumentPromotion(Arg, VariadicConstructor)) |
| 3655 | return true; |
| 3656 | |
| 3657 | ConvertedArgs.push_back(Arg); |
| 3658 | Args[i] = 0; |
| 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | return false; |
Douglas Gregor | 18fe568 | 2008-11-03 20:45:27 +0000 | [diff] [blame] | 3663 | } |
| 3664 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3665 | /// CompareReferenceRelationship - Compare the two types T1 and T2 to |
| 3666 | /// determine whether they are reference-related, |
| 3667 | /// reference-compatible, reference-compatible with added |
| 3668 | /// qualification, or incompatible, for use in C++ initialization by |
| 3669 | /// reference (C++ [dcl.ref.init]p4). Neither type can be a reference |
| 3670 | /// type, and the first type (T1) is the pointee type of the reference |
| 3671 | /// type being initialized. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | Sema::ReferenceCompareResult |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3673 | Sema::CompareReferenceRelationship(SourceLocation Loc, |
| 3674 | QualType OrigT1, QualType OrigT2, |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3675 | bool& DerivedToBase) { |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3676 | assert(!OrigT1->isReferenceType() && |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3677 | "T1 must be the pointee type of the reference type"); |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3678 | assert(!OrigT2->isReferenceType() && "T2 cannot be a reference type"); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3679 | |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3680 | QualType T1 = Context.getCanonicalType(OrigT1); |
| 3681 | QualType T2 = Context.getCanonicalType(OrigT2); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3682 | QualType UnqualT1 = T1.getLocalUnqualifiedType(); |
| 3683 | QualType UnqualT2 = T2.getLocalUnqualifiedType(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3684 | |
| 3685 | // C++ [dcl.init.ref]p4: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3686 | // Given types "cv1 T1" and "cv2 T2," "cv1 T1" is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3687 | // reference-related to "cv2 T2" if T1 is the same type as T2, or |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3688 | // T1 is a base class of T2. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3689 | if (UnqualT1 == UnqualT2) |
| 3690 | DerivedToBase = false; |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3691 | else if (!RequireCompleteType(Loc, OrigT1, PDiag()) && |
| 3692 | !RequireCompleteType(Loc, OrigT2, PDiag()) && |
| 3693 | IsDerivedFrom(UnqualT2, UnqualT1)) |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3694 | DerivedToBase = true; |
| 3695 | else |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3696 | return Ref_Incompatible; |
| 3697 | |
| 3698 | // At this point, we know that T1 and T2 are reference-related (at |
| 3699 | // least). |
| 3700 | |
| 3701 | // C++ [dcl.init.ref]p4: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3702 | // "cv1 T1" is reference-compatible with "cv2 T2" if T1 is |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3703 | // reference-related to T2 and cv1 is the same cv-qualification |
| 3704 | // as, or greater cv-qualification than, cv2. For purposes of |
| 3705 | // overload resolution, cases for which cv1 is greater |
| 3706 | // cv-qualification than cv2 are identified as |
| 3707 | // reference-compatible with added qualification (see 13.3.3.2). |
| 3708 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 3709 | return Ref_Compatible; |
| 3710 | else if (T1.isMoreQualifiedThan(T2)) |
| 3711 | return Ref_Compatible_With_Added_Qualification; |
| 3712 | else |
| 3713 | return Ref_Related; |
| 3714 | } |
| 3715 | |
| 3716 | /// CheckReferenceInit - Check the initialization of a reference |
| 3717 | /// variable with the given initializer (C++ [dcl.init.ref]). Init is |
| 3718 | /// the initializer (either a simple initializer or an initializer |
Douglas Gregor | 3205a78 | 2008-10-29 23:31:03 +0000 | [diff] [blame] | 3719 | /// list), and DeclType is the type of the declaration. When ICS is |
| 3720 | /// non-null, this routine will compute the implicit conversion |
| 3721 | /// sequence according to C++ [over.ics.ref] and will not produce any |
| 3722 | /// diagnostics; when ICS is null, it will emit diagnostics when any |
| 3723 | /// errors are found. Either way, a return value of true indicates |
| 3724 | /// that there was a failure, a return value of false indicates that |
| 3725 | /// the reference initialization succeeded. |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 3726 | /// |
| 3727 | /// When @p SuppressUserConversions, user-defined conversions are |
| 3728 | /// suppressed. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3729 | /// When @p AllowExplicit, we also permit explicit user-defined |
| 3730 | /// conversion functions. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3731 | /// When @p ForceRValue, we unconditionally treat the initializer as an rvalue. |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 3732 | /// When @p IgnoreBaseAccess, we don't do access control on to-base conversion. |
| 3733 | /// This is used when this is called from a C-style cast. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | bool |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3735 | Sema::CheckReferenceInit(Expr *&Init, QualType DeclType, |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3736 | SourceLocation DeclLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3737 | bool SuppressUserConversions, |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 3738 | bool AllowExplicit, bool ForceRValue, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 3739 | ImplicitConversionSequence *ICS, |
| 3740 | bool IgnoreBaseAccess) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3741 | assert(DeclType->isReferenceType() && "Reference init needs a reference"); |
| 3742 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3743 | QualType T1 = DeclType->getAs<ReferenceType>()->getPointeeType(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3744 | QualType T2 = Init->getType(); |
| 3745 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3746 | // If the initializer is the address of an overloaded function, try |
| 3747 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3748 | // type of the resulting function. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 3749 | if (Context.getCanonicalType(T2) == Context.OverloadTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3750 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Init, DeclType, |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3751 | ICS != 0); |
| 3752 | if (Fn) { |
| 3753 | // Since we're performing this reference-initialization for |
| 3754 | // real, update the initializer with the resulting function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3755 | if (!ICS) { |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3756 | if (DiagnoseUseOfDecl(Fn, DeclLoc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3757 | return true; |
| 3758 | |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 3759 | Init = FixOverloadedFunctionReference(Init, Fn); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3760 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3761 | |
| 3762 | T2 = Fn->getType(); |
| 3763 | } |
| 3764 | } |
| 3765 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3766 | // Compute some basic properties of the types and the initializer. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3767 | bool isRValRef = DeclType->isRValueReferenceType(); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3768 | bool DerivedToBase = false; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 3769 | Expr::isLvalueResult InitLvalue = ForceRValue ? Expr::LV_InvalidExpression : |
| 3770 | Init->isLvalue(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3771 | ReferenceCompareResult RefRelationship |
Douglas Gregor | 393896f | 2009-11-05 13:06:35 +0000 | [diff] [blame] | 3772 | = CompareReferenceRelationship(DeclLoc, T1, T2, DerivedToBase); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3773 | |
| 3774 | // Most paths end in a failed conversion. |
| 3775 | if (ICS) |
| 3776 | ICS->ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3777 | |
| 3778 | // C++ [dcl.init.ref]p5: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3779 | // A reference to type "cv1 T1" is initialized by an expression |
| 3780 | // of type "cv2 T2" as follows: |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3781 | |
| 3782 | // -- If the initializer expression |
| 3783 | |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3784 | // Rvalue references cannot bind to lvalues (N2812). |
| 3785 | // There is absolutely no situation where they can. In particular, note that |
| 3786 | // this is ill-formed, even if B has a user-defined conversion to A&&: |
| 3787 | // B b; |
| 3788 | // A&& r = b; |
| 3789 | if (isRValRef && InitLvalue == Expr::LV_Valid) { |
| 3790 | if (!ICS) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3791 | Diag(DeclLoc, diag::err_lvalue_to_rvalue_ref) |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3792 | << Init->getSourceRange(); |
| 3793 | return true; |
| 3794 | } |
| 3795 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3796 | bool BindsDirectly = false; |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3797 | // -- is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 3798 | // reference-compatible with "cv2 T2," or |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3799 | // |
| 3800 | // Note that the bit-field check is skipped if we are just computing |
| 3801 | // the implicit conversion sequence (C++ [over.best.ics]p2). |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3802 | if (InitLvalue == Expr::LV_Valid && (ICS || !Init->getBitField()) && |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3803 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3804 | BindsDirectly = true; |
| 3805 | |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3806 | if (ICS) { |
| 3807 | // C++ [over.ics.ref]p1: |
| 3808 | // When a parameter of reference type binds directly (8.5.3) |
| 3809 | // to an argument expression, the implicit conversion sequence |
| 3810 | // is the identity conversion, unless the argument expression |
| 3811 | // has a type that is a derived class of the parameter type, |
| 3812 | // in which case the implicit conversion sequence is a |
| 3813 | // derived-to-base Conversion (13.3.3.1). |
| 3814 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 3815 | ICS->Standard.First = ICK_Identity; |
| 3816 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 3817 | ICS->Standard.Third = ICK_Identity; |
| 3818 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 3819 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 3820 | ICS->Standard.ReferenceBinding = true; |
| 3821 | ICS->Standard.DirectBinding = true; |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3822 | ICS->Standard.RRefBinding = false; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3823 | ICS->Standard.CopyConstructor = 0; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3824 | |
| 3825 | // Nothing more to do: the inaccessibility/ambiguity check for |
| 3826 | // derived-to-base conversions is suppressed when we're |
| 3827 | // computing the implicit conversion sequence (C++ |
| 3828 | // [over.best.ics]p2). |
| 3829 | return false; |
| 3830 | } else { |
| 3831 | // Perform the conversion. |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3832 | CastExpr::CastKind CK = CastExpr::CK_NoOp; |
| 3833 | if (DerivedToBase) |
| 3834 | CK = CastExpr::CK_DerivedToBase; |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 3835 | else if(CheckExceptionSpecCompatibility(Init, T1)) |
| 3836 | return true; |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 3837 | ImpCastExprToType(Init, T1, CK, /*isLvalue=*/true); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3838 | } |
| 3839 | } |
| 3840 | |
| 3841 | // -- has a class type (i.e., T2 is a class type) and can be |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3842 | // implicitly converted to an lvalue of type "cv3 T3," |
| 3843 | // where "cv1 T1" is reference-compatible with "cv3 T3" |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3844 | // 92) (this conversion is selected by enumerating the |
| 3845 | // applicable conversion functions (13.3.1.6) and choosing |
| 3846 | // the best one through overload resolution (13.3)), |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 3847 | if (!isRValRef && !SuppressUserConversions && T2->isRecordType() && |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 3848 | !RequireCompleteType(DeclLoc, T2, 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3849 | CXXRecordDecl *T2RecordDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3850 | = dyn_cast<CXXRecordDecl>(T2->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3851 | |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3852 | OverloadCandidateSet CandidateSet; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3853 | OverloadedFunctionDecl *Conversions |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 3854 | = T2RecordDecl->getVisibleConversionFunctions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | for (OverloadedFunctionDecl::function_iterator Func |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3856 | = Conversions->function_begin(); |
| 3857 | Func != Conversions->function_end(); ++Func) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3858 | FunctionTemplateDecl *ConvTemplate |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3859 | = dyn_cast<FunctionTemplateDecl>(*Func); |
| 3860 | CXXConversionDecl *Conv; |
| 3861 | if (ConvTemplate) |
| 3862 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3863 | else |
| 3864 | Conv = cast<CXXConversionDecl>(*Func); |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 3865 | |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3866 | // If the conversion function doesn't return a reference type, |
| 3867 | // it can't be considered for this conversion. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3868 | if (Conv->getConversionType()->isLValueReferenceType() && |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3869 | (AllowExplicit || !Conv->isExplicit())) { |
| 3870 | if (ConvTemplate) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3871 | AddTemplateConversionCandidate(ConvTemplate, Init, DeclType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3872 | CandidateSet); |
| 3873 | else |
| 3874 | AddConversionCandidate(Conv, Init, DeclType, CandidateSet); |
| 3875 | } |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
| 3878 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3879 | switch (BestViableFunction(CandidateSet, DeclLoc, Best)) { |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3880 | case OR_Success: |
| 3881 | // This is a direct binding. |
| 3882 | BindsDirectly = true; |
| 3883 | |
| 3884 | if (ICS) { |
| 3885 | // C++ [over.ics.ref]p1: |
| 3886 | // |
| 3887 | // [...] If the parameter binds directly to the result of |
| 3888 | // applying a conversion function to the argument |
| 3889 | // expression, the implicit conversion sequence is a |
| 3890 | // user-defined conversion sequence (13.3.3.1.2), with the |
| 3891 | // second standard conversion sequence either an identity |
| 3892 | // conversion or, if the conversion function returns an |
| 3893 | // entity of a type that is a derived class of the parameter |
| 3894 | // type, a derived-to-base Conversion. |
| 3895 | ICS->ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
| 3896 | ICS->UserDefined.Before = Best->Conversions[0].Standard; |
| 3897 | ICS->UserDefined.After = Best->FinalConversion; |
| 3898 | ICS->UserDefined.ConversionFunction = Best->Function; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 3899 | ICS->UserDefined.EllipsisConversion = false; |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3900 | assert(ICS->UserDefined.After.ReferenceBinding && |
| 3901 | ICS->UserDefined.After.DirectBinding && |
| 3902 | "Expected a direct reference binding!"); |
| 3903 | return false; |
| 3904 | } else { |
Fariborz Jahanian | 8f489d6 | 2009-09-23 22:34:00 +0000 | [diff] [blame] | 3905 | OwningExprResult InitConversion = |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3906 | BuildCXXCastArgument(DeclLoc, QualType(), |
Fariborz Jahanian | 8f489d6 | 2009-09-23 22:34:00 +0000 | [diff] [blame] | 3907 | CastExpr::CK_UserDefinedConversion, |
| 3908 | cast<CXXMethodDecl>(Best->Function), |
| 3909 | Owned(Init)); |
| 3910 | Init = InitConversion.takeAs<Expr>(); |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 3911 | |
| 3912 | if (CheckExceptionSpecCompatibility(Init, T1)) |
| 3913 | return true; |
Fariborz Jahanian | 8f489d6 | 2009-09-23 22:34:00 +0000 | [diff] [blame] | 3914 | ImpCastExprToType(Init, T1, CastExpr::CK_UserDefinedConversion, |
| 3915 | /*isLvalue=*/true); |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3916 | } |
| 3917 | break; |
| 3918 | |
| 3919 | case OR_Ambiguous: |
Fariborz Jahanian | d9290cb | 2009-10-14 00:52:43 +0000 | [diff] [blame] | 3920 | if (ICS) { |
| 3921 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3922 | Cand != CandidateSet.end(); ++Cand) |
| 3923 | if (Cand->Viable) |
| 3924 | ICS->ConversionFunctionSet.push_back(Cand->Function); |
| 3925 | break; |
| 3926 | } |
| 3927 | Diag(DeclLoc, diag::err_ref_init_ambiguous) << DeclType << Init->getType() |
| 3928 | << Init->getSourceRange(); |
| 3929 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3930 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3931 | |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3932 | case OR_No_Viable_Function: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3933 | case OR_Deleted: |
| 3934 | // There was no suitable conversion, or we found a deleted |
| 3935 | // conversion; continue with other checks. |
Douglas Gregor | cb9b977 | 2008-11-10 16:14:15 +0000 | [diff] [blame] | 3936 | break; |
| 3937 | } |
| 3938 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3939 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3940 | if (BindsDirectly) { |
| 3941 | // C++ [dcl.init.ref]p4: |
| 3942 | // [...] In all cases where the reference-related or |
| 3943 | // reference-compatible relationship of two types is used to |
| 3944 | // establish the validity of a reference binding, and T1 is a |
| 3945 | // base class of T2, a program that necessitates such a binding |
| 3946 | // is ill-formed if T1 is an inaccessible (clause 11) or |
| 3947 | // ambiguous (10.2) base class of T2. |
| 3948 | // |
| 3949 | // Note that we only check this condition when we're allowed to |
| 3950 | // complain about errors, because we should not be checking for |
| 3951 | // ambiguity (or inaccessibility) unless the reference binding |
| 3952 | // actually happens. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3953 | if (DerivedToBase) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3954 | return CheckDerivedToBaseConversion(T2, T1, DeclLoc, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 3955 | Init->getSourceRange(), |
| 3956 | IgnoreBaseAccess); |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3957 | else |
| 3958 | return false; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
| 3961 | // -- Otherwise, the reference shall be to a non-volatile const |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3962 | // type (i.e., cv1 shall be const), or the reference shall be an |
| 3963 | // rvalue reference and the initializer expression shall be an rvalue. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3964 | if (!isRValRef && T1.getCVRQualifiers() != Qualifiers::Const) { |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3965 | if (!ICS) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 3966 | Diag(DeclLoc, diag::err_not_reference_to_const_init) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3967 | << T1 << (InitLvalue != Expr::LV_Valid? "temporary" : "value") |
| 3968 | << T2 << Init->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3969 | return true; |
| 3970 | } |
| 3971 | |
| 3972 | // -- If the initializer expression is an rvalue, with T2 a |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3973 | // class type, and "cv1 T1" is reference-compatible with |
| 3974 | // "cv2 T2," the reference is bound in one of the |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3975 | // following ways (the choice is implementation-defined): |
| 3976 | // |
| 3977 | // -- The reference is bound to the object represented by |
| 3978 | // the rvalue (see 3.10) or to a sub-object within that |
| 3979 | // object. |
| 3980 | // |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 3981 | // -- A temporary of type "cv1 T2" [sic] is created, and |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3982 | // a constructor is called to copy the entire rvalue |
| 3983 | // object into the temporary. The reference is bound to |
| 3984 | // the temporary or to a sub-object within the |
| 3985 | // temporary. |
| 3986 | // |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3987 | // The constructor that would be used to make the copy |
| 3988 | // shall be callable whether or not the copy is actually |
| 3989 | // done. |
| 3990 | // |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 3991 | // Note that C++0x [dcl.init.ref]p5 takes away this implementation |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3992 | // freedom, so we will always take the first option and never build |
| 3993 | // a temporary in this case. FIXME: We will, however, have to check |
| 3994 | // for the presence of a copy constructor in C++98/03 mode. |
| 3995 | if (InitLvalue != Expr::LV_Valid && T2->isRecordType() && |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3996 | RefRelationship >= Ref_Compatible_With_Added_Qualification) { |
| 3997 | if (ICS) { |
| 3998 | ICS->ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 3999 | ICS->Standard.First = ICK_Identity; |
| 4000 | ICS->Standard.Second = DerivedToBase? ICK_Derived_To_Base : ICK_Identity; |
| 4001 | ICS->Standard.Third = ICK_Identity; |
| 4002 | ICS->Standard.FromTypePtr = T2.getAsOpaquePtr(); |
| 4003 | ICS->Standard.ToTypePtr = T1.getAsOpaquePtr(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 4004 | ICS->Standard.ReferenceBinding = true; |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4005 | ICS->Standard.DirectBinding = false; |
| 4006 | ICS->Standard.RRefBinding = isRValRef; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 4007 | ICS->Standard.CopyConstructor = 0; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4008 | } else { |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4009 | CastExpr::CastKind CK = CastExpr::CK_NoOp; |
| 4010 | if (DerivedToBase) |
| 4011 | CK = CastExpr::CK_DerivedToBase; |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 4012 | else if(CheckExceptionSpecCompatibility(Init, T1)) |
| 4013 | return true; |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 4014 | ImpCastExprToType(Init, T1, CK, /*isLvalue=*/false); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4015 | } |
| 4016 | return false; |
| 4017 | } |
| 4018 | |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4019 | // -- Otherwise, a temporary of type "cv1 T1" is created and |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4020 | // initialized from the initializer expression using the |
| 4021 | // rules for a non-reference copy initialization (8.5). The |
| 4022 | // reference is then bound to the temporary. If T1 is |
| 4023 | // reference-related to T2, cv1 must be the same |
| 4024 | // cv-qualification as, or greater cv-qualification than, |
| 4025 | // cv2; otherwise, the program is ill-formed. |
| 4026 | if (RefRelationship == Ref_Related) { |
| 4027 | // If cv1 == cv2 or cv1 is a greater cv-qualified than cv2, then |
| 4028 | // we would be reference-compatible or reference-compatible with |
| 4029 | // added qualification. But that wasn't the case, so the reference |
| 4030 | // initialization fails. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4031 | if (!ICS) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 4032 | Diag(DeclLoc, diag::err_reference_init_drops_quals) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4033 | << T1 << (InitLvalue != Expr::LV_Valid? "temporary" : "value") |
| 4034 | << T2 << Init->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4035 | return true; |
| 4036 | } |
| 4037 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 4038 | // If at least one of the types is a class type, the types are not |
| 4039 | // related, and we aren't allowed any user conversions, the |
| 4040 | // reference binding fails. This case is important for breaking |
| 4041 | // recursion, since TryImplicitConversion below will attempt to |
| 4042 | // create a temporary through the use of a copy constructor. |
| 4043 | if (SuppressUserConversions && RefRelationship == Ref_Incompatible && |
| 4044 | (T1->isRecordType() || T2->isRecordType())) { |
| 4045 | if (!ICS) |
Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 4046 | Diag(DeclLoc, diag::err_typecheck_convert_incompatible) |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 4047 | << DeclType << Init->getType() << "initializing" << Init->getSourceRange(); |
| 4048 | return true; |
| 4049 | } |
| 4050 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4051 | // Actually try to convert the initializer to T1. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4052 | if (ICS) { |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4053 | // C++ [over.ics.ref]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4054 | // |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4055 | // When a parameter of reference type is not bound directly to |
| 4056 | // an argument expression, the conversion sequence is the one |
| 4057 | // required to convert the argument expression to the |
| 4058 | // underlying type of the reference according to |
| 4059 | // 13.3.3.1. Conceptually, this conversion sequence corresponds |
| 4060 | // to copy-initializing a temporary of the underlying type with |
| 4061 | // the argument expression. Any difference in top-level |
| 4062 | // cv-qualification is subsumed by the initialization itself |
| 4063 | // and does not constitute a conversion. |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 4064 | *ICS = TryImplicitConversion(Init, T1, SuppressUserConversions, |
| 4065 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 4066 | /*ForceRValue=*/false, |
| 4067 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4068 | |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4069 | // Of course, that's still a reference binding. |
| 4070 | if (ICS->ConversionKind == ImplicitConversionSequence::StandardConversion) { |
| 4071 | ICS->Standard.ReferenceBinding = true; |
| 4072 | ICS->Standard.RRefBinding = isRValRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4073 | } else if (ICS->ConversionKind == |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 4074 | ImplicitConversionSequence::UserDefinedConversion) { |
| 4075 | ICS->UserDefined.After.ReferenceBinding = true; |
| 4076 | ICS->UserDefined.After.RRefBinding = isRValRef; |
| 4077 | } |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4078 | return ICS->ConversionKind == ImplicitConversionSequence::BadConversion; |
| 4079 | } else { |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 4080 | ImplicitConversionSequence Conversions; |
| 4081 | bool badConversion = PerformImplicitConversion(Init, T1, "initializing", |
| 4082 | false, false, |
| 4083 | Conversions); |
| 4084 | if (badConversion) { |
| 4085 | if ((Conversions.ConversionKind == |
| 4086 | ImplicitConversionSequence::BadConversion) |
Fariborz Jahanian | 82ad87b | 2009-09-28 22:03:07 +0000 | [diff] [blame] | 4087 | && !Conversions.ConversionFunctionSet.empty()) { |
Fariborz Jahanian | 7ad2d56 | 2009-09-24 00:42:43 +0000 | [diff] [blame] | 4088 | Diag(DeclLoc, |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 4089 | diag::err_lvalue_to_rvalue_ambig_ref) << Init->getSourceRange(); |
| 4090 | for (int j = Conversions.ConversionFunctionSet.size()-1; |
| 4091 | j >= 0; j--) { |
| 4092 | FunctionDecl *Func = Conversions.ConversionFunctionSet[j]; |
| 4093 | Diag(Func->getLocation(), diag::err_ovl_candidate); |
| 4094 | } |
| 4095 | } |
Fariborz Jahanian | 893f955 | 2009-09-30 21:23:30 +0000 | [diff] [blame] | 4096 | else { |
| 4097 | if (isRValRef) |
| 4098 | Diag(DeclLoc, diag::err_lvalue_to_rvalue_ref) |
| 4099 | << Init->getSourceRange(); |
| 4100 | else |
| 4101 | Diag(DeclLoc, diag::err_invalid_initialization) |
| 4102 | << DeclType << Init->getType() << Init->getSourceRange(); |
| 4103 | } |
Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 4104 | } |
| 4105 | return badConversion; |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 4106 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 4107 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4108 | |
| 4109 | /// CheckOverloadedOperatorDeclaration - Check whether the declaration |
| 4110 | /// of this overloaded operator is well-formed. If so, returns false; |
| 4111 | /// otherwise, emits appropriate diagnostics and returns true. |
| 4112 | bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) { |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4113 | assert(FnDecl && FnDecl->isOverloadedOperator() && |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4114 | "Expected an overloaded operator declaration"); |
| 4115 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4116 | OverloadedOperatorKind Op = FnDecl->getOverloadedOperator(); |
| 4117 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4118 | // C++ [over.oper]p5: |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4119 | // The allocation and deallocation functions, operator new, |
| 4120 | // operator new[], operator delete and operator delete[], are |
| 4121 | // described completely in 3.7.3. The attributes and restrictions |
| 4122 | // found in the rest of this subclause do not apply to them unless |
| 4123 | // explicitly stated in 3.7.3. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4124 | // FIXME: Write a separate routine for checking this. For now, just allow it. |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 4125 | if (Op == OO_Delete || Op == OO_Array_Delete) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4126 | return false; |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 4127 | |
| 4128 | if (Op == OO_New || Op == OO_Array_New) { |
| 4129 | bool ret = false; |
| 4130 | if (FunctionDecl::param_iterator Param = FnDecl->param_begin()) { |
| 4131 | QualType SizeTy = Context.getCanonicalType(Context.getSizeType()); |
| 4132 | QualType T = Context.getCanonicalType((*Param)->getType()); |
| 4133 | if (!T->isDependentType() && SizeTy != T) { |
| 4134 | Diag(FnDecl->getLocation(), |
| 4135 | diag::err_operator_new_param_type) << FnDecl->getDeclName() |
| 4136 | << SizeTy; |
| 4137 | ret = true; |
| 4138 | } |
| 4139 | } |
| 4140 | QualType ResultTy = Context.getCanonicalType(FnDecl->getResultType()); |
| 4141 | if (!ResultTy->isDependentType() && ResultTy != Context.VoidPtrTy) |
| 4142 | return Diag(FnDecl->getLocation(), |
| 4143 | diag::err_operator_new_result_type) << FnDecl->getDeclName() |
Douglas Gregor | cffecd0 | 2009-11-12 16:49:45 +0000 | [diff] [blame] | 4144 | << static_cast<QualType>(Context.VoidPtrTy); |
Fariborz Jahanian | b03bfa5 | 2009-11-10 23:47:18 +0000 | [diff] [blame] | 4145 | return ret; |
| 4146 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4147 | |
| 4148 | // C++ [over.oper]p6: |
| 4149 | // An operator function shall either be a non-static member |
| 4150 | // function or be a non-member function and have at least one |
| 4151 | // parameter whose type is a class, a reference to a class, an |
| 4152 | // enumeration, or a reference to an enumeration. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4153 | if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4154 | if (MethodDecl->isStatic()) |
| 4155 | return Diag(FnDecl->getLocation(), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4156 | diag::err_operator_overload_static) << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4157 | } else { |
| 4158 | bool ClassOrEnumParam = false; |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4159 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(), |
| 4160 | ParamEnd = FnDecl->param_end(); |
| 4161 | Param != ParamEnd; ++Param) { |
| 4162 | QualType ParamType = (*Param)->getType().getNonReferenceType(); |
Eli Friedman | 5d39dee | 2009-06-27 05:59:59 +0000 | [diff] [blame] | 4163 | if (ParamType->isDependentType() || ParamType->isRecordType() || |
| 4164 | ParamType->isEnumeralType()) { |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4165 | ClassOrEnumParam = true; |
| 4166 | break; |
| 4167 | } |
| 4168 | } |
| 4169 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4170 | if (!ClassOrEnumParam) |
| 4171 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4172 | diag::err_operator_overload_needs_class_or_enum) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4173 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4174 | } |
| 4175 | |
| 4176 | // C++ [over.oper]p8: |
| 4177 | // An operator function cannot have default arguments (8.3.6), |
| 4178 | // except where explicitly stated below. |
| 4179 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4180 | // Only the function-call operator allows default arguments |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4181 | // (C++ [over.call]p1). |
| 4182 | if (Op != OO_Call) { |
| 4183 | for (FunctionDecl::param_iterator Param = FnDecl->param_begin(); |
| 4184 | Param != FnDecl->param_end(); ++Param) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4185 | if ((*Param)->hasUnparsedDefaultArg()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4186 | return Diag((*Param)->getLocation(), |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4187 | diag::err_operator_overload_default_arg) |
| 4188 | << FnDecl->getDeclName(); |
| 4189 | else if (Expr *DefArg = (*Param)->getDefaultArg()) |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4190 | return Diag((*Param)->getLocation(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4191 | diag::err_operator_overload_default_arg) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4192 | << FnDecl->getDeclName() << DefArg->getSourceRange(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4193 | } |
| 4194 | } |
| 4195 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4196 | static const bool OperatorUses[NUM_OVERLOADED_OPERATORS][3] = { |
| 4197 | { false, false, false } |
| 4198 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 4199 | , { Unary, Binary, MemberOnly } |
| 4200 | #include "clang/Basic/OperatorKinds.def" |
| 4201 | }; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4202 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4203 | bool CanBeUnaryOperator = OperatorUses[Op][0]; |
| 4204 | bool CanBeBinaryOperator = OperatorUses[Op][1]; |
| 4205 | bool MustBeMemberOperator = OperatorUses[Op][2]; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4206 | |
| 4207 | // C++ [over.oper]p8: |
| 4208 | // [...] Operator functions cannot have more or fewer parameters |
| 4209 | // than the number required for the corresponding operator, as |
| 4210 | // described in the rest of this subclause. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4211 | unsigned NumParams = FnDecl->getNumParams() |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4212 | + (isa<CXXMethodDecl>(FnDecl)? 1 : 0); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4213 | if (Op != OO_Call && |
| 4214 | ((NumParams == 1 && !CanBeUnaryOperator) || |
| 4215 | (NumParams == 2 && !CanBeBinaryOperator) || |
| 4216 | (NumParams < 1) || (NumParams > 2))) { |
| 4217 | // We have the wrong number of parameters. |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4218 | unsigned ErrorKind; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4219 | if (CanBeUnaryOperator && CanBeBinaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4220 | ErrorKind = 2; // 2 -> unary or binary. |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4221 | } else if (CanBeUnaryOperator) { |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4222 | ErrorKind = 0; // 0 -> unary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4223 | } else { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 4224 | assert(CanBeBinaryOperator && |
| 4225 | "All non-call overloaded operators are unary or binary!"); |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4226 | ErrorKind = 1; // 1 -> binary |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 4227 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4228 | |
Chris Lattner | 416e46f | 2008-11-21 07:57:12 +0000 | [diff] [blame] | 4229 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_must_be) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4230 | << FnDecl->getDeclName() << NumParams << ErrorKind; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4231 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4232 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4233 | // Overloaded operators other than operator() cannot be variadic. |
| 4234 | if (Op != OO_Call && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4235 | FnDecl->getType()->getAs<FunctionProtoType>()->isVariadic()) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4236 | return Diag(FnDecl->getLocation(), diag::err_operator_overload_variadic) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4237 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
| 4240 | // Some operators must be non-static member functions. |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4241 | if (MustBeMemberOperator && !isa<CXXMethodDecl>(FnDecl)) { |
| 4242 | return Diag(FnDecl->getLocation(), |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 4243 | diag::err_operator_overload_must_be_member) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 4244 | << FnDecl->getDeclName(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
| 4247 | // C++ [over.inc]p1: |
| 4248 | // The user-defined function called operator++ implements the |
| 4249 | // prefix and postfix ++ operator. If this function is a member |
| 4250 | // function with no parameters, or a non-member function with one |
| 4251 | // parameter of class or enumeration type, it defines the prefix |
| 4252 | // increment operator ++ for objects of that type. If the function |
| 4253 | // is a member function with one parameter (which shall be of type |
| 4254 | // int) or a non-member function with two parameters (the second |
| 4255 | // of which shall be of type int), it defines the postfix |
| 4256 | // increment operator ++ for objects of that type. |
| 4257 | if ((Op == OO_PlusPlus || Op == OO_MinusMinus) && NumParams == 2) { |
| 4258 | ParmVarDecl *LastParam = FnDecl->getParamDecl(FnDecl->getNumParams() - 1); |
| 4259 | bool ParamIsInt = false; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4260 | if (const BuiltinType *BT = LastParam->getType()->getAs<BuiltinType>()) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4261 | ParamIsInt = BT->getKind() == BuiltinType::Int; |
| 4262 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 4263 | if (!ParamIsInt) |
| 4264 | return Diag(LastParam->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4265 | diag::err_operator_overload_post_incdec_must_be_int) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4266 | << LastParam->getType() << (Op == OO_MinusMinus); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 4269 | // Notify the class if it got an assignment operator. |
| 4270 | if (Op == OO_Equal) { |
| 4271 | // Would have returned earlier otherwise. |
| 4272 | assert(isa<CXXMethodDecl>(FnDecl) && |
| 4273 | "Overloaded = not member, but not filtered."); |
| 4274 | CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl); |
| 4275 | Method->getParent()->addedAssignmentOperator(Context, Method); |
| 4276 | } |
| 4277 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 4278 | return false; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4279 | } |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 4280 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4281 | /// ActOnStartLinkageSpecification - Parsed the beginning of a C++ |
| 4282 | /// linkage specification, including the language and (if present) |
| 4283 | /// the '{'. ExternLoc is the location of the 'extern', LangLoc is |
| 4284 | /// the location of the language string literal, which is provided |
| 4285 | /// by Lang/StrSize. LBraceLoc, if valid, provides the location of |
| 4286 | /// the '{' brace. Otherwise, this linkage specification does not |
| 4287 | /// have any braces. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4288 | Sema::DeclPtrTy Sema::ActOnStartLinkageSpecification(Scope *S, |
| 4289 | SourceLocation ExternLoc, |
| 4290 | SourceLocation LangLoc, |
| 4291 | const char *Lang, |
| 4292 | unsigned StrSize, |
| 4293 | SourceLocation LBraceLoc) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4294 | LinkageSpecDecl::LanguageIDs Language; |
| 4295 | if (strncmp(Lang, "\"C\"", StrSize) == 0) |
| 4296 | Language = LinkageSpecDecl::lang_c; |
| 4297 | else if (strncmp(Lang, "\"C++\"", StrSize) == 0) |
| 4298 | Language = LinkageSpecDecl::lang_cxx; |
| 4299 | else { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4300 | Diag(LangLoc, diag::err_bad_language); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4301 | return DeclPtrTy(); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4302 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4303 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4304 | // FIXME: Add all the various semantics of linkage specifications |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4305 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4306 | LinkageSpecDecl *D = LinkageSpecDecl::Create(Context, CurContext, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4307 | LangLoc, Language, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4308 | LBraceLoc.isValid()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4309 | CurContext->addDecl(D); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4310 | PushDeclContext(S, D); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4311 | return DeclPtrTy::make(D); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4314 | /// ActOnFinishLinkageSpecification - Completely the definition of |
| 4315 | /// the C++ linkage specification LinkageSpec. If RBraceLoc is |
| 4316 | /// valid, it's the position of the closing '}' brace in a linkage |
| 4317 | /// specification that uses braces. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4318 | Sema::DeclPtrTy Sema::ActOnFinishLinkageSpecification(Scope *S, |
| 4319 | DeclPtrTy LinkageSpec, |
| 4320 | SourceLocation RBraceLoc) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 4321 | if (LinkageSpec) |
| 4322 | PopDeclContext(); |
| 4323 | return LinkageSpec; |
Chris Lattner | 5a003a4 | 2008-12-17 07:09:26 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4326 | /// \brief Perform semantic analysis for the variable declaration that |
| 4327 | /// occurs within a C++ catch clause, returning the newly-created |
| 4328 | /// variable. |
| 4329 | VarDecl *Sema::BuildExceptionDeclaration(Scope *S, QualType ExDeclType, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4330 | DeclaratorInfo *DInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4331 | IdentifierInfo *Name, |
| 4332 | SourceLocation Loc, |
| 4333 | SourceRange Range) { |
| 4334 | bool Invalid = false; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4335 | |
| 4336 | // Arrays and functions decay. |
| 4337 | if (ExDeclType->isArrayType()) |
| 4338 | ExDeclType = Context.getArrayDecayedType(ExDeclType); |
| 4339 | else if (ExDeclType->isFunctionType()) |
| 4340 | ExDeclType = Context.getPointerType(ExDeclType); |
| 4341 | |
| 4342 | // C++ 15.3p1: The exception-declaration shall not denote an incomplete type. |
| 4343 | // The exception-declaration shall not denote a pointer or reference to an |
| 4344 | // incomplete type, other than [cv] void*. |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4345 | // N2844 forbids rvalue references. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4346 | if (!ExDeclType->isDependentType() && ExDeclType->isRValueReferenceType()) { |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4347 | Diag(Loc, diag::err_catch_rvalue_ref) << Range; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4348 | Invalid = true; |
| 4349 | } |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4350 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4351 | QualType BaseType = ExDeclType; |
| 4352 | int Mode = 0; // 0 for direct type, 1 for pointer, 2 for reference |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4353 | unsigned DK = diag::err_catch_incomplete; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4354 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4355 | BaseType = Ptr->getPointeeType(); |
| 4356 | Mode = 1; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4357 | DK = diag::err_catch_incomplete_ptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4358 | } else if (const ReferenceType *Ref = BaseType->getAs<ReferenceType>()) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4359 | // For the purpose of error recovery, we treat rvalue refs like lvalue refs. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4360 | BaseType = Ref->getPointeeType(); |
| 4361 | Mode = 2; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4362 | DK = diag::err_catch_incomplete_ref; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4363 | } |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 4364 | if (!Invalid && (Mode == 0 || !BaseType->isVoidType()) && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4365 | !BaseType->isDependentType() && RequireCompleteType(Loc, BaseType, DK)) |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4366 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4367 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4368 | if (!Invalid && !ExDeclType->isDependentType() && |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4369 | RequireNonAbstractType(Loc, ExDeclType, |
| 4370 | diag::err_abstract_type_in_decl, |
| 4371 | AbstractVariableType)) |
Sebastian Redl | fef9f59 | 2009-04-27 21:03:30 +0000 | [diff] [blame] | 4372 | Invalid = true; |
| 4373 | |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4374 | // FIXME: Need to test for ability to copy-construct and destroy the |
| 4375 | // exception variable. |
| 4376 | |
Sebastian Redl | 8351da0 | 2008-12-22 21:35:02 +0000 | [diff] [blame] | 4377 | // FIXME: Need to check for abstract classes. |
| 4378 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4379 | VarDecl *ExDecl = VarDecl::Create(Context, CurContext, Loc, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 4380 | Name, ExDeclType, DInfo, VarDecl::None); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4381 | |
| 4382 | if (Invalid) |
| 4383 | ExDecl->setInvalidDecl(); |
| 4384 | |
| 4385 | return ExDecl; |
| 4386 | } |
| 4387 | |
| 4388 | /// ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch |
| 4389 | /// handler. |
| 4390 | Sema::DeclPtrTy Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) { |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4391 | DeclaratorInfo *DInfo = 0; |
| 4392 | QualType ExDeclType = GetTypeForDeclarator(D, S, &DInfo); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4393 | |
| 4394 | bool Invalid = D.isInvalidType(); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4395 | IdentifierInfo *II = D.getIdentifier(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4396 | if (NamedDecl *PrevDecl = LookupSingleName(S, II, LookupOrdinaryName)) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4397 | // The scope should be freshly made just for us. There is just no way |
| 4398 | // it contains any previous declaration. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4399 | assert(!S->isDeclScope(DeclPtrTy::make(PrevDecl))); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4400 | if (PrevDecl->isTemplateParameter()) { |
| 4401 | // Maybe we will complain about the shadowed template parameter. |
| 4402 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4406 | if (D.getCXXScopeSpec().isSet() && !Invalid) { |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4407 | Diag(D.getIdentifierLoc(), diag::err_qualified_catch_declarator) |
| 4408 | << D.getCXXScopeSpec().getRange(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4409 | Invalid = true; |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4412 | VarDecl *ExDecl = BuildExceptionDeclaration(S, ExDeclType, DInfo, |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4413 | D.getIdentifier(), |
| 4414 | D.getIdentifierLoc(), |
| 4415 | D.getDeclSpec().getSourceRange()); |
| 4416 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 4417 | if (Invalid) |
| 4418 | ExDecl->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4419 | |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4420 | // Add the exception declaration into this scope. |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4421 | if (II) |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 4422 | PushOnScopeChains(ExDecl, S); |
| 4423 | else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4424 | CurContext->addDecl(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4425 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4426 | ProcessDeclAttributes(S, ExDecl, D); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4427 | return DeclPtrTy::make(ExDecl); |
Sebastian Redl | 4b07b29 | 2008-12-22 19:15:10 +0000 | [diff] [blame] | 4428 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 4429 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4430 | Sema::DeclPtrTy Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4431 | ExprArg assertexpr, |
| 4432 | ExprArg assertmessageexpr) { |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 4433 | Expr *AssertExpr = (Expr *)assertexpr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4434 | StringLiteral *AssertMessage = |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 4435 | cast<StringLiteral>((Expr *)assertmessageexpr.get()); |
| 4436 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 4437 | if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) { |
| 4438 | llvm::APSInt Value(32); |
| 4439 | if (!AssertExpr->isIntegerConstantExpr(Value, Context)) { |
| 4440 | Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) << |
| 4441 | AssertExpr->getSourceRange(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4442 | return DeclPtrTy(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 4443 | } |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 4444 | |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 4445 | if (Value == 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4446 | std::string str(AssertMessage->getStrData(), |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 4447 | AssertMessage->getByteLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4448 | Diag(AssertLoc, diag::err_static_assert_failed) |
Anders Carlsson | 94b15fb | 2009-03-15 18:44:04 +0000 | [diff] [blame] | 4449 | << str << AssertExpr->getSourceRange(); |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 4450 | } |
| 4451 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4452 | |
Anders Carlsson | 77d8142 | 2009-03-15 17:35:16 +0000 | [diff] [blame] | 4453 | assertexpr.release(); |
| 4454 | assertmessageexpr.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4455 | Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 4456 | AssertExpr, AssertMessage); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4457 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4458 | CurContext->addDecl(Decl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4459 | return DeclPtrTy::make(Decl); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 4460 | } |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4461 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 4462 | /// Handle a friend type declaration. This works in tandem with |
| 4463 | /// ActOnTag. |
| 4464 | /// |
| 4465 | /// Notes on friend class templates: |
| 4466 | /// |
| 4467 | /// We generally treat friend class declarations as if they were |
| 4468 | /// declaring a class. So, for example, the elaborated type specifier |
| 4469 | /// in a friend declaration is required to obey the restrictions of a |
| 4470 | /// class-head (i.e. no typedefs in the scope chain), template |
| 4471 | /// parameters are required to match up with simple template-ids, &c. |
| 4472 | /// However, unlike when declaring a template specialization, it's |
| 4473 | /// okay to refer to a template specialization without an empty |
| 4474 | /// template parameter declaration, e.g. |
| 4475 | /// friend class A<T>::B<unsigned>; |
| 4476 | /// We permit this as a special case; if there are any template |
| 4477 | /// parameters present at all, require proper matching, i.e. |
| 4478 | /// template <> template <class T> friend class A<int>::B; |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 4479 | Sema::DeclPtrTy Sema::ActOnFriendTypeDecl(Scope *S, const DeclSpec &DS, |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 4480 | MultiTemplateParamsArg TempParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4481 | SourceLocation Loc = DS.getSourceRange().getBegin(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4482 | |
| 4483 | assert(DS.isFriendSpecified()); |
| 4484 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 4485 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 4486 | // Try to convert the decl specifier to a type. This works for |
| 4487 | // friend templates because ActOnTag never produces a ClassTemplateDecl |
| 4488 | // for a TUK_Friend. |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 4489 | Declarator TheDeclarator(DS, Declarator::MemberContext); |
Chris Lattner | c7f1904 | 2009-10-25 17:47:27 +0000 | [diff] [blame] | 4490 | QualType T = GetTypeForDeclarator(TheDeclarator, S); |
| 4491 | if (TheDeclarator.isInvalidType()) |
| 4492 | return DeclPtrTy(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4493 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 4494 | // This is definitely an error in C++98. It's probably meant to |
| 4495 | // be forbidden in C++0x, too, but the specification is just |
| 4496 | // poorly written. |
| 4497 | // |
| 4498 | // The problem is with declarations like the following: |
| 4499 | // template <T> friend A<T>::foo; |
| 4500 | // where deciding whether a class C is a friend or not now hinges |
| 4501 | // on whether there exists an instantiation of A that causes |
| 4502 | // 'foo' to equal C. There are restrictions on class-heads |
| 4503 | // (which we declare (by fiat) elaborated friend declarations to |
| 4504 | // be) that makes this tractable. |
| 4505 | // |
| 4506 | // FIXME: handle "template <> friend class A<T>;", which |
| 4507 | // is possibly well-formed? Who even knows? |
| 4508 | if (TempParams.size() && !isa<ElaboratedType>(T)) { |
| 4509 | Diag(Loc, diag::err_tagless_friend_type_template) |
| 4510 | << DS.getSourceRange(); |
| 4511 | return DeclPtrTy(); |
| 4512 | } |
| 4513 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4514 | // C++ [class.friend]p2: |
| 4515 | // An elaborated-type-specifier shall be used in a friend declaration |
| 4516 | // for a class.* |
| 4517 | // * The class-key of the elaborated-type-specifier is required. |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 4518 | // This is one of the rare places in Clang where it's legitimate to |
| 4519 | // ask about the "spelling" of the type. |
| 4520 | if (!getLangOptions().CPlusPlus0x && !isa<ElaboratedType>(T)) { |
| 4521 | // If we evaluated the type to a record type, suggest putting |
| 4522 | // a tag in front. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4523 | if (const RecordType *RT = T->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 4524 | RecordDecl *RD = RT->getDecl(); |
| 4525 | |
| 4526 | std::string InsertionText = std::string(" ") + RD->getKindName(); |
| 4527 | |
John McCall | e3af023 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 4528 | Diag(DS.getTypeSpecTypeLoc(), diag::err_unelaborated_friend_type) |
| 4529 | << (unsigned) RD->getTagKind() |
| 4530 | << T |
| 4531 | << SourceRange(DS.getFriendSpecLoc()) |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 4532 | << CodeModificationHint::CreateInsertion(DS.getTypeSpecTypeLoc(), |
| 4533 | InsertionText); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4534 | return DeclPtrTy(); |
| 4535 | }else { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 4536 | Diag(DS.getFriendSpecLoc(), diag::err_unexpected_friend) |
| 4537 | << DS.getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4538 | return DeclPtrTy(); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4539 | } |
| 4540 | } |
| 4541 | |
John McCall | e3af023 | 2009-10-07 23:34:25 +0000 | [diff] [blame] | 4542 | // Enum types cannot be friends. |
| 4543 | if (T->getAs<EnumType>()) { |
| 4544 | Diag(DS.getTypeSpecTypeLoc(), diag::err_enum_friend) |
| 4545 | << SourceRange(DS.getFriendSpecLoc()); |
| 4546 | return DeclPtrTy(); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 4547 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4548 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4549 | // C++98 [class.friend]p1: A friend of a class is a function |
| 4550 | // or class that is not a member of the class . . . |
| 4551 | // But that's a silly restriction which nobody implements for |
| 4552 | // inner classes, and C++0x removes it anyway, so we only report |
| 4553 | // this (as a warning) if we're being pedantic. |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 4554 | if (!getLangOptions().CPlusPlus0x) |
| 4555 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 4556 | if (RT->getDecl()->getDeclContext() == CurContext) |
| 4557 | Diag(DS.getFriendSpecLoc(), diag::ext_friend_inner_class); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4558 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 4559 | Decl *D; |
| 4560 | if (TempParams.size()) |
| 4561 | D = FriendTemplateDecl::Create(Context, CurContext, Loc, |
| 4562 | TempParams.size(), |
| 4563 | (TemplateParameterList**) TempParams.release(), |
| 4564 | T.getTypePtr(), |
| 4565 | DS.getFriendSpecLoc()); |
| 4566 | else |
| 4567 | D = FriendDecl::Create(Context, CurContext, Loc, T.getTypePtr(), |
| 4568 | DS.getFriendSpecLoc()); |
| 4569 | D->setAccess(AS_public); |
| 4570 | CurContext->addDecl(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4571 | |
John McCall | dd4a3b0 | 2009-09-16 22:47:08 +0000 | [diff] [blame] | 4572 | return DeclPtrTy::make(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
John McCall | bbbcdd9 | 2009-09-11 21:02:39 +0000 | [diff] [blame] | 4575 | Sema::DeclPtrTy |
| 4576 | Sema::ActOnFriendFunctionDecl(Scope *S, |
| 4577 | Declarator &D, |
| 4578 | bool IsDefinition, |
| 4579 | MultiTemplateParamsArg TemplateParams) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4580 | const DeclSpec &DS = D.getDeclSpec(); |
| 4581 | |
| 4582 | assert(DS.isFriendSpecified()); |
| 4583 | assert(DS.getStorageClassSpec() == DeclSpec::SCS_unspecified); |
| 4584 | |
| 4585 | SourceLocation Loc = D.getIdentifierLoc(); |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4586 | DeclaratorInfo *DInfo = 0; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4587 | QualType T = GetTypeForDeclarator(D, S, &DInfo); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4588 | |
| 4589 | // C++ [class.friend]p1 |
| 4590 | // A friend of a class is a function or class.... |
| 4591 | // Note that this sees through typedefs, which is intended. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4592 | // It *doesn't* see through dependent types, which is correct |
| 4593 | // according to [temp.arg.type]p3: |
| 4594 | // If a declaration acquires a function type through a |
| 4595 | // type dependent on a template-parameter and this causes |
| 4596 | // a declaration that does not use the syntactic form of a |
| 4597 | // function declarator to have a function type, the program |
| 4598 | // is ill-formed. |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4599 | if (!T->isFunctionType()) { |
| 4600 | Diag(Loc, diag::err_unexpected_friend); |
| 4601 | |
| 4602 | // It might be worthwhile to try to recover by creating an |
| 4603 | // appropriate declaration. |
| 4604 | return DeclPtrTy(); |
| 4605 | } |
| 4606 | |
| 4607 | // C++ [namespace.memdef]p3 |
| 4608 | // - If a friend declaration in a non-local class first declares a |
| 4609 | // class or function, the friend class or function is a member |
| 4610 | // of the innermost enclosing namespace. |
| 4611 | // - The name of the friend is not found by simple name lookup |
| 4612 | // until a matching declaration is provided in that namespace |
| 4613 | // scope (either before or after the class declaration granting |
| 4614 | // friendship). |
| 4615 | // - If a friend function is called, its name may be found by the |
| 4616 | // name lookup that considers functions from namespaces and |
| 4617 | // classes associated with the types of the function arguments. |
| 4618 | // - When looking for a prior declaration of a class or a function |
| 4619 | // declared as a friend, scopes outside the innermost enclosing |
| 4620 | // namespace scope are not considered. |
| 4621 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4622 | CXXScopeSpec &ScopeQual = D.getCXXScopeSpec(); |
| 4623 | DeclarationName Name = GetNameForDeclarator(D); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4624 | assert(Name); |
| 4625 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4626 | // The context we found the declaration in, or in which we should |
| 4627 | // create the declaration. |
| 4628 | DeclContext *DC; |
| 4629 | |
| 4630 | // FIXME: handle local classes |
| 4631 | |
| 4632 | // Recover from invalid scope qualifiers as if they just weren't there. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4633 | NamedDecl *PrevDecl = 0; |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4634 | if (!ScopeQual.isInvalid() && ScopeQual.isSet()) { |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4635 | // FIXME: RequireCompleteDeclContext |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4636 | DC = computeDeclContext(ScopeQual); |
| 4637 | |
| 4638 | // FIXME: handle dependent contexts |
| 4639 | if (!DC) return DeclPtrTy(); |
| 4640 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4641 | LookupResult R(*this, Name, Loc, LookupOrdinaryName, |
| 4642 | LookupResult::ForRedeclaration); |
| 4643 | LookupQualifiedName(R, DC); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4644 | PrevDecl = R.getAsSingleDecl(Context); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4645 | |
| 4646 | // If searching in that context implicitly found a declaration in |
| 4647 | // a different context, treat it like it wasn't found at all. |
| 4648 | // TODO: better diagnostics for this case. Suggesting the right |
| 4649 | // qualified scope would be nice... |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4650 | if (!PrevDecl || !PrevDecl->getDeclContext()->Equals(DC)) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4651 | D.setInvalidType(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4652 | Diag(Loc, diag::err_qualified_friend_not_found) << Name << T; |
| 4653 | return DeclPtrTy(); |
| 4654 | } |
| 4655 | |
| 4656 | // C++ [class.friend]p1: A friend of a class is a function or |
| 4657 | // class that is not a member of the class . . . |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4658 | if (DC->Equals(CurContext)) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4659 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 4660 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4661 | // Otherwise walk out to the nearest namespace scope looking for matches. |
| 4662 | } else { |
| 4663 | // TODO: handle local class contexts. |
| 4664 | |
| 4665 | DC = CurContext; |
| 4666 | while (true) { |
| 4667 | // Skip class contexts. If someone can cite chapter and verse |
| 4668 | // for this behavior, that would be nice --- it's what GCC and |
| 4669 | // EDG do, and it seems like a reasonable intent, but the spec |
| 4670 | // really only says that checks for unqualified existing |
| 4671 | // declarations should stop at the nearest enclosing namespace, |
| 4672 | // not that they should only consider the nearest enclosing |
| 4673 | // namespace. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4674 | while (DC->isRecord()) |
| 4675 | DC = DC->getParent(); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4676 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 4677 | LookupResult R(*this, Name, Loc, LookupOrdinaryName, |
| 4678 | LookupResult::ForRedeclaration); |
| 4679 | LookupQualifiedName(R, DC); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4680 | PrevDecl = R.getAsSingleDecl(Context); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4681 | |
| 4682 | // TODO: decide what we think about using declarations. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4683 | if (PrevDecl) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4684 | break; |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4685 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4686 | if (DC->isFileContext()) break; |
| 4687 | DC = DC->getParent(); |
| 4688 | } |
| 4689 | |
| 4690 | // C++ [class.friend]p1: A friend of a class is a function or |
| 4691 | // class that is not a member of the class . . . |
John McCall | 7f27d92 | 2009-08-06 20:49:32 +0000 | [diff] [blame] | 4692 | // C++0x changes this for both friend types and functions. |
| 4693 | // Most C++ 98 compilers do seem to give an error here, so |
| 4694 | // we do, too. |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4695 | if (PrevDecl && DC->Equals(CurContext) && !getLangOptions().CPlusPlus0x) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4696 | Diag(DS.getFriendSpecLoc(), diag::err_friend_is_member); |
| 4697 | } |
| 4698 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4699 | if (DC->isFileContext()) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4700 | // This implies that it has to be an operator or function. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4701 | if (D.getName().getKind() == UnqualifiedId::IK_ConstructorName || |
| 4702 | D.getName().getKind() == UnqualifiedId::IK_DestructorName || |
| 4703 | D.getName().getKind() == UnqualifiedId::IK_ConversionFunctionId) { |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4704 | Diag(Loc, diag::err_introducing_special_friend) << |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4705 | (D.getName().getKind() == UnqualifiedId::IK_ConstructorName ? 0 : |
| 4706 | D.getName().getKind() == UnqualifiedId::IK_DestructorName ? 1 : 2); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4707 | return DeclPtrTy(); |
| 4708 | } |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4711 | bool Redeclaration = false; |
| 4712 | NamedDecl *ND = ActOnFunctionDeclarator(S, D, DC, T, DInfo, PrevDecl, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4713 | move(TemplateParams), |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 4714 | IsDefinition, |
| 4715 | Redeclaration); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4716 | if (!ND) return DeclPtrTy(); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 4717 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4718 | assert(ND->getDeclContext() == DC); |
| 4719 | assert(ND->getLexicalDeclContext() == CurContext); |
John McCall | 88232aa | 2009-08-18 00:00:49 +0000 | [diff] [blame] | 4720 | |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 4721 | // Add the function declaration to the appropriate lookup tables, |
| 4722 | // adjusting the redeclarations list as necessary. We don't |
| 4723 | // want to do this yet if the friending class is dependent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4724 | // |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 4725 | // Also update the scope-based lookup if the target context's |
| 4726 | // lookup context is in lexical scope. |
| 4727 | if (!CurContext->isDependentContext()) { |
| 4728 | DC = DC->getLookupContext(); |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4729 | DC->makeDeclVisibleInContext(ND, /* Recoverable=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 4730 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4731 | PushOnScopeChains(ND, EnclosingScope, /*AddToContext=*/ false); |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 4732 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4733 | |
| 4734 | FriendDecl *FrD = FriendDecl::Create(Context, CurContext, |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4735 | D.getIdentifierLoc(), ND, |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4736 | DS.getFriendSpecLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 4737 | FrD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4738 | CurContext->addDecl(FrD); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 4739 | |
Douglas Gregor | 182ddf0 | 2009-09-28 00:08:27 +0000 | [diff] [blame] | 4740 | return DeclPtrTy::make(ND); |
Anders Carlsson | 0033836 | 2009-05-11 22:55:49 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4743 | void Sema::SetDeclDeleted(DeclPtrTy dcl, SourceLocation DelLoc) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4744 | AdjustDeclIfTemplate(dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4745 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 4746 | Decl *Dcl = dcl.getAs<Decl>(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4747 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(Dcl); |
| 4748 | if (!Fn) { |
| 4749 | Diag(DelLoc, diag::err_deleted_non_function); |
| 4750 | return; |
| 4751 | } |
| 4752 | if (const FunctionDecl *Prev = Fn->getPreviousDeclaration()) { |
| 4753 | Diag(DelLoc, diag::err_deleted_decl_not_first); |
| 4754 | Diag(Prev->getLocation(), diag::note_previous_declaration); |
| 4755 | // If the declaration wasn't the first, we delete the function anyway for |
| 4756 | // recovery. |
| 4757 | } |
| 4758 | Fn->setDeleted(); |
| 4759 | } |
Sebastian Redl | 13e8854 | 2009-04-27 21:33:24 +0000 | [diff] [blame] | 4760 | |
| 4761 | static void SearchForReturnInStmt(Sema &Self, Stmt *S) { |
| 4762 | for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E; |
| 4763 | ++CI) { |
| 4764 | Stmt *SubStmt = *CI; |
| 4765 | if (!SubStmt) |
| 4766 | continue; |
| 4767 | if (isa<ReturnStmt>(SubStmt)) |
| 4768 | Self.Diag(SubStmt->getSourceRange().getBegin(), |
| 4769 | diag::err_return_in_constructor_handler); |
| 4770 | if (!isa<Expr>(SubStmt)) |
| 4771 | SearchForReturnInStmt(Self, SubStmt); |
| 4772 | } |
| 4773 | } |
| 4774 | |
| 4775 | void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) { |
| 4776 | for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { |
| 4777 | CXXCatchStmt *Handler = TryBlock->getHandler(I); |
| 4778 | SearchForReturnInStmt(*this, Handler); |
| 4779 | } |
| 4780 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4781 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4782 | bool Sema::CheckOverridingFunctionReturnType(const CXXMethodDecl *New, |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4783 | const CXXMethodDecl *Old) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4784 | QualType NewTy = New->getType()->getAs<FunctionType>()->getResultType(); |
| 4785 | QualType OldTy = Old->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4786 | |
| 4787 | QualType CNewTy = Context.getCanonicalType(NewTy); |
| 4788 | QualType COldTy = Context.getCanonicalType(OldTy); |
| 4789 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4790 | if (CNewTy == COldTy && |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4791 | CNewTy.getLocalCVRQualifiers() == COldTy.getLocalCVRQualifiers()) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4792 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4793 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4794 | // Check if the return types are covariant |
| 4795 | QualType NewClassTy, OldClassTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4796 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4797 | /// Both types must be pointers or references to classes. |
| 4798 | if (PointerType *NewPT = dyn_cast<PointerType>(NewTy)) { |
| 4799 | if (PointerType *OldPT = dyn_cast<PointerType>(OldTy)) { |
| 4800 | NewClassTy = NewPT->getPointeeType(); |
| 4801 | OldClassTy = OldPT->getPointeeType(); |
| 4802 | } |
| 4803 | } else if (ReferenceType *NewRT = dyn_cast<ReferenceType>(NewTy)) { |
| 4804 | if (ReferenceType *OldRT = dyn_cast<ReferenceType>(OldTy)) { |
| 4805 | NewClassTy = NewRT->getPointeeType(); |
| 4806 | OldClassTy = OldRT->getPointeeType(); |
| 4807 | } |
| 4808 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4809 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4810 | // The return types aren't either both pointers or references to a class type. |
| 4811 | if (NewClassTy.isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4812 | Diag(New->getLocation(), |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4813 | diag::err_different_return_type_for_overriding_virtual_function) |
| 4814 | << New->getDeclName() << NewTy << OldTy; |
| 4815 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4816 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4817 | return true; |
| 4818 | } |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4819 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4820 | if (!Context.hasSameUnqualifiedType(NewClassTy, OldClassTy)) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4821 | // Check if the new class derives from the old class. |
| 4822 | if (!IsDerivedFrom(NewClassTy, OldClassTy)) { |
| 4823 | Diag(New->getLocation(), |
| 4824 | diag::err_covariant_return_not_derived) |
| 4825 | << New->getDeclName() << NewTy << OldTy; |
| 4826 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4827 | return true; |
| 4828 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4829 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4830 | // Check if we the conversion from derived to base is valid. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4831 | if (CheckDerivedToBaseConversion(NewClassTy, OldClassTy, |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4832 | diag::err_covariant_return_inaccessible_base, |
| 4833 | diag::err_covariant_return_ambiguous_derived_to_base_conv, |
| 4834 | // FIXME: Should this point to the return type? |
| 4835 | New->getLocation(), SourceRange(), New->getDeclName())) { |
| 4836 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4837 | return true; |
| 4838 | } |
| 4839 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4840 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4841 | // The qualifiers of the return types must be the same. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4842 | if (CNewTy.getLocalCVRQualifiers() != COldTy.getLocalCVRQualifiers()) { |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4843 | Diag(New->getLocation(), |
| 4844 | diag::err_covariant_return_type_different_qualifications) |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4845 | << New->getDeclName() << NewTy << OldTy; |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4846 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4847 | return true; |
| 4848 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4849 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4850 | |
| 4851 | // The new class type must have the same or less qualifiers as the old type. |
| 4852 | if (NewClassTy.isMoreQualifiedThan(OldClassTy)) { |
| 4853 | Diag(New->getLocation(), |
| 4854 | diag::err_covariant_return_type_class_type_more_qualified) |
| 4855 | << New->getDeclName() << NewTy << OldTy; |
| 4856 | Diag(Old->getLocation(), diag::note_overridden_virtual_function); |
| 4857 | return true; |
| 4858 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4859 | |
Anders Carlsson | c3a68b2 | 2009-05-14 19:52:19 +0000 | [diff] [blame] | 4860 | return false; |
Anders Carlsson | d7ba27d | 2009-05-14 01:09:04 +0000 | [diff] [blame] | 4861 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4862 | |
| 4863 | /// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse an |
| 4864 | /// initializer for the declaration 'Dcl'. |
| 4865 | /// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a |
| 4866 | /// static data member of class X, names should be looked up in the scope of |
| 4867 | /// class X. |
| 4868 | void Sema::ActOnCXXEnterDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4869 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4870 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4871 | Decl *D = Dcl.getAs<Decl>(); |
| 4872 | // If there is no declaration, there was an error parsing it. |
| 4873 | if (D == 0) |
| 4874 | return; |
| 4875 | |
| 4876 | // Check whether it is a declaration with a nested name specifier like |
| 4877 | // int foo::bar; |
| 4878 | if (!D->isOutOfLine()) |
| 4879 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4880 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4881 | // C++ [basic.lookup.unqual]p13 |
| 4882 | // |
| 4883 | // A name used in the definition of a static data member of class X |
| 4884 | // (after the qualified-id of the static member) is looked up as if the name |
| 4885 | // was used in a member function of X. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4886 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4887 | // Change current context into the context of the initializing declaration. |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 4888 | EnterDeclaratorContext(S, D->getDeclContext()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
| 4891 | /// ActOnCXXExitDeclInitializer - Invoked after we are finished parsing an |
| 4892 | /// initializer for the declaration 'Dcl'. |
| 4893 | void Sema::ActOnCXXExitDeclInitializer(Scope *S, DeclPtrTy Dcl) { |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 4894 | AdjustDeclIfTemplate(Dcl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4895 | |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4896 | Decl *D = Dcl.getAs<Decl>(); |
| 4897 | // If there is no declaration, there was an error parsing it. |
| 4898 | if (D == 0) |
| 4899 | return; |
| 4900 | |
| 4901 | // Check whether it is a declaration with a nested name specifier like |
| 4902 | // int foo::bar; |
| 4903 | if (!D->isOutOfLine()) |
| 4904 | return; |
| 4905 | |
| 4906 | assert(S->getEntity() == D->getDeclContext() && "Context imbalance!"); |
Argyrios Kyrtzidis | 1d17553 | 2009-06-17 23:15:40 +0000 | [diff] [blame] | 4907 | ExitDeclaratorContext(S); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 4908 | } |