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 "CGRecordLayout.h"
20 #include "CodeGenModule.h"
21 #include "clang/CodeGen/CGFunctionInfo.h"
22 #include "clang/Lex/HeaderSearchOptions.h"
23 #include "clang/Lex/PreprocessorOptions.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 &
61 CodeGen::arrangeFreeFunctionCall(CodeGenModule &CGM,
62                                  CanQualType returnType,
63                                  ArrayRef<CanQualType> argTypes,
64                                  FunctionType::ExtInfo info,
65                                  RequiredArgs args) {
66   return CGM.getTypes().arrangeLLVMFunctionInfo(
67       returnType, /*instanceMethod=*/false, /*chainCall=*/false, argTypes,
68       info, {}, args);
69 }
70 
71 llvm::FunctionType *
72 CodeGen::convertFreeFunctionType(CodeGenModule &CGM, const FunctionDecl *FD) {
73   assert(FD != nullptr && "Expected a non-null function declaration!");
74   llvm::Type *T = CGM.getTypes().ConvertType(FD->getType());
75 
76   if (auto FT = dyn_cast<llvm::FunctionType>(T))
77     return FT;
78 
79   return nullptr;
80 }
81 
82 llvm::Type *
83 CodeGen::convertTypeForMemory(CodeGenModule &CGM, QualType T) {
84   return CGM.getTypes().ConvertTypeForMem(T);
85 }
86 
87 unsigned CodeGen::getLLVMFieldNumber(CodeGenModule &CGM,
88                                      const RecordDecl *RD,
89                                      const FieldDecl *FD) {
90   return CGM.getTypes().getCGRecordLayout(RD).getLLVMFieldNo(FD);
91 }
92