Skip to content

Commit 4a069c9

Browse files
authored
Merge pull request #76790 from eeckstein/add-ast-module
SwiftCompilerSources: introduce the AST module
2 parents f8d6012 + 6fdb713 commit 4a069c9

File tree

97 files changed

+1325
-750
lines changed

Some content is hidden

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

97 files changed

+1325
-750
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
add_swift_compiler_module(AST
10+
DEPENDS
11+
Basic
12+
SOURCES
13+
Declarations.swift
14+
Conformance.swift
15+
Registration.swift
16+
SubstitutionMap.swift
17+
Type.swift)
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- ProtocolConformance.swift ----------------------------------------===//
1+
//===--- Conformance.swift ------------------------------------------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,29 +10,41 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import SILBridging
13+
import Basic
14+
import ASTBridging
1415

15-
// TODO: move `ProtocolConformance` to an AST module, once we have it
16+
/// Describes how a particular type conforms to a given protocol, providing the mapping from the protocol
17+
/// members to the type (or extension) members that provide the functionality for the concrete type.
18+
///
19+
/// TODO: Ideally, `Conformance` should be an enum
20+
public struct Conformance: CustomStringConvertible, NoReflectionChildren {
21+
public let bridged: BridgedConformance
1622

17-
public struct ProtocolConformance {
18-
public let bridged: BridgedProtocolConformance
19-
20-
init(bridged: BridgedProtocolConformance) {
23+
public init(bridged: BridgedConformance) {
2124
self.bridged = bridged
2225
}
2326

27+
public var description: String {
28+
return String(taking: bridged.getDebugDescription())
29+
}
30+
2431
public var isConcrete: Bool { bridged.isConcrete() }
2532

2633
public var isValid: Bool { bridged.isValid() }
2734

35+
public var type: Type {
36+
assert(isConcrete)
37+
return Type(bridged: bridged.getType())
38+
}
39+
2840
public var isSpecialized: Bool {
2941
assert(isConcrete)
3042
return bridged.isSpecializedConformance()
3143
}
3244

33-
public var genericConformance: ProtocolConformance {
45+
public var genericConformance: Conformance {
3446
assert(isSpecialized)
35-
return bridged.getGenericConformance().protocolConformance
47+
return bridged.getGenericConformance().conformance
3648
}
3749

3850
public var specializedSubstitutions: SubstitutionMap {
@@ -41,18 +53,18 @@ public struct ProtocolConformance {
4153
}
4254
}
4355

44-
public struct ProtocolConformanceArray : RandomAccessCollection, CustomReflectable {
45-
public let bridged: BridgedProtocolConformanceArray
56+
public struct ConformanceArray : RandomAccessCollection, CustomReflectable {
57+
public let bridged: BridgedConformanceArray
4658

4759
public var startIndex: Int { return 0 }
4860
public var endIndex: Int { return bridged.getCount() }
4961

50-
public init(bridged: BridgedProtocolConformanceArray) {
62+
public init(bridged: BridgedConformanceArray) {
5163
self.bridged = bridged
5264
}
5365

54-
public subscript(_ index: Int) -> ProtocolConformance {
55-
bridged.getAt(index).protocolConformance
66+
public subscript(_ index: Int) -> Conformance {
67+
bridged.getAt(index).conformance
5668
}
5769

5870
public var customMirror: Mirror {
@@ -61,6 +73,6 @@ public struct ProtocolConformanceArray : RandomAccessCollection, CustomReflectab
6173
}
6274
}
6375

64-
extension BridgedProtocolConformance {
65-
public var protocolConformance: ProtocolConformance { ProtocolConformance(bridged: self) }
76+
extension BridgedConformance {
77+
public var conformance: Conformance { Conformance(bridged: self) }
6678
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}
34+
35+
public class TypeDecl: ValueDecl {
36+
final public var name: StringRef { StringRef(bridged: bridged.Type_getName()) }
37+
}
38+
39+
public class GenericTypeDecl: TypeDecl {
40+
final public var isGenericAtAnyLevel: Bool { bridged.GenericType_isGenericAtAnyLevel() }
41+
}
42+
43+
public class NominalTypeDecl: GenericTypeDecl {
44+
final public var isGlobalActor: Bool { bridged.NominalType_isGlobalActor() }
45+
46+
final public var valueTypeDestructor: DestructorDecl? {
47+
bridged.NominalType_getValueTypeDestructor().getAs(DestructorDecl.self)
48+
}
49+
}
50+
51+
final public class EnumDecl: NominalTypeDecl {}
52+
53+
final public class StructDecl: NominalTypeDecl {
54+
public var hasUnreferenceableStorage: Bool { bridged.Struct_hasUnreferenceableStorage() }
55+
}
56+
57+
final public class ClassDecl: NominalTypeDecl {
58+
public var superClass: Type? { Type(bridgedOrNil: bridged.Class_getSuperclass()) }
59+
}
60+
61+
final public class ProtocolDecl: NominalTypeDecl {}
62+
63+
final public class BuiltinTupleDecl: NominalTypeDecl {}
64+
65+
final public class OpaqueTypeDecl: GenericTypeDecl {}
66+
67+
final public class TypeAliasDecl: GenericTypeDecl {}
68+
69+
final public class GenericTypeParamDecl: TypeDecl {}
70+
71+
final public class AssociatedTypeDecl: TypeDecl {}
72+
73+
final public class ModuleDecl: TypeDecl {}
74+
75+
public class AbstractStorageDecl: ValueDecl {}
76+
77+
public class VarDecl: AbstractStorageDecl {}
78+
79+
final public class ParamDecl: VarDecl {}
80+
81+
final public class SubscriptDecl: AbstractStorageDecl {}
82+
83+
public class AbstractFunctionDecl: ValueDecl {}
84+
85+
final public class ConstructorDecl: AbstractFunctionDecl {}
86+
87+
final public class DestructorDecl: AbstractFunctionDecl {}
88+
89+
public class FuncDecl: AbstractFunctionDecl {}
90+
91+
final public class AccessorDecl: FuncDecl {}
92+
93+
final public class MacroDecl: ValueDecl {}
94+
95+
final public class EnumElementDecl: ValueDecl {}
96+
97+
final public class ExtensionDecl: Decl {}
98+
99+
final public class TopLevelCodeDecl: Decl {}
100+
101+
final public class ImportDecl: Decl {}
102+
103+
final public class PoundDiagnosticDecl: Decl {}
104+
105+
final public class PrecedenceGroupDecl: Decl {}
106+
107+
final public class MissingDecl: Decl {}
108+
109+
final public class MissingMemberDecl: Decl {}
110+
111+
final public class PatternBindingDecl: Decl {}
112+
113+
final public class EnumCaseDecl: Decl {}
114+
115+
public class OperatorDecl: Decl {}
116+
117+
final public class InfixOperatorDecl: OperatorDecl {}
118+
119+
final public class PrefixOperatorDecl: OperatorDecl {}
120+
121+
final public class PostfixOperatorDecl: OperatorDecl {}
122+
123+
final public class MacroExpansionDecl: Decl {}
124+
125+
// Bridging utilities
126+
127+
extension BridgedDeclObj {
128+
public var decl: Decl { obj.getAs(Decl.self) }
129+
public func getAs<T: Decl>(_ declType: T.Type) -> T { obj.getAs(T.self) }
130+
}
131+
132+
extension OptionalBridgedDeclObj {
133+
public var decl: Decl? { obj.getAs(Decl.self) }
134+
public func getAs<T: Decl>(_ declType: T.Type) -> T? { obj.getAs(T.self) }
135+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===--- Registration.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+
public func registerAST() {
17+
registerDecl(EnumDecl.self)
18+
registerDecl(StructDecl.self)
19+
registerDecl(ClassDecl.self)
20+
registerDecl(ProtocolDecl.self)
21+
registerDecl(BuiltinTupleDecl.self)
22+
registerDecl(OpaqueTypeDecl.self)
23+
registerDecl(TypeAliasDecl.self)
24+
registerDecl(GenericTypeParamDecl.self)
25+
registerDecl(AssociatedTypeDecl.self)
26+
registerDecl(ModuleDecl.self)
27+
registerDecl(VarDecl.self)
28+
registerDecl(ParamDecl.self)
29+
registerDecl(SubscriptDecl.self)
30+
registerDecl(ConstructorDecl.self)
31+
registerDecl(DestructorDecl.self)
32+
registerDecl(FuncDecl.self)
33+
registerDecl(AccessorDecl.self)
34+
registerDecl(MacroDecl.self)
35+
registerDecl(EnumElementDecl.self)
36+
registerDecl(ExtensionDecl.self)
37+
registerDecl(TopLevelCodeDecl.self)
38+
registerDecl(ImportDecl.self)
39+
registerDecl(PoundDiagnosticDecl.self)
40+
registerDecl(PrecedenceGroupDecl.self)
41+
registerDecl(MissingDecl.self)
42+
registerDecl(MissingMemberDecl.self)
43+
registerDecl(PatternBindingDecl.self)
44+
registerDecl(EnumCaseDecl.self)
45+
registerDecl(InfixOperatorDecl.self)
46+
registerDecl(PrefixOperatorDecl.self)
47+
registerDecl(PostfixOperatorDecl.self)
48+
registerDecl(MacroExpansionDecl.self)
49+
}
50+
51+
private func registerDecl<T: AnyObject>(_ cl: T.Type) {
52+
String(describing: cl)._withBridgedStringRef { nameStr in
53+
let metatype = unsafeBitCast(cl, to: SwiftMetatype.self)
54+
registerBridgedDecl(nameStr, metatype)
55+
}
56+
}

SwiftCompilerSources/Sources/SIL/SubstitutionMap.swift renamed to SwiftCompilerSources/Sources/AST/SubstitutionMap.swift

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2024 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
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import SILBridging
13+
import Basic
14+
import ASTBridging
1415

16+
/// SubstitutionMap describes the mapping of abstract types to replacement types,
17+
/// together with associated conformances to use for deriving nested types and conformances.
18+
///
19+
/// Substitution maps are primarily used when performing substitutions into any entity that
20+
/// can reference type parameters and conformances.
1521
public struct SubstitutionMap {
1622
public let bridged: BridgedSubstitutionMap
1723

@@ -29,15 +35,6 @@ public struct SubstitutionMap {
2935

3036
public var conformances: ConformanceArray { ConformanceArray(substitutionMap: self) }
3137

32-
public var replacementTypes: OptionalTypeArray {
33-
let types = BridgedTypeArray.fromReplacementTypes(bridged)
34-
return OptionalTypeArray(bridged: types)
35-
}
36-
37-
public func getMethodSubstitutions(for method: Function) -> SubstitutionMap {
38-
return SubstitutionMap(bridged: bridged.getMethodSubstitutions(method.bridged))
39-
}
40-
4138
public struct ConformanceArray : BridgedRandomAccessCollection {
4239
fileprivate let bridgedSubs: BridgedSubstitutionMap
4340
public let count: Int
@@ -50,9 +47,9 @@ public struct SubstitutionMap {
5047
public var startIndex: Int { return 0 }
5148
public var endIndex: Int { return count }
5249

53-
public subscript(_ index: Int) -> ProtocolConformance {
50+
public subscript(_ index: Int) -> Conformance {
5451
assert(index >= startIndex && index < endIndex)
55-
return ProtocolConformance(bridged: bridgedSubs.getConformance(index))
52+
return Conformance(bridged: bridgedSubs.getConformance(index))
5653
}
5754
}
5855
}

0 commit comments

Comments
 (0)