-
Notifications
You must be signed in to change notification settings - Fork 793
/
Copy pathCompiler.fs
1343 lines (1079 loc) · 61.2 KB
/
Compiler.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Test
open FSharp.Compiler.Interactive.Shell
open FSharp.Compiler.IO
open FSharp.Compiler.Diagnostics
open FSharp.Compiler.Symbols
open FSharp.Test.Assert
open FSharp.Test.Utilities
open FSharp.Test.ScriptHelpers
open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.CSharp
open NUnit.Framework
open System
open System.Collections.Immutable
open System.IO
open System.Text
open System.Text.RegularExpressions
open System.Reflection
open System.Reflection.Metadata
open System.Reflection.PortableExecutable
open FSharp.Test.CompilerAssertHelpers
open TestFramework
open System.Reflection.Metadata
module rec Compiler =
type BaselineFile =
{
FilePath: string
BslSource: string
Content: string option
}
type Baseline =
{
SourceFilename: string option
FSBaseline: BaselineFile
ILBaseline: BaselineFile
}
type TestType =
| Text of string
| Path of string
type CompilationUnit =
| FS of FSharpCompilationSource
| CS of CSharpCompilationSource
| IL of ILCompilationSource
override this.ToString() = match this with | FS fs -> fs.ToString() | _ -> (sprintf "%A" this )
type FSharpCompilationSource =
{ Source: SourceCodeFileKind
AdditionalSources:SourceCodeFileKind list
Baseline: Baseline option
Options: string list
OutputType: CompileOutput
OutputDirectory: DirectoryInfo option
Name: string option
IgnoreWarnings: bool
References: CompilationUnit list }
member this.CreateOutputDirectory() =
match this.OutputDirectory with
| Some d -> d.Create()
| None -> ()
member this.FullName =
match this.OutputDirectory, this.Name with
| Some directory, Some name -> Some(Path.Combine(directory.FullName, name))
| None, _ -> this.Name
| _ -> None
member this.OutputFileName =
match this.FullName, this.OutputType with
| Some fullName, CompileOutput.Library -> Some (Path.ChangeExtension(fullName, ".dll"))
| Some fullName, CompileOutput.Exe -> Some (Path.ChangeExtension(fullName, ".exe"))
| _ -> None
override this.ToString() = match this.Name with | Some n -> n | _ -> (sprintf "%A" this)
type CSharpCompilationSource =
{
Source: SourceCodeFileKind
LangVersion: CSharpLanguageVersion
TargetFramework: TargetFramework
OutputType: CompileOutput
OutputDirectory: DirectoryInfo option
Name: string option
References: CompilationUnit list
}
type ILCompilationSource =
{
Source: TestType
References: CompilationUnit list
}
type ErrorType = Error of int | Warning of int | Information of int | Hidden of int
type SymbolType =
| MemberOrFunctionOrValue of string
| Entity of string
| GenericParameter of string
| Parameter of string
| StaticParameter of string
| ActivePatternCase of string
| UnionCase of string
| Field of string
member this.FullName () =
match this with
| MemberOrFunctionOrValue fullname
| Entity fullname
| GenericParameter fullname
| Parameter fullname
| StaticParameter fullname
| ActivePatternCase fullname
| UnionCase fullname
| Field fullname -> fullname
let mapDiagnosticSeverity severity errorNumber =
match severity with
| FSharpDiagnosticSeverity.Hidden -> Hidden errorNumber
| FSharpDiagnosticSeverity.Info -> Information errorNumber
| FSharpDiagnosticSeverity.Warning -> Warning errorNumber
| FSharpDiagnosticSeverity.Error -> Error errorNumber
type Line = Line of int
type Col = Col of int
type Range =
{ StartLine: int
StartColumn: int
EndLine: int
EndColumn: int }
type Disposable (dispose : unit -> unit) =
interface IDisposable with
member this.Dispose() =
dispose()
type ErrorInfo =
{ Error: ErrorType
Range: Range
Message: string }
type ExecutionOutput =
{ ExitCode: int
StdOut: string
StdErr: string }
type RunOutput =
| EvalOutput of Result<FsiValue option, exn>
| ExecutionOutput of ExecutionOutput
type CompilationOutput =
{ OutputPath: string option
Dependencies: string list
Adjust: int
Diagnostics: ErrorInfo list
Output: RunOutput option
Compilation: CompilationUnit }
[<RequireQualifiedAccess>]
type CompilationResult =
| Success of CompilationOutput
| Failure of CompilationOutput
with
member this.Output = match this with Success o | Failure o -> o
member this.RunOutput = this.Output.Output
type ExecutionPlatform =
| Anycpu = 0
| AnyCpu32bitPreferred = 1
| X86 = 2
| Itanium = 3
| X64 = 4
| Arm = 5
| Arm64 = 6
let private defaultOptions : string list = []
// Not very safe version of reading stuff from file, but we want to fail fast for now if anything goes wrong.
let private getSource (src: TestType) : string =
match src with
| Text t -> t
| Path p ->
use stream = FileSystem.OpenFileForReadShim(p)
stream.ReadAllText()
// Load the source file from the path
let loadSourceFromFile path = getSource(TestType.Path path)
let private fsFromString (source: SourceCodeFileKind): FSharpCompilationSource =
{
Source = source
AdditionalSources = []
Baseline = None
Options = defaultOptions
OutputType = Library
OutputDirectory = None
Name = None
IgnoreWarnings = false
References = []
}
let private csFromString (source: SourceCodeFileKind) : CSharpCompilationSource =
{
Source = source
LangVersion = CSharpLanguageVersion.CSharp9
TargetFramework = TargetFramework.Current
OutputType = Library
OutputDirectory = None
Name = None
References = []
}
let private fromFSharpDiagnostic (errors: FSharpDiagnostic[]) : ErrorInfo list =
let toErrorInfo (e: FSharpDiagnostic) : ErrorInfo =
let errorNumber = e.ErrorNumber
let severity = e.Severity
let error = if severity = FSharpDiagnosticSeverity.Warning then Warning errorNumber else Error errorNumber
{ Error = error
Range =
{ StartLine = e.StartLine
StartColumn = e.StartColumn
EndLine = e.EndLine
EndColumn = e.EndColumn }
Message = e.Message }
errors
|> List.ofArray
|> List.distinctBy (fun e -> e.Severity, e.ErrorNumber, e.StartLine, e.StartColumn, e.EndLine, e.EndColumn, e.Message)
|> List.map toErrorInfo
let private partitionErrors diagnostics = diagnostics |> List.partition (fun e -> match e.Error with Error _ -> true | _ -> false)
let private getErrors diagnostics = diagnostics |> List.filter (fun e -> match e.Error with Error _ -> true | _ -> false)
let private getWarnings diagnostics = diagnostics |> List.filter (fun e -> match e.Error with Warning _ -> true | _ -> false)
let private adjustRange (range: Range) (adjust: int) : Range =
{ range with
StartLine = range.StartLine - adjust
StartColumn = range.StartColumn + 1
EndLine = range.EndLine - adjust
EndColumn = range.EndColumn + 1 }
let FsxSourceCode source =
SourceCodeFileKind.Fsx({FileName="test.fsx"; SourceText=Some source})
let Source source =
SourceCodeFileKind.Create("test.fs", source)
let SourceFromPath path =
SourceCodeFileKind.Create(path)
let FsiSource source =
SourceCodeFileKind.Fsi({FileName="test.fsi"; SourceText=Some source })
let FsSource source =
SourceCodeFileKind.Fs({FileName="test.fs"; SourceText=Some source })
let CsSource source =
SourceCodeFileKind.Cs({FileName="test.cs"; SourceText=Some source })
let CsFromPath (path: string) : CompilationUnit =
csFromString (SourceFromPath path)
|> CS
|> withName (Path.GetFileNameWithoutExtension(path))
let Fsx (source: string) : CompilationUnit =
fsFromString (FsxSourceCode source) |> FS
let FsxFromPath (path: string) : CompilationUnit =
fsFromString (SourceFromPath path) |> FS
let Fs (source: string) : CompilationUnit =
fsFromString (FsSource source) |> FS
let Fsi (source: string) : CompilationUnit =
fsFromString (FsiSource source) |> FS
let FSharp (source: string) : CompilationUnit =
Fs source
let FsFromPath (path: string) : CompilationUnit =
fsFromString (SourceFromPath path)
|> FS
|> withName (Path.GetFileNameWithoutExtension(path))
let FSharpWithInputAndOutputPath (src: string) (inputFilePath: string) (outputFilePath: string) : CompilationUnit =
let compileDirectory = Path.GetDirectoryName(outputFilePath)
let name = Path.GetFileName(outputFilePath)
{
Source = SourceCodeFileKind.Create(inputFilePath, src)
AdditionalSources = []
Baseline = None
Options = defaultOptions
OutputType = Library
OutputDirectory = Some(DirectoryInfo(compileDirectory))
Name = Some name
IgnoreWarnings = false
References = []
} |> FS
let CSharp (source: string) : CompilationUnit =
csFromString (SourceCodeFileKind.Fs({FileName="test.cs"; SourceText=Some source })) |> CS
let CSharpFromPath (path: string) : CompilationUnit =
csFromString (SourceFromPath path) |> CS
let asFsx (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS src -> FS {src with Source=SourceCodeFileKind.Fsx({FileName=src.Source.GetSourceFileName; SourceText=src.Source.GetSourceText})}
| _ -> failwith "Only F# compilation can be of type Fsx."
let asFs (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS src -> FS {src with Source=SourceCodeFileKind.Fs({FileName=src.Source.GetSourceFileName; SourceText=src.Source.GetSourceText})}
| _ -> failwith "Only F# compilation can be of type Fs."
let withName (name: string) (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS src -> FS { src with Name = Some name }
| CS src -> CS { src with Name = Some name }
| IL _ -> failwith "IL Compilation cannot be named."
let withReferences (references: CompilationUnit list) (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS fs -> FS { fs with References = fs.References @ references }
| CS cs -> CS { cs with References = cs.References @ references }
| IL _ -> failwith "References are not supported in IL"
let withAdditionalSourceFiles (additionalSources: SourceCodeFileKind list) (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS fs -> FS { fs with AdditionalSources = fs.AdditionalSources @ additionalSources }
| CS _ -> failwith "References are not supported in C#"
| IL _ -> failwith "References are not supported in IL"
let withAdditionalSourceFile (additionalSource: SourceCodeFileKind) (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS fs -> FS { fs with AdditionalSources = fs.AdditionalSources @ [additionalSource]}
| CS _ -> failwith "References are not supported in C#"
| IL _ -> failwith "References are not supported in IL"
let private withOptionsHelper (options: string list) (message:string) (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS fs -> FS { fs with Options = fs.Options @ options }
| _ -> failwith message
let withDebug (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--debug+" ] "debug+ is only supported on F#" cUnit
let withNoDebug (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--debug-" ] "debug- is only supported on F#" cUnit
let withOcamlCompat (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--mlcompatibility" ] "withOcamlCompat is only supported on F#" cUnit
let withOptions (options: string list) (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper options "withOptions is only supported for F#" cUnit
let withOutputDirectory (path: string) (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS fs -> FS { fs with OutputDirectory = Some (DirectoryInfo(path)) }
| _ -> failwith "withOutputDirectory is only supported on F#"
let withDefines (defines: string list) (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper (defines |> List.map(fun define -> $"--define:{define}")) "withDefines is only supported on F#" cUnit
let withErrorRanges (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--test:ErrorRanges" ] "withErrorRanges is only supported on F#" cUnit
let withLangVersion46 (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--langversion:4.6" ] "withLangVersion46 is only supported on F#" cUnit
let withLangVersion47 (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--langversion:4.7" ] "withLangVersion47 is only supported on F#" cUnit
let withLangVersion50 (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--langversion:5.0" ] "withLangVersion50 is only supported on F#" cUnit
let withLangVersion60 (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--langversion:6.0" ] "withLangVersion60 is only supported on F#" cUnit
let withLangVersion70 (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--langversion:7.0" ] "withLangVersion70 is only supported on F#" cUnit
let withLangVersionPreview (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--langversion:preview" ] "withLangVersionPreview is only supported on F#" cUnit
let withAssemblyVersion (version:string) (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ $"--version:{version}" ] "withAssemblyVersion is only supported on F#" cUnit
let withWarnOn (cUnit: CompilationUnit) warning : CompilationUnit =
withOptionsHelper [ $"--warnon:{warning}" ] "withWarnOn is only supported for F#" cUnit
let withNoWarn warning (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ $"--nowarn:{warning}" ] "withNoWarn is only supported for F#" cUnit
let withNoOptimize (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--optimize-" ] "withNoOptimize is only supported for F#" cUnit
let withOptimize (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--optimize+" ] "withOptimize is only supported for F#" cUnit
let withFullPdb(cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--debug:full" ] "withFullPdb is only supported for F#" cUnit
let withPdbOnly(cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--debug:pdbonly" ] "withPdbOnly is only supported for F#" cUnit
let withPortablePdb(cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--debug:portable" ] "withPortablePdb is only supported for F#" cUnit
let withEmbeddedPdb(cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--debug:embedded" ] "withEmbeddedPdb is only supported for F#" cUnit
let withEmbedAllSource(cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--embed+" ] "withEmbedAllSource is only supported for F#" cUnit
let withEmbedNoSource(cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--embed-" ] "withEmbedNoSource is only supported for F#" cUnit
let withEmbedSourceFiles(cUnit: CompilationUnit) files : CompilationUnit =
withOptionsHelper [ $"--embed:{files}" ] "withEmbedSourceFiles is only supported for F#" cUnit
/// Turns on checks that check integrity of XML doc comments
let withXmlCommentChecking (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--warnon:3390" ] "withXmlCommentChecking is only supported for F#" cUnit
/// Turns on checks that force the documentation of all parameters
let withXmlCommentStrictParamChecking (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--warnon:3391" ] "withXmlCommentChecking is only supported for F#" cUnit
/// Only include optimization information essential for implementing inlined constructs. Inhibits cross-module inlining but improves binary compatibility.
let withNoOptimizationData (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--nooptimizationdata" ] "withNoOptimizationData is only supported for F#" cUnit
/// Don't add a resource to the generated assembly containing F#-specific metadata
let withNoInterfaceData (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ "--nointerfacedata" ] "withNoInterfaceData is only supported for F#" cUnit
//--refonly[+|-]
let withRefOnly (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ $"--refonly+" ] "withRefOnly is only supported for F#" cUnit
//--refonly[+|-]
let withNoRefOnly (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ $"--refonly-" ] "withRefOnly is only supported for F#" cUnit
//--refout:<file> Produce a reference assembly with the specified file path.
let withRefOut (name:string) (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ $"--refout:{name}" ] "withNoInterfaceData is only supported for F#" cUnit
let withCSharpLanguageVersion (ver: CSharpLanguageVersion) (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| CS cs -> CS { cs with LangVersion = ver }
| _ -> failwith "Only supported in C#"
let asLibrary (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS fs -> FS { fs with OutputType = CompileOutput.Library }
| _ -> failwith "TODO: Implement asLibrary where applicable."
let asExe (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS x -> FS { x with OutputType = Exe }
| CS x -> CS { x with OutputType = Exe }
| _ -> failwith "TODO: Implement where applicable."
let withPlatform (platform:ExecutionPlatform) (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS _ ->
let p =
match platform with
| ExecutionPlatform.Anycpu -> "anycpu"
| ExecutionPlatform.AnyCpu32bitPreferred -> "anycpu32bitpreferred"
| ExecutionPlatform.Itanium -> "Itanium"
| ExecutionPlatform.X64 -> "x64"
| ExecutionPlatform.X86 -> "x86"
| ExecutionPlatform.Arm -> "arm"
| ExecutionPlatform.Arm64 -> "arm64"
| _ -> failwith $"Unknown value for ExecutionPlatform: {platform}"
withOptionsHelper [ $"--platform:{p}" ] "withPlatform is only supported for F#" cUnit
| _ -> failwith "TODO: Implement ignorewarnings for the rest."
let ignoreWarnings (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS fs -> FS { fs with IgnoreWarnings = true }
| _ -> failwith "TODO: Implement ignorewarnings for the rest."
let withCulture culture (cUnit: CompilationUnit) : CompilationUnit =
withOptionsHelper [ $"--preferreduilang:%s{culture}" ] "preferreduilang is only supported for F#" cUnit
let rec private asMetadataReference (cUnit: CompilationUnit) reference =
match reference with
| CompilationReference (cmpl, _) ->
let result = compileFSharpCompilation cmpl false cUnit
match result with
| CompilationResult.Failure f ->
let message = sprintf "Operation failed (expected to succeed).\n All errors:\n%A" (f.Diagnostics)
failwith message
| CompilationResult.Success s ->
match s.OutputPath with
| None -> failwith "Operation didn't produce any output!"
| Some p -> p |> MetadataReference.CreateFromFile
| _ -> failwith "Conversion isn't possible"
let private processReferences (references: CompilationUnit list) defaultOutputDirectory =
let rec loop acc = function
| [] -> List.rev acc
| x::xs ->
match x with
| FS fs ->
let refs = loop [] fs.References
let options = fs.Options |> List.toArray
let name = defaultArg fs.Name null
let outDir =
match fs.OutputDirectory with
| Some outputDirectory -> outputDirectory
| _ -> defaultOutputDirectory
let cmpl =
Compilation.CreateFromSources([fs.Source] @ fs.AdditionalSources, fs.OutputType, options, refs, name, outDir) |> CompilationReference.CreateFSharp
loop (cmpl::acc) xs
| CS cs ->
let refs = loop [] cs.References
let name = defaultArg cs.Name null
let metadataReferences = List.map (asMetadataReference x) refs
let cmpl =
CompilationUtil.CreateCSharpCompilation(cs.Source, cs.LangVersion, cs.TargetFramework, additionalReferences = metadataReferences.ToImmutableArray().As<PortableExecutableReference>(), name = name)
|> CompilationReference.Create
loop (cmpl::acc) xs
| IL _ -> failwith "TODO: Process references for IL"
loop [] references
let private compileFSharpCompilation compilation ignoreWarnings (cUnit: CompilationUnit) : CompilationResult =
let ((err: FSharpDiagnostic[], outputFilePath: string), deps) = CompilerAssert.CompileRaw(compilation, ignoreWarnings)
let diagnostics = err |> fromFSharpDiagnostic
let result =
{ OutputPath = None
Dependencies = deps
Adjust = 0
Diagnostics = diagnostics
Output = None
Compilation = cUnit }
let (errors, warnings) = partitionErrors diagnostics
// Treat warnings as errors if "IgnoreWarnings" is false
if errors.Length > 0 || (warnings.Length > 0 && not ignoreWarnings) then
CompilationResult.Failure result
else
CompilationResult.Success { result with OutputPath = Some outputFilePath }
let private compileFSharp (fs: FSharpCompilationSource) : CompilationResult =
let output = fs.OutputType
let options = fs.Options |> Array.ofList
let name = defaultArg fs.Name null
let outputDirectory =
match fs.OutputDirectory with
| Some di -> di
| None -> DirectoryInfo(tryCreateTemporaryDirectory())
let references = processReferences fs.References outputDirectory
let compilation = Compilation.CreateFromSources([fs.Source] @ fs.AdditionalSources, output, options, references, name, outputDirectory)
compileFSharpCompilation compilation fs.IgnoreWarnings (FS fs)
let toErrorInfo (d: Diagnostic) =
let span = d.Location.GetMappedLineSpan().Span
let number = d.Id |> Seq.where Char.IsDigit |> String.Concat |> int
{ Error =
match d.Severity with
| DiagnosticSeverity.Error -> Error
| DiagnosticSeverity.Warning -> Warning
| DiagnosticSeverity.Info -> Information
| DiagnosticSeverity.Hidden -> Hidden
| x -> failwith $"Unknown severity {x}"
|> (|>) number
Range =
{ StartLine = span.Start.Line
StartColumn = span.Start.Character
EndLine = span.End.Line
EndColumn = span.End.Character }
Message = d.GetMessage() }
let private compileCSharpCompilation (compilation: CSharpCompilation) csSource (filePath : string) dependencies : CompilationResult =
let cmplResult = compilation.Emit filePath
let result =
{ OutputPath = None
Dependencies = dependencies
Adjust = 0
Diagnostics = cmplResult.Diagnostics |> Seq.map toErrorInfo |> Seq.toList
Output = None
Compilation = CS csSource }
if cmplResult.Success then
CompilationResult.Success { result with OutputPath = Some filePath }
else
CompilationResult.Failure result
let private compileCSharp (csSource: CSharpCompilationSource) : CompilationResult =
let source = csSource.Source.GetSourceText |> Option.defaultValue ""
let name = defaultArg csSource.Name (tryCreateTemporaryFileName())
let outputDirectory =
match csSource.OutputDirectory with
| Some di -> di
| None -> DirectoryInfo(tryCreateTemporaryDirectory())
let additionalReferences =
processReferences csSource.References outputDirectory
|> List.map (asMetadataReference (CS csSource))
let additionalMetadataReferences = additionalReferences.ToImmutableArray().As<MetadataReference>()
let additionalReferencePaths = [for r in additionalReferences -> r.FilePath]
let references = TargetFrameworkUtil.getReferences csSource.TargetFramework
let lv =
match csSource.LangVersion with
| CSharpLanguageVersion.CSharp8 -> LanguageVersion.CSharp8
| CSharpLanguageVersion.CSharp9 -> LanguageVersion.CSharp9
| CSharpLanguageVersion.Preview -> LanguageVersion.Preview
| _ -> LanguageVersion.Default
let outputKind, extension =
match csSource.OutputType with
| Exe -> OutputKind.ConsoleApplication, "exe"
| Library -> OutputKind.DynamicallyLinkedLibrary, "dll"
let cmpl =
CSharpCompilation.Create(
name,
[ CSharpSyntaxTree.ParseText (source, CSharpParseOptions lv) ],
references.As<MetadataReference>().AddRange additionalMetadataReferences,
CSharpCompilationOptions outputKind)
let filename = Path.ChangeExtension(cmpl.AssemblyName, extension)
let filePath = Path.Combine(outputDirectory.FullName, filename)
compileCSharpCompilation cmpl csSource filePath additionalReferencePaths
let compile (cUnit: CompilationUnit) : CompilationResult =
match cUnit with
| FS fs -> compileFSharp fs
| CS cs -> compileCSharp cs
| _ -> failwith "TODO"
let private getAssemblyInBytes (result: CompilationResult) =
match result with
| CompilationResult.Success output ->
match output.OutputPath with
| Some filePath -> File.ReadAllBytes(filePath)
| _ -> failwith "Output path not found."
| _ ->
failwith "Compilation has errors."
let getAssembly = getAssemblyInBytes >> Assembly.Load
let withPeReader func compilationResult =
let bytes = getAssemblyInBytes compilationResult
use reader = new PEReader(bytes.ToImmutableArray())
func reader
let withMetadataReader func =
withPeReader (fun reader -> reader.GetMetadataReader() |> func)
let compileGuid cUnit =
cUnit
|> compile
|> shouldSucceed
|> withMetadataReader (fun reader -> reader.GetModuleDefinition().Mvid |> reader.GetGuid)
let compileAssembly cUnit =
cUnit
|> compile
|> shouldSucceed
|> getAssembly
let private parseFSharp (fsSource: FSharpCompilationSource) : CompilationResult =
let source = fsSource.Source.GetSourceText |> Option.defaultValue ""
let fileName = fsSource.Source.ChangeExtension.GetSourceFileName
let parseResults = CompilerAssert.Parse(source, fileName = fileName)
let failed = parseResults.ParseHadErrors
let diagnostics = parseResults.Diagnostics |> fromFSharpDiagnostic
let result =
{ OutputPath = None
Dependencies = []
Adjust = 0
Diagnostics = diagnostics
Output = None
Compilation = FS fsSource }
if failed then
CompilationResult.Failure result
else
CompilationResult.Success result
let parse (cUnit: CompilationUnit) : CompilationResult =
match cUnit with
| FS fs -> parseFSharp fs
| _ -> failwith "Parsing only supported for F#."
let private typecheckFSharpSourceAndReturnErrors (fsSource: FSharpCompilationSource) : FSharpDiagnostic [] =
let source =
match fsSource.Source.GetSourceText with
| None -> File.ReadAllText(fsSource.Source.GetSourceFileName)
| Some text -> text
let options = fsSource.Options |> Array.ofList
let (err: FSharpDiagnostic []) = CompilerAssert.TypeCheckWithOptionsAndName options (fsSource.Name |> Option.defaultValue "test.fs") source
err
let private typecheckFSharpSource (fsSource: FSharpCompilationSource) : CompilationResult =
let (err: FSharpDiagnostic []) = typecheckFSharpSourceAndReturnErrors fsSource
let diagnostics = err |> fromFSharpDiagnostic
let result =
{ OutputPath = None
Dependencies = []
Adjust = 0
Diagnostics = diagnostics
Output = None
Compilation = FS fsSource }
let (errors, warnings) = partitionErrors diagnostics
// Treat warnings as errors if "IgnoreWarnings" is false;
if errors.Length > 0 || (warnings.Length > 0 && not fsSource.IgnoreWarnings) then
CompilationResult.Failure result
else
CompilationResult.Success result
let private typecheckFSharp (fsSource: FSharpCompilationSource) : CompilationResult =
match fsSource.Source with
| _ -> typecheckFSharpSource fsSource
let typecheck (cUnit: CompilationUnit) : CompilationResult =
match cUnit with
| FS fs -> typecheckFSharp fs
| _ -> failwith "Typecheck only supports F#"
let typecheckResults (cUnit: CompilationUnit) : FSharp.Compiler.CodeAnalysis.FSharpCheckFileResults =
match cUnit with
| FS fsSource ->
let source = fsSource.Source.GetSourceText |> Option.defaultValue ""
let fileName = fsSource.Source.ChangeExtension.GetSourceFileName
let options = fsSource.Options |> Array.ofList
CompilerAssert.TypeCheck(options, fileName, source)
| _ -> failwith "Typecheck only supports F#"
let run (result: CompilationResult) : CompilationResult =
match result with
| CompilationResult.Failure f -> failwith (sprintf "Compilation should be successful in order to run.\n Errors: %A" (f.Diagnostics))
| CompilationResult.Success s ->
match s.OutputPath with
| None -> failwith "Compilation didn't produce any output. Unable to run. (Did you forget to set output type to Exe?)"
| Some p ->
let (exitCode, output, errors) = CompilerAssert.ExecuteAndReturnResult (p, s.Dependencies, false)
printfn "---------output-------\n%s\n-------" output
printfn "---------errors-------\n%s\n-------" errors
let executionResult = { s with Output = Some (ExecutionOutput { ExitCode = exitCode; StdOut = output; StdErr = errors }) }
if exitCode = 0 then
CompilationResult.Success executionResult
else
CompilationResult.Failure executionResult
let compileAndRun = compile >> run
let compileExeAndRun = asExe >> compileAndRun
let private evalFSharp (fs: FSharpCompilationSource) : CompilationResult =
let source = fs.Source.GetSourceText |> Option.defaultValue ""
let options = fs.Options |> Array.ofList
use script = new FSharpScript(additionalArgs=options)
let (evalResult: Result<FsiValue option, exn>), (err: FSharpDiagnostic[]) = script.Eval(source)
let diagnostics = err |> fromFSharpDiagnostic
let result =
{ OutputPath = None
Dependencies = []
Adjust = 0
Diagnostics = diagnostics
Output = Some (EvalOutput evalResult)
Compilation = FS fs }
let (errors, warnings) = partitionErrors diagnostics
let evalError = match evalResult with Ok _ -> false | _ -> true
if evalError || errors.Length > 0 || (warnings.Length > 0 && not fs.IgnoreWarnings) then
CompilationResult.Failure result
else
CompilationResult.Success result
let eval (cUnit: CompilationUnit) : CompilationResult =
match cUnit with
| FS fs -> evalFSharp fs
| _ -> failwith "Script evaluation is only supported for F#."
let runFsi (cUnit: CompilationUnit) : CompilationResult =
match cUnit with
| FS fs ->
let disposals = ResizeArray<IDisposable>()
try
let source = fs.Source.GetSourceText |> Option.defaultValue ""
let name = fs.Name |> Option.defaultValue "unnamed"
let options = fs.Options |> Array.ofList
let outputDirectory =
match fs.OutputDirectory with
| Some di -> di
| None -> DirectoryInfo(tryCreateTemporaryDirectory())
outputDirectory.Create()
disposals.Add({ new IDisposable with member _.Dispose() = outputDirectory.Delete(true) })
let references = processReferences fs.References outputDirectory
let cmpl = Compilation.Create(fs.Source, fs.OutputType, options, references, name, outputDirectory)
let _compilationRefs, _deps = evaluateReferences outputDirectory disposals fs.IgnoreWarnings cmpl
let options =
let opts = new ResizeArray<string>(fs.Options)
// For every built reference add a -I path so that fsi can find it easily
for reference in references do
match reference with
| CompilationReference( cmpl, _) ->
match cmpl with
| Compilation(_sources, _outputType, _options, _references, _name, outputDirectory) ->
if outputDirectory.IsSome then
opts.Add($"-I:\"{(outputDirectory.Value.FullName)}\"")
| _ -> ()
opts.ToArray()
let errors = CompilerAssert.RunScriptWithOptionsAndReturnResult options source
let result =
{ OutputPath = None
Dependencies = []
Adjust = 0
Diagnostics = []
Output = None
Compilation = cUnit }
if errors.Count > 0 then
let output = ExecutionOutput {
ExitCode = -1
StdOut = String.Empty
StdErr = ((errors |> String.concat "\n").Replace("\r\n","\n")) }
CompilationResult.Failure { result with Output = Some output }
else
CompilationResult.Success result
finally
disposals
|> Seq.iter (fun x -> x.Dispose())
| _ -> failwith "FSI running only supports F#."
let private createBaselineErrors (baselineFile: BaselineFile) (actualErrors: string) : unit =
FileSystem.OpenFileForWriteShim(baselineFile.FilePath + ".err").Write(actualErrors)
let private verifyFSBaseline (fs) : unit =
match fs.Baseline with
| None -> failwith "Baseline was not provided."
| Some bsl ->
let errorsExpectedBaseLine =
match bsl.FSBaseline.Content with
| Some b -> b.Replace("\r\n","\n")
| None -> String.Empty
let typecheckDiagnostics = fs |> typecheckFSharpSourceAndReturnErrors
let errorsActual = (typecheckDiagnostics |> Array.map (sprintf "%A") |> String.concat "\n").Replace("\r\n","\n")
if errorsExpectedBaseLine <> errorsActual then
fs.CreateOutputDirectory()
createBaselineErrors bsl.FSBaseline errorsActual
elif FileSystem.FileExistsShim(bsl.FSBaseline.FilePath) then
FileSystem.FileDeleteShim(bsl.FSBaseline.FilePath)
Assert.AreEqual(errorsExpectedBaseLine, errorsActual, $"\nExpected:\n{errorsExpectedBaseLine}\nActual:\n{errorsActual}")
/// Check the typechecker output against the baseline, if invoked with empty baseline, will expect no error/warnings output.
let verifyBaseline (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS fs -> (verifyFSBaseline fs) |> ignore
| _ -> failwith "Baseline tests are only supported for F#."
cUnit
let private doILCheck func (il: string list) result =
match result with
| CompilationResult.Success s ->
match s.OutputPath with
| None -> failwith "Operation didn't produce any output!"
| Some p -> func p il
| CompilationResult.Failure _ -> failwith "Result should be \"Success\" in order to get IL."
let verifyIL = doILCheck ILChecker.checkIL
let verifyILNotPresent = doILCheck ILChecker.checkILNotPresent
let verifyILBinary (il: string list) (dll: string)= ILChecker.checkIL dll il
let private verifyFSILBaseline (baseline: Baseline option) (result: CompilationOutput) : unit =
match baseline with
| None -> failwith "Baseline was not provided."
| Some bsl ->
match result.OutputPath with
| None -> failwith "Operation didn't produce any output!"
| Some p ->
let expectedIL =
match bsl.ILBaseline.Content with
| Some b -> b
| None -> String.Empty
let (success, errorMsg, actualIL) = ILChecker.verifyILAndReturnActual p expectedIL
if not success then
// Failed try update baselines if required
// If we are here then the il file has been produced we can write it back to the baseline location
// if the environment variable TEST_UPDATE_BSL has been set
if snd (Int32.TryParse(Environment.GetEnvironmentVariable("TEST_UPDATE_BSL"))) <> 0 then
match baseline with
| Some baseline -> System.IO.File.Copy(baseline.ILBaseline.FilePath, baseline.ILBaseline.BslSource, true)
| None -> ()
createBaselineErrors bsl.ILBaseline actualIL
Assert.Fail(errorMsg)
let verifyILBaseline (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS fs ->
match fs |> compileFSharp with
| CompilationResult.Failure a -> failwith $"Build failure: {a}"
| CompilationResult.Success s -> verifyFSILBaseline fs.Baseline s
| _ -> failwith "Baseline tests are only supported for F#."
cUnit
let verifyBaselines = verifyBaseline >> verifyILBaseline
type ImportScope = { Kind: ImportDefinitionKind; Name: string }
type PdbVerificationOption =
| VerifyImportScopes of ImportScope list list
| VerifySequencePoints of (Line * Col * Line * Col) list
| VerifyDocuments of string list
| Dummy of unit
let private verifyPdbFormat (reader: MetadataReader) compilationType =
if reader.MetadataVersion <> "PDB v1.0" then
failwith $"Invalid PDB file version. Expected: \"PDB v1.0\"; Got {reader.MetadataVersion}"
if reader.MetadataKind <> MetadataKind.Ecma335 then
failwith $"Invalid metadata kind detected. Expected {MetadataKind.Ecma335}; Got {reader.MetadataKind}"
// This should not happen, just a sanity check:
if reader.IsAssembly then
failwith $"Unexpected PDB type, expected `IsAssembly` to be `false`."
let shouldHaveEntryPoint = (compilationType = CompileOutput.Exe)
// Sanity check, we want to verify, that Entrypoint is non-nil, if we are building "Exe" target.
if reader.DebugMetadataHeader.EntryPoint.IsNil && shouldHaveEntryPoint then
failwith $"EntryPoint expected to be {shouldHaveEntryPoint}, but was {reader.DebugMetadataHeader.EntryPoint.IsNil}"
let private verifyPdbImportTables (reader: MetadataReader) (scopes: ImportScope list list) =
// There always should be 2 import scopes - 1 empty "root" one, and one flattened table of imports for current scope.
if reader.ImportScopes.Count < 2 then
failwith $"Expected to have at least 2 import scopes, but found {reader.ImportScopes.Count}."
// Sanity check: explicitly test that first import scope is indeed an apty one (i.e. there are no imports).
let rootScope = reader.ImportScopes.ToImmutableArray().Item(0) |> reader.GetImportScope
let rootScopeImportsLength = rootScope.GetImports().ToImmutableArray().Length
if rootScopeImportsLength <> 0 then
failwith $"Expected root scope to have 0 imports, but got {rootScopeImportsLength}."
let pdbScopes = [ for import in reader.ImportScopes -> reader.GetImportScope import ] |> List.skip 1 |> List.rev
if pdbScopes.Length <> scopes.Length then
failwith $"Expected import scopes amount is {scopes.Length}, but got {pdbScopes.Length}."
for (pdbScope, expectedScope) in List.zip pdbScopes scopes do
let imports = [ for import in pdbScope.GetImports() ->
match import.Kind with
| ImportDefinitionKind.ImportNamespace ->
let targetNamespaceBlob = import.TargetNamespace
let targetNamespaceBytes = reader.GetBlobBytes(targetNamespaceBlob)
let name = Encoding.UTF8.GetString(targetNamespaceBytes, 0, targetNamespaceBytes.Length)
Some { Kind = import.Kind; Name = name }