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