forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobalVariable.swift
101 lines (80 loc) · 3.03 KB
/
GlobalVariable.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//===--- 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)
}
}