1 //==--- CodeGenABITypes.cpp - Convert Clang types to LLVM types for ABI ----==//
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
7 //===----------------------------------------------------------------------===//
9 // CodeGenABITypes is a simple interface for getting LLVM types for
10 // the parameters and the return value of a function given the Clang
13 // The class is implemented as a public wrapper around the private
14 // CodeGenTypes class in lib/CodeGen.
16 //===----------------------------------------------------------------------===//
18 #include "clang/CodeGen/CodeGenABITypes.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"
27 using namespace clang
;
28 using namespace CodeGen
;
30 void CodeGen::addDefaultFunctionDefinitionAttributes(CodeGenModule
&CGM
,
31 llvm::AttrBuilder
&attrs
) {
32 CGM
.addDefaultFunctionDefinitionAttributes(attrs
);
35 const CGFunctionInfo
&
36 CodeGen::arrangeObjCMessageSendSignature(CodeGenModule
&CGM
,
37 const ObjCMethodDecl
*MD
,
38 QualType receiverType
) {
39 return CGM
.getTypes().arrangeObjCMessageSendSignature(MD
, receiverType
);
42 const CGFunctionInfo
&
43 CodeGen::arrangeFreeFunctionType(CodeGenModule
&CGM
,
44 CanQual
<FunctionProtoType
> Ty
) {
45 return CGM
.getTypes().arrangeFreeFunctionType(Ty
);
48 const CGFunctionInfo
&
49 CodeGen::arrangeFreeFunctionType(CodeGenModule
&CGM
,
50 CanQual
<FunctionNoProtoType
> Ty
) {
51 return CGM
.getTypes().arrangeFreeFunctionType(Ty
);
54 const CGFunctionInfo
&
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
);
62 const CGFunctionInfo
&
63 CodeGen::arrangeFreeFunctionCall(CodeGenModule
&CGM
,
64 CanQualType returnType
,
65 ArrayRef
<CanQualType
> argTypes
,
66 FunctionType::ExtInfo info
,
68 return CGM
.getTypes().arrangeLLVMFunctionInfo(returnType
, FnInfoOpts::None
,
69 argTypes
, info
, {}, args
);
72 ImplicitCXXConstructorArgs
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
);
90 for (const auto &arg
: addedArgs
.Suffix
) {
91 implicitArgs
.Suffix
.push_back(arg
.Value
);
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());
101 if (auto FT
= dyn_cast
<llvm::FunctionType
>(T
))
108 CodeGen::convertTypeForMemory(CodeGenModule
&CGM
, QualType T
) {
109 return CGM
.getTypes().ConvertTypeForMem(T
);
112 unsigned CodeGen::getLLVMFieldNumber(CodeGenModule
&CGM
,
113 const RecordDecl
*RD
,
114 const FieldDecl
*FD
) {
115 return CGM
.getTypes().getCGRecordLayout(RD
).getLLVMFieldNo(FD
);
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);
125 CGF
.CurFn
= InsertBlock
->getParent();
126 CGF
.Builder
.SetInsertPoint(InsertBlock
, InsertPoint
);
127 return CGM
.getCXXABI().getCXXDestructorImplicitParam(
128 CGF
, D
, Type
, ForVirtualBase
, Delegating
);