Skip to content

Commit 2b741ad

Browse files
committed
Move ASTGen source-file handling to its own (Swift) file
1 parent d339985 commit 2b741ad

File tree

3 files changed

+50
-47
lines changed

3 files changed

+50
-47
lines changed

lib/ASTGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ if (SWIFT_SWIFT_PARSER)
55
Sources/ASTGen/Exprs.swift
66
Sources/ASTGen/Literals.swift
77
Sources/ASTGen/Misc.swift
8+
Sources/ASTGen/SourceFile.swift
89
Sources/ASTGen/Stmts.swift
910
)
1011

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -69,53 +69,6 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
6969
}
7070
}
7171

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
112-
) {
113-
sourceFilePtr.withMemoryRebound(to: ExportedSourceFile.self, capacity: 1) { sourceFile in
114-
sourceFile.deinitialize(count: 1)
115-
sourceFile.deallocate()
116-
}
117-
}
118-
11972
/// Generate AST nodes for all top-level entities in the given source file.
12073
@_cdecl("swift_ASTGen_buildTopLevelASTNodes")
12174
public func buildTopLevelASTNodes(
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import SwiftParser
2+
import SwiftSyntax
3+
4+
/// Describes a source file that has been "exported" to the C++ part of the
5+
/// compiler, with enough information to interface with the C++ layer.
6+
struct ExportedSourceFile {
7+
/// The underlying buffer within the C++ SourceManager, which is used
8+
/// for computations of source locations.
9+
let buffer: UnsafeBufferPointer<UInt8>
10+
11+
/// The name of the enclosing module.
12+
let moduleName: String
13+
14+
/// The name of the source file being parsed.
15+
let fileName: String
16+
17+
/// The syntax tree for the complete source file.
18+
let syntax: SourceFileSyntax
19+
}
20+
21+
/// Parses the given source file and produces a pointer to a new
22+
/// ExportedSourceFile instance.
23+
@_cdecl("swift_ASTGen_parseSourceFile")
24+
public func parseSourceFile(
25+
buffer: UnsafePointer<UInt8>, bufferLength: Int,
26+
moduleName: UnsafePointer<UInt8>, filename: UnsafePointer<UInt8>
27+
) -> UnsafeRawPointer {
28+
let buffer = UnsafeBufferPointer(start: buffer, count: bufferLength)
29+
let sourceFile = Parser.parse(source: buffer)
30+
31+
let exportedPtr = UnsafeMutablePointer<ExportedSourceFile>.allocate(capacity: 1)
32+
exportedPtr.initialize(to: .init(
33+
buffer: buffer, moduleName: String(cString: moduleName),
34+
fileName: String(cString: filename), syntax: sourceFile)
35+
)
36+
37+
return UnsafeRawPointer(exportedPtr)
38+
}
39+
40+
/// Deallocate a parsed source file.
41+
@_cdecl("swift_ASTGen_destroySourceFile")
42+
public func destroySourceFile(
43+
sourceFilePtr: UnsafeMutableRawPointer
44+
) {
45+
sourceFilePtr.withMemoryRebound(to: ExportedSourceFile.self, capacity: 1) { sourceFile in
46+
sourceFile.deinitialize(count: 1)
47+
sourceFile.deallocate()
48+
}
49+
}

0 commit comments

Comments
 (0)