1 //==--- CodeGenABITypes.cpp - Convert Clang types to LLVM types for ABI ----==// 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 // CodeGenABITypes is a simple interface for getting LLVM types for 10 // the parameters and the return value of a function given the Clang 11 // types. 12 // 13 // The class is implemented as a public wrapper around the private 14 // CodeGenTypes class in lib/CodeGen. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "clang/CodeGen/CodeGenABITypes.h" 19 #include "CGCXXABI.h" 20 #include "CGRecordLayout.h" 21 #include "CodeGenFunction.h" 22 #include "CodeGenModule.h" 23 #include "clang/CodeGen/CGFunctionInfo.h" 24 25 using namespace clang; 26 using namespace CodeGen; 27 28 void CodeGen::addDefaultFunctionDefinitionAttributes(CodeGenModule &CGM, 29 llvm::AttrBuilder &attrs) { 30 CGM.addDefaultFunctionDefinitionAttributes(attrs); 31 } 32 33 const CGFunctionInfo & 34 CodeGen::arrangeObjCMessageSendSignature(CodeGenModule &CGM, 35 const ObjCMethodDecl *MD, 36 QualType receiverType) { 37 return CGM.getTypes().arrangeObjCMessageSendSignature(MD, receiverType); 38 } 39 40 const CGFunctionInfo & 41 CodeGen::arrangeFreeFunctionType(CodeGenModule &CGM, 42 CanQual<FunctionProtoType> Ty) { 43 return CGM.getTypes().arrangeFreeFunctionType(Ty); 44 } 45 46 const CGFunctionInfo & 47 CodeGen::arrangeFreeFunctionType(CodeGenModule &CGM, 48 CanQual<FunctionNoProtoType> Ty) { 49 return CGM.getTypes().arrangeFreeFunctionType(Ty); 50 } 51 52 const CGFunctionInfo & 53 CodeGen::arrangeCXXMethodType(CodeGenModule &CGM, 54 const CXXRecordDecl *RD, 55 const FunctionProtoType *FTP, 56 const CXXMethodDecl *MD) { 57 return CGM.getTypes().arrangeCXXMethodType(RD, FTP, MD); 58 } 59 60 const CGFunctionInfo &CodeGen::arrangeCXXMethodCall( 61 CodeGenModule &CGM, CanQualType returnType, ArrayRef<CanQualType> argTypes, 62 FunctionType::ExtInfo info, 63 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos, 64 RequiredArgs args) { 65 return CGM.getTypes().arrangeLLVMFunctionInfo( 66 returnType, FnInfoOpts::IsInstanceMethod, argTypes, info, paramInfos, 67 args); 68 } 69 70 const CGFunctionInfo &CodeGen::arrangeFreeFunctionCall( 71 CodeGenModule &CGM, CanQualType returnType, ArrayRef<CanQualType> argTypes, 72 FunctionType::ExtInfo info, 73 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos, 74 RequiredArgs args) { 75 return CGM.getTypes().arrangeLLVMFunctionInfo( 76 returnType, FnInfoOpts::None, argTypes, info, paramInfos, args); 77 } 78 79 ImplicitCXXConstructorArgs 80 CodeGen::getImplicitCXXConstructorArgs(CodeGenModule &CGM, 81 const CXXConstructorDecl *D) { 82 // We have to create a dummy CodeGenFunction here to pass to 83 // getImplicitConstructorArgs(). In some cases (base and delegating 84 // constructor calls), getImplicitConstructorArgs() can reach into the 85 // CodeGenFunction to find parameters of the calling constructor to pass on to 86 // the called constructor, but that can't happen here because we're asking for 87 // the args for a complete, non-delegating constructor call. 88 CodeGenFunction CGF(CGM, /* suppressNewContext= */ true); 89 CGCXXABI::AddedStructorArgs addedArgs = 90 CGM.getCXXABI().getImplicitConstructorArgs(CGF, D, Ctor_Complete, 91 /* ForVirtualBase= */ false, 92 /* Delegating= */ false); 93 ImplicitCXXConstructorArgs implicitArgs; 94 for (const auto &arg : addedArgs.Prefix) { 95 implicitArgs.Prefix.push_back(arg.Value); 96 } 97 for (const auto &arg : addedArgs.Suffix) { 98 implicitArgs.Suffix.push_back(arg.Value); 99 } 100 return implicitArgs; 101 } 102 103 llvm::FunctionType * 104 CodeGen::convertFreeFunctionType(CodeGenModule &CGM, const FunctionDecl *FD) { 105 assert(FD != nullptr && "Expected a non-null function declaration!"); 106 llvm::Type *T = CGM.getTypes().ConvertType(FD->getType()); 107 108 if (auto FT = dyn_cast<llvm::FunctionType>(T)) 109 return FT; 110 111 return nullptr; 112 } 113 114 llvm::Type * 115 CodeGen::convertTypeForMemory(CodeGenModule &CGM, QualType T) { 116 return CGM.getTypes().ConvertTypeForMem(T); 117 } 118 119 unsigned CodeGen::getLLVMFieldNumber(CodeGenModule &CGM, 120 const RecordDecl *RD, 121 const FieldDecl *FD) { 122 return CGM.getTypes().getCGRecordLayout(RD).getLLVMFieldNo(FD); 123 } 124 125 llvm::Value *CodeGen::getCXXDestructorImplicitParam( 126 CodeGenModule &CGM, llvm::BasicBlock *InsertBlock, 127 llvm::BasicBlock::iterator InsertPoint, const CXXDestructorDecl *D, 128 CXXDtorType Type, bool ForVirtualBase, bool Delegating) { 129 CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); 130 CGF.CurCodeDecl = D; 131 CGF.CurFuncDecl = D; 132 CGF.CurFn = InsertBlock->getParent(); 133 CGF.Builder.SetInsertPoint(InsertBlock, InsertPoint); 134 return CGM.getCXXABI().getCXXDestructorImplicitParam( 135 CGF, D, Type, ForVirtualBase, Delegating); 136 } 137