|
| 1 | +//===--- CIRGenExprCXX.cpp - Emit CIR Code for C++ expressions ------------===// |
| 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 code generation of C++ expressions |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "CIRGenCXXABI.h" |
| 14 | +#include "CIRGenFunction.h" |
| 15 | + |
| 16 | +#include "clang/AST/DeclCXX.h" |
| 17 | +#include "clang/AST/ExprCXX.h" |
| 18 | +#include "clang/CIR/MissingFeatures.h" |
| 19 | + |
| 20 | +using namespace clang; |
| 21 | +using namespace clang::CIRGen; |
| 22 | + |
| 23 | +namespace { |
| 24 | +struct MemberCallInfo { |
| 25 | + RequiredArgs reqArgs; |
| 26 | + // Number of prefix arguments for the call. Ignores the `this` pointer. |
| 27 | + unsigned prefixSize; |
| 28 | +}; |
| 29 | +} // namespace |
| 30 | + |
| 31 | +static MemberCallInfo commonBuildCXXMemberOrOperatorCall( |
| 32 | + CIRGenFunction &cgf, const CXXMethodDecl *md, mlir::Value thisPtr, |
| 33 | + mlir::Value implicitParam, QualType implicitParamTy, const CallExpr *ce, |
| 34 | + CallArgList &args, CallArgList *rtlArgs) { |
| 35 | + assert(ce == nullptr || isa<CXXMemberCallExpr>(ce) || |
| 36 | + isa<CXXOperatorCallExpr>(ce)); |
| 37 | + assert(md->isInstance() && |
| 38 | + "Trying to emit a member or operator call expr on a static method!"); |
| 39 | + |
| 40 | + // Push the this ptr. |
| 41 | + const CXXRecordDecl *rd = |
| 42 | + cgf.cgm.getCXXABI().getThisArgumentTypeForMethod(md); |
| 43 | + args.add(RValue::get(thisPtr), cgf.getTypes().deriveThisType(rd, md)); |
| 44 | + |
| 45 | + // If there is an implicit parameter (e.g. VTT), emit it. |
| 46 | + if (implicitParam) { |
| 47 | + args.add(RValue::get(implicitParam), implicitParamTy); |
| 48 | + } |
| 49 | + |
| 50 | + const auto *fpt = md->getType()->castAs<FunctionProtoType>(); |
| 51 | + RequiredArgs required = |
| 52 | + RequiredArgs::getFromProtoWithExtraSlots(fpt, args.size()); |
| 53 | + unsigned prefixSize = args.size() - 1; |
| 54 | + |
| 55 | + // Add the rest of the call args |
| 56 | + if (rtlArgs) { |
| 57 | + // Special case: if the caller emitted the arguments right-to-left already |
| 58 | + // (prior to emitting the *this argument), we're done. This happens for |
| 59 | + // assignment operators. |
| 60 | + args.addFrom(*rtlArgs); |
| 61 | + } else if (ce) { |
| 62 | + // Special case: skip first argument of CXXOperatorCall (it is "this"). |
| 63 | + unsigned argsToSkip = isa<CXXOperatorCallExpr>(ce) ? 1 : 0; |
| 64 | + cgf.emitCallArgs(args, fpt, drop_begin(ce->arguments(), argsToSkip), |
| 65 | + ce->getDirectCallee()); |
| 66 | + } else { |
| 67 | + assert( |
| 68 | + fpt->getNumParams() == 0 && |
| 69 | + "No CallExpr specified for function with non-zero number of arguments"); |
| 70 | + } |
| 71 | + |
| 72 | + // return {required, prefixSize}; |
| 73 | + return {required, prefixSize}; |
| 74 | +} |
| 75 | + |
| 76 | +RValue CIRGenFunction::emitCXXMemberOrOperatorMemberCallExpr( |
| 77 | + const CallExpr *ce, const CXXMethodDecl *md, ReturnValueSlot returnValue, |
| 78 | + bool hasQualifier, NestedNameSpecifier *qualifier, bool isArrow, |
| 79 | + const Expr *base) { |
| 80 | + assert(isa<CXXMemberCallExpr>(ce) || isa<CXXOperatorCallExpr>(ce)); |
| 81 | + |
| 82 | + if (md->isVirtual()) { |
| 83 | + cgm.errorNYI(ce->getSourceRange(), |
| 84 | + "emitCXXMemberOrOperatorMemberCallExpr: virtual call"); |
| 85 | + return RValue::get(nullptr); |
| 86 | + } |
| 87 | + |
| 88 | + bool trivialForCodegen = |
| 89 | + md->isTrivial() || (md->isDefaulted() && md->getParent()->isUnion()); |
| 90 | + bool trivialAssignment = |
| 91 | + trivialForCodegen && |
| 92 | + (md->isCopyAssignmentOperator() || md->isMoveAssignmentOperator()) && |
| 93 | + !md->getParent()->mayInsertExtraPadding(); |
| 94 | + (void)trivialAssignment; |
| 95 | + |
| 96 | + // C++17 demands that we evaluate the RHS of a (possibly-compound) assignment |
| 97 | + // operator before the LHS. |
| 98 | + CallArgList rtlArgStorage; |
| 99 | + CallArgList *rtlArgs = nullptr; |
| 100 | + if (auto *oce = dyn_cast<CXXOperatorCallExpr>(ce)) { |
| 101 | + cgm.errorNYI(oce->getSourceRange(), |
| 102 | + "emitCXXMemberOrOperatorMemberCallExpr: operator call"); |
| 103 | + return RValue::get(nullptr); |
| 104 | + } |
| 105 | + |
| 106 | + LValue thisPtr; |
| 107 | + if (isArrow) { |
| 108 | + LValueBaseInfo baseInfo; |
| 109 | + assert(!cir::MissingFeatures::opTBAA()); |
| 110 | + Address thisValue = emitPointerWithAlignment(base, &baseInfo); |
| 111 | + thisPtr = makeAddrLValue(thisValue, base->getType(), baseInfo); |
| 112 | + } else { |
| 113 | + thisPtr = emitLValue(base); |
| 114 | + } |
| 115 | + |
| 116 | + if (const CXXConstructorDecl *ctor = dyn_cast<CXXConstructorDecl>(md)) { |
| 117 | + cgm.errorNYI(ce->getSourceRange(), |
| 118 | + "emitCXXMemberOrOperatorMemberCallExpr: constructor call"); |
| 119 | + return RValue::get(nullptr); |
| 120 | + } |
| 121 | + |
| 122 | + if (trivialForCodegen) { |
| 123 | + if (isa<CXXDestructorDecl>(md)) |
| 124 | + return RValue::get(nullptr); |
| 125 | + |
| 126 | + if (trivialAssignment) { |
| 127 | + cgm.errorNYI(ce->getSourceRange(), |
| 128 | + "emitCXXMemberOrOperatorMemberCallExpr: trivial assignment"); |
| 129 | + return RValue::get(nullptr); |
| 130 | + } else { |
| 131 | + assert(md->getParent()->mayInsertExtraPadding() && |
| 132 | + "unknown trivial member function"); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // Compute the function type we're calling |
| 137 | + const CXXMethodDecl *calleeDecl = md; |
| 138 | + const CIRGenFunctionInfo *fInfo = nullptr; |
| 139 | + if (const auto *dtor = dyn_cast<CXXDestructorDecl>(calleeDecl)) { |
| 140 | + cgm.errorNYI(ce->getSourceRange(), |
| 141 | + "emitCXXMemberOrOperatorMemberCallExpr: destructor call"); |
| 142 | + return RValue::get(nullptr); |
| 143 | + } else { |
| 144 | + fInfo = &cgm.getTypes().arrangeCXXMethodDeclaration(calleeDecl); |
| 145 | + } |
| 146 | + |
| 147 | + mlir::Type ty = cgm.getTypes().getFunctionType(*fInfo); |
| 148 | + |
| 149 | + assert(!cir::MissingFeatures::sanitizers()); |
| 150 | + assert(!cir::MissingFeatures::emitTypeCheck()); |
| 151 | + |
| 152 | + if (const auto *dtor = dyn_cast<CXXDestructorDecl>(calleeDecl)) { |
| 153 | + cgm.errorNYI(ce->getSourceRange(), |
| 154 | + "emitCXXMemberOrOperatorMemberCallExpr: destructor call"); |
| 155 | + return RValue::get(nullptr); |
| 156 | + } |
| 157 | + |
| 158 | + assert(!cir::MissingFeatures::sanitizers()); |
| 159 | + if (getLangOpts().AppleKext) { |
| 160 | + cgm.errorNYI(ce->getSourceRange(), |
| 161 | + "emitCXXMemberOrOperatorMemberCallExpr: AppleKext"); |
| 162 | + return RValue::get(nullptr); |
| 163 | + } |
| 164 | + CIRGenCallee callee = |
| 165 | + CIRGenCallee::forDirect(cgm.getAddrOfFunction(md, ty), GlobalDecl(md)); |
| 166 | + |
| 167 | + return emitCXXMemberOrOperatorCall( |
| 168 | + calleeDecl, callee, returnValue, thisPtr.getPointer(), |
| 169 | + /*ImplicitParam=*/nullptr, QualType(), ce, rtlArgs); |
| 170 | +} |
| 171 | + |
| 172 | +RValue CIRGenFunction::emitCXXMemberOrOperatorCall( |
| 173 | + const CXXMethodDecl *md, const CIRGenCallee &callee, |
| 174 | + ReturnValueSlot returnValue, mlir::Value thisPtr, mlir::Value implicitParam, |
| 175 | + QualType implicitParamTy, const CallExpr *ce, CallArgList *rtlArgs) { |
| 176 | + const auto *fpt = md->getType()->castAs<FunctionProtoType>(); |
| 177 | + CallArgList args; |
| 178 | + MemberCallInfo callInfo = commonBuildCXXMemberOrOperatorCall( |
| 179 | + *this, md, thisPtr, implicitParam, implicitParamTy, ce, args, rtlArgs); |
| 180 | + auto &fnInfo = cgm.getTypes().arrangeCXXMethodCall( |
| 181 | + args, fpt, callInfo.reqArgs, callInfo.prefixSize); |
| 182 | + assert((ce || currSrcLoc) && "expected source location"); |
| 183 | + mlir::Location loc = ce ? getLoc(ce->getExprLoc()) : *currSrcLoc; |
| 184 | + assert(!cir::MissingFeatures::opCallMustTail()); |
| 185 | + return emitCall(fnInfo, callee, returnValue, args, nullptr, loc); |
| 186 | +} |
0 commit comments