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