Skip to content

Commit 843c8e1

Browse files
committed
SwiftCompilerSources: VarDecl bridging for SIL.
1 parent ae7e00f commit 843c8e1

File tree

3 files changed

+182
-24
lines changed

3 files changed

+182
-24
lines changed

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,36 @@ final public class HopToExecutorInst : Instruction, UnaryInstruction {}
353353

354354
final public class FixLifetimeInst : Instruction, UnaryInstruction {}
355355

356-
final public class DebugValueInst : Instruction, UnaryInstruction {}
356+
// VarDecl is a struct wrapper around a C++ VarDecl pointer. This insulates the Swift interface from the C++ interface and avoids the need to fake a Swift class. When the AST exposes VarDecl, then this can be replaced whenever it is convenient.
357+
public struct VarDecl {
358+
var bridged: BridgedVarDecl
359+
360+
public init?(bridged: OptionalBridgedVarDecl) {
361+
guard let decl = bridged.decl else { return nil }
362+
self.bridged = BridgedVarDecl(decl: decl)
363+
}
364+
365+
public var userFacingName: String { String(bridged.getUserFacingName()) }
366+
}
367+
368+
// See C++ VarDeclCarryingInst
369+
public protocol VarDeclInst {
370+
var varDecl: VarDecl? { get }
371+
}
372+
373+
public protocol DebugVariableInst : VarDeclInst {
374+
var debugVariable: OptionalBridgedSILDebugVariable { get }
375+
}
376+
377+
final public class DebugValueInst : Instruction, UnaryInstruction, DebugVariableInst {
378+
public var varDecl: VarDecl? {
379+
VarDecl(bridged: bridged.DebugValue_getDecl())
380+
}
381+
382+
public var debugVariable: OptionalBridgedSILDebugVariable {
383+
return bridged.DebugValue_getVarInfo()
384+
}
385+
}
357386

358387
final public class DebugStepInst : Instruction {}
359388

