1 //==---- CodeGenABITypes.h - 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 // It allows other clients, like LLDB, to determine the LLVM types that are 17 // actually used in function calls, which makes it possible to then determine 18 // the actual ABI locations (e.g. registers, stack locations, etc.) that 19 // these parameters are stored in. 20 // 21 //===----------------------------------------------------------------------===// 22 23 #ifndef LLVM_CLANG_CODEGEN_CODEGENABITYPES_H 24 #define LLVM_CLANG_CODEGEN_CODEGENABITYPES_H 25 26 #include "clang/AST/CanonicalType.h" 27 #include "clang/AST/Type.h" 28 #include "clang/Basic/ABI.h" 29 #include "clang/CodeGen/CGFunctionInfo.h" 30 #include "llvm/IR/BasicBlock.h" 31 32 namespace llvm { 33 class AttrBuilder; 34 class Constant; 35 class Function; 36 class FunctionType; 37 class Type; 38 } 39 40 namespace clang { 41 class CXXConstructorDecl; 42 class CXXDestructorDecl; 43 class CXXRecordDecl; 44 class CXXMethodDecl; 45 class GlobalDecl; 46 class ObjCMethodDecl; 47 class ObjCProtocolDecl; 48 49 namespace CodeGen { 50 class CGFunctionInfo; 51 class CodeGenModule; 52 53 /// Additional implicit arguments to add to a constructor argument list. 54 struct ImplicitCXXConstructorArgs { 55 /// Implicit arguments to add before the explicit arguments, but after the 56 /// `*this` argument (which always comes first). 57 SmallVector<llvm::Value *, 1> Prefix; 58 59 /// Implicit arguments to add after the explicit arguments. 60 SmallVector<llvm::Value *, 1> Suffix; 61 }; 62 63 const CGFunctionInfo &arrangeObjCMessageSendSignature(CodeGenModule &CGM, 64 const ObjCMethodDecl *MD, 65 QualType receiverType); 66 67 const CGFunctionInfo &arrangeFreeFunctionType(CodeGenModule &CGM, 68 CanQual<FunctionProtoType> Ty); 69 70 const CGFunctionInfo &arrangeFreeFunctionType(CodeGenModule &CGM, 71 CanQual<FunctionNoProtoType> Ty); 72 73 const CGFunctionInfo &arrangeCXXMethodType(CodeGenModule &CGM, 74 const CXXRecordDecl *RD, 75 const FunctionProtoType *FTP, 76 const CXXMethodDecl *MD); 77 78 const CGFunctionInfo & 79 arrangeCXXMethodCall(CodeGenModule &CGM, CanQualType returnType, 80 ArrayRef<CanQualType> argTypes, FunctionType::ExtInfo info, 81 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos, 82 RequiredArgs args); 83 84 const CGFunctionInfo &arrangeFreeFunctionCall( 85 CodeGenModule &CGM, CanQualType returnType, ArrayRef<CanQualType> argTypes, 86 FunctionType::ExtInfo info, 87 ArrayRef<FunctionProtoType::ExtParameterInfo> paramInfos, 88 RequiredArgs args); 89 90 // An overload with an empty `paramInfos` 91 inline const CGFunctionInfo & 92 arrangeFreeFunctionCall(CodeGenModule &CGM, CanQualType returnType, 93 ArrayRef<CanQualType> argTypes, 94 FunctionType::ExtInfo info, RequiredArgs args) { 95 return arrangeFreeFunctionCall(CGM, returnType, argTypes, info, {}, args); 96 } 97 98 /// Returns the implicit arguments to add to a complete, non-delegating C++ 99 /// constructor call. 100 ImplicitCXXConstructorArgs 101 getImplicitCXXConstructorArgs(CodeGenModule &CGM, const CXXConstructorDecl *D); 102 103 llvm::Value * 104 getCXXDestructorImplicitParam(CodeGenModule &CGM, llvm::BasicBlock *InsertBlock, 105 llvm::BasicBlock::iterator InsertPoint, 106 const CXXDestructorDecl *D, CXXDtorType Type, 107 bool ForVirtualBase, bool Delegating); 108 109 /// Returns null if the function type is incomplete and can't be lowered. 110 llvm::FunctionType *convertFreeFunctionType(CodeGenModule &CGM, 111 const FunctionDecl *FD); 112 113 llvm::Type *convertTypeForMemory(CodeGenModule &CGM, QualType T); 114 115 /// Given a non-bitfield struct field, return its index within the elements of 116 /// the struct's converted type. The returned index refers to a field number in 117 /// the complete object type which is returned by convertTypeForMemory. FD must 118 /// be a field in RD directly (i.e. not an inherited field). 119 unsigned getLLVMFieldNumber(CodeGenModule &CGM, 120 const RecordDecl *RD, const FieldDecl *FD); 121 122 /// Return a declaration discriminator for the given global decl. 123 uint16_t getPointerAuthDeclDiscriminator(CodeGenModule &CGM, GlobalDecl GD); 124 125 /// Return a type discriminator for the given function type. 126 uint16_t getPointerAuthTypeDiscriminator(CodeGenModule &CGM, 127 QualType FunctionType); 128 129 /// Given the language and code-generation options that Clang was configured 130 /// with, set the default LLVM IR attributes for a function definition. 131 /// The attributes set here are mostly global target-configuration and 132 /// pipeline-configuration options like the target CPU, variant stack 133 /// rules, whether to optimize for size, and so on. This is useful for 134 /// frontends (such as Swift) that generally intend to interoperate with 135 /// C code and rely on Clang's target configuration logic. 136 /// 137 /// As a general rule, this function assumes that meaningful attributes 138 /// haven't already been added to the builder. It won't intentionally 139 /// displace any existing attributes, but it also won't check to avoid 140 /// overwriting them. Callers should generally apply customizations after 141 /// making this call. 142 /// 143 /// This function assumes that the caller is not defining a function that 144 /// requires special no-builtin treatment. 145 void addDefaultFunctionDefinitionAttributes(CodeGenModule &CGM, 146 llvm::AttrBuilder &attrs); 147 148 /// Returns the default constructor for a C struct with non-trivially copyable 149 /// fields, generating it if necessary. The returned function uses the `cdecl` 150 /// calling convention, returns void, and takes a single argument that is a 151 /// pointer to the address of the struct. 152 llvm::Function *getNonTrivialCStructDefaultConstructor(CodeGenModule &GCM, 153 CharUnits DstAlignment, 154 bool IsVolatile, 155 QualType QT); 156 157 /// Returns the copy constructor for a C struct with non-trivially copyable 158 /// fields, generating it if necessary. The returned function uses the `cdecl` 159 /// calling convention, returns void, and takes two arguments: pointers to the 160 /// addresses of the destination and source structs, respectively. 161 llvm::Function *getNonTrivialCStructCopyConstructor(CodeGenModule &CGM, 162 CharUnits DstAlignment, 163 CharUnits SrcAlignment, 164 bool IsVolatile, 165 QualType QT); 166 167 /// Returns the move constructor for a C struct with non-trivially copyable 168 /// fields, generating it if necessary. The returned function uses the `cdecl` 169 /// calling convention, returns void, and takes two arguments: pointers to the 170 /// addresses of the destination and source structs, respectively. 171 llvm::Function *getNonTrivialCStructMoveConstructor(CodeGenModule &CGM, 172 CharUnits DstAlignment, 173 CharUnits SrcAlignment, 174 bool IsVolatile, 175 QualType QT); 176 177 /// Returns the copy assignment operator for a C struct with non-trivially 178 /// copyable fields, generating it if necessary. The returned function uses the 179 /// `cdecl` calling convention, returns void, and takes two arguments: pointers 180 /// to the addresses of the destination and source structs, respectively. 181 llvm::Function *getNonTrivialCStructCopyAssignmentOperator( 182 CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, 183 bool IsVolatile, QualType QT); 184 185 /// Return the move assignment operator for a C struct with non-trivially 186 /// copyable fields, generating it if necessary. The returned function uses the 187 /// `cdecl` calling convention, returns void, and takes two arguments: pointers 188 /// to the addresses of the destination and source structs, respectively. 189 llvm::Function *getNonTrivialCStructMoveAssignmentOperator( 190 CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, 191 bool IsVolatile, QualType QT); 192 193 /// Returns the destructor for a C struct with non-trivially copyable fields, 194 /// generating it if necessary. The returned function uses the `cdecl` calling 195 /// convention, returns void, and takes a single argument that is a pointer to 196 /// the address of the struct. 197 llvm::Function *getNonTrivialCStructDestructor(CodeGenModule &CGM, 198 CharUnits DstAlignment, 199 bool IsVolatile, QualType QT); 200 201 /// Get a pointer to a protocol object for the given declaration, emitting it if 202 /// it hasn't already been emitted in this translation unit. Note that the ABI 203 /// for emitting a protocol reference in code (e.g. for a protocol expression) 204 /// in most runtimes is not as simple as just materializing a pointer to this 205 /// object. 206 llvm::Constant *emitObjCProtocolObject(CodeGenModule &CGM, 207 const ObjCProtocolDecl *p); 208 } // end namespace CodeGen 209 } // end namespace clang 210 211 #endif 212