-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFunctionsGenerator.java
More file actions
560 lines (485 loc) · 21.9 KB
/
FunctionsGenerator.java
File metadata and controls
560 lines (485 loc) · 21.9 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
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
package builder;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import utils.BuilderUtils;
import utils.Pair;
/**
* Class used to generate the functions from the MEOS library.
* Run with directly with java through the main class
*
* @author Killian Monnier and Nidhal Mareghni
* @since 27/06/2023
*/
public class FunctionsGenerator {
/**
* Path of the file generated by {@link FunctionsExtractor}. Contains a list of functions signature.
*/
private final Path C_functionsPath;
/**
* Path of the file generated by {@link FunctionsExtractor}. Contains a list of types definition.
*/
@SuppressWarnings("FieldCanBeLocal")
private final Path C_typesPath;
/**
* Path of the file generated by this class.
*/
private final Path functionsFilePath;
/**
* Types dictionary. Contains the C types and there equivalent in Java
*/
private final HashMap<String, String> equivalentTypes = buildEquivalentTypes();
private final HashMap<String, String> conversionTypes = buildConversionTypes();
private final HashMap<String, String> conversionTypedefs;
private final ArrayList<String> unsupportedEquivalentTypes = new ArrayList<>(); // List of unsupported types
private final ArrayList<String> unsupportedConversionTypedefs = new ArrayList<>(); // List of unsupported types
private final ArrayList<String> unsupportedConversionTypes = new ArrayList<>(); // List of unsupported types
/**
* Constructor of {@link FunctionsExtractor}.
*
* @throws URISyntaxException thrown when resources not found
*/
public FunctionsGenerator() throws URISyntaxException {
this.C_functionsPath = Paths.get(Objects.requireNonNull(this.getClass().getResource("meos_functions.h")).toURI());
this.C_typesPath = Paths.get(Objects.requireNonNull(this.getClass().getResource("meos_types.h")).toURI());
this.functionsFilePath = Paths.get(new URI(Objects.requireNonNull(this.getClass().getResource("")) + "functions.java"));
this.conversionTypedefs = this.buildConversionTypedefs(this.C_typesPath.toString());
}
/**
* Gives the types of the function's parameters and return from a line corresponding to the format of a function.
*
* @param signature function signature
* @return types list
*/
private static ArrayList<String> getFunctionTypes(String signature) {
ArrayList<String> functionTypes = BuilderUtils.extractFunctionTypes(signature);
/* Remove Array Types from the functionTypes list then change it to normal type */
List<String> arrayTypesList = functionTypes.stream().filter(type -> type.contains("[]")).toList();
functionTypes.removeAll(arrayTypesList);
List<String> newTypesList = arrayTypesList.stream().map(arrayType -> arrayType.replace("[]", "")).toList();
functionTypes.addAll(newTypesList);
return functionTypes;
}
/**
* Launch process of extraction.
*
* @param args arguments
* @throws URISyntaxException thrown when resources not found
*/
public static void main(String[] args) throws URISyntaxException {
var generator = new FunctionsGenerator();
/* Generation of all the functions signature */
StringBuilder functionsInterfaceBuilder = generator.generateFunctions(generator.C_functionsPath.toString(), false);
StringBuilder functionsClassBuilder = generator.generateFunctions(generator.C_functionsPath.toString(), true);
System.out.println("Unsupported types: " + generator.unsupportedEquivalentTypes);
System.out.println("Unsupported conversion typedefs: " + generator.unsupportedConversionTypedefs);
System.out.println("Unsupported conversion types: " + generator.unsupportedConversionTypes);
/* Generation of the file */
StringBuilder interfaceBuilder = generator.generateInterface(functionsInterfaceBuilder);
StringBuilder classBuilder = generator.generateClass(functionsClassBuilder, interfaceBuilder);
BuilderUtils.writeFileFromBuilder(classBuilder, generator.functionsFilePath.toString());
String tmp_functionsFilePath = Paths.get("").toAbsolutePath() + "/src/main/java/functions/functions.java";
BuilderUtils.writeFileFromBuilder(classBuilder, tmp_functionsFilePath);
}
/**
* Processes the rows to generate the functions.
*
* @param line the line corresponding to a function
* @return the processed line
*/
private static String performTypeConversion(String line) {
if (!line.isBlank()) {
/* Remove keywords that are not of interest to us */
line = line.replaceAll("extern ", "");
line = line.replaceAll("const ", "");
line = line.replaceAll("static inline ", "");
/* Changing types with * */
line = line.replaceAll("char\\s?\\*\\*", "**char ");
line = line.replaceAll("char\\s?\\*", "*char ");
// line = line.replaceAll("(\\w+\\s?\\*\\*\\*)(?!\\*)", "*[][] ");
// line = line.replaceAll("(\\w+\\s?\\*\\*)(?!\\*)", "*[] ");
line = line.replaceAll("\\w+\\s?(\\*+)", "* ");
// line = line.replaceAll("(\\w+\\s?\\*)(?!\\*)", "* ");
line = line.replaceAll("\\s\\.\\.\\.", " * args");
/* Changing special types or names */
line = line.replaceAll("\\(void\\)", "()"); // Remove the void parameter (for the function meos_finish(void)) //
line = line.replaceAll("synchronized", "synchronize"); // Change the keyword used by Java (for the function temporal_simplify(const Temporal *temp, double eps_dist, bool synchronized))
}
return line;
}
/**
* Builds the type modification array.
* <pre>
* Key: old type in C or modified
* Value: new type in Java
* </pre>
*
* @return type dictionary
*/
private static HashMap<String, String> buildEquivalentTypes() {
HashMap<String, String> types = new HashMap<>();
types.put("\\*", "Pointer");
// types.put("\\*\\[\\]", "Pointer[]");
// types.put("\\*\\[\\]\\[\\]", "Pointer[][]");
// types.put("\\*\\[]", "Pointer[]");
// types.put("\\*\\*\\[]", "Pointer[][]");
types.put("\\*char", "String");
types.put("\\*\\*char", "Pointer");
types.put("Pointer\\[\\]", "Pointer"); // Keep this line, otherwise operand error in JNR-FFI
types.put("bool", "boolean");
types.put("float", "float");
types.put("float8", "double");
types.put("double", "double");
types.put("void", "void");
types.put("int", "int");
types.put("short", "short");
types.put("long", "long");
types.put("int8", "byte");
types.put("int16", "short");
types.put("int32", "int");
types.put("int64", "long");
types.put("int8_t", "byte");
types.put("int16_t", "short");
types.put("int32_t", "int");
types.put("int64_t", "long");
types.put("uint8", "byte");
types.put("uint16", "short");
types.put("uint32", "int");
types.put("uint64", "long");
types.put("uint8_t", "byte");
types.put("uint16_t", "short");
types.put("uint32_t", "int");
types.put("uint64_t", "long");
types.put("uintptr_t", "long");
types.put("size_t", "long");
types.put("interpType", "int"); // enum in C
//types.put("\\char **","Pointer");
// Nouveaux types pour 1.3
types.put("DateADT", "int");
types.put("TimeADT", "long");
types.put("Timestamp", "long");
types.put("TimestampTz", "long");
types.put("TimeOffset", "long");
types.put("fsec_t", "int");
types.put("Datum", "long");
types.put("uintptr_t", "long");
types.put("OffsetDateTime", "OffsetDateTime");
types.put("LocalDateTime", "LocalDateTime");
return types;
}
/**
* Build the dictionary of conversion types.
* <pre>
* Key: old type in C or modified
* Value: new type in Java
* </pre>
*
* @return types dictionary
*/
private static HashMap<String, String> buildConversionTypes() {
HashMap<String, String> conversionTypes = new HashMap<>();
conversionTypes.put("Timestamp", "LocalDateTime");
conversionTypes.put("TimestampTz", "OffsetDateTime");
return conversionTypes;
}
/**
* Check unsupported types.
*
* @param signature function signature
*/
private static List<String> getUnsupportedTypes(String signature, Map<String, String> types) {
/* Retrieving unsupported types for the line */
return getFunctionTypes(signature).stream().filter(type -> !types.containsValue(type)).toList();
}
/**
* Build the dictionary of typedef conversion.
* <pre>
* Key: old type in C or modified
* Value: new type in Java
* </pre>
*
* @param filePath file path of C typedefs
* @return types dictionary
*/
private HashMap<String, String> buildConversionTypedefs(String filePath) {
HashMap<String, String> typedefs = new HashMap<>();
/* Added typedefs extracted from C file */
BuilderUtils.readFileLines(filePath, line -> {
Pattern pattern = Pattern.compile("^typedef\\s(\\w+)\\s(\\w+);");
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
String rawType = matcher.group(1);
String typeDef = matcher.group(2);
typedefs.put(typeDef, rawType);
} else {
System.err.println("Cannot extract type for row: " + line);
}
});
return typedefs;
}
/**
* Produce the process of adding code to handle conversion of certain types for a function.
*
* @param signature function signature
* @return list of {@link Pair} defined as follows :
* <ul>
* <li>Key : {@link Pair} defined as follows :</li>
* <ul>
* <li>Key : old type name</li>
* <li>Value : new type name</li>
* </ul>
* <li>Value : list of lines for the conversion process</li>
* </ul>
*/
private List<Pair<Pair<String, String>, List<String>>> generateConversionProcess(String signature) {
List<Pair<Pair<String, String>, List<String>>> conversionList = new ArrayList<>();
List<Pair<String, String>> typesList = BuilderUtils.extractParamTypesAndNames(signature);
/* For each types */
for (Pair<String, String> type : typesList) {
String typeValue = type.key();
/* If this type needs a conversion */
if (conversionTypes.containsValue(typeValue)) {
List<String> conversionLines = new ArrayList<>();
String typeName = type.value();
String newTypeName = typeName + "_new";
switch (typeValue) {
case "LocalDateTime" -> conversionLines.add("var " + newTypeName + " = " + typeName + ".toEpochSecond(ZoneOffset.UTC);");
case "OffsetDateTime" -> conversionLines.add("var " + newTypeName + " = " + typeName + ".toEpochSecond();");
default -> throw new TypeNotPresentException(typeValue, new Throwable("Type not supported by the builder conversion process"));
}
conversionList.add(new Pair<>(new Pair<>(typeName, newTypeName), conversionLines));
}
}
return conversionList;
}
/**
* utility function to count the number if occurences of pointer, pointer[] and pointer[][]
*/
public static int countOccurrences(String text, String pattern) {
String[] parts = text.split(Pattern.quote(pattern), -1);
return parts.length - 1;
}
/**
* Produce the returning process for a function
*
* @param signature function signature
* @param typesNamesList list of types names to change it in the calling of the equivalent interface function
* @return the returning process
*/
private List<String> generateReturnProcess(String signature, List<Pair<String, String>> typesNamesList) {
List<String> functionCallingProcess = new ArrayList<>();
List<String> paramNames = BuilderUtils.extractParamNames(signature);
/* Manage the calling of meos library associate function */
if (!typesNamesList.isEmpty()) {
/* Modify the names of the parameter types that has endured conversion */
for (var typeNames : typesNamesList) {
String oldName = typeNames.key();
String newName = typeNames.value();
paramNames = BuilderUtils.modifyList(paramNames, oldName, newName);
}
}
if (! (paramNames.contains("result") || paramNames.contains("size_out"))){
functionCallingProcess.add("MeosLibrary.meos." + BuilderUtils.extractFunctionName(signature) + "(" + BuilderUtils.getListWithoutBrackets(paramNames) + ");");
}
/* Manage the return process : if there is something to return */
if (!getFunctionTypes(signature).getFirst().equals("void")) {
var classReturnType = BuilderUtils.extractFunctionTypes(signature).getFirst();
/* Manage the returning process of conversion types */
if (conversionTypes.containsValue(classReturnType)) {
List<String> returnProcess = new ArrayList<>();
var functionCall = BuilderUtils.removeSemicolon(functionCallingProcess.getFirst());
returnProcess.add("var result = " + functionCall + ";");
switch (classReturnType) {
case "LocalDateTime" -> returnProcess.add(
"LocalDateTime.ofEpochSecond(result, 0, ZoneOffset.UTC);"
);
case "OffsetDateTime" -> returnProcess.addAll(List.of(
"Instant instant = Instant.ofEpochSecond(result);",
"OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);"
));
default -> throw new TypeNotPresentException(classReturnType, new Throwable("Type not supported by the builder returning conversion process"));
}
returnProcess.set(returnProcess.size() - 1, "return " + returnProcess.get(returnProcess.size() - 1)); // Add return at the last line of the list
functionCallingProcess = returnProcess;
} else {
if (paramNames.contains("result")){
functionCallingProcess.add("boolean out;");
functionCallingProcess.add("Runtime runtime = Runtime.getSystemRuntime();");
// int pointer_total = signature.split(Pattern.quote("Pointer"), -1).length -1;
// int pointer_array_total = signature.split(Pattern.quote("Pointer[]"), -1).length -1;
// Count occurrences of "Pointer"
int pointerCount = countOccurrences(signature, "Pointer");
// Count occurrences of "Pointer[]"
int pointerArrayCount = countOccurrences(signature, "Pointer\\[\\]");
// Count occurrences of "Pointer[][]"
int pointerArrayArrayCount = countOccurrences(signature, "Pointer\\[\\]\\[\\]");
if (pointerCount > 1){
functionCallingProcess.add("Pointer result = Memory.allocateDirect(runtime, Long.BYTES);");
functionCallingProcess.add("out = MeosLibrary.meos." + BuilderUtils.extractFunctionName(signature) + "(" + BuilderUtils.getListWithoutBrackets(paramNames) + ");");
functionCallingProcess.add("Pointer new_result = result.getPointer(0);");
functionCallingProcess.add("return out ? new_result : null ;");
}
else if (signature.contains("int ")){
functionCallingProcess.add("Pointer result = Memory.allocateDirect(runtime, Integer.BYTES);");
functionCallingProcess.add("out = MeosLibrary.meos." + BuilderUtils.extractFunctionName(signature) + "(" + BuilderUtils.getListWithoutBrackets(paramNames) + ");");
functionCallingProcess.add("return out ? result.getInt(0) : null ;");
}
else if (signature.contains("double ")){
functionCallingProcess.add("Pointer result = Memory.allocateDirect(runtime, Double.BYTES);");
functionCallingProcess.add("out = MeosLibrary.meos." + BuilderUtils.extractFunctionName(signature) + "(" + BuilderUtils.getListWithoutBrackets(paramNames) + ");");
functionCallingProcess.add("return out ? result.getDouble(0) : null ;");
}
else if (signature.contains("long ")){
functionCallingProcess.add("Pointer result = Memory.allocateDirect(runtime, Long.BYTES);");
functionCallingProcess.add("out = MeosLibrary.meos." + BuilderUtils.extractFunctionName(signature) + "(" + BuilderUtils.getListWithoutBrackets(paramNames) + ");");
functionCallingProcess.add("return out ? result.getLong(0) : null ;");
}
else if (signature.contains("boolean ")){
functionCallingProcess.add("Pointer result = Memory.allocateDirect(runtime, Long.BYTES);");
functionCallingProcess.add("out = MeosLibrary.meos." + BuilderUtils.extractFunctionName(signature) + "(" + BuilderUtils.getListWithoutBrackets(paramNames) + ");");
functionCallingProcess.add("return out ? true : false ;");
}
}
else if (paramNames.contains("size_out")){
functionCallingProcess.add("Runtime runtime = Runtime.getSystemRuntime();");
functionCallingProcess.add("Pointer size_out = Memory.allocateDirect(runtime, Long.BYTES);");
functionCallingProcess.add("return MeosLibrary.meos." + BuilderUtils.extractFunctionName(signature) + "(" + BuilderUtils.getListWithoutBrackets(paramNames) + ");");
}
else{
functionCallingProcess.set(0, "return " + functionCallingProcess.getFirst());
}
}
}
return functionCallingProcess;
}
/**
* Used to generate the class of functions.
*
* @param functionsBuilder builder of functions
* @param interfaceBuilder interface builder
* @return the class builder
*/
private StringBuilder generateClass(StringBuilder functionsBuilder, StringBuilder interfaceBuilder) {
var builder = new StringBuilder();
String imports_and_package = """
package functions;
import jnr.ffi.Pointer;
import jnr.ffi.Memory;
import jnr.ffi.Runtime;
import jnr.ffi.byref.PointerByReference;
import jnr.ffi.Struct;
import utils.JarLibraryLoader;
import utils.meosCatalog.MeosEnums.meosType;
import utils.meosCatalog.MeosEnums.meosOper;
import java.time.*;
""";
String className = "public class functions {";
builder.append(imports_and_package).append("\n").append(className).append("\n");
/* Added interface */
BuilderUtils.appendStringBuilders(interfaceBuilder, builder, "\t", "\n\n");
/* Addition of functions */
StringBuilder functionBodyBuilder = new StringBuilder();
BuilderUtils.readBuilderLines(functionsBuilder, line -> {
if (!line.isBlank()) {
String functionSignature = "public static " + BuilderUtils.removeSemicolon(line) + " {\n";
/* Generate the conversion process */
var conversionProcess = this.generateConversionProcess(line);
var conversionProcessList = BuilderUtils.extractPairValues(conversionProcess);
var conversionProcessContent = conversionProcessList.stream().flatMap(Collection::stream).toList();
/* Generate the returning process */
var typesNamesList = BuilderUtils.extractPairKeys(conversionProcess);
var returnProcessContent = this.generateReturnProcess(line, typesNamesList);
String functionSignature2 = "public static " + BuilderUtils.removeSemicolon(line) + " {\n";
if (functionSignature2.contains("result")){
int total = functionSignature2.split(Pattern.quote("Pointer"), -1).length -1;
if (total > 1){
functionSignature2 = functionSignature2.replace("public static boolean","public static Pointer");
}
else if (functionSignature2.contains("int ")){
functionSignature2 = functionSignature2.replace("public static boolean","public static int");
}
else if (functionSignature2.contains("double ")){
functionSignature2 = functionSignature2.replace("public static boolean","public static double");
}
else if (functionSignature2.contains("long ")){
functionSignature2 = functionSignature2.replace("public static boolean","public static long");
}
functionSignature2 = functionSignature2.replace(", Pointer result","");
}
else if (functionSignature2.contains("as_hexwkb") || functionSignature2.contains("as_wkb")){
functionSignature2 = functionSignature2.replace(", Pointer size_out","");
}
/* Add all the different parts in the function body builder */
functionBodyBuilder.append("@SuppressWarnings(\"unused\")\n")
.append(functionSignature2)
.append(BuilderUtils.formattingLineList(conversionProcessContent, "\t", "\n"))
.append(BuilderUtils.formattingLineList(returnProcessContent, "\t", "\n"))
.append(BuilderUtils.formattingLine("}", "", "\n\n"));
}
});
BuilderUtils.appendStringBuilders(functionBodyBuilder, builder, "\t", "\n");
builder.append("}");
return builder;
}
/**
* Generation of the interface.
*
* @param functionsBuilder builder of functions
* @return the interface builder
*/
private StringBuilder generateInterface(StringBuilder functionsBuilder) {
var builder = new StringBuilder();
builder.append("""
public interface MeosLibrary {
String libraryPath = "libmeos.so";
MeosLibrary INSTANCE = JarLibraryLoader.create(MeosLibrary.class, libraryPath).getLibraryInstance();
MeosLibrary meos = MeosLibrary.INSTANCE;
""");
BuilderUtils.appendStringBuilders(functionsBuilder, builder, "\t", "\n");
builder.append("}");
return builder;
}
/**
* Generation of functions with their conversion types, typedef conversion types and equivalent types.
*
* @param filePath file path of C functions
* @param performTypesConversion true if it needs to perform a types conversion using {@link FunctionsGenerator#conversionTypes}
* @return the function builder
*/
private StringBuilder generateFunctions(String filePath, boolean performTypesConversion) {
var builder = new StringBuilder();
Set<String> seen = new HashSet<>();
BuilderUtils.readFileLines(filePath, line -> {
line = performTypeConversion(line);
/* Perform types conversion */
if (performTypesConversion) {
line = BuilderUtils.replaceTypes(conversionTypes, line);
unsupportedConversionTypes.addAll(getUnsupportedTypes(line, conversionTypes).stream().filter(type -> !unsupportedConversionTypes.contains(type)).toList());
}
/* Perform typedef conversion */
line = BuilderUtils.replaceTypes(conversionTypedefs, line);
unsupportedConversionTypedefs.addAll(getUnsupportedTypes(line, conversionTypedefs).stream().filter(type -> !unsupportedConversionTypedefs.contains(type)).toList());
/* Perform equivalent type conversion */
line = BuilderUtils.replaceTypes(equivalentTypes, line);
unsupportedEquivalentTypes.addAll(getUnsupportedTypes(line, equivalentTypes).stream().filter(type -> !unsupportedEquivalentTypes.contains(type)).toList());
String fnName = BuilderUtils.extractFunctionName(line);
int arity = BuilderUtils.extractParamTypesAndNames(line).size();
String key = fnName + "#" + arity;
if (seen.add(key)){
builder.append(line).append("\n");
}
});
return builder;
}
}