@@ -608,7 +637,11 @@ final public class DynamicFunctionRefInst : FunctionRefBaseInst {
608637
final public class PreviousDynamicFunctionRefInst : FunctionRefBaseInst {
609638
}
610639

611-
final public class GlobalAddrInst : GlobalAccessInst {}
640+
final public class GlobalAddrInst : GlobalAccessInst, VarDeclInst {
641+
public var varDecl: VarDecl? {
642+
VarDecl(bridged: bridged.GlobalAddr_getDecl())
643+
}
644+
}
612645

613646
final public class GlobalValueInst : GlobalAccessInst {
614647
public var isBare: Bool { bridged.GlobalValueInst_isBare() }
@@ -711,13 +744,17 @@ final public class SelectEnumInst : SingleValueInstruction {
711744
public var enumOperand: Operand { operands[0] }
712745
}
713746

714-
final public class RefElementAddrInst : SingleValueInstruction, UnaryInstruction {
747+
final public class RefElementAddrInst : SingleValueInstruction, UnaryInstruction, VarDeclInst {
715748
public var instance: Value { operand.value }
716749
public var fieldIndex: Int { bridged.RefElementAddrInst_fieldIndex() }
717750

718751
public var fieldIsLet: Bool { bridged.RefElementAddrInst_fieldIsLet() }
719752

720753
public var isImmutable: Bool { bridged.RefElementAddrInst_isImmutable() }
754+
755+
public var varDecl: VarDecl? {
756+
VarDecl(bridged: bridged.RefElementAddr_getDecl())
757+
}
721758
}
722759

723760
final public class RefTailAddrInst : SingleValueInstruction, UnaryInstruction {
@@ -942,8 +979,16 @@ final public class LinearFunctionInst: SingleValueInstruction, ForwardingInstruc
942979

943980
public protocol Allocation : SingleValueInstruction { }
944981

945-
final public class AllocStackInst : SingleValueInstruction, Allocation {
982+
final public class AllocStackInst : SingleValueInstruction, Allocation, DebugVariableInst {
946983
public var hasDynamicLifetime: Bool { bridged.AllocStackInst_hasDynamicLifetime() }
984+
985+
public var varDecl: VarDecl? {
986+
VarDecl(bridged: bridged.AllocStack_getDecl())
987+
}
988+
989+
public var debugVariable: OptionalBridgedSILDebugVariable {
990+
return bridged.AllocStack_getVarInfo()
991+
}
947992
}
948993

949994
public class AllocRefInstBase : SingleValueInstruction, Allocation {
@@ -973,7 +1018,15 @@ final public class AllocRefDynamicInst : AllocRefInstBase {
9731018
}
9741019
}
9751020

976-
final public class AllocBoxInst : SingleValueInstruction, Allocation {
1021+
final public class AllocBoxInst : SingleValueInstruction, Allocation, DebugVariableInst {
1022+
1023+
public var varDecl: VarDecl? {
1024+
VarDecl(bridged: bridged.AllocBox_getDecl())
1025+
}
1026+
1027+
public var debugVariable: OptionalBridgedSILDebugVariable {
1028+
return bridged.AllocBox_getVarInfo()
1029+
}
9771030
}
9781031

9791032
final public class AllocExistentialBoxInst : SingleValueInstruction, Allocation {

include/swift/SIL/SILBridging.h

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#ifdef USED_IN_CPP_SOURCE
2323
#include "llvm/ADT/ArrayRef.h"
2424
#include "swift/SIL/SILBuilder.h"
25+
#include "swift/SIL/SILDebugVariable.h"
26+
#include "swift/SIL/SILDefaultWitnessTable.h"
27+
#include "swift/SIL/SILFunctionConventions.h"
2528
#include "swift/SIL/SILInstruction.h"
2629
#include "swift/SIL/SILWitnessTable.h"
2730
#endif
@@ -53,6 +56,7 @@ class SILVTable;
5356
class SILWitnessTable;
5457
class SILDefaultWitnessTable;
5558
class NominalTypeDecl;
59+
class VarDecl;
5660
class SwiftPassInvocation;
5761
class GenericSpecializationInformation;
5862
}
@@ -126,6 +130,28 @@ struct BridgedType {
126130
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getTupleElementType(SwiftInt idx) const;
127131
};
128132

133+
// AST bridging
134+
135+
struct BridgedNominalTypeDecl {
136+
swift::NominalTypeDecl * _Nonnull decl;
137+
138+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getName() const;
139+
bool isStructWithUnreferenceableStorage() const;
140+
BRIDGED_INLINE bool isGlobalActor() const;
141+
};
142+
143+
struct BridgedVarDecl {
144+
const swift::VarDecl * _Nonnull decl;
145+
146+
BRIDGED_INLINE BridgedStringRef getUserFacingName() const;
147+
};
148+
149+
struct OptionalBridgedVarDecl {
150+
const swift::VarDecl * _Nullable decl;
151+
};
152+
153+
// SIL Bridging
154+
129155
struct BridgedValue {
130156
SwiftObject obj;
131157

@@ -447,6 +473,23 @@ struct BridgedGenericSpecializationInformation {
447473
const swift::GenericSpecializationInformation * _Nullable data = nullptr;
448474
};
449475

476+
struct OptionalBridgedSILDebugVariable {
477+
uint64_t storage[16];
478+
479+
#ifdef USED_IN_CPP_SOURCE
480+
using OptionalSILDebugVariable = llvm::Optional<swift::SILDebugVariable>;
481+
482+
OptionalBridgedSILDebugVariable(
483+
OptionalSILDebugVariable &&debugVariable) {
484+
static_assert(sizeof(OptionalSILDebugVariable) <= 16*8);
485+
*reinterpret_cast<OptionalSILDebugVariable *>(&storage) = debugVariable;
486+
}
487+
const OptionalSILDebugVariable &getDebugVar() const {
488+
return *reinterpret_cast<const OptionalSILDebugVariable *>(&storage);
489+
}
490+
#endif
491+
};
492+
450493
struct BridgedInstruction {
451494
SwiftObject obj;
452495

@@ -603,6 +646,26 @@ struct BridgedInstruction {
603646
BRIDGED_INLINE BridgedArgumentConvention ApplySite_getArgumentConvention(SwiftInt calleeArgIdx) const;
604647
BRIDGED_INLINE SwiftInt ApplySite_getNumArguments() const;
605648
BRIDGED_INLINE SwiftInt FullApplySite_numIndirectResultArguments() const;
649+
650+
// =========================================================================//
651+
// VarDeclInst and DebugVariableInst
652+
// =========================================================================//
653+
654+
BRIDGED_INLINE OptionalBridgedVarDecl DebugValue_getDecl() const;
655+
656+
BRIDGED_INLINE OptionalBridgedVarDecl AllocStack_getDecl() const;
657+
658+
BRIDGED_INLINE OptionalBridgedVarDecl AllocBox_getDecl() const;
659+
660+
BRIDGED_INLINE OptionalBridgedVarDecl GlobalAddr_getDecl() const;
661+
662+
BRIDGED_INLINE OptionalBridgedVarDecl RefElementAddr_getDecl() const;
663+
664+
BRIDGED_INLINE OptionalBridgedSILDebugVariable DebugValue_getVarInfo() const;
665+
666+
BRIDGED_INLINE OptionalBridgedSILDebugVariable AllocStack_getVarInfo() const;
667+
668+
BRIDGED_INLINE OptionalBridgedSILDebugVariable AllocBox_getVarInfo() const;
606669
};
607670

608671
struct OptionalBridgedInstruction {
@@ -862,15 +925,11 @@ struct BridgedBuilder{
862925
bool keepUnique) const;
863926
};
864927

865-
// AST bridging
866-
867-
struct BridgedNominalTypeDecl {
868-
swift::NominalTypeDecl * _Nonnull decl;
928+
// Passmanager and Context
869929

870-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef getName() const;
871-
bool isStructWithUnreferenceableStorage() const;
872-
BRIDGED_INLINE bool isGlobalActor() const;
873-
};
930+
namespace swift {
931+
class SwiftPassInvocation;
932+
}
874933

875934
struct BridgedChangeNotificationHandler {
876935
swift::SwiftPassInvocation * _Nonnull invocation;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,24 @@ BridgedType BridgedType::getTupleElementType(SwiftInt idx) const {
213213
return get().getTupleElementType(idx);
214214
}
215215

216+
//===----------------------------------------------------------------------===//
217+
// BridgedNominalTypeDecl
218+
//===----------------------------------------------------------------------===//
219+
220+
BridgedStringRef BridgedNominalTypeDecl::getName() const {
221+
return decl->getName().str();
222+
}
223+
224+
bool BridgedNominalTypeDecl::isGlobalActor() const { return decl->isGlobalActor(); }
225+
226+
//===----------------------------------------------------------------------===//
227+
// BridgedVarDecl
228+
//===----------------------------------------------------------------------===//
229+
230+
BridgedStringRef BridgedVarDecl::getUserFacingName() const {
231+
return decl->getBaseName().userFacingName();
232+
}
233+
216234
//===----------------------------------------------------------------------===//
217235
// BridgedValue
218236
//===----------------------------------------------------------------------===//
@@ -997,6 +1015,45 @@ SwiftInt BridgedInstruction::FullApplySite_numIndirectResultArguments() const {
9971015
return fas.getNumIndirectSILResults();
9981016
}
9991017

1018+
//===----------------------------------------------------------------------===//
1019+
// VarDeclInst and DebugVariableInst
1020+
//===----------------------------------------------------------------------===//
1021+
1022+
OptionalBridgedVarDecl BridgedInstruction::DebugValue_getDecl() const {
1023+
return {getAs<swift::DebugValueInst>()->getDecl()};
1024+
}
1025+
1026+
OptionalBridgedVarDecl BridgedInstruction::AllocStack_getDecl() const {
1027+
return {getAs<swift::AllocStackInst>()->getDecl()};
1028+
}
1029+
1030+
OptionalBridgedVarDecl BridgedInstruction::AllocBox_getDecl() const {
1031+
return {getAs<swift::AllocBoxInst>()->getDecl()};
1032+
}
1033+
1034+
OptionalBridgedVarDecl BridgedInstruction::GlobalAddr_getDecl() const {
1035+
return {getAs<swift::DebugValueInst>()->getDecl()};
1036+
}
1037+
1038+
OptionalBridgedVarDecl BridgedInstruction::RefElementAddr_getDecl() const {
1039+
return {getAs<swift::DebugValueInst>()->getDecl()};
1040+
}
1041+
1042+
OptionalBridgedSILDebugVariable
1043+
BridgedInstruction::DebugValue_getVarInfo() const {
1044+
return getAs<swift::DebugValueInst>()->getVarInfo();
1045+
}
1046+
1047+
OptionalBridgedSILDebugVariable
1048+
BridgedInstruction::AllocStack_getVarInfo() const {
1049+
return getAs<swift::AllocStackInst>()->getVarInfo();
1050+
}
1051+
1052+
OptionalBridgedSILDebugVariable
1053+
BridgedInstruction::AllocBox_getVarInfo() const {
1054+
return getAs<swift::AllocBoxInst>()->getVarInfo();
1055+
}
1056+
10001057
//===----------------------------------------------------------------------===//
10011058
// BridgedBasicBlock
10021059
//===----------------------------------------------------------------------===//
@@ -1377,17 +1434,6 @@ BridgedInstruction BridgedBuilder::createEndCOWMutation(BridgedValue instance, b
13771434
return {get().createEndCOWMutation(regularLoc(), instance.getSILValue(), keepUnique)};
13781435
}
13791436

1380-
//===----------------------------------------------------------------------===//
1381-
// BridgedNominalTypeDecl
1382-
//===----------------------------------------------------------------------===//
1383-
1384-
BridgedStringRef BridgedNominalTypeDecl::getName() const {
1385-
return decl->getName().str();
1386-
}
1387-
1388-
bool BridgedNominalTypeDecl::isGlobalActor() const { return decl->isGlobalActor(); }
1389-
1390-
13911437
SWIFT_END_NULLABILITY_ANNOTATIONS
13921438

13931439
#endif

0 commit comments

Comments
 (0)