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 " )
1818public 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
3963public 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
4573public 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
6197final 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
65121final public class StructDecl : NominalTypeDecl {
@@ -84,23 +140,45 @@ final public class OpaqueTypeDecl: GenericTypeDecl {}
84140
85141final 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
89157final 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
93163public class AbstractStorageDecl : ValueDecl {
94164 final public var isConst : Bool { bridged. AbstractStorage_isConst ( ) }
95165}
96166
97167public 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
119197final 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
127223final 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+ }
0 commit comments