Skip to content

Commit 7ecb407

Browse files
committed
SwiftCompilerSources: bridge SILDeclRef
And move NominalTypeDecl and DeclRef from Type.swift into a new file Declarations.swift. Also add some APIs to NominalTypeDecl
1 parent f3a9b08 commit 7ecb407

File tree

8 files changed

+135
-35
lines changed

8 files changed

+135
-35
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/DiagnosticEngine.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import ASTBridging
1414

1515
import Basic
16+
import SIL
1617

1718
public typealias DiagID = BridgedDiagID
1819

@@ -29,6 +30,11 @@ extension Int: DiagnosticArgument {
2930
fn(BridgedDiagnosticArgument(self))
3031
}
3132
}
33+
extension DeclRef: DiagnosticArgument {
34+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
35+
fn(bridged.asDiagnosticArgument())
36+
}
37+
}
3238

3339
public struct DiagnosticFixIt {
3440
public let start: SourceLoc

SwiftCompilerSources/Sources/SIL/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_swift_compiler_module(SIL
1414
Argument.swift
1515
BasicBlock.swift
1616
Builder.swift
17+
Declarations.swift
1718
Effects.swift
1819
ForwardingInstruction.swift
1920
Function.swift
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===--- Declarations.swift -----------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SILBridging
14+
15+
// TODO: move all declarations to an AST module, once we have it
16+
17+
public struct NominalTypeDecl : Equatable, Hashable {
18+
public let bridged: BridgedNominalTypeDecl
19+
20+
public init(_bridged: BridgedNominalTypeDecl) {
21+
self.bridged = _bridged
22+
}
23+
24+
public var name: StringRef { StringRef(bridged: bridged.getName()) }
25+
26+
public static func ==(lhs: NominalTypeDecl, rhs: NominalTypeDecl) -> Bool {
27+
lhs.bridged.raw == rhs.bridged.raw
28+
}
29+
30+
public func hash(into hasher: inout Hasher) {
31+
hasher.combine(bridged.raw)
32+
}
33+
34+
public var location: Location { Location(bridged: BridgedLocation.fromNominalTypeDecl(bridged)) }
35+
36+
public func isResilient(in function: Function) -> Bool {
37+
function.bridged.isResilientNominalDecl(bridged)
38+
}
39+
40+
public var isStructWithUnreferenceableStorage: Bool {
41+
bridged.isStructWithUnreferenceableStorage()
42+
}
43+
44+
public var isGlobalActor: Bool {
45+
return bridged.isGlobalActor()
46+
}
47+
48+
public var hasValueDeinit: Bool {
49+
return bridged.hasValueDeinit()
50+
}
51+
52+
public var isClass: Bool { bridged.isClass() }
53+
54+
public var superClassType: Type? {
55+
precondition(isClass)
56+
return BridgedType.getSuperClassTypeOfClassDecl(bridged).typeOrNil
57+
}
58+
59+
public var isGenericAtAnyLevel: Bool { bridged.isGenericAtAnyLevel() }
60+
}
61+
62+
public struct DeclRef {
63+
public let bridged: BridgedDeclRef
64+
65+
public var location: Location { Location(bridged: bridged.getLocation()) }
66+
}

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -314,38 +314,3 @@ extension BridgedType {
314314
public var type: Type { Type(bridged: self) }
315315
var typeOrNil: Type? { isNull() ? nil : type }
316316
}
317-
318-
// TODO: use an AST type for this once we have it
319-
public struct NominalTypeDecl : Equatable, Hashable {
320-
public let bridged: BridgedNominalTypeDecl
321-
322-
public init(_bridged: BridgedNominalTypeDecl) {
323-
self.bridged = _bridged
324-
}
325-
326-
public var name: StringRef { StringRef(bridged: bridged.getName()) }
327-
328-
public static func ==(lhs: NominalTypeDecl, rhs: NominalTypeDecl) -> Bool {
329-
lhs.bridged.raw == rhs.bridged.raw
330-
}
331-
332-
public func hash(into hasher: inout Hasher) {
333-
hasher.combine(bridged.raw)
334-
}
335-
336-
public func isResilient(in function: Function) -> Bool {
337-
function.bridged.isResilientNominalDecl(bridged)
338-
}
339-
340-
public var isStructWithUnreferenceableStorage: Bool {
341-
bridged.isStructWithUnreferenceableStorage()
342-
}
343-
344-
public var isGlobalActor: Bool {
345-
return bridged.isGlobalActor()
346-
}
347-
348-
public var hasValueDeinit: Bool {
349-
return bridged.hasValueDeinit()
350-
}
351-
}

include/swift/AST/ASTBridging.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,14 @@ SWIFT_NAME("BridgedNominalTypeDecl.hasValueDeinit(self:)")
11121112
BRIDGED_INLINE
11131113
bool BridgedNominalTypeDecl_hasValueDeinit(BridgedNominalTypeDecl decl);
11141114

1115+
SWIFT_NAME("BridgedNominalTypeDecl.isClass(self:)")
1116+
BRIDGED_INLINE
1117+
bool BridgedNominalTypeDecl_isClass(BridgedNominalTypeDecl decl);
1118+
1119+
SWIFT_NAME("BridgedNominalTypeDecl.isGenericAtAnyLevel(self:)")
1120+
BRIDGED_INLINE
1121+
bool BridgedNominalTypeDecl_isGenericAtAnyLevel(BridgedNominalTypeDecl decl);
1122+
11151123
SWIFT_NAME("BridgedNominalTypeDecl.setParsedMembers(self:_:)")
11161124
void BridgedNominalTypeDecl_setParsedMembers(BridgedNominalTypeDecl decl,
11171125
BridgedArrayRef members);

include/swift/AST/ASTBridgingImpl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ bool BridgedNominalTypeDecl_hasValueDeinit(BridgedNominalTypeDecl decl) {
4545
return decl.unbridged()->getValueTypeDestructor() != nullptr;
4646
}
4747

48+
bool BridgedNominalTypeDecl_isClass(BridgedNominalTypeDecl decl) {
49+
return swift::isa<swift::ClassDecl>(decl.unbridged());
50+
}
51+
52+
bool BridgedNominalTypeDecl_isGenericAtAnyLevel(BridgedNominalTypeDecl decl) {
53+
return decl.unbridged()->isGenericContext();
54+
}
55+
4856
//===----------------------------------------------------------------------===//
4957
// MARK: BridgedSubscriptDecl
5058
//===----------------------------------------------------------------------===//

include/swift/SIL/SILBridging.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ struct BridgedType {
336336
getTupleElementType(SwiftInt idx) const;
337337
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getFunctionTypeWithNoEscape(bool withNoEscape) const;
338338
BRIDGED_INLINE BridgedArgumentConvention getCalleeConvention() const;
339+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE
340+
static BridgedType getSuperClassTypeOfClassDecl(BridgedNominalTypeDecl decl);
339341
};
340342

341343
// SIL Bridging
@@ -470,6 +472,7 @@ struct BridgedLocation {
470472
BRIDGED_INLINE bool isEqualTo(BridgedLocation rhs) const;
471473
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSourceLoc getSourceLocation() const;
472474
BRIDGED_INLINE bool hasSameSourceLocation(BridgedLocation rhs) const;
475+
static BRIDGED_INLINE BridgedLocation fromNominalTypeDecl(BridgedNominalTypeDecl decl);
473476
static BRIDGED_INLINE BridgedLocation getArtificialUnreachableLocation();
474477
};
475478

@@ -1067,6 +1070,23 @@ struct BridgedSuccessorArray {
10671070
SwiftInt count;
10681071
};
10691072

1073+
struct BridgedDeclRef {
1074+
uint64_t storage[3];
1075+
1076+
#ifdef USED_IN_CPP_SOURCE
1077+
BridgedDeclRef(swift::SILDeclRef declRef) {
1078+
*reinterpret_cast<swift::SILDeclRef *>(&storage) = declRef;
1079+
}
1080+
1081+
swift::SILDeclRef unbridged() const {
1082+
return *reinterpret_cast<const swift::SILDeclRef *>(&storage);
1083+
}
1084+
#endif
1085+
1086+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedLocation getLocation() const;
1087+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDiagnosticArgument asDiagnosticArgument() const;
1088+
};
1089+
10701090
struct BridgedVTableEntry {
10711091
const swift::SILVTableEntry * _Nonnull entry;
10721092

include/swift/SIL/SILBridgingImpl.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,14 @@ BridgedArgumentConvention BridgedType::getCalleeConvention() const {
531531
return getArgumentConvention(fnType->getCalleeConvention());
532532
}
533533

534+
BridgedType BridgedType::getSuperClassTypeOfClassDecl(BridgedNominalTypeDecl decl) {
535+
swift::Type superTy = swift::cast<swift::ClassDecl>(decl.unbridged())->getSuperclass();
536+
if (!superTy) {
537+
return BridgedType(swift::SILType());
538+
}
539+
return swift::SILType::getPrimitiveObjectType(superTy->getCanonicalType());
540+
}
541+
534542
//===----------------------------------------------------------------------===//
535543
// BridgedValue
536544
//===----------------------------------------------------------------------===//
@@ -718,6 +726,9 @@ BridgedSourceLoc BridgedLocation::getSourceLocation() const {
718726
bool BridgedLocation::hasSameSourceLocation(BridgedLocation rhs) const {
719727
return getLoc().hasSameSourceLocation(rhs.getLoc());
720728
}
729+
BridgedLocation BridgedLocation::fromNominalTypeDecl(BridgedNominalTypeDecl decl) {
730+
return swift::SILDebugLocation(decl.unbridged(), nullptr);
731+
}
721732
BridgedLocation BridgedLocation::getArtificialUnreachableLocation() {
722733
return swift::SILDebugLocation::getArtificialUnreachableLocation();
723734
}
@@ -1703,6 +1714,21 @@ BridgedSuccessor OptionalBridgedSuccessor::advancedBy(SwiftInt index) const {
17031714
return {succ + index};
17041715
}
17051716

1717+
//===----------------------------------------------------------------------===//
1718+
// BridgedDeclRef
1719+
//===----------------------------------------------------------------------===//
1720+
1721+
static_assert(sizeof(BridgedDeclRef) >= sizeof(swift::SILDeclRef),
1722+
"BridgedDeclRef has wrong size");
1723+
1724+
BridgedLocation BridgedDeclRef::getLocation() const {
1725+
return swift::SILDebugLocation(unbridged().getDecl(), nullptr);
1726+
}
1727+
1728+
BridgedDiagnosticArgument BridgedDeclRef::asDiagnosticArgument() const {
1729+
return swift::DiagnosticArgument(unbridged().getDecl()->getName());
1730+
}
1731+
17061732
//===----------------------------------------------------------------------===//
17071733
// BridgedVTable
17081734
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)