blob: 22d712782ca87646640a68a5d4753efeb6f03563 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for C++ expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Sebastian Redl9ac68aa2008-10-31 14:43:28 +000015#include "SemaInherit.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ExprCXX.h"
Steve Naroffac5d4f12007-08-25 14:02:58 +000017#include "clang/AST/ASTContext.h"
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +000018#include "clang/Parse/DeclSpec.h"
Argiris Kirtzidiscf4e8f82008-10-06 23:16:35 +000019#include "clang/Lex/Preprocessor.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000020#include "clang/Basic/Diagnostic.h"
Douglas Gregorf8e92702008-10-24 15:36:09 +000021#include "llvm/ADT/SmallVector.h"
22#include "llvm/Support/Debug.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
Douglas Gregor21a04f32008-10-27 19:41:14 +000025/// ActOnCXXNamedCast - Parse {dynamic,static,reinterpret,const}_cast's.
Chris Lattner4b009652007-07-25 00:24:17 +000026Action::ExprResult
Douglas Gregor21a04f32008-10-27 19:41:14 +000027Sema::ActOnCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
28 SourceLocation LAngleBracketLoc, TypeTy *Ty,
29 SourceLocation RAngleBracketLoc,
30 SourceLocation LParenLoc, ExprTy *E,
31 SourceLocation RParenLoc) {
Douglas Gregorf8e92702008-10-24 15:36:09 +000032 Expr *Ex = (Expr*)E;
33 QualType DestType = QualType::getFromOpaquePtr(Ty);
Sebastian Redl87789742008-11-02 22:21:33 +000034 SourceRange OpRange(OpLoc, RParenLoc);
35 SourceRange DestRange(LAngleBracketLoc, RAngleBracketLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000036
37 switch (Kind) {
38 default: assert(0 && "Unknown C++ cast!");
Douglas Gregor21a04f32008-10-27 19:41:14 +000039
Douglas Gregorf8e92702008-10-24 15:36:09 +000040 case tok::kw_const_cast:
Sebastian Redl87789742008-11-02 22:21:33 +000041 CheckConstCast(Ex, DestType, OpRange, DestRange);
Douglas Gregor21a04f32008-10-27 19:41:14 +000042 return new CXXConstCastExpr(DestType.getNonReferenceType(), Ex,
43 DestType, OpLoc);
44
Douglas Gregorf8e92702008-10-24 15:36:09 +000045 case tok::kw_dynamic_cast:
Sebastian Redl87789742008-11-02 22:21:33 +000046 CheckDynamicCast(Ex, DestType, OpRange, DestRange);
Douglas Gregor21a04f32008-10-27 19:41:14 +000047 return new CXXDynamicCastExpr(DestType.getNonReferenceType(), Ex,
48 DestType, OpLoc);
49
Douglas Gregorf8e92702008-10-24 15:36:09 +000050 case tok::kw_reinterpret_cast:
Sebastian Redl87789742008-11-02 22:21:33 +000051 CheckReinterpretCast(Ex, DestType, OpRange, DestRange);
Douglas Gregor21a04f32008-10-27 19:41:14 +000052 return new CXXReinterpretCastExpr(DestType.getNonReferenceType(), Ex,
53 DestType, OpLoc);
54
Douglas Gregorf8e92702008-10-24 15:36:09 +000055 case tok::kw_static_cast:
Sebastian Redl87789742008-11-02 22:21:33 +000056 CheckStaticCast(Ex, DestType, OpRange);
Douglas Gregor21a04f32008-10-27 19:41:14 +000057 return new CXXStaticCastExpr(DestType.getNonReferenceType(), Ex,
58 DestType, OpLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000059 }
Sebastian Redl9ac68aa2008-10-31 14:43:28 +000060
Douglas Gregor21a04f32008-10-27 19:41:14 +000061 return true;
Douglas Gregorf8e92702008-10-24 15:36:09 +000062}
63
64/// CheckConstCast - Check that a const_cast\<DestType\>(SrcExpr) is valid.
65/// Refer to C++ 5.2.11 for details. const_cast is typically used in code
66/// like this:
67/// const char *str = "literal";
68/// legacy_function(const_cast\<char*\>(str));
69void
Sebastian Redl87789742008-11-02 22:21:33 +000070Sema::CheckConstCast(Expr *&SrcExpr, QualType DestType,
71 const SourceRange &OpRange, const SourceRange &DestRange)
Douglas Gregorf8e92702008-10-24 15:36:09 +000072{
73 QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
74
75 DestType = Context.getCanonicalType(DestType);
76 QualType SrcType = SrcExpr->getType();
77 if (const ReferenceType *DestTypeTmp = DestType->getAsReferenceType()) {
78 if (SrcExpr->isLvalue(Context) != Expr::LV_Valid) {
79 // Cannot cast non-lvalue to reference type.
Sebastian Redl87789742008-11-02 22:21:33 +000080 Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue,
81 "const_cast", OrigDestType.getAsString(), SrcExpr->getSourceRange());
Douglas Gregorf8e92702008-10-24 15:36:09 +000082 return;
83 }
84
85 // C++ 5.2.11p4: An lvalue of type T1 can be [cast] to an lvalue of type T2
86 // [...] if a pointer to T1 can be [cast] to the type pointer to T2.
87 DestType = Context.getPointerType(DestTypeTmp->getPointeeType());
Douglas Gregorf8e92702008-10-24 15:36:09 +000088 SrcType = Context.getPointerType(SrcType);
89 } else {
90 // C++ 5.2.11p1: Otherwise, the result is an rvalue and the
91 // lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard
92 // conversions are performed on the expression.
93 DefaultFunctionArrayConversion(SrcExpr);
94 SrcType = SrcExpr->getType();
95 }
96
97 if (!DestType->isPointerType()) {
98 // Cannot cast to non-pointer, non-reference type. Note that, if DestType
99 // was a reference type, we converted it to a pointer above.
100 // C++ 5.2.11p3: For two pointer types [...]
Sebastian Redl12aee862008-11-04 15:59:10 +0000101 Diag(OpRange.getBegin(), diag::err_bad_const_cast_dest,
102 OrigDestType.getAsString(), DestRange);
Douglas Gregorf8e92702008-10-24 15:36:09 +0000103 return;
104 }
105 if (DestType->isFunctionPointerType()) {
106 // Cannot cast direct function pointers.
107 // C++ 5.2.11p2: [...] where T is any object type or the void type [...]
108 // T is the ultimate pointee of source and target type.
Sebastian Redl12aee862008-11-04 15:59:10 +0000109 Diag(OpRange.getBegin(), diag::err_bad_const_cast_dest,
110 OrigDestType.getAsString(), DestRange);
Douglas Gregorf8e92702008-10-24 15:36:09 +0000111 return;
112 }
113 SrcType = Context.getCanonicalType(SrcType);
114
115 // Unwrap the pointers. Ignore qualifiers. Terminate early if the types are
116 // completely equal.
117 // FIXME: const_cast should probably not be able to convert between pointers
118 // to different address spaces.
119 // C++ 5.2.11p3 describes the core semantics of const_cast. All cv specifiers
120 // in multi-level pointers may change, but the level count must be the same,
121 // as must be the final pointee type.
122 while (SrcType != DestType && UnwrapSimilarPointerTypes(SrcType, DestType)) {
123 SrcType = SrcType.getUnqualifiedType();
124 DestType = DestType.getUnqualifiedType();
125 }
126
127 // Doug Gregor said to disallow this until users complain.
128#if 0
129 // If we end up with constant arrays of equal size, unwrap those too. A cast
130 // from const int [N] to int (&)[N] is invalid by my reading of the
131 // standard, but g++ accepts it even with -ansi -pedantic.
132 // No more than one level, though, so don't embed this in the unwrap loop
133 // above.
134 const ConstantArrayType *SrcTypeArr, *DestTypeArr;
135 if ((SrcTypeArr = Context.getAsConstantArrayType(SrcType)) &&
136 (DestTypeArr = Context.getAsConstantArrayType(DestType)))
137 {
138 if (SrcTypeArr->getSize() != DestTypeArr->getSize()) {
139 // Different array sizes.
Sebastian Redl87789742008-11-02 22:21:33 +0000140 Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic, "const_cast",
141 OrigDestType.getAsString(), OrigSrcType.getAsString(), OpRange);
Douglas Gregorf8e92702008-10-24 15:36:09 +0000142 return;
143 }
144 SrcType = SrcTypeArr->getElementType().getUnqualifiedType();
145 DestType = DestTypeArr->getElementType().getUnqualifiedType();
146 }
147#endif
148
149 // Since we're dealing in canonical types, the remainder must be the same.
150 if (SrcType != DestType) {
151 // Cast between unrelated types.
Sebastian Redl87789742008-11-02 22:21:33 +0000152 Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic, "const_cast",
153 OrigDestType.getAsString(), OrigSrcType.getAsString(), OpRange);
Douglas Gregorf8e92702008-10-24 15:36:09 +0000154 return;
155 }
156}
157
158/// CheckReinterpretCast - Check that a reinterpret_cast\<DestType\>(SrcExpr) is
159/// valid.
160/// Refer to C++ 5.2.10 for details. reinterpret_cast is typically used in code
161/// like this:
162/// char *bytes = reinterpret_cast\<char*\>(int_ptr);
163void
Sebastian Redl87789742008-11-02 22:21:33 +0000164Sema::CheckReinterpretCast(Expr *&SrcExpr, QualType DestType,
165 const SourceRange &OpRange,
166 const SourceRange &DestRange)
Douglas Gregorf8e92702008-10-24 15:36:09 +0000167{
168 QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
169
170 DestType = Context.getCanonicalType(DestType);
171 QualType SrcType = SrcExpr->getType();
172 if (const ReferenceType *DestTypeTmp = DestType->getAsReferenceType()) {
173 if (SrcExpr->isLvalue(Context) != Expr::LV_Valid) {
174 // Cannot cast non-lvalue to reference type.
Sebastian Redl87789742008-11-02 22:21:33 +0000175 Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue,
176 "reinterpret_cast", OrigDestType.getAsString(),
177 SrcExpr->getSourceRange());
Douglas Gregorf8e92702008-10-24 15:36:09 +0000178 return;
179 }
180
181 // C++ 5.2.10p10: [...] a reference cast reinterpret_cast<T&>(x) has the
182 // same effect as the conversion *reinterpret_cast<T*>(&x) with the
183 // built-in & and * operators.
184 // This code does this transformation for the checked types.
185 DestType = Context.getPointerType(DestTypeTmp->getPointeeType());
Douglas Gregorf8e92702008-10-24 15:36:09 +0000186 SrcType = Context.getPointerType(SrcType);
187 } else {
188 // C++ 5.2.10p1: [...] the lvalue-to-rvalue, array-to-pointer, and
189 // function-to-pointer standard conversions are performed on the
190 // expression v.
191 DefaultFunctionArrayConversion(SrcExpr);
192 SrcType = SrcExpr->getType();
193 }
194
195 // Canonicalize source for comparison.
196 SrcType = Context.getCanonicalType(SrcType);
197
198 bool destIsPtr = DestType->isPointerType();
199 bool srcIsPtr = SrcType->isPointerType();
200 if (!destIsPtr && !srcIsPtr) {
201 // Except for std::nullptr_t->integer, which is not supported yet, and
202 // lvalue->reference, which is handled above, at least one of the two
203 // arguments must be a pointer.
Sebastian Redl87789742008-11-02 22:21:33 +0000204 Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic, "reinterpret_cast",
205 OrigDestType.getAsString(), OrigSrcType.getAsString(), OpRange);
Douglas Gregorf8e92702008-10-24 15:36:09 +0000206 return;
207 }
208
209 if (SrcType == DestType) {
210 // C++ 5.2.10p2 has a note that mentions that, subject to all other
211 // restrictions, a cast to the same type is allowed. The intent is not
212 // entirely clear here, since all other paragraphs explicitly forbid casts
213 // to the same type. However, the behavior of compilers is pretty consistent
214 // on this point: allow same-type conversion if the involved are pointers,
215 // disallow otherwise.
216 return;
217 }
218
219 // Note: Clang treats enumeration types as integral types. If this is ever
220 // changed for C++, the additional check here will be redundant.
221 if (DestType->isIntegralType() && !DestType->isEnumeralType()) {
222 assert(srcIsPtr);
223 // C++ 5.2.10p4: A pointer can be explicitly converted to any integral
224 // type large enough to hold it.
225 if (Context.getTypeSize(SrcType) > Context.getTypeSize(DestType)) {
Sebastian Redl87789742008-11-02 22:21:33 +0000226 Diag(OpRange.getBegin(), diag::err_bad_reinterpret_cast_small_int,
227 OrigDestType.getAsString(), DestRange);
Douglas Gregorf8e92702008-10-24 15:36:09 +0000228 }
229 return;
230 }
231
232 if (SrcType->isIntegralType() || SrcType->isEnumeralType()) {
233 assert(destIsPtr);
234 // C++ 5.2.10p5: A value of integral or enumeration type can be explicitly
235 // converted to a pointer.
236 return;
237 }
238
239 if (!destIsPtr || !srcIsPtr) {
240 // With the valid non-pointer conversions out of the way, we can be even
241 // more stringent.
Sebastian Redl87789742008-11-02 22:21:33 +0000242 Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic, "reinterpret_cast",
243 OrigDestType.getAsString(), OrigSrcType.getAsString(), OpRange);
Douglas Gregorf8e92702008-10-24 15:36:09 +0000244 return;
245 }
246
247 // C++ 5.2.10p2: The reinterpret_cast operator shall not cast away constness.
248 if (CastsAwayConstness(SrcType, DestType)) {
Sebastian Redl87789742008-11-02 22:21:33 +0000249 Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away,
250 "reinterpret_cast", OrigDestType.getAsString(), OrigSrcType.getAsString(),
251 OpRange);
Douglas Gregorf8e92702008-10-24 15:36:09 +0000252 return;
253 }
254
255 // Not casting away constness, so the only remaining check is for compatible
256 // pointer categories.
257
258 if (SrcType->isFunctionPointerType()) {
259 if (DestType->isFunctionPointerType()) {
260 // C++ 5.2.10p6: A pointer to a function can be explicitly converted to
261 // a pointer to a function of a different type.
262 return;
263 }
264
265 // FIXME: Handle member pointers.
266
267 // C++0x 5.2.10p8: Converting a pointer to a function into a pointer to
268 // an object type or vice versa is conditionally-supported.
269 // Compilers support it in C++03 too, though, because it's necessary for
270 // casting the return value of dlsym() and GetProcAddress().
271 // FIXME: Conditionally-supported behavior should be configurable in the
272 // TargetInfo or similar.
273 if (!getLangOptions().CPlusPlus0x) {
Sebastian Redl87789742008-11-02 22:21:33 +0000274 Diag(OpRange.getBegin(), diag::ext_reinterpret_cast_fn_obj, OpRange);
Douglas Gregorf8e92702008-10-24 15:36:09 +0000275 }
276 return;
277 }
278
279 // FIXME: Handle member pointers.
280
281 if (DestType->isFunctionPointerType()) {
282 // See above.
283 if (!getLangOptions().CPlusPlus0x) {
Sebastian Redl87789742008-11-02 22:21:33 +0000284 Diag(OpRange.getBegin(), diag::ext_reinterpret_cast_fn_obj, OpRange);
Douglas Gregorf8e92702008-10-24 15:36:09 +0000285 }
286 return;
287 }
288
289 // C++ 5.2.10p7: A pointer to an object can be explicitly converted to
290 // a pointer to an object of different type.
291 // Void pointers are not specified, but supported by every compiler out there.
292 // So we finish by allowing everything that remains - it's got to be two
293 // object pointers.
294}
295
Douglas Gregor651d1cc2008-10-24 16:17:19 +0000296/// CastsAwayConstness - Check if the pointer conversion from SrcType
297/// to DestType casts away constness as defined in C++
298/// 5.2.11p8ff. This is used by the cast checkers. Both arguments
299/// must denote pointer types.
Douglas Gregorf8e92702008-10-24 15:36:09 +0000300bool
301Sema::CastsAwayConstness(QualType SrcType, QualType DestType)
302{
303 // Casting away constness is defined in C++ 5.2.11p8 with reference to
304 // C++ 4.4.
305 // We piggyback on Sema::IsQualificationConversion for this, since the rules
306 // are non-trivial. So first we construct Tcv *...cv* as described in
307 // C++ 5.2.11p8.
Douglas Gregorf8e92702008-10-24 15:36:09 +0000308
309 QualType UnwrappedSrcType = SrcType, UnwrappedDestType = DestType;
310 llvm::SmallVector<unsigned, 8> cv1, cv2;
311
312 // Find the qualifications.
313 while (UnwrapSimilarPointerTypes(UnwrappedSrcType, UnwrappedDestType)) {
314 cv1.push_back(UnwrappedSrcType.getCVRQualifiers());
315 cv2.push_back(UnwrappedDestType.getCVRQualifiers());
316 }
317 assert(cv1.size() > 0 && "Must have at least one pointer level.");
318
319 // Construct void pointers with those qualifiers (in reverse order of
320 // unwrapping, of course).
321 QualType SrcConstruct = Context.VoidTy;
322 QualType DestConstruct = Context.VoidTy;
323 for (llvm::SmallVector<unsigned, 8>::reverse_iterator i1 = cv1.rbegin(),
324 i2 = cv2.rbegin();
325 i1 != cv1.rend(); ++i1, ++i2)
326 {
327 SrcConstruct = Context.getPointerType(SrcConstruct.getQualifiedType(*i1));
328 DestConstruct = Context.getPointerType(DestConstruct.getQualifiedType(*i2));
329 }
330
331 // Test if they're compatible.
332 return SrcConstruct != DestConstruct &&
333 !IsQualificationConversion(SrcConstruct, DestConstruct);
334}
335
336/// CheckStaticCast - Check that a static_cast\<DestType\>(SrcExpr) is valid.
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000337/// Refer to C++ 5.2.9 for details. Static casts are mostly used for making
338/// implicit conversions explicit and getting rid of data loss warnings.
Douglas Gregorf8e92702008-10-24 15:36:09 +0000339void
Sebastian Redl87789742008-11-02 22:21:33 +0000340Sema::CheckStaticCast(Expr *&SrcExpr, QualType DestType,
341 const SourceRange &OpRange)
Douglas Gregorf8e92702008-10-24 15:36:09 +0000342{
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000343 // Conversions are tried roughly in the order the standard specifies them.
344 // This is necessary because there are some conversions that can be
345 // interpreted in more than one way, and the order disambiguates.
346 // DR 427 specifies that paragraph 5 is to be applied before paragraph 2.
347
348 // This option is unambiguous and simple, so put it here.
349 // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
350 if (DestType->isVoidType()) {
351 return;
352 }
353
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000354 // C++ 5.2.9p5, reference downcast.
355 // See the function for details.
356 if (IsStaticReferenceDowncast(SrcExpr, DestType)) {
357 return;
358 }
359
360 // C++ 5.2.9p2: An expression e can be explicitly converted to a type T
361 // [...] if the declaration "T t(e);" is well-formed, [...].
362 ImplicitConversionSequence ICS = TryDirectInitialization(SrcExpr, DestType);
363 if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) {
364 if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion &&
365 ICS.Standard.First != ICK_Identity)
366 {
367 DefaultFunctionArrayConversion(SrcExpr);
368 }
369 return;
370 }
371 // FIXME: Missing the validation of the conversion, e.g. for an accessible
372 // base.
373
374 // C++ 5.2.9p6: May apply the reverse of any standard conversion, except
375 // lvalue-to-rvalue, array-to-pointer, function-to-pointer, and boolean
376 // conversions, subject to further restrictions.
377 // Also, C++ 5.2.9p1 forbids casting away constness, which makes reversal
378 // of qualification conversions impossible.
379
380 // The lvalue-to-rvalue, array-to-pointer and function-to-pointer conversions
381 // are applied to the expression.
Sebastian Redl12aee862008-11-04 15:59:10 +0000382 QualType OrigSrcType = SrcExpr->getType();
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000383 DefaultFunctionArrayConversion(SrcExpr);
384
385 QualType SrcType = Context.getCanonicalType(SrcExpr->getType());
386
387 // Reverse integral promotion/conversion. All such conversions are themselves
388 // again integral promotions or conversions and are thus already handled by
389 // p2 (TryDirectInitialization above).
390 // (Note: any data loss warnings should be suppressed.)
391 // The exception is the reverse of enum->integer, i.e. integer->enum (and
392 // enum->enum). See also C++ 5.2.9p7.
393 // The same goes for reverse floating point promotion/conversion and
394 // floating-integral conversions. Again, only floating->enum is relevant.
395 if (DestType->isEnumeralType()) {
396 if (SrcType->isComplexType() || SrcType->isVectorType()) {
397 // Fall through - these cannot be converted.
398 } else if (SrcType->isArithmeticType() || SrcType->isEnumeralType()) {
Douglas Gregorf8e92702008-10-24 15:36:09 +0000399 return;
400 }
Douglas Gregorf8e92702008-10-24 15:36:09 +0000401 }
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000402
403 // Reverse pointer upcast. C++ 4.10p3 specifies pointer upcast.
404 // C++ 5.2.9p8 additionally disallows a cast path through virtual inheritance.
405 if (IsStaticPointerDowncast(SrcType, DestType)) {
406 return;
407 }
408
409 // Reverse member pointer conversion. C++ 5.11 specifies member pointer
410 // conversion. C++ 5.2.9p9 has additional information.
411 // DR54's access restrictions apply here also.
412 // FIXME: Don't have member pointers yet.
413
414 // Reverse pointer conversion to void*. C++ 4.10.p2 specifies conversion to
415 // void*. C++ 5.2.9p10 specifies additional restrictions, which really is
416 // just the usual constness stuff.
417 if (const PointerType *SrcPointer = SrcType->getAsPointerType()) {
418 QualType SrcPointee = SrcPointer->getPointeeType();
419 if (SrcPointee->isVoidType()) {
420 if (const PointerType *DestPointer = DestType->getAsPointerType()) {
421 QualType DestPointee = DestPointer->getPointeeType();
422 if (DestPointee->isObjectType() &&
423 DestPointee.isAtLeastAsQualifiedAs(SrcPointee))
424 {
425 return;
426 }
427 }
428 }
429 }
430
431 // We tried everything. Everything! Nothing works! :-(
432 // FIXME: Error reporting could be a lot better. Should store the reason
433 // why every substep failed and, at the end, select the most specific and
434 // report that.
Sebastian Redl87789742008-11-02 22:21:33 +0000435 Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_generic, "static_cast",
Sebastian Redl12aee862008-11-04 15:59:10 +0000436 DestType.getAsString(), OrigSrcType.getAsString(), OpRange);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000437}
438
439/// Tests whether a conversion according to C++ 5.2.9p5 is valid.
440bool
441Sema::IsStaticReferenceDowncast(Expr *SrcExpr, QualType DestType)
442{
443 // C++ 5.2.9p5: An lvalue of type "cv1 B", where B is a class type, can be
444 // cast to type "reference to cv2 D", where D is a class derived from B,
445 // if a valid standard conversion from "pointer to D" to "pointer to B"
446 // exists, cv2 >= cv1, and B is not a virtual base class of D.
447 // In addition, DR54 clarifies that the base must be accessible in the
448 // current context. Although the wording of DR54 only applies to the pointer
449 // variant of this rule, the intent is clearly for it to apply to the this
450 // conversion as well.
451
452 if (SrcExpr->isLvalue(Context) != Expr::LV_Valid) {
453 return false;
454 }
455
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000456 const ReferenceType *DestReference = DestType->getAsReferenceType();
457 if (!DestReference) {
458 return false;
459 }
460 QualType DestPointee = DestReference->getPointeeType();
461
Sebastian Redl12aee862008-11-04 15:59:10 +0000462 return IsStaticDowncast(SrcExpr->getType(), DestPointee);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000463}
464
465/// Tests whether a conversion according to C++ 5.2.9p8 is valid.
466bool
467Sema::IsStaticPointerDowncast(QualType SrcType, QualType DestType)
468{
469 // C++ 5.2.9p8: An rvalue of type "pointer to cv1 B", where B is a class
470 // type, can be converted to an rvalue of type "pointer to cv2 D", where D
471 // is a class derived from B, if a valid standard conversion from "pointer
472 // to D" to "pointer to B" exists, cv2 >= cv1, and B is not a virtual base
473 // class of D.
474 // In addition, DR54 clarifies that the base must be accessible in the
475 // current context.
476
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000477 const PointerType *SrcPointer = SrcType->getAsPointerType();
478 if (!SrcPointer) {
479 return false;
480 }
481
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000482 const PointerType *DestPointer = DestType->getAsPointerType();
483 if (!DestPointer) {
484 return false;
485 }
486
487 return IsStaticDowncast(SrcPointer->getPointeeType(),
488 DestPointer->getPointeeType());
489}
490
491/// IsStaticDowncast - Common functionality of IsStaticReferenceDowncast and
492/// IsStaticPointerDowncast. Tests whether a static downcast from SrcType to
493/// DestType, both of which must be canonical, is possible and allowed.
494bool
495Sema::IsStaticDowncast(QualType SrcType, QualType DestType)
496{
Sebastian Redl12aee862008-11-04 15:59:10 +0000497 // Downcast can only happen in class hierarchies, so we need classes.
498 if (!DestType->isRecordType() || !SrcType->isRecordType()) {
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000499 return false;
500 }
501
502 // Comparing cv is cheaper, so do it first.
503 if (!DestType.isAtLeastAsQualifiedAs(SrcType)) {
504 return false;
505 }
506
507 BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false,
508 /*DetectVirtual=*/true);
509 if (!IsDerivedFrom(DestType, SrcType, Paths)) {
510 return false;
511 }
512
513 if (Paths.isAmbiguous(SrcType.getUnqualifiedType())) {
514 return false;
515 }
516
517 if (Paths.getDetectedVirtual() != 0) {
518 return false;
519 }
520
521 // FIXME: Test accessibility.
522
523 return true;
524}
525
526/// TryDirectInitialization - Attempt to direct-initialize a value of the
527/// given type (DestType) from the given expression (SrcExpr), as one would
528/// do when creating an object with new with parameters. This function returns
529/// an implicit conversion sequence that can be used to perform the
530/// initialization.
531/// This routine is very similar to TryCopyInitialization; the differences
532/// between the two (C++ 8.5p12 and C++ 8.5p14) are:
533/// 1) In direct-initialization, all constructors of the target type are
534/// considered, including those marked as explicit.
535/// 2) In direct-initialization, overload resolution is performed over the
536/// constructors of the target type. In copy-initialization, overload
537/// resolution is performed over all conversion functions that result in
538/// the target type. This can lead to different functions used.
539ImplicitConversionSequence
540Sema::TryDirectInitialization(Expr *SrcExpr, QualType DestType)
541{
542 if (!DestType->isRecordType()) {
543 // For non-class types, copy and direct initialization are identical.
544 // C++ 8.5p11
545 // FIXME: Those parts should be in a common function, actually.
546 return TryCopyInitialization(SrcExpr, DestType);
547 }
548
549 // Not enough support for the rest yet, actually.
550 ImplicitConversionSequence ICS;
551 ICS.ConversionKind = ImplicitConversionSequence::BadConversion;
552 return ICS;
553}
554
555/// CheckDynamicCast - Check that a dynamic_cast\<DestType\>(SrcExpr) is valid.
556/// Refer to C++ 5.2.7 for details. Dynamic casts are used mostly for runtime-
557/// checked downcasts in class hierarchies.
558void
Sebastian Redl87789742008-11-02 22:21:33 +0000559Sema::CheckDynamicCast(Expr *&SrcExpr, QualType DestType,
560 const SourceRange &OpRange,
561 const SourceRange &DestRange)
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000562{
563 QualType OrigDestType = DestType, OrigSrcType = SrcExpr->getType();
564 DestType = Context.getCanonicalType(DestType);
565
566 // C++ 5.2.7p1: T shall be a pointer or reference to a complete class type,
567 // or "pointer to cv void".
568
569 QualType DestPointee;
570 const PointerType *DestPointer = DestType->getAsPointerType();
571 const ReferenceType *DestReference = DestType->getAsReferenceType();
572 if (DestPointer) {
573 DestPointee = DestPointer->getPointeeType();
574 } else if (DestReference) {
575 DestPointee = DestReference->getPointeeType();
576 } else {
Sebastian Redl12aee862008-11-04 15:59:10 +0000577 Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ref_or_ptr,
578 OrigDestType.getAsString(), DestRange);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000579 return;
580 }
581
582 const RecordType *DestRecord = DestPointee->getAsRecordType();
583 if (DestPointee->isVoidType()) {
584 assert(DestPointer && "Reference to void is not possible");
585 } else if (DestRecord) {
586 if (!DestRecord->getDecl()->isDefinition()) {
Sebastian Redl12aee862008-11-04 15:59:10 +0000587 Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_incomplete,
588 DestPointee.getUnqualifiedType().getAsString(), DestRange);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000589 return;
590 }
591 } else {
Sebastian Redl12aee862008-11-04 15:59:10 +0000592 Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class,
593 DestPointee.getUnqualifiedType().getAsString(), DestRange);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000594 return;
595 }
596
597 // C++ 5.2.7p2: If T is a pointer type, v shall be an rvalue of a pointer to
598 // complete class type, [...]. If T is a reference type, v shall be an
599 // lvalue of a complete class type, [...].
600
601 QualType SrcType = Context.getCanonicalType(OrigSrcType);
602 QualType SrcPointee;
603 if (DestPointer) {
604 if (const PointerType *SrcPointer = SrcType->getAsPointerType()) {
605 SrcPointee = SrcPointer->getPointeeType();
606 } else {
Sebastian Redl12aee862008-11-04 15:59:10 +0000607 Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_ptr,
608 OrigSrcType.getAsString(), SrcExpr->getSourceRange());
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000609 return;
610 }
611 } else {
612 if (SrcExpr->isLvalue(Context) != Expr::LV_Valid) {
Sebastian Redl12aee862008-11-04 15:59:10 +0000613 Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_rvalue, "dynamic_cast",
614 OrigDestType.getAsString(), OpRange);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000615 }
616 SrcPointee = SrcType;
617 }
618
619 const RecordType *SrcRecord = SrcPointee->getAsRecordType();
620 if (SrcRecord) {
621 if (!SrcRecord->getDecl()->isDefinition()) {
Sebastian Redl12aee862008-11-04 15:59:10 +0000622 Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_incomplete,
623 SrcPointee.getUnqualifiedType().getAsString(),
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000624 SrcExpr->getSourceRange());
625 return;
626 }
627 } else {
Sebastian Redl12aee862008-11-04 15:59:10 +0000628 Diag(OpRange.getBegin(), diag::err_bad_dynamic_cast_not_class,
629 SrcPointee.getUnqualifiedType().getAsString(),
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000630 SrcExpr->getSourceRange());
631 return;
632 }
633
Sebastian Redl12aee862008-11-04 15:59:10 +0000634 assert((DestPointer || DestReference) &&
635 "Bad destination non-ptr/ref slipped through.");
636 assert((DestRecord || DestPointee->isVoidType()) &&
637 "Bad destination pointee slipped through.");
638 assert(SrcRecord && "Bad source pointee slipped through.");
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000639
640 // C++ 5.2.7p1: The dynamic_cast operator shall not cast away constness.
641 if (!DestPointee.isAtLeastAsQualifiedAs(SrcPointee)) {
Sebastian Redl87789742008-11-02 22:21:33 +0000642 Diag(OpRange.getBegin(), diag::err_bad_cxx_cast_const_away, "dynamic_cast",
643 OrigDestType.getAsString(), OrigSrcType.getAsString(), OpRange);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000644 return;
645 }
646
647 // C++ 5.2.7p3: If the type of v is the same as the required result type,
648 // [except for cv].
649 if (DestRecord == SrcRecord) {
650 return;
651 }
652
653 // C++ 5.2.7p5
654 // Upcasts are resolved statically.
655 if (DestRecord && IsDerivedFrom(SrcPointee, DestPointee)) {
Sebastian Redl87789742008-11-02 22:21:33 +0000656 CheckDerivedToBaseConversion(SrcPointee, DestPointee, OpRange.getBegin(),
657 OpRange);
Sebastian Redl9ac68aa2008-10-31 14:43:28 +0000658 // Diagnostic already emitted on error.
659 return;
660 }
661
662 // C++ 5.2.7p6: Otherwise, v shall be [polymorphic].
663 // FIXME: Information not yet available.
664
665 // Done. Everything else is run-time checks.
Chris Lattner4b009652007-07-25 00:24:17 +0000666}
667
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000668/// ActOnCXXBoolLiteral - Parse {true,false} literals.
Chris Lattner4b009652007-07-25 00:24:17 +0000669Action::ExprResult
Steve Naroff5cbb02f2007-09-16 14:56:35 +0000670Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
Douglas Gregorf8e92702008-10-24 15:36:09 +0000671 assert((Kind == tok::kw_true || Kind == tok::kw_false) &&
Chris Lattner4b009652007-07-25 00:24:17 +0000672 "Unknown C++ Boolean value!");
Steve Naroffac5d4f12007-08-25 14:02:58 +0000673 return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000674}
Chris Lattnera7447ba2008-02-26 00:51:44 +0000675
676/// ActOnCXXThrow - Parse throw expressions.
677Action::ExprResult
678Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) {
679 return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc);
680}
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000681
682Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) {
683 /// C++ 9.3.2: In the body of a non-static member function, the keyword this
684 /// is a non-lvalue expression whose value is the address of the object for
685 /// which the function is called.
686
687 if (!isa<FunctionDecl>(CurContext)) {
688 Diag(ThisLoc, diag::err_invalid_this_use);
689 return ExprResult(true);
690 }
691
692 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext))
693 if (MD->isInstance())
Douglas Gregora5b022a2008-11-04 14:32:21 +0000694 return new CXXThisExpr(ThisLoc, MD->getThisType(Context));
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000695
696 return Diag(ThisLoc, diag::err_invalid_this_use);
697}
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000698
699/// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
700/// Can be interpreted either as function-style casting ("int(x)")
701/// or class type construction ("ClassType(x,y,z)")
702/// or creation of a value-initialized type ("int()").
703Action::ExprResult
704Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep,
705 SourceLocation LParenLoc,
706 ExprTy **ExprTys, unsigned NumExprs,
707 SourceLocation *CommaLocs,
708 SourceLocation RParenLoc) {
709 assert(TypeRep && "Missing type!");
710 QualType Ty = QualType::getFromOpaquePtr(TypeRep);
711 Expr **Exprs = (Expr**)ExprTys;
712 SourceLocation TyBeginLoc = TypeRange.getBegin();
713 SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc);
714
715 if (const RecordType *RT = Ty->getAsRecordType()) {
716 // C++ 5.2.3p1:
717 // If the simple-type-specifier specifies a class type, the class type shall
718 // be complete.
719 //
720 if (!RT->getDecl()->isDefinition())
721 return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use,
722 Ty.getAsString(), FullRange);
723
Argiris Kirtzidiscf4e8f82008-10-06 23:16:35 +0000724 unsigned DiagID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error,
725 "class constructors are not supported yet");
726 return Diag(TyBeginLoc, DiagID);
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000727 }
728
729 // C++ 5.2.3p1:
730 // If the expression list is a single expression, the type conversion
731 // expression is equivalent (in definedness, and if defined in meaning) to the
732 // corresponding cast expression.
733 //
734 if (NumExprs == 1) {
735 if (CheckCastTypes(TypeRange, Ty, Exprs[0]))
736 return true;
Douglas Gregor21a04f32008-10-27 19:41:14 +0000737 return new CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, TyBeginLoc,
738 Exprs[0], RParenLoc);
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000739 }
740
741 // C++ 5.2.3p1:
742 // If the expression list specifies more than a single value, the type shall
743 // be a class with a suitably declared constructor.
744 //
745 if (NumExprs > 1)
746 return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg,
747 FullRange);
748
749 assert(NumExprs == 0 && "Expected 0 expressions");
750
751 // C++ 5.2.3p2:
752 // The expression T(), where T is a simple-type-specifier for a non-array
753 // complete object type or the (possibly cv-qualified) void type, creates an
754 // rvalue of the specified type, which is value-initialized.
755 //
756 if (Ty->isArrayType())
757 return Diag(TyBeginLoc, diag::err_value_init_for_array_type, FullRange);
758 if (Ty->isIncompleteType() && !Ty->isVoidType())
759 return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use,
760 Ty.getAsString(), FullRange);
761
762 return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc);
763}
Argiris Kirtzidis810c0f72008-09-10 02:17:11 +0000764
765
766/// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a
767/// C++ if/switch/while/for statement.
768/// e.g: "if (int x = f()) {...}"
769Action::ExprResult
770Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc,
771 Declarator &D,
772 SourceLocation EqualLoc,
773 ExprTy *AssignExprVal) {
774 assert(AssignExprVal && "Null assignment expression");
775
776 // C++ 6.4p2:
777 // The declarator shall not specify a function or an array.
778 // The type-specifier-seq shall not contain typedef and shall not declare a
779 // new class or enumeration.
780
781 assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
782 "Parser allowed 'typedef' as storage class of condition decl.");
783
784 QualType Ty = GetTypeForDeclarator(D, S);
785
786 if (Ty->isFunctionType()) { // The declarator shall not specify a function...
787 // We exit without creating a CXXConditionDeclExpr because a FunctionDecl
788 // would be created and CXXConditionDeclExpr wants a VarDecl.
789 return Diag(StartLoc, diag::err_invalid_use_of_function_type,
790 SourceRange(StartLoc, EqualLoc));
791 } else if (Ty->isArrayType()) { // ...or an array.
792 Diag(StartLoc, diag::err_invalid_use_of_array_type,
793 SourceRange(StartLoc, EqualLoc));
794 } else if (const RecordType *RT = Ty->getAsRecordType()) {
795 RecordDecl *RD = RT->getDecl();
796 // The type-specifier-seq shall not declare a new class...
797 if (RD->isDefinition() && (RD->getIdentifier() == 0 || S->isDeclScope(RD)))
798 Diag(RD->getLocation(), diag::err_type_defined_in_condition);
799 } else if (const EnumType *ET = Ty->getAsEnumType()) {
800 EnumDecl *ED = ET->getDecl();
801 // ...or enumeration.
802 if (ED->isDefinition() && (ED->getIdentifier() == 0 || S->isDeclScope(ED)))
803 Diag(ED->getLocation(), diag::err_type_defined_in_condition);
804 }
805
806 DeclTy *Dcl = ActOnDeclarator(S, D, 0);
807 if (!Dcl)
808 return true;
809 AddInitializerToDecl(Dcl, AssignExprVal);
810
811 return new CXXConditionDeclExpr(StartLoc, EqualLoc,
812 cast<VarDecl>(static_cast<Decl *>(Dcl)));
813}
814
815/// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid.
816bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) {
817 // C++ 6.4p4:
818 // The value of a condition that is an initialized declaration in a statement
819 // other than a switch statement is the value of the declared variable
820 // implicitly converted to type bool. If that conversion is ill-formed, the
821 // program is ill-formed.
822 // The value of a condition that is an expression is the value of the
823 // expression, implicitly converted to bool.
824 //
825 QualType Ty = CondExpr->getType(); // Save the type.
826 AssignConvertType
827 ConvTy = CheckSingleAssignmentConstraints(Context.BoolTy, CondExpr);
828 if (ConvTy == Incompatible)
829 return Diag(CondExpr->getLocStart(), diag::err_typecheck_bool_condition,
830 Ty.getAsString(), CondExpr->getSourceRange());
831 return false;
832}
Douglas Gregor1815b3b2008-09-12 00:47:35 +0000833
834/// Helper function to determine whether this is the (deprecated) C++
835/// conversion from a string literal to a pointer to non-const char or
836/// non-const wchar_t (for narrow and wide string literals,
837/// respectively).
838bool
839Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) {
840 // Look inside the implicit cast, if it exists.
841 if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From))
842 From = Cast->getSubExpr();
843
844 // A string literal (2.13.4) that is not a wide string literal can
845 // be converted to an rvalue of type "pointer to char"; a wide
846 // string literal can be converted to an rvalue of type "pointer
847 // to wchar_t" (C++ 4.2p2).
848 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From))
849 if (const PointerType *ToPtrType = ToType->getAsPointerType())
850 if (const BuiltinType *ToPointeeType
851 = ToPtrType->getPointeeType()->getAsBuiltinType()) {
852 // This conversion is considered only when there is an
853 // explicit appropriate pointer target type (C++ 4.2p2).
854 if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 &&
855 ((StrLit->isWide() && ToPointeeType->isWideCharType()) ||
856 (!StrLit->isWide() &&
857 (ToPointeeType->getKind() == BuiltinType::Char_U ||
858 ToPointeeType->getKind() == BuiltinType::Char_S))))
859 return true;
860 }
861
862 return false;
863}
Douglas Gregorbb461502008-10-24 04:54:22 +0000864
865/// PerformImplicitConversion - Perform an implicit conversion of the
866/// expression From to the type ToType. Returns true if there was an
867/// error, false otherwise. The expression From is replaced with the
868/// converted expression.
869bool
870Sema::PerformImplicitConversion(Expr *&From, QualType ToType)
871{
Douglas Gregor81c29152008-10-29 00:13:59 +0000872 ImplicitConversionSequence ICS = TryImplicitConversion(From, ToType);
Douglas Gregorbb461502008-10-24 04:54:22 +0000873 switch (ICS.ConversionKind) {
874 case ImplicitConversionSequence::StandardConversion:
875 if (PerformImplicitConversion(From, ToType, ICS.Standard))
876 return true;
877 break;
878
879 case ImplicitConversionSequence::UserDefinedConversion:
880 // FIXME: This is, of course, wrong. We'll need to actually call
881 // the constructor or conversion operator, and then cope with the
882 // standard conversions.
883 ImpCastExprToType(From, ToType);
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000884 return false;
Douglas Gregorbb461502008-10-24 04:54:22 +0000885
886 case ImplicitConversionSequence::EllipsisConversion:
887 assert(false && "Cannot perform an ellipsis conversion");
Douglas Gregorb72e9da2008-10-31 16:23:19 +0000888 return false;
Douglas Gregorbb461502008-10-24 04:54:22 +0000889
890 case ImplicitConversionSequence::BadConversion:
891 return true;
892 }
893
894 // Everything went well.
895 return false;
896}
897
898/// PerformImplicitConversion - Perform an implicit conversion of the
899/// expression From to the type ToType by following the standard
900/// conversion sequence SCS. Returns true if there was an error, false
901/// otherwise. The expression From is replaced with the converted
902/// expression.
903bool
904Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
905 const StandardConversionSequence& SCS)
906{
907 // Overall FIXME: we are recomputing too many types here and doing
908 // far too much extra work. What this means is that we need to keep
909 // track of more information that is computed when we try the
910 // implicit conversion initially, so that we don't need to recompute
911 // anything here.
912 QualType FromType = From->getType();
913
Douglas Gregora3b34bb2008-11-03 19:09:14 +0000914 if (SCS.CopyConstructor) {
915 // FIXME: Create a temporary object by calling the copy
916 // constructor.
917 ImpCastExprToType(From, ToType);
918 return false;
919 }
920
Douglas Gregorbb461502008-10-24 04:54:22 +0000921 // Perform the first implicit conversion.
922 switch (SCS.First) {
923 case ICK_Identity:
924 case ICK_Lvalue_To_Rvalue:
925 // Nothing to do.
926 break;
927
928 case ICK_Array_To_Pointer:
929 FromType = Context.getArrayDecayedType(FromType);
930 ImpCastExprToType(From, FromType);
931 break;
932
933 case ICK_Function_To_Pointer:
934 FromType = Context.getPointerType(FromType);
935 ImpCastExprToType(From, FromType);
936 break;
937
938 default:
939 assert(false && "Improper first standard conversion");
940 break;
941 }
942
943 // Perform the second implicit conversion
944 switch (SCS.Second) {
945 case ICK_Identity:
946 // Nothing to do.
947 break;
948
949 case ICK_Integral_Promotion:
950 case ICK_Floating_Promotion:
951 case ICK_Integral_Conversion:
952 case ICK_Floating_Conversion:
953 case ICK_Floating_Integral:
954 FromType = ToType.getUnqualifiedType();
955 ImpCastExprToType(From, FromType);
956 break;
957
958 case ICK_Pointer_Conversion:
959 if (CheckPointerConversion(From, ToType))
960 return true;
961 ImpCastExprToType(From, ToType);
962 break;
963
964 case ICK_Pointer_Member:
965 // FIXME: Implement pointer-to-member conversions.
966 assert(false && "Pointer-to-member conversions are unsupported");
967 break;
968
969 case ICK_Boolean_Conversion:
970 FromType = Context.BoolTy;
971 ImpCastExprToType(From, FromType);
972 break;
973
974 default:
975 assert(false && "Improper second standard conversion");
976 break;
977 }
978
979 switch (SCS.Third) {
980 case ICK_Identity:
981 // Nothing to do.
982 break;
983
984 case ICK_Qualification:
985 ImpCastExprToType(From, ToType);
986 break;
987
988 default:
989 assert(false && "Improper second standard conversion");
990 break;
991 }
992
993 return false;
994}
995