|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This contains code dealing with C++ code generation of classes |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "CIRGenFunction.h" |
| 14 | + |
| 15 | +#include "clang/AST/RecordLayout.h" |
| 16 | +#include "clang/CIR/MissingFeatures.h" |
| 17 | + |
| 18 | +using namespace clang; |
| 19 | +using namespace clang::CIRGen; |
| 20 | + |
| 21 | +Address |
| 22 | +CIRGenFunction::getAddressOfBaseClass(Address value, |
| 23 | + const CXXRecordDecl *derived, |
| 24 | + CastExpr::path_const_iterator pathBegin, |
| 25 | + CastExpr::path_const_iterator pathEnd, |
| 26 | + bool nullCheckValue, SourceLocation loc) { |
| 27 | + assert(pathBegin != pathEnd && "Base path should not be empty!"); |
| 28 | + |
| 29 | + CastExpr::path_const_iterator start = pathBegin; |
| 30 | + |
| 31 | + if ((*start)->isVirtual()) { |
| 32 | + // The implementation here is actually complete, but let's flag this |
| 33 | + // as an error until the rest of the virtual base class support is in place. |
| 34 | + cgm.errorNYI(loc, "getAddrOfBaseClass: virtual base"); |
| 35 | + return Address::invalid(); |
| 36 | + } |
| 37 | + |
| 38 | + // Compute the static offset of the ultimate destination within its |
| 39 | + // allocating subobject (the virtual base, if there is one, or else |
| 40 | + // the "complete" object that we see). |
| 41 | + CharUnits nonVirtualOffset = |
| 42 | + cgm.computeNonVirtualBaseClassOffset(derived, start, pathEnd); |
| 43 | + |
| 44 | + // Get the base pointer type. |
| 45 | + mlir::Type baseValueTy = convertType((pathEnd[-1])->getType()); |
| 46 | + assert(!cir::MissingFeatures::addressSpace()); |
| 47 | + |
| 48 | + // The if statement here is redundant now, but it will be needed when we add |
| 49 | + // support for virtual base classes. |
| 50 | + // If there is no virtual base, use cir.base_class_addr. It takes care of |
| 51 | + // the adjustment and the null pointer check. |
| 52 | + if (nonVirtualOffset.isZero()) { |
| 53 | + assert(!cir::MissingFeatures::sanitizers()); |
| 54 | + return builder.createBaseClassAddr(getLoc(loc), value, baseValueTy, 0, |
| 55 | + /*assumeNotNull=*/true); |
| 56 | + } |
| 57 | + |
| 58 | + assert(!cir::MissingFeatures::sanitizers()); |
| 59 | + |
| 60 | + // Apply the offset |
| 61 | + value = builder.createBaseClassAddr(getLoc(loc), value, baseValueTy, |
| 62 | + nonVirtualOffset.getQuantity(), |
| 63 | + /*assumeNotNull=*/true); |
| 64 | + |
| 65 | + // Cast to the destination type. |
| 66 | + value = value.withElementType(builder, baseValueTy); |
| 67 | + |
| 68 | + return value; |
| 69 | +} |
0 commit comments