allevato created this revision.
allevato added a reviewer: rjmccall.
Herald added subscribers: cfe-commits, jdoerfert.
Herald added a project: clang.

This change exposes the helper functions that generate ctors/dtors
for non-trivial structs so that they can be called from the Swift
compiler (see https://github.com/apple/swift/pull/23405) to
enable it to import such C records.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D60161

Files:
  clang/include/clang/CodeGen/CodeGenABITypes.h
  clang/lib/CodeGen/CGNonTrivialStruct.cpp

Index: clang/lib/CodeGen/CGNonTrivialStruct.cpp
===================================================================
--- clang/lib/CodeGen/CGNonTrivialStruct.cpp
+++ 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 <array>
 
@@ -907,3 +908,71 @@
   callSpecialFunction(GenMoveAssignment(getContext()), FuncName, QT, IsVolatile,
                       *this, std::array<Address, 2>({{DstPtr, SrcPtr}}));
 }
+
+void clang::CodeGen::callNonTrivialCStructDefaultConstructor(
+    CodeGenModule &CGM, llvm::BasicBlock *BB,
+    llvm::BasicBlock::iterator InsertPoint, llvm::Value *Dst,
+    CharUnits DstAlignment, QualType QT) {
+  CodeGenFunction CGF(CGM, true);
+  CGF.Builder.SetInsertPoint(BB, InsertPoint);
+  LValue DstLValue = CGF.MakeAddrLValue(Dst, QT, DstAlignment);
+  CGF.callCStructDefaultConstructor(DstLValue);
+}
+
+void clang::CodeGen::callNonTrivialCStructCopyConstructor(
+    CodeGenModule &CGM, llvm::BasicBlock *BB,
+    llvm::BasicBlock::iterator InsertPoint, llvm::Value *Dst,
+    CharUnits DstAlignment, llvm::Value *Src, CharUnits SrcAlignment,
+    QualType QT) {
+  CodeGenFunction CGF(CGM, true);
+  CGF.Builder.SetInsertPoint(BB, InsertPoint);
+  LValue DstLValue = CGF.MakeAddrLValue(Dst, QT, DstAlignment);
+  LValue SrcLValue = CGF.MakeAddrLValue(Src, QT, SrcAlignment);
+  CGF.callCStructCopyConstructor(DstLValue, SrcLValue);
+}
+
+void clang::CodeGen::callNonTrivialCStructMoveConstructor(
+    CodeGenModule &CGM, llvm::BasicBlock *BB,
+    llvm::BasicBlock::iterator InsertPoint, llvm::Value *Dst,
+    CharUnits DstAlignment, llvm::Value *Src, CharUnits SrcAlignment,
+    QualType QT) {
+  CodeGenFunction CGF(CGM, true);
+  CGF.Builder.SetInsertPoint(BB, InsertPoint);
+  LValue DstLValue = CGF.MakeAddrLValue(Dst, QT, DstAlignment);
+  LValue SrcLValue = CGF.MakeAddrLValue(Src, QT, SrcAlignment);
+  CGF.callCStructMoveConstructor(DstLValue, SrcLValue);
+}
+
+void clang::CodeGen::callNonTrivialCStructCopyAssignmentOperator(
+    CodeGenModule &CGM, llvm::BasicBlock *BB,
+    llvm::BasicBlock::iterator InsertPoint, llvm::Value *Dst,
+    CharUnits DstAlignment, llvm::Value *Src, CharUnits SrcAlignment,
+    QualType QT) {
+  CodeGenFunction CGF(CGM, true);
+  CGF.Builder.SetInsertPoint(BB, InsertPoint);
+  LValue DstLValue = CGF.MakeAddrLValue(Dst, QT, DstAlignment);
+  LValue SrcLValue = CGF.MakeAddrLValue(Src, QT, SrcAlignment);
+  CGF.callCStructCopyAssignmentOperator(DstLValue, SrcLValue);
+}
+
+void clang::CodeGen::callNonTrivialCStructMoveAssignmentOperator(
+    CodeGenModule &CGM, llvm::BasicBlock *BB,
+    llvm::BasicBlock::iterator InsertPoint, llvm::Value *Dst,
+    CharUnits DstAlignment, llvm::Value *Src, CharUnits SrcAlignment,
+    QualType QT) {
+  CodeGenFunction CGF(CGM, true);
+  CGF.Builder.SetInsertPoint(BB, InsertPoint);
+  LValue DstLValue = CGF.MakeAddrLValue(Dst, QT, DstAlignment);
+  LValue SrcLValue = CGF.MakeAddrLValue(Src, QT, SrcAlignment);
+  CGF.callCStructMoveAssignmentOperator(DstLValue, SrcLValue);
+}
+
+void clang::CodeGen::callNonTrivialCStructDestructor(
+    CodeGenModule &CGM, llvm::BasicBlock *BB,
+    llvm::BasicBlock::iterator InsertPoint, llvm::Value *Dst,
+    CharUnits Alignment, QualType QT) {
+  CodeGenFunction CGF(CGM, true);
+  CGF.Builder.SetInsertPoint(BB, InsertPoint);
+  LValue lvalue = CGF.MakeAddrLValue(Dst, QT, Alignment);
+  CGF.callCStructDestructor(lvalue);
+}
Index: clang/include/clang/CodeGen/CodeGenABITypes.h
===================================================================
--- clang/include/clang/CodeGen/CodeGenABITypes.h
+++ clang/include/clang/CodeGen/CodeGenABITypes.h
@@ -26,12 +26,14 @@
 #include "clang/AST/CanonicalType.h"
 #include "clang/AST/Type.h"
 #include "clang/CodeGen/CGFunctionInfo.h"
