Skip to content

Commit 51312b1

Browse files
committed
XXX
1 parent 4d3a12d commit 51312b1

File tree

1 file changed

+131
-77
lines changed

1 file changed

+131
-77
lines changed

Sources/LLVM/DIBuilder.swift

Lines changed: 131 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
import cllvm
33
#endif
44

5+
// FIXME: This should be in the bindings.
6+
public enum MachineInfoRecordType: UInt32 {
7+
// Macinfo Type Encodings
8+
case define = 0x01
9+
case undef = 0x02
10+
case startFile = 0x03
11+
case endFile = 0x04
12+
case vendorExt = 0xff
13+
}
14+
515
public struct DebugInfoFlags: OptionSet {
616
public let rawValue: LLVMDIFlags.RawValue
717

@@ -363,7 +373,7 @@ public enum DWARFTypeEncoding {
363373

364374
case ascii
365375

366-
static let typeEncodingMapping: [DWARFTypeEncoding: LLVMDWARFTypeEncoding] = [
376+
private static let typeEncodingMapping: [DWARFTypeEncoding: LLVMDWARFTypeEncoding] = [
367377
.address: LLVMDWARFTypeEncoding_address,
368378
.boolean: LLVMDWARFTypeEncoding_boolean,
369379
.complexFloat: LLVMDWARFTypeEncoding_complex_float,
@@ -395,13 +405,67 @@ public enum DWARFEmissionKind {
395405
case lineTablesOnly
396406
}
397407

398-
public struct MetaData {
408+
public protocol Metadata {
409+
func asMetadata() -> LLVMMetadataRef
410+
}
411+
412+
struct AnyMetadata: Metadata {
413+
let llvm: LLVMMetadataRef
414+
415+
func asMetadata() -> LLVMMetadataRef {
416+
return llvm
417+
}
418+
}
419+
420+
struct AnyDIType: Metadata, DIType {
421+
let llvm: LLVMMetadataRef
422+
423+
func asMetadata() -> LLVMMetadataRef {
424+
return llvm
425+
}
426+
427+
func asMetadata(with builder: DIBuilder) -> LLVMMetadataRef {
428+
return llvm
429+
}
430+
}
431+
432+
public struct FileMetadata: Metadata {
433+
internal let llvm: LLVMMetadataRef
434+
435+
public func asMetadata() -> LLVMMetadataRef {
436+
return llvm
437+
}
438+
}
439+
440+
public struct Scope: Metadata {
441+
internal let llvm: LLVMMetadataRef
442+
443+
public func asMetadata() -> LLVMMetadataRef {
444+
return llvm
445+
}
446+
}
447+
448+
public struct Macro: Metadata {
449+
internal let llvm: LLVMMetadataRef
450+
451+
public func asMetadata() -> LLVMMetadataRef {
452+
return llvm
453+
}
454+
}
455+
456+
public struct DIModule: Metadata {
399457
internal let llvm: LLVMMetadataRef
400458

401-
/// Find subprogram that is enclosing this scope.
402-
var subprogram: MetaData? {
403-
guard let prgm = LLVMGetSubprogram(self.llvm) else { return nil }
404-
return MetaData(llvm: prgm)
459+
public func asMetadata() -> LLVMMetadataRef {
460+
return llvm
461+
}
462+
}
463+
464+
public struct DebugLocation: Metadata {
465+
internal let llvm: LLVMMetadataRef
466+
467+
public func asMetadata() -> LLVMMetadataRef {
468+
return llvm
405469
}
406470
}
407471

@@ -416,27 +480,78 @@ public final class DIBuilder {
416480
self.llvm = LLVMDIBuilderCreate(module.llvm, allowUnresolved ? 1 : 0)
417481
}
418482

483+
/// Creates a new DebugLocation that describes a source location.
484+
public func createDebugLocation(at loc : (line: Int, column: Int), in scope: Scope, inlinedAt: Scope? = nil) -> DebugLocation {
485+
guard let loc = LLVMDIBuilderCreateDebugLocation(self.module.context.llvm, UInt32(loc.line), UInt32(loc.column), scope.llvm, inlinedAt?.llvm) else {
486+
fatalError()
487+
}
488+
return DebugLocation(llvm: loc)
489+
}
490+
419491
/// Create a file descriptor to hold debugging information for a file.
420-
public func createFile(named name: String, in directory: String) -> MetaData? {
492+
public func createFile(named name: String, in directory: String) -> FileMetadata? {
421493
guard let file = LLVMDIBuilderCreateFile(self.llvm, name, directory) else { return nil }
422-
return MetaData(llvm: file)
494+
return FileMetadata(llvm: file)
423495
}
424496

425497
/// Create debugging information temporary entry for a macro file.
426-
public func createTemporaryMacroFile(at line: UInt32, in file: MetaData, withParent parent: MetaData? = nil) -> MetaData? {
498+
public func createTemporaryMacroFile(at line: UInt32, in file: FileMetadata, withParent parent: FileMetadata? = nil) -> FileMetadata? {
427499
guard let file = LLVMDIBuilderCreateTempMacroFile(self.llvm, parent?.llvm, line, file.llvm) else { return nil }
428-
return MetaData(llvm: file)
500+
return FileMetadata(llvm: file)
501+
}
502+
503+
/// This creates a descriptor for a lexical block with the
504+
/// specified parent context.
505+
public func createLexicalBlock(in scope: Scope, in file: FileMetadata, at loc : (line: Int, column: Int)) -> Scope {
506+
guard let scope = LLVMDIBuilderCreateLexicalBlock(self.llvm, scope.llvm, file.llvm, UInt32(loc.line), UInt32(loc.column)) else {
507+
fatalError()
508+
}
509+
return Scope(llvm: scope)
510+
}
511+
512+
/// This creates new descriptor for a module with the specified
513+
/// parent scope.
514+
public func createModule(in scope: Scope, named name: String, macros: [String], include ipath: String, _ isysRoot: String) -> DIModule {
515+
guard let module = LLVMDIBuilderCreateModule(self.llvm, scope.llvm, name, macros.joined(separator: " "), ipath, isysRoot) else {
516+
fatalError()
517+
}
518+
return DIModule(llvm: module)
519+
}
520+
521+
/// Create debugging information entry for a macro.
522+
public func createMacro(in parent: FileMetadata, at line: Int, type macroTy: MachineInfoRecordType, name: String) -> Macro! {
523+
guard let macro = LLVMDIBuilderCreateMacro(self.llvm, parent.asMetadata(), UInt32(line), macroTy.rawValue, name) else {
524+
fatalError()
525+
}
526+
return Macro(llvm: macro)
429527
}
430528

431-
/// Create subroutine type.
432-
public func createSubroutineType(in file: MetaData, types: [MetaData]) -> MetaData? {
433-
var vals = types.map { $0.llvm as Optional }
434-
return vals.withUnsafeMutableBufferPointer { buf in
435-
guard let file = LLVMDIBuilderCreateSubroutineType(self.llvm, file.llvm, buf.baseAddress, UInt32(types.count)) else { return nil }
436-
return MetaData(llvm: file)
529+
/// Create debugging information entry for a typedef.
530+
public func createTypedef(of type: DIType, named name: String, in file: FileMetadata, at line: Int, in scope: Scope) -> DIType {
531+
guard let typedef = LLVMDIBuilderCreateTypedef(self.llvm, type.asMetadata(with: self), name, file.llvm, UInt32(line), scope.llvm) else {
532+
fatalError()
437533
}
534+
return AnyDIType(llvm: typedef)
438535
}
439536

537+
/// Create a new DIType with "artificial" flag set.
538+
public func createArtificialType(of type: DIType) -> DIType {
539+
guard let ty = LLVMDIBuilderCreateArtificialType(self.llvm, type.asMetadata(with: self)) else {
540+
fatalError()
541+
}
542+
return AnyDIType(llvm: ty)
543+
}
544+
545+
/// Create a new descriptor for an auto variable. This is a local variable
546+
/// that is not a subprogram parameter.
547+
public func createAutoVariable(in scope: Scope, named name: String, in file: FileMetadata, at line: Int, type: DIType, alwaysPreserve: Bool, flags: DebugInfoFlags, aligned alignInBits: Int) -> Metadata {
548+
guard let vari = LLVMDIBuilderCreateAutoVariable(self.llvm, scope.llvm, name, file.llvm, UInt32(line), type.asMetadata(with: self), alwaysPreserve ? 1 : 0, flags.llvm, UInt32(alignInBits)) else {
549+
fatalError()
550+
}
551+
return AnyMetadata(llvm: vari)
552+
}
553+
554+
440555
/// Construct any deferred debug info descriptors.
441556
public func finalize() {
442557
LLVMDIBuilderFinalize(self.llvm)
@@ -495,20 +610,6 @@ public func LLVMDIBuilderCreateFunction(_ Builder: LLVMDIBuilderRef!, _ Scope: L
495610
/// except that the resulting DbgNode is meant to be RAUWed.
496611
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!
497612

498-
/// Create C++11 nullptr type.
499-
500-
/// This creates new descriptor for a module with the specified
501-
/// parent scope.
502-
/// \param Builder The DIBuilder.
503-
/// \param Scope Parent scope
504-
/// \param Name Name of this module
505-
/// \param ConfigurationMacros
506-
/// A space-separated shell-quoted list of -D macro
507-
/// definitions as they would appear on a command line.
508-
/// \param IncludePath The path to the module map file.
509-
/// \param ISysRoot The clang system root (value of -isysroot).
510-
public func LLVMDIBuilderCreateModule(_ Builder: LLVMDIBuilderRef!, _ Scope: LLVMMetadataRef!, _ Name: UnsafePointer<Int8>!, _ ConfigurationMacros: UnsafePointer<Int8>!, _ IncludePath: UnsafePointer<Int8>!, _ ISysRoot: UnsafePointer<Int8>!) -> LLVMMetadataRef!
511-
512613
/// Create debugging information entry for a class.
513614
/// \param Scope Scope in which this class is defined.
514615
/// \param Name class name.
@@ -523,19 +624,6 @@ public func LLVMDIBuilderCreateModule(_ Builder: LLVMDIBuilderRef!, _ Scope: LLV
523624
/// \param TemplateParms Template type parameters.
524625
public func LLVMDIBuilderCreateClassType(_ Builder: LLVMDIBuilderRef!, _ Scope: LLVMMetadataRef!, _ Name: UnsafePointer<Int8>!, _ File: LLVMMetadataRef!, _ LineNumber: UInt32, _ SizeInBits: UInt64, _ AlignInBits: UInt32, _ OffsetInBits: UInt64, _ Flags: LLVMDIFlags, _ Elements: UnsafeMutablePointer<LLVMMetadataRef?>!, _ NumElements: UInt32, _ DerivedFrom: LLVMMetadataRef!, _ TemplateParamsNode: LLVMMetadataRef!) -> LLVMMetadataRef!
525626

526-
/// Create a new DIType* with "artificial" flag set.
527-
public func LLVMDIBuilderCreateArtificialType(_ Builder: LLVMDIBuilderRef!, _ Type: LLVMMetadataRef!) -> LLVMMetadataRef!
528-
529-
/// Create a new descriptor for an auto variable. This is a local variable
530-
/// that is not a subprogram parameter.
531-
///
532-
/// \c Scope must be a \a DILocalScope, and thus its scope chain eventually
533-
/// leads to a \a DISubprogram.
534-
///
535-
/// If \c AlwaysPreserve, this variable will be referenced from its
536-
/// containing subprogram, and will survive some optimizations.
537-
public func LLVMDIBuilderCreateAutoVariable(_ Builder: LLVMDIBuilderRef!, _ Scope: LLVMMetadataRef!, _ Name: UnsafePointer<Int8>!, _ File: LLVMMetadataRef!, _ LineNo: UInt32, _ Type: LLVMMetadataRef!, _ AlwaysPreserve: UInt8, _ Flags: LLVMDIFlags, _ AlignInBits: UInt32) -> LLVMMetadataRef!
538-
539627
/// Create a new descriptor for the specified variable.
540628
/// \param Context Variable scope.
541629
/// \param Name Name of the variable.
@@ -584,15 +672,6 @@ public func LLVMDIBuilderCreateForwardDecl(_ Builder: LLVMDIBuilderRef!, _ Tag:
584672
/// \param TParams Function template parameters.
585673
public func LLVMDIBuilderCreateMethod(_ Builder: LLVMDIBuilderRef!, _ Scope: LLVMMetadataRef!, _ Name: UnsafePointer<Int8>!, _ LinkageName: UnsafePointer<Int8>!, _ File: LLVMMetadataRef!, _ LineNumber: UInt32, _ FuncTy: LLVMMetadataRef!, _ Flags: LLVMDIFlags, _ IsLocalToUnit: UInt8, _ IsDefinition: UInt8, _ IsOptimized: UInt8, _ TemplateParameters: UnsafeMutablePointer<LLVMMetadataRef?>!, _ NumTemplateParameters: UInt32) -> LLVMMetadataRef!
586674

587-
/// Create debugging information entry for a typedef.
588-
/// \param Builder The DIBuilder.
589-
/// \param Ty Original type.
590-
/// \param Name Typedef name.
591-
/// \param File File where this type is defined.
592-
/// \param LineNo Line number.
593-
/// \param Scope The surrounding context for the typedef.
594-
public func LLVMDIBuilderCreateTypedef(_ Builder: LLVMDIBuilderRef!, _ Type: LLVMMetadataRef!, _ Name: UnsafePointer<Int8>!, _ File: LLVMMetadataRef!, _ Line: UInt32, _ Scope: LLVMMetadataRef!) -> LLVMMetadataRef!
595-
596675
/// Create debugging information entry to establish
597676
/// inheritance relationship between two types.
598677
/// \param Builder The DIBuilder.
@@ -603,15 +682,6 @@ public func LLVMDIBuilderCreateTypedef(_ Builder: LLVMDIBuilderRef!, _ Type: LLV
603682
/// e.g. private
604683
public func LLVMDIBuilderCreateInheritance(_ Builder: LLVMDIBuilderRef!, _ Type: LLVMMetadataRef!, _ BaseType: LLVMMetadataRef!, _ BaseOffset: UInt64, _ Flags: LLVMDIFlags) -> LLVMMetadataRef!
605684

606-
/// Create debugging information entry for a macro.
607-
/// \param Builder The DIBuilder.
608-
/// \param Parent Macro parent (could be nullptr).
609-
/// \param Line Source line number where the macro is defined.
610-
/// \param MacroType DW_MACINFO_define or DW_MACINFO_undef.
611-
/// \param Name Macro name.
612-
/// \param Value Macro value.
613-
public func LLVMDIBuilderCreateMacro(_ Builder: LLVMDIBuilderRef!, _ ParentMacroFile: LLVMMetadataRef!, _ Line: UInt32, _ MacroType: UInt32, _ Name: UnsafePointer<Int8>!) -> LLVMMetadataRef!
614-
615685
/// Create debugging information entry for a 'friend'.
616686
public func LLVMDIBuilderCreateFriend(_ Builder: LLVMDIBuilderRef!, _ Type: LLVMMetadataRef!, _ FriendType: LLVMMetadataRef!) -> LLVMMetadataRef!
617687

@@ -736,14 +806,6 @@ public func LLVMDIBuilderCreateUnspecifiedParameter(_ Builder: LLVMDIBuilderRef!
736806
/// Create a DWARF unspecified type.
737807
public func LLVMDIBuilderCreateUnspecifiedType(_ Builder: LLVMDIBuilderRef!, _ Name: UnsafePointer<Int8>!) -> LLVMMetadataRef!
738808

739-
/// This creates a descriptor for a lexical block with the
740-
/// specified parent context.
741-
/// \param Builder The DIBuilder.
742-
/// \param Scope Parent lexical scope.
743-
/// \param File Source file.
744-
/// \param Line Line number.
745-
/// \param Col Column number.
746-
public func LLVMDIBuilderCreateLexicalBlock(_ Builder: LLVMDIBuilderRef!, _ Scope: LLVMMetadataRef!, _ File: LLVMMetadataRef!, _ Line: UInt32, _ Col: UInt32) -> LLVMMetadataRef!
747809

748810
/// Create a new descriptor for a parameter variable.
749811
///
@@ -911,12 +973,4 @@ public func LLVMDIBuilderCreateNameSpace(_ Builder: LLVMDIBuilderRef!, _ Scope:
911973
/// has a self-reference -- \a DIBuilder needs to track the array to
912974
/// resolve cycles.
913975
public func LLVMDICompositeTypeSetTypeArray(_ Builder: LLVMDIBuilderRef!, _ CompositeTy: LLVMMetadataRef!, _ Types: UnsafeMutablePointer<LLVMMetadataRef?>!, _ NumTypes: UInt32)
914-
915-
/// Creates a new DebugLocation that describes a source location.
916-
/// \param Line The line in the source file.
917-
/// \param Column The column in the source file.
918-
/// \param Scope The scope in which the location resides.
919-
/// \param InlinedAt The scope where this location was inlined, if at all.
920-
/// (optional).
921-
public func LLVMDIBuilderCreateDebugLocation(_ Ctx: LLVMContextRef!, _ Line: UInt32, _ Column: UInt32, _ Scope: LLVMMetadataRef!, _ InlinedAt: LLVMMetadataRef!) -> LLVMMetadataRef!
922976
*/

0 commit comments

Comments
 (0)