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 provides an abstract class for C++ code generation. Concrete subclasses
10// of this implement code generation for specific C++ ABIs.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CIRGenCXXABI.h"
15#include "CIRGenFunction.h"
16
17#include "clang/AST/Decl.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/GlobalDecl.h"
20
21using namespace clang;
22using namespace clang::CIRGen;
23
24CIRGenCXXABI::~CIRGenCXXABI() {}
25
26CIRGenCXXABI::AddedStructorArgCounts CIRGenCXXABI::addImplicitConstructorArgs(
27 CIRGenFunction &cgf, const CXXConstructorDecl *d, CXXCtorType type,
28 bool forVirtualBase, bool delegating, CallArgList &args) {
29 AddedStructorArgs addedArgs =
30 getImplicitConstructorArgs(cgf, d, type, forVirtualBase, delegating);
31 for (auto [idx, prefixArg] : llvm::enumerate(First&: addedArgs.prefix))
32 args.insert(I: args.begin() + 1 + idx,
33 Elt: CallArg(RValue::get(v: prefixArg.value), prefixArg.type));
34 for (const auto &arg : addedArgs.suffix)
35 args.add(rvalue: RValue::get(v: arg.value), type: arg.type);
36 return AddedStructorArgCounts(addedArgs.prefix.size(),
37 addedArgs.suffix.size());
38}
39
40CatchTypeInfo CIRGenCXXABI::getCatchAllTypeInfo() {
41 return CatchTypeInfo{.rtti: {}, .flags: 0};
42}
43
44void CIRGenCXXABI::buildThisParam(CIRGenFunction &cgf,
45 FunctionArgList &params) {
46 const auto *md = cast<CXXMethodDecl>(Val: cgf.curGD.getDecl());
47
48 // FIXME: I'm not entirely sure I like using a fake decl just for code
49 // generation. Maybe we can come up with a better way?
50 auto *thisDecl =
51 ImplicitParamDecl::Create(C&: cgm.getASTContext(), DC: nullptr, IdLoc: md->getLocation(),
52 Id: &cgm.getASTContext().Idents.get(Name: "this"),
53 T: md->getThisType(), ParamKind: ImplicitParamKind::CXXThis);
54 params.push_back(Elt: thisDecl);
55 cgf.cxxabiThisDecl = thisDecl;
56
57 // Classic codegen computes the alignment of thisDecl and saves it in
58 // CodeGenFunction::CXXABIThisAlignment, but it is only used in emitTypeCheck
59 // in CodeGenFunction::StartFunction().
60 assert(!cir::MissingFeatures::cxxabiThisAlignment());
61}
62
63cir::GlobalLinkageKind CIRGenCXXABI::getCXXDestructorLinkage(
64 GVALinkage linkage, const CXXDestructorDecl *dtor, CXXDtorType dt) const {
65 // Delegate back to cgm by default.
66 return cgm.getCIRLinkageForDeclarator(dd: dtor, linkage);
67}
68
69mlir::Value CIRGenCXXABI::loadIncomingCXXThis(CIRGenFunction &cgf) {
70 ImplicitParamDecl *vd = getThisDecl(cgf);
71 Address addr = cgf.getAddrOfLocalVar(vd);
72 return cir::LoadOp::create(builder&: cgf.getBuilder(), location: cgf.getLoc(srcLoc: vd->getLocation()),
73 result: addr.getElementType(), addr: addr.getPointer());
74}
75
76void CIRGenCXXABI::setCXXABIThisValue(CIRGenFunction &cgf,
77 mlir::Value thisPtr) {
78 /// Initialize the 'this' slot.
79 assert(getThisDecl(cgf) && "no 'this' variable for function");
80 cgf.cxxabiThisValue = thisPtr;
81}
82
83CharUnits CIRGenCXXABI::getArrayCookieSize(const CXXNewExpr *e) {
84 if (!requiresArrayCookie(e))
85 return CharUnits::Zero();
86
87 return getArrayCookieSizeImpl(elementType: e->getAllocatedType());
88}
89
90bool CIRGenCXXABI::requiresArrayCookie(const CXXNewExpr *e) {
91 // If the class's usual deallocation function takes two arguments,
92 // it needs a cookie.
93 if (e->doesUsualArrayDeleteWantSize())
94 return true;
95
96 return e->getAllocatedType().isDestructedType();
97}
98
99void CIRGenCXXABI::emitReturnFromThunk(CIRGenFunction &cgf, RValue rv,
100 QualType resultType) {
101 assert(!cgf.hasAggregateEvaluationKind(resultType) &&
102 "cannot handle aggregates");
103 mlir::Location loc = cgf.getBuilder().getUnknownLoc();
104 cgf.emitReturnOfRValue(loc, rv, ty: resultType);
105}
106