1a8e7df36SMark Lacey //==--- CodeGenABITypes.cpp - Convert Clang types to LLVM types for ABI ----==//
2a8e7df36SMark Lacey //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a8e7df36SMark Lacey //
7a8e7df36SMark Lacey //===----------------------------------------------------------------------===//
8a8e7df36SMark Lacey //
9a8e7df36SMark Lacey // CodeGenABITypes is a simple interface for getting LLVM types for
10a8e7df36SMark Lacey // the parameters and the return value of a function given the Clang
11a8e7df36SMark Lacey // types.
12a8e7df36SMark Lacey //
13a8e7df36SMark Lacey // The class is implemented as a public wrapper around the private
14a8e7df36SMark Lacey // CodeGenTypes class in lib/CodeGen.
15a8e7df36SMark Lacey //
16a8e7df36SMark Lacey //===----------------------------------------------------------------------===//
17a8e7df36SMark Lacey 
18a8e7df36SMark Lacey #include "clang/CodeGen/CodeGenABITypes.h"
194c09289fSMartin Böhme #include "CGCXXABI.h"
20f3d9b488SAdrian Prantl #include "CGRecordLayout.h"
214c09289fSMartin Böhme #include "CodeGenFunction.h"
225553d0d4SChandler Carruth #include "CodeGenModule.h"
23a8e7df36SMark Lacey #include "clang/CodeGen/CGFunctionInfo.h"
24e74f525bSAdrian Prantl #include "clang/Lex/HeaderSearchOptions.h"
25e74f525bSAdrian Prantl #include "clang/Lex/PreprocessorOptions.h"
26a8e7df36SMark Lacey 
27a8e7df36SMark Lacey using namespace clang;
28a8e7df36SMark Lacey using namespace CodeGen;
29a8e7df36SMark Lacey 
addDefaultFunctionDefinitionAttributes(CodeGenModule & CGM,llvm::AttrBuilder & attrs)3032870a84SJohn McCall void CodeGen::addDefaultFunctionDefinitionAttributes(CodeGenModule &CGM,
3132870a84SJohn McCall                                                      llvm::AttrBuilder &attrs) {
3232870a84SJohn McCall   CGM.addDefaultFunctionDefinitionAttributes(attrs);
3332870a84SJohn McCall }
3432870a84SJohn McCall 
351b7dd8d7SBenjamin Kramer const CGFunctionInfo &
arrangeObjCMessageSendSignature(CodeGenModule & CGM,const ObjCMethodDecl * MD,QualType receiverType)366909fee7SJohn McCall CodeGen::arrangeObjCMessageSendSignature(CodeGenModule &CGM,
376909fee7SJohn McCall                                          const ObjCMethodDecl *MD,
381b7dd8d7SBenjamin Kramer                                          QualType receiverType) {
396909fee7SJohn McCall   return CGM.getTypes().arrangeObjCMessageSendSignature(MD, receiverType);
401b7dd8d7SBenjamin Kramer }
411b7dd8d7SBenjamin Kramer 
421b7dd8d7SBenjamin Kramer const CGFunctionInfo &
arrangeFreeFunctionType(CodeGenModule & CGM,CanQual<FunctionProtoType> Ty)436909fee7SJohn McCall CodeGen::arrangeFreeFunctionType(CodeGenModule &CGM,
44916db651SJames Y Knight                                  CanQual<FunctionProtoType> Ty) {
45916db651SJames Y Knight   return CGM.getTypes().arrangeFreeFunctionType(Ty);
461b7dd8d7SBenjamin Kramer }
471b7dd8d7SBenjamin Kramer 
481b7dd8d7SBenjamin Kramer const CGFunctionInfo &
arrangeFreeFunctionType(CodeGenModule & CGM,CanQual<FunctionNoProtoType> Ty)496909fee7SJohn McCall CodeGen::arrangeFreeFunctionType(CodeGenModule &CGM,
506909fee7SJohn McCall                                  CanQual<FunctionNoProtoType> Ty) {
516909fee7SJohn McCall   return CGM.getTypes().arrangeFreeFunctionType(Ty);
521b7dd8d7SBenjamin Kramer }
531b7dd8d7SBenjamin Kramer 
541b7dd8d7SBenjamin Kramer const CGFunctionInfo &
arrangeCXXMethodType(CodeGenModule & CGM,const CXXRecordDecl * RD,const FunctionProtoType * FTP,const CXXMethodDecl * MD)556909fee7SJohn McCall CodeGen::arrangeCXXMethodType(CodeGenModule &CGM,
566909fee7SJohn McCall                               const CXXRecordDecl *RD,
573e3bb95bSGeorge Burgess IV                               const FunctionProtoType *FTP,
583e3bb95bSGeorge Burgess IV                               const CXXMethodDecl *MD) {
596909fee7SJohn McCall   return CGM.getTypes().arrangeCXXMethodType(RD, FTP, MD);
601b7dd8d7SBenjamin Kramer }
611b7dd8d7SBenjamin Kramer 
626909fee7SJohn McCall const CGFunctionInfo &
arrangeFreeFunctionCall(CodeGenModule & CGM,CanQualType returnType,ArrayRef<CanQualType> argTypes,FunctionType::ExtInfo info,RequiredArgs args)636909fee7SJohn McCall CodeGen::arrangeFreeFunctionCall(CodeGenModule &CGM,
646909fee7SJohn McCall                                  CanQualType returnType,
656909fee7SJohn McCall                                  ArrayRef<CanQualType> argTypes,
666909fee7SJohn McCall                                  FunctionType::ExtInfo info,
676909fee7SJohn McCall                                  RequiredArgs args) {
686909fee7SJohn McCall   return CGM.getTypes().arrangeLLVMFunctionInfo(
6949a3ad21SRui Ueyama       returnType, /*instanceMethod=*/false, /*chainCall=*/false, argTypes,
70c56a8b32SJohn McCall       info, {}, args);
711b7dd8d7SBenjamin Kramer }
720a2cde36SJohn McCall 
734c09289fSMartin Böhme ImplicitCXXConstructorArgs
getImplicitCXXConstructorArgs(CodeGenModule & CGM,const CXXConstructorDecl * D)744c09289fSMartin Böhme CodeGen::getImplicitCXXConstructorArgs(CodeGenModule &CGM,
754c09289fSMartin Böhme                                        const CXXConstructorDecl *D) {
764c09289fSMartin Böhme   // We have to create a dummy CodeGenFunction here to pass to
774c09289fSMartin Böhme   // getImplicitConstructorArgs(). In some cases (base and delegating
784c09289fSMartin Böhme   // constructor calls), getImplicitConstructorArgs() can reach into the
794c09289fSMartin Böhme   // CodeGenFunction to find parameters of the calling constructor to pass on to
804c09289fSMartin Böhme   // the called constructor, but that can't happen here because we're asking for
814c09289fSMartin Böhme   // the args for a complete, non-delegating constructor call.
824c09289fSMartin Böhme   CodeGenFunction CGF(CGM, /* suppressNewContext= */ true);
834c09289fSMartin Böhme   CGCXXABI::AddedStructorArgs addedArgs =
844c09289fSMartin Böhme       CGM.getCXXABI().getImplicitConstructorArgs(CGF, D, Ctor_Complete,
854c09289fSMartin Böhme                                                  /* ForVirtualBase= */ false,
864c09289fSMartin Böhme                                                  /* Delegating= */ false);
874c09289fSMartin Böhme   ImplicitCXXConstructorArgs implicitArgs;
884c09289fSMartin Böhme   for (const auto &arg : addedArgs.Prefix) {
894c09289fSMartin Böhme     implicitArgs.Prefix.push_back(arg.Value);
904c09289fSMartin Böhme   }
914c09289fSMartin Böhme   for (const auto &arg : addedArgs.Suffix) {
924c09289fSMartin Böhme     implicitArgs.Suffix.push_back(arg.Value);
934c09289fSMartin Böhme   }
944c09289fSMartin Böhme   return implicitArgs;
954c09289fSMartin Böhme }
964c09289fSMartin Böhme 
970a2cde36SJohn McCall llvm::FunctionType *
convertFreeFunctionType(CodeGenModule & CGM,const FunctionDecl * FD)980a2cde36SJohn McCall CodeGen::convertFreeFunctionType(CodeGenModule &CGM, const FunctionDecl *FD) {
990a2cde36SJohn McCall   assert(FD != nullptr && "Expected a non-null function declaration!");
100916db651SJames Y Knight   llvm::Type *T = CGM.getTypes().ConvertType(FD->getType());
1010a2cde36SJohn McCall 
1020a2cde36SJohn McCall   if (auto FT = dyn_cast<llvm::FunctionType>(T))
1030a2cde36SJohn McCall     return FT;
1040a2cde36SJohn McCall 
1050a2cde36SJohn McCall   return nullptr;
1060a2cde36SJohn McCall }
1070a2cde36SJohn McCall 
1080a2cde36SJohn McCall llvm::Type *
convertTypeForMemory(CodeGenModule & CGM,QualType T)1090a2cde36SJohn McCall CodeGen::convertTypeForMemory(CodeGenModule &CGM, QualType T) {
1100a2cde36SJohn McCall   return CGM.getTypes().ConvertTypeForMem(T);
1110a2cde36SJohn McCall }
112f3d9b488SAdrian Prantl 
getLLVMFieldNumber(CodeGenModule & CGM,const RecordDecl * RD,const FieldDecl * FD)113f3d9b488SAdrian Prantl unsigned CodeGen::getLLVMFieldNumber(CodeGenModule &CGM,
114f3d9b488SAdrian Prantl                                      const RecordDecl *RD,
115f3d9b488SAdrian Prantl                                      const FieldDecl *FD) {
116f3d9b488SAdrian Prantl   return CGM.getTypes().getCGRecordLayout(RD).getLLVMFieldNo(FD);
117f3d9b488SAdrian Prantl }
118*e7c5da57Szoecarver 
getCXXDestructorImplicitParam(CodeGenModule & CGM,llvm::BasicBlock * InsertBlock,llvm::BasicBlock::iterator InsertPoint,const CXXDestructorDecl * D,CXXDtorType Type,bool ForVirtualBase,bool Delegating)119*e7c5da57Szoecarver llvm::Value *CodeGen::getCXXDestructorImplicitParam(
120*e7c5da57Szoecarver     CodeGenModule &CGM, llvm::BasicBlock *InsertBlock,
121*e7c5da57Szoecarver     llvm::BasicBlock::iterator InsertPoint, const CXXDestructorDecl *D,
122*e7c5da57Szoecarver     CXXDtorType Type, bool ForVirtualBase, bool Delegating) {
123*e7c5da57Szoecarver   CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
124*e7c5da57Szoecarver   CGF.CurCodeDecl = D;
125*e7c5da57Szoecarver   CGF.CurFuncDecl = D;
126*e7c5da57Szoecarver   CGF.CurFn = InsertBlock->getParent();
127*e7c5da57Szoecarver   CGF.Builder.SetInsertPoint(InsertBlock, InsertPoint);
128*e7c5da57Szoecarver   return CGM.getCXXABI().getCXXDestructorImplicitParam(
129*e7c5da57Szoecarver       CGF, D, Type, ForVirtualBase, Delegating);
130*e7c5da57Szoecarver }
131