Skip to content

Commit 8e9c7e3

Browse files
committed
XXX
1 parent d1547a3 commit 8e9c7e3

File tree

2 files changed

+96
-81
lines changed

2 files changed

+96
-81
lines changed

Sources/LLVM/DIBuilder.swift

Lines changed: 95 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if !NO_SWIFTPM
1+
#if SWIFT_PACKAGE
22
import cllvm
33
#endif
44

@@ -584,8 +584,11 @@ public final class DIBuilder {
584584

585585
/// Creates a new DebugLocation that describes a source location.
586586
public func createDebugLocation(at loc : (line: Int, column: Int), in scope: Scope, inlinedAt: Scope? = nil) -> DebugLocation {
587-
guard let loc = LLVMDIBuilderCreateDebugLocation(self.module.context.llvm, UInt32(loc.line), UInt32(loc.column), scope.llvm, inlinedAt?.llvm) else {
588-
fatalError()
587+
guard let loc = LLVMDIBuilderCreateDebugLocation(
588+
self.module.context.llvm, UInt32(loc.line), UInt32(loc.column),
589+
scope.llvm, inlinedAt?.llvm
590+
) else {
591+
fatalError("Failed to allocate metadata for a debug location")
589592
}
590593
return DebugLocation(llvm: loc)
591594
}
@@ -610,14 +613,18 @@ public final class DIBuilder {
610613
}
611614

612615
/// Create a file descriptor to hold debugging information for a file.
613-
public func createFile(named name: String, in directory: String) -> FileMetadata? {
614-
guard let file = LLVMDIBuilderCreateFile(self.llvm, name, directory) else { return nil }
616+
public func createFile(named name: String, in directory: String) -> FileMetadata {
617+
guard let file = LLVMDIBuilderCreateFile(self.llvm, name, directory) else {
618+
fatalError("Failed to allocate metadata for a file")
619+
}
615620
return FileMetadata(llvm: file)
616621
}
617622

618623
/// Create debugging information temporary entry for a macro file.
619-
public func createTemporaryMacroFile(at line: UInt32, in file: FileMetadata, withParent parent: FileMetadata? = nil) -> FileMetadata? {
620-
guard let file = LLVMDIBuilderCreateTempMacroFile(self.llvm, parent?.llvm, line, file.llvm) else { return nil }
624+
public func createTemporaryMacroFile(at line: Int, in file: FileMetadata, withParent parent: FileMetadata? = nil) -> FileMetadata {
625+
guard let file = LLVMDIBuilderCreateTempMacroFile(self.llvm, parent?.llvm, UInt32(line), file.llvm) else {
626+
fatalError("Failed to allocate metadata for a temporary macro file")
627+
}
621628
return FileMetadata(llvm: file)
622629
}
623630

@@ -654,17 +661,68 @@ public final class DIBuilder {
654661
isOptimized ? 1 : 0,
655662
body.llvm, nil, 0, nil
656663
) else {
657-
fatalError()
664+
fatalError("Failed to allocate metadata for a function")
658665
}
659666
return Scope(llvm: gn)
660667
}
661668
}
662669

670+
/// Identical to LLVMDIBuilderCreateFunction,
671+
/// except that the resulting DbgNode is meant to be RAUWed.
672+
public func createTemporaryFunctionForwardDeclaration(
673+
in scope: Scope, named name: String, linkageName mangled: String,
674+
in file: FileMetadata, at line: Int, body: Function,
675+
isLocal: Bool, isDefinition: Bool,
676+
scopeLine: Int,
677+
flags: DebugInfoFlags = .zero,
678+
isOptimized: Bool = false
679+
) -> Scope {
680+
// HACK: Functions come in as function pointers.
681+
let bodyTy: FunctionType
682+
if let pfty = body.type as? PointerType, pfty.pointee is FunctionType {
683+
bodyTy = pfty.pointee as! FunctionType
684+
} else {
685+
bodyTy = body.type as! FunctionType
686+
}
687+
var elemMetadata = bodyTy.argTypes.map { (ty) -> LLVMMetadataRef? in
688+
if let elemDI = ty as? DIType {
689+
return elemDI.asMetadata(with: self)
690+
} else {
691+
return LLVMDIBuilderCreateUnspecifiedType(self.llvm, "<unspecified>")
692+
}
693+
}
694+
return elemMetadata.withUnsafeMutableBufferPointer { buf in
695+
let fnTy = LLVMDIBuilderCreateSubroutineType(self.llvm, file.llvm, buf.baseAddress!, UInt32(elemMetadata.count))
696+
guard let tffd = LLVMDIBuilderCreateTempFunctionFwdDecl(
697+
self.llvm, scope.llvm, name, mangled, file.llvm, UInt32(line), fnTy,
698+
isLocal ? 1 : 0,
699+
isDefinition ? 1 : 0,
700+
UInt32(scopeLine), flags.llvm,
701+
isOptimized ? 1 : 0,
702+
body.llvm, nil, 0, nil
703+
) else {
704+
fatalError("Failed to allocate metadata for a temporary forward declaration")
705+
}
706+
return Scope(llvm: tffd)
707+
}
708+
}
709+
710+
711+
/// This creates a descriptor for a lexical block with a new file
712+
/// attached. This merely extends the existing
713+
/// lexical block as it crosses a file.
714+
public func createLexicalBlockFile(in scope: Scope, in file: FileMetadata) -> Scope {
715+
guard let scope = LLVMDIBuilderCreateLexicalBlockFile(self.llvm, scope.llvm, file.llvm) else {
716+
fatalError("Failed to allocate metadata for a lexical block file extension")
717+
}
718+
return Scope(llvm: scope)
719+
}
720+
663721
/// This creates a descriptor for a lexical block with the
664722
/// specified parent context.
665723
public func createLexicalBlock(in scope: Scope, in file: FileMetadata, at loc : (line: Int, column: Int)) -> Scope {
666724
guard let scope = LLVMDIBuilderCreateLexicalBlock(self.llvm, scope.llvm, file.llvm, UInt32(loc.line), UInt32(loc.column)) else {
667-
fatalError()
725+
fatalError("Failed to allocate metadata for a lexical block")
668726
}
669727
return Scope(llvm: scope)
670728
}
@@ -673,35 +731,56 @@ public final class DIBuilder {
673731
/// parent scope.
674732
public func createModule(in scope: Scope, named name: String, macros: [String], include ipath: String, _ isysRoot: String) -> DIModule {
675733
guard let module = LLVMDIBuilderCreateModule(self.llvm, scope.llvm, name, macros.joined(separator: " "), ipath, isysRoot) else {
676-
fatalError()
734+
fatalError("Failed to allocate metadata for a module")
677735
}
678736
return DIModule(llvm: module)
679737
}
680738

681739
/// Create debugging information entry for a macro.
682-
public func createMacro(in parent: FileMetadata, at line: Int, type macroTy: MachineInfoRecordType, name: String) -> Macro! {
740+
public func createMacro(in parent: FileMetadata, at line: Int, type macroTy: MachineInfoRecordType, name: String) -> Macro {
683741
guard let macro = LLVMDIBuilderCreateMacro(self.llvm, parent.asMetadata(), UInt32(line), macroTy.rawValue, name) else {
684-
fatalError()
742+
fatalError("Failed to allocate metadata for a macro")
685743
}
686744
return Macro(llvm: macro)
687745
}
688746

689747
/// Create debugging information entry for a typedef.
690748
public func createTypedef(of type: DIType, named name: String, in file: FileMetadata, at line: Int, in scope: Scope) -> DIType {
691749
guard let typedef = LLVMDIBuilderCreateTypedef(self.llvm, type.asMetadata(with: self), name, file.llvm, UInt32(line), scope.llvm) else {
692-
fatalError()
750+
fatalError("Failed to allocate metadata for a typedef")
693751
}
694752
return AnyDIType(llvm: typedef)
695753
}
696754

697755
/// Create a new DIType with "artificial" flag set.
698756
public func createArtificialType(of type: DIType) -> DIType {
699757
guard let ty = LLVMDIBuilderCreateArtificialType(self.llvm, type.asMetadata(with: self)) else {
700-
fatalError()
758+
fatalError("Failed to allocate metadata for an artificial type")
701759
}
702760
return AnyDIType(llvm: ty)
703761
}
704762

763+
/// Create a permanent forward-declared type.
764+
public func createForwardDecl(in scope: Scope, named name: String, in file: FileMetadata, at line: Int, tag: Int) -> DIType {
765+
guard let fd = LLVMDIBuilderCreateForwardDecl(self.llvm, UInt32(tag), name, scope.llvm, file.llvm, UInt32(line)) else {
766+
fatalError("Failed to allocate metadata for a forward declaration")
767+
}
768+
return AnyDIType(llvm: fd)
769+
}
770+
771+
/// Create a new descriptor for a parameter variable.
772+
public func createParameterVariable(in scope: Scope, named name: String, at index: UInt32, in file: FileMetadata, at line: Int, type: DIType, _ alwaysPreserve: Bool = true, flags: DebugInfoFlags = .zero) -> VariableMetadata {
773+
guard let pv = LLVMDIBuilderCreateParameterVariable(
774+
self.llvm, scope.llvm, name, UInt32(index), file.llvm, UInt32(line),
775+
type.asMetadata(with: self),
776+
alwaysPreserve ? 1 : 0, flags.llvm
777+
) else {
778+
fatalError("Failed to allocate metadata for a parameter variable")
779+
}
780+
return VariableMetadata(llvm: pv)
781+
}
782+
783+
705784
/// Create a new descriptor for an auto variable. This is a local variable
706785
/// that is not a subprogram parameter.
707786
public func createAutoVariable(in scope: Scope, named name: String, in file: FileMetadata, at line: Int, type: DIType, alwaysPreserve: Bool, flags: DebugInfoFlags, aligned alignInBits: Int) -> VariableMetadata {
@@ -718,7 +797,7 @@ public final class DIBuilder {
718797
}
719798

720799
/// Create debugging information entry for a struct.
721-
public func createStructType(for type: StructType, in scope: Scope, in file: FileMetadata, at line: Int, flags: DebugInfoFlags = DebugInfoFlags.zero, uniqueID: String) -> Metadata {
800+
public func createStructType(for type: StructType, in scope: Scope, in file: FileMetadata, at line: Int, flags: DebugInfoFlags = DebugInfoFlags.zero, uniqueID: String) -> DIType {
722801
var elemMetadata = type.elementTypes.map { (ty) -> LLVMMetadataRef? in
723802
if let elemDI = ty as? DIType {
724803
return elemDI.asMetadata(with: self)
@@ -728,7 +807,7 @@ public final class DIBuilder {
728807
}
729808

730809
return elemMetadata.withUnsafeMutableBufferPointer { buf in
731-
return AnyMetadata(llvm:
810+
return AnyDIType(llvm:
732811
LLVMDIBuilderCreateStructType(
733812
self.llvm, scope.llvm, type.name, file.llvm, UInt32(line),
734813
UInt64(self.module.dataLayout.sizeOfTypeInBits(type)),
@@ -752,10 +831,6 @@ public final class DIBuilder {
752831

753832
/*
754833

755-
/// Identical to LLVMDIBuilderCreateFunction,
756-
/// except that the resulting DbgNode is meant to be RAUWed.
757-
public func LLVMDIBuilderCreateTempFunctionFwdDecl(_ Builder: LLVMDIBuilderRef!, _ Scope: LLVMMetadataRef!, _ Name: UnsafePointer<Int8>!, _ LinkageName: UnsafePointer<Int8>!, _ File: LLVMMetadataRef!, _ LineNo: UInt32, _ Ty: LLVMMetadataRef!, _ IsLocalToUnit: UInt8, _ IsDefinition: UInt8, _ ScopeLine: UInt32, _ Flags: LLVMDIFlags, _ IsOptimized: UInt8, _ Fn: LLVMValueRef!, _ TemplateParams: UnsafeMutablePointer<LLVMMetadataRef?>!, _ NumTemplateParams: UInt32, _ Decl: LLVMMetadataRef!) -> LLVMMetadataRef!
758-
759834
/// Create debugging information entry for a class.
760835
/// \param Scope Scope in which this class is defined.
761836
/// \param Name class name.
@@ -799,9 +874,6 @@ public func LLVMDIBuilderCreateGlobalVariableExpression(_ Builder: LLVMDIBuilder
799874
/// \param Type Parent type.
800875
public func LLVMDIBuilderCreateBitFieldMemberType(_ Builder: LLVMDIBuilderRef!, _ Scope: LLVMMetadataRef!, _ Name: UnsafePointer<Int8>!, _ File: LLVMMetadataRef!, _ LineNumber: UInt32, _ SizeInBits: UInt64, _ OffsetInBits: UInt64, _ StorageOffsetInBits: UInt64, _ Flags: LLVMDIFlags, _ Type: LLVMMetadataRef!) -> LLVMMetadataRef!
801876

802-
/// Create a permanent forward-declared type.
803-
public func LLVMDIBuilderCreateForwardDecl(_ Builder: LLVMDIBuilderRef!, _ Tag: UInt32, _ Name: UnsafePointer<Int8>!, _ Scope: LLVMMetadataRef!, _ File: LLVMMetadataRef!, _ Line: UInt32) -> LLVMMetadataRef!
804-
805877
/// Create a new descriptor for the specified C++ method.
806878
/// See comments in \a DISubprogram* for descriptions of these fields.
807879
/// \param Scope Function scope.
@@ -937,63 +1009,6 @@ public func LLVMDIBuilderCreateUnspecifiedParameter(_ Builder: LLVMDIBuilderRef!
9371009
/// Create a DWARF unspecified type.
9381010
public func LLVMDIBuilderCreateUnspecifiedType(_ Builder: LLVMDIBuilderRef!, _ Name: UnsafePointer<Int8>!) -> LLVMMetadataRef!
9391011

940-
941-
/// Create a new descriptor for a parameter variable.
942-
///
943-
/// \c Scope must be a \a DILocalScope, and thus its scope chain eventually
944-
/// leads to a \a DISubprogram.
945-
///
946-
/// \c ArgNo is the index (starting from \c 1) of this variable in the
947-
/// subprogram parameters. \c ArgNo should not conflict with other
948-
/// parameters of the same subprogram.
949-
///
950-
/// If \c AlwaysPreserve, this variable will be referenced from its
951-
/// containing subprogram, and will survive some optimizations.
952-
public func LLVMDIBuilderCreateParameterVariable(_ Builder: LLVMDIBuilderRef!, _ Scope: LLVMMetadataRef!, _ Name: UnsafePointer<Int8>!, _ ArgNum: UInt32, _ File: LLVMMetadataRef!, _ LineNum: UInt32, _ Type: LLVMMetadataRef!, _ AlwaysPreserve: UInt8, _ Flags: LLVMDIFlags) -> LLVMMetadataRef!
953-
954-
/// This creates a descriptor for a lexical block with a new file
955-
/// attached. This merely extends the existing
956-
/// lexical block as it crosses a file.
957-
/// \param Builder The DIBuilder.
958-
/// \param Scope Lexical block.
959-
/// \param File Source file.
960-
/// \param Discriminator DWARF path discriminator value.
961-
public func LLVMDIBuilderCreateLexicalBlockFile(_ Builder: LLVMDIBuilderRef!, _ Scope: LLVMMetadataRef!, _ File: LLVMMetadataRef!) -> LLVMMetadataRef!
962-
963-
/// Create an expression for a variable that does not have an address, but
964-
/// does have a constant value.
965-
public func LLVMDIBuilderCreateConstantValueExpression(_ Builder: LLVMDIBuilderRef!, _ Val: UInt64) -> LLVMMetadataRef!
966-
967-
/// Create a descriptor to describe one part
968-
/// of aggregate variable that is fragmented across multiple Values.
969-
///
970-
/// \param Builder The DIBuilder.
971-
/// \param OffsetInBits Offset of the piece in bits.
972-
/// \param SizeInBits Size of the piece in bits.
973-
public func LLVMDIBuilderCreateFragmentExpression(_ Builder: LLVMDIBuilderRef!, _ OffsetInBits: UInt32, _ SizeInBits: UInt32) -> LLVMMetadataRef!
974-
975-
/// Create a new descriptor for the specified
976-
/// variable which has a complex address expression for its address.
977-
/// \param Builder The DIBuilder.
978-
/// \param Addrs One or more complex address operations.
979-
/// \param NumAddrs The number of addresses that \c Addrs points to.
980-
public func LLVMDIBuilderCreateExpression(_ Builder: LLVMDIBuilderRef!, _ Addrs: UnsafeMutablePointer<Int64>!, _ NumAddrs: UInt32) -> LLVMMetadataRef!
981-
982-
/// Create debugging information entry for an array.
983-
/// \param Builder The DIBuilder.
984-
/// \param Size Array size.
985-
/// \param AlignInBits Alignment.
986-
/// \param Ty Element type.
987-
/// \param Subscripts Subscripts.
988-
public func LLVMDIBuilderCreateArrayType(_ Builder: LLVMDIBuilderRef!, _ Size: UInt64, _ AlignInBits: UInt32, _ Ty: LLVMMetadataRef!, _ Subscripts: UnsafeMutablePointer<LLVMMetadataRef?>!, _ NumSubscripts: UInt32) -> LLVMMetadataRef!
989-
990-
/// Create debugging information entry for a vector type.
991-
/// \param Size Array size.
992-
/// \param AlignInBits Alignment.
993-
/// \param Ty Element type.
994-
/// \param Subscripts Subscripts.
995-
public func LLVMDIBuilderCreateVectorType(_ Builder: LLVMDIBuilderRef!, _ Size: UInt64, _ AlignInBits: UInt32, _ Ty: LLVMMetadataRef!, _ Subscripts: UnsafeMutablePointer<LLVMMetadataRef?>!, _ NumSubscripts: UInt32) -> LLVMMetadataRef!
996-
9971012
/// Create a descriptor for a value range. This
9981013
/// implicitly uniques the values returned.
9991014
public func LLVMDIBuilderGetOrCreateSubrange(_ Builder: LLVMDIBuilderRef!, _ Lo: Int64, _ Count: Int64) -> LLVMMetadataRef!

Sources/LLVM/DIType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if !NO_SWIFTPM
1+
#if SWIFT_PACKAGE
22
import cllvm
33
#endif
44

0 commit comments

Comments
 (0)