diff --git a/clang/include/clang/CodeGen/CodeGenABITypes.h b/clang/include/clang/CodeGen/CodeGenABITypes.h --- a/clang/include/clang/CodeGen/CodeGenABITypes.h +++ b/clang/include/clang/CodeGen/CodeGenABITypes.h @@ -30,6 +30,7 @@ namespace llvm { class DataLayout; class Module; + class Function; class FunctionType; class Type; } @@ -83,6 +84,60 @@ unsigned getLLVMFieldNumber(CodeGenModule &CGM, const RecordDecl *RD, const FieldDecl *FD); +/// Returns the default constructor for a C struct with non-trivially copyable +/// fields, generating it if necessary. The returned function uses the `cdecl` +/// calling convention, returns void, and takes a single argument that is a +/// pointer to the address of the struct. +llvm::Function *generateNonTrivialCStructDefaultConstructor(CodeGenModule &GCM, + CharUnits Alignment, + bool IsVolatile, + QualType QT); + +/// Returns the copy constructor for a C struct with non-trivially copyable +/// fields, generating it if necessary. The returned function uses the `cdecl` +/// calling convention, returns void, and takes two arguments: pointers to the +/// addresses of the destination and source structs, respectively. +llvm::Function *generateNonTrivialCStructCopyConstructor(CodeGenModule &CGM, + CharUnits DstAlignment, + CharUnits SrcAlignment, + bool IsVolatile, + QualType QT); + +/// Returns the move constructor for a C struct with non-trivially copyable +/// fields, generating it if necessary. The returned function uses the `cdecl` +/// calling convention, returns void, and takes two arguments: pointers to the +/// addresses of the destination and source structs, respectively. +llvm::Function *generateNonTrivialCStructMoveConstructor(CodeGenModule &CGM, + CharUnits DstAlignment, + CharUnits SrcAlignment, + bool IsVolatile, + QualType QT); + +/// Returns the copy assignment operator for a C struct with non-trivially +/// copyable fields, generating it if necessary. The returned function uses the +/// `cdecl` calling convention, returns void, and takes two arguments: pointers +/// to the addresses of the destination and source structs, respectively. +llvm::Function *generateNonTrivialCStructCopyAssignmentOperator( + CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, + bool IsVolatile, QualType QT); + +/// Return the move assignment operator for a C struct with non-trivially +/// copyable fields, generating it if necessary. The returned function uses the +/// `cdecl` calling convention, returns void, and takes two arguments: pointers +/// to the addresses of the destination and source structs, respectively. +llvm::Function *generateNonTrivialCStructMoveAssignmentOperator( + CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, + bool IsVolatile, QualType QT); + +/// Returns the destructor for a C struct with non-trivially copyable fields, +/// generating it if necessary. The returned function uses the `cdecl` calling +/// convention, returns void, and takes a single argument that is a pointer to +/// the address of the struct. +llvm::Function *generateNonTrivialCStructDestructor(CodeGenModule &CGM, + CharUnits Alignment, + bool IsVolatile, + QualType QT); + } // end namespace CodeGen } // end namespace clang diff --git a/clang/lib/CodeGen/CGNonTrivialStruct.cpp b/clang/lib/CodeGen/CGNonTrivialStruct.cpp --- a/clang/lib/CodeGen/CGNonTrivialStruct.cpp +++ b/clang/lib/CodeGen/CGNonTrivialStruct.cpp @@ -14,6 +14,7 @@ #include "CodeGenFunction.h" #include "CodeGenModule.h" #include "clang/AST/NonTrivialTypeVisitor.h" +#include "clang/CodeGen/CodeGenABITypes.h" #include "llvm/Support/ScopedPrinter.h" #include @@ -822,6 +823,30 @@ Gen.callFunc(FuncName, QT, Addrs, CGF); } +template std::array createNullAddressArray(); + +template <> std::array createNullAddressArray() { + return std::array({Address(nullptr, CharUnits::Zero())}); +} + +template <> std::array createNullAddressArray() { + return std::array({Address(nullptr, CharUnits::Zero()), + Address(nullptr, CharUnits::Zero())}); +} + +template +static llvm::Function * +generateSpecialFunction(G &&Gen, StringRef FuncName, QualType QT, + bool IsVolatile, std::array Alignments, + CodeGenModule &CGM) { + QT = IsVolatile ? QT.withVolatile() : QT; + // The following call requires an array of addresses as arguments, but doesn't + // actually use them (it overwrites them with the addresses of the arguments + // of the created function). + return Gen.getFunction(FuncName, QT, createNullAddressArray(), Alignments, + CGM); +} + // Functions to emit calls to the special functions of a non-trivial C struct. void CodeGenFunction::callCStructDefaultConstructor(LValue Dst) { bool IsVolatile = Dst.isVolatile(); @@ -907,3 +932,70 @@ callSpecialFunction(GenMoveAssignment(getContext()), FuncName, QT, IsVolatile, *this, std::array({{DstPtr, SrcPtr}})); } + +llvm::Function *clang::CodeGen::generateNonTrivialCStructDefaultConstructor( + CodeGenModule &CGM, CharUnits DstAlignment, bool IsVolatile, QualType QT) { + ASTContext &Ctx = CGM.getContext(); + GenDefaultInitializeFuncName GenName(DstAlignment, Ctx); + std::string FuncName = GenName.getName(QT, IsVolatile); + return generateSpecialFunction( + GenDefaultInitialize(Ctx), FuncName, QT, IsVolatile, + std::array({{DstAlignment}}), CGM); +} + +llvm::Function *clang::CodeGen::generateNonTrivialCStructCopyConstructor( + CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, + bool IsVolatile, QualType QT) { + ASTContext &Ctx = CGM.getContext(); + GenBinaryFuncName GenName("__copy_constructor_", DstAlignment, + SrcAlignment, Ctx); + std::string FuncName = GenName.getName(QT, IsVolatile); + return generateSpecialFunction( + GenCopyConstructor(Ctx), FuncName, QT, IsVolatile, + std::array({{DstAlignment, SrcAlignment}}), CGM); +} + +llvm::Function *clang::CodeGen::generateNonTrivialCStructMoveConstructor( + CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, + bool IsVolatile, QualType QT) { + ASTContext &Ctx = CGM.getContext(); + GenBinaryFuncName GenName("__move_constructor_", DstAlignment, + SrcAlignment, Ctx); + std::string FuncName = GenName.getName(QT, IsVolatile); + return generateSpecialFunction( + GenMoveConstructor(Ctx), FuncName, QT, IsVolatile, + std::array({{DstAlignment, SrcAlignment}}), CGM); +} + +llvm::Function *clang::CodeGen::generateNonTrivialCStructCopyAssignmentOperator( + CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, + bool IsVolatile, QualType QT) { + ASTContext &Ctx = CGM.getContext(); + GenBinaryFuncName GenName("__copy_assignment_", DstAlignment, + SrcAlignment, Ctx); + std::string FuncName = GenName.getName(QT, IsVolatile); + return generateSpecialFunction( + GenCopyAssignment(Ctx), FuncName, QT, IsVolatile, + std::array({{DstAlignment, SrcAlignment}}), CGM); +} + +llvm::Function *clang::CodeGen::generateNonTrivialCStructMoveAssignmentOperator( + CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, + bool IsVolatile, QualType QT) { + ASTContext &Ctx = CGM.getContext(); + GenBinaryFuncName GenName("__move_assignment_", DstAlignment, + SrcAlignment, Ctx); + std::string FuncName = GenName.getName(QT, IsVolatile); + return generateSpecialFunction( + GenMoveAssignment(Ctx), FuncName, QT, IsVolatile, + std::array({{DstAlignment, SrcAlignment}}), CGM); +} + +llvm::Function *clang::CodeGen::generateNonTrivialCStructDestructor( + CodeGenModule &CGM, CharUnits Alignment, bool IsVolatile, QualType QT) { + ASTContext &Ctx = CGM.getContext(); + GenDestructorFuncName GenName("__destructor_", Alignment, Ctx); + std::string FuncName = GenName.getName(QT, IsVolatile); + return generateSpecialFunction(GenDestructor(Ctx), FuncName, QT, IsVolatile, + std::array({{Alignment}}), CGM); +}