13
13
import ASTBridging
14
14
15
15
import Basic
16
- import SIL
17
16
18
- typealias DiagID = BridgedDiagID
17
+ public typealias DiagID = BridgedDiagID
19
18
20
- protocol DiagnosticArgument {
19
+ public protocol DiagnosticArgument {
21
20
func _withBridgedDiagnosticArgument( _ fn: ( BridgedDiagnosticArgument ) -> Void )
22
21
}
23
22
extension String : DiagnosticArgument {
24
- func _withBridgedDiagnosticArgument( _ fn: ( BridgedDiagnosticArgument ) -> Void ) {
23
+ public func _withBridgedDiagnosticArgument( _ fn: ( BridgedDiagnosticArgument ) -> Void ) {
25
24
_withBridgedStringRef { fn ( BridgedDiagnosticArgument ( $0) ) }
26
25
}
27
26
}
28
27
extension StringRef : DiagnosticArgument {
29
- func _withBridgedDiagnosticArgument( _ fn: ( BridgedDiagnosticArgument ) -> Void ) {
28
+ public func _withBridgedDiagnosticArgument( _ fn: ( BridgedDiagnosticArgument ) -> Void ) {
30
29
fn ( BridgedDiagnosticArgument ( _bridged) )
31
30
}
32
31
}
33
32
extension Int : DiagnosticArgument {
34
- func _withBridgedDiagnosticArgument( _ fn: ( BridgedDiagnosticArgument ) -> Void ) {
33
+ public func _withBridgedDiagnosticArgument( _ fn: ( BridgedDiagnosticArgument ) -> Void ) {
35
34
fn ( BridgedDiagnosticArgument ( self ) )
36
35
}
37
36
}
38
37
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 ) {
45
39
fn ( bridged. asDiagnosticArgument ( ) )
46
40
}
47
41
}
48
42
49
- struct DiagnosticFixIt {
43
+ public struct DiagnosticFixIt {
50
44
let start : SourceLoc
51
45
let byteLength : Int
52
46
let text : String
@@ -67,10 +61,10 @@ struct DiagnosticFixIt {
67
61
}
68
62
}
69
63
70
- struct DiagnosticEngine {
64
+ public struct DiagnosticEngine {
71
65
private let bridged : BridgedDiagnosticEngine
72
66
73
- init ( bridged: BridgedDiagnosticEngine ) {
67
+ public init ( bridged: BridgedDiagnosticEngine ) {
74
68
self . bridged = bridged
75
69
}
76
70
init ? ( bridged: BridgedNullableDiagnosticEngine ) {
@@ -80,9 +74,9 @@ struct DiagnosticEngine {
80
74
self . bridged = BridgedDiagnosticEngine ( raw: raw)
81
75
}
82
76
83
- func diagnose( _ position: SourceLoc ? ,
84
- _ id: DiagID ,
77
+ public func diagnose( _ id: DiagID ,
85
78
_ args: [ DiagnosticArgument ] ,
79
+ at position: SourceLoc ? ,
86
80
highlight: CharSourceRange ? = nil ,
87
81
fixIts: [ DiagnosticFixIt ] = [ ] ) {
88
82
@@ -136,11 +130,32 @@ struct DiagnosticEngine {
136
130
closure ( )
137
131
}
138
132
139
- func diagnose( _ position: SourceLoc ? ,
140
- _ id: DiagID ,
133
+ public func diagnose( _ id: DiagID ,
141
134
_ args: DiagnosticArgument ... ,
135
+ at position: SourceLoc ? ,
142
136
highlight: CharSourceRange ? = nil ,
143
137
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
145
160
}
146
161
}
0 commit comments