-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotPythonModuleBuilder.cs
More file actions
194 lines (177 loc) · 6.31 KB
/
Copy pathDotPythonModuleBuilder.cs
File metadata and controls
194 lines (177 loc) · 6.31 KB
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
using DotPython.Compiler;
using DotPython.Compiler.Artifacts;
using DotPython.Contracts;
using DotPython.Interop.Contracts;
using DotPython.Language.Ast;
using DotPython.Language.Diagnostics;
using DotPython.Language.Text;
using DotPython.ParserGenerator;
namespace DotPython.Build.Tasks;
internal static class DotPythonModuleBuilder
{
internal static bool Build(DotPythonModuleBuildOptions options)
{
ArgumentNullException.ThrowIfNull(options);
var source = ReadSource(options.SourcePath);
var stub = ReadSource(options.ContractPath);
var contractResult = PythonStubContractParser.Parse(
stub,
new PythonStubContractOptions
{
ModuleName = options.ModuleName,
ClrNamespace = options.ClrNamespace,
ClrTypeName = options.ClrTypeName,
StatePolicy = options.StatePolicy,
}
);
if (!ReportDiagnostics(contractResult.Source, contractResult.Diagnostics))
{
return false;
}
var contract = contractResult.Contract!;
var parseResult = PythonParser.Parse(source);
if (!ReportDiagnostics(parseResult.Source, parseResult.Diagnostics))
{
return false;
}
if (!ValidateSourceContract(parseResult.Module, contract, source))
{
return false;
}
var compilation = PythonCompiler.Compile(parseResult.Module, options.ModuleName);
if (!ReportDiagnostics(source, compilation.Diagnostics))
{
return false;
}
var exports = contract.Functions.Select(function => new DotPythonModuleExport(
function.PythonName,
function.ClrName,
DotPythonExportKind.Function
));
var artifact = DotPythonModuleArtifact.Create(
options.ModuleName,
compilation.Code,
exports,
options.LanguageVersion
);
DeterministicFileWriter.Write(
options.ArtifactOutputPath,
DotPythonModuleArtifactSerializer.Serialize(artifact)
);
DeterministicFileWriter.Write(
options.ContractOutputPath,
System.Text.Encoding.UTF8.GetBytes(PythonModuleContractJson.Serialize(contract))
);
DeterministicFileWriter.Write(
options.FacadeOutputPath,
System.Text.Encoding.UTF8.GetBytes(
CSharpModuleFacadeGenerator.Generate(contract, options.ArtifactResourceName)
)
);
return true;
}
private static SourceText ReadSource(string path)
{
if (!File.Exists(path))
{
throw new FileNotFoundException("A DotPython build input does not exist.", path);
}
return new SourceText(File.ReadAllText(path), Path.GetFullPath(path));
}
private static bool ValidateSourceContract(
PythonModule module,
PythonModuleContract contract,
SourceText source
)
{
var definitions = module
.Statements.OfType<PythonFunctionDefinitionStatement>()
.ToDictionary(definition => definition.Name.Name, StringComparer.Ordinal);
var valid = true;
foreach (var function in contract.Functions)
{
if (!definitions.TryGetValue(function.PythonName, out var definition))
{
Report(
source,
"DPY7004",
$"Contract export '{function.PythonName}' has no top-level source function.",
module.Span
);
valid = false;
continue;
}
if (definition.Parameters.Count != function.Parameters.Count)
{
Report(
source,
"DPY7004",
$"Contract export '{function.PythonName}' declares a different parameter count than its source function.",
definition.Span
);
valid = false;
}
if (
function.CallShape != PythonCallShape.Synchronous
|| function.Parameters.Any(parameter => parameter.HasDefault)
|| function.Parameters.Any(parameter =>
!IsSupportedRuntimeType(parameter.Type, allowVoid: false)
)
|| !IsSupportedRuntimeType(function.ReturnType, allowVoid: true)
)
{
Report(
source,
"DPY7004",
$"Contract export '{function.PythonName}' uses a call or value shape not yet supported by the managed module runtime.",
definition.Span
);
valid = false;
}
}
return valid;
}
private static bool IsSupportedRuntimeType(PythonTypeContract type, bool allowVoid)
{
if (type.TypeArguments.Count != 0)
{
return false;
}
return type.ClrTypeName
is "System.Boolean"
or "System.Numerics.BigInteger"
or "System.Double"
or "System.String"
or "System.Byte[]"
|| allowVoid
&& string.Equals(type.ClrTypeName, "System.Void", StringComparison.Ordinal);
}
private static bool ReportDiagnostics(SourceText source, IReadOnlyList<Diagnostic> diagnostics)
{
foreach (var diagnostic in diagnostics)
{
Report(
source,
diagnostic.Code,
diagnostic.Message,
diagnostic.Span,
diagnostic.Severity
);
}
return diagnostics.All(diagnostic => diagnostic.Severity != DiagnosticSeverity.Error);
}
private static void Report(
SourceText source,
string code,
string message,
TextSpan span,
DiagnosticSeverity severity = DiagnosticSeverity.Error
)
{
var position = source.GetLinePosition(Math.Min(span.Start, source.Length));
var kind = severity == DiagnosticSeverity.Error ? "error" : "warning";
Console.Error.WriteLine(
$"{source.FilePath}({position.Line + 1},{position.Character + 1}): {kind} {code}: {message}"
);
}
}