@@ -17,7 +17,8 @@ extension UnsafePointer {
17
17
}
18
18
}
19
19
20
- /// Little utility wrapper that lets us
20
+ /// Little utility wrapper that lets us have some mutable state within
21
+ /// immutable structs, and is therefore pretty evil.
21
22
@propertyWrapper
22
23
class Boxed < Value> {
23
24
var wrappedValue : Value
@@ -29,7 +30,7 @@ class Boxed<Value> {
29
30
30
31
struct ASTGenVisitor : SyntaxTransformVisitor {
31
32
let ctx : UnsafeMutableRawPointer
32
- let base : UnsafePointer < CChar >
33
+ let base : UnsafePointer < UInt8 >
33
34
34
35
@Boxed var declContext : UnsafeMutableRawPointer
35
36
@@ -68,15 +69,65 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
68
69
}
69
70
}
70
71
71
- @_cdecl ( " parseTopLevelSwift " )
72
- public func parseTopLevelSwift(
73
- buffer: UnsafePointer < CChar > , dc: UnsafeMutableRawPointer ,
74
- ctx: UnsafeMutableRawPointer ,
75
- outputContext: UnsafeMutableRawPointer ,
76
- callback: @convention ( c) ( UnsafeMutableRawPointer , UnsafeMutableRawPointer ) -> Void
72
+ /// Describes a source file that has been "exported" to the C++ part of the
73
+ /// compiler, with enough information to interface with the C++ layer.
74
+ struct ExportedSourceFile {
75
+ /// The underlying buffer within the C++ SourceManager, which is used
76
+ /// for computations of source locations.
77
+ let buffer : UnsafeBufferPointer < UInt8 >
78
+
79
+ /// The name of the enclosing module.
80
+ let moduleName : String
81
+
82
+ /// The name of the source file being parsed.
83
+ let fileName : String
84
+
85
+ /// The syntax tree for the complete source file.
86
+ let syntax : SourceFileSyntax
87
+ }
88
+
89
+ /// Parses the given source file and produces a pointer to a new
90
+ /// ExportedSourceFile instance.
91
+ @_cdecl ( " swift_ASTGen_parseSourceFile " )
92
+ public func parseSourceFile(
93
+ buffer: UnsafePointer < UInt8 > , bufferLength: Int ,
94
+ moduleName: UnsafePointer < UInt8 > , filename: UnsafePointer < UInt8 >
95
+ ) -> UnsafeRawPointer {
96
+ let buffer = UnsafeBufferPointer ( start: buffer, count: bufferLength)
97
+ let sourceFile = Parser . parse ( source: buffer)
98
+
99
+ let exportedPtr = UnsafeMutablePointer< ExportedSourceFile> . allocate( capacity: 1 )
100
+ exportedPtr. initialize ( to: . init(
101
+ buffer: buffer, moduleName: String ( cString: moduleName) ,
102
+ fileName: String ( cString: filename) , syntax: sourceFile)
103
+ )
104
+
105
+ return UnsafeRawPointer ( exportedPtr)
106
+ }
107
+
108
+ /// Deallocate a parsed source file.
109
+ @_cdecl ( " swift_ASTGen_destroySourceFile " )
110
+ public func destroySourceFile(
111
+ sourceFilePtr: UnsafeMutableRawPointer
77
112
) {
78
- let syntax = try ! Parser . parse ( source: String ( cString: buffer) )
79
- ASTGenVisitor ( ctx: ctx, base: buffer, declContext: dc)
80
- . visit ( syntax)
81
- . forEach { callback ( $0, outputContext) }
113
+ sourceFilePtr. withMemoryRebound ( to: ExportedSourceFile . self, capacity: 1 ) { sourceFile in
114
+ sourceFile. deinitialize ( count: 1 )
115
+ sourceFile. deallocate ( )
116
+ }
117
+ }
118
+
119
+ /// Generate AST nodes for all top-level entities in the given source file.
120
+ @_cdecl ( " swift_ASTGen_buildTopLevelASTNodes " )
121
+ public func buildTopLevelASTNodes(
122
+ sourceFilePtr: UnsafeRawPointer ,
123
+ dc: UnsafeMutableRawPointer ,
124
+ ctx: UnsafeMutableRawPointer ,
125
+ outputContext: UnsafeMutableRawPointer ,
126
+ callback: @convention ( c) ( UnsafeMutableRawPointer , UnsafeMutableRawPointer ) -> Void
127
+ ) {
128
+ sourceFilePtr. withMemoryRebound ( to: ExportedSourceFile . self, capacity: 1 ) { sourceFile in
129
+ ASTGenVisitor ( ctx: ctx, base: sourceFile. pointee. buffer. baseAddress!, declContext: dc)
130
+ . visit ( sourceFile. pointee. syntax)
131
+ . forEach { callback ( $0, outputContext) }
132
+ }
82
133
}
0 commit comments