Skip to content

Commit 58e6795

Browse files
committed
[SwiftCompiler] Use bridged DiagnosticEngine to emit regex literal parsing
WIP
1 parent 05d5e0c commit 58e6795

File tree

18 files changed

+327
-135
lines changed

18 files changed

+327
-135
lines changed

SwiftCompilerSources/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ function(add_swift_compiler_modules_library name)
8383
list(APPEND swift_compile_options "-O" "-cross-module-optimization")
8484
endif()
8585

86+
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
87+
list(APPEND swift_compile_options "-D" "SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING")
88+
endif()
89+
8690
get_bootstrapping_path(build_dir ${CMAKE_CURRENT_BINARY_DIR} "${ALS_BOOTSTRAPPING}")
8791

8892
set(sdk_option "")
@@ -251,3 +255,12 @@ else()
251255

252256
endif()
253257

258+
# Configure SwiftPM package.
259+
260+
set(swiftcompiler_source_dir_name "_Sources")
261+
configure_file(Package.swift.in
262+
"${CMAKE_CURRENT_BINARY_DIR}/Package.swift" @ONLY)
263+
# SwiftPM requires all sources are inside the directory of 'Package.swift'.
264+
execute_process(COMMAND
265+
"${CMAKE_COMMAND}" -E create_symlink
266+
"${CMAKE_CURRENT_SOURCE_DIR}/Sources" "${CMAKE_CURRENT_BINARY_DIR}/${swiftcompiler_source_dir_name}")

SwiftCompilerSources/Package.swift

Lines changed: 0 additions & 43 deletions
This file was deleted.

SwiftCompilerSources/Package.swift.in

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// swift-tools-version:5.3
2+
//===--- Package.swift.in - SwiftCompiler SwiftPM package -----------------===//
3+
//
4+
// This source file is part of the Swift.org open source project
5+
//
6+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
7+
// Licensed under Apache License v2.0 with Runtime Library Exception
8+
//
9+
// See https://swift.org/LICENSE.txt for license information
10+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
// NOTE: This 'Package.swift.in' file is for CMake configure_file().
15+
// Generated 'Package.swift' can be found in
16+
// '${swift_build_dir}/SwiftCompilerSources/Package.swift'.
17+
18+
19+
import PackageDescription
20+
21+
var swiftSettings: [SwiftSetting] = [
22+
.unsafeFlags([
23+
"-Xfrontend", "-validate-tbd-against-ir=none",
24+
"-Xfrontend", "-enable-cxx-interop",
25+
// Bridging modules and headers
26+
"-Xcc", "-I", "-Xcc", "@SWIFT_SOURCE_DIR@/include",
27+
// Generated C headers
28+
"-Xcc", "-I", "-Xcc", "@CMAKE_BINARY_DIR@/include",
29+
"-cross-module-optimization"
30+
]),
31+
]
32+
33+
if "@SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING@" == "TRUE" {
34+
swiftSettings.append(.define("SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING"))
35+
}
36+
37+
func getTarget(_ name: String, dependencies: [Target.Dependency]) -> Target {
38+
.target(
39+
name: name,
40+
dependencies: dependencies,
41+
path: "@swiftcompiler_source_dir_name@/\(name)",
42+
exclude: ["CMakeLists.txt"],
43+
swiftSettings: swiftSettings)
44+
}
45+
46+
let package = Package(
47+
name: "SwiftCompilerSources",
48+
platforms: [
49+
.macOS("10.9"),
50+
],
51+
products: [
52+
.library(
53+
name: "SwiftCompiler",
54+
type: .static,
55+
targets: ["Basic", "AST", "Parse", "SIL", "Optimizer"]),
56+
],
57+
dependencies: [
58+
],
59+
// Note that all modules must be added to LIBSWIFT_MODULES in the top-level
60+
// CMakeLists.txt file to get debugging working.
61+
targets: [
62+
getTarget("Basic", dependencies: []),
63+
getTarget("AST", dependencies: ["Basic"]),
64+
getTarget("Parse", dependencies: ["Basic", "AST"]),
65+
getTarget("SIL", dependencies: ["Basic"]),
66+
getTarget("Optimizer", dependencies: ["Basic", "SIL", "Parse"]),
67+
]
68+
)

