1 package org.simantics.scl.compiler.internal.codegen.utils;
3 import org.simantics.scl.compiler.types.TCon;
4 import org.simantics.scl.compiler.types.TPred;
7 public class JavaNamingPolicy {
9 private final String moduleName;
10 private final String moduleClassName;
13 public JavaNamingPolicy(String moduleName) {
14 this.moduleName = moduleName;
15 this.moduleClassName = "scl/" + moduleNameToClassName(moduleName);
18 private static boolean isAllowableClassNameChar(char c) {
19 return (c >= 'a' && c <= 'z')
20 || (c >= 'A' && c <= 'Z')
21 || (c >= '0' && c <= '9')
26 public static String moduleNameToClassName(String moduleName) {
27 StringBuilder b = new StringBuilder(moduleName.length());
28 int length = moduleName.length();
29 for(int i=0;i<length;++i) {
30 char c = moduleName.charAt(i);
31 if(c == '/' || isAllowableClassNameChar(c))
34 b.append('_').append((int)c).append('_');
36 //System.out.println("m2c " + moduleName + " -> " + b.toString());
37 return b.toString().replace("..", "._.");
40 public static String classNameToModuleName(String className) {
41 className = className.replace("._.", "..");
42 StringBuilder b = new StringBuilder(className.length());
43 int length = className.length();
44 for(int i=0;i<length;++i) {
45 char c = className.charAt(i);
49 StringBuilder nb = new StringBuilder(4);
51 c = className.charAt(++i);
52 if(Character.isDigit(c))
57 b.append((char)Integer.parseInt(nb.toString()));
62 //System.out.println("c2m " + className + " -> " + b.toString());
66 public String getModuleClassName() {
67 return moduleClassName;
70 public String getMethodName(String name) {
71 return NameMangling.mangle(name);
74 public String getFreshClosureClassName() {
75 return moduleClassName + "$" + (++closureCount);
78 public String getDataTypeClassName(String name) {
79 return moduleClassName + "$" + name;
82 public String getConstructorClassName(String name) {
83 return moduleClassName + "$" + name + "Constructor";
86 public String getTypeClassInterfaceName(TCon name) {
87 return moduleClassName + "$" + name.name + "Class";
90 public String getTypeClassInstanceClassName(String name) {
91 return moduleClassName + "$" + name + "Instance";
94 public String getInstanceClassName(TPred instance) {
95 return moduleClassName + "$" + instance.toName();
98 public String getModuleName() {