|
| 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 Basic |
| 14 | +import ASTBridging |
| 15 | + |
| 16 | +/// The base class for all declarations in Swift. |
| 17 | +@_semantics("arc.immortal") |
| 18 | +public class Decl: CustomStringConvertible, Hashable { |
| 19 | + public var bridged: BridgedDeclObj { BridgedDeclObj(SwiftObject(self)) } |
| 20 | + |
| 21 | + public var description: String { String(taking: bridged.getDebugDescription()) } |
| 22 | + |
| 23 | + public static func ==(lhs: Decl, rhs: Decl) -> Bool { lhs === rhs } |
| 24 | + |
| 25 | + public func hash(into hasher: inout Hasher) { |
| 26 | + hasher.combine(ObjectIdentifier(self)) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +public class ValueDecl: Decl { |
| 31 | + final public var nameLoc: SourceLoc? { SourceLoc(bridged: bridged.Value_getNameLoc()) } |
| 32 | + final public var userFacingName: StringRef { StringRef(bridged: bridged.Value_getUserFacingName()) } |
| 33 | + final public var isObjC: Bool { bridged.Value_isObjC() } |
| 34 | +} |
| 35 | + |
| 36 | +public class TypeDecl: ValueDecl { |
| 37 | + final public var name: StringRef { StringRef(bridged: bridged.Type_getName()) } |
| 38 | +} |
| 39 | + |
| 40 | +public class GenericTypeDecl: TypeDecl { |
| 41 | + final public var isGenericAtAnyLevel: Bool { bridged.GenericType_isGenericAtAnyLevel() } |
| 42 | +} |
| 43 | + |
| 44 | +public class NominalTypeDecl: GenericTypeDecl { |
| 45 | + final public var isGlobalActor: Bool { bridged.NominalType_isGlobalActor() } |
| 46 | + |
| 47 | + final public var valueTypeDestructor: DestructorDecl? { |
| 48 | + bridged.NominalType_getValueTypeDestructor().getAs(DestructorDecl.self) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +final public class EnumDecl: NominalTypeDecl {} |
| 53 | + |
| 54 | +final public class StructDecl: NominalTypeDecl { |
| 55 | + public var hasUnreferenceableStorage: Bool { bridged.Struct_hasUnreferenceableStorage() } |
| 56 | +} |
| 57 | + |
| 58 | +final public class ClassDecl: NominalTypeDecl { |
| 59 | + public var superClass: Type? { Type(bridgedOrNil: bridged.Class_getSuperclass()) } |
| 60 | +} |
| 61 | + |
| 62 | +final public class ProtocolDecl: NominalTypeDecl {} |
| 63 | + |
| 64 | +final public class BuiltinTupleDecl: NominalTypeDecl {} |
| 65 | + |
| 66 | +final public class OpaqueTypeDecl: GenericTypeDecl {} |
| 67 | + |
| 68 | +final public class TypeAliasDecl: GenericTypeDecl {} |
| 69 | + |
| 70 | +final public class GenericTypeParamDecl: TypeDecl {} |
| 71 | + |
| 72 | +final public class AssociatedTypeDecl: TypeDecl {} |
| 73 | + |
| 74 | +final public class ModuleDecl: TypeDecl {} |
| 75 | + |
| 76 | +public class AbstractStorageDecl: ValueDecl {} |
| 77 | + |
| 78 | +public class VarDecl: AbstractStorageDecl {} |
| 79 | + |
| 80 | +final public class ParamDecl: VarDecl {} |
| 81 | + |
| 82 | +final public class SubscriptDecl: AbstractStorageDecl {} |
| 83 | + |
| 84 | +public class AbstractFunctionDecl: ValueDecl {} |
| 85 | + |
| 86 | +final public class ConstructorDecl: AbstractFunctionDecl {} |
| 87 | + |
| 88 | +final public class DestructorDecl: AbstractFunctionDecl {} |
| 89 | + |
| 90 | +public class FuncDecl: AbstractFunctionDecl {} |
| 91 | + |
| 92 | +final public class AccessorDecl: FuncDecl {} |
| 93 | + |
| 94 | +final public class MacroDecl: ValueDecl {} |
| 95 | + |
| 96 | +final public class EnumElementDecl: ValueDecl {} |
| 97 | + |
| 98 | +final public class ExtensionDecl: Decl {} |
| 99 | + |
| 100 | +final public class TopLevelCodeDecl: Decl {} |
| 101 | + |
| 102 | +final public class ImportDecl: Decl {} |
| 103 | + |
| 104 | +final public class PoundDiagnosticDecl: Decl {} |
| 105 | + |
| 106 | +final public class PrecedenceGroupDecl: Decl {} |
| 107 | + |
| 108 | +final public class MissingDecl: Decl {} |
| 109 | + |
| 110 | +final public class MissingMemberDecl: Decl {} |
| 111 | + |
| 112 | +final public class PatternBindingDecl: Decl {} |
| 113 | + |
| 114 | +final public class EnumCaseDecl: Decl {} |
| 115 | + |
| 116 | +public class OperatorDecl: Decl {} |
| 117 | + |
| 118 | +final public class InfixOperatorDecl: OperatorDecl {} |
| 119 | + |
| 120 | +final public class PrefixOperatorDecl: OperatorDecl {} |
| 121 | + |
| 122 | +final public class PostfixOperatorDecl: OperatorDecl {} |
| 123 | + |
| 124 | +final public class MacroExpansionDecl: Decl {} |
| 125 | + |
| 126 | +// Bridging utilities |
| 127 | + |
| 128 | +extension BridgedDeclObj { |
| 129 | + public var decl: Decl { obj.getAs(Decl.self) } |
| 130 | + public func getAs<T: Decl>(_ declType: T.Type) -> T { obj.getAs(T.self) } |
| 131 | +} |
| 132 | + |
| 133 | +extension OptionalBridgedDeclObj { |
| 134 | + public var decl: Decl? { obj.getAs(Decl.self) } |
| 135 | + public func getAs<T: Decl>(_ declType: T.Type) -> T? { obj.getAs(T.self) } |
| 136 | +} |
0 commit comments