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
@@ -23,19 +23,22 @@ public class Decl: CustomStringConvertible, Hashable {
2323 /// The module in which this declaration resides.
2424 final public var parentModule : ModuleDecl { bridged. getModuleContext ( ) . getAs ( ModuleDecl . self) }
2525
26- /// The parent DeclContext if it is a Decl.
27- final public var parent : Decl ? { bridged. getParent ( ) . decl }
28-
29- // True if this declaration is imported from C/C++/ObjC.
30- final public var hasClangNode : Bool { bridged. hasClangNode ( ) }
31-
32- final public var declContext : DeclContext {
33- if let decl = parent {
26+ /// The parent DeclContext.
27+ final public var parentDeclContext : DeclContext ? {
28+ if let decl = bridged. getParent ( ) . decl {
3429 return decl as! DeclContext
3530 }
36- return UnknownDeclContext ( bridged: bridged. getDeclContext ( ) )
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
3737 }
3838
39+ // True if this declaration is imported from C/C++/ObjC.
40+ final public var hasClangNode : Bool { bridged. hasClangNode ( ) }
41+
3942 final public var bridgedDecl : BridgedDecl { BridgedDecl ( raw: bridged. obj) }
4043
4144 public static func == ( lhs: Decl , rhs: Decl ) -> Bool { lhs === rhs }
@@ -153,7 +156,9 @@ final public class GenericTypeParamDecl: TypeDecl {
153156
154157final public class AssociatedTypeDecl : TypeDecl { }
155158
156- final public class ModuleDecl : TypeDecl { }
159+ final public class ModuleDecl : TypeDecl , DeclContext {
160+ public var bridgedDeclContext : BridgedDeclContext { bridged. asModuleDecl ( ) }
161+ }
157162
158163public class AbstractStorageDecl : ValueDecl {
159164 final public var isConst : Bool { bridged. AbstractStorage_isConst ( ) }
@@ -171,9 +176,9 @@ final public class ParamDecl: VarDecl {
171176 }
172177}
173178
174- final public class SubscriptDecl : AbstractStorageDecl { }
179+ final public class SubscriptDecl : AbstractStorageDecl , GenericContext { }
175180
176- public class AbstractFunctionDecl : ValueDecl {
181+ public class AbstractFunctionDecl : ValueDecl , GenericContext {
177182 public var isOverridden : Bool { bridged. AbstractFunction_isOverridden ( ) }
178183}
179184
@@ -209,9 +214,11 @@ final public class EnumElementDecl: ValueDecl {
209214 }
210215}
211216
212- final public class ExtensionDecl : Decl { }
217+ final public class ExtensionDecl : Decl , GenericContext { }
213218
214- final public class TopLevelCodeDecl : Decl { }
219+ final public class TopLevelCodeDecl : Decl , DeclContext {
220+ public var bridgedDeclContext : BridgedDeclContext { bridged. asTopLevelCodeDecl ( ) }
221+ }
215222
216223final public class ImportDecl : Decl { }
217224
@@ -320,9 +327,17 @@ extension GenericParameterList {
320327
321328extension BridgedDecl {
322329 public var declObj : BridgedDeclObj {
323- BridgedDeclObj ( SwiftObject ( raw. bindMemory ( to: BridgedSwiftObject . self, capacity: 1 ) ) )
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)
324340 }
325- public var decl : Decl { declObj. decl }
326341}
327342
328343extension SourceFile {
0 commit comments