Skip to content

Commit a172134

Browse files
committed
Address review comments
1 parent c12819f commit a172134

File tree

6 files changed

+61
-21
lines changed

6 files changed

+61
-21
lines changed

SwiftCompilerSources/Sources/AST/DeclContext.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
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+
113
import ASTBridging
214

315
public protocol DeclContext : AnyObject {
@@ -8,6 +20,8 @@ extension DeclContext {
820
public var astContext: ASTContext { bridgedDeclContext.astContext }
921
}
1022

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
1125
public class UnknownDeclContext : DeclContext {
1226
public var bridgedDeclContext: BridgedDeclContext
1327
public init(bridged: BridgedDeclContext) { bridgedDeclContext = bridged }

SwiftCompilerSources/Sources/AST/Declarations.swift

Lines changed: 32 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
@@ -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

154157
final 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

158163
public 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

216223
final public class ImportDecl: Decl {}
217224

@@ -320,9 +327,17 @@ extension GenericParameterList {
320327

321328
extension 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

328343
extension SourceFile {

SwiftCompilerSources/Sources/Optimizer/ModulePasses/EmbeddedSwiftDiagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ private extension Function {
347347
if decl is DestructorDecl || decl is ConstructorDecl {
348348
return 4
349349
}
350-
if let parent = decl.parent, parent is ClassDecl {
350+
if let parent = decl.parentDeclContext, parent is ClassDecl {
351351
return 2
352352
}
353353
}

include/swift/AST/ASTBridging.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,15 @@ struct BridgedDeclObj {
318318
#endif
319319

320320
BridgedDeclObj(SwiftObject obj) : obj(obj) {}
321+
BridgedDeclObj(BridgedDecl decl) : obj(decl.unbridged()) {}
321322
BridgedOwnedString getDebugDescription() const;
322323
BRIDGED_INLINE swift::SourceLoc getLoc() const;
323324
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclObj getModuleContext() const;
324325
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj getParent() const;
325-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclContext getDeclContext() const;
326+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedNullableDeclContext getDeclContext() const;
326327
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclContext asGenericContext() const;
328+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclContext asTopLevelCodeDecl() const;
329+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclContext asModuleDecl() const;
327330
BRIDGED_INLINE void setImplicit() const;
328331
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef Type_getName() const;
329332
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef Value_getUserFacingName() const;

include/swift/AST/ASTBridgingImpl.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,22 @@ OptionalBridgedDeclObj BridgedDeclObj::getParent() const {
177177
return {unbridged()->getDeclContext()->getAsDecl()};
178178
}
179179

180-
BridgedDeclContext BridgedDeclObj::getDeclContext() const {
180+
BridgedNullableDeclContext BridgedDeclObj::getDeclContext() const {
181181
return {unbridged()->getDeclContext()};
182182
}
183183

184184
BridgedDeclContext BridgedDeclObj::asGenericContext() const {
185185
return {static_cast<swift::DeclContext *>(getAs<swift::GenericContext>())};
186186
}
187187

188+
BridgedDeclContext BridgedDeclObj::asTopLevelCodeDecl() const {
189+
return {static_cast<swift::DeclContext *>(getAs<swift::TopLevelCodeDecl>())};
190+
}
191+
192+
BridgedDeclContext BridgedDeclObj::asModuleDecl() const {
193+
return {static_cast<swift::DeclContext *>(getAs<swift::ModuleDecl>())};
194+
}
195+
188196
void BridgedDeclObj::setImplicit() const {
189197
unbridged()->setImplicit();
190198
}

include/swift/AST/ASTBridgingWrappers.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
// Some of the base classes need to be nullable to allow them to be used as
9595
// optional parameters.
9696
AST_BRIDGING_WRAPPER_NULLABLE(Decl)
97-
AST_BRIDGING_WRAPPER_NONNULL(DeclContext)
97+
AST_BRIDGING_WRAPPER_NULLABLE(DeclContext)
9898
AST_BRIDGING_WRAPPER_NONNULL(GenericContext)
9999
AST_BRIDGING_WRAPPER_NONNULL(FileUnit)
100100
AST_BRIDGING_WRAPPER_NULLABLE(SourceFile)

0 commit comments

Comments
 (0)