brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.7 KiB · aa0182e Raw
99 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This provides an abstract class for C++ code generation. Concrete subclasses10// 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(addedArgs.prefix))32    args.insert(args.begin() + 1 + idx,33                CallArg(RValue::get(prefixArg.value), prefixArg.type));34  for (const auto &arg : addedArgs.suffix)35    args.add(RValue::get(arg.value), arg.type);36  return AddedStructorArgCounts(addedArgs.prefix.size(),37                                addedArgs.suffix.size());38}39 40CatchTypeInfo CIRGenCXXABI::getCatchAllTypeInfo() {41  return CatchTypeInfo{{}, 0};42}43 44void CIRGenCXXABI::buildThisParam(CIRGenFunction &cgf,45                                  FunctionArgList &params) {46  const auto *md = cast<CXXMethodDecl>(cgf.curGD.getDecl());47 48  // FIXME: I'm not entirely sure I like using a fake decl just for code49  // generation. Maybe we can come up with a better way?50  auto *thisDecl =51      ImplicitParamDecl::Create(cgm.getASTContext(), nullptr, md->getLocation(),52                                &cgm.getASTContext().Idents.get("this"),53                                md->getThisType(), ImplicitParamKind::CXXThis);54  params.push_back(thisDecl);55  cgf.cxxabiThisDecl = thisDecl;56 57  // Classic codegen computes the alignment of thisDecl and saves it in58  // CodeGenFunction::CXXABIThisAlignment, but it is only used in emitTypeCheck59  // 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(dtor, linkage,67                                        /*isConstantVariable=*/false);68}69 70mlir::Value CIRGenCXXABI::loadIncomingCXXThis(CIRGenFunction &cgf) {71  ImplicitParamDecl *vd = getThisDecl(cgf);72  Address addr = cgf.getAddrOfLocalVar(vd);73  return cir::LoadOp::create(cgf.getBuilder(), cgf.getLoc(vd->getLocation()),74                             addr.getElementType(), addr.getPointer());75}76 77void CIRGenCXXABI::setCXXABIThisValue(CIRGenFunction &cgf,78                                      mlir::Value thisPtr) {79  /// Initialize the 'this' slot.80  assert(getThisDecl(cgf) && "no 'this' variable for function");81  cgf.cxxabiThisValue = thisPtr;82}83 84CharUnits CIRGenCXXABI::getArrayCookieSize(const CXXNewExpr *e) {85  if (!requiresArrayCookie(e))86    return CharUnits::Zero();87 88  return getArrayCookieSizeImpl(e->getAllocatedType());89}90 91bool CIRGenCXXABI::requiresArrayCookie(const CXXNewExpr *e) {92  // If the class's usual deallocation function takes two arguments,93  // it needs a cookie.94  if (e->doesUsualArrayDeleteWantSize())95    return true;96 97  return e->getAllocatedType().isDestructedType();98}99