Skip to content

Commit a93dc8f

Browse files
authored
Merge branch 'main' into shahmishal-patch-2
2 parents e8f6173 + c72e57e commit a93dc8f

File tree

292 files changed

+6926
-2132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

292 files changed

+6926
-2132
lines changed

Runtimes/Core/core/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ target_compile_options(swiftCore PRIVATE
301301
$<$<COMPILE_LANGUAGE:Swift>:-nostdimport>
302302
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -group-info-path -Xfrontend ${CMAKE_CURRENT_SOURCE_DIR}/GroupInfo.json>"
303303
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-objc-attr-requires-foundation-module>"
304-
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -require-explicit-availability=ignore>")
304+
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -require-explicit-availability=ignore>"
305+
"$<$<AND:$<PLATFORM_ID:Darwin>,$<COMPILE_LANGUAGE:Swift>>:SHELL:-Xfrontend -previous-module-installname-map-file -Xfrontend ${CMAKE_CURRENT_SOURCE_DIR}/PreviousModuleInstallName.json>")
305306
if(NOT "${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
306307
# Using these in MinSizeRel would result in a 15% increase in the binary size
307308
target_compile_options(swiftCore PRIVATE

SwiftCompilerSources/Sources/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_swift_compiler_module(AST
1212
SOURCES
1313
Declarations.swift
1414
Conformance.swift
15+
DiagnosticEngine.swift
1516
GenericSignature.swift
1617
Registration.swift
1718
SubstitutionMap.swift

SwiftCompilerSources/Sources/AST/Conformance.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import ASTBridging
1717
/// members to the type (or extension) members that provide the functionality for the concrete type.
1818
///
1919
/// TODO: Ideally, `Conformance` should be an enum
20-
public struct Conformance: CustomStringConvertible, NoReflectionChildren {
20+
public struct Conformance: CustomStringConvertible, Hashable, NoReflectionChildren {
2121
public let bridged: BridgedConformance
2222

2323
public init(bridged: BridgedConformance) {
@@ -28,6 +28,14 @@ public struct Conformance: CustomStringConvertible, NoReflectionChildren {
2828
return String(taking: bridged.getDebugDescription())
2929
}
3030

31+
public func hash(into hasher: inout Hasher) {
32+
hasher.combine(bridged.opaqueValue)
33+
}
34+
35+
public static func ==(lhs: Conformance, rhs: Conformance) -> Bool {
36+
lhs.bridged.opaqueValue == rhs.bridged.opaqueValue
37+
}
38+
3139
public var isConcrete: Bool { bridged.isConcrete() }
3240

3341
public var isValid: Bool { bridged.isValid() }
@@ -37,7 +45,7 @@ public struct Conformance: CustomStringConvertible, NoReflectionChildren {
3745
return Type(bridged: bridged.getType())
3846
}
3947

40-
public var proto: ProtocolDecl {
48+
public var `protocol`: ProtocolDecl {
4149
return bridged.getRequirement().getAs(ProtocolDecl.self)
4250
}
4351
public var isSpecialized: Bool {

SwiftCompilerSources/Sources/AST/Declarations.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public class Decl: CustomStringConvertible, Hashable {
2323
/// The module in which this declaration resides.
2424
public var parentModule: ModuleDecl { bridged.getModuleContext().getAs(ModuleDecl.self) }
2525

26+
/// The parent DeclContext if it is a Decl.
27+
public var parent: Decl? { bridged.getParent().decl }
28+
2629
// True if this declaration is imported from C/C++/ObjC.
2730
public var hasClangNode: Bool { bridged.hasClangNode() }
2831

@@ -69,7 +72,9 @@ final public class ClassDecl: NominalTypeDecl {
6972
}
7073
}
7174

72-
final public class ProtocolDecl: NominalTypeDecl {}
75+
final public class ProtocolDecl: NominalTypeDecl {
76+
public var requiresClass: Bool { bridged.ProtocolDecl_requiresClass() }
77+
}
7378

7479
final public class BuiltinTupleDecl: NominalTypeDecl {}
7580

SwiftCompilerSources/Sources/Optimizer/Utilities/DiagnosticEngine.swift renamed to SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,34 @@
1313
import ASTBridging
1414

1515
import Basic
16-
import SIL
1716

18-
typealias DiagID = BridgedDiagID
17+
public typealias DiagID = BridgedDiagID
1918

20-
protocol DiagnosticArgument {
19+
public protocol DiagnosticArgument {
2120
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void)
2221
}
2322
extension String: DiagnosticArgument {
24-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
23+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
2524
_withBridgedStringRef { fn(BridgedDiagnosticArgument($0)) }
2625
}
2726
}
2827
extension StringRef: DiagnosticArgument {
29-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
28+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
3029
fn(BridgedDiagnosticArgument(_bridged))
3130
}
3231
}
3332
extension Int: DiagnosticArgument {
34-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
33+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
3534
fn(BridgedDiagnosticArgument(self))
3635
}
3736
}
3837
extension Type: DiagnosticArgument {
39-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
40-
fn(bridged.asDiagnosticArgument())
41-
}
42-
}
43-
extension DeclRef: DiagnosticArgument {
44-
func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
38+
public func _withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
4539
fn(bridged.asDiagnosticArgument())
4640
}
4741
}
4842

49-
struct DiagnosticFixIt {
43+
public struct DiagnosticFixIt {
5044
let start: SourceLoc
5145
let byteLength: Int
5246
let text: String
@@ -67,10 +61,10 @@ struct DiagnosticFixIt {
6761
}
6862
}
6963

70-
struct DiagnosticEngine {
64+
public struct DiagnosticEngine {
7165
private let bridged: BridgedDiagnosticEngine
7266

73-
init(bridged: BridgedDiagnosticEngine) {
67+
public init(bridged: BridgedDiagnosticEngine) {
7468
self.bridged = bridged
7569
}
7670
init?(bridged: BridgedNullableDiagnosticEngine) {
@@ -80,9 +74,9 @@ struct DiagnosticEngine {
8074
self.bridged = BridgedDiagnosticEngine(raw: raw)
8175
}
8276

83-
func diagnose(_ position: SourceLoc?,
84-
_ id: DiagID,
77+
public func diagnose(_ id: DiagID,
8578
_ args: [DiagnosticArgument],
79+
at position: SourceLoc?,
8680
highlight: CharSourceRange? = nil,
8781
fixIts: [DiagnosticFixIt] = []) {
8882

@@ -136,11 +130,32 @@ struct DiagnosticEngine {
136130
closure()
137131
}
138132

139-
func diagnose(_ position: SourceLoc?,
140-
_ id: DiagID,
133+
public func diagnose(_ id: DiagID,
141134
_ args: DiagnosticArgument...,
135+
at position: SourceLoc?,
142136
highlight: CharSourceRange? = nil,
143137
fixIts: DiagnosticFixIt...) {
144-
diagnose(position, id, args, highlight: highlight, fixIts: fixIts)
138+
diagnose(id, args, at: position, highlight: highlight, fixIts: fixIts)
139+
}
140+
141+
public func diagnose(_ diagnostic: Diagnostic) {
142+
diagnose(diagnostic.id, diagnostic.arguments, at: diagnostic.position)
143+
}
144+
}
145+
146+
/// A utility struct which allows throwing a Diagnostic.
147+
public struct Diagnostic : Error {
148+
public let id: DiagID
149+
public let arguments: [DiagnosticArgument]
150+
public let position: SourceLoc?
151+
152+
public init(_ id: DiagID, _ arguments: DiagnosticArgument..., at position: SourceLoc?) {
153+
self.init(id, arguments, at: position)
154+
}
155+
156+
public init(_ id: DiagID, _ arguments: [DiagnosticArgument], at position: SourceLoc?) {
157+
self.id = id
158+
self.arguments = arguments
159+
self.position = position
145160
}
146161
}

SwiftCompilerSources/Sources/AST/GenericSignature.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ public struct GenericSignature: CustomStringConvertible, NoReflectionChildren {
2929
public var genericParameters: TypeArray {
3030
TypeArray(bridged: bridged.getGenericParams())
3131
}
32+
33+
public func mapTypeIntoContext(_ type: Type) -> Type {
34+
Type(bridged: bridged.mapTypeIntoContext(type.bridged))
35+
}
3236
}

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre
4848

4949
public var instanceTypeOfMetatype: Type { Type(bridged: bridged.getInstanceTypeOfMetatype()) }
5050

51+
public var staticTypeOfDynamicSelf: Type { Type(bridged: bridged.getStaticTypeOfDynamicSelf()) }
52+
5153
public var superClassType: Type? {
5254
precondition(isClass)
5355
let bridgedSuperClassTy = bridged.getSuperClassType()
@@ -136,10 +138,12 @@ extension TypeProperties {
136138

137139
public var isTuple: Bool { rawType.bridged.isTuple() }
138140
public var isFunction: Bool { rawType.bridged.isFunction() }
141+
public var isArchetype: Bool { rawType.bridged.isArchetype() }
139142
public var isExistentialArchetype: Bool { rawType.bridged.isExistentialArchetype() }
140143
public var isExistentialArchetypeWithError: Bool { rawType.bridged.isExistentialArchetypeWithError() }
141144
public var isExistential: Bool { rawType.bridged.isExistential() }
142145
public var isClassExistential: Bool { rawType.bridged.isClassExistential() }
146+
public var isGenericTypeParameter: Bool { rawType.bridged.isGenericTypeParam() }
143147
public var isUnownedStorageType: Bool { return rawType.bridged.isUnownedStorageType() }
144148
public var isMetatype: Bool { rawType.bridged.isMetatypeType() }
145149
public var isExistentialMetatype: Bool { rawType.bridged.isExistentialMetatypeType() }
@@ -186,6 +190,7 @@ extension TypeProperties {
186190
public var hasLocalArchetype: Bool { rawType.bridged.hasLocalArchetype() }
187191
public var isEscapable: Bool { rawType.bridged.isEscapable() }
188192
public var isNoEscape: Bool { rawType.bridged.isNoEscape() }
193+
public var archetypeRequiresClass: Bool { rawType.bridged.archetypeRequiresClass() }
189194

190195
public var representationOfMetatype: AST.`Type`.MetatypeRepresentation {
191196
rawType.bridged.getRepresentationOfMetatype().representation

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ public struct StringRef : CustomStringConvertible, NoReflectionChildren {
9696
return buffer[index]
9797
}
9898

99+
public func startsWith(_ prefix: StaticString) -> Bool {
100+
return prefix.withUTF8Buffer { (prefixBuffer: UnsafeBufferPointer<UInt8>) in
101+
if count < prefixBuffer.count {
102+
return false
103+
}
104+
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.data, count: prefixBuffer.count)
105+
return buffer.elementsEqual(prefixBuffer, by: ==)
106+
}
107+
}
108+
99109
public static func ==(lhs: StringRef, rhs: StringRef) -> Bool {
100110
let lhsBuffer = UnsafeBufferPointer<UInt8>(start: lhs._bridged.data, count: lhs.count)
101111
let rhsBuffer = UnsafeBufferPointer<UInt8>(start: rhs._bridged.data, count: rhs.count)

SwiftCompilerSources/Sources/Optimizer/DataStructures/InstructionRange.swift

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,122 @@ struct InstructionRange : CustomStringConvertible, NoReflectionChildren {
181181
blockRange.deinitialize()
182182
}
183183
}
184+
185+
extension InstructionRange {
186+
enum PathOverlap {
187+
// range: ---
188+
// | pathBegin
189+
// | |
190+
// | pathEnd
191+
// ---
192+
case containsPath
193+
194+
// range: ---
195+
// | pathBegin
196+
// --- |
197+
// pathEnd
198+
case containsBegin
199+
200+
// pathBegin
201+
// range: --- |
202+
// | pathEnd
203+
// ---
204+
case containsEnd
205+
206+
// pathBegin
207+
// range: --- |
208+
// | |
209+
// --- |
210+
// pathEnd
211+
case overlappedByPath
212+
213+
// either: pathBegin
214+
// |
215+
// pathEnd
216+
// range: ---
217+
// |
218+
// ---
219+
// or: pathBegin
220+
// |
221+
// pathEnd
222+
case disjoint
223+
}
224+
225+
/// Return true if any exclusive path from `begin` to `end` includes an instruction in this exclusive range.
226+
///
227+
/// Returns .containsBegin, if this range has the same begin and end as the path.
228+
///
229+
/// Precondition: `begin` dominates `end`.
230+
func overlaps(pathBegin: Instruction, pathEnd: Instruction, _ context: some Context) -> PathOverlap {
231+
assert(pathBegin != pathEnd, "expect an exclusive path")
232+
if contains(pathBegin) {
233+
// Note: pathEnd != self.begin here since self.contains(pathBegin)
234+
if contains(pathEnd) { return .containsPath }
235+
return .containsBegin
236+
}
237+
if contains(pathEnd) {
238+
if let rangeBegin = self.begin, rangeBegin == pathEnd {
239+
return .disjoint
240+
}
241+
return .containsEnd
242+
}
243+
// Neither end-point is contained. If a backward path walk encouters this range, then it must overlap this
244+
// range. Otherwise, it is disjoint.
245+
var backwardBlocks = BasicBlockWorklist(context)
246+
defer { backwardBlocks.deinitialize() }
247+
backwardBlocks.pushIfNotVisited(pathEnd.parentBlock)
248+
while let block = backwardBlocks.pop() {
249+
if blockRange.inclusiveRangeContains(block) {
250+
// This range overlaps with this block, but there are still three possibilities:
251+
// (1) range, pathBegin, pathEnd = disjoint (range might not begin in this block)
252+
// (2) pathBegin, pathEnd, range = disjoint (pathBegin might not be in this block)
253+
// (3) pathBegin, range, pathEnd = overlappedByPath (range or pathBegin might not be in this block)
254+
//
255+
// Walk backward from pathEnd to find either pathBegin or an instruction in this range.
256+
// Both this range and the path may or may not begin in this block.
257+
let endInBlock = block == pathEnd.parentBlock ? pathEnd : block.terminator
258+
for inst in ReverseInstructionList(first: endInBlock) {
259+
// Check pathBegin first because the range is exclusive.
260+
if inst == pathBegin {
261+
break
262+
}
263+
// Check inclusiveRangeContains() in case the range end is the first instruction in this block.
264+
if inclusiveRangeContains(inst) {
265+
return .overlappedByPath
266+
}
267+
}
268+
// No instructions in this range occur between pathBegin and pathEnd.
269+
return .disjoint
270+
}
271+
// No range blocks have been reached.
272+
if block == pathBegin.parentBlock {
273+
return .disjoint
274+
}
275+
backwardBlocks.pushIfNotVisited(contentsOf: block.predecessors)
276+
}
277+
fatalError("begin: \(pathBegin)\n must dominate end: \(pathEnd)")
278+
}
279+
}
280+
281+
let rangeOverlapsPathTest = FunctionTest("range_overlaps_path") {
282+
function, arguments, context in
283+
let rangeValue = arguments.takeValue()
284+
print("Range of: \(rangeValue)")
285+
var range = computeLinearLiveness(for: rangeValue, context)
286+
defer { range.deinitialize() }
287+
let pathInst = arguments.takeInstruction()
288+
print("Path begin: \(pathInst)")
289+
if let pathBegin = pathInst as? ScopedInstruction {
290+
for end in pathBegin.endInstructions {
291+
print("Overlap kind:", range.overlaps(pathBegin: pathInst, pathEnd: end, context))
292+
}
293+
return
294+
}
295+
if let pathValue = pathInst as? SingleValueInstruction, pathValue.ownership == .owned {
296+
for end in pathValue.uses.endingLifetime {
297+
print("Overlap kind:", range.overlaps(pathBegin: pathInst, pathEnd: end.instruction, context))
298+
}
299+
return
300+
}
301+
print("Test specification error: not a scoped or owned instruction: \(pathInst)")
302+
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/DiagnoseInfiniteRecursion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ private struct Analysis {
289289
worklist.pushIfNotVisited(function.entryBlock)
290290
while let block = worklist.pop() {
291291
if case .recursive(let apply) = block.getKind(for: invariants, context) {
292-
context.diagnosticEngine.diagnose(apply.location.sourceLoc, .warn_infinite_recursive_call)
292+
context.diagnosticEngine.diagnose(.warn_infinite_recursive_call, at: apply.location)
293293
} else {
294294
for succ in block.successors where isInInfiniteRecursionLoop(succ) {
295295
worklist.pushIfNotVisited(succ)

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceDiagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private struct DiagnoseDependence {
135135

136136
func diagnose(_ position: SourceLoc?, _ id: DiagID,
137137
_ args: DiagnosticArgument...) {
138-
context.diagnosticEngine.diagnose(position, id, args)
138+
context.diagnosticEngine.diagnose(id, args, at: position)
139139
}
140140

141141
/// Check that this use is inside the dependence scope.
@@ -292,7 +292,7 @@ private struct LifetimeVariable {
292292
return .abortWalk
293293
}
294294
defer { useDefVisitor.deinitialize() }
295-
_ = useDefVisitor.walkUp(valueOrAddress: value)
295+
_ = useDefVisitor.walkUp(newLifetime: value)
296296
return introducer
297297
}
298298

0 commit comments

Comments
 (0)