SwiftCompilerSources/Sources/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
add_subdirectory(Basic)
1212
add_subdirectory(AST)
13-
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
14-
add_subdirectory(ExperimentalRegex)
15-
endif()
13+
add_subdirectory(Parse)
1614
add_subdirectory(SIL)
1715
add_subdirectory(Optimizer)

SwiftCompilerSources/Sources/ExperimentalRegex/CMakeLists.txt

Lines changed: 0 additions & 21 deletions
This file was deleted.

SwiftCompilerSources/Sources/ExperimentalRegex/Regex.swift

Lines changed: 0 additions & 6 deletions
This file was deleted.

SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9-
set(dependencies)
10-
list(APPEND dependencies Basic SIL)
11-
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
12-
list(APPEND dependencies ExperimentalRegex)
13-
endif()
14-
15-
add_swift_compiler_module(Optimizer DEPENDS ${dependencies})
9+
add_swift_compiler_module(Optimizer
10+
DEPENDS
11+
Basic
12+
SIL
13+
Parse)
1614

1715
add_subdirectory(Analysis)
1816
add_subdirectory(DataStructures)

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import SIL
14+
import Parse
1415
import OptimizerBridging
1516

16-
#if canImport(ExperimentalRegex)
17-
import ExperimentalRegex
18-
#endif
19-
2017
@_cdecl("initializeSwiftModules")
2118
public func initializeSwiftModules() {
2219
registerSILClasses()
2320
registerSwiftPasses()
2421

25-
#if canImport(ExperimentalRegex)
22+
#if SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING
2623
registerRegexParser()
2724
#endif
2825
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2021 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+
set(LIBSWIFT_EXPERIMENTAL_REGEX_SOURCES)
10+
11+
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
12+
file(GLOB_RECURSE _LIBSWIFT_EXPERIMENTAL_REGEX_SOURCES
13+
"${EXPERIMENTAL_STRING_PROCESSING_SOURCE_DIR}/Sources/_MatchingEngine/*.swift")
14+
foreach(source ${_LIBSWIFT_EXPERIMENTAL_REGEX_SOURCES})
15+
file(TO_CMAKE_PATH "${source}" source)
16+
list(APPEND LIBSWIFT_EXPERIMENTAL_REGEX_SOURCES ${source})
17+
endforeach()
18+
message(STATUS "Using Experimental String Processing library for libswift ExperimentalRegex (${EXPERIMENTAL_STRING_PROCESSING_SOURCE_DIR}).")
19+
endif()
20+
21+
add_swift_compiler_module(Parse
22+
DEPENDS
23+
Basic
24+
AST
25+
SOURCES
26+
"${LIBSWIFT_EXPERIMENTAL_REGEX_SOURCES}"
27+
Regex.swift)
28+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//===--- Regex.swift - Regex compilation bridging layer -------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 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+
// Bridging layer between 'string-processing' module and the compiler
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#if SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING
18+
19+
// FIXME: Currenly, 'swift-experimental-string-processing' sources are compiled
20+
// with this file. Ideally, it should be built as an independent module, so it
21+
// can be imported in this file.
22+
23+
// import _MatchingEngine
24+
import ParseBridging
25+
import AST
26+
import Basic
27+
28+
public func registerRegexParser() {
29+
Parser_registerRegexLiteralParsingFn(libswiftParseRegexLiteral)
30+
Parser_registerRegexLiteralLexingFn(bridgingLexRegexLiteral)
31+
}
32+
33+
func bridgingLexRegexLiteral(
34+
_ curPtrPtr: UnsafeMutablePointer<UnsafePointer<CChar>?>,
35+
_ bufferEndPtr: UnsafePointer<CChar>,
36+
_ bridgedDiagEngine: BridgedDiagnosticEngine) -> Bool {
37+
38+
var errOut: UnsafePointer<CChar>? = nil;
39+
let completelyErroneous = libswiftLexRegexLiteral(curPtrPtr, bufferEndPtr, &errOut);
40+
41+
// NOTE: Temporary diagnostics.
42+
if let errCString = errOut {
43+
let errStr = String(cString: errCString);
44+
45+
let diagEngine = DiagnosticEngine(bridged: bridgedDiagEngine)
46+
let loc = SourceLoc(pointer: curPtrPtr.pointee)
47+
diagEngine.diagnose(loc, .regex_literal_parsing_error, [errStr],
48+
highlight: nil, fixIts: [])
49+
}
50+
51+
return completelyErroneous;
52+
}
53+
54+
// -- Prototype pseudo implementation --
55+
#if PROTOTYPE_IMPL
56+
func bridgingLexRegexLiteral(
57+
_ curPtrPtr: UnsafeMutablePointer<UnsafePointer<CChar>?>?,
58+
_ bufferEndPtr: UnsafePointer<CChar>?,
59+
_ bridgedDiagEngine: BridgedDiagnosticEngine) {
60+
61+
let source = Source(curPtrPtr, buffferEndPointer)
62+
let lexer = RegexLexer(source)
63+
64+
let pos: SourcePosition? = lexer.lex()
65+
66+
let completelyErroneous: Bool
67+
if let pos = pos {
68+
completelyErroneous = false
69+
curPtrPtr = pos.pointer
70+
} else {
71+
completelyErroneous = true
72+
}
73+
74+
let diagEngine = DiagnosticEngine(bridged: bridgedDiagEngine)
75+
for diagnostic in lexer.diagnostics {
76+
let loc = getSourceLoc(diagnostic.sourceLocation)
77+
switch diagnostic {
78+
case .someError(let arg):
79+
diagEngine.diagnose(loc, .regex_lex_some_error, [arg])
80+
case .otherWarning:
81+
diagEngine.diagnose(loc, .regex_lex_other_warning, [])
82+
}
83+
}
84+
85+
return completelyErroneous;
86+
}
87+
#endif
88+
89+
#endif // SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===--- _MatchingEnginge_stubs.swift -------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 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+
// Only provides stub functions for Package.swift builds which doesn't know
14+
// where the "string-processing" sources exist.
15+
// NOTE: This file is not used in CMake builds.
16+
17+
func libswiftLexRegexLiteral(
18+
_ curPtrPtr: UnsafeMutablePointer<UnsafePointer<CChar>?>?,
19+
_ bufferEndPtr: UnsafePointer<CChar>?,
20+
_ errOut: UnsafeMutablePointer<UnsafePointer<CChar>?>?
21+
) -> /*CompletelyErroneous*/ CBool {
22+
fatalError("not implemented")
23+
}
24+
25+
func libswiftParseRegexLiteral(
26+
_ inputPtr: UnsafePointer<CChar>?,
27+
_ errOut: UnsafeMutablePointer<UnsafePointer<CChar>?>?,
28+
_ versionOut: UnsafeMutablePointer<CUnsignedInt>?,
29+
_ captureStructureOut: UnsafeMutableRawPointer?,
30+
_ captureStructureSize: CUnsignedInt
31+
) {
32+
fatalError("not implemented")
33+
}

include/module.modulemap

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module BasicBridging {
2+
header "swift/Basic/BasicBridging.h"
3+
header "swift/Basic/BridgedSwiftObject.h"
4+
export *
5+
}
6+
7+
module ASTBridging {
8+
header "swift/AST/ASTBridging.h"
9+
export *
10+
}
11+
12+
module SILBridging {
13+
header "swift/SIL/SILBridging.h"
14+
export *
15+
}
16+
17+
module OptimizerBridging {
18+
header "swift/SILOptimizer/OptimizerBridging.h"
19+
export *
20+
}
21+
22+
module ParseBridging {
23+
header "swift/Parse/ParseBridging.h"
24+
export *
25+
}

0 commit comments

Comments
 (0)