Skip to content

Commit 2c9013b

Browse files
authored
Merge pull request #84648 from swiftlang/users/kovdan01/ast-bridges-for-autodiff-closure-spec
Bridging: Implement bridges required for ongoing AutoDiff changes
2 parents d0df110 + 5bd3d80 commit 2c9013b

23 files changed

+709
-27
lines changed

SwiftCompilerSources/Sources/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_swift_compiler_module(AST
1111
Basic
1212
SOURCES
1313
Declarations.swift
14+
DeclContext.swift
1415
Conformance.swift
1516
DiagnosticEngine.swift
1617
GenericSignature.swift
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===--- DeclContext.swift -----------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 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 ASTBridging
14+
15+
public protocol DeclContext : AnyObject {
16+
var bridgedDeclContext: BridgedDeclContext { get }
17+
}
18+
19+
extension DeclContext {
20+
public var astContext: ASTContext { bridgedDeclContext.astContext }
21+
}
22+
23+
// Used for DeclContext classes which are not Decls and are not bridged, yet. E.g. `FileUnit`.
24+
// TODO: once we have bridged those DeclContext classes, get rid of UnknownDeclContext
25+
public class UnknownDeclContext : DeclContext {
26+
public var bridgedDeclContext: BridgedDeclContext
27+
public init(bridged: BridgedDeclContext) { bridgedDeclContext = bridged }
28+
}

SwiftCompilerSources/Sources/AST/Declarations.swift

Lines changed: 234 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -16,37 +16,65 @@ import ASTBridging
1616
/// The base class for all declarations in Swift.
1717
@_semantics("arc.immortal")
1818
public class Decl: CustomStringConvertible, Hashable {
19-
public var bridged: BridgedDeclObj { BridgedDeclObj(SwiftObject(self)) }
19+
final public var bridged: BridgedDeclObj { BridgedDeclObj(SwiftObject(self)) }
2020

21-
public var description: String { String(taking: bridged.getDebugDescription()) }
21+
final public var description: String { String(taking: bridged.getDebugDescription()) }
2222

2323
/// The module in which this declaration resides.
24-
public var parentModule: ModuleDecl { bridged.getModuleContext().getAs(ModuleDecl.self) }
25-
26-
/// The parent DeclContext if it is a Decl.
27-
public var parent: Decl? { bridged.getParent().decl }
24+
final public var parentModule: ModuleDecl { bridged.getModuleContext().getAs(ModuleDecl.self) }
25+
26+
/// The parent DeclContext.
27+
final public var parentDeclContext: DeclContext? {
28+
if let decl = bridged.getParent().decl {
29+
return decl as! DeclContext
30+
}
31+
if let bridgedDeclContext = BridgedDeclContext(bridged: bridged.getDeclContext()) {
32+
// A DeclContext which is not a Decl.
33+
// TODO: once we have bridged those DeclContext classes, get rid of UnknownDeclContext
34+
return UnknownDeclContext(bridged: bridgedDeclContext)
35+
}
36+
return nil
37+
}
2838

2939
// True if this declaration is imported from C/C++/ObjC.
30-
public var hasClangNode: Bool { bridged.hasClangNode() }
40+
final public var hasClangNode: Bool { bridged.hasClangNode() }
41+
42+
final public var bridgedDecl: BridgedDecl { BridgedDecl(raw: bridged.obj) }
3143

3244
public static func ==(lhs: Decl, rhs: Decl) -> Bool { lhs === rhs }
3345

34-
public func hash(into hasher: inout Hasher) {
46+
final public func hash(into hasher: inout Hasher) {
3547
hasher.combine(ObjectIdentifier(self))
3648
}
49+
50+
final public func setImplicit() { bridged.setImplicit() }
51+
}
52+
53+
public protocol GenericContext: Decl, DeclContext {}
54+
55+
extension GenericContext {
56+
public func setGenericSignature(_ genericSignature: GenericSignature) {
57+
bridged.GenericContext_setGenericSignature(genericSignature.bridged)
58+
}
59+
60+
public var bridgedDeclContext: BridgedDeclContext { bridged.asGenericContext() }
3761
}
3862

3963
public class ValueDecl: Decl {
4064
final public var nameLoc: SourceLoc? { SourceLoc(bridged: bridged.Value_getNameLoc()) }
4165
final public var userFacingName: StringRef { StringRef(bridged: bridged.Value_getUserFacingName()) }
66+
final public var baseIdentifier: Identifier { bridged.Value_getBaseIdentifier() }
4267
final public var isObjC: Bool { bridged.Value_isObjC() }
68+
final public func setAccess(_ accessLevel : AccessLevel) {
69+
bridged.ValueDecl_setAccess(accessLevel)
70+
}
4371
}
4472

4573
public class TypeDecl: ValueDecl {
4674
final public var name: StringRef { StringRef(bridged: bridged.Type_getName()) }
4775
}
4876

49-
public class GenericTypeDecl: TypeDecl {
77+
public class GenericTypeDecl: TypeDecl, GenericContext {
5078
final public var isGenericAtAnyLevel: Bool { bridged.GenericType_isGenericAtAnyLevel() }
5179
}
5280

@@ -56,10 +84,38 @@ public class NominalTypeDecl: GenericTypeDecl {
5684
final public var valueTypeDestructor: DestructorDecl? {
5785
bridged.NominalType_getValueTypeDestructor().getAs(DestructorDecl.self)
5886
}
87+
88+
public var declaredInterfaceType: Type {
89+
Type(bridged: bridged.NominalType_getDeclaredInterfaceType())
90+
}
91+
92+
public func add(member: Decl) {
93+
bridged.NominalTypeDecl_addMember(member.bridged)
94+
}
5995
}
6096

6197
final public class EnumDecl: NominalTypeDecl {
6298
public var hasRawType: Bool { bridged.Enum_hasRawType() }
99+
100+
public static func create(
101+
declContext: DeclContext, enumKeywordLoc: SourceLoc?, name: String,
102+
nameLoc: SourceLoc?, genericParamList: GenericParameterList?, inheritedTypes: [Type],
103+
genericWhereClause: TrailingWhereClause?, braceRange: SourceRange, _ astContext: ASTContext
104+
) -> EnumDecl {
105+
name.withCString { strPtr in
106+
inheritedTypes.withBridgedArrayRef { types in
107+
ASTBridging.BridgedEnumDecl.createParsed(
108+
astContext, declContext: declContext.bridgedDeclContext,
109+
enumKeywordLoc: enumKeywordLoc.bridgedLocation,
110+
name: astContext.getIdentifier(BridgedStringRef(data: strPtr, count: name.count)),
111+
nameLoc: nameLoc.bridgedLocation,
112+
genericParamList: genericParamList.bridged,
113+
inheritedTypes: types,
114+
genericWhereClause: genericWhereClause.bridged,
115+
braceRange: braceRange.bridged)
116+
}
117+
}.asDecl.declObj.getAs(EnumDecl.self)
118+
}
63119
}
64120

65121
final public class StructDecl: NominalTypeDecl {
@@ -84,23 +140,45 @@ final public class OpaqueTypeDecl: GenericTypeDecl {}
84140

85141
final public class TypeAliasDecl: GenericTypeDecl {}
86142

87-
final public class GenericTypeParamDecl: TypeDecl {}
143+
final public class GenericTypeParamDecl: TypeDecl {
144+
public static func create(
145+
declContext: DeclContext,
146+
name: Identifier,
147+
depth: Int,
148+
index: Int,
149+
paramKind: GenericTypeParameterKind) -> GenericTypeParamDecl {
150+
ASTBridging.BridgedGenericTypeParamDecl.createImplicit(
151+
declContext: declContext.bridgedDeclContext,
152+
name: name, depth: depth, index: index,
153+
paramKind: paramKind).asDecl.declObj.getAs(GenericTypeParamDecl.self)
154+
}
155+
}
88156

89157
final public class AssociatedTypeDecl: TypeDecl {}
90158

91-
final public class ModuleDecl: TypeDecl {}
159+
final public class ModuleDecl: TypeDecl, DeclContext {
160+
public var bridgedDeclContext: BridgedDeclContext { bridged.asModuleDecl() }
161+
}
92162

93163
public class AbstractStorageDecl: ValueDecl {
94164
final public var isConst: Bool { bridged.AbstractStorage_isConst() }
95165
}
96166

97167
public class VarDecl: AbstractStorageDecl {}
98168

99-
final public class ParamDecl: VarDecl {}
169+
final public class ParamDecl: VarDecl {
170+
public func cloneWithoutType() -> ParamDecl {
171+
BridgedParamDecl(raw: bridged.obj).cloneWithoutType().asDecl.declObj.getAs(ParamDecl.self)
172+
}
100173

101-
final public class SubscriptDecl: AbstractStorageDecl {}
174+
public func setInterfaceType(type: Type) {
175+
BridgedParamDecl(raw: bridged.obj).setInterfaceType(type.bridged)
176+
}
177+
}
102178

103-
public class AbstractFunctionDecl: ValueDecl {
179+
final public class SubscriptDecl: AbstractStorageDecl, GenericContext {}
180+
181+
public class AbstractFunctionDecl: ValueDecl, GenericContext {
104182
public var isOverridden: Bool { bridged.AbstractFunction_isOverridden() }
105183
}
106184

@@ -118,11 +196,29 @@ final public class MacroDecl: ValueDecl {}
118196

119197
final public class EnumElementDecl: ValueDecl {
120198
public var hasAssociatedValues: Bool { bridged.EnumElementDecl_hasAssociatedValues() }
199+
public var parameterList: ParameterList { ParameterList(bridged: bridged.EnumElementDecl_getParameterList()) }
200+
public var name: StringRef { StringRef(bridged: bridged.EnumElementDecl_getNameStr()) }
201+
202+
public static func create(
203+
declContext: DeclContext,
204+
name: Identifier, nameLoc: SourceLoc?,
205+
parameterList: ParameterList?,
206+
equalsLoc: SourceLoc?, rawValue: Expr?, _ astContext: ASTContext
207+
) -> EnumElementDecl {
208+
BridgedEnumElementDecl.createParsed(
209+
astContext, declContext: declContext.bridgedDeclContext,
210+
name: name, nameLoc: nameLoc.bridgedLocation,
211+
parameterList: parameterList.bridged.bridged,
212+
equalsLoc: equalsLoc.bridgedLocation,
213+
rawValue: rawValue.bridged).asDecl.declObj.getAs(EnumElementDecl.self)
214+
}
121215
}
122216

123-
final public class ExtensionDecl: Decl {}
217+
final public class ExtensionDecl: Decl, GenericContext {}
124218

125-
final public class TopLevelCodeDecl: Decl {}
219+
final public class TopLevelCodeDecl: Decl, DeclContext {
220+
public var bridgedDeclContext: BridgedDeclContext { bridged.asTopLevelCodeDecl() }
221+
}
126222

127223
final public class ImportDecl: Decl {}
128224

@@ -165,3 +261,124 @@ extension Optional where Wrapped == Decl {
165261
OptionalBridgedDeclObj(self?.bridged.obj)
166262
}
167263
}
264+
265+
public typealias AccessLevel = swift.AccessLevel
266+
267+
public typealias Identifier = swift.Identifier
268+
269+
public typealias GenericTypeParamKind = swift.GenericTypeParamKind
270+
271+
public typealias ASTContext = BridgedASTContext
272+
273+
public typealias Expr = BridgedExpr
274+
275+
public typealias SourceFile = BridgedSourceFile
276+
277+
public typealias FileUnit = BridgedFileUnit
278+
279+
public class GenericParameterList {
280+
public var bridged: BridgedGenericParamList
281+
public init(bridged: BridgedGenericParamList) { self.bridged = bridged }
282+
}
283+
284+
public typealias TrailingWhereClause = BridgedTrailingWhereClause
285+
286+
public class ParameterList : RandomAccessCollection {
287+
public var startIndex: Int { 0 }
288+
public var endIndex: Int { bridged.size }
289+
290+
public var bridged: BridgedParameterList
291+
292+
public init(bridged: BridgedParameterList) {
293+
self.bridged = bridged
294+
}
295+
296+
public subscript(_ index: Int) -> ParamDecl {
297+
return bridged.get(index).asDecl.declObj.getAs(ParamDecl.self)
298+
}
299+
300+
public static func create(
301+
leftParenLoc: SourceLoc?, parameters: [ParamDecl],
302+
rightParenLoc: SourceLoc?, _ astContext: ASTContext
303+
) -> ParameterList {
304+
ParameterList(bridged: parameters.map{BridgedParamDecl(raw: $0.bridged.obj)}.withBridgedArrayRef {
305+
BridgedParameterList.createParsed(
306+
astContext, leftParenLoc: leftParenLoc.bridgedLocation, parameters: $0,
307+
rightParenLoc: rightParenLoc.bridgedLocation)
308+
})
309+
}
310+
}
311+
312+
extension GenericParameterList {
313+
public static func create(
314+
leftAngleLoc: SourceLoc?, parameters: [GenericTypeParamDecl],
315+
genericWhereClause: TrailingWhereClause?,
316+
rightAngleLoc: SourceLoc?, _ astContext: ASTContext
317+
) -> GenericParameterList {
318+
let paramsNew = parameters.map{ ASTBridging.BridgedGenericTypeParamDecl(raw: $0.bridged.obj) }
319+
return paramsNew.withBridgedArrayRef {
320+
GenericParameterList(bridged: BridgedGenericParamList.createParsed(
321+
astContext, leftAngleLoc: leftAngleLoc.bridgedLocation, parameters: $0,
322+
genericWhereClause: genericWhereClause.bridged, rightAngleLoc: rightAngleLoc.bridgedLocation
323+
))
324+
}
325+
}
326+
}
327+
328+
extension BridgedDecl {
329+
public var declObj: BridgedDeclObj {
330+
BridgedDeclObj(self)
331+
}
332+
}
333+
334+
extension BridgedDeclContext {
335+
public init?(bridged: BridgedNullableDeclContext) {
336+
guard let raw = bridged.raw else {
337+
return nil
338+
}
339+
self.init(raw: raw)
340+
}
341+
}
342+
343+
extension SourceFile {
344+
public init?(bridged: BridgedNullableSourceFile) {
345+
guard let raw = bridged.raw else {
346+
return nil
347+
}
348+
self.init(raw: raw)
349+
}
350+
}
351+
352+
extension FileUnit {
353+
public var asSourceFile: SourceFile? { SourceFile(bridged: self.castToSourceFile()) }
354+
}
355+
356+
extension ParameterList? {
357+
public var bridged: BridgedParameterList? {
358+
return self?.bridged
359+
}
360+
}
361+
362+
extension BridgedParameterList? {
363+
public var bridged: BridgedNullableParameterList {
364+
BridgedNullableParameterList(raw: self?.raw)
365+
}
366+
}
367+
368+
extension GenericParameterList? {
369+
public var bridged: BridgedNullableGenericParamList {
370+
BridgedNullableGenericParamList(raw: self?.bridged.raw)
371+
}
372+
}
373+
374+
extension Expr? {
375+
public var bridged: BridgedNullableExpr {
376+
BridgedNullableExpr(raw: self?.raw)
377+
}
378+
}
379+
380+
extension TrailingWhereClause? {
381+
public var bridged: BridgedNullableTrailingWhereClause {
382+
BridgedNullableTrailingWhereClause(raw: self?.raw)
383+
}
384+
}

SwiftCompilerSources/Sources/AST/GenericSignature.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,22 @@ public struct GenericSignature: CustomStringConvertible, NoReflectionChildren {
3535
}
3636

3737
public var isEmpty: Bool { bridged.impl == nil }
38+
39+
public var canonicalSignature: CanonicalGenericSignature {
40+
CanonicalGenericSignature(bridged: bridged.getCanonicalSignature())
41+
}
42+
}
43+
44+
public struct CanonicalGenericSignature {
45+
public let bridged: BridgedCanGenericSignature
46+
47+
public init(bridged: BridgedCanGenericSignature) {
48+
self.bridged = bridged
49+
}
50+
51+
public var isEmpty: Bool { bridged.impl == nil }
52+
53+
public var genericSignature: GenericSignature {
54+
GenericSignature(bridged: bridged.getGenericSignature())
55+
}
3856
}

0 commit comments

Comments
 (0)