Files
swift-mirror/SwiftCompilerSources/Sources/SIL/GlobalVariable.swift
Erik Eckstein 10782cf42b SwiftCompilerSources: introduce the AST module
As the optimizer uses more and more AST stuff, it's now time to create an "AST" module.
Initially it defines following AST datastructures:
* declarations: `Decl` + derived classes
* `Conformance`
* `SubstitutionMap`
* `Type` and `CanonicalType`

Some of those were already defined in the SIL module and are now moved to the AST module.
This change also cleans up a few things:
* proper definition of `NominalTypeDecl`-related APIs in `SIL.Type`
* rename `ProtocolConformance` to `Conformance`
* use `AST.Type`/`AST.CanonicalType` instead of `BridgedASTType` in SIL and the Optimizer
2024-10-02 07:10:29 +02:00

102 lines
3.0 KiB
Swift

//===--- GlobalVariable.swift - Defines the GlobalVariable class ----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basic
import AST
import SILBridging
final public class GlobalVariable : CustomStringConvertible, HasShortDescription, Hashable {
public var varDecl: VarDecl? {
bridged.getDecl().getAs(VarDecl.self)
}
public var name: StringRef {
return StringRef(bridged: bridged.getName())
}
public var description: String {
return String(taking: bridged.getDebugDescription())
}
public var shortDescription: String { name.string }
public var isLet: Bool { bridged.isLet() }
public var linkage: Linkage { bridged.getLinkage().linkage }
/// True, if the linkage of the global variable indicates that it is visible outside the current
/// compilation unit and therefore not all of its uses are known.
///
/// For example, `public` linkage.
public var isPossiblyUsedExternally: Bool {
return bridged.isPossiblyUsedExternally()
}
/// True, if the linkage of the global indicates that it has a definition outside the
/// current compilation unit.
///
/// For example, `public_external` linkage.
public var isDefinedExternally: Bool { linkage.isExternal }
public var staticInitializerInstructions: InstructionList? {
if let firstStaticInitInst = bridged.getFirstStaticInitInst().instruction {
return InstructionList(first: firstStaticInitInst)
}
return nil
}
public var staticInitValue: SingleValueInstruction? {
if let staticInitInsts = staticInitializerInstructions {
return staticInitInsts.reversed().first! as? SingleValueInstruction
}
return nil
}
/// True if the global's linkage and resilience expansion allow the global
/// to be initialized statically.
public var canBeInitializedStatically: Bool {
return bridged.canBeInitializedStatically()
}
public var mustBeInitializedStatically: Bool {
return bridged.mustBeInitializedStatically()
}
public static func ==(lhs: GlobalVariable, rhs: GlobalVariable) -> Bool {
lhs === rhs
}
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
public var bridged: BridgedGlobalVar { BridgedGlobalVar(SwiftObject(self)) }
}
// Bridging utilities
extension BridgedGlobalVar {
public var globalVar: GlobalVariable { obj.getAs(GlobalVariable.self) }
public var optional: OptionalBridgedGlobalVar {
OptionalBridgedGlobalVar(obj: self.obj)
}
}
extension OptionalBridgedGlobalVar {
public var globalVar: GlobalVariable? { obj.getAs(GlobalVariable.self) }
public static var none: OptionalBridgedGlobalVar {
OptionalBridgedGlobalVar(obj: nil)
}
}