blob: e88a3b2079f21b9728effd236b692ceb0ccb0a34 [file] [log] [blame]
Sebastian Redl2b6b14c2008-11-05 21:50:06 +00001//===--- SemaNamedCast.cpp - Semantic Analysis for Named Casts ------------===//
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++ named casts.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "SemaInherit.h"
16#include "clang/AST/ExprCXX.h"
17#include "clang/AST/ASTContext.h"
Chris Lattner52a425b2009-01-27 18:30:58 +000018#include "clang/Basic/DiagnosticSema.h"
Sebastian Redl2b6b14c2008-11-05 21:50:06 +000019#include "llvm/ADT/SmallVector.h"
Sebastian Redl0528e1c2008-11-07 23:29:29 +000020#include <set>
Sebastian Redl2b6b14c2008-11-05 21:50:06 +000021using namespace clang;
22
Sebastian Redlf831eeb2008-11-08 13:00:26 +000023enum TryStaticCastResult {
24 TSC_NotApplicable, ///< The cast method is not applicable.
25 TSC_Success, ///< The cast method is appropriate and successful.
26 TSC_Failed ///< The cast method is appropriate, but failed. A
27 ///< diagnostic has been emitted.
28};
29
30static void CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
31 const SourceRange &OpRange,
32 const SourceRange &DestRange);
33static void CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
34 const SourceRange &OpRange,
35 const SourceRange &DestRange);
36static void CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
37 const SourceRange &OpRange);
38static void CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
39 const SourceRange &OpRange,
40 const SourceRange &DestRange);
41
42static bool CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType);
43static TryStaticCastResult TryStaticReferenceDowncast(
44 Sema &Self, Expr *SrcExpr, QualType DestType, const SourceRange &OpRange);
45static TryStaticCastResult TryStaticPointerDowncast(
46 Sema &Self, QualType SrcType, QualType DestType, const SourceRange &OpRange);
47static TryStaticCastResult TryStaticDowncast(Sema &Self, QualType SrcType,
48 QualType DestType,
49 const SourceRange &OpRange,
50 QualType OrigSrcType,
51 QualType OrigDestType);
52static TryStaticCastResult TryStaticImplicitCast(Sema &Self, Expr *SrcExpr,
53 QualType DestType,
54 const SourceRange &OpRange);
55
Sebastian Redl2b6b14c2008-11-05 21:50:06 +000056/// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's.
57Action::ExprResult
58Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
59 SourceLocation LAngleBracketLoc, TypeTy *Ty,
60 SourceLocation RAngleBracketLoc,
61 SourceLocation LParenLoc, ExprTy *E,
62 SourceLocation RParenLoc) {
63 Expr *Ex = (Expr*)E;
64 QualType DestType = QualType::getFromOpaquePtr(Ty);
65 SourceRange OpRange(OpLoc, RParenLoc);
66 SourceRange DestRange(LAngleBracketLoc, RAngleBracketLoc);
67
Douglas Gregore6be68a2008-12-17 22:52:20 +000068 // If the type is dependent, we won't do the semantic analysis now.
69 // FIXME: should we check this in a more fine-grained manner?
70 bool TypeDependent = DestType->isDependentType() || Ex->isTypeDependent();
71
Sebastian Redl2b6b14c2008-11-05 21:50:06 +000072 switch (Kind) {
73 default: assert(0 && "Unknown C++ cast!");
74
75 case tok::kw_const_cast:
Douglas Gregore6be68a2008-12-17 22:52:20 +000076 if (!TypeDependent)
77 CheckConstCast(*this, Ex, DestType, OpRange, DestRange);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +000078 return new CXXConstCastExpr(DestType.getNonReferenceType(), Ex,
79 DestType, OpLoc);
80
81 case tok::kw_dynamic_cast:
Douglas Gregore6be68a2008-12-17 22:52:20 +000082 if (!TypeDependent)
83 CheckDynamicCast(*this, Ex, DestType, OpRange, DestRange);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +000084 return new CXXDynamicCastExpr(DestType.getNonReferenceType(), Ex,
85 DestType, OpLoc);
86
87 case tok::kw_reinterpret_cast:
Douglas Gregore6be68a2008-12-17 22:52:20 +000088 if (!TypeDependent)
89 CheckReinterpretCast(*this, Ex, DestType, OpRange, DestRange);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +000090 return new CXXReinterpretCastExpr(DestType.getNonReferenceType(), Ex,
91 DestType, OpLoc);
92
93 case tok::kw_static_cast:
Douglas Gregore6be68a2008-12-17 22:52:20 +000094 if (!TypeDependent)
95 CheckStaticCast(*this, Ex, DestType, OpRange);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +000096 return new CXXStaticCastExpr(DestType.getNonReferenceType(), Ex,
97 DestType, OpLoc);
98 }
99
100 return true;
101}
102
103/// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid.
104/// Refer to C++ 5.2.11 for details. const_cast is typically used in code
105/// like this:
106/// const char *str = "literal";
107/// legacy_function(const_cast\<char*\>(str));
108void
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000109CheckConstCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
110 const SourceRange &OpRange, const SourceRange &DestRange)
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000111{
112 QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
113
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000114 DestType = Self.Context.getCanonicalType(DestType);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000115 QualType SrcType = SrcExpr->getType();
116 if (const ReferenceType *DestTypeTmp = DestType->getAsReferenceType()) {
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000117 if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000118 // Cannot cast non-lvalue to reference type.
Chris Lattner70b93d82008-11-18 22:52:51 +0000119 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000120 << "const_cast" << OrigDestType << SrcExpr->getSourceRange();
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000121 return;
122 }
123
124 // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2
125 // [...] if a pointer to T1 can be [cast] to the type pointer to T2.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000126 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
127 SrcType = Self.Context.getPointerType(SrcType);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000128 } else {
129 // C++ 5.2.11p1: Otherwise, the result is an rvalue and the
130 // lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard
131 // conversions are performed on the expression.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000132 Self.DefaultFunctionArrayConversion(SrcExpr);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000133 SrcType = SrcExpr->getType();
134 }
135
Sebastian Redl08cdb992009-01-26 22:19:12 +0000136 // C++ 5.2.11p5: For a const_cast involving pointers to data members [...]
137 // the rules for const_cast are the same as those used for pointers.
138
139 if (!DestType->isPointerType() && !DestType->isMemberPointerType()) {
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000140 // Cannot cast to non-pointer, non-reference type. Note that, if DestType
141 // was a reference type, we converted it to a pointer above.
142 // C++ 5.2.11p3: For two pointer types [...]
Chris Lattner70b93d82008-11-18 22:52:51 +0000143 Self.Diag(OpRange.getBegin(), diag::err_bad_const_cast_dest)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000144 << OrigDestType << DestRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000145 return;
146 }
Sebastian Redl08cdb992009-01-26 22:19:12 +0000147 if (DestType->isFunctionPointerType() ||
148 DestType->isMemberFunctionPointerType()) {
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000149 // Cannot cast direct function pointers.
150 // C++ 5.2.11p2: [...] where T is any object type or the void type [...]
151 // T is the ultimate pointee of source and target type.
Chris Lattner70b93d82008-11-18 22:52:51 +0000152 Self.Diag(OpRange.getBegin(), diag::err_bad_const_cast_dest)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000153 << OrigDestType << DestRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000154 return;
155 }
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000156 SrcType = Self.Context.getCanonicalType(SrcType);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000157
158 // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are
159 // completely equal.
160 // FIXME: const_cast should probably not be able to convert between pointers
161 // to different address spaces.
162 // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers
163 // in multi-level pointers may change, but the level count must be the same,
164 // as must be the final pointee type.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000165 while (SrcType != DestType &&
166 Self.UnwrapSimilarPointerTypes(SrcType, DestType)) {
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000167 SrcType = SrcType.getUnqualifiedType();
168 DestType = DestType.getUnqualifiedType();
169 }
170
171 // Doug Gregor said to disallow this until users complain.
172#if 0
173 // If we end up with constant arrays of equal size, unwrap those too. A cast
174 // from const int [N] to int (&)[N] is invalid by my reading of the
175 // standard, but g++ accepts it even with -ansi -pedantic.
176 // No more than one level, though, so don't embed this in the unwrap loop
177 // above.
178 const ConstantArrayType *SrcTypeArr, *DestTypeArr;
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000179 if ((SrcTypeArr = Self.Context.getAsConstantArrayType(SrcType)) &&
180 (DestTypeArr = Self.Context.getAsConstantArrayType(DestType)))
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000181 {
182 if (SrcTypeArr->getSize() != DestTypeArr->getSize()) {
183 // Different array sizes.
Chris Lattner70b93d82008-11-18 22:52:51 +0000184 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000185 << "const_cast" << OrigDestType << OrigSrcType << OpRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000186 return;
187 }
188 SrcType = SrcTypeArr->getElementType().getUnqualifiedType();
189 DestType = DestTypeArr->getElementType().getUnqualifiedType();
190 }
191#endif
192
193 // Since we're dealing in canonical types, the remainder must be the same.
194 if (SrcType != DestType) {
195 // Cast between unrelated types.
Chris Lattner70b93d82008-11-18 22:52:51 +0000196 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000197 << "const_cast" << OrigDestType << OrigSrcType << OpRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000198 return;
199 }
200}
201
202/// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is
203/// valid.
204/// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code
205/// like this:
206/// char *bytes = reinterpret_cast\<char*\>(int_ptr);
207void
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000208CheckReinterpretCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
209 const SourceRange &OpRange, const SourceRange &DestRange)
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000210{
211 QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
212
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000213 DestType = Self.Context.getCanonicalType(DestType);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000214 QualType SrcType = SrcExpr->getType();
215 if (const ReferenceType *DestTypeTmp = DestType->getAsReferenceType()) {
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000216 if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000217 // Cannot cast non-lvalue to reference type.
Chris Lattner70b93d82008-11-18 22:52:51 +0000218 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000219 << "reinterpret_cast" << OrigDestType << SrcExpr->getSourceRange();
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000220 return;
221 }
222
223 // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
224 // same effect as the conversion *reinterpret_cast<T*>(&x) with the
225 // built-in & and * operators.
226 // This code does this transformation for the checked types.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000227 DestType = Self.Context.getPointerType(DestTypeTmp->getPointeeType());
228 SrcType = Self.Context.getPointerType(SrcType);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000229 } else {
230 // C++ 5.2.10p1: [...] the lvalue-to-rvalue, array-to-pointer, and
231 // function-to-pointer standard conversions are performed on the
232 // expression v.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000233 Self.DefaultFunctionArrayConversion(SrcExpr);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000234 SrcType = SrcExpr->getType();
235 }
236
237 // Canonicalize source for comparison.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000238 SrcType = Self.Context.getCanonicalType(SrcType);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000239
Sebastian Redl04b3f352009-01-27 23:18:31 +0000240 const MemberPointerType *DestMemPtr = DestType->getAsMemberPointerType(),
241 *SrcMemPtr = SrcType->getAsMemberPointerType();
242 if (DestMemPtr && SrcMemPtr) {
243 // C++ 5.2.10p9: An rvalue of type "pointer to member of X of type T1"
244 // can be explicitly converted to an rvalue of type "pointer to member
245 // of Y of type T2" if T1 and T2 are both function types or both object
246 // types.
247 if (DestMemPtr->getPointeeType()->isFunctionType() !=
248 SrcMemPtr->getPointeeType()->isFunctionType()) {
249 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic)
250 << "reinterpret_cast" << OrigDestType << OrigSrcType << OpRange;
251 return;
252 }
253
254 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away
255 // constness.
256 if (CastsAwayConstness(Self, SrcType, DestType)) {
257 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away)
258 << "reinterpret_cast" << OrigDestType << OrigSrcType << OpRange;
259 return;
260 }
261
262 // A valid member pointer cast.
263 return;
264 }
265
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000266 bool destIsPtr = DestType->isPointerType();
267 bool srcIsPtr = SrcType->isPointerType();
268 if (!destIsPtr && !srcIsPtr) {
269 // Except for std::nullptr_t->integer, which is not supported yet, and
270 // lvalue->reference, which is handled above, at least one of the two
271 // arguments must be a pointer.
Chris Lattner70b93d82008-11-18 22:52:51 +0000272 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000273 << "reinterpret_cast" << OrigDestType << OrigSrcType << OpRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000274 return;
275 }
276
277 if (SrcType == DestType) {
278 // C++ 5.2.10p2 has a note that mentions that, subject to all other
279 // restrictions, a cast to the same type is allowed. The intent is not
280 // entirely clear here, since all other paragraphs explicitly forbid casts
281 // to the same type. However, the behavior of compilers is pretty consistent
Sebastian Redl04b3f352009-01-27 23:18:31 +0000282 // on this point: allow same-type conversion if the involved types are
283 // pointers, disallow otherwise.
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000284 return;
285 }
286
287 // Note: Clang treats enumeration types as integral types. If this is ever
288 // changed for C++, the additional check here will be redundant.
289 if (DestType->isIntegralType() && !DestType->isEnumeralType()) {
Sebastian Redldbed5902008-11-05 22:15:14 +0000290 assert(srcIsPtr && "One type must be a pointer");
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000291 // C++ 5.2.10p4: A pointer can be explicitly converted to any integral
292 // type large enough to hold it.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000293 if (Self.Context.getTypeSize(SrcType) >
294 Self.Context.getTypeSize(DestType)) {
Chris Lattner70b93d82008-11-18 22:52:51 +0000295 Self.Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_small_int)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000296 << OrigDestType << DestRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000297 }
298 return;
299 }
300
301 if (SrcType->isIntegralType() || SrcType->isEnumeralType()) {
Sebastian Redldbed5902008-11-05 22:15:14 +0000302 assert(destIsPtr && "One type must be a pointer");
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000303 // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
304 // converted to a pointer.
305 return;
306 }
307
308 if (!destIsPtr || !srcIsPtr) {
309 // With the valid non-pointer conversions out of the way, we can be even
310 // more stringent.
Chris Lattner70b93d82008-11-18 22:52:51 +0000311 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000312 << "reinterpret_cast" << OrigDestType << OrigSrcType << OpRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000313 return;
314 }
315
316 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000317 if (CastsAwayConstness(Self, SrcType, DestType)) {
Chris Lattner70b93d82008-11-18 22:52:51 +0000318 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000319 << "reinterpret_cast" << OrigDestType << OrigSrcType << OpRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000320 return;
321 }
322
323 // Not casting away constness, so the only remaining check is for compatible
324 // pointer categories.
325
326 if (SrcType->isFunctionPointerType()) {
327 if (DestType->isFunctionPointerType()) {
328 // C++ 5.2.10p6: A pointer to a function can be explicitly converted to
329 // a pointer to a function of a different type.
330 return;
331 }
332
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000333 // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to
334 // an object type or vice versa is conditionally-supported.
335 // Compilers support it in C++03 too, though, because it's necessary for
336 // casting the return value of dlsym() and GetProcAddress().
337 // FIXME: Conditionally-supported behavior should be configurable in the
338 // TargetInfo or similar.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000339 if (!Self.getLangOptions().CPlusPlus0x) {
Chris Lattner70b93d82008-11-18 22:52:51 +0000340 Self.Diag(OpRange.getBegin(), diag::ext_reinterpret_cast_fn_obj)
341 << OpRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000342 }
343 return;
344 }
345
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000346 if (DestType->isFunctionPointerType()) {
347 // See above.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000348 if (!Self.getLangOptions().CPlusPlus0x) {
Chris Lattner70b93d82008-11-18 22:52:51 +0000349 Self.Diag(OpRange.getBegin(), diag::ext_reinterpret_cast_fn_obj)
350 << OpRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000351 }
352 return;
353 }
354
355 // C++ 5.2.10p7: A pointer to an object can be explicitly converted to
356 // a pointer to an object of different type.
357 // Void pointers are not specified, but supported by every compiler out there.
358 // So we finish by allowing everything that remains - it's got to be two
359 // object pointers.
360}
361
Sebastian Redl04b3f352009-01-27 23:18:31 +0000362/// CastsAwayConstness - Check if the pointer conversion from SrcType to
363/// DestType casts away constness as defined in C++ 5.2.11p8ff. This is used by
364/// the cast checkers. Both arguments must denote pointer (possibly to member)
365/// types.
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000366bool
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000367CastsAwayConstness(Sema &Self, QualType SrcType, QualType DestType)
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000368{
Sebastian Redl04b3f352009-01-27 23:18:31 +0000369 // Casting away constness is defined in C++ 5.2.11p8 with reference to
370 // C++ 4.4. We piggyback on Sema::IsQualificationConversion for this, since
371 // the rules are non-trivial. So first we construct Tcv *...cv* as described
372 // in C++ 5.2.11p8.
373 assert((SrcType->isPointerType() || SrcType->isMemberPointerType()) &&
374 "Source type is not pointer or pointer to member.");
375 assert((DestType->isPointerType() || DestType->isMemberPointerType()) &&
376 "Destination type is not pointer or pointer to member.");
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000377
378 QualType UnwrappedSrcType = SrcType, UnwrappedDestType = DestType;
379 llvm::SmallVector<unsigned, 8> cv1, cv2;
380
381 // Find the qualifications.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000382 while (Self.UnwrapSimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) {
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000383 cv1.push_back(UnwrappedSrcType.getCVRQualifiers());
384 cv2.push_back(UnwrappedDestType.getCVRQualifiers());
385 }
386 assert(cv1.size() > 0 && "Must have at least one pointer level.");
387
388 // Construct void pointers with those qualifiers (in reverse order of
389 // unwrapping, of course).
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000390 QualType SrcConstruct = Self.Context.VoidTy;
391 QualType DestConstruct = Self.Context.VoidTy;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000392 for (llvm::SmallVector<unsigned, 8>::reverse_iterator i1 = cv1.rbegin(),
393 i2 = cv2.rbegin();
394 i1 != cv1.rend(); ++i1, ++i2)
395 {
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000396 SrcConstruct = Self.Context.getPointerType(
397 SrcConstruct.getQualifiedType(*i1));
398 DestConstruct = Self.Context.getPointerType(
399 DestConstruct.getQualifiedType(*i2));
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000400 }
401
402 // Test if they're compatible.
403 return SrcConstruct != DestConstruct &&
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000404 !Self.IsQualificationConversion(SrcConstruct, DestConstruct);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000405}
406
407/// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid.
408/// Refer to C++ 5.2.9 for details. Static casts are mostly used for making
409/// implicit conversions explicit and getting rid of data loss warnings.
410void
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000411CheckStaticCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
412 const SourceRange &OpRange)
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000413{
414 // The order the tests is not entirely arbitrary. There is one conversion
415 // that can be handled in two different ways. Given:
416 // struct A {};
417 // struct B : public A {
418 // B(); B(const A&);
419 // };
420 // const A &a = B();
421 // the cast static_cast<const B&>(a) could be seen as either a static
422 // reference downcast, or an explicit invocation of the user-defined
423 // conversion using B's conversion constructor.
424 // DR 427 specifies that the downcast is to be applied here.
425
426 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
427 if (DestType->isVoidType()) {
428 return;
429 }
430
431 // C++ 5.2.9p5, reference downcast.
432 // See the function for details.
433 // DR 427 specifies that this is to be applied before paragraph 2.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000434 if (TryStaticReferenceDowncast(Self, SrcExpr, DestType, OpRange)
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000435 > TSC_NotApplicable) {
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000436 return;
437 }
438
439 // C++ 5.2.9p2: An expression e can be explicitly converted to a type T
440 // [...] if the declaration "T t(e);" is well-formed, [...].
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000441 if (TryStaticImplicitCast(Self, SrcExpr, DestType, OpRange) >
442 TSC_NotApplicable) {
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000443 return;
444 }
445
446 // C++ 5.2.9p6: May apply the reverse of any standard conversion, except
447 // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean
448 // conversions, subject to further restrictions.
449 // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal
450 // of qualification conversions impossible.
451
452 // The lvalue-to-rvalue, array-to-pointer and function-to-pointer conversions
453 // are applied to the expression.
454 QualType OrigSrcType = SrcExpr->getType();
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000455 Self.DefaultFunctionArrayConversion(SrcExpr);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000456
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000457 QualType SrcType = Self.Context.getCanonicalType(SrcExpr->getType());
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000458
459 // Reverse integral promotion/conversion. All such conversions are themselves
460 // again integral promotions or conversions and are thus already handled by
461 // p2 (TryDirectInitialization above).
462 // (Note: any data loss warnings should be suppressed.)
463 // The exception is the reverse of enum->integer, i.e. integer->enum (and
464 // enum->enum). See also C++ 5.2.9p7.
465 // The same goes for reverse floating point promotion/conversion and
466 // floating-integral conversions. Again, only floating->enum is relevant.
467 if (DestType->isEnumeralType()) {
468 if (SrcType->isComplexType() || SrcType->isVectorType()) {
469 // Fall through - these cannot be converted.
470 } else if (SrcType->isArithmeticType() || SrcType->isEnumeralType()) {
471 return;
472 }
473 }
474
475 // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast.
476 // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000477 if (TryStaticPointerDowncast(Self, SrcType, DestType, OpRange)
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000478 > TSC_NotApplicable) {
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000479 return;
480 }
481
482 // Reverse member pointer conversion. C++ 5.11 specifies member pointer
483 // conversion. C++ 5.2.9p9 has additional information.
484 // DR54's access restrictions apply here also.
485 // FIXME: Don't have member pointers yet.
486
487 // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to
488 // void*. C++ 5.2.9p10 specifies additional restrictions, which really is
489 // just the usual constness stuff.
490 if (const PointerType *SrcPointer = SrcType->getAsPointerType()) {
491 QualType SrcPointee = SrcPointer->getPointeeType();
492 if (SrcPointee->isVoidType()) {
493 if (const PointerType *DestPointer = DestType->getAsPointerType()) {
494 QualType DestPointee = DestPointer->getPointeeType();
495 if (DestPointee->isObjectType()) {
496 // This is definitely the intended conversion, but it might fail due
497 // to a const violation.
498 if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
Chris Lattner70b93d82008-11-18 22:52:51 +0000499 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000500 << "static_cast" << DestType << OrigSrcType << OpRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000501 }
502 return;
503 }
504 }
505 }
506 }
507
508 // We tried everything. Everything! Nothing works! :-(
509 // FIXME: Error reporting could be a lot better. Should store the reason
510 // why every substep failed and, at the end, select the most specific and
511 // report that.
Chris Lattner70b93d82008-11-18 22:52:51 +0000512 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000513 << "static_cast" << DestType << OrigSrcType
Chris Lattner70b93d82008-11-18 22:52:51 +0000514 << OpRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000515}
516
517/// Tests whether a conversion according to C++ 5.2.9p5 is valid.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000518TryStaticCastResult
519TryStaticReferenceDowncast(Sema &Self, Expr *SrcExpr, QualType DestType,
520 const SourceRange &OpRange)
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000521{
522 // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be
523 // cast to type "reference to cv2 D", where D is a class derived from B,
524 // if a valid standard conversion from "pointer to D" to "pointer to B"
525 // exists, cv2 >= cv1, and B is not a virtual base class of D.
526 // In addition, DR54 clarifies that the base must be accessible in the
527 // current context. Although the wording of DR54 only applies to the pointer
528 // variant of this rule, the intent is clearly for it to apply to the this
529 // conversion as well.
530
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000531 if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000532 return TSC_NotApplicable;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000533 }
534
535 const ReferenceType *DestReference = DestType->getAsReferenceType();
536 if (!DestReference) {
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000537 return TSC_NotApplicable;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000538 }
539 QualType DestPointee = DestReference->getPointeeType();
540
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000541 return TryStaticDowncast(Self, SrcExpr->getType(), DestPointee, OpRange,
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000542 SrcExpr->getType(), DestType);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000543}
544
545/// Tests whether a conversion according to C++ 5.2.9p8 is valid.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000546TryStaticCastResult
547TryStaticPointerDowncast(Sema &Self, QualType SrcType, QualType DestType,
548 const SourceRange &OpRange)
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000549{
550 // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class
551 // type, can be converted to an rvalue of type "pointer to cv2 D", where D
552 // is a class derived from B, if a valid standard conversion from "pointer
553 // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base
554 // class of D.
555 // In addition, DR54 clarifies that the base must be accessible in the
556 // current context.
557
558 const PointerType *SrcPointer = SrcType->getAsPointerType();
559 if (!SrcPointer) {
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000560 return TSC_NotApplicable;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000561 }
562
563 const PointerType *DestPointer = DestType->getAsPointerType();
564 if (!DestPointer) {
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000565 return TSC_NotApplicable;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000566 }
567
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000568 return TryStaticDowncast(Self, SrcPointer->getPointeeType(),
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000569 DestPointer->getPointeeType(),
570 OpRange, SrcType, DestType);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000571}
572
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000573/// TryStaticDowncast - Common functionality of TryStaticReferenceDowncast and
574/// TryStaticPointerDowncast. Tests whether a static downcast from SrcType to
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000575/// DestType, both of which must be canonical, is possible and allowed.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000576TryStaticCastResult
577TryStaticDowncast(Sema &Self, QualType SrcType, QualType DestType,
578 const SourceRange &OpRange, QualType OrigSrcType,
579 QualType OrigDestType)
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000580{
581 // Downcast can only happen in class hierarchies, so we need classes.
582 if (!DestType->isRecordType() || !SrcType->isRecordType()) {
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000583 return TSC_NotApplicable;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000584 }
585
586 BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false,
587 /*DetectVirtual=*/true);
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000588 if (!Self.IsDerivedFrom(DestType, SrcType, Paths)) {
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000589 return TSC_NotApplicable;
590 }
591
592 // Target type does derive from source type. Now we're serious. If an error
593 // appears now, it's not ignored.
594 // This may not be entirely in line with the standard. Take for example:
595 // struct A {};
596 // struct B : virtual A {
597 // B(A&);
598 // };
599 //
600 // void f()
601 // {
602 // (void)static_cast<const B&>(*((A*)0));
603 // }
604 // As far as the standard is concerned, p5 does not apply (A is virtual), so
605 // p2 should be used instead - "const B& t(*((A*)0));" is perfectly valid.
606 // However, both GCC and Comeau reject this example, and accepting it would
607 // mean more complex code if we're to preserve the nice error message.
608 // FIXME: Being 100% compliant here would be nice to have.
609
610 // Must preserve cv, as always.
611 if (!DestType.isAtLeastAsQualifiedAs(SrcType)) {
Chris Lattner70b93d82008-11-18 22:52:51 +0000612 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000613 << "static_cast" << OrigDestType << OrigSrcType << OpRange;
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000614 return TSC_Failed;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000615 }
616
617 if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000618 // This code is analoguous to that in CheckDerivedToBaseConversion, except
619 // that it builds the paths in reverse order.
620 // To sum up: record all paths to the base and build a nice string from
621 // them. Use it to spice up the error message.
622 Paths.clear();
623 Paths.setRecordingPaths(true);
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000624 Self.IsDerivedFrom(DestType, SrcType, Paths);
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000625 std::string PathDisplayStr;
626 std::set<unsigned> DisplayedPaths;
627 for (BasePaths::paths_iterator Path = Paths.begin();
628 Path != Paths.end(); ++Path) {
629 if (DisplayedPaths.insert(Path->back().SubobjectNumber).second) {
630 // We haven't displayed a path to this particular base
631 // class subobject yet.
632 PathDisplayStr += "\n ";
633 for (BasePath::const_reverse_iterator Element = Path->rbegin();
634 Element != Path->rend(); ++Element)
635 PathDisplayStr += Element->Base->getType().getAsString() + " -> ";
636 PathDisplayStr += DestType.getAsString();
637 }
638 }
639
Chris Lattner70b93d82008-11-18 22:52:51 +0000640 Self.Diag(OpRange.getBegin(), diag::err_ambiguous_base_to_derived_cast)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000641 << SrcType.getUnqualifiedType() << DestType.getUnqualifiedType()
Chris Lattner70b93d82008-11-18 22:52:51 +0000642 << PathDisplayStr << OpRange;
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000643 return TSC_Failed;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000644 }
645
646 if (Paths.getDetectedVirtual() != 0) {
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000647 QualType VirtualBase(Paths.getDetectedVirtual(), 0);
Chris Lattner70b93d82008-11-18 22:52:51 +0000648 Self.Diag(OpRange.getBegin(), diag::err_static_downcast_via_virtual)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000649 << OrigSrcType << OrigDestType << VirtualBase << OpRange;
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000650 return TSC_Failed;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000651 }
652
653 // FIXME: Test accessibility.
654
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000655 return TSC_Success;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000656}
657
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000658/// TryStaticImplicitCast - Tests whether a conversion according to C++ 5.2.9p2
659/// is valid:
660///
661/// An expression e can be explicitly converted to a type T using a
662/// @c static_cast if the declaration "T t(e);" is well-formed [...].
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000663TryStaticCastResult
664TryStaticImplicitCast(Sema &Self, Expr *SrcExpr, QualType DestType,
665 const SourceRange &OpRange)
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000666{
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000667 if (DestType->isReferenceType()) {
668 // At this point of CheckStaticCast, if the destination is a reference,
669 // this has to work. There is no other way that works.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000670 return Self.CheckReferenceInit(SrcExpr, DestType) ?
671 TSC_Failed : TSC_Success;
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000672 }
673 if (DestType->isRecordType()) {
674 // FIXME: Use an implementation of C++ [over.match.ctor] for this.
675 return TSC_NotApplicable;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000676 }
677
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000678 // FIXME: To get a proper error from invalid conversions here, we need to
679 // reimplement more of this.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000680 ImplicitConversionSequence ICS = Self.TryImplicitConversion(
681 SrcExpr, DestType);
Sebastian Redl0528e1c2008-11-07 23:29:29 +0000682 return ICS.ConversionKind == ImplicitConversionSequence::BadConversion ?
683 TSC_NotApplicable : TSC_Success;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000684}
685
686/// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid.
687/// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime-
688/// checked downcasts in class hierarchies.
689void
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000690CheckDynamicCast(Sema &Self, Expr *&SrcExpr, QualType DestType,
691 const SourceRange &OpRange,
692 const SourceRange &DestRange)
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000693{
694 QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000695 DestType = Self.Context.getCanonicalType(DestType);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000696
697 // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type,
698 // or "pointer to cv void".
699
700 QualType DestPointee;
701 const PointerType *DestPointer = DestType->getAsPointerType();
702 const ReferenceType *DestReference = DestType->getAsReferenceType();
703 if (DestPointer) {
704 DestPointee = DestPointer->getPointeeType();
705 } else if (DestReference) {
706 DestPointee = DestReference->getPointeeType();
707 } else {
Chris Lattner70b93d82008-11-18 22:52:51 +0000708 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000709 << OrigDestType << DestRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000710 return;
711 }
712
713 const RecordType *DestRecord = DestPointee->getAsRecordType();
714 if (DestPointee->isVoidType()) {
715 assert(DestPointer && "Reference to void is not possible");
716 } else if (DestRecord) {
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000717 if (Self.DiagnoseIncompleteType(OpRange.getBegin(), DestPointee,
718 diag::err_bad_dynamic_cast_incomplete,
719 DestRange))
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000720 return;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000721 } else {
Chris Lattner70b93d82008-11-18 22:52:51 +0000722 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000723 << DestPointee.getUnqualifiedType() << DestRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000724 return;
725 }
726
727 // C++ 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to
728 // complete class type, [...]. If T is a reference type, v shall be an
729 // lvalue of a complete class type, [...].
730
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000731 QualType SrcType = Self.Context.getCanonicalType(OrigSrcType);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000732 QualType SrcPointee;
733 if (DestPointer) {
734 if (const PointerType *SrcPointer = SrcType->getAsPointerType()) {
735 SrcPointee = SrcPointer->getPointeeType();
736 } else {
Chris Lattner70b93d82008-11-18 22:52:51 +0000737 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000738 << OrigSrcType << SrcExpr->getSourceRange();
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000739 return;
740 }
741 } else {
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000742 if (SrcExpr->isLvalue(Self.Context) != Expr::LV_Valid) {
Chris Lattner70b93d82008-11-18 22:52:51 +0000743 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000744 << "dynamic_cast" << OrigDestType << OpRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000745 }
746 SrcPointee = SrcType;
747 }
748
749 const RecordType *SrcRecord = SrcPointee->getAsRecordType();
750 if (SrcRecord) {
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000751 if (Self.DiagnoseIncompleteType(OpRange.getBegin(), SrcPointee,
752 diag::err_bad_dynamic_cast_incomplete,
753 SrcExpr->getSourceRange()))
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000754 return;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000755 } else {
Chris Lattner77d52da2008-11-20 06:06:08 +0000756 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000757 << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000758 return;
759 }
760
761 assert((DestPointer || DestReference) &&
762 "Bad destination non-ptr/ref slipped through.");
763 assert((DestRecord || DestPointee->isVoidType()) &&
764 "Bad destination pointee slipped through.");
765 assert(SrcRecord && "Bad source pointee slipped through.");
766
767 // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness.
768 if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
Chris Lattner70b93d82008-11-18 22:52:51 +0000769 Self.Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000770 << "dynamic_cast" << OrigDestType << OrigSrcType << OpRange;
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000771 return;
772 }
773
774 // C++ 5.2.7p3: If the type of v is the same as the required result type,
775 // [except for cv].
776 if (DestRecord == SrcRecord) {
777 return;
778 }
779
780 // C++ 5.2.7p5
781 // Upcasts are resolved statically.
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000782 if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) {
783 Self.CheckDerivedToBaseConversion(SrcPointee, DestPointee,
Chris Lattner4bfd2232008-11-24 06:25:27 +0000784 OpRange.getBegin(), OpRange);
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000785 // Diagnostic already emitted on error.
786 return;
787 }
788
789 // C++ 5.2.7p6: Otherwise, v shall be [polymorphic].
Sebastian Redlf831eeb2008-11-08 13:00:26 +0000790 const RecordDecl *SrcDecl = SrcRecord->getDecl()->getDefinition(Self.Context);
Sebastian Redla1cf66a2008-11-06 15:59:35 +0000791 assert(SrcDecl && "Definition missing");
792 if (!cast<CXXRecordDecl>(SrcDecl)->isPolymorphic()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000793 Self.Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_polymorphic)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000794 << SrcPointee.getUnqualifiedType() << SrcExpr->getSourceRange();
Sebastian Redla1cf66a2008-11-06 15:59:35 +0000795 }
Sebastian Redl2b6b14c2008-11-05 21:50:06 +0000796
797 // Done. Everything else is run-time checks.
798}