+#include "llvm/IR/BasicBlock.h"
 
 namespace llvm {
   class DataLayout;
   class Module;
   class FunctionType;
   class Type;
+  class Value;
 }
 
 namespace clang {
@@ -83,6 +85,52 @@
 unsigned getLLVMFieldNumber(CodeGenModule &CGM,
                             const RecordDecl *RD, const FieldDecl *FD);
 
+/// Generates a call to the default constructor for a C struct with
+/// non-trivially copyable fields.
+void callNonTrivialCStructDefaultConstructor(
+    CodeGenModule &GCM, llvm::BasicBlock *BB,
+    llvm::BasicBlock::iterator InsertPoint, llvm::Value *Dst,
+    CharUnits Alignment, QualType QT);
+
+/// Generates a call to the copy constructor for a C struct with non-trivially
+/// copyable fields.
+void callNonTrivialCStructCopyConstructor(
+    CodeGenModule &CGM, llvm::BasicBlock *BB,
+    llvm::BasicBlock::iterator InsertPoint, llvm::Value *Dst,
+    CharUnits DstAlignment, llvm::Value *Src, CharUnits SrcAlignment,
+    QualType QT);
+
+/// Generates a call to the move constructor for a C struct with non-trivially
+/// copyable fields.
+void callNonTrivialCStructMoveConstructor(
+    CodeGenModule &CGM, llvm::BasicBlock *BB,
+    llvm::BasicBlock::iterator InsertPoint, llvm::Value *Dst,
+    CharUnits DstAlignment, llvm::Value *Src, CharUnits SrcAlignment,
+    QualType QT);
+
+/// Generates a call to the copy assignment operator for a C struct with
+/// non-trivially copyable fields.
+void callNonTrivialCStructCopyAssignmentOperator(
+    CodeGenModule &CGM, llvm::BasicBlock *BB,
+    llvm::BasicBlock::iterator InsertPoint, llvm::Value *Dst,
+    CharUnits DstAlignment, llvm::Value *Src, CharUnits SrcAlignment,
+    QualType QT);
+
+/// Generates a call to the move assignment operator for a C struct with
+/// non-trivially copyable fields.
+void callNonTrivialCStructMoveAssignmentOperator(
+    CodeGenModule &CGM, llvm::BasicBlock *BB,
+    llvm::BasicBlock::iterator InsertPoint, llvm::Value *Dst,
+    CharUnits DstAlignment, llvm::Value *Src, CharUnits SrcAlignment,
+    QualType QT);
+
+/// Generates a call to the destructor for a C struct with non-trivially
+/// copyable fields.
+void callNonTrivialCStructDestructor(CodeGenModule &CGM, llvm::BasicBlock *BB,
+                                     llvm::BasicBlock::iterator InsertPoint,
+                                     llvm::Value *Dst, CharUnits Alignment,
+                                     QualType QT);
+
 }  // end namespace CodeGen
 }  // end namespace clang